Constify X509_PUBKEY_get(), X509_PUBKEY_get0(), and X509_PUBKEY_get0_param()
[openssl.git] / crypto / asn1 / ameth_lib.c
1 /*
2  * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include "e_os.h"               /* for strncasecmp */
11 #include "internal/cryptlib.h"
12 #include <stdio.h>
13 #include <openssl/asn1t.h>
14 #include <openssl/x509.h>
15 #include <openssl/engine.h>
16 #include "crypto/asn1.h"
17 #include "crypto/evp.h"
18
19 #include "standard_methods.h"
20
21 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
22 static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
23
24 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
25                            const EVP_PKEY_ASN1_METHOD *, ameth);
26
27 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
28                      const EVP_PKEY_ASN1_METHOD *const *b)
29 {
30     return ((*a)->pkey_id - (*b)->pkey_id);
31 }
32
33 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
34                              const EVP_PKEY_ASN1_METHOD *, ameth);
35
36 int EVP_PKEY_asn1_get_count(void)
37 {
38     int num = OSSL_NELEM(standard_methods);
39     if (app_methods)
40         num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
41     return num;
42 }
43
44 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
45 {
46     int num = OSSL_NELEM(standard_methods);
47     if (idx < 0)
48         return NULL;
49     if (idx < num)
50         return standard_methods[idx];
51     idx -= num;
52     return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
53 }
54
55 static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
56 {
57     EVP_PKEY_ASN1_METHOD tmp;
58     const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
59
60     tmp.pkey_id = type;
61     if (app_methods) {
62         int idx;
63         idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
64         if (idx >= 0)
65             return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
66     }
67     ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
68     if (ret == NULL || *ret == NULL)
69         return NULL;
70     return *ret;
71 }
72
73 /*
74  * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
75  * search through engines and set *pe to a functional reference to the engine
76  * implementing 'type' or NULL if no engine implements it.
77  */
78
79 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
80 {
81     const EVP_PKEY_ASN1_METHOD *t;
82
83     for (;;) {
84         t = pkey_asn1_find(type);
85         if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
86             break;
87         type = t->pkey_base_id;
88     }
89     if (pe) {
90 #ifndef OPENSSL_NO_ENGINE
91         ENGINE *e;
92         /* type will contain the final unaliased type */
93         e = ENGINE_get_pkey_asn1_meth_engine(type);
94         if (e) {
95             *pe = e;
96             return ENGINE_get_pkey_asn1_meth(e, type);
97         }
98 #endif
99         *pe = NULL;
100     }
101     return t;
102 }
103
104 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
105                                                    const char *str, int len)
106 {
107     int i;
108     const EVP_PKEY_ASN1_METHOD *ameth = NULL;
109
110     if (len == -1)
111         len = strlen(str);
112     if (pe) {
113 #ifndef OPENSSL_NO_ENGINE
114         ENGINE *e;
115         ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
116         if (ameth) {
117             /*
118              * Convert structural into functional reference
119              */
120             if (!ENGINE_init(e))
121                 ameth = NULL;
122             ENGINE_free(e);
123             *pe = e;
124             return ameth;
125         }
126 #endif
127         *pe = NULL;
128     }
129     for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
130         ameth = EVP_PKEY_asn1_get0(i);
131         if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
132             continue;
133         if ((int)strlen(ameth->pem_str) == len
134             && strncasecmp(ameth->pem_str, str, len) == 0)
135             return ameth;
136     }
137     return NULL;
138 }
139
140 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
141 {
142     EVP_PKEY_ASN1_METHOD tmp = { 0, };
143
144     /*
145      * One of the following must be true:
146      *
147      * pem_str == NULL AND ASN1_PKEY_ALIAS is set
148      * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
149      *
150      * Anything else is an error and may lead to a corrupt ASN1 method table
151      */
152     if (!((ameth->pem_str == NULL
153            && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
154           || (ameth->pem_str != NULL
155               && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
156         EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0, ERR_R_PASSED_INVALID_ARGUMENT);
157         return 0;
158     }
159
160     if (app_methods == NULL) {
161         app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
162         if (app_methods == NULL)
163             return 0;
164     }
165
166     tmp.pkey_id = ameth->pkey_id;
167     if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
168         EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0,
169                EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
170         return 0;
171     }
172
173     if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
174         return 0;
175     sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
176     return 1;
177 }
178
179 int EVP_PKEY_asn1_add_alias(int to, int from)
180 {
181     EVP_PKEY_ASN1_METHOD *ameth;
182     ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
183     if (ameth == NULL)
184         return 0;
185     ameth->pkey_base_id = to;
186     if (!EVP_PKEY_asn1_add0(ameth)) {
187         EVP_PKEY_asn1_free(ameth);
188         return 0;
189     }
190     return 1;
191 }
192
193 int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
194                             int *ppkey_flags, const char **pinfo,
195                             const char **ppem_str,
196                             const EVP_PKEY_ASN1_METHOD *ameth)
197 {
198     if (!ameth)
199         return 0;
200     if (ppkey_id)
201         *ppkey_id = ameth->pkey_id;
202     if (ppkey_base_id)
203         *ppkey_base_id = ameth->pkey_base_id;
204     if (ppkey_flags)
205         *ppkey_flags = ameth->pkey_flags;
206     if (pinfo)
207         *pinfo = ameth->info;
208     if (ppem_str)
209         *ppem_str = ameth->pem_str;
210     return 1;
211 }
212
213 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
214 {
215     return pkey->ameth;
216 }
217
218 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
219                                         const char *pem_str, const char *info)
220 {
221     EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
222
223     if (ameth == NULL)
224         return NULL;
225
226     ameth->pkey_id = id;
227     ameth->pkey_base_id = id;
228     ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
229
230     if (info) {
231         ameth->info = OPENSSL_strdup(info);
232         if (!ameth->info)
233             goto err;
234     }
235
236     if (pem_str) {
237         ameth->pem_str = OPENSSL_strdup(pem_str);
238         if (!ameth->pem_str)
239             goto err;
240     }
241
242     return ameth;
243
244  err:
245     EVP_PKEY_asn1_free(ameth);
246     return NULL;
247
248 }
249
250 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
251                         const EVP_PKEY_ASN1_METHOD *src)
252 {
253     int pkey_id = dst->pkey_id;
254     int pkey_base_id = dst->pkey_base_id;
255     unsigned long pkey_flags = dst->pkey_flags;
256     char *pem_str = dst->pem_str;
257     char *info = dst->info;
258
259     *dst = *src;
260
261     /* We only copy the function pointers so restore the other values */
262     dst->pkey_id = pkey_id;
263     dst->pkey_base_id = pkey_base_id;
264     dst->pkey_flags = pkey_flags;
265     dst->pem_str = pem_str;
266     dst->info = info;
267 }
268
269 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
270 {
271     if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
272         OPENSSL_free(ameth->pem_str);
273         OPENSSL_free(ameth->info);
274         OPENSSL_free(ameth);
275     }
276 }
277
278 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
279                               int (*pub_decode) (EVP_PKEY *pk,
280                                                  const X509_PUBKEY *pub),
281                               int (*pub_encode) (X509_PUBKEY *pub,
282                                                  const EVP_PKEY *pk),
283                               int (*pub_cmp) (const EVP_PKEY *a,
284                                               const EVP_PKEY *b),
285                               int (*pub_print) (BIO *out,
286                                                 const EVP_PKEY *pkey,
287                                                 int indent, ASN1_PCTX *pctx),
288                               int (*pkey_size) (const EVP_PKEY *pk),
289                               int (*pkey_bits) (const EVP_PKEY *pk))
290 {
291     ameth->pub_decode = pub_decode;
292     ameth->pub_encode = pub_encode;
293     ameth->pub_cmp = pub_cmp;
294     ameth->pub_print = pub_print;
295     ameth->pkey_size = pkey_size;
296     ameth->pkey_bits = pkey_bits;
297 }
298
299 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
300                                int (*priv_decode) (EVP_PKEY *pk,
301                                                    const PKCS8_PRIV_KEY_INFO
302                                                    *p8inf),
303                                int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
304                                                    const EVP_PKEY *pk),
305                                int (*priv_print) (BIO *out,
306                                                   const EVP_PKEY *pkey,
307                                                   int indent,
308                                                   ASN1_PCTX *pctx))
309 {
310     ameth->priv_decode = priv_decode;
311     ameth->priv_encode = priv_encode;
312     ameth->priv_print = priv_print;
313 }
314
315 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
316                              int (*param_decode) (EVP_PKEY *pkey,
317                                                   const unsigned char **pder,
318                                                   int derlen),
319                              int (*param_encode) (const EVP_PKEY *pkey,
320                                                   unsigned char **pder),
321                              int (*param_missing) (const EVP_PKEY *pk),
322                              int (*param_copy) (EVP_PKEY *to,
323                                                 const EVP_PKEY *from),
324                              int (*param_cmp) (const EVP_PKEY *a,
325                                                const EVP_PKEY *b),
326                              int (*param_print) (BIO *out,
327                                                  const EVP_PKEY *pkey,
328                                                  int indent, ASN1_PCTX *pctx))
329 {
330     ameth->param_decode = param_decode;
331     ameth->param_encode = param_encode;
332     ameth->param_missing = param_missing;
333     ameth->param_copy = param_copy;
334     ameth->param_cmp = param_cmp;
335     ameth->param_print = param_print;
336 }
337
338 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
339                             void (*pkey_free) (EVP_PKEY *pkey))
340 {
341     ameth->pkey_free = pkey_free;
342 }
343
344 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
345                             int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
346                                               long arg1, void *arg2))
347 {
348     ameth->pkey_ctrl = pkey_ctrl;
349 }
350
351 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
352                                      int (*pkey_security_bits) (const EVP_PKEY
353                                                                 *pk))
354 {
355     ameth->pkey_security_bits = pkey_security_bits;
356 }
357
358 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
359                             int (*item_verify) (EVP_MD_CTX *ctx,
360                                                 const ASN1_ITEM *it,
361                                                 void *asn,
362                                                 X509_ALGOR *a,
363                                                 ASN1_BIT_STRING *sig,
364                                                 EVP_PKEY *pkey),
365                             int (*item_sign) (EVP_MD_CTX *ctx,
366                                               const ASN1_ITEM *it,
367                                               void *asn,
368                                               X509_ALGOR *alg1,
369                                               X509_ALGOR *alg2,
370                                               ASN1_BIT_STRING *sig))
371 {
372     ameth->item_sign = item_sign;
373     ameth->item_verify = item_verify;
374 }
375
376 void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
377                               int (*siginf_set) (X509_SIG_INFO *siginf,
378                                                  const X509_ALGOR *alg,
379                                                  const ASN1_STRING *sig))
380 {
381     ameth->siginf_set = siginf_set;
382 }
383
384 void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
385                              int (*pkey_check) (const EVP_PKEY *pk))
386 {
387     ameth->pkey_check = pkey_check;
388 }
389
390 void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
391                                     int (*pkey_pub_check) (const EVP_PKEY *pk))
392 {
393     ameth->pkey_public_check = pkey_pub_check;
394 }
395
396 void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
397                                    int (*pkey_param_check) (const EVP_PKEY *pk))
398 {
399     ameth->pkey_param_check = pkey_param_check;
400 }
401
402 void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
403                                     int (*set_priv_key) (EVP_PKEY *pk,
404                                                          const unsigned char
405                                                             *priv,
406                                                          size_t len))
407 {
408     ameth->set_priv_key = set_priv_key;
409 }
410
411 void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
412                                    int (*set_pub_key) (EVP_PKEY *pk,
413                                                        const unsigned char *pub,
414                                                        size_t len))
415 {
416     ameth->set_pub_key = set_pub_key;
417 }
418
419 void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
420                                     int (*get_priv_key) (const EVP_PKEY *pk,
421                                                          unsigned char *priv,
422                                                          size_t *len))
423 {
424     ameth->get_priv_key = get_priv_key;
425 }
426
427 void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
428                                    int (*get_pub_key) (const EVP_PKEY *pk,
429                                                        unsigned char *pub,
430                                                        size_t *len))
431 {
432     ameth->get_pub_key = get_pub_key;
433 }