2 * Copyright 2006-2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include "internal/cryptlib.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"
21 #include "standard_methods.h"
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;
26 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
27 const EVP_PKEY_ASN1_METHOD *, ameth);
29 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
30 const EVP_PKEY_ASN1_METHOD *const *b)
32 return ((*a)->pkey_id - (*b)->pkey_id);
35 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
36 const EVP_PKEY_ASN1_METHOD *, ameth);
38 int EVP_PKEY_asn1_get_count(void)
40 int num = OSSL_NELEM(standard_methods);
42 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
46 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
48 int num = OSSL_NELEM(standard_methods);
52 return standard_methods[idx];
54 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
57 static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
59 EVP_PKEY_ASN1_METHOD tmp;
60 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
65 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
67 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
69 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
70 if (ret == NULL || *ret == NULL)
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.
81 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
83 const EVP_PKEY_ASN1_METHOD *t;
86 t = pkey_asn1_find(type);
87 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
89 type = t->pkey_base_id;
92 #ifndef OPENSSL_NO_ENGINE
94 /* type will contain the final unaliased type */
95 e = ENGINE_get_pkey_asn1_meth_engine(type);
98 return ENGINE_get_pkey_asn1_meth(e, type);
106 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
107 const char *str, int len)
110 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
115 #ifndef OPENSSL_NO_ENGINE
117 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
120 * Convert structural into functional reference
131 for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
132 ameth = EVP_PKEY_asn1_get0(i);
133 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
135 if ((int)strlen(ameth->pem_str) == len
136 && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0)
142 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
144 EVP_PKEY_ASN1_METHOD tmp = { 0, };
147 * One of the following must be true:
149 * pem_str == NULL AND ASN1_PKEY_ALIAS is set
150 * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
152 * Anything else is an error and may lead to a corrupt ASN1 method table
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))) {
158 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
162 if (app_methods == NULL) {
163 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
164 if (app_methods == NULL)
168 tmp.pkey_id = ameth->pkey_id;
169 if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
170 ERR_raise(ERR_LIB_EVP,
171 EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
175 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
177 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
181 int EVP_PKEY_asn1_add_alias(int to, int from)
183 EVP_PKEY_ASN1_METHOD *ameth;
184 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
187 ameth->pkey_base_id = to;
188 if (!EVP_PKEY_asn1_add0(ameth)) {
189 EVP_PKEY_asn1_free(ameth);
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)
203 *ppkey_id = ameth->pkey_id;
205 *ppkey_base_id = ameth->pkey_base_id;
207 *ppkey_flags = ameth->pkey_flags;
209 *pinfo = ameth->info;
211 *ppem_str = ameth->pem_str;
215 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
220 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
221 const char *pem_str, const char *info)
223 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
226 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
231 ameth->pkey_base_id = id;
232 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
235 ameth->info = OPENSSL_strdup(info);
236 if (ameth->info == NULL)
241 ameth->pem_str = OPENSSL_strdup(pem_str);
242 if (ameth->pem_str == NULL)
249 EVP_PKEY_asn1_free(ameth);
250 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
254 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
255 const EVP_PKEY_ASN1_METHOD *src)
257 int pkey_id = dst->pkey_id;
258 int pkey_base_id = dst->pkey_base_id;
259 unsigned long pkey_flags = dst->pkey_flags;
260 char *pem_str = dst->pem_str;
261 char *info = dst->info;
265 /* We only copy the function pointers so restore the other values */
266 dst->pkey_id = pkey_id;
267 dst->pkey_base_id = pkey_base_id;
268 dst->pkey_flags = pkey_flags;
269 dst->pem_str = pem_str;
273 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
275 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
276 OPENSSL_free(ameth->pem_str);
277 OPENSSL_free(ameth->info);
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,
287 int (*pub_cmp) (const EVP_PKEY *a,
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))
295 ameth->pub_decode = pub_decode;
296 ameth->pub_encode = pub_encode;
297 ameth->pub_cmp = pub_cmp;
298 ameth->pub_print = pub_print;
299 ameth->pkey_size = pkey_size;
300 ameth->pkey_bits = pkey_bits;
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
307 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
309 int (*priv_print) (BIO *out,
310 const EVP_PKEY *pkey,
314 ameth->priv_decode = priv_decode;
315 ameth->priv_encode = priv_encode;
316 ameth->priv_print = priv_print;
319 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
320 int (*param_decode) (EVP_PKEY *pkey,
321 const unsigned char **pder,
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,
330 int (*param_print) (BIO *out,
331 const EVP_PKEY *pkey,
332 int indent, ASN1_PCTX *pctx))
334 ameth->param_decode = param_decode;
335 ameth->param_encode = param_encode;
336 ameth->param_missing = param_missing;
337 ameth->param_copy = param_copy;
338 ameth->param_cmp = param_cmp;
339 ameth->param_print = param_print;
342 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
343 void (*pkey_free) (EVP_PKEY *pkey))
345 ameth->pkey_free = pkey_free;
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))
352 ameth->pkey_ctrl = pkey_ctrl;
355 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
356 int (*pkey_security_bits) (const EVP_PKEY
359 ameth->pkey_security_bits = pkey_security_bits;
362 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
363 int (*item_verify) (EVP_MD_CTX *ctx,
367 const ASN1_BIT_STRING *sig,
369 int (*item_sign) (EVP_MD_CTX *ctx,
374 ASN1_BIT_STRING *sig))
376 ameth->item_sign = item_sign;
377 ameth->item_verify = item_verify;
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))
385 ameth->siginf_set = siginf_set;
388 void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
389 int (*pkey_check) (const EVP_PKEY *pk))
391 ameth->pkey_check = pkey_check;
394 void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
395 int (*pkey_pub_check) (const EVP_PKEY *pk))
397 ameth->pkey_public_check = pkey_pub_check;
400 void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
401 int (*pkey_param_check) (const EVP_PKEY *pk))
403 ameth->pkey_param_check = pkey_param_check;
406 void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
407 int (*set_priv_key) (EVP_PKEY *pk,
412 ameth->set_priv_key = set_priv_key;
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,
420 ameth->set_pub_key = set_pub_key;
423 void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
424 int (*get_priv_key) (const EVP_PKEY *pk,
428 ameth->get_priv_key = get_priv_key;
431 void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
432 int (*get_pub_key) (const EVP_PKEY *pk,
436 ameth->get_pub_key = get_pub_key;