2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "internal/namemap.h"
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 #include <openssl/objects.h>
24 #include <openssl/evp.h>
25 #include <openssl/rsa.h>
26 #include <openssl/dsa.h>
27 #include <openssl/dh.h>
28 #include <openssl/ec.h>
29 #include <openssl/cmac.h>
31 # include <openssl/engine.h>
33 #include <openssl/params.h>
34 #include <openssl/param_build.h>
35 #include <openssl/encoder.h>
36 #include <openssl/core_names.h>
38 #include "internal/numbers.h" /* includes SIZE_MAX */
39 #include "internal/ffc.h"
40 #include "crypto/evp.h"
41 #include "crypto/dh.h"
42 #include "crypto/dsa.h"
43 #include "crypto/ec.h"
44 #include "crypto/ecx.h"
45 #include "crypto/rsa.h"
47 # include "crypto/asn1.h"
48 # include "crypto/x509.h"
50 #include "internal/provider.h"
51 #include "evp_local.h"
53 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
54 int len
, EVP_KEYMGMT
*keymgmt
);
55 static void evp_pkey_free_it(EVP_PKEY
*key
);
57 /* The type of parameters selected in key parameter functions */
58 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
61 int EVP_PKEY_get_bits(const EVP_PKEY
*pkey
)
66 size
= pkey
->cache
.bits
;
67 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
68 size
= pkey
->ameth
->pkey_bits(pkey
);
71 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_BITS
);
77 int EVP_PKEY_get_security_bits(const EVP_PKEY
*pkey
)
82 size
= pkey
->cache
.security_bits
;
83 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
84 size
= pkey
->ameth
->pkey_security_bits(pkey
);
87 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_SECURITY_BITS
);
93 int EVP_PKEY_get_security_category(const EVP_PKEY
*pkey
)
95 return pkey
!= NULL
? pkey
->cache
.security_category
: -1;
98 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
100 # ifndef OPENSSL_NO_DSA
101 if (pkey
->type
== EVP_PKEY_DSA
) {
102 int ret
= pkey
->save_parameters
;
105 pkey
->save_parameters
= mode
;
109 # ifndef OPENSSL_NO_EC
110 if (pkey
->type
== EVP_PKEY_EC
) {
111 int ret
= pkey
->save_parameters
;
114 pkey
->save_parameters
= mode
;
121 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
123 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
126 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
128 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
130 #endif /* !FIPS_MODULE */
132 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
135 * Clean up legacy stuff from this function when legacy support is gone.
138 EVP_PKEY
*downgraded_from
= NULL
;
143 * If |to| is a legacy key and |from| isn't, we must make a downgraded
144 * copy of |from|. If that fails, this function fails.
146 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
147 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
149 from
= downgraded_from
;
151 #endif /* !FIPS_MODULE */
154 * Make sure |to| is typed. Content is less important at this early
157 * 1. If |to| is untyped, assign |from|'s key type to it.
158 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
159 * (|from| was already downgraded above)
161 * If |to| is a provided key, there's nothing more to do here, functions
162 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
163 * further down help us find out if they are the same or not.
165 if (evp_pkey_is_blank(to
)) {
167 if (evp_pkey_is_legacy(from
)) {
168 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
171 #endif /* !FIPS_MODULE */
173 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
178 else if (evp_pkey_is_legacy(to
)) {
179 if (to
->type
!= from
->type
) {
180 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
184 #endif /* !FIPS_MODULE */
186 if (EVP_PKEY_missing_parameters(from
)) {
187 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
191 if (!EVP_PKEY_missing_parameters(to
)) {
192 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
195 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
199 /* For purely provided keys, we just call the keymgmt utility */
200 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
201 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
207 * If |to| is provided, we know that |from| is legacy at this point.
208 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
209 * to copy the appropriate data to |to|'s keydata.
210 * We cannot override existing data so do it only if there is no keydata
213 if (to
->keymgmt
!= NULL
&& to
->keydata
== NULL
) {
214 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
216 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
220 * If we get a NULL, it could be an internal error, or it could be
221 * that there's a key mismatch. We're pretending the latter...
223 if (from_keydata
== NULL
)
224 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
226 ok
= (to
->keydata
= evp_keymgmt_dup(to
->keymgmt
,
228 SELECT_PARAMETERS
)) != NULL
;
232 /* Both keys are legacy */
233 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
234 ok
= from
->ameth
->param_copy(to
, from
);
235 #endif /* !FIPS_MODULE */
237 EVP_PKEY_free(downgraded_from
);
241 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
245 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
247 if (pkey
->keymgmt
!= NULL
)
248 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
249 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
250 return pkey
->ameth
->param_missing(pkey
);
251 #endif /* FIPS_MODULE */
257 * This function is called for any mixture of keys except pure legacy pair.
258 * When legacy keys are gone, we replace a call to this functions with
259 * a call to evp_keymgmt_util_match().
261 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
265 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
267 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
268 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
270 /* If none of them are provided, this function shouldn't have been called */
271 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
274 /* For purely provided keys, we just call the keymgmt utility */
275 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
276 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
279 * At this point, one of them is provided, the other not. This allows
280 * us to compare types using legacy NIDs.
282 if (evp_pkey_is_legacy(a
)
283 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
284 return -1; /* not the same key type */
285 if (evp_pkey_is_legacy(b
)
286 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
287 return -1; /* not the same key type */
290 * We've determined that they both are the same keytype, so the next
291 * step is to do a bit of cross export to ensure we have keydata for
292 * both keys in the same keymgmt.
294 keymgmt1
= a
->keymgmt
;
295 keydata1
= a
->keydata
;
296 keymgmt2
= b
->keymgmt
;
297 keydata2
= b
->keydata
;
299 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
301 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
302 if (tmp_keydata
!= NULL
) {
304 keydata1
= tmp_keydata
;
307 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
309 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
310 if (tmp_keydata
!= NULL
) {
312 keydata2
= tmp_keydata
;
316 /* If we still don't have matching keymgmt implementations, we give up */
317 if (keymgmt1
!= keymgmt2
)
320 /* If the keymgmt implementations are NULL, the export failed */
321 if (keymgmt1
== NULL
)
324 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
325 #endif /* FIPS_MODULE */
329 # ifndef OPENSSL_NO_DEPRECATED_3_0
330 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
332 return EVP_PKEY_parameters_eq(a
, b
);
335 #endif /* FIPS_MODULE */
337 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
340 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
343 * This will just call evp_keymgmt_util_match when legacy support
347 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
348 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
350 /* All legacy keys */
351 if (a
->type
!= b
->type
)
353 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
354 return a
->ameth
->param_cmp(a
, b
);
356 #endif /* !FIPS_MODULE */
360 # ifndef OPENSSL_NO_DEPRECATED_3_0
361 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
363 return EVP_PKEY_eq(a
, b
);
366 #endif /* !FIPS_MODULE */
368 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
371 * This will just call evp_keymgmt_util_match when legacy support
375 /* Trivial shortcuts */
378 if (a
== NULL
|| b
== NULL
)
382 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
383 #endif /* !FIPS_MODULE */
385 int selection
= SELECT_PARAMETERS
;
387 if (evp_keymgmt_util_has((EVP_PKEY
*)a
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
)
388 && evp_keymgmt_util_has((EVP_PKEY
*)b
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
))
389 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
391 selection
|= OSSL_KEYMGMT_SELECT_KEYPAIR
;
392 return evp_pkey_cmp_any(a
, b
, selection
);
396 /* All legacy keys */
397 if (a
->type
!= b
->type
)
400 if (a
->ameth
!= NULL
) {
402 /* Compare parameters if the algorithm has them */
403 if (a
->ameth
->param_cmp
!= NULL
) {
404 ret
= a
->ameth
->param_cmp(a
, b
);
409 if (a
->ameth
->pub_cmp
!= NULL
)
410 return a
->ameth
->pub_cmp(a
, b
);
414 #endif /* !FIPS_MODULE */
418 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
423 const unsigned char *key
,
427 EVP_PKEY
*pkey
= NULL
;
428 EVP_PKEY_CTX
*ctx
= NULL
;
429 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
432 # ifndef OPENSSL_NO_ENGINE
433 /* Check if there is an Engine for this type */
438 ameth
= EVP_PKEY_asn1_find_str(&tmpe
, strtype
, -1);
439 else if (nidtype
!= EVP_PKEY_NONE
)
440 ameth
= EVP_PKEY_asn1_find(&tmpe
, nidtype
);
442 /* If tmpe is NULL then no engine is claiming to support this type */
450 if (e
== NULL
&& ameth
== NULL
) {
452 * No engine is claiming to support this type, so lets see if we have
455 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
456 strtype
!= NULL
? strtype
457 : OBJ_nid2sn(nidtype
),
461 /* May fail if no provider available */
463 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
464 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
466 ERR_clear_last_mark();
467 params
[0] = OSSL_PARAM_construct_octet_string(
468 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
469 : OSSL_PKEY_PARAM_PUB_KEY
,
472 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
473 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
477 EVP_PKEY_CTX_free(ctx
);
482 /* else not supported so fallback to legacy */
485 /* Legacy code path */
487 pkey
= EVP_PKEY_new();
489 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
493 if (!pkey_set_type(pkey
, e
, nidtype
, strtype
, -1, NULL
)) {
494 /* ERR_raise(ERR_LIB_EVP, ...) already called */
498 if (!ossl_assert(pkey
->ameth
!= NULL
))
502 if (pkey
->ameth
->set_priv_key
== NULL
) {
503 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
507 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
508 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
512 if (pkey
->ameth
->set_pub_key
== NULL
) {
513 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
517 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
518 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
529 EVP_PKEY_CTX_free(ctx
);
533 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
536 const unsigned char *priv
, size_t len
)
538 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, priv
,
542 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
543 const unsigned char *priv
,
546 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, priv
, len
, 1);
549 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
550 const char *keytype
, const char *propq
,
551 const unsigned char *pub
, size_t len
)
553 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, pub
,
557 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
558 const unsigned char *pub
,
561 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, pub
, len
, 0);
564 struct raw_key_details_st
{
570 static OSSL_CALLBACK get_raw_key_details
;
571 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
573 const OSSL_PARAM
*p
= NULL
;
574 struct raw_key_details_st
*raw_key
= arg
;
576 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
577 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
579 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
580 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
582 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
583 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
585 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
586 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
593 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
596 if (pkey
->keymgmt
!= NULL
) {
597 struct raw_key_details_st raw_key
;
599 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
601 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
603 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
604 get_raw_key_details
, &raw_key
);
607 if (pkey
->ameth
== NULL
) {
608 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
612 if (pkey
->ameth
->get_priv_key
== NULL
) {
613 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
617 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
618 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
625 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
628 if (pkey
->keymgmt
!= NULL
) {
629 struct raw_key_details_st raw_key
;
631 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
633 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
635 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
636 get_raw_key_details
, &raw_key
);
639 if (pkey
->ameth
== NULL
) {
640 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
644 if (pkey
->ameth
->get_pub_key
== NULL
) {
645 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
649 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
650 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
657 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
658 const char *cipher_name
,
659 const EVP_CIPHER
*cipher
,
660 OSSL_LIB_CTX
*libctx
,
661 const char *propq
, ENGINE
*e
)
663 # ifndef OPENSSL_NO_CMAC
664 # ifndef OPENSSL_NO_ENGINE
665 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
667 OSSL_PARAM params
[5], *p
= params
;
668 EVP_PKEY
*pkey
= NULL
;
672 cipher_name
= EVP_CIPHER_get0_name(cipher
);
674 if (cipher_name
== NULL
) {
675 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
679 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
683 if (EVP_PKEY_fromdata_init(ctx
) <= 0) {
684 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
688 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
690 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
691 (char *)cipher_name
, 0);
693 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
695 # ifndef OPENSSL_NO_ENGINE
696 if (engine_id
!= NULL
)
697 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE
,
698 (char *)engine_id
, 0);
700 *p
= OSSL_PARAM_construct_end();
702 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) <= 0) {
703 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
708 EVP_PKEY_CTX_free(ctx
);
712 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
717 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
718 size_t len
, const EVP_CIPHER
*cipher
)
720 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
, e
);
723 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
725 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
728 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
730 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
733 # ifndef OPENSSL_NO_ENGINE
734 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
737 if (!ENGINE_init(e
)) {
738 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
741 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
743 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
747 ENGINE_finish(pkey
->pmeth_engine
);
748 pkey
->pmeth_engine
= e
;
752 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
758 # ifndef OPENSSL_NO_DEPRECATED_3_0
759 static void detect_foreign_key(EVP_PKEY
*pkey
)
761 switch (pkey
->type
) {
763 case EVP_PKEY_RSA_PSS
:
764 pkey
->foreign
= pkey
->pkey
.rsa
!= NULL
765 && ossl_rsa_is_foreign(pkey
->pkey
.rsa
);
767 # ifndef OPENSSL_NO_EC
771 pkey
->foreign
= pkey
->pkey
.ec
!= NULL
772 && ossl_ec_key_is_foreign(pkey
->pkey
.ec
);
775 # ifndef OPENSSL_NO_DSA
777 pkey
->foreign
= pkey
->pkey
.dsa
!= NULL
778 && ossl_dsa_is_foreign(pkey
->pkey
.dsa
);
781 # ifndef OPENSSL_NO_DH
783 pkey
->foreign
= pkey
->pkey
.dh
!= NULL
784 && ossl_dh_is_foreign(pkey
->pkey
.dh
);
793 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
795 # ifndef OPENSSL_NO_EC
798 pktype
= EVP_PKEY_type(type
);
799 if ((key
!= NULL
) && (pktype
== EVP_PKEY_EC
|| pktype
== EVP_PKEY_SM2
)) {
800 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
803 int curve
= EC_GROUP_get_curve_name(group
);
806 * Regardless of what is requested the SM2 curve must be SM2 type,
807 * and non SM2 curves are EC type.
809 if (curve
== NID_sm2
&& pktype
== EVP_PKEY_EC
)
811 else if(curve
!= NID_sm2
&& pktype
== EVP_PKEY_SM2
)
817 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
820 pkey
->pkey
.ptr
= key
;
821 detect_foreign_key(pkey
);
823 return (key
!= NULL
);
827 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
832 if (!evp_pkey_is_provided(pkey
))
833 return pkey
->pkey
.ptr
;
838 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
840 const ASN1_OCTET_STRING
*os
= NULL
;
841 if (pkey
->type
!= EVP_PKEY_HMAC
) {
842 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
845 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
853 # ifndef OPENSSL_NO_POLY1305
854 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
856 const ASN1_OCTET_STRING
*os
= NULL
;
857 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
858 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
861 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
870 # ifndef OPENSSL_NO_SIPHASH
871 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
873 const ASN1_OCTET_STRING
*os
= NULL
;
875 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
876 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
879 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
888 # ifndef OPENSSL_NO_DSA
889 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
891 if (pkey
->type
!= EVP_PKEY_DSA
) {
892 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
895 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
898 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
900 return evp_pkey_get0_DSA_int(pkey
);
903 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
907 if (!DSA_up_ref(key
))
910 ret
= EVP_PKEY_assign_DSA(pkey
, key
);
917 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
919 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
921 if (ret
!= NULL
&& !DSA_up_ref(ret
))
926 # endif /* OPENSSL_NO_DSA */
928 # ifndef OPENSSL_NO_ECX
929 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
931 if (EVP_PKEY_get_base_id(pkey
) != type
) {
932 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
935 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
938 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
940 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
942 if (ret
!= NULL
&& !ossl_ecx_key_up_ref(ret
))
947 # define IMPLEMENT_ECX_VARIANT(NAME) \
948 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
950 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
952 IMPLEMENT_ECX_VARIANT(X25519
)
953 IMPLEMENT_ECX_VARIANT(X448
)
954 IMPLEMENT_ECX_VARIANT(ED25519
)
955 IMPLEMENT_ECX_VARIANT(ED448
)
957 # endif /* OPENSSL_NO_ECX */
959 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
961 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*dhkey
)
966 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
967 * related to ffdhe and modp (which cache q = (p - 1) / 2),
968 * and returns 0 for all other dh parameter generation types including
969 * RFC5114 named groups.
971 * The EVP_PKEY_DH type is used for dh parameter generation types:
972 * - named safe prime groups related to ffdhe and modp
973 * - safe prime generator
975 * The type EVP_PKEY_DHX is used for dh parameter generation types
976 * - fips186-4 and fips186-2
977 * - rfc5114 named groups.
979 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
981 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
982 * q value to be stored.
984 if (ossl_dh_is_named_safe_prime_group(dhkey
))
987 type
= DH_get0_q(dhkey
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
989 if (!DH_up_ref(dhkey
))
992 ret
= EVP_PKEY_assign(pkey
, type
, dhkey
);
1000 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
1002 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
1003 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
1006 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
1009 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
1011 return evp_pkey_get0_DH_int(pkey
);
1014 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
1016 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
1018 if (ret
!= NULL
&& !DH_up_ref(ret
))
1025 int EVP_PKEY_type(int type
)
1028 const EVP_PKEY_ASN1_METHOD
*ameth
;
1030 ameth
= EVP_PKEY_asn1_find(&e
, type
);
1032 ret
= ameth
->pkey_id
;
1035 # ifndef OPENSSL_NO_ENGINE
1041 int EVP_PKEY_get_id(const EVP_PKEY
*pkey
)
1046 int EVP_PKEY_get_base_id(const EVP_PKEY
*pkey
)
1048 return EVP_PKEY_type(pkey
->type
);
1052 * These hard coded cases are pure hackery to get around the fact
1053 * that names in crypto/objects/objects.txt are a mess. There is
1054 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
1055 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
1056 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
1057 * "DSA" is accurate... but still, better be safe and hard-code
1058 * names that we know.
1059 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
1060 * EVP_PKEY_EC, because of aliasing.
1061 * This should be cleaned away along with all other #legacy support.
1063 static const OSSL_ITEM standard_name2type
[] = {
1064 { EVP_PKEY_RSA
, "RSA" },
1065 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
1066 { EVP_PKEY_EC
, "EC" },
1067 { EVP_PKEY_ED25519
, "ED25519" },
1068 { EVP_PKEY_ED448
, "ED448" },
1069 { EVP_PKEY_X25519
, "X25519" },
1070 { EVP_PKEY_X448
, "X448" },
1071 { EVP_PKEY_SM2
, "SM2" },
1072 { EVP_PKEY_DH
, "DH" },
1073 { EVP_PKEY_DHX
, "X9.42 DH" },
1074 { EVP_PKEY_DHX
, "DHX" },
1075 { EVP_PKEY_DSA
, "DSA" },
1078 int evp_pkey_name2type(const char *name
)
1083 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1084 if (OPENSSL_strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
1085 return (int)standard_name2type
[i
].id
;
1088 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
1090 return EVP_PKEY_type(OBJ_ln2nid(name
));
1093 const char *evp_pkey_type2name(int type
)
1097 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1098 if (type
== (int)standard_name2type
[i
].id
)
1099 return standard_name2type
[i
].ptr
;
1102 return OBJ_nid2sn(type
);
1105 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
1109 if (pkey
->keymgmt
== NULL
)
1110 return pkey
->type
== evp_pkey_name2type(name
);
1111 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
1114 int EVP_PKEY_type_names_do_all(const EVP_PKEY
*pkey
,
1115 void (*fn
)(const char *name
, void *data
),
1118 if (!evp_pkey_is_typed(pkey
))
1121 if (!evp_pkey_is_provided(pkey
)) {
1122 const char *name
= OBJ_nid2sn(EVP_PKEY_get_id(pkey
));
1127 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1130 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1132 if (pkey
->keymgmt
== NULL
) {
1133 switch (EVP_PKEY_get_base_id(pkey
)) {
1135 case EVP_PKEY_RSA_PSS
:
1137 # ifndef OPENSSL_NO_DSA
1141 # ifndef OPENSSL_NO_EC
1142 case EVP_PKEY_ED25519
:
1143 case EVP_PKEY_ED448
:
1145 case EVP_PKEY_EC
: /* Including SM2 */
1146 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1152 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_get0_provider(pkey
->keymgmt
);
1153 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1154 const char *supported_sig
=
1155 pkey
->keymgmt
->query_operation_name
!= NULL
1156 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
1157 : EVP_KEYMGMT_get0_name(pkey
->keymgmt
);
1158 EVP_SIGNATURE
*signature
= NULL
;
1160 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
1161 if (signature
!= NULL
) {
1162 EVP_SIGNATURE_free(signature
);
1169 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1171 BIO_set_indent(*out
, saved_indent
);
1173 BIO
*next
= BIO_pop(*out
);
1181 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1187 long i
= BIO_get_indent(*out
);
1189 *saved_indent
= (i
< 0 ? 0 : i
);
1190 if (BIO_set_indent(*out
, indent
) <= 0) {
1191 BIO
*prefbio
= BIO_new(BIO_f_prefix());
1193 if (prefbio
== NULL
)
1195 *out
= BIO_push(prefbio
, *out
);
1198 if (BIO_set_indent(*out
, indent
) <= 0) {
1199 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1206 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1209 return BIO_indent(out
, indent
, 128)
1210 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1211 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
1214 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1215 int selection
/* For provided encoding */,
1216 const char *propquery
/* For provided encoding */,
1217 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1218 int indent
, ASN1_PCTX
*pctx
),
1219 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1223 OSSL_ENCODER_CTX
*ctx
= NULL
;
1224 int ret
= -2; /* default to unsupported */
1226 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1229 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1231 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1232 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1233 OSSL_ENCODER_CTX_free(ctx
);
1238 /* legacy fallback */
1239 if (legacy_print
!= NULL
)
1240 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1242 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1245 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1249 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1250 int indent
, ASN1_PCTX
*pctx
)
1252 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1253 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1257 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1258 int indent
, ASN1_PCTX
*pctx
)
1260 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PRIVATE_KEY
, NULL
,
1261 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1265 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1266 int indent
, ASN1_PCTX
*pctx
)
1268 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1269 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1273 # ifndef OPENSSL_NO_STDIO
1274 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1275 int indent
, ASN1_PCTX
*pctx
)
1278 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1282 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1287 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1288 int indent
, ASN1_PCTX
*pctx
)
1291 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1295 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1300 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1301 int indent
, ASN1_PCTX
*pctx
)
1304 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1308 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1314 static void mdname2nid(const char *mdname
, void *data
)
1316 int *nid
= (int *)data
;
1318 if (*nid
!= NID_undef
)
1321 *nid
= OBJ_sn2nid(mdname
);
1322 if (*nid
== NID_undef
)
1323 *nid
= OBJ_ln2nid(mdname
);
1326 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1327 int arg1
, void *arg2
)
1329 if (pkey
->keymgmt
== NULL
)
1332 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
1334 char mdname
[80] = "";
1335 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1340 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1341 /* Make sure the MD is in the namemap if available */
1343 OSSL_NAMEMAP
*namemap
;
1344 int nid
= NID_undef
;
1346 (void)ERR_set_mark();
1347 md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1348 (void)ERR_pop_to_mark();
1349 namemap
= ossl_namemap_stored(libctx
);
1352 * The only reason to fetch the MD was to make sure it is in the
1353 * namemap. We can immediately free it.
1356 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1361 * We have the namemap number - now we need to find the
1364 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1375 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1377 if (pkey
->ameth
== NULL
)
1378 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1379 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1381 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1384 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1388 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1391 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1392 char *mdname
, size_t mdname_sz
)
1394 if (pkey
->ameth
== NULL
)
1395 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1400 int nid
= NID_undef
;
1401 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1402 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1405 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1410 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1413 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1414 gname
, gname_sz
, gname_len
);
1417 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY
*pkey
, OSSL_LIB_CTX
*libctx
,
1418 const char *name
, const char *propq
)
1421 EVP_MD_CTX
*ctx
= NULL
;
1423 if ((ctx
= EVP_MD_CTX_new()) == NULL
)
1427 rv
= EVP_DigestSignInit_ex(ctx
, NULL
, name
, libctx
,
1431 EVP_MD_CTX_free(ctx
);
1434 #endif /* !FIPS_MODULE */
1436 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1442 if (evp_pkey_is_provided(pkey
))
1443 #endif /* !FIPS_MODULE */
1445 EVP_PKEY_set_octet_string_param(pkey
,
1446 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1447 (unsigned char *)pub
, publen
);
1450 if (publen
> INT_MAX
)
1452 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1453 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, (int)publen
,
1457 #endif /* !FIPS_MODULE */
1460 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1465 if (evp_pkey_is_provided(pkey
))
1468 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1472 * We know that this is going to fail, but it will give us a size
1475 EVP_PKEY_get_octet_string_param(pkey
,
1476 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1477 NULL
, 0, &return_size
);
1478 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1482 buf
= OPENSSL_malloc(return_size
);
1486 if (!EVP_PKEY_get_octet_string_param(pkey
,
1487 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1488 buf
, return_size
, NULL
)) {
1498 int rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1503 #endif /* !FIPS_MODULE */
1506 /*- All methods below can also be used in FIPS_MODULE */
1508 EVP_PKEY
*EVP_PKEY_new(void)
1510 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1515 ret
->type
= EVP_PKEY_NONE
;
1516 ret
->save_type
= EVP_PKEY_NONE
;
1518 if (!CRYPTO_NEW_REF(&ret
->references
, 1))
1521 ret
->lock
= CRYPTO_THREAD_lock_new();
1522 if (ret
->lock
== NULL
) {
1523 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1528 ret
->save_parameters
= 1;
1529 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1530 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1537 CRYPTO_FREE_REF(&ret
->references
);
1538 CRYPTO_THREAD_lock_free(ret
->lock
);
1544 * Setup a public key management method.
1546 * For legacy keys, either |type| or |str| is expected to have the type
1547 * information. In this case, the setup consists of finding an ASN1 method
1548 * and potentially an ENGINE, and setting those fields in |pkey|.
1550 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1551 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1553 * If pkey is NULL just return 1 or 0 if the key management method exists.
1556 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1557 int len
, EVP_KEYMGMT
*keymgmt
)
1560 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1561 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1565 * The setups can't set both legacy and provider side methods.
1568 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1569 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1570 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1578 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1580 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1582 evp_pkey_free_it(pkey
);
1585 * If key type matches and a method exists then this lookup has
1586 * succeeded once so just indicate success.
1588 if (pkey
->type
!= EVP_PKEY_NONE
1589 && type
== pkey
->save_type
1590 && pkey
->ameth
!= NULL
)
1592 # ifndef OPENSSL_NO_ENGINE
1593 /* If we have ENGINEs release them */
1594 ENGINE_finish(pkey
->engine
);
1595 pkey
->engine
= NULL
;
1596 ENGINE_finish(pkey
->pmeth_engine
);
1597 pkey
->pmeth_engine
= NULL
;
1603 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1604 else if (type
!= EVP_PKEY_NONE
)
1605 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1606 # ifndef OPENSSL_NO_ENGINE
1607 if (pkey
== NULL
&& eptr
!= NULL
)
1617 check
= check
&& ameth
== NULL
;
1619 check
= check
&& keymgmt
== NULL
;
1621 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1626 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1627 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1631 pkey
->keymgmt
= keymgmt
;
1633 pkey
->save_type
= type
;
1638 * If the internal "origin" key is provider side, don't save |ameth|.
1639 * The main reason is that |ameth| is one factor to detect that the
1640 * internal "origin" key is a legacy one.
1642 if (keymgmt
== NULL
)
1643 pkey
->ameth
= ameth
;
1646 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1647 * for any key type that has a legacy implementation, regardless of
1648 * if the internal key is a legacy or a provider side one. When
1649 * there is no legacy implementation for the key, the type becomes
1650 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1651 * with functions that expect legacy internal keys.
1653 if (ameth
!= NULL
) {
1654 if (type
== EVP_PKEY_NONE
)
1655 pkey
->type
= ameth
->pkey_id
;
1657 pkey
->type
= EVP_PKEY_KEYMGMT
;
1659 # ifndef OPENSSL_NO_ENGINE
1660 if (eptr
== NULL
&& e
!= NULL
&& !ENGINE_init(e
)) {
1661 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1672 static void find_ameth(const char *name
, void *data
)
1674 const char **str
= data
;
1677 * The error messages from pkey_set_type() are uninteresting here,
1682 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, (int)strlen(name
),
1686 else if (str
[1] == NULL
)
1694 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1697 # define EVP_PKEY_TYPE_STR str[0]
1698 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1700 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1701 * Ideally, only one should be found. If two (or more) are found, the
1702 * match is ambiguous. This should never happen, but...
1704 const char *str
[2] = { NULL
, NULL
};
1706 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1707 || str
[1] != NULL
) {
1708 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1712 # define EVP_PKEY_TYPE_STR NULL
1713 # define EVP_PKEY_TYPE_STRLEN -1
1715 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1716 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1719 #undef EVP_PKEY_TYPE_STR
1720 #undef EVP_PKEY_TYPE_STRLEN
1723 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1727 if (CRYPTO_UP_REF(&pkey
->references
, &i
) <= 0)
1730 REF_PRINT_COUNT("EVP_PKEY", i
, pkey
);
1731 REF_ASSERT_ISNT(i
< 2);
1732 return ((i
> 1) ? 1 : 0);
1735 EVP_PKEY
*EVP_PKEY_dup(EVP_PKEY
*pkey
)
1740 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
1744 if ((dup_pk
= EVP_PKEY_new()) == NULL
)
1747 if (evp_pkey_is_blank(pkey
))
1751 if (evp_pkey_is_provided(pkey
))
1752 #endif /* !FIPS_MODULE */
1754 if (!evp_keymgmt_util_copy(dup_pk
, pkey
,
1755 OSSL_KEYMGMT_SELECT_ALL
))
1761 if (evp_pkey_is_legacy(pkey
)) {
1762 const EVP_PKEY_ASN1_METHOD
*ameth
= pkey
->ameth
;
1764 if (ameth
== NULL
|| ameth
->copy
== NULL
) {
1765 if (pkey
->pkey
.ptr
== NULL
/* empty key, just set type */
1766 && EVP_PKEY_set_type(dup_pk
, pkey
->type
) != 0)
1768 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_KEY_TYPE
);
1771 if (!ameth
->copy(dup_pk
, pkey
))
1775 #endif /* !FIPS_MODULE */
1780 /* copy auxiliary data */
1781 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
,
1782 &dup_pk
->ex_data
, &pkey
->ex_data
))
1785 if (pkey
->attributes
!= NULL
) {
1786 if ((dup_pk
->attributes
= ossl_x509at_dup(pkey
->attributes
)) == NULL
)
1789 #endif /* !FIPS_MODULE */
1792 EVP_PKEY_free(dup_pk
);
1797 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1799 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1800 ENGINE
*tmpe
= NULL
;
1802 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1803 ameth
= EVP_PKEY_asn1_find(&tmpe
, x
->type
);
1805 if (ameth
!= NULL
) {
1806 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1808 * We should never have both a legacy origin key, and a key in the
1811 assert(x
->pkey
.ptr
== NULL
);
1813 * For the purposes of freeing we make the legacy cache look like
1814 * a legacy origin key.
1816 x
->pkey
= x
->legacy_cache_pkey
;
1817 x
->legacy_cache_pkey
.ptr
= NULL
;
1819 if (ameth
->pkey_free
!= NULL
)
1820 ameth
->pkey_free(x
);
1823 # ifndef OPENSSL_NO_ENGINE
1824 ENGINE_finish(tmpe
);
1825 ENGINE_finish(x
->engine
);
1827 ENGINE_finish(x
->pmeth_engine
);
1828 x
->pmeth_engine
= NULL
;
1831 #endif /* FIPS_MODULE */
1833 static void evp_pkey_free_it(EVP_PKEY
*x
)
1835 /* internal function; x is never NULL */
1836 evp_keymgmt_util_clear_operation_cache(x
);
1838 evp_pkey_free_legacy(x
);
1841 if (x
->keymgmt
!= NULL
) {
1842 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1843 EVP_KEYMGMT_free(x
->keymgmt
);
1847 x
->type
= EVP_PKEY_NONE
;
1850 void EVP_PKEY_free(EVP_PKEY
*x
)
1857 CRYPTO_DOWN_REF(&x
->references
, &i
);
1858 REF_PRINT_COUNT("EVP_PKEY", i
, x
);
1861 REF_ASSERT_ISNT(i
< 0);
1862 evp_pkey_free_it(x
);
1864 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1866 CRYPTO_THREAD_lock_free(x
->lock
);
1867 CRYPTO_FREE_REF(&x
->references
);
1869 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1874 int EVP_PKEY_get_size(const EVP_PKEY
*pkey
)
1879 size
= pkey
->cache
.size
;
1881 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1882 size
= pkey
->ameth
->pkey_size(pkey
);
1886 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_MAX_SIZE
);
1892 const char *EVP_PKEY_get0_description(const EVP_PKEY
*pkey
)
1894 if (!evp_pkey_is_assigned(pkey
))
1897 if (evp_pkey_is_provided(pkey
) && pkey
->keymgmt
->description
!= NULL
)
1898 return pkey
->keymgmt
->description
;
1900 if (pkey
->ameth
!= NULL
)
1901 return pkey
->ameth
->info
;
1906 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1907 EVP_KEYMGMT
**keymgmt
,
1908 const char *propquery
)
1910 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1911 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1912 int selection
= OSSL_KEYMGMT_SELECT_ALL
;
1913 void *keydata
= NULL
;
1919 /* No key data => nothing to export */
1922 check
= check
&& pk
->pkey
.ptr
== NULL
;
1924 check
= check
&& pk
->keydata
== NULL
;
1929 if (pk
->pkey
.ptr
!= NULL
) {
1931 * If the legacy key doesn't have an dirty counter or export function,
1934 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1939 if (keymgmt
!= NULL
) {
1940 tmp_keymgmt
= *keymgmt
;
1945 * If no keymgmt was given or found, get a default keymgmt. We do so by
1946 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1948 if (tmp_keymgmt
== NULL
) {
1949 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1953 allocated_keymgmt
= tmp_keymgmt
= ctx
->keymgmt
;
1954 ctx
->keymgmt
= NULL
;
1955 EVP_PKEY_CTX_free(ctx
);
1958 /* If there's still no keymgmt to be had, give up */
1959 if (tmp_keymgmt
== NULL
)
1963 if (pk
->pkey
.ptr
!= NULL
) {
1967 * If the legacy "origin" hasn't changed since last time, we try
1968 * to find our keymgmt in the operation cache. If it has changed,
1969 * |i| remains zero, and we will clear the cache further down.
1971 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1972 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1974 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
,
1978 * If |tmp_keymgmt| is present in the operation cache, it means
1979 * that export doesn't need to be redone. In that case, we take
1980 * token copies of the cached pointers, to have token success
1981 * values to return. It is possible (e.g. in a no-cached-fetch
1982 * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1983 * even though the name/provider must be the same. In other words
1984 * the keymgmt instance may be different but still equivalent, i.e.
1985 * same algorithm/provider instance - but we make the simplifying
1986 * assumption that the keydata can be used with either keymgmt
1987 * instance. Not doing so introduces significant complexity and
1988 * probably requires refactoring - since we would have to ripple
1989 * the change in keymgmt instance up the call chain.
1991 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1992 keydata
= op
->keydata
;
1993 CRYPTO_THREAD_unlock(pk
->lock
);
1996 CRYPTO_THREAD_unlock(pk
->lock
);
1999 /* Make sure that the keymgmt key type matches the legacy NID */
2000 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
2003 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
2006 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
->import
,
2007 libctx
, propquery
)) {
2008 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
2014 * If the dirty counter changed since last time, then clear the
2015 * operation cache. In that case, we know that |i| is zero. Just
2016 * in case this is a re-export, we increment then decrement the
2017 * keymgmt reference counter.
2019 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
2020 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
2025 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2027 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
2028 && !evp_keymgmt_util_clear_operation_cache(pk
)) {
2029 CRYPTO_THREAD_unlock(pk
->lock
);
2030 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
2032 EVP_KEYMGMT_free(tmp_keymgmt
);
2035 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
2037 /* Check to make sure some other thread didn't get there first */
2038 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
, selection
);
2039 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
2040 void *tmp_keydata
= op
->keydata
;
2042 CRYPTO_THREAD_unlock(pk
->lock
);
2043 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
2044 keydata
= tmp_keydata
;
2048 /* Add the new export to the operation cache */
2049 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
,
2051 CRYPTO_THREAD_unlock(pk
->lock
);
2052 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
2057 /* Synchronize the dirty count */
2058 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
2060 CRYPTO_THREAD_unlock(pk
->lock
);
2063 #endif /* FIPS_MODULE */
2065 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
, selection
);
2069 * If nothing was exported, |tmp_keymgmt| might point at a freed
2070 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
2071 * the caller either way in that case.
2073 if (keydata
== NULL
)
2076 if (keymgmt
!= NULL
&& tmp_keymgmt
!= NULL
) {
2077 *keymgmt
= tmp_keymgmt
;
2078 allocated_keymgmt
= NULL
;
2081 EVP_KEYMGMT_free(allocated_keymgmt
);
2086 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
2088 EVP_PKEY
*allocpkey
= NULL
;
2090 if (!ossl_assert(dest
!= NULL
))
2093 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
2094 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
2095 void *keydata
= src
->keydata
;
2096 int type
= src
->type
;
2097 const char *keytype
= NULL
;
2099 keytype
= EVP_KEYMGMT_get0_name(keymgmt
);
2102 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2103 * else in our code. If it's not one of the well known EVP_PKEY_xxx
2104 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2105 * The check is kept as a safety measure.
2107 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
2108 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
2109 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2114 /* Prefer the legacy key type name for error reporting */
2115 if (type
!= EVP_PKEY_KEYMGMT
)
2116 keytype
= OBJ_nid2sn(type
);
2118 /* Make sure we have a clean slate to copy into */
2119 if (*dest
== NULL
) {
2120 allocpkey
= *dest
= EVP_PKEY_new();
2121 if (*dest
== NULL
) {
2122 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2126 evp_pkey_free_it(*dest
);
2129 if (EVP_PKEY_set_type(*dest
, type
)) {
2130 /* If the key is typed but empty, we're done */
2131 if (keydata
== NULL
)
2134 if ((*dest
)->ameth
->import_from
== NULL
) {
2135 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
2136 "key type = %s", keytype
);
2139 * We perform the export in the same libctx as the keymgmt
2140 * that we are using.
2142 OSSL_LIB_CTX
*libctx
=
2143 ossl_provider_libctx(keymgmt
->prov
);
2144 EVP_PKEY_CTX
*pctx
=
2145 EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
2148 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2151 && evp_keymgmt_export(keymgmt
, keydata
,
2152 OSSL_KEYMGMT_SELECT_ALL
,
2153 (*dest
)->ameth
->import_from
,
2155 /* Synchronize the dirty count */
2156 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
2158 EVP_PKEY_CTX_free(pctx
);
2161 EVP_PKEY_CTX_free(pctx
);
2164 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
2165 "key type = %s", keytype
);
2169 if (allocpkey
!= NULL
) {
2170 EVP_PKEY_free(allocpkey
);
2176 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
2178 EVP_PKEY
*tmp_copy
= NULL
;
2181 if (!ossl_assert(pk
!= NULL
))
2185 * If this isn't an assigned provider side key, we just use any existing
2186 * origin legacy key.
2188 if (!evp_pkey_is_assigned(pk
))
2190 if (!evp_pkey_is_provided(pk
))
2191 return pk
->pkey
.ptr
;
2193 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
2196 ret
= pk
->legacy_cache_pkey
.ptr
;
2198 if (!CRYPTO_THREAD_unlock(pk
->lock
))
2204 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
2207 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2210 /* Check again in case some other thread has updated it in the meantime */
2211 ret
= pk
->legacy_cache_pkey
.ptr
;
2213 /* Steal the legacy key reference from the temporary copy */
2214 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
2215 tmp_copy
->pkey
.ptr
= NULL
;
2218 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
2224 EVP_PKEY_free(tmp_copy
);
2228 #endif /* FIPS_MODULE */
2230 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
2234 OSSL_PARAM params
[2];
2235 unsigned char buffer
[2048];
2236 unsigned char *buf
= NULL
;
2239 if (key_name
== NULL
2243 memset(buffer
, 0, sizeof(buffer
));
2244 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
2245 params
[1] = OSSL_PARAM_construct_end();
2246 if (!EVP_PKEY_get_params(pkey
, params
)) {
2247 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
2249 buf_sz
= params
[0].return_size
;
2251 * If it failed because the buffer was too small then allocate the
2252 * required buffer size and retry.
2254 buf
= OPENSSL_zalloc(buf_sz
);
2257 params
[0].data
= buf
;
2258 params
[0].data_size
= buf_sz
;
2260 if (!EVP_PKEY_get_params(pkey
, params
))
2263 /* Fail if the param was not found */
2264 if (!OSSL_PARAM_modified(params
))
2266 ret
= OSSL_PARAM_get_BN(params
, bn
);
2269 if (OSSL_PARAM_modified(params
))
2270 OPENSSL_clear_free(buf
, buf_sz
);
2273 } else if (OSSL_PARAM_modified(params
)) {
2274 OPENSSL_cleanse(buffer
, params
[0].data_size
);
2279 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2280 unsigned char *buf
, size_t max_buf_sz
,
2283 OSSL_PARAM params
[2];
2284 int ret1
= 0, ret2
= 0;
2286 if (key_name
== NULL
)
2289 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2290 params
[1] = OSSL_PARAM_construct_end();
2291 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2292 ret2
= OSSL_PARAM_modified(params
);
2293 if (ret2
&& out_len
!= NULL
)
2294 *out_len
= params
[0].return_size
;
2295 return ret1
&& ret2
;
2298 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2299 char *str
, size_t max_buf_sz
,
2302 OSSL_PARAM params
[2];
2303 int ret1
= 0, ret2
= 0;
2305 if (key_name
== NULL
)
2308 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2309 params
[1] = OSSL_PARAM_construct_end();
2310 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2311 ret2
= OSSL_PARAM_modified(params
);
2312 if (ret2
&& out_len
!= NULL
)
2313 *out_len
= params
[0].return_size
;
2315 if (ret2
&& params
[0].return_size
== max_buf_sz
)
2316 /* There was no space for a NUL byte */
2318 /* Add a terminating NUL byte for good measure */
2319 if (ret2
&& str
!= NULL
)
2320 str
[params
[0].return_size
] = '\0';
2322 return ret1
&& ret2
;
2325 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2328 OSSL_PARAM params
[2];
2330 if (key_name
== NULL
)
2333 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2334 params
[1] = OSSL_PARAM_construct_end();
2335 return EVP_PKEY_get_params(pkey
, params
)
2336 && OSSL_PARAM_modified(params
);
2339 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2342 OSSL_PARAM params
[2];
2344 if (key_name
== NULL
)
2347 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2348 params
[1] = OSSL_PARAM_construct_end();
2349 return EVP_PKEY_get_params(pkey
, params
)
2350 && OSSL_PARAM_modified(params
);
2353 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2355 OSSL_PARAM params
[2];
2357 if (key_name
== NULL
)
2360 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2361 params
[1] = OSSL_PARAM_construct_end();
2362 return EVP_PKEY_set_params(pkey
, params
);
2365 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2367 OSSL_PARAM params
[2];
2369 if (key_name
== NULL
)
2372 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2373 params
[1] = OSSL_PARAM_construct_end();
2374 return EVP_PKEY_set_params(pkey
, params
);
2377 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2380 OSSL_PARAM params
[2];
2381 unsigned char buffer
[2048];
2384 if (key_name
== NULL
2387 || !evp_pkey_is_assigned(pkey
))
2390 bsize
= BN_num_bytes(bn
);
2391 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2394 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2396 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2397 params
[1] = OSSL_PARAM_construct_end();
2398 return EVP_PKEY_set_params(pkey
, params
);
2401 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2404 OSSL_PARAM params
[2];
2406 if (key_name
== NULL
)
2409 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2410 params
[1] = OSSL_PARAM_construct_end();
2411 return EVP_PKEY_set_params(pkey
, params
);
2414 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2415 const unsigned char *buf
, size_t bsize
)
2417 OSSL_PARAM params
[2];
2419 if (key_name
== NULL
)
2422 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2423 (unsigned char *)buf
, bsize
);
2424 params
[1] = OSSL_PARAM_construct_end();
2425 return EVP_PKEY_set_params(pkey
, params
);
2428 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2430 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2431 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2435 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2438 if (evp_pkey_is_provided(pkey
)) {
2440 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2444 * We will hopefully never find the need to set individual data in
2445 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2446 * sure. This bit of code can be enabled if we find the need. If
2447 * not, it can safely be removed when #legacy support is removed.
2450 else if (evp_pkey_is_legacy(pkey
)) {
2451 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2456 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2460 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2462 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2463 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2467 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2470 if (evp_pkey_is_provided(pkey
))
2471 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
) > 0;
2473 else if (evp_pkey_is_legacy(pkey
))
2474 return evp_pkey_get_params_to_ctrl(pkey
, params
) > 0;
2477 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2482 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2490 if (pkey
->keymgmt
== NULL
2491 || pkey
->keydata
== NULL
) {
2492 # ifndef OPENSSL_NO_EC
2493 /* Might work through the legacy route */
2494 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2499 return EC_KEY_get_conv_form(ec
);
2505 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2506 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2507 name
, sizeof(name
), &name_len
))
2510 if (strcmp(name
, "uncompressed") == 0)
2511 return POINT_CONVERSION_UNCOMPRESSED
;
2513 if (strcmp(name
, "compressed") == 0)
2514 return POINT_CONVERSION_COMPRESSED
;
2516 if (strcmp(name
, "hybrid") == 0)
2517 return POINT_CONVERSION_HYBRID
;
2522 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2530 if (pkey
->keymgmt
== NULL
2531 || pkey
->keydata
== NULL
) {
2532 # ifndef OPENSSL_NO_EC
2533 /* Might work through the legacy route */
2534 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2535 const EC_GROUP
*grp
;
2539 grp
= EC_KEY_get0_group(ec
);
2543 return EC_GROUP_get_field_type(grp
);
2549 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2550 fstr
, sizeof(fstr
), &fstrlen
))
2553 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2554 return NID_X9_62_prime_field
;
2555 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2556 return NID_X9_62_characteristic_two_field
;