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/core_names.h>
26 #include "internal/asn1_int.h"
27 #include "internal/evp_int.h"
28 #include "internal/provider.h"
30 static void EVP_PKEY_free_it(EVP_PKEY *x);
32 int EVP_PKEY_bits(const EVP_PKEY *pkey)
34 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
35 return pkey->ameth->pkey_bits(pkey);
39 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
43 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
45 return pkey->ameth->pkey_security_bits(pkey);
48 int EVP_PKEY_size(const EVP_PKEY *pkey)
50 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
51 return pkey->ameth->pkey_size(pkey);
55 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
57 #ifndef OPENSSL_NO_DSA
58 if (pkey->type == EVP_PKEY_DSA) {
59 int ret = pkey->save_parameters;
62 pkey->save_parameters = mode;
67 if (pkey->type == EVP_PKEY_EC) {
68 int ret = pkey->save_parameters;
71 pkey->save_parameters = mode;
78 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
80 if (to->type == EVP_PKEY_NONE) {
81 if (EVP_PKEY_set_type(to, from->type) == 0)
83 } else if (to->type != from->type) {
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
88 if (EVP_PKEY_missing_parameters(from)) {
89 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
93 if (!EVP_PKEY_missing_parameters(to)) {
94 if (EVP_PKEY_cmp_parameters(to, from) == 1)
96 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
100 if (from->ameth && from->ameth->param_copy)
101 return from->ameth->param_copy(to, from);
106 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
108 if (pkey->ameth && pkey->ameth->param_missing)
109 return pkey->ameth->param_missing(pkey);
113 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
115 if (a->type != b->type)
117 if (a->ameth && a->ameth->param_cmp)
118 return a->ameth->param_cmp(a, b);
122 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
124 if (a->type != b->type)
129 /* Compare parameters if the algorithm has them */
130 if (a->ameth->param_cmp) {
131 ret = a->ameth->param_cmp(a, b);
136 if (a->ameth->pub_cmp)
137 return a->ameth->pub_cmp(a, b);
143 EVP_PKEY *EVP_PKEY_new(void)
145 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
148 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
151 ret->type = EVP_PKEY_NONE;
152 ret->save_type = EVP_PKEY_NONE;
154 ret->save_parameters = 1;
155 ret->lock = CRYPTO_THREAD_lock_new();
156 if (ret->lock == NULL) {
157 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
164 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
168 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
171 REF_PRINT_COUNT("EVP_PKEY", pkey);
172 REF_ASSERT_ISNT(i < 2);
173 return ((i > 1) ? 1 : 0);
177 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
178 * is NULL just return 1 or 0 if the algorithm exists.
181 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
184 const EVP_PKEY_ASN1_METHOD *ameth;
185 ENGINE **eptr = (e == NULL) ? &e : NULL;
189 EVP_PKEY_free_it(pkey);
191 * If key type matches and a method exists then this lookup has
192 * succeeded once so just indicate success.
194 if ((type == pkey->save_type) && pkey->ameth)
196 #ifndef OPENSSL_NO_ENGINE
197 /* If we have ENGINEs release them */
198 ENGINE_finish(pkey->engine);
200 ENGINE_finish(pkey->pmeth_engine);
201 pkey->pmeth_engine = NULL;
205 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
207 ameth = EVP_PKEY_asn1_find(eptr, type);
208 #ifndef OPENSSL_NO_ENGINE
209 if (pkey == NULL && eptr != NULL)
213 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
220 pkey->type = pkey->ameth->pkey_id;
221 pkey->save_type = type;
226 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
227 const unsigned char *priv,
230 EVP_PKEY *ret = EVP_PKEY_new();
233 || !pkey_set_type(ret, e, type, NULL, -1)) {
234 /* EVPerr already called */
238 if (ret->ameth->set_priv_key == NULL) {
239 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
240 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
244 if (!ret->ameth->set_priv_key(ret, priv, len)) {
245 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
256 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
257 const unsigned char *pub,
260 EVP_PKEY *ret = EVP_PKEY_new();
263 || !pkey_set_type(ret, e, type, NULL, -1)) {
264 /* EVPerr already called */
268 if (ret->ameth->set_pub_key == NULL) {
269 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
270 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274 if (!ret->ameth->set_pub_key(ret, pub, len)) {
275 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
286 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
289 if (pkey->ameth->get_priv_key == NULL) {
290 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
291 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
295 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
296 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
303 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
306 if (pkey->ameth->get_pub_key == NULL) {
307 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
308 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
312 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
313 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
320 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
321 size_t len, const EVP_CIPHER *cipher)
323 #ifndef OPENSSL_NO_CMAC
324 # ifndef OPENSSL_NO_ENGINE
325 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
327 const char *cipher_name = EVP_CIPHER_name(cipher);
328 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
329 OPENSSL_CTX *libctx =
330 prov == NULL ? NULL : ossl_provider_library_context(prov);
331 EVP_PKEY *ret = EVP_PKEY_new();
332 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
333 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
334 OSSL_PARAM params[4];
339 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
340 /* EVPerr already called */
344 # ifndef OPENSSL_NO_ENGINE
345 if (engine_id != NULL)
347 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
348 (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) {
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 if (pkey->ameth && pkey->ameth->pub_print)
671 return pkey->ameth->pub_print(out, pkey, indent, pctx);
673 return unsup_alg(out, pkey, indent, "Public Key");
676 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
677 int indent, ASN1_PCTX *pctx)
679 if (pkey->ameth && pkey->ameth->priv_print)
680 return pkey->ameth->priv_print(out, pkey, indent, pctx);
682 return unsup_alg(out, pkey, indent, "Private Key");
685 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
686 int indent, ASN1_PCTX *pctx)
688 if (pkey->ameth && pkey->ameth->param_print)
689 return pkey->ameth->param_print(out, pkey, indent, pctx);
690 return unsup_alg(out, pkey, indent, "Parameters");
693 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
695 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
697 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
700 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
702 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
705 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
709 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
712 * If there is a mandatory default digest and this isn't it, then
713 * the answer is 'no'.
715 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
717 return (nid == default_nid);
718 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
725 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
726 const unsigned char *pt, size_t ptlen)
730 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
736 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
739 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);