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