]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_lib.c
providers/implementations/digests/sha3_prov.c optimize ossl_(un)likely
[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>
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
53static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
54 int len, EVP_KEYMGMT *keymgmt);
e683582b
SL
55static 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 61int 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 77int 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
93int EVP_PKEY_get_security_category(const EVP_PKEY *pkey)
94{
95 return pkey != NULL ? pkey->cache.security_category : -1;
96}
97
6b691a5c 98int 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
121int 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
126void *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 132int 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 241int 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 */
261static 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 330int 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
337int 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 361int 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
368int 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 418static 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 533EVP_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
542EVP_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 549EVP_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
557EVP_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 564struct raw_key_details_st {
c19d8978
MC
565 unsigned char **key;
566 size_t *len;
567 int selection;
568};
569
570static OSSL_CALLBACK get_raw_key_details;
571static 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
593int 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
625int 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
657static 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
717EVP_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 723int 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
728int 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
734int 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
752ENGINE *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
759static 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 793int 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 827void *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
838const 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
854const 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
871const 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 889static 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
898const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
899{
900 return evp_pkey_get0_DSA_int(pkey);
901}
902
b03ec3b5
SL
903int 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
917DSA *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 929static 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 938static 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 }
952IMPLEMENT_ECX_VARIANT(X25519)
953IMPLEMENT_ECX_VARIANT(X448)
954IMPLEMENT_ECX_VARIANT(ED25519)
955IMPLEMENT_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 961int 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 1000DH *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
1009const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
1010{
1011 return evp_pkey_get0_DH_int(pkey);
1012}
1013
2872dbe1
DSH
1014DH *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
6b691a5c 1025int EVP_PKEY_type(int type)
0f113f3e
MC
1026{
1027 int ret;
1028 const EVP_PKEY_ASN1_METHOD *ameth;
1029 ENGINE *e;
1030 ameth = EVP_PKEY_asn1_find(&e, type);
1031 if (ameth)
1032 ret = ameth->pkey_id;
1033 else
1034 ret = NID_undef;
e683582b 1035# ifndef OPENSSL_NO_ENGINE
7c96dbcd 1036 ENGINE_finish(e);
e683582b 1037# endif
0f113f3e
MC
1038 return ret;
1039}
d02b48c6 1040
ed576acd 1041int EVP_PKEY_get_id(const EVP_PKEY *pkey)
0f113f3e
MC
1042{
1043 return pkey->type;
1044}
7f57b076 1045
ed576acd 1046int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
0f113f3e
MC
1047{
1048 return EVP_PKEY_type(pkey->type);
1049}
7f57b076 1050
977e95b9
RL
1051/*
1052 * These hard coded cases are pure hackery to get around the fact
1053 * that names in crypto/objects/objects.txt are a mess. There is
1054 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
1055 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
1056 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
1057 * "DSA" is accurate... but still, better be safe and hard-code
1058 * names that we know.
1059 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
1060 * EVP_PKEY_EC, because of aliasing.
37cddb2e 1061 * This should be cleaned away along with all other #legacy support.
977e95b9
RL
1062 */
1063static const OSSL_ITEM standard_name2type[] = {
1064 { EVP_PKEY_RSA, "RSA" },
1065 { EVP_PKEY_RSA_PSS, "RSA-PSS" },
1066 { EVP_PKEY_EC, "EC" },
1067 { EVP_PKEY_ED25519, "ED25519" },
1068 { EVP_PKEY_ED448, "ED448" },
1069 { EVP_PKEY_X25519, "X25519" },
1070 { EVP_PKEY_X448, "X448" },
1071 { EVP_PKEY_SM2, "SM2" },
1072 { EVP_PKEY_DH, "DH" },
1073 { EVP_PKEY_DHX, "X9.42 DH" },
1074 { EVP_PKEY_DHX, "DHX" },
1075 { EVP_PKEY_DSA, "DSA" },
1076};
1077
50914496
RL
1078int evp_pkey_name2type(const char *name)
1079{
977e95b9
RL
1080 int type;
1081 size_t i;
1082
1083 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
fba140c7 1084 if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
977e95b9
RL
1085 return (int)standard_name2type[i].id;
1086 }
1087
1088 if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1089 return type;
1090 return EVP_PKEY_type(OBJ_ln2nid(name));
1091}
1092
1093const char *evp_pkey_type2name(int type)
1094{
1095 size_t i;
1096
1097 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1098 if (type == (int)standard_name2type[i].id)
1099 return standard_name2type[i].ptr;
1100 }
1101
1102 return OBJ_nid2sn(type);
50914496 1103}
50914496 1104
4f76d62f
RL
1105int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1106{
ee8db8c5
P
1107 if (pkey == NULL)
1108 return 0;
1109 if (pkey->keymgmt == NULL)
1110 return pkey->type == evp_pkey_name2type(name);
4f76d62f
RL
1111 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1112}
1113
ddf0d149
TM
1114int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1115 void (*fn)(const char *name, void *data),
1116 void *data)
ae12eac0
RL
1117{
1118 if (!evp_pkey_is_typed(pkey))
d84f5515 1119 return 0;
ae12eac0
RL
1120
1121 if (!evp_pkey_is_provided(pkey)) {
ed576acd 1122 const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
ae12eac0
RL
1123
1124 fn(name, data);
d84f5515 1125 return 1;
ae12eac0 1126 }
d84f5515 1127 return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
ae12eac0
RL
1128}
1129
4f76d62f
RL
1130int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1131{
1132 if (pkey->keymgmt == NULL) {
ed576acd 1133 switch (EVP_PKEY_get_base_id(pkey)) {
4f76d62f 1134 case EVP_PKEY_RSA:
e2972982 1135 case EVP_PKEY_RSA_PSS:
4f76d62f 1136 return 1;
0e2f87c0 1137# ifndef OPENSSL_NO_DSA
4f76d62f
RL
1138 case EVP_PKEY_DSA:
1139 return 1;
0e2f87c0
TM
1140# endif
1141# ifndef OPENSSL_NO_EC
4f76d62f
RL
1142 case EVP_PKEY_ED25519:
1143 case EVP_PKEY_ED448:
1144 return 1;
1145 case EVP_PKEY_EC: /* Including SM2 */
1146 return EC_KEY_can_sign(pkey->pkey.ec);
0e2f87c0 1147# endif
4f76d62f
RL
1148 default:
1149 break;
1150 }
1151 } else {
ed576acd 1152 const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
a829b735 1153 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
4f76d62f
RL
1154 const char *supported_sig =
1155 pkey->keymgmt->query_operation_name != NULL
1156 ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
ed576acd 1157 : EVP_KEYMGMT_get0_name(pkey->keymgmt);
4f76d62f
RL
1158 EVP_SIGNATURE *signature = NULL;
1159
1160 signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
1161 if (signature != NULL) {
1162 EVP_SIGNATURE_free(signature);
1163 return 1;
1164 }
1165 }
1166 return 0;
1167}
d02b48c6 1168
f1299839
RL
1169static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1170{
1171 BIO_set_indent(*out, saved_indent);
1172 if (pop_f_prefix) {
1173 BIO *next = BIO_pop(*out);
1174
1175 BIO_free(*out);
1176 *out = next;
1177 }
1178 return 1;
1179}
1180
1181static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1182 long indent)
1183{
1184 *pop_f_prefix = 0;
1185 *saved_indent = 0;
1186 if (indent > 0) {
1187 long i = BIO_get_indent(*out);
1188
1189 *saved_indent = (i < 0 ? 0 : i);
1190 if (BIO_set_indent(*out, indent) <= 0) {
d8732803
TM
1191 BIO *prefbio = BIO_new(BIO_f_prefix());
1192
1193 if (prefbio == NULL)
f1299839 1194 return 0;
d8732803 1195 *out = BIO_push(prefbio, *out);
f1299839
RL
1196 *pop_f_prefix = 1;
1197 }
1198 if (BIO_set_indent(*out, indent) <= 0) {
1199 print_reset_indent(out, *pop_f_prefix, *saved_indent);
1200 return 0;
1201 }
1202 }
1203 return 1;
1204}
1205
35208f36 1206static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
1207 const char *kstr)
1208{
5310a4e6
P
1209 return BIO_indent(out, indent, 128)
1210 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1211 kstr, OBJ_nid2ln(pkey->type)) > 0;
0f113f3e 1212}
35208f36 1213
f1299839 1214static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
97bb8dff 1215 int selection /* For provided encoding */,
ece9304c 1216 const char *propquery /* For provided encoding */,
f1299839
RL
1217 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1218 int indent, ASN1_PCTX *pctx),
1219 ASN1_PCTX *legacy_pctx /* For legacy print */)
0f113f3e 1220{
f1299839
RL
1221 int pop_f_prefix;
1222 long saved_indent;
ece9304c 1223 OSSL_ENCODER_CTX *ctx = NULL;
f1299839
RL
1224 int ret = -2; /* default to unsupported */
1225
1226 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1227 return 0;
54c1711f 1228
fe75766c
TM
1229 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1230 propquery);
97bb8dff 1231 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
ece9304c
RL
1232 ret = OSSL_ENCODER_to_bio(ctx, out);
1233 OSSL_ENCODER_CTX_free(ctx);
54c1711f
RL
1234
1235 if (ret != -2)
f1299839 1236 goto end;
54c1711f
RL
1237
1238 /* legacy fallback */
f1299839
RL
1239 if (legacy_print != NULL)
1240 ret = legacy_print(out, pkey, 0, legacy_pctx);
1241 else
1242 ret = unsup_alg(out, pkey, 0, "Public Key");
0f113f3e 1243
f1299839
RL
1244 end:
1245 print_reset_indent(&out, pop_f_prefix, saved_indent);
1246 return ret;
1247}
1248
1249int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1250 int indent, ASN1_PCTX *pctx)
1251{
b03da688 1252 return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
f1299839
RL
1253 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1254 pctx);
0f113f3e 1255}
35208f36
DSH
1256
1257int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
1258 int indent, ASN1_PCTX *pctx)
1259{
1296c2ec 1260 return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
f1299839
RL
1261 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1262 pctx);
0f113f3e 1263}
35208f36
DSH
1264
1265int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
1266 int indent, ASN1_PCTX *pctx)
1267{
b03da688 1268 return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
f1299839
RL
1269 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1270 pctx);
0f113f3e 1271}
03919683 1272
0e2f87c0
TM
1273# ifndef OPENSSL_NO_STDIO
1274int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1275 int indent, ASN1_PCTX *pctx)
1276{
1277 int ret;
1278 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1279
1280 if (b == NULL)
1281 return 0;
1282 ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1283 BIO_free(b);
1284 return ret;
1285}
1286
1287int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1288 int indent, ASN1_PCTX *pctx)
1289{
1290 int ret;
1291 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1292
1293 if (b == NULL)
1294 return 0;
1295 ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1296 BIO_free(b);
1297 return ret;
1298}
1299
1300int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1301 int indent, ASN1_PCTX *pctx)
1302{
1303 int ret;
1304 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1305
1306 if (b == NULL)
1307 return 0;
1308 ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1309 BIO_free(b);
1310 return ret;
1311}
1312# endif
1313
fc52ae8c 1314static void mdname2nid(const char *mdname, void *data)
5060cd5f
MC
1315{
1316 int *nid = (int *)data;
1317
1318 if (*nid != NID_undef)
1319 return;
1320
1321 *nid = OBJ_sn2nid(mdname);
1322 if (*nid == NID_undef)
1323 *nid = OBJ_ln2nid(mdname);
1324}
1325
ead0d234
RL
1326static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1327 int arg1, void *arg2)
1328{
3c6ed955 1329 if (pkey->keymgmt == NULL)
ead0d234
RL
1330 return 0;
1331 switch (op) {
1332 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
1333 {
1334 char mdname[80] = "";
ead0d234
RL
1335 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1336 sizeof(mdname));
1337
90ef39f4 1338 if (rv > 0) {
5060cd5f
MC
1339 int mdnum;
1340 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1341 /* Make sure the MD is in the namemap if available */
3c15d677
SL
1342 EVP_MD *md;
1343 OSSL_NAMEMAP *namemap;
5060cd5f
MC
1344 int nid = NID_undef;
1345
3c15d677
SL
1346 (void)ERR_set_mark();
1347 md = EVP_MD_fetch(libctx, mdname, NULL);
1348 (void)ERR_pop_to_mark();
1349 namemap = ossl_namemap_stored(libctx);
1350
5060cd5f
MC
1351 /*
1352 * The only reason to fetch the MD was to make sure it is in the
1353 * namemap. We can immediately free it.
1354 */
1355 EVP_MD_free(md);
1356 mdnum = ossl_namemap_name2num(namemap, mdname);
1357 if (mdnum == 0)
1358 return 0;
1359
1360 /*
1361 * We have the namemap number - now we need to find the
1362 * associated nid
1363 */
d84f5515
MC
1364 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1365 return 0;
90ef39f4
RL
1366 *(int *)arg2 = nid;
1367 }
1368 return rv;
ead0d234
RL
1369 }
1370 default:
1371 return -2;
1372 }
1373}
1374
5d6aaf8a 1375static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
0f113f3e 1376{
ead0d234
RL
1377 if (pkey->ameth == NULL)
1378 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1379 if (pkey->ameth->pkey_ctrl == NULL)
0f113f3e 1380 return -2;
5d6aaf8a
DSH
1381 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1382}
1383
1384int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1385{
ab5a172f 1386 if (pkey == NULL)
1387 return 0;
5d6aaf8a
DSH
1388 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1389}
1390
ead0d234
RL
1391int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1392 char *mdname, size_t mdname_sz)
1393{
3b924da0
RL
1394 if (pkey->ameth == NULL)
1395 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1396 pkey->keydata,
1397 mdname, mdname_sz);
ead0d234
RL
1398
1399 {
1400 int nid = NID_undef;
1401 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1402 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1403
1404 if (rv > 0)
1405 OPENSSL_strlcpy(mdname, name, mdname_sz);
1406 return rv;
1407 }
1408}
1409
88bddad4
RL
1410int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1411 size_t *gname_len)
1412{
6fcd92d3
RL
1413 return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1414 gname, gname_sz, gname_len);
88bddad4
RL
1415}
1416
e9fe0f7e
TM
1417int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1418 const char *name, const char *propq)
ecbb2fca 1419{
e9fe0f7e
TM
1420 int rv;
1421 EVP_MD_CTX *ctx = NULL;
ecbb2fca 1422
e9fe0f7e
TM
1423 if ((ctx = EVP_MD_CTX_new()) == NULL)
1424 return -1;
1425
1426 ERR_set_mark();
1427 rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1428 propq, pkey, NULL);
1429 ERR_pop_to_mark();
1430
1431 EVP_MD_CTX_free(ctx);
ecbb2fca
DW
1432 return rv;
1433}
b6c53426 1434#endif /* !FIPS_MODULE */
ecbb2fca 1435
5ac8fb58
MC
1436int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1437 size_t publen)
5d6aaf8a 1438{
ab5a172f 1439 if (pkey == NULL)
1440 return 0;
b6c53426 1441#ifndef FIPS_MODULE
ab5a172f 1442 if (evp_pkey_is_provided(pkey))
b6c53426 1443#endif /* !FIPS_MODULE */
76624df1
RL
1444 return
1445 EVP_PKEY_set_octet_string_param(pkey,
1446 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1447 (unsigned char *)pub, publen);
6a9bd929 1448
b6c53426 1449#ifndef FIPS_MODULE
5ac8fb58 1450 if (publen > INT_MAX)
5d6aaf8a 1451 return 0;
5ac8fb58 1452 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
bb86c43f 1453 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, (int)publen,
5ac8fb58 1454 (void *)pub) <= 0)
5d6aaf8a
DSH
1455 return 0;
1456 return 1;
b6c53426 1457#endif /* !FIPS_MODULE */
5d6aaf8a
DSH
1458}
1459
5ac8fb58 1460size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
5d6aaf8a 1461{
ab5a172f 1462 if (pkey == NULL)
1463 return 0;
b6c53426
P
1464#ifndef FIPS_MODULE
1465 if (evp_pkey_is_provided(pkey))
1466#endif
1467 {
76624df1 1468 size_t return_size = OSSL_PARAM_UNMODIFIED;
4e9a4997 1469 unsigned char *buf;
6a9bd929 1470
76624df1
RL
1471 /*
1472 * We know that this is going to fail, but it will give us a size
1473 * to allocate.
1474 */
1475 EVP_PKEY_get_octet_string_param(pkey,
1476 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1477 NULL, 0, &return_size);
1478 if (return_size == OSSL_PARAM_UNMODIFIED)
6a9bd929
MC
1479 return 0;
1480
4e9a4997 1481 *ppub = NULL;
1482 buf = OPENSSL_malloc(return_size);
1483 if (buf == NULL)
6a9bd929
MC
1484 return 0;
1485
76624df1
RL
1486 if (!EVP_PKEY_get_octet_string_param(pkey,
1487 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
4e9a4997 1488 buf, return_size, NULL)) {
1489 OPENSSL_free(buf);
6a9bd929 1490 return 0;
4e9a4997 1491 }
1492 *ppub = buf;
76624df1 1493 return return_size;
6a9bd929
MC
1494 }
1495
b6c53426
P
1496#ifndef FIPS_MODULE
1497 {
1498 int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1499 if (rv <= 0)
1500 return 0;
1501 return rv;
1502 }
1503#endif /* !FIPS_MODULE */
0f113f3e 1504}
e683582b 1505
f844f9eb 1506/*- All methods below can also be used in FIPS_MODULE */
e683582b
SL
1507
1508EVP_PKEY *EVP_PKEY_new(void)
1509{
1510 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1511
e077455e 1512 if (ret == NULL)
e683582b 1513 return NULL;
4ce1025a 1514
b574c6a9
MC
1515 ret->type = EVP_PKEY_NONE;
1516 ret->save_type = EVP_PKEY_NONE;
6be83ac1
P
1517
1518 if (!CRYPTO_NEW_REF(&ret->references, 1))
1519 goto err;
4ce1025a 1520
8dc34b1f
DB
1521 ret->lock = CRYPTO_THREAD_lock_new();
1522 if (ret->lock == NULL) {
e077455e 1523 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
8dc34b1f
DB
1524 goto err;
1525 }
1526
f844f9eb 1527#ifndef FIPS_MODULE
b247113c 1528 ret->save_parameters = 1;
ff1f7cde 1529 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
e077455e 1530 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
ff1f7cde 1531 goto err;
e683582b 1532 }
ff1f7cde 1533#endif
e683582b 1534 return ret;
ff1f7cde
AT
1535
1536 err:
6be83ac1 1537 CRYPTO_FREE_REF(&ret->references);
ff1f7cde
AT
1538 CRYPTO_THREAD_lock_free(ret->lock);
1539 OPENSSL_free(ret);
1540 return NULL;
e683582b
SL
1541}
1542
8243d8d1
RL
1543/*
1544 * Setup a public key management method.
1545 *
1546 * For legacy keys, either |type| or |str| is expected to have the type
1547 * information. In this case, the setup consists of finding an ASN1 method
1548 * and potentially an ENGINE, and setting those fields in |pkey|.
1549 *
1550 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1551 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1552 *
1553 * If pkey is NULL just return 1 or 0 if the key management method exists.
1554 */
1555
1556static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1557 int len, EVP_KEYMGMT *keymgmt)
1558{
f844f9eb 1559#ifndef FIPS_MODULE
8243d8d1
RL
1560 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1561 ENGINE **eptr = (e == NULL) ? &e : NULL;
1562#endif
1563
1564 /*
1565 * The setups can't set both legacy and provider side methods.
1566 * It is forbidden
1567 */
1568 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1569 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1570 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1571 return 0;
1572 }
1573
1574 if (pkey != NULL) {
1575 int free_it = 0;
1576
f844f9eb 1577#ifndef FIPS_MODULE
8243d8d1
RL
1578 free_it = free_it || pkey->pkey.ptr != NULL;
1579#endif
1580 free_it = free_it || pkey->keydata != NULL;
1581 if (free_it)
1582 evp_pkey_free_it(pkey);
f844f9eb 1583#ifndef FIPS_MODULE
8243d8d1
RL
1584 /*
1585 * If key type matches and a method exists then this lookup has
1586 * succeeded once so just indicate success.
1587 */
1588 if (pkey->type != EVP_PKEY_NONE
1589 && type == pkey->save_type
1590 && pkey->ameth != NULL)
1591 return 1;
1592# ifndef OPENSSL_NO_ENGINE
1593 /* If we have ENGINEs release them */
1594 ENGINE_finish(pkey->engine);
1595 pkey->engine = NULL;
1596 ENGINE_finish(pkey->pmeth_engine);
1597 pkey->pmeth_engine = NULL;
1598# endif
1599#endif
1600 }
f844f9eb 1601#ifndef FIPS_MODULE
8243d8d1
RL
1602 if (str != NULL)
1603 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1604 else if (type != EVP_PKEY_NONE)
1605 ameth = EVP_PKEY_asn1_find(eptr, type);
1606# ifndef OPENSSL_NO_ENGINE
1607 if (pkey == NULL && eptr != NULL)
1608 ENGINE_finish(e);
1609# endif
1610#endif
1611
1612
1613 {
1614 int check = 1;
1615
f844f9eb 1616#ifndef FIPS_MODULE
8243d8d1
RL
1617 check = check && ameth == NULL;
1618#endif
1619 check = check && keymgmt == NULL;
1620 if (check) {
9311d0c4 1621 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
8243d8d1
RL
1622 return 0;
1623 }
1624 }
1625 if (pkey != NULL) {
1626 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1627 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1628 return 0;
1629 }
1630
1631 pkey->keymgmt = keymgmt;
1632
1633 pkey->save_type = type;
1634 pkey->type = type;
1635
f844f9eb 1636#ifndef FIPS_MODULE
8243d8d1
RL
1637 /*
1638 * If the internal "origin" key is provider side, don't save |ameth|.
1639 * The main reason is that |ameth| is one factor to detect that the
1640 * internal "origin" key is a legacy one.
1641 */
1642 if (keymgmt == NULL)
1643 pkey->ameth = ameth;
8243d8d1
RL
1644
1645 /*
5e5bc836
RL
1646 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1647 * for any key type that has a legacy implementation, regardless of
1648 * if the internal key is a legacy or a provider side one. When
1649 * there is no legacy implementation for the key, the type becomes
1650 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1651 * with functions that expect legacy internal keys.
8243d8d1 1652 */
28fd8953
MC
1653 if (ameth != NULL) {
1654 if (type == EVP_PKEY_NONE)
1655 pkey->type = ameth->pkey_id;
1656 } else {
5e5bc836 1657 pkey->type = EVP_PKEY_KEYMGMT;
28fd8953 1658 }
f7d6868d
MC
1659# ifndef OPENSSL_NO_ENGINE
1660 if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
1661 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1662 return 0;
1663 }
1664# endif
1665 pkey->engine = e;
8243d8d1
RL
1666#endif
1667 }
1668 return 1;
1669}
1670
f844f9eb 1671#ifndef FIPS_MODULE
8243d8d1
RL
1672static void find_ameth(const char *name, void *data)
1673{
1674 const char **str = data;
1675
1676 /*
1677 * The error messages from pkey_set_type() are uninteresting here,
1678 * and misleading.
1679 */
1680 ERR_set_mark();
1681
bb86c43f 1682 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, (int)strlen(name),
8243d8d1
RL
1683 NULL)) {
1684 if (str[0] == NULL)
1685 str[0] = name;
1686 else if (str[1] == NULL)
1687 str[1] = name;
1688 }
1689
1690 ERR_pop_to_mark();
1691}
1692#endif
1693
1694int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1695{
f844f9eb 1696#ifndef FIPS_MODULE
8243d8d1
RL
1697# define EVP_PKEY_TYPE_STR str[0]
1698# define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1699 /*
1700 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1701 * Ideally, only one should be found. If two (or more) are found, the
1702 * match is ambiguous. This should never happen, but...
1703 */
1704 const char *str[2] = { NULL, NULL };
1705
d84f5515
MC
1706 if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1707 || str[1] != NULL) {
8243d8d1
RL
1708 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1709 return 0;
1710 }
1711#else
1712# define EVP_PKEY_TYPE_STR NULL
1713# define EVP_PKEY_TYPE_STRLEN -1
1714#endif
1715 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1716 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1717 keymgmt);
1718
1719#undef EVP_PKEY_TYPE_STR
1720#undef EVP_PKEY_TYPE_STRLEN
1721}
1722
e683582b
SL
1723int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1724{
1725 int i;
1726
6be83ac1 1727 if (CRYPTO_UP_REF(&pkey->references, &i) <= 0)
e683582b
SL
1728 return 0;
1729
dc10ffc2 1730 REF_PRINT_COUNT("EVP_PKEY", i, pkey);
e683582b
SL
1731 REF_ASSERT_ISNT(i < 2);
1732 return ((i > 1) ? 1 : 0);
1733}
1734
2145ba5e
TM
1735EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1736{
1737 EVP_PKEY *dup_pk;
1738
1739 if (pkey == NULL) {
1740 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1741 return NULL;
1742 }
1743
1744 if ((dup_pk = EVP_PKEY_new()) == NULL)
1745 return NULL;
1746
1747 if (evp_pkey_is_blank(pkey))
1748 goto done;
1749
b6c53426
P
1750#ifndef FIPS_MODULE
1751 if (evp_pkey_is_provided(pkey))
1752#endif /* !FIPS_MODULE */
1753 {
2145ba5e
TM
1754 if (!evp_keymgmt_util_copy(dup_pk, pkey,
1755 OSSL_KEYMGMT_SELECT_ALL))
1756 goto err;
1757 goto done;
1758 }
1759
b6c53426 1760#ifndef FIPS_MODULE
2145ba5e
TM
1761 if (evp_pkey_is_legacy(pkey)) {
1762 const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1763
1764 if (ameth == NULL || ameth->copy == NULL) {
1765 if (pkey->pkey.ptr == NULL /* empty key, just set type */
1766 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1767 goto done;
1768 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1769 goto err;
1770 }
1771 if (!ameth->copy(dup_pk, pkey))
1772 goto err;
1773 goto done;
1774 }
b6c53426 1775#endif /* !FIPS_MODULE */
2145ba5e
TM
1776
1777 goto err;
1778done:
b6c53426 1779#ifndef FIPS_MODULE
2145ba5e
TM
1780 /* copy auxiliary data */
1781 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1782 &dup_pk->ex_data, &pkey->ex_data))
1783 goto err;
1784
1785 if (pkey->attributes != NULL) {
1786 if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1787 goto err;
1788 }
b6c53426 1789#endif /* !FIPS_MODULE */
2145ba5e
TM
1790 return dup_pk;
1791err:
1792 EVP_PKEY_free(dup_pk);
1793 return NULL;
1794}
1795
b6c53426 1796#ifndef FIPS_MODULE
62924755 1797void evp_pkey_free_legacy(EVP_PKEY *x)
badf51c8 1798{
b574c6a9
MC
1799 const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1800 ENGINE *tmpe = NULL;
1801
1802 if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1803 ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
1804
1805 if (ameth != NULL) {
1806 if (x->legacy_cache_pkey.ptr != NULL) {
1807 /*
1808 * We should never have both a legacy origin key, and a key in the
1809 * legacy cache.
1810 */
1811 assert(x->pkey.ptr == NULL);
1812 /*
1813 * For the purposes of freeing we make the legacy cache look like
1814 * a legacy origin key.
1815 */
1816 x->pkey = x->legacy_cache_pkey;
1817 x->legacy_cache_pkey.ptr = NULL;
1818 }
1819 if (ameth->pkey_free != NULL)
1820 ameth->pkey_free(x);
badf51c8 1821 x->pkey.ptr = NULL;
badf51c8
RL
1822 }
1823# ifndef OPENSSL_NO_ENGINE
b574c6a9 1824 ENGINE_finish(tmpe);
badf51c8
RL
1825 ENGINE_finish(x->engine);
1826 x->engine = NULL;
1827 ENGINE_finish(x->pmeth_engine);
1828 x->pmeth_engine = NULL;
1829# endif
badf51c8 1830}
f844f9eb 1831#endif /* FIPS_MODULE */
badf51c8 1832
e683582b
SL
1833static void evp_pkey_free_it(EVP_PKEY *x)
1834{
1835 /* internal function; x is never NULL */
36424806 1836 evp_keymgmt_util_clear_operation_cache(x);
f844f9eb 1837#ifndef FIPS_MODULE
badf51c8
RL
1838 evp_pkey_free_legacy(x);
1839#endif
e683582b 1840
3c6ed955
RL
1841 if (x->keymgmt != NULL) {
1842 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1843 EVP_KEYMGMT_free(x->keymgmt);
1844 x->keymgmt = NULL;
1845 x->keydata = NULL;
1846 }
5e5bc836 1847 x->type = EVP_PKEY_NONE;
e683582b
SL
1848}
1849
1850void EVP_PKEY_free(EVP_PKEY *x)
1851{
1852 int i;
1853
1854 if (x == NULL)
1855 return;
1856
6be83ac1 1857 CRYPTO_DOWN_REF(&x->references, &i);
dc10ffc2 1858 REF_PRINT_COUNT("EVP_PKEY", i, x);
e683582b
SL
1859 if (i > 0)
1860 return;
1861 REF_ASSERT_ISNT(i < 0);
1862 evp_pkey_free_it(x);
f844f9eb 1863#ifndef FIPS_MODULE
ff1f7cde
AT
1864 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1865#endif
e683582b 1866 CRYPTO_THREAD_lock_free(x->lock);
6be83ac1 1867 CRYPTO_FREE_REF(&x->references);
f844f9eb 1868#ifndef FIPS_MODULE
e683582b
SL
1869 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1870#endif
1871 OPENSSL_free(x);
1872}
1873
ed576acd 1874int EVP_PKEY_get_size(const EVP_PKEY *pkey)
e683582b 1875{
adc9f731
RL
1876 int size = 0;
1877
6508e858 1878 if (pkey != NULL) {
adc9f731 1879 size = pkey->cache.size;
f844f9eb 1880#ifndef FIPS_MODULE
adc9f731
RL
1881 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1882 size = pkey->ameth->pkey_size(pkey);
1883#endif
6508e858 1884 }
ae643b32
DDO
1885 if (size <= 0) {
1886 ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
1887 return 0;
1888 }
1889 return size;
e683582b 1890}
f6aa5774 1891
ed576acd 1892const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
03888233
RL
1893{
1894 if (!evp_pkey_is_assigned(pkey))
1895 return NULL;
1896
1897 if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1898 return pkey->keymgmt->description;
1899#ifndef FIPS_MODULE
1900 if (pkey->ameth != NULL)
1901 return pkey->ameth->info;
1902#endif
1903 return NULL;
1904}
1905
b4250010 1906void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
3c6ed955
RL
1907 EVP_KEYMGMT **keymgmt,
1908 const char *propquery)
f6aa5774
RL
1909{
1910 EVP_KEYMGMT *allocated_keymgmt = NULL;
1911 EVP_KEYMGMT *tmp_keymgmt = NULL;
98642df4 1912 int selection = OSSL_KEYMGMT_SELECT_ALL;
b305452f 1913 void *keydata = NULL;
adc9f731 1914 int check;
f6aa5774
RL
1915
1916 if (pk == NULL)
1917 return NULL;
1918
adc9f731
RL
1919 /* No key data => nothing to export */
1920 check = 1;
f844f9eb 1921#ifndef FIPS_MODULE
adc9f731
RL
1922 check = check && pk->pkey.ptr == NULL;
1923#endif
1924 check = check && pk->keydata == NULL;
1925 if (check)
1926 return NULL;
1927
f844f9eb 1928#ifndef FIPS_MODULE
3f7ce7f1 1929 if (pk->pkey.ptr != NULL) {
3f7ce7f1 1930 /*
3c6ed955
RL
1931 * If the legacy key doesn't have an dirty counter or export function,
1932 * give up
3f7ce7f1 1933 */
3c6ed955
RL
1934 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1935 return NULL;
3f7ce7f1
RL
1936 }
1937#endif
1938
3c6ed955
RL
1939 if (keymgmt != NULL) {
1940 tmp_keymgmt = *keymgmt;
1941 *keymgmt = NULL;
1942 }
1943
4b9e90f4
RL
1944 /*
1945 * If no keymgmt was given or found, get a default keymgmt. We do so by
1946 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1947 */
f6aa5774 1948 if (tmp_keymgmt == NULL) {
2ee4a50a 1949 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
f6aa5774 1950
9dddcd90 1951 if (ctx == NULL)
1952 goto end;
115eb945 1953 allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;
4b9e90f4 1954 ctx->keymgmt = NULL;
f6aa5774
RL
1955 EVP_PKEY_CTX_free(ctx);
1956 }
1957
3c6ed955 1958 /* If there's still no keymgmt to be had, give up */
3f7ce7f1
RL
1959 if (tmp_keymgmt == NULL)
1960 goto end;
f6aa5774 1961
f844f9eb 1962#ifndef FIPS_MODULE
3f7ce7f1 1963 if (pk->pkey.ptr != NULL) {
64954e2f 1964 OP_CACHE_ELEM *op;
3f7ce7f1
RL
1965
1966 /*
3c6ed955
RL
1967 * If the legacy "origin" hasn't changed since last time, we try
1968 * to find our keymgmt in the operation cache. If it has changed,
1969 * |i| remains zero, and we will clear the cache further down.
3f7ce7f1 1970 */
3c6ed955 1971 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
0b07db6f
MC
1972 if (!CRYPTO_THREAD_read_lock(pk->lock))
1973 goto end;
98642df4
SS
1974 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
1975 selection);
3c6ed955
RL
1976
1977 /*
1978 * If |tmp_keymgmt| is present in the operation cache, it means
1979 * that export doesn't need to be redone. In that case, we take
1980 * token copies of the cached pointers, to have token success
dc9bc6c8
MC
1981 * values to return. It is possible (e.g. in a no-cached-fetch
1982 * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1983 * even though the name/provider must be the same. In other words
1984 * the keymgmt instance may be different but still equivalent, i.e.
1985 * same algorithm/provider instance - but we make the simplifying
1986 * assumption that the keydata can be used with either keymgmt
1987 * instance. Not doing so introduces significant complexity and
1988 * probably requires refactoring - since we would have to ripple
1989 * the change in keymgmt instance up the call chain.
3c6ed955 1990 */
64954e2f
P
1991 if (op != NULL && op->keymgmt != NULL) {
1992 keydata = op->keydata;
0b07db6f 1993 CRYPTO_THREAD_unlock(pk->lock);
3c6ed955
RL
1994 goto end;
1995 }
0b07db6f 1996 CRYPTO_THREAD_unlock(pk->lock);
3f7ce7f1
RL
1997 }
1998
3f7ce7f1 1999 /* Make sure that the keymgmt key type matches the legacy NID */
0fc39c90 2000 if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
3f7ce7f1
RL
2001 goto end;
2002
2003 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
2004 goto end;
2005
bed7437b
RL
2006 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,
2007 libctx, propquery)) {
3f7ce7f1
RL
2008 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2009 keydata = NULL;
2010 goto end;
2011 }
2012
3c6ed955
RL
2013 /*
2014 * If the dirty counter changed since last time, then clear the
2015 * operation cache. In that case, we know that |i| is zero. Just
2016 * in case this is a re-export, we increment then decrement the
2017 * keymgmt reference counter.
2018 */
2019 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
2020 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2021 keydata = NULL;
2022 goto end;
2023 }
0b07db6f
MC
2024
2025 if (!CRYPTO_THREAD_write_lock(pk->lock))
2026 goto end;
2027 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
36424806 2028 && !evp_keymgmt_util_clear_operation_cache(pk)) {
0b07db6f
MC
2029 CRYPTO_THREAD_unlock(pk->lock);
2030 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2031 keydata = NULL;
2032 EVP_KEYMGMT_free(tmp_keymgmt);
2033 goto end;
2034 }
3c6ed955
RL
2035 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
2036
64954e2f 2037 /* Check to make sure some other thread didn't get there first */
98642df4 2038 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
64954e2f
P
2039 if (op != NULL && op->keymgmt != NULL) {
2040 void *tmp_keydata = op->keydata;
2041
2042 CRYPTO_THREAD_unlock(pk->lock);
2043 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2044 keydata = tmp_keydata;
2045 goto end;
2046 }
2047
3c6ed955 2048 /* Add the new export to the operation cache */
98642df4
SS
2049 if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
2050 selection)) {
0b07db6f 2051 CRYPTO_THREAD_unlock(pk->lock);
3c6ed955
RL
2052 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2053 keydata = NULL;
2054 goto end;
2055 }
3f7ce7f1
RL
2056
2057 /* Synchronize the dirty count */
2058 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
4a9fe33c 2059
0b07db6f 2060 CRYPTO_THREAD_unlock(pk->lock);
3f7ce7f1
RL
2061 goto end;
2062 }
f844f9eb 2063#endif /* FIPS_MODULE */
3f7ce7f1 2064
98642df4 2065 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
3f7ce7f1
RL
2066
2067 end:
f6aa5774
RL
2068 /*
2069 * If nothing was exported, |tmp_keymgmt| might point at a freed
2070 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
2071 * the caller either way in that case.
2072 */
b305452f 2073 if (keydata == NULL)
f6aa5774
RL
2074 tmp_keymgmt = NULL;
2075
115eb945 2076 if (keymgmt != NULL && tmp_keymgmt != NULL) {
f6aa5774 2077 *keymgmt = tmp_keymgmt;
115eb945
K
2078 allocated_keymgmt = NULL;
2079 }
f6aa5774
RL
2080
2081 EVP_KEYMGMT_free(allocated_keymgmt);
b305452f 2082 return keydata;
f6aa5774 2083}
badf51c8 2084
f844f9eb 2085#ifndef FIPS_MODULE
4ce1025a 2086int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
badf51c8 2087{
ae4d9573
MC
2088 EVP_PKEY *allocpkey = NULL;
2089
4ce1025a
RL
2090 if (!ossl_assert(dest != NULL))
2091 return 0;
badf51c8 2092
4ce1025a
RL
2093 if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
2094 EVP_KEYMGMT *keymgmt = src->keymgmt;
2095 void *keydata = src->keydata;
2096 int type = src->type;
2097 const char *keytype = NULL;
acb90ba8 2098
ed576acd 2099 keytype = EVP_KEYMGMT_get0_name(keymgmt);
badf51c8 2100
4ce1025a
RL
2101 /*
2102 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2103 * else in our code. If it's not one of the well known EVP_PKEY_xxx
2104 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
37cddb2e 2105 * The check is kept as a safety measure.
4ce1025a
RL
2106 */
2107 if (!ossl_assert(type != EVP_PKEY_NONE)) {
2108 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
2109 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2110 keytype);
2111 return 0;
2112 }
badf51c8 2113
4ce1025a
RL
2114 /* Prefer the legacy key type name for error reporting */
2115 if (type != EVP_PKEY_KEYMGMT)
2116 keytype = OBJ_nid2sn(type);
5e5bc836 2117
4ce1025a 2118 /* Make sure we have a clean slate to copy into */
ec961f86 2119 if (*dest == NULL) {
ae4d9573 2120 allocpkey = *dest = EVP_PKEY_new();
ec961f86 2121 if (*dest == NULL) {
e077455e 2122 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
ec961f86
MC
2123 return 0;
2124 }
2125 } else {
4ce1025a 2126 evp_pkey_free_it(*dest);
ec961f86 2127 }
badf51c8 2128
4ce1025a
RL
2129 if (EVP_PKEY_set_type(*dest, type)) {
2130 /* If the key is typed but empty, we're done */
2131 if (keydata == NULL)
2132 return 1;
629c72db 2133
4ce1025a
RL
2134 if ((*dest)->ameth->import_from == NULL) {
2135 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2136 "key type = %s", keytype);
2137 } else {
629c72db 2138 /*
4ce1025a
RL
2139 * We perform the export in the same libctx as the keymgmt
2140 * that we are using.
629c72db 2141 */
b4250010 2142 OSSL_LIB_CTX *libctx =
a829b735 2143 ossl_provider_libctx(keymgmt->prov);
4ce1025a
RL
2144 EVP_PKEY_CTX *pctx =
2145 EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
629c72db 2146
4ce1025a 2147 if (pctx == NULL)
e077455e 2148 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
629c72db 2149
4ce1025a
RL
2150 if (pctx != NULL
2151 && evp_keymgmt_export(keymgmt, keydata,
2152 OSSL_KEYMGMT_SELECT_ALL,
2153 (*dest)->ameth->import_from,
2154 pctx)) {
2155 /* Synchronize the dirty count */
2156 (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2157
2158 EVP_PKEY_CTX_free(pctx);
2159 return 1;
2160 }
2161 EVP_PKEY_CTX_free(pctx);
629c72db 2162 }
badf51c8 2163
4ce1025a
RL
2164 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2165 "key type = %s", keytype);
2166 }
badf51c8
RL
2167 }
2168
ae4d9573
MC
2169 if (allocpkey != NULL) {
2170 EVP_PKEY_free(allocpkey);
2171 *dest = NULL;
2172 }
4ce1025a
RL
2173 return 0;
2174}
2175
b574c6a9 2176void *evp_pkey_get_legacy(EVP_PKEY *pk)
4ce1025a 2177{
b574c6a9
MC
2178 EVP_PKEY *tmp_copy = NULL;
2179 void *ret = NULL;
a8154452
RL
2180
2181 if (!ossl_assert(pk != NULL))
b574c6a9 2182 return NULL;
a8154452
RL
2183
2184 /*
b574c6a9
MC
2185 * If this isn't an assigned provider side key, we just use any existing
2186 * origin legacy key.
a8154452 2187 */
b574c6a9
MC
2188 if (!evp_pkey_is_assigned(pk))
2189 return NULL;
2190 if (!evp_pkey_is_provided(pk))
2191 return pk->pkey.ptr;
4ce1025a 2192
b574c6a9
MC
2193 if (!CRYPTO_THREAD_read_lock(pk->lock))
2194 return NULL;
4ce1025a 2195
b574c6a9 2196 ret = pk->legacy_cache_pkey.ptr;
4ce1025a 2197
b574c6a9
MC
2198 if (!CRYPTO_THREAD_unlock(pk->lock))
2199 return NULL;
a8154452 2200
b574c6a9
MC
2201 if (ret != NULL)
2202 return ret;
4ce1025a 2203
b574c6a9 2204 if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
d8732803 2205 goto err;
4ce1025a 2206
b574c6a9
MC
2207 if (!CRYPTO_THREAD_write_lock(pk->lock))
2208 goto err;
4ce1025a 2209
b574c6a9
MC
2210 /* Check again in case some other thread has updated it in the meantime */
2211 ret = pk->legacy_cache_pkey.ptr;
2212 if (ret == NULL) {
2213 /* Steal the legacy key reference from the temporary copy */
2214 ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2215 tmp_copy->pkey.ptr = NULL;
2216 }
4ce1025a 2217
b574c6a9
MC
2218 if (!CRYPTO_THREAD_unlock(pk->lock)) {
2219 ret = NULL;
2220 goto err;
acb90ba8 2221 }
4ce1025a 2222
b574c6a9
MC
2223 err:
2224 EVP_PKEY_free(tmp_copy);
2225
2226 return ret;
badf51c8 2227}
f844f9eb 2228#endif /* FIPS_MODULE */
96ebe52e 2229
a73a1892
RL
2230int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2231 BIGNUM **bn)
96ebe52e
SL
2232{
2233 int ret = 0;
2234 OSSL_PARAM params[2];
2235 unsigned char buffer[2048];
96ebe52e
SL
2236 unsigned char *buf = NULL;
2237 size_t buf_sz = 0;
2238
d82c7f3d 2239 if (key_name == NULL
6084b5c2 2240 || bn == NULL)
96ebe52e
SL
2241 return 0;
2242
2243 memset(buffer, 0, sizeof(buffer));
2244 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
96ebe52e 2245 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2246 if (!EVP_PKEY_get_params(pkey, params)) {
99ea4f02 2247 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
96ebe52e
SL
2248 return 0;
2249 buf_sz = params[0].return_size;
2250 /*
2251 * If it failed because the buffer was too small then allocate the
2252 * required buffer size and retry.
2253 */
2254 buf = OPENSSL_zalloc(buf_sz);
2255 if (buf == NULL)
2256 return 0;
2257 params[0].data = buf;
2258 params[0].data_size = buf_sz;
2259
13e85fb3 2260 if (!EVP_PKEY_get_params(pkey, params))
96ebe52e
SL
2261 goto err;
2262 }
2263 /* Fail if the param was not found */
99ea4f02 2264 if (!OSSL_PARAM_modified(params))
96ebe52e
SL
2265 goto err;
2266 ret = OSSL_PARAM_get_BN(params, bn);
2267err:
34e4a962
AL
2268 if (buf != NULL) {
2269 if (OSSL_PARAM_modified(params))
2270 OPENSSL_clear_free(buf, buf_sz);
2271 else
2272 OPENSSL_free(buf);
2273 } else if (OSSL_PARAM_modified(params)) {
2274 OPENSSL_cleanse(buffer, params[0].data_size);
2275 }
96ebe52e
SL
2276 return ret;
2277}
2278
a73a1892 2279int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
96ebe52e 2280 unsigned char *buf, size_t max_buf_sz,
4e92d5c7 2281 size_t *out_len)
96ebe52e
SL
2282{
2283 OSSL_PARAM params[2];
76624df1 2284 int ret1 = 0, ret2 = 0;
96ebe52e 2285
6084b5c2 2286 if (key_name == NULL)
96ebe52e
SL
2287 return 0;
2288
2289 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
96ebe52e 2290 params[1] = OSSL_PARAM_construct_end();
76624df1
RL
2291 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2292 ret2 = OSSL_PARAM_modified(params);
4e92d5c7
RL
2293 if (ret2 && out_len != NULL)
2294 *out_len = params[0].return_size;
76624df1 2295 return ret1 && ret2;
96ebe52e
SL
2296}
2297
a73a1892 2298int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
96ebe52e 2299 char *str, size_t max_buf_sz,
4e92d5c7 2300 size_t *out_len)
96ebe52e
SL
2301{
2302 OSSL_PARAM params[2];
76624df1 2303 int ret1 = 0, ret2 = 0;
96ebe52e 2304
d82c7f3d 2305 if (key_name == NULL)
96ebe52e
SL
2306 return 0;
2307
2308 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
96ebe52e 2309 params[1] = OSSL_PARAM_construct_end();
76624df1
RL
2310 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2311 ret2 = OSSL_PARAM_modified(params);
4e92d5c7
RL
2312 if (ret2 && out_len != NULL)
2313 *out_len = params[0].return_size;
2314
2315 if (ret2 && params[0].return_size == max_buf_sz)
2316 /* There was no space for a NUL byte */
2317 return 0;
2318 /* Add a terminating NUL byte for good measure */
2319 if (ret2 && str != NULL)
2320 str[params[0].return_size] = '\0';
2321
76624df1 2322 return ret1 && ret2;
96ebe52e
SL
2323}
2324
a73a1892
RL
2325int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2326 int *out)
96ebe52e
SL
2327{
2328 OSSL_PARAM params[2];
96ebe52e 2329
d82c7f3d 2330 if (key_name == NULL)
96ebe52e
SL
2331 return 0;
2332
2333 params[0] = OSSL_PARAM_construct_int(key_name, out);
96ebe52e 2334 params[1] = OSSL_PARAM_construct_end();
13e85fb3
RL
2335 return EVP_PKEY_get_params(pkey, params)
2336 && OSSL_PARAM_modified(params);
96ebe52e
SL
2337}
2338
a73a1892
RL
2339int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2340 size_t *out)
96ebe52e
SL
2341{
2342 OSSL_PARAM params[2];
96ebe52e 2343
d82c7f3d 2344 if (key_name == NULL)
96ebe52e
SL
2345 return 0;
2346
2347 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
96ebe52e 2348 params[1] = OSSL_PARAM_construct_end();
13e85fb3
RL
2349 return EVP_PKEY_get_params(pkey, params)
2350 && OSSL_PARAM_modified(params);
96ebe52e 2351}
98dbf2c1
SL
2352
2353int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2354{
2355 OSSL_PARAM params[2];
2356
d82c7f3d 2357 if (key_name == NULL)
98dbf2c1
SL
2358 return 0;
2359
2360 params[0] = OSSL_PARAM_construct_int(key_name, &in);
2361 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2362 return EVP_PKEY_set_params(pkey, params);
98dbf2c1
SL
2363}
2364
2365int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2366{
2367 OSSL_PARAM params[2];
2368
d82c7f3d 2369 if (key_name == NULL)
98dbf2c1
SL
2370 return 0;
2371
2372 params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2373 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2374 return EVP_PKEY_set_params(pkey, params);
98dbf2c1
SL
2375}
2376
13e85fb3
RL
2377int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2378 const BIGNUM *bn)
98dbf2c1
SL
2379{
2380 OSSL_PARAM params[2];
2381 unsigned char buffer[2048];
2382 int bsize = 0;
2383
d82c7f3d
RL
2384 if (key_name == NULL
2385 || bn == NULL
2386 || pkey == NULL
6fcd92d3 2387 || !evp_pkey_is_assigned(pkey))
98dbf2c1
SL
2388 return 0;
2389
2390 bsize = BN_num_bytes(bn);
2391 if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2392 return 0;
2393
2394 if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2395 return 0;
2396 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2397 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2398 return EVP_PKEY_set_params(pkey, params);
98dbf2c1
SL
2399}
2400
2401int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
13e85fb3 2402 const char *str)
98dbf2c1
SL
2403{
2404 OSSL_PARAM params[2];
2405
d82c7f3d 2406 if (key_name == NULL)
98dbf2c1
SL
2407 return 0;
2408
13e85fb3 2409 params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
98dbf2c1 2410 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2411 return EVP_PKEY_set_params(pkey, params);
98dbf2c1
SL
2412}
2413
2414int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
13e85fb3 2415 const unsigned char *buf, size_t bsize)
98dbf2c1
SL
2416{
2417 OSSL_PARAM params[2];
2418
d82c7f3d 2419 if (key_name == NULL)
98dbf2c1
SL
2420 return 0;
2421
13e85fb3
RL
2422 params[0] = OSSL_PARAM_construct_octet_string(key_name,
2423 (unsigned char *)buf, bsize);
98dbf2c1 2424 params[1] = OSSL_PARAM_construct_end();
13e85fb3 2425 return EVP_PKEY_set_params(pkey, params);
98dbf2c1
SL
2426}
2427
d82c7f3d 2428const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
98dbf2c1 2429{
d82c7f3d
RL
2430 return (pkey != NULL && evp_pkey_is_provided(pkey))
2431 ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2432 : NULL;
98dbf2c1
SL
2433}
2434
2435int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2436{
6fcd92d3
RL
2437 if (pkey != NULL) {
2438 if (evp_pkey_is_provided(pkey)) {
2439 pkey->dirty_cnt++;
2440 return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2441 }
2442#ifndef FIPS_MODULE
2443 /*
6fcd92d3
RL
2444 * We will hopefully never find the need to set individual data in
2445 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2446 * sure. This bit of code can be enabled if we find the need. If
2447 * not, it can safely be removed when #legacy support is removed.
2448 */
2449# if 0
2450 else if (evp_pkey_is_legacy(pkey)) {
2451 return evp_pkey_set_params_to_ctrl(pkey, params);
2452 }
2453# endif
2454#endif
2455 }
2456 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2457 return 0;
98dbf2c1 2458}
3d34bedf 2459
13e85fb3
RL
2460const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2461{
d82c7f3d
RL
2462 return (pkey != NULL && evp_pkey_is_provided(pkey))
2463 ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2464 : NULL;
13e85fb3
RL
2465}
2466
2467int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2468{
6fcd92d3
RL
2469 if (pkey != NULL) {
2470 if (evp_pkey_is_provided(pkey))
7e5e9117 2471 return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
6fcd92d3
RL
2472#ifndef FIPS_MODULE
2473 else if (evp_pkey_is_legacy(pkey))
7e5e9117 2474 return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
6fcd92d3
RL
2475#endif
2476 }
2477 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2478 return 0;
13e85fb3
RL
2479}
2480
3d34bedf
MC
2481#ifndef FIPS_MODULE
2482int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2483{
2484 char name[80];
2485 size_t name_len;
2486
2487 if (pkey == NULL)
2488 return 0;
2489
2490 if (pkey->keymgmt == NULL
2491 || pkey->keydata == NULL) {
0e2f87c0 2492# ifndef OPENSSL_NO_EC
3d34bedf 2493 /* Might work through the legacy route */
7bc0fdd3 2494 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
3d34bedf
MC
2495
2496 if (ec == NULL)
2497 return 0;
2498
2499 return EC_KEY_get_conv_form(ec);
0e2f87c0 2500# else
3d34bedf 2501 return 0;
0e2f87c0 2502# endif
3d34bedf
MC
2503 }
2504
2505 if (!EVP_PKEY_get_utf8_string_param(pkey,
2506 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2507 name, sizeof(name), &name_len))
2508 return 0;
2509
2510 if (strcmp(name, "uncompressed") == 0)
2511 return POINT_CONVERSION_UNCOMPRESSED;
2512
2513 if (strcmp(name, "compressed") == 0)
2514 return POINT_CONVERSION_COMPRESSED;
2515
2516 if (strcmp(name, "hybrid") == 0)
2517 return POINT_CONVERSION_HYBRID;
2518
2519 return 0;
2520}
2521
2522int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2523{
2524 char fstr[80];
2525 size_t fstrlen;
2526
2527 if (pkey == NULL)
2528 return 0;
2529
2530 if (pkey->keymgmt == NULL
2531 || pkey->keydata == NULL) {
0e2f87c0 2532# ifndef OPENSSL_NO_EC
3d34bedf 2533 /* Might work through the legacy route */
7bc0fdd3 2534 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
3d34bedf
MC
2535 const EC_GROUP *grp;
2536
2537 if (ec == NULL)
2538 return 0;
2539 grp = EC_KEY_get0_group(ec);
82a46200
TM
2540 if (grp == NULL)
2541 return 0;
3d34bedf
MC
2542
2543 return EC_GROUP_get_field_type(grp);
0e2f87c0 2544# else
3d34bedf 2545 return 0;
0e2f87c0 2546# endif
3d34bedf
MC
2547 }
2548
2549 if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2550 fstr, sizeof(fstr), &fstrlen))
2551 return 0;
2552
2553 if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2554 return NID_X9_62_prime_field;
2555 else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2556 return NID_X9_62_characteristic_two_field;
2557
2558 return 0;
2559}
2560#endif