2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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/engine.h>
23 #include "internal/asn1_int.h"
24 #include "internal/evp_int.h"
26 static void EVP_PKEY_free_it(EVP_PKEY *x);
28 int EVP_PKEY_bits(const EVP_PKEY *pkey)
30 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
31 return pkey->ameth->pkey_bits(pkey);
35 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
39 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
41 return pkey->ameth->pkey_security_bits(pkey);
44 int EVP_PKEY_size(EVP_PKEY *pkey)
46 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
47 return pkey->ameth->pkey_size(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;
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->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);
139 EVP_PKEY *EVP_PKEY_new(void)
141 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
144 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
147 ret->type = EVP_PKEY_NONE;
148 ret->save_type = EVP_PKEY_NONE;
150 ret->save_parameters = 1;
151 ret->lock = CRYPTO_THREAD_lock_new();
152 if (ret->lock == NULL) {
153 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
160 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
164 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
167 REF_PRINT_COUNT("EVP_PKEY", pkey);
168 REF_ASSERT_ISNT(i < 2);
169 return ((i > 1) ? 1 : 0);
173 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
174 * is NULL just return 1 or 0 if the algorithm exists.
177 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
180 const EVP_PKEY_ASN1_METHOD *ameth;
181 ENGINE **eptr = (e == NULL) ? &e : NULL;
185 EVP_PKEY_free_it(pkey);
187 * If key type matches and a method exists then this lookup has
188 * succeeded once so just indicate success.
190 if ((type == pkey->save_type) && pkey->ameth)
192 #ifndef OPENSSL_NO_ENGINE
193 /* If we have ENGINEs release them */
194 ENGINE_finish(pkey->engine);
196 ENGINE_finish(pkey->pmeth_engine);
197 pkey->pmeth_engine = NULL;
201 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
203 ameth = EVP_PKEY_asn1_find(eptr, type);
204 #ifndef OPENSSL_NO_ENGINE
205 if (pkey == NULL && eptr != NULL)
209 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
216 pkey->type = pkey->ameth->pkey_id;
217 pkey->save_type = type;
222 EVP_PKEY *EVP_PKEY_new_private_key(int type, ENGINE *e,
223 const unsigned char *priv,
226 EVP_PKEY *ret = EVP_PKEY_new();
229 || !pkey_set_type(ret, e, type, NULL, -1)) {
230 /* EVPerr already called */
234 if (ret->ameth->set_priv_key == NULL) {
235 EVPerr(EVP_F_EVP_PKEY_NEW_PRIVATE_KEY,
236 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
240 if (!ret->ameth->set_priv_key(ret, priv, len)) {
241 /* We assume the method function calls EVPerr */
252 EVP_PKEY *EVP_PKEY_new_public_key(int type, ENGINE *e,
253 const unsigned char *pub,
256 EVP_PKEY *ret = EVP_PKEY_new();
259 || !pkey_set_type(ret, e, type, NULL, -1)) {
260 /* EVPerr already called */
264 if (ret->ameth->set_pub_key == NULL) {
265 EVPerr(EVP_F_EVP_PKEY_NEW_PUBLIC_KEY,
266 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
270 if (!ret->ameth->set_pub_key(ret, pub, len)) {
271 /* We assume the method function calls EVPerr */
283 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
285 return pkey_set_type(pkey, NULL, type, NULL, -1);
288 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
290 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
292 #ifndef OPENSSL_NO_ENGINE
293 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
296 if (!ENGINE_init(e)) {
297 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
300 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
302 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
306 ENGINE_finish(pkey->pmeth_engine);
307 pkey->pmeth_engine = e;
311 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
313 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
315 pkey->pkey.ptr = key;
316 return (key != NULL);
319 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
321 return pkey->pkey.ptr;
324 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
326 ASN1_OCTET_STRING *os = NULL;
327 if (pkey->type != EVP_PKEY_HMAC) {
328 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
331 os = EVP_PKEY_get0(pkey);
336 #ifndef OPENSSL_NO_POLY1305
337 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
339 ASN1_OCTET_STRING *os = NULL;
340 if (pkey->type != EVP_PKEY_POLY1305) {
341 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
344 os = EVP_PKEY_get0(pkey);
350 #ifndef OPENSSL_NO_SIPHASH
351 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
353 ASN1_OCTET_STRING *os = NULL;
355 if (pkey->type != EVP_PKEY_SIPHASH) {
356 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
359 os = EVP_PKEY_get0(pkey);
365 #ifndef OPENSSL_NO_RSA
366 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
368 int ret = EVP_PKEY_assign_RSA(pkey, key);
374 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
376 if (pkey->type != EVP_PKEY_RSA) {
377 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
380 return pkey->pkey.rsa;
383 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
385 RSA *ret = EVP_PKEY_get0_RSA(pkey);
392 #ifndef OPENSSL_NO_DSA
393 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
395 int ret = EVP_PKEY_assign_DSA(pkey, key);
401 DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
403 if (pkey->type != EVP_PKEY_DSA) {
404 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
407 return pkey->pkey.dsa;
410 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
412 DSA *ret = EVP_PKEY_get0_DSA(pkey);
419 #ifndef OPENSSL_NO_EC
421 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
423 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
429 EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
431 if (pkey->type != EVP_PKEY_EC) {
432 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
435 return pkey->pkey.ec;
438 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
440 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
447 #ifndef OPENSSL_NO_DH
449 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
451 int ret = EVP_PKEY_assign_DH(pkey, key);
457 DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
459 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
460 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
463 return pkey->pkey.dh;
466 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
468 DH *ret = EVP_PKEY_get0_DH(pkey);
475 int EVP_PKEY_type(int type)
478 const EVP_PKEY_ASN1_METHOD *ameth;
480 ameth = EVP_PKEY_asn1_find(&e, type);
482 ret = ameth->pkey_id;
485 #ifndef OPENSSL_NO_ENGINE
491 int EVP_PKEY_id(const EVP_PKEY *pkey)
496 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
498 return EVP_PKEY_type(pkey->type);
501 void EVP_PKEY_free(EVP_PKEY *x)
508 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
509 REF_PRINT_COUNT("EVP_PKEY", x);
512 REF_ASSERT_ISNT(i < 0);
514 CRYPTO_THREAD_lock_free(x->lock);
515 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
519 static void EVP_PKEY_free_it(EVP_PKEY *x)
521 /* internal function; x is never NULL */
522 if (x->ameth && x->ameth->pkey_free) {
523 x->ameth->pkey_free(x);
526 #ifndef OPENSSL_NO_ENGINE
527 ENGINE_finish(x->engine);
529 ENGINE_finish(x->pmeth_engine);
530 x->pmeth_engine = NULL;
534 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
537 BIO_indent(out, indent, 128);
538 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
539 kstr, OBJ_nid2ln(pkey->type));
543 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
544 int indent, ASN1_PCTX *pctx)
546 if (pkey->ameth && pkey->ameth->pub_print)
547 return pkey->ameth->pub_print(out, pkey, indent, pctx);
549 return unsup_alg(out, pkey, indent, "Public Key");
552 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
553 int indent, ASN1_PCTX *pctx)
555 if (pkey->ameth && pkey->ameth->priv_print)
556 return pkey->ameth->priv_print(out, pkey, indent, pctx);
558 return unsup_alg(out, pkey, indent, "Private Key");
561 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
562 int indent, ASN1_PCTX *pctx)
564 if (pkey->ameth && pkey->ameth->param_print)
565 return pkey->ameth->param_print(out, pkey, indent, pctx);
566 return unsup_alg(out, pkey, indent, "Parameters");
569 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
571 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
573 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
576 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
578 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
581 int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
582 const unsigned char *pt, size_t ptlen)
586 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
592 size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
595 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);