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