/src/openssl30/crypto/encode_decode/decoder_pkey.c
Line | Count | Source (jump to first uncovered line) |
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 "encoder_local.h" |
21 | | |
22 | | int OSSL_DECODER_CTX_set_passphrase(OSSL_DECODER_CTX *ctx, |
23 | | const unsigned char *kstr, |
24 | | size_t klen) |
25 | 0 | { |
26 | 0 | return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen); |
27 | 0 | } |
28 | | |
29 | | int OSSL_DECODER_CTX_set_passphrase_ui(OSSL_DECODER_CTX *ctx, |
30 | | const UI_METHOD *ui_method, |
31 | | void *ui_data) |
32 | 0 | { |
33 | 0 | return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data); |
34 | 0 | } |
35 | | |
36 | | int OSSL_DECODER_CTX_set_pem_password_cb(OSSL_DECODER_CTX *ctx, |
37 | | pem_password_cb *cb, void *cbarg) |
38 | 55.4k | { |
39 | 55.4k | return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg); |
40 | 55.4k | } |
41 | | |
42 | | int OSSL_DECODER_CTX_set_passphrase_cb(OSSL_DECODER_CTX *ctx, |
43 | | OSSL_PASSPHRASE_CALLBACK *cb, |
44 | | void *cbarg) |
45 | 0 | { |
46 | 0 | return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg); |
47 | 0 | } |
48 | | |
49 | | /* |
50 | | * Support for OSSL_DECODER_CTX_new_for_pkey: |
51 | | * The construct data, and collecting keymgmt information for it |
52 | | */ |
53 | | |
54 | | DEFINE_STACK_OF(EVP_KEYMGMT) |
55 | | |
56 | | struct decoder_pkey_data_st { |
57 | | OSSL_LIB_CTX *libctx; |
58 | | char *propq; |
59 | | int selection; |
60 | | |
61 | | STACK_OF(EVP_KEYMGMT) *keymgmts; |
62 | | char *object_type; /* recorded object data type, may be NULL */ |
63 | | void **object; /* Where the result should end up */ |
64 | | }; |
65 | | |
66 | | static int decoder_construct_pkey(OSSL_DECODER_INSTANCE *decoder_inst, |
67 | | const OSSL_PARAM *params, |
68 | | void *construct_data) |
69 | 960k | { |
70 | 960k | struct decoder_pkey_data_st *data = construct_data; |
71 | 960k | OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst); |
72 | 960k | void *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst); |
73 | 960k | const OSSL_PROVIDER *decoder_prov = OSSL_DECODER_get0_provider(decoder); |
74 | 960k | EVP_KEYMGMT *keymgmt = NULL; |
75 | 960k | const OSSL_PROVIDER *keymgmt_prov = NULL; |
76 | 960k | int i, end; |
77 | | /* |
78 | | * |object_ref| points to a provider reference to an object, its exact |
79 | | * contents entirely opaque to us, but may be passed to any provider |
80 | | * function that expects this (such as OSSL_FUNC_keymgmt_load(). |
81 | | * |
82 | | * This pointer is considered volatile, i.e. whatever it points at |
83 | | * is assumed to be freed as soon as this function returns. |
84 | | */ |
85 | 960k | void *object_ref = NULL; |
86 | 960k | size_t object_ref_sz = 0; |
87 | 960k | const OSSL_PARAM *p; |
88 | | |
89 | 960k | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_DATA_TYPE); |
90 | 960k | if (p != NULL) { |
91 | 960k | char *object_type = NULL; |
92 | | |
93 | 960k | if (!OSSL_PARAM_get_utf8_string(p, &object_type, 0)) |
94 | 0 | return 0; |
95 | 960k | OPENSSL_free(data->object_type); |
96 | 960k | data->object_type = object_type; |
97 | 960k | } |
98 | | |
99 | | /* |
100 | | * For stuff that should end up in an EVP_PKEY, we only accept an object |
101 | | * reference for the moment. This enforces that the key data itself |
102 | | * remains with the provider. |
103 | | */ |
104 | 960k | p = OSSL_PARAM_locate_const(params, OSSL_OBJECT_PARAM_REFERENCE); |
105 | 960k | if (p == NULL || p->data_type != OSSL_PARAM_OCTET_STRING) |
106 | 618k | return 0; |
107 | 342k | object_ref = p->data; |
108 | 342k | object_ref_sz = p->data_size; |
109 | | |
110 | | /* |
111 | | * First, we try to find a keymgmt that comes from the same provider as |
112 | | * the decoder that passed the params. |
113 | | */ |
114 | 342k | end = sk_EVP_KEYMGMT_num(data->keymgmts); |
115 | 712k | for (i = 0; i < end; i++) { |
116 | 712k | keymgmt = sk_EVP_KEYMGMT_value(data->keymgmts, i); |
117 | 712k | keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt); |
118 | | |
119 | 712k | if (keymgmt_prov == decoder_prov |
120 | 712k | && evp_keymgmt_has_load(keymgmt) |
121 | 712k | && EVP_KEYMGMT_is_a(keymgmt, data->object_type)) |
122 | 342k | break; |
123 | 712k | } |
124 | 342k | if (i < end) { |
125 | | /* To allow it to be freed further down */ |
126 | 342k | if (!EVP_KEYMGMT_up_ref(keymgmt)) |
127 | 0 | return 0; |
128 | 342k | } else if ((keymgmt = EVP_KEYMGMT_fetch(data->libctx, |
129 | 0 | data->object_type, |
130 | 0 | data->propq)) != NULL) { |
131 | 0 | keymgmt_prov = EVP_KEYMGMT_get0_provider(keymgmt); |
132 | 0 | } |
133 | | |
134 | 342k | if (keymgmt != NULL) { |
135 | 342k | EVP_PKEY *pkey = NULL; |
136 | 342k | void *keydata = NULL; |
137 | | |
138 | | /* |
139 | | * If the EVP_KEYMGMT and the OSSL_DECODER are from the |
140 | | * same provider, we assume that the KEYMGMT has a key loading |
141 | | * function that can handle the provider reference we hold. |
142 | | * |
143 | | * Otherwise, we export from the decoder and import the |
144 | | * result in the keymgmt. |
145 | | */ |
146 | 342k | if (keymgmt_prov == decoder_prov) { |
147 | 342k | keydata = evp_keymgmt_load(keymgmt, object_ref, object_ref_sz); |
148 | 342k | } else { |
149 | 0 | struct evp_keymgmt_util_try_import_data_st import_data; |
150 | |
|
151 | 0 | import_data.keymgmt = keymgmt; |
152 | 0 | import_data.keydata = NULL; |
153 | 0 | if (data->selection == 0) |
154 | | /* import/export functions do not tolerate 0 selection */ |
155 | 0 | import_data.selection = OSSL_KEYMGMT_SELECT_ALL; |
156 | 0 | else |
157 | 0 | import_data.selection = data->selection; |
158 | | |
159 | | /* |
160 | | * No need to check for errors here, the value of |
161 | | * |import_data.keydata| is as much an indicator. |
162 | | */ |
163 | 0 | (void)decoder->export_object(decoderctx, |
164 | 0 | object_ref, object_ref_sz, |
165 | 0 | &evp_keymgmt_util_try_import, |
166 | 0 | &import_data); |
167 | 0 | keydata = import_data.keydata; |
168 | 0 | import_data.keydata = NULL; |
169 | 0 | } |
170 | | |
171 | 342k | if (keydata != NULL |
172 | 342k | && (pkey = evp_keymgmt_util_make_pkey(keymgmt, keydata)) == NULL) |
173 | 0 | evp_keymgmt_freedata(keymgmt, keydata); |
174 | | |
175 | 342k | *data->object = pkey; |
176 | | |
177 | | /* |
178 | | * evp_keymgmt_util_make_pkey() increments the reference count when |
179 | | * assigning the EVP_PKEY, so we can free the keymgmt here. |
180 | | */ |
181 | 342k | EVP_KEYMGMT_free(keymgmt); |
182 | 342k | } |
183 | | /* |
184 | | * We successfully looked through, |*ctx->object| determines if we |
185 | | * actually found something. |
186 | | */ |
187 | 342k | return (*data->object != NULL); |
188 | 342k | } |
189 | | |
190 | | static void decoder_clean_pkey_construct_arg(void *construct_data) |
191 | 1.42M | { |
192 | 1.42M | struct decoder_pkey_data_st *data = construct_data; |
193 | | |
194 | 1.42M | if (data != NULL) { |
195 | 959k | sk_EVP_KEYMGMT_pop_free(data->keymgmts, EVP_KEYMGMT_free); |
196 | 959k | OPENSSL_free(data->propq); |
197 | 959k | OPENSSL_free(data->object_type); |
198 | 959k | OPENSSL_free(data); |
199 | 959k | } |
200 | 1.42M | } |
201 | | |
202 | | static void collect_name(const char *name, void *arg) |
203 | 3.70M | { |
204 | 3.70M | STACK_OF(OPENSSL_CSTRING) *names = arg; |
205 | | |
206 | 3.70M | sk_OPENSSL_CSTRING_push(names, name); |
207 | 3.70M | } |
208 | | |
209 | | static void collect_keymgmt(EVP_KEYMGMT *keymgmt, void *arg) |
210 | 4.82M | { |
211 | 4.82M | STACK_OF(EVP_KEYMGMT) *keymgmts = arg; |
212 | | |
213 | 4.82M | if (!EVP_KEYMGMT_up_ref(keymgmt) /* ref++ */) |
214 | 0 | return; |
215 | 4.82M | if (sk_EVP_KEYMGMT_push(keymgmts, keymgmt) <= 0) { |
216 | 0 | EVP_KEYMGMT_free(keymgmt); /* ref-- */ |
217 | 0 | return; |
218 | 0 | } |
219 | 4.82M | } |
220 | | |
221 | | struct collect_decoder_data_st { |
222 | | STACK_OF(OPENSSL_CSTRING) *names; |
223 | | OSSL_DECODER_CTX *ctx; |
224 | | |
225 | | int total; |
226 | | unsigned int error_occurred:1; |
227 | | }; |
228 | | |
229 | | static void collect_decoder(OSSL_DECODER *decoder, void *arg) |
230 | 10.4M | { |
231 | 10.4M | struct collect_decoder_data_st *data = arg; |
232 | 10.4M | size_t i, end_i; |
233 | 10.4M | const OSSL_PROVIDER *prov = OSSL_DECODER_get0_provider(decoder); |
234 | 10.4M | void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
235 | | |
236 | 10.4M | if (data->error_occurred) |
237 | 0 | return; |
238 | | |
239 | 10.4M | if (data->names == NULL) { |
240 | 0 | data->error_occurred = 1; |
241 | 0 | return; |
242 | 0 | } |
243 | | |
244 | | /* |
245 | | * Either the caller didn't give a selection, or if they did, |
246 | | * the decoder must tell us if it supports that selection to |
247 | | * be accepted. If the decoder doesn't have |does_selection|, |
248 | | * it's seen as taking anything. |
249 | | */ |
250 | 10.4M | if (decoder->does_selection != NULL |
251 | 10.4M | && !decoder->does_selection(provctx, data->ctx->selection)) |
252 | 4.91M | return; |
253 | | |
254 | 5.54M | OSSL_TRACE_BEGIN(DECODER) { |
255 | 0 | BIO_printf(trc_out, |
256 | 0 | "(ctx %p) Checking out decoder %p:\n" |
257 | 0 | " %s with %s\n", |
258 | 0 | (void *)data->ctx, (void *)decoder, |
259 | 0 | OSSL_DECODER_get0_name(decoder), |
260 | 0 | OSSL_DECODER_get0_properties(decoder)); |
261 | 5.54M | } OSSL_TRACE_END(DECODER); |
262 | | |
263 | 5.54M | end_i = sk_OPENSSL_CSTRING_num(data->names); |
264 | 51.5M | for (i = 0; i < end_i; i++) { |
265 | 47.4M | const char *name = sk_OPENSSL_CSTRING_value(data->names, i); |
266 | | |
267 | 47.4M | if (OSSL_DECODER_is_a(decoder, name)) { |
268 | 1.42M | void *decoderctx = NULL; |
269 | 1.42M | OSSL_DECODER_INSTANCE *di = NULL; |
270 | | |
271 | 1.42M | if ((decoderctx = decoder->newctx(provctx)) == NULL) { |
272 | 0 | data->error_occurred = 1; |
273 | 0 | return; |
274 | 0 | } |
275 | 1.42M | if ((di = ossl_decoder_instance_new(decoder, decoderctx)) == NULL) { |
276 | 0 | decoder->freectx(decoderctx); |
277 | 0 | data->error_occurred = 1; |
278 | 0 | return; |
279 | 0 | } |
280 | | |
281 | 1.42M | OSSL_TRACE_BEGIN(DECODER) { |
282 | 0 | BIO_printf(trc_out, |
283 | 0 | "(ctx %p) Checking out decoder %p:\n" |
284 | 0 | " %s with %s\n", |
285 | 0 | (void *)data->ctx, (void *)decoder, |
286 | 0 | OSSL_DECODER_get0_name(decoder), |
287 | 0 | OSSL_DECODER_get0_properties(decoder)); |
288 | 1.42M | } OSSL_TRACE_END(DECODER); |
289 | | |
290 | 1.42M | if (!ossl_decoder_ctx_add_decoder_inst(data->ctx, di)) { |
291 | 0 | ossl_decoder_instance_free(di); |
292 | 0 | data->error_occurred = 1; |
293 | 0 | return; |
294 | 0 | } |
295 | 1.42M | data->total++; |
296 | | |
297 | | /* Success */ |
298 | 1.42M | return; |
299 | 1.42M | } |
300 | 47.4M | } |
301 | | |
302 | | /* Decoder not suitable - but not a fatal error */ |
303 | 4.11M | data->error_occurred = 0; |
304 | 4.11M | } |
305 | | |
306 | | int ossl_decoder_ctx_setup_for_pkey(OSSL_DECODER_CTX *ctx, |
307 | | EVP_PKEY **pkey, const char *keytype, |
308 | | OSSL_LIB_CTX *libctx, |
309 | | const char *propquery) |
310 | 268k | { |
311 | 268k | struct decoder_pkey_data_st *process_data = NULL; |
312 | 268k | STACK_OF(OPENSSL_CSTRING) *names = NULL; |
313 | 268k | const char *input_type = ctx->start_input_type; |
314 | 268k | const char *input_structure = ctx->input_structure; |
315 | 268k | int ok = 0; |
316 | 268k | int isecoid = 0; |
317 | 268k | int i, end; |
318 | | |
319 | 268k | if (keytype != NULL |
320 | 268k | && (strcmp(keytype, "id-ecPublicKey") == 0 |
321 | 223k | || strcmp(keytype, "1.2.840.10045.2.1") == 0)) |
322 | 107k | isecoid = 1; |
323 | | |
324 | 268k | OSSL_TRACE_BEGIN(DECODER) { |
325 | 0 | BIO_printf(trc_out, |
326 | 0 | "(ctx %p) Looking for decoders producing %s%s%s%s%s%s\n", |
327 | 0 | (void *)ctx, |
328 | 0 | keytype != NULL ? keytype : "", |
329 | 0 | keytype != NULL ? " keys" : "keys of any type", |
330 | 0 | input_type != NULL ? " from " : "", |
331 | 0 | input_type != NULL ? input_type : "", |
332 | 0 | input_structure != NULL ? " with " : "", |
333 | 0 | input_structure != NULL ? input_structure : ""); |
334 | 268k | } OSSL_TRACE_END(DECODER); |
335 | | |
336 | 268k | if ((process_data = OPENSSL_zalloc(sizeof(*process_data))) == NULL |
337 | 268k | || (propquery != NULL |
338 | 268k | && (process_data->propq = OPENSSL_strdup(propquery)) == NULL) |
339 | 268k | || (process_data->keymgmts = sk_EVP_KEYMGMT_new_null()) == NULL |
340 | 268k | || (names = sk_OPENSSL_CSTRING_new_null()) == NULL) { |
341 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE); |
342 | 0 | goto err; |
343 | 0 | } |
344 | | |
345 | 268k | process_data->object = (void **)pkey; |
346 | 268k | process_data->libctx = libctx; |
347 | 268k | process_data->selection = ctx->selection; |
348 | | |
349 | | /* First, find all keymgmts to form goals */ |
350 | 268k | EVP_KEYMGMT_do_all_provided(libctx, collect_keymgmt, |
351 | 268k | process_data->keymgmts); |
352 | | |
353 | | /* Then, we collect all the keymgmt names */ |
354 | 268k | end = sk_EVP_KEYMGMT_num(process_data->keymgmts); |
355 | 5.09M | for (i = 0; i < end; i++) { |
356 | 4.82M | EVP_KEYMGMT *keymgmt = sk_EVP_KEYMGMT_value(process_data->keymgmts, i); |
357 | | |
358 | | /* |
359 | | * If the key type is given by the caller, we only use the matching |
360 | | * KEYMGMTs, otherwise we use them all. |
361 | | * We have to special case SM2 here because of its abuse of the EC OID. |
362 | | * The EC OID can be used to identify an EC key or an SM2 key - so if |
363 | | * we have seen that OID we try both key types |
364 | | */ |
365 | 4.82M | if (keytype == NULL |
366 | 4.82M | || EVP_KEYMGMT_is_a(keymgmt, keytype) |
367 | 4.82M | || (isecoid && EVP_KEYMGMT_is_a(keymgmt, "SM2"))) { |
368 | 1.12M | if (!EVP_KEYMGMT_names_do_all(keymgmt, collect_name, names)) { |
369 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR); |
370 | 0 | goto err; |
371 | 0 | } |
372 | 1.12M | } |
373 | 4.82M | } |
374 | | |
375 | 268k | OSSL_TRACE_BEGIN(DECODER) { |
376 | 0 | end = sk_OPENSSL_CSTRING_num(names); |
377 | 0 | BIO_printf(trc_out, |
378 | 0 | " Found %d keytypes (possibly with duplicates)", |
379 | 0 | end); |
380 | 0 | for (i = 0; i < end; i++) |
381 | 0 | BIO_printf(trc_out, "%s%s", |
382 | 0 | i == 0 ? ": " : ", ", |
383 | 0 | sk_OPENSSL_CSTRING_value(names, i)); |
384 | 0 | BIO_printf(trc_out, "\n"); |
385 | 268k | } OSSL_TRACE_END(DECODER); |
386 | | |
387 | | /* |
388 | | * Finally, find all decoders that have any keymgmt of the collected |
389 | | * keymgmt names |
390 | | */ |
391 | 268k | { |
392 | 268k | struct collect_decoder_data_st collect_decoder_data = { NULL, }; |
393 | | |
394 | 268k | collect_decoder_data.names = names; |
395 | 268k | collect_decoder_data.ctx = ctx; |
396 | 268k | OSSL_DECODER_do_all_provided(libctx, |
397 | 268k | collect_decoder, &collect_decoder_data); |
398 | 268k | sk_OPENSSL_CSTRING_free(names); |
399 | 268k | names = NULL; |
400 | | |
401 | 268k | if (collect_decoder_data.error_occurred) |
402 | 0 | goto err; |
403 | | |
404 | 268k | OSSL_TRACE_BEGIN(DECODER) { |
405 | 0 | BIO_printf(trc_out, |
406 | 0 | "(ctx %p) Got %d decoders producing keys\n", |
407 | 0 | (void *)ctx, collect_decoder_data.total); |
408 | 268k | } OSSL_TRACE_END(DECODER); |
409 | 268k | } |
410 | | |
411 | 268k | if (OSSL_DECODER_CTX_get_num_decoders(ctx) != 0) { |
412 | 250k | if (!OSSL_DECODER_CTX_set_construct(ctx, decoder_construct_pkey) |
413 | 250k | || !OSSL_DECODER_CTX_set_construct_data(ctx, process_data) |
414 | 250k | || !OSSL_DECODER_CTX_set_cleanup(ctx, |
415 | 250k | decoder_clean_pkey_construct_arg)) |
416 | 0 | goto err; |
417 | | |
418 | 250k | process_data = NULL; /* Avoid it being freed */ |
419 | 250k | } |
420 | | |
421 | 268k | ok = 1; |
422 | 268k | err: |
423 | 268k | decoder_clean_pkey_construct_arg(process_data); |
424 | 268k | sk_OPENSSL_CSTRING_free(names); |
425 | | |
426 | 268k | return ok; |
427 | 268k | } |
428 | | |
429 | | OSSL_DECODER_CTX * |
430 | | OSSL_DECODER_CTX_new_for_pkey(EVP_PKEY **pkey, |
431 | | const char *input_type, |
432 | | const char *input_structure, |
433 | | const char *keytype, int selection, |
434 | | OSSL_LIB_CTX *libctx, const char *propquery) |
435 | 498k | { |
436 | 498k | OSSL_DECODER_CTX *ctx = NULL; |
437 | | |
438 | 498k | if ((ctx = OSSL_DECODER_CTX_new()) == NULL) { |
439 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE); |
440 | 0 | return NULL; |
441 | 0 | } |
442 | | |
443 | 498k | OSSL_TRACE_BEGIN(DECODER) { |
444 | 0 | BIO_printf(trc_out, |
445 | 0 | "(ctx %p) Looking for %s decoders with selection %d\n", |
446 | 0 | (void *)ctx, keytype, selection); |
447 | 0 | BIO_printf(trc_out, " input type: %s, input structure: %s\n", |
448 | 0 | input_type, input_structure); |
449 | 498k | } OSSL_TRACE_END(DECODER); |
450 | | |
451 | 498k | if (OSSL_DECODER_CTX_set_input_type(ctx, input_type) |
452 | 498k | && OSSL_DECODER_CTX_set_input_structure(ctx, input_structure) |
453 | 498k | && OSSL_DECODER_CTX_set_selection(ctx, selection) |
454 | 498k | && ossl_decoder_ctx_setup_for_pkey(ctx, pkey, keytype, |
455 | 498k | libctx, propquery) |
456 | 498k | && OSSL_DECODER_CTX_add_extra(ctx, libctx, propquery)) { |
457 | 498k | OSSL_TRACE_BEGIN(DECODER) { |
458 | 0 | BIO_printf(trc_out, "(ctx %p) Got %d decoders\n", |
459 | 0 | (void *)ctx, OSSL_DECODER_CTX_get_num_decoders(ctx)); |
460 | 498k | } OSSL_TRACE_END(DECODER); |
461 | 498k | return ctx; |
462 | 498k | } |
463 | | |
464 | 0 | OSSL_DECODER_CTX_free(ctx); |
465 | 0 | return NULL; |
466 | 498k | } |