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