]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
e39e295e | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
de9fcfe3 | 3 | * |
3c120f91 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
de9fcfe3 DSH |
8 | */ |
9 | ||
e4468e6d P |
10 | /* We need to use some engine deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
677963e5 | 13 | #include "e_os.h" |
706457b7 | 14 | #include "eng_local.h" |
1e26a8ba | 15 | #include <openssl/evp.h> |
25f2138b | 16 | #include "crypto/asn1.h" |
de9fcfe3 | 17 | |
0f113f3e MC |
18 | /* |
19 | * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the | |
01b8b3c7 DSH |
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 | |
0f113f3e MC |
22 | * 'nid'. |
23 | */ | |
de9fcfe3 DSH |
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) | |
0f113f3e MC |
29 | { |
30 | engine_table_unregister(&pkey_asn1_meth_table, e); | |
31 | } | |
de9fcfe3 DSH |
32 | |
33 | static void engine_unregister_all_pkey_asn1_meths(void) | |
0f113f3e MC |
34 | { |
35 | engine_table_cleanup(&pkey_asn1_meth_table); | |
36 | } | |
de9fcfe3 DSH |
37 | |
38 | int ENGINE_register_pkey_asn1_meths(ENGINE *e) | |
0f113f3e MC |
39 | { |
40 | if (e->pkey_asn1_meths) { | |
41 | const int *nids; | |
42 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | |
43 | if (num_nids > 0) | |
44 | return engine_table_register(&pkey_asn1_meth_table, | |
45 | engine_unregister_all_pkey_asn1_meths, | |
46 | e, nids, num_nids, 0); | |
47 | } | |
48 | return 1; | |
49 | } | |
de9fcfe3 | 50 | |
777c47ac | 51 | void ENGINE_register_all_pkey_asn1_meths(void) |
0f113f3e MC |
52 | { |
53 | ENGINE *e; | |
de9fcfe3 | 54 | |
0f113f3e MC |
55 | for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) |
56 | ENGINE_register_pkey_asn1_meths(e); | |
57 | } | |
de9fcfe3 DSH |
58 | |
59 | int ENGINE_set_default_pkey_asn1_meths(ENGINE *e) | |
0f113f3e MC |
60 | { |
61 | if (e->pkey_asn1_meths) { | |
62 | const int *nids; | |
63 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | |
64 | if (num_nids > 0) | |
65 | return engine_table_register(&pkey_asn1_meth_table, | |
66 | engine_unregister_all_pkey_asn1_meths, | |
67 | e, nids, num_nids, 1); | |
68 | } | |
69 | return 1; | |
70 | } | |
71 | ||
72 | /* | |
73 | * Exposed API function to get a functional reference from the implementation | |
de9fcfe3 | 74 | * table (ie. try to get a functional reference from the tabled structural |
0f113f3e MC |
75 | * references) for a given pkey_asn1_meth 'nid' |
76 | */ | |
de9fcfe3 | 77 | ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid) |
0f113f3e MC |
78 | { |
79 | return engine_table_select(&pkey_asn1_meth_table, nid); | |
80 | } | |
de9fcfe3 | 81 | |
0f113f3e MC |
82 | /* |
83 | * Obtains a pkey_asn1_meth implementation from an ENGINE functional | |
84 | * reference | |
85 | */ | |
de9fcfe3 | 86 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) |
0f113f3e MC |
87 | { |
88 | EVP_PKEY_ASN1_METHOD *ret; | |
89 | ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); | |
90 | if (!fn || !fn(e, &ret, NULL, nid)) { | |
9311d0c4 | 91 | ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); |
0f113f3e MC |
92 | return NULL; |
93 | } | |
94 | return ret; | |
95 | } | |
de9fcfe3 DSH |
96 | |
97 | /* Gets the pkey_asn1_meth callback from an ENGINE structure */ | |
98 | ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e) | |
0f113f3e MC |
99 | { |
100 | return e->pkey_asn1_meths; | |
101 | } | |
de9fcfe3 DSH |
102 | |
103 | /* Sets the pkey_asn1_meth callback in an ENGINE structure */ | |
104 | int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f) | |
0f113f3e MC |
105 | { |
106 | e->pkey_asn1_meths = f; | |
107 | return 1; | |
108 | } | |
de9fcfe3 | 109 | |
0f113f3e MC |
110 | /* |
111 | * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an | |
de9fcfe3 DSH |
112 | * ENGINE is destroyed |
113 | */ | |
114 | ||
115 | void engine_pkey_asn1_meths_free(ENGINE *e) | |
0f113f3e MC |
116 | { |
117 | int i; | |
118 | EVP_PKEY_ASN1_METHOD *pkm; | |
119 | if (e->pkey_asn1_meths) { | |
120 | const int *pknids; | |
121 | int npknids; | |
122 | npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); | |
123 | for (i = 0; i < npknids; i++) { | |
124 | if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) { | |
125 | EVP_PKEY_asn1_free(pkm); | |
126 | } | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | /* | |
132 | * Find a method based on a string. This does a linear search through all | |
133 | * implemented algorithms. This is OK in practice because only a small number | |
134 | * of algorithms are likely to be implemented in an engine and it is not used | |
135 | * for speed critical operations. | |
01b8b3c7 DSH |
136 | */ |
137 | ||
138 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, | |
0f113f3e MC |
139 | const char *str, |
140 | int len) | |
141 | { | |
142 | int i, nidcount; | |
143 | const int *nids; | |
144 | EVP_PKEY_ASN1_METHOD *ameth; | |
145 | if (!e->pkey_asn1_meths) | |
146 | return NULL; | |
147 | if (len == -1) | |
148 | len = strlen(str); | |
149 | nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); | |
150 | for (i = 0; i < nidcount; i++) { | |
151 | e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); | |
9bada854 P |
152 | if (ameth != NULL |
153 | && ((int)strlen(ameth->pem_str) == len) | |
86885c28 | 154 | && strncasecmp(ameth->pem_str, str, len) == 0) |
0f113f3e MC |
155 | return ameth; |
156 | } | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | typedef struct { | |
161 | ENGINE *e; | |
162 | const EVP_PKEY_ASN1_METHOD *ameth; | |
163 | const char *str; | |
164 | int len; | |
165 | } ENGINE_FIND_STR; | |
2f0550c4 DSH |
166 | |
167 | static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) | |
0f113f3e MC |
168 | { |
169 | ENGINE_FIND_STR *lk = arg; | |
170 | int i; | |
171 | if (lk->ameth) | |
172 | return; | |
173 | for (i = 0; i < sk_ENGINE_num(sk); i++) { | |
174 | ENGINE *e = sk_ENGINE_value(sk, i); | |
175 | EVP_PKEY_ASN1_METHOD *ameth; | |
176 | e->pkey_asn1_meths(e, &ameth, NULL, nid); | |
5eb77432 RL |
177 | if (ameth != NULL |
178 | && ((int)strlen(ameth->pem_str) == lk->len) | |
86885c28 | 179 | && strncasecmp(ameth->pem_str, lk->str, lk->len) == 0) { |
0f113f3e MC |
180 | lk->e = e; |
181 | lk->ameth = ameth; | |
182 | return; | |
183 | } | |
184 | } | |
185 | } | |
2f0550c4 DSH |
186 | |
187 | const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, | |
0f113f3e MC |
188 | const char *str, |
189 | int len) | |
190 | { | |
191 | ENGINE_FIND_STR fstr; | |
192 | fstr.e = NULL; | |
193 | fstr.ameth = NULL; | |
194 | fstr.str = str; | |
195 | fstr.len = len; | |
40e068d5 | 196 | |
c2e4e5d2 | 197 | if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)) { |
9311d0c4 | 198 | ERR_raise(ERR_LIB_ENGINE, ERR_R_MALLOC_FAILURE); |
c2e4e5d2 RL |
199 | return NULL; |
200 | } | |
201 | ||
40e068d5 | 202 | CRYPTO_THREAD_write_lock(global_engine_lock); |
0f113f3e MC |
203 | engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); |
204 | /* If found obtain a structural reference to engine */ | |
205 | if (fstr.e) { | |
206 | fstr.e->struct_ref++; | |
43d6702d | 207 | engine_ref_debug(fstr.e, 0, 1); |
0f113f3e MC |
208 | } |
209 | *pe = fstr.e; | |
40e068d5 | 210 | CRYPTO_THREAD_unlock(global_engine_lock); |
0f113f3e MC |
211 | return fstr.ameth; |
212 | } |