]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* | |
| 2 | * Copyright 1995-2025 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 | /* | |
| 11 | * DSA low level APIs are deprecated for public use, but still ok for | |
| 12 | * internal use. | |
| 13 | */ | |
| 14 | #include "internal/deprecated.h" | |
| 15 | ||
| 16 | #include <assert.h> | |
| 17 | #include <stdio.h> | |
| 18 | #include "internal/cryptlib.h" | |
| 19 | #include "internal/refcount.h" | |
| 20 | #include "internal/namemap.h" | |
| 21 | #include <openssl/bn.h> | |
| 22 | #include <openssl/err.h> | |
| 23 | #include <openssl/objects.h> | |
| 24 | #include <openssl/evp.h> | |
| 25 | #include <openssl/rsa.h> | |
| 26 | #include <openssl/dsa.h> | |
| 27 | #include <openssl/dh.h> | |
| 28 | #include <openssl/ec.h> | |
| 29 | #include <openssl/cmac.h> | |
| 30 | #include <openssl/params.h> | |
| 31 | #include <openssl/param_build.h> | |
| 32 | #include <openssl/encoder.h> | |
| 33 | #include <openssl/core_names.h> | |
| 34 | ||
| 35 | #include "internal/numbers.h" /* includes SIZE_MAX */ | |
| 36 | #include "internal/ffc.h" | |
| 37 | #include "crypto/evp.h" | |
| 38 | #include "crypto/dh.h" | |
| 39 | #include "crypto/dsa.h" | |
| 40 | #include "crypto/ec.h" | |
| 41 | #include "crypto/ecx.h" | |
| 42 | #include "crypto/rsa.h" | |
| 43 | #ifndef FIPS_MODULE | |
| 44 | #include "crypto/asn1.h" | |
| 45 | #include "crypto/x509.h" | |
| 46 | #endif | |
| 47 | #include "internal/provider.h" | |
| 48 | #include "internal/common.h" | |
| 49 | #include "evp_local.h" | |
| 50 | ||
| 51 | static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, | |
| 52 | int len, EVP_KEYMGMT *keymgmt); | |
| 53 | static void evp_pkey_free_it(EVP_PKEY *key); | |
| 54 | ||
| 55 | /* The type of parameters selected in key parameter functions */ | |
| 56 | #define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS | |
| 57 | ||
| 58 | #ifndef FIPS_MODULE | |
| 59 | int EVP_PKEY_get_bits(const EVP_PKEY *pkey) | |
| 60 | { | |
| 61 | int size = 0; | |
| 62 | ||
| 63 | if (pkey != NULL) { | |
| 64 | size = pkey->cache.bits; | |
| 65 | if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL) | |
| 66 | size = pkey->ameth->pkey_bits(pkey); | |
| 67 | } | |
| 68 | if (size <= 0) { | |
| 69 | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS); | |
| 70 | return 0; | |
| 71 | } | |
| 72 | return size; | |
| 73 | } | |
| 74 | ||
| 75 | int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey) | |
| 76 | { | |
| 77 | int size = 0; | |
| 78 | ||
| 79 | if (pkey != NULL) { | |
| 80 | size = pkey->cache.security_bits; | |
| 81 | if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL) | |
| 82 | size = pkey->ameth->pkey_security_bits(pkey); | |
| 83 | } | |
| 84 | if (size <= 0) { | |
| 85 | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS); | |
| 86 | return 0; | |
| 87 | } | |
| 88 | return size; | |
| 89 | } | |
| 90 | ||
| 91 | int EVP_PKEY_get_security_category(const EVP_PKEY *pkey) | |
| 92 | { | |
| 93 | return pkey != NULL ? pkey->cache.security_category : -1; | |
| 94 | } | |
| 95 | ||
| 96 | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) | |
| 97 | { | |
| 98 | #ifndef OPENSSL_NO_DSA | |
| 99 | if (pkey->type == EVP_PKEY_DSA) { | |
| 100 | int ret = pkey->save_parameters; | |
| 101 | ||
| 102 | if (mode >= 0) | |
| 103 | pkey->save_parameters = mode; | |
| 104 | return ret; | |
| 105 | } | |
| 106 | #endif | |
| 107 | #ifndef OPENSSL_NO_EC | |
| 108 | if (pkey->type == EVP_PKEY_EC) { | |
| 109 | int ret = pkey->save_parameters; | |
| 110 | ||
| 111 | if (mode >= 0) | |
| 112 | pkey->save_parameters = mode; | |
| 113 | return ret; | |
| 114 | } | |
| 115 | #endif | |
| 116 | return 0; | |
| 117 | } | |
| 118 | ||
| 119 | int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg) | |
| 120 | { | |
| 121 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
| 122 | } | |
| 123 | ||
| 124 | void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx) | |
| 125 | { | |
| 126 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
| 127 | } | |
| 128 | #endif /* !FIPS_MODULE */ | |
| 129 | ||
| 130 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | |
| 131 | { | |
| 132 | /* | |
| 133 | * Clean up legacy stuff from this function when legacy support is gone. | |
| 134 | */ | |
| 135 | ||
| 136 | EVP_PKEY *downgraded_from = NULL; | |
| 137 | int ok = 0; | |
| 138 | ||
| 139 | #ifndef FIPS_MODULE | |
| 140 | /* | |
| 141 | * If |to| is a legacy key and |from| isn't, we must make a downgraded | |
| 142 | * copy of |from|. If that fails, this function fails. | |
| 143 | */ | |
| 144 | if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) { | |
| 145 | if (!evp_pkey_copy_downgraded(&downgraded_from, from)) | |
| 146 | goto end; | |
| 147 | from = downgraded_from; | |
| 148 | } | |
| 149 | #endif /* !FIPS_MODULE */ | |
| 150 | ||
| 151 | /* | |
| 152 | * Make sure |to| is typed. Content is less important at this early | |
| 153 | * stage. | |
| 154 | * | |
| 155 | * 1. If |to| is untyped, assign |from|'s key type to it. | |
| 156 | * 2. If |to| contains a legacy key, compare its |type| to |from|'s. | |
| 157 | * (|from| was already downgraded above) | |
| 158 | * | |
| 159 | * If |to| is a provided key, there's nothing more to do here, functions | |
| 160 | * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called | |
| 161 | * further down help us find out if they are the same or not. | |
| 162 | */ | |
| 163 | if (evp_pkey_is_blank(to)) { | |
| 164 | #ifndef FIPS_MODULE | |
| 165 | if (evp_pkey_is_legacy(from)) { | |
| 166 | if (EVP_PKEY_set_type(to, from->type) == 0) | |
| 167 | goto end; | |
| 168 | } else | |
| 169 | #endif /* !FIPS_MODULE */ | |
| 170 | { | |
| 171 | if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0) | |
| 172 | goto end; | |
| 173 | } | |
| 174 | } | |
| 175 | #ifndef FIPS_MODULE | |
| 176 | else if (evp_pkey_is_legacy(to)) { | |
| 177 | if (to->type != from->type) { | |
| 178 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); | |
| 179 | goto end; | |
| 180 | } | |
| 181 | } | |
| 182 | #endif /* !FIPS_MODULE */ | |
| 183 | ||
| 184 | if (EVP_PKEY_missing_parameters(from)) { | |
| 185 | ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS); | |
| 186 | goto end; | |
| 187 | } | |
| 188 | ||
| 189 | if (!EVP_PKEY_missing_parameters(to)) { | |
| 190 | if (EVP_PKEY_parameters_eq(to, from) == 1) | |
| 191 | ok = 1; | |
| 192 | else | |
| 193 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS); | |
| 194 | goto end; | |
| 195 | } | |
| 196 | ||
| 197 | /* For purely provided keys, we just call the keymgmt utility */ | |
| 198 | if (to->keymgmt != NULL && from->keymgmt != NULL) { | |
| 199 | ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS); | |
| 200 | goto end; | |
| 201 | } | |
| 202 | ||
| 203 | #ifndef FIPS_MODULE | |
| 204 | /* | |
| 205 | * If |to| is provided, we know that |from| is legacy at this point. | |
| 206 | * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup() | |
| 207 | * to copy the appropriate data to |to|'s keydata. | |
| 208 | * We cannot override existing data so do it only if there is no keydata | |
| 209 | * in |to| yet. | |
| 210 | */ | |
| 211 | if (to->keymgmt != NULL && to->keydata == NULL) { | |
| 212 | EVP_KEYMGMT *to_keymgmt = to->keymgmt; | |
| 213 | void *from_keydata = evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt, | |
| 214 | NULL); | |
| 215 | ||
| 216 | /* | |
| 217 | * If we get a NULL, it could be an internal error, or it could be | |
| 218 | * that there's a key mismatch. We're pretending the latter... | |
| 219 | */ | |
| 220 | if (from_keydata == NULL) | |
| 221 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); | |
| 222 | else | |
| 223 | ok = (to->keydata = evp_keymgmt_dup(to->keymgmt, | |
| 224 | from_keydata, | |
| 225 | SELECT_PARAMETERS)) | |
| 226 | != NULL; | |
| 227 | goto end; | |
| 228 | } | |
| 229 | ||
| 230 | /* Both keys are legacy */ | |
| 231 | if (from->ameth != NULL && from->ameth->param_copy != NULL) | |
| 232 | ok = from->ameth->param_copy(to, from); | |
| 233 | #endif /* !FIPS_MODULE */ | |
| 234 | end: | |
| 235 | EVP_PKEY_free(downgraded_from); | |
| 236 | return ok; | |
| 237 | } | |
| 238 | ||
| 239 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) | |
| 240 | { | |
| 241 | if (pkey != NULL) { | |
| 242 | #ifdef FIPS_MODULE | |
| 243 | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS); | |
| 244 | #else | |
| 245 | if (pkey->keymgmt != NULL) | |
| 246 | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS); | |
| 247 | if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL) | |
| 248 | return pkey->ameth->param_missing(pkey); | |
| 249 | #endif /* FIPS_MODULE */ | |
| 250 | } | |
| 251 | return 0; | |
| 252 | } | |
| 253 | ||
| 254 | /* | |
| 255 | * This function is called for any mixture of keys except pure legacy pair. | |
| 256 | * When legacy keys are gone, we replace a call to this functions with | |
| 257 | * a call to evp_keymgmt_util_match(). | |
| 258 | */ | |
| 259 | static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b, | |
| 260 | int selection) | |
| 261 | { | |
| 262 | #ifdef FIPS_MODULE | |
| 263 | return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection); | |
| 264 | #else | |
| 265 | EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL; | |
| 266 | void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL; | |
| 267 | ||
| 268 | /* If none of them are provided, this function shouldn't have been called */ | |
| 269 | if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b))) | |
| 270 | return -2; | |
| 271 | ||
| 272 | /* For purely provided keys, we just call the keymgmt utility */ | |
| 273 | if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b)) | |
| 274 | return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection); | |
| 275 | ||
| 276 | /* | |
| 277 | * At this point, one of them is provided, the other not. This allows | |
| 278 | * us to compare types using legacy NIDs. | |
| 279 | */ | |
| 280 | if (evp_pkey_is_legacy(a) | |
| 281 | && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type))) | |
| 282 | return -1; /* not the same key type */ | |
| 283 | if (evp_pkey_is_legacy(b) | |
| 284 | && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type))) | |
| 285 | return -1; /* not the same key type */ | |
| 286 | ||
| 287 | /* | |
| 288 | * We've determined that they both are the same keytype, so the next | |
| 289 | * step is to do a bit of cross export to ensure we have keydata for | |
| 290 | * both keys in the same keymgmt. | |
| 291 | */ | |
| 292 | keymgmt1 = a->keymgmt; | |
| 293 | keydata1 = a->keydata; | |
| 294 | keymgmt2 = b->keymgmt; | |
| 295 | keydata2 = b->keydata; | |
| 296 | ||
| 297 | if (keymgmt2 != NULL && keymgmt2->match != NULL) { | |
| 298 | tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL); | |
| 299 | if (tmp_keydata != NULL) { | |
| 300 | keymgmt1 = keymgmt2; | |
| 301 | keydata1 = tmp_keydata; | |
| 302 | } | |
| 303 | } | |
| 304 | if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) { | |
| 305 | tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL); | |
| 306 | if (tmp_keydata != NULL) { | |
| 307 | keymgmt2 = keymgmt1; | |
| 308 | keydata2 = tmp_keydata; | |
| 309 | } | |
| 310 | } | |
| 311 | ||
| 312 | /* If we still don't have matching keymgmt implementations, we give up */ | |
| 313 | if (keymgmt1 != keymgmt2) | |
| 314 | return -2; | |
| 315 | ||
| 316 | /* If the keymgmt implementations are NULL, the export failed */ | |
| 317 | if (keymgmt1 == NULL) | |
| 318 | return -2; | |
| 319 | ||
| 320 | return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection); | |
| 321 | #endif /* FIPS_MODULE */ | |
| 322 | } | |
| 323 | ||
| 324 | #ifndef FIPS_MODULE | |
| 325 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
| 326 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | |
| 327 | { | |
| 328 | return EVP_PKEY_parameters_eq(a, b); | |
| 329 | } | |
| 330 | #endif | |
| 331 | #endif /* FIPS_MODULE */ | |
| 332 | ||
| 333 | int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b) | |
| 334 | { | |
| 335 | #ifdef FIPS_MODULE | |
| 336 | return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS); | |
| 337 | #else | |
| 338 | /* | |
| 339 | * This will just call evp_keymgmt_util_match when legacy support | |
| 340 | * is gone. | |
| 341 | */ | |
| 342 | ||
| 343 | if (a->keymgmt != NULL || b->keymgmt != NULL) | |
| 344 | return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS); | |
| 345 | ||
| 346 | /* All legacy keys */ | |
| 347 | if (a->type != b->type) | |
| 348 | return -1; | |
| 349 | if (a->ameth != NULL && a->ameth->param_cmp != NULL) | |
| 350 | return a->ameth->param_cmp(a, b); | |
| 351 | return -2; | |
| 352 | #endif /* !FIPS_MODULE */ | |
| 353 | } | |
| 354 | ||
| 355 | #ifndef FIPS_MODULE | |
| 356 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
| 357 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) | |
| 358 | { | |
| 359 | return EVP_PKEY_eq(a, b); | |
| 360 | } | |
| 361 | #endif | |
| 362 | #endif /* !FIPS_MODULE */ | |
| 363 | ||
| 364 | int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b) | |
| 365 | { | |
| 366 | /* | |
| 367 | * This will just call evp_keymgmt_util_match when legacy support | |
| 368 | * is gone. | |
| 369 | */ | |
| 370 | ||
| 371 | /* Trivial shortcuts */ | |
| 372 | if (a == b) | |
| 373 | return 1; | |
| 374 | if (a == NULL || b == NULL) | |
| 375 | return 0; | |
| 376 | ||
| 377 | #ifndef FIPS_MODULE | |
| 378 | if (a->keymgmt != NULL || b->keymgmt != NULL) | |
| 379 | #endif /* !FIPS_MODULE */ | |
| 380 | { | |
| 381 | int selection = SELECT_PARAMETERS; | |
| 382 | ||
| 383 | if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY) | |
| 384 | && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) | |
| 385 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; | |
| 386 | else | |
| 387 | selection |= OSSL_KEYMGMT_SELECT_KEYPAIR; | |
| 388 | return evp_pkey_cmp_any(a, b, selection); | |
| 389 | } | |
| 390 | ||
| 391 | #ifndef FIPS_MODULE | |
| 392 | /* All legacy keys */ | |
| 393 | if (a->type != b->type) | |
| 394 | return -1; | |
| 395 | ||
| 396 | if (a->ameth != NULL) { | |
| 397 | int ret; | |
| 398 | /* Compare parameters if the algorithm has them */ | |
| 399 | if (a->ameth->param_cmp != NULL) { | |
| 400 | ret = a->ameth->param_cmp(a, b); | |
| 401 | if (ret <= 0) | |
| 402 | return ret; | |
| 403 | } | |
| 404 | ||
| 405 | if (a->ameth->pub_cmp != NULL) | |
| 406 | return a->ameth->pub_cmp(a, b); | |
| 407 | } | |
| 408 | ||
| 409 | return -2; | |
| 410 | #endif /* !FIPS_MODULE */ | |
| 411 | } | |
| 412 | ||
| 413 | #ifndef FIPS_MODULE | |
| 414 | static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx, | |
| 415 | const char *strtype, | |
| 416 | const char *propq, | |
| 417 | int nidtype, | |
| 418 | const unsigned char *key, | |
| 419 | size_t len, | |
| 420 | int key_is_priv) | |
| 421 | { | |
| 422 | EVP_PKEY *pkey = NULL; | |
| 423 | EVP_PKEY_CTX *ctx = NULL; | |
| 424 | int result = 0; | |
| 425 | ||
| 426 | ctx = EVP_PKEY_CTX_new_from_name(libctx, | |
| 427 | strtype != NULL ? strtype | |
| 428 | : OBJ_nid2sn(nidtype), | |
| 429 | propq); | |
| 430 | if (ctx == NULL) | |
| 431 | goto err; | |
| 432 | /* May fail if no provider available */ | |
| 433 | ERR_set_mark(); | |
| 434 | if (EVP_PKEY_fromdata_init(ctx) == 1) { | |
| 435 | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
| 436 | ||
| 437 | ERR_clear_last_mark(); | |
| 438 | params[0] = OSSL_PARAM_construct_octet_string( | |
| 439 | key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY | |
| 440 | : OSSL_PKEY_PARAM_PUB_KEY, | |
| 441 | (void *)key, len); | |
| 442 | ||
| 443 | if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) { | |
| 444 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 445 | goto err; | |
| 446 | } | |
| 447 | ||
| 448 | EVP_PKEY_CTX_free(ctx); | |
| 449 | ||
| 450 | return pkey; | |
| 451 | } | |
| 452 | ERR_pop_to_mark(); | |
| 453 | /* else not supported so fallback to legacy */ | |
| 454 | ||
| 455 | /* Legacy code path */ | |
| 456 | ||
| 457 | pkey = EVP_PKEY_new(); | |
| 458 | if (pkey == NULL) { | |
| 459 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); | |
| 460 | goto err; | |
| 461 | } | |
| 462 | ||
| 463 | if (!pkey_set_type(pkey, nidtype, strtype, -1, NULL)) { | |
| 464 | /* ERR_raise(ERR_LIB_EVP, ...) already called */ | |
| 465 | goto err; | |
| 466 | } | |
| 467 | ||
| 468 | if (!ossl_assert(pkey->ameth != NULL)) | |
| 469 | goto err; | |
| 470 | ||
| 471 | if (key_is_priv) { | |
| 472 | if (pkey->ameth->set_priv_key == NULL) { | |
| 473 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 474 | goto err; | |
| 475 | } | |
| 476 | ||
| 477 | if (!pkey->ameth->set_priv_key(pkey, key, len)) { | |
| 478 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 479 | goto err; | |
| 480 | } | |
| 481 | } else { | |
| 482 | if (pkey->ameth->set_pub_key == NULL) { | |
| 483 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 484 | goto err; | |
| 485 | } | |
| 486 | ||
| 487 | if (!pkey->ameth->set_pub_key(pkey, key, len)) { | |
| 488 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 489 | goto err; | |
| 490 | } | |
| 491 | } | |
| 492 | ||
| 493 | result = 1; | |
| 494 | err: | |
| 495 | if (!result) { | |
| 496 | EVP_PKEY_free(pkey); | |
| 497 | pkey = NULL; | |
| 498 | } | |
| 499 | EVP_PKEY_CTX_free(ctx); | |
| 500 | return pkey; | |
| 501 | } | |
| 502 | ||
| 503 | EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx, | |
| 504 | const char *keytype, | |
| 505 | const char *propq, | |
| 506 | const unsigned char *priv, size_t len) | |
| 507 | { | |
| 508 | return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, priv, | |
| 509 | len, 1); | |
| 510 | } | |
| 511 | ||
| 512 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, | |
| 513 | const unsigned char *priv, | |
| 514 | size_t len) | |
| 515 | { | |
| 516 | if (!ossl_assert(e == NULL)) | |
| 517 | return NULL; | |
| 518 | return new_raw_key_int(NULL, NULL, NULL, type, priv, len, 1); | |
| 519 | } | |
| 520 | ||
| 521 | EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx, | |
| 522 | const char *keytype, const char *propq, | |
| 523 | const unsigned char *pub, size_t len) | |
| 524 | { | |
| 525 | return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, pub, | |
| 526 | len, 0); | |
| 527 | } | |
| 528 | ||
| 529 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, | |
| 530 | const unsigned char *pub, | |
| 531 | size_t len) | |
| 532 | { | |
| 533 | if (!ossl_assert(e == NULL)) | |
| 534 | return NULL; | |
| 535 | return new_raw_key_int(NULL, NULL, NULL, type, pub, len, 0); | |
| 536 | } | |
| 537 | ||
| 538 | struct raw_key_details_st { | |
| 539 | unsigned char **key; | |
| 540 | size_t *len; | |
| 541 | int selection; | |
| 542 | }; | |
| 543 | ||
| 544 | static OSSL_CALLBACK get_raw_key_details; | |
| 545 | static int get_raw_key_details(const OSSL_PARAM params[], void *arg) | |
| 546 | { | |
| 547 | const OSSL_PARAM *p = NULL; | |
| 548 | struct raw_key_details_st *raw_key = arg; | |
| 549 | ||
| 550 | if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) { | |
| 551 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY)) | |
| 552 | != NULL) | |
| 553 | return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key, | |
| 554 | raw_key->key == NULL ? 0 : *raw_key->len, | |
| 555 | raw_key->len); | |
| 556 | } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) { | |
| 557 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY)) | |
| 558 | != NULL) | |
| 559 | return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key, | |
| 560 | raw_key->key == NULL ? 0 : *raw_key->len, | |
| 561 | raw_key->len); | |
| 562 | } | |
| 563 | ||
| 564 | return 0; | |
| 565 | } | |
| 566 | ||
| 567 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, | |
| 568 | size_t *len) | |
| 569 | { | |
| 570 | if (pkey->keymgmt != NULL) { | |
| 571 | struct raw_key_details_st raw_key; | |
| 572 | ||
| 573 | raw_key.key = priv == NULL ? NULL : &priv; | |
| 574 | raw_key.len = len; | |
| 575 | raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY; | |
| 576 | ||
| 577 | return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, | |
| 578 | get_raw_key_details, &raw_key); | |
| 579 | } | |
| 580 | ||
| 581 | if (pkey->ameth == NULL) { | |
| 582 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 583 | return 0; | |
| 584 | } | |
| 585 | ||
| 586 | if (pkey->ameth->get_priv_key == NULL) { | |
| 587 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 588 | return 0; | |
| 589 | } | |
| 590 | ||
| 591 | if (!pkey->ameth->get_priv_key(pkey, priv, len)) { | |
| 592 | ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED); | |
| 593 | return 0; | |
| 594 | } | |
| 595 | ||
| 596 | return 1; | |
| 597 | } | |
| 598 | ||
| 599 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, | |
| 600 | size_t *len) | |
| 601 | { | |
| 602 | if (pkey->keymgmt != NULL) { | |
| 603 | struct raw_key_details_st raw_key; | |
| 604 | ||
| 605 | raw_key.key = pub == NULL ? NULL : &pub; | |
| 606 | raw_key.len = len; | |
| 607 | raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY; | |
| 608 | ||
| 609 | return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, | |
| 610 | get_raw_key_details, &raw_key); | |
| 611 | } | |
| 612 | ||
| 613 | if (pkey->ameth == NULL) { | |
| 614 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 615 | return 0; | |
| 616 | } | |
| 617 | ||
| 618 | if (pkey->ameth->get_pub_key == NULL) { | |
| 619 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 620 | return 0; | |
| 621 | } | |
| 622 | ||
| 623 | if (!pkey->ameth->get_pub_key(pkey, pub, len)) { | |
| 624 | ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED); | |
| 625 | return 0; | |
| 626 | } | |
| 627 | ||
| 628 | return 1; | |
| 629 | } | |
| 630 | ||
| 631 | static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len, | |
| 632 | const char *cipher_name, | |
| 633 | const EVP_CIPHER *cipher, | |
| 634 | OSSL_LIB_CTX *libctx, | |
| 635 | const char *propq) | |
| 636 | { | |
| 637 | #ifndef OPENSSL_NO_CMAC | |
| 638 | OSSL_PARAM params[5], *p = params; | |
| 639 | EVP_PKEY *pkey = NULL; | |
| 640 | EVP_PKEY_CTX *ctx; | |
| 641 | ||
| 642 | if (cipher != NULL) | |
| 643 | cipher_name = EVP_CIPHER_get0_name(cipher); | |
| 644 | ||
| 645 | if (cipher_name == NULL) { | |
| 646 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 647 | return NULL; | |
| 648 | } | |
| 649 | ||
| 650 | ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq); | |
| 651 | if (ctx == NULL) | |
| 652 | goto err; | |
| 653 | ||
| 654 | if (EVP_PKEY_fromdata_init(ctx) <= 0) { | |
| 655 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 656 | goto err; | |
| 657 | } | |
| 658 | ||
| 659 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
| 660 | (void *)priv, len); | |
| 661 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER, | |
| 662 | (char *)cipher_name, 0); | |
| 663 | if (propq != NULL) | |
| 664 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES, | |
| 665 | (char *)propq, 0); | |
| 666 | *p = OSSL_PARAM_construct_end(); | |
| 667 | ||
| 668 | if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) { | |
| 669 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); | |
| 670 | goto err; | |
| 671 | } | |
| 672 | ||
| 673 | err: | |
| 674 | EVP_PKEY_CTX_free(ctx); | |
| 675 | ||
| 676 | return pkey; | |
| 677 | #else | |
| 678 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
| 679 | return NULL; | |
| 680 | #endif | |
| 681 | } | |
| 682 | ||
| 683 | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, | |
| 684 | size_t len, const EVP_CIPHER *cipher) | |
| 685 | { | |
| 686 | if (!ossl_assert(e == NULL)) | |
| 687 | return NULL; | |
| 688 | return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL); | |
| 689 | } | |
| 690 | ||
| 691 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) | |
| 692 | { | |
| 693 | return pkey_set_type(pkey, type, NULL, -1, NULL); | |
| 694 | } | |
| 695 | ||
| 696 | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) | |
| 697 | { | |
| 698 | return pkey_set_type(pkey, EVP_PKEY_NONE, str, len, NULL); | |
| 699 | } | |
| 700 | ||
| 701 | #ifndef OPENSSL_NO_DEPRECATED_3_0 | |
| 702 | static void detect_foreign_key(EVP_PKEY *pkey) | |
| 703 | { | |
| 704 | switch (pkey->type) { | |
| 705 | case EVP_PKEY_RSA: | |
| 706 | case EVP_PKEY_RSA_PSS: | |
| 707 | pkey->foreign = pkey->pkey.rsa != NULL | |
| 708 | && ossl_rsa_is_foreign(pkey->pkey.rsa); | |
| 709 | break; | |
| 710 | #ifndef OPENSSL_NO_EC | |
| 711 | case EVP_PKEY_SM2: | |
| 712 | break; | |
| 713 | case EVP_PKEY_EC: | |
| 714 | pkey->foreign = pkey->pkey.ec != NULL | |
| 715 | && ossl_ec_key_is_foreign(pkey->pkey.ec); | |
| 716 | break; | |
| 717 | #endif | |
| 718 | #ifndef OPENSSL_NO_DSA | |
| 719 | case EVP_PKEY_DSA: | |
| 720 | pkey->foreign = pkey->pkey.dsa != NULL | |
| 721 | && ossl_dsa_is_foreign(pkey->pkey.dsa); | |
| 722 | break; | |
| 723 | #endif | |
| 724 | #ifndef OPENSSL_NO_DH | |
| 725 | case EVP_PKEY_DH: | |
| 726 | pkey->foreign = pkey->pkey.dh != NULL | |
| 727 | && ossl_dh_is_foreign(pkey->pkey.dh); | |
| 728 | break; | |
| 729 | #endif | |
| 730 | default: | |
| 731 | pkey->foreign = 0; | |
| 732 | break; | |
| 733 | } | |
| 734 | } | |
| 735 | ||
| 736 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) | |
| 737 | { | |
| 738 | #ifndef OPENSSL_NO_EC | |
| 739 | int pktype; | |
| 740 | ||
| 741 | pktype = EVP_PKEY_type(type); | |
| 742 | if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) { | |
| 743 | const EC_GROUP *group = EC_KEY_get0_group(key); | |
| 744 | ||
| 745 | if (group != NULL) { | |
| 746 | int curve = EC_GROUP_get_curve_name(group); | |
| 747 | ||
| 748 | /* | |
| 749 | * Regardless of what is requested the SM2 curve must be SM2 type, | |
| 750 | * and non SM2 curves are EC type. | |
| 751 | */ | |
| 752 | if (curve == NID_sm2 && pktype == EVP_PKEY_EC) | |
| 753 | type = EVP_PKEY_SM2; | |
| 754 | else if (curve != NID_sm2 && pktype == EVP_PKEY_SM2) | |
| 755 | type = EVP_PKEY_EC; | |
| 756 | } | |
| 757 | } | |
| 758 | #endif | |
| 759 | ||
| 760 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) | |
| 761 | return 0; | |
| 762 | ||
| 763 | pkey->pkey.ptr = key; | |
| 764 | detect_foreign_key(pkey); | |
| 765 | ||
| 766 | return (key != NULL); | |
| 767 | } | |
| 768 | #endif | |
| 769 | ||
| 770 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) | |
| 771 | { | |
| 772 | if (pkey == NULL) | |
| 773 | return NULL; | |
| 774 | ||
| 775 | if (!evp_pkey_is_provided(pkey)) | |
| 776 | return pkey->pkey.ptr; | |
| 777 | ||
| 778 | return NULL; | |
| 779 | } | |
| 780 | ||
| 781 | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) | |
| 782 | { | |
| 783 | const ASN1_OCTET_STRING *os = NULL; | |
| 784 | if (pkey->type != EVP_PKEY_HMAC) { | |
| 785 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY); | |
| 786 | return NULL; | |
| 787 | } | |
| 788 | os = evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 789 | if (os != NULL) { | |
| 790 | *len = os->length; | |
| 791 | return os->data; | |
| 792 | } | |
| 793 | return NULL; | |
| 794 | } | |
| 795 | ||
| 796 | #ifndef OPENSSL_NO_POLY1305 | |
| 797 | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) | |
| 798 | { | |
| 799 | const ASN1_OCTET_STRING *os = NULL; | |
| 800 | if (pkey->type != EVP_PKEY_POLY1305) { | |
| 801 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY); | |
| 802 | return NULL; | |
| 803 | } | |
| 804 | os = evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 805 | if (os != NULL) { | |
| 806 | *len = os->length; | |
| 807 | return os->data; | |
| 808 | } | |
| 809 | return NULL; | |
| 810 | } | |
| 811 | #endif | |
| 812 | ||
| 813 | #ifndef OPENSSL_NO_SIPHASH | |
| 814 | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) | |
| 815 | { | |
| 816 | const ASN1_OCTET_STRING *os = NULL; | |
| 817 | ||
| 818 | if (pkey->type != EVP_PKEY_SIPHASH) { | |
| 819 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY); | |
| 820 | return NULL; | |
| 821 | } | |
| 822 | os = evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 823 | if (os != NULL) { | |
| 824 | *len = os->length; | |
| 825 | return os->data; | |
| 826 | } | |
| 827 | return NULL; | |
| 828 | } | |
| 829 | #endif | |
| 830 | ||
| 831 | #ifndef OPENSSL_NO_DSA | |
| 832 | static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey) | |
| 833 | { | |
| 834 | if (pkey->type != EVP_PKEY_DSA) { | |
| 835 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY); | |
| 836 | return NULL; | |
| 837 | } | |
| 838 | return evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 839 | } | |
| 840 | ||
| 841 | const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) | |
| 842 | { | |
| 843 | return evp_pkey_get0_DSA_int(pkey); | |
| 844 | } | |
| 845 | ||
| 846 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) | |
| 847 | { | |
| 848 | int ret; | |
| 849 | ||
| 850 | if (!DSA_up_ref(key)) | |
| 851 | return 0; | |
| 852 | ||
| 853 | ret = EVP_PKEY_assign_DSA(pkey, key); | |
| 854 | ||
| 855 | if (!ret) | |
| 856 | DSA_free(key); | |
| 857 | ||
| 858 | return ret; | |
| 859 | } | |
| 860 | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) | |
| 861 | { | |
| 862 | DSA *ret = evp_pkey_get0_DSA_int(pkey); | |
| 863 | ||
| 864 | if (ret != NULL && !DSA_up_ref(ret)) | |
| 865 | return NULL; | |
| 866 | ||
| 867 | return ret; | |
| 868 | } | |
| 869 | #endif /* OPENSSL_NO_DSA */ | |
| 870 | ||
| 871 | #ifndef OPENSSL_NO_ECX | |
| 872 | static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type) | |
| 873 | { | |
| 874 | if (EVP_PKEY_get_base_id(pkey) != type) { | |
| 875 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY); | |
| 876 | return NULL; | |
| 877 | } | |
| 878 | return evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 879 | } | |
| 880 | ||
| 881 | static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type) | |
| 882 | { | |
| 883 | ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type); | |
| 884 | ||
| 885 | if (ret != NULL && !ossl_ecx_key_up_ref(ret)) | |
| 886 | ret = NULL; | |
| 887 | return ret; | |
| 888 | } | |
| 889 | ||
| 890 | #define IMPLEMENT_ECX_VARIANT(NAME) \ | |
| 891 | ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \ | |
| 892 | { \ | |
| 893 | return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \ | |
| 894 | } | |
| 895 | IMPLEMENT_ECX_VARIANT(X25519) | |
| 896 | IMPLEMENT_ECX_VARIANT(X448) | |
| 897 | IMPLEMENT_ECX_VARIANT(ED25519) | |
| 898 | IMPLEMENT_ECX_VARIANT(ED448) | |
| 899 | ||
| 900 | #endif /* OPENSSL_NO_ECX */ | |
| 901 | ||
| 902 | #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0) | |
| 903 | ||
| 904 | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey) | |
| 905 | { | |
| 906 | int ret, type; | |
| 907 | ||
| 908 | /* | |
| 909 | * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups | |
| 910 | * related to ffdhe and modp (which cache q = (p - 1) / 2), | |
| 911 | * and returns 0 for all other dh parameter generation types including | |
| 912 | * RFC5114 named groups. | |
| 913 | * | |
| 914 | * The EVP_PKEY_DH type is used for dh parameter generation types: | |
| 915 | * - named safe prime groups related to ffdhe and modp | |
| 916 | * - safe prime generator | |
| 917 | * | |
| 918 | * The type EVP_PKEY_DHX is used for dh parameter generation types | |
| 919 | * - fips186-4 and fips186-2 | |
| 920 | * - rfc5114 named groups. | |
| 921 | * | |
| 922 | * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored | |
| 923 | * without a q value. | |
| 924 | * The EVP_PKEY_DHX type is used to save X9.42 data that requires the | |
| 925 | * q value to be stored. | |
| 926 | */ | |
| 927 | if (ossl_dh_is_named_safe_prime_group(dhkey)) | |
| 928 | type = EVP_PKEY_DH; | |
| 929 | else | |
| 930 | type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX; | |
| 931 | ||
| 932 | if (!DH_up_ref(dhkey)) | |
| 933 | return 0; | |
| 934 | ||
| 935 | ret = EVP_PKEY_assign(pkey, type, dhkey); | |
| 936 | ||
| 937 | if (!ret) | |
| 938 | DH_free(dhkey); | |
| 939 | ||
| 940 | return ret; | |
| 941 | } | |
| 942 | ||
| 943 | DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey) | |
| 944 | { | |
| 945 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { | |
| 946 | ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY); | |
| 947 | return NULL; | |
| 948 | } | |
| 949 | return evp_pkey_get_legacy((EVP_PKEY *)pkey); | |
| 950 | } | |
| 951 | ||
| 952 | const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) | |
| 953 | { | |
| 954 | return evp_pkey_get0_DH_int(pkey); | |
| 955 | } | |
| 956 | ||
| 957 | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) | |
| 958 | { | |
| 959 | DH *ret = evp_pkey_get0_DH_int(pkey); | |
| 960 | ||
| 961 | if (ret != NULL && !DH_up_ref(ret)) | |
| 962 | ret = NULL; | |
| 963 | ||
| 964 | return ret; | |
| 965 | } | |
| 966 | #endif | |
| 967 | ||
| 968 | int EVP_PKEY_get_id(const EVP_PKEY *pkey) | |
| 969 | { | |
| 970 | return pkey->type; | |
| 971 | } | |
| 972 | ||
| 973 | int EVP_PKEY_get_base_id(const EVP_PKEY *pkey) | |
| 974 | { | |
| 975 | return EVP_PKEY_type(pkey->type); | |
| 976 | } | |
| 977 | ||
| 978 | /* | |
| 979 | * These hard coded cases are pure hackery to get around the fact | |
| 980 | * that names in crypto/objects/objects.txt are a mess. There is | |
| 981 | * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's | |
| 982 | * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1, | |
| 983 | * the NID of which is used for EVP_PKEY_RSA. Strangely enough, | |
| 984 | * "DSA" is accurate... but still, better be safe and hard-code | |
| 985 | * names that we know. | |
| 986 | * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in | |
| 987 | * EVP_PKEY_EC, because of aliasing. | |
| 988 | * This should be cleaned away along with all other #legacy support. | |
| 989 | */ | |
| 990 | static const OSSL_ITEM standard_name2type[] = { | |
| 991 | { EVP_PKEY_RSA, "RSA" }, | |
| 992 | { EVP_PKEY_RSA_PSS, "RSA-PSS" }, | |
| 993 | { EVP_PKEY_EC, "EC" }, | |
| 994 | { EVP_PKEY_ED25519, "ED25519" }, | |
| 995 | { EVP_PKEY_ED448, "ED448" }, | |
| 996 | { EVP_PKEY_X25519, "X25519" }, | |
| 997 | { EVP_PKEY_X448, "X448" }, | |
| 998 | { EVP_PKEY_SM2, "SM2" }, | |
| 999 | { EVP_PKEY_DH, "DH" }, | |
| 1000 | { EVP_PKEY_DHX, "X9.42 DH" }, | |
| 1001 | { EVP_PKEY_DHX, "DHX" }, | |
| 1002 | { EVP_PKEY_DSA, "DSA" }, | |
| 1003 | }; | |
| 1004 | ||
| 1005 | int evp_pkey_name2type(const char *name) | |
| 1006 | { | |
| 1007 | int type; | |
| 1008 | size_t i; | |
| 1009 | ||
| 1010 | for (i = 0; i < OSSL_NELEM(standard_name2type); i++) { | |
| 1011 | if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0) | |
| 1012 | return (int)standard_name2type[i].id; | |
| 1013 | } | |
| 1014 | ||
| 1015 | if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef) | |
| 1016 | return type; | |
| 1017 | return EVP_PKEY_type(OBJ_ln2nid(name)); | |
| 1018 | } | |
| 1019 | ||
| 1020 | const char *evp_pkey_type2name(int type) | |
| 1021 | { | |
| 1022 | size_t i; | |
| 1023 | ||
| 1024 | for (i = 0; i < OSSL_NELEM(standard_name2type); i++) { | |
| 1025 | if (type == (int)standard_name2type[i].id) | |
| 1026 | return standard_name2type[i].ptr; | |
| 1027 | } | |
| 1028 | ||
| 1029 | return OBJ_nid2sn(type); | |
| 1030 | } | |
| 1031 | ||
| 1032 | int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name) | |
| 1033 | { | |
| 1034 | if (pkey == NULL) | |
| 1035 | return 0; | |
| 1036 | if (pkey->keymgmt == NULL) | |
| 1037 | return pkey->type == evp_pkey_name2type(name); | |
| 1038 | return EVP_KEYMGMT_is_a(pkey->keymgmt, name); | |
| 1039 | } | |
| 1040 | ||
| 1041 | int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey, | |
| 1042 | void (*fn)(const char *name, void *data), | |
| 1043 | void *data) | |
| 1044 | { | |
| 1045 | if (!evp_pkey_is_typed(pkey)) | |
| 1046 | return 0; | |
| 1047 | ||
| 1048 | if (!evp_pkey_is_provided(pkey)) { | |
| 1049 | const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey)); | |
| 1050 | ||
| 1051 | fn(name, data); | |
| 1052 | return 1; | |
| 1053 | } | |
| 1054 | return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data); | |
| 1055 | } | |
| 1056 | ||
| 1057 | int EVP_PKEY_can_sign(const EVP_PKEY *pkey) | |
| 1058 | { | |
| 1059 | if (pkey->keymgmt == NULL) { | |
| 1060 | switch (EVP_PKEY_get_base_id(pkey)) { | |
| 1061 | case EVP_PKEY_RSA: | |
| 1062 | case EVP_PKEY_RSA_PSS: | |
| 1063 | return 1; | |
| 1064 | #ifndef OPENSSL_NO_DSA | |
| 1065 | case EVP_PKEY_DSA: | |
| 1066 | return 1; | |
| 1067 | #endif | |
| 1068 | #ifndef OPENSSL_NO_EC | |
| 1069 | case EVP_PKEY_ED25519: | |
| 1070 | case EVP_PKEY_ED448: | |
| 1071 | return 1; | |
| 1072 | case EVP_PKEY_EC: /* Including SM2 */ | |
| 1073 | return EC_KEY_can_sign(pkey->pkey.ec); | |
| 1074 | #endif | |
| 1075 | default: | |
| 1076 | break; | |
| 1077 | } | |
| 1078 | } else { | |
| 1079 | const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt); | |
| 1080 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); | |
| 1081 | EVP_SIGNATURE *sig; | |
| 1082 | const char *name; | |
| 1083 | ||
| 1084 | name = evp_keymgmt_util_query_operation_name(pkey->keymgmt, | |
| 1085 | OSSL_OP_SIGNATURE); | |
| 1086 | sig = EVP_SIGNATURE_fetch(libctx, name, NULL); | |
| 1087 | if (sig != NULL) { | |
| 1088 | EVP_SIGNATURE_free(sig); | |
| 1089 | return 1; | |
| 1090 | } | |
| 1091 | } | |
| 1092 | return 0; | |
| 1093 | } | |
| 1094 | ||
| 1095 | static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent) | |
| 1096 | { | |
| 1097 | BIO_set_indent(*out, saved_indent); | |
| 1098 | if (pop_f_prefix) { | |
| 1099 | BIO *next = BIO_pop(*out); | |
| 1100 | ||
| 1101 | BIO_free(*out); | |
| 1102 | *out = next; | |
| 1103 | } | |
| 1104 | return 1; | |
| 1105 | } | |
| 1106 | ||
| 1107 | static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent, | |
| 1108 | long indent) | |
| 1109 | { | |
| 1110 | *pop_f_prefix = 0; | |
| 1111 | *saved_indent = 0; | |
| 1112 | if (indent > 0) { | |
| 1113 | long i = BIO_get_indent(*out); | |
| 1114 | ||
| 1115 | *saved_indent = (i < 0 ? 0 : i); | |
| 1116 | if (BIO_set_indent(*out, indent) <= 0) { | |
| 1117 | BIO *prefbio = BIO_new(BIO_f_prefix()); | |
| 1118 | ||
| 1119 | if (prefbio == NULL) | |
| 1120 | return 0; | |
| 1121 | *out = BIO_push(prefbio, *out); | |
| 1122 | *pop_f_prefix = 1; | |
| 1123 | } | |
| 1124 | if (BIO_set_indent(*out, indent) <= 0) { | |
| 1125 | print_reset_indent(out, *pop_f_prefix, *saved_indent); | |
| 1126 | return 0; | |
| 1127 | } | |
| 1128 | } | |
| 1129 | return 1; | |
| 1130 | } | |
| 1131 | ||
| 1132 | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, | |
| 1133 | const char *kstr) | |
| 1134 | { | |
| 1135 | return BIO_indent(out, indent, 128) | |
| 1136 | && BIO_printf(out, "%s algorithm \"%s\" unsupported\n", | |
| 1137 | kstr, OBJ_nid2ln(pkey->type)) | |
| 1138 | > 0; | |
| 1139 | } | |
| 1140 | ||
| 1141 | static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent, | |
| 1142 | int selection /* For provided encoding */, | |
| 1143 | const char *propquery /* For provided encoding */, | |
| 1144 | int (*legacy_print)(BIO *out, const EVP_PKEY *pkey, | |
| 1145 | int indent, ASN1_PCTX *pctx), | |
| 1146 | ASN1_PCTX *legacy_pctx /* For legacy print */) | |
| 1147 | { | |
| 1148 | int pop_f_prefix; | |
| 1149 | long saved_indent; | |
| 1150 | OSSL_ENCODER_CTX *ctx = NULL; | |
| 1151 | int ret = -2; /* default to unsupported */ | |
| 1152 | ||
| 1153 | if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent)) | |
| 1154 | return 0; | |
| 1155 | ||
| 1156 | ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL, | |
| 1157 | propquery); | |
| 1158 | if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) | |
| 1159 | ret = OSSL_ENCODER_to_bio(ctx, out); | |
| 1160 | OSSL_ENCODER_CTX_free(ctx); | |
| 1161 | ||
| 1162 | if (ret != -2) | |
| 1163 | goto end; | |
| 1164 | ||
| 1165 | /* legacy fallback */ | |
| 1166 | if (legacy_print != NULL) | |
| 1167 | ret = legacy_print(out, pkey, 0, legacy_pctx); | |
| 1168 | else | |
| 1169 | ret = unsup_alg(out, pkey, 0, "Public Key"); | |
| 1170 | ||
| 1171 | end: | |
| 1172 | print_reset_indent(&out, pop_f_prefix, saved_indent); | |
| 1173 | return ret; | |
| 1174 | } | |
| 1175 | ||
| 1176 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, | |
| 1177 | int indent, ASN1_PCTX *pctx) | |
| 1178 | { | |
| 1179 | return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL, | |
| 1180 | (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL), | |
| 1181 | pctx); | |
| 1182 | } | |
| 1183 | ||
| 1184 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, | |
| 1185 | int indent, ASN1_PCTX *pctx) | |
| 1186 | { | |
| 1187 | return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL, | |
| 1188 | (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL), | |
| 1189 | pctx); | |
| 1190 | } | |
| 1191 | ||
| 1192 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, | |
| 1193 | int indent, ASN1_PCTX *pctx) | |
| 1194 | { | |
| 1195 | return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL, | |
| 1196 | (pkey->ameth != NULL ? pkey->ameth->param_print : NULL), | |
| 1197 | pctx); | |
| 1198 | } | |
| 1199 | ||
| 1200 | #ifndef OPENSSL_NO_STDIO | |
| 1201 | int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey, | |
| 1202 | int indent, ASN1_PCTX *pctx) | |
| 1203 | { | |
| 1204 | int ret; | |
| 1205 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); | |
| 1206 | ||
| 1207 | if (b == NULL) | |
| 1208 | return 0; | |
| 1209 | ret = EVP_PKEY_print_public(b, pkey, indent, pctx); | |
| 1210 | BIO_free(b); | |
| 1211 | return ret; | |
| 1212 | } | |
| 1213 | ||
| 1214 | int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey, | |
| 1215 | int indent, ASN1_PCTX *pctx) | |
| 1216 | { | |
| 1217 | int ret; | |
| 1218 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); | |
| 1219 | ||
| 1220 | if (b == NULL) | |
| 1221 | return 0; | |
| 1222 | ret = EVP_PKEY_print_private(b, pkey, indent, pctx); | |
| 1223 | BIO_free(b); | |
| 1224 | return ret; | |
| 1225 | } | |
| 1226 | ||
| 1227 | int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey, | |
| 1228 | int indent, ASN1_PCTX *pctx) | |
| 1229 | { | |
| 1230 | int ret; | |
| 1231 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); | |
| 1232 | ||
| 1233 | if (b == NULL) | |
| 1234 | return 0; | |
| 1235 | ret = EVP_PKEY_print_params(b, pkey, indent, pctx); | |
| 1236 | BIO_free(b); | |
| 1237 | return ret; | |
| 1238 | } | |
| 1239 | #endif | |
| 1240 | ||
| 1241 | static void mdname2nid(const char *mdname, void *data) | |
| 1242 | { | |
| 1243 | int *nid = (int *)data; | |
| 1244 | ||
| 1245 | if (*nid != NID_undef) | |
| 1246 | return; | |
| 1247 | ||
| 1248 | *nid = OBJ_sn2nid(mdname); | |
| 1249 | if (*nid == NID_undef) | |
| 1250 | *nid = OBJ_ln2nid(mdname); | |
| 1251 | } | |
| 1252 | ||
| 1253 | static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op, | |
| 1254 | int arg1, void *arg2) | |
| 1255 | { | |
| 1256 | if (pkey->keymgmt == NULL) | |
| 1257 | return 0; | |
| 1258 | switch (op) { | |
| 1259 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: { | |
| 1260 | char mdname[80] = ""; | |
| 1261 | int rv = EVP_PKEY_get_default_digest_name(pkey, mdname, | |
| 1262 | sizeof(mdname)); | |
| 1263 | ||
| 1264 | if (rv > 0) { | |
| 1265 | int mdnum; | |
| 1266 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov); | |
| 1267 | /* Make sure the MD is in the namemap if available */ | |
| 1268 | EVP_MD *md; | |
| 1269 | OSSL_NAMEMAP *namemap; | |
| 1270 | int nid = NID_undef; | |
| 1271 | ||
| 1272 | (void)ERR_set_mark(); | |
| 1273 | md = EVP_MD_fetch(libctx, mdname, NULL); | |
| 1274 | (void)ERR_pop_to_mark(); | |
| 1275 | namemap = ossl_namemap_stored(libctx); | |
| 1276 | ||
| 1277 | /* | |
| 1278 | * The only reason to fetch the MD was to make sure it is in the | |
| 1279 | * namemap. We can immediately free it. | |
| 1280 | */ | |
| 1281 | EVP_MD_free(md); | |
| 1282 | mdnum = ossl_namemap_name2num(namemap, mdname); | |
| 1283 | if (mdnum == 0) | |
| 1284 | return 0; | |
| 1285 | ||
| 1286 | /* | |
| 1287 | * We have the namemap number - now we need to find the | |
| 1288 | * associated nid | |
| 1289 | */ | |
| 1290 | if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid)) | |
| 1291 | return 0; | |
| 1292 | *(int *)arg2 = nid; | |
| 1293 | } | |
| 1294 | return rv; | |
| 1295 | } | |
| 1296 | default: | |
| 1297 | return -2; | |
| 1298 | } | |
| 1299 | } | |
| 1300 | ||
| 1301 | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) | |
| 1302 | { | |
| 1303 | if (pkey->ameth == NULL) | |
| 1304 | return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2); | |
| 1305 | if (pkey->ameth->pkey_ctrl == NULL) | |
| 1306 | return -2; | |
| 1307 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); | |
| 1308 | } | |
| 1309 | ||
| 1310 | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) | |
| 1311 | { | |
| 1312 | if (pkey == NULL) | |
| 1313 | return 0; | |
| 1314 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); | |
| 1315 | } | |
| 1316 | ||
| 1317 | int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey, | |
| 1318 | char *mdname, size_t mdname_sz) | |
| 1319 | { | |
| 1320 | if (pkey->ameth == NULL) | |
| 1321 | return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt, | |
| 1322 | pkey->keydata, | |
| 1323 | mdname, mdname_sz); | |
| 1324 | ||
| 1325 | { | |
| 1326 | int nid = NID_undef; | |
| 1327 | int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid); | |
| 1328 | const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL; | |
| 1329 | ||
| 1330 | if (rv > 0) | |
| 1331 | OPENSSL_strlcpy(mdname, name, mdname_sz); | |
| 1332 | return rv; | |
| 1333 | } | |
| 1334 | } | |
| 1335 | ||
| 1336 | int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz, | |
| 1337 | size_t *gname_len) | |
| 1338 | { | |
| 1339 | return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME, | |
| 1340 | gname, gname_sz, gname_len); | |
| 1341 | } | |
| 1342 | ||
| 1343 | int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx, | |
| 1344 | const char *name, const char *propq) | |
| 1345 | { | |
| 1346 | int rv; | |
| 1347 | EVP_MD_CTX *ctx = NULL; | |
| 1348 | ||
| 1349 | if ((ctx = EVP_MD_CTX_new()) == NULL) | |
| 1350 | return -1; | |
| 1351 | ||
| 1352 | ERR_set_mark(); | |
| 1353 | rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx, | |
| 1354 | propq, pkey, NULL); | |
| 1355 | ERR_pop_to_mark(); | |
| 1356 | ||
| 1357 | EVP_MD_CTX_free(ctx); | |
| 1358 | return rv; | |
| 1359 | } | |
| 1360 | #endif /* !FIPS_MODULE */ | |
| 1361 | ||
| 1362 | int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub, | |
| 1363 | size_t publen) | |
| 1364 | { | |
| 1365 | if (pkey == NULL) | |
| 1366 | return 0; | |
| 1367 | #ifndef FIPS_MODULE | |
| 1368 | if (evp_pkey_is_provided(pkey)) | |
| 1369 | #endif /* !FIPS_MODULE */ | |
| 1370 | return EVP_PKEY_set_octet_string_param(pkey, | |
| 1371 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, | |
| 1372 | (unsigned char *)pub, publen); | |
| 1373 | ||
| 1374 | #ifndef FIPS_MODULE | |
| 1375 | if (publen > INT_MAX) | |
| 1376 | return 0; | |
| 1377 | /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */ | |
| 1378 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, (int)publen, | |
| 1379 | (void *)pub) | |
| 1380 | <= 0) | |
| 1381 | return 0; | |
| 1382 | return 1; | |
| 1383 | #endif /* !FIPS_MODULE */ | |
| 1384 | } | |
| 1385 | ||
| 1386 | size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub) | |
| 1387 | { | |
| 1388 | if (pkey == NULL) | |
| 1389 | return 0; | |
| 1390 | #ifndef FIPS_MODULE | |
| 1391 | if (evp_pkey_is_provided(pkey)) | |
| 1392 | #endif | |
| 1393 | { | |
| 1394 | size_t return_size = OSSL_PARAM_UNMODIFIED; | |
| 1395 | unsigned char *buf; | |
| 1396 | ||
| 1397 | /* | |
| 1398 | * We know that this is going to fail, but it will give us a size | |
| 1399 | * to allocate. | |
| 1400 | */ | |
| 1401 | EVP_PKEY_get_octet_string_param(pkey, | |
| 1402 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, | |
| 1403 | NULL, 0, &return_size); | |
| 1404 | if (return_size == OSSL_PARAM_UNMODIFIED) | |
| 1405 | return 0; | |
| 1406 | ||
| 1407 | *ppub = NULL; | |
| 1408 | buf = OPENSSL_malloc(return_size); | |
| 1409 | if (buf == NULL) | |
| 1410 | return 0; | |
| 1411 | ||
| 1412 | if (!EVP_PKEY_get_octet_string_param(pkey, | |
| 1413 | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, | |
| 1414 | buf, return_size, NULL)) { | |
| 1415 | OPENSSL_free(buf); | |
| 1416 | return 0; | |
| 1417 | } | |
| 1418 | *ppub = buf; | |
| 1419 | return return_size; | |
| 1420 | } | |
| 1421 | ||
| 1422 | #ifndef FIPS_MODULE | |
| 1423 | { | |
| 1424 | int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub); | |
| 1425 | if (rv <= 0) | |
| 1426 | return 0; | |
| 1427 | return rv; | |
| 1428 | } | |
| 1429 | #endif /* !FIPS_MODULE */ | |
| 1430 | } | |
| 1431 | ||
| 1432 | /*- All methods below can also be used in FIPS_MODULE */ | |
| 1433 | ||
| 1434 | EVP_PKEY *EVP_PKEY_new(void) | |
| 1435 | { | |
| 1436 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); | |
| 1437 | ||
| 1438 | if (ret == NULL) | |
| 1439 | return NULL; | |
| 1440 | ||
| 1441 | ret->type = EVP_PKEY_NONE; | |
| 1442 | ret->save_type = EVP_PKEY_NONE; | |
| 1443 | ||
| 1444 | if (!CRYPTO_NEW_REF(&ret->references, 1)) | |
| 1445 | goto err; | |
| 1446 | ||
| 1447 | ret->lock = CRYPTO_THREAD_lock_new(); | |
| 1448 | if (ret->lock == NULL) { | |
| 1449 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); | |
| 1450 | goto err; | |
| 1451 | } | |
| 1452 | ||
| 1453 | #ifndef FIPS_MODULE | |
| 1454 | ret->save_parameters = 1; | |
| 1455 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) { | |
| 1456 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); | |
| 1457 | goto err; | |
| 1458 | } | |
| 1459 | #endif | |
| 1460 | return ret; | |
| 1461 | ||
| 1462 | err: | |
| 1463 | CRYPTO_FREE_REF(&ret->references); | |
| 1464 | CRYPTO_THREAD_lock_free(ret->lock); | |
| 1465 | OPENSSL_free(ret); | |
| 1466 | return NULL; | |
| 1467 | } | |
| 1468 | ||
| 1469 | /* | |
| 1470 | * Setup a public key management method. | |
| 1471 | * | |
| 1472 | * For legacy keys, either |type| or |str| is expected to have the type | |
| 1473 | * information. In this case, the setup consists of finding an ASN1 method | |
| 1474 | * and setting those fields in |pkey|. | |
| 1475 | * | |
| 1476 | * For provider side keys, |keymgmt| is expected to be non-NULL. In this | |
| 1477 | * case, the setup consists of setting the |keymgmt| field in |pkey|. | |
| 1478 | * | |
| 1479 | * If pkey is NULL just return 1 or 0 if the key management method exists. | |
| 1480 | */ | |
| 1481 | ||
| 1482 | static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, | |
| 1483 | int len, EVP_KEYMGMT *keymgmt) | |
| 1484 | { | |
| 1485 | #ifndef FIPS_MODULE | |
| 1486 | const EVP_PKEY_ASN1_METHOD *ameth = NULL; | |
| 1487 | #endif | |
| 1488 | ||
| 1489 | /* | |
| 1490 | * The setups can't set both legacy and provider side methods. | |
| 1491 | * It is forbidden | |
| 1492 | */ | |
| 1493 | if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)) { | |
| 1494 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
| 1495 | return 0; | |
| 1496 | } | |
| 1497 | ||
| 1498 | if (pkey != NULL) { | |
| 1499 | int free_it = 0; | |
| 1500 | ||
| 1501 | #ifndef FIPS_MODULE | |
| 1502 | free_it = free_it || pkey->pkey.ptr != NULL; | |
| 1503 | #endif | |
| 1504 | free_it = free_it || pkey->keydata != NULL; | |
| 1505 | if (free_it) | |
| 1506 | evp_pkey_free_it(pkey); | |
| 1507 | #ifndef FIPS_MODULE | |
| 1508 | /* | |
| 1509 | * If key type matches and a method exists then this lookup has | |
| 1510 | * succeeded once so just indicate success. | |
| 1511 | */ | |
| 1512 | if (pkey->type != EVP_PKEY_NONE | |
| 1513 | && type == pkey->save_type | |
| 1514 | && pkey->ameth != NULL) | |
| 1515 | return 1; | |
| 1516 | #endif | |
| 1517 | } | |
| 1518 | #ifndef FIPS_MODULE | |
| 1519 | if (str != NULL) | |
| 1520 | ameth = EVP_PKEY_asn1_find_str(NULL, str, len); | |
| 1521 | else if (type != EVP_PKEY_NONE) | |
| 1522 | ameth = EVP_PKEY_asn1_find(NULL, type); | |
| 1523 | #endif | |
| 1524 | ||
| 1525 | { | |
| 1526 | int check = 1; | |
| 1527 | ||
| 1528 | #ifndef FIPS_MODULE | |
| 1529 | check = check && ameth == NULL; | |
| 1530 | #endif | |
| 1531 | check = check && keymgmt == NULL; | |
| 1532 | if (check) { | |
| 1533 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); | |
| 1534 | return 0; | |
| 1535 | } | |
| 1536 | } | |
| 1537 | if (pkey != NULL) { | |
| 1538 | if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) { | |
| 1539 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
| 1540 | return 0; | |
| 1541 | } | |
| 1542 | ||
| 1543 | pkey->keymgmt = keymgmt; | |
| 1544 | ||
| 1545 | pkey->save_type = type; | |
| 1546 | pkey->type = type; | |
| 1547 | ||
| 1548 | #ifndef FIPS_MODULE | |
| 1549 | /* | |
| 1550 | * If the internal "origin" key is provider side, don't save |ameth|. | |
| 1551 | * The main reason is that |ameth| is one factor to detect that the | |
| 1552 | * internal "origin" key is a legacy one. | |
| 1553 | */ | |
| 1554 | if (keymgmt == NULL) | |
| 1555 | pkey->ameth = ameth; | |
| 1556 | ||
| 1557 | /* | |
| 1558 | * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose | |
| 1559 | * for any key type that has a legacy implementation, regardless of | |
| 1560 | * if the internal key is a legacy or a provider side one. When | |
| 1561 | * there is no legacy implementation for the key, the type becomes | |
| 1562 | * EVP_PKEY_KEYMGMT, which indicates that one should be cautious | |
| 1563 | * with functions that expect legacy internal keys. | |
| 1564 | */ | |
| 1565 | if (ameth != NULL) { | |
| 1566 | if (type == EVP_PKEY_NONE) | |
| 1567 | pkey->type = ameth->pkey_id; | |
| 1568 | } else { | |
| 1569 | pkey->type = EVP_PKEY_KEYMGMT; | |
| 1570 | } | |
| 1571 | #endif | |
| 1572 | } | |
| 1573 | return 1; | |
| 1574 | } | |
| 1575 | ||
| 1576 | #ifndef FIPS_MODULE | |
| 1577 | static void find_ameth(const char *name, void *data) | |
| 1578 | { | |
| 1579 | const char **str = data; | |
| 1580 | ||
| 1581 | /* | |
| 1582 | * The error messages from pkey_set_type() are uninteresting here, | |
| 1583 | * and misleading. | |
| 1584 | */ | |
| 1585 | ERR_set_mark(); | |
| 1586 | ||
| 1587 | if (pkey_set_type(NULL, EVP_PKEY_NONE, name, (int)strlen(name), | |
| 1588 | NULL)) { | |
| 1589 | if (str[0] == NULL) | |
| 1590 | str[0] = name; | |
| 1591 | else if (str[1] == NULL) | |
| 1592 | str[1] = name; | |
| 1593 | } | |
| 1594 | ||
| 1595 | ERR_pop_to_mark(); | |
| 1596 | } | |
| 1597 | #endif | |
| 1598 | ||
| 1599 | int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt) | |
| 1600 | { | |
| 1601 | #ifndef FIPS_MODULE | |
| 1602 | #define EVP_PKEY_TYPE_STR str[0] | |
| 1603 | #define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0])) | |
| 1604 | /* | |
| 1605 | * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD | |
| 1606 | * Ideally, only one should be found. If two (or more) are found, the | |
| 1607 | * match is ambiguous. This should never happen, but... | |
| 1608 | */ | |
| 1609 | const char *str[2] = { NULL, NULL }; | |
| 1610 | ||
| 1611 | if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str) | |
| 1612 | || str[1] != NULL) { | |
| 1613 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
| 1614 | return 0; | |
| 1615 | } | |
| 1616 | #else | |
| 1617 | #define EVP_PKEY_TYPE_STR NULL | |
| 1618 | #define EVP_PKEY_TYPE_STRLEN -1 | |
| 1619 | #endif | |
| 1620 | return pkey_set_type(pkey, EVP_PKEY_NONE, | |
| 1621 | EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN, | |
| 1622 | keymgmt); | |
| 1623 | ||
| 1624 | #undef EVP_PKEY_TYPE_STR | |
| 1625 | #undef EVP_PKEY_TYPE_STRLEN | |
| 1626 | } | |
| 1627 | ||
| 1628 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) | |
| 1629 | { | |
| 1630 | int i; | |
| 1631 | ||
| 1632 | if (CRYPTO_UP_REF(&pkey->references, &i) <= 0) | |
| 1633 | return 0; | |
| 1634 | ||
| 1635 | REF_PRINT_COUNT("EVP_PKEY", i, pkey); | |
| 1636 | REF_ASSERT_ISNT(i < 2); | |
| 1637 | return ((i > 1) ? 1 : 0); | |
| 1638 | } | |
| 1639 | ||
| 1640 | EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) | |
| 1641 | { | |
| 1642 | EVP_PKEY *dup_pk; | |
| 1643 | ||
| 1644 | if (pkey == NULL) { | |
| 1645 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); | |
| 1646 | return NULL; | |
| 1647 | } | |
| 1648 | ||
| 1649 | if ((dup_pk = EVP_PKEY_new()) == NULL) | |
| 1650 | return NULL; | |
| 1651 | ||
| 1652 | if (evp_pkey_is_blank(pkey)) | |
| 1653 | goto done; | |
| 1654 | ||
| 1655 | #ifndef FIPS_MODULE | |
| 1656 | if (evp_pkey_is_provided(pkey)) | |
| 1657 | #endif /* !FIPS_MODULE */ | |
| 1658 | { | |
| 1659 | if (!evp_keymgmt_util_copy(dup_pk, pkey, | |
| 1660 | OSSL_KEYMGMT_SELECT_ALL)) | |
| 1661 | goto err; | |
| 1662 | goto done; | |
| 1663 | } | |
| 1664 | ||
| 1665 | #ifndef FIPS_MODULE | |
| 1666 | if (evp_pkey_is_legacy(pkey)) { | |
| 1667 | const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth; | |
| 1668 | ||
| 1669 | if (ameth == NULL || ameth->copy == NULL) { | |
| 1670 | if (pkey->pkey.ptr == NULL /* empty key, just set type */ | |
| 1671 | && EVP_PKEY_set_type(dup_pk, pkey->type) != 0) | |
| 1672 | goto done; | |
| 1673 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE); | |
| 1674 | goto err; | |
| 1675 | } | |
| 1676 | if (!ameth->copy(dup_pk, pkey)) | |
| 1677 | goto err; | |
| 1678 | goto done; | |
| 1679 | } | |
| 1680 | #endif /* !FIPS_MODULE */ | |
| 1681 | ||
| 1682 | goto err; | |
| 1683 | done: | |
| 1684 | #ifndef FIPS_MODULE | |
| 1685 | /* copy auxiliary data */ | |
| 1686 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, | |
| 1687 | &dup_pk->ex_data, &pkey->ex_data)) | |
| 1688 | goto err; | |
| 1689 | ||
| 1690 | if (pkey->attributes != NULL) { | |
| 1691 | if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL) | |
| 1692 | goto err; | |
| 1693 | } | |
| 1694 | #endif /* !FIPS_MODULE */ | |
| 1695 | return dup_pk; | |
| 1696 | err: | |
| 1697 | EVP_PKEY_free(dup_pk); | |
| 1698 | return NULL; | |
| 1699 | } | |
| 1700 | ||
| 1701 | #ifndef FIPS_MODULE | |
| 1702 | void evp_pkey_free_legacy(EVP_PKEY *x) | |
| 1703 | { | |
| 1704 | const EVP_PKEY_ASN1_METHOD *ameth = x->ameth; | |
| 1705 | ||
| 1706 | if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL) | |
| 1707 | ameth = EVP_PKEY_asn1_find(NULL, x->type); | |
| 1708 | ||
| 1709 | if (ameth != NULL) { | |
| 1710 | if (x->legacy_cache_pkey.ptr != NULL) { | |
| 1711 | /* | |
| 1712 | * We should never have both a legacy origin key, and a key in the | |
| 1713 | * legacy cache. | |
| 1714 | */ | |
| 1715 | assert(x->pkey.ptr == NULL); | |
| 1716 | /* | |
| 1717 | * For the purposes of freeing we make the legacy cache look like | |
| 1718 | * a legacy origin key. | |
| 1719 | */ | |
| 1720 | x->pkey = x->legacy_cache_pkey; | |
| 1721 | x->legacy_cache_pkey.ptr = NULL; | |
| 1722 | } | |
| 1723 | if (ameth->pkey_free != NULL) | |
| 1724 | ameth->pkey_free(x); | |
| 1725 | x->pkey.ptr = NULL; | |
| 1726 | } | |
| 1727 | } | |
| 1728 | #endif /* FIPS_MODULE */ | |
| 1729 | ||
| 1730 | static void evp_pkey_free_it(EVP_PKEY *x) | |
| 1731 | { | |
| 1732 | /* internal function; x is never NULL */ | |
| 1733 | evp_keymgmt_util_clear_operation_cache(x); | |
| 1734 | #ifndef FIPS_MODULE | |
| 1735 | evp_pkey_free_legacy(x); | |
| 1736 | #endif | |
| 1737 | ||
| 1738 | if (x->keymgmt != NULL) { | |
| 1739 | evp_keymgmt_freedata(x->keymgmt, x->keydata); | |
| 1740 | EVP_KEYMGMT_free(x->keymgmt); | |
| 1741 | x->keymgmt = NULL; | |
| 1742 | x->keydata = NULL; | |
| 1743 | } | |
| 1744 | x->type = EVP_PKEY_NONE; | |
| 1745 | } | |
| 1746 | ||
| 1747 | void EVP_PKEY_free(EVP_PKEY *x) | |
| 1748 | { | |
| 1749 | int i; | |
| 1750 | ||
| 1751 | if (x == NULL) | |
| 1752 | return; | |
| 1753 | ||
| 1754 | CRYPTO_DOWN_REF(&x->references, &i); | |
| 1755 | REF_PRINT_COUNT("EVP_PKEY", i, x); | |
| 1756 | if (i > 0) | |
| 1757 | return; | |
| 1758 | REF_ASSERT_ISNT(i < 0); | |
| 1759 | evp_pkey_free_it(x); | |
| 1760 | #ifndef FIPS_MODULE | |
| 1761 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data); | |
| 1762 | #endif | |
| 1763 | CRYPTO_THREAD_lock_free(x->lock); | |
| 1764 | CRYPTO_FREE_REF(&x->references); | |
| 1765 | #ifndef FIPS_MODULE | |
| 1766 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); | |
| 1767 | #endif | |
| 1768 | OPENSSL_free(x); | |
| 1769 | } | |
| 1770 | ||
| 1771 | int EVP_PKEY_get_size(const EVP_PKEY *pkey) | |
| 1772 | { | |
| 1773 | int size = 0; | |
| 1774 | ||
| 1775 | if (pkey != NULL) { | |
| 1776 | size = pkey->cache.size; | |
| 1777 | #ifndef FIPS_MODULE | |
| 1778 | if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL) | |
| 1779 | size = pkey->ameth->pkey_size(pkey); | |
| 1780 | #endif | |
| 1781 | } | |
| 1782 | if (size <= 0) { | |
| 1783 | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE); | |
| 1784 | return 0; | |
| 1785 | } | |
| 1786 | return size; | |
| 1787 | } | |
| 1788 | ||
| 1789 | const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey) | |
| 1790 | { | |
| 1791 | if (!evp_pkey_is_assigned(pkey)) | |
| 1792 | return NULL; | |
| 1793 | ||
| 1794 | if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL) | |
| 1795 | return pkey->keymgmt->description; | |
| 1796 | #ifndef FIPS_MODULE | |
| 1797 | if (pkey->ameth != NULL) | |
| 1798 | return pkey->ameth->info; | |
| 1799 | #endif | |
| 1800 | return NULL; | |
| 1801 | } | |
| 1802 | ||
| 1803 | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx, | |
| 1804 | EVP_KEYMGMT **keymgmt, | |
| 1805 | const char *propquery) | |
| 1806 | { | |
| 1807 | EVP_KEYMGMT *allocated_keymgmt = NULL; | |
| 1808 | EVP_KEYMGMT *tmp_keymgmt = NULL; | |
| 1809 | int selection = OSSL_KEYMGMT_SELECT_ALL; | |
| 1810 | void *keydata = NULL; | |
| 1811 | int check; | |
| 1812 | ||
| 1813 | if (pk == NULL) | |
| 1814 | return NULL; | |
| 1815 | ||
| 1816 | /* No key data => nothing to export */ | |
| 1817 | check = 1; | |
| 1818 | #ifndef FIPS_MODULE | |
| 1819 | check = check && pk->pkey.ptr == NULL; | |
| 1820 | #endif | |
| 1821 | check = check && pk->keydata == NULL; | |
| 1822 | if (check) | |
| 1823 | return NULL; | |
| 1824 | ||
| 1825 | #ifndef FIPS_MODULE | |
| 1826 | if (pk->pkey.ptr != NULL) { | |
| 1827 | /* | |
| 1828 | * If the legacy key doesn't have an dirty counter or export function, | |
| 1829 | * give up | |
| 1830 | */ | |
| 1831 | if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL) | |
| 1832 | return NULL; | |
| 1833 | } | |
| 1834 | #endif | |
| 1835 | ||
| 1836 | if (keymgmt != NULL) { | |
| 1837 | tmp_keymgmt = *keymgmt; | |
| 1838 | *keymgmt = NULL; | |
| 1839 | } | |
| 1840 | ||
| 1841 | /* | |
| 1842 | * If no keymgmt was given or found, get a default keymgmt. We do so by | |
| 1843 | * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it. | |
| 1844 | */ | |
| 1845 | if (tmp_keymgmt == NULL) { | |
| 1846 | EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery); | |
| 1847 | ||
| 1848 | if (ctx == NULL) | |
| 1849 | goto end; | |
| 1850 | allocated_keymgmt = tmp_keymgmt = ctx->keymgmt; | |
| 1851 | ctx->keymgmt = NULL; | |
| 1852 | EVP_PKEY_CTX_free(ctx); | |
| 1853 | } | |
| 1854 | ||
| 1855 | /* If there's still no keymgmt to be had, give up */ | |
| 1856 | if (tmp_keymgmt == NULL) | |
| 1857 | goto end; | |
| 1858 | ||
| 1859 | #ifndef FIPS_MODULE | |
| 1860 | if (pk->pkey.ptr != NULL) { | |
| 1861 | OP_CACHE_ELEM *op; | |
| 1862 | ||
| 1863 | /* | |
| 1864 | * If the legacy "origin" hasn't changed since last time, we try | |
| 1865 | * to find our keymgmt in the operation cache. If it has changed, | |
| 1866 | * |i| remains zero, and we will clear the cache further down. | |
| 1867 | */ | |
| 1868 | if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) { | |
| 1869 | if (!CRYPTO_THREAD_read_lock(pk->lock)) | |
| 1870 | goto end; | |
| 1871 | op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, | |
| 1872 | selection); | |
| 1873 | ||
| 1874 | /* | |
| 1875 | * If |tmp_keymgmt| is present in the operation cache, it means | |
| 1876 | * that export doesn't need to be redone. In that case, we take | |
| 1877 | * token copies of the cached pointers, to have token success | |
| 1878 | * values to return. It is possible (e.g. in a no-cached-fetch | |
| 1879 | * build), for op->keymgmt to be a different pointer to tmp_keymgmt | |
| 1880 | * even though the name/provider must be the same. In other words | |
| 1881 | * the keymgmt instance may be different but still equivalent, i.e. | |
| 1882 | * same algorithm/provider instance - but we make the simplifying | |
| 1883 | * assumption that the keydata can be used with either keymgmt | |
| 1884 | * instance. Not doing so introduces significant complexity and | |
| 1885 | * probably requires refactoring - since we would have to ripple | |
| 1886 | * the change in keymgmt instance up the call chain. | |
| 1887 | */ | |
| 1888 | if (op != NULL && op->keymgmt != NULL) { | |
| 1889 | keydata = op->keydata; | |
| 1890 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1891 | goto end; | |
| 1892 | } | |
| 1893 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1894 | } | |
| 1895 | ||
| 1896 | /* Make sure that the keymgmt key type matches the legacy NID */ | |
| 1897 | if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))) | |
| 1898 | goto end; | |
| 1899 | ||
| 1900 | if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL) | |
| 1901 | goto end; | |
| 1902 | ||
| 1903 | if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import, | |
| 1904 | libctx, propquery)) { | |
| 1905 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
| 1906 | keydata = NULL; | |
| 1907 | goto end; | |
| 1908 | } | |
| 1909 | ||
| 1910 | /* | |
| 1911 | * If the dirty counter changed since last time, then clear the | |
| 1912 | * operation cache. In that case, we know that |i| is zero. Just | |
| 1913 | * in case this is a re-export, we increment then decrement the | |
| 1914 | * keymgmt reference counter. | |
| 1915 | */ | |
| 1916 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */ | |
| 1917 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
| 1918 | keydata = NULL; | |
| 1919 | goto end; | |
| 1920 | } | |
| 1921 | ||
| 1922 | if (!CRYPTO_THREAD_write_lock(pk->lock)) | |
| 1923 | goto end; | |
| 1924 | if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy | |
| 1925 | && !evp_keymgmt_util_clear_operation_cache(pk)) { | |
| 1926 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1927 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
| 1928 | keydata = NULL; | |
| 1929 | EVP_KEYMGMT_free(tmp_keymgmt); | |
| 1930 | goto end; | |
| 1931 | } | |
| 1932 | EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */ | |
| 1933 | ||
| 1934 | /* Check to make sure some other thread didn't get there first */ | |
| 1935 | op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection); | |
| 1936 | if (op != NULL && op->keymgmt != NULL) { | |
| 1937 | void *tmp_keydata = op->keydata; | |
| 1938 | ||
| 1939 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1940 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
| 1941 | keydata = tmp_keydata; | |
| 1942 | goto end; | |
| 1943 | } | |
| 1944 | ||
| 1945 | /* Add the new export to the operation cache */ | |
| 1946 | if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata, | |
| 1947 | selection)) { | |
| 1948 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1949 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
| 1950 | keydata = NULL; | |
| 1951 | goto end; | |
| 1952 | } | |
| 1953 | ||
| 1954 | /* Synchronize the dirty count */ | |
| 1955 | pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk); | |
| 1956 | ||
| 1957 | CRYPTO_THREAD_unlock(pk->lock); | |
| 1958 | goto end; | |
| 1959 | } | |
| 1960 | #endif /* FIPS_MODULE */ | |
| 1961 | ||
| 1962 | keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection); | |
| 1963 | ||
| 1964 | end: | |
| 1965 | /* | |
| 1966 | * If nothing was exported, |tmp_keymgmt| might point at a freed | |
| 1967 | * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for | |
| 1968 | * the caller either way in that case. | |
| 1969 | */ | |
| 1970 | if (keydata == NULL) | |
| 1971 | tmp_keymgmt = NULL; | |
| 1972 | ||
| 1973 | if (keymgmt != NULL && tmp_keymgmt != NULL) { | |
| 1974 | *keymgmt = tmp_keymgmt; | |
| 1975 | allocated_keymgmt = NULL; | |
| 1976 | } | |
| 1977 | ||
| 1978 | EVP_KEYMGMT_free(allocated_keymgmt); | |
| 1979 | return keydata; | |
| 1980 | } | |
| 1981 | ||
| 1982 | #ifndef FIPS_MODULE | |
| 1983 | int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src) | |
| 1984 | { | |
| 1985 | EVP_PKEY *allocpkey = NULL; | |
| 1986 | ||
| 1987 | if (!ossl_assert(dest != NULL)) | |
| 1988 | return 0; | |
| 1989 | ||
| 1990 | if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) { | |
| 1991 | EVP_KEYMGMT *keymgmt = src->keymgmt; | |
| 1992 | void *keydata = src->keydata; | |
| 1993 | int type = src->type; | |
| 1994 | const char *keytype = NULL; | |
| 1995 | ||
| 1996 | keytype = EVP_KEYMGMT_get0_name(keymgmt); | |
| 1997 | ||
| 1998 | /* | |
| 1999 | * If the type is EVP_PKEY_NONE, then we have a problem somewhere | |
| 2000 | * else in our code. If it's not one of the well known EVP_PKEY_xxx | |
| 2001 | * values, it should at least be EVP_PKEY_KEYMGMT at this point. | |
| 2002 | * The check is kept as a safety measure. | |
| 2003 | */ | |
| 2004 | if (!ossl_assert(type != EVP_PKEY_NONE)) { | |
| 2005 | ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR, | |
| 2006 | "keymgmt key type = %s but legacy type = EVP_PKEY_NONE", | |
| 2007 | keytype); | |
| 2008 | return 0; | |
| 2009 | } | |
| 2010 | ||
| 2011 | /* Prefer the legacy key type name for error reporting */ | |
| 2012 | if (type != EVP_PKEY_KEYMGMT) | |
| 2013 | keytype = OBJ_nid2sn(type); | |
| 2014 | ||
| 2015 | /* Make sure we have a clean slate to copy into */ | |
| 2016 | if (*dest == NULL) { | |
| 2017 | allocpkey = *dest = EVP_PKEY_new(); | |
| 2018 | if (*dest == NULL) { | |
| 2019 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); | |
| 2020 | return 0; | |
| 2021 | } | |
| 2022 | } else { | |
| 2023 | evp_pkey_free_it(*dest); | |
| 2024 | } | |
| 2025 | ||
| 2026 | if (EVP_PKEY_set_type(*dest, type)) { | |
| 2027 | /* If the key is typed but empty, we're done */ | |
| 2028 | if (keydata == NULL) | |
| 2029 | return 1; | |
| 2030 | ||
| 2031 | if ((*dest)->ameth->import_from == NULL) { | |
| 2032 | ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION, | |
| 2033 | "key type = %s", keytype); | |
| 2034 | } else { | |
| 2035 | /* | |
| 2036 | * We perform the export in the same libctx as the keymgmt | |
| 2037 | * that we are using. | |
| 2038 | */ | |
| 2039 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(keymgmt->prov); | |
| 2040 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL); | |
| 2041 | ||
| 2042 | if (pctx == NULL) | |
| 2043 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); | |
| 2044 | ||
| 2045 | if (pctx != NULL | |
| 2046 | && evp_keymgmt_export(keymgmt, keydata, | |
| 2047 | OSSL_KEYMGMT_SELECT_ALL, | |
| 2048 | (*dest)->ameth->import_from, | |
| 2049 | pctx)) { | |
| 2050 | /* Synchronize the dirty count */ | |
| 2051 | (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest); | |
| 2052 | ||
| 2053 | EVP_PKEY_CTX_free(pctx); | |
| 2054 | return 1; | |
| 2055 | } | |
| 2056 | EVP_PKEY_CTX_free(pctx); | |
| 2057 | } | |
| 2058 | ||
| 2059 | ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE, | |
| 2060 | "key type = %s", keytype); | |
| 2061 | } | |
| 2062 | } | |
| 2063 | ||
| 2064 | if (allocpkey != NULL) { | |
| 2065 | EVP_PKEY_free(allocpkey); | |
| 2066 | *dest = NULL; | |
| 2067 | } | |
| 2068 | return 0; | |
| 2069 | } | |
| 2070 | ||
| 2071 | void *evp_pkey_get_legacy(EVP_PKEY *pk) | |
| 2072 | { | |
| 2073 | EVP_PKEY *tmp_copy = NULL; | |
| 2074 | void *ret = NULL; | |
| 2075 | ||
| 2076 | if (!ossl_assert(pk != NULL)) | |
| 2077 | return NULL; | |
| 2078 | ||
| 2079 | /* | |
| 2080 | * If this isn't an assigned provider side key, we just use any existing | |
| 2081 | * origin legacy key. | |
| 2082 | */ | |
| 2083 | if (!evp_pkey_is_assigned(pk)) | |
| 2084 | return NULL; | |
| 2085 | if (!evp_pkey_is_provided(pk)) | |
| 2086 | return pk->pkey.ptr; | |
| 2087 | ||
| 2088 | if (!CRYPTO_THREAD_read_lock(pk->lock)) | |
| 2089 | return NULL; | |
| 2090 | ||
| 2091 | ret = pk->legacy_cache_pkey.ptr; | |
| 2092 | ||
| 2093 | if (!CRYPTO_THREAD_unlock(pk->lock)) | |
| 2094 | return NULL; | |
| 2095 | ||
| 2096 | if (ret != NULL) | |
| 2097 | return ret; | |
| 2098 | ||
| 2099 | if (!evp_pkey_copy_downgraded(&tmp_copy, pk)) | |
| 2100 | goto err; | |
| 2101 | ||
| 2102 | if (!CRYPTO_THREAD_write_lock(pk->lock)) | |
| 2103 | goto err; | |
| 2104 | ||
| 2105 | /* Check again in case some other thread has updated it in the meantime */ | |
| 2106 | ret = pk->legacy_cache_pkey.ptr; | |
| 2107 | if (ret == NULL) { | |
| 2108 | /* Steal the legacy key reference from the temporary copy */ | |
| 2109 | ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr; | |
| 2110 | tmp_copy->pkey.ptr = NULL; | |
| 2111 | } | |
| 2112 | ||
| 2113 | if (!CRYPTO_THREAD_unlock(pk->lock)) { | |
| 2114 | ret = NULL; | |
| 2115 | goto err; | |
| 2116 | } | |
| 2117 | ||
| 2118 | err: | |
| 2119 | EVP_PKEY_free(tmp_copy); | |
| 2120 | ||
| 2121 | return ret; | |
| 2122 | } | |
| 2123 | #endif /* FIPS_MODULE */ | |
| 2124 | ||
| 2125 | int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name, | |
| 2126 | BIGNUM **bn) | |
| 2127 | { | |
| 2128 | int ret = 0; | |
| 2129 | OSSL_PARAM params[2]; | |
| 2130 | unsigned char buffer[2048]; | |
| 2131 | unsigned char *buf = NULL; | |
| 2132 | size_t buf_sz = 0; | |
| 2133 | ||
| 2134 | if (key_name == NULL | |
| 2135 | || bn == NULL) | |
| 2136 | return 0; | |
| 2137 | ||
| 2138 | memset(buffer, 0, sizeof(buffer)); | |
| 2139 | params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer)); | |
| 2140 | params[1] = OSSL_PARAM_construct_end(); | |
| 2141 | if (!EVP_PKEY_get_params(pkey, params)) { | |
| 2142 | if (!OSSL_PARAM_modified(params) || params[0].return_size == 0) | |
| 2143 | return 0; | |
| 2144 | buf_sz = params[0].return_size; | |
| 2145 | /* | |
| 2146 | * If it failed because the buffer was too small then allocate the | |
| 2147 | * required buffer size and retry. | |
| 2148 | */ | |
| 2149 | buf = OPENSSL_zalloc(buf_sz); | |
| 2150 | if (buf == NULL) | |
| 2151 | return 0; | |
| 2152 | params[0].data = buf; | |
| 2153 | params[0].data_size = buf_sz; | |
| 2154 | ||
| 2155 | if (!EVP_PKEY_get_params(pkey, params)) | |
| 2156 | goto err; | |
| 2157 | } | |
| 2158 | /* Fail if the param was not found */ | |
| 2159 | if (!OSSL_PARAM_modified(params)) | |
| 2160 | goto err; | |
| 2161 | ret = OSSL_PARAM_get_BN(params, bn); | |
| 2162 | err: | |
| 2163 | if (buf != NULL) { | |
| 2164 | if (OSSL_PARAM_modified(params)) | |
| 2165 | OPENSSL_clear_free(buf, buf_sz); | |
| 2166 | else | |
| 2167 | OPENSSL_free(buf); | |
| 2168 | } else if (OSSL_PARAM_modified(params)) { | |
| 2169 | OPENSSL_cleanse(buffer, params[0].data_size); | |
| 2170 | } | |
| 2171 | return ret; | |
| 2172 | } | |
| 2173 | ||
| 2174 | int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name, | |
| 2175 | unsigned char *buf, size_t max_buf_sz, | |
| 2176 | size_t *out_len) | |
| 2177 | { | |
| 2178 | OSSL_PARAM params[2]; | |
| 2179 | int ret1 = 0, ret2 = 0; | |
| 2180 | ||
| 2181 | if (key_name == NULL) | |
| 2182 | return 0; | |
| 2183 | ||
| 2184 | params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz); | |
| 2185 | params[1] = OSSL_PARAM_construct_end(); | |
| 2186 | if ((ret1 = EVP_PKEY_get_params(pkey, params))) | |
| 2187 | ret2 = OSSL_PARAM_modified(params); | |
| 2188 | if (ret2 && out_len != NULL) | |
| 2189 | *out_len = params[0].return_size; | |
| 2190 | return ret1 && ret2; | |
| 2191 | } | |
| 2192 | ||
| 2193 | int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name, | |
| 2194 | char *str, size_t max_buf_sz, | |
| 2195 | size_t *out_len) | |
| 2196 | { | |
| 2197 | OSSL_PARAM params[2]; | |
| 2198 | int ret1 = 0, ret2 = 0; | |
| 2199 | ||
| 2200 | if (key_name == NULL) | |
| 2201 | return 0; | |
| 2202 | ||
| 2203 | params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz); | |
| 2204 | params[1] = OSSL_PARAM_construct_end(); | |
| 2205 | if ((ret1 = EVP_PKEY_get_params(pkey, params))) | |
| 2206 | ret2 = OSSL_PARAM_modified(params); | |
| 2207 | if (ret2 && out_len != NULL) | |
| 2208 | *out_len = params[0].return_size; | |
| 2209 | ||
| 2210 | if (ret2 && params[0].return_size == max_buf_sz) | |
| 2211 | /* There was no space for a NUL byte */ | |
| 2212 | return 0; | |
| 2213 | /* Add a terminating NUL byte for good measure */ | |
| 2214 | if (ret2 && str != NULL) | |
| 2215 | str[params[0].return_size] = '\0'; | |
| 2216 | ||
| 2217 | return ret1 && ret2; | |
| 2218 | } | |
| 2219 | ||
| 2220 | int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name, | |
| 2221 | int *out) | |
| 2222 | { | |
| 2223 | OSSL_PARAM params[2]; | |
| 2224 | ||
| 2225 | if (key_name == NULL) | |
| 2226 | return 0; | |
| 2227 | ||
| 2228 | params[0] = OSSL_PARAM_construct_int(key_name, out); | |
| 2229 | params[1] = OSSL_PARAM_construct_end(); | |
| 2230 | return EVP_PKEY_get_params(pkey, params) | |
| 2231 | && OSSL_PARAM_modified(params); | |
| 2232 | } | |
| 2233 | ||
| 2234 | int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name, | |
| 2235 | size_t *out) | |
| 2236 | { | |
| 2237 | OSSL_PARAM params[2]; | |
| 2238 | ||
| 2239 | if (key_name == NULL) | |
| 2240 | return 0; | |
| 2241 | ||
| 2242 | params[0] = OSSL_PARAM_construct_size_t(key_name, out); | |
| 2243 | params[1] = OSSL_PARAM_construct_end(); | |
| 2244 | return EVP_PKEY_get_params(pkey, params) | |
| 2245 | && OSSL_PARAM_modified(params); | |
| 2246 | } | |
| 2247 | ||
| 2248 | int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in) | |
| 2249 | { | |
| 2250 | OSSL_PARAM params[2]; | |
| 2251 | ||
| 2252 | if (key_name == NULL) | |
| 2253 | return 0; | |
| 2254 | ||
| 2255 | params[0] = OSSL_PARAM_construct_int(key_name, &in); | |
| 2256 | params[1] = OSSL_PARAM_construct_end(); | |
| 2257 | return EVP_PKEY_set_params(pkey, params); | |
| 2258 | } | |
| 2259 | ||
| 2260 | int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in) | |
| 2261 | { | |
| 2262 | OSSL_PARAM params[2]; | |
| 2263 | ||
| 2264 | if (key_name == NULL) | |
| 2265 | return 0; | |
| 2266 | ||
| 2267 | params[0] = OSSL_PARAM_construct_size_t(key_name, &in); | |
| 2268 | params[1] = OSSL_PARAM_construct_end(); | |
| 2269 | return EVP_PKEY_set_params(pkey, params); | |
| 2270 | } | |
| 2271 | ||
| 2272 | int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name, | |
| 2273 | const BIGNUM *bn) | |
| 2274 | { | |
| 2275 | OSSL_PARAM params[2]; | |
| 2276 | unsigned char buffer[2048]; | |
| 2277 | int bsize = 0; | |
| 2278 | ||
| 2279 | if (key_name == NULL | |
| 2280 | || bn == NULL | |
| 2281 | || pkey == NULL | |
| 2282 | || !evp_pkey_is_assigned(pkey)) | |
| 2283 | return 0; | |
| 2284 | ||
| 2285 | bsize = BN_num_bytes(bn); | |
| 2286 | if (!ossl_assert(bsize <= (int)sizeof(buffer))) | |
| 2287 | return 0; | |
| 2288 | ||
| 2289 | if (BN_bn2nativepad(bn, buffer, bsize) < 0) | |
| 2290 | return 0; | |
| 2291 | params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize); | |
| 2292 | params[1] = OSSL_PARAM_construct_end(); | |
| 2293 | return EVP_PKEY_set_params(pkey, params); | |
| 2294 | } | |
| 2295 | ||
| 2296 | int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name, | |
| 2297 | const char *str) | |
| 2298 | { | |
| 2299 | OSSL_PARAM params[2]; | |
| 2300 | ||
| 2301 | if (key_name == NULL) | |
| 2302 | return 0; | |
| 2303 | ||
| 2304 | params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0); | |
| 2305 | params[1] = OSSL_PARAM_construct_end(); | |
| 2306 | return EVP_PKEY_set_params(pkey, params); | |
| 2307 | } | |
| 2308 | ||
| 2309 | int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name, | |
| 2310 | const unsigned char *buf, size_t bsize) | |
| 2311 | { | |
| 2312 | OSSL_PARAM params[2]; | |
| 2313 | ||
| 2314 | if (key_name == NULL) | |
| 2315 | return 0; | |
| 2316 | ||
| 2317 | params[0] = OSSL_PARAM_construct_octet_string(key_name, | |
| 2318 | (unsigned char *)buf, bsize); | |
| 2319 | params[1] = OSSL_PARAM_construct_end(); | |
| 2320 | return EVP_PKEY_set_params(pkey, params); | |
| 2321 | } | |
| 2322 | ||
| 2323 | const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey) | |
| 2324 | { | |
| 2325 | return (pkey != NULL && evp_pkey_is_provided(pkey)) | |
| 2326 | ? EVP_KEYMGMT_settable_params(pkey->keymgmt) | |
| 2327 | : NULL; | |
| 2328 | } | |
| 2329 | ||
| 2330 | int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[]) | |
| 2331 | { | |
| 2332 | if (pkey != NULL) { | |
| 2333 | if (evp_pkey_is_provided(pkey)) { | |
| 2334 | pkey->dirty_cnt++; | |
| 2335 | return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params); | |
| 2336 | } | |
| 2337 | #ifndef FIPS_MODULE | |
| 2338 | /* | |
| 2339 | * We will hopefully never find the need to set individual data in | |
| 2340 | * EVP_PKEYs with a legacy internal key, but we can't be entirely | |
| 2341 | * sure. This bit of code can be enabled if we find the need. If | |
| 2342 | * not, it can safely be removed when #legacy support is removed. | |
| 2343 | */ | |
| 2344 | #if 0 | |
| 2345 | else if (evp_pkey_is_legacy(pkey)) { | |
| 2346 | return evp_pkey_set_params_to_ctrl(pkey, params); | |
| 2347 | } | |
| 2348 | #endif | |
| 2349 | #endif | |
| 2350 | } | |
| 2351 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); | |
| 2352 | return 0; | |
| 2353 | } | |
| 2354 | ||
| 2355 | const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey) | |
| 2356 | { | |
| 2357 | return (pkey != NULL && evp_pkey_is_provided(pkey)) | |
| 2358 | ? EVP_KEYMGMT_gettable_params(pkey->keymgmt) | |
| 2359 | : NULL; | |
| 2360 | } | |
| 2361 | ||
| 2362 | int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[]) | |
| 2363 | { | |
| 2364 | if (pkey != NULL) { | |
| 2365 | if (evp_pkey_is_provided(pkey)) | |
| 2366 | return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0; | |
| 2367 | #ifndef FIPS_MODULE | |
| 2368 | else if (evp_pkey_is_legacy(pkey)) | |
| 2369 | return evp_pkey_get_params_to_ctrl(pkey, params) > 0; | |
| 2370 | #endif | |
| 2371 | } | |
| 2372 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); | |
| 2373 | return 0; | |
| 2374 | } | |
| 2375 | ||
| 2376 | #ifndef FIPS_MODULE | |
| 2377 | int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey) | |
| 2378 | { | |
| 2379 | char name[80]; | |
| 2380 | size_t name_len; | |
| 2381 | ||
| 2382 | if (pkey == NULL) | |
| 2383 | return 0; | |
| 2384 | ||
| 2385 | if (pkey->keymgmt == NULL | |
| 2386 | || pkey->keydata == NULL) { | |
| 2387 | #ifndef OPENSSL_NO_EC | |
| 2388 | /* Might work through the legacy route */ | |
| 2389 | const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); | |
| 2390 | ||
| 2391 | if (ec == NULL) | |
| 2392 | return 0; | |
| 2393 | ||
| 2394 | return EC_KEY_get_conv_form(ec); | |
| 2395 | #else | |
| 2396 | return 0; | |
| 2397 | #endif | |
| 2398 | } | |
| 2399 | ||
| 2400 | if (!EVP_PKEY_get_utf8_string_param(pkey, | |
| 2401 | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, | |
| 2402 | name, sizeof(name), &name_len)) | |
| 2403 | return 0; | |
| 2404 | ||
| 2405 | if (strcmp(name, "uncompressed") == 0) | |
| 2406 | return POINT_CONVERSION_UNCOMPRESSED; | |
| 2407 | ||
| 2408 | if (strcmp(name, "compressed") == 0) | |
| 2409 | return POINT_CONVERSION_COMPRESSED; | |
| 2410 | ||
| 2411 | if (strcmp(name, "hybrid") == 0) | |
| 2412 | return POINT_CONVERSION_HYBRID; | |
| 2413 | ||
| 2414 | return 0; | |
| 2415 | } | |
| 2416 | ||
| 2417 | int EVP_PKEY_get_field_type(const EVP_PKEY *pkey) | |
| 2418 | { | |
| 2419 | char fstr[80]; | |
| 2420 | size_t fstrlen; | |
| 2421 | ||
| 2422 | if (pkey == NULL) | |
| 2423 | return 0; | |
| 2424 | ||
| 2425 | if (pkey->keymgmt == NULL | |
| 2426 | || pkey->keydata == NULL) { | |
| 2427 | #ifndef OPENSSL_NO_EC | |
| 2428 | /* Might work through the legacy route */ | |
| 2429 | const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); | |
| 2430 | const EC_GROUP *grp; | |
| 2431 | ||
| 2432 | if (ec == NULL) | |
| 2433 | return 0; | |
| 2434 | grp = EC_KEY_get0_group(ec); | |
| 2435 | if (grp == NULL) | |
| 2436 | return 0; | |
| 2437 | ||
| 2438 | return EC_GROUP_get_field_type(grp); | |
| 2439 | #else | |
| 2440 | return 0; | |
| 2441 | #endif | |
| 2442 | } | |
| 2443 | ||
| 2444 | if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE, | |
| 2445 | fstr, sizeof(fstr), &fstrlen)) | |
| 2446 | return 0; | |
| 2447 | ||
| 2448 | if (strcmp(fstr, SN_X9_62_prime_field) == 0) | |
| 2449 | return NID_X9_62_prime_field; | |
| 2450 | else if (strcmp(fstr, SN_X9_62_characteristic_two_field)) | |
| 2451 | return NID_X9_62_characteristic_two_field; | |
| 2452 | ||
| 2453 | return 0; | |
| 2454 | } | |
| 2455 | #endif |