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
46 typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
47 static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
53 for (i = 0; i < OSSL_NELEM(standard_methods); i++)
54 fprintf(stderr, "Number %d id=%d (%s)\n", i,
55 standard_methods[i]->pkey_id,
56 OBJ_nid2sn(standard_methods[i]->pkey_id));
60 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
61 const EVP_PKEY_ASN1_METHOD *, ameth);
63 static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
64 const EVP_PKEY_ASN1_METHOD *const *b)
66 return ((*a)->pkey_id - (*b)->pkey_id);
69 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
70 const EVP_PKEY_ASN1_METHOD *, ameth);
72 int EVP_PKEY_asn1_get_count(void)
74 int num = OSSL_NELEM(standard_methods);
76 num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
80 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
82 int num = OSSL_NELEM(standard_methods);
86 return standard_methods[idx];
88 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
91 static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
93 EVP_PKEY_ASN1_METHOD tmp;
94 const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
98 idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
100 return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
102 ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
109 * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
110 * search through engines and set *pe to a functional reference to the engine
111 * implementing 'type' or NULL if no engine implements it.
114 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
116 const EVP_PKEY_ASN1_METHOD *t;
119 t = pkey_asn1_find(type);
120 if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
122 type = t->pkey_base_id;
125 #ifndef OPENSSL_NO_ENGINE
127 /* type will contain the final unaliased type */
128 e = ENGINE_get_pkey_asn1_meth_engine(type);
131 return ENGINE_get_pkey_asn1_meth(e, type);
139 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
140 const char *str, int len)
143 const EVP_PKEY_ASN1_METHOD *ameth;
147 #ifndef OPENSSL_NO_ENGINE
149 ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
152 * Convert structural into functional reference
163 for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
164 ameth = EVP_PKEY_asn1_get0(i);
165 if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
167 if (((int)strlen(ameth->pem_str) == len)
168 && (strncasecmp(ameth->pem_str, str, len) == 0))
174 int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
176 if (app_methods == NULL) {
177 app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
178 if (app_methods == NULL)
181 if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
183 sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
187 int EVP_PKEY_asn1_add_alias(int to, int from)
189 EVP_PKEY_ASN1_METHOD *ameth;
190 ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
193 ameth->pkey_base_id = to;
194 if (!EVP_PKEY_asn1_add0(ameth)) {
195 EVP_PKEY_asn1_free(ameth);
201 int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
202 int *ppkey_flags, const char **pinfo,
203 const char **ppem_str,
204 const EVP_PKEY_ASN1_METHOD *ameth)
209 *ppkey_id = ameth->pkey_id;
211 *ppkey_base_id = ameth->pkey_base_id;
213 *ppkey_flags = ameth->pkey_flags;
215 *pinfo = ameth->info;
217 *ppem_str = ameth->pem_str;
221 const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
226 EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
227 const char *pem_str, const char *info)
229 EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
235 ameth->pkey_base_id = id;
236 ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
239 ameth->info = OPENSSL_strdup(info);
245 ameth->pem_str = OPENSSL_strdup(pem_str);
253 EVP_PKEY_asn1_free(ameth);
258 void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
259 const EVP_PKEY_ASN1_METHOD *src)
262 dst->pub_decode = src->pub_decode;
263 dst->pub_encode = src->pub_encode;
264 dst->pub_cmp = src->pub_cmp;
265 dst->pub_print = src->pub_print;
267 dst->priv_decode = src->priv_decode;
268 dst->priv_encode = src->priv_encode;
269 dst->priv_print = src->priv_print;
271 dst->old_priv_encode = src->old_priv_encode;
272 dst->old_priv_decode = src->old_priv_decode;
274 dst->pkey_size = src->pkey_size;
275 dst->pkey_bits = src->pkey_bits;
277 dst->param_decode = src->param_decode;
278 dst->param_encode = src->param_encode;
279 dst->param_missing = src->param_missing;
280 dst->param_copy = src->param_copy;
281 dst->param_cmp = src->param_cmp;
282 dst->param_print = src->param_print;
284 dst->pkey_free = src->pkey_free;
285 dst->pkey_ctrl = src->pkey_ctrl;
287 dst->item_sign = src->item_sign;
288 dst->item_verify = src->item_verify;
292 void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
294 if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
295 OPENSSL_free(ameth->pem_str);
296 OPENSSL_free(ameth->info);
301 void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
302 int (*pub_decode) (EVP_PKEY *pk,
304 int (*pub_encode) (X509_PUBKEY *pub,
306 int (*pub_cmp) (const EVP_PKEY *a,
308 int (*pub_print) (BIO *out,
309 const EVP_PKEY *pkey,
310 int indent, ASN1_PCTX *pctx),
311 int (*pkey_size) (const EVP_PKEY *pk),
312 int (*pkey_bits) (const EVP_PKEY *pk))
314 ameth->pub_decode = pub_decode;
315 ameth->pub_encode = pub_encode;
316 ameth->pub_cmp = pub_cmp;
317 ameth->pub_print = pub_print;
318 ameth->pkey_size = pkey_size;
319 ameth->pkey_bits = pkey_bits;
322 void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
323 int (*priv_decode) (EVP_PKEY *pk,
326 int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
328 int (*priv_print) (BIO *out,
329 const EVP_PKEY *pkey,
333 ameth->priv_decode = priv_decode;
334 ameth->priv_encode = priv_encode;
335 ameth->priv_print = priv_print;
338 void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
339 int (*param_decode) (EVP_PKEY *pkey,
340 const unsigned char **pder,
342 int (*param_encode) (const EVP_PKEY *pkey,
343 unsigned char **pder),
344 int (*param_missing) (const EVP_PKEY *pk),
345 int (*param_copy) (EVP_PKEY *to,
346 const EVP_PKEY *from),
347 int (*param_cmp) (const EVP_PKEY *a,
349 int (*param_print) (BIO *out,
350 const EVP_PKEY *pkey,
351 int indent, ASN1_PCTX *pctx))
353 ameth->param_decode = param_decode;
354 ameth->param_encode = param_encode;
355 ameth->param_missing = param_missing;
356 ameth->param_copy = param_copy;
357 ameth->param_cmp = param_cmp;
358 ameth->param_print = param_print;
361 void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
362 void (*pkey_free) (EVP_PKEY *pkey))
364 ameth->pkey_free = pkey_free;
367 void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
368 int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
369 long arg1, void *arg2))
371 ameth->pkey_ctrl = pkey_ctrl;
374 void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
375 int (*pkey_security_bits) (const EVP_PKEY
378 ameth->pkey_security_bits = pkey_security_bits;
381 void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
382 int (*item_verify) (EVP_MD_CTX *ctx,
386 ASN1_BIT_STRING *sig,
388 int (*item_sign) (EVP_MD_CTX *ctx,
393 ASN1_BIT_STRING *sig))
395 ameth->item_sign = item_sign;
396 ameth->item_verify = item_verify;