2 * Copyright 1995-2016 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 <openssl/bn.h>
13 #include <openssl/err.h>
14 #include <openssl/objects.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509.h>
17 #include <openssl/rsa.h>
18 #include <openssl/dsa.h>
19 #include <openssl/dh.h>
20 #include <openssl/engine.h>
22 #include "internal/asn1_int.h"
23 #include "internal/evp_int.h"
25 static void EVP_PKEY_free_it(EVP_PKEY *x);
27 int EVP_PKEY_bits(EVP_PKEY *pkey)
29 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
30 return pkey->ameth->pkey_bits(pkey);
34 int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
38 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
40 return pkey->ameth->pkey_security_bits(pkey);
43 int EVP_PKEY_size(EVP_PKEY *pkey)
45 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
46 return pkey->ameth->pkey_size(pkey);
50 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
52 #ifndef OPENSSL_NO_DSA
53 if (pkey->type == EVP_PKEY_DSA) {
54 int ret = pkey->save_parameters;
57 pkey->save_parameters = mode;
62 if (pkey->type == EVP_PKEY_EC) {
63 int ret = pkey->save_parameters;
66 pkey->save_parameters = mode;
73 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
75 if (to->type == EVP_PKEY_NONE) {
76 if (EVP_PKEY_set_type(to, from->type) == 0)
78 } else if (to->type != from->type) {
79 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
83 if (EVP_PKEY_missing_parameters(from)) {
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
87 if (from->ameth && from->ameth->param_copy)
88 return from->ameth->param_copy(to, from);
93 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
95 if (pkey->ameth && pkey->ameth->param_missing)
96 return pkey->ameth->param_missing(pkey);
100 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
102 if (a->type != b->type)
104 if (a->ameth && a->ameth->param_cmp)
105 return a->ameth->param_cmp(a, b);
109 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
111 if (a->type != b->type)
116 /* Compare parameters if the algorithm has them */
117 if (a->ameth->param_cmp) {
118 ret = a->ameth->param_cmp(a, b);
123 if (a->ameth->pub_cmp)
124 return a->ameth->pub_cmp(a, b);
130 EVP_PKEY *EVP_PKEY_new(void)
132 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
135 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
138 ret->type = EVP_PKEY_NONE;
139 ret->save_type = EVP_PKEY_NONE;
141 ret->save_parameters = 1;
142 ret->lock = CRYPTO_THREAD_lock_new();
143 if (ret->lock == NULL) {
144 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
151 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
155 if (CRYPTO_atomic_add(&pkey->references, 1, &i, pkey->lock) <= 0)
158 REF_PRINT_COUNT("EVP_PKEY", pkey);
159 REF_ASSERT_ISNT(i < 2);
160 return ((i > 1) ? 1 : 0);
164 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
165 * is NULL just return 1 or 0 if the algorithm exists.
168 static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
170 const EVP_PKEY_ASN1_METHOD *ameth;
174 EVP_PKEY_free_it(pkey);
176 * If key type matches and a method exists then this lookup has
177 * succeeded once so just indicate success.
179 if ((type == pkey->save_type) && pkey->ameth)
181 #ifndef OPENSSL_NO_ENGINE
182 /* If we have an ENGINE release it */
183 ENGINE_finish(pkey->engine);
188 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
190 ameth = EVP_PKEY_asn1_find(&e, type);
191 #ifndef OPENSSL_NO_ENGINE
196 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
203 pkey->type = pkey->ameth->pkey_id;
204 pkey->save_type = type;
209 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
211 return pkey_set_type(pkey, type, NULL, -1);
214 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
216 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
219 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
221 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
223 pkey->pkey.ptr = key;
224 return (key != NULL);
227 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
229 return pkey->pkey.ptr;
232 #ifndef OPENSSL_NO_RSA
233 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
235 int ret = EVP_PKEY_assign_RSA(pkey, key);
241 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
243 if (pkey->type != EVP_PKEY_RSA) {
244 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
247 return pkey->pkey.rsa;
250 RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
252 RSA *ret = EVP_PKEY_get0_RSA(pkey);
259 #ifndef OPENSSL_NO_DSA
260 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
262 int ret = EVP_PKEY_assign_DSA(pkey, key);
268 DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
270 if (pkey->type != EVP_PKEY_DSA) {
271 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
274 return pkey->pkey.dsa;
277 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
279 DSA *ret = EVP_PKEY_get0_DSA(pkey);
286 #ifndef OPENSSL_NO_EC
288 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
290 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
296 EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
298 if (pkey->type != EVP_PKEY_EC) {
299 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
302 return pkey->pkey.ec;
305 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
307 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
314 #ifndef OPENSSL_NO_DH
316 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
318 int ret = EVP_PKEY_assign_DH(pkey, key);
324 DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
326 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
327 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
330 return pkey->pkey.dh;
333 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
335 DH *ret = EVP_PKEY_get0_DH(pkey);
342 int EVP_PKEY_type(int type)
345 const EVP_PKEY_ASN1_METHOD *ameth;
347 ameth = EVP_PKEY_asn1_find(&e, type);
349 ret = ameth->pkey_id;
352 #ifndef OPENSSL_NO_ENGINE
358 int EVP_PKEY_id(const EVP_PKEY *pkey)
363 int EVP_PKEY_base_id(const EVP_PKEY *pkey)
365 return EVP_PKEY_type(pkey->type);
368 void EVP_PKEY_free(EVP_PKEY *x)
375 CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
376 REF_PRINT_COUNT("EVP_PKEY", x);
379 REF_ASSERT_ISNT(i < 0);
381 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
385 static void EVP_PKEY_free_it(EVP_PKEY *x)
387 /* internal function; x is never NULL */
388 if (x->ameth && x->ameth->pkey_free) {
389 x->ameth->pkey_free(x);
392 #ifndef OPENSSL_NO_ENGINE
393 ENGINE_finish(x->engine);
396 CRYPTO_THREAD_lock_free(x->lock);
399 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
402 BIO_indent(out, indent, 128);
403 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
404 kstr, OBJ_nid2ln(pkey->type));
408 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
409 int indent, ASN1_PCTX *pctx)
411 if (pkey->ameth && pkey->ameth->pub_print)
412 return pkey->ameth->pub_print(out, pkey, indent, pctx);
414 return unsup_alg(out, pkey, indent, "Public Key");
417 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
418 int indent, ASN1_PCTX *pctx)
420 if (pkey->ameth && pkey->ameth->priv_print)
421 return pkey->ameth->priv_print(out, pkey, indent, pctx);
423 return unsup_alg(out, pkey, indent, "Private Key");
426 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
427 int indent, ASN1_PCTX *pctx)
429 if (pkey->ameth && pkey->ameth->param_print)
430 return pkey->ameth->param_print(out, pkey, indent, pctx);
431 return unsup_alg(out, pkey, indent, "Parameters");
434 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
436 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
438 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,