2 * Copyright 1995-2021 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 #include "e_os.h" /* strcasecmp on Windows */
55 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
56 int len
, EVP_KEYMGMT
*keymgmt
);
57 static void evp_pkey_free_it(EVP_PKEY
*key
);
61 /* The type of parameters selected in key parameter functions */
62 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
64 int EVP_PKEY_get_bits(const EVP_PKEY
*pkey
)
69 size
= pkey
->cache
.bits
;
70 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
71 size
= pkey
->ameth
->pkey_bits(pkey
);
73 return size
< 0 ? 0 : size
;
76 int EVP_PKEY_get_security_bits(const EVP_PKEY
*pkey
)
81 size
= pkey
->cache
.security_bits
;
82 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
83 size
= pkey
->ameth
->pkey_security_bits(pkey
);
85 return size
< 0 ? 0 : size
;
88 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
90 # ifndef OPENSSL_NO_DSA
91 if (pkey
->type
== EVP_PKEY_DSA
) {
92 int ret
= pkey
->save_parameters
;
95 pkey
->save_parameters
= mode
;
99 # ifndef OPENSSL_NO_EC
100 if (pkey
->type
== EVP_PKEY_EC
) {
101 int ret
= pkey
->save_parameters
;
104 pkey
->save_parameters
= mode
;
111 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
113 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
116 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
118 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
121 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
124 * Clean up legacy stuff from this function when legacy support is gone.
127 EVP_PKEY
*downgraded_from
= NULL
;
131 * If |to| is a legacy key and |from| isn't, we must make a downgraded
132 * copy of |from|. If that fails, this function fails.
134 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
135 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
137 from
= downgraded_from
;
141 * Make sure |to| is typed. Content is less important at this early
144 * 1. If |to| is untyped, assign |from|'s key type to it.
145 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
146 * (|from| was already downgraded above)
148 * If |to| is a provided key, there's nothing more to do here, functions
149 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
150 * further down help us find out if they are the same or not.
152 if (evp_pkey_is_blank(to
)) {
153 if (evp_pkey_is_legacy(from
)) {
154 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
157 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
160 } else if (evp_pkey_is_legacy(to
)) {
161 if (to
->type
!= from
->type
) {
162 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
167 if (EVP_PKEY_missing_parameters(from
)) {
168 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
172 if (!EVP_PKEY_missing_parameters(to
)) {
173 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
176 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
180 /* For purely provided keys, we just call the keymgmt utility */
181 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
182 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
187 * If |to| is provided, we know that |from| is legacy at this point.
188 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
189 * to copy the appropriate data to |to|'s keydata.
190 * We cannot override existing data so do it only if there is no keydata
193 if (to
->keymgmt
!= NULL
&& to
->keydata
== NULL
) {
194 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
196 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
200 * If we get a NULL, it could be an internal error, or it could be
201 * that there's a key mismatch. We're pretending the latter...
203 if (from_keydata
== NULL
)
204 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
206 ok
= (to
->keydata
= evp_keymgmt_dup(to
->keymgmt
,
208 SELECT_PARAMETERS
)) != NULL
;
212 /* Both keys are legacy */
213 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
214 ok
= from
->ameth
->param_copy(to
, from
);
216 EVP_PKEY_free(downgraded_from
);
220 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
223 if (pkey
->keymgmt
!= NULL
)
224 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
225 else if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
226 return pkey
->ameth
->param_missing(pkey
);
232 * This function is called for any mixture of keys except pure legacy pair.
233 * When legacy keys are gone, we replace a call to this functions with
234 * a call to evp_keymgmt_util_match().
236 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
239 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
240 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
242 /* If none of them are provided, this function shouldn't have been called */
243 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
246 /* For purely provided keys, we just call the keymgmt utility */
247 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
248 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
251 * At this point, one of them is provided, the other not. This allows
252 * us to compare types using legacy NIDs.
254 if (evp_pkey_is_legacy(a
)
255 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
256 return -1; /* not the same key type */
257 if (evp_pkey_is_legacy(b
)
258 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
259 return -1; /* not the same key type */
262 * We've determined that they both are the same keytype, so the next
263 * step is to do a bit of cross export to ensure we have keydata for
264 * both keys in the same keymgmt.
266 keymgmt1
= a
->keymgmt
;
267 keydata1
= a
->keydata
;
268 keymgmt2
= b
->keymgmt
;
269 keydata2
= b
->keydata
;
271 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
273 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
274 if (tmp_keydata
!= NULL
) {
276 keydata1
= tmp_keydata
;
279 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
281 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
282 if (tmp_keydata
!= NULL
) {
284 keydata2
= tmp_keydata
;
288 /* If we still don't have matching keymgmt implementations, we give up */
289 if (keymgmt1
!= keymgmt2
)
292 /* If the keymgmt implementations are NULL, the export failed */
293 if (keymgmt1
== NULL
)
296 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
299 # ifndef OPENSSL_NO_DEPRECATED_3_0
300 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
302 return EVP_PKEY_parameters_eq(a
, b
);
306 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
309 * This will just call evp_keymgmt_util_match when legacy support
313 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
314 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
316 /* All legacy keys */
317 if (a
->type
!= b
->type
)
319 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
320 return a
->ameth
->param_cmp(a
, b
);
324 # ifndef OPENSSL_NO_DEPRECATED_3_0
325 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
327 return EVP_PKEY_eq(a
, b
);
331 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
334 * This will just call evp_keymgmt_util_match when legacy support
338 /* Trivial shortcuts */
341 if (a
== NULL
|| b
== NULL
)
344 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
345 return evp_pkey_cmp_any(a
, b
, (SELECT_PARAMETERS
346 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY
));
348 /* All legacy keys */
349 if (a
->type
!= b
->type
)
352 if (a
->ameth
!= NULL
) {
354 /* Compare parameters if the algorithm has them */
355 if (a
->ameth
->param_cmp
!= NULL
) {
356 ret
= a
->ameth
->param_cmp(a
, b
);
361 if (a
->ameth
->pub_cmp
!= NULL
)
362 return a
->ameth
->pub_cmp(a
, b
);
369 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
374 const unsigned char *key
,
378 EVP_PKEY
*pkey
= NULL
;
379 EVP_PKEY_CTX
*ctx
= NULL
;
380 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
383 # ifndef OPENSSL_NO_ENGINE
384 /* Check if there is an Engine for this type */
389 ameth
= EVP_PKEY_asn1_find_str(&tmpe
, strtype
, -1);
390 else if (nidtype
!= EVP_PKEY_NONE
)
391 ameth
= EVP_PKEY_asn1_find(&tmpe
, nidtype
);
393 /* If tmpe is NULL then no engine is claiming to support this type */
401 if (e
== NULL
&& ameth
== NULL
) {
403 * No engine is claiming to support this type, so lets see if we have
406 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
407 strtype
!= NULL
? strtype
408 : OBJ_nid2sn(nidtype
),
412 /* May fail if no provider available */
414 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
415 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
417 ERR_clear_last_mark();
418 params
[0] = OSSL_PARAM_construct_octet_string(
419 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
420 : OSSL_PKEY_PARAM_PUB_KEY
,
423 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
424 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
428 EVP_PKEY_CTX_free(ctx
);
433 /* else not supported so fallback to legacy */
436 /* Legacy code path */
438 pkey
= EVP_PKEY_new();
440 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
444 if (!pkey_set_type(pkey
, e
, nidtype
, strtype
, -1, NULL
)) {
445 /* EVPerr already called */
449 if (!ossl_assert(pkey
->ameth
!= NULL
))
453 if (pkey
->ameth
->set_priv_key
== NULL
) {
454 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
458 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
459 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
463 if (pkey
->ameth
->set_pub_key
== NULL
) {
464 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
468 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
469 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
480 EVP_PKEY_CTX_free(ctx
);
484 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
487 const unsigned char *priv
, size_t len
)
489 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, priv
,
493 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
494 const unsigned char *priv
,
497 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, priv
, len
, 1);
500 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
501 const char *keytype
, const char *propq
,
502 const unsigned char *pub
, size_t len
)
504 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, pub
,
508 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
509 const unsigned char *pub
,
512 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, pub
, len
, 0);
515 struct raw_key_details_st
522 static OSSL_CALLBACK get_raw_key_details
;
523 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
525 const OSSL_PARAM
*p
= NULL
;
526 struct raw_key_details_st
*raw_key
= arg
;
528 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
529 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
531 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
532 SIZE_MAX
, raw_key
->len
);
533 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
534 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
536 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
537 SIZE_MAX
, raw_key
->len
);
543 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
546 if (pkey
->keymgmt
!= NULL
) {
547 struct raw_key_details_st raw_key
;
549 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
551 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
553 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
554 get_raw_key_details
, &raw_key
);
557 if (pkey
->ameth
== NULL
) {
558 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
562 if (pkey
->ameth
->get_priv_key
== NULL
) {
563 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
567 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
568 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
575 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
578 if (pkey
->keymgmt
!= NULL
) {
579 struct raw_key_details_st raw_key
;
581 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
583 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
585 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
586 get_raw_key_details
, &raw_key
);
589 if (pkey
->ameth
== NULL
) {
590 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
594 if (pkey
->ameth
->get_pub_key
== NULL
) {
595 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
599 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
600 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
607 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
608 const char *cipher_name
,
609 const EVP_CIPHER
*cipher
,
610 OSSL_LIB_CTX
*libctx
,
611 const char *propq
, ENGINE
*e
)
613 # ifndef OPENSSL_NO_CMAC
614 # ifndef OPENSSL_NO_ENGINE
615 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
617 OSSL_PARAM params
[5], *p
= params
;
618 EVP_PKEY
*pkey
= NULL
;
622 cipher_name
= EVP_CIPHER_get0_name(cipher
);
624 if (cipher_name
== NULL
) {
625 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
629 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
633 if (!EVP_PKEY_fromdata_init(ctx
)) {
634 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
638 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
640 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
641 (char *)cipher_name
, 0);
643 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
645 # ifndef OPENSSL_NO_ENGINE
646 if (engine_id
!= NULL
)
647 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE
,
648 (char *)engine_id
, 0);
650 *p
= OSSL_PARAM_construct_end();
652 if (!EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
)) {
653 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
658 EVP_PKEY_CTX_free(ctx
);
662 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
667 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
668 size_t len
, const EVP_CIPHER
*cipher
)
670 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
, e
);
673 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
675 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
678 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
680 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
683 # ifndef OPENSSL_NO_ENGINE
684 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
687 if (!ENGINE_init(e
)) {
688 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
691 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
693 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
697 ENGINE_finish(pkey
->pmeth_engine
);
698 pkey
->pmeth_engine
= e
;
702 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
708 # ifndef OPENSSL_NO_DEPRECATED_3_0
709 static void detect_foreign_key(EVP_PKEY
*pkey
)
711 switch (pkey
->type
) {
713 pkey
->foreign
= pkey
->pkey
.rsa
!= NULL
714 && ossl_rsa_is_foreign(pkey
->pkey
.rsa
);
716 # ifndef OPENSSL_NO_EC
719 pkey
->foreign
= pkey
->pkey
.ec
!= NULL
720 && ossl_ec_key_is_foreign(pkey
->pkey
.ec
);
723 # ifndef OPENSSL_NO_DSA
725 pkey
->foreign
= pkey
->pkey
.dsa
!= NULL
726 && ossl_dsa_is_foreign(pkey
->pkey
.dsa
);
729 # ifndef OPENSSL_NO_DH
731 pkey
->foreign
= pkey
->pkey
.dh
!= NULL
732 && ossl_dh_is_foreign(pkey
->pkey
.dh
);
741 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
743 # ifndef OPENSSL_NO_EC
746 pktype
= EVP_PKEY_type(type
);
747 if ((key
!= NULL
) && (pktype
== EVP_PKEY_EC
|| pktype
== EVP_PKEY_SM2
)) {
748 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
751 int curve
= EC_GROUP_get_curve_name(group
);
754 * Regardless of what is requested the SM2 curve must be SM2 type,
755 * and non SM2 curves are EC type.
757 if (curve
== NID_sm2
&& pktype
== EVP_PKEY_EC
)
759 else if(curve
!= NID_sm2
&& pktype
== EVP_PKEY_SM2
)
765 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
768 pkey
->pkey
.ptr
= key
;
769 detect_foreign_key(pkey
);
771 return (key
!= NULL
);
775 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
780 if (!evp_pkey_is_provided(pkey
))
781 return pkey
->pkey
.ptr
;
786 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
788 const ASN1_OCTET_STRING
*os
= NULL
;
789 if (pkey
->type
!= EVP_PKEY_HMAC
) {
790 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
793 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
801 # ifndef OPENSSL_NO_POLY1305
802 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
804 const ASN1_OCTET_STRING
*os
= NULL
;
805 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
806 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
809 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
818 # ifndef OPENSSL_NO_SIPHASH
819 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
821 const ASN1_OCTET_STRING
*os
= NULL
;
823 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
824 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
827 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
836 # ifndef OPENSSL_NO_DSA
837 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
839 if (pkey
->type
!= EVP_PKEY_DSA
) {
840 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
843 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
846 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
848 return evp_pkey_get0_DSA_int(pkey
);
851 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
853 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
858 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
860 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
866 # endif /* OPENSSL_NO_DSA */
868 # ifndef OPENSSL_NO_EC
869 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
871 if (EVP_PKEY_get_base_id(pkey
) != type
) {
872 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
875 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
878 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
880 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
882 if (ret
!= NULL
&& !ossl_ecx_key_up_ref(ret
))
887 # define IMPLEMENT_ECX_VARIANT(NAME) \
888 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
890 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
892 IMPLEMENT_ECX_VARIANT(X25519
)
893 IMPLEMENT_ECX_VARIANT(X448
)
894 IMPLEMENT_ECX_VARIANT(ED25519
)
895 IMPLEMENT_ECX_VARIANT(ED448
)
899 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
901 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*dhkey
)
906 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
907 * related to ffdhe and modp (which cache q = (p - 1) / 2),
908 * and returns 0 for all other dh parameter generation types including
909 * RFC5114 named groups.
911 * The EVP_PKEY_DH type is used for dh parameter generation types:
912 * - named safe prime groups related to ffdhe and modp
913 * - safe prime generator
915 * The type EVP_PKEY_DHX is used for dh parameter generation types
916 * - fips186-4 and fips186-2
917 * - rfc5114 named groups.
919 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
921 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
922 * q value to be stored.
924 if (ossl_dh_is_named_safe_prime_group(dhkey
))
927 type
= DH_get0_q(dhkey
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
929 ret
= EVP_PKEY_assign(pkey
, type
, dhkey
);
936 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
938 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
939 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
942 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
945 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
947 return evp_pkey_get0_DH_int(pkey
);
950 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
952 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
960 int EVP_PKEY_type(int type
)
963 const EVP_PKEY_ASN1_METHOD
*ameth
;
965 ameth
= EVP_PKEY_asn1_find(&e
, type
);
967 ret
= ameth
->pkey_id
;
970 # ifndef OPENSSL_NO_ENGINE
976 int EVP_PKEY_get_id(const EVP_PKEY
*pkey
)
981 int EVP_PKEY_get_base_id(const EVP_PKEY
*pkey
)
983 return EVP_PKEY_type(pkey
->type
);
987 * These hard coded cases are pure hackery to get around the fact
988 * that names in crypto/objects/objects.txt are a mess. There is
989 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
990 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
991 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
992 * "DSA" is accurate... but still, better be safe and hard-code
993 * names that we know.
994 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
995 * EVP_PKEY_EC, because of aliasing.
996 * This should be cleaned away along with all other #legacy support.
998 static const OSSL_ITEM standard_name2type
[] = {
999 { EVP_PKEY_RSA
, "RSA" },
1000 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
1001 { EVP_PKEY_EC
, "EC" },
1002 { EVP_PKEY_ED25519
, "ED25519" },
1003 { EVP_PKEY_ED448
, "ED448" },
1004 { EVP_PKEY_X25519
, "X25519" },
1005 { EVP_PKEY_X448
, "X448" },
1006 { EVP_PKEY_SM2
, "SM2" },
1007 { EVP_PKEY_DH
, "DH" },
1008 { EVP_PKEY_DHX
, "X9.42 DH" },
1009 { EVP_PKEY_DHX
, "DHX" },
1010 { EVP_PKEY_DSA
, "DSA" },
1013 int evp_pkey_name2type(const char *name
)
1018 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1019 if (strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
1020 return (int)standard_name2type
[i
].id
;
1023 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
1025 return EVP_PKEY_type(OBJ_ln2nid(name
));
1028 const char *evp_pkey_type2name(int type
)
1032 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1033 if (type
== (int)standard_name2type
[i
].id
)
1034 return standard_name2type
[i
].ptr
;
1037 return OBJ_nid2sn(type
);
1040 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
1042 if (pkey
->keymgmt
== NULL
) {
1043 int type
= evp_pkey_name2type(name
);
1045 return pkey
->type
== type
;
1047 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
1050 int EVP_PKEY_type_names_do_all(const EVP_PKEY
*pkey
,
1051 void (*fn
)(const char *name
, void *data
),
1054 if (!evp_pkey_is_typed(pkey
))
1057 if (!evp_pkey_is_provided(pkey
)) {
1058 const char *name
= OBJ_nid2sn(EVP_PKEY_get_id(pkey
));
1063 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1066 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1068 if (pkey
->keymgmt
== NULL
) {
1069 switch (EVP_PKEY_get_base_id(pkey
)) {
1072 # ifndef OPENSSL_NO_DSA
1076 # ifndef OPENSSL_NO_EC
1077 case EVP_PKEY_ED25519
:
1078 case EVP_PKEY_ED448
:
1080 case EVP_PKEY_EC
: /* Including SM2 */
1081 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1087 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_get0_provider(pkey
->keymgmt
);
1088 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1089 const char *supported_sig
=
1090 pkey
->keymgmt
->query_operation_name
!= NULL
1091 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
1092 : EVP_KEYMGMT_get0_name(pkey
->keymgmt
);
1093 EVP_SIGNATURE
*signature
= NULL
;
1095 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
1096 if (signature
!= NULL
) {
1097 EVP_SIGNATURE_free(signature
);
1104 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1106 BIO_set_indent(*out
, saved_indent
);
1108 BIO
*next
= BIO_pop(*out
);
1116 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1122 long i
= BIO_get_indent(*out
);
1124 *saved_indent
= (i
< 0 ? 0 : i
);
1125 if (BIO_set_indent(*out
, indent
) <= 0) {
1126 if ((*out
= BIO_push(BIO_new(BIO_f_prefix()), *out
)) == NULL
)
1130 if (BIO_set_indent(*out
, indent
) <= 0) {
1131 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1138 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1141 return BIO_indent(out
, indent
, 128)
1142 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1143 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
1146 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1147 int selection
/* For provided encoding */,
1148 const char *propquery
/* For provided encoding */,
1149 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1150 int indent
, ASN1_PCTX
*pctx
),
1151 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1155 OSSL_ENCODER_CTX
*ctx
= NULL
;
1156 int ret
= -2; /* default to unsupported */
1158 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1161 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1163 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1164 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1165 OSSL_ENCODER_CTX_free(ctx
);
1170 /* legacy fallback */
1171 if (legacy_print
!= NULL
)
1172 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1174 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1177 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1181 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1182 int indent
, ASN1_PCTX
*pctx
)
1184 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1185 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1189 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1190 int indent
, ASN1_PCTX
*pctx
)
1192 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEYPAIR
, NULL
,
1193 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1197 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1198 int indent
, ASN1_PCTX
*pctx
)
1200 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1201 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1205 # ifndef OPENSSL_NO_STDIO
1206 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1207 int indent
, ASN1_PCTX
*pctx
)
1210 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1214 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1219 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1220 int indent
, ASN1_PCTX
*pctx
)
1223 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1227 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1232 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1233 int indent
, ASN1_PCTX
*pctx
)
1236 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1240 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1246 static void mdname2nid(const char *mdname
, void *data
)
1248 int *nid
= (int *)data
;
1250 if (*nid
!= NID_undef
)
1253 *nid
= OBJ_sn2nid(mdname
);
1254 if (*nid
== NID_undef
)
1255 *nid
= OBJ_ln2nid(mdname
);
1258 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1259 int arg1
, void *arg2
)
1261 if (pkey
->keymgmt
== NULL
)
1264 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
1266 char mdname
[80] = "";
1267 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1272 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1273 /* Make sure the MD is in the namemap if available */
1275 OSSL_NAMEMAP
*namemap
;
1276 int nid
= NID_undef
;
1278 (void)ERR_set_mark();
1279 md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1280 (void)ERR_pop_to_mark();
1281 namemap
= ossl_namemap_stored(libctx
);
1284 * The only reason to fetch the MD was to make sure it is in the
1285 * namemap. We can immediately free it.
1288 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1293 * We have the namemap number - now we need to find the
1296 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1307 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1309 if (pkey
->ameth
== NULL
)
1310 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1311 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1313 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1316 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1318 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1321 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1322 char *mdname
, size_t mdname_sz
)
1324 if (pkey
->ameth
== NULL
)
1325 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1330 int nid
= NID_undef
;
1331 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1332 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1335 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1340 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1343 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1344 gname
, gname_sz
, gname_len
);
1347 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY
*pkey
, OSSL_LIB_CTX
*libctx
,
1348 const char *name
, const char *propq
)
1351 EVP_MD_CTX
*ctx
= NULL
;
1353 if ((ctx
= EVP_MD_CTX_new()) == NULL
)
1357 rv
= EVP_DigestSignInit_ex(ctx
, NULL
, name
, libctx
,
1361 EVP_MD_CTX_free(ctx
);
1365 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1368 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
1370 EVP_PKEY_set_octet_string_param(pkey
,
1371 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1372 (unsigned char *)pub
, publen
);
1374 if (publen
> INT_MAX
)
1376 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1377 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, publen
,
1383 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1387 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
1388 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1391 * We know that this is going to fail, but it will give us a size
1394 EVP_PKEY_get_octet_string_param(pkey
,
1395 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1396 NULL
, 0, &return_size
);
1397 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1400 *ppub
= OPENSSL_malloc(return_size
);
1404 if (!EVP_PKEY_get_octet_string_param(pkey
,
1405 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1406 *ppub
, return_size
, NULL
))
1412 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1418 #endif /* FIPS_MODULE */
1420 /*- All methods below can also be used in FIPS_MODULE */
1422 EVP_PKEY
*EVP_PKEY_new(void)
1424 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1427 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1431 ret
->type
= EVP_PKEY_NONE
;
1432 ret
->save_type
= EVP_PKEY_NONE
;
1433 ret
->references
= 1;
1435 ret
->lock
= CRYPTO_THREAD_lock_new();
1436 if (ret
->lock
== NULL
) {
1437 EVPerr(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1442 ret
->save_parameters
= 1;
1443 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1444 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1451 CRYPTO_THREAD_lock_free(ret
->lock
);
1457 * Setup a public key management method.
1459 * For legacy keys, either |type| or |str| is expected to have the type
1460 * information. In this case, the setup consists of finding an ASN1 method
1461 * and potentially an ENGINE, and setting those fields in |pkey|.
1463 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1464 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1466 * If pkey is NULL just return 1 or 0 if the key management method exists.
1469 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1470 int len
, EVP_KEYMGMT
*keymgmt
)
1473 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1474 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1478 * The setups can't set both legacy and provider side methods.
1481 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1482 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1483 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1491 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1493 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1495 evp_pkey_free_it(pkey
);
1498 * If key type matches and a method exists then this lookup has
1499 * succeeded once so just indicate success.
1501 if (pkey
->type
!= EVP_PKEY_NONE
1502 && type
== pkey
->save_type
1503 && pkey
->ameth
!= NULL
)
1505 # ifndef OPENSSL_NO_ENGINE
1506 /* If we have ENGINEs release them */
1507 ENGINE_finish(pkey
->engine
);
1508 pkey
->engine
= NULL
;
1509 ENGINE_finish(pkey
->pmeth_engine
);
1510 pkey
->pmeth_engine
= NULL
;
1516 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1517 else if (type
!= EVP_PKEY_NONE
)
1518 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1519 # ifndef OPENSSL_NO_ENGINE
1520 if (pkey
== NULL
&& eptr
!= NULL
)
1530 check
= check
&& ameth
== NULL
;
1532 check
= check
&& keymgmt
== NULL
;
1534 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1539 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1540 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1544 pkey
->keymgmt
= keymgmt
;
1546 pkey
->save_type
= type
;
1551 * If the internal "origin" key is provider side, don't save |ameth|.
1552 * The main reason is that |ameth| is one factor to detect that the
1553 * internal "origin" key is a legacy one.
1555 if (keymgmt
== NULL
)
1556 pkey
->ameth
= ameth
;
1560 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1561 * for any key type that has a legacy implementation, regardless of
1562 * if the internal key is a legacy or a provider side one. When
1563 * there is no legacy implementation for the key, the type becomes
1564 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1565 * with functions that expect legacy internal keys.
1567 if (ameth
!= NULL
) {
1568 if (type
== EVP_PKEY_NONE
)
1569 pkey
->type
= ameth
->pkey_id
;
1571 pkey
->type
= EVP_PKEY_KEYMGMT
;
1579 static void find_ameth(const char *name
, void *data
)
1581 const char **str
= data
;
1584 * The error messages from pkey_set_type() are uninteresting here,
1589 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, strlen(name
),
1593 else if (str
[1] == NULL
)
1601 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1604 # define EVP_PKEY_TYPE_STR str[0]
1605 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1607 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1608 * Ideally, only one should be found. If two (or more) are found, the
1609 * match is ambiguous. This should never happen, but...
1611 const char *str
[2] = { NULL
, NULL
};
1613 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1614 || str
[1] != NULL
) {
1615 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1619 # define EVP_PKEY_TYPE_STR NULL
1620 # define EVP_PKEY_TYPE_STRLEN -1
1622 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1623 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1626 #undef EVP_PKEY_TYPE_STR
1627 #undef EVP_PKEY_TYPE_STRLEN
1630 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1634 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
1637 REF_PRINT_COUNT("EVP_PKEY", pkey
);
1638 REF_ASSERT_ISNT(i
< 2);
1639 return ((i
> 1) ? 1 : 0);
1643 EVP_PKEY
*EVP_PKEY_dup(EVP_PKEY
*pkey
)
1648 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
1652 if ((dup_pk
= EVP_PKEY_new()) == NULL
)
1655 if (evp_pkey_is_blank(pkey
))
1658 if (evp_pkey_is_provided(pkey
)) {
1659 if (!evp_keymgmt_util_copy(dup_pk
, pkey
,
1660 OSSL_KEYMGMT_SELECT_ALL
))
1665 if (evp_pkey_is_legacy(pkey
)) {
1666 const EVP_PKEY_ASN1_METHOD
*ameth
= pkey
->ameth
;
1668 if (ameth
== NULL
|| ameth
->copy
== NULL
) {
1669 if (pkey
->pkey
.ptr
== NULL
/* empty key, just set type */
1670 && EVP_PKEY_set_type(dup_pk
, pkey
->type
) != 0)
1672 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_KEY_TYPE
);
1675 if (!ameth
->copy(dup_pk
, pkey
))
1682 /* copy auxiliary data */
1683 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
,
1684 &dup_pk
->ex_data
, &pkey
->ex_data
))
1687 if (pkey
->attributes
!= NULL
) {
1688 if ((dup_pk
->attributes
= ossl_x509at_dup(pkey
->attributes
)) == NULL
)
1693 EVP_PKEY_free(dup_pk
);
1697 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1699 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1700 ENGINE
*tmpe
= NULL
;
1702 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1703 ameth
= EVP_PKEY_asn1_find(&tmpe
, x
->type
);
1705 if (ameth
!= NULL
) {
1706 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1708 * We should never have both a legacy origin key, and a key in the
1711 assert(x
->pkey
.ptr
== NULL
);
1713 * For the purposes of freeing we make the legacy cache look like
1714 * a legacy origin key.
1716 x
->pkey
= x
->legacy_cache_pkey
;
1717 x
->legacy_cache_pkey
.ptr
= NULL
;
1719 if (ameth
->pkey_free
!= NULL
)
1720 ameth
->pkey_free(x
);
1723 # ifndef OPENSSL_NO_ENGINE
1724 ENGINE_finish(tmpe
);
1725 ENGINE_finish(x
->engine
);
1727 ENGINE_finish(x
->pmeth_engine
);
1728 x
->pmeth_engine
= NULL
;
1731 #endif /* FIPS_MODULE */
1733 static void evp_pkey_free_it(EVP_PKEY
*x
)
1735 /* internal function; x is never NULL */
1736 evp_keymgmt_util_clear_operation_cache(x
, 1);
1738 evp_pkey_free_legacy(x
);
1741 if (x
->keymgmt
!= NULL
) {
1742 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1743 EVP_KEYMGMT_free(x
->keymgmt
);
1747 x
->type
= EVP_PKEY_NONE
;
1750 void EVP_PKEY_free(EVP_PKEY
*x
)
1757 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
1758 REF_PRINT_COUNT("EVP_PKEY", x
);
1761 REF_ASSERT_ISNT(i
< 0);
1762 evp_pkey_free_it(x
);
1764 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1766 CRYPTO_THREAD_lock_free(x
->lock
);
1768 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1773 int EVP_PKEY_get_size(const EVP_PKEY
*pkey
)
1778 size
= pkey
->cache
.size
;
1780 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1781 size
= pkey
->ameth
->pkey_size(pkey
);
1784 return size
< 0 ? 0 : size
;
1787 const char *EVP_PKEY_get0_description(const EVP_PKEY
*pkey
)
1789 if (!evp_pkey_is_assigned(pkey
))
1792 if (evp_pkey_is_provided(pkey
) && pkey
->keymgmt
->description
!= NULL
)
1793 return pkey
->keymgmt
->description
;
1795 if (pkey
->ameth
!= NULL
)
1796 return pkey
->ameth
->info
;
1801 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1802 EVP_KEYMGMT
**keymgmt
,
1803 const char *propquery
)
1805 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1806 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1807 void *keydata
= NULL
;
1813 /* No key data => nothing to export */
1816 check
= check
&& pk
->pkey
.ptr
== NULL
;
1818 check
= check
&& pk
->keydata
== NULL
;
1823 if (pk
->pkey
.ptr
!= NULL
) {
1825 * If the legacy key doesn't have an dirty counter or export function,
1828 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1833 if (keymgmt
!= NULL
) {
1834 tmp_keymgmt
= *keymgmt
;
1839 * If no keymgmt was given or found, get a default keymgmt. We do so by
1840 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1842 if (tmp_keymgmt
== NULL
) {
1843 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1845 tmp_keymgmt
= ctx
->keymgmt
;
1846 ctx
->keymgmt
= NULL
;
1847 EVP_PKEY_CTX_free(ctx
);
1850 /* If there's still no keymgmt to be had, give up */
1851 if (tmp_keymgmt
== NULL
)
1855 if (pk
->pkey
.ptr
!= NULL
) {
1859 * If the legacy "origin" hasn't changed since last time, we try
1860 * to find our keymgmt in the operation cache. If it has changed,
1861 * |i| remains zero, and we will clear the cache further down.
1863 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1864 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1866 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1869 * If |tmp_keymgmt| is present in the operation cache, it means
1870 * that export doesn't need to be redone. In that case, we take
1871 * token copies of the cached pointers, to have token success
1874 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1875 keydata
= op
->keydata
;
1876 CRYPTO_THREAD_unlock(pk
->lock
);
1879 CRYPTO_THREAD_unlock(pk
->lock
);
1882 /* Make sure that the keymgmt key type matches the legacy NID */
1883 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
1886 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1889 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
->import
,
1890 libctx
, propquery
)) {
1891 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1897 * If the dirty counter changed since last time, then clear the
1898 * operation cache. In that case, we know that |i| is zero. Just
1899 * in case this is a re-export, we increment then decrement the
1900 * keymgmt reference counter.
1902 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1903 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1908 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1910 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
1911 && !evp_keymgmt_util_clear_operation_cache(pk
, 0)) {
1912 CRYPTO_THREAD_unlock(pk
->lock
);
1913 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1915 EVP_KEYMGMT_free(tmp_keymgmt
);
1918 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1920 /* Check to make sure some other thread didn't get there first */
1921 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1922 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1923 void *tmp_keydata
= op
->keydata
;
1925 CRYPTO_THREAD_unlock(pk
->lock
);
1926 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1927 keydata
= tmp_keydata
;
1931 /* Add the new export to the operation cache */
1932 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
)) {
1933 CRYPTO_THREAD_unlock(pk
->lock
);
1934 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1939 /* Synchronize the dirty count */
1940 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1942 CRYPTO_THREAD_unlock(pk
->lock
);
1945 #endif /* FIPS_MODULE */
1947 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
);
1951 * If nothing was exported, |tmp_keymgmt| might point at a freed
1952 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1953 * the caller either way in that case.
1955 if (keydata
== NULL
)
1958 if (keymgmt
!= NULL
)
1959 *keymgmt
= tmp_keymgmt
;
1961 EVP_KEYMGMT_free(allocated_keymgmt
);
1966 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
1968 if (!ossl_assert(dest
!= NULL
))
1971 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
1972 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
1973 void *keydata
= src
->keydata
;
1974 int type
= src
->type
;
1975 const char *keytype
= NULL
;
1977 keytype
= EVP_KEYMGMT_get0_name(keymgmt
);
1980 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1981 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1982 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1983 * The check is kept as a safety measure.
1985 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
1986 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
1987 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1992 /* Prefer the legacy key type name for error reporting */
1993 if (type
!= EVP_PKEY_KEYMGMT
)
1994 keytype
= OBJ_nid2sn(type
);
1996 /* Make sure we have a clean slate to copy into */
1997 if (*dest
== NULL
) {
1998 *dest
= EVP_PKEY_new();
1999 if (*dest
== NULL
) {
2000 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
2004 evp_pkey_free_it(*dest
);
2007 if (EVP_PKEY_set_type(*dest
, type
)) {
2008 /* If the key is typed but empty, we're done */
2009 if (keydata
== NULL
)
2012 if ((*dest
)->ameth
->import_from
== NULL
) {
2013 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
2014 "key type = %s", keytype
);
2017 * We perform the export in the same libctx as the keymgmt
2018 * that we are using.
2020 OSSL_LIB_CTX
*libctx
=
2021 ossl_provider_libctx(keymgmt
->prov
);
2022 EVP_PKEY_CTX
*pctx
=
2023 EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
2026 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
2029 && evp_keymgmt_export(keymgmt
, keydata
,
2030 OSSL_KEYMGMT_SELECT_ALL
,
2031 (*dest
)->ameth
->import_from
,
2033 /* Synchronize the dirty count */
2034 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
2036 EVP_PKEY_CTX_free(pctx
);
2039 EVP_PKEY_CTX_free(pctx
);
2042 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
2043 "key type = %s", keytype
);
2050 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
2052 EVP_PKEY
*tmp_copy
= NULL
;
2055 if (!ossl_assert(pk
!= NULL
))
2059 * If this isn't an assigned provider side key, we just use any existing
2060 * origin legacy key.
2062 if (!evp_pkey_is_assigned(pk
))
2064 if (!evp_pkey_is_provided(pk
))
2065 return pk
->pkey
.ptr
;
2067 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
2070 ret
= pk
->legacy_cache_pkey
.ptr
;
2072 if (!CRYPTO_THREAD_unlock(pk
->lock
))
2078 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
2081 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2084 /* Check again in case some other thread has updated it in the meantime */
2085 ret
= pk
->legacy_cache_pkey
.ptr
;
2087 /* Steal the legacy key reference from the temporary copy */
2088 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
2089 tmp_copy
->pkey
.ptr
= NULL
;
2092 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
2098 EVP_PKEY_free(tmp_copy
);
2102 #endif /* FIPS_MODULE */
2104 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
2108 OSSL_PARAM params
[2];
2109 unsigned char buffer
[2048];
2110 unsigned char *buf
= NULL
;
2113 if (key_name
== NULL
2117 memset(buffer
, 0, sizeof(buffer
));
2118 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
2119 params
[1] = OSSL_PARAM_construct_end();
2120 if (!EVP_PKEY_get_params(pkey
, params
)) {
2121 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
2123 buf_sz
= params
[0].return_size
;
2125 * If it failed because the buffer was too small then allocate the
2126 * required buffer size and retry.
2128 buf
= OPENSSL_zalloc(buf_sz
);
2131 params
[0].data
= buf
;
2132 params
[0].data_size
= buf_sz
;
2134 if (!EVP_PKEY_get_params(pkey
, params
))
2137 /* Fail if the param was not found */
2138 if (!OSSL_PARAM_modified(params
))
2140 ret
= OSSL_PARAM_get_BN(params
, bn
);
2146 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2147 unsigned char *buf
, size_t max_buf_sz
,
2150 OSSL_PARAM params
[2];
2151 int ret1
= 0, ret2
= 0;
2153 if (key_name
== NULL
)
2156 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2157 params
[1] = OSSL_PARAM_construct_end();
2158 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2159 ret2
= OSSL_PARAM_modified(params
);
2160 if (ret2
&& out_sz
!= NULL
)
2161 *out_sz
= params
[0].return_size
;
2162 return ret1
&& ret2
;
2165 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2166 char *str
, size_t max_buf_sz
,
2169 OSSL_PARAM params
[2];
2170 int ret1
= 0, ret2
= 0;
2172 if (key_name
== NULL
)
2175 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2176 params
[1] = OSSL_PARAM_construct_end();
2177 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2178 ret2
= OSSL_PARAM_modified(params
);
2179 if (ret2
&& out_sz
!= NULL
)
2180 *out_sz
= params
[0].return_size
;
2181 return ret1
&& ret2
;
2184 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2187 OSSL_PARAM params
[2];
2189 if (key_name
== NULL
)
2192 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2193 params
[1] = OSSL_PARAM_construct_end();
2194 return EVP_PKEY_get_params(pkey
, params
)
2195 && OSSL_PARAM_modified(params
);
2198 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2201 OSSL_PARAM params
[2];
2203 if (key_name
== NULL
)
2206 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2207 params
[1] = OSSL_PARAM_construct_end();
2208 return EVP_PKEY_get_params(pkey
, params
)
2209 && OSSL_PARAM_modified(params
);
2212 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2214 OSSL_PARAM params
[2];
2216 if (key_name
== NULL
)
2219 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2220 params
[1] = OSSL_PARAM_construct_end();
2221 return EVP_PKEY_set_params(pkey
, params
);
2224 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2226 OSSL_PARAM params
[2];
2228 if (key_name
== NULL
)
2231 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2232 params
[1] = OSSL_PARAM_construct_end();
2233 return EVP_PKEY_set_params(pkey
, params
);
2236 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2239 OSSL_PARAM params
[2];
2240 unsigned char buffer
[2048];
2243 if (key_name
== NULL
2246 || !evp_pkey_is_assigned(pkey
))
2249 bsize
= BN_num_bytes(bn
);
2250 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2253 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2255 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2256 params
[1] = OSSL_PARAM_construct_end();
2257 return EVP_PKEY_set_params(pkey
, params
);
2260 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2263 OSSL_PARAM params
[2];
2265 if (key_name
== NULL
)
2268 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2269 params
[1] = OSSL_PARAM_construct_end();
2270 return EVP_PKEY_set_params(pkey
, params
);
2273 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2274 const unsigned char *buf
, size_t bsize
)
2276 OSSL_PARAM params
[2];
2278 if (key_name
== NULL
)
2281 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2282 (unsigned char *)buf
, bsize
);
2283 params
[1] = OSSL_PARAM_construct_end();
2284 return EVP_PKEY_set_params(pkey
, params
);
2287 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2289 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2290 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2294 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2297 if (evp_pkey_is_provided(pkey
)) {
2299 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2303 * We will hopefully never find the need to set individual data in
2304 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2305 * sure. This bit of code can be enabled if we find the need. If
2306 * not, it can safely be removed when #legacy support is removed.
2309 else if (evp_pkey_is_legacy(pkey
)) {
2310 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2315 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2319 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2321 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2322 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2326 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2329 if (evp_pkey_is_provided(pkey
))
2330 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2332 else if (evp_pkey_is_legacy(pkey
))
2333 return evp_pkey_get_params_to_ctrl(pkey
, params
);
2336 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2341 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2349 if (pkey
->keymgmt
== NULL
2350 || pkey
->keydata
== NULL
) {
2351 # ifndef OPENSSL_NO_EC
2352 /* Might work through the legacy route */
2353 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2358 return EC_KEY_get_conv_form(ec
);
2364 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2365 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2366 name
, sizeof(name
), &name_len
))
2369 if (strcmp(name
, "uncompressed") == 0)
2370 return POINT_CONVERSION_UNCOMPRESSED
;
2372 if (strcmp(name
, "compressed") == 0)
2373 return POINT_CONVERSION_COMPRESSED
;
2375 if (strcmp(name
, "hybrid") == 0)
2376 return POINT_CONVERSION_HYBRID
;
2381 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2389 if (pkey
->keymgmt
== NULL
2390 || pkey
->keydata
== NULL
) {
2391 # ifndef OPENSSL_NO_EC
2392 /* Might work through the legacy route */
2393 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2394 const EC_GROUP
*grp
;
2398 grp
= EC_KEY_get0_group(ec
);
2402 return EC_GROUP_get_field_type(grp
);
2408 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2409 fstr
, sizeof(fstr
), &fstrlen
))
2412 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2413 return NID_X9_62_prime_field
;
2414 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2415 return NID_X9_62_characteristic_two_field
;