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>
30 #include <openssl/params.h>
31 #include <openssl/param_build.h>
32 #include <openssl/encoder.h>
33 #include <openssl/core_names.h>
35 #include "internal/numbers.h" /* includes SIZE_MAX */
36 #include "internal/ffc.h"
37 #include "crypto/evp.h"
38 #include "crypto/dh.h"
39 #include "crypto/dsa.h"
40 #include "crypto/ec.h"
41 #include "crypto/ecx.h"
42 #include "crypto/rsa.h"
44 #include "crypto/asn1.h"
45 #include "crypto/x509.h"
47 #include "internal/provider.h"
48 #include "internal/common.h"
49 #include "evp_local.h"
51 static int pkey_set_type(EVP_PKEY
*pkey
, int type
, const char *str
,
52 int len
, EVP_KEYMGMT
*keymgmt
);
53 static void evp_pkey_free_it(EVP_PKEY
*key
);
55 /* The type of parameters selected in key parameter functions */
56 #define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
59 int EVP_PKEY_get_bits(const EVP_PKEY
*pkey
)
64 size
= pkey
->cache
.bits
;
65 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
66 size
= pkey
->ameth
->pkey_bits(pkey
);
69 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_BITS
);
75 int EVP_PKEY_get_security_bits(const EVP_PKEY
*pkey
)
80 size
= pkey
->cache
.security_bits
;
81 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
82 size
= pkey
->ameth
->pkey_security_bits(pkey
);
85 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_SECURITY_BITS
);
91 int EVP_PKEY_get_security_category(const EVP_PKEY
*pkey
)
93 return pkey
!= NULL
? pkey
->cache
.security_category
: -1;
96 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
98 #ifndef OPENSSL_NO_DSA
99 if (pkey
->type
== EVP_PKEY_DSA
) {
100 int ret
= pkey
->save_parameters
;
103 pkey
->save_parameters
= mode
;
107 #ifndef OPENSSL_NO_EC
108 if (pkey
->type
== EVP_PKEY_EC
) {
109 int ret
= pkey
->save_parameters
;
112 pkey
->save_parameters
= mode
;
119 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
121 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
124 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
126 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
128 #endif /* !FIPS_MODULE */
130 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
133 * Clean up legacy stuff from this function when legacy support is gone.
136 EVP_PKEY
*downgraded_from
= NULL
;
141 * If |to| is a legacy key and |from| isn't, we must make a downgraded
142 * copy of |from|. If that fails, this function fails.
144 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
145 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
147 from
= downgraded_from
;
149 #endif /* !FIPS_MODULE */
152 * Make sure |to| is typed. Content is less important at this early
155 * 1. If |to| is untyped, assign |from|'s key type to it.
156 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
157 * (|from| was already downgraded above)
159 * If |to| is a provided key, there's nothing more to do here, functions
160 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
161 * further down help us find out if they are the same or not.
163 if (evp_pkey_is_blank(to
)) {
165 if (evp_pkey_is_legacy(from
)) {
166 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
169 #endif /* !FIPS_MODULE */
171 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
176 else if (evp_pkey_is_legacy(to
)) {
177 if (to
->type
!= from
->type
) {
178 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
182 #endif /* !FIPS_MODULE */
184 if (EVP_PKEY_missing_parameters(from
)) {
185 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
189 if (!EVP_PKEY_missing_parameters(to
)) {
190 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
193 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
197 /* For purely provided keys, we just call the keymgmt utility */
198 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
199 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
205 * If |to| is provided, we know that |from| is legacy at this point.
206 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
207 * to copy the appropriate data to |to|'s keydata.
208 * We cannot override existing data so do it only if there is no keydata
211 if (to
->keymgmt
!= NULL
&& to
->keydata
== NULL
) {
212 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
213 void *from_keydata
= evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
217 * If we get a NULL, it could be an internal error, or it could be
218 * that there's a key mismatch. We're pretending the latter...
220 if (from_keydata
== NULL
)
221 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
223 ok
= (to
->keydata
= evp_keymgmt_dup(to
->keymgmt
,
230 /* Both keys are legacy */
231 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
232 ok
= from
->ameth
->param_copy(to
, from
);
233 #endif /* !FIPS_MODULE */
235 EVP_PKEY_free(downgraded_from
);
239 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
243 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
245 if (pkey
->keymgmt
!= NULL
)
246 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
247 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
248 return pkey
->ameth
->param_missing(pkey
);
249 #endif /* FIPS_MODULE */
255 * This function is called for any mixture of keys except pure legacy pair.
256 * When legacy keys are gone, we replace a call to this functions with
257 * a call to evp_keymgmt_util_match().
259 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
263 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
265 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
266 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
268 /* If none of them are provided, this function shouldn't have been called */
269 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
272 /* For purely provided keys, we just call the keymgmt utility */
273 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
274 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
277 * At this point, one of them is provided, the other not. This allows
278 * us to compare types using legacy NIDs.
280 if (evp_pkey_is_legacy(a
)
281 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
282 return -1; /* not the same key type */
283 if (evp_pkey_is_legacy(b
)
284 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
285 return -1; /* not the same key type */
288 * We've determined that they both are the same keytype, so the next
289 * step is to do a bit of cross export to ensure we have keydata for
290 * both keys in the same keymgmt.
292 keymgmt1
= a
->keymgmt
;
293 keydata1
= a
->keydata
;
294 keymgmt2
= b
->keymgmt
;
295 keydata2
= b
->keydata
;
297 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
298 tmp_keydata
= evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
299 if (tmp_keydata
!= NULL
) {
301 keydata1
= tmp_keydata
;
304 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
305 tmp_keydata
= evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
306 if (tmp_keydata
!= NULL
) {
308 keydata2
= tmp_keydata
;
312 /* If we still don't have matching keymgmt implementations, we give up */
313 if (keymgmt1
!= keymgmt2
)
316 /* If the keymgmt implementations are NULL, the export failed */
317 if (keymgmt1
== NULL
)
320 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
321 #endif /* FIPS_MODULE */
325 #ifndef OPENSSL_NO_DEPRECATED_3_0
326 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
328 return EVP_PKEY_parameters_eq(a
, b
);
331 #endif /* FIPS_MODULE */
333 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
336 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
339 * This will just call evp_keymgmt_util_match when legacy support
343 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
344 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
346 /* All legacy keys */
347 if (a
->type
!= b
->type
)
349 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
350 return a
->ameth
->param_cmp(a
, b
);
352 #endif /* !FIPS_MODULE */
356 #ifndef OPENSSL_NO_DEPRECATED_3_0
357 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
359 return EVP_PKEY_eq(a
, b
);
362 #endif /* !FIPS_MODULE */
364 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
367 * This will just call evp_keymgmt_util_match when legacy support
371 /* Trivial shortcuts */
374 if (a
== NULL
|| b
== NULL
)
378 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
379 #endif /* !FIPS_MODULE */
381 int selection
= SELECT_PARAMETERS
;
383 if (evp_keymgmt_util_has((EVP_PKEY
*)a
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
)
384 && evp_keymgmt_util_has((EVP_PKEY
*)b
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
))
385 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
387 selection
|= OSSL_KEYMGMT_SELECT_KEYPAIR
;
388 return evp_pkey_cmp_any(a
, b
, selection
);
392 /* All legacy keys */
393 if (a
->type
!= b
->type
)
396 if (a
->ameth
!= NULL
) {
398 /* Compare parameters if the algorithm has them */
399 if (a
->ameth
->param_cmp
!= NULL
) {
400 ret
= a
->ameth
->param_cmp(a
, b
);
405 if (a
->ameth
->pub_cmp
!= NULL
)
406 return a
->ameth
->pub_cmp(a
, b
);
410 #endif /* !FIPS_MODULE */
414 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
418 const unsigned char *key
,
422 EVP_PKEY
*pkey
= NULL
;
423 EVP_PKEY_CTX
*ctx
= NULL
;
426 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
427 strtype
!= NULL
? strtype
428 : OBJ_nid2sn(nidtype
),
432 /* May fail if no provider available */
434 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
435 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
437 ERR_clear_last_mark();
438 params
[0] = OSSL_PARAM_construct_octet_string(
439 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
440 : OSSL_PKEY_PARAM_PUB_KEY
,
443 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
444 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
448 EVP_PKEY_CTX_free(ctx
);
453 /* else not supported so fallback to legacy */
455 /* Legacy code path */
457 pkey
= EVP_PKEY_new();
459 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
463 if (!pkey_set_type(pkey
, nidtype
, strtype
, -1, NULL
)) {
464 /* ERR_raise(ERR_LIB_EVP, ...) already called */
468 if (!ossl_assert(pkey
->ameth
!= NULL
))
472 if (pkey
->ameth
->set_priv_key
== NULL
) {
473 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
477 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
478 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
482 if (pkey
->ameth
->set_pub_key
== NULL
) {
483 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
487 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
488 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
499 EVP_PKEY_CTX_free(ctx
);
503 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
506 const unsigned char *priv
, size_t len
)
508 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, priv
,
512 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
513 const unsigned char *priv
,
516 if (!ossl_assert(e
== NULL
))
518 return new_raw_key_int(NULL
, NULL
, NULL
, type
, priv
, len
, 1);
521 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
522 const char *keytype
, const char *propq
,
523 const unsigned char *pub
, size_t len
)
525 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, pub
,
529 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
530 const unsigned char *pub
,
533 if (!ossl_assert(e
== NULL
))
535 return new_raw_key_int(NULL
, NULL
, NULL
, type
, pub
, len
, 0);
538 struct raw_key_details_st
{
544 static OSSL_CALLBACK get_raw_key_details
;
545 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
547 const OSSL_PARAM
*p
= NULL
;
548 struct raw_key_details_st
*raw_key
= arg
;
550 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
551 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
553 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
554 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
556 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
557 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
559 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
560 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
567 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
570 if (pkey
->keymgmt
!= NULL
) {
571 struct raw_key_details_st raw_key
;
573 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
575 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
577 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
578 get_raw_key_details
, &raw_key
);
581 if (pkey
->ameth
== NULL
) {
582 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
586 if (pkey
->ameth
->get_priv_key
== NULL
) {
587 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
591 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
592 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
599 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
602 if (pkey
->keymgmt
!= NULL
) {
603 struct raw_key_details_st raw_key
;
605 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
607 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
609 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
610 get_raw_key_details
, &raw_key
);
613 if (pkey
->ameth
== NULL
) {
614 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
618 if (pkey
->ameth
->get_pub_key
== NULL
) {
619 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
623 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
624 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
631 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
632 const char *cipher_name
,
633 const EVP_CIPHER
*cipher
,
634 OSSL_LIB_CTX
*libctx
,
637 #ifndef OPENSSL_NO_CMAC
638 OSSL_PARAM params
[5], *p
= params
;
639 EVP_PKEY
*pkey
= NULL
;
643 cipher_name
= EVP_CIPHER_get0_name(cipher
);
645 if (cipher_name
== NULL
) {
646 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
650 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
654 if (EVP_PKEY_fromdata_init(ctx
) <= 0) {
655 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
659 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
661 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
662 (char *)cipher_name
, 0);
664 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
666 *p
= OSSL_PARAM_construct_end();
668 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) <= 0) {
669 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
674 EVP_PKEY_CTX_free(ctx
);
678 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
683 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
684 size_t len
, const EVP_CIPHER
*cipher
)
686 if (!ossl_assert(e
== NULL
))
688 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
);
691 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
693 return pkey_set_type(pkey
, type
, NULL
, -1, NULL
);
696 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
698 return pkey_set_type(pkey
, EVP_PKEY_NONE
, str
, len
, NULL
);
701 #ifndef OPENSSL_NO_DEPRECATED_3_0
702 static void detect_foreign_key(EVP_PKEY
*pkey
)
704 switch (pkey
->type
) {
706 case EVP_PKEY_RSA_PSS
:
707 pkey
->foreign
= pkey
->pkey
.rsa
!= NULL
708 && ossl_rsa_is_foreign(pkey
->pkey
.rsa
);
710 #ifndef OPENSSL_NO_EC
714 pkey
->foreign
= pkey
->pkey
.ec
!= NULL
715 && ossl_ec_key_is_foreign(pkey
->pkey
.ec
);
718 #ifndef OPENSSL_NO_DSA
720 pkey
->foreign
= pkey
->pkey
.dsa
!= NULL
721 && ossl_dsa_is_foreign(pkey
->pkey
.dsa
);
724 #ifndef OPENSSL_NO_DH
726 pkey
->foreign
= pkey
->pkey
.dh
!= NULL
727 && ossl_dh_is_foreign(pkey
->pkey
.dh
);
736 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
738 #ifndef OPENSSL_NO_EC
741 pktype
= EVP_PKEY_type(type
);
742 if ((key
!= NULL
) && (pktype
== EVP_PKEY_EC
|| pktype
== EVP_PKEY_SM2
)) {
743 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
746 int curve
= EC_GROUP_get_curve_name(group
);
749 * Regardless of what is requested the SM2 curve must be SM2 type,
750 * and non SM2 curves are EC type.
752 if (curve
== NID_sm2
&& pktype
== EVP_PKEY_EC
)
754 else if (curve
!= NID_sm2
&& pktype
== EVP_PKEY_SM2
)
760 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
763 pkey
->pkey
.ptr
= key
;
764 detect_foreign_key(pkey
);
766 return (key
!= NULL
);
770 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
775 if (!evp_pkey_is_provided(pkey
))
776 return pkey
->pkey
.ptr
;
781 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
783 const ASN1_OCTET_STRING
*os
= NULL
;
784 if (pkey
->type
!= EVP_PKEY_HMAC
) {
785 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
788 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
796 #ifndef OPENSSL_NO_POLY1305
797 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
799 const ASN1_OCTET_STRING
*os
= NULL
;
800 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
801 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
804 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
813 #ifndef OPENSSL_NO_SIPHASH
814 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
816 const ASN1_OCTET_STRING
*os
= NULL
;
818 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
819 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
822 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
831 #ifndef OPENSSL_NO_DSA
832 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
834 if (pkey
->type
!= EVP_PKEY_DSA
) {
835 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
838 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
841 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
843 return evp_pkey_get0_DSA_int(pkey
);
846 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
850 if (!DSA_up_ref(key
))
853 ret
= EVP_PKEY_assign_DSA(pkey
, key
);
860 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
862 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
864 if (ret
!= NULL
&& !DSA_up_ref(ret
))
869 #endif /* OPENSSL_NO_DSA */
871 #ifndef OPENSSL_NO_ECX
872 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
874 if (EVP_PKEY_get_base_id(pkey
) != type
) {
875 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
878 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
881 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
883 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
885 if (ret
!= NULL
&& !ossl_ecx_key_up_ref(ret
))
890 #define IMPLEMENT_ECX_VARIANT(NAME) \
891 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
893 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
895 IMPLEMENT_ECX_VARIANT(X25519
)
896 IMPLEMENT_ECX_VARIANT(X448
)
897 IMPLEMENT_ECX_VARIANT(ED25519
)
898 IMPLEMENT_ECX_VARIANT(ED448
)
900 #endif /* OPENSSL_NO_ECX */
902 #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
904 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*dhkey
)
909 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
910 * related to ffdhe and modp (which cache q = (p - 1) / 2),
911 * and returns 0 for all other dh parameter generation types including
912 * RFC5114 named groups.
914 * The EVP_PKEY_DH type is used for dh parameter generation types:
915 * - named safe prime groups related to ffdhe and modp
916 * - safe prime generator
918 * The type EVP_PKEY_DHX is used for dh parameter generation types
919 * - fips186-4 and fips186-2
920 * - rfc5114 named groups.
922 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
924 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
925 * q value to be stored.
927 if (ossl_dh_is_named_safe_prime_group(dhkey
))
930 type
= DH_get0_q(dhkey
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
932 if (!DH_up_ref(dhkey
))
935 ret
= EVP_PKEY_assign(pkey
, type
, dhkey
);
943 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
945 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
946 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
949 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
952 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
954 return evp_pkey_get0_DH_int(pkey
);
957 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
959 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
961 if (ret
!= NULL
&& !DH_up_ref(ret
))
968 int EVP_PKEY_get_id(const EVP_PKEY
*pkey
)
973 int EVP_PKEY_get_base_id(const EVP_PKEY
*pkey
)
975 return EVP_PKEY_type(pkey
->type
);
979 * These hard coded cases are pure hackery to get around the fact
980 * that names in crypto/objects/objects.txt are a mess. There is
981 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
982 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
983 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
984 * "DSA" is accurate... but still, better be safe and hard-code
985 * names that we know.
986 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
987 * EVP_PKEY_EC, because of aliasing.
988 * This should be cleaned away along with all other #legacy support.
990 static const OSSL_ITEM standard_name2type
[] = {
991 { EVP_PKEY_RSA
, "RSA" },
992 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
993 { EVP_PKEY_EC
, "EC" },
994 { EVP_PKEY_ED25519
, "ED25519" },
995 { EVP_PKEY_ED448
, "ED448" },
996 { EVP_PKEY_X25519
, "X25519" },
997 { EVP_PKEY_X448
, "X448" },
998 { EVP_PKEY_SM2
, "SM2" },
999 { EVP_PKEY_DH
, "DH" },
1000 { EVP_PKEY_DHX
, "X9.42 DH" },
1001 { EVP_PKEY_DHX
, "DHX" },
1002 { EVP_PKEY_DSA
, "DSA" },
1005 int evp_pkey_name2type(const char *name
)
1010 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1011 if (OPENSSL_strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
1012 return (int)standard_name2type
[i
].id
;
1015 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
1017 return EVP_PKEY_type(OBJ_ln2nid(name
));
1020 const char *evp_pkey_type2name(int type
)
1024 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1025 if (type
== (int)standard_name2type
[i
].id
)
1026 return standard_name2type
[i
].ptr
;
1029 return OBJ_nid2sn(type
);
1032 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
1036 if (pkey
->keymgmt
== NULL
)
1037 return pkey
->type
== evp_pkey_name2type(name
);
1038 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
1041 int EVP_PKEY_type_names_do_all(const EVP_PKEY
*pkey
,
1042 void (*fn
)(const char *name
, void *data
),
1045 if (!evp_pkey_is_typed(pkey
))
1048 if (!evp_pkey_is_provided(pkey
)) {
1049 const char *name
= OBJ_nid2sn(EVP_PKEY_get_id(pkey
));
1054 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1057 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1059 if (pkey
->keymgmt
== NULL
) {
1060 switch (EVP_PKEY_get_base_id(pkey
)) {
1062 case EVP_PKEY_RSA_PSS
:
1064 #ifndef OPENSSL_NO_DSA
1068 #ifndef OPENSSL_NO_EC
1069 case EVP_PKEY_ED25519
:
1070 case EVP_PKEY_ED448
:
1072 case EVP_PKEY_EC
: /* Including SM2 */
1073 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1079 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_get0_provider(pkey
->keymgmt
);
1080 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1084 name
= evp_keymgmt_util_query_operation_name(pkey
->keymgmt
,
1086 sig
= EVP_SIGNATURE_fetch(libctx
, name
, NULL
);
1088 EVP_SIGNATURE_free(sig
);
1095 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1097 BIO_set_indent(*out
, saved_indent
);
1099 BIO
*next
= BIO_pop(*out
);
1107 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1113 long i
= BIO_get_indent(*out
);
1115 *saved_indent
= (i
< 0 ? 0 : i
);
1116 if (BIO_set_indent(*out
, indent
) <= 0) {
1117 BIO
*prefbio
= BIO_new(BIO_f_prefix());
1119 if (prefbio
== NULL
)
1121 *out
= BIO_push(prefbio
, *out
);
1124 if (BIO_set_indent(*out
, indent
) <= 0) {
1125 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1132 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1135 return BIO_indent(out
, indent
, 128)
1136 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1137 kstr
, OBJ_nid2ln(pkey
->type
))
1141 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1142 int selection
/* For provided encoding */,
1143 const char *propquery
/* For provided encoding */,
1144 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1145 int indent
, ASN1_PCTX
*pctx
),
1146 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1150 OSSL_ENCODER_CTX
*ctx
= NULL
;
1151 int ret
= -2; /* default to unsupported */
1153 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1156 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1158 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1159 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1160 OSSL_ENCODER_CTX_free(ctx
);
1165 /* legacy fallback */
1166 if (legacy_print
!= NULL
)
1167 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1169 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1172 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1176 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1177 int indent
, ASN1_PCTX
*pctx
)
1179 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1180 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1184 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1185 int indent
, ASN1_PCTX
*pctx
)
1187 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PRIVATE_KEY
, NULL
,
1188 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1192 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1193 int indent
, ASN1_PCTX
*pctx
)
1195 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1196 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1200 #ifndef OPENSSL_NO_STDIO
1201 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1202 int indent
, ASN1_PCTX
*pctx
)
1205 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1209 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1214 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1215 int indent
, ASN1_PCTX
*pctx
)
1218 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1222 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1227 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1228 int indent
, ASN1_PCTX
*pctx
)
1231 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1235 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1241 static void mdname2nid(const char *mdname
, void *data
)
1243 int *nid
= (int *)data
;
1245 if (*nid
!= NID_undef
)
1248 *nid
= OBJ_sn2nid(mdname
);
1249 if (*nid
== NID_undef
)
1250 *nid
= OBJ_ln2nid(mdname
);
1253 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1254 int arg1
, void *arg2
)
1256 if (pkey
->keymgmt
== NULL
)
1259 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
: {
1260 char mdname
[80] = "";
1261 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1266 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1267 /* Make sure the MD is in the namemap if available */
1269 OSSL_NAMEMAP
*namemap
;
1270 int nid
= NID_undef
;
1272 (void)ERR_set_mark();
1273 md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1274 (void)ERR_pop_to_mark();
1275 namemap
= ossl_namemap_stored(libctx
);
1278 * The only reason to fetch the MD was to make sure it is in the
1279 * namemap. We can immediately free it.
1282 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1287 * We have the namemap number - now we need to find the
1290 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1301 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1303 if (pkey
->ameth
== NULL
)
1304 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1305 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1307 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1310 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1314 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1317 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1318 char *mdname
, size_t mdname_sz
)
1320 if (pkey
->ameth
== NULL
)
1321 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1326 int nid
= NID_undef
;
1327 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1328 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1331 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1336 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1339 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1340 gname
, gname_sz
, gname_len
);
1343 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY
*pkey
, OSSL_LIB_CTX
*libctx
,
1344 const char *name
, const char *propq
)
1347 EVP_MD_CTX
*ctx
= NULL
;
1349 if ((ctx
= EVP_MD_CTX_new()) == NULL
)
1353 rv
= EVP_DigestSignInit_ex(ctx
, NULL
, name
, libctx
,
1357 EVP_MD_CTX_free(ctx
);
1360 #endif /* !FIPS_MODULE */
1362 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1368 if (evp_pkey_is_provided(pkey
))
1369 #endif /* !FIPS_MODULE */
1370 return EVP_PKEY_set_octet_string_param(pkey
,
1371 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1372 (unsigned char *)pub
, publen
);
1375 if (publen
> INT_MAX
)
1377 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1378 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, (int)publen
,
1383 #endif /* !FIPS_MODULE */
1386 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1391 if (evp_pkey_is_provided(pkey
))
1394 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1398 * We know that this is going to fail, but it will give us a size
1401 EVP_PKEY_get_octet_string_param(pkey
,
1402 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1403 NULL
, 0, &return_size
);
1404 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1408 buf
= OPENSSL_malloc(return_size
);
1412 if (!EVP_PKEY_get_octet_string_param(pkey
,
1413 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1414 buf
, return_size
, NULL
)) {
1424 int rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1429 #endif /* !FIPS_MODULE */
1432 /*- All methods below can also be used in FIPS_MODULE */
1434 EVP_PKEY
*EVP_PKEY_new(void)
1436 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1441 ret
->type
= EVP_PKEY_NONE
;
1442 ret
->save_type
= EVP_PKEY_NONE
;
1444 if (!CRYPTO_NEW_REF(&ret
->references
, 1))
1447 ret
->lock
= CRYPTO_THREAD_lock_new();
1448 if (ret
->lock
== NULL
) {
1449 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1454 ret
->save_parameters
= 1;
1455 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1456 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1463 CRYPTO_FREE_REF(&ret
->references
);
1464 CRYPTO_THREAD_lock_free(ret
->lock
);
1470 * Setup a public key management method.
1472 * For legacy keys, either |type| or |str| is expected to have the type
1473 * information. In this case, the setup consists of finding an ASN1 method
1474 * and setting those fields in |pkey|.
1476 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1477 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1479 * If pkey is NULL just return 1 or 0 if the key management method exists.
1482 static int pkey_set_type(EVP_PKEY
*pkey
, int type
, const char *str
,
1483 int len
, EVP_KEYMGMT
*keymgmt
)
1486 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1490 * The setups can't set both legacy and provider side methods.
1493 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)) {
1494 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1502 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1504 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1506 evp_pkey_free_it(pkey
);
1509 * If key type matches and a method exists then this lookup has
1510 * succeeded once so just indicate success.
1512 if (pkey
->type
!= EVP_PKEY_NONE
1513 && type
== pkey
->save_type
1514 && pkey
->ameth
!= NULL
)
1520 ameth
= EVP_PKEY_asn1_find_str(NULL
, str
, len
);
1521 else if (type
!= EVP_PKEY_NONE
)
1522 ameth
= EVP_PKEY_asn1_find(NULL
, type
);
1529 check
= check
&& ameth
== NULL
;
1531 check
= check
&& keymgmt
== NULL
;
1533 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1538 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1539 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1543 pkey
->keymgmt
= keymgmt
;
1545 pkey
->save_type
= type
;
1550 * If the internal "origin" key is provider side, don't save |ameth|.
1551 * The main reason is that |ameth| is one factor to detect that the
1552 * internal "origin" key is a legacy one.
1554 if (keymgmt
== NULL
)
1555 pkey
->ameth
= ameth
;
1558 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1559 * for any key type that has a legacy implementation, regardless of
1560 * if the internal key is a legacy or a provider side one. When
1561 * there is no legacy implementation for the key, the type becomes
1562 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1563 * with functions that expect legacy internal keys.
1565 if (ameth
!= NULL
) {
1566 if (type
== EVP_PKEY_NONE
)
1567 pkey
->type
= ameth
->pkey_id
;
1569 pkey
->type
= EVP_PKEY_KEYMGMT
;
1577 static void find_ameth(const char *name
, void *data
)
1579 const char **str
= data
;
1582 * The error messages from pkey_set_type() are uninteresting here,
1587 if (pkey_set_type(NULL
, EVP_PKEY_NONE
, name
, (int)strlen(name
),
1591 else if (str
[1] == NULL
)
1599 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1602 #define EVP_PKEY_TYPE_STR str[0]
1603 #define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1605 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1606 * Ideally, only one should be found. If two (or more) are found, the
1607 * match is ambiguous. This should never happen, but...
1609 const char *str
[2] = { NULL
, NULL
};
1611 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1612 || str
[1] != NULL
) {
1613 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1617 #define EVP_PKEY_TYPE_STR NULL
1618 #define EVP_PKEY_TYPE_STRLEN -1
1620 return pkey_set_type(pkey
, EVP_PKEY_NONE
,
1621 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1624 #undef EVP_PKEY_TYPE_STR
1625 #undef EVP_PKEY_TYPE_STRLEN
1628 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1632 if (CRYPTO_UP_REF(&pkey
->references
, &i
) <= 0)
1635 REF_PRINT_COUNT("EVP_PKEY", i
, pkey
);
1636 REF_ASSERT_ISNT(i
< 2);
1637 return ((i
> 1) ? 1 : 0);
1640 EVP_PKEY
*EVP_PKEY_dup(EVP_PKEY
*pkey
)
1645 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
1649 if ((dup_pk
= EVP_PKEY_new()) == NULL
)
1652 if (evp_pkey_is_blank(pkey
))
1656 if (evp_pkey_is_provided(pkey
))
1657 #endif /* !FIPS_MODULE */
1659 if (!evp_keymgmt_util_copy(dup_pk
, pkey
,
1660 OSSL_KEYMGMT_SELECT_ALL
))
1666 if (evp_pkey_is_legacy(pkey
)) {
1667 const EVP_PKEY_ASN1_METHOD
*ameth
= pkey
->ameth
;
1669 if (ameth
== NULL
|| ameth
->copy
== NULL
) {
1670 if (pkey
->pkey
.ptr
== NULL
/* empty key, just set type */
1671 && EVP_PKEY_set_type(dup_pk
, pkey
->type
) != 0)
1673 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_KEY_TYPE
);
1676 if (!ameth
->copy(dup_pk
, pkey
))
1680 #endif /* !FIPS_MODULE */
1685 /* copy auxiliary data */
1686 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
,
1687 &dup_pk
->ex_data
, &pkey
->ex_data
))
1690 if (pkey
->attributes
!= NULL
) {
1691 if ((dup_pk
->attributes
= ossl_x509at_dup(pkey
->attributes
)) == NULL
)
1694 #endif /* !FIPS_MODULE */
1697 EVP_PKEY_free(dup_pk
);
1702 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1704 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1706 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1707 ameth
= EVP_PKEY_asn1_find(NULL
, x
->type
);
1709 if (ameth
!= NULL
) {
1710 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1712 * We should never have both a legacy origin key, and a key in the
1715 assert(x
->pkey
.ptr
== NULL
);
1717 * For the purposes of freeing we make the legacy cache look like
1718 * a legacy origin key.
1720 x
->pkey
= x
->legacy_cache_pkey
;
1721 x
->legacy_cache_pkey
.ptr
= NULL
;
1723 if (ameth
->pkey_free
!= NULL
)
1724 ameth
->pkey_free(x
);
1728 #endif /* FIPS_MODULE */
1730 static void evp_pkey_free_it(EVP_PKEY
*x
)
1732 /* internal function; x is never NULL */
1733 evp_keymgmt_util_clear_operation_cache(x
);
1735 evp_pkey_free_legacy(x
);
1738 if (x
->keymgmt
!= NULL
) {
1739 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1740 EVP_KEYMGMT_free(x
->keymgmt
);
1744 x
->type
= EVP_PKEY_NONE
;
1747 void EVP_PKEY_free(EVP_PKEY
*x
)
1754 CRYPTO_DOWN_REF(&x
->references
, &i
);
1755 REF_PRINT_COUNT("EVP_PKEY", i
, x
);
1758 REF_ASSERT_ISNT(i
< 0);
1759 evp_pkey_free_it(x
);
1761 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1763 CRYPTO_THREAD_lock_free(x
->lock
);
1764 CRYPTO_FREE_REF(&x
->references
);
1766 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1771 int EVP_PKEY_get_size(const EVP_PKEY
*pkey
)
1776 size
= pkey
->cache
.size
;
1778 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1779 size
= pkey
->ameth
->pkey_size(pkey
);
1783 ERR_raise(ERR_LIB_EVP
, EVP_R_UNKNOWN_MAX_SIZE
);
1789 const char *EVP_PKEY_get0_description(const EVP_PKEY
*pkey
)
1791 if (!evp_pkey_is_assigned(pkey
))
1794 if (evp_pkey_is_provided(pkey
) && pkey
->keymgmt
->description
!= NULL
)
1795 return pkey
->keymgmt
->description
;
1797 if (pkey
->ameth
!= NULL
)
1798 return pkey
->ameth
->info
;
1803 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1804 EVP_KEYMGMT
**keymgmt
,
1805 const char *propquery
)
1807 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1808 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1809 int selection
= OSSL_KEYMGMT_SELECT_ALL
;
1810 void *keydata
= NULL
;
1816 /* No key data => nothing to export */
1819 check
= check
&& pk
->pkey
.ptr
== NULL
;
1821 check
= check
&& pk
->keydata
== NULL
;
1826 if (pk
->pkey
.ptr
!= NULL
) {
1828 * If the legacy key doesn't have an dirty counter or export function,
1831 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1836 if (keymgmt
!= NULL
) {
1837 tmp_keymgmt
= *keymgmt
;
1842 * If no keymgmt was given or found, get a default keymgmt. We do so by
1843 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1845 if (tmp_keymgmt
== NULL
) {
1846 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1850 allocated_keymgmt
= tmp_keymgmt
= ctx
->keymgmt
;
1851 ctx
->keymgmt
= NULL
;
1852 EVP_PKEY_CTX_free(ctx
);
1855 /* If there's still no keymgmt to be had, give up */
1856 if (tmp_keymgmt
== NULL
)
1860 if (pk
->pkey
.ptr
!= NULL
) {
1864 * If the legacy "origin" hasn't changed since last time, we try
1865 * to find our keymgmt in the operation cache. If it has changed,
1866 * |i| remains zero, and we will clear the cache further down.
1868 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1869 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1871 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
,
1875 * If |tmp_keymgmt| is present in the operation cache, it means
1876 * that export doesn't need to be redone. In that case, we take
1877 * token copies of the cached pointers, to have token success
1878 * values to return. It is possible (e.g. in a no-cached-fetch
1879 * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1880 * even though the name/provider must be the same. In other words
1881 * the keymgmt instance may be different but still equivalent, i.e.
1882 * same algorithm/provider instance - but we make the simplifying
1883 * assumption that the keydata can be used with either keymgmt
1884 * instance. Not doing so introduces significant complexity and
1885 * probably requires refactoring - since we would have to ripple
1886 * the change in keymgmt instance up the call chain.
1888 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1889 keydata
= op
->keydata
;
1890 CRYPTO_THREAD_unlock(pk
->lock
);
1893 CRYPTO_THREAD_unlock(pk
->lock
);
1896 /* Make sure that the keymgmt key type matches the legacy NID */
1897 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
1900 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1903 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
->import,
1904 libctx
, propquery
)) {
1905 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1911 * If the dirty counter changed since last time, then clear the
1912 * operation cache. In that case, we know that |i| is zero. Just
1913 * in case this is a re-export, we increment then decrement the
1914 * keymgmt reference counter.
1916 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1917 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1922 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1924 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
1925 && !evp_keymgmt_util_clear_operation_cache(pk
)) {
1926 CRYPTO_THREAD_unlock(pk
->lock
);
1927 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1929 EVP_KEYMGMT_free(tmp_keymgmt
);
1932 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1934 /* Check to make sure some other thread didn't get there first */
1935 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
, selection
);
1936 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1937 void *tmp_keydata
= op
->keydata
;
1939 CRYPTO_THREAD_unlock(pk
->lock
);
1940 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1941 keydata
= tmp_keydata
;
1945 /* Add the new export to the operation cache */
1946 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
,
1948 CRYPTO_THREAD_unlock(pk
->lock
);
1949 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1954 /* Synchronize the dirty count */
1955 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1957 CRYPTO_THREAD_unlock(pk
->lock
);
1960 #endif /* FIPS_MODULE */
1962 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
, selection
);
1966 * If nothing was exported, |tmp_keymgmt| might point at a freed
1967 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1968 * the caller either way in that case.
1970 if (keydata
== NULL
)
1973 if (keymgmt
!= NULL
&& tmp_keymgmt
!= NULL
) {
1974 *keymgmt
= tmp_keymgmt
;
1975 allocated_keymgmt
= NULL
;
1978 EVP_KEYMGMT_free(allocated_keymgmt
);
1983 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
1985 EVP_PKEY
*allocpkey
= NULL
;
1987 if (!ossl_assert(dest
!= NULL
))
1990 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
1991 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
1992 void *keydata
= src
->keydata
;
1993 int type
= src
->type
;
1994 const char *keytype
= NULL
;
1996 keytype
= EVP_KEYMGMT_get0_name(keymgmt
);
1999 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2000 * else in our code. If it's not one of the well known EVP_PKEY_xxx
2001 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2002 * The check is kept as a safety measure.
2004 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
2005 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
2006 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2011 /* Prefer the legacy key type name for error reporting */
2012 if (type
!= EVP_PKEY_KEYMGMT
)
2013 keytype
= OBJ_nid2sn(type
);
2015 /* Make sure we have a clean slate to copy into */
2016 if (*dest
== NULL
) {
2017 allocpkey
= *dest
= EVP_PKEY_new();
2018 if (*dest
== NULL
) {
2019 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2023 evp_pkey_free_it(*dest
);
2026 if (EVP_PKEY_set_type(*dest
, type
)) {
2027 /* If the key is typed but empty, we're done */
2028 if (keydata
== NULL
)
2031 if ((*dest
)->ameth
->import_from
== NULL
) {
2032 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
2033 "key type = %s", keytype
);
2036 * We perform the export in the same libctx as the keymgmt
2037 * that we are using.
2039 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(keymgmt
->prov
);
2040 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
2043 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2046 && evp_keymgmt_export(keymgmt
, keydata
,
2047 OSSL_KEYMGMT_SELECT_ALL
,
2048 (*dest
)->ameth
->import_from
,
2050 /* Synchronize the dirty count */
2051 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
2053 EVP_PKEY_CTX_free(pctx
);
2056 EVP_PKEY_CTX_free(pctx
);
2059 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
2060 "key type = %s", keytype
);
2064 if (allocpkey
!= NULL
) {
2065 EVP_PKEY_free(allocpkey
);
2071 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
2073 EVP_PKEY
*tmp_copy
= NULL
;
2076 if (!ossl_assert(pk
!= NULL
))
2080 * If this isn't an assigned provider side key, we just use any existing
2081 * origin legacy key.
2083 if (!evp_pkey_is_assigned(pk
))
2085 if (!evp_pkey_is_provided(pk
))
2086 return pk
->pkey
.ptr
;
2088 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
2091 ret
= pk
->legacy_cache_pkey
.ptr
;
2093 if (!CRYPTO_THREAD_unlock(pk
->lock
))
2099 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
2102 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2105 /* Check again in case some other thread has updated it in the meantime */
2106 ret
= pk
->legacy_cache_pkey
.ptr
;
2108 /* Steal the legacy key reference from the temporary copy */
2109 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
2110 tmp_copy
->pkey
.ptr
= NULL
;
2113 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
2119 EVP_PKEY_free(tmp_copy
);
2123 #endif /* FIPS_MODULE */
2125 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
2129 OSSL_PARAM params
[2];
2130 unsigned char buffer
[2048];
2131 unsigned char *buf
= NULL
;
2134 if (key_name
== NULL
2138 memset(buffer
, 0, sizeof(buffer
));
2139 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
2140 params
[1] = OSSL_PARAM_construct_end();
2141 if (!EVP_PKEY_get_params(pkey
, params
)) {
2142 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
2144 buf_sz
= params
[0].return_size
;
2146 * If it failed because the buffer was too small then allocate the
2147 * required buffer size and retry.
2149 buf
= OPENSSL_zalloc(buf_sz
);
2152 params
[0].data
= buf
;
2153 params
[0].data_size
= buf_sz
;
2155 if (!EVP_PKEY_get_params(pkey
, params
))
2158 /* Fail if the param was not found */
2159 if (!OSSL_PARAM_modified(params
))
2161 ret
= OSSL_PARAM_get_BN(params
, bn
);
2164 if (OSSL_PARAM_modified(params
))
2165 OPENSSL_clear_free(buf
, buf_sz
);
2168 } else if (OSSL_PARAM_modified(params
)) {
2169 OPENSSL_cleanse(buffer
, params
[0].data_size
);
2174 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2175 unsigned char *buf
, size_t max_buf_sz
,
2178 OSSL_PARAM params
[2];
2179 int ret1
= 0, ret2
= 0;
2181 if (key_name
== NULL
)
2184 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2185 params
[1] = OSSL_PARAM_construct_end();
2186 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2187 ret2
= OSSL_PARAM_modified(params
);
2188 if (ret2
&& out_len
!= NULL
)
2189 *out_len
= params
[0].return_size
;
2190 return ret1
&& ret2
;
2193 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2194 char *str
, size_t max_buf_sz
,
2197 OSSL_PARAM params
[2];
2198 int ret1
= 0, ret2
= 0;
2200 if (key_name
== NULL
)
2203 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2204 params
[1] = OSSL_PARAM_construct_end();
2205 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2206 ret2
= OSSL_PARAM_modified(params
);
2207 if (ret2
&& out_len
!= NULL
)
2208 *out_len
= params
[0].return_size
;
2210 if (ret2
&& params
[0].return_size
== max_buf_sz
)
2211 /* There was no space for a NUL byte */
2213 /* Add a terminating NUL byte for good measure */
2214 if (ret2
&& str
!= NULL
)
2215 str
[params
[0].return_size
] = '\0';
2217 return ret1
&& ret2
;
2220 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2223 OSSL_PARAM params
[2];
2225 if (key_name
== NULL
)
2228 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2229 params
[1] = OSSL_PARAM_construct_end();
2230 return EVP_PKEY_get_params(pkey
, params
)
2231 && OSSL_PARAM_modified(params
);
2234 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2237 OSSL_PARAM params
[2];
2239 if (key_name
== NULL
)
2242 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2243 params
[1] = OSSL_PARAM_construct_end();
2244 return EVP_PKEY_get_params(pkey
, params
)
2245 && OSSL_PARAM_modified(params
);
2248 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2250 OSSL_PARAM params
[2];
2252 if (key_name
== NULL
)
2255 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2256 params
[1] = OSSL_PARAM_construct_end();
2257 return EVP_PKEY_set_params(pkey
, params
);
2260 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2262 OSSL_PARAM params
[2];
2264 if (key_name
== NULL
)
2267 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2268 params
[1] = OSSL_PARAM_construct_end();
2269 return EVP_PKEY_set_params(pkey
, params
);
2272 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2275 OSSL_PARAM params
[2];
2276 unsigned char buffer
[2048];
2279 if (key_name
== NULL
2282 || !evp_pkey_is_assigned(pkey
))
2285 bsize
= BN_num_bytes(bn
);
2286 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2289 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2291 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2292 params
[1] = OSSL_PARAM_construct_end();
2293 return EVP_PKEY_set_params(pkey
, params
);
2296 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2299 OSSL_PARAM params
[2];
2301 if (key_name
== NULL
)
2304 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2305 params
[1] = OSSL_PARAM_construct_end();
2306 return EVP_PKEY_set_params(pkey
, params
);
2309 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2310 const unsigned char *buf
, size_t bsize
)
2312 OSSL_PARAM params
[2];
2314 if (key_name
== NULL
)
2317 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2318 (unsigned char *)buf
, bsize
);
2319 params
[1] = OSSL_PARAM_construct_end();
2320 return EVP_PKEY_set_params(pkey
, params
);
2323 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2325 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2326 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2330 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2333 if (evp_pkey_is_provided(pkey
)) {
2335 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2339 * We will hopefully never find the need to set individual data in
2340 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2341 * sure. This bit of code can be enabled if we find the need. If
2342 * not, it can safely be removed when #legacy support is removed.
2345 else if (evp_pkey_is_legacy(pkey
)) {
2346 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2351 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2355 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2357 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2358 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2362 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2365 if (evp_pkey_is_provided(pkey
))
2366 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
) > 0;
2368 else if (evp_pkey_is_legacy(pkey
))
2369 return evp_pkey_get_params_to_ctrl(pkey
, params
) > 0;
2372 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2377 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2385 if (pkey
->keymgmt
== NULL
2386 || pkey
->keydata
== NULL
) {
2387 #ifndef OPENSSL_NO_EC
2388 /* Might work through the legacy route */
2389 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2394 return EC_KEY_get_conv_form(ec
);
2400 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2401 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2402 name
, sizeof(name
), &name_len
))
2405 if (strcmp(name
, "uncompressed") == 0)
2406 return POINT_CONVERSION_UNCOMPRESSED
;
2408 if (strcmp(name
, "compressed") == 0)
2409 return POINT_CONVERSION_COMPRESSED
;
2411 if (strcmp(name
, "hybrid") == 0)
2412 return POINT_CONVERSION_HYBRID
;
2417 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2425 if (pkey
->keymgmt
== NULL
2426 || pkey
->keydata
== NULL
) {
2427 #ifndef OPENSSL_NO_EC
2428 /* Might work through the legacy route */
2429 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2430 const EC_GROUP
*grp
;
2434 grp
= EC_KEY_get0_group(ec
);
2438 return EC_GROUP_get_field_type(grp
);
2444 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2445 fstr
, sizeof(fstr
), &fstrlen
))
2448 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2449 return NID_X9_62_prime_field
;
2450 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2451 return NID_X9_62_characteristic_two_field
;