]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/ameth_lib.c
Added an explicit yield (OP_SLEEP) to QUIC testing for cooperative threading.
[thirdparty/openssl.git] / crypto / asn1 / ameth_lib.c
CommitLineData
0f113f3e 1/*
fecb3aae 2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
448be743 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
448be743
DSH
8 */
9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
b39fc560 13#include "internal/cryptlib.h"
677963e5 14#include <stdio.h>
448be743
DSH
15#include <openssl/asn1t.h>
16#include <openssl/x509.h>
3c27208f 17#include <openssl/engine.h>
25f2138b
DMSP
18#include "crypto/asn1.h"
19#include "crypto/evp.h"
448be743 20
2c166171 21#include "standard_methods.h"
448be743 22
0f113f3e 23typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
5ce278a7 24static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
18e377b4 25
606f6c47 26DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
0f113f3e 27 const EVP_PKEY_ASN1_METHOD *, ameth);
babb3798 28
0f113f3e
MC
29static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
30 const EVP_PKEY_ASN1_METHOD *const *b)
31{
32 return ((*a)->pkey_id - (*b)->pkey_id);
33}
448be743 34
606f6c47 35IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
0f113f3e 36 const EVP_PKEY_ASN1_METHOD *, ameth);
babb3798 37
e4263314 38int EVP_PKEY_asn1_get_count(void)
0f113f3e 39{
b6eb9827 40 int num = OSSL_NELEM(standard_methods);
0f113f3e
MC
41 if (app_methods)
42 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
43 return num;
44}
e4263314
DSH
45
46const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
0f113f3e 47{
b6eb9827 48 int num = OSSL_NELEM(standard_methods);
0f113f3e
MC
49 if (idx < 0)
50 return NULL;
51 if (idx < num)
52 return standard_methods[idx];
53 idx -= num;
54 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
55}
e4263314 56
01b8b3c7 57static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
0f113f3e
MC
58{
59 EVP_PKEY_ASN1_METHOD tmp;
60 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
12a765a5 61
0f113f3e
MC
62 tmp.pkey_id = type;
63 if (app_methods) {
64 int idx;
65 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
66 if (idx >= 0)
67 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
68 }
b6eb9827 69 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
12a765a5 70 if (ret == NULL || *ret == NULL)
0f113f3e
MC
71 return NULL;
72 return *ret;
73}
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.
01b8b3c7
DSH
79 */
80
81const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
0f113f3e
MC
82{
83 const EVP_PKEY_ASN1_METHOD *t;
84
85 for (;;) {
86 t = pkey_asn1_find(type);
87 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
88 break;
89 type = t->pkey_base_id;
90 }
91 if (pe) {
01b8b3c7 92#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
93 ENGINE *e;
94 /* type will contain the final unaliased type */
95 e = ENGINE_get_pkey_asn1_meth_engine(type);
96 if (e) {
97 *pe = e;
98 return ENGINE_get_pkey_asn1_meth(e, type);
99 }
01b8b3c7 100#endif
0f113f3e
MC
101 *pe = NULL;
102 }
103 return t;
104}
01b8b3c7
DSH
105
106const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
0f113f3e
MC
107 const char *str, int len)
108{
109 int i;
3bf0c3fe
RL
110 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
111
0f113f3e
MC
112 if (len == -1)
113 len = strlen(str);
114 if (pe) {
01b8b3c7 115#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
116 ENGINE *e;
117 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
118 if (ameth) {
119 /*
120 * Convert structural into functional reference
121 */
122 if (!ENGINE_init(e))
123 ameth = NULL;
124 ENGINE_free(e);
125 *pe = e;
126 return ameth;
127 }
01b8b3c7 128#endif
0f113f3e
MC
129 *pe = NULL;
130 }
3bf0c3fe 131 for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
0f113f3e
MC
132 ameth = EVP_PKEY_asn1_get0(i);
133 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
134 continue;
3bf0c3fe 135 if ((int)strlen(ameth->pem_str) == len
fba140c7 136 && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0)
0f113f3e
MC
137 return ameth;
138 }
139 return NULL;
140}
e4263314 141
e46691a0 142int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
0f113f3e 143{
3bf0c3fe
RL
144 EVP_PKEY_ASN1_METHOD tmp = { 0, };
145
a8600316
RL
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 if (!((ameth->pem_str == NULL
155 && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
156 || (ameth->pem_str != NULL
157 && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
9311d0c4 158 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
a8600316
RL
159 return 0;
160 }
161
0f113f3e
MC
162 if (app_methods == NULL) {
163 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
90945fa3 164 if (app_methods == NULL)
0f113f3e
MC
165 return 0;
166 }
3bf0c3fe
RL
167
168 tmp.pkey_id = ameth->pkey_id;
169 if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
9311d0c4
RL
170 ERR_raise(ERR_LIB_EVP,
171 EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
3bf0c3fe
RL
172 return 0;
173 }
174
0f113f3e
MC
175 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
176 return 0;
177 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
178 return 1;
179}
18e377b4 180
e46691a0 181int EVP_PKEY_asn1_add_alias(int to, int from)
0f113f3e
MC
182{
183 EVP_PKEY_ASN1_METHOD *ameth;
184 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
90945fa3 185 if (ameth == NULL)
0f113f3e
MC
186 return 0;
187 ameth->pkey_base_id = to;
188 if (!EVP_PKEY_asn1_add0(ameth)) {
189 EVP_PKEY_asn1_free(ameth);
190 return 0;
191 }
192 return 1;
193}
194
195int 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{
200 if (!ameth)
201 return 0;
202 if (ppkey_id)
203 *ppkey_id = ameth->pkey_id;
204 if (ppkey_base_id)
205 *ppkey_base_id = ameth->pkey_base_id;
206 if (ppkey_flags)
207 *ppkey_flags = ameth->pkey_flags;
208 if (pinfo)
209 *pinfo = ameth->info;
210 if (ppem_str)
211 *ppem_str = ameth->pem_str;
212 return 1;
213}
214
8900f3e3 215const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
0f113f3e
MC
216{
217 return pkey->ameth;
218}
219
220EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
221 const char *pem_str, const char *info)
222{
b51bce94
RS
223 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
224
e077455e 225 if (ameth == NULL)
0f113f3e
MC
226 return NULL;
227
0f113f3e
MC
228 ameth->pkey_id = id;
229 ameth->pkey_base_id = id;
230 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
231
232 if (info) {
7644a9ae 233 ameth->info = OPENSSL_strdup(info);
89947af2 234 if (ameth->info == NULL)
0f113f3e 235 goto err;
64b25758 236 }
0f113f3e
MC
237
238 if (pem_str) {
7644a9ae 239 ameth->pem_str = OPENSSL_strdup(pem_str);
89947af2 240 if (ameth->pem_str == NULL)
0f113f3e 241 goto err;
64b25758 242 }
0f113f3e
MC
243
244 return ameth;
245
246 err:
0f113f3e
MC
247 EVP_PKEY_asn1_free(ameth);
248 return NULL;
0f113f3e
MC
249}
250
251void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
252 const EVP_PKEY_ASN1_METHOD *src)
253{
472a88b7
MC
254 int pkey_id = dst->pkey_id;
255 int pkey_base_id = dst->pkey_base_id;
256 unsigned long pkey_flags = dst->pkey_flags;
257 char *pem_str = dst->pem_str;
258 char *info = dst->info;
259
260 *dst = *src;
261
262 /* We only copy the function pointers so restore the other values */
263 dst->pkey_id = pkey_id;
264 dst->pkey_base_id = pkey_base_id;
265 dst->pkey_flags = pkey_flags;
266 dst->pem_str = pem_str;
267 dst->info = info;
0f113f3e 268}
e69adea5 269
d82e2718 270void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
0f113f3e
MC
271{
272 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
b548a1f1
RS
273 OPENSSL_free(ameth->pem_str);
274 OPENSSL_free(ameth->info);
0f113f3e
MC
275 OPENSSL_free(ameth);
276 }
277}
18e377b4
DSH
278
279void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e 280 int (*pub_decode) (EVP_PKEY *pk,
7674e923 281 const X509_PUBKEY *pub),
0f113f3e
MC
282 int (*pub_encode) (X509_PUBKEY *pub,
283 const EVP_PKEY *pk),
284 int (*pub_cmp) (const EVP_PKEY *a,
285 const EVP_PKEY *b),
286 int (*pub_print) (BIO *out,
287 const EVP_PKEY *pkey,
288 int indent, ASN1_PCTX *pctx),
289 int (*pkey_size) (const EVP_PKEY *pk),
290 int (*pkey_bits) (const EVP_PKEY *pk))
291{
292 ameth->pub_decode = pub_decode;
293 ameth->pub_encode = pub_encode;
294 ameth->pub_cmp = pub_cmp;
295 ameth->pub_print = pub_print;
296 ameth->pkey_size = pkey_size;
297 ameth->pkey_bits = pkey_bits;
298}
18e377b4
DSH
299
300void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e 301 int (*priv_decode) (EVP_PKEY *pk,
245c6bc3 302 const PKCS8_PRIV_KEY_INFO
0f113f3e
MC
303 *p8inf),
304 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
305 const EVP_PKEY *pk),
306 int (*priv_print) (BIO *out,
307 const EVP_PKEY *pkey,
308 int indent,
309 ASN1_PCTX *pctx))
310{
311 ameth->priv_decode = priv_decode;
312 ameth->priv_encode = priv_encode;
313 ameth->priv_print = priv_print;
314}
18e377b4
DSH
315
316void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
317 int (*param_decode) (EVP_PKEY *pkey,
318 const unsigned char **pder,
319 int derlen),
320 int (*param_encode) (const EVP_PKEY *pkey,
321 unsigned char **pder),
322 int (*param_missing) (const EVP_PKEY *pk),
323 int (*param_copy) (EVP_PKEY *to,
324 const EVP_PKEY *from),
325 int (*param_cmp) (const EVP_PKEY *a,
326 const EVP_PKEY *b),
327 int (*param_print) (BIO *out,
328 const EVP_PKEY *pkey,
329 int indent, ASN1_PCTX *pctx))
330{
331 ameth->param_decode = param_decode;
332 ameth->param_encode = param_encode;
333 ameth->param_missing = param_missing;
334 ameth->param_copy = param_copy;
335 ameth->param_cmp = param_cmp;
336 ameth->param_print = param_print;
337}
18e377b4
DSH
338
339void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
340 void (*pkey_free) (EVP_PKEY *pkey))
341{
342 ameth->pkey_free = pkey_free;
343}
18e377b4
DSH
344
345void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
346 int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
347 long arg1, void *arg2))
348{
349 ameth->pkey_ctrl = pkey_ctrl;
350}
2514fa79
DSH
351
352void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
0f113f3e
MC
353 int (*pkey_security_bits) (const EVP_PKEY
354 *pk))
355{
356 ameth->pkey_security_bits = pkey_security_bits;
357}
3418f7b7
SA
358
359void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
360 int (*item_verify) (EVP_MD_CTX *ctx,
361 const ASN1_ITEM *it,
ded346fa
DDO
362 const void *data,
363 const X509_ALGOR *a,
364 const ASN1_BIT_STRING *sig,
3418f7b7
SA
365 EVP_PKEY *pkey),
366 int (*item_sign) (EVP_MD_CTX *ctx,
367 const ASN1_ITEM *it,
ded346fa 368 const void *data,
3418f7b7
SA
369 X509_ALGOR *alg1,
370 X509_ALGOR *alg2,
371 ASN1_BIT_STRING *sig))
372{
373 ameth->item_sign = item_sign;
374 ameth->item_verify = item_verify;
375}
5e006082
RL
376
377void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
378 int (*siginf_set) (X509_SIG_INFO *siginf,
379 const X509_ALGOR *alg,
380 const ASN1_STRING *sig))
381{
382 ameth->siginf_set = siginf_set;
383}
384
385void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
386 int (*pkey_check) (const EVP_PKEY *pk))
387{
388 ameth->pkey_check = pkey_check;
389}
b0004708
PY
390
391void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
392 int (*pkey_pub_check) (const EVP_PKEY *pk))
393{
394 ameth->pkey_public_check = pkey_pub_check;
395}
396
397void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
398 int (*pkey_param_check) (const EVP_PKEY *pk))
399{
400 ameth->pkey_param_check = pkey_param_check;
401}
e8f9f08f
MC
402
403void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
404 int (*set_priv_key) (EVP_PKEY *pk,
405 const unsigned char
406 *priv,
407 size_t len))
408{
409 ameth->set_priv_key = set_priv_key;
410}
411
412void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
413 int (*set_pub_key) (EVP_PKEY *pk,
414 const unsigned char *pub,
415 size_t len))
416{
417 ameth->set_pub_key = set_pub_key;
418}
72ff0a54
MC
419
420void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
421 int (*get_priv_key) (const EVP_PKEY *pk,
422 unsigned char *priv,
423 size_t *len))
424{
425 ameth->get_priv_key = get_priv_key;
426}
427
428void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
429 int (*get_pub_key) (const EVP_PKEY *pk,
430 unsigned char *pub,
431 size_t *len))
432{
433 ameth->get_pub_key = get_pub_key;
434}