5662e176883c24aa9f66934b17c976815198887d
[openssl.git] / crypto / asn1 / ameth_lib.c
1 /*
2  * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "internal/asn1_int.h"
17 #include "internal/evp_int.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     tmp.pkey_id = type;
60     if (app_methods) {
61         int idx;
62         idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
63         if (idx >= 0)
64             return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
65     }
66     ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
67     if (!ret || !*ret)
68         return NULL;
69     return *ret;
70 }
71
72 /*
73  * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
74  * search through engines and set *pe to a functional reference to the engine
75  * implementing 'type' or NULL if no engine implements it.
76  */
77
78 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
79 {
80     const EVP_PKEY_ASN1_METHOD *t;
81
82     for (;;) {
83         t = pkey_asn1_find(type);
84         if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
85             break;
86         type = t->pkey_base_id;
87     }
88     if (pe) {
89 #ifndef OPENSSL_NO_ENGINE
90         ENGINE *e;
91         /* type will contain the final unaliased type */
92         e = ENGINE_get_pkey_asn1_meth_engine(type);
93         if (e) {
94             *pe = e;
95             return ENGINE_get_pkey_asn1_meth(e, type);
96         }
97 #endif
98         *pe = NULL;
99     }
100     return t;
101 }
102
103 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
104                                                    const char *str, int len)
105 {
106     int i;
107     const EVP_PKEY_ASN1_METHOD *ameth;
108     if (len == -1)
109         len = strlen(str);
110     if (pe) {
111 #ifndef OPENSSL_NO_ENGINE
112         ENGINE *e;
113         ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
114         if (ameth) {
115             /*
116              * Convert structural into functional reference
117              */
118             if (!ENGINE_init(e))
119                 ameth = NULL;
120             ENGINE_free(e);
121             *pe = e;
122             return ameth;
123         }
124 #endif
125         *pe = NULL;
126     }
127     for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
128         ameth = EVP_PKEY_asn1_get0(i);
129         if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
130             continue;
131         if (((int)strlen(ameth->pem_str) == len)
132             && (strncasecmp(ameth->pem_str, str, len) == 0))
133             return ameth;
134     }
135     return NULL;
136 }
137
138 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
139 {
140     if (pkey_asn1_find(ameth->pkey_id) != NULL) {
141         EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0,
142                EVP_R_PKEY_ASN1_METHOD_ALREADY_REGISTERED);
143         return 0;
144     }
145     if (app_methods == NULL) {
146         app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
147         if (app_methods == NULL)
148             return 0;
149     }
150     if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
151         return 0;
152     sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
153     return 1;
154 }
155
156 int EVP_PKEY_asn1_add_alias(int to, int from)
157 {
158     EVP_PKEY_ASN1_METHOD *ameth;
159     ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
160     if (ameth == NULL)
161         return 0;
162     ameth->pkey_base_id = to;
163     if (!EVP_PKEY_asn1_add0(ameth)) {
164         EVP_PKEY_asn1_free(ameth);
165         return 0;
166     }
167     return 1;
168 }
169
170 int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
171                             int *ppkey_flags, const char **pinfo,
172                             const char **ppem_str,
173                             const EVP_PKEY_ASN1_METHOD *ameth)
174 {
175     if (!ameth)
176         return 0;
177     if (ppkey_id)
178         *ppkey_id = ameth->pkey_id;
179     if (ppkey_base_id)
180         *ppkey_base_id = ameth->pkey_base_id;
181     if (ppkey_flags)
182         *ppkey_flags = ameth->pkey_flags;
183     if (pinfo)
184         *pinfo = ameth->info;
185     if (ppem_str)
186         *ppem_str = ameth->pem_str;
187     return 1;
188 }
189
190 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
191 {
192     return pkey->ameth;
193 }
194
195 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
196                                         const char *pem_str, const char *info)
197 {
198     EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
199
200     if (ameth == NULL)
201         return NULL;
202
203     ameth->pkey_id = id;
204     ameth->pkey_base_id = id;
205     ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
206
207     if (info) {
208         ameth->info = OPENSSL_strdup(info);
209         if (!ameth->info)
210             goto err;
211     }
212
213     if (pem_str) {
214         ameth->pem_str = OPENSSL_strdup(pem_str);
215         if (!ameth->pem_str)
216             goto err;
217     }
218
219     return ameth;
220
221  err:
222     EVP_PKEY_asn1_free(ameth);
223     return NULL;
224
225 }
226
227 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
228                         const EVP_PKEY_ASN1_METHOD *src)
229 {
230
231     dst->pub_decode = src->pub_decode;
232     dst->pub_encode = src->pub_encode;
233     dst->pub_cmp = src->pub_cmp;
234     dst->pub_print = src->pub_print;
235
236     dst->priv_decode = src->priv_decode;
237     dst->priv_encode = src->priv_encode;
238     dst->priv_print = src->priv_print;
239
240     dst->old_priv_encode = src->old_priv_encode;
241     dst->old_priv_decode = src->old_priv_decode;
242
243     dst->pkey_size = src->pkey_size;
244     dst->pkey_bits = src->pkey_bits;
245
246     dst->param_decode = src->param_decode;
247     dst->param_encode = src->param_encode;
248     dst->param_missing = src->param_missing;
249     dst->param_copy = src->param_copy;
250     dst->param_cmp = src->param_cmp;
251     dst->param_print = src->param_print;
252
253     dst->pkey_free = src->pkey_free;
254     dst->pkey_ctrl = src->pkey_ctrl;
255
256     dst->item_sign = src->item_sign;
257     dst->item_verify = src->item_verify;
258
259     dst->siginf_set = src->siginf_set;
260
261     dst->pkey_check = src->pkey_check;
262
263 }
264
265 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
266 {
267     if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
268         OPENSSL_free(ameth->pem_str);
269         OPENSSL_free(ameth->info);
270         OPENSSL_free(ameth);
271     }
272 }
273
274 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
275                               int (*pub_decode) (EVP_PKEY *pk,
276                                                  X509_PUBKEY *pub),
277                               int (*pub_encode) (X509_PUBKEY *pub,
278                                                  const EVP_PKEY *pk),
279                               int (*pub_cmp) (const EVP_PKEY *a,
280                                               const EVP_PKEY *b),
281                               int (*pub_print) (BIO *out,
282                                                 const EVP_PKEY *pkey,
283                                                 int indent, ASN1_PCTX *pctx),
284                               int (*pkey_size) (const EVP_PKEY *pk),
285                               int (*pkey_bits) (const EVP_PKEY *pk))
286 {
287     ameth->pub_decode = pub_decode;
288     ameth->pub_encode = pub_encode;
289     ameth->pub_cmp = pub_cmp;
290     ameth->pub_print = pub_print;
291     ameth->pkey_size = pkey_size;
292     ameth->pkey_bits = pkey_bits;
293 }
294
295 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
296                                int (*priv_decode) (EVP_PKEY *pk,
297                                                    const PKCS8_PRIV_KEY_INFO
298                                                    *p8inf),
299                                int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
300                                                    const EVP_PKEY *pk),
301                                int (*priv_print) (BIO *out,
302                                                   const EVP_PKEY *pkey,
303                                                   int indent,
304                                                   ASN1_PCTX *pctx))
305 {
306     ameth->priv_decode = priv_decode;
307     ameth->priv_encode = priv_encode;
308     ameth->priv_print = priv_print;
309 }
310
311 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
312                              int (*param_decode) (EVP_PKEY *pkey,
313                                                   const unsigned char **pder,
314                                                   int derlen),
315                              int (*param_encode) (const EVP_PKEY *pkey,
316                                                   unsigned char **pder),
317                              int (*param_missing) (const EVP_PKEY *pk),
318                              int (*param_copy) (EVP_PKEY *to,
319                                                 const EVP_PKEY *from),
320                              int (*param_cmp) (const EVP_PKEY *a,
321                                                const EVP_PKEY *b),
322                              int (*param_print) (BIO *out,
323                                                  const EVP_PKEY *pkey,
324                                                  int indent, ASN1_PCTX *pctx))
325 {
326     ameth->param_decode = param_decode;
327     ameth->param_encode = param_encode;
328     ameth->param_missing = param_missing;
329     ameth->param_copy = param_copy;
330     ameth->param_cmp = param_cmp;
331     ameth->param_print = param_print;
332 }
333
334 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
335                             void (*pkey_free) (EVP_PKEY *pkey))
336 {
337     ameth->pkey_free = pkey_free;
338 }
339
340 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
341                             int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
342                                               long arg1, void *arg2))
343 {
344     ameth->pkey_ctrl = pkey_ctrl;
345 }
346
347 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
348                                      int (*pkey_security_bits) (const EVP_PKEY
349                                                                 *pk))
350 {
351     ameth->pkey_security_bits = pkey_security_bits;
352 }
353
354 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
355                             int (*item_verify) (EVP_MD_CTX *ctx,
356                                                 const ASN1_ITEM *it,
357                                                 void *asn,
358                                                 X509_ALGOR *a,
359                                                 ASN1_BIT_STRING *sig,
360                                                 EVP_PKEY *pkey),
361                             int (*item_sign) (EVP_MD_CTX *ctx,
362                                               const ASN1_ITEM *it,
363                                               void *asn,
364                                               X509_ALGOR *alg1,
365                                               X509_ALGOR *alg2,
366                                               ASN1_BIT_STRING *sig))
367 {
368     ameth->item_sign = item_sign;
369     ameth->item_verify = item_verify;
370 }
371
372 void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
373                               int (*siginf_set) (X509_SIG_INFO *siginf,
374                                                  const X509_ALGOR *alg,
375                                                  const ASN1_STRING *sig))
376 {
377     ameth->siginf_set = siginf_set;
378 }
379
380 void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
381                              int (*pkey_check) (const EVP_PKEY *pk))
382 {
383     ameth->pkey_check = pkey_check;
384 }
385
386 void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
387                                     int (*pkey_pub_check) (const EVP_PKEY *pk))
388 {
389     ameth->pkey_public_check = pkey_pub_check;
390 }
391
392 void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
393                                    int (*pkey_param_check) (const EVP_PKEY *pk))
394 {
395     ameth->pkey_param_check = pkey_param_check;
396 }