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 "crypto/x509.h"
43 #include "internal/provider.h"
44 #include "evp_local.h"
46 #include "crypto/ec.h"
48 #include "e_os.h" /* strcasecmp on Windows */
50 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
51 int len
, EVP_KEYMGMT
*keymgmt
);
52 static void evp_pkey_free_it(EVP_PKEY
*key
);
56 /* The type of parameters selected in key parameter functions */
57 # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
59 int EVP_PKEY_bits(const EVP_PKEY
*pkey
)
64 size
= pkey
->cache
.bits
;
65 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_bits
!= NULL
)
66 size
= pkey
->ameth
->pkey_bits(pkey
);
68 return size
< 0 ? 0 : size
;
71 int EVP_PKEY_security_bits(const EVP_PKEY
*pkey
)
76 size
= pkey
->cache
.security_bits
;
77 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_security_bits
!= NULL
)
78 size
= pkey
->ameth
->pkey_security_bits(pkey
);
80 return size
< 0 ? 0 : size
;
83 int EVP_PKEY_save_parameters(EVP_PKEY
*pkey
, int mode
)
85 # ifndef OPENSSL_NO_DSA
86 if (pkey
->type
== EVP_PKEY_DSA
) {
87 int ret
= pkey
->save_parameters
;
90 pkey
->save_parameters
= mode
;
94 # ifndef OPENSSL_NO_EC
95 if (pkey
->type
== EVP_PKEY_EC
) {
96 int ret
= pkey
->save_parameters
;
99 pkey
->save_parameters
= mode
;
106 int EVP_PKEY_set_ex_data(EVP_PKEY
*key
, int idx
, void *arg
)
108 return CRYPTO_set_ex_data(&key
->ex_data
, idx
, arg
);
111 void *EVP_PKEY_get_ex_data(const EVP_PKEY
*key
, int idx
)
113 return CRYPTO_get_ex_data(&key
->ex_data
, idx
);
116 int EVP_PKEY_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
119 * Clean up legacy stuff from this function when legacy support is gone.
122 EVP_PKEY
*downgraded_from
= NULL
;
126 * If |to| is a legacy key and |from| isn't, we must make a downgraded
127 * copy of |from|. If that fails, this function fails.
129 if (evp_pkey_is_legacy(to
) && evp_pkey_is_provided(from
)) {
130 if (!evp_pkey_copy_downgraded(&downgraded_from
, from
))
132 from
= downgraded_from
;
136 * Make sure |to| is typed. Content is less important at this early
139 * 1. If |to| is untyped, assign |from|'s key type to it.
140 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
141 * (|from| was already downgraded above)
143 * If |to| is a provided key, there's nothing more to do here, functions
144 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
145 * further down help us find out if they are the same or not.
147 if (evp_pkey_is_blank(to
)) {
148 if (evp_pkey_is_legacy(from
)) {
149 if (EVP_PKEY_set_type(to
, from
->type
) == 0)
152 if (EVP_PKEY_set_type_by_keymgmt(to
, from
->keymgmt
) == 0)
155 } else if (evp_pkey_is_legacy(to
)) {
156 if (to
->type
!= from
->type
) {
157 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
162 if (EVP_PKEY_missing_parameters(from
)) {
163 ERR_raise(ERR_LIB_EVP
, EVP_R_MISSING_PARAMETERS
);
167 if (!EVP_PKEY_missing_parameters(to
)) {
168 if (EVP_PKEY_parameters_eq(to
, from
) == 1)
171 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_PARAMETERS
);
175 /* For purely provided keys, we just call the keymgmt utility */
176 if (to
->keymgmt
!= NULL
&& from
->keymgmt
!= NULL
) {
177 ok
= evp_keymgmt_util_copy(to
, (EVP_PKEY
*)from
, SELECT_PARAMETERS
);
182 * If |to| is provided, we know that |from| is legacy at this point.
183 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy()
184 * to copy the appropriate data to |to|'s keydata.
186 if (to
->keymgmt
!= NULL
) {
187 EVP_KEYMGMT
*to_keymgmt
= to
->keymgmt
;
189 evp_pkey_export_to_provider((EVP_PKEY
*)from
, NULL
, &to_keymgmt
,
193 * If we get a NULL, it could be an internal error, or it could be
194 * that there's a key mismatch. We're pretending the latter...
196 if (from_keydata
== NULL
)
197 ERR_raise(ERR_LIB_EVP
, EVP_R_DIFFERENT_KEY_TYPES
);
199 ok
= evp_keymgmt_copy(to
->keymgmt
, to
->keydata
, from_keydata
,
204 /* Both keys are legacy */
205 if (from
->ameth
!= NULL
&& from
->ameth
->param_copy
!= NULL
)
206 ok
= from
->ameth
->param_copy(to
, from
);
208 EVP_PKEY_free(downgraded_from
);
212 int EVP_PKEY_missing_parameters(const EVP_PKEY
*pkey
)
215 if (pkey
->keymgmt
!= NULL
)
216 return !evp_keymgmt_util_has((EVP_PKEY
*)pkey
, SELECT_PARAMETERS
);
217 else if (pkey
->ameth
!= NULL
&& pkey
->ameth
->param_missing
!= NULL
)
218 return pkey
->ameth
->param_missing(pkey
);
224 * This function is called for any mixture of keys except pure legacy pair.
225 * When legacy keys are gone, we replace a call to this functions with
226 * a call to evp_keymgmt_util_match().
228 static int evp_pkey_cmp_any(const EVP_PKEY
*a
, const EVP_PKEY
*b
,
231 EVP_KEYMGMT
*keymgmt1
= NULL
, *keymgmt2
= NULL
;
232 void *keydata1
= NULL
, *keydata2
= NULL
, *tmp_keydata
= NULL
;
234 /* If none of them are provided, this function shouldn't have been called */
235 if (!ossl_assert(evp_pkey_is_provided(a
) || evp_pkey_is_provided(b
)))
238 /* For purely provided keys, we just call the keymgmt utility */
239 if (evp_pkey_is_provided(a
) && evp_pkey_is_provided(b
))
240 return evp_keymgmt_util_match((EVP_PKEY
*)a
, (EVP_PKEY
*)b
, selection
);
243 * At this point, one of them is provided, the other not. This allows
244 * us to compare types using legacy NIDs.
246 if (evp_pkey_is_legacy(a
)
247 && !EVP_KEYMGMT_is_a(b
->keymgmt
, OBJ_nid2sn(a
->type
)))
248 return -1; /* not the same key type */
249 if (evp_pkey_is_legacy(b
)
250 && !EVP_KEYMGMT_is_a(a
->keymgmt
, OBJ_nid2sn(b
->type
)))
251 return -1; /* not the same key type */
254 * We've determined that they both are the same keytype, so the next
255 * step is to do a bit of cross export to ensure we have keydata for
256 * both keys in the same keymgmt.
258 keymgmt1
= a
->keymgmt
;
259 keydata1
= a
->keydata
;
260 keymgmt2
= b
->keymgmt
;
261 keydata2
= b
->keydata
;
263 if (keymgmt2
!= NULL
&& keymgmt2
->match
!= NULL
) {
265 evp_pkey_export_to_provider((EVP_PKEY
*)a
, NULL
, &keymgmt2
, NULL
);
266 if (tmp_keydata
!= NULL
) {
268 keydata1
= tmp_keydata
;
271 if (tmp_keydata
== NULL
&& keymgmt1
!= NULL
&& keymgmt1
->match
!= NULL
) {
273 evp_pkey_export_to_provider((EVP_PKEY
*)b
, NULL
, &keymgmt1
, NULL
);
274 if (tmp_keydata
!= NULL
) {
276 keydata2
= tmp_keydata
;
280 /* If we still don't have matching keymgmt implementations, we give up */
281 if (keymgmt1
!= keymgmt2
)
284 /* If the keymgmt implementations are NULL, the export failed */
285 if (keymgmt1
== NULL
)
288 return evp_keymgmt_match(keymgmt1
, keydata1
, keydata2
, selection
);
291 int EVP_PKEY_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
293 return EVP_PKEY_parameters_eq(a
, b
);
296 int EVP_PKEY_parameters_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
299 * This will just call evp_keymgmt_util_match when legacy support
303 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
304 return evp_pkey_cmp_any(a
, b
, SELECT_PARAMETERS
);
306 /* All legacy keys */
307 if (a
->type
!= b
->type
)
309 if (a
->ameth
!= NULL
&& a
->ameth
->param_cmp
!= NULL
)
310 return a
->ameth
->param_cmp(a
, b
);
314 int EVP_PKEY_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
316 return EVP_PKEY_eq(a
, b
);
319 int EVP_PKEY_eq(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
322 * This will just call evp_keymgmt_util_match when legacy support
326 if (a
->keymgmt
!= NULL
|| b
->keymgmt
!= NULL
)
327 return evp_pkey_cmp_any(a
, b
, (SELECT_PARAMETERS
328 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY
));
330 /* All legacy keys */
331 if (a
->type
!= b
->type
)
334 if (a
->ameth
!= NULL
) {
336 /* Compare parameters if the algorithm has them */
337 if (a
->ameth
->param_cmp
!= NULL
) {
338 ret
= a
->ameth
->param_cmp(a
, b
);
343 if (a
->ameth
->pub_cmp
!= NULL
)
344 return a
->ameth
->pub_cmp(a
, b
);
351 static EVP_PKEY
*new_raw_key_int(OSSL_LIB_CTX
*libctx
,
356 const unsigned char *key
,
360 EVP_PKEY
*pkey
= NULL
;
361 EVP_PKEY_CTX
*ctx
= NULL
;
362 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
365 # ifndef OPENSSL_NO_ENGINE
366 /* Check if there is an Engine for this type */
371 ameth
= EVP_PKEY_asn1_find_str(&tmpe
, strtype
, -1);
372 else if (nidtype
!= EVP_PKEY_NONE
)
373 ameth
= EVP_PKEY_asn1_find(&tmpe
, nidtype
);
375 /* If tmpe is NULL then no engine is claiming to support this type */
383 if (e
== NULL
&& ameth
== NULL
) {
385 * No engine is claiming to support this type, so lets see if we have
388 ctx
= EVP_PKEY_CTX_new_from_name(libctx
,
389 strtype
!= NULL
? strtype
390 : OBJ_nid2sn(nidtype
),
394 /* May fail if no provider available */
396 if (EVP_PKEY_fromdata_init(ctx
) == 1) {
397 OSSL_PARAM params
[] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
399 ERR_clear_last_mark();
400 params
[0] = OSSL_PARAM_construct_octet_string(
401 key_is_priv
? OSSL_PKEY_PARAM_PRIV_KEY
402 : OSSL_PKEY_PARAM_PUB_KEY
,
405 if (EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
) != 1) {
406 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
410 EVP_PKEY_CTX_free(ctx
);
415 /* else not supported so fallback to legacy */
418 /* Legacy code path */
420 pkey
= EVP_PKEY_new();
422 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
426 if (!pkey_set_type(pkey
, e
, nidtype
, strtype
, -1, NULL
)) {
427 /* EVPerr already called */
431 if (!ossl_assert(pkey
->ameth
!= NULL
))
435 if (pkey
->ameth
->set_priv_key
== NULL
) {
436 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
440 if (!pkey
->ameth
->set_priv_key(pkey
, key
, len
)) {
441 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
445 if (pkey
->ameth
->set_pub_key
== NULL
) {
446 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
450 if (!pkey
->ameth
->set_pub_key(pkey
, key
, len
)) {
451 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
462 EVP_PKEY_CTX_free(ctx
);
466 EVP_PKEY
*EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX
*libctx
,
469 const unsigned char *priv
, size_t len
)
471 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, priv
,
475 EVP_PKEY
*EVP_PKEY_new_raw_private_key(int type
, ENGINE
*e
,
476 const unsigned char *priv
,
479 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, priv
, len
, 1);
482 EVP_PKEY
*EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX
*libctx
,
483 const char *keytype
, const char *propq
,
484 const unsigned char *pub
, size_t len
)
486 return new_raw_key_int(libctx
, keytype
, propq
, EVP_PKEY_NONE
, NULL
, pub
,
490 EVP_PKEY
*EVP_PKEY_new_raw_public_key(int type
, ENGINE
*e
,
491 const unsigned char *pub
,
494 return new_raw_key_int(NULL
, NULL
, NULL
, type
, e
, pub
, len
, 0);
497 struct raw_key_details_st
504 static OSSL_CALLBACK get_raw_key_details
;
505 static int get_raw_key_details(const OSSL_PARAM params
[], void *arg
)
507 const OSSL_PARAM
*p
= NULL
;
508 struct raw_key_details_st
*raw_key
= arg
;
510 if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) {
511 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PRIV_KEY
))
513 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
514 SIZE_MAX
, raw_key
->len
);
515 } else if (raw_key
->selection
== OSSL_KEYMGMT_SELECT_PUBLIC_KEY
) {
516 if ((p
= OSSL_PARAM_locate_const(params
, OSSL_PKEY_PARAM_PUB_KEY
))
518 return OSSL_PARAM_get_octet_string(p
, (void **)raw_key
->key
,
519 SIZE_MAX
, raw_key
->len
);
525 int EVP_PKEY_get_raw_private_key(const EVP_PKEY
*pkey
, unsigned char *priv
,
528 if (pkey
->keymgmt
!= NULL
) {
529 struct raw_key_details_st raw_key
;
531 raw_key
.key
= priv
== NULL
? NULL
: &priv
;
533 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PRIVATE_KEY
;
535 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PRIVATE_KEY
,
536 get_raw_key_details
, &raw_key
);
539 if (pkey
->ameth
== NULL
) {
540 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
544 if (pkey
->ameth
->get_priv_key
== NULL
) {
545 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
549 if (!pkey
->ameth
->get_priv_key(pkey
, priv
, len
)) {
550 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
557 int EVP_PKEY_get_raw_public_key(const EVP_PKEY
*pkey
, unsigned char *pub
,
560 if (pkey
->keymgmt
!= NULL
) {
561 struct raw_key_details_st raw_key
;
563 raw_key
.key
= pub
== NULL
? NULL
: &pub
;
565 raw_key
.selection
= OSSL_KEYMGMT_SELECT_PUBLIC_KEY
;
567 return evp_keymgmt_util_export(pkey
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
,
568 get_raw_key_details
, &raw_key
);
571 if (pkey
->ameth
== NULL
) {
572 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
576 if (pkey
->ameth
->get_pub_key
== NULL
) {
577 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
581 if (!pkey
->ameth
->get_pub_key(pkey
, pub
, len
)) {
582 ERR_raise(ERR_LIB_EVP
, EVP_R_GET_RAW_KEY_FAILED
);
589 static EVP_PKEY
*new_cmac_key_int(const unsigned char *priv
, size_t len
,
590 const char *cipher_name
,
591 const EVP_CIPHER
*cipher
,
592 OSSL_LIB_CTX
*libctx
,
593 const char *propq
, ENGINE
*e
)
595 # ifndef OPENSSL_NO_CMAC
596 # ifndef OPENSSL_NO_ENGINE
597 const char *engine_id
= e
!= NULL
? ENGINE_get_id(e
) : NULL
;
599 OSSL_PARAM params
[5], *p
= params
;
600 EVP_PKEY
*pkey
= NULL
;
604 cipher_name
= EVP_CIPHER_name(cipher
);
606 if (cipher_name
== NULL
) {
607 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
611 ctx
= EVP_PKEY_CTX_new_from_name(libctx
, "CMAC", propq
);
615 if (!EVP_PKEY_fromdata_init(ctx
)) {
616 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
620 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY
,
622 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER
,
623 (char *)cipher_name
, 0);
625 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES
,
627 # ifndef OPENSSL_NO_ENGINE
628 if (engine_id
!= NULL
)
629 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE
,
630 (char *)engine_id
, 0);
632 *p
= OSSL_PARAM_construct_end();
634 if (!EVP_PKEY_fromdata(ctx
, &pkey
, EVP_PKEY_KEYPAIR
, params
)) {
635 ERR_raise(ERR_LIB_EVP
, EVP_R_KEY_SETUP_FAILED
);
640 EVP_PKEY_CTX_free(ctx
);
644 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
649 EVP_PKEY
*EVP_PKEY_new_CMAC_key(ENGINE
*e
, const unsigned char *priv
,
650 size_t len
, const EVP_CIPHER
*cipher
)
652 return new_cmac_key_int(priv
, len
, NULL
, cipher
, NULL
, NULL
, e
);
655 int EVP_PKEY_set_type(EVP_PKEY
*pkey
, int type
)
657 return pkey_set_type(pkey
, NULL
, type
, NULL
, -1, NULL
);
660 int EVP_PKEY_set_type_str(EVP_PKEY
*pkey
, const char *str
, int len
)
662 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
, str
, len
, NULL
);
665 # ifndef OPENSSL_NO_DEPRECATED_3_0
666 int EVP_PKEY_set_alias_type(EVP_PKEY
*pkey
, int type
)
668 if (!evp_pkey_is_legacy(pkey
)) {
669 const char *name
= OBJ_nid2sn(type
);
671 if (name
!= NULL
&& EVP_PKEY_is_a(pkey
, name
))
674 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
678 if (pkey
->type
== type
) {
679 return 1; /* it already is that type */
683 * The application is requesting to alias this to a different pkey type,
684 * but not one that resolves to the base type.
686 if (EVP_PKEY_type(type
) != EVP_PKEY_base_id(pkey
)) {
687 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
696 # ifndef OPENSSL_NO_ENGINE
697 int EVP_PKEY_set1_engine(EVP_PKEY
*pkey
, ENGINE
*e
)
700 if (!ENGINE_init(e
)) {
701 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
704 if (ENGINE_get_pkey_meth(e
, pkey
->type
) == NULL
) {
706 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
710 ENGINE_finish(pkey
->pmeth_engine
);
711 pkey
->pmeth_engine
= e
;
715 ENGINE
*EVP_PKEY_get0_engine(const EVP_PKEY
*pkey
)
721 # ifndef OPENSSL_NO_DEPRECATED_3_0
722 int EVP_PKEY_assign(EVP_PKEY
*pkey
, int type
, void *key
)
726 # ifndef OPENSSL_NO_EC
727 if ((key
!= NULL
) && (EVP_PKEY_type(type
) == EVP_PKEY_EC
)) {
728 const EC_GROUP
*group
= EC_KEY_get0_group(key
);
730 if (group
!= NULL
&& EC_GROUP_get_curve_name(group
) == NID_sm2
)
731 alias
= EVP_PKEY_SM2
;
735 if (pkey
== NULL
|| !EVP_PKEY_set_type(pkey
, type
))
737 if (!EVP_PKEY_set_alias_type(pkey
, alias
))
739 pkey
->pkey
.ptr
= key
;
740 return (key
!= NULL
);
744 void *EVP_PKEY_get0(const EVP_PKEY
*pkey
)
749 if (!evp_pkey_is_provided(pkey
))
750 return pkey
->pkey
.ptr
;
755 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY
*pkey
, size_t *len
)
757 const ASN1_OCTET_STRING
*os
= NULL
;
758 if (pkey
->type
!= EVP_PKEY_HMAC
) {
759 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_AN_HMAC_KEY
);
762 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
770 # ifndef OPENSSL_NO_POLY1305
771 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY
*pkey
, size_t *len
)
773 const ASN1_OCTET_STRING
*os
= NULL
;
774 if (pkey
->type
!= EVP_PKEY_POLY1305
) {
775 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_POLY1305_KEY
);
778 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
787 # ifndef OPENSSL_NO_SIPHASH
788 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY
*pkey
, size_t *len
)
790 const ASN1_OCTET_STRING
*os
= NULL
;
792 if (pkey
->type
!= EVP_PKEY_SIPHASH
) {
793 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_SIPHASH_KEY
);
796 os
= evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
805 # ifndef OPENSSL_NO_DSA
806 static DSA
*evp_pkey_get0_DSA_int(const EVP_PKEY
*pkey
)
808 if (pkey
->type
!= EVP_PKEY_DSA
) {
809 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DSA_KEY
);
812 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
815 const DSA
*EVP_PKEY_get0_DSA(const EVP_PKEY
*pkey
)
817 return evp_pkey_get0_DSA_int(pkey
);
820 int EVP_PKEY_set1_DSA(EVP_PKEY
*pkey
, DSA
*key
)
822 int ret
= EVP_PKEY_assign_DSA(pkey
, key
);
827 DSA
*EVP_PKEY_get1_DSA(EVP_PKEY
*pkey
)
829 DSA
*ret
= evp_pkey_get0_DSA_int(pkey
);
835 # endif /* OPENSSL_NO_DSA */
837 # ifndef OPENSSL_NO_EC
838 static const ECX_KEY
*evp_pkey_get0_ECX_KEY(const EVP_PKEY
*pkey
, int type
)
840 if (EVP_PKEY_base_id(pkey
) != type
) {
841 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_ECX_KEY
);
844 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
847 static ECX_KEY
*evp_pkey_get1_ECX_KEY(EVP_PKEY
*pkey
, int type
)
849 ECX_KEY
*ret
= (ECX_KEY
*)evp_pkey_get0_ECX_KEY(pkey
, type
);
851 if (ret
!= NULL
&& !ossl_ecx_key_up_ref(ret
))
856 # define IMPLEMENT_ECX_VARIANT(NAME) \
857 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
859 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
861 IMPLEMENT_ECX_VARIANT(X25519
)
862 IMPLEMENT_ECX_VARIANT(X448
)
863 IMPLEMENT_ECX_VARIANT(ED25519
)
864 IMPLEMENT_ECX_VARIANT(ED448
)
868 # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
870 int EVP_PKEY_set1_DH(EVP_PKEY
*pkey
, DH
*key
)
872 int type
= DH_get0_q(key
) == NULL
? EVP_PKEY_DH
: EVP_PKEY_DHX
;
873 int ret
= EVP_PKEY_assign(pkey
, type
, key
);
880 DH
*evp_pkey_get0_DH_int(const EVP_PKEY
*pkey
)
882 if (pkey
->type
!= EVP_PKEY_DH
&& pkey
->type
!= EVP_PKEY_DHX
) {
883 ERR_raise(ERR_LIB_EVP
, EVP_R_EXPECTING_A_DH_KEY
);
886 return evp_pkey_get_legacy((EVP_PKEY
*)pkey
);
889 const DH
*EVP_PKEY_get0_DH(const EVP_PKEY
*pkey
)
891 return evp_pkey_get0_DH_int(pkey
);
894 DH
*EVP_PKEY_get1_DH(EVP_PKEY
*pkey
)
896 DH
*ret
= evp_pkey_get0_DH_int(pkey
);
904 int EVP_PKEY_type(int type
)
907 const EVP_PKEY_ASN1_METHOD
*ameth
;
909 ameth
= EVP_PKEY_asn1_find(&e
, type
);
911 ret
= ameth
->pkey_id
;
914 # ifndef OPENSSL_NO_ENGINE
920 int EVP_PKEY_id(const EVP_PKEY
*pkey
)
925 int EVP_PKEY_base_id(const EVP_PKEY
*pkey
)
927 return EVP_PKEY_type(pkey
->type
);
931 * These hard coded cases are pure hackery to get around the fact
932 * that names in crypto/objects/objects.txt are a mess. There is
933 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
934 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
935 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
936 * "DSA" is accurate... but still, better be safe and hard-code
937 * names that we know.
938 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
939 * EVP_PKEY_EC, because of aliasing.
940 * This should be cleaned away along with all other #legacy support.
942 static const OSSL_ITEM standard_name2type
[] = {
943 { EVP_PKEY_RSA
, "RSA" },
944 { EVP_PKEY_RSA_PSS
, "RSA-PSS" },
945 { EVP_PKEY_EC
, "EC" },
946 { EVP_PKEY_ED25519
, "ED25519" },
947 { EVP_PKEY_ED448
, "ED448" },
948 { EVP_PKEY_X25519
, "X25519" },
949 { EVP_PKEY_X448
, "X448" },
950 { EVP_PKEY_SM2
, "SM2" },
951 { EVP_PKEY_DH
, "DH" },
952 { EVP_PKEY_DHX
, "X9.42 DH" },
953 { EVP_PKEY_DHX
, "DHX" },
954 { EVP_PKEY_DSA
, "DSA" },
957 int evp_pkey_name2type(const char *name
)
962 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
963 if (strcasecmp(name
, standard_name2type
[i
].ptr
) == 0)
964 return (int)standard_name2type
[i
].id
;
967 if ((type
= EVP_PKEY_type(OBJ_sn2nid(name
))) != NID_undef
)
969 return EVP_PKEY_type(OBJ_ln2nid(name
));
972 const char *evp_pkey_type2name(int type
)
976 for (i
= 0; i
< OSSL_NELEM(standard_name2type
); i
++) {
977 if (type
== (int)standard_name2type
[i
].id
)
978 return standard_name2type
[i
].ptr
;
981 return OBJ_nid2sn(type
);
984 int EVP_PKEY_is_a(const EVP_PKEY
*pkey
, const char *name
)
986 if (pkey
->keymgmt
== NULL
) {
987 int type
= evp_pkey_name2type(name
);
989 return pkey
->type
== type
;
991 return EVP_KEYMGMT_is_a(pkey
->keymgmt
, name
);
994 int EVP_PKEY_typenames_do_all(const EVP_PKEY
*pkey
,
995 void (*fn
)(const char *name
, void *data
),
998 if (!evp_pkey_is_typed(pkey
))
1001 if (!evp_pkey_is_provided(pkey
)) {
1002 const char *name
= OBJ_nid2sn(EVP_PKEY_id(pkey
));
1007 return EVP_KEYMGMT_names_do_all(pkey
->keymgmt
, fn
, data
);
1010 int EVP_PKEY_can_sign(const EVP_PKEY
*pkey
)
1012 if (pkey
->keymgmt
== NULL
) {
1013 switch (EVP_PKEY_base_id(pkey
)) {
1016 # ifndef OPENSSL_NO_DSA
1020 # ifndef OPENSSL_NO_EC
1021 case EVP_PKEY_ED25519
:
1022 case EVP_PKEY_ED448
:
1024 case EVP_PKEY_EC
: /* Including SM2 */
1025 return EC_KEY_can_sign(pkey
->pkey
.ec
);
1031 const OSSL_PROVIDER
*prov
= EVP_KEYMGMT_provider(pkey
->keymgmt
);
1032 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
1033 const char *supported_sig
=
1034 pkey
->keymgmt
->query_operation_name
!= NULL
1035 ? pkey
->keymgmt
->query_operation_name(OSSL_OP_SIGNATURE
)
1036 : evp_first_name(prov
, pkey
->keymgmt
->name_id
);
1037 EVP_SIGNATURE
*signature
= NULL
;
1039 signature
= EVP_SIGNATURE_fetch(libctx
, supported_sig
, NULL
);
1040 if (signature
!= NULL
) {
1041 EVP_SIGNATURE_free(signature
);
1048 static int print_reset_indent(BIO
**out
, int pop_f_prefix
, long saved_indent
)
1050 BIO_set_indent(*out
, saved_indent
);
1052 BIO
*next
= BIO_pop(*out
);
1060 static int print_set_indent(BIO
**out
, int *pop_f_prefix
, long *saved_indent
,
1066 long i
= BIO_get_indent(*out
);
1068 *saved_indent
= (i
< 0 ? 0 : i
);
1069 if (BIO_set_indent(*out
, indent
) <= 0) {
1070 if ((*out
= BIO_push(BIO_new(BIO_f_prefix()), *out
)) == NULL
)
1074 if (BIO_set_indent(*out
, indent
) <= 0) {
1075 print_reset_indent(out
, *pop_f_prefix
, *saved_indent
);
1082 static int unsup_alg(BIO
*out
, const EVP_PKEY
*pkey
, int indent
,
1085 return BIO_indent(out
, indent
, 128)
1086 && BIO_printf(out
, "%s algorithm \"%s\" unsupported\n",
1087 kstr
, OBJ_nid2ln(pkey
->type
)) > 0;
1090 static int print_pkey(const EVP_PKEY
*pkey
, BIO
*out
, int indent
,
1091 int selection
/* For provided encoding */,
1092 const char *propquery
/* For provided encoding */,
1093 int (*legacy_print
)(BIO
*out
, const EVP_PKEY
*pkey
,
1094 int indent
, ASN1_PCTX
*pctx
),
1095 ASN1_PCTX
*legacy_pctx
/* For legacy print */)
1099 OSSL_ENCODER_CTX
*ctx
= NULL
;
1100 int ret
= -2; /* default to unsupported */
1102 if (!print_set_indent(&out
, &pop_f_prefix
, &saved_indent
, indent
))
1105 ctx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
, "TEXT", NULL
,
1107 if (OSSL_ENCODER_CTX_get_num_encoders(ctx
) != 0)
1108 ret
= OSSL_ENCODER_to_bio(ctx
, out
);
1109 OSSL_ENCODER_CTX_free(ctx
);
1114 /* legacy fallback */
1115 if (legacy_print
!= NULL
)
1116 ret
= legacy_print(out
, pkey
, 0, legacy_pctx
);
1118 ret
= unsup_alg(out
, pkey
, 0, "Public Key");
1121 print_reset_indent(&out
, pop_f_prefix
, saved_indent
);
1125 int EVP_PKEY_print_public(BIO
*out
, const EVP_PKEY
*pkey
,
1126 int indent
, ASN1_PCTX
*pctx
)
1128 return print_pkey(pkey
, out
, indent
, EVP_PKEY_PUBLIC_KEY
, NULL
,
1129 (pkey
->ameth
!= NULL
? pkey
->ameth
->pub_print
: NULL
),
1133 int EVP_PKEY_print_private(BIO
*out
, const EVP_PKEY
*pkey
,
1134 int indent
, ASN1_PCTX
*pctx
)
1136 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEYPAIR
, NULL
,
1137 (pkey
->ameth
!= NULL
? pkey
->ameth
->priv_print
: NULL
),
1141 int EVP_PKEY_print_params(BIO
*out
, const EVP_PKEY
*pkey
,
1142 int indent
, ASN1_PCTX
*pctx
)
1144 return print_pkey(pkey
, out
, indent
, EVP_PKEY_KEY_PARAMETERS
, NULL
,
1145 (pkey
->ameth
!= NULL
? pkey
->ameth
->param_print
: NULL
),
1149 # ifndef OPENSSL_NO_STDIO
1150 int EVP_PKEY_print_public_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1151 int indent
, ASN1_PCTX
*pctx
)
1154 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1158 ret
= EVP_PKEY_print_public(b
, pkey
, indent
, pctx
);
1163 int EVP_PKEY_print_private_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1164 int indent
, ASN1_PCTX
*pctx
)
1167 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1171 ret
= EVP_PKEY_print_private(b
, pkey
, indent
, pctx
);
1176 int EVP_PKEY_print_params_fp(FILE *fp
, const EVP_PKEY
*pkey
,
1177 int indent
, ASN1_PCTX
*pctx
)
1180 BIO
*b
= BIO_new_fp(fp
, BIO_NOCLOSE
);
1184 ret
= EVP_PKEY_print_params(b
, pkey
, indent
, pctx
);
1190 static void mdname2nid(const char *mdname
, void *data
)
1192 int *nid
= (int *)data
;
1194 if (*nid
!= NID_undef
)
1197 *nid
= OBJ_sn2nid(mdname
);
1198 if (*nid
== NID_undef
)
1199 *nid
= OBJ_ln2nid(mdname
);
1202 static int legacy_asn1_ctrl_to_param(EVP_PKEY
*pkey
, int op
,
1203 int arg1
, void *arg2
)
1205 if (pkey
->keymgmt
== NULL
)
1208 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
1210 char mdname
[80] = "";
1211 int rv
= EVP_PKEY_get_default_digest_name(pkey
, mdname
,
1216 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(pkey
->keymgmt
->prov
);
1217 /* Make sure the MD is in the namemap if available */
1218 EVP_MD
*md
= EVP_MD_fetch(libctx
, mdname
, NULL
);
1219 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
1220 int nid
= NID_undef
;
1223 * The only reason to fetch the MD was to make sure it is in the
1224 * namemap. We can immediately free it.
1227 mdnum
= ossl_namemap_name2num(namemap
, mdname
);
1232 * We have the namemap number - now we need to find the
1235 if (!ossl_namemap_doall_names(namemap
, mdnum
, mdname2nid
, &nid
))
1246 static int evp_pkey_asn1_ctrl(EVP_PKEY
*pkey
, int op
, int arg1
, void *arg2
)
1248 if (pkey
->ameth
== NULL
)
1249 return legacy_asn1_ctrl_to_param(pkey
, op
, arg1
, arg2
);
1250 if (pkey
->ameth
->pkey_ctrl
== NULL
)
1252 return pkey
->ameth
->pkey_ctrl(pkey
, op
, arg1
, arg2
);
1255 int EVP_PKEY_get_default_digest_nid(EVP_PKEY
*pkey
, int *pnid
)
1257 return evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_DEFAULT_MD_NID
, 0, pnid
);
1260 int EVP_PKEY_get_default_digest_name(EVP_PKEY
*pkey
,
1261 char *mdname
, size_t mdname_sz
)
1263 if (pkey
->ameth
== NULL
)
1264 return evp_keymgmt_util_get_deflt_digest_name(pkey
->keymgmt
,
1269 int nid
= NID_undef
;
1270 int rv
= EVP_PKEY_get_default_digest_nid(pkey
, &nid
);
1271 const char *name
= rv
> 0 ? OBJ_nid2sn(nid
) : NULL
;
1274 OPENSSL_strlcpy(mdname
, name
, mdname_sz
);
1279 int EVP_PKEY_get_group_name(const EVP_PKEY
*pkey
, char *gname
, size_t gname_sz
,
1282 return EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_GROUP_NAME
,
1283 gname
, gname_sz
, gname_len
);
1286 int EVP_PKEY_supports_digest_nid(EVP_PKEY
*pkey
, int nid
)
1288 int rv
, default_nid
;
1290 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SUPPORTS_MD_NID
, nid
, NULL
);
1293 * If there is a mandatory default digest and this isn't it, then
1294 * the answer is 'no'.
1296 rv
= EVP_PKEY_get_default_digest_nid(pkey
, &default_nid
);
1298 return (nid
== default_nid
);
1299 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
1306 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY
*pkey
, const unsigned char *pub
,
1309 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
1311 EVP_PKEY_set_octet_string_param(pkey
,
1312 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1313 (unsigned char *)pub
, publen
);
1315 if (publen
> INT_MAX
)
1317 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1318 if (evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_SET1_TLS_ENCPT
, publen
,
1324 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY
*pkey
, unsigned char **ppub
)
1328 if (pkey
!= NULL
&& evp_pkey_is_provided(pkey
)) {
1329 size_t return_size
= OSSL_PARAM_UNMODIFIED
;
1332 * We know that this is going to fail, but it will give us a size
1335 EVP_PKEY_get_octet_string_param(pkey
,
1336 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1337 NULL
, 0, &return_size
);
1338 if (return_size
== OSSL_PARAM_UNMODIFIED
)
1341 *ppub
= OPENSSL_malloc(return_size
);
1345 if (!EVP_PKEY_get_octet_string_param(pkey
,
1346 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY
,
1347 *ppub
, return_size
, NULL
))
1353 rv
= evp_pkey_asn1_ctrl(pkey
, ASN1_PKEY_CTRL_GET1_TLS_ENCPT
, 0, ppub
);
1359 #endif /* FIPS_MODULE */
1361 /*- All methods below can also be used in FIPS_MODULE */
1363 EVP_PKEY
*EVP_PKEY_new(void)
1365 EVP_PKEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
1368 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1372 ret
->type
= EVP_PKEY_NONE
;
1373 ret
->save_type
= EVP_PKEY_NONE
;
1374 ret
->references
= 1;
1375 ret
->save_parameters
= 1;
1377 ret
->lock
= CRYPTO_THREAD_lock_new();
1378 if (ret
->lock
== NULL
) {
1379 EVPerr(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1384 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, ret
, &ret
->ex_data
)) {
1385 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1392 CRYPTO_THREAD_lock_free(ret
->lock
);
1398 * Setup a public key management method.
1400 * For legacy keys, either |type| or |str| is expected to have the type
1401 * information. In this case, the setup consists of finding an ASN1 method
1402 * and potentially an ENGINE, and setting those fields in |pkey|.
1404 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1405 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1407 * If pkey is NULL just return 1 or 0 if the key management method exists.
1410 static int pkey_set_type(EVP_PKEY
*pkey
, ENGINE
*e
, int type
, const char *str
,
1411 int len
, EVP_KEYMGMT
*keymgmt
)
1414 const EVP_PKEY_ASN1_METHOD
*ameth
= NULL
;
1415 ENGINE
**eptr
= (e
== NULL
) ? &e
: NULL
;
1419 * The setups can't set both legacy and provider side methods.
1422 if (!ossl_assert(type
== EVP_PKEY_NONE
|| keymgmt
== NULL
)
1423 || !ossl_assert(e
== NULL
|| keymgmt
== NULL
)) {
1424 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1432 free_it
= free_it
|| pkey
->pkey
.ptr
!= NULL
;
1434 free_it
= free_it
|| pkey
->keydata
!= NULL
;
1436 evp_pkey_free_it(pkey
);
1439 * If key type matches and a method exists then this lookup has
1440 * succeeded once so just indicate success.
1442 if (pkey
->type
!= EVP_PKEY_NONE
1443 && type
== pkey
->save_type
1444 && pkey
->ameth
!= NULL
)
1446 # ifndef OPENSSL_NO_ENGINE
1447 /* If we have ENGINEs release them */
1448 ENGINE_finish(pkey
->engine
);
1449 pkey
->engine
= NULL
;
1450 ENGINE_finish(pkey
->pmeth_engine
);
1451 pkey
->pmeth_engine
= NULL
;
1457 ameth
= EVP_PKEY_asn1_find_str(eptr
, str
, len
);
1458 else if (type
!= EVP_PKEY_NONE
)
1459 ameth
= EVP_PKEY_asn1_find(eptr
, type
);
1460 # ifndef OPENSSL_NO_ENGINE
1461 if (pkey
== NULL
&& eptr
!= NULL
)
1471 check
= check
&& ameth
== NULL
;
1473 check
= check
&& keymgmt
== NULL
;
1475 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_ALGORITHM
);
1480 if (keymgmt
!= NULL
&& !EVP_KEYMGMT_up_ref(keymgmt
)) {
1481 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1485 pkey
->keymgmt
= keymgmt
;
1487 pkey
->save_type
= type
;
1492 * If the internal "origin" key is provider side, don't save |ameth|.
1493 * The main reason is that |ameth| is one factor to detect that the
1494 * internal "origin" key is a legacy one.
1496 if (keymgmt
== NULL
)
1497 pkey
->ameth
= ameth
;
1501 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1502 * for any key type that has a legacy implementation, regardless of
1503 * if the internal key is a legacy or a provider side one. When
1504 * there is no legacy implementation for the key, the type becomes
1505 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1506 * with functions that expect legacy internal keys.
1509 pkey
->type
= ameth
->pkey_id
;
1511 pkey
->type
= EVP_PKEY_KEYMGMT
;
1518 static void find_ameth(const char *name
, void *data
)
1520 const char **str
= data
;
1523 * The error messages from pkey_set_type() are uninteresting here,
1528 if (pkey_set_type(NULL
, NULL
, EVP_PKEY_NONE
, name
, strlen(name
),
1532 else if (str
[1] == NULL
)
1540 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY
*pkey
, EVP_KEYMGMT
*keymgmt
)
1543 # define EVP_PKEY_TYPE_STR str[0]
1544 # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1546 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1547 * Ideally, only one should be found. If two (or more) are found, the
1548 * match is ambiguous. This should never happen, but...
1550 const char *str
[2] = { NULL
, NULL
};
1552 if (!EVP_KEYMGMT_names_do_all(keymgmt
, find_ameth
, &str
)
1553 || str
[1] != NULL
) {
1554 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1558 # define EVP_PKEY_TYPE_STR NULL
1559 # define EVP_PKEY_TYPE_STRLEN -1
1561 return pkey_set_type(pkey
, NULL
, EVP_PKEY_NONE
,
1562 EVP_PKEY_TYPE_STR
, EVP_PKEY_TYPE_STRLEN
,
1565 #undef EVP_PKEY_TYPE_STR
1566 #undef EVP_PKEY_TYPE_STRLEN
1569 int EVP_PKEY_up_ref(EVP_PKEY
*pkey
)
1573 if (CRYPTO_UP_REF(&pkey
->references
, &i
, pkey
->lock
) <= 0)
1576 REF_PRINT_COUNT("EVP_PKEY", pkey
);
1577 REF_ASSERT_ISNT(i
< 2);
1578 return ((i
> 1) ? 1 : 0);
1582 EVP_PKEY
*EVP_PKEY_dup(EVP_PKEY
*pkey
)
1587 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
1591 if ((dup_pk
= EVP_PKEY_new()) == NULL
)
1594 if (evp_pkey_is_blank(pkey
))
1597 if (evp_pkey_is_provided(pkey
)) {
1598 if (!evp_keymgmt_util_copy(dup_pk
, pkey
,
1599 OSSL_KEYMGMT_SELECT_ALL
))
1604 if (evp_pkey_is_legacy(pkey
)) {
1605 const EVP_PKEY_ASN1_METHOD
*ameth
= pkey
->ameth
;
1607 if (ameth
== NULL
|| ameth
->copy
== NULL
) {
1608 if (pkey
->pkey
.ptr
== NULL
/* empty key, just set type */
1609 && EVP_PKEY_set_type(dup_pk
, pkey
->type
) != 0)
1611 ERR_raise(ERR_LIB_EVP
, EVP_R_UNSUPPORTED_KEY_TYPE
);
1614 if (!ameth
->copy(dup_pk
, pkey
))
1621 /* copy auxiliary data */
1622 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
,
1623 &dup_pk
->ex_data
, &pkey
->ex_data
))
1626 if (pkey
->attributes
!= NULL
) {
1627 if ((dup_pk
->attributes
= ossl_x509at_dup(pkey
->attributes
)) == NULL
)
1632 EVP_PKEY_free(dup_pk
);
1636 void evp_pkey_free_legacy(EVP_PKEY
*x
)
1638 const EVP_PKEY_ASN1_METHOD
*ameth
= x
->ameth
;
1639 ENGINE
*tmpe
= NULL
;
1641 if (ameth
== NULL
&& x
->legacy_cache_pkey
.ptr
!= NULL
)
1642 ameth
= EVP_PKEY_asn1_find(&tmpe
, x
->type
);
1644 if (ameth
!= NULL
) {
1645 if (x
->legacy_cache_pkey
.ptr
!= NULL
) {
1647 * We should never have both a legacy origin key, and a key in the
1650 assert(x
->pkey
.ptr
== NULL
);
1652 * For the purposes of freeing we make the legacy cache look like
1653 * a legacy origin key.
1655 x
->pkey
= x
->legacy_cache_pkey
;
1656 x
->legacy_cache_pkey
.ptr
= NULL
;
1658 if (ameth
->pkey_free
!= NULL
)
1659 ameth
->pkey_free(x
);
1662 # ifndef OPENSSL_NO_ENGINE
1663 ENGINE_finish(tmpe
);
1664 ENGINE_finish(x
->engine
);
1666 ENGINE_finish(x
->pmeth_engine
);
1667 x
->pmeth_engine
= NULL
;
1670 #endif /* FIPS_MODULE */
1672 static void evp_pkey_free_it(EVP_PKEY
*x
)
1674 /* internal function; x is never NULL */
1675 evp_keymgmt_util_clear_operation_cache(x
, 1);
1677 evp_pkey_free_legacy(x
);
1680 if (x
->keymgmt
!= NULL
) {
1681 evp_keymgmt_freedata(x
->keymgmt
, x
->keydata
);
1682 EVP_KEYMGMT_free(x
->keymgmt
);
1686 x
->type
= EVP_PKEY_NONE
;
1689 void EVP_PKEY_free(EVP_PKEY
*x
)
1696 CRYPTO_DOWN_REF(&x
->references
, &i
, x
->lock
);
1697 REF_PRINT_COUNT("EVP_PKEY", x
);
1700 REF_ASSERT_ISNT(i
< 0);
1701 evp_pkey_free_it(x
);
1703 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY
, x
, &x
->ex_data
);
1705 CRYPTO_THREAD_lock_free(x
->lock
);
1707 sk_X509_ATTRIBUTE_pop_free(x
->attributes
, X509_ATTRIBUTE_free
);
1712 int EVP_PKEY_size(const EVP_PKEY
*pkey
)
1717 size
= pkey
->cache
.size
;
1719 if (pkey
->ameth
!= NULL
&& pkey
->ameth
->pkey_size
!= NULL
)
1720 size
= pkey
->ameth
->pkey_size(pkey
);
1723 return size
< 0 ? 0 : size
;
1726 const char *EVP_PKEY_description(const EVP_PKEY
*pkey
)
1728 if (!evp_pkey_is_assigned(pkey
))
1731 if (evp_pkey_is_provided(pkey
) && pkey
->keymgmt
->description
!= NULL
)
1732 return pkey
->keymgmt
->description
;
1734 if (pkey
->ameth
!= NULL
)
1735 return pkey
->ameth
->info
;
1740 void *evp_pkey_export_to_provider(EVP_PKEY
*pk
, OSSL_LIB_CTX
*libctx
,
1741 EVP_KEYMGMT
**keymgmt
,
1742 const char *propquery
)
1744 EVP_KEYMGMT
*allocated_keymgmt
= NULL
;
1745 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
1746 void *keydata
= NULL
;
1752 /* No key data => nothing to export */
1755 check
= check
&& pk
->pkey
.ptr
== NULL
;
1757 check
= check
&& pk
->keydata
== NULL
;
1762 if (pk
->pkey
.ptr
!= NULL
) {
1764 * If the legacy key doesn't have an dirty counter or export function,
1767 if (pk
->ameth
->dirty_cnt
== NULL
|| pk
->ameth
->export_to
== NULL
)
1772 if (keymgmt
!= NULL
) {
1773 tmp_keymgmt
= *keymgmt
;
1778 * If no keymgmt was given or found, get a default keymgmt. We do so by
1779 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1781 if (tmp_keymgmt
== NULL
) {
1782 EVP_PKEY_CTX
*ctx
= EVP_PKEY_CTX_new_from_pkey(libctx
, pk
, propquery
);
1784 tmp_keymgmt
= ctx
->keymgmt
;
1785 ctx
->keymgmt
= NULL
;
1786 EVP_PKEY_CTX_free(ctx
);
1789 /* If there's still no keymgmt to be had, give up */
1790 if (tmp_keymgmt
== NULL
)
1794 if (pk
->pkey
.ptr
!= NULL
) {
1798 * If the legacy "origin" hasn't changed since last time, we try
1799 * to find our keymgmt in the operation cache. If it has changed,
1800 * |i| remains zero, and we will clear the cache further down.
1802 if (pk
->ameth
->dirty_cnt(pk
) == pk
->dirty_cnt_copy
) {
1803 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
1805 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1808 * If |tmp_keymgmt| is present in the operation cache, it means
1809 * that export doesn't need to be redone. In that case, we take
1810 * token copies of the cached pointers, to have token success
1813 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1814 keydata
= op
->keydata
;
1815 CRYPTO_THREAD_unlock(pk
->lock
);
1818 CRYPTO_THREAD_unlock(pk
->lock
);
1821 /* Make sure that the keymgmt key type matches the legacy NID */
1822 if (!EVP_KEYMGMT_is_a(tmp_keymgmt
, OBJ_nid2sn(pk
->type
)))
1825 if ((keydata
= evp_keymgmt_newdata(tmp_keymgmt
)) == NULL
)
1828 if (!pk
->ameth
->export_to(pk
, keydata
, tmp_keymgmt
, libctx
, propquery
)) {
1829 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1835 * If the dirty counter changed since last time, then clear the
1836 * operation cache. In that case, we know that |i| is zero. Just
1837 * in case this is a re-export, we increment then decrement the
1838 * keymgmt reference counter.
1840 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt
)) { /* refcnt++ */
1841 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1846 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
1848 if (pk
->ameth
->dirty_cnt(pk
) != pk
->dirty_cnt_copy
1849 && !evp_keymgmt_util_clear_operation_cache(pk
, 0)) {
1850 CRYPTO_THREAD_unlock(pk
->lock
);
1851 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1853 EVP_KEYMGMT_free(tmp_keymgmt
);
1856 EVP_KEYMGMT_free(tmp_keymgmt
); /* refcnt-- */
1858 /* Check to make sure some other thread didn't get there first */
1859 op
= evp_keymgmt_util_find_operation_cache(pk
, tmp_keymgmt
);
1860 if (op
!= NULL
&& op
->keymgmt
!= NULL
) {
1861 void *tmp_keydata
= op
->keydata
;
1863 CRYPTO_THREAD_unlock(pk
->lock
);
1864 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1865 keydata
= tmp_keydata
;
1869 /* Add the new export to the operation cache */
1870 if (!evp_keymgmt_util_cache_keydata(pk
, tmp_keymgmt
, keydata
)) {
1871 CRYPTO_THREAD_unlock(pk
->lock
);
1872 evp_keymgmt_freedata(tmp_keymgmt
, keydata
);
1877 /* Synchronize the dirty count */
1878 pk
->dirty_cnt_copy
= pk
->ameth
->dirty_cnt(pk
);
1880 CRYPTO_THREAD_unlock(pk
->lock
);
1883 #endif /* FIPS_MODULE */
1885 keydata
= evp_keymgmt_util_export_to_provider(pk
, tmp_keymgmt
);
1889 * If nothing was exported, |tmp_keymgmt| might point at a freed
1890 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1891 * the caller either way in that case.
1893 if (keydata
== NULL
)
1896 if (keymgmt
!= NULL
)
1897 *keymgmt
= tmp_keymgmt
;
1899 EVP_KEYMGMT_free(allocated_keymgmt
);
1904 int evp_pkey_copy_downgraded(EVP_PKEY
**dest
, const EVP_PKEY
*src
)
1906 if (!ossl_assert(dest
!= NULL
))
1909 if (evp_pkey_is_assigned(src
) && evp_pkey_is_provided(src
)) {
1910 EVP_KEYMGMT
*keymgmt
= src
->keymgmt
;
1911 void *keydata
= src
->keydata
;
1912 int type
= src
->type
;
1913 const char *keytype
= NULL
;
1915 keytype
= evp_first_name(EVP_KEYMGMT_provider(keymgmt
),
1919 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
1920 * else in our code. If it's not one of the well known EVP_PKEY_xxx
1921 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
1922 * The check is kept as a safety measure.
1924 if (!ossl_assert(type
!= EVP_PKEY_NONE
)) {
1925 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
1926 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
1931 /* Prefer the legacy key type name for error reporting */
1932 if (type
!= EVP_PKEY_KEYMGMT
)
1933 keytype
= OBJ_nid2sn(type
);
1935 /* Make sure we have a clean slate to copy into */
1936 if (*dest
== NULL
) {
1937 *dest
= EVP_PKEY_new();
1938 if (*dest
== NULL
) {
1939 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1943 evp_pkey_free_it(*dest
);
1946 if (EVP_PKEY_set_type(*dest
, type
)) {
1947 /* If the key is typed but empty, we're done */
1948 if (keydata
== NULL
)
1951 if ((*dest
)->ameth
->import_from
== NULL
) {
1952 ERR_raise_data(ERR_LIB_EVP
, EVP_R_NO_IMPORT_FUNCTION
,
1953 "key type = %s", keytype
);
1956 * We perform the export in the same libctx as the keymgmt
1957 * that we are using.
1959 OSSL_LIB_CTX
*libctx
=
1960 ossl_provider_libctx(keymgmt
->prov
);
1961 EVP_PKEY_CTX
*pctx
=
1962 EVP_PKEY_CTX_new_from_pkey(libctx
, *dest
, NULL
);
1965 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1968 && evp_keymgmt_export(keymgmt
, keydata
,
1969 OSSL_KEYMGMT_SELECT_ALL
,
1970 (*dest
)->ameth
->import_from
,
1972 /* Synchronize the dirty count */
1973 (*dest
)->dirty_cnt_copy
= (*dest
)->ameth
->dirty_cnt(*dest
);
1975 EVP_PKEY_CTX_free(pctx
);
1978 EVP_PKEY_CTX_free(pctx
);
1981 ERR_raise_data(ERR_LIB_EVP
, EVP_R_KEYMGMT_EXPORT_FAILURE
,
1982 "key type = %s", keytype
);
1989 void *evp_pkey_get_legacy(EVP_PKEY
*pk
)
1991 EVP_PKEY
*tmp_copy
= NULL
;
1994 if (!ossl_assert(pk
!= NULL
))
1998 * If this isn't an assigned provider side key, we just use any existing
1999 * origin legacy key.
2001 if (!evp_pkey_is_assigned(pk
))
2003 if (!evp_pkey_is_provided(pk
))
2004 return pk
->pkey
.ptr
;
2006 if (!CRYPTO_THREAD_read_lock(pk
->lock
))
2009 ret
= pk
->legacy_cache_pkey
.ptr
;
2011 if (!CRYPTO_THREAD_unlock(pk
->lock
))
2017 if (!evp_pkey_copy_downgraded(&tmp_copy
, pk
))
2020 if (!CRYPTO_THREAD_write_lock(pk
->lock
))
2023 /* Check again in case some other thread has updated it in the meantime */
2024 ret
= pk
->legacy_cache_pkey
.ptr
;
2026 /* Steal the legacy key reference from the temporary copy */
2027 ret
= pk
->legacy_cache_pkey
.ptr
= tmp_copy
->pkey
.ptr
;
2028 tmp_copy
->pkey
.ptr
= NULL
;
2031 if (!CRYPTO_THREAD_unlock(pk
->lock
)) {
2037 EVP_PKEY_free(tmp_copy
);
2041 #endif /* FIPS_MODULE */
2043 int EVP_PKEY_get_bn_param(const EVP_PKEY
*pkey
, const char *key_name
,
2047 OSSL_PARAM params
[2];
2048 unsigned char buffer
[2048];
2049 unsigned char *buf
= NULL
;
2052 if (key_name
== NULL
2056 memset(buffer
, 0, sizeof(buffer
));
2057 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, sizeof(buffer
));
2058 params
[1] = OSSL_PARAM_construct_end();
2059 if (!EVP_PKEY_get_params(pkey
, params
)) {
2060 if (!OSSL_PARAM_modified(params
) || params
[0].return_size
== 0)
2062 buf_sz
= params
[0].return_size
;
2064 * If it failed because the buffer was too small then allocate the
2065 * required buffer size and retry.
2067 buf
= OPENSSL_zalloc(buf_sz
);
2070 params
[0].data
= buf
;
2071 params
[0].data_size
= buf_sz
;
2073 if (!EVP_PKEY_get_params(pkey
, params
))
2076 /* Fail if the param was not found */
2077 if (!OSSL_PARAM_modified(params
))
2079 ret
= OSSL_PARAM_get_BN(params
, bn
);
2085 int EVP_PKEY_get_octet_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2086 unsigned char *buf
, size_t max_buf_sz
,
2089 OSSL_PARAM params
[2];
2090 int ret1
= 0, ret2
= 0;
2092 if (key_name
== NULL
)
2095 params
[0] = OSSL_PARAM_construct_octet_string(key_name
, buf
, max_buf_sz
);
2096 params
[1] = OSSL_PARAM_construct_end();
2097 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2098 ret2
= OSSL_PARAM_modified(params
);
2099 if (ret2
&& out_sz
!= NULL
)
2100 *out_sz
= params
[0].return_size
;
2101 return ret1
&& ret2
;
2104 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY
*pkey
, const char *key_name
,
2105 char *str
, size_t max_buf_sz
,
2108 OSSL_PARAM params
[2];
2109 int ret1
= 0, ret2
= 0;
2111 if (key_name
== NULL
)
2114 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, str
, max_buf_sz
);
2115 params
[1] = OSSL_PARAM_construct_end();
2116 if ((ret1
= EVP_PKEY_get_params(pkey
, params
)))
2117 ret2
= OSSL_PARAM_modified(params
);
2118 if (ret2
&& out_sz
!= NULL
)
2119 *out_sz
= params
[0].return_size
;
2120 return ret1
&& ret2
;
2123 int EVP_PKEY_get_int_param(const EVP_PKEY
*pkey
, const char *key_name
,
2126 OSSL_PARAM params
[2];
2128 if (key_name
== NULL
)
2131 params
[0] = OSSL_PARAM_construct_int(key_name
, out
);
2132 params
[1] = OSSL_PARAM_construct_end();
2133 return EVP_PKEY_get_params(pkey
, params
)
2134 && OSSL_PARAM_modified(params
);
2137 int EVP_PKEY_get_size_t_param(const EVP_PKEY
*pkey
, const char *key_name
,
2140 OSSL_PARAM params
[2];
2142 if (key_name
== NULL
)
2145 params
[0] = OSSL_PARAM_construct_size_t(key_name
, out
);
2146 params
[1] = OSSL_PARAM_construct_end();
2147 return EVP_PKEY_get_params(pkey
, params
)
2148 && OSSL_PARAM_modified(params
);
2151 int EVP_PKEY_set_int_param(EVP_PKEY
*pkey
, const char *key_name
, int in
)
2153 OSSL_PARAM params
[2];
2155 if (key_name
== NULL
)
2158 params
[0] = OSSL_PARAM_construct_int(key_name
, &in
);
2159 params
[1] = OSSL_PARAM_construct_end();
2160 return EVP_PKEY_set_params(pkey
, params
);
2163 int EVP_PKEY_set_size_t_param(EVP_PKEY
*pkey
, const char *key_name
, size_t in
)
2165 OSSL_PARAM params
[2];
2167 if (key_name
== NULL
)
2170 params
[0] = OSSL_PARAM_construct_size_t(key_name
, &in
);
2171 params
[1] = OSSL_PARAM_construct_end();
2172 return EVP_PKEY_set_params(pkey
, params
);
2175 int EVP_PKEY_set_bn_param(EVP_PKEY
*pkey
, const char *key_name
,
2178 OSSL_PARAM params
[2];
2179 unsigned char buffer
[2048];
2182 if (key_name
== NULL
2185 || !evp_pkey_is_assigned(pkey
))
2188 bsize
= BN_num_bytes(bn
);
2189 if (!ossl_assert(bsize
<= (int)sizeof(buffer
)))
2192 if (BN_bn2nativepad(bn
, buffer
, bsize
) < 0)
2194 params
[0] = OSSL_PARAM_construct_BN(key_name
, buffer
, bsize
);
2195 params
[1] = OSSL_PARAM_construct_end();
2196 return EVP_PKEY_set_params(pkey
, params
);
2199 int EVP_PKEY_set_utf8_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2202 OSSL_PARAM params
[2];
2204 if (key_name
== NULL
)
2207 params
[0] = OSSL_PARAM_construct_utf8_string(key_name
, (char *)str
, 0);
2208 params
[1] = OSSL_PARAM_construct_end();
2209 return EVP_PKEY_set_params(pkey
, params
);
2212 int EVP_PKEY_set_octet_string_param(EVP_PKEY
*pkey
, const char *key_name
,
2213 const unsigned char *buf
, size_t bsize
)
2215 OSSL_PARAM params
[2];
2217 if (key_name
== NULL
)
2220 params
[0] = OSSL_PARAM_construct_octet_string(key_name
,
2221 (unsigned char *)buf
, bsize
);
2222 params
[1] = OSSL_PARAM_construct_end();
2223 return EVP_PKEY_set_params(pkey
, params
);
2226 const OSSL_PARAM
*EVP_PKEY_settable_params(const EVP_PKEY
*pkey
)
2228 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2229 ? EVP_KEYMGMT_settable_params(pkey
->keymgmt
)
2233 int EVP_PKEY_set_params(EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2236 if (evp_pkey_is_provided(pkey
)) {
2238 return evp_keymgmt_set_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2242 * We will hopefully never find the need to set individual data in
2243 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2244 * sure. This bit of code can be enabled if we find the need. If
2245 * not, it can safely be removed when #legacy support is removed.
2248 else if (evp_pkey_is_legacy(pkey
)) {
2249 return evp_pkey_set_params_to_ctrl(pkey
, params
);
2254 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2258 const OSSL_PARAM
*EVP_PKEY_gettable_params(const EVP_PKEY
*pkey
)
2260 return (pkey
!= NULL
&& evp_pkey_is_provided(pkey
))
2261 ? EVP_KEYMGMT_gettable_params(pkey
->keymgmt
)
2265 int EVP_PKEY_get_params(const EVP_PKEY
*pkey
, OSSL_PARAM params
[])
2268 if (evp_pkey_is_provided(pkey
))
2269 return evp_keymgmt_get_params(pkey
->keymgmt
, pkey
->keydata
, params
);
2271 else if (evp_pkey_is_legacy(pkey
))
2272 return evp_pkey_get_params_to_ctrl(pkey
, params
);
2275 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY
);
2280 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY
*pkey
)
2288 if (pkey
->keymgmt
== NULL
2289 || pkey
->keydata
== NULL
) {
2290 # ifndef OPENSSL_NO_EC
2291 /* Might work through the legacy route */
2292 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2297 return EC_KEY_get_conv_form(ec
);
2303 if (!EVP_PKEY_get_utf8_string_param(pkey
,
2304 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT
,
2305 name
, sizeof(name
), &name_len
))
2308 if (strcmp(name
, "uncompressed") == 0)
2309 return POINT_CONVERSION_UNCOMPRESSED
;
2311 if (strcmp(name
, "compressed") == 0)
2312 return POINT_CONVERSION_COMPRESSED
;
2314 if (strcmp(name
, "hybrid") == 0)
2315 return POINT_CONVERSION_HYBRID
;
2320 int EVP_PKEY_get_field_type(const EVP_PKEY
*pkey
)
2328 if (pkey
->keymgmt
== NULL
2329 || pkey
->keydata
== NULL
) {
2330 # ifndef OPENSSL_NO_EC
2331 /* Might work through the legacy route */
2332 const EC_KEY
*ec
= EVP_PKEY_get0_EC_KEY(pkey
);
2333 const EC_GROUP
*grp
;
2337 grp
= EC_KEY_get0_group(ec
);
2341 return EC_GROUP_get_field_type(grp
);
2347 if (!EVP_PKEY_get_utf8_string_param(pkey
, OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
2348 fstr
, sizeof(fstr
), &fstrlen
))
2351 if (strcmp(fstr
, SN_X9_62_prime_field
) == 0)
2352 return NID_X9_62_prime_field
;
2353 else if (strcmp(fstr
, SN_X9_62_characteristic_two_field
))
2354 return NID_X9_62_characteristic_two_field
;