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