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 /* Keep this sorted in type order !! */
19 static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {
20 #ifndef OPENSSL_NO_RSA
27 #ifndef OPENSSL_NO_DSA
38 #ifndef OPENSSL_NO_CMAC
49 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
50 static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
56 for (i = 0; i < OSSL_NELEM(standard_methods); i++)
57 fprintf(stderr, "Number %d id=%d (%s)\n", i,
58 standard_methods[i]->pkey_id,
59 OBJ_nid2sn(standard_methods[i]->pkey_id));
63 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
64 const EVP_PKEY_ASN1_METHOD *, ameth);
66 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
67 const EVP_PKEY_ASN1_METHOD *const *b)
69 return ((*a)->pkey_id - (*b)->pkey_id);
72 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
73 const EVP_PKEY_ASN1_METHOD *, ameth);
75 int EVP_PKEY_asn1_get_count(void)
77 int num = OSSL_NELEM(standard_methods);
79 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
83 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
85 int num = OSSL_NELEM(standard_methods);
89 return standard_methods[idx];
91 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
94 static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
96 EVP_PKEY_ASN1_METHOD tmp;
97 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
101 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
103 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
105 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
112 * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
113 * search through engines and set *pe to a functional reference to the engine
114 * implementing 'type' or NULL if no engine implements it.
117 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
119 const EVP_PKEY_ASN1_METHOD *t;
122 t = pkey_asn1_find(type);
123 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
125 type = t->pkey_base_id;
128 #ifndef OPENSSL_NO_ENGINE
130 /* type will contain the final unaliased type */
131 e = ENGINE_get_pkey_asn1_meth_engine(type);
134 return ENGINE_get_pkey_asn1_meth(e, type);
142 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
143 const char *str, int len)
146 const EVP_PKEY_ASN1_METHOD *ameth;
150 #ifndef OPENSSL_NO_ENGINE
152 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
155 * Convert structural into functional reference
166 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
167 ameth = EVP_PKEY_asn1_get0(i);
168 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
170 if (((int)strlen(ameth->pem_str) == len)
171 && (strncasecmp(ameth->pem_str, str, len) == 0))
177 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
179 if (app_methods == NULL) {
180 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
181 if (app_methods == NULL)
184 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
186 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
190 int EVP_PKEY_asn1_add_alias(int to, int from)
192 EVP_PKEY_ASN1_METHOD *ameth;
193 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
196 ameth->pkey_base_id = to;
197 if (!EVP_PKEY_asn1_add0(ameth)) {
198 EVP_PKEY_asn1_free(ameth);
204 int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
205 int *ppkey_flags, const char **pinfo,
206 const char **ppem_str,
207 const EVP_PKEY_ASN1_METHOD *ameth)
212 *ppkey_id = ameth->pkey_id;
214 *ppkey_base_id = ameth->pkey_base_id;
216 *ppkey_flags = ameth->pkey_flags;
218 *pinfo = ameth->info;
220 *ppem_str = ameth->pem_str;
224 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
229 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
230 const char *pem_str, const char *info)
232 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
238 ameth->pkey_base_id = id;
239 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
242 ameth->info = OPENSSL_strdup(info);
248 ameth->pem_str = OPENSSL_strdup(pem_str);
256 EVP_PKEY_asn1_free(ameth);
261 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
262 const EVP_PKEY_ASN1_METHOD *src)
265 dst->pub_decode = src->pub_decode;
266 dst->pub_encode = src->pub_encode;
267 dst->pub_cmp = src->pub_cmp;
268 dst->pub_print = src->pub_print;
270 dst->priv_decode = src->priv_decode;
271 dst->priv_encode = src->priv_encode;
272 dst->priv_print = src->priv_print;
274 dst->old_priv_encode = src->old_priv_encode;
275 dst->old_priv_decode = src->old_priv_decode;
277 dst->pkey_size = src->pkey_size;
278 dst->pkey_bits = src->pkey_bits;
280 dst->param_decode = src->param_decode;
281 dst->param_encode = src->param_encode;
282 dst->param_missing = src->param_missing;
283 dst->param_copy = src->param_copy;
284 dst->param_cmp = src->param_cmp;
285 dst->param_print = src->param_print;
287 dst->pkey_free = src->pkey_free;
288 dst->pkey_ctrl = src->pkey_ctrl;
290 dst->item_sign = src->item_sign;
291 dst->item_verify = src->item_verify;
295 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
297 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
298 OPENSSL_free(ameth->pem_str);
299 OPENSSL_free(ameth->info);
304 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
305 int (*pub_decode) (EVP_PKEY *pk,
307 int (*pub_encode) (X509_PUBKEY *pub,
309 int (*pub_cmp) (const EVP_PKEY *a,
311 int (*pub_print) (BIO *out,
312 const EVP_PKEY *pkey,
313 int indent, ASN1_PCTX *pctx),
314 int (*pkey_size) (const EVP_PKEY *pk),
315 int (*pkey_bits) (const EVP_PKEY *pk))
317 ameth->pub_decode = pub_decode;
318 ameth->pub_encode = pub_encode;
319 ameth->pub_cmp = pub_cmp;
320 ameth->pub_print = pub_print;
321 ameth->pkey_size = pkey_size;
322 ameth->pkey_bits = pkey_bits;
325 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
326 int (*priv_decode) (EVP_PKEY *pk,
327 const PKCS8_PRIV_KEY_INFO
329 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
331 int (*priv_print) (BIO *out,
332 const EVP_PKEY *pkey,
336 ameth->priv_decode = priv_decode;
337 ameth->priv_encode = priv_encode;
338 ameth->priv_print = priv_print;
341 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
342 int (*param_decode) (EVP_PKEY *pkey,
343 const unsigned char **pder,
345 int (*param_encode) (const EVP_PKEY *pkey,
346 unsigned char **pder),
347 int (*param_missing) (const EVP_PKEY *pk),
348 int (*param_copy) (EVP_PKEY *to,
349 const EVP_PKEY *from),
350 int (*param_cmp) (const EVP_PKEY *a,
352 int (*param_print) (BIO *out,
353 const EVP_PKEY *pkey,
354 int indent, ASN1_PCTX *pctx))
356 ameth->param_decode = param_decode;
357 ameth->param_encode = param_encode;
358 ameth->param_missing = param_missing;
359 ameth->param_copy = param_copy;
360 ameth->param_cmp = param_cmp;
361 ameth->param_print = param_print;
364 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
365 void (*pkey_free) (EVP_PKEY *pkey))
367 ameth->pkey_free = pkey_free;
370 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
371 int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
372 long arg1, void *arg2))
374 ameth->pkey_ctrl = pkey_ctrl;
377 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
378 int (*pkey_security_bits) (const EVP_PKEY
381 ameth->pkey_security_bits = pkey_security_bits;
384 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
385 int (*item_verify) (EVP_MD_CTX *ctx,
389 ASN1_BIT_STRING *sig,
391 int (*item_sign) (EVP_MD_CTX *ctx,
396 ASN1_BIT_STRING *sig))
398 ameth->item_sign = item_sign;
399 ameth->item_verify = item_verify;