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