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