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