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 const char *engine_name = e != NULL ? ENGINE_get_name(e) : NULL;
325 const char *cipher_name = EVP_CIPHER_name(cipher);
326 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
327 OPENSSL_CTX *libctx =
328 prov == NULL ? NULL : ossl_provider_library_context(prov);
329 EVP_PKEY *ret = EVP_PKEY_new();
330 EVP_MAC *cmac = EVP_MAC_fetch(libctx, "CMAC", NULL);
331 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
332 OSSL_PARAM params[4];
337 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
338 /* EVPerr already called */
342 if (engine_name != NULL)
344 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
346 strlen(engine_name) + 1);
348 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM,
350 strlen(cipher_name) + 1);
352 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
354 params[paramsn] = OSSL_PARAM_construct_end();
356 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
357 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
361 ret->pkey.ptr = cmctx;
366 EVP_MAC_CTX_free(cmctx);
370 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
371 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
376 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
378 return pkey_set_type(pkey, NULL, type, NULL, -1);
381 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
383 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
386 int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
388 if (pkey->type == type) {
389 return 1; /* it already is that type */
393 * The application is requesting to alias this to a different pkey type,
394 * but not one that resolves to the base type.
396 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
397 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
405 #ifndef OPENSSL_NO_ENGINE
406 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
409 if (!ENGINE_init(e)) {
410 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
413 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
415 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
419 ENGINE_finish(pkey->pmeth_engine);
420 pkey->pmeth_engine = e;
424 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
429 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
431 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
433 pkey->pkey.ptr = key;
434 return (key != NULL);
437 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
439 return pkey->pkey.ptr;
442 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
444 ASN1_OCTET_STRING *os = NULL;
445 if (pkey->type != EVP_PKEY_HMAC) {
446 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
449 os = EVP_PKEY_get0(pkey);
454 #ifndef OPENSSL_NO_POLY1305
455 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
457 ASN1_OCTET_STRING *os = NULL;
458 if (pkey->type != EVP_PKEY_POLY1305) {
459 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
462 os = EVP_PKEY_get0(pkey);
468 #ifndef OPENSSL_NO_SIPHASH
469 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
471 ASN1_OCTET_STRING *os = NULL;
473 if (pkey->type != EVP_PKEY_SIPHASH) {
474 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
477 os = EVP_PKEY_get0(pkey);
483 #ifndef OPENSSL_NO_RSA
484 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
486 int ret = EVP_PKEY_assign_RSA(pkey, key);
492 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
494 if (pkey->type != EVP_PKEY_RSA) {
495 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
498 return pkey->pkey.rsa;
501 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
503 RSA *ret = EVP_PKEY_get0_RSA(pkey);
510 #ifndef OPENSSL_NO_DSA
511 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
513 int ret = EVP_PKEY_assign_DSA(pkey, key);
519 DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
521 if (pkey->type != EVP_PKEY_DSA) {
522 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
525 return pkey->pkey.dsa;
528 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
530 DSA *ret = EVP_PKEY_get0_DSA(pkey);
537 #ifndef OPENSSL_NO_EC
539 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
541 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
547 EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
549 if (pkey->type != EVP_PKEY_EC) {
550 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
553 return pkey->pkey.ec;
556 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
558 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
565 #ifndef OPENSSL_NO_DH
567 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
569 int ret = EVP_PKEY_assign_DH(pkey, key);
575 DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
577 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
578 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
581 return pkey->pkey.dh;
584 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
586 DH *ret = EVP_PKEY_get0_DH(pkey);
593 int EVP_PKEY_type(int type)
596 const EVP_PKEY_ASN1_METHOD *ameth;
598 ameth = EVP_PKEY_asn1_find(&e, type);
600 ret = ameth->pkey_id;
603 #ifndef OPENSSL_NO_ENGINE
609 int EVP_PKEY_id(const EVP_PKEY *pkey)
614 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
616 return EVP_PKEY_type(pkey->type);
619 void EVP_PKEY_free(EVP_PKEY *x)
626 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
627 REF_PRINT_COUNT("EVP_PKEY", x);
630 REF_ASSERT_ISNT(i < 0);
632 CRYPTO_THREAD_lock_free(x->lock);
633 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
637 static void EVP_PKEY_free_it(EVP_PKEY *x)
639 /* internal function; x is never NULL */
641 evp_keymgmt_clear_pkey_cache(x);
643 if (x->ameth && x->ameth->pkey_free) {
644 x->ameth->pkey_free(x);
647 #ifndef OPENSSL_NO_ENGINE
648 ENGINE_finish(x->engine);
650 ENGINE_finish(x->pmeth_engine);
651 x->pmeth_engine = NULL;
655 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
658 BIO_indent(out, indent, 128);
659 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
660 kstr, OBJ_nid2ln(pkey->type));
664 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
665 int indent, ASN1_PCTX *pctx)
667 if (pkey->ameth && pkey->ameth->pub_print)
668 return pkey->ameth->pub_print(out, pkey, indent, pctx);
670 return unsup_alg(out, pkey, indent, "Public Key");
673 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
674 int indent, ASN1_PCTX *pctx)
676 if (pkey->ameth && pkey->ameth->priv_print)
677 return pkey->ameth->priv_print(out, pkey, indent, pctx);
679 return unsup_alg(out, pkey, indent, "Private Key");
682 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
683 int indent, ASN1_PCTX *pctx)
685 if (pkey->ameth && pkey->ameth->param_print)
686 return pkey->ameth->param_print(out, pkey, indent, pctx);
687 return unsup_alg(out, pkey, indent, "Parameters");
690 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
692 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
694 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
697 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
699 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
702 int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
706 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
709 * If there is a mandatory default digest and this isn't it, then
710 * the answer is 'no'.
712 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
714 return (nid == default_nid);
715 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
722 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
723 const unsigned char *pt, size_t ptlen)
727 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
733 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
736 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);