blob: b5e5206778ffd2d5d1c13e6c0b805ce7b2fc2314 [file] [log] [blame]
Rich Salz62867572016-05-17 14:24:46 -04001/*
Todd Short3f5616d2017-01-11 16:38:44 -05002 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00003 *
Rich Salz62867572016-05-17 14:24:46 -04004 * 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
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +00008 */
9
10#include <stdio.h>
Richard Levitteb39fc562015-05-14 16:56:48 +020011#include "internal/cryptlib.h"
Bodo Möller4d94ae02002-02-13 18:21:51 +000012#include <openssl/bn.h>
13#include <openssl/err.h>
Bodo Möllerec577821999-04-23 22:13:45 +000014#include <openssl/objects.h>
15#include <openssl/evp.h>
Bodo Möllerec577821999-04-23 22:13:45 +000016#include <openssl/x509.h>
Rich Salz3c272082016-03-18 14:30:20 -040017#include <openssl/rsa.h>
18#include <openssl/dsa.h>
19#include <openssl/dh.h>
20#include <openssl/engine.h>
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +000021
Dr. Stephen Henson5fe736e2015-03-23 18:42:42 +000022#include "internal/asn1_int.h"
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +000023#include "internal/evp_int.h"
Dr. Stephen Henson18e377b2006-03-22 17:59:49 +000024
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000025static void EVP_PKEY_free_it(EVP_PKEY *x);
Ben Lauriebb2297a2001-08-03 18:48:35 +000026
Dr. Stephen Henson8900f3e2016-08-17 13:50:48 +010027int EVP_PKEY_bits(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +000028{
29 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
30 return pkey->ameth->pkey_bits(pkey);
31 return 0;
32}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +000033
Dr. Stephen Henson2514fa72014-01-18 14:51:40 +000034int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +000035{
36 if (pkey == NULL)
37 return 0;
38 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
39 return -2;
40 return pkey->ameth->pkey_security_bits(pkey);
41}
Dr. Stephen Henson2514fa72014-01-18 14:51:40 +000042
Ulf Möller6b691a51999-04-19 21:31:43 +000043int EVP_PKEY_size(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +000044{
45 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
46 return pkey->ameth->pkey_size(pkey);
47 return 0;
48}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000049
Ulf Möller6b691a51999-04-19 21:31:43 +000050int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
Matt Caswell0f113f32015-01-22 03:40:55 +000051{
Richard Levittecf1b7d92001-02-19 16:06:34 +000052#ifndef OPENSSL_NO_DSA
Matt Caswell0f113f32015-01-22 03:40:55 +000053 if (pkey->type == EVP_PKEY_DSA) {
54 int ret = pkey->save_parameters;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000055
Matt Caswell0f113f32015-01-22 03:40:55 +000056 if (mode >= 0)
57 pkey->save_parameters = mode;
58 return (ret);
59 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000060#endif
Bodo Möller5488bb62002-08-12 08:47:41 +000061#ifndef OPENSSL_NO_EC
Matt Caswell0f113f32015-01-22 03:40:55 +000062 if (pkey->type == EVP_PKEY_EC) {
63 int ret = pkey->save_parameters;
Bodo Möller4d94ae02002-02-13 18:21:51 +000064
Matt Caswell0f113f32015-01-22 03:40:55 +000065 if (mode >= 0)
66 pkey->save_parameters = mode;
67 return (ret);
68 }
Bodo Möller4d94ae02002-02-13 18:21:51 +000069#endif
Matt Caswell0f113f32015-01-22 03:40:55 +000070 return (0);
71}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000072
Richard Levittea8b72842003-04-07 10:09:44 +000073int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
Matt Caswell0f113f32015-01-22 03:40:55 +000074{
Dr. Stephen Henson2986ecd2015-12-13 17:28:40 +000075 if (to->type == EVP_PKEY_NONE) {
76 if (EVP_PKEY_set_type(to, from->type) == 0)
77 return 0;
78 } else if (to->type != from->type) {
Matt Caswell0f113f32015-01-22 03:40:55 +000079 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
80 goto err;
81 }
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +000082
Matt Caswell0f113f32015-01-22 03:40:55 +000083 if (EVP_PKEY_missing_parameters(from)) {
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
85 goto err;
86 }
Dr. Stephen Hensonf72f00d2016-05-27 14:18:40 +010087
88 if (!EVP_PKEY_missing_parameters(to)) {
89 if (EVP_PKEY_cmp_parameters(to, from) == 1)
90 return 1;
91 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
92 return 0;
93 }
94
Matt Caswell0f113f32015-01-22 03:40:55 +000095 if (from->ameth && from->ameth->param_copy)
96 return from->ameth->param_copy(to, from);
97 err:
98 return 0;
99}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000100
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000101int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000102{
103 if (pkey->ameth && pkey->ameth->param_missing)
104 return pkey->ameth->param_missing(pkey);
105 return 0;
106}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000107
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000108int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000109{
110 if (a->type != b->type)
111 return -1;
112 if (a->ameth && a->ameth->param_cmp)
113 return a->ameth->param_cmp(a, b);
114 return -2;
115}
Ralf S. Engelschall58964a41998-12-21 10:56:39 +0000116
Richard Levitteaf0f0f32003-04-06 15:31:18 +0000117int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
Matt Caswell0f113f32015-01-22 03:40:55 +0000118{
119 if (a->type != b->type)
120 return -1;
Richard Levittee6526fb2003-04-03 22:27:24 +0000121
Matt Caswell0f113f32015-01-22 03:40:55 +0000122 if (a->ameth) {
123 int ret;
124 /* Compare parameters if the algorithm has them */
125 if (a->ameth->param_cmp) {
126 ret = a->ameth->param_cmp(a, b);
127 if (ret <= 0)
128 return ret;
129 }
Richard Levittea8b72842003-04-07 10:09:44 +0000130
Matt Caswell0f113f32015-01-22 03:40:55 +0000131 if (a->ameth->pub_cmp)
132 return a->ameth->pub_cmp(a, b);
133 }
Richard Levittee6526fb2003-04-03 22:27:24 +0000134
Matt Caswell0f113f32015-01-22 03:40:55 +0000135 return -2;
136}
Richard Levittee6526fb2003-04-03 22:27:24 +0000137
Ulf Möller6b691a51999-04-19 21:31:43 +0000138EVP_PKEY *EVP_PKEY_new(void)
Matt Caswell0f113f32015-01-22 03:40:55 +0000139{
FdaSilvaYYa2d0baa2016-02-14 18:44:30 +0100140 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000141
Matt Caswell0f113f32015-01-22 03:40:55 +0000142 if (ret == NULL) {
143 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
Alessandro Ghedini03273d62016-02-26 12:21:15 +0000144 return NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000145 }
146 ret->type = EVP_PKEY_NONE;
147 ret->save_type = EVP_PKEY_NONE;
148 ret->references = 1;
Matt Caswell0f113f32015-01-22 03:40:55 +0000149 ret->save_parameters = 1;
Alessandro Ghedini03273d62016-02-26 12:21:15 +0000150 ret->lock = CRYPTO_THREAD_lock_new();
151 if (ret->lock == NULL) {
152 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
153 OPENSSL_free(ret);
154 return NULL;
155 }
156 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000157}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000158
FdaSilvaYYc5ebfca2016-03-07 22:45:58 +0100159int EVP_PKEY_up_ref(EVP_PKEY *pkey)
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000160{
Alessandro Ghedini03273d62016-02-26 12:21:15 +0000161 int i;
FdaSilvaYYc5ebfca2016-03-07 22:45:58 +0100162
Kurt Roeckx2f545ae2016-08-27 16:01:08 +0200163 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
FdaSilvaYYc5ebfca2016-03-07 22:45:58 +0100164 return 0;
165
166 REF_PRINT_COUNT("EVP_PKEY", pkey);
167 REF_ASSERT_ISNT(i < 2);
168 return ((i > 1) ? 1 : 0);
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000169}
170
Matt Caswell0f113f32015-01-22 03:40:55 +0000171/*
172 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
173 * is NULL just return 1 or 0 if the algorithm exists.
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000174 */
175
176static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000177{
178 const EVP_PKEY_ASN1_METHOD *ameth;
179 ENGINE *e = NULL;
180 if (pkey) {
181 if (pkey->pkey.ptr)
182 EVP_PKEY_free_it(pkey);
183 /*
184 * If key type matches and a method exists then this lookup has
185 * succeeded once so just indicate success.
186 */
187 if ((type == pkey->save_type) && pkey->ameth)
188 return 1;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000189#ifndef OPENSSL_NO_ENGINE
Matt Caswell0f113f32015-01-22 03:40:55 +0000190 /* If we have an ENGINE release it */
Rich Salz7c96dbc2016-02-25 12:09:06 -0500191 ENGINE_finish(pkey->engine);
192 pkey->engine = NULL;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000193#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000194 }
195 if (str)
196 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
197 else
198 ameth = EVP_PKEY_asn1_find(&e, type);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000199#ifndef OPENSSL_NO_ENGINE
Rich Salz7c96dbc2016-02-25 12:09:06 -0500200 if (pkey == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000201 ENGINE_finish(e);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000202#endif
Rich Salz7c96dbc2016-02-25 12:09:06 -0500203 if (ameth == NULL) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000204 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
205 return 0;
206 }
207 if (pkey) {
208 pkey->ameth = ameth;
209 pkey->engine = e;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000210
Matt Caswell0f113f32015-01-22 03:40:55 +0000211 pkey->type = pkey->ameth->pkey_id;
212 pkey->save_type = type;
213 }
214 return 1;
215}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000216
217int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
Matt Caswell0f113f32015-01-22 03:40:55 +0000218{
219 return pkey_set_type(pkey, type, NULL, -1);
220}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000221
222int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
Matt Caswell0f113f32015-01-22 03:40:55 +0000223{
224 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
225}
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000226
227int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
Matt Caswell0f113f32015-01-22 03:40:55 +0000228{
Emilia Kaspere34c66c2015-09-01 16:31:55 +0200229 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
Matt Caswell0f113f32015-01-22 03:40:55 +0000230 return 0;
231 pkey->pkey.ptr = key;
232 return (key != NULL);
233}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000234
Dr. Stephen Henson3aeb9342016-01-19 00:21:12 +0000235void *EVP_PKEY_get0(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000236{
237 return pkey->pkey.ptr;
238}
Dr. Stephen Hensondb98bbc2006-03-24 13:46:58 +0000239
Nathaniel McCallumebad0b02016-06-15 14:02:04 -0400240const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
241{
242 ASN1_OCTET_STRING *os = NULL;
243 if (pkey->type != EVP_PKEY_HMAC) {
244 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
245 return NULL;
246 }
247 os = EVP_PKEY_get0(pkey);
248 *len = os->length;
249 return os->data;
250}
251
Todd Short52ad5b62016-12-16 17:00:43 -0500252#ifndef OPENSSL_NO_POLY1305
253const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
254{
255 ASN1_OCTET_STRING *os = NULL;
256 if (pkey->type != EVP_PKEY_POLY1305) {
257 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
258 return NULL;
259 }
260 os = EVP_PKEY_get0(pkey);
261 *len = os->length;
262 return os->data;
263}
264#endif
265
Todd Short3f5616d2017-01-11 16:38:44 -0500266#ifndef OPENSSL_NO_SIPHASH
267const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
268{
269 ASN1_OCTET_STRING *os = NULL;
270
271 if (pkey->type != EVP_PKEY_SIPHASH) {
272 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
273 return NULL;
274 }
275 os = EVP_PKEY_get0(pkey);
276 *len = os->length;
277 return os->data;
278}
279#endif
280
Richard Levittecf1b7d92001-02-19 16:06:34 +0000281#ifndef OPENSSL_NO_RSA
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000282int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000283{
Matt Caswell0f113f32015-01-22 03:40:55 +0000284 int ret = EVP_PKEY_assign_RSA(pkey, key);
285 if (ret)
286 RSA_up_ref(key);
287 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000288}
289
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000290RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000291{
292 if (pkey->type != EVP_PKEY_RSA) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000293 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000294 return NULL;
295 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000296 return pkey->pkey.rsa;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000297}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000298
299RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
300{
301 RSA *ret = EVP_PKEY_get0_RSA(pkey);
302 if (ret != NULL)
303 RSA_up_ref(ret);
304 return ret;
305}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000306#endif
307
Richard Levittecf1b7d92001-02-19 16:06:34 +0000308#ifndef OPENSSL_NO_DSA
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000309int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000310{
Matt Caswell0f113f32015-01-22 03:40:55 +0000311 int ret = EVP_PKEY_assign_DSA(pkey, key);
312 if (ret)
313 DSA_up_ref(key);
314 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000315}
316
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000317DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000318{
319 if (pkey->type != EVP_PKEY_DSA) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000320 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000321 return NULL;
322 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000323 return pkey->pkey.dsa;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000324}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000325
326DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
327{
328 DSA *ret = EVP_PKEY_get0_DSA(pkey);
329 if (ret != NULL)
330 DSA_up_ref(ret);
331 return ret;
332}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000333#endif
334
Bodo Möller14a7cfb2002-08-07 10:49:54 +0000335#ifndef OPENSSL_NO_EC
Bodo Möller4d94ae02002-02-13 18:21:51 +0000336
Bodo Möller14a7cfb2002-08-07 10:49:54 +0000337int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
Bodo Möller4d94ae02002-02-13 18:21:51 +0000338{
Matt Caswell0f113f32015-01-22 03:40:55 +0000339 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
340 if (ret)
341 EC_KEY_up_ref(key);
342 return ret;
Bodo Möller4d94ae02002-02-13 18:21:51 +0000343}
344
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000345EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
Bodo Möller4d94ae02002-02-13 18:21:51 +0000346{
Matt Caswell0f113f32015-01-22 03:40:55 +0000347 if (pkey->type != EVP_PKEY_EC) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000348 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000349 return NULL;
350 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000351 return pkey->pkey.ec;
Bodo Möller4d94ae02002-02-13 18:21:51 +0000352}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000353
354EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
355{
356 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
357 if (ret != NULL)
358 EC_KEY_up_ref(ret);
359 return ret;
360}
Bodo Möller4d94ae02002-02-13 18:21:51 +0000361#endif
362
Richard Levittecf1b7d92001-02-19 16:06:34 +0000363#ifndef OPENSSL_NO_DH
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000364
Dr. Stephen Hensonc7cb16a2000-02-26 01:55:33 +0000365int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000366{
Matt Caswell0f113f32015-01-22 03:40:55 +0000367 int ret = EVP_PKEY_assign_DH(pkey, key);
368 if (ret)
369 DH_up_ref(key);
370 return ret;
Dr. Stephen Henson52664f51999-11-21 22:28:31 +0000371}
372
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000373DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000374{
375 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000376 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
Matt Caswell0f113f32015-01-22 03:40:55 +0000377 return NULL;
378 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000379 return pkey->pkey.dh;
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000380}
Dr. Stephen Henson2872dbe2015-12-13 17:57:01 +0000381
382DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
383{
384 DH *ret = EVP_PKEY_get0_DH(pkey);
385 if (ret != NULL)
386 DH_up_ref(ret);
387 return ret;
388}
Dr. Stephen Hensonf769ce31999-10-25 02:00:09 +0000389#endif
390
Ulf Möller6b691a51999-04-19 21:31:43 +0000391int EVP_PKEY_type(int type)
Matt Caswell0f113f32015-01-22 03:40:55 +0000392{
393 int ret;
394 const EVP_PKEY_ASN1_METHOD *ameth;
395 ENGINE *e;
396 ameth = EVP_PKEY_asn1_find(&e, type);
397 if (ameth)
398 ret = ameth->pkey_id;
399 else
400 ret = NID_undef;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000401#ifndef OPENSSL_NO_ENGINE
Rich Salz7c96dbc2016-02-25 12:09:06 -0500402 ENGINE_finish(e);
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000403#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000404 return ret;
405}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000406
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000407int EVP_PKEY_id(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000408{
409 return pkey->type;
410}
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000411
412int EVP_PKEY_base_id(const EVP_PKEY *pkey)
Matt Caswell0f113f32015-01-22 03:40:55 +0000413{
414 return EVP_PKEY_type(pkey->type);
415}
Dr. Stephen Henson7f57b072006-05-11 21:33:00 +0000416
Ulf Möller6b691a51999-04-19 21:31:43 +0000417void EVP_PKEY_free(EVP_PKEY *x)
Matt Caswell0f113f32015-01-22 03:40:55 +0000418{
419 int i;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000420
Matt Caswell0f113f32015-01-22 03:40:55 +0000421 if (x == NULL)
422 return;
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000423
Kurt Roeckx2f545ae2016-08-27 16:01:08 +0200424 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
Rich Salzf3f1cf82016-01-30 12:04:25 -0500425 REF_PRINT_COUNT("EVP_PKEY", x);
Matt Caswell0f113f32015-01-22 03:40:55 +0000426 if (i > 0)
427 return;
Rich Salzf3f1cf82016-01-30 12:04:25 -0500428 REF_ASSERT_ISNT(i < 0);
Matt Caswell0f113f32015-01-22 03:40:55 +0000429 EVP_PKEY_free_it(x);
Alex Gaynord65c3612016-09-07 09:41:20 -0400430 CRYPTO_THREAD_lock_free(x->lock);
Rich Salz222561f2015-04-30 17:33:59 -0400431 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
Matt Caswell0f113f32015-01-22 03:40:55 +0000432 OPENSSL_free(x);
433}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000434
Ulf Möller6b691a51999-04-19 21:31:43 +0000435static void EVP_PKEY_free_it(EVP_PKEY *x)
Matt Caswell0f113f32015-01-22 03:40:55 +0000436{
Rich Salzc5ba2d92015-03-28 10:54:15 -0400437 /* internal function; x is never NULL */
Matt Caswell0f113f32015-01-22 03:40:55 +0000438 if (x->ameth && x->ameth->pkey_free) {
439 x->ameth->pkey_free(x);
440 x->pkey.ptr = NULL;
441 }
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000442#ifndef OPENSSL_NO_ENGINE
Rich Salz7c96dbc2016-02-25 12:09:06 -0500443 ENGINE_finish(x->engine);
444 x->engine = NULL;
Dr. Stephen Henson01b8b3c2006-06-05 11:52:46 +0000445#endif
Matt Caswell0f113f32015-01-22 03:40:55 +0000446}
Ralf S. Engelschalld02b48c1998-12-21 10:52:47 +0000447
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000448static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
Matt Caswell0f113f32015-01-22 03:40:55 +0000449 const char *kstr)
450{
451 BIO_indent(out, indent, 128);
452 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
453 kstr, OBJ_nid2ln(pkey->type));
454 return 1;
455}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000456
457int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000458 int indent, ASN1_PCTX *pctx)
459{
460 if (pkey->ameth && pkey->ameth->pub_print)
461 return pkey->ameth->pub_print(out, pkey, indent, pctx);
462
463 return unsup_alg(out, pkey, indent, "Public Key");
464}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000465
466int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000467 int indent, ASN1_PCTX *pctx)
468{
469 if (pkey->ameth && pkey->ameth->priv_print)
470 return pkey->ameth->priv_print(out, pkey, indent, pctx);
471
472 return unsup_alg(out, pkey, indent, "Private Key");
473}
Dr. Stephen Henson35208f32006-03-22 13:09:35 +0000474
475int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
Matt Caswell0f113f32015-01-22 03:40:55 +0000476 int indent, ASN1_PCTX *pctx)
477{
478 if (pkey->ameth && pkey->ameth->param_print)
479 return pkey->ameth->param_print(out, pkey, indent, pctx);
480 return unsup_alg(out, pkey, indent, "Parameters");
481}
Dr. Stephen Henson03919682006-05-07 17:09:39 +0000482
Dr. Stephen Henson5d6aaf82016-08-10 22:30:43 +0100483static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
484{
485 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
486 return -2;
487 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
488}
489
Dr. Stephen Henson03919682006-05-07 17:09:39 +0000490int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
Matt Caswell0f113f32015-01-22 03:40:55 +0000491{
Dr. Stephen Henson5d6aaf82016-08-10 22:30:43 +0100492 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
493}
494
495int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
496 const unsigned char *pt, size_t ptlen)
497{
498 if (ptlen > INT_MAX)
499 return 0;
500 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
501 (void *)pt) <= 0)
502 return 0;
503 return 1;
504}
505
506size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
507{
508 int rv;
509 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
510 if (rv <= 0)
511 return 0;
512 return rv;
Matt Caswell0f113f32015-01-22 03:40:55 +0000513}