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