2 * Copyright 1995-2018 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 #include "internal/cryptlib.h"
12 #include "internal/refcount.h"
13 #include <openssl/bn.h>
14 #include <openssl/err.h>
15 #include <openssl/objects.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509.h>
18 #include <openssl/rsa.h>
19 #include <openssl/dsa.h>
20 #include <openssl/dh.h>
21 #include <openssl/cmac.h>
22 #include <openssl/engine.h>
23 #include <openssl/params.h>
24 #include <openssl/serializer.h>
25 #include <openssl/core_names.h>
27 #include "crypto/asn1.h"
28 #include "crypto/evp.h"
29 #include "internal/provider.h"
31 static void evp_pkey_free_it(EVP_PKEY *key);
35 int EVP_PKEY_bits(const EVP_PKEY *pkey)
37 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
38 return pkey->ameth->pkey_bits(pkey);
42 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
46 if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
48 return pkey->ameth->pkey_security_bits(pkey);
51 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
53 # ifndef OPENSSL_NO_DSA
54 if (pkey->type == EVP_PKEY_DSA) {
55 int ret = pkey->save_parameters;
58 pkey->save_parameters = mode;
62 # ifndef OPENSSL_NO_EC
63 if (pkey->type == EVP_PKEY_EC) {
64 int ret = pkey->save_parameters;
67 pkey->save_parameters = mode;
74 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
76 if (to->type == EVP_PKEY_NONE) {
77 if (EVP_PKEY_set_type(to, from->type) == 0)
79 } else if (to->type != from->type) {
80 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
84 if (EVP_PKEY_missing_parameters(from)) {
85 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
89 if (!EVP_PKEY_missing_parameters(to)) {
90 if (EVP_PKEY_cmp_parameters(to, from) == 1)
92 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
96 if (from->ameth && from->ameth->param_copy)
97 return from->ameth->param_copy(to, from);
102 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
104 if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
105 return pkey->ameth->param_missing(pkey);
109 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
111 if (a->type != b->type)
113 if (a->ameth && a->ameth->param_cmp)
114 return a->ameth->param_cmp(a, b);
118 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
120 if (a->type != b->type)
125 /* Compare parameters if the algorithm has them */
126 if (a->ameth->param_cmp) {
127 ret = a->ameth->param_cmp(a, b);
132 if (a->ameth->pub_cmp)
133 return a->ameth->pub_cmp(a, b);
141 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
142 * is NULL just return 1 or 0 if the algorithm exists.
145 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
148 const EVP_PKEY_ASN1_METHOD *ameth;
149 ENGINE **eptr = (e == NULL) ? &e : NULL;
153 evp_pkey_free_it(pkey);
155 * If key type matches and a method exists then this lookup has
156 * succeeded once so just indicate success.
158 if ((type == pkey->save_type) && pkey->ameth)
160 # ifndef OPENSSL_NO_ENGINE
161 /* If we have ENGINEs release them */
162 ENGINE_finish(pkey->engine);
164 ENGINE_finish(pkey->pmeth_engine);
165 pkey->pmeth_engine = NULL;
169 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
171 ameth = EVP_PKEY_asn1_find(eptr, type);
172 # ifndef OPENSSL_NO_ENGINE
173 if (pkey == NULL && eptr != NULL)
177 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
184 pkey->type = pkey->ameth->pkey_id;
185 pkey->save_type = type;
190 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
191 const unsigned char *priv,
194 EVP_PKEY *ret = EVP_PKEY_new();
197 || !pkey_set_type(ret, e, type, NULL, -1)) {
198 /* EVPerr already called */
202 if (ret->ameth->set_priv_key == NULL) {
203 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
204 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
208 if (!ret->ameth->set_priv_key(ret, priv, len)) {
209 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
220 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
221 const unsigned char *pub,
224 EVP_PKEY *ret = EVP_PKEY_new();
227 || !pkey_set_type(ret, e, type, NULL, -1)) {
228 /* EVPerr already called */
232 if (ret->ameth->set_pub_key == NULL) {
233 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
234 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
238 if (!ret->ameth->set_pub_key(ret, pub, len)) {
239 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
250 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
253 if (pkey->ameth->get_priv_key == NULL) {
254 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
255 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
259 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
260 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
267 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
270 if (pkey->ameth->get_pub_key == NULL) {
271 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
272 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
276 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
277 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
284 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
285 size_t len, const EVP_CIPHER *cipher)
287 # ifndef OPENSSL_NO_CMAC
288 # ifndef OPENSSL_NO_ENGINE
289 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
291 const char *cipher_name = EVP_CIPHER_name(cipher);
292 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
293 OPENSSL_CTX *libctx =
294 prov == NULL ? NULL : ossl_provider_library_context(prov);
295 EVP_PKEY *ret = EVP_PKEY_new();
296 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
297 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
298 OSSL_PARAM params[4];
303 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
304 /* EVPerr already called */
308 # ifndef OPENSSL_NO_ENGINE
309 if (engine_id != NULL)
311 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
315 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
316 (char *)cipher_name, 0);
318 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
320 params[paramsn] = OSSL_PARAM_construct_end();
322 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
323 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
327 ret->pkey.ptr = cmctx;
332 EVP_MAC_CTX_free(cmctx);
336 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
337 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
342 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
344 return pkey_set_type(pkey, NULL, type, NULL, -1);
347 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
349 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
352 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
354 if (pkey->type == type) {
355 return 1; /* it already is that type */
359 * The application is requesting to alias this to a different pkey type,
360 * but not one that resolves to the base type.
362 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
363 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
371 # ifndef OPENSSL_NO_ENGINE
372 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
375 if (!ENGINE_init(e)) {
376 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
379 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
381 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
385 ENGINE_finish(pkey->pmeth_engine);
386 pkey->pmeth_engine = e;
390 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
395 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
397 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
399 pkey->pkey.ptr = key;
400 return (key != NULL);
403 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
405 return pkey->pkey.ptr;
408 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
410 ASN1_OCTET_STRING *os = NULL;
411 if (pkey->type != EVP_PKEY_HMAC) {
412 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
415 os = EVP_PKEY_get0(pkey);
420 # ifndef OPENSSL_NO_POLY1305
421 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
423 ASN1_OCTET_STRING *os = NULL;
424 if (pkey->type != EVP_PKEY_POLY1305) {
425 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
428 os = EVP_PKEY_get0(pkey);
434 # ifndef OPENSSL_NO_SIPHASH
435 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
437 ASN1_OCTET_STRING *os = NULL;
439 if (pkey->type != EVP_PKEY_SIPHASH) {
440 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
443 os = EVP_PKEY_get0(pkey);
449 # ifndef OPENSSL_NO_RSA
450 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
452 int ret = EVP_PKEY_assign_RSA(pkey, key);
458 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
460 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
461 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
464 return pkey->pkey.rsa;
467 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
469 RSA *ret = EVP_PKEY_get0_RSA(pkey);
476 # ifndef OPENSSL_NO_DSA
477 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
479 int ret = EVP_PKEY_assign_DSA(pkey, key);
485 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
487 if (pkey->type != EVP_PKEY_DSA) {
488 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
491 return pkey->pkey.dsa;
494 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
496 DSA *ret = EVP_PKEY_get0_DSA(pkey);
503 # ifndef OPENSSL_NO_EC
505 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
507 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
513 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
515 if (pkey->type != EVP_PKEY_EC) {
516 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
519 return pkey->pkey.ec;
522 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
524 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
531 # ifndef OPENSSL_NO_DH
533 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
535 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
536 int ret = EVP_PKEY_assign(pkey, type, key);
543 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
545 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
546 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
549 return pkey->pkey.dh;
552 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
554 DH *ret = EVP_PKEY_get0_DH(pkey);
561 int EVP_PKEY_type(int type)
564 const EVP_PKEY_ASN1_METHOD *ameth;
566 ameth = EVP_PKEY_asn1_find(&e, type);
568 ret = ameth->pkey_id;
571 # ifndef OPENSSL_NO_ENGINE
577 int EVP_PKEY_id(const EVP_PKEY *pkey)
582 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
584 return EVP_PKEY_type(pkey->type);
588 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
590 BIO_set_indent(*out, saved_indent);
592 BIO *next = BIO_pop(*out);
600 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
606 long i = BIO_get_indent(*out);
608 *saved_indent = (i < 0 ? 0 : i);
609 if (BIO_set_indent(*out, indent) <= 0) {
610 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
614 if (BIO_set_indent(*out, indent) <= 0) {
615 print_reset_indent(out, *pop_f_prefix, *saved_indent);
622 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
625 return BIO_indent(out, indent, 128)
626 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
627 kstr, OBJ_nid2ln(pkey->type)) > 0;
630 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
631 const char *propquery /* For provided serialization */,
632 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
633 int indent, ASN1_PCTX *pctx),
634 ASN1_PCTX *legacy_pctx /* For legacy print */)
638 OSSL_SERIALIZER_CTX *ctx = NULL;
639 int ret = -2; /* default to unsupported */
641 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
644 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
645 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
646 ret = OSSL_SERIALIZER_to_bio(ctx, out);
647 OSSL_SERIALIZER_CTX_free(ctx);
652 /* legacy fallback */
653 if (legacy_print != NULL)
654 ret = legacy_print(out, pkey, 0, legacy_pctx);
656 ret = unsup_alg(out, pkey, 0, "Public Key");
659 print_reset_indent(&out, pop_f_prefix, saved_indent);
663 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
664 int indent, ASN1_PCTX *pctx)
666 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
667 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
671 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
672 int indent, ASN1_PCTX *pctx)
674 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
675 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
679 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
680 int indent, ASN1_PCTX *pctx)
682 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
683 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
687 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
689 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
691 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
694 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
696 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
699 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
703 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
706 * If there is a mandatory default digest and this isn't it, then
707 * the answer is 'no'.
709 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
711 return (nid == default_nid);
712 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
719 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
720 const unsigned char *pt, size_t ptlen)
724 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
730 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
733 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
739 #endif /* FIPS_MODE */
741 /*- All methods below can also be used in FIPS_MODE */
743 EVP_PKEY *EVP_PKEY_new(void)
745 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
748 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
751 ret->type = EVP_PKEY_NONE;
752 ret->save_type = EVP_PKEY_NONE;
754 ret->save_parameters = 1;
755 ret->lock = CRYPTO_THREAD_lock_new();
756 if (ret->lock == NULL) {
757 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
764 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
768 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
771 REF_PRINT_COUNT("EVP_PKEY", pkey);
772 REF_ASSERT_ISNT(i < 2);
773 return ((i > 1) ? 1 : 0);
776 static void evp_pkey_free_it(EVP_PKEY *x)
778 /* internal function; x is never NULL */
780 evp_keymgmt_clear_pkey_cache(x);
782 if (x->ameth && x->ameth->pkey_free) {
783 x->ameth->pkey_free(x);
786 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
787 ENGINE_finish(x->engine);
789 ENGINE_finish(x->pmeth_engine);
790 x->pmeth_engine = NULL;
794 void EVP_PKEY_free(EVP_PKEY *x)
801 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
802 REF_PRINT_COUNT("EVP_PKEY", x);
805 REF_ASSERT_ISNT(i < 0);
807 CRYPTO_THREAD_lock_free(x->lock);
809 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
814 /* TODO (3.0) : Needs to call getparams fo non legacy case */
815 int EVP_PKEY_size(const EVP_PKEY *pkey)
817 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
818 return pkey->ameth->pkey_size(pkey);