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/x509.h>
26 #include <openssl/rsa.h>
27 #include <openssl/dsa.h>
28 #include <openssl/dh.h>
29 #include <openssl/ec.h>
30 #include <openssl/cmac.h>
31 #include <openssl/engine.h>
32 #include <openssl/params.h>
33 #include <openssl/param_build.h>
34 #include <openssl/encoder.h>
35 #include <openssl/core_names.h>
37 #include "internal/ffc.h"
38 #include "crypto/asn1.h"
39 #include "crypto/evp.h"
40 #include "crypto/ec.h"
41 #include "crypto/ecx.h"
42 #include "internal/provider.h"
43 #include "evp_local.h"
45 #include "crypto/ec.h"
47 #include "e_os.h" /* strcasecmp on Windows */
49 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
50 int len
, EVP_KEYMGMT
*keymgmt
);
51 static void evp_pkey_free_it(EVP_PKEY
*key
);
55 /* The type of parameters selected in key parameter functions */
56 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
58 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
63 size
= pkey
->cache
.bits
;
64 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
65 size
= pkey
->ameth
->pkey_bits(pkey
);
67 return size
< 0 ? 0 : size
;
70 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
75 size
= pkey
->cache
.security_bits
;
76 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
77 size
= pkey
->ameth
->pkey_security_bits(pkey
);
79 return size
< 0 ? 0 : size
;
82 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
84 # ifndef OPENSSL_NO_DSA
85 if (pkey
->type
== EVP_PKEY_DSA
) {
86 int ret
= pkey
->save_parameters
;
89 pkey
->save_parameters
= mode
;
93 # ifndef OPENSSL_NO_EC
94 if (pkey
->type
== EVP_PKEY_EC
) {
95 int ret
= pkey
->save_parameters
;
98 pkey
->save_parameters
= mode
;
105 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
107 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
110 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
112 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
115 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
118 * Clean up legacy stuff from this function when legacy support is gone.
121 EVP_PKEY
*downgraded_from
= NULL
;
125 * If |to| is a legacy key and |from| isn't, we must make a downgraded
126 * copy of |from|. If that fails, this function fails.
128 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
129 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
131 from
= downgraded_from
;
135 * Make sure |to| is typed. Content is less important at this early
138 * 1. If |to| is untyped, assign |from|'s key type to it.
139 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
140 * (|from| was already downgraded above)
142 * If |to| is a provided key, there's nothing more to do here, functions
143 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
144 * further down help us find out if they are the same or not.
146 if (evp_pkey_is_blank(to
)) {
147 if (evp_pkey_is_legacy(from
)) {
148 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
151 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
154 } else if (evp_pkey_is_legacy(to
)) {
155 if (to
->type
!= from
->type
) {
156 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
161 if (EVP_PKEY_missing_parameters(from
)) {
162 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
166 if (!EVP_PKEY_missing_parameters(to
)) {
167 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
170 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
174 /* For purely provided keys, we just call the keymgmt utility */
175 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
176 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
181 * If |to| is provided, we know that |from| is legacy at this point.
182 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
183 * to copy the appropriate data to |to|'s keydata.
185 if (to
->keymgmt
!= NULL
) {
186 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
188 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
192 * If we get a NULL, it could be an internal error, or it could be
193 * that there's a key mismatch. We're pretending the latter...
195 if (from_keydata
== NULL
)
196 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
198 ok
= evp_keymgmt_copy(to
->keymgmt
, to
->keydata
, from_keydata
,
203 /* Both keys are legacy */
204 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
205 ok
= from
->ameth
->param_copy(to
, from
);
207 EVP_PKEY_free(downgraded_from
);
211 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
214 if (pkey
->keymgmt
!= NULL
)
215 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
216 else if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
217 return pkey
->ameth
->param_missing(pkey
);
223 * This function is called for any mixture of keys except pure legacy pair.
224 * When legacy keys are gone, we replace a call to this functions with
225 * a call to evp_keymgmt_util_match().
227 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
230 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
231 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
233 /* If none of them are provided, this function shouldn't have been called */
234 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
237 /* For purely provided keys, we just call the keymgmt utility */
238 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
239 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
242 * At this point, one of them is provided, the other not. This allows
243 * us to compare types using legacy NIDs.
245 if (evp_pkey_is_legacy(a
)
246 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
247 return -1; /* not the same key type */
248 if (evp_pkey_is_legacy(b
)
249 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
250 return -1; /* not the same key type */
253 * We've determined that they both are the same keytype, so the next
254 * step is to do a bit of cross export to ensure we have keydata for
255 * both keys in the same keymgmt.
257 keymgmt1
= a
->keymgmt
;
258 keydata1
= a
->keydata
;
259 keymgmt2
= b
->keymgmt
;
260 keydata2
= b
->keydata
;
262 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
264 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
265 if (tmp_keydata
!= NULL
) {
267 keydata1
= tmp_keydata
;
270 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
272 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
273 if (tmp_keydata
!= NULL
) {
275 keydata2
= tmp_keydata
;
279 /* If we still don't have matching keymgmt implementations, we give up */
280 if (keymgmt1
!= keymgmt2
)
283 /* If the keymgmt implementations are NULL, the export failed */
284 if (keymgmt1
== NULL
)
287 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
290 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
292 return EVP_PKEY_parameters_eq(a
, b
);
295 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
298 * This will just call evp_keymgmt_util_match when legacy support
302 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
303 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
305 /* All legacy keys */
306 if (a
->type
!= b
->type
)
308 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
309 return a
->ameth
->param_cmp(a
, b
);
313 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
315 return EVP_PKEY_eq(a
, b
);
318 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
321 * This will just call evp_keymgmt_util_match when legacy support
325 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
326 return evp_pkey_cmp_any(a
, b
, (SELECT_PARAMETERS
327 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY
));
329 /* All legacy keys */
330 if (a
->type
!= b
->type
)
333 if (a
->ameth
!= NULL
) {
335 /* Compare parameters if the algorithm has them */
336 if (a
->ameth
->param_cmp
!= NULL
) {
337 ret
= a
->ameth
->param_cmp(a
, b
);
342 if (a
->ameth
->pub_cmp
!= NULL
)
343 return a
->ameth
->pub_cmp(a
, b
);
350 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
355 const unsigned char *key
,
359 EVP_PKEY
*pkey
= NULL
;
360 EVP_PKEY_CTX
*ctx
= NULL
;
361 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
364 # ifndef OPENSSL_NO_ENGINE
365 /* Check if there is an Engine for this type */
370 ameth
= EVP_PKEY_asn1_find_str(&tmpe
, strtype
, -1);
371 else if (nidtype
!= EVP_PKEY_NONE
)
372 ameth
= EVP_PKEY_asn1_find(&tmpe
, nidtype
);
374 /* If tmpe is NULL then no engine is claiming to support this type */
382 if (e
== NULL
&& ameth
== NULL
) {
384 * No engine is claiming to support this type, so lets see if we have
387 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
388 strtype
!= NULL
? strtype
389 : OBJ_nid2sn(nidtype
),
393 /* May fail if no provider available */
395 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
396 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
398 ERR_clear_last_mark();
399 params
[0] = OSSL_PARAM_construct_octet_string(
400 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
401 : OSSL_PKEY_PARAM_PUB_KEY
,
404 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
405 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
409 EVP_PKEY_CTX_free(ctx
);
414 /* else not supported so fallback to legacy */
417 /* Legacy code path */
419 pkey
= EVP_PKEY_new();
421 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
425 if (!pkey_set_type(pkey
, e
, nidtype
, strtype
, -1, NULL
)) {
426 /* EVPerr already called */
430 if (!ossl_assert(pkey
->ameth
!= NULL
))
434 if (pkey
->ameth
->set_priv_key
== NULL
) {
435 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
439 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
440 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
444 if (pkey
->ameth
->set_pub_key
== NULL
) {
445 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
449 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
450 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
461 EVP_PKEY_CTX_free(ctx
);
465 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
468 const unsigned char *priv
, size_t len
)
470 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, priv
,
474 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
475 const unsigned char *priv
,
478 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, priv
, len
, 1);
481 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
482 const char *keytype
, const char *propq
,
483 const unsigned char *pub
, size_t len
)
485 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, pub
,
489 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
490 const unsigned char *pub
,
493 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, pub
, len
, 0);
496 struct raw_key_details_st
503 static OSSL_CALLBACK get_raw_key_details
;
504 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
506 const OSSL_PARAM
*p
= NULL
;
507 struct raw_key_details_st
*raw_key
= arg
;
509 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
510 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
512 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
513 SIZE_MAX
, raw_key
->len
);
514 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
515 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
517 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
518 SIZE_MAX
, raw_key
->len
);
524 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
527 if (pkey
->keymgmt
!= NULL
) {
528 struct raw_key_details_st raw_key
;
530 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
532 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
534 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
535 get_raw_key_details
, &raw_key
);
538 if (pkey
->ameth
== NULL
) {
539 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
543 if (pkey
->ameth
->get_priv_key
== NULL
) {
544 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
548 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
549 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
556 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
559 if (pkey
->keymgmt
!= NULL
) {
560 struct raw_key_details_st raw_key
;
562 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
564 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
566 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
567 get_raw_key_details
, &raw_key
);
570 if (pkey
->ameth
== NULL
) {
571 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
575 if (pkey
->ameth
->get_pub_key
== NULL
) {
576 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
580 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
581 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
588 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
589 const char *cipher_name
,
590 const EVP_CIPHER
*cipher
,
591 OSSL_LIB_CTX
*libctx
,
592 const char *propq
, ENGINE
*e
)
594 # ifndef OPENSSL_NO_CMAC
595 # ifndef OPENSSL_NO_ENGINE
596 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
598 OSSL_PARAM params
[5], *p
= params
;
599 EVP_PKEY
*pkey
= NULL
;
603 cipher_name
= EVP_CIPHER_name(cipher
);
605 if (cipher_name
== NULL
) {
606 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
610 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
614 if (!EVP_PKEY_fromdata_init(ctx
)) {
615 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
619 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
621 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
622 (char *)cipher_name
, 0);
624 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
626 # ifndef OPENSSL_NO_ENGINE
627 if (engine_id
!= NULL
)
628 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE
,
629 (char *)engine_id
, 0);
631 *p
= OSSL_PARAM_construct_end();
633 if (!EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
)) {
634 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
639 EVP_PKEY_CTX_free(ctx
);
643 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
648 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
649 size_t len
, const EVP_CIPHER
*cipher
)
651 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
, e
);
654 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
656 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
659 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
661 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
664 # ifndef OPENSSL_NO_DEPRECATED_3_0
665 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
667 if (!evp_pkey_is_legacy(pkey
)) {
668 const char *name
= OBJ_nid2sn(type
);
670 if (name
!= NULL
&& EVP_PKEY_is_a(pkey
, name
))
673 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
677 if (pkey
->type
== type
) {
678 return 1; /* it already is that type */
682 * The application is requesting to alias this to a different pkey type,
683 * but not one that resolves to the base type.
685 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
686 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
695 # ifndef OPENSSL_NO_ENGINE
696 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
699 if (!ENGINE_init(e
)) {
700 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
703 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
705 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
709 ENGINE_finish(pkey
->pmeth_engine
);
710 pkey
->pmeth_engine
= e
;
714 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
720 # ifndef OPENSSL_NO_DEPRECATED_3_0
721 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
725 # ifndef OPENSSL_NO_EC
726 if ((key
!= NULL
) && (EVP_PKEY_type(type
) == EVP_PKEY_EC
)) {
727 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
729 if (group
!= NULL
&& EC_GROUP_get_curve_name(group
) == NID_sm2
)
730 alias
= EVP_PKEY_SM2
;
734 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
736 if (!EVP_PKEY_set_alias_type(pkey
, alias
))
738 pkey
->pkey
.ptr
= key
;
739 return (key
!= NULL
);
743 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
748 if (!evp_pkey_is_provided(pkey
))
749 return pkey
->pkey
.ptr
;
754 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
756 const ASN1_OCTET_STRING
*os
= NULL
;
757 if (pkey
->type
!= EVP_PKEY_HMAC
) {
758 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
761 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
769 # ifndef OPENSSL_NO_POLY1305
770 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
772 const ASN1_OCTET_STRING
*os
= NULL
;
773 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
774 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
777 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
786 # ifndef OPENSSL_NO_SIPHASH
787 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
789 const ASN1_OCTET_STRING
*os
= NULL
;
791 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
792 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
795 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
804 # ifndef OPENSSL_NO_DSA
805 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
807 if (pkey
->type
!= EVP_PKEY_DSA
) {
808 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
811 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
814 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
816 return evp_pkey_get0_DSA_int(pkey
);
819 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
821 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
826 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
828 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
834 # endif /* OPENSSL_NO_DSA */
836 # ifndef OPENSSL_NO_EC
837 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
839 if (EVP_PKEY_base_id(pkey
) != type
) {
840 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
843 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
846 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
848 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
850 ossl_ecx_key_up_ref(ret
);
854 # define IMPLEMENT_ECX_VARIANT(NAME) \
855 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
857 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
859 IMPLEMENT_ECX_VARIANT(X25519
)
860 IMPLEMENT_ECX_VARIANT(X448
)
861 IMPLEMENT_ECX_VARIANT(ED25519
)
862 IMPLEMENT_ECX_VARIANT(ED448
)
866 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
868 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
870 int type
= DH_get0_q(key
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
871 int ret
= EVP_PKEY_assign(pkey
, type
, key
);
878 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
880 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
881 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
884 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
887 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
889 return evp_pkey_get0_DH_int(pkey
);
892 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
894 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
902 int EVP_PKEY_type(int type
)
905 const EVP_PKEY_ASN1_METHOD
*ameth
;
907 ameth
= EVP_PKEY_asn1_find(&e
, type
);
909 ret
= ameth
->pkey_id
;
912 # ifndef OPENSSL_NO_ENGINE
918 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
923 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
925 return EVP_PKEY_type(pkey
->type
);
929 * These hard coded cases are pure hackery to get around the fact
930 * that names in crypto/objects/objects.txt are a mess. There is
931 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
932 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
933 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
934 * "DSA" is accurate... but still, better be safe and hard-code
935 * names that we know.
936 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
937 * EVP_PKEY_EC, because of aliasing.
938 * This should be cleaned away along with all other #legacy support.
940 static const OSSL_ITEM standard_name2type
[] = {
941 { EVP_PKEY_RSA
, "RSA" },
942 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
943 { EVP_PKEY_EC
, "EC" },
944 { EVP_PKEY_ED25519
, "ED25519" },
945 { EVP_PKEY_ED448
, "ED448" },
946 { EVP_PKEY_X25519
, "X25519" },
947 { EVP_PKEY_X448
, "X448" },
948 { EVP_PKEY_SM2
, "SM2" },
949 { EVP_PKEY_DH
, "DH" },
950 { EVP_PKEY_DHX
, "X9.42 DH" },
951 { EVP_PKEY_DHX
, "DHX" },
952 { EVP_PKEY_DSA
, "DSA" },
955 int evp_pkey_name2type(const char *name
)
960 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
961 if (strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
962 return (int)standard_name2type
[i
].id
;
965 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
967 return EVP_PKEY_type(OBJ_ln2nid(name
));
970 const char *evp_pkey_type2name(int type
)
974 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
975 if (type
== (int)standard_name2type
[i
].id
)
976 return standard_name2type
[i
].ptr
;
979 return OBJ_nid2sn(type
);
982 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
984 if (pkey
->keymgmt
== NULL
) {
985 int type
= evp_pkey_name2type(name
);
987 return pkey
->type
== type
;
989 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
992 int EVP_PKEY_typenames_do_all(const EVP_PKEY
*pkey
,
993 void (*fn
)(const char *name
, void *data
),
996 if (!evp_pkey_is_typed(pkey
))
999 if (!evp_pkey_is_provided(pkey
)) {
1000 const char *name
= OBJ_nid2sn(EVP_PKEY_id(pkey
));
1005 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1008 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1010 if (pkey
->keymgmt
== NULL
) {
1011 switch (EVP_PKEY_base_id(pkey
)) {
1014 # ifndef OPENSSL_NO_DSA
1018 # ifndef OPENSSL_NO_EC
1019 case EVP_PKEY_ED25519
:
1020 case EVP_PKEY_ED448
:
1022 case EVP_PKEY_EC
: /* Including SM2 */
1023 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1029 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_provider(pkey
->keymgmt
);
1030 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1031 const char *supported_sig
=
1032 pkey
->keymgmt
->query_operation_name
!= NULL
1033 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
1034 : evp_first_name(prov
, pkey
->keymgmt
->name_id
);
1035 EVP_SIGNATURE
*signature
= NULL
;
1037 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
1038 if (signature
!= NULL
) {
1039 EVP_SIGNATURE_free(signature
);
1046 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1048 BIO_set_indent(*out
, saved_indent
);
1050 BIO
*next
= BIO_pop(*out
);
1058 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1064 long i
= BIO_get_indent(*out
);
1066 *saved_indent
= (i
< 0 ? 0 : i
);
1067 if (BIO_set_indent(*out
, indent
) <= 0) {
1068 if ((*out
= BIO_push(BIO_new(BIO_f_prefix()), *out
)) == NULL
)
1072 if (BIO_set_indent(*out
, indent
) <= 0) {
1073 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1080 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1083 return BIO_indent(out
, indent
, 128)
1084 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1085 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
1088 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1089 int selection
/* For provided encoding */,
1090 const char *propquery
/* For provided encoding */,
1091 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1092 int indent
, ASN1_PCTX
*pctx
),
1093 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1097 OSSL_ENCODER_CTX
*ctx
= NULL
;
1098 int ret
= -2; /* default to unsupported */
1100 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1103 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1105 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1106 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1107 OSSL_ENCODER_CTX_free(ctx
);
1112 /* legacy fallback */
1113 if (legacy_print
!= NULL
)
1114 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1116 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1119 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1123 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1124 int indent
, ASN1_PCTX
*pctx
)
1126 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1127 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1131 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1132 int indent
, ASN1_PCTX
*pctx
)
1134 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEYPAIR
, NULL
,
1135 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1139 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1140 int indent
, ASN1_PCTX
*pctx
)
1142 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1143 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1147 # ifndef OPENSSL_NO_STDIO
1148 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1149 int indent
, ASN1_PCTX
*pctx
)
1152 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1156 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1161 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1162 int indent
, ASN1_PCTX
*pctx
)
1165 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1169 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1174 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1175 int indent
, ASN1_PCTX
*pctx
)
1178 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1182 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1188 static void mdname2nid(const char *mdname
, void *data
)
1190 int *nid
= (int *)data
;
1192 if (*nid
!= NID_undef
)
1195 *nid
= OBJ_sn2nid(mdname
);
1196 if (*nid
== NID_undef
)
1197 *nid
= OBJ_ln2nid(mdname
);
1200 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1201 int arg1
, void *arg2
)
1203 if (pkey
->keymgmt
== NULL
)
1206 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
1208 char mdname
[80] = "";
1209 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1214 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1215 /* Make sure the MD is in the namemap if available */
1216 EVP_MD
*md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1217 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
1218 int nid
= NID_undef
;
1221 * The only reason to fetch the MD was to make sure it is in the
1222 * namemap. We can immediately free it.
1225 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1230 * We have the namemap number - now we need to find the
1233 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1244 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1246 if (pkey
->ameth
== NULL
)
1247 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1248 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1250 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1253 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1255 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1258 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1259 char *mdname
, size_t mdname_sz
)
1261 if (pkey
->ameth
== NULL
)
1262 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1267 int nid
= NID_undef
;
1268 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1269 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1272 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1277 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1280 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1281 gname
, gname_sz
, gname_len
);
1284 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
1286 int rv
, default_nid
;
1288 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
1291 * If there is a mandatory default digest and this isn't it, then
1292 * the answer is 'no'.
1294 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
1296 return (nid
== default_nid
);
1297 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1304 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1307 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
1309 EVP_PKEY_set_octet_string_param(pkey
,
1310 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1311 (unsigned char *)pub
, publen
);
1313 if (publen
> INT_MAX
)
1315 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1316 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, publen
,
1322 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1326 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
1327 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1330 * We know that this is going to fail, but it will give us a size
1333 EVP_PKEY_get_octet_string_param(pkey
,
1334 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1335 NULL
, 0, &return_size
);
1336 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1339 *ppub
= OPENSSL_malloc(return_size
);
1343 if (!EVP_PKEY_get_octet_string_param(pkey
,
1344 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1345 *ppub
, return_size
, NULL
))
1351 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1357 #endif /* FIPS_MODULE */
1359 /*- All methods below can also be used in FIPS_MODULE */
1361 EVP_PKEY
*EVP_PKEY_new(void)
1363 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1366 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1370 ret
->type
= EVP_PKEY_NONE
;
1371 ret
->save_type
= EVP_PKEY_NONE
;
1372 ret
->references
= 1;
1373 ret
->save_parameters
= 1;
1375 ret
->lock
= CRYPTO_THREAD_lock_new();
1376 if (ret
->lock
== NULL
) {
1377 EVPerr(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1382 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1383 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1390 CRYPTO_THREAD_lock_free(ret
->lock
);
1396 * Setup a public key management method.
1398 * For legacy keys, either |type| or |str| is expected to have the type
1399 * information. In this case, the setup consists of finding an ASN1 method
1400 * and potentially an ENGINE, and setting those fields in |pkey|.
1402 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1403 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1405 * If pkey is NULL just return 1 or 0 if the key management method exists.
1408 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1409 int len
, EVP_KEYMGMT
*keymgmt
)
1412 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1413 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1417 * The setups can't set both legacy and provider side methods.
1420 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1421 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1422 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1430 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1432 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1434 evp_pkey_free_it(pkey
);
1437 * If key type matches and a method exists then this lookup has
1438 * succeeded once so just indicate success.
1440 if (pkey
->type
!= EVP_PKEY_NONE
1441 && type
== pkey
->save_type
1442 && pkey
->ameth
!= NULL
)
1444 # ifndef OPENSSL_NO_ENGINE
1445 /* If we have ENGINEs release them */
1446 ENGINE_finish(pkey
->engine
);
1447 pkey
->engine
= NULL
;
1448 ENGINE_finish(pkey
->pmeth_engine
);
1449 pkey
->pmeth_engine
= NULL
;
1455 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1456 else if (type
!= EVP_PKEY_NONE
)
1457 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1458 # ifndef OPENSSL_NO_ENGINE
1459 if (pkey
== NULL
&& eptr
!= NULL
)
1469 check
= check
&& ameth
== NULL
;
1471 check
= check
&& keymgmt
== NULL
;
1473 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1478 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1479 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1483 pkey
->keymgmt
= keymgmt
;
1485 pkey
->save_type
= type
;
1490 * If the internal "origin" key is provider side, don't save |ameth|.
1491 * The main reason is that |ameth| is one factor to detect that the
1492 * internal "origin" key is a legacy one.
1494 if (keymgmt
== NULL
)
1495 pkey
->ameth
= ameth
;
1499 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1500 * for any key type that has a legacy implementation, regardless of
1501 * if the internal key is a legacy or a provider side one. When
1502 * there is no legacy implementation for the key, the type becomes
1503 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1504 * with functions that expect legacy internal keys.
1507 pkey
->type
= ameth
->pkey_id
;
1509 pkey
->type
= EVP_PKEY_KEYMGMT
;
1516 static void find_ameth(const char *name
, void *data
)
1518 const char **str
= data
;
1521 * The error messages from pkey_set_type() are uninteresting here,
1526 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, strlen(name
),
1530 else if (str
[1] == NULL
)
1538 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1541 # define EVP_PKEY_TYPE_STR str[0]
1542 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1544 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1545 * Ideally, only one should be found. If two (or more) are found, the
1546 * match is ambiguous. This should never happen, but...
1548 const char *str
[2] = { NULL
, NULL
};
1550 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1551 || str
[1] != NULL
) {
1552 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1556 # define EVP_PKEY_TYPE_STR NULL
1557 # define EVP_PKEY_TYPE_STRLEN -1
1559 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1560 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1563 #undef EVP_PKEY_TYPE_STR
1564 #undef EVP_PKEY_TYPE_STRLEN
1567 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1571 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
1574 REF_PRINT_COUNT("EVP_PKEY", pkey
);
1575 REF_ASSERT_ISNT(i
< 2);
1576 return ((i
> 1) ? 1 : 0);
1580 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1582 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1583 ENGINE
*tmpe
= NULL
;
1585 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1586 ameth
= EVP_PKEY_asn1_find(&tmpe
, x
->type
);
1588 if (ameth
!= NULL
) {
1589 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1591 * We should never have both a legacy origin key, and a key in the
1594 assert(x
->pkey
.ptr
== NULL
);
1596 * For the purposes of freeing we make the legacy cache look like
1597 * a legacy origin key.
1599 x
->pkey
= x
->legacy_cache_pkey
;
1600 x
->legacy_cache_pkey
.ptr
= NULL
;
1602 if (ameth
->pkey_free
!= NULL
)
1603 ameth
->pkey_free(x
);
1606 # ifndef OPENSSL_NO_ENGINE
1607 ENGINE_finish(tmpe
);
1608 ENGINE_finish(x
->engine
);
1610 ENGINE_finish(x
->pmeth_engine
);
1611 x
->pmeth_engine
= NULL
;
1614 #endif /* FIPS_MODULE */
1616 static void evp_pkey_free_it(EVP_PKEY
*x
)
1618 /* internal function; x is never NULL */
1619 evp_keymgmt_util_clear_operation_cache(x
, 1);
1621 evp_pkey_free_legacy(x
);
1624 if (x
->keymgmt
!= NULL
) {
1625 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1626 EVP_KEYMGMT_free(x
->keymgmt
);
1630 x
->type
= EVP_PKEY_NONE
;
1633 void EVP_PKEY_free(EVP_PKEY
*x
)
1640 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
1641 REF_PRINT_COUNT("EVP_PKEY", x
);
1644 REF_ASSERT_ISNT(i
< 0);
1645 evp_pkey_free_it(x
);
1647 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1649 CRYPTO_THREAD_lock_free(x
->lock
);
1651 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1656 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
1661 size
= pkey
->cache
.size
;
1663 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1664 size
= pkey
->ameth
->pkey_size(pkey
);
1667 return size
< 0 ? 0 : size
;
1670 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1671 EVP_KEYMGMT
**keymgmt
,
1672 const char *propquery
)
1674 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1675 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1676 void *keydata
= NULL
;
1682 /* No key data => nothing to export */
1685 check
= check
&& pk
->pkey
.ptr
== NULL
;
1687 check
= check
&& pk
->keydata
== NULL
;
1692 if (pk
->pkey
.ptr
!= NULL
) {
1694 * If the legacy key doesn't have an dirty counter or export function,
1697 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1702 if (keymgmt
!= NULL
) {
1703 tmp_keymgmt
= *keymgmt
;
1708 * If no keymgmt was given or found, get a default keymgmt. We do so by
1709 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1711 if (tmp_keymgmt
== NULL
) {
1712 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1714 tmp_keymgmt
= ctx
->keymgmt
;
1715 ctx
->keymgmt
= NULL
;
1716 EVP_PKEY_CTX_free(ctx
);
1719 /* If there's still no keymgmt to be had, give up */
1720 if (tmp_keymgmt
== NULL
)
1724 if (pk
->pkey
.ptr
!= NULL
) {
1728 * If the legacy "origin" hasn't changed since last time, we try
1729 * to find our keymgmt in the operation cache. If it has changed,
1730 * |i| remains zero, and we will clear the cache further down.
1732 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1733 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1735 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1738 * If |tmp_keymgmt| is present in the operation cache, it means
1739 * that export doesn't need to be redone. In that case, we take
1740 * token copies of the cached pointers, to have token success
1743 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1744 keydata
= op
->keydata
;
1745 CRYPTO_THREAD_unlock(pk
->lock
);
1748 CRYPTO_THREAD_unlock(pk
->lock
);
1751 /* Make sure that the keymgmt key type matches the legacy NID */
1752 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
1755 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1758 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
, libctx
, propquery
)) {
1759 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1765 * If the dirty counter changed since last time, then clear the
1766 * operation cache. In that case, we know that |i| is zero. Just
1767 * in case this is a re-export, we increment then decrement the
1768 * keymgmt reference counter.
1770 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1771 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1776 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1778 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
1779 && !evp_keymgmt_util_clear_operation_cache(pk
, 0)) {
1780 CRYPTO_THREAD_unlock(pk
->lock
);
1781 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1783 EVP_KEYMGMT_free(tmp_keymgmt
);
1786 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1788 /* Check to make sure some other thread didn't get there first */
1789 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1790 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1791 void *tmp_keydata
= op
->keydata
;
1793 CRYPTO_THREAD_unlock(pk
->lock
);
1794 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1795 keydata
= tmp_keydata
;
1799 /* Add the new export to the operation cache */
1800 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
)) {
1801 CRYPTO_THREAD_unlock(pk
->lock
);
1802 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1807 /* Synchronize the dirty count */
1808 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1810 CRYPTO_THREAD_unlock(pk
->lock
);
1813 #endif /* FIPS_MODULE */
1815 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
);
1819 * If nothing was exported, |tmp_keymgmt| might point at a freed
1820 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1821 * the caller either way in that case.
1823 if (keydata
== NULL
)
1826 if (keymgmt
!= NULL
)
1827 *keymgmt
= tmp_keymgmt
;
1829 EVP_KEYMGMT_free(allocated_keymgmt
);
1834 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
1836 if (!ossl_assert(dest
!= NULL
))
1839 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
1840 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
1841 void *keydata
= src
->keydata
;
1842 int type
= src
->type
;
1843 const char *keytype
= NULL
;
1845 keytype
= evp_first_name(EVP_KEYMGMT_provider(keymgmt
),
1849 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1850 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1851 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1852 * The check is kept as a safety measure.
1854 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
1855 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
1856 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1861 /* Prefer the legacy key type name for error reporting */
1862 if (type
!= EVP_PKEY_KEYMGMT
)
1863 keytype
= OBJ_nid2sn(type
);
1865 /* Make sure we have a clean slate to copy into */
1866 if (*dest
== NULL
) {
1867 *dest
= EVP_PKEY_new();
1868 if (*dest
== NULL
) {
1869 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1873 evp_pkey_free_it(*dest
);
1876 if (EVP_PKEY_set_type(*dest
, type
)) {
1877 /* If the key is typed but empty, we're done */
1878 if (keydata
== NULL
)
1881 if ((*dest
)->ameth
->import_from
== NULL
) {
1882 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
1883 "key type = %s", keytype
);
1886 * We perform the export in the same libctx as the keymgmt
1887 * that we are using.
1889 OSSL_LIB_CTX
*libctx
=
1890 ossl_provider_libctx(keymgmt
->prov
);
1891 EVP_PKEY_CTX
*pctx
=
1892 EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
1895 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1898 && evp_keymgmt_export(keymgmt
, keydata
,
1899 OSSL_KEYMGMT_SELECT_ALL
,
1900 (*dest
)->ameth
->import_from
,
1902 /* Synchronize the dirty count */
1903 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
1905 EVP_PKEY_CTX_free(pctx
);
1908 EVP_PKEY_CTX_free(pctx
);
1911 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
1912 "key type = %s", keytype
);
1919 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
1921 EVP_PKEY
*tmp_copy
= NULL
;
1924 if (!ossl_assert(pk
!= NULL
))
1928 * If this isn't an assigned provider side key, we just use any existing
1929 * origin legacy key.
1931 if (!evp_pkey_is_assigned(pk
))
1933 if (!evp_pkey_is_provided(pk
))
1934 return pk
->pkey
.ptr
;
1936 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1939 ret
= pk
->legacy_cache_pkey
.ptr
;
1941 if (!CRYPTO_THREAD_unlock(pk
->lock
))
1947 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
1950 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1953 /* Check again in case some other thread has updated it in the meantime */
1954 ret
= pk
->legacy_cache_pkey
.ptr
;
1956 /* Steal the legacy key reference from the temporary copy */
1957 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
1958 tmp_copy
->pkey
.ptr
= NULL
;
1961 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
1967 EVP_PKEY_free(tmp_copy
);
1971 #endif /* FIPS_MODULE */
1973 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
1977 OSSL_PARAM params
[2];
1978 unsigned char buffer
[2048];
1979 unsigned char *buf
= NULL
;
1982 if (key_name
== NULL
1986 memset(buffer
, 0, sizeof(buffer
));
1987 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
1988 params
[1] = OSSL_PARAM_construct_end();
1989 if (!EVP_PKEY_get_params(pkey
, params
)) {
1990 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
1992 buf_sz
= params
[0].return_size
;
1994 * If it failed because the buffer was too small then allocate the
1995 * required buffer size and retry.
1997 buf
= OPENSSL_zalloc(buf_sz
);
2000 params
[0].data
= buf
;
2001 params
[0].data_size
= buf_sz
;
2003 if (!EVP_PKEY_get_params(pkey
, params
))
2006 /* Fail if the param was not found */
2007 if (!OSSL_PARAM_modified(params
))
2009 ret
= OSSL_PARAM_get_BN(params
, bn
);
2015 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2016 unsigned char *buf
, size_t max_buf_sz
,
2019 OSSL_PARAM params
[2];
2020 int ret1
= 0, ret2
= 0;
2022 if (key_name
== NULL
)
2025 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2026 params
[1] = OSSL_PARAM_construct_end();
2027 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2028 ret2
= OSSL_PARAM_modified(params
);
2029 if (ret2
&& out_sz
!= NULL
)
2030 *out_sz
= params
[0].return_size
;
2031 return ret1
&& ret2
;
2034 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2035 char *str
, size_t max_buf_sz
,
2038 OSSL_PARAM params
[2];
2039 int ret1
= 0, ret2
= 0;
2041 if (key_name
== NULL
)
2044 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2045 params
[1] = OSSL_PARAM_construct_end();
2046 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2047 ret2
= OSSL_PARAM_modified(params
);
2048 if (ret2
&& out_sz
!= NULL
)
2049 *out_sz
= params
[0].return_size
;
2050 return ret1
&& ret2
;
2053 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2056 OSSL_PARAM params
[2];
2058 if (key_name
== NULL
)
2061 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2062 params
[1] = OSSL_PARAM_construct_end();
2063 return EVP_PKEY_get_params(pkey
, params
)
2064 && OSSL_PARAM_modified(params
);
2067 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2070 OSSL_PARAM params
[2];
2072 if (key_name
== NULL
)
2075 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2076 params
[1] = OSSL_PARAM_construct_end();
2077 return EVP_PKEY_get_params(pkey
, params
)
2078 && OSSL_PARAM_modified(params
);
2081 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2083 OSSL_PARAM params
[2];
2085 if (key_name
== NULL
)
2088 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2089 params
[1] = OSSL_PARAM_construct_end();
2090 return EVP_PKEY_set_params(pkey
, params
);
2093 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2095 OSSL_PARAM params
[2];
2097 if (key_name
== NULL
)
2100 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2101 params
[1] = OSSL_PARAM_construct_end();
2102 return EVP_PKEY_set_params(pkey
, params
);
2105 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2108 OSSL_PARAM params
[2];
2109 unsigned char buffer
[2048];
2112 if (key_name
== NULL
2115 || !evp_pkey_is_assigned(pkey
))
2118 bsize
= BN_num_bytes(bn
);
2119 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2122 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2124 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2125 params
[1] = OSSL_PARAM_construct_end();
2126 return EVP_PKEY_set_params(pkey
, params
);
2129 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2132 OSSL_PARAM params
[2];
2134 if (key_name
== NULL
)
2137 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2138 params
[1] = OSSL_PARAM_construct_end();
2139 return EVP_PKEY_set_params(pkey
, params
);
2142 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2143 const unsigned char *buf
, size_t bsize
)
2145 OSSL_PARAM params
[2];
2147 if (key_name
== NULL
)
2150 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2151 (unsigned char *)buf
, bsize
);
2152 params
[1] = OSSL_PARAM_construct_end();
2153 return EVP_PKEY_set_params(pkey
, params
);
2156 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2158 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2159 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2163 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2166 if (evp_pkey_is_provided(pkey
)) {
2168 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2172 * We will hopefully never find the need to set individual data in
2173 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2174 * sure. This bit of code can be enabled if we find the need. If
2175 * not, it can safely be removed when #legacy support is removed.
2178 else if (evp_pkey_is_legacy(pkey
)) {
2179 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2184 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2188 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2190 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2191 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2195 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2198 if (evp_pkey_is_provided(pkey
))
2199 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2201 else if (evp_pkey_is_legacy(pkey
))
2202 return evp_pkey_get_params_to_ctrl(pkey
, params
);
2205 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2210 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2218 if (pkey
->keymgmt
== NULL
2219 || pkey
->keydata
== NULL
) {
2220 # ifndef OPENSSL_NO_EC
2221 /* Might work through the legacy route */
2222 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2227 return EC_KEY_get_conv_form(ec
);
2233 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2234 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2235 name
, sizeof(name
), &name_len
))
2238 if (strcmp(name
, "uncompressed") == 0)
2239 return POINT_CONVERSION_UNCOMPRESSED
;
2241 if (strcmp(name
, "compressed") == 0)
2242 return POINT_CONVERSION_COMPRESSED
;
2244 if (strcmp(name
, "hybrid") == 0)
2245 return POINT_CONVERSION_HYBRID
;
2250 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2258 if (pkey
->keymgmt
== NULL
2259 || pkey
->keydata
== NULL
) {
2260 # ifndef OPENSSL_NO_EC
2261 /* Might work through the legacy route */
2262 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2263 const EC_GROUP
*grp
;
2267 grp
= EC_KEY_get0_group(ec
);
2271 return EC_GROUP_get_field_type(grp
);
2277 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2278 fstr
, sizeof(fstr
), &fstrlen
))
2281 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2282 return NID_X9_62_prime_field
;
2283 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2284 return NID_X9_62_characteristic_two_field
;