/src/openssl30/crypto/evp/pmeth_gn.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2006-2022 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 <stdio.h> | 
| 11 |  | #include <stdlib.h> | 
| 12 |  | #include <openssl/core.h> | 
| 13 |  | #include <openssl/core_names.h> | 
| 14 |  | #include "internal/cryptlib.h" | 
| 15 |  | #include "internal/core.h" | 
| 16 |  | #include <openssl/objects.h> | 
| 17 |  | #include <openssl/evp.h> | 
| 18 |  | #include "crypto/bn.h" | 
| 19 |  | #ifndef FIPS_MODULE | 
| 20 |  | # include "crypto/asn1.h" | 
| 21 |  | #endif | 
| 22 |  | #include "crypto/evp.h" | 
| 23 |  | #include "evp_local.h" | 
| 24 |  |  | 
| 25 |  | static int gen_init(EVP_PKEY_CTX *ctx, int operation) | 
| 26 | 23.5k | { | 
| 27 | 23.5k |     int ret = 0; | 
| 28 |  |  | 
| 29 | 23.5k |     if (ctx == NULL) | 
| 30 | 0 |         goto not_supported; | 
| 31 |  |  | 
| 32 | 23.5k |     evp_pkey_ctx_free_old_ops(ctx); | 
| 33 | 23.5k |     ctx->operation = operation; | 
| 34 |  |  | 
| 35 | 23.5k |     if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL) | 
| 36 | 0 |         goto legacy; | 
| 37 |  |  | 
| 38 | 23.5k |     switch (operation) { | 
| 39 | 4.13k |     case EVP_PKEY_OP_PARAMGEN: | 
| 40 | 4.13k |         ctx->op.keymgmt.genctx = | 
| 41 | 4.13k |             evp_keymgmt_gen_init(ctx->keymgmt, | 
| 42 | 4.13k |                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, NULL); | 
| 43 | 4.13k |         break; | 
| 44 | 19.4k |     case EVP_PKEY_OP_KEYGEN: | 
| 45 | 19.4k |         ctx->op.keymgmt.genctx = | 
| 46 | 19.4k |             evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR, | 
| 47 | 19.4k |                                  NULL); | 
| 48 | 19.4k |         break; | 
| 49 | 23.5k |     } | 
| 50 |  |  | 
| 51 | 23.5k |     if (ctx->op.keymgmt.genctx == NULL) | 
| 52 | 23.5k |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 53 | 23.5k |     else | 
| 54 | 23.5k |         ret = 1; | 
| 55 | 23.5k |     goto end; | 
| 56 |  |  | 
| 57 | 0 |  legacy: | 
| 58 |  | #ifdef FIPS_MODULE | 
| 59 |  |     goto not_supported; | 
| 60 |  | #else | 
| 61 | 0 |     if (ctx->pmeth == NULL | 
| 62 | 0 |         || (operation == EVP_PKEY_OP_PARAMGEN | 
| 63 | 0 |             && ctx->pmeth->paramgen == NULL) | 
| 64 | 0 |         || (operation == EVP_PKEY_OP_KEYGEN | 
| 65 | 0 |             && ctx->pmeth->keygen == NULL)) | 
| 66 | 0 |         goto not_supported; | 
| 67 |  |  | 
| 68 | 0 |     ret = 1; | 
| 69 | 0 |     switch (operation) { | 
| 70 | 0 |     case EVP_PKEY_OP_PARAMGEN: | 
| 71 | 0 |         if (ctx->pmeth->paramgen_init != NULL) | 
| 72 | 0 |             ret = ctx->pmeth->paramgen_init(ctx); | 
| 73 | 0 |         break; | 
| 74 | 0 |     case EVP_PKEY_OP_KEYGEN: | 
| 75 | 0 |         if (ctx->pmeth->keygen_init != NULL) | 
| 76 | 0 |             ret = ctx->pmeth->keygen_init(ctx); | 
| 77 | 0 |         break; | 
| 78 | 0 |     } | 
| 79 | 0 | #endif | 
| 80 |  |  | 
| 81 | 23.5k |  end: | 
| 82 | 23.5k |     if (ret <= 0 && ctx != NULL) { | 
| 83 | 0 |         evp_pkey_ctx_free_old_ops(ctx); | 
| 84 | 0 |         ctx->operation = EVP_PKEY_OP_UNDEFINED; | 
| 85 | 0 |     } | 
| 86 | 23.5k |     return ret; | 
| 87 |  |  | 
| 88 | 0 |  not_supported: | 
| 89 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 90 | 0 |     ret = -2; | 
| 91 | 0 |     goto end; | 
| 92 | 0 | } | 
| 93 |  |  | 
| 94 |  | int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx) | 
| 95 | 4.13k | { | 
| 96 | 4.13k |     return gen_init(ctx, EVP_PKEY_OP_PARAMGEN); | 
| 97 | 4.13k | } | 
| 98 |  |  | 
| 99 |  | int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx) | 
| 100 | 19.4k | { | 
| 101 | 19.4k |     return gen_init(ctx, EVP_PKEY_OP_KEYGEN); | 
| 102 | 19.4k | } | 
| 103 |  |  | 
| 104 |  | static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg) | 
| 105 | 0 | { | 
| 106 | 0 |     EVP_PKEY_CTX *ctx = arg; | 
| 107 | 0 |     const OSSL_PARAM *param = NULL; | 
| 108 | 0 |     int p = -1, n = -1; | 
| 109 |  | 
 | 
| 110 | 0 |     if (ctx->pkey_gencb == NULL) | 
| 111 | 0 |         return 1;                /* No callback?  That's fine */ | 
| 112 |  |  | 
| 113 | 0 |     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL)) | 
| 114 | 0 |         == NULL | 
| 115 | 0 |         || !OSSL_PARAM_get_int(param, &p)) | 
| 116 | 0 |         return 0; | 
| 117 | 0 |     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION)) | 
| 118 | 0 |         == NULL | 
| 119 | 0 |         || !OSSL_PARAM_get_int(param, &n)) | 
| 120 | 0 |         return 0; | 
| 121 |  |  | 
| 122 | 0 |     ctx->keygen_info[0] = p; | 
| 123 | 0 |     ctx->keygen_info[1] = n; | 
| 124 |  | 
 | 
| 125 | 0 |     return ctx->pkey_gencb(ctx); | 
| 126 | 0 | } | 
| 127 |  |  | 
| 128 |  | int EVP_PKEY_generate(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) | 
| 129 | 23.5k | { | 
| 130 | 23.5k |     int ret = 0; | 
| 131 | 23.5k |     EVP_PKEY *allocated_pkey = NULL; | 
| 132 |  |     /* Legacy compatible keygen callback info, only used with provider impls */ | 
| 133 | 23.5k |     int gentmp[2]; | 
| 134 |  |  | 
| 135 | 23.5k |     if (ppkey == NULL) | 
| 136 | 0 |         return -1; | 
| 137 |  |  | 
| 138 | 23.5k |     if (ctx == NULL) | 
| 139 | 0 |         goto not_supported; | 
| 140 |  |  | 
| 141 | 23.5k |     if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0) | 
| 142 | 0 |         goto not_initialized; | 
| 143 |  |  | 
| 144 | 23.5k |     if (*ppkey == NULL) | 
| 145 | 23.5k |         *ppkey = allocated_pkey = EVP_PKEY_new(); | 
| 146 |  |  | 
| 147 | 23.5k |     if (*ppkey == NULL) { | 
| 148 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 149 | 0 |         return -1; | 
| 150 | 0 |     } | 
| 151 |  |  | 
| 152 | 23.5k |     if (ctx->op.keymgmt.genctx == NULL) | 
| 153 | 0 |         goto legacy; | 
| 154 |  |  | 
| 155 |  |     /* | 
| 156 |  |      * Asssigning gentmp to ctx->keygen_info is something our legacy | 
| 157 |  |      * implementations do.  Because the provider implementations aren't | 
| 158 |  |      * allowed to reach into our EVP_PKEY_CTX, we need to provide similar | 
| 159 |  |      * space for backward compatibility.  It's ok that we attach a local | 
| 160 |  |      * variable, as it should only be useful in the calls down from here. | 
| 161 |  |      * This is cleared as soon as it isn't useful any more, i.e. directly | 
| 162 |  |      * after the evp_keymgmt_util_gen() call. | 
| 163 |  |      */ | 
| 164 | 23.5k |     ctx->keygen_info = gentmp; | 
| 165 | 23.5k |     ctx->keygen_info_count = 2; | 
| 166 |  |  | 
| 167 | 23.5k |     ret = 1; | 
| 168 | 23.5k |     if (ctx->pkey != NULL) { | 
| 169 | 3.40k |         EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt; | 
| 170 | 3.40k |         void *keydata = | 
| 171 | 3.40k |             evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, | 
| 172 | 3.40k |                                         &tmp_keymgmt, ctx->propquery); | 
| 173 |  |  | 
| 174 | 3.40k |         if (tmp_keymgmt == NULL) | 
| 175 | 0 |             goto not_supported; | 
| 176 |  |         /* | 
| 177 |  |          * It's ok if keydata is NULL here.  The backend is expected to deal | 
| 178 |  |          * with that as it sees fit. | 
| 179 |  |          */ | 
| 180 | 3.40k |         ret = evp_keymgmt_gen_set_template(ctx->keymgmt, | 
| 181 | 3.40k |                                            ctx->op.keymgmt.genctx, keydata); | 
| 182 | 3.40k |     } | 
| 183 |  |  | 
| 184 |  |     /* | 
| 185 |  |      * the returned value from evp_keymgmt_util_gen() is cached in *ppkey, | 
| 186 |  |      * so we do not need to save it, just check it. | 
| 187 |  |      */ | 
| 188 | 23.5k |     ret = ret | 
| 189 | 23.5k |         && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx, | 
| 190 | 23.5k |                                  ossl_callback_to_pkey_gencb, ctx) | 
| 191 | 23.5k |             != NULL); | 
| 192 |  |  | 
| 193 | 23.5k |     ctx->keygen_info = NULL; | 
| 194 |  |  | 
| 195 | 23.5k | #ifndef FIPS_MODULE | 
| 196 |  |     /* In case |*ppkey| was originally a legacy key */ | 
| 197 | 23.5k |     if (ret) | 
| 198 | 23.5k |         evp_pkey_free_legacy(*ppkey); | 
| 199 | 23.5k | #endif | 
| 200 |  |  | 
| 201 |  |     /* | 
| 202 |  |      * Because we still have legacy keys | 
| 203 |  |      */ | 
| 204 | 23.5k |     (*ppkey)->type = ctx->legacy_keytype; | 
| 205 |  |  | 
| 206 | 23.5k |     goto end; | 
| 207 |  |  | 
| 208 | 0 |  legacy: | 
| 209 |  | #ifdef FIPS_MODULE | 
| 210 |  |     goto not_supported; | 
| 211 |  | #else | 
| 212 |  |     /* | 
| 213 |  |      * If we get here then we're using legacy paramgen/keygen. In that case | 
| 214 |  |      * the pkey in ctx (if there is one) had better not be provided (because the | 
| 215 |  |      * legacy methods may not know how to handle it). However we can only get | 
| 216 |  |      * here if ctx->op.keymgmt.genctx == NULL, but that should never be the case | 
| 217 |  |      * if ctx->pkey is provided because we don't allow this when we initialise | 
| 218 |  |      * the ctx. | 
| 219 |  |      */ | 
| 220 | 0 |     if (ctx->pkey != NULL && !ossl_assert(!evp_pkey_is_provided(ctx->pkey))) | 
| 221 | 0 |         goto not_accessible; | 
| 222 |  |  | 
| 223 | 0 |     switch (ctx->operation) { | 
| 224 | 0 |     case EVP_PKEY_OP_PARAMGEN: | 
| 225 | 0 |         ret = ctx->pmeth->paramgen(ctx, *ppkey); | 
| 226 | 0 |         break; | 
| 227 | 0 |     case EVP_PKEY_OP_KEYGEN: | 
| 228 | 0 |         ret = ctx->pmeth->keygen(ctx, *ppkey); | 
| 229 | 0 |         break; | 
| 230 | 0 |     default: | 
| 231 | 0 |         goto not_supported; | 
| 232 | 0 |     } | 
| 233 | 0 | #endif | 
| 234 |  |  | 
| 235 | 23.5k |  end: | 
| 236 | 23.5k |     if (ret <= 0) { | 
| 237 | 0 |         if (allocated_pkey != NULL) | 
| 238 | 0 |             *ppkey = NULL; | 
| 239 | 0 |         EVP_PKEY_free(allocated_pkey); | 
| 240 | 0 |     } | 
| 241 | 23.5k |     return ret; | 
| 242 |  |  | 
| 243 | 0 |  not_supported: | 
| 244 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 245 | 0 |     ret = -2; | 
| 246 | 0 |     goto end; | 
| 247 | 0 |  not_initialized: | 
| 248 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); | 
| 249 | 0 |     ret = -1; | 
| 250 | 0 |     goto end; | 
| 251 | 0 | #ifndef FIPS_MODULE | 
| 252 | 0 |  not_accessible: | 
| 253 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS); | 
| 254 | 0 |     ret = -1; | 
| 255 | 0 |     goto end; | 
| 256 | 0 | #endif | 
| 257 | 0 | } | 
| 258 |  |  | 
| 259 |  | int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) | 
| 260 | 4.13k | { | 
| 261 | 4.13k |     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) { | 
| 262 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); | 
| 263 | 0 |         return -1; | 
| 264 | 0 |     } | 
| 265 | 4.13k |     return EVP_PKEY_generate(ctx, ppkey); | 
| 266 | 4.13k | } | 
| 267 |  |  | 
| 268 |  | int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey) | 
| 269 | 19.4k | { | 
| 270 | 19.4k |     if (ctx->operation != EVP_PKEY_OP_KEYGEN) { | 
| 271 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); | 
| 272 | 0 |         return -1; | 
| 273 | 0 |     } | 
| 274 | 19.4k |     return EVP_PKEY_generate(ctx, ppkey); | 
| 275 | 19.4k | } | 
| 276 |  |  | 
| 277 |  | void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb) | 
| 278 | 0 | { | 
| 279 | 0 |     ctx->pkey_gencb = cb; | 
| 280 | 0 | } | 
| 281 |  |  | 
| 282 |  | EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx) | 
| 283 | 0 | { | 
| 284 | 0 |     return ctx->pkey_gencb; | 
| 285 | 0 | } | 
| 286 |  |  | 
| 287 |  | /* | 
| 288 |  |  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style | 
| 289 |  |  * callbacks. | 
| 290 |  |  */ | 
| 291 |  |  | 
| 292 |  | static int trans_cb(int a, int b, BN_GENCB *gcb) | 
| 293 | 0 | { | 
| 294 | 0 |     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb); | 
| 295 | 0 |     ctx->keygen_info[0] = a; | 
| 296 | 0 |     ctx->keygen_info[1] = b; | 
| 297 | 0 |     return ctx->pkey_gencb(ctx); | 
| 298 | 0 | } | 
| 299 |  |  | 
| 300 |  | void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx) | 
| 301 | 0 | { | 
| 302 | 0 |     BN_GENCB_set(cb, trans_cb, ctx); | 
| 303 | 0 | } | 
| 304 |  |  | 
| 305 |  | int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx) | 
| 306 | 0 | { | 
| 307 | 0 |     if (idx == -1) | 
| 308 | 0 |         return ctx->keygen_info_count; | 
| 309 | 0 |     if (idx < 0 || idx > ctx->keygen_info_count) | 
| 310 | 0 |         return 0; | 
| 311 | 0 |     return ctx->keygen_info[idx]; | 
| 312 | 0 | } | 
| 313 |  |  | 
| 314 |  | #ifndef FIPS_MODULE | 
| 315 |  |  | 
| 316 |  | EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e, | 
| 317 |  |                                const unsigned char *key, int keylen) | 
| 318 | 1.89k | { | 
| 319 | 1.89k |     EVP_PKEY_CTX *mac_ctx = NULL; | 
| 320 | 1.89k |     EVP_PKEY *mac_key = NULL; | 
| 321 | 1.89k |     mac_ctx = EVP_PKEY_CTX_new_id(type, e); | 
| 322 | 1.89k |     if (!mac_ctx) | 
| 323 | 0 |         return NULL; | 
| 324 | 1.89k |     if (EVP_PKEY_keygen_init(mac_ctx) <= 0) | 
| 325 | 0 |         goto merr; | 
| 326 | 1.89k |     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0) | 
| 327 | 0 |         goto merr; | 
| 328 | 1.89k |     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0) | 
| 329 | 0 |         goto merr; | 
| 330 | 1.89k |  merr: | 
| 331 | 1.89k |     EVP_PKEY_CTX_free(mac_ctx); | 
| 332 | 1.89k |     return mac_key; | 
| 333 | 1.89k | } | 
| 334 |  |  | 
| 335 |  | #endif /* FIPS_MODULE */ | 
| 336 |  |  | 
| 337 |  | /*- All methods below can also be used in FIPS_MODULE */ | 
| 338 |  |  | 
| 339 |  | static int fromdata_init(EVP_PKEY_CTX *ctx, int operation) | 
| 340 | 5.48k | { | 
| 341 | 5.48k |     if (ctx == NULL || ctx->keytype == NULL) | 
| 342 | 0 |         goto not_supported; | 
| 343 |  |  | 
| 344 | 5.48k |     evp_pkey_ctx_free_old_ops(ctx); | 
| 345 | 5.48k |     if (ctx->keymgmt == NULL) | 
| 346 | 0 |         goto not_supported; | 
| 347 |  |  | 
| 348 | 5.48k |     ctx->operation = operation; | 
| 349 | 5.48k |     return 1; | 
| 350 |  |  | 
| 351 | 0 |  not_supported: | 
| 352 | 0 |     if (ctx != NULL) | 
| 353 | 0 |         ctx->operation = EVP_PKEY_OP_UNDEFINED; | 
| 354 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 355 | 0 |     return -2; | 
| 356 | 5.48k | } | 
| 357 |  |  | 
| 358 |  | int EVP_PKEY_fromdata_init(EVP_PKEY_CTX *ctx) | 
| 359 | 5.48k | { | 
| 360 | 5.48k |     return fromdata_init(ctx, EVP_PKEY_OP_FROMDATA); | 
| 361 | 5.48k | } | 
| 362 |  |  | 
| 363 |  | int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, int selection, | 
| 364 |  |                       OSSL_PARAM params[]) | 
| 365 | 5.48k | { | 
| 366 | 5.48k |     void *keydata = NULL; | 
| 367 | 5.48k |     EVP_PKEY *allocated_pkey = NULL; | 
| 368 |  |  | 
| 369 | 5.48k |     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_FROMDATA) == 0) { | 
| 370 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | 
| 371 | 0 |         return -2; | 
| 372 | 0 |     } | 
| 373 |  |  | 
| 374 | 5.48k |     if (ppkey == NULL) | 
| 375 | 0 |         return -1; | 
| 376 |  |  | 
| 377 | 5.48k |     if (*ppkey == NULL) | 
| 378 | 5.48k |         allocated_pkey = *ppkey = EVP_PKEY_new(); | 
| 379 |  |  | 
| 380 | 5.48k |     if (*ppkey == NULL) { | 
| 381 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); | 
| 382 | 0 |         return -1; | 
| 383 | 0 |     } | 
| 384 |  |  | 
| 385 | 5.48k |     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection, params); | 
| 386 | 5.48k |     if (keydata == NULL) { | 
| 387 | 0 |         if (allocated_pkey != NULL) { | 
| 388 | 0 |             *ppkey = NULL; | 
| 389 | 0 |             EVP_PKEY_free(allocated_pkey); | 
| 390 | 0 |         } | 
| 391 | 0 |         return 0; | 
| 392 | 0 |     } | 
| 393 |  |     /* keydata is cached in *ppkey, so we need not bother with it further */ | 
| 394 | 5.48k |     return 1; | 
| 395 | 5.48k | } | 
| 396 |  |  | 
| 397 |  | const OSSL_PARAM *EVP_PKEY_fromdata_settable(EVP_PKEY_CTX *ctx, int selection) | 
| 398 | 0 | { | 
| 399 |  |     /* We call fromdata_init to get ctx->keymgmt populated */ | 
| 400 | 0 |     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED) == 1) | 
| 401 | 0 |         return evp_keymgmt_import_types(ctx->keymgmt, selection); | 
| 402 | 0 |     return NULL; | 
| 403 | 0 | } | 
| 404 |  |  | 
| 405 |  | static OSSL_CALLBACK ossl_pkey_todata_cb; | 
| 406 |  |  | 
| 407 |  | static int ossl_pkey_todata_cb(const OSSL_PARAM params[], void *arg) | 
| 408 | 0 | { | 
| 409 | 0 |     OSSL_PARAM **ret = arg; | 
| 410 |  | 
 | 
| 411 | 0 |     *ret = OSSL_PARAM_dup(params); | 
| 412 | 0 |     return 1; | 
| 413 | 0 | } | 
| 414 |  |  | 
| 415 |  | int EVP_PKEY_todata(const EVP_PKEY *pkey, int selection, OSSL_PARAM **params) | 
| 416 | 0 | { | 
| 417 | 0 |     if (params == NULL) | 
| 418 | 0 |         return 0; | 
| 419 | 0 |     return EVP_PKEY_export(pkey, selection, ossl_pkey_todata_cb, params); | 
| 420 | 0 | } | 
| 421 |  |  | 
| 422 |  | #ifndef FIPS_MODULE | 
| 423 |  | struct fake_import_data_st { | 
| 424 |  |     OSSL_CALLBACK *export_cb; | 
| 425 |  |     void *export_cbarg; | 
| 426 |  | }; | 
| 427 |  |  | 
| 428 |  | static OSSL_FUNC_keymgmt_import_fn pkey_fake_import; | 
| 429 |  | static int pkey_fake_import(void *fake_keydata, int ignored_selection, | 
| 430 |  |                             const OSSL_PARAM params[]) | 
| 431 | 0 | { | 
| 432 | 0 |     struct fake_import_data_st *data = fake_keydata; | 
| 433 |  | 
 | 
| 434 | 0 |     return data->export_cb(params, data->export_cbarg); | 
| 435 | 0 | } | 
| 436 |  | #endif | 
| 437 |  |  | 
| 438 |  | int EVP_PKEY_export(const EVP_PKEY *pkey, int selection, | 
| 439 |  |                     OSSL_CALLBACK *export_cb, void *export_cbarg) | 
| 440 | 0 | { | 
| 441 | 0 |     if (pkey == NULL) { | 
| 442 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | 
| 443 | 0 |         return 0; | 
| 444 | 0 |     } | 
| 445 | 0 | #ifndef FIPS_MODULE | 
| 446 | 0 |     if (evp_pkey_is_legacy(pkey)) { | 
| 447 | 0 |         struct fake_import_data_st data; | 
| 448 |  | 
 | 
| 449 | 0 |         data.export_cb = export_cb; | 
| 450 | 0 |         data.export_cbarg = export_cbarg; | 
| 451 |  |  | 
| 452 |  |         /* | 
| 453 |  |          * We don't need to care about libctx or propq here, as we're only | 
| 454 |  |          * interested in the resulting OSSL_PARAM array. | 
| 455 |  |          */ | 
| 456 | 0 |         return pkey->ameth->export_to(pkey, &data, pkey_fake_import, | 
| 457 | 0 |                                       NULL, NULL); | 
| 458 | 0 |     } | 
| 459 | 0 | #endif | 
| 460 | 0 |     return evp_keymgmt_util_export(pkey, selection, export_cb, export_cbarg); | 
| 461 | 0 | } |