/src/openssl/crypto/encode_decode/encoder_pkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-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/err.h> |
11 | | #include <openssl/ui.h> |
12 | | #include <openssl/params.h> |
13 | | #include <openssl/encoder.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include <openssl/provider.h> |
16 | | #include <openssl/safestack.h> |
17 | | #include <openssl/trace.h> |
18 | | #include "internal/provider.h" |
19 | | #include "internal/property.h" |
20 | | #include "internal/namemap.h" |
21 | | #include "crypto/evp.h" |
22 | | #include "encoder_local.h" |
23 | | |
24 | | DEFINE_STACK_OF(OSSL_ENCODER) |
25 | | |
26 | | int OSSL_ENCODER_CTX_set_cipher(OSSL_ENCODER_CTX *ctx, |
27 | | const char *cipher_name, |
28 | | const char *propquery) |
29 | 0 | { |
30 | 0 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END }; |
31 | |
|
32 | 0 | params[0] = |
33 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_CIPHER, |
34 | 0 | (void *)cipher_name, 0); |
35 | 0 | params[1] = |
36 | 0 | OSSL_PARAM_construct_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, |
37 | 0 | (void *)propquery, 0); |
38 | |
|
39 | 0 | return OSSL_ENCODER_CTX_set_params(ctx, params); |
40 | 0 | } |
41 | | |
42 | | int OSSL_ENCODER_CTX_set_passphrase(OSSL_ENCODER_CTX *ctx, |
43 | | const unsigned char *kstr, |
44 | | size_t klen) |
45 | 0 | { |
46 | 0 | return ossl_pw_set_passphrase(&ctx->pwdata, kstr, klen); |
47 | 0 | } |
48 | | |
49 | | int OSSL_ENCODER_CTX_set_passphrase_ui(OSSL_ENCODER_CTX *ctx, |
50 | | const UI_METHOD *ui_method, |
51 | | void *ui_data) |
52 | 0 | { |
53 | 0 | return ossl_pw_set_ui_method(&ctx->pwdata, ui_method, ui_data); |
54 | 0 | } |
55 | | |
56 | | int OSSL_ENCODER_CTX_set_pem_password_cb(OSSL_ENCODER_CTX *ctx, |
57 | | pem_password_cb *cb, void *cbarg) |
58 | 0 | { |
59 | 0 | return ossl_pw_set_pem_password_cb(&ctx->pwdata, cb, cbarg); |
60 | 0 | } |
61 | | |
62 | | int OSSL_ENCODER_CTX_set_passphrase_cb(OSSL_ENCODER_CTX *ctx, |
63 | | OSSL_PASSPHRASE_CALLBACK *cb, |
64 | | void *cbarg) |
65 | 0 | { |
66 | 0 | return ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, cb, cbarg); |
67 | 0 | } |
68 | | |
69 | | /* |
70 | | * Support for OSSL_ENCODER_CTX_new_for_type: |
71 | | * finding a suitable encoder |
72 | | */ |
73 | | |
74 | | struct collected_encoder_st { |
75 | | STACK_OF(OPENSSL_CSTRING) *names; |
76 | | int *id_names; |
77 | | const char *output_structure; |
78 | | const char *output_type; |
79 | | |
80 | | const OSSL_PROVIDER *keymgmt_prov; |
81 | | OSSL_ENCODER_CTX *ctx; |
82 | | unsigned int flag_find_same_provider:1; |
83 | | |
84 | | int error_occurred; |
85 | | }; |
86 | | |
87 | | static void collect_encoder(OSSL_ENCODER *encoder, void *arg) |
88 | 307k | { |
89 | 307k | struct collected_encoder_st *data = arg; |
90 | 307k | const OSSL_PROVIDER *prov; |
91 | | |
92 | 307k | if (data->error_occurred) |
93 | 0 | return; |
94 | | |
95 | 307k | data->error_occurred = 1; /* Assume the worst */ |
96 | | |
97 | 307k | prov = OSSL_ENCODER_get0_provider(encoder); |
98 | | /* |
99 | | * collect_encoder() is called in two passes, one where the encoders |
100 | | * from the same provider as the keymgmt are looked up, and one where |
101 | | * the other encoders are looked up. |data->flag_find_same_provider| |
102 | | * tells us which pass we're in. |
103 | | */ |
104 | 307k | if ((data->keymgmt_prov == prov) == data->flag_find_same_provider) { |
105 | 153k | void *provctx = OSSL_PROVIDER_get0_provider_ctx(prov); |
106 | 153k | int i, end_i = sk_OPENSSL_CSTRING_num(data->names); |
107 | 153k | int match; |
108 | | |
109 | 684k | for (i = 0; i < end_i; i++) { |
110 | 541k | if (data->flag_find_same_provider) |
111 | 541k | match = (data->id_names[i] == encoder->base.id); |
112 | 0 | else |
113 | 0 | match = OSSL_ENCODER_is_a(encoder, |
114 | 0 | sk_OPENSSL_CSTRING_value(data->names, i)); |
115 | 541k | if (!match |
116 | 541k | || (encoder->does_selection != NULL |
117 | 38.8k | && !encoder->does_selection(provctx, data->ctx->selection)) |
118 | 541k | || (data->keymgmt_prov != prov |
119 | 11.1k | && encoder->import_object == NULL)) |
120 | 530k | continue; |
121 | | |
122 | | /* Only add each encoder implementation once */ |
123 | 11.1k | if (OSSL_ENCODER_CTX_add_encoder(data->ctx, encoder)) |
124 | 11.1k | break; |
125 | 11.1k | } |
126 | 153k | } |
127 | | |
128 | 307k | data->error_occurred = 0; /* All is good now */ |
129 | 307k | } |
130 | | |
131 | | struct collected_names_st { |
132 | | STACK_OF(OPENSSL_CSTRING) *names; |
133 | | unsigned int error_occurred:1; |
134 | | }; |
135 | | |
136 | | static void collect_name(const char *name, void *arg) |
137 | 5.00k | { |
138 | 5.00k | struct collected_names_st *data = arg; |
139 | | |
140 | 5.00k | if (data->error_occurred) |
141 | 0 | return; |
142 | | |
143 | 5.00k | data->error_occurred = 1; /* Assume the worst */ |
144 | | |
145 | 5.00k | if (sk_OPENSSL_CSTRING_push(data->names, name) <= 0) |
146 | 0 | return; |
147 | | |
148 | 5.00k | data->error_occurred = 0; /* All is good now */ |
149 | 5.00k | } |
150 | | |
151 | | /* |
152 | | * Support for OSSL_ENCODER_to_bio: |
153 | | * writing callback for the OSSL_PARAM (the implementation doesn't have |
154 | | * intimate knowledge of the provider side object) |
155 | | */ |
156 | | |
157 | | struct construct_data_st { |
158 | | const EVP_PKEY *pk; |
159 | | int selection; |
160 | | |
161 | | OSSL_ENCODER_INSTANCE *encoder_inst; |
162 | | const void *obj; |
163 | | void *constructed_obj; |
164 | | }; |
165 | | |
166 | | static int encoder_import_cb(const OSSL_PARAM params[], void *arg) |
167 | 0 | { |
168 | 0 | struct construct_data_st *construct_data = arg; |
169 | 0 | OSSL_ENCODER_INSTANCE *encoder_inst = construct_data->encoder_inst; |
170 | 0 | OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst); |
171 | 0 | void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst); |
172 | |
|
173 | 0 | construct_data->constructed_obj = |
174 | 0 | encoder->import_object(encoderctx, construct_data->selection, params); |
175 | |
|
176 | 0 | return (construct_data->constructed_obj != NULL); |
177 | 0 | } |
178 | | |
179 | | static const void * |
180 | | encoder_construct_pkey(OSSL_ENCODER_INSTANCE *encoder_inst, void *arg) |
181 | 1.33k | { |
182 | 1.33k | struct construct_data_st *data = arg; |
183 | | |
184 | 1.33k | if (data->obj == NULL) { |
185 | 1.33k | OSSL_ENCODER *encoder = |
186 | 1.33k | OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst); |
187 | 1.33k | const EVP_PKEY *pk = data->pk; |
188 | 1.33k | const OSSL_PROVIDER *k_prov = EVP_KEYMGMT_get0_provider(pk->keymgmt); |
189 | 1.33k | const OSSL_PROVIDER *e_prov = OSSL_ENCODER_get0_provider(encoder); |
190 | | |
191 | 1.33k | if (k_prov != e_prov) { |
192 | 0 | data->encoder_inst = encoder_inst; |
193 | |
|
194 | 0 | if (!evp_keymgmt_export(pk->keymgmt, pk->keydata, data->selection, |
195 | 0 | &encoder_import_cb, data)) |
196 | 0 | return NULL; |
197 | 0 | data->obj = data->constructed_obj; |
198 | 1.33k | } else { |
199 | 1.33k | data->obj = pk->keydata; |
200 | 1.33k | } |
201 | 1.33k | } |
202 | | |
203 | 1.33k | return data->obj; |
204 | 1.33k | } |
205 | | |
206 | | static void encoder_destruct_pkey(void *arg) |
207 | 1.33k | { |
208 | 1.33k | struct construct_data_st *data = arg; |
209 | | |
210 | 1.33k | if (data->encoder_inst != NULL) { |
211 | 0 | OSSL_ENCODER *encoder = |
212 | 0 | OSSL_ENCODER_INSTANCE_get_encoder(data->encoder_inst); |
213 | |
|
214 | 0 | encoder->free_object(data->constructed_obj); |
215 | 0 | } |
216 | 1.33k | data->constructed_obj = NULL; |
217 | 1.33k | } |
218 | | |
219 | | /* |
220 | | * OSSL_ENCODER_CTX_new_for_pkey() returns a ctx with no encoder if |
221 | | * it couldn't find a suitable encoder. This allows a caller to detect if |
222 | | * a suitable encoder was found, with OSSL_ENCODER_CTX_get_num_encoder(), |
223 | | * and to use fallback methods if the result is NULL. |
224 | | */ |
225 | | static int ossl_encoder_ctx_setup_for_pkey(OSSL_ENCODER_CTX *ctx, |
226 | | const EVP_PKEY *pkey, |
227 | | int selection, |
228 | | const char *propquery) |
229 | 1.33k | { |
230 | 1.33k | struct construct_data_st *data = NULL; |
231 | 1.33k | const OSSL_PROVIDER *prov = NULL; |
232 | 1.33k | OSSL_LIB_CTX *libctx = NULL; |
233 | 1.33k | int ok = 0, i, end; |
234 | 1.33k | OSSL_NAMEMAP *namemap; |
235 | | |
236 | 1.33k | if (!ossl_assert(ctx != NULL) || !ossl_assert(pkey != NULL)) { |
237 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
238 | 0 | return 0; |
239 | 0 | } |
240 | | |
241 | 1.33k | if (evp_pkey_is_provided(pkey)) { |
242 | 1.33k | prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt); |
243 | 1.33k | libctx = ossl_provider_libctx(prov); |
244 | 1.33k | } |
245 | | |
246 | 1.33k | if (pkey->keymgmt != NULL) { |
247 | 1.33k | struct collected_encoder_st encoder_data; |
248 | 1.33k | struct collected_names_st keymgmt_data; |
249 | | |
250 | 1.33k | if ((data = OPENSSL_zalloc(sizeof(*data))) == NULL) |
251 | 0 | goto err; |
252 | | |
253 | | /* |
254 | | * Select the first encoder implementations in two steps. |
255 | | * First, collect the keymgmt names, then the encoders that match. |
256 | | */ |
257 | 1.33k | keymgmt_data.names = sk_OPENSSL_CSTRING_new_null(); |
258 | 1.33k | if (keymgmt_data.names == NULL) { |
259 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB); |
260 | 0 | goto err; |
261 | 0 | } |
262 | | |
263 | 1.33k | keymgmt_data.error_occurred = 0; |
264 | 1.33k | EVP_KEYMGMT_names_do_all(pkey->keymgmt, collect_name, &keymgmt_data); |
265 | 1.33k | if (keymgmt_data.error_occurred) { |
266 | 0 | sk_OPENSSL_CSTRING_free(keymgmt_data.names); |
267 | 0 | goto err; |
268 | 0 | } |
269 | | |
270 | 1.33k | encoder_data.names = keymgmt_data.names; |
271 | 1.33k | encoder_data.output_type = ctx->output_type; |
272 | 1.33k | encoder_data.output_structure = ctx->output_structure; |
273 | 1.33k | encoder_data.error_occurred = 0; |
274 | 1.33k | encoder_data.keymgmt_prov = prov; |
275 | 1.33k | encoder_data.ctx = ctx; |
276 | 1.33k | encoder_data.id_names = NULL; |
277 | | |
278 | | /* |
279 | | * collect_encoder() is called many times, and for every call it converts all encoder_data.names |
280 | | * into namemap ids if it calls OSSL_ENCODER_is_a(). We cache the ids here instead, |
281 | | * and can use them for encoders with the same provider as the keymgmt. |
282 | | */ |
283 | 1.33k | namemap = ossl_namemap_stored(libctx); |
284 | 1.33k | end = sk_OPENSSL_CSTRING_num(encoder_data.names); |
285 | 1.33k | if (end > 0) { |
286 | 1.33k | encoder_data.id_names = OPENSSL_malloc(end * sizeof(int)); |
287 | 1.33k | if (encoder_data.id_names == NULL) { |
288 | 0 | sk_OPENSSL_CSTRING_free(keymgmt_data.names); |
289 | 0 | goto err; |
290 | 0 | } |
291 | 6.33k | for (i = 0; i < end; ++i) { |
292 | 5.00k | const char *name = sk_OPENSSL_CSTRING_value(keymgmt_data.names, i); |
293 | | |
294 | 5.00k | encoder_data.id_names[i] = ossl_namemap_name2num(namemap, name); |
295 | 5.00k | } |
296 | 1.33k | } |
297 | | /* |
298 | | * Place the encoders with the a different provider as the keymgmt |
299 | | * last (the chain is processed in reverse order) |
300 | | */ |
301 | 1.33k | encoder_data.flag_find_same_provider = 0; |
302 | 1.33k | OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data); |
303 | | |
304 | | /* |
305 | | * Place the encoders with the same provider as the keymgmt first |
306 | | * (the chain is processed in reverse order) |
307 | | */ |
308 | 1.33k | encoder_data.flag_find_same_provider = 1; |
309 | 1.33k | OSSL_ENCODER_do_all_provided(libctx, collect_encoder, &encoder_data); |
310 | | |
311 | 1.33k | OPENSSL_free(encoder_data.id_names); |
312 | 1.33k | sk_OPENSSL_CSTRING_free(keymgmt_data.names); |
313 | 1.33k | if (encoder_data.error_occurred) { |
314 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_CRYPTO_LIB); |
315 | 0 | goto err; |
316 | 0 | } |
317 | 1.33k | } |
318 | | |
319 | 1.33k | if (data != NULL && OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) { |
320 | 1.33k | if (!OSSL_ENCODER_CTX_set_construct(ctx, encoder_construct_pkey) |
321 | 1.33k | || !OSSL_ENCODER_CTX_set_construct_data(ctx, data) |
322 | 1.33k | || !OSSL_ENCODER_CTX_set_cleanup(ctx, encoder_destruct_pkey)) |
323 | 0 | goto err; |
324 | | |
325 | 1.33k | data->pk = pkey; |
326 | 1.33k | data->selection = selection; |
327 | | |
328 | 1.33k | data = NULL; /* Avoid it being freed */ |
329 | 1.33k | } |
330 | | |
331 | 1.33k | ok = 1; |
332 | 1.33k | err: |
333 | 1.33k | if (data != NULL) { |
334 | 0 | OSSL_ENCODER_CTX_set_construct_data(ctx, NULL); |
335 | 0 | OPENSSL_free(data); |
336 | 0 | } |
337 | 1.33k | return ok; |
338 | 1.33k | } |
339 | | |
340 | | OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new_for_pkey(const EVP_PKEY *pkey, |
341 | | int selection, |
342 | | const char *output_type, |
343 | | const char *output_struct, |
344 | | const char *propquery) |
345 | 1.33k | { |
346 | 1.33k | OSSL_ENCODER_CTX *ctx = NULL; |
347 | 1.33k | OSSL_LIB_CTX *libctx = NULL; |
348 | | |
349 | 1.33k | if (pkey == NULL) { |
350 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER); |
351 | 0 | return NULL; |
352 | 0 | } |
353 | | |
354 | 1.33k | if (!evp_pkey_is_assigned(pkey)) { |
355 | 0 | ERR_raise_data(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT, |
356 | 0 | "The passed EVP_PKEY must be assigned a key"); |
357 | 0 | return NULL; |
358 | 0 | } |
359 | | |
360 | 1.33k | if ((ctx = OSSL_ENCODER_CTX_new()) == NULL) { |
361 | 0 | ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_OSSL_ENCODER_LIB); |
362 | 0 | return NULL; |
363 | 0 | } |
364 | | |
365 | 1.33k | if (evp_pkey_is_provided(pkey)) { |
366 | 1.33k | const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt); |
367 | | |
368 | 1.33k | libctx = ossl_provider_libctx(prov); |
369 | 1.33k | } |
370 | | |
371 | 1.33k | OSSL_TRACE_BEGIN(ENCODER) { |
372 | 0 | BIO_printf(trc_out, |
373 | 0 | "(ctx %p) Looking for %s encoders with selection %d\n", |
374 | 0 | (void *)ctx, EVP_PKEY_get0_type_name(pkey), selection); |
375 | 0 | BIO_printf(trc_out, " output type: %s, output structure: %s\n", |
376 | 0 | output_type, output_struct); |
377 | 1.33k | } OSSL_TRACE_END(ENCODER); |
378 | | |
379 | 1.33k | if (OSSL_ENCODER_CTX_set_output_type(ctx, output_type) |
380 | 1.33k | && (output_struct == NULL |
381 | 1.33k | || OSSL_ENCODER_CTX_set_output_structure(ctx, output_struct)) |
382 | 1.33k | && OSSL_ENCODER_CTX_set_selection(ctx, selection) |
383 | 1.33k | && ossl_encoder_ctx_setup_for_pkey(ctx, pkey, selection, propquery) |
384 | 1.33k | && OSSL_ENCODER_CTX_add_extra(ctx, libctx, propquery)) { |
385 | 1.33k | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
386 | 1.33k | int save_parameters = pkey->save_parameters; |
387 | | |
388 | 1.33k | params[0] = OSSL_PARAM_construct_int(OSSL_ENCODER_PARAM_SAVE_PARAMETERS, |
389 | 1.33k | &save_parameters); |
390 | | /* ignoring error as this is only auxiliary parameter */ |
391 | 1.33k | (void)OSSL_ENCODER_CTX_set_params(ctx, params); |
392 | | |
393 | 1.33k | OSSL_TRACE_BEGIN(ENCODER) { |
394 | 0 | BIO_printf(trc_out, "(ctx %p) Got %d encoders\n", |
395 | 0 | (void *)ctx, OSSL_ENCODER_CTX_get_num_encoders(ctx)); |
396 | 1.33k | } OSSL_TRACE_END(ENCODER); |
397 | 1.33k | return ctx; |
398 | 1.33k | } |
399 | | |
400 | 0 | OSSL_ENCODER_CTX_free(ctx); |
401 | 0 | return NULL; |
402 | 1.33k | } |