/src/openssl34/crypto/encode_decode/decoder_pkey.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-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 | | #include <openssl/core_names.h> |
11 | | #include <openssl/core_object.h> |
12 | | #include <openssl/provider.h> |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/ui.h> |
15 | | #include <openssl/decoder.h> |
16 | | #include <openssl/safestack.h> |
17 | | #include <openssl/trace.h> |
18 | | #include "crypto/evp.h" |
19 | | #include "crypto/decoder.h" |
20 | | #include "crypto/evp/evp_local.h" |
21 | | #include "crypto/lhash.h" |
22 | | #include "encoder_local.h" |
23 | | #include "internal/namemap.h" |
24 | | #include "internal/sizes.h" |
25 | | |
26 | | int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx, |
27 | | const unsigned char *kstr, |
28 | | size_t klen) |
29 | 0 | { |
30 | 0 | return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen); |
31 | 0 | } |
32 | | |
33 | | int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx, |
34 | | const UI_METHOD *ui_method, |
35 | | void *ui_data) |
36 | 0 | { |
37 | 0 | return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data); |
38 | 0 | } |
39 | | |
40 | | int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx, |
41 | | pem_password_cb *cb, void *cbarg) |
42 | 124k | { |
43 | 124k | return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg); |
44 | 124k | } |
45 | | |
46 | | int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx, |
47 | | OSSL_PASSPHRASE_CALLBACK *cb, |
48 | | void *cbarg) |
49 | 0 | { |
50 | 0 | return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg); |
51 | 0 | } |
52 | | |
53 | | /* |
54 | | * Support for OSSL_DECODER_CTX_new_for_pkey: |
55 | | * The construct data, and collecting keymgmt information for it |
56 | | */ |
57 | | |
58 | | DEFINE_STACK_OF(EVP_KEYMGMT) |
59 | | |
60 | | struct decoder_pkey_data_st { |
61 | | OSSL_LIB_CTX *libctx; |
62 | | char *propq; |
63 | | int selection; |
64 | | |
65 | | STACK_OF(EVP_KEYMGMT) *keymgmts; |
66 | | char *object_type; /* recorded object data type, may be NULL */ |
67 | | void **object; /* Where the result should end up */ |
68 | | }; |
69 | | |
70 | | static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst, |
71 | | const OSSL_PARAM *params, |
72 | | void *construct_data) |
73 | 929k | { |
74 | 929k | struct decoder_pkey_data_st *data = construct_data; |
75 | 929k | OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst); |
76 | 929k | void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst); |
77 | 929k | const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder); |
78 | 929k | EVP_KEYMGMT *keymgmt = NULL; |
79 | 929k | const OSSL_PROVIDER *keymgmt_prov = NULL; |
80 | 929k | int i, end; |
81 | | /* |
82 | | * |object_ref| points to a provider reference to an object, its exact |
83 | | * contents entirely opaque to us, but may be passed to any provider |
84 | | * function that expects this (such as OSSL_FUNC_keymgmt_load(). |
85 | | * |
86 | | * This pointer is considered volatile, i.e. whatever it points at |
87 | | * is assumed to be freed as soon as this function returns. |
88 | | */ |
89 | 929k | void *object_ref = NULL; |
90 | 929k | size_t object_ref_sz = 0; |
91 | 929k | const OSSL_PARAM *p; |
92 | | |
93 | 929k | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE); |
94 | 929k | if (p != NULL) { |
95 | 920k | char *object_type = NULL; |
96 | | |
97 | 920k | if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0)) |
98 | 0 | return 0; |
99 | 920k | OPENSSL_free(data->object_type); |
100 | 920k | data->object_type = object_type; |
101 | 920k | } |
102 | | |
103 | | /* |
104 | | * For stuff that should end up in an EVP_PKEY, we only accept an object |
105 | | * reference for the moment. This enforces that the key data itself |
106 | | * remains with the provider. |
107 | | */ |
108 | 929k | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE); |
109 | 929k | if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING) |
110 | 590k | return 0; |
111 | 339k | object_ref = p->data; |
112 | 339k | object_ref_sz = p->data_size; |
113 | | |
114 | | /* |
115 | | * First, we try to find a keymgmt that comes from the same provider as |
116 | | * the decoder that passed the params. |
117 | | */ |
118 | 339k | end = sk_EVP_KEYMGMT_num(data->keymgmts); |
119 | 758k | for (i = 0; i < end; i++) { |
120 | 758k | keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i); |
121 | 758k | keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt); |
122 | | |
123 | 758k | if (keymgmt_prov == decoder_prov |
124 | 758k | && evp_keymgmt_has_load(keymgmt) |
125 | 758k | && EVP_KEYMGMT_is_a(keymgmt, data->object_type)) |
126 | 339k | break; |
127 | 758k | } |
128 | 339k | if (i < end) { |
129 | | /* To allow it to be freed further down */ |
130 | 339k | if (!EVP_KEYMGMT_up_ref(keymgmt)) |
131 | 0 | return 0; |
132 | 339k | } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx, |
133 | 0 | data->object_type, |
134 | 0 | data->propq)) |
135 | 0 | != NULL) { |
136 | 0 | keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt); |
137 | 0 | } |
138 | | |
139 | 339k | if (keymgmt != NULL) { |
140 | 339k | EVP_PKEY *pkey = NULL; |
141 | 339k | void *keydata = NULL; |
142 | | |
143 | | /* |
144 | | * If the EVP_KEYMGMT and the OSSL_DECODER are from the |
145 | | * same provider, we assume that the KEYMGMT has a key loading |
146 | | * function that can handle the provider reference we hold. |
147 | | * |
148 | | * Otherwise, we export from the decoder and import the |
149 | | * result in the keymgmt. |
150 | | */ |
151 | 339k | if (keymgmt_prov == decoder_prov) { |
152 | 339k | keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz); |
153 | 339k | } else { |
154 | 0 | struct evp_keymgmt_util_try_import_data_st import_data; |
155 | |
|
156 | 0 | import_data.keymgmt = keymgmt; |
157 | 0 | import_data.keydata = NULL; |
158 | 0 | if (data->selection == 0) |
159 | | /* import/export functions do not tolerate 0 selection */ |
160 | 0 | import_data.selection = OSSL_KEYMGMT_SELECT_ALL; |
161 | 0 | else |
162 | 0 | import_data.selection = data->selection; |
163 | | |
164 | | /* |
165 | | * No need to check for errors here, the value of |
166 | | * |import_data.keydata| is as much an indicator. |
167 | | */ |
168 | 0 | (void)decoder->export_object(decoderctx, |
169 | 0 | object_ref, object_ref_sz, |
170 | 0 | &evp_keymgmt_util_try_import, |
171 | 0 | &import_data); |
172 | 0 | keydata = import_data.keydata; |
173 | 0 | import_data.keydata = NULL; |
174 | 0 | } |
175 | | |
176 | 339k | if (keydata != NULL |
177 | 339k | && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL) |
178 | 0 | evp_keymgmt_freedata(keymgmt, keydata); |
179 | | |
180 | 339k | *data->object = pkey; |
181 | | |
182 | | /* |
183 | | * evp_keymgmt_util_make_pkey() increments the reference count when |
184 | | * assigning the EVP_PKEY, so we can free the keymgmt here. |
185 | | */ |
186 | 339k | EVP_KEYMGMT_free(keymgmt); |
187 | 339k | } |
188 | | /* |
189 | | * We successfully looked through, |*ctx->object| determines if we |
190 | | * actually found something. |
191 | | */ |
192 | 339k | return (*data->object != NULL); |
193 | 339k | } |
194 | | |
195 | | static void decoder_clean_pkey_construct_arg(void *construct_data) |
196 | 1.52M | { |
197 | 1.52M | struct decoder_pkey_data_st *data = construct_data; |
198 | | |
199 | 1.52M | if (data != NULL) { |
200 | 1.27M | sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free); |
201 | 1.27M | OPENSSL_free(data->propq); |
202 | 1.27M | OPENSSL_free(data->object_type); |
203 | 1.27M | OPENSSL_free(data); |
204 | 1.27M | } |
205 | 1.52M | } |
206 | | |
207 | | struct collect_data_st { |
208 | | OSSL_LIB_CTX *libctx; |
209 | | OSSL_DECODER_CTX *ctx; |
210 | | |
211 | | const char *keytype; /* the keytype requested, if any */ |
212 | | int keytype_id; /* if keytype_resolved is set, keymgmt name_id; else 0 */ |
213 | | int sm2_id; /* if keytype_resolved is set and EC, SM2 name_id; else 0 */ |
214 | | int total; /* number of matching results */ |
215 | | char error_occurred; |
216 | | char keytype_resolved; |
217 | | |
218 | | STACK_OF(EVP_KEYMGMT) *keymgmts; |
219 | | }; |
220 | | |
221 | | static void collect_decoder_keymgmt(EVP_KEYMGMT *keymgmt, OSSL_DECODER *decoder, |
222 | | void *provctx, struct collect_data_st *data) |
223 | 7.75k | { |
224 | 7.75k | void *decoderctx = NULL; |
225 | 7.75k | OSSL_DECODER_INSTANCE *di = NULL; |
226 | | |
227 | | /* |
228 | | * We already checked the EVP_KEYMGMT is applicable in check_keymgmt so we |
229 | | * don't check it again here. |
230 | | */ |
231 | | |
232 | 7.75k | if (keymgmt->name_id != decoder->base.id) |
233 | | /* Mismatch is not an error, continue. */ |
234 | 7.20k | return; |
235 | | |
236 | 553 | if ((decoderctx = decoder->newctx(provctx)) == NULL) { |
237 | 0 | data->error_occurred = 1; |
238 | 0 | return; |
239 | 0 | } |
240 | | |
241 | 553 | if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) { |
242 | 0 | decoder->freectx(decoderctx); |
243 | 0 | data->error_occurred = 1; |
244 | 0 | return; |
245 | 0 | } |
246 | | |
247 | 553 | OSSL_TRACE_BEGIN(DECODER) |
248 | 0 | { |
249 | 0 | BIO_printf(trc_out, |
250 | 0 | "(ctx %p) Checking out decoder %p:\n" |
251 | 0 | " %s with %s\n", |
252 | 0 | (void *)data->ctx, (void *)decoder, |
253 | 0 | OSSL_DECODER_get0_name(decoder), |
254 | 0 | OSSL_DECODER_get0_properties(decoder)); |
255 | 0 | } |
256 | 553 | OSSL_TRACE_END(DECODER); |
257 | | |
258 | 553 | if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) { |
259 | 0 | ossl_decoder_instance_free(di); |
260 | 0 | data->error_occurred = 1; |
261 | 0 | return; |
262 | 0 | } |
263 | | |
264 | 553 | ++data->total; |
265 | 553 | } |
266 | | |
267 | | static void collect_decoder(OSSL_DECODER *decoder, void *arg) |
268 | 244k | { |
269 | 244k | struct collect_data_st *data = arg; |
270 | 244k | STACK_OF(EVP_KEYMGMT) *keymgmts = data->keymgmts; |
271 | 244k | int i, end_i; |
272 | 244k | EVP_KEYMGMT *keymgmt; |
273 | 244k | const OSSL_PROVIDER *prov; |
274 | 244k | void *provctx; |
275 | | |
276 | 244k | if (data->error_occurred) |
277 | 0 | return; |
278 | | |
279 | 244k | prov = OSSL_DECODER_get0_provider(decoder); |
280 | 244k | provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
281 | | |
282 | | /* |
283 | | * Either the caller didn't give us a selection, or if they did, the decoder |
284 | | * must tell us if it supports that selection to be accepted. If the decoder |
285 | | * doesn't have |does_selection|, it's seen as taking anything. |
286 | | */ |
287 | 244k | if (decoder->does_selection != NULL |
288 | 225k | && !decoder->does_selection(provctx, data->ctx->selection)) |
289 | 121k | return; |
290 | | |
291 | 122k | OSSL_TRACE_BEGIN(DECODER) |
292 | 0 | { |
293 | 0 | BIO_printf(trc_out, |
294 | 0 | "(ctx %p) Checking out decoder %p:\n" |
295 | 0 | " %s with %s\n", |
296 | 0 | (void *)data->ctx, (void *)decoder, |
297 | 0 | OSSL_DECODER_get0_name(decoder), |
298 | 0 | OSSL_DECODER_get0_properties(decoder)); |
299 | 0 | } |
300 | 122k | OSSL_TRACE_END(DECODER); |
301 | | |
302 | 122k | end_i = sk_EVP_KEYMGMT_num(keymgmts); |
303 | 130k | for (i = 0; i < end_i; ++i) { |
304 | 7.75k | keymgmt = sk_EVP_KEYMGMT_value(keymgmts, i); |
305 | | |
306 | 7.75k | collect_decoder_keymgmt(keymgmt, decoder, provctx, data); |
307 | 7.75k | if (data->error_occurred) |
308 | 0 | return; |
309 | 7.75k | } |
310 | 122k | } |
311 | | |
312 | | /* |
313 | | * Is this EVP_KEYMGMT applicable given the key type given in the call to |
314 | | * ossl_decoder_ctx_setup_for_pkey (if any)? |
315 | | */ |
316 | | static int check_keymgmt(EVP_KEYMGMT *keymgmt, struct collect_data_st *data) |
317 | 529k | { |
318 | | /* If no keytype was specified, everything matches. */ |
319 | 529k | if (data->keytype == NULL) |
320 | 624 | return 1; |
321 | | |
322 | 528k | if (!data->keytype_resolved) { |
323 | | /* We haven't cached the IDs from the keytype string yet. */ |
324 | 16.5k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(data->libctx); |
325 | 16.5k | data->keytype_id = ossl_namemap_name2num(namemap, data->keytype); |
326 | | |
327 | | /* |
328 | | * If keytype is a value ambiguously used for both EC and SM2, |
329 | | * collect the ID for SM2 as well. |
330 | | */ |
331 | 16.5k | if (data->keytype_id != 0 |
332 | 958 | && (strcmp(data->keytype, "id-ecPublicKey") == 0 |
333 | 913 | || strcmp(data->keytype, "1.2.840.10045.2.1") == 0)) |
334 | 45 | data->sm2_id = ossl_namemap_name2num(namemap, "SM2"); |
335 | | |
336 | | /* |
337 | | * If keytype_id is zero the name was not found, but we still |
338 | | * set keytype_resolved to avoid trying all this again. |
339 | | */ |
340 | 16.5k | data->keytype_resolved = 1; |
341 | 16.5k | } |
342 | | |
343 | | /* Specified keytype could not be resolved, so nothing matches. */ |
344 | 528k | if (data->keytype_id == 0) |
345 | 494k | return 0; |
346 | | |
347 | | /* Does not match the keytype specified, so skip. */ |
348 | 33.6k | if (keymgmt->name_id != data->keytype_id |
349 | 32.8k | && keymgmt->name_id != data->sm2_id) |
350 | 32.7k | return 0; |
351 | | |
352 | 848 | return 1; |
353 | 33.6k | } |
354 | | |
355 | | static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg) |
356 | 529k | { |
357 | 529k | struct collect_data_st *data = arg; |
358 | | |
359 | 529k | if (!check_keymgmt(keymgmt, data)) |
360 | 527k | return; |
361 | | |
362 | | /* |
363 | | * We have to ref EVP_KEYMGMT here because in the success case, |
364 | | * data->keymgmts is referenced by the constructor we register in the |
365 | | * OSSL_DECODER_CTX. The registered cleanup function |
366 | | * (decoder_clean_pkey_construct_arg) unrefs every element of the stack and |
367 | | * frees it. |
368 | | */ |
369 | 1.47k | if (!EVP_KEYMGMT_up_ref(keymgmt)) |
370 | 0 | return; |
371 | | |
372 | 1.47k | if (sk_EVP_KEYMGMT_push(data->keymgmts, keymgmt) <= 0) { |
373 | 0 | EVP_KEYMGMT_free(keymgmt); |
374 | 0 | data->error_occurred = 1; |
375 | 0 | } |
376 | 1.47k | } |
377 | | |
378 | | /* |
379 | | * This function does the actual binding of decoders to the OSSL_DECODER_CTX. It |
380 | | * searches for decoders matching 'keytype', which is a string like "RSA", "DH", |
381 | | * etc. If 'keytype' is NULL, decoders for all keytypes are bound. |
382 | | */ |
383 | | static int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx, |
384 | | const char *keytype, |
385 | | OSSL_LIB_CTX *libctx, |
386 | | const char *propquery) |
387 | 6.10k | { |
388 | 6.10k | int ok = 0; |
389 | 6.10k | struct decoder_pkey_data_st *process_data = NULL; |
390 | 6.10k | struct collect_data_st collect_data = { NULL }; |
391 | 6.10k | STACK_OF(EVP_KEYMGMT) *keymgmts = NULL; |
392 | | |
393 | 6.10k | OSSL_TRACE_BEGIN(DECODER) |
394 | 0 | { |
395 | 0 | const char *input_type = ctx->start_input_type; |
396 | 0 | const char *input_structure = ctx->input_structure; |
397 | |
|
398 | 0 | BIO_printf(trc_out, |
399 | 0 | "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n", |
400 | 0 | (void *)ctx, |
401 | 0 | keytype != NULL ? keytype : "", |
402 | 0 | keytype != NULL ? " keys" : "keys of any type", |
403 | 0 | input_type != NULL ? " from " : "", |
404 | 0 | input_type != NULL ? input_type : "", |
405 | 0 | input_structure != NULL ? " with " : "", |
406 | 0 | input_structure != NULL ? input_structure : ""); |
407 | 0 | } |
408 | 6.10k | OSSL_TRACE_END(DECODER); |
409 | | |
410 | | /* Allocate data. */ |
411 | 6.10k | if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL) |
412 | 0 | goto err; |
413 | 6.10k | if ((propquery != NULL |
414 | 0 | && (process_data->propq = OPENSSL_strdup(propquery)) == NULL)) |
415 | 0 | goto err; |
416 | | |
417 | | /* Allocate our list of EVP_KEYMGMTs. */ |
418 | 6.10k | keymgmts = sk_EVP_KEYMGMT_new_null(); |
419 | 6.10k | if (keymgmts == NULL) { |
420 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
421 | 0 | goto err; |
422 | 0 | } |
423 | | |
424 | 6.10k | process_data->object = NULL; |
425 | 6.10k | process_data->libctx = libctx; |
426 | 6.10k | process_data->selection = ctx->selection; |
427 | 6.10k | process_data->keymgmts = keymgmts; |
428 | | |
429 | | /* |
430 | | * Enumerate all keymgmts into a stack. |
431 | | * |
432 | | * We could nest EVP_KEYMGMT_do_all_provided inside |
433 | | * OSSL_DECODER_do_all_provided or vice versa but these functions become |
434 | | * bottlenecks if called repeatedly, which is why we collect the |
435 | | * EVP_KEYMGMTs into a stack here and call both functions only once. |
436 | | * |
437 | | * We resolve the keytype string to a name ID so we don't have to resolve it |
438 | | * multiple times, avoiding repeated calls to EVP_KEYMGMT_is_a, which is a |
439 | | * performance bottleneck. However, we do this lazily on the first call to |
440 | | * collect_keymgmt made by EVP_KEYMGMT_do_all_provided, rather than do it |
441 | | * upfront, as this ensures that the names for all loaded providers have |
442 | | * been registered by the time we try to resolve the keytype string. |
443 | | */ |
444 | 6.10k | collect_data.ctx = ctx; |
445 | 6.10k | collect_data.libctx = libctx; |
446 | 6.10k | collect_data.keymgmts = keymgmts; |
447 | 6.10k | collect_data.keytype = keytype; |
448 | 6.10k | EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, &collect_data); |
449 | | |
450 | 6.10k | if (collect_data.error_occurred) |
451 | 0 | goto err; |
452 | | |
453 | | /* Enumerate all matching decoders. */ |
454 | 6.10k | OSSL_DECODER_do_all_provided(libctx, collect_decoder, &collect_data); |
455 | | |
456 | 6.10k | if (collect_data.error_occurred) |
457 | 0 | goto err; |
458 | | |
459 | 6.10k | OSSL_TRACE_BEGIN(DECODER) |
460 | 0 | { |
461 | 0 | BIO_printf(trc_out, |
462 | 0 | "(ctx %p) Got %d decoders producing keys\n", |
463 | 0 | (void *)ctx, collect_data.total); |
464 | 0 | } |
465 | 6.10k | OSSL_TRACE_END(DECODER); |
466 | | |
467 | | /* |
468 | | * Finish initializing the decoder context. If one or more decoders matched |
469 | | * above then the number of decoders attached to the OSSL_DECODER_CTX will |
470 | | * be nonzero. Else nothing was found and we do nothing. |
471 | | */ |
472 | 6.10k | if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) { |
473 | 161 | if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey) |
474 | 161 | || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data) |
475 | 161 | || !OSSL_DECODER_CTX_set_cleanup(ctx, |
476 | 161 | decoder_clean_pkey_construct_arg)) |
477 | 0 | goto err; |
478 | | |
479 | 161 | process_data = NULL; /* Avoid it being freed */ |
480 | 161 | } |
481 | | |
482 | 6.10k | ok = 1; |
483 | 6.10k | err: |
484 | 6.10k | decoder_clean_pkey_construct_arg(process_data); |
485 | 6.10k | return ok; |
486 | 6.10k | } |
487 | | |
488 | | /* Only const here because deep_copy requires it */ |
489 | | static EVP_KEYMGMT *keymgmt_dup(const EVP_KEYMGMT *keymgmt) |
490 | 7.81M | { |
491 | 7.81M | if (!EVP_KEYMGMT_up_ref((EVP_KEYMGMT *)keymgmt)) |
492 | 0 | return NULL; |
493 | | |
494 | 7.81M | return (EVP_KEYMGMT *)keymgmt; |
495 | 7.81M | } |
496 | | |
497 | | /* |
498 | | * Duplicates a template OSSL_DECODER_CTX that has been setup for an EVP_PKEY |
499 | | * operation and sets up the duplicate for a new operation. |
500 | | * It does not duplicate the pwdata on the assumption that this does not form |
501 | | * part of the template. That is set up later. |
502 | | */ |
503 | | static OSSL_DECODER_CTX * |
504 | | ossl_decoder_ctx_for_pkey_dup(OSSL_DECODER_CTX *src, |
505 | | EVP_PKEY **pkey, |
506 | | const char *input_type, |
507 | | const char *input_structure) |
508 | 456k | { |
509 | 456k | OSSL_DECODER_CTX *dest; |
510 | 456k | struct decoder_pkey_data_st *process_data_src, *process_data_dest = NULL; |
511 | | |
512 | 456k | if (src == NULL) |
513 | 0 | return NULL; |
514 | | |
515 | 456k | if ((dest = OSSL_DECODER_CTX_new()) == NULL) { |
516 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
517 | 0 | return NULL; |
518 | 0 | } |
519 | | |
520 | 456k | if (!OSSL_DECODER_CTX_set_input_type(dest, input_type) |
521 | 456k | || !OSSL_DECODER_CTX_set_input_structure(dest, input_structure)) { |
522 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
523 | 0 | goto err; |
524 | 0 | } |
525 | 456k | dest->selection = src->selection; |
526 | | |
527 | 456k | if (src->decoder_insts != NULL) { |
528 | 422k | dest->decoder_insts |
529 | 422k | = sk_OSSL_DECODER_INSTANCE_deep_copy(src->decoder_insts, |
530 | 422k | ossl_decoder_instance_dup, |
531 | 422k | ossl_decoder_instance_free); |
532 | 422k | if (dest->decoder_insts == NULL) { |
533 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
534 | 0 | goto err; |
535 | 0 | } |
536 | 422k | } |
537 | | |
538 | 456k | if (!OSSL_DECODER_CTX_set_construct(dest, |
539 | 456k | OSSL_DECODER_CTX_get_construct(src))) { |
540 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
541 | 0 | goto err; |
542 | 0 | } |
543 | | |
544 | 456k | process_data_src = OSSL_DECODER_CTX_get_construct_data(src); |
545 | 456k | if (process_data_src != NULL) { |
546 | 422k | process_data_dest = OPENSSL_zalloc(sizeof(*process_data_dest)); |
547 | 422k | if (process_data_dest == NULL) { |
548 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
549 | 0 | goto err; |
550 | 0 | } |
551 | 422k | if (process_data_src->propq != NULL) { |
552 | 0 | process_data_dest->propq = OPENSSL_strdup(process_data_src->propq); |
553 | 0 | if (process_data_dest->propq == NULL) { |
554 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
555 | 0 | goto err; |
556 | 0 | } |
557 | 0 | } |
558 | | |
559 | 422k | if (process_data_src->keymgmts != NULL) { |
560 | 422k | process_data_dest->keymgmts |
561 | 422k | = sk_EVP_KEYMGMT_deep_copy(process_data_src->keymgmts, |
562 | 422k | keymgmt_dup, |
563 | 422k | EVP_KEYMGMT_free); |
564 | 422k | if (process_data_dest->keymgmts == NULL) { |
565 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_EVP_LIB); |
566 | 0 | goto err; |
567 | 0 | } |
568 | 422k | } |
569 | | |
570 | 422k | process_data_dest->object = (void **)pkey; |
571 | 422k | process_data_dest->libctx = process_data_src->libctx; |
572 | 422k | process_data_dest->selection = process_data_src->selection; |
573 | 422k | if (!OSSL_DECODER_CTX_set_construct_data(dest, process_data_dest)) { |
574 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
575 | 0 | goto err; |
576 | 0 | } |
577 | 422k | process_data_dest = NULL; |
578 | 422k | } |
579 | | |
580 | 456k | if (!OSSL_DECODER_CTX_set_cleanup(dest, |
581 | 456k | OSSL_DECODER_CTX_get_cleanup(src))) { |
582 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
583 | 0 | goto err; |
584 | 0 | } |
585 | | |
586 | 456k | return dest; |
587 | 0 | err: |
588 | 0 | if (process_data_dest != NULL) { |
589 | 0 | OPENSSL_free(process_data_dest->propq); |
590 | 0 | sk_EVP_KEYMGMT_pop_free(process_data_dest->keymgmts, EVP_KEYMGMT_free); |
591 | 0 | OPENSSL_free(process_data_dest); |
592 | 0 | } |
593 | 0 | OSSL_DECODER_CTX_free(dest); |
594 | 0 | return NULL; |
595 | 456k | } |
596 | | |
597 | | typedef struct { |
598 | | char *input_type; |
599 | | char *input_structure; |
600 | | char *keytype; |
601 | | int selection; |
602 | | char *propquery; |
603 | | OSSL_DECODER_CTX *template; |
604 | | } DECODER_CACHE_ENTRY; |
605 | | |
606 | | DEFINE_LHASH_OF_EX(DECODER_CACHE_ENTRY); |
607 | | |
608 | | typedef struct { |
609 | | CRYPTO_RWLOCK *lock; |
610 | | LHASH_OF(DECODER_CACHE_ENTRY) *hashtable; |
611 | | } DECODER_CACHE; |
612 | | |
613 | | static void decoder_cache_entry_free(DECODER_CACHE_ENTRY *entry) |
614 | 12.6k | { |
615 | 12.6k | if (entry == NULL) |
616 | 0 | return; |
617 | 12.6k | OPENSSL_free(entry->input_type); |
618 | 12.6k | OPENSSL_free(entry->input_structure); |
619 | 12.6k | OPENSSL_free(entry->keytype); |
620 | 12.6k | OPENSSL_free(entry->propquery); |
621 | 12.6k | OSSL_DECODER_CTX_free(entry->template); |
622 | 12.6k | OPENSSL_free(entry); |
623 | 12.6k | } |
624 | | |
625 | | static unsigned long decoder_cache_entry_hash(const DECODER_CACHE_ENTRY *cache) |
626 | 1.11M | { |
627 | 1.11M | unsigned long hash = 17; |
628 | | |
629 | 1.11M | hash = (hash * 23) |
630 | 1.11M | + (cache->propquery == NULL |
631 | 1.11M | ? 0 |
632 | 1.11M | : ossl_lh_strcasehash(cache->propquery)); |
633 | 1.11M | hash = (hash * 23) |
634 | 1.11M | + (cache->input_structure == NULL |
635 | 1.11M | ? 0 |
636 | 1.11M | : ossl_lh_strcasehash(cache->input_structure)); |
637 | 1.11M | hash = (hash * 23) |
638 | 1.11M | + (cache->input_type == NULL |
639 | 1.11M | ? 0 |
640 | 1.11M | : ossl_lh_strcasehash(cache->input_type)); |
641 | 1.11M | hash = (hash * 23) |
642 | 1.11M | + (cache->keytype == NULL |
643 | 1.11M | ? 0 |
644 | 1.11M | : ossl_lh_strcasehash(cache->keytype)); |
645 | | |
646 | 1.11M | hash ^= cache->selection; |
647 | | |
648 | 1.11M | return hash; |
649 | 1.11M | } |
650 | | |
651 | | static ossl_inline int nullstrcmp(const char *a, const char *b, int casecmp) |
652 | 4.24M | { |
653 | 4.24M | if (a == NULL || b == NULL) { |
654 | 1.46M | if (a == NULL) { |
655 | 1.46M | if (b == NULL) |
656 | 1.46M | return 0; |
657 | 0 | else |
658 | 0 | return 1; |
659 | 1.46M | } else { |
660 | 0 | return -1; |
661 | 0 | } |
662 | 2.77M | } else { |
663 | 2.77M | if (casecmp) |
664 | 2.77M | return OPENSSL_strcasecmp(a, b); |
665 | 0 | else |
666 | 0 | return strcmp(a, b); |
667 | 2.77M | } |
668 | 4.24M | } |
669 | | |
670 | | static int decoder_cache_entry_cmp(const DECODER_CACHE_ENTRY *a, |
671 | | const DECODER_CACHE_ENTRY *b) |
672 | 1.06M | { |
673 | 1.06M | int cmp; |
674 | | |
675 | 1.06M | if (a->selection != b->selection) |
676 | 0 | return (a->selection < b->selection) ? -1 : 1; |
677 | | |
678 | 1.06M | cmp = nullstrcmp(a->keytype, b->keytype, 1); |
679 | 1.06M | if (cmp != 0) |
680 | 14 | return cmp; |
681 | | |
682 | 1.06M | cmp = nullstrcmp(a->input_type, b->input_type, 1); |
683 | 1.06M | if (cmp != 0) |
684 | 0 | return cmp; |
685 | | |
686 | 1.06M | cmp = nullstrcmp(a->input_structure, b->input_structure, 1); |
687 | 1.06M | if (cmp != 0) |
688 | 0 | return cmp; |
689 | | |
690 | 1.06M | cmp = nullstrcmp(a->propquery, b->propquery, 0); |
691 | | |
692 | 1.06M | return cmp; |
693 | 1.06M | } |
694 | | |
695 | | void *ossl_decoder_cache_new(OSSL_LIB_CTX *ctx) |
696 | 378 | { |
697 | 378 | DECODER_CACHE *cache = OPENSSL_malloc(sizeof(*cache)); |
698 | | |
699 | 378 | if (cache == NULL) |
700 | 0 | return NULL; |
701 | | |
702 | 378 | cache->lock = CRYPTO_THREAD_lock_new(); |
703 | 378 | if (cache->lock == NULL) { |
704 | 0 | OPENSSL_free(cache); |
705 | 0 | return NULL; |
706 | 0 | } |
707 | 378 | cache->hashtable = lh_DECODER_CACHE_ENTRY_new(decoder_cache_entry_hash, |
708 | 378 | decoder_cache_entry_cmp); |
709 | 378 | if (cache->hashtable == NULL) { |
710 | 0 | CRYPTO_THREAD_lock_free(cache->lock); |
711 | 0 | OPENSSL_free(cache); |
712 | 0 | return NULL; |
713 | 0 | } |
714 | | |
715 | 378 | return cache; |
716 | 378 | } |
717 | | |
718 | | void ossl_decoder_cache_free(void *vcache) |
719 | 196 | { |
720 | 196 | DECODER_CACHE *cache = (DECODER_CACHE *)vcache; |
721 | | |
722 | 196 | lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free); |
723 | 196 | lh_DECODER_CACHE_ENTRY_free(cache->hashtable); |
724 | 196 | CRYPTO_THREAD_lock_free(cache->lock); |
725 | 196 | OPENSSL_free(cache); |
726 | 196 | } |
727 | | |
728 | | /* |
729 | | * Called whenever a provider gets activated/deactivated. In that case the |
730 | | * decoders that are available might change so we flush our cache. |
731 | | */ |
732 | | int ossl_decoder_cache_flush(OSSL_LIB_CTX *libctx) |
733 | 291 | { |
734 | 291 | DECODER_CACHE *cache |
735 | 291 | = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX); |
736 | | |
737 | 291 | if (cache == NULL) |
738 | 165 | return 0; |
739 | | |
740 | 126 | if (!CRYPTO_THREAD_write_lock(cache->lock)) { |
741 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
742 | 0 | return 0; |
743 | 0 | } |
744 | | |
745 | 126 | lh_DECODER_CACHE_ENTRY_doall(cache->hashtable, decoder_cache_entry_free); |
746 | 126 | lh_DECODER_CACHE_ENTRY_flush(cache->hashtable); |
747 | | |
748 | 126 | CRYPTO_THREAD_unlock(cache->lock); |
749 | 126 | return 1; |
750 | 126 | } |
751 | | |
752 | | OSSL_DECODER_CTX * |
753 | | OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey, |
754 | | const char *input_type, |
755 | | const char *input_structure, |
756 | | const char *keytype, int selection, |
757 | | OSSL_LIB_CTX *libctx, const char *propquery) |
758 | 456k | { |
759 | 456k | OSSL_DECODER_CTX *ctx = NULL; |
760 | 456k | OSSL_PARAM decoder_params[] = { |
761 | 456k | OSSL_PARAM_END, |
762 | 456k | OSSL_PARAM_END |
763 | 456k | }; |
764 | 456k | DECODER_CACHE *cache |
765 | 456k | = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_CACHE_INDEX); |
766 | 456k | DECODER_CACHE_ENTRY cacheent, *res, *newcache = NULL; |
767 | | |
768 | 456k | if (cache == NULL) { |
769 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
770 | 0 | return NULL; |
771 | 0 | } |
772 | 456k | if (propquery != NULL) |
773 | 0 | decoder_params[0] = OSSL_PARAM_construct_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, |
774 | 0 | (char *)propquery, 0); |
775 | | |
776 | | /* It is safe to cast away the const here */ |
777 | 456k | cacheent.input_type = (char *)input_type; |
778 | 456k | cacheent.input_structure = (char *)input_structure; |
779 | 456k | cacheent.keytype = (char *)keytype; |
780 | 456k | cacheent.selection = selection; |
781 | 456k | cacheent.propquery = (char *)propquery; |
782 | | |
783 | 456k | if (!CRYPTO_THREAD_read_lock(cache->lock)) { |
784 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
785 | 0 | return NULL; |
786 | 0 | } |
787 | | |
788 | | /* First see if we have a template OSSL_DECODER_CTX */ |
789 | 456k | res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent); |
790 | | |
791 | 456k | if (res == NULL) { |
792 | | /* |
793 | | * There is no template so we will have to construct one. This will be |
794 | | * time consuming so release the lock and we will later upgrade it to a |
795 | | * write lock. |
796 | | */ |
797 | 6.10k | CRYPTO_THREAD_unlock(cache->lock); |
798 | | |
799 | 6.10k | if ((ctx = OSSL_DECODER_CTX_new()) == NULL) { |
800 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
801 | 0 | return NULL; |
802 | 0 | } |
803 | | |
804 | 6.10k | OSSL_TRACE_BEGIN(DECODER) |
805 | 0 | { |
806 | 0 | BIO_printf(trc_out, |
807 | 0 | "(ctx %p) Looking for %s decoders with selection %d\n", |
808 | 0 | (void *)ctx, keytype, selection); |
809 | 0 | BIO_printf(trc_out, " input type: %s, input structure: %s\n", |
810 | 0 | input_type, input_structure); |
811 | 0 | } |
812 | 6.10k | OSSL_TRACE_END(DECODER); |
813 | | |
814 | 6.10k | if (OSSL_DECODER_CTX_set_input_type(ctx, input_type) |
815 | 6.10k | && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure) |
816 | 6.10k | && OSSL_DECODER_CTX_set_selection(ctx, selection) |
817 | 6.10k | && ossl_decoder_ctx_setup_for_pkey(ctx, keytype, libctx, propquery) |
818 | 6.10k | && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery) |
819 | 6.10k | && (propquery == NULL |
820 | 6.10k | || OSSL_DECODER_CTX_set_params(ctx, decoder_params))) { |
821 | 6.10k | OSSL_TRACE_BEGIN(DECODER) |
822 | 0 | { |
823 | 0 | BIO_printf(trc_out, "(ctx %p) Got %d decoders\n", |
824 | 0 | (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx)); |
825 | 0 | } |
826 | 6.10k | OSSL_TRACE_END(DECODER); |
827 | 6.10k | } else { |
828 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_OSSL_DECODER_LIB); |
829 | 0 | OSSL_DECODER_CTX_free(ctx); |
830 | 0 | return NULL; |
831 | 0 | } |
832 | | |
833 | 6.10k | newcache = OPENSSL_zalloc(sizeof(*newcache)); |
834 | 6.10k | if (newcache == NULL) { |
835 | 0 | OSSL_DECODER_CTX_free(ctx); |
836 | 0 | return NULL; |
837 | 0 | } |
838 | | |
839 | 6.10k | if (input_type != NULL) { |
840 | 6.10k | newcache->input_type = OPENSSL_strdup(input_type); |
841 | 6.10k | if (newcache->input_type == NULL) |
842 | 0 | goto err; |
843 | 6.10k | } |
844 | 6.10k | if (input_structure != NULL) { |
845 | 6.10k | newcache->input_structure = OPENSSL_strdup(input_structure); |
846 | 6.10k | if (newcache->input_structure == NULL) |
847 | 0 | goto err; |
848 | 6.10k | } |
849 | 6.10k | if (keytype != NULL) { |
850 | 6.09k | newcache->keytype = OPENSSL_strdup(keytype); |
851 | 6.09k | if (newcache->keytype == NULL) |
852 | 0 | goto err; |
853 | 6.09k | } |
854 | 6.10k | if (propquery != NULL) { |
855 | 0 | newcache->propquery = OPENSSL_strdup(propquery); |
856 | 0 | if (newcache->propquery == NULL) |
857 | 0 | goto err; |
858 | 0 | } |
859 | 6.10k | newcache->selection = selection; |
860 | 6.10k | newcache->template = ctx; |
861 | | |
862 | 6.10k | if (!CRYPTO_THREAD_write_lock(cache->lock)) { |
863 | 0 | ctx = NULL; |
864 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
865 | 0 | goto err; |
866 | 0 | } |
867 | 6.10k | res = lh_DECODER_CACHE_ENTRY_retrieve(cache->hashtable, &cacheent); |
868 | 6.10k | if (res == NULL) { |
869 | 6.10k | (void)lh_DECODER_CACHE_ENTRY_insert(cache->hashtable, newcache); |
870 | 6.10k | if (lh_DECODER_CACHE_ENTRY_error(cache->hashtable)) { |
871 | 0 | ctx = NULL; |
872 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB); |
873 | 0 | goto err; |
874 | 0 | } |
875 | 6.10k | } else { |
876 | | /* |
877 | | * We raced with another thread to construct this and lost. Free |
878 | | * what we just created and use the entry from the hashtable instead |
879 | | */ |
880 | 0 | decoder_cache_entry_free(newcache); |
881 | 0 | ctx = res->template; |
882 | 0 | } |
883 | 450k | } else { |
884 | 450k | ctx = res->template; |
885 | 450k | } |
886 | | |
887 | 456k | ctx = ossl_decoder_ctx_for_pkey_dup(ctx, pkey, input_type, input_structure); |
888 | 456k | CRYPTO_THREAD_unlock(cache->lock); |
889 | | |
890 | 456k | return ctx; |
891 | 0 | err: |
892 | 0 | decoder_cache_entry_free(newcache); |
893 | 0 | OSSL_DECODER_CTX_free(ctx); |
894 | | return NULL; |
895 | 456k | } |