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 <openssl/evp.h>
12 #include "internal/asn1_int.h"
15 * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
16 * function that is used by EVP to hook in pkey_asn1_meth code and cache
17 * defaults (etc), will display brief debugging summaries to stderr with the
20 /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
22 static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
24 void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
26 engine_table_unregister(&pkey_asn1_meth_table, e);
29 static void engine_unregister_all_pkey_asn1_meths(void)
31 engine_table_cleanup(&pkey_asn1_meth_table);
34 int ENGINE_register_pkey_asn1_meths(ENGINE *e)
36 if (e->pkey_asn1_meths) {
38 int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
40 return engine_table_register(&pkey_asn1_meth_table,
41 engine_unregister_all_pkey_asn1_meths,
42 e, nids, num_nids, 0);
47 void ENGINE_register_all_pkey_asn1_meths(void)
51 for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
52 ENGINE_register_pkey_asn1_meths(e);
55 int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
57 if (e->pkey_asn1_meths) {
59 int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
61 return engine_table_register(&pkey_asn1_meth_table,
62 engine_unregister_all_pkey_asn1_meths,
63 e, nids, num_nids, 1);
69 * Exposed API function to get a functional reference from the implementation
70 * table (ie. try to get a functional reference from the tabled structural
71 * references) for a given pkey_asn1_meth 'nid'
73 ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
75 return engine_table_select(&pkey_asn1_meth_table, nid);
79 * Obtains a pkey_asn1_meth implementation from an ENGINE functional
82 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
84 EVP_PKEY_ASN1_METHOD *ret;
85 ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
86 if (!fn || !fn(e, &ret, NULL, nid)) {
87 ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH,
88 ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
94 /* Gets the pkey_asn1_meth callback from an ENGINE structure */
95 ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
97 return e->pkey_asn1_meths;
100 /* Sets the pkey_asn1_meth callback in an ENGINE structure */
101 int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
103 e->pkey_asn1_meths = f;
108 * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
109 * ENGINE is destroyed
112 void engine_pkey_asn1_meths_free(ENGINE *e)
115 EVP_PKEY_ASN1_METHOD *pkm;
116 if (e->pkey_asn1_meths) {
119 npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
120 for (i = 0; i < npknids; i++) {
121 if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
122 EVP_PKEY_asn1_free(pkm);
129 * Find a method based on a string. This does a linear search through all
130 * implemented algorithms. This is OK in practice because only a small number
131 * of algorithms are likely to be implemented in an engine and it is not used
132 * for speed critical operations.
135 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
141 EVP_PKEY_ASN1_METHOD *ameth;
142 if (!e->pkey_asn1_meths)
146 nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
147 for (i = 0; i < nidcount; i++) {
148 e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
149 if (((int)strlen(ameth->pem_str) == len)
150 && strncasecmp(ameth->pem_str, str, len) == 0)
158 const EVP_PKEY_ASN1_METHOD *ameth;
163 static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
165 ENGINE_FIND_STR *lk = arg;
169 for (i = 0; i < sk_ENGINE_num(sk); i++) {
170 ENGINE *e = sk_ENGINE_value(sk, i);
171 EVP_PKEY_ASN1_METHOD *ameth;
172 e->pkey_asn1_meths(e, &ameth, NULL, nid);
173 if (((int)strlen(ameth->pem_str) == lk->len)
174 && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
182 const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
186 ENGINE_FIND_STR fstr;
192 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
193 ENGINEerr(ENGINE_F_ENGINE_PKEY_ASN1_FIND_STR, ERR_R_MALLOC_FAILURE);
197 CRYPTO_THREAD_write_lock(global_engine_lock);
198 engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
199 /* If found obtain a structural reference to engine */
201 fstr.e->struct_ref++;
202 engine_ref_debug(fstr.e, 0, 1);
205 CRYPTO_THREAD_unlock(global_engine_lock);