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