2 * Copyright 2006-2021 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
14 #include "eng_local.h"
15 #include <openssl/evp.h>
16 #include "crypto/asn1.h"
19 * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the
20 * function that is used by EVP to hook in pkey_asn1_meth code and cache
21 * defaults (etc), will display brief debugging summaries to stderr with the
24 /* #define ENGINE_PKEY_ASN1_METH_DEBUG */
26 static ENGINE_TABLE *pkey_asn1_meth_table = NULL;
28 void ENGINE_unregister_pkey_asn1_meths(ENGINE *e)
30 engine_table_unregister(&pkey_asn1_meth_table, e);
33 static void engine_unregister_all_pkey_asn1_meths(void)
35 engine_table_cleanup(&pkey_asn1_meth_table);
38 int ENGINE_register_pkey_asn1_meths(ENGINE *e)
40 if (e->pkey_asn1_meths) {
42 int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
44 return engine_table_register(&pkey_asn1_meth_table,
45 engine_unregister_all_pkey_asn1_meths,
46 e, nids, num_nids, 0);
51 void ENGINE_register_all_pkey_asn1_meths(void)
55 for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
56 ENGINE_register_pkey_asn1_meths(e);
59 int ENGINE_set_default_pkey_asn1_meths(ENGINE *e)
61 if (e->pkey_asn1_meths) {
63 int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0);
65 return engine_table_register(&pkey_asn1_meth_table,
66 engine_unregister_all_pkey_asn1_meths,
67 e, nids, num_nids, 1);
73 * Exposed API function to get a functional reference from the implementation
74 * table (ie. try to get a functional reference from the tabled structural
75 * references) for a given pkey_asn1_meth 'nid'
77 ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid)
79 return ossl_engine_table_select(&pkey_asn1_meth_table, nid,
80 OPENSSL_FILE, OPENSSL_LINE);
84 * Obtains a pkey_asn1_meth implementation from an ENGINE functional
87 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid)
89 EVP_PKEY_ASN1_METHOD *ret;
90 ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e);
91 if (!fn || !fn(e, &ret, NULL, nid)) {
92 ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD);
98 /* Gets the pkey_asn1_meth callback from an ENGINE structure */
99 ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e)
101 return e->pkey_asn1_meths;
104 /* Sets the pkey_asn1_meth callback in an ENGINE structure */
105 int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f)
107 e->pkey_asn1_meths = f;
112 * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an
113 * ENGINE is destroyed
116 void engine_pkey_asn1_meths_free(ENGINE *e)
119 EVP_PKEY_ASN1_METHOD *pkm;
120 if (e->pkey_asn1_meths) {
123 npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0);
124 for (i = 0; i < npknids; i++) {
125 if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) {
126 EVP_PKEY_asn1_free(pkm);
133 * Find a method based on a string. This does a linear search through all
134 * implemented algorithms. This is OK in practice because only a small number
135 * of algorithms are likely to be implemented in an engine and it is not used
136 * for speed critical operations.
139 const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e,
145 EVP_PKEY_ASN1_METHOD *ameth;
146 if (!e->pkey_asn1_meths)
150 nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0);
151 for (i = 0; i < nidcount; i++) {
152 e->pkey_asn1_meths(e, &ameth, NULL, nids[i]);
154 && ((int)strlen(ameth->pem_str) == len)
155 && strncasecmp(ameth->pem_str, str, len) == 0)
163 const EVP_PKEY_ASN1_METHOD *ameth;
168 static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg)
170 ENGINE_FIND_STR *lk = arg;
174 for (i = 0; i < sk_ENGINE_num(sk); i++) {
175 ENGINE *e = sk_ENGINE_value(sk, i);
176 EVP_PKEY_ASN1_METHOD *ameth;
177 e->pkey_asn1_meths(e, &ameth, NULL, nid);
179 && ((int)strlen(ameth->pem_str) == lk->len)
180 && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) {
188 const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe,
192 ENGINE_FIND_STR fstr;
198 if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) {
199 ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE);
203 if (!CRYPTO_THREAD_write_lock(global_engine_lock))
205 engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr);
206 /* If found obtain a structural reference to engine */
208 fstr.e->struct_ref++;
209 ENGINE_REF_PRINT(fstr.e, 0, 1);
212 CRYPTO_THREAD_unlock(global_engine_lock);