]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_lib.c
New function EVP_CIPHER_free()
[thirdparty/openssl.git] / crypto / evp / p_lib.c
CommitLineData
62867571 1/*
b0edda11 2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
62867571
RS
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>
b3831fbb 21#include <openssl/cmac.h>
3c27208f 22#include <openssl/engine.h>
e74bd290
RL
23#include <openssl/params.h>
24#include <openssl/core_names.h>
01b8b3c7 25
5fe736e5 26#include "internal/asn1_int.h"
3aeb9348 27#include "internal/evp_int.h"
e74bd290 28#include "internal/provider.h"
18e377b4 29
d02b48c6 30static void EVP_PKEY_free_it(EVP_PKEY *x);
bb2297a4 31
8900f3e3 32int EVP_PKEY_bits(const EVP_PKEY *pkey)
0f113f3e
MC
33{
34 if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
35 return pkey->ameth->pkey_bits(pkey);
36 return 0;
37}
58964a49 38
2514fa79 39int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
40{
41 if (pkey == NULL)
42 return 0;
43 if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
44 return -2;
45 return pkey->ameth->pkey_security_bits(pkey);
46}
2514fa79 47
47ec2367 48int EVP_PKEY_size(const EVP_PKEY *pkey)
0f113f3e
MC
49{
50 if (pkey && pkey->ameth && pkey->ameth->pkey_size)
51 return pkey->ameth->pkey_size(pkey);
52 return 0;
53}
d02b48c6 54
6b691a5c 55int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
0f113f3e 56{
cf1b7d96 57#ifndef OPENSSL_NO_DSA
0f113f3e
MC
58 if (pkey->type == EVP_PKEY_DSA) {
59 int ret = pkey->save_parameters;
60
61 if (mode >= 0)
62 pkey->save_parameters = mode;
26a7d938 63 return ret;
0f113f3e 64 }
4d94ae00 65#endif
5488bb61 66#ifndef OPENSSL_NO_EC
0f113f3e
MC
67 if (pkey->type == EVP_PKEY_EC) {
68 int ret = pkey->save_parameters;
69
70 if (mode >= 0)
71 pkey->save_parameters = mode;
26a7d938 72 return ret;
0f113f3e 73 }
d02b48c6 74#endif
26a7d938 75 return 0;
0f113f3e 76}
d02b48c6 77
a8b72844 78int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
0f113f3e 79{
2986ecdc
DSH
80 if (to->type == EVP_PKEY_NONE) {
81 if (EVP_PKEY_set_type(to, from->type) == 0)
82 return 0;
83 } else if (to->type != from->type) {
0f113f3e
MC
84 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
85 goto err;
86 }
87
88 if (EVP_PKEY_missing_parameters(from)) {
89 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
90 goto err;
91 }
f72f00d4
DSH
92
93 if (!EVP_PKEY_missing_parameters(to)) {
94 if (EVP_PKEY_cmp_parameters(to, from) == 1)
95 return 1;
96 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
97 return 0;
98 }
99
0f113f3e
MC
100 if (from->ameth && from->ameth->param_copy)
101 return from->ameth->param_copy(to, from);
102 err:
103 return 0;
104}
d02b48c6 105
af0f0f3e 106int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
0f113f3e
MC
107{
108 if (pkey->ameth && pkey->ameth->param_missing)
109 return pkey->ameth->param_missing(pkey);
110 return 0;
111}
d02b48c6 112
af0f0f3e 113int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
114{
115 if (a->type != b->type)
116 return -1;
117 if (a->ameth && a->ameth->param_cmp)
118 return a->ameth->param_cmp(a, b);
119 return -2;
120}
58964a49 121
af0f0f3e 122int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
123{
124 if (a->type != b->type)
125 return -1;
126
127 if (a->ameth) {
128 int ret;
129 /* Compare parameters if the algorithm has them */
130 if (a->ameth->param_cmp) {
131 ret = a->ameth->param_cmp(a, b);
132 if (ret <= 0)
133 return ret;
134 }
135
136 if (a->ameth->pub_cmp)
137 return a->ameth->pub_cmp(a, b);
138 }
139
140 return -2;
141}
e6526fbf 142
6b691a5c 143EVP_PKEY *EVP_PKEY_new(void)
0f113f3e 144{
a2d0baa2 145 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e 146
0f113f3e
MC
147 if (ret == NULL) {
148 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
03273d61 149 return NULL;
0f113f3e
MC
150 }
151 ret->type = EVP_PKEY_NONE;
152 ret->save_type = EVP_PKEY_NONE;
153 ret->references = 1;
0f113f3e 154 ret->save_parameters = 1;
03273d61
AG
155 ret->lock = CRYPTO_THREAD_lock_new();
156 if (ret->lock == NULL) {
157 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
158 OPENSSL_free(ret);
159 return NULL;
160 }
161 return ret;
0f113f3e
MC
162}
163
c5ebfcab 164int EVP_PKEY_up_ref(EVP_PKEY *pkey)
2872dbe1 165{
03273d61 166 int i;
c5ebfcab 167
2f545ae4 168 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
c5ebfcab
F
169 return 0;
170
171 REF_PRINT_COUNT("EVP_PKEY", pkey);
172 REF_ASSERT_ISNT(i < 2);
173 return ((i > 1) ? 1 : 0);
2872dbe1
DSH
174}
175
0f113f3e
MC
176/*
177 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
178 * is NULL just return 1 or 0 if the algorithm exists.
01b8b3c7
DSH
179 */
180
a08802ce
MC
181static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
182 int len)
0f113f3e
MC
183{
184 const EVP_PKEY_ASN1_METHOD *ameth;
a08802ce
MC
185 ENGINE **eptr = (e == NULL) ? &e : NULL;
186
0f113f3e
MC
187 if (pkey) {
188 if (pkey->pkey.ptr)
189 EVP_PKEY_free_it(pkey);
190 /*
191 * If key type matches and a method exists then this lookup has
192 * succeeded once so just indicate success.
193 */
194 if ((type == pkey->save_type) && pkey->ameth)
195 return 1;
01b8b3c7 196#ifndef OPENSSL_NO_ENGINE
d19b01ad 197 /* If we have ENGINEs release them */
7c96dbcd
RS
198 ENGINE_finish(pkey->engine);
199 pkey->engine = NULL;
d19b01ad
DSH
200 ENGINE_finish(pkey->pmeth_engine);
201 pkey->pmeth_engine = NULL;
01b8b3c7 202#endif
0f113f3e
MC
203 }
204 if (str)
a08802ce 205 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
0f113f3e 206 else
a08802ce 207 ameth = EVP_PKEY_asn1_find(eptr, type);
01b8b3c7 208#ifndef OPENSSL_NO_ENGINE
a08802ce 209 if (pkey == NULL && eptr != NULL)
0f113f3e 210 ENGINE_finish(e);
01b8b3c7 211#endif
7c96dbcd 212 if (ameth == NULL) {
0f113f3e
MC
213 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
214 return 0;
215 }
216 if (pkey) {
217 pkey->ameth = ameth;
218 pkey->engine = e;
219
220 pkey->type = pkey->ameth->pkey_id;
221 pkey->save_type = type;
222 }
223 return 1;
224}
01b8b3c7 225
f929439f
MC
226EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
227 const unsigned char *priv,
228 size_t len)
a08802ce
MC
229{
230 EVP_PKEY *ret = EVP_PKEY_new();
231
232 if (ret == NULL
233 || !pkey_set_type(ret, e, type, NULL, -1)) {
234 /* EVPerr already called */
235 goto err;
236 }
237
238 if (ret->ameth->set_priv_key == NULL) {
f929439f 239 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
a08802ce
MC
240 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
241 goto err;
242 }
243
244 if (!ret->ameth->set_priv_key(ret, priv, len)) {
f929439f 245 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
a08802ce
MC
246 goto err;
247 }
248
249 return ret;
250
251 err:
252 EVP_PKEY_free(ret);
253 return NULL;
254}
255
f929439f
MC
256EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
257 const unsigned char *pub,
258 size_t len)
a08802ce
MC
259{
260 EVP_PKEY *ret = EVP_PKEY_new();
261
262 if (ret == NULL
263 || !pkey_set_type(ret, e, type, NULL, -1)) {
264 /* EVPerr already called */
265 goto err;
266 }
267
268 if (ret->ameth->set_pub_key == NULL) {
f929439f 269 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
a08802ce
MC
270 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
271 goto err;
272 }
273
274 if (!ret->ameth->set_pub_key(ret, pub, len)) {
f929439f 275 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
a08802ce
MC
276 goto err;
277 }
278
279 return ret;
280
281 err:
282 EVP_PKEY_free(ret);
283 return NULL;
284}
285
0d124b0a
MC
286int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
287 size_t *len)
288{
289 if (pkey->ameth->get_priv_key == NULL) {
290 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
291 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
292 return 0;
293 }
294
295 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
296 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
297 return 0;
298 }
299
300 return 1;
301}
302
303int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
304 size_t *len)
305{
306 if (pkey->ameth->get_pub_key == NULL) {
307 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
308 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
309 return 0;
310 }
311
312 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
313 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
314 return 0;
315 }
316
317 return 1;
318}
319
b3831fbb
MC
320EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
321 size_t len, const EVP_CIPHER *cipher)
322{
df6d51e2 323#ifndef OPENSSL_NO_CMAC
3be06e0d 324# ifndef OPENSSL_NO_ENGINE
9a7846df 325 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
3be06e0d 326# endif
e74bd290
RL
327 const char *cipher_name = EVP_CIPHER_name(cipher);
328 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
329 OPENSSL_CTX *libctx =
330 prov == NULL ? NULL : ossl_provider_library_context(prov);
b3831fbb 331 EVP_PKEY *ret = EVP_PKEY_new();
81ff9eeb 332 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
e74bd290
RL
333 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
334 OSSL_PARAM params[4];
335 size_t paramsn = 0;
b3831fbb
MC
336
337 if (ret == NULL
338 || cmctx == NULL
339 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
340 /* EVPerr already called */
341 goto err;
342 }
343
3be06e0d 344# ifndef OPENSSL_NO_ENGINE
9a7846df 345 if (engine_id != NULL)
e74bd290
RL
346 params[paramsn++] =
347 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ENGINE,
9a7846df
MC
348 (char *)engine_id,
349 strlen(engine_id) + 1);
3be06e0d
MC
350# endif
351
e74bd290 352 params[paramsn++] =
703170d4 353 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
e74bd290
RL
354 (char *)cipher_name,
355 strlen(cipher_name) + 1);
356 params[paramsn++] =
357 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
358 (char *)priv, len);
359 params[paramsn] = OSSL_PARAM_construct_end();
360
361 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
b3831fbb
MC
362 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
363 goto err;
364 }
365
366 ret->pkey.ptr = cmctx;
367 return ret;
368
369 err:
370 EVP_PKEY_free(ret);
b8d77c9b 371 EVP_MAC_CTX_free(cmctx);
e74bd290 372 EVP_MAC_free(cmac);
b3831fbb 373 return NULL;
df6d51e2
MC
374#else
375 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
376 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
377 return NULL;
378#endif
b3831fbb 379}
a08802ce 380
01b8b3c7 381int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
0f113f3e 382{
a08802ce 383 return pkey_set_type(pkey, NULL, type, NULL, -1);
0f113f3e 384}
01b8b3c7
DSH
385
386int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
0f113f3e 387{
a08802ce 388 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
0f113f3e 389}
2f2e6b62
JL
390
391int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
392{
393 if (pkey->type == type) {
394 return 1; /* it already is that type */
395 }
396
397 /*
398 * The application is requesting to alias this to a different pkey type,
399 * but not one that resolves to the base type.
400 */
401 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
402 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
403 return 0;
404 }
405
406 pkey->type = type;
407 return 1;
408}
409
d19b01ad
DSH
410#ifndef OPENSSL_NO_ENGINE
411int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
412{
413 if (e != NULL) {
414 if (!ENGINE_init(e)) {
415 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
416 return 0;
417 }
418 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
419 ENGINE_finish(e);
420 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
421 return 0;
422 }
423 }
424 ENGINE_finish(pkey->pmeth_engine);
425 pkey->pmeth_engine = e;
426 return 1;
427}
229f7b38
DB
428
429ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
430{
431 return pkey->engine;
432}
d19b01ad 433#endif
01b8b3c7 434int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
0f113f3e 435{
e34c66c6 436 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
0f113f3e
MC
437 return 0;
438 pkey->pkey.ptr = key;
439 return (key != NULL);
440}
d02b48c6 441
3aeb9348 442void *EVP_PKEY_get0(const EVP_PKEY *pkey)
0f113f3e
MC
443{
444 return pkey->pkey.ptr;
445}
db98bbc1 446
ebad0b0b
NM
447const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
448{
449 ASN1_OCTET_STRING *os = NULL;
450 if (pkey->type != EVP_PKEY_HMAC) {
451 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
452 return NULL;
453 }
454 os = EVP_PKEY_get0(pkey);
455 *len = os->length;
456 return os->data;
457}
458
52ad5b60
TS
459#ifndef OPENSSL_NO_POLY1305
460const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
461{
462 ASN1_OCTET_STRING *os = NULL;
463 if (pkey->type != EVP_PKEY_POLY1305) {
464 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
465 return NULL;
466 }
467 os = EVP_PKEY_get0(pkey);
468 *len = os->length;
469 return os->data;
470}
471#endif
472
3f5616d7
TS
473#ifndef OPENSSL_NO_SIPHASH
474const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
475{
476 ASN1_OCTET_STRING *os = NULL;
477
478 if (pkey->type != EVP_PKEY_SIPHASH) {
479 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
480 return NULL;
481 }
482 os = EVP_PKEY_get0(pkey);
483 *len = os->length;
484 return os->data;
485}
486#endif
487
cf1b7d96 488#ifndef OPENSSL_NO_RSA
c7cb16a8 489int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
52664f50 490{
0f113f3e
MC
491 int ret = EVP_PKEY_assign_RSA(pkey, key);
492 if (ret)
493 RSA_up_ref(key);
494 return ret;
52664f50
DSH
495}
496
9fdcc21f 497RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
0f113f3e
MC
498{
499 if (pkey->type != EVP_PKEY_RSA) {
2872dbe1 500 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
0f113f3e
MC
501 return NULL;
502 }
0f113f3e 503 return pkey->pkey.rsa;
f769ce3e 504}
2872dbe1
DSH
505
506RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
507{
508 RSA *ret = EVP_PKEY_get0_RSA(pkey);
509 if (ret != NULL)
510 RSA_up_ref(ret);
511 return ret;
512}
f769ce3e
DSH
513#endif
514
cf1b7d96 515#ifndef OPENSSL_NO_DSA
c7cb16a8 516int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
52664f50 517{
0f113f3e
MC
518 int ret = EVP_PKEY_assign_DSA(pkey, key);
519 if (ret)
520 DSA_up_ref(key);
521 return ret;
52664f50
DSH
522}
523
9fdcc21f 524DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
0f113f3e
MC
525{
526 if (pkey->type != EVP_PKEY_DSA) {
2872dbe1 527 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
0f113f3e
MC
528 return NULL;
529 }
0f113f3e 530 return pkey->pkey.dsa;
f769ce3e 531}
2872dbe1
DSH
532
533DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
534{
535 DSA *ret = EVP_PKEY_get0_DSA(pkey);
536 if (ret != NULL)
537 DSA_up_ref(ret);
538 return ret;
539}
f769ce3e
DSH
540#endif
541
14a7cfb3 542#ifndef OPENSSL_NO_EC
4d94ae00 543
14a7cfb3 544int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
4d94ae00 545{
0f113f3e
MC
546 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
547 if (ret)
548 EC_KEY_up_ref(key);
549 return ret;
4d94ae00
BM
550}
551
9fdcc21f 552EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
4d94ae00 553{
0f113f3e 554 if (pkey->type != EVP_PKEY_EC) {
2872dbe1 555 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
0f113f3e
MC
556 return NULL;
557 }
0f113f3e 558 return pkey->pkey.ec;
4d94ae00 559}
2872dbe1
DSH
560
561EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
562{
563 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
564 if (ret != NULL)
565 EC_KEY_up_ref(ret);
566 return ret;
567}
4d94ae00
BM
568#endif
569
cf1b7d96 570#ifndef OPENSSL_NO_DH
52664f50 571
c7cb16a8 572int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
52664f50 573{
0f113f3e
MC
574 int ret = EVP_PKEY_assign_DH(pkey, key);
575 if (ret)
576 DH_up_ref(key);
577 return ret;
52664f50
DSH
578}
579
9fdcc21f 580DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
0f113f3e
MC
581{
582 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
2872dbe1 583 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
0f113f3e
MC
584 return NULL;
585 }
0f113f3e 586 return pkey->pkey.dh;
f769ce3e 587}
2872dbe1
DSH
588
589DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
590{
591 DH *ret = EVP_PKEY_get0_DH(pkey);
592 if (ret != NULL)
593 DH_up_ref(ret);
594 return ret;
595}
f769ce3e
DSH
596#endif
597
6b691a5c 598int EVP_PKEY_type(int type)
0f113f3e
MC
599{
600 int ret;
601 const EVP_PKEY_ASN1_METHOD *ameth;
602 ENGINE *e;
603 ameth = EVP_PKEY_asn1_find(&e, type);
604 if (ameth)
605 ret = ameth->pkey_id;
606 else
607 ret = NID_undef;
01b8b3c7 608#ifndef OPENSSL_NO_ENGINE
7c96dbcd 609 ENGINE_finish(e);
01b8b3c7 610#endif
0f113f3e
MC
611 return ret;
612}
d02b48c6 613
7f57b076 614int EVP_PKEY_id(const EVP_PKEY *pkey)
0f113f3e
MC
615{
616 return pkey->type;
617}
7f57b076
DSH
618
619int EVP_PKEY_base_id(const EVP_PKEY *pkey)
0f113f3e
MC
620{
621 return EVP_PKEY_type(pkey->type);
622}
7f57b076 623
6b691a5c 624void EVP_PKEY_free(EVP_PKEY *x)
0f113f3e
MC
625{
626 int i;
d02b48c6 627
0f113f3e
MC
628 if (x == NULL)
629 return;
d02b48c6 630
2f545ae4 631 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
f3f1cf84 632 REF_PRINT_COUNT("EVP_PKEY", x);
0f113f3e
MC
633 if (i > 0)
634 return;
f3f1cf84 635 REF_ASSERT_ISNT(i < 0);
0f113f3e 636 EVP_PKEY_free_it(x);
d65c3615 637 CRYPTO_THREAD_lock_free(x->lock);
222561fe 638 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
0f113f3e
MC
639 OPENSSL_free(x);
640}
d02b48c6 641
6b691a5c 642static void EVP_PKEY_free_it(EVP_PKEY *x)
0f113f3e 643{
c5ba2d99 644 /* internal function; x is never NULL */
4cae07fe
RL
645
646 evp_keymgmt_clear_pkey_cache(x);
647
0f113f3e
MC
648 if (x->ameth && x->ameth->pkey_free) {
649 x->ameth->pkey_free(x);
650 x->pkey.ptr = NULL;
651 }
01b8b3c7 652#ifndef OPENSSL_NO_ENGINE
7c96dbcd
RS
653 ENGINE_finish(x->engine);
654 x->engine = NULL;
d19b01ad
DSH
655 ENGINE_finish(x->pmeth_engine);
656 x->pmeth_engine = NULL;
01b8b3c7 657#endif
0f113f3e 658}
d02b48c6 659
35208f36 660static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
661 const char *kstr)
662{
663 BIO_indent(out, indent, 128);
664 BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
665 kstr, OBJ_nid2ln(pkey->type));
666 return 1;
667}
35208f36
DSH
668
669int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
670 int indent, ASN1_PCTX *pctx)
671{
672 if (pkey->ameth && pkey->ameth->pub_print)
673 return pkey->ameth->pub_print(out, pkey, indent, pctx);
674
675 return unsup_alg(out, pkey, indent, "Public Key");
676}
35208f36
DSH
677
678int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
679 int indent, ASN1_PCTX *pctx)
680{
681 if (pkey->ameth && pkey->ameth->priv_print)
682 return pkey->ameth->priv_print(out, pkey, indent, pctx);
683
684 return unsup_alg(out, pkey, indent, "Private Key");
685}
35208f36
DSH
686
687int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
688 int indent, ASN1_PCTX *pctx)
689{
690 if (pkey->ameth && pkey->ameth->param_print)
691 return pkey->ameth->param_print(out, pkey, indent, pctx);
692 return unsup_alg(out, pkey, indent, "Parameters");
693}
03919683 694
5d6aaf8a 695static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
0f113f3e 696{
5d6aaf8a 697 if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
0f113f3e 698 return -2;
5d6aaf8a
DSH
699 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
700}
701
702int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
703{
704 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
705}
706
ecbb2fca
DW
707int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
708{
709 int rv, default_nid;
710
711 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
712 if (rv == -2) {
713 /*
714 * If there is a mandatory default digest and this isn't it, then
715 * the answer is 'no'.
716 */
717 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
718 if (rv == 2)
719 return (nid == default_nid);
720 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
721 if (rv == 0)
722 return -1;
723 }
724 return rv;
725}
726
5d6aaf8a
DSH
727int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
728 const unsigned char *pt, size_t ptlen)
729{
730 if (ptlen > INT_MAX)
731 return 0;
732 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
733 (void *)pt) <= 0)
734 return 0;
735 return 1;
736}
737
738size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
739{
740 int rv;
741 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
742 if (rv <= 0)
743 return 0;
744 return rv;
0f113f3e 745}