2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "internal/namemap.h"
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 #include <openssl/objects.h>
24 #include <openssl/evp.h>
25 #include <openssl/rsa.h>
26 #include <openssl/dsa.h>
27 #include <openssl/dh.h>
28 #include <openssl/ec.h>
29 #include <openssl/cmac.h>
31 # include <openssl/engine.h>
33 #include <openssl/params.h>
34 #include <openssl/param_build.h>
35 #include <openssl/encoder.h>
36 #include <openssl/core_names.h>
38 #include "internal/numbers.h" /* includes SIZE_MAX */
39 #include "internal/ffc.h"
40 #include "crypto/evp.h"
41 #include "crypto/dh.h"
42 #include "crypto/dsa.h"
43 #include "crypto/ec.h"
44 #include "crypto/ecx.h"
45 #include "crypto/rsa.h"
47 # include "crypto/asn1.h"
48 # include "crypto/x509.h"
50 #include "internal/provider.h"
51 #include "evp_local.h"
53 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
54 int len
, EVP_KEYMGMT
*keymgmt
);
55 static void evp_pkey_free_it(EVP_PKEY
*key
);
59 /* The type of parameters selected in key parameter functions */
60 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
62 int EVP_PKEY_get_bits(const EVP_PKEY
*pkey
)
67 size
= pkey
->cache
.bits
;
68 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
69 size
= pkey
->ameth
->pkey_bits(pkey
);
71 return size
< 0 ? 0 : size
;
74 int EVP_PKEY_get_security_bits(const EVP_PKEY
*pkey
)
79 size
= pkey
->cache
.security_bits
;
80 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
81 size
= pkey
->ameth
->pkey_security_bits(pkey
);
83 return size
< 0 ? 0 : size
;
86 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
88 # ifndef OPENSSL_NO_DSA
89 if (pkey
->type
== EVP_PKEY_DSA
) {
90 int ret
= pkey
->save_parameters
;
93 pkey
->save_parameters
= mode
;
97 # ifndef OPENSSL_NO_EC
98 if (pkey
->type
== EVP_PKEY_EC
) {
99 int ret
= pkey
->save_parameters
;
102 pkey
->save_parameters
= mode
;
109 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
111 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
114 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
116 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
119 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
122 * Clean up legacy stuff from this function when legacy support is gone.
125 EVP_PKEY
*downgraded_from
= NULL
;
129 * If |to| is a legacy key and |from| isn't, we must make a downgraded
130 * copy of |from|. If that fails, this function fails.
132 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
133 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
135 from
= downgraded_from
;
139 * Make sure |to| is typed. Content is less important at this early
142 * 1. If |to| is untyped, assign |from|'s key type to it.
143 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
144 * (|from| was already downgraded above)
146 * If |to| is a provided key, there's nothing more to do here, functions
147 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
148 * further down help us find out if they are the same or not.
150 if (evp_pkey_is_blank(to
)) {
151 if (evp_pkey_is_legacy(from
)) {
152 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
155 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
158 } else if (evp_pkey_is_legacy(to
)) {
159 if (to
->type
!= from
->type
) {
160 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
165 if (EVP_PKEY_missing_parameters(from
)) {
166 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
170 if (!EVP_PKEY_missing_parameters(to
)) {
171 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
174 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
178 /* For purely provided keys, we just call the keymgmt utility */
179 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
180 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
185 * If |to| is provided, we know that |from| is legacy at this point.
186 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
187 * to copy the appropriate data to |to|'s keydata.
188 * We cannot override existing data so do it only if there is no keydata
191 if (to
->keymgmt
!= NULL
&& to
->keydata
== NULL
) {
192 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
194 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
198 * If we get a NULL, it could be an internal error, or it could be
199 * that there's a key mismatch. We're pretending the latter...
201 if (from_keydata
== NULL
)
202 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
204 ok
= (to
->keydata
= evp_keymgmt_dup(to
->keymgmt
,
206 SELECT_PARAMETERS
)) != NULL
;
210 /* Both keys are legacy */
211 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
212 ok
= from
->ameth
->param_copy(to
, from
);
214 EVP_PKEY_free(downgraded_from
);
218 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
221 if (pkey
->keymgmt
!= NULL
)
222 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
223 else if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
224 return pkey
->ameth
->param_missing(pkey
);
230 * This function is called for any mixture of keys except pure legacy pair.
231 * When legacy keys are gone, we replace a call to this functions with
232 * a call to evp_keymgmt_util_match().
234 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
237 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
238 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
240 /* If none of them are provided, this function shouldn't have been called */
241 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
244 /* For purely provided keys, we just call the keymgmt utility */
245 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
246 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
249 * At this point, one of them is provided, the other not. This allows
250 * us to compare types using legacy NIDs.
252 if (evp_pkey_is_legacy(a
)
253 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
254 return -1; /* not the same key type */
255 if (evp_pkey_is_legacy(b
)
256 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
257 return -1; /* not the same key type */
260 * We've determined that they both are the same keytype, so the next
261 * step is to do a bit of cross export to ensure we have keydata for
262 * both keys in the same keymgmt.
264 keymgmt1
= a
->keymgmt
;
265 keydata1
= a
->keydata
;
266 keymgmt2
= b
->keymgmt
;
267 keydata2
= b
->keydata
;
269 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
271 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
272 if (tmp_keydata
!= NULL
) {
274 keydata1
= tmp_keydata
;
277 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
279 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
280 if (tmp_keydata
!= NULL
) {
282 keydata2
= tmp_keydata
;
286 /* If we still don't have matching keymgmt implementations, we give up */
287 if (keymgmt1
!= keymgmt2
)
290 /* If the keymgmt implementations are NULL, the export failed */
291 if (keymgmt1
== NULL
)
294 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
297 # ifndef OPENSSL_NO_DEPRECATED_3_0
298 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
300 return EVP_PKEY_parameters_eq(a
, b
);
304 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
307 * This will just call evp_keymgmt_util_match when legacy support
311 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
312 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
314 /* All legacy keys */
315 if (a
->type
!= b
->type
)
317 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
318 return a
->ameth
->param_cmp(a
, b
);
322 # ifndef OPENSSL_NO_DEPRECATED_3_0
323 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
325 return EVP_PKEY_eq(a
, b
);
329 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
332 * This will just call evp_keymgmt_util_match when legacy support
336 /* Trivial shortcuts */
339 if (a
== NULL
|| b
== NULL
)
342 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
) {
343 int selection
= SELECT_PARAMETERS
;
345 if (evp_keymgmt_util_has((EVP_PKEY
*)a
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
)
346 && evp_keymgmt_util_has((EVP_PKEY
*)b
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
))
347 selection
|= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
349 selection
|= OSSL_KEYMGMT_SELECT_KEYPAIR
;
350 return evp_pkey_cmp_any(a
, b
, selection
);
353 /* All legacy keys */
354 if (a
->type
!= b
->type
)
357 if (a
->ameth
!= NULL
) {
359 /* Compare parameters if the algorithm has them */
360 if (a
->ameth
->param_cmp
!= NULL
) {
361 ret
= a
->ameth
->param_cmp(a
, b
);
366 if (a
->ameth
->pub_cmp
!= NULL
)
367 return a
->ameth
->pub_cmp(a
, b
);
374 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
379 const unsigned char *key
,
383 EVP_PKEY
*pkey
= NULL
;
384 EVP_PKEY_CTX
*ctx
= NULL
;
385 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
388 # ifndef OPENSSL_NO_ENGINE
389 /* Check if there is an Engine for this type */
394 ameth
= EVP_PKEY_asn1_find_str(&tmpe
, strtype
, -1);
395 else if (nidtype
!= EVP_PKEY_NONE
)
396 ameth
= EVP_PKEY_asn1_find(&tmpe
, nidtype
);
398 /* If tmpe is NULL then no engine is claiming to support this type */
406 if (e
== NULL
&& ameth
== NULL
) {
408 * No engine is claiming to support this type, so lets see if we have
411 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
412 strtype
!= NULL
? strtype
413 : OBJ_nid2sn(nidtype
),
417 /* May fail if no provider available */
419 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
420 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
422 ERR_clear_last_mark();
423 params
[0] = OSSL_PARAM_construct_octet_string(
424 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
425 : OSSL_PKEY_PARAM_PUB_KEY
,
428 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
429 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
433 EVP_PKEY_CTX_free(ctx
);
438 /* else not supported so fallback to legacy */
441 /* Legacy code path */
443 pkey
= EVP_PKEY_new();
445 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
449 if (!pkey_set_type(pkey
, e
, nidtype
, strtype
, -1, NULL
)) {
450 /* ERR_raise(ERR_LIB_EVP, ...) already called */
454 if (!ossl_assert(pkey
->ameth
!= NULL
))
458 if (pkey
->ameth
->set_priv_key
== NULL
) {
459 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
463 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
464 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
468 if (pkey
->ameth
->set_pub_key
== NULL
) {
469 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
473 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
474 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
485 EVP_PKEY_CTX_free(ctx
);
489 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
492 const unsigned char *priv
, size_t len
)
494 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, priv
,
498 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
499 const unsigned char *priv
,
502 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, priv
, len
, 1);
505 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
506 const char *keytype
, const char *propq
,
507 const unsigned char *pub
, size_t len
)
509 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, pub
,
513 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
514 const unsigned char *pub
,
517 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, pub
, len
, 0);
520 struct raw_key_details_st
527 static OSSL_CALLBACK get_raw_key_details
;
528 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
530 const OSSL_PARAM
*p
= NULL
;
531 struct raw_key_details_st
*raw_key
= arg
;
533 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
534 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
536 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
537 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
539 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
540 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
542 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
543 raw_key
->key
== NULL
? 0 : *raw_key
->len
,
550 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
553 if (pkey
->keymgmt
!= NULL
) {
554 struct raw_key_details_st raw_key
;
556 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
558 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
560 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
561 get_raw_key_details
, &raw_key
);
564 if (pkey
->ameth
== NULL
) {
565 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
569 if (pkey
->ameth
->get_priv_key
== NULL
) {
570 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
574 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
575 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
582 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
585 if (pkey
->keymgmt
!= NULL
) {
586 struct raw_key_details_st raw_key
;
588 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
590 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
592 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
593 get_raw_key_details
, &raw_key
);
596 if (pkey
->ameth
== NULL
) {
597 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
601 if (pkey
->ameth
->get_pub_key
== NULL
) {
602 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
606 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
607 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
614 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
615 const char *cipher_name
,
616 const EVP_CIPHER
*cipher
,
617 OSSL_LIB_CTX
*libctx
,
618 const char *propq
, ENGINE
*e
)
620 # ifndef OPENSSL_NO_CMAC
621 # ifndef OPENSSL_NO_ENGINE
622 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
624 OSSL_PARAM params
[5], *p
= params
;
625 EVP_PKEY
*pkey
= NULL
;
629 cipher_name
= EVP_CIPHER_get0_name(cipher
);
631 if (cipher_name
== NULL
) {
632 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
636 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
640 if (EVP_PKEY_fromdata_init(ctx
) <= 0) {
641 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
645 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
647 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
648 (char *)cipher_name
, 0);
650 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
652 # ifndef OPENSSL_NO_ENGINE
653 if (engine_id
!= NULL
)
654 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE
,
655 (char *)engine_id
, 0);
657 *p
= OSSL_PARAM_construct_end();
659 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) <= 0) {
660 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
665 EVP_PKEY_CTX_free(ctx
);
669 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
674 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
675 size_t len
, const EVP_CIPHER
*cipher
)
677 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
, e
);
680 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
682 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
685 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
687 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
690 # ifndef OPENSSL_NO_ENGINE
691 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
694 if (!ENGINE_init(e
)) {
695 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
698 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
700 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
704 ENGINE_finish(pkey
->pmeth_engine
);
705 pkey
->pmeth_engine
= e
;
709 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
715 # ifndef OPENSSL_NO_DEPRECATED_3_0
716 static void detect_foreign_key(EVP_PKEY
*pkey
)
718 switch (pkey
->type
) {
720 case EVP_PKEY_RSA_PSS
:
721 pkey
->foreign
= pkey
->pkey
.rsa
!= NULL
722 && ossl_rsa_is_foreign(pkey
->pkey
.rsa
);
724 # ifndef OPENSSL_NO_EC
728 pkey
->foreign
= pkey
->pkey
.ec
!= NULL
729 && ossl_ec_key_is_foreign(pkey
->pkey
.ec
);
732 # ifndef OPENSSL_NO_DSA
734 pkey
->foreign
= pkey
->pkey
.dsa
!= NULL
735 && ossl_dsa_is_foreign(pkey
->pkey
.dsa
);
738 # ifndef OPENSSL_NO_DH
740 pkey
->foreign
= pkey
->pkey
.dh
!= NULL
741 && ossl_dh_is_foreign(pkey
->pkey
.dh
);
750 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
752 # ifndef OPENSSL_NO_EC
755 pktype
= EVP_PKEY_type(type
);
756 if ((key
!= NULL
) && (pktype
== EVP_PKEY_EC
|| pktype
== EVP_PKEY_SM2
)) {
757 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
760 int curve
= EC_GROUP_get_curve_name(group
);
763 * Regardless of what is requested the SM2 curve must be SM2 type,
764 * and non SM2 curves are EC type.
766 if (curve
== NID_sm2
&& pktype
== EVP_PKEY_EC
)
768 else if(curve
!= NID_sm2
&& pktype
== EVP_PKEY_SM2
)
774 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
777 pkey
->pkey
.ptr
= key
;
778 detect_foreign_key(pkey
);
780 return (key
!= NULL
);
784 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
789 if (!evp_pkey_is_provided(pkey
))
790 return pkey
->pkey
.ptr
;
795 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
797 const ASN1_OCTET_STRING
*os
= NULL
;
798 if (pkey
->type
!= EVP_PKEY_HMAC
) {
799 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
802 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
810 # ifndef OPENSSL_NO_POLY1305
811 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
813 const ASN1_OCTET_STRING
*os
= NULL
;
814 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
815 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
818 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
827 # ifndef OPENSSL_NO_SIPHASH
828 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
830 const ASN1_OCTET_STRING
*os
= NULL
;
832 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
833 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
836 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
845 # ifndef OPENSSL_NO_DSA
846 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
848 if (pkey
->type
!= EVP_PKEY_DSA
) {
849 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
852 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
855 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
857 return evp_pkey_get0_DSA_int(pkey
);
860 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
862 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
867 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
869 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
875 # endif /* OPENSSL_NO_DSA */
877 # ifndef OPENSSL_NO_ECX
878 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
880 if (EVP_PKEY_get_base_id(pkey
) != type
) {
881 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
884 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
887 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
889 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
891 if (ret
!= NULL
&& !ossl_ecx_key_up_ref(ret
))
896 # define IMPLEMENT_ECX_VARIANT(NAME) \
897 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
899 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
901 IMPLEMENT_ECX_VARIANT(X25519
)
902 IMPLEMENT_ECX_VARIANT(X448
)
903 IMPLEMENT_ECX_VARIANT(ED25519
)
904 IMPLEMENT_ECX_VARIANT(ED448
)
906 # endif /* OPENSSL_NO_ECX */
908 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
910 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*dhkey
)
915 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
916 * related to ffdhe and modp (which cache q = (p - 1) / 2),
917 * and returns 0 for all other dh parameter generation types including
918 * RFC5114 named groups.
920 * The EVP_PKEY_DH type is used for dh parameter generation types:
921 * - named safe prime groups related to ffdhe and modp
922 * - safe prime generator
924 * The type EVP_PKEY_DHX is used for dh parameter generation types
925 * - fips186-4 and fips186-2
926 * - rfc5114 named groups.
928 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
930 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
931 * q value to be stored.
933 if (ossl_dh_is_named_safe_prime_group(dhkey
))
936 type
= DH_get0_q(dhkey
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
938 ret
= EVP_PKEY_assign(pkey
, type
, dhkey
);
945 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
947 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
948 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
951 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
954 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
956 return evp_pkey_get0_DH_int(pkey
);
959 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
961 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
969 int EVP_PKEY_type(int type
)
972 const EVP_PKEY_ASN1_METHOD
*ameth
;
974 ameth
= EVP_PKEY_asn1_find(&e
, type
);
976 ret
= ameth
->pkey_id
;
979 # ifndef OPENSSL_NO_ENGINE
985 int EVP_PKEY_get_id(const EVP_PKEY
*pkey
)
990 int EVP_PKEY_get_base_id(const EVP_PKEY
*pkey
)
992 return EVP_PKEY_type(pkey
->type
);
996 * These hard coded cases are pure hackery to get around the fact
997 * that names in crypto/objects/objects.txt are a mess. There is
998 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
999 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
1000 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
1001 * "DSA" is accurate... but still, better be safe and hard-code
1002 * names that we know.
1003 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
1004 * EVP_PKEY_EC, because of aliasing.
1005 * This should be cleaned away along with all other #legacy support.
1007 static const OSSL_ITEM standard_name2type
[] = {
1008 { EVP_PKEY_RSA
, "RSA" },
1009 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
1010 { EVP_PKEY_EC
, "EC" },
1011 { EVP_PKEY_ED25519
, "ED25519" },
1012 { EVP_PKEY_ED448
, "ED448" },
1013 { EVP_PKEY_X25519
, "X25519" },
1014 { EVP_PKEY_X448
, "X448" },
1015 { EVP_PKEY_SM2
, "SM2" },
1016 { EVP_PKEY_DH
, "DH" },
1017 { EVP_PKEY_DHX
, "X9.42 DH" },
1018 { EVP_PKEY_DHX
, "DHX" },
1019 { EVP_PKEY_DSA
, "DSA" },
1022 int evp_pkey_name2type(const char *name
)
1027 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1028 if (OPENSSL_strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
1029 return (int)standard_name2type
[i
].id
;
1032 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
1034 return EVP_PKEY_type(OBJ_ln2nid(name
));
1037 const char *evp_pkey_type2name(int type
)
1041 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
1042 if (type
== (int)standard_name2type
[i
].id
)
1043 return standard_name2type
[i
].ptr
;
1046 return OBJ_nid2sn(type
);
1049 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
1053 if (pkey
->keymgmt
== NULL
)
1054 return pkey
->type
== evp_pkey_name2type(name
);
1055 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
1058 int EVP_PKEY_type_names_do_all(const EVP_PKEY
*pkey
,
1059 void (*fn
)(const char *name
, void *data
),
1062 if (!evp_pkey_is_typed(pkey
))
1065 if (!evp_pkey_is_provided(pkey
)) {
1066 const char *name
= OBJ_nid2sn(EVP_PKEY_get_id(pkey
));
1071 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1074 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1076 if (pkey
->keymgmt
== NULL
) {
1077 switch (EVP_PKEY_get_base_id(pkey
)) {
1079 case EVP_PKEY_RSA_PSS
:
1081 # ifndef OPENSSL_NO_DSA
1085 # ifndef OPENSSL_NO_EC
1086 case EVP_PKEY_ED25519
:
1087 case EVP_PKEY_ED448
:
1089 case EVP_PKEY_EC
: /* Including SM2 */
1090 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1096 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_get0_provider(pkey
->keymgmt
);
1097 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1098 const char *supported_sig
=
1099 pkey
->keymgmt
->query_operation_name
!= NULL
1100 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
1101 : EVP_KEYMGMT_get0_name(pkey
->keymgmt
);
1102 EVP_SIGNATURE
*signature
= NULL
;
1104 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
1105 if (signature
!= NULL
) {
1106 EVP_SIGNATURE_free(signature
);
1113 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1115 BIO_set_indent(*out
, saved_indent
);
1117 BIO
*next
= BIO_pop(*out
);
1125 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1131 long i
= BIO_get_indent(*out
);
1133 *saved_indent
= (i
< 0 ? 0 : i
);
1134 if (BIO_set_indent(*out
, indent
) <= 0) {
1135 BIO
*prefbio
= BIO_new(BIO_f_prefix());
1137 if (prefbio
== NULL
)
1139 *out
= BIO_push(prefbio
, *out
);
1142 if (BIO_set_indent(*out
, indent
) <= 0) {
1143 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1150 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1153 return BIO_indent(out
, indent
, 128)
1154 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1155 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
1158 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1159 int selection
/* For provided encoding */,
1160 const char *propquery
/* For provided encoding */,
1161 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1162 int indent
, ASN1_PCTX
*pctx
),
1163 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1167 OSSL_ENCODER_CTX
*ctx
= NULL
;
1168 int ret
= -2; /* default to unsupported */
1170 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1173 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1175 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1176 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1177 OSSL_ENCODER_CTX_free(ctx
);
1182 /* legacy fallback */
1183 if (legacy_print
!= NULL
)
1184 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1186 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1189 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1193 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1194 int indent
, ASN1_PCTX
*pctx
)
1196 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1197 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1201 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1202 int indent
, ASN1_PCTX
*pctx
)
1204 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PRIVATE_KEY
, NULL
,
1205 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1209 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1210 int indent
, ASN1_PCTX
*pctx
)
1212 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1213 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1217 # ifndef OPENSSL_NO_STDIO
1218 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1219 int indent
, ASN1_PCTX
*pctx
)
1222 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1226 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1231 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1232 int indent
, ASN1_PCTX
*pctx
)
1235 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1239 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1244 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1245 int indent
, ASN1_PCTX
*pctx
)
1248 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1252 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1258 static void mdname2nid(const char *mdname
, void *data
)
1260 int *nid
= (int *)data
;
1262 if (*nid
!= NID_undef
)
1265 *nid
= OBJ_sn2nid(mdname
);
1266 if (*nid
== NID_undef
)
1267 *nid
= OBJ_ln2nid(mdname
);
1270 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1271 int arg1
, void *arg2
)
1273 if (pkey
->keymgmt
== NULL
)
1276 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
1278 char mdname
[80] = "";
1279 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1284 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1285 /* Make sure the MD is in the namemap if available */
1287 OSSL_NAMEMAP
*namemap
;
1288 int nid
= NID_undef
;
1290 (void)ERR_set_mark();
1291 md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1292 (void)ERR_pop_to_mark();
1293 namemap
= ossl_namemap_stored(libctx
);
1296 * The only reason to fetch the MD was to make sure it is in the
1297 * namemap. We can immediately free it.
1300 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1305 * We have the namemap number - now we need to find the
1308 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1319 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1321 if (pkey
->ameth
== NULL
)
1322 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1323 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1325 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1328 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1332 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1335 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1336 char *mdname
, size_t mdname_sz
)
1338 if (pkey
->ameth
== NULL
)
1339 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1344 int nid
= NID_undef
;
1345 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1346 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1349 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1354 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1357 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1358 gname
, gname_sz
, gname_len
);
1361 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY
*pkey
, OSSL_LIB_CTX
*libctx
,
1362 const char *name
, const char *propq
)
1365 EVP_MD_CTX
*ctx
= NULL
;
1367 if ((ctx
= EVP_MD_CTX_new()) == NULL
)
1371 rv
= EVP_DigestSignInit_ex(ctx
, NULL
, name
, libctx
,
1375 EVP_MD_CTX_free(ctx
);
1379 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1384 if (evp_pkey_is_provided(pkey
))
1386 EVP_PKEY_set_octet_string_param(pkey
,
1387 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1388 (unsigned char *)pub
, publen
);
1390 if (publen
> INT_MAX
)
1392 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1393 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, publen
,
1399 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1405 if (evp_pkey_is_provided(pkey
)) {
1406 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1410 * We know that this is going to fail, but it will give us a size
1413 EVP_PKEY_get_octet_string_param(pkey
,
1414 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1415 NULL
, 0, &return_size
);
1416 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1420 buf
= OPENSSL_malloc(return_size
);
1424 if (!EVP_PKEY_get_octet_string_param(pkey
,
1425 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1426 buf
, return_size
, NULL
)) {
1435 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1441 #endif /* FIPS_MODULE */
1443 /*- All methods below can also be used in FIPS_MODULE */
1445 EVP_PKEY
*EVP_PKEY_new(void)
1447 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1452 ret
->type
= EVP_PKEY_NONE
;
1453 ret
->save_type
= EVP_PKEY_NONE
;
1455 if (!CRYPTO_NEW_REF(&ret
->references
, 1))
1458 ret
->lock
= CRYPTO_THREAD_lock_new();
1459 if (ret
->lock
== NULL
) {
1460 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1465 ret
->save_parameters
= 1;
1466 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1467 ERR_raise(ERR_LIB_EVP
, ERR_R_CRYPTO_LIB
);
1474 CRYPTO_FREE_REF(&ret
->references
);
1475 CRYPTO_THREAD_lock_free(ret
->lock
);
1481 * Setup a public key management method.
1483 * For legacy keys, either |type| or |str| is expected to have the type
1484 * information. In this case, the setup consists of finding an ASN1 method
1485 * and potentially an ENGINE, and setting those fields in |pkey|.
1487 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1488 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1490 * If pkey is NULL just return 1 or 0 if the key management method exists.
1493 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1494 int len
, EVP_KEYMGMT
*keymgmt
)
1497 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1498 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1502 * The setups can't set both legacy and provider side methods.
1505 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1506 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1507 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1515 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1517 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1519 evp_pkey_free_it(pkey
);
1522 * If key type matches and a method exists then this lookup has
1523 * succeeded once so just indicate success.
1525 if (pkey
->type
!= EVP_PKEY_NONE
1526 && type
== pkey
->save_type
1527 && pkey
->ameth
!= NULL
)
1529 # ifndef OPENSSL_NO_ENGINE
1530 /* If we have ENGINEs release them */
1531 ENGINE_finish(pkey
->engine
);
1532 pkey
->engine
= NULL
;
1533 ENGINE_finish(pkey
->pmeth_engine
);
1534 pkey
->pmeth_engine
= NULL
;
1540 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1541 else if (type
!= EVP_PKEY_NONE
)
1542 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1543 # ifndef OPENSSL_NO_ENGINE
1544 if (pkey
== NULL
&& eptr
!= NULL
)
1554 check
= check
&& ameth
== NULL
;
1556 check
= check
&& keymgmt
== NULL
;
1558 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1563 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1564 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1568 pkey
->keymgmt
= keymgmt
;
1570 pkey
->save_type
= type
;
1575 * If the internal "origin" key is provider side, don't save |ameth|.
1576 * The main reason is that |ameth| is one factor to detect that the
1577 * internal "origin" key is a legacy one.
1579 if (keymgmt
== NULL
)
1580 pkey
->ameth
= ameth
;
1583 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1584 * for any key type that has a legacy implementation, regardless of
1585 * if the internal key is a legacy or a provider side one. When
1586 * there is no legacy implementation for the key, the type becomes
1587 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1588 * with functions that expect legacy internal keys.
1590 if (ameth
!= NULL
) {
1591 if (type
== EVP_PKEY_NONE
)
1592 pkey
->type
= ameth
->pkey_id
;
1594 pkey
->type
= EVP_PKEY_KEYMGMT
;
1596 # ifndef OPENSSL_NO_ENGINE
1597 if (eptr
== NULL
&& e
!= NULL
&& !ENGINE_init(e
)) {
1598 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1609 static void find_ameth(const char *name
, void *data
)
1611 const char **str
= data
;
1614 * The error messages from pkey_set_type() are uninteresting here,
1619 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, strlen(name
),
1623 else if (str
[1] == NULL
)
1631 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1634 # define EVP_PKEY_TYPE_STR str[0]
1635 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1637 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1638 * Ideally, only one should be found. If two (or more) are found, the
1639 * match is ambiguous. This should never happen, but...
1641 const char *str
[2] = { NULL
, NULL
};
1643 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1644 || str
[1] != NULL
) {
1645 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1649 # define EVP_PKEY_TYPE_STR NULL
1650 # define EVP_PKEY_TYPE_STRLEN -1
1652 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1653 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1656 #undef EVP_PKEY_TYPE_STR
1657 #undef EVP_PKEY_TYPE_STRLEN
1660 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1664 if (CRYPTO_UP_REF(&pkey
->references
, &i
) <= 0)
1667 REF_PRINT_COUNT("EVP_PKEY", pkey
);
1668 REF_ASSERT_ISNT(i
< 2);
1669 return ((i
> 1) ? 1 : 0);
1673 EVP_PKEY
*EVP_PKEY_dup(EVP_PKEY
*pkey
)
1678 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
1682 if ((dup_pk
= EVP_PKEY_new()) == NULL
)
1685 if (evp_pkey_is_blank(pkey
))
1688 if (evp_pkey_is_provided(pkey
)) {
1689 if (!evp_keymgmt_util_copy(dup_pk
, pkey
,
1690 OSSL_KEYMGMT_SELECT_ALL
))
1695 if (evp_pkey_is_legacy(pkey
)) {
1696 const EVP_PKEY_ASN1_METHOD
*ameth
= pkey
->ameth
;
1698 if (ameth
== NULL
|| ameth
->copy
== NULL
) {
1699 if (pkey
->pkey
.ptr
== NULL
/* empty key, just set type */
1700 && EVP_PKEY_set_type(dup_pk
, pkey
->type
) != 0)
1702 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_KEY_TYPE
);
1705 if (!ameth
->copy(dup_pk
, pkey
))
1712 /* copy auxiliary data */
1713 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
,
1714 &dup_pk
->ex_data
, &pkey
->ex_data
))
1717 if (pkey
->attributes
!= NULL
) {
1718 if ((dup_pk
->attributes
= ossl_x509at_dup(pkey
->attributes
)) == NULL
)
1723 EVP_PKEY_free(dup_pk
);
1727 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1729 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1730 ENGINE
*tmpe
= NULL
;
1732 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1733 ameth
= EVP_PKEY_asn1_find(&tmpe
, x
->type
);
1735 if (ameth
!= NULL
) {
1736 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1738 * We should never have both a legacy origin key, and a key in the
1741 assert(x
->pkey
.ptr
== NULL
);
1743 * For the purposes of freeing we make the legacy cache look like
1744 * a legacy origin key.
1746 x
->pkey
= x
->legacy_cache_pkey
;
1747 x
->legacy_cache_pkey
.ptr
= NULL
;
1749 if (ameth
->pkey_free
!= NULL
)
1750 ameth
->pkey_free(x
);
1753 # ifndef OPENSSL_NO_ENGINE
1754 ENGINE_finish(tmpe
);
1755 ENGINE_finish(x
->engine
);
1757 ENGINE_finish(x
->pmeth_engine
);
1758 x
->pmeth_engine
= NULL
;
1761 #endif /* FIPS_MODULE */
1763 static void evp_pkey_free_it(EVP_PKEY
*x
)
1765 /* internal function; x is never NULL */
1766 evp_keymgmt_util_clear_operation_cache(x
);
1768 evp_pkey_free_legacy(x
);
1771 if (x
->keymgmt
!= NULL
) {
1772 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1773 EVP_KEYMGMT_free(x
->keymgmt
);
1777 x
->type
= EVP_PKEY_NONE
;
1780 void EVP_PKEY_free(EVP_PKEY
*x
)
1787 CRYPTO_DOWN_REF(&x
->references
, &i
);
1788 REF_PRINT_COUNT("EVP_PKEY", x
);
1791 REF_ASSERT_ISNT(i
< 0);
1792 evp_pkey_free_it(x
);
1794 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1796 CRYPTO_THREAD_lock_free(x
->lock
);
1797 CRYPTO_FREE_REF(&x
->references
);
1799 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1804 int EVP_PKEY_get_size(const EVP_PKEY
*pkey
)
1809 size
= pkey
->cache
.size
;
1811 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1812 size
= pkey
->ameth
->pkey_size(pkey
);
1815 return size
< 0 ? 0 : size
;
1818 const char *EVP_PKEY_get0_description(const EVP_PKEY
*pkey
)
1820 if (!evp_pkey_is_assigned(pkey
))
1823 if (evp_pkey_is_provided(pkey
) && pkey
->keymgmt
->description
!= NULL
)
1824 return pkey
->keymgmt
->description
;
1826 if (pkey
->ameth
!= NULL
)
1827 return pkey
->ameth
->info
;
1832 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1833 EVP_KEYMGMT
**keymgmt
,
1834 const char *propquery
)
1836 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1837 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1838 int selection
= OSSL_KEYMGMT_SELECT_ALL
;
1839 void *keydata
= NULL
;
1845 /* No key data => nothing to export */
1848 check
= check
&& pk
->pkey
.ptr
== NULL
;
1850 check
= check
&& pk
->keydata
== NULL
;
1855 if (pk
->pkey
.ptr
!= NULL
) {
1857 * If the legacy key doesn't have an dirty counter or export function,
1860 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1865 if (keymgmt
!= NULL
) {
1866 tmp_keymgmt
= *keymgmt
;
1871 * If no keymgmt was given or found, get a default keymgmt. We do so by
1872 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1874 if (tmp_keymgmt
== NULL
) {
1875 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1879 allocated_keymgmt
= tmp_keymgmt
= ctx
->keymgmt
;
1880 ctx
->keymgmt
= NULL
;
1881 EVP_PKEY_CTX_free(ctx
);
1884 /* If there's still no keymgmt to be had, give up */
1885 if (tmp_keymgmt
== NULL
)
1889 if (pk
->pkey
.ptr
!= NULL
) {
1893 * If the legacy "origin" hasn't changed since last time, we try
1894 * to find our keymgmt in the operation cache. If it has changed,
1895 * |i| remains zero, and we will clear the cache further down.
1897 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1898 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1900 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
,
1904 * If |tmp_keymgmt| is present in the operation cache, it means
1905 * that export doesn't need to be redone. In that case, we take
1906 * token copies of the cached pointers, to have token success
1909 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1910 keydata
= op
->keydata
;
1911 CRYPTO_THREAD_unlock(pk
->lock
);
1914 CRYPTO_THREAD_unlock(pk
->lock
);
1917 /* Make sure that the keymgmt key type matches the legacy NID */
1918 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
1921 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1924 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
->import
,
1925 libctx
, propquery
)) {
1926 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1932 * If the dirty counter changed since last time, then clear the
1933 * operation cache. In that case, we know that |i| is zero. Just
1934 * in case this is a re-export, we increment then decrement the
1935 * keymgmt reference counter.
1937 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1938 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1943 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1945 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
1946 && !evp_keymgmt_util_clear_operation_cache(pk
)) {
1947 CRYPTO_THREAD_unlock(pk
->lock
);
1948 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1950 EVP_KEYMGMT_free(tmp_keymgmt
);
1953 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1955 /* Check to make sure some other thread didn't get there first */
1956 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
, selection
);
1957 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1958 void *tmp_keydata
= op
->keydata
;
1960 CRYPTO_THREAD_unlock(pk
->lock
);
1961 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1962 keydata
= tmp_keydata
;
1966 /* Add the new export to the operation cache */
1967 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
,
1969 CRYPTO_THREAD_unlock(pk
->lock
);
1970 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1975 /* Synchronize the dirty count */
1976 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1978 CRYPTO_THREAD_unlock(pk
->lock
);
1981 #endif /* FIPS_MODULE */
1983 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
, selection
);
1987 * If nothing was exported, |tmp_keymgmt| might point at a freed
1988 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1989 * the caller either way in that case.
1991 if (keydata
== NULL
)
1994 if (keymgmt
!= NULL
&& tmp_keymgmt
!= NULL
) {
1995 *keymgmt
= tmp_keymgmt
;
1996 allocated_keymgmt
= NULL
;
1999 EVP_KEYMGMT_free(allocated_keymgmt
);
2004 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
2006 EVP_PKEY
*allocpkey
= NULL
;
2008 if (!ossl_assert(dest
!= NULL
))
2011 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
2012 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
2013 void *keydata
= src
->keydata
;
2014 int type
= src
->type
;
2015 const char *keytype
= NULL
;
2017 keytype
= EVP_KEYMGMT_get0_name(keymgmt
);
2020 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2021 * else in our code. If it's not one of the well known EVP_PKEY_xxx
2022 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2023 * The check is kept as a safety measure.
2025 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
2026 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
2027 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2032 /* Prefer the legacy key type name for error reporting */
2033 if (type
!= EVP_PKEY_KEYMGMT
)
2034 keytype
= OBJ_nid2sn(type
);
2036 /* Make sure we have a clean slate to copy into */
2037 if (*dest
== NULL
) {
2038 allocpkey
= *dest
= EVP_PKEY_new();
2039 if (*dest
== NULL
) {
2040 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2044 evp_pkey_free_it(*dest
);
2047 if (EVP_PKEY_set_type(*dest
, type
)) {
2048 /* If the key is typed but empty, we're done */
2049 if (keydata
== NULL
)
2052 if ((*dest
)->ameth
->import_from
== NULL
) {
2053 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
2054 "key type = %s", keytype
);
2057 * We perform the export in the same libctx as the keymgmt
2058 * that we are using.
2060 OSSL_LIB_CTX
*libctx
=
2061 ossl_provider_libctx(keymgmt
->prov
);
2062 EVP_PKEY_CTX
*pctx
=
2063 EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
2066 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
2069 && evp_keymgmt_export(keymgmt
, keydata
,
2070 OSSL_KEYMGMT_SELECT_ALL
,
2071 (*dest
)->ameth
->import_from
,
2073 /* Synchronize the dirty count */
2074 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
2076 EVP_PKEY_CTX_free(pctx
);
2079 EVP_PKEY_CTX_free(pctx
);
2082 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
2083 "key type = %s", keytype
);
2087 if (allocpkey
!= NULL
) {
2088 EVP_PKEY_free(allocpkey
);
2094 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
2096 EVP_PKEY
*tmp_copy
= NULL
;
2099 if (!ossl_assert(pk
!= NULL
))
2103 * If this isn't an assigned provider side key, we just use any existing
2104 * origin legacy key.
2106 if (!evp_pkey_is_assigned(pk
))
2108 if (!evp_pkey_is_provided(pk
))
2109 return pk
->pkey
.ptr
;
2111 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
2114 ret
= pk
->legacy_cache_pkey
.ptr
;
2116 if (!CRYPTO_THREAD_unlock(pk
->lock
))
2122 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
2125 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2128 /* Check again in case some other thread has updated it in the meantime */
2129 ret
= pk
->legacy_cache_pkey
.ptr
;
2131 /* Steal the legacy key reference from the temporary copy */
2132 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
2133 tmp_copy
->pkey
.ptr
= NULL
;
2136 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
2142 EVP_PKEY_free(tmp_copy
);
2146 #endif /* FIPS_MODULE */
2148 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
2152 OSSL_PARAM params
[2];
2153 unsigned char buffer
[2048];
2154 unsigned char *buf
= NULL
;
2157 if (key_name
== NULL
2161 memset(buffer
, 0, sizeof(buffer
));
2162 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
2163 params
[1] = OSSL_PARAM_construct_end();
2164 if (!EVP_PKEY_get_params(pkey
, params
)) {
2165 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
2167 buf_sz
= params
[0].return_size
;
2169 * If it failed because the buffer was too small then allocate the
2170 * required buffer size and retry.
2172 buf
= OPENSSL_zalloc(buf_sz
);
2175 params
[0].data
= buf
;
2176 params
[0].data_size
= buf_sz
;
2178 if (!EVP_PKEY_get_params(pkey
, params
))
2181 /* Fail if the param was not found */
2182 if (!OSSL_PARAM_modified(params
))
2184 ret
= OSSL_PARAM_get_BN(params
, bn
);
2187 if (OSSL_PARAM_modified(params
))
2188 OPENSSL_clear_free(buf
, buf_sz
);
2191 } else if (OSSL_PARAM_modified(params
)) {
2192 OPENSSL_cleanse(buffer
, params
[0].data_size
);
2197 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2198 unsigned char *buf
, size_t max_buf_sz
,
2201 OSSL_PARAM params
[2];
2202 int ret1
= 0, ret2
= 0;
2204 if (key_name
== NULL
)
2207 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2208 params
[1] = OSSL_PARAM_construct_end();
2209 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2210 ret2
= OSSL_PARAM_modified(params
);
2211 if (ret2
&& out_len
!= NULL
)
2212 *out_len
= params
[0].return_size
;
2213 return ret1
&& ret2
;
2216 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2217 char *str
, size_t max_buf_sz
,
2220 OSSL_PARAM params
[2];
2221 int ret1
= 0, ret2
= 0;
2223 if (key_name
== NULL
)
2226 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2227 params
[1] = OSSL_PARAM_construct_end();
2228 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2229 ret2
= OSSL_PARAM_modified(params
);
2230 if (ret2
&& out_len
!= NULL
)
2231 *out_len
= params
[0].return_size
;
2233 if (ret2
&& params
[0].return_size
== max_buf_sz
)
2234 /* There was no space for a NUL byte */
2236 /* Add a terminating NUL byte for good measure */
2237 if (ret2
&& str
!= NULL
)
2238 str
[params
[0].return_size
] = '\0';
2240 return ret1
&& ret2
;
2243 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2246 OSSL_PARAM params
[2];
2248 if (key_name
== NULL
)
2251 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2252 params
[1] = OSSL_PARAM_construct_end();
2253 return EVP_PKEY_get_params(pkey
, params
)
2254 && OSSL_PARAM_modified(params
);
2257 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2260 OSSL_PARAM params
[2];
2262 if (key_name
== NULL
)
2265 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2266 params
[1] = OSSL_PARAM_construct_end();
2267 return EVP_PKEY_get_params(pkey
, params
)
2268 && OSSL_PARAM_modified(params
);
2271 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2273 OSSL_PARAM params
[2];
2275 if (key_name
== NULL
)
2278 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2279 params
[1] = OSSL_PARAM_construct_end();
2280 return EVP_PKEY_set_params(pkey
, params
);
2283 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2285 OSSL_PARAM params
[2];
2287 if (key_name
== NULL
)
2290 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2291 params
[1] = OSSL_PARAM_construct_end();
2292 return EVP_PKEY_set_params(pkey
, params
);
2295 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2298 OSSL_PARAM params
[2];
2299 unsigned char buffer
[2048];
2302 if (key_name
== NULL
2305 || !evp_pkey_is_assigned(pkey
))
2308 bsize
= BN_num_bytes(bn
);
2309 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2312 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2314 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2315 params
[1] = OSSL_PARAM_construct_end();
2316 return EVP_PKEY_set_params(pkey
, params
);
2319 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2322 OSSL_PARAM params
[2];
2324 if (key_name
== NULL
)
2327 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2328 params
[1] = OSSL_PARAM_construct_end();
2329 return EVP_PKEY_set_params(pkey
, params
);
2332 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2333 const unsigned char *buf
, size_t bsize
)
2335 OSSL_PARAM params
[2];
2337 if (key_name
== NULL
)
2340 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2341 (unsigned char *)buf
, bsize
);
2342 params
[1] = OSSL_PARAM_construct_end();
2343 return EVP_PKEY_set_params(pkey
, params
);
2346 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2348 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2349 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2353 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2356 if (evp_pkey_is_provided(pkey
)) {
2358 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2362 * We will hopefully never find the need to set individual data in
2363 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2364 * sure. This bit of code can be enabled if we find the need. If
2365 * not, it can safely be removed when #legacy support is removed.
2368 else if (evp_pkey_is_legacy(pkey
)) {
2369 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2374 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2378 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2380 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2381 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2385 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2388 if (evp_pkey_is_provided(pkey
))
2389 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
) > 0;
2391 else if (evp_pkey_is_legacy(pkey
))
2392 return evp_pkey_get_params_to_ctrl(pkey
, params
) > 0;
2395 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2400 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2408 if (pkey
->keymgmt
== NULL
2409 || pkey
->keydata
== NULL
) {
2410 # ifndef OPENSSL_NO_EC
2411 /* Might work through the legacy route */
2412 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2417 return EC_KEY_get_conv_form(ec
);
2423 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2424 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2425 name
, sizeof(name
), &name_len
))
2428 if (strcmp(name
, "uncompressed") == 0)
2429 return POINT_CONVERSION_UNCOMPRESSED
;
2431 if (strcmp(name
, "compressed") == 0)
2432 return POINT_CONVERSION_COMPRESSED
;
2434 if (strcmp(name
, "hybrid") == 0)
2435 return POINT_CONVERSION_HYBRID
;
2440 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2448 if (pkey
->keymgmt
== NULL
2449 || pkey
->keydata
== NULL
) {
2450 # ifndef OPENSSL_NO_EC
2451 /* Might work through the legacy route */
2452 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2453 const EC_GROUP
*grp
;
2457 grp
= EC_KEY_get0_group(ec
);
2461 return EC_GROUP_get_field_type(grp
);
2467 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2468 fstr
, sizeof(fstr
), &fstrlen
))
2471 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2472 return NID_X9_62_prime_field
;
2473 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2474 return NID_X9_62_characteristic_two_field
;