]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_lib.c
Fix memory leak on lookup failure
[thirdparty/openssl.git] / crypto / evp / p_lib.c
CommitLineData
62867571 1/*
3f5616d7 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
62867571
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
cd420b0b 12#include "internal/refcount.h"
4d94ae00
BM
13#include <openssl/bn.h>
14#include <openssl/err.h>
ec577822
BM
15#include <openssl/objects.h>
16#include <openssl/evp.h>
ec577822 17#include <openssl/x509.h>
3c27208f
RS
18#include <openssl/rsa.h>
19#include <openssl/dsa.h>
20#include <openssl/dh.h>
21#include <openssl/engine.h>
01b8b3c7 22
5fe736e5 23#include "internal/asn1_int.h"
3aeb9348 24#include "internal/evp_int.h"
18e377b4 25
d02b48c6 26static void EVP_PKEY_free_it(EVP_PKEY *x);
bb2297a4 27
8900f3e3 28int EVP_PKEY_bits(const EVP_PKEY *pkey)
0f113f3e
MC
29{
30 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
31 return pkey->ameth->pkey_bits(pkey);
32 return 0;
33}
58964a49 34
2514fa79 35int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
36{
37 if (pkey == NULL)
38 return 0;
39 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
40 return -2;
41 return pkey->ameth->pkey_security_bits(pkey);
42}
2514fa79 43
6b691a5c 44int EVP_PKEY_size(EVP_PKEY *pkey)
0f113f3e
MC
45{
46 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
47 return pkey->ameth->pkey_size(pkey);
48 return 0;
49}
d02b48c6 50
6b691a5c 51int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
0f113f3e 52{
cf1b7d96 53#ifndef OPENSSL_NO_DSA
0f113f3e
MC
54 if (pkey->type == EVP_PKEY_DSA) {
55 int ret = pkey->save_parameters;
56
57 if (mode >= 0)
58 pkey->save_parameters = mode;
59 return (ret);
60 }
4d94ae00 61#endif
5488bb61 62#ifndef OPENSSL_NO_EC
0f113f3e
MC
63 if (pkey->type == EVP_PKEY_EC) {
64 int ret = pkey->save_parameters;
65
66 if (mode >= 0)
67 pkey->save_parameters = mode;
68 return (ret);
69 }
d02b48c6 70#endif
0f113f3e
MC
71 return (0);
72}
d02b48c6 73
a8b72844 74int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
0f113f3e 75{
2986ecdc
DSH
76 if (to->type == EVP_PKEY_NONE) {
77 if (EVP_PKEY_set_type(to, from->type) == 0)
78 return 0;
79 } else if (to->type != from->type) {
0f113f3e
MC
80 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
81 goto err;
82 }
83
84 if (EVP_PKEY_missing_parameters(from)) {
85 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
86 goto err;
87 }
f72f00d4
DSH
88
89 if (!EVP_PKEY_missing_parameters(to)) {
90 if (EVP_PKEY_cmp_parameters(to, from) == 1)
91 return 1;
92 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
93 return 0;
94 }
95
0f113f3e
MC
96 if (from->ameth && from->ameth->param_copy)
97 return from->ameth->param_copy(to, from);
98 err:
99 return 0;
100}
d02b48c6 101
af0f0f3e 102int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
0f113f3e
MC
103{
104 if (pkey->ameth && pkey->ameth->param_missing)
105 return pkey->ameth->param_missing(pkey);
106 return 0;
107}
d02b48c6 108
af0f0f3e 109int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
110{
111 if (a->type != b->type)
112 return -1;
113 if (a->ameth && a->ameth->param_cmp)
114 return a->ameth->param_cmp(a, b);
115 return -2;
116}
58964a49 117
af0f0f3e 118int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
119{
120 if (a->type != b->type)
121 return -1;
122
123 if (a->ameth) {
124 int ret;
125 /* Compare parameters if the algorithm has them */
126 if (a->ameth->param_cmp) {
127 ret = a->ameth->param_cmp(a, b);
128 if (ret <= 0)
129 return ret;
130 }
131
132 if (a->ameth->pub_cmp)
133 return a->ameth->pub_cmp(a, b);
134 }
135
136 return -2;
137}
e6526fbf 138
6b691a5c 139EVP_PKEY *EVP_PKEY_new(void)
0f113f3e 140{
a2d0baa2 141 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 142
0f113f3e
MC
143 if (ret == NULL) {
144 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
03273d61 145 return NULL;
0f113f3e
MC
146 }
147 ret->type = EVP_PKEY_NONE;
148 ret->save_type = EVP_PKEY_NONE;
149 ret->references = 1;
0f113f3e 150 ret->save_parameters = 1;
03273d61
AG
151 ret->lock = CRYPTO_THREAD_lock_new();
152 if (ret->lock == NULL) {
153 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
154 OPENSSL_free(ret);
155 return NULL;
156 }
157 return ret;
0f113f3e
MC
158}
159
c5ebfcab 160int EVP_PKEY_up_ref(EVP_PKEY *pkey)
2872dbe1 161{
03273d61 162 int i;
c5ebfcab 163
2f545ae4 164 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
c5ebfcab
F
165 return 0;
166
167 REF_PRINT_COUNT("EVP_PKEY", pkey);
168 REF_ASSERT_ISNT(i < 2);
169 return ((i > 1) ? 1 : 0);
2872dbe1
DSH
170}
171
0f113f3e
MC
172/*
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.
01b8b3c7
DSH
175 */
176
177static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
0f113f3e
MC
178{
179 const EVP_PKEY_ASN1_METHOD *ameth;
180 ENGINE *e = NULL;
181 if (pkey) {
182 if (pkey->pkey.ptr)
183 EVP_PKEY_free_it(pkey);
184 /*
185 * If key type matches and a method exists then this lookup has
186 * succeeded once so just indicate success.
187 */
188 if ((type == pkey->save_type) && pkey->ameth)
189 return 1;
01b8b3c7 190#ifndef OPENSSL_NO_ENGINE
0f113f3e 191 /* If we have an ENGINE release it */
7c96dbcd
RS
192 ENGINE_finish(pkey->engine);
193 pkey->engine = NULL;
01b8b3c7 194#endif
0f113f3e
MC
195 }
196 if (str)
197 ameth = EVP_PKEY_asn1_find_str(&e, str, len);
198 else
199 ameth = EVP_PKEY_asn1_find(&e, type);
01b8b3c7 200#ifndef OPENSSL_NO_ENGINE
7c96dbcd 201 if (pkey == NULL)
0f113f3e 202 ENGINE_finish(e);
01b8b3c7 203#endif
7c96dbcd 204 if (ameth == NULL) {
0f113f3e
MC
205 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
206 return 0;
207 }
208 if (pkey) {
209 pkey->ameth = ameth;
210 pkey->engine = e;
211
212 pkey->type = pkey->ameth->pkey_id;
213 pkey->save_type = type;
214 }
215 return 1;
216}
01b8b3c7
DSH
217
218int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
0f113f3e
MC
219{
220 return pkey_set_type(pkey, type, NULL, -1);
221}
01b8b3c7
DSH
222
223int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
0f113f3e
MC
224{
225 return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
226}
01b8b3c7
DSH
227
228int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
0f113f3e 229{
e34c66c6 230 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
0f113f3e
MC
231 return 0;
232 pkey->pkey.ptr = key;
233 return (key != NULL);
234}
d02b48c6 235
3aeb9348 236void *EVP_PKEY_get0(const EVP_PKEY *pkey)
0f113f3e
MC
237{
238 return pkey->pkey.ptr;
239}
db98bbc1 240
ebad0b0b
NM
241const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
242{
243 ASN1_OCTET_STRING *os = NULL;
244 if (pkey->type != EVP_PKEY_HMAC) {
245 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
246 return NULL;
247 }
248 os = EVP_PKEY_get0(pkey);
249 *len = os->length;
250 return os->data;
251}
252
52ad5b60
TS
253#ifndef OPENSSL_NO_POLY1305
254const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
255{
256 ASN1_OCTET_STRING *os = NULL;
257 if (pkey->type != EVP_PKEY_POLY1305) {
258 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
259 return NULL;
260 }
261 os = EVP_PKEY_get0(pkey);
262 *len = os->length;
263 return os->data;
264}
265#endif
266
3f5616d7
TS
267#ifndef OPENSSL_NO_SIPHASH
268const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
269{
270 ASN1_OCTET_STRING *os = NULL;
271
272 if (pkey->type != EVP_PKEY_SIPHASH) {
273 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
274 return NULL;
275 }
276 os = EVP_PKEY_get0(pkey);
277 *len = os->length;
278 return os->data;
279}
280#endif
281
cf1b7d96 282#ifndef OPENSSL_NO_RSA
c7cb16a8 283int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
52664f50 284{
0f113f3e
MC
285 int ret = EVP_PKEY_assign_RSA(pkey, key);
286 if (ret)
287 RSA_up_ref(key);
288 return ret;
52664f50
DSH
289}
290
2872dbe1 291RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
0f113f3e
MC
292{
293 if (pkey->type != EVP_PKEY_RSA) {
2872dbe1 294 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
0f113f3e
MC
295 return NULL;
296 }
0f113f3e 297 return pkey->pkey.rsa;
f769ce3e 298}
2872dbe1
DSH
299
300RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
301{
302 RSA *ret = EVP_PKEY_get0_RSA(pkey);
303 if (ret != NULL)
304 RSA_up_ref(ret);
305 return ret;
306}
f769ce3e
DSH
307#endif
308
cf1b7d96 309#ifndef OPENSSL_NO_DSA
c7cb16a8 310int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
52664f50 311{
0f113f3e
MC
312 int ret = EVP_PKEY_assign_DSA(pkey, key);
313 if (ret)
314 DSA_up_ref(key);
315 return ret;
52664f50
DSH
316}
317
2872dbe1 318DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
0f113f3e
MC
319{
320 if (pkey->type != EVP_PKEY_DSA) {
2872dbe1 321 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
0f113f3e
MC
322 return NULL;
323 }
0f113f3e 324 return pkey->pkey.dsa;
f769ce3e 325}
2872dbe1
DSH
326
327DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
328{
329 DSA *ret = EVP_PKEY_get0_DSA(pkey);
330 if (ret != NULL)
331 DSA_up_ref(ret);
332 return ret;
333}
f769ce3e
DSH
334#endif
335
14a7cfb3 336#ifndef OPENSSL_NO_EC
4d94ae00 337
14a7cfb3 338int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
4d94ae00 339{
0f113f3e
MC
340 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
341 if (ret)
342 EC_KEY_up_ref(key);
343 return ret;
4d94ae00
BM
344}
345
2872dbe1 346EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
4d94ae00 347{
0f113f3e 348 if (pkey->type != EVP_PKEY_EC) {
2872dbe1 349 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
0f113f3e
MC
350 return NULL;
351 }
0f113f3e 352 return pkey->pkey.ec;
4d94ae00 353}
2872dbe1
DSH
354
355EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
356{
357 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
358 if (ret != NULL)
359 EC_KEY_up_ref(ret);
360 return ret;
361}
4d94ae00
BM
362#endif
363
cf1b7d96 364#ifndef OPENSSL_NO_DH
52664f50 365
c7cb16a8 366int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
52664f50 367{
0f113f3e
MC
368 int ret = EVP_PKEY_assign_DH(pkey, key);
369 if (ret)
370 DH_up_ref(key);
371 return ret;
52664f50
DSH
372}
373
2872dbe1 374DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
0f113f3e
MC
375{
376 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
2872dbe1 377 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
0f113f3e
MC
378 return NULL;
379 }
0f113f3e 380 return pkey->pkey.dh;
f769ce3e 381}
2872dbe1
DSH
382
383DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
384{
385 DH *ret = EVP_PKEY_get0_DH(pkey);
386 if (ret != NULL)
387 DH_up_ref(ret);
388 return ret;
389}
f769ce3e
DSH
390#endif
391
6b691a5c 392int EVP_PKEY_type(int type)
0f113f3e
MC
393{
394 int ret;
395 const EVP_PKEY_ASN1_METHOD *ameth;
396 ENGINE *e;
397 ameth = EVP_PKEY_asn1_find(&e, type);
398 if (ameth)
399 ret = ameth->pkey_id;
400 else
401 ret = NID_undef;
01b8b3c7 402#ifndef OPENSSL_NO_ENGINE
7c96dbcd 403 ENGINE_finish(e);
01b8b3c7 404#endif
0f113f3e
MC
405 return ret;
406}
d02b48c6 407
7f57b076 408int EVP_PKEY_id(const EVP_PKEY *pkey)
0f113f3e
MC
409{
410 return pkey->type;
411}
7f57b076
DSH
412
413int EVP_PKEY_base_id(const EVP_PKEY *pkey)
0f113f3e
MC
414{
415 return EVP_PKEY_type(pkey->type);
416}
7f57b076 417
6b691a5c 418void EVP_PKEY_free(EVP_PKEY *x)
0f113f3e
MC
419{
420 int i;
d02b48c6 421
0f113f3e
MC
422 if (x == NULL)
423 return;
d02b48c6 424
2f545ae4 425 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
f3f1cf84 426 REF_PRINT_COUNT("EVP_PKEY", x);
0f113f3e
MC
427 if (i > 0)
428 return;
f3f1cf84 429 REF_ASSERT_ISNT(i < 0);
0f113f3e 430 EVP_PKEY_free_it(x);
d65c3615 431 CRYPTO_THREAD_lock_free(x->lock);
222561fe 432 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
0f113f3e
MC
433 OPENSSL_free(x);
434}
d02b48c6 435
6b691a5c 436static void EVP_PKEY_free_it(EVP_PKEY *x)
0f113f3e 437{
c5ba2d99 438 /* internal function; x is never NULL */
0f113f3e
MC
439 if (x->ameth && x->ameth->pkey_free) {
440 x->ameth->pkey_free(x);
441 x->pkey.ptr = NULL;
442 }
01b8b3c7 443#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
444 ENGINE_finish(x->engine);
445 x->engine = NULL;
01b8b3c7 446#endif
0f113f3e 447}
d02b48c6 448
35208f36 449static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
450 const char *kstr)
451{
452 BIO_indent(out, indent, 128);
453 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
454 kstr, OBJ_nid2ln(pkey->type));
455 return 1;
456}
35208f36
DSH
457
458int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
459 int indent, ASN1_PCTX *pctx)
460{
461 if (pkey->ameth && pkey->ameth->pub_print)
462 return pkey->ameth->pub_print(out, pkey, indent, pctx);
463
464 return unsup_alg(out, pkey, indent, "Public Key");
465}
35208f36
DSH
466
467int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
468 int indent, ASN1_PCTX *pctx)
469{
470 if (pkey->ameth && pkey->ameth->priv_print)
471 return pkey->ameth->priv_print(out, pkey, indent, pctx);
472
473 return unsup_alg(out, pkey, indent, "Private Key");
474}
35208f36
DSH
475
476int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
477 int indent, ASN1_PCTX *pctx)
478{
479 if (pkey->ameth && pkey->ameth->param_print)
480 return pkey->ameth->param_print(out, pkey, indent, pctx);
481 return unsup_alg(out, pkey, indent, "Parameters");
482}
03919683 483
5d6aaf8a 484static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
0f113f3e 485{
5d6aaf8a 486 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
0f113f3e 487 return -2;
5d6aaf8a
DSH
488 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
489}
490
491int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
492{
493 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
494}
495
496int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
497 const unsigned char *pt, size_t ptlen)
498{
499 if (ptlen > INT_MAX)
500 return 0;
501 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
502 (void *)pt) <= 0)
503 return 0;
504 return 1;
505}
506
507size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
508{
509 int rv;
510 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
511 if (rv <= 0)
512 return 0;
513 return rv;
0f113f3e 514}