blob: 221178dd31d40831033f1baaea524971477a25ee [file] [log] [blame]
Ralf S. Engelschall58964a41998-12-21 10:56:39 +00001/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00002 * All rights reserved.
3 *
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
Matt Caswell0f113f32015-01-22 03:40:55 +00007 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
Matt Caswell0f113f32015-01-22 03:40:55 +000014 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000015 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
Matt Caswell0f113f32015-01-22 03:40:55 +000021 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000022 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
Matt Caswell0f113f32015-01-22 03:40:55 +000036 * 4. If you include any Windows specific code (or a derivative thereof) from
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000037 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
Matt Caswell0f113f32015-01-22 03:40:55 +000039 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000040 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50 * SUCH DAMAGE.
Matt Caswell0f113f32015-01-22 03:40:55 +000051 *
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000052 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
56 */
57
58#include <stdio.h>
Richard Levitteb39fc562015-05-14 16:56:48 +020059#include "internal/cryptlib.h"
Bodo Möller4d94ae02002-02-13 18:21:51 +000060#include <openssl/bn.h>
61#include <openssl/err.h>
Bodo Möllerec577821999-04-23 22:13:45 +000062#include <openssl/objects.h>
63#include <openssl/evp.h>
Bodo Möllerec577821999-04-23 22:13:45 +000064#include <openssl/x509.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000065#ifndef OPENSSL_NO_RSA
Matt Caswell0f113f32015-01-22 03:40:55 +000066# include <openssl/rsa.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000067#endif
68#ifndef OPENSSL_NO_DSA
Matt Caswell0f113f32015-01-22 03:40:55 +000069# include <openssl/dsa.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000070#endif
71#ifndef OPENSSL_NO_DH
Matt Caswell0f113f32015-01-22 03:40:55 +000072# include <openssl/dh.h>
Nils Larsch3eeaab42005-07-16 12:37:36 +000073#endif
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000074
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +000075#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +000076# include <openssl/engine.h>
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +000077#endif
78
Dr. Stephen Henson5fe736e2015-03-23 18:42:42 +000079#include "internal/asn1_int.h"
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000080#include "internal/evp_int.h"
Dr. Stephen Henson18e377b2006-03-22 17:59:49 +000081
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000082static void EVP_PKEY_free_it(EVP_PKEY *x);
Ben Lauriebb2297a2001-08-03 18:48:35 +000083
Ulf Möller6b691a51999-04-19 21:31:43 +000084int EVP_PKEY_bits(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +000085{
86 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
87 return pkey->ameth->pkey_bits(pkey);
88 return 0;
89}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000090
Dr. Stephen Henson2514fa72014-01-18 14:51:40 +000091int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +000092{
93 if (pkey == NULL)
94 return 0;
95 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
96 return -2;
97 return pkey->ameth->pkey_security_bits(pkey);
98}
Dr. Stephen Henson2514fa72014-01-18 14:51:40 +000099
Ulf Möller6b691a51999-04-19 21:31:43 +0000100int EVP_PKEY_size(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000101{
102 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
103 return pkey->ameth->pkey_size(pkey);
104 return 0;
105}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000106
Ulf Möller6b691a51999-04-19 21:31:43 +0000107int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
Matt Caswell0f113f32015-01-22 03:40:55 +0000108{
Richard Levittecf1b7d92001-02-19 16:06:34 +0000109#ifndef OPENSSL_NO_DSA
Matt Caswell0f113f32015-01-22 03:40:55 +0000110 if (pkey->type == EVP_PKEY_DSA) {
111 int ret = pkey->save_parameters;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000112
Matt Caswell0f113f32015-01-22 03:40:55 +0000113 if (mode >= 0)
114 pkey->save_parameters = mode;
115 return (ret);
116 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000117#endif
Bodo Möller5488bb62002-08-12 08:47:41 +0000118#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +0000119 if (pkey->type == EVP_PKEY_EC) {
120 int ret = pkey->save_parameters;
Bodo Möller4d94ae02002-02-13 18:21:51 +0000121
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 if (mode >= 0)
123 pkey->save_parameters = mode;
124 return (ret);
125 }
Bodo Möller4d94ae02002-02-13 18:21:51 +0000126#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000127 return (0);
128}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000129
Richard Levittea8b72842003-04-07 10:09:44 +0000130int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
Matt Caswell0f113f32015-01-22 03:40:55 +0000131{
Dr. Stephen Henson2986ecd2015-12-13 17:28:40 +0000132 if (to->type == EVP_PKEY_NONE) {
133 if (EVP_PKEY_set_type(to, from->type) == 0)
134 return 0;
135 } else if (to->type != from->type) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000136 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
137 goto err;
138 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000139
Matt Caswell0f113f32015-01-22 03:40:55 +0000140 if (EVP_PKEY_missing_parameters(from)) {
141 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
142 goto err;
143 }
144 if (from->ameth && from->ameth->param_copy)
145 return from->ameth->param_copy(to, from);
146 err:
147 return 0;
148}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000149
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000150int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000151{
152 if (pkey->ameth && pkey->ameth->param_missing)
153 return pkey->ameth->param_missing(pkey);
154 return 0;
155}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000156
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000157int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000158{
159 if (a->type != b->type)
160 return -1;
161 if (a->ameth && a->ameth->param_cmp)
162 return a->ameth->param_cmp(a, b);
163 return -2;
164}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000165
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000166int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000167{
168 if (a->type != b->type)
169 return -1;
Richard Levittee6526fb2003-04-03 22:27:24 +0000170
Matt Caswell0f113f32015-01-22 03:40:55 +0000171 if (a->ameth) {
172 int ret;
173 /* Compare parameters if the algorithm has them */
174 if (a->ameth->param_cmp) {
175 ret = a->ameth->param_cmp(a, b);
176 if (ret <= 0)
177 return ret;
178 }
Richard Levittea8b72842003-04-07 10:09:44 +0000179
Matt Caswell0f113f32015-01-22 03:40:55 +0000180 if (a->ameth->pub_cmp)
181 return a->ameth->pub_cmp(a, b);
182 }
Richard Levittee6526fb2003-04-03 22:27:24 +0000183
Matt Caswell0f113f32015-01-22 03:40:55 +0000184 return -2;
185}
Richard Levittee6526fb2003-04-03 22:27:24 +0000186
Ulf Möller6b691a51999-04-19 21:31:43 +0000187EVP_PKEY *EVP_PKEY_new(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000188{
FdaSilvaYYa2d0baa2016-02-14 18:44:30 +0100189 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000190
Matt Caswell0f113f32015-01-22 03:40:55 +0000191 if (ret == NULL) {
192 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
193 return (NULL);
194 }
195 ret->type = EVP_PKEY_NONE;
196 ret->save_type = EVP_PKEY_NONE;
197 ret->references = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000198 ret->save_parameters = 1;
199 return (ret);
200}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000201
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000202void EVP_PKEY_up_ref(EVP_PKEY *pkey)
203{
204 CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
205}
206
Matt Caswell0f113f32015-01-22 03:40:55 +0000207/*
208 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
209 * is NULL just return 1 or 0 if the algorithm exists.
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000210 */
211
212static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000213{
214 const EVP_PKEY_ASN1_METHOD *ameth;
215 ENGINE *e = NULL;
216 if (pkey) {
217 if (pkey->pkey.ptr)
218 EVP_PKEY_free_it(pkey);
219 /*
220 * If key type matches and a method exists then this lookup has
221 * succeeded once so just indicate success.
222 */
223 if ((type == pkey->save_type) && pkey->ameth)
224 return 1;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000225#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000226 /* If we have an ENGINE release it */
227 if (pkey->engine) {
228 ENGINE_finish(pkey->engine);
229 pkey->engine = NULL;
230 }
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000231#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000232 }
233 if (str)
234 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
235 else
236 ameth = EVP_PKEY_asn1_find(&e, type);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000237#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000238 if (!pkey && e)
239 ENGINE_finish(e);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000240#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000241 if (!ameth) {
242 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
243 return 0;
244 }
245 if (pkey) {
246 pkey->ameth = ameth;
247 pkey->engine = e;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000248
Matt Caswell0f113f32015-01-22 03:40:55 +0000249 pkey->type = pkey->ameth->pkey_id;
250 pkey->save_type = type;
251 }
252 return 1;
253}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000254
255int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
Matt Caswell0f113f32015-01-22 03:40:55 +0000256{
257 return pkey_set_type(pkey, type, NULL, -1);
258}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000259
260int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000261{
262 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
263}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000264
265int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
Matt Caswell0f113f32015-01-22 03:40:55 +0000266{
Emilia Kaspere34c66c2015-09-01 16:31:55 +0200267 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
Matt Caswell0f113f32015-01-22 03:40:55 +0000268 return 0;
269 pkey->pkey.ptr = key;
270 return (key != NULL);
271}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000272
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +0000273void *EVP_PKEY_get0(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000274{
275 return pkey->pkey.ptr;
276}
Dr. Stephen Hensondb98bbc2006-03-24 13:46:58 +0000277
Richard Levittecf1b7d92001-02-19 16:06:34 +0000278#ifndef OPENSSL_NO_RSA
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000279int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000280{
Matt Caswell0f113f32015-01-22 03:40:55 +0000281 int ret = EVP_PKEY_assign_RSA(pkey, key);
282 if (ret)
283 RSA_up_ref(key);
284 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000285}
286
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000287RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000288{
289 if (pkey->type != EVP_PKEY_RSA) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000290 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000291 return NULL;
292 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000293 return pkey->pkey.rsa;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000294}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000295
296RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
297{
298 RSA *ret = EVP_PKEY_get0_RSA(pkey);
299 if (ret != NULL)
300 RSA_up_ref(ret);
301 return ret;
302}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000303#endif
304
Richard Levittecf1b7d92001-02-19 16:06:34 +0000305#ifndef OPENSSL_NO_DSA
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000306int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000307{
Matt Caswell0f113f32015-01-22 03:40:55 +0000308 int ret = EVP_PKEY_assign_DSA(pkey, key);
309 if (ret)
310 DSA_up_ref(key);
311 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000312}
313
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000314DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000315{
316 if (pkey->type != EVP_PKEY_DSA) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000317 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000318 return NULL;
319 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000320 return pkey->pkey.dsa;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000321}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000322
323DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
324{
325 DSA *ret = EVP_PKEY_get0_DSA(pkey);
326 if (ret != NULL)
327 DSA_up_ref(ret);
328 return ret;
329}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000330#endif
331
Bodo Möller14a7cfb2002-08-07 10:49:54 +0000332#ifndef OPENSSL_NO_EC
Bodo Möller4d94ae02002-02-13 18:21:51 +0000333
Bodo Möller14a7cfb2002-08-07 10:49:54 +0000334int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
Bodo Möller4d94ae02002-02-13 18:21:51 +0000335{
Matt Caswell0f113f32015-01-22 03:40:55 +0000336 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
337 if (ret)
338 EC_KEY_up_ref(key);
339 return ret;
Bodo Möller4d94ae02002-02-13 18:21:51 +0000340}
341
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000342EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
Bodo Möller4d94ae02002-02-13 18:21:51 +0000343{
Matt Caswell0f113f32015-01-22 03:40:55 +0000344 if (pkey->type != EVP_PKEY_EC) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000345 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000346 return NULL;
347 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000348 return pkey->pkey.ec;
Bodo Möller4d94ae02002-02-13 18:21:51 +0000349}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000350
351EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
352{
353 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
354 if (ret != NULL)
355 EC_KEY_up_ref(ret);
356 return ret;
357}
Bodo Möller4d94ae02002-02-13 18:21:51 +0000358#endif
359
Richard Levittecf1b7d92001-02-19 16:06:34 +0000360#ifndef OPENSSL_NO_DH
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000361
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000362int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000363{
Matt Caswell0f113f32015-01-22 03:40:55 +0000364 int ret = EVP_PKEY_assign_DH(pkey, key);
365 if (ret)
366 DH_up_ref(key);
367 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000368}
369
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000370DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000371{
372 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000373 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000374 return NULL;
375 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000376 return pkey->pkey.dh;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000377}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000378
379DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
380{
381 DH *ret = EVP_PKEY_get0_DH(pkey);
382 if (ret != NULL)
383 DH_up_ref(ret);
384 return ret;
385}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000386#endif
387
Ulf Möller6b691a51999-04-19 21:31:43 +0000388int EVP_PKEY_type(int type)
Matt Caswell0f113f32015-01-22 03:40:55 +0000389{
390 int ret;
391 const EVP_PKEY_ASN1_METHOD *ameth;
392 ENGINE *e;
393 ameth = EVP_PKEY_asn1_find(&e, type);
394 if (ameth)
395 ret = ameth->pkey_id;
396 else
397 ret = NID_undef;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000398#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000399 if (e)
400 ENGINE_finish(e);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000401#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000402 return ret;
403}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000404
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000405int EVP_PKEY_id(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000406{
407 return pkey->type;
408}
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000409
410int EVP_PKEY_base_id(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000411{
412 return EVP_PKEY_type(pkey->type);
413}
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000414
Ulf Möller6b691a51999-04-19 21:31:43 +0000415void EVP_PKEY_free(EVP_PKEY *x)
Matt Caswell0f113f32015-01-22 03:40:55 +0000416{
417 int i;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000418
Matt Caswell0f113f32015-01-22 03:40:55 +0000419 if (x == NULL)
420 return;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000421
Matt Caswell0f113f32015-01-22 03:40:55 +0000422 i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
Rich Salzf3f1cf82016-01-30 12:04:25 -0500423 REF_PRINT_COUNT("EVP_PKEY", x);
Matt Caswell0f113f32015-01-22 03:40:55 +0000424 if (i > 0)
425 return;
Rich Salzf3f1cf82016-01-30 12:04:25 -0500426 REF_ASSERT_ISNT(i < 0);
Matt Caswell0f113f32015-01-22 03:40:55 +0000427 EVP_PKEY_free_it(x);
Rich Salz222561f2015-04-30 17:33:59 -0400428 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
Matt Caswell0f113f32015-01-22 03:40:55 +0000429 OPENSSL_free(x);
430}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000431
Ulf Möller6b691a51999-04-19 21:31:43 +0000432static void EVP_PKEY_free_it(EVP_PKEY *x)
Matt Caswell0f113f32015-01-22 03:40:55 +0000433{
Rich Salzc5ba2d92015-03-28 10:54:15 -0400434 /* internal function; x is never NULL */
Matt Caswell0f113f32015-01-22 03:40:55 +0000435 if (x->ameth && x->ameth->pkey_free) {
436 x->ameth->pkey_free(x);
437 x->pkey.ptr = NULL;
438 }
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000439#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000440 if (x->engine) {
441 ENGINE_finish(x->engine);
442 x->engine = NULL;
443 }
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000444#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000445}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000446
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000447static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
Matt Caswell0f113f32015-01-22 03:40:55 +0000448 const char *kstr)
449{
450 BIO_indent(out, indent, 128);
451 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
452 kstr, OBJ_nid2ln(pkey->type));
453 return 1;
454}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000455
456int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000457 int indent, ASN1_PCTX *pctx)
458{
459 if (pkey->ameth && pkey->ameth->pub_print)
460 return pkey->ameth->pub_print(out, pkey, indent, pctx);
461
462 return unsup_alg(out, pkey, indent, "Public Key");
463}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000464
465int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000466 int indent, ASN1_PCTX *pctx)
467{
468 if (pkey->ameth && pkey->ameth->priv_print)
469 return pkey->ameth->priv_print(out, pkey, indent, pctx);
470
471 return unsup_alg(out, pkey, indent, "Private Key");
472}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000473
474int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000475 int indent, ASN1_PCTX *pctx)
476{
477 if (pkey->ameth && pkey->ameth->param_print)
478 return pkey->ameth->param_print(out, pkey, indent, pctx);
479 return unsup_alg(out, pkey, indent, "Parameters");
480}
Dr. Stephen Henson03919682006-05-07 17:09:39 +0000481
482int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000483{
484 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
485 return -2;
486 return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
487 0, pnid);
488}