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