/src/openssl/crypto/engine/tb_asnmth.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | /* We need to use some engine deprecated APIs */ |
11 | | #define OPENSSL_SUPPRESS_DEPRECATED |
12 | | |
13 | | #include "internal/e_os.h" |
14 | | #include "eng_local.h" |
15 | | #include <openssl/evp.h> |
16 | | #include "crypto/asn1.h" |
17 | | |
18 | | /* |
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 |
22 | | * 'nid'. |
23 | | */ |
24 | | /* #define ENGINE_PKEY_ASN1_METH_DEBUG */ |
25 | | |
26 | | static ENGINE_TABLE *pkey_asn1_meth_table = NULL; |
27 | | |
28 | | void ENGINE_unregister_pkey_asn1_meths(ENGINE *e) |
29 | 0 | { |
30 | 0 | engine_table_unregister(&pkey_asn1_meth_table, e); |
31 | 0 | } |
32 | | |
33 | | static void engine_unregister_all_pkey_asn1_meths(void) |
34 | 0 | { |
35 | 0 | engine_table_cleanup(&pkey_asn1_meth_table); |
36 | 0 | } |
37 | | |
38 | | int ENGINE_register_pkey_asn1_meths(ENGINE *e) |
39 | 0 | { |
40 | 0 | if (e->pkey_asn1_meths) { |
41 | 0 | const int *nids; |
42 | 0 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); |
43 | 0 | if (num_nids > 0) |
44 | 0 | return engine_table_register(&pkey_asn1_meth_table, |
45 | 0 | engine_unregister_all_pkey_asn1_meths, |
46 | 0 | e, nids, num_nids, 0); |
47 | 0 | } |
48 | 0 | return 1; |
49 | 0 | } |
50 | | |
51 | | void ENGINE_register_all_pkey_asn1_meths(void) |
52 | 0 | { |
53 | 0 | ENGINE *e; |
54 | |
|
55 | 0 | for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) |
56 | 0 | ENGINE_register_pkey_asn1_meths(e); |
57 | 0 | } |
58 | | |
59 | | int ENGINE_set_default_pkey_asn1_meths(ENGINE *e) |
60 | 0 | { |
61 | 0 | if (e->pkey_asn1_meths) { |
62 | 0 | const int *nids; |
63 | 0 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); |
64 | 0 | if (num_nids > 0) |
65 | 0 | return engine_table_register(&pkey_asn1_meth_table, |
66 | 0 | engine_unregister_all_pkey_asn1_meths, |
67 | 0 | e, nids, num_nids, 1); |
68 | 0 | } |
69 | 0 | return 1; |
70 | 0 | } |
71 | | |
72 | | /* |
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' |
76 | | */ |
77 | | ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid) |
78 | 0 | { |
79 | 0 | return ossl_engine_table_select(&pkey_asn1_meth_table, nid, |
80 | 0 | OPENSSL_FILE, OPENSSL_LINE); |
81 | 0 | } |
82 | | |
83 | | /* |
84 | | * Obtains a pkey_asn1_meth implementation from an ENGINE functional |
85 | | * reference |
86 | | */ |
87 | | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) |
88 | 0 | { |
89 | 0 | EVP_PKEY_ASN1_METHOD *ret; |
90 | 0 | ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); |
91 | 0 | if (!fn || !fn(e, &ret, NULL, nid)) { |
92 | 0 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | return ret; |
96 | 0 | } |
97 | | |
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) |
100 | 0 | { |
101 | 0 | return e->pkey_asn1_meths; |
102 | 0 | } |
103 | | |
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) |
106 | 0 | { |
107 | 0 | e->pkey_asn1_meths = f; |
108 | 0 | return 1; |
109 | 0 | } |
110 | | |
111 | | /* |
112 | | * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an |
113 | | * ENGINE is destroyed |
114 | | */ |
115 | | |
116 | | void engine_pkey_asn1_meths_free(ENGINE *e) |
117 | 0 | { |
118 | 0 | int i; |
119 | 0 | EVP_PKEY_ASN1_METHOD *pkm; |
120 | 0 | if (e->pkey_asn1_meths) { |
121 | 0 | const int *pknids; |
122 | 0 | int npknids; |
123 | 0 | npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); |
124 | 0 | for (i = 0; i < npknids; i++) { |
125 | 0 | if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) { |
126 | 0 | EVP_PKEY_asn1_free(pkm); |
127 | 0 | } |
128 | 0 | } |
129 | 0 | } |
130 | 0 | } |
131 | | |
132 | | /* |
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. |
137 | | */ |
138 | | |
139 | | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, |
140 | | const char *str, |
141 | | int len) |
142 | 0 | { |
143 | 0 | int i, nidcount; |
144 | 0 | const int *nids; |
145 | 0 | EVP_PKEY_ASN1_METHOD *ameth; |
146 | 0 | if (!e->pkey_asn1_meths) |
147 | 0 | return NULL; |
148 | 0 | if (len == -1) |
149 | 0 | len = strlen(str); |
150 | 0 | nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); |
151 | 0 | for (i = 0; i < nidcount; i++) { |
152 | 0 | e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); |
153 | 0 | if (ameth != NULL |
154 | 0 | && ((int)strlen(ameth->pem_str) == len) |
155 | 0 | && OPENSSL_strncasecmp(ameth->pem_str, str, len) == 0) |
156 | 0 | return ameth; |
157 | 0 | } |
158 | 0 | return NULL; |
159 | 0 | } |
160 | | |
161 | | typedef struct { |
162 | | ENGINE *e; |
163 | | const EVP_PKEY_ASN1_METHOD *ameth; |
164 | | const char *str; |
165 | | int len; |
166 | | } ENGINE_FIND_STR; |
167 | | |
168 | | static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) |
169 | 0 | { |
170 | 0 | ENGINE_FIND_STR *lk = arg; |
171 | 0 | int i; |
172 | 0 | if (lk->ameth) |
173 | 0 | return; |
174 | 0 | for (i = 0; i < sk_ENGINE_num(sk); i++) { |
175 | 0 | ENGINE *e = sk_ENGINE_value(sk, i); |
176 | 0 | EVP_PKEY_ASN1_METHOD *ameth; |
177 | 0 | e->pkey_asn1_meths(e, &ameth, NULL, nid); |
178 | 0 | if (ameth != NULL |
179 | 0 | && ((int)strlen(ameth->pem_str) == lk->len) |
180 | 0 | && OPENSSL_strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) { |
181 | 0 | lk->e = e; |
182 | 0 | lk->ameth = ameth; |
183 | 0 | return; |
184 | 0 | } |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | | const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, |
189 | | const char *str, |
190 | | int len) |
191 | 0 | { |
192 | 0 | ENGINE_FIND_STR fstr; |
193 | 0 | fstr.e = NULL; |
194 | 0 | fstr.ameth = NULL; |
195 | 0 | fstr.str = str; |
196 | 0 | fstr.len = len; |
197 | |
|
198 | 0 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { |
199 | | /* Maybe this should be raised in do_engine_lock_init() */ |
200 | 0 | ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB); |
201 | 0 | return NULL; |
202 | 0 | } |
203 | | |
204 | 0 | if (!CRYPTO_THREAD_read_lock(global_engine_lock)) |
205 | 0 | return NULL; |
206 | 0 | engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); |
207 | | /* If found obtain a structural reference to engine */ |
208 | 0 | if (fstr.e != NULL) { |
209 | 0 | int ref; |
210 | |
|
211 | 0 | if (!CRYPTO_UP_REF(&fstr.e->struct_ref, &ref)) { |
212 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
213 | 0 | ERR_raise(ERR_LIB_ENGINE, ERR_R_CRYPTO_LIB); |
214 | 0 | return NULL; |
215 | 0 | } |
216 | 0 | ENGINE_REF_PRINT(fstr.e, 0, 1); |
217 | 0 | } |
218 | 0 | *pe = fstr.e; |
219 | 0 | CRYPTO_THREAD_unlock(global_engine_lock); |
220 | 0 | return fstr.ameth; |
221 | 0 | } |