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