]> git.ipfire.org Git - thirdparty/openssl.git/blame_incremental - crypto/evp/p_lib.c
KEYMGMT: Add a keydata matching function
[thirdparty/openssl.git] / crypto / evp / p_lib.c
... / ...
CommitLineData
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/*
11 * DSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
16#include <stdio.h>
17#include "internal/cryptlib.h"
18#include "internal/refcount.h"
19#include <openssl/bn.h>
20#include <openssl/err.h>
21#include <openssl/objects.h>
22#include <openssl/evp.h>
23#include <openssl/x509.h>
24#include <openssl/rsa.h>
25#include <openssl/dsa.h>
26#include <openssl/dh.h>
27#include <openssl/cmac.h>
28#include <openssl/engine.h>
29#include <openssl/params.h>
30#include <openssl/serializer.h>
31#include <openssl/core_names.h>
32
33#include "crypto/asn1.h"
34#include "crypto/evp.h"
35#include "internal/provider.h"
36#include "evp_local.h"
37
38static void evp_pkey_free_it(EVP_PKEY *key);
39
40#ifndef FIPS_MODE
41
42int EVP_PKEY_bits(const EVP_PKEY *pkey)
43{
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 }
50 return 0;
51}
52
53int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
54{
55 if (pkey == NULL)
56 return 0;
57 if (pkey->ameth == NULL)
58 return pkey->cache.security_bits;
59 if (pkey->ameth->pkey_security_bits == NULL)
60 return -2;
61 return pkey->ameth->pkey_security_bits(pkey);
62}
63
64int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
65{
66# ifndef OPENSSL_NO_DSA
67 if (pkey->type == EVP_PKEY_DSA) {
68 int ret = pkey->save_parameters;
69
70 if (mode >= 0)
71 pkey->save_parameters = mode;
72 return ret;
73 }
74# endif
75# ifndef OPENSSL_NO_EC
76 if (pkey->type == EVP_PKEY_EC) {
77 int ret = pkey->save_parameters;
78
79 if (mode >= 0)
80 pkey->save_parameters = mode;
81 return ret;
82 }
83# endif
84 return 0;
85}
86
87int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
88{
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) {
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 }
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
109 if (from->ameth && from->ameth->param_copy)
110 return from->ameth->param_copy(to, from);
111 err:
112 return 0;
113}
114
115int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
116{
117 if (pkey != NULL) {
118 if (pkey->keymgmt != NULL)
119 return !evp_keymgmt_util_has((EVP_PKEY *)pkey,
120 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
121 else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
122 return pkey->ameth->param_missing(pkey);
123 }
124 return 0;
125}
126
127int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
128{
129 if (a->type != b->type)
130 return -1;
131 if (a->ameth && a->ameth->param_cmp)
132 return a->ameth->param_cmp(a, b);
133 return -2;
134}
135
136int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
137{
138 if (a->type != b->type)
139 return -1;
140
141 if (a->ameth) {
142 int ret;
143 /* Compare parameters if the algorithm has them */
144 if (a->ameth->param_cmp) {
145 ret = a->ameth->param_cmp(a, b);
146 if (ret <= 0)
147 return ret;
148 }
149
150 if (a->ameth->pub_cmp)
151 return a->ameth->pub_cmp(a, b);
152 }
153
154 return -2;
155}
156
157
158/*
159 * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
160 * is NULL just return 1 or 0 if the algorithm exists.
161 */
162
163static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
164 int len)
165{
166 const EVP_PKEY_ASN1_METHOD *ameth;
167 ENGINE **eptr = (e == NULL) ? &e : NULL;
168
169 if (pkey) {
170 if (pkey->pkey.ptr)
171 evp_pkey_free_it(pkey);
172 /*
173 * If key type matches and a method exists then this lookup has
174 * succeeded once so just indicate success.
175 */
176 if ((type == pkey->save_type) && pkey->ameth)
177 return 1;
178# ifndef OPENSSL_NO_ENGINE
179 /* If we have ENGINEs release them */
180 ENGINE_finish(pkey->engine);
181 pkey->engine = NULL;
182 ENGINE_finish(pkey->pmeth_engine);
183 pkey->pmeth_engine = NULL;
184# endif
185 }
186 if (str)
187 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
188 else
189 ameth = EVP_PKEY_asn1_find(eptr, type);
190# ifndef OPENSSL_NO_ENGINE
191 if (pkey == NULL && eptr != NULL)
192 ENGINE_finish(e);
193# endif
194 if (ameth == NULL) {
195 EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
196 return 0;
197 }
198 if (pkey) {
199 pkey->ameth = ameth;
200 pkey->engine = e;
201
202 pkey->type = pkey->ameth->pkey_id;
203 pkey->save_type = type;
204 }
205 return 1;
206}
207
208EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
209 const unsigned char *priv,
210 size_t len)
211{
212 EVP_PKEY *ret = EVP_PKEY_new();
213
214 if (ret == NULL
215 || !pkey_set_type(ret, e, type, NULL, -1)) {
216 /* EVPerr already called */
217 goto err;
218 }
219
220 if (ret->ameth->set_priv_key == NULL) {
221 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
222 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
223 goto err;
224 }
225
226 if (!ret->ameth->set_priv_key(ret, priv, len)) {
227 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
228 goto err;
229 }
230
231 return ret;
232
233 err:
234 EVP_PKEY_free(ret);
235 return NULL;
236}
237
238EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
239 const unsigned char *pub,
240 size_t len)
241{
242 EVP_PKEY *ret = EVP_PKEY_new();
243
244 if (ret == NULL
245 || !pkey_set_type(ret, e, type, NULL, -1)) {
246 /* EVPerr already called */
247 goto err;
248 }
249
250 if (ret->ameth->set_pub_key == NULL) {
251 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
252 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
253 goto err;
254 }
255
256 if (!ret->ameth->set_pub_key(ret, pub, len)) {
257 EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
258 goto err;
259 }
260
261 return ret;
262
263 err:
264 EVP_PKEY_free(ret);
265 return NULL;
266}
267
268int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
269 size_t *len)
270{
271 if (pkey->ameth->get_priv_key == NULL) {
272 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
273 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
274 return 0;
275 }
276
277 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
278 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
279 return 0;
280 }
281
282 return 1;
283}
284
285int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
286 size_t *len)
287{
288 if (pkey->ameth->get_pub_key == NULL) {
289 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
290 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
291 return 0;
292 }
293
294 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
295 EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
296 return 0;
297 }
298
299 return 1;
300}
301
302EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
303 size_t len, const EVP_CIPHER *cipher)
304{
305# ifndef OPENSSL_NO_CMAC
306# ifndef OPENSSL_NO_ENGINE
307 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
308# endif
309 const char *cipher_name = EVP_CIPHER_name(cipher);
310 const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher);
311 OPENSSL_CTX *libctx =
312 prov == NULL ? NULL : ossl_provider_library_context(prov);
313 EVP_PKEY *ret = EVP_PKEY_new();
314 EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL);
315 EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL;
316 OSSL_PARAM params[4];
317 size_t paramsn = 0;
318
319 if (ret == NULL
320 || cmctx == NULL
321 || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
322 /* EVPerr already called */
323 goto err;
324 }
325
326# ifndef OPENSSL_NO_ENGINE
327 if (engine_id != NULL)
328 params[paramsn++] =
329 OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0);
330# endif
331
332 params[paramsn++] =
333 OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
334 (char *)cipher_name, 0);
335 params[paramsn++] =
336 OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
337 (char *)priv, len);
338 params[paramsn] = OSSL_PARAM_construct_end();
339
340 if (!EVP_MAC_CTX_set_params(cmctx, params)) {
341 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
342 goto err;
343 }
344
345 ret->pkey.ptr = cmctx;
346 return ret;
347
348 err:
349 EVP_PKEY_free(ret);
350 EVP_MAC_CTX_free(cmctx);
351 EVP_MAC_free(cmac);
352 return NULL;
353# else
354 EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
355 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
356 return NULL;
357# endif
358}
359
360int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
361{
362 return pkey_set_type(pkey, NULL, type, NULL, -1);
363}
364
365int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
366{
367 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
368}
369
370int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
371{
372 if (pkey->type == type) {
373 return 1; /* it already is that type */
374 }
375
376 /*
377 * The application is requesting to alias this to a different pkey type,
378 * but not one that resolves to the base type.
379 */
380 if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
381 EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
382 return 0;
383 }
384
385 pkey->type = type;
386 return 1;
387}
388
389# ifndef OPENSSL_NO_ENGINE
390int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
391{
392 if (e != NULL) {
393 if (!ENGINE_init(e)) {
394 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
395 return 0;
396 }
397 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
398 ENGINE_finish(e);
399 EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
400 return 0;
401 }
402 }
403 ENGINE_finish(pkey->pmeth_engine);
404 pkey->pmeth_engine = e;
405 return 1;
406}
407
408ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
409{
410 return pkey->engine;
411}
412# endif
413int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
414{
415 int alias = type;
416
417#ifndef OPENSSL_NO_EC
418 if (EVP_PKEY_type(type) == EVP_PKEY_EC) {
419 const EC_GROUP *group = EC_KEY_get0_group(key);
420
421 if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
422 alias = EVP_PKEY_SM2;
423 }
424#endif
425
426 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
427 return 0;
428 if (!EVP_PKEY_set_alias_type(pkey, alias))
429 return 0;
430 pkey->pkey.ptr = key;
431 return (key != NULL);
432}
433
434void *EVP_PKEY_get0(const EVP_PKEY *pkey)
435{
436 return pkey->pkey.ptr;
437}
438
439const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
440{
441 ASN1_OCTET_STRING *os = NULL;
442 if (pkey->type != EVP_PKEY_HMAC) {
443 EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
444 return NULL;
445 }
446 os = EVP_PKEY_get0(pkey);
447 *len = os->length;
448 return os->data;
449}
450
451# ifndef OPENSSL_NO_POLY1305
452const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
453{
454 ASN1_OCTET_STRING *os = NULL;
455 if (pkey->type != EVP_PKEY_POLY1305) {
456 EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
457 return NULL;
458 }
459 os = EVP_PKEY_get0(pkey);
460 *len = os->length;
461 return os->data;
462}
463# endif
464
465# ifndef OPENSSL_NO_SIPHASH
466const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
467{
468 ASN1_OCTET_STRING *os = NULL;
469
470 if (pkey->type != EVP_PKEY_SIPHASH) {
471 EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
472 return NULL;
473 }
474 os = EVP_PKEY_get0(pkey);
475 *len = os->length;
476 return os->data;
477}
478# endif
479
480# ifndef OPENSSL_NO_RSA
481int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
482{
483 int ret = EVP_PKEY_assign_RSA(pkey, key);
484 if (ret)
485 RSA_up_ref(key);
486 return ret;
487}
488
489RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey)
490{
491 if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
492 EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
493 return NULL;
494 }
495 return pkey->pkey.rsa;
496}
497
498RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
499{
500 RSA *ret = EVP_PKEY_get0_RSA(pkey);
501 if (ret != NULL)
502 RSA_up_ref(ret);
503 return ret;
504}
505# endif
506
507# ifndef OPENSSL_NO_DSA
508int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
509{
510 int ret = EVP_PKEY_assign_DSA(pkey, key);
511 if (ret)
512 DSA_up_ref(key);
513 return ret;
514}
515
516DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
517{
518 if (pkey->type != EVP_PKEY_DSA) {
519 EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
520 return NULL;
521 }
522 return pkey->pkey.dsa;
523}
524
525DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
526{
527 DSA *ret = EVP_PKEY_get0_DSA(pkey);
528 if (ret != NULL)
529 DSA_up_ref(ret);
530 return ret;
531}
532# endif
533
534# ifndef OPENSSL_NO_EC
535
536int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
537{
538 int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
539 if (ret)
540 EC_KEY_up_ref(key);
541 return ret;
542}
543
544EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
545{
546 if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
547 EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
548 return NULL;
549 }
550 return pkey->pkey.ec;
551}
552
553EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
554{
555 EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
556 if (ret != NULL)
557 EC_KEY_up_ref(ret);
558 return ret;
559}
560# endif
561
562# ifndef OPENSSL_NO_DH
563
564int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
565{
566 int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
567 int ret = EVP_PKEY_assign(pkey, type, key);
568
569 if (ret)
570 DH_up_ref(key);
571 return ret;
572}
573
574DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
575{
576 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
577 EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
578 return NULL;
579 }
580 return pkey->pkey.dh;
581}
582
583DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
584{
585 DH *ret = EVP_PKEY_get0_DH(pkey);
586 if (ret != NULL)
587 DH_up_ref(ret);
588 return ret;
589}
590# endif
591
592int EVP_PKEY_type(int type)
593{
594 int ret;
595 const EVP_PKEY_ASN1_METHOD *ameth;
596 ENGINE *e;
597 ameth = EVP_PKEY_asn1_find(&e, type);
598 if (ameth)
599 ret = ameth->pkey_id;
600 else
601 ret = NID_undef;
602# ifndef OPENSSL_NO_ENGINE
603 ENGINE_finish(e);
604# endif
605 return ret;
606}
607
608int EVP_PKEY_id(const EVP_PKEY *pkey)
609{
610 return pkey->type;
611}
612
613int EVP_PKEY_base_id(const EVP_PKEY *pkey)
614{
615 return EVP_PKEY_type(pkey->type);
616}
617
618
619static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
620{
621 BIO_set_indent(*out, saved_indent);
622 if (pop_f_prefix) {
623 BIO *next = BIO_pop(*out);
624
625 BIO_free(*out);
626 *out = next;
627 }
628 return 1;
629}
630
631static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
632 long indent)
633{
634 *pop_f_prefix = 0;
635 *saved_indent = 0;
636 if (indent > 0) {
637 long i = BIO_get_indent(*out);
638
639 *saved_indent = (i < 0 ? 0 : i);
640 if (BIO_set_indent(*out, indent) <= 0) {
641 if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
642 return 0;
643 *pop_f_prefix = 1;
644 }
645 if (BIO_set_indent(*out, indent) <= 0) {
646 print_reset_indent(out, *pop_f_prefix, *saved_indent);
647 return 0;
648 }
649 }
650 return 1;
651}
652
653static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
654 const char *kstr)
655{
656 return BIO_indent(out, indent, 128)
657 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
658 kstr, OBJ_nid2ln(pkey->type)) > 0;
659}
660
661static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
662 const char *propquery /* For provided serialization */,
663 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
664 int indent, ASN1_PCTX *pctx),
665 ASN1_PCTX *legacy_pctx /* For legacy print */)
666{
667 int pop_f_prefix;
668 long saved_indent;
669 OSSL_SERIALIZER_CTX *ctx = NULL;
670 int ret = -2; /* default to unsupported */
671
672 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
673 return 0;
674
675 ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery);
676 if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL)
677 ret = OSSL_SERIALIZER_to_bio(ctx, out);
678 OSSL_SERIALIZER_CTX_free(ctx);
679
680 if (ret != -2)
681 goto end;
682
683 /* legacy fallback */
684 if (legacy_print != NULL)
685 ret = legacy_print(out, pkey, 0, legacy_pctx);
686 else
687 ret = unsup_alg(out, pkey, 0, "Public Key");
688
689 end:
690 print_reset_indent(&out, pop_f_prefix, saved_indent);
691 return ret;
692}
693
694int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
695 int indent, ASN1_PCTX *pctx)
696{
697 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ,
698 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
699 pctx);
700}
701
702int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
703 int indent, ASN1_PCTX *pctx)
704{
705 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ,
706 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
707 pctx);
708}
709
710int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
711 int indent, ASN1_PCTX *pctx)
712{
713 return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ,
714 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
715 pctx);
716}
717
718static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
719 int arg1, void *arg2)
720{
721 if (pkey->keymgmt == NULL)
722 return 0;
723 switch (op) {
724 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
725 {
726 char mdname[80] = "";
727 int nid;
728 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
729 sizeof(mdname));
730
731 if (rv <= 0)
732 return rv;
733 nid = OBJ_sn2nid(mdname);
734 if (nid == NID_undef)
735 nid = OBJ_ln2nid(mdname);
736 if (nid == NID_undef)
737 return 0;
738 *(int *)arg2 = nid;
739 return 1;
740 }
741 default:
742 return -2;
743 }
744}
745
746static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
747{
748 if (pkey->ameth == NULL)
749 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
750 if (pkey->ameth->pkey_ctrl == NULL)
751 return -2;
752 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
753}
754
755int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
756{
757 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
758}
759
760int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
761 char *mdname, size_t mdname_sz)
762{
763 if (pkey->ameth == NULL) {
764 OSSL_PARAM params[3];
765 char mddefault[100] = "";
766 char mdmandatory[100] = "";
767
768 params[0] =
769 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST,
770 mddefault, sizeof(mddefault));
771 params[1] =
772 OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST,
773 mdmandatory,
774 sizeof(mdmandatory));
775 params[2] = OSSL_PARAM_construct_end();
776 if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
777 return 0;
778 if (mdmandatory[0] != '\0') {
779 OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz);
780 return 2;
781 }
782 OPENSSL_strlcpy(mdname, mddefault, mdname_sz);
783 return 1;
784 }
785
786 {
787 int nid = NID_undef;
788 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
789 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
790
791 if (rv > 0)
792 OPENSSL_strlcpy(mdname, name, mdname_sz);
793 return rv;
794 }
795}
796
797int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
798{
799 int rv, default_nid;
800
801 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
802 if (rv == -2) {
803 /*
804 * If there is a mandatory default digest and this isn't it, then
805 * the answer is 'no'.
806 */
807 rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
808 if (rv == 2)
809 return (nid == default_nid);
810 /* zero is an error from EVP_PKEY_get_default_digest_nid() */
811 if (rv == 0)
812 return -1;
813 }
814 return rv;
815}
816
817int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
818 const unsigned char *pt, size_t ptlen)
819{
820 if (ptlen > INT_MAX)
821 return 0;
822 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
823 (void *)pt) <= 0)
824 return 0;
825 return 1;
826}
827
828size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
829{
830 int rv;
831 rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
832 if (rv <= 0)
833 return 0;
834 return rv;
835}
836
837#endif /* FIPS_MODE */
838
839/*- All methods below can also be used in FIPS_MODE */
840
841EVP_PKEY *EVP_PKEY_new(void)
842{
843 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
844
845 if (ret == NULL) {
846 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
847 return NULL;
848 }
849 ret->type = EVP_PKEY_NONE;
850 ret->save_type = EVP_PKEY_NONE;
851 ret->references = 1;
852 ret->save_parameters = 1;
853 ret->lock = CRYPTO_THREAD_lock_new();
854 if (ret->lock == NULL) {
855 EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
856 OPENSSL_free(ret);
857 return NULL;
858 }
859 return ret;
860}
861
862int EVP_PKEY_up_ref(EVP_PKEY *pkey)
863{
864 int i;
865
866 if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
867 return 0;
868
869 REF_PRINT_COUNT("EVP_PKEY", pkey);
870 REF_ASSERT_ISNT(i < 2);
871 return ((i > 1) ? 1 : 0);
872}
873
874#ifndef FIPS_MODE
875static void evp_pkey_free_legacy(EVP_PKEY *x)
876{
877 if (x->ameth != NULL) {
878 if (x->ameth->pkey_free)
879 x->ameth->pkey_free(x);
880 x->pkey.ptr = NULL;
881 x->ameth = NULL;
882 }
883# ifndef OPENSSL_NO_ENGINE
884 ENGINE_finish(x->engine);
885 x->engine = NULL;
886 ENGINE_finish(x->pmeth_engine);
887 x->pmeth_engine = NULL;
888# endif
889 x->type = x->save_type = EVP_PKEY_NONE;
890}
891#endif /* FIPS_MODE */
892
893static void evp_pkey_free_it(EVP_PKEY *x)
894{
895 /* internal function; x is never NULL */
896
897 evp_keymgmt_util_clear_operation_cache(x);
898#ifndef FIPS_MODE
899 evp_pkey_free_legacy(x);
900#endif
901
902 if (x->keymgmt != NULL) {
903 evp_keymgmt_freedata(x->keymgmt, x->keydata);
904 EVP_KEYMGMT_free(x->keymgmt);
905 x->keymgmt = NULL;
906 x->keydata = NULL;
907 }
908}
909
910void EVP_PKEY_free(EVP_PKEY *x)
911{
912 int i;
913
914 if (x == NULL)
915 return;
916
917 CRYPTO_DOWN_REF(&x->references, &i, x->lock);
918 REF_PRINT_COUNT("EVP_PKEY", x);
919 if (i > 0)
920 return;
921 REF_ASSERT_ISNT(i < 0);
922 evp_pkey_free_it(x);
923 CRYPTO_THREAD_lock_free(x->lock);
924#ifndef FIPS_MODE
925 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
926#endif
927 OPENSSL_free(x);
928}
929
930int EVP_PKEY_size(const EVP_PKEY *pkey)
931{
932 if (pkey != NULL) {
933 if (pkey->ameth == NULL)
934 return pkey->cache.size;
935 else if (pkey->ameth->pkey_size != NULL)
936 return pkey->ameth->pkey_size(pkey);
937 }
938 return 0;
939}
940
941void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
942 EVP_KEYMGMT **keymgmt,
943 const char *propquery)
944{
945 EVP_KEYMGMT *allocated_keymgmt = NULL;
946 EVP_KEYMGMT *tmp_keymgmt = NULL;
947 void *keydata = NULL;
948
949 if (pk == NULL)
950 return NULL;
951
952#ifndef FIPS_MODE
953 if (pk->pkey.ptr != NULL) {
954 /*
955 * If the legacy key doesn't have an dirty counter or export function,
956 * give up
957 */
958 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
959 return NULL;
960 }
961#endif
962
963 if (keymgmt != NULL) {
964 tmp_keymgmt = *keymgmt;
965 *keymgmt = NULL;
966 }
967
968 /* If no keymgmt was given or found, get a default keymgmt */
969 if (tmp_keymgmt == NULL) {
970 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
971
972 if (ctx != NULL && ctx->keytype != NULL)
973 tmp_keymgmt = allocated_keymgmt =
974 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
975 EVP_PKEY_CTX_free(ctx);
976 }
977
978 /* If there's still no keymgmt to be had, give up */
979 if (tmp_keymgmt == NULL)
980 goto end;
981
982#ifndef FIPS_MODE
983 if (pk->pkey.ptr != NULL) {
984 size_t i = 0;
985
986 /*
987 * If the legacy "origin" hasn't changed since last time, we try
988 * to find our keymgmt in the operation cache. If it has changed,
989 * |i| remains zero, and we will clear the cache further down.
990 */
991 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
992 i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
993
994 /*
995 * If |tmp_keymgmt| is present in the operation cache, it means
996 * that export doesn't need to be redone. In that case, we take
997 * token copies of the cached pointers, to have token success
998 * values to return.
999 */
1000 if (i < OSSL_NELEM(pk->operation_cache)
1001 && pk->operation_cache[i].keymgmt != NULL) {
1002 keydata = pk->operation_cache[i].keydata;
1003 goto end;
1004 }
1005 }
1006
1007 /*
1008 * TODO(3.0) Right now, we assume we have ample space. We will have
1009 * to think about a cache aging scheme, though, if |i| indexes outside
1010 * the array.
1011 */
1012 if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
1013 goto end;
1014
1015 /* Make sure that the keymgmt key type matches the legacy NID */
1016 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1017 goto end;
1018
1019 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1020 goto end;
1021
1022 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) {
1023 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1024 keydata = NULL;
1025 goto end;
1026 }
1027
1028 /*
1029 * If the dirty counter changed since last time, then clear the
1030 * operation cache. In that case, we know that |i| is zero. Just
1031 * in case this is a re-export, we increment then decrement the
1032 * keymgmt reference counter.
1033 */
1034 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
1035 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1036 keydata = NULL;
1037 goto end;
1038 }
1039 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
1040 evp_keymgmt_util_clear_operation_cache(pk);
1041 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
1042
1043 /* Add the new export to the operation cache */
1044 if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
1045 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1046 keydata = NULL;
1047 goto end;
1048 }
1049
1050 /* Synchronize the dirty count */
1051 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
1052 goto end;
1053 }
1054#endif /* FIPS_MODE */
1055
1056 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
1057
1058 end:
1059 /*
1060 * If nothing was exported, |tmp_keymgmt| might point at a freed
1061 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1062 * the caller either way in that case.
1063 */
1064 if (keydata == NULL)
1065 tmp_keymgmt = NULL;
1066
1067 if (keymgmt != NULL)
1068 *keymgmt = tmp_keymgmt;
1069
1070 EVP_KEYMGMT_free(allocated_keymgmt);
1071 return keydata;
1072}
1073
1074#ifndef FIPS_MODE
1075/*
1076 * This differs from exporting in that it releases the legacy key and assigns
1077 * the export keymgmt and keydata to the "origin" provider side key instead
1078 * of the operation cache.
1079 */
1080void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx,
1081 EVP_KEYMGMT **keymgmt,
1082 const char *propquery)
1083{
1084 EVP_KEYMGMT *allocated_keymgmt = NULL;
1085 EVP_KEYMGMT *tmp_keymgmt = NULL;
1086 void *keydata = NULL;
1087
1088 if (pk == NULL)
1089 return NULL;
1090
1091 /*
1092 * If this key is already "upgraded", this function shouldn't have been
1093 * called.
1094 */
1095 if (!ossl_assert(pk->keymgmt == NULL))
1096 return NULL;
1097
1098 if (keymgmt != NULL) {
1099 tmp_keymgmt = *keymgmt;
1100 *keymgmt = NULL;
1101 }
1102
1103 /* If the key isn't a legacy one, bail out, but with proper values */
1104 if (pk->pkey.ptr == NULL) {
1105 tmp_keymgmt = pk->keymgmt;
1106 keydata = pk->keydata;
1107 } else {
1108 /* If the legacy key doesn't have an export function, give up */
1109 if (pk->ameth->export_to == NULL)
1110 return NULL;
1111
1112 /* If no keymgmt was given, get a default keymgmt */
1113 if (tmp_keymgmt == NULL) {
1114 EVP_PKEY_CTX *ctx =
1115 EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1116
1117 if (ctx != NULL && ctx->keytype != NULL)
1118 tmp_keymgmt = allocated_keymgmt =
1119 EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery);
1120 EVP_PKEY_CTX_free(ctx);
1121 }
1122
1123 /* If we still don't have a keymgmt, give up */
1124 if (tmp_keymgmt == NULL)
1125 goto end;
1126
1127 /* Make sure that the keymgmt key type matches the legacy NID */
1128 if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
1129 goto end;
1130
1131 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1132 goto end;
1133
1134 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)
1135 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
1136 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1137 keydata = NULL;
1138 goto end;
1139 }
1140
1141 /*
1142 * Clear the operation cache, all the legacy data, as well as the
1143 * dirty counters
1144 */
1145 evp_pkey_free_legacy(pk);
1146 pk->dirty_cnt_copy = 0;
1147
1148 evp_keymgmt_util_clear_operation_cache(pk);
1149 pk->keymgmt = tmp_keymgmt;
1150 pk->keydata = keydata;
1151 evp_keymgmt_util_cache_keyinfo(pk);
1152 }
1153
1154 end:
1155 /*
1156 * If nothing was upgraded, |tmp_keymgmt| might point at a freed
1157 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
1158 * the caller either way in that case.
1159 */
1160 if (keydata == NULL)
1161 tmp_keymgmt = NULL;
1162
1163 if (keymgmt != NULL)
1164 *keymgmt = tmp_keymgmt;
1165
1166 EVP_KEYMGMT_free(allocated_keymgmt);
1167 return keydata;
1168}
1169#endif /* FIPS_MODE */