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