2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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"
18 #include "standard_methods.h"
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;
23 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
24 const EVP_PKEY_ASN1_METHOD *, ameth);
26 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
27 const EVP_PKEY_ASN1_METHOD *const *b)
29 return ((*a)->pkey_id - (*b)->pkey_id);
32 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
33 const EVP_PKEY_ASN1_METHOD *, ameth);
35 int EVP_PKEY_asn1_get_count(void)
37 int num = OSSL_NELEM(standard_methods);
39 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
43 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
45 int num = OSSL_NELEM(standard_methods);
49 return standard_methods[idx];
51 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
54 static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
56 EVP_PKEY_ASN1_METHOD tmp;
57 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
61 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
63 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
65 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
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.
77 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
79 const EVP_PKEY_ASN1_METHOD *t;
82 t = pkey_asn1_find(type);
83 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
85 type = t->pkey_base_id;
88 #ifndef OPENSSL_NO_ENGINE
90 /* type will contain the final unaliased type */
91 e = ENGINE_get_pkey_asn1_meth_engine(type);
94 return ENGINE_get_pkey_asn1_meth(e, type);
102 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
103 const char *str, int len)
106 const EVP_PKEY_ASN1_METHOD *ameth;
110 #ifndef OPENSSL_NO_ENGINE
112 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
115 * Convert structural into functional reference
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)
130 if (((int)strlen(ameth->pem_str) == len)
131 && (strncasecmp(ameth->pem_str, str, len) == 0))
137 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
139 if (app_methods == NULL) {
140 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
141 if (app_methods == NULL)
144 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
146 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
150 int EVP_PKEY_asn1_add_alias(int to, int from)
152 EVP_PKEY_ASN1_METHOD *ameth;
153 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
156 ameth->pkey_base_id = to;
157 if (!EVP_PKEY_asn1_add0(ameth)) {
158 EVP_PKEY_asn1_free(ameth);
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)
172 *ppkey_id = ameth->pkey_id;
174 *ppkey_base_id = ameth->pkey_base_id;
176 *ppkey_flags = ameth->pkey_flags;
178 *pinfo = ameth->info;
180 *ppem_str = ameth->pem_str;
184 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
189 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
190 const char *pem_str, const char *info)
192 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
198 ameth->pkey_base_id = id;
199 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
202 ameth->info = OPENSSL_strdup(info);
208 ameth->pem_str = OPENSSL_strdup(pem_str);
216 EVP_PKEY_asn1_free(ameth);
221 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
222 const EVP_PKEY_ASN1_METHOD *src)
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;
230 dst->priv_decode = src->priv_decode;
231 dst->priv_encode = src->priv_encode;
232 dst->priv_print = src->priv_print;
234 dst->old_priv_encode = src->old_priv_encode;
235 dst->old_priv_decode = src->old_priv_decode;
237 dst->pkey_size = src->pkey_size;
238 dst->pkey_bits = src->pkey_bits;
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;
247 dst->pkey_free = src->pkey_free;
248 dst->pkey_ctrl = src->pkey_ctrl;
250 dst->item_sign = src->item_sign;
251 dst->item_verify = src->item_verify;
255 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
257 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
258 OPENSSL_free(ameth->pem_str);
259 OPENSSL_free(ameth->info);
264 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
265 int (*pub_decode) (EVP_PKEY *pk,
267 int (*pub_encode) (X509_PUBKEY *pub,
269 int (*pub_cmp) (const EVP_PKEY *a,
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))
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;
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
289 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
291 int (*priv_print) (BIO *out,
292 const EVP_PKEY *pkey,
296 ameth->priv_decode = priv_decode;
297 ameth->priv_encode = priv_encode;
298 ameth->priv_print = priv_print;
301 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
302 int (*param_decode) (EVP_PKEY *pkey,
303 const unsigned char **pder,
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,
312 int (*param_print) (BIO *out,
313 const EVP_PKEY *pkey,
314 int indent, ASN1_PCTX *pctx))
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;
324 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
325 void (*pkey_free) (EVP_PKEY *pkey))
327 ameth->pkey_free = pkey_free;
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))
334 ameth->pkey_ctrl = pkey_ctrl;
337 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
338 int (*pkey_security_bits) (const EVP_PKEY
341 ameth->pkey_security_bits = pkey_security_bits;
344 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
345 int (*item_verify) (EVP_MD_CTX *ctx,
349 ASN1_BIT_STRING *sig,
351 int (*item_sign) (EVP_MD_CTX *ctx,
356 ASN1_BIT_STRING *sig))
358 ameth->item_sign = item_sign;
359 ameth->item_verify = item_verify;