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 *x);
33 int EVP_PKEY_bits(const EVP_PKEY *pkey)
35 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
36 return pkey->ameth->pkey_bits(pkey);
40 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
44 if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL)
46 return pkey->ameth->pkey_security_bits(pkey);
49 int EVP_PKEY_size(const EVP_PKEY *pkey)
51 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
52 return pkey->ameth->pkey_size(pkey);
56 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
58 #ifndef OPENSSL_NO_DSA
59 if (pkey->type == EVP_PKEY_DSA) {
60 int ret = pkey->save_parameters;
63 pkey->save_parameters = mode;
68 if (pkey->type == EVP_PKEY_EC) {
69 int ret = pkey->save_parameters;
72 pkey->save_parameters = mode;
79 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
81 if (to->type == EVP_PKEY_NONE) {
82 if (EVP_PKEY_set_type(to, from->type) == 0)
84 } else if (to->type != from->type) {
85 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
89 if (EVP_PKEY_missing_parameters(from)) {
90 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
94 if (!EVP_PKEY_missing_parameters(to)) {
95 if (EVP_PKEY_cmp_parameters(to, from) == 1)
97 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
101 if (from->ameth && from->ameth->param_copy)
102 return from->ameth->param_copy(to, from);
107 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
109 if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
110 return pkey->ameth->param_missing(pkey);
114 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
116 if (a->type != b->type)
118 if (a->ameth && a->ameth->param_cmp)
119 return a->ameth->param_cmp(a, b);
123 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
125 if (a->type != b->type)
130 /* Compare parameters if the algorithm has them */
131 if (a->ameth->param_cmp) {
132 ret = a->ameth->param_cmp(a, b);
137 if (a->ameth->pub_cmp)
138 return a->ameth->pub_cmp(a, b);
144 EVP_PKEY *EVP_PKEY_new(void)
146 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
149 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
152 ret->type = EVP_PKEY_NONE;
153 ret->save_type = EVP_PKEY_NONE;
155 ret->save_parameters = 1;
156 ret->lock = CRYPTO_THREAD_lock_new();
157 if (ret->lock == NULL) {
158 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
165 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
169 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
172 REF_PRINT_COUNT("EVP_PKEY", pkey);
173 REF_ASSERT_ISNT(i < 2);
174 return ((i > 1) ? 1 : 0);
178 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
179 * is NULL just return 1 or 0 if the algorithm exists.
182 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
185 const EVP_PKEY_ASN1_METHOD *ameth;
186 ENGINE **eptr = (e == NULL) ? &e : NULL;
190 EVP_PKEY_free_it(pkey);
192 * If key type matches and a method exists then this lookup has
193 * succeeded once so just indicate success.
195 if ((type == pkey->save_type) && pkey->ameth)
197 #ifndef OPENSSL_NO_ENGINE
198 /* If we have ENGINEs release them */
199 ENGINE_finish(pkey->engine);
201 ENGINE_finish(pkey->pmeth_engine);
202 pkey->pmeth_engine = NULL;
206 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
208 ameth = EVP_PKEY_asn1_find(eptr, type);
209 #ifndef OPENSSL_NO_ENGINE
210 if (pkey == NULL && eptr != NULL)
214 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
221 pkey->type = pkey->ameth->pkey_id;
222 pkey->save_type = type;
227 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
228 const unsigned char *priv,
231 EVP_PKEY *ret = EVP_PKEY_new();
234 || !pkey_set_type(ret, e, type, NULL, -1)) {
235 /* EVPerr already called */
239 if (ret->ameth->set_priv_key == NULL) {
240 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
241 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
245 if (!ret->ameth->set_priv_key(ret, priv, len)) {
246 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
257 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
258 const unsigned char *pub,
261 EVP_PKEY *ret = EVP_PKEY_new();
264 || !pkey_set_type(ret, e, type, NULL, -1)) {
265 /* EVPerr already called */
269 if (ret->ameth->set_pub_key == NULL) {
270 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
271 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
275 if (!ret->ameth->set_pub_key(ret, pub, len)) {
276 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
287 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
290 if (pkey->ameth->get_priv_key == NULL) {
291 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
292 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
296 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
297 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
304 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
307 if (pkey->ameth->get_pub_key == NULL) {
308 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
309 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
313 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
314 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
321 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
322 size_t len, const EVP_CIPHER *cipher)
324 #ifndef OPENSSL_NO_CMAC
325 # ifndef OPENSSL_NO_ENGINE
326 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
328 const char *cipher_name = EVP_CIPHER_name(cipher);
329 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
330 OPENSSL_CTX *libctx =
331 prov == NULL ? NULL : ossl_provider_library_context(prov);
332 EVP_PKEY *ret = EVP_PKEY_new();
333 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
334 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
335 OSSL_PARAM params[4];
340 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
341 /* EVPerr already called */
345 # ifndef OPENSSL_NO_ENGINE
346 if (engine_id != NULL)
348 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
352 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
353 (char *)cipher_name, 0);
355 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
357 params[paramsn] = OSSL_PARAM_construct_end();
359 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
360 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
364 ret->pkey.ptr = cmctx;
369 EVP_MAC_CTX_free(cmctx);
373 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
374 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
379 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
381 return pkey_set_type(pkey, NULL, type, NULL, -1);
384 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
386 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
389 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
391 if (pkey->type == type) {
392 return 1; /* it already is that type */
396 * The application is requesting to alias this to a different pkey type,
397 * but not one that resolves to the base type.
399 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
400 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
408 #ifndef OPENSSL_NO_ENGINE
409 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
412 if (!ENGINE_init(e)) {
413 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
416 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
418 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
422 ENGINE_finish(pkey->pmeth_engine);
423 pkey->pmeth_engine = e;
427 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
432 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
434 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
436 pkey->pkey.ptr = key;
437 return (key != NULL);
440 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
442 return pkey->pkey.ptr;
445 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
447 ASN1_OCTET_STRING *os = NULL;
448 if (pkey->type != EVP_PKEY_HMAC) {
449 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
452 os = EVP_PKEY_get0(pkey);
457 #ifndef OPENSSL_NO_POLY1305
458 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
460 ASN1_OCTET_STRING *os = NULL;
461 if (pkey->type != EVP_PKEY_POLY1305) {
462 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
465 os = EVP_PKEY_get0(pkey);
471 #ifndef OPENSSL_NO_SIPHASH
472 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
474 ASN1_OCTET_STRING *os = NULL;
476 if (pkey->type != EVP_PKEY_SIPHASH) {
477 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
480 os = EVP_PKEY_get0(pkey);
486 #ifndef OPENSSL_NO_RSA
487 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
489 int ret = EVP_PKEY_assign_RSA(pkey, key);
495 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
497 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
498 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
501 return pkey->pkey.rsa;
504 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
506 RSA *ret = EVP_PKEY_get0_RSA(pkey);
513 #ifndef OPENSSL_NO_DSA
514 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
516 int ret = EVP_PKEY_assign_DSA(pkey, key);
522 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
524 if (pkey->type != EVP_PKEY_DSA) {
525 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
528 return pkey->pkey.dsa;
531 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
533 DSA *ret = EVP_PKEY_get0_DSA(pkey);
540 #ifndef OPENSSL_NO_EC
542 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
544 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
550 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
552 if (pkey->type != EVP_PKEY_EC) {
553 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
556 return pkey->pkey.ec;
559 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
561 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
568 #ifndef OPENSSL_NO_DH
570 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
572 int ret = EVP_PKEY_assign_DH(pkey, key);
578 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
580 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
581 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
584 return pkey->pkey.dh;
587 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
589 DH *ret = EVP_PKEY_get0_DH(pkey);
596 int EVP_PKEY_type(int type)
599 const EVP_PKEY_ASN1_METHOD *ameth;
601 ameth = EVP_PKEY_asn1_find(&e, type);
603 ret = ameth->pkey_id;
606 #ifndef OPENSSL_NO_ENGINE
612 int EVP_PKEY_id(const EVP_PKEY *pkey)
617 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
619 return EVP_PKEY_type(pkey->type);
622 void EVP_PKEY_free(EVP_PKEY *x)
629 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
630 REF_PRINT_COUNT("EVP_PKEY", x);
633 REF_ASSERT_ISNT(i < 0);
635 CRYPTO_THREAD_lock_free(x->lock);
636 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
640 static void EVP_PKEY_free_it(EVP_PKEY *x)
642 /* internal function; x is never NULL */
644 evp_keymgmt_clear_pkey_cache(x);
646 if (x->ameth && x->ameth->pkey_free) {
647 x->ameth->pkey_free(x);
650 #ifndef OPENSSL_NO_ENGINE
651 ENGINE_finish(x->engine);
653 ENGINE_finish(x->pmeth_engine);
654 x->pmeth_engine = NULL;
658 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
661 BIO_indent(out, indent, 128);
662 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
663 kstr, OBJ_nid2ln(pkey->type));
667 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
668 int indent, ASN1_PCTX *pctx)
670 const char *pq = OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ;
671 OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq);
672 int ret = -2; /* mark as unsupported */
674 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
675 ret = OSSL_SERIALIZER_to_bio(ctx, out);
676 OSSL_SERIALIZER_CTX_free(ctx);
681 /* legacy fallback */
682 if (pkey->ameth && pkey->ameth->pub_print)
683 return pkey->ameth->pub_print(out, pkey, indent, pctx);
685 return unsup_alg(out, pkey, indent, "Public Key");
688 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
689 int indent, ASN1_PCTX *pctx)
691 const char *pq = OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ;
692 OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq);
693 int ret = -2; /* mark as unsupported */
695 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
696 ret = OSSL_SERIALIZER_to_bio(ctx, out);
697 OSSL_SERIALIZER_CTX_free(ctx);
702 /* legacy fallback */
703 if (pkey->ameth && pkey->ameth->priv_print)
704 return pkey->ameth->priv_print(out, pkey, indent, pctx);
706 return unsup_alg(out, pkey, indent, "Private Key");
709 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
710 int indent, ASN1_PCTX *pctx)
712 const char *pq = OSSL_SERIALIZER_Parameters_TO_TEXT_PQ;
713 OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq);
714 int ret = -2; /* mark as unsupported */
716 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
717 ret = OSSL_SERIALIZER_to_bio(ctx, out);
718 OSSL_SERIALIZER_CTX_free(ctx);
723 /* legacy fallback */
724 if (pkey->ameth && pkey->ameth->param_print)
725 return pkey->ameth->param_print(out, pkey, indent, pctx);
727 return unsup_alg(out, pkey, indent, "Parameters");
730 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
732 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
734 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
737 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
739 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
742 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
746 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
749 * If there is a mandatory default digest and this isn't it, then
750 * the answer is 'no'.
752 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
754 return (nid == default_nid);
755 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
762 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
763 const unsigned char *pt, size_t ptlen)
767 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
773 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
776 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);