Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/asn1/ameth_lib.c

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