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