]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_lib.c
DSA: More conforming names in crypto/dsa/dsa_aid.c
[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
f41ac0ee
P
10/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
d02b48c6 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
cd420b0b 18#include "internal/refcount.h"
4d94ae00
BM
19#include <openssl/bn.h>
20#include <openssl/err.h>
ec577822
BM
21#include <openssl/objects.h>
22#include <openssl/evp.h>
ec577822 23#include <openssl/x509.h>
3c27208f
RS
24#include <openssl/rsa.h>
25#include <openssl/dsa.h>
26#include <openssl/dh.h>
b3831fbb 27#include <openssl/cmac.h>
3c27208f 28#include <openssl/engine.h>
e74bd290 29#include <openssl/params.h>
54c1711f 30#include <openssl/serializer.h>
e74bd290 31#include <openssl/core_names.h>
01b8b3c7 32
25f2138b
DMSP
33#include "crypto/asn1.h"
34#include "crypto/evp.h"
e74bd290 35#include "internal/provider.h"
f6aa5774 36#include "evp_local.h"
18e377b4 37
e683582b
SL
38static void evp_pkey_free_it(EVP_PKEY *key);
39
40#ifndef FIPS_MODE
bb2297a4 41
8900f3e3 42int EVP_PKEY_bits(const EVP_PKEY *pkey)
0f113f3e 43{
6508e858
RL
44 if (pkey != NULL) {
45 if (pkey->ameth == NULL)
46 return pkey->cache.bits;
47 else if (pkey->ameth->pkey_bits)
48 return pkey->ameth->pkey_bits(pkey);
49 }
0f113f3e
MC
50 return 0;
51}
58964a49 52
2514fa79 53int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
0f113f3e
MC
54{
55 if (pkey == NULL)
56 return 0;
6508e858
RL
57 if (pkey->ameth == NULL)
58 return pkey->cache.security_bits;
59 if (pkey->ameth->pkey_security_bits == NULL)
0f113f3e
MC
60 return -2;
61 return pkey->ameth->pkey_security_bits(pkey);
62}
2514fa79 63
6b691a5c 64int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
0f113f3e 65{
e683582b 66# ifndef OPENSSL_NO_DSA
0f113f3e
MC
67 if (pkey->type == EVP_PKEY_DSA) {
68 int ret = pkey->save_parameters;
69
70 if (mode >= 0)
71 pkey->save_parameters = mode;
26a7d938 72 return ret;
0f113f3e 73 }
e683582b
SL
74# endif
75# ifndef OPENSSL_NO_EC
0f113f3e
MC
76 if (pkey->type == EVP_PKEY_EC) {
77 int ret = pkey->save_parameters;
78
79 if (mode >= 0)
80 pkey->save_parameters = mode;
26a7d938 81 return ret;
0f113f3e 82 }
e683582b 83# endif
26a7d938 84 return 0;
0f113f3e 85}
d02b48c6 86
a8b72844 87int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
0f113f3e 88{
2986ecdc
DSH
89 if (to->type == EVP_PKEY_NONE) {
90 if (EVP_PKEY_set_type(to, from->type) == 0)
91 return 0;
92 } else if (to->type != from->type) {
0f113f3e
MC
93 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
94 goto err;
95 }
96
97 if (EVP_PKEY_missing_parameters(from)) {
98 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
99 goto err;
100 }
f72f00d4
DSH
101
102 if (!EVP_PKEY_missing_parameters(to)) {
103 if (EVP_PKEY_cmp_parameters(to, from) == 1)
104 return 1;
105 EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
106 return 0;
107 }
108
0f113f3e
MC
109 if (from->ameth && from->ameth->param_copy)
110 return from->ameth->param_copy(to, from);
111 err:
112 return 0;
113}
d02b48c6 114
af0f0f3e 115int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
0f113f3e 116{
ab5c77b4 117 if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing)
0f113f3e
MC
118 return pkey->ameth->param_missing(pkey);
119 return 0;
120}
d02b48c6 121
af0f0f3e 122int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
123{
124 if (a->type != b->type)
125 return -1;
126 if (a->ameth && a->ameth->param_cmp)
127 return a->ameth->param_cmp(a, b);
128 return -2;
129}
58964a49 130
af0f0f3e 131int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
0f113f3e
MC
132{
133 if (a->type != b->type)
134 return -1;
135
136 if (a->ameth) {
137 int ret;
138 /* Compare parameters if the algorithm has them */
139 if (a->ameth->param_cmp) {
140 ret = a->ameth->param_cmp(a, b);
141 if (ret <= 0)
142 return ret;
143 }
144
145 if (a->ameth->pub_cmp)
146 return a->ameth->pub_cmp(a, b);
147 }
148
149 return -2;
150}
e6526fbf 151
2872dbe1 152
0f113f3e
MC
153/*
154 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
155 * is NULL just return 1 or 0 if the algorithm exists.
01b8b3c7
DSH
156 */
157
a08802ce
MC
158static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
159 int len)
0f113f3e
MC
160{
161 const EVP_PKEY_ASN1_METHOD *ameth;
a08802ce
MC
162 ENGINE **eptr = (e == NULL) ? &e : NULL;
163
0f113f3e
MC
164 if (pkey) {
165 if (pkey->pkey.ptr)
e683582b 166 evp_pkey_free_it(pkey);
0f113f3e
MC
167 /*
168 * If key type matches and a method exists then this lookup has
169 * succeeded once so just indicate success.
170 */
171 if ((type == pkey->save_type) && pkey->ameth)
172 return 1;
e683582b 173# ifndef OPENSSL_NO_ENGINE
d19b01ad 174 /* If we have ENGINEs release them */
7c96dbcd
RS
175 ENGINE_finish(pkey->engine);
176 pkey->engine = NULL;
d19b01ad
DSH
177 ENGINE_finish(pkey->pmeth_engine);
178 pkey->pmeth_engine = NULL;
e683582b 179# endif
0f113f3e
MC
180 }
181 if (str)
a08802ce 182 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
0f113f3e 183 else
a08802ce 184 ameth = EVP_PKEY_asn1_find(eptr, type);
e683582b 185# ifndef OPENSSL_NO_ENGINE
a08802ce 186 if (pkey == NULL && eptr != NULL)
0f113f3e 187 ENGINE_finish(e);
e683582b 188# endif
7c96dbcd 189 if (ameth == NULL) {
0f113f3e
MC
190 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
191 return 0;
192 }
193 if (pkey) {
194 pkey->ameth = ameth;
195 pkey->engine = e;
196
197 pkey->type = pkey->ameth->pkey_id;
198 pkey->save_type = type;
199 }
200 return 1;
201}
01b8b3c7 202
f929439f
MC
203EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
204 const unsigned char *priv,
205 size_t len)
a08802ce
MC
206{
207 EVP_PKEY *ret = EVP_PKEY_new();
208
209 if (ret == NULL
210 || !pkey_set_type(ret, e, type, NULL, -1)) {
211 /* EVPerr already called */
212 goto err;
213 }
214
215 if (ret->ameth->set_priv_key == NULL) {
f929439f 216 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
a08802ce
MC
217 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
218 goto err;
219 }
220
221 if (!ret->ameth->set_priv_key(ret, priv, len)) {
f929439f 222 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
a08802ce
MC
223 goto err;
224 }
225
226 return ret;
227
228 err:
229 EVP_PKEY_free(ret);
230 return NULL;
231}
232
f929439f
MC
233EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
234 const unsigned char *pub,
235 size_t len)
a08802ce
MC
236{
237 EVP_PKEY *ret = EVP_PKEY_new();
238
239 if (ret == NULL
240 || !pkey_set_type(ret, e, type, NULL, -1)) {
241 /* EVPerr already called */
242 goto err;
243 }
244
245 if (ret->ameth->set_pub_key == NULL) {
f929439f 246 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
a08802ce
MC
247 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
248 goto err;
249 }
250
251 if (!ret->ameth->set_pub_key(ret, pub, len)) {
f929439f 252 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
a08802ce
MC
253 goto err;
254 }
255
256 return ret;
257
258 err:
259 EVP_PKEY_free(ret);
260 return NULL;
261}
262
0d124b0a
MC
263int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
264 size_t *len)
265{
266 if (pkey->ameth->get_priv_key == NULL) {
267 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
268 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
269 return 0;
270 }
271
272 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
273 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
274 return 0;
275 }
276
277 return 1;
278}
279
280int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
281 size_t *len)
282{
283 if (pkey->ameth->get_pub_key == NULL) {
284 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
285 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
286 return 0;
287 }
288
289 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
290 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
291 return 0;
292 }
293
294 return 1;
295}
296
b3831fbb
MC
297EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
298 size_t len, const EVP_CIPHER *cipher)
299{
e683582b
SL
300# ifndef OPENSSL_NO_CMAC
301# ifndef OPENSSL_NO_ENGINE
9a7846df 302 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
e683582b 303# endif
e74bd290
RL
304 const char *cipher_name = EVP_CIPHER_name(cipher);
305 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
306 OPENSSL_CTX *libctx =
307 prov == NULL ? NULL : ossl_provider_library_context(prov);
b3831fbb 308 EVP_PKEY *ret = EVP_PKEY_new();
81ff9eeb 309 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
e74bd290
RL
310 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
311 OSSL_PARAM params[4];
312 size_t paramsn = 0;
b3831fbb
MC
313
314 if (ret == NULL
315 || cmctx == NULL
316 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
317 /* EVPerr already called */
318 goto err;
319 }
320
e683582b 321# ifndef OPENSSL_NO_ENGINE
9a7846df 322 if (engine_id != NULL)
e74bd290 323 params[paramsn++] =
69db3044 324 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
e683582b 325# endif
3be06e0d 326
e74bd290 327 params[paramsn++] =
703170d4 328 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
7f588d20 329 (char *)cipher_name, 0);
e74bd290
RL
330 params[paramsn++] =
331 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
332 (char *)priv, len);
333 params[paramsn] = OSSL_PARAM_construct_end();
334
335 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
b3831fbb
MC
336 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
337 goto err;
338 }
339
340 ret->pkey.ptr = cmctx;
341 return ret;
342
343 err:
344 EVP_PKEY_free(ret);
b8d77c9b 345 EVP_MAC_CTX_free(cmctx);
e74bd290 346 EVP_MAC_free(cmac);
b3831fbb 347 return NULL;
e683582b 348# else
df6d51e2
MC
349 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
350 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
351 return NULL;
e683582b 352# endif
b3831fbb 353}
a08802ce 354
01b8b3c7 355int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
0f113f3e 356{
a08802ce 357 return pkey_set_type(pkey, NULL, type, NULL, -1);
0f113f3e 358}
01b8b3c7
DSH
359
360int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
0f113f3e 361{
a08802ce 362 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
0f113f3e 363}
2f2e6b62
JL
364
365int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
366{
367 if (pkey->type == type) {
368 return 1; /* it already is that type */
369 }
370
371 /*
372 * The application is requesting to alias this to a different pkey type,
373 * but not one that resolves to the base type.
374 */
375 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
376 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
377 return 0;
378 }
379
380 pkey->type = type;
381 return 1;
382}
383
e683582b 384# ifndef OPENSSL_NO_ENGINE
d19b01ad
DSH
385int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
386{
387 if (e != NULL) {
388 if (!ENGINE_init(e)) {
389 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
390 return 0;
391 }
392 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
393 ENGINE_finish(e);
394 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
395 return 0;
396 }
397 }
398 ENGINE_finish(pkey->pmeth_engine);
399 pkey->pmeth_engine = e;
400 return 1;
401}
229f7b38
DB
402
403ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
404{
405 return pkey->engine;
406}
e683582b 407# endif
01b8b3c7 408int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
0f113f3e 409{
f4e4382c
RL
410 int alias = type;
411
ad5b71be 412#ifndef OPENSSL_NO_EC
f4e4382c
RL
413 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
414 const EC_GROUP *group = EC_KEY_get0_group(key);
415
416 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
417 alias = EVP_PKEY_SM2;
418 }
ad5b71be 419#endif
f4e4382c 420
e34c66c6 421 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
0f113f3e 422 return 0;
f4e4382c
RL
423 if (!EVP_PKEY_set_alias_type(pkey, alias))
424 return 0;
0f113f3e
MC
425 pkey->pkey.ptr = key;
426 return (key != NULL);
427}
d02b48c6 428
3aeb9348 429void *EVP_PKEY_get0(const EVP_PKEY *pkey)
0f113f3e
MC
430{
431 return pkey->pkey.ptr;
432}
db98bbc1 433
ebad0b0b
NM
434const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
435{
436 ASN1_OCTET_STRING *os = NULL;
437 if (pkey->type != EVP_PKEY_HMAC) {
438 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
439 return NULL;
440 }
441 os = EVP_PKEY_get0(pkey);
442 *len = os->length;
443 return os->data;
444}
445
e683582b 446# ifndef OPENSSL_NO_POLY1305
52ad5b60
TS
447const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
448{
449 ASN1_OCTET_STRING *os = NULL;
450 if (pkey->type != EVP_PKEY_POLY1305) {
451 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
452 return NULL;
453 }
454 os = EVP_PKEY_get0(pkey);
455 *len = os->length;
456 return os->data;
457}
e683582b 458# endif
52ad5b60 459
e683582b 460# ifndef OPENSSL_NO_SIPHASH
3f5616d7
TS
461const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
462{
463 ASN1_OCTET_STRING *os = NULL;
464
465 if (pkey->type != EVP_PKEY_SIPHASH) {
466 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
467 return NULL;
468 }
469 os = EVP_PKEY_get0(pkey);
470 *len = os->length;
471 return os->data;
472}
e683582b 473# endif
3f5616d7 474
e683582b 475# ifndef OPENSSL_NO_RSA
c7cb16a8 476int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
52664f50 477{
0f113f3e
MC
478 int ret = EVP_PKEY_assign_RSA(pkey, key);
479 if (ret)
480 RSA_up_ref(key);
481 return ret;
52664f50
DSH
482}
483
9fdcc21f 484RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
0f113f3e 485{
465a58b1 486 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
2872dbe1 487 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
0f113f3e
MC
488 return NULL;
489 }
0f113f3e 490 return pkey->pkey.rsa;
f769ce3e 491}
2872dbe1
DSH
492
493RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
494{
495 RSA *ret = EVP_PKEY_get0_RSA(pkey);
496 if (ret != NULL)
497 RSA_up_ref(ret);
498 return ret;
499}
e683582b 500# endif
f769ce3e 501
e683582b 502# ifndef OPENSSL_NO_DSA
c7cb16a8 503int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
52664f50 504{
0f113f3e
MC
505 int ret = EVP_PKEY_assign_DSA(pkey, key);
506 if (ret)
507 DSA_up_ref(key);
508 return ret;
52664f50
DSH
509}
510
9fdcc21f 511DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
0f113f3e
MC
512{
513 if (pkey->type != EVP_PKEY_DSA) {
2872dbe1 514 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
0f113f3e
MC
515 return NULL;
516 }
0f113f3e 517 return pkey->pkey.dsa;
f769ce3e 518}
2872dbe1
DSH
519
520DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
521{
522 DSA *ret = EVP_PKEY_get0_DSA(pkey);
523 if (ret != NULL)
524 DSA_up_ref(ret);
525 return ret;
526}
e683582b 527# endif
f769ce3e 528
e683582b 529# ifndef OPENSSL_NO_EC
4d94ae00 530
14a7cfb3 531int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
4d94ae00 532{
0f113f3e
MC
533 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
534 if (ret)
535 EC_KEY_up_ref(key);
536 return ret;
4d94ae00
BM
537}
538
9fdcc21f 539EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
4d94ae00 540{
f4e4382c 541 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
2872dbe1 542 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
0f113f3e
MC
543 return NULL;
544 }
0f113f3e 545 return pkey->pkey.ec;
4d94ae00 546}
2872dbe1
DSH
547
548EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
549{
550 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
551 if (ret != NULL)
552 EC_KEY_up_ref(ret);
553 return ret;
554}
e683582b 555# endif
4d94ae00 556
e683582b 557# ifndef OPENSSL_NO_DH
52664f50 558
c7cb16a8 559int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
52664f50 560{
32c869ff
MC
561 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
562 int ret = EVP_PKEY_assign(pkey, type, key);
563
0f113f3e
MC
564 if (ret)
565 DH_up_ref(key);
566 return ret;
52664f50
DSH
567}
568
9fdcc21f 569DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
0f113f3e
MC
570{
571 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
2872dbe1 572 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
0f113f3e
MC
573 return NULL;
574 }
0f113f3e 575 return pkey->pkey.dh;
f769ce3e 576}
2872dbe1
DSH
577
578DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
579{
580 DH *ret = EVP_PKEY_get0_DH(pkey);
581 if (ret != NULL)
582 DH_up_ref(ret);
583 return ret;
584}
e683582b 585# endif
f769ce3e 586
6b691a5c 587int EVP_PKEY_type(int type)
0f113f3e
MC
588{
589 int ret;
590 const EVP_PKEY_ASN1_METHOD *ameth;
591 ENGINE *e;
592 ameth = EVP_PKEY_asn1_find(&e, type);
593 if (ameth)
594 ret = ameth->pkey_id;
595 else
596 ret = NID_undef;
e683582b 597# ifndef OPENSSL_NO_ENGINE
7c96dbcd 598 ENGINE_finish(e);
e683582b 599# endif
0f113f3e
MC
600 return ret;
601}
d02b48c6 602
7f57b076 603int EVP_PKEY_id(const EVP_PKEY *pkey)
0f113f3e
MC
604{
605 return pkey->type;
606}
7f57b076
DSH
607
608int EVP_PKEY_base_id(const EVP_PKEY *pkey)
0f113f3e
MC
609{
610 return EVP_PKEY_type(pkey->type);
611}
7f57b076 612
d02b48c6 613
f1299839
RL
614static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
615{
616 BIO_set_indent(*out, saved_indent);
617 if (pop_f_prefix) {
618 BIO *next = BIO_pop(*out);
619
620 BIO_free(*out);
621 *out = next;
622 }
623 return 1;
624}
625
626static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
627 long indent)
628{
629 *pop_f_prefix = 0;
630 *saved_indent = 0;
631 if (indent > 0) {
632 long i = BIO_get_indent(*out);
633
634 *saved_indent = (i < 0 ? 0 : i);
635 if (BIO_set_indent(*out, indent) <= 0) {
636 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
637 return 0;
638 *pop_f_prefix = 1;
639 }
640 if (BIO_set_indent(*out, indent) <= 0) {
641 print_reset_indent(out, *pop_f_prefix, *saved_indent);
642 return 0;
643 }
644 }
645 return 1;
646}
647
35208f36 648static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
0f113f3e
MC
649 const char *kstr)
650{
5310a4e6
P
651 return BIO_indent(out, indent, 128)
652 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
653 kstr, OBJ_nid2ln(pkey->type)) > 0;
0f113f3e 654}
35208f36 655
f1299839
RL
656static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
657 const char *propquery /* For provided serialization */,
658 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
659 int indent, ASN1_PCTX *pctx),
660 ASN1_PCTX *legacy_pctx /* For legacy print */)
0f113f3e 661{
f1299839
RL
662 int pop_f_prefix;
663 long saved_indent;
664 OSSL_SERIALIZER_CTX *ctx = NULL;
665 int ret = -2; /* default to unsupported */
666
667 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
668 return 0;
54c1711f 669
f1299839 670 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
54c1711f
RL
671 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
672 ret = OSSL_SERIALIZER_to_bio(ctx, out);
673 OSSL_SERIALIZER_CTX_free(ctx);
674
675 if (ret != -2)
f1299839 676 goto end;
54c1711f
RL
677
678 /* legacy fallback */
f1299839
RL
679 if (legacy_print != NULL)
680 ret = legacy_print(out, pkey, 0, legacy_pctx);
681 else
682 ret = unsup_alg(out, pkey, 0, "Public Key");
0f113f3e 683
f1299839
RL
684 end:
685 print_reset_indent(&out, pop_f_prefix, saved_indent);
686 return ret;
687}
688
689int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
690 int indent, ASN1_PCTX *pctx)
691{
692 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
693 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
694 pctx);
0f113f3e 695}
35208f36
DSH
696
697int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
698 int indent, ASN1_PCTX *pctx)
699{
f1299839
RL
700 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
701 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
702 pctx);
0f113f3e 703}
35208f36
DSH
704
705int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
0f113f3e
MC
706 int indent, ASN1_PCTX *pctx)
707{
f1299839
RL
708 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
709 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
710 pctx);
0f113f3e 711}
03919683 712
ead0d234
RL
713static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
714 int arg1, void *arg2)
715{
716 if (pkey->pkeys[0].keymgmt == NULL)
717 return 0;
718 switch (op) {
719 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
720 {
721 char mdname[80] = "";
722 int nid;
723 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
724 sizeof(mdname));
725
726 if (rv <= 0)
727 return rv;
728 nid = OBJ_sn2nid(mdname);
729 if (nid == NID_undef)
730 nid = OBJ_ln2nid(mdname);
731 if (nid == NID_undef)
732 return 0;
733 *(int *)arg2 = nid;
734 return 1;
735 }
736 default:
737 return -2;
738 }
739}
740
5d6aaf8a 741static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
0f113f3e 742{
ead0d234
RL
743 if (pkey->ameth == NULL)
744 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
745 if (pkey->ameth->pkey_ctrl == NULL)
0f113f3e 746 return -2;
5d6aaf8a
DSH
747 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
748}
749
750int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
751{
752 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
753}
754
ead0d234
RL
755int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
756 char *mdname, size_t mdname_sz)
757{
758 if (pkey->ameth == NULL) {
759 OSSL_PARAM params[3];
760 char mddefault[100] = "";
761 char mdmandatory[100] = "";
762
763 params[0] =
764 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
765 mddefault, sizeof(mddefault));
766 params[1] =
767 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
768 mdmandatory,
769 sizeof(mdmandatory));
770 params[2] = OSSL_PARAM_construct_end();
b305452f
RL
771 if (!evp_keymgmt_get_params(pkey->pkeys[0].keymgmt,
772 pkey->pkeys[0].keydata,
773 params))
ead0d234
RL
774 return 0;
775 if (mdmandatory[0] != '\0') {
776 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
777 return 2;
778 }
779 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
780 return 1;
781 }
782
783 {
784 int nid = NID_undef;
785 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
786 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
787
788 if (rv > 0)
789 OPENSSL_strlcpy(mdname, name, mdname_sz);
790 return rv;
791 }
792}
793
ecbb2fca
DW
794int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
795{
796 int rv, default_nid;
797
798 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
799 if (rv == -2) {
800 /*
801 * If there is a mandatory default digest and this isn't it, then
802 * the answer is 'no'.
803 */
804 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
805 if (rv == 2)
806 return (nid == default_nid);
807 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
808 if (rv == 0)
809 return -1;
810 }
811 return rv;
812}
813
5d6aaf8a
DSH
814int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
815 const unsigned char *pt, size_t ptlen)
816{
817 if (ptlen > INT_MAX)
818 return 0;
819 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
820 (void *)pt) <= 0)
821 return 0;
822 return 1;
823}
824
825size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
826{
827 int rv;
828 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
829 if (rv <= 0)
830 return 0;
831 return rv;
0f113f3e 832}
e683582b
SL
833
834#endif /* FIPS_MODE */
835
836/*- All methods below can also be used in FIPS_MODE */
837
838EVP_PKEY *EVP_PKEY_new(void)
839{
840 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
841
842 if (ret == NULL) {
843 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
844 return NULL;
845 }
846 ret->type = EVP_PKEY_NONE;
847 ret->save_type = EVP_PKEY_NONE;
848 ret->references = 1;
849 ret->save_parameters = 1;
850 ret->lock = CRYPTO_THREAD_lock_new();
851 if (ret->lock == NULL) {
852 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
853 OPENSSL_free(ret);
854 return NULL;
855 }
856 return ret;
857}
858
859int EVP_PKEY_up_ref(EVP_PKEY *pkey)
860{
861 int i;
862
863 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
864 return 0;
865
866 REF_PRINT_COUNT("EVP_PKEY", pkey);
867 REF_ASSERT_ISNT(i < 2);
868 return ((i > 1) ? 1 : 0);
869}
870
871static void evp_pkey_free_it(EVP_PKEY *x)
872{
873 /* internal function; x is never NULL */
874
68552cde 875 evp_keymgmt_util_clear_pkey_cache(x);
e683582b
SL
876
877 if (x->ameth && x->ameth->pkey_free) {
878 x->ameth->pkey_free(x);
879 x->pkey.ptr = NULL;
880 }
881#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
882 ENGINE_finish(x->engine);
883 x->engine = NULL;
884 ENGINE_finish(x->pmeth_engine);
885 x->pmeth_engine = NULL;
886#endif
887}
888
889void EVP_PKEY_free(EVP_PKEY *x)
890{
891 int i;
892
893 if (x == NULL)
894 return;
895
896 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
897 REF_PRINT_COUNT("EVP_PKEY", x);
898 if (i > 0)
899 return;
900 REF_ASSERT_ISNT(i < 0);
901 evp_pkey_free_it(x);
902 CRYPTO_THREAD_lock_free(x->lock);
903#ifndef FIPS_MODE
904 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
905#endif
906 OPENSSL_free(x);
907}
908
e683582b
SL
909int EVP_PKEY_size(const EVP_PKEY *pkey)
910{
6508e858
RL
911 if (pkey != NULL) {
912 if (pkey->ameth == NULL)
913 return pkey->cache.size;
914 else if (pkey->ameth->pkey_size != NULL)
915 return pkey->ameth->pkey_size(pkey);
916 }
e683582b
SL
917 return 0;
918}
f6aa5774
RL
919
920void *evp_pkey_make_provided(EVP_PKEY *pk, OPENSSL_CTX *libctx,
b305452f 921 EVP_KEYMGMT **keymgmt, const char *propquery)
f6aa5774
RL
922{
923 EVP_KEYMGMT *allocated_keymgmt = NULL;
924 EVP_KEYMGMT *tmp_keymgmt = NULL;
b305452f 925 void *keydata = NULL;
f6aa5774
RL
926
927 if (pk == NULL)
928 return NULL;
929
930 if (keymgmt != NULL) {
931 tmp_keymgmt = *keymgmt;
932 *keymgmt = NULL;
933 }
934
935 if (tmp_keymgmt == NULL) {
2ee4a50a 936 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
f6aa5774
RL
937
938 if (ctx != NULL && ctx->keytype != NULL)
939 tmp_keymgmt = allocated_keymgmt =
940 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
941 EVP_PKEY_CTX_free(ctx);
942 }
943
944 if (tmp_keymgmt != NULL)
b305452f
RL
945 keydata =
946 evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
f6aa5774
RL
947
948 /*
949 * If nothing was exported, |tmp_keymgmt| might point at a freed
950 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
951 * the caller either way in that case.
952 */
b305452f 953 if (keydata == NULL)
f6aa5774
RL
954 tmp_keymgmt = NULL;
955
956 if (keymgmt != NULL)
957 *keymgmt = tmp_keymgmt;
958
959 EVP_KEYMGMT_free(allocated_keymgmt);
b305452f 960 return keydata;
f6aa5774 961}