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