Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/evp/p_lib.c

raw
1 /*
2 * Copyright 1995-2025 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 <assert.h>
17 #include <stdio.h>
18 #include "internal/cryptlib.h"
19 #include "internal/refcount.h"
20 #include "internal/namemap.h"
21 #include <openssl/bn.h>
22 #include <openssl/err.h>
23 #include <openssl/objects.h>
24 #include <openssl/evp.h>
25 #include <openssl/rsa.h>
26 #include <openssl/dsa.h>
27 #include <openssl/dh.h>
28 #include <openssl/ec.h>
29 #include <openssl/cmac.h>
30 #ifndef FIPS_MODULE
31 #include <openssl/engine.h>
32 #endif
33 #include <openssl/params.h>
34 #include <openssl/param_build.h>
35 #include <openssl/encoder.h>
36 #include <openssl/core_names.h>
37
38 #include "internal/numbers.h" /* includes SIZE_MAX */
39 #include "internal/ffc.h"
40 #include "crypto/evp.h"
41 #include "crypto/dh.h"
42 #include "crypto/dsa.h"
43 #include "crypto/ec.h"
44 #include "crypto/ecx.h"
45 #include "crypto/rsa.h"
46 #ifndef FIPS_MODULE
47 #include "crypto/asn1.h"
48 #include "crypto/x509.h"
49 #endif
50 #include "internal/provider.h"
51 #include "evp_local.h"
52
53 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
54 int len, EVP_KEYMGMT *keymgmt);
55 static void evp_pkey_free_it(EVP_PKEY *key);
56
57 /* The type of parameters selected in key parameter functions */
58 #define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
59
60 #ifndef FIPS_MODULE
61 int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
62 {
63 int size = 0;
64
65 if (pkey != NULL) {
66 size = pkey->cache.bits;
67 if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
68 size = pkey->ameth->pkey_bits(pkey);
69 }
70 if (size <= 0) {
71 ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS);
72 return 0;
73 }
74 return size;
75 }
76
77 int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
78 {
79 int size = 0;
80
81 if (pkey != NULL) {
82 size = pkey->cache.security_bits;
83 if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
84 size = pkey->ameth->pkey_security_bits(pkey);
85 }
86 if (size <= 0) {
87 ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS);
88 return 0;
89 }
90 return size;
91 }
92
93 int EVP_PKEY_get_security_category(const EVP_PKEY *pkey)
94 {
95 return pkey != NULL ? pkey->cache.security_category : -1;
96 }
97
98 int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
99 {
100 #ifndef OPENSSL_NO_DSA
101 if (pkey->type == EVP_PKEY_DSA) {
102 int ret = pkey->save_parameters;
103
104 if (mode >= 0)
105 pkey->save_parameters = mode;
106 return ret;
107 }
108 #endif
109 #ifndef OPENSSL_NO_EC
110 if (pkey->type == EVP_PKEY_EC) {
111 int ret = pkey->save_parameters;
112
113 if (mode >= 0)
114 pkey->save_parameters = mode;
115 return ret;
116 }
117 #endif
118 return 0;
119 }
120
121 int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
122 {
123 return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
124 }
125
126 void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
127 {
128 return CRYPTO_get_ex_data(&key->ex_data, idx);
129 }
130 #endif /* !FIPS_MODULE */
131
132 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
133 {
134 /*
135 * Clean up legacy stuff from this function when legacy support is gone.
136 */
137
138 EVP_PKEY *downgraded_from = NULL;
139 int ok = 0;
140
141 #ifndef FIPS_MODULE
142 /*
143 * If |to| is a legacy key and |from| isn't, we must make a downgraded
144 * copy of |from|. If that fails, this function fails.
145 */
146 if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
147 if (!evp_pkey_copy_downgraded(&downgraded_from, from))
148 goto end;
149 from = downgraded_from;
150 }
151 #endif /* !FIPS_MODULE */
152
153 /*
154 * Make sure |to| is typed. Content is less important at this early
155 * stage.
156 *
157 * 1. If |to| is untyped, assign |from|'s key type to it.
158 * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
159 * (|from| was already downgraded above)
160 *
161 * If |to| is a provided key, there's nothing more to do here, functions
162 * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
163 * further down help us find out if they are the same or not.
164 */
165 if (evp_pkey_is_blank(to)) {
166 #ifndef FIPS_MODULE
167 if (evp_pkey_is_legacy(from)) {
168 if (EVP_PKEY_set_type(to, from->type) == 0)
169 goto end;
170 } else
171 #endif /* !FIPS_MODULE */
172 {
173 if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
174 goto end;
175 }
176 }
177 #ifndef FIPS_MODULE
178 else if (evp_pkey_is_legacy(to)) {
179 if (to->type != from->type) {
180 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
181 goto end;
182 }
183 }
184 #endif /* !FIPS_MODULE */
185
186 if (EVP_PKEY_missing_parameters(from)) {
187 ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
188 goto end;
189 }
190
191 if (!EVP_PKEY_missing_parameters(to)) {
192 if (EVP_PKEY_parameters_eq(to, from) == 1)
193 ok = 1;
194 else
195 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
196 goto end;
197 }
198
199 /* For purely provided keys, we just call the keymgmt utility */
200 if (to->keymgmt != NULL && from->keymgmt != NULL) {
201 ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
202 goto end;
203 }
204
205 #ifndef FIPS_MODULE
206 /*
207 * If |to| is provided, we know that |from| is legacy at this point.
208 * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
209 * to copy the appropriate data to |to|'s keydata.
210 * We cannot override existing data so do it only if there is no keydata
211 * in |to| yet.
212 */
213 if (to->keymgmt != NULL && to->keydata == NULL) {
214 EVP_KEYMGMT *to_keymgmt = to->keymgmt;
215 void *from_keydata = evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
216 NULL);
217
218 /*
219 * If we get a NULL, it could be an internal error, or it could be
220 * that there's a key mismatch. We're pretending the latter...
221 */
222 if (from_keydata == NULL)
223 ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
224 else
225 ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
226 from_keydata,
227 SELECT_PARAMETERS))
228 != NULL;
229 goto end;
230 }
231
232 /* Both keys are legacy */
233 if (from->ameth != NULL && from->ameth->param_copy != NULL)
234 ok = from->ameth->param_copy(to, from);
235 #endif /* !FIPS_MODULE */
236 end:
237 EVP_PKEY_free(downgraded_from);
238 return ok;
239 }
240
241 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
242 {
243 if (pkey != NULL) {
244 #ifdef FIPS_MODULE
245 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
246 #else
247 if (pkey->keymgmt != NULL)
248 return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
249 if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
250 return pkey->ameth->param_missing(pkey);
251 #endif /* FIPS_MODULE */
252 }
253 return 0;
254 }
255
256 /*
257 * This function is called for any mixture of keys except pure legacy pair.
258 * When legacy keys are gone, we replace a call to this functions with
259 * a call to evp_keymgmt_util_match().
260 */
261 static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
262 int selection)
263 {
264 #ifdef FIPS_MODULE
265 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
266 #else
267 EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
268 void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
269
270 /* If none of them are provided, this function shouldn't have been called */
271 if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
272 return -2;
273
274 /* For purely provided keys, we just call the keymgmt utility */
275 if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
276 return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
277
278 /*
279 * At this point, one of them is provided, the other not. This allows
280 * us to compare types using legacy NIDs.
281 */
282 if (evp_pkey_is_legacy(a)
283 && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
284 return -1; /* not the same key type */
285 if (evp_pkey_is_legacy(b)
286 && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
287 return -1; /* not the same key type */
288
289 /*
290 * We've determined that they both are the same keytype, so the next
291 * step is to do a bit of cross export to ensure we have keydata for
292 * both keys in the same keymgmt.
293 */
294 keymgmt1 = a->keymgmt;
295 keydata1 = a->keydata;
296 keymgmt2 = b->keymgmt;
297 keydata2 = b->keydata;
298
299 if (keymgmt2 != NULL && keymgmt2->match != NULL) {
300 tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
301 if (tmp_keydata != NULL) {
302 keymgmt1 = keymgmt2;
303 keydata1 = tmp_keydata;
304 }
305 }
306 if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
307 tmp_keydata = evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
308 if (tmp_keydata != NULL) {
309 keymgmt2 = keymgmt1;
310 keydata2 = tmp_keydata;
311 }
312 }
313
314 /* If we still don't have matching keymgmt implementations, we give up */
315 if (keymgmt1 != keymgmt2)
316 return -2;
317
318 /* If the keymgmt implementations are NULL, the export failed */
319 if (keymgmt1 == NULL)
320 return -2;
321
322 return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
323 #endif /* FIPS_MODULE */
324 }
325
326 #ifndef FIPS_MODULE
327 #ifndef OPENSSL_NO_DEPRECATED_3_0
328 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
329 {
330 return EVP_PKEY_parameters_eq(a, b);
331 }
332 #endif
333 #endif /* FIPS_MODULE */
334
335 int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
336 {
337 #ifdef FIPS_MODULE
338 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
339 #else
340 /*
341 * This will just call evp_keymgmt_util_match when legacy support
342 * is gone.
343 */
344
345 if (a->keymgmt != NULL || b->keymgmt != NULL)
346 return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
347
348 /* All legacy keys */
349 if (a->type != b->type)
350 return -1;
351 if (a->ameth != NULL && a->ameth->param_cmp != NULL)
352 return a->ameth->param_cmp(a, b);
353 return -2;
354 #endif /* !FIPS_MODULE */
355 }
356
357 #ifndef FIPS_MODULE
358 #ifndef OPENSSL_NO_DEPRECATED_3_0
359 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
360 {
361 return EVP_PKEY_eq(a, b);
362 }
363 #endif
364 #endif /* !FIPS_MODULE */
365
366 int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
367 {
368 /*
369 * This will just call evp_keymgmt_util_match when legacy support
370 * is gone.
371 */
372
373 /* Trivial shortcuts */
374 if (a == b)
375 return 1;
376 if (a == NULL || b == NULL)
377 return 0;
378
379 #ifndef FIPS_MODULE
380 if (a->keymgmt != NULL || b->keymgmt != NULL)
381 #endif /* !FIPS_MODULE */
382 {
383 int selection = SELECT_PARAMETERS;
384
385 if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
386 && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
387 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
388 else
389 selection |= OSSL_KEYMGMT_SELECT_KEYPAIR;
390 return evp_pkey_cmp_any(a, b, selection);
391 }
392
393 #ifndef FIPS_MODULE
394 /* All legacy keys */
395 if (a->type != b->type)
396 return -1;
397
398 if (a->ameth != NULL) {
399 int ret;
400 /* Compare parameters if the algorithm has them */
401 if (a->ameth->param_cmp != NULL) {
402 ret = a->ameth->param_cmp(a, b);
403 if (ret <= 0)
404 return ret;
405 }
406
407 if (a->ameth->pub_cmp != NULL)
408 return a->ameth->pub_cmp(a, b);
409 }
410
411 return -2;
412 #endif /* !FIPS_MODULE */
413 }
414
415 #ifndef FIPS_MODULE
416 static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
417 const char *strtype,
418 const char *propq,
419 int nidtype,
420 ENGINE *e,
421 const unsigned char *key,
422 size_t len,
423 int key_is_priv)
424 {
425 EVP_PKEY *pkey = NULL;
426 EVP_PKEY_CTX *ctx = NULL;
427 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
428 int result = 0;
429
430 #ifndef OPENSSL_NO_ENGINE
431 /* Check if there is an Engine for this type */
432 if (e == NULL) {
433 ENGINE *tmpe = NULL;
434
435 if (strtype != NULL)
436 ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
437 else if (nidtype != EVP_PKEY_NONE)
438 ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
439
440 /* If tmpe is NULL then no engine is claiming to support this type */
441 if (tmpe == NULL)
442 ameth = NULL;
443
444 ENGINE_finish(tmpe);
445 }
446 #endif
447
448 if (e == NULL && ameth == NULL) {
449 /*
450 * No engine is claiming to support this type, so lets see if we have
451 * a provider.
452 */
453 ctx = EVP_PKEY_CTX_new_from_name(libctx,
454 strtype != NULL ? strtype
455 : OBJ_nid2sn(nidtype),
456 propq);
457 if (ctx == NULL)
458 goto err;
459 /* May fail if no provider available */
460 ERR_set_mark();
461 if (EVP_PKEY_fromdata_init(ctx) == 1) {
462 OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
463
464 ERR_clear_last_mark();
465 params[0] = OSSL_PARAM_construct_octet_string(
466 key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
467 : OSSL_PKEY_PARAM_PUB_KEY,
468 (void *)key, len);
469
470 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
471 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
472 goto err;
473 }
474
475 EVP_PKEY_CTX_free(ctx);
476
477 return pkey;
478 }
479 ERR_pop_to_mark();
480 /* else not supported so fallback to legacy */
481 }
482
483 /* Legacy code path */
484
485 pkey = EVP_PKEY_new();
486 if (pkey == NULL) {
487 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
488 goto err;
489 }
490
491 if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
492 /* ERR_raise(ERR_LIB_EVP, ...) already called */
493 goto err;
494 }
495
496 if (!ossl_assert(pkey->ameth != NULL))
497 goto err;
498
499 if (key_is_priv) {
500 if (pkey->ameth->set_priv_key == NULL) {
501 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
502 goto err;
503 }
504
505 if (!pkey->ameth->set_priv_key(pkey, key, len)) {
506 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
507 goto err;
508 }
509 } else {
510 if (pkey->ameth->set_pub_key == NULL) {
511 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
512 goto err;
513 }
514
515 if (!pkey->ameth->set_pub_key(pkey, key, len)) {
516 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
517 goto err;
518 }
519 }
520
521 result = 1;
522 err:
523 if (!result) {
524 EVP_PKEY_free(pkey);
525 pkey = NULL;
526 }
527 EVP_PKEY_CTX_free(ctx);
528 return pkey;
529 }
530
531 EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
532 const char *keytype,
533 const char *propq,
534 const unsigned char *priv, size_t len)
535 {
536 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
537 len, 1);
538 }
539
540 EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
541 const unsigned char *priv,
542 size_t len)
543 {
544 return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
545 }
546
547 EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
548 const char *keytype, const char *propq,
549 const unsigned char *pub, size_t len)
550 {
551 return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
552 len, 0);
553 }
554
555 EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
556 const unsigned char *pub,
557 size_t len)
558 {
559 return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
560 }
561
562 struct raw_key_details_st {
563 unsigned char **key;
564 size_t *len;
565 int selection;
566 };
567
568 static OSSL_CALLBACK get_raw_key_details;
569 static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
570 {
571 const OSSL_PARAM *p = NULL;
572 struct raw_key_details_st *raw_key = arg;
573
574 if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
575 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
576 != NULL)
577 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
578 raw_key->key == NULL ? 0 : *raw_key->len,
579 raw_key->len);
580 } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
581 if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
582 != NULL)
583 return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
584 raw_key->key == NULL ? 0 : *raw_key->len,
585 raw_key->len);
586 }
587
588 return 0;
589 }
590
591 int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
592 size_t *len)
593 {
594 if (pkey->keymgmt != NULL) {
595 struct raw_key_details_st raw_key;
596
597 raw_key.key = priv == NULL ? NULL : &priv;
598 raw_key.len = len;
599 raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
600
601 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
602 get_raw_key_details, &raw_key);
603 }
604
605 if (pkey->ameth == NULL) {
606 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
607 return 0;
608 }
609
610 if (pkey->ameth->get_priv_key == NULL) {
611 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
612 return 0;
613 }
614
615 if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
616 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
617 return 0;
618 }
619
620 return 1;
621 }
622
623 int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
624 size_t *len)
625 {
626 if (pkey->keymgmt != NULL) {
627 struct raw_key_details_st raw_key;
628
629 raw_key.key = pub == NULL ? NULL : &pub;
630 raw_key.len = len;
631 raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
632
633 return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
634 get_raw_key_details, &raw_key);
635 }
636
637 if (pkey->ameth == NULL) {
638 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
639 return 0;
640 }
641
642 if (pkey->ameth->get_pub_key == NULL) {
643 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
644 return 0;
645 }
646
647 if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
648 ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
649 return 0;
650 }
651
652 return 1;
653 }
654
655 static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
656 const char *cipher_name,
657 const EVP_CIPHER *cipher,
658 OSSL_LIB_CTX *libctx,
659 const char *propq, ENGINE *e)
660 {
661 #ifndef OPENSSL_NO_CMAC
662 #ifndef OPENSSL_NO_ENGINE
663 const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
664 #endif
665 OSSL_PARAM params[5], *p = params;
666 EVP_PKEY *pkey = NULL;
667 EVP_PKEY_CTX *ctx;
668
669 if (cipher != NULL)
670 cipher_name = EVP_CIPHER_get0_name(cipher);
671
672 if (cipher_name == NULL) {
673 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
674 return NULL;
675 }
676
677 ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
678 if (ctx == NULL)
679 goto err;
680
681 if (EVP_PKEY_fromdata_init(ctx) <= 0) {
682 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
683 goto err;
684 }
685
686 *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
687 (void *)priv, len);
688 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
689 (char *)cipher_name, 0);
690 if (propq != NULL)
691 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
692 (char *)propq, 0);
693 #ifndef OPENSSL_NO_ENGINE
694 if (engine_id != NULL)
695 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
696 (char *)engine_id, 0);
697 #endif
698 *p = OSSL_PARAM_construct_end();
699
700 if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) {
701 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
702 goto err;
703 }
704
705 err:
706 EVP_PKEY_CTX_free(ctx);
707
708 return pkey;
709 #else
710 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
711 return NULL;
712 #endif
713 }
714
715 EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
716 size_t len, const EVP_CIPHER *cipher)
717 {
718 return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
719 }
720
721 int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
722 {
723 return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
724 }
725
726 int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
727 {
728 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
729 }
730
731 #ifndef OPENSSL_NO_ENGINE
732 int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
733 {
734 if (e != NULL) {
735 if (!ENGINE_init(e)) {
736 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
737 return 0;
738 }
739 if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
740 ENGINE_finish(e);
741 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
742 return 0;
743 }
744 }
745 ENGINE_finish(pkey->pmeth_engine);
746 pkey->pmeth_engine = e;
747 return 1;
748 }
749
750 ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
751 {
752 return pkey->engine;
753 }
754 #endif
755
756 #ifndef OPENSSL_NO_DEPRECATED_3_0
757 static void detect_foreign_key(EVP_PKEY *pkey)
758 {
759 switch (pkey->type) {
760 case EVP_PKEY_RSA:
761 case EVP_PKEY_RSA_PSS:
762 pkey->foreign = pkey->pkey.rsa != NULL
763 && ossl_rsa_is_foreign(pkey->pkey.rsa);
764 break;
765 #ifndef OPENSSL_NO_EC
766 case EVP_PKEY_SM2:
767 break;
768 case EVP_PKEY_EC:
769 pkey->foreign = pkey->pkey.ec != NULL
770 && ossl_ec_key_is_foreign(pkey->pkey.ec);
771 break;
772 #endif
773 #ifndef OPENSSL_NO_DSA
774 case EVP_PKEY_DSA:
775 pkey->foreign = pkey->pkey.dsa != NULL
776 && ossl_dsa_is_foreign(pkey->pkey.dsa);
777 break;
778 #endif
779 #ifndef OPENSSL_NO_DH
780 case EVP_PKEY_DH:
781 pkey->foreign = pkey->pkey.dh != NULL
782 && ossl_dh_is_foreign(pkey->pkey.dh);
783 break;
784 #endif
785 default:
786 pkey->foreign = 0;
787 break;
788 }
789 }
790
791 int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
792 {
793 #ifndef OPENSSL_NO_EC
794 int pktype;
795
796 pktype = EVP_PKEY_type(type);
797 if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
798 const EC_GROUP *group = EC_KEY_get0_group(key);
799
800 if (group != NULL) {
801 int curve = EC_GROUP_get_curve_name(group);
802
803 /*
804 * Regardless of what is requested the SM2 curve must be SM2 type,
805 * and non SM2 curves are EC type.
806 */
807 if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
808 type = EVP_PKEY_SM2;
809 else if (curve != NID_sm2 && pktype == EVP_PKEY_SM2)
810 type = EVP_PKEY_EC;
811 }
812 }
813 #endif
814
815 if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
816 return 0;
817
818 pkey->pkey.ptr = key;
819 detect_foreign_key(pkey);
820
821 return (key != NULL);
822 }
823 #endif
824
825 void *EVP_PKEY_get0(const EVP_PKEY *pkey)
826 {
827 if (pkey == NULL)
828 return NULL;
829
830 if (!evp_pkey_is_provided(pkey))
831 return pkey->pkey.ptr;
832
833 return NULL;
834 }
835
836 const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
837 {
838 const ASN1_OCTET_STRING *os = NULL;
839 if (pkey->type != EVP_PKEY_HMAC) {
840 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
841 return NULL;
842 }
843 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
844 if (os != NULL) {
845 *len = os->length;
846 return os->data;
847 }
848 return NULL;
849 }
850
851 #ifndef OPENSSL_NO_POLY1305
852 const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
853 {
854 const ASN1_OCTET_STRING *os = NULL;
855 if (pkey->type != EVP_PKEY_POLY1305) {
856 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
857 return NULL;
858 }
859 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
860 if (os != NULL) {
861 *len = os->length;
862 return os->data;
863 }
864 return NULL;
865 }
866 #endif
867
868 #ifndef OPENSSL_NO_SIPHASH
869 const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
870 {
871 const ASN1_OCTET_STRING *os = NULL;
872
873 if (pkey->type != EVP_PKEY_SIPHASH) {
874 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
875 return NULL;
876 }
877 os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
878 if (os != NULL) {
879 *len = os->length;
880 return os->data;
881 }
882 return NULL;
883 }
884 #endif
885
886 #ifndef OPENSSL_NO_DSA
887 static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
888 {
889 if (pkey->type != EVP_PKEY_DSA) {
890 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
891 return NULL;
892 }
893 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
894 }
895
896 const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
897 {
898 return evp_pkey_get0_DSA_int(pkey);
899 }
900
901 int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
902 {
903 int ret;
904
905 if (!DSA_up_ref(key))
906 return 0;
907
908 ret = EVP_PKEY_assign_DSA(pkey, key);
909
910 if (!ret)
911 DSA_free(key);
912
913 return ret;
914 }
915 DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
916 {
917 DSA *ret = evp_pkey_get0_DSA_int(pkey);
918
919 if (ret != NULL && !DSA_up_ref(ret))
920 return NULL;
921
922 return ret;
923 }
924 #endif /* OPENSSL_NO_DSA */
925
926 #ifndef OPENSSL_NO_ECX
927 static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
928 {
929 if (EVP_PKEY_get_base_id(pkey) != type) {
930 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
931 return NULL;
932 }
933 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
934 }
935
936 static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
937 {
938 ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
939
940 if (ret != NULL && !ossl_ecx_key_up_ref(ret))
941 ret = NULL;
942 return ret;
943 }
944
945 #define IMPLEMENT_ECX_VARIANT(NAME) \
946 ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
947 { \
948 return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
949 }
950 IMPLEMENT_ECX_VARIANT(X25519)
951 IMPLEMENT_ECX_VARIANT(X448)
952 IMPLEMENT_ECX_VARIANT(ED25519)
953 IMPLEMENT_ECX_VARIANT(ED448)
954
955 #endif /* OPENSSL_NO_ECX */
956
957 #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
958
959 int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
960 {
961 int ret, type;
962
963 /*
964 * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
965 * related to ffdhe and modp (which cache q = (p - 1) / 2),
966 * and returns 0 for all other dh parameter generation types including
967 * RFC5114 named groups.
968 *
969 * The EVP_PKEY_DH type is used for dh parameter generation types:
970 * - named safe prime groups related to ffdhe and modp
971 * - safe prime generator
972 *
973 * The type EVP_PKEY_DHX is used for dh parameter generation types
974 * - fips186-4 and fips186-2
975 * - rfc5114 named groups.
976 *
977 * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
978 * without a q value.
979 * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
980 * q value to be stored.
981 */
982 if (ossl_dh_is_named_safe_prime_group(dhkey))
983 type = EVP_PKEY_DH;
984 else
985 type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
986
987 if (!DH_up_ref(dhkey))
988 return 0;
989
990 ret = EVP_PKEY_assign(pkey, type, dhkey);
991
992 if (!ret)
993 DH_free(dhkey);
994
995 return ret;
996 }
997
998 DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
999 {
1000 if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
1001 ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
1002 return NULL;
1003 }
1004 return evp_pkey_get_legacy((EVP_PKEY *)pkey);
1005 }
1006
1007 const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
1008 {
1009 return evp_pkey_get0_DH_int(pkey);
1010 }
1011
1012 DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
1013 {
1014 DH *ret = evp_pkey_get0_DH_int(pkey);
1015
1016 if (ret != NULL && !DH_up_ref(ret))
1017 ret = NULL;
1018
1019 return ret;
1020 }
1021 #endif
1022
1023 int EVP_PKEY_get_id(const EVP_PKEY *pkey)
1024 {
1025 return pkey->type;
1026 }
1027
1028 int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
1029 {
1030 return EVP_PKEY_type(pkey->type);
1031 }
1032
1033 /*
1034 * These hard coded cases are pure hackery to get around the fact
1035 * that names in crypto/objects/objects.txt are a mess. There is
1036 * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
1037 * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
1038 * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
1039 * "DSA" is accurate... but still, better be safe and hard-code
1040 * names that we know.
1041 * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
1042 * EVP_PKEY_EC, because of aliasing.
1043 * This should be cleaned away along with all other #legacy support.
1044 */
1045 static const OSSL_ITEM standard_name2type[] = {
1046 { EVP_PKEY_RSA, "RSA" },
1047 { EVP_PKEY_RSA_PSS, "RSA-PSS" },
1048 { EVP_PKEY_EC, "EC" },
1049 { EVP_PKEY_ED25519, "ED25519" },
1050 { EVP_PKEY_ED448, "ED448" },
1051 { EVP_PKEY_X25519, "X25519" },
1052 { EVP_PKEY_X448, "X448" },
1053 { EVP_PKEY_SM2, "SM2" },
1054 { EVP_PKEY_DH, "DH" },
1055 { EVP_PKEY_DHX, "X9.42 DH" },
1056 { EVP_PKEY_DHX, "DHX" },
1057 { EVP_PKEY_DSA, "DSA" },
1058 };
1059
1060 int evp_pkey_name2type(const char *name)
1061 {
1062 int type;
1063 size_t i;
1064
1065 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1066 if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
1067 return (int)standard_name2type[i].id;
1068 }
1069
1070 if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
1071 return type;
1072 return EVP_PKEY_type(OBJ_ln2nid(name));
1073 }
1074
1075 const char *evp_pkey_type2name(int type)
1076 {
1077 size_t i;
1078
1079 for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
1080 if (type == (int)standard_name2type[i].id)
1081 return standard_name2type[i].ptr;
1082 }
1083
1084 return OBJ_nid2sn(type);
1085 }
1086
1087 int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
1088 {
1089 if (pkey == NULL)
1090 return 0;
1091 if (pkey->keymgmt == NULL)
1092 return pkey->type == evp_pkey_name2type(name);
1093 return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
1094 }
1095
1096 int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
1097 void (*fn)(const char *name, void *data),
1098 void *data)
1099 {
1100 if (!evp_pkey_is_typed(pkey))
1101 return 0;
1102
1103 if (!evp_pkey_is_provided(pkey)) {
1104 const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
1105
1106 fn(name, data);
1107 return 1;
1108 }
1109 return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
1110 }
1111
1112 int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
1113 {
1114 if (pkey->keymgmt == NULL) {
1115 switch (EVP_PKEY_get_base_id(pkey)) {
1116 case EVP_PKEY_RSA:
1117 case EVP_PKEY_RSA_PSS:
1118 return 1;
1119 #ifndef OPENSSL_NO_DSA
1120 case EVP_PKEY_DSA:
1121 return 1;
1122 #endif
1123 #ifndef OPENSSL_NO_EC
1124 case EVP_PKEY_ED25519:
1125 case EVP_PKEY_ED448:
1126 return 1;
1127 case EVP_PKEY_EC: /* Including SM2 */
1128 return EC_KEY_can_sign(pkey->pkey.ec);
1129 #endif
1130 default:
1131 break;
1132 }
1133 } else {
1134 const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
1135 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
1136 EVP_SIGNATURE *sig;
1137 const char *name;
1138
1139 name = evp_keymgmt_util_query_operation_name(pkey->keymgmt,
1140 OSSL_OP_SIGNATURE);
1141 sig = EVP_SIGNATURE_fetch(libctx, name, NULL);
1142 if (sig != NULL) {
1143 EVP_SIGNATURE_free(sig);
1144 return 1;
1145 }
1146 }
1147 return 0;
1148 }
1149
1150 static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
1151 {
1152 BIO_set_indent(*out, saved_indent);
1153 if (pop_f_prefix) {
1154 BIO *next = BIO_pop(*out);
1155
1156 BIO_free(*out);
1157 *out = next;
1158 }
1159 return 1;
1160 }
1161
1162 static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
1163 long indent)
1164 {
1165 *pop_f_prefix = 0;
1166 *saved_indent = 0;
1167 if (indent > 0) {
1168 long i = BIO_get_indent(*out);
1169
1170 *saved_indent = (i < 0 ? 0 : i);
1171 if (BIO_set_indent(*out, indent) <= 0) {
1172 BIO *prefbio = BIO_new(BIO_f_prefix());
1173
1174 if (prefbio == NULL)
1175 return 0;
1176 *out = BIO_push(prefbio, *out);
1177 *pop_f_prefix = 1;
1178 }
1179 if (BIO_set_indent(*out, indent) <= 0) {
1180 print_reset_indent(out, *pop_f_prefix, *saved_indent);
1181 return 0;
1182 }
1183 }
1184 return 1;
1185 }
1186
1187 static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
1188 const char *kstr)
1189 {
1190 return BIO_indent(out, indent, 128)
1191 && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
1192 kstr, OBJ_nid2ln(pkey->type))
1193 > 0;
1194 }
1195
1196 static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
1197 int selection /* For provided encoding */,
1198 const char *propquery /* For provided encoding */,
1199 int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
1200 int indent, ASN1_PCTX *pctx),
1201 ASN1_PCTX *legacy_pctx /* For legacy print */)
1202 {
1203 int pop_f_prefix;
1204 long saved_indent;
1205 OSSL_ENCODER_CTX *ctx = NULL;
1206 int ret = -2; /* default to unsupported */
1207
1208 if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
1209 return 0;
1210
1211 ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
1212 propquery);
1213 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
1214 ret = OSSL_ENCODER_to_bio(ctx, out);
1215 OSSL_ENCODER_CTX_free(ctx);
1216
1217 if (ret != -2)
1218 goto end;
1219
1220 /* legacy fallback */
1221 if (legacy_print != NULL)
1222 ret = legacy_print(out, pkey, 0, legacy_pctx);
1223 else
1224 ret = unsup_alg(out, pkey, 0, "Public Key");
1225
1226 end:
1227 print_reset_indent(&out, pop_f_prefix, saved_indent);
1228 return ret;
1229 }
1230
1231 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
1232 int indent, ASN1_PCTX *pctx)
1233 {
1234 return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
1235 (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
1236 pctx);
1237 }
1238
1239 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
1240 int indent, ASN1_PCTX *pctx)
1241 {
1242 return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
1243 (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
1244 pctx);
1245 }
1246
1247 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
1248 int indent, ASN1_PCTX *pctx)
1249 {
1250 return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
1251 (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
1252 pctx);
1253 }
1254
1255 #ifndef OPENSSL_NO_STDIO
1256 int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
1257 int indent, ASN1_PCTX *pctx)
1258 {
1259 int ret;
1260 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1261
1262 if (b == NULL)
1263 return 0;
1264 ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
1265 BIO_free(b);
1266 return ret;
1267 }
1268
1269 int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
1270 int indent, ASN1_PCTX *pctx)
1271 {
1272 int ret;
1273 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1274
1275 if (b == NULL)
1276 return 0;
1277 ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
1278 BIO_free(b);
1279 return ret;
1280 }
1281
1282 int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
1283 int indent, ASN1_PCTX *pctx)
1284 {
1285 int ret;
1286 BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
1287
1288 if (b == NULL)
1289 return 0;
1290 ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
1291 BIO_free(b);
1292 return ret;
1293 }
1294 #endif
1295
1296 static void mdname2nid(const char *mdname, void *data)
1297 {
1298 int *nid = (int *)data;
1299
1300 if (*nid != NID_undef)
1301 return;
1302
1303 *nid = OBJ_sn2nid(mdname);
1304 if (*nid == NID_undef)
1305 *nid = OBJ_ln2nid(mdname);
1306 }
1307
1308 static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
1309 int arg1, void *arg2)
1310 {
1311 if (pkey->keymgmt == NULL)
1312 return 0;
1313 switch (op) {
1314 case ASN1_PKEY_CTRL_DEFAULT_MD_NID: {
1315 char mdname[80] = "";
1316 int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
1317 sizeof(mdname));
1318
1319 if (rv > 0) {
1320 int mdnum;
1321 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
1322 /* Make sure the MD is in the namemap if available */
1323 EVP_MD *md;
1324 OSSL_NAMEMAP *namemap;
1325 int nid = NID_undef;
1326
1327 (void)ERR_set_mark();
1328 md = EVP_MD_fetch(libctx, mdname, NULL);
1329 (void)ERR_pop_to_mark();
1330 namemap = ossl_namemap_stored(libctx);
1331
1332 /*
1333 * The only reason to fetch the MD was to make sure it is in the
1334 * namemap. We can immediately free it.
1335 */
1336 EVP_MD_free(md);
1337 mdnum = ossl_namemap_name2num(namemap, mdname);
1338 if (mdnum == 0)
1339 return 0;
1340
1341 /*
1342 * We have the namemap number - now we need to find the
1343 * associated nid
1344 */
1345 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
1346 return 0;
1347 *(int *)arg2 = nid;
1348 }
1349 return rv;
1350 }
1351 default:
1352 return -2;
1353 }
1354 }
1355
1356 static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
1357 {
1358 if (pkey->ameth == NULL)
1359 return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
1360 if (pkey->ameth->pkey_ctrl == NULL)
1361 return -2;
1362 return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
1363 }
1364
1365 int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
1366 {
1367 if (pkey == NULL)
1368 return 0;
1369 return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
1370 }
1371
1372 int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
1373 char *mdname, size_t mdname_sz)
1374 {
1375 if (pkey->ameth == NULL)
1376 return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
1377 pkey->keydata,
1378 mdname, mdname_sz);
1379
1380 {
1381 int nid = NID_undef;
1382 int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
1383 const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
1384
1385 if (rv > 0)
1386 OPENSSL_strlcpy(mdname, name, mdname_sz);
1387 return rv;
1388 }
1389 }
1390
1391 int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
1392 size_t *gname_len)
1393 {
1394 return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
1395 gname, gname_sz, gname_len);
1396 }
1397
1398 int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
1399 const char *name, const char *propq)
1400 {
1401 int rv;
1402 EVP_MD_CTX *ctx = NULL;
1403
1404 if ((ctx = EVP_MD_CTX_new()) == NULL)
1405 return -1;
1406
1407 ERR_set_mark();
1408 rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
1409 propq, pkey, NULL);
1410 ERR_pop_to_mark();
1411
1412 EVP_MD_CTX_free(ctx);
1413 return rv;
1414 }
1415 #endif /* !FIPS_MODULE */
1416
1417 int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
1418 size_t publen)
1419 {
1420 if (pkey == NULL)
1421 return 0;
1422 #ifndef FIPS_MODULE
1423 if (evp_pkey_is_provided(pkey))
1424 #endif /* !FIPS_MODULE */
1425 return EVP_PKEY_set_octet_string_param(pkey,
1426 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1427 (unsigned char *)pub, publen);
1428
1429 #ifndef FIPS_MODULE
1430 if (publen > INT_MAX)
1431 return 0;
1432 /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
1433 if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, (int)publen,
1434 (void *)pub)
1435 <= 0)
1436 return 0;
1437 return 1;
1438 #endif /* !FIPS_MODULE */
1439 }
1440
1441 size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
1442 {
1443 if (pkey == NULL)
1444 return 0;
1445 #ifndef FIPS_MODULE
1446 if (evp_pkey_is_provided(pkey))
1447 #endif
1448 {
1449 size_t return_size = OSSL_PARAM_UNMODIFIED;
1450 unsigned char *buf;
1451
1452 /*
1453 * We know that this is going to fail, but it will give us a size
1454 * to allocate.
1455 */
1456 EVP_PKEY_get_octet_string_param(pkey,
1457 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1458 NULL, 0, &return_size);
1459 if (return_size == OSSL_PARAM_UNMODIFIED)
1460 return 0;
1461
1462 *ppub = NULL;
1463 buf = OPENSSL_malloc(return_size);
1464 if (buf == NULL)
1465 return 0;
1466
1467 if (!EVP_PKEY_get_octet_string_param(pkey,
1468 OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
1469 buf, return_size, NULL)) {
1470 OPENSSL_free(buf);
1471 return 0;
1472 }
1473 *ppub = buf;
1474 return return_size;
1475 }
1476
1477 #ifndef FIPS_MODULE
1478 {
1479 int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
1480 if (rv <= 0)
1481 return 0;
1482 return rv;
1483 }
1484 #endif /* !FIPS_MODULE */
1485 }
1486
1487 /*- All methods below can also be used in FIPS_MODULE */
1488
1489 EVP_PKEY *EVP_PKEY_new(void)
1490 {
1491 EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
1492
1493 if (ret == NULL)
1494 return NULL;
1495
1496 ret->type = EVP_PKEY_NONE;
1497 ret->save_type = EVP_PKEY_NONE;
1498
1499 if (!CRYPTO_NEW_REF(&ret->references, 1))
1500 goto err;
1501
1502 ret->lock = CRYPTO_THREAD_lock_new();
1503 if (ret->lock == NULL) {
1504 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1505 goto err;
1506 }
1507
1508 #ifndef FIPS_MODULE
1509 ret->save_parameters = 1;
1510 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
1511 ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
1512 goto err;
1513 }
1514 #endif
1515 return ret;
1516
1517 err:
1518 CRYPTO_FREE_REF(&ret->references);
1519 CRYPTO_THREAD_lock_free(ret->lock);
1520 OPENSSL_free(ret);
1521 return NULL;
1522 }
1523
1524 /*
1525 * Setup a public key management method.
1526 *
1527 * For legacy keys, either |type| or |str| is expected to have the type
1528 * information. In this case, the setup consists of finding an ASN1 method
1529 * and potentially an ENGINE, and setting those fields in |pkey|.
1530 *
1531 * For provider side keys, |keymgmt| is expected to be non-NULL. In this
1532 * case, the setup consists of setting the |keymgmt| field in |pkey|.
1533 *
1534 * If pkey is NULL just return 1 or 0 if the key management method exists.
1535 */
1536
1537 static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
1538 int len, EVP_KEYMGMT *keymgmt)
1539 {
1540 #ifndef FIPS_MODULE
1541 const EVP_PKEY_ASN1_METHOD *ameth = NULL;
1542 ENGINE **eptr = (e == NULL) ? &e : NULL;
1543 #endif
1544
1545 /*
1546 * The setups can't set both legacy and provider side methods.
1547 * It is forbidden
1548 */
1549 if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
1550 || !ossl_assert(e == NULL || keymgmt == NULL)) {
1551 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1552 return 0;
1553 }
1554
1555 if (pkey != NULL) {
1556 int free_it = 0;
1557
1558 #ifndef FIPS_MODULE
1559 free_it = free_it || pkey->pkey.ptr != NULL;
1560 #endif
1561 free_it = free_it || pkey->keydata != NULL;
1562 if (free_it)
1563 evp_pkey_free_it(pkey);
1564 #ifndef FIPS_MODULE
1565 /*
1566 * If key type matches and a method exists then this lookup has
1567 * succeeded once so just indicate success.
1568 */
1569 if (pkey->type != EVP_PKEY_NONE
1570 && type == pkey->save_type
1571 && pkey->ameth != NULL)
1572 return 1;
1573 #ifndef OPENSSL_NO_ENGINE
1574 /* If we have ENGINEs release them */
1575 ENGINE_finish(pkey->engine);
1576 pkey->engine = NULL;
1577 ENGINE_finish(pkey->pmeth_engine);
1578 pkey->pmeth_engine = NULL;
1579 #endif
1580 #endif
1581 }
1582 #ifndef FIPS_MODULE
1583 if (str != NULL)
1584 ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
1585 else if (type != EVP_PKEY_NONE)
1586 ameth = EVP_PKEY_asn1_find(eptr, type);
1587 #ifndef OPENSSL_NO_ENGINE
1588 if (pkey == NULL && eptr != NULL)
1589 ENGINE_finish(e);
1590 #endif
1591 #endif
1592
1593 {
1594 int check = 1;
1595
1596 #ifndef FIPS_MODULE
1597 check = check && ameth == NULL;
1598 #endif
1599 check = check && keymgmt == NULL;
1600 if (check) {
1601 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
1602 return 0;
1603 }
1604 }
1605 if (pkey != NULL) {
1606 if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
1607 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1608 return 0;
1609 }
1610
1611 pkey->keymgmt = keymgmt;
1612
1613 pkey->save_type = type;
1614 pkey->type = type;
1615
1616 #ifndef FIPS_MODULE
1617 /*
1618 * If the internal "origin" key is provider side, don't save |ameth|.
1619 * The main reason is that |ameth| is one factor to detect that the
1620 * internal "origin" key is a legacy one.
1621 */
1622 if (keymgmt == NULL)
1623 pkey->ameth = ameth;
1624
1625 /*
1626 * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
1627 * for any key type that has a legacy implementation, regardless of
1628 * if the internal key is a legacy or a provider side one. When
1629 * there is no legacy implementation for the key, the type becomes
1630 * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
1631 * with functions that expect legacy internal keys.
1632 */
1633 if (ameth != NULL) {
1634 if (type == EVP_PKEY_NONE)
1635 pkey->type = ameth->pkey_id;
1636 } else {
1637 pkey->type = EVP_PKEY_KEYMGMT;
1638 }
1639 #ifndef OPENSSL_NO_ENGINE
1640 if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
1641 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1642 return 0;
1643 }
1644 #endif
1645 pkey->engine = e;
1646 #endif
1647 }
1648 return 1;
1649 }
1650
1651 #ifndef FIPS_MODULE
1652 static void find_ameth(const char *name, void *data)
1653 {
1654 const char **str = data;
1655
1656 /*
1657 * The error messages from pkey_set_type() are uninteresting here,
1658 * and misleading.
1659 */
1660 ERR_set_mark();
1661
1662 if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, (int)strlen(name),
1663 NULL)) {
1664 if (str[0] == NULL)
1665 str[0] = name;
1666 else if (str[1] == NULL)
1667 str[1] = name;
1668 }
1669
1670 ERR_pop_to_mark();
1671 }
1672 #endif
1673
1674 int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
1675 {
1676 #ifndef FIPS_MODULE
1677 #define EVP_PKEY_TYPE_STR str[0]
1678 #define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
1679 /*
1680 * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
1681 * Ideally, only one should be found. If two (or more) are found, the
1682 * match is ambiguous. This should never happen, but...
1683 */
1684 const char *str[2] = { NULL, NULL };
1685
1686 if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
1687 || str[1] != NULL) {
1688 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1689 return 0;
1690 }
1691 #else
1692 #define EVP_PKEY_TYPE_STR NULL
1693 #define EVP_PKEY_TYPE_STRLEN -1
1694 #endif
1695 return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
1696 EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
1697 keymgmt);
1698
1699 #undef EVP_PKEY_TYPE_STR
1700 #undef EVP_PKEY_TYPE_STRLEN
1701 }
1702
1703 int EVP_PKEY_up_ref(EVP_PKEY *pkey)
1704 {
1705 int i;
1706
1707 if (CRYPTO_UP_REF(&pkey->references, &i) <= 0)
1708 return 0;
1709
1710 REF_PRINT_COUNT("EVP_PKEY", i, pkey);
1711 REF_ASSERT_ISNT(i < 2);
1712 return ((i > 1) ? 1 : 0);
1713 }
1714
1715 EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
1716 {
1717 EVP_PKEY *dup_pk;
1718
1719 if (pkey == NULL) {
1720 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
1721 return NULL;
1722 }
1723
1724 if ((dup_pk = EVP_PKEY_new()) == NULL)
1725 return NULL;
1726
1727 if (evp_pkey_is_blank(pkey))
1728 goto done;
1729
1730 #ifndef FIPS_MODULE
1731 if (evp_pkey_is_provided(pkey))
1732 #endif /* !FIPS_MODULE */
1733 {
1734 if (!evp_keymgmt_util_copy(dup_pk, pkey,
1735 OSSL_KEYMGMT_SELECT_ALL))
1736 goto err;
1737 goto done;
1738 }
1739
1740 #ifndef FIPS_MODULE
1741 if (evp_pkey_is_legacy(pkey)) {
1742 const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
1743
1744 if (ameth == NULL || ameth->copy == NULL) {
1745 if (pkey->pkey.ptr == NULL /* empty key, just set type */
1746 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
1747 goto done;
1748 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
1749 goto err;
1750 }
1751 if (!ameth->copy(dup_pk, pkey))
1752 goto err;
1753 goto done;
1754 }
1755 #endif /* !FIPS_MODULE */
1756
1757 goto err;
1758 done:
1759 #ifndef FIPS_MODULE
1760 /* copy auxiliary data */
1761 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
1762 &dup_pk->ex_data, &pkey->ex_data))
1763 goto err;
1764
1765 if (pkey->attributes != NULL) {
1766 if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
1767 goto err;
1768 }
1769 #endif /* !FIPS_MODULE */
1770 return dup_pk;
1771 err:
1772 EVP_PKEY_free(dup_pk);
1773 return NULL;
1774 }
1775
1776 #ifndef FIPS_MODULE
1777 void evp_pkey_free_legacy(EVP_PKEY *x)
1778 {
1779 const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
1780 ENGINE *tmpe = NULL;
1781
1782 if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
1783 ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
1784
1785 if (ameth != NULL) {
1786 if (x->legacy_cache_pkey.ptr != NULL) {
1787 /*
1788 * We should never have both a legacy origin key, and a key in the
1789 * legacy cache.
1790 */
1791 assert(x->pkey.ptr == NULL);
1792 /*
1793 * For the purposes of freeing we make the legacy cache look like
1794 * a legacy origin key.
1795 */
1796 x->pkey = x->legacy_cache_pkey;
1797 x->legacy_cache_pkey.ptr = NULL;
1798 }
1799 if (ameth->pkey_free != NULL)
1800 ameth->pkey_free(x);
1801 x->pkey.ptr = NULL;
1802 }
1803 #ifndef OPENSSL_NO_ENGINE
1804 ENGINE_finish(tmpe);
1805 ENGINE_finish(x->engine);
1806 x->engine = NULL;
1807 ENGINE_finish(x->pmeth_engine);
1808 x->pmeth_engine = NULL;
1809 #endif
1810 }
1811 #endif /* FIPS_MODULE */
1812
1813 static void evp_pkey_free_it(EVP_PKEY *x)
1814 {
1815 /* internal function; x is never NULL */
1816 evp_keymgmt_util_clear_operation_cache(x);
1817 #ifndef FIPS_MODULE
1818 evp_pkey_free_legacy(x);
1819 #endif
1820
1821 if (x->keymgmt != NULL) {
1822 evp_keymgmt_freedata(x->keymgmt, x->keydata);
1823 EVP_KEYMGMT_free(x->keymgmt);
1824 x->keymgmt = NULL;
1825 x->keydata = NULL;
1826 }
1827 x->type = EVP_PKEY_NONE;
1828 }
1829
1830 void EVP_PKEY_free(EVP_PKEY *x)
1831 {
1832 int i;
1833
1834 if (x == NULL)
1835 return;
1836
1837 CRYPTO_DOWN_REF(&x->references, &i);
1838 REF_PRINT_COUNT("EVP_PKEY", i, x);
1839 if (i > 0)
1840 return;
1841 REF_ASSERT_ISNT(i < 0);
1842 evp_pkey_free_it(x);
1843 #ifndef FIPS_MODULE
1844 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
1845 #endif
1846 CRYPTO_THREAD_lock_free(x->lock);
1847 CRYPTO_FREE_REF(&x->references);
1848 #ifndef FIPS_MODULE
1849 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
1850 #endif
1851 OPENSSL_free(x);
1852 }
1853
1854 int EVP_PKEY_get_size(const EVP_PKEY *pkey)
1855 {
1856 int size = 0;
1857
1858 if (pkey != NULL) {
1859 size = pkey->cache.size;
1860 #ifndef FIPS_MODULE
1861 if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
1862 size = pkey->ameth->pkey_size(pkey);
1863 #endif
1864 }
1865 if (size <= 0) {
1866 ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
1867 return 0;
1868 }
1869 return size;
1870 }
1871
1872 const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
1873 {
1874 if (!evp_pkey_is_assigned(pkey))
1875 return NULL;
1876
1877 if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
1878 return pkey->keymgmt->description;
1879 #ifndef FIPS_MODULE
1880 if (pkey->ameth != NULL)
1881 return pkey->ameth->info;
1882 #endif
1883 return NULL;
1884 }
1885
1886 void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
1887 EVP_KEYMGMT **keymgmt,
1888 const char *propquery)
1889 {
1890 EVP_KEYMGMT *allocated_keymgmt = NULL;
1891 EVP_KEYMGMT *tmp_keymgmt = NULL;
1892 int selection = OSSL_KEYMGMT_SELECT_ALL;
1893 void *keydata = NULL;
1894 int check;
1895
1896 if (pk == NULL)
1897 return NULL;
1898
1899 /* No key data => nothing to export */
1900 check = 1;
1901 #ifndef FIPS_MODULE
1902 check = check && pk->pkey.ptr == NULL;
1903 #endif
1904 check = check && pk->keydata == NULL;
1905 if (check)
1906 return NULL;
1907
1908 #ifndef FIPS_MODULE
1909 if (pk->pkey.ptr != NULL) {
1910 /*
1911 * If the legacy key doesn't have an dirty counter or export function,
1912 * give up
1913 */
1914 if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
1915 return NULL;
1916 }
1917 #endif
1918
1919 if (keymgmt != NULL) {
1920 tmp_keymgmt = *keymgmt;
1921 *keymgmt = NULL;
1922 }
1923
1924 /*
1925 * If no keymgmt was given or found, get a default keymgmt. We do so by
1926 * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
1927 */
1928 if (tmp_keymgmt == NULL) {
1929 EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
1930
1931 if (ctx == NULL)
1932 goto end;
1933 allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;
1934 ctx->keymgmt = NULL;
1935 EVP_PKEY_CTX_free(ctx);
1936 }
1937
1938 /* If there's still no keymgmt to be had, give up */
1939 if (tmp_keymgmt == NULL)
1940 goto end;
1941
1942 #ifndef FIPS_MODULE
1943 if (pk->pkey.ptr != NULL) {
1944 OP_CACHE_ELEM *op;
1945
1946 /*
1947 * If the legacy "origin" hasn't changed since last time, we try
1948 * to find our keymgmt in the operation cache. If it has changed,
1949 * |i| remains zero, and we will clear the cache further down.
1950 */
1951 if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
1952 if (!CRYPTO_THREAD_read_lock(pk->lock))
1953 goto end;
1954 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
1955 selection);
1956
1957 /*
1958 * If |tmp_keymgmt| is present in the operation cache, it means
1959 * that export doesn't need to be redone. In that case, we take
1960 * token copies of the cached pointers, to have token success
1961 * values to return. It is possible (e.g. in a no-cached-fetch
1962 * build), for op->keymgmt to be a different pointer to tmp_keymgmt
1963 * even though the name/provider must be the same. In other words
1964 * the keymgmt instance may be different but still equivalent, i.e.
1965 * same algorithm/provider instance - but we make the simplifying
1966 * assumption that the keydata can be used with either keymgmt
1967 * instance. Not doing so introduces significant complexity and
1968 * probably requires refactoring - since we would have to ripple
1969 * the change in keymgmt instance up the call chain.
1970 */
1971 if (op != NULL && op->keymgmt != NULL) {
1972 keydata = op->keydata;
1973 CRYPTO_THREAD_unlock(pk->lock);
1974 goto end;
1975 }
1976 CRYPTO_THREAD_unlock(pk->lock);
1977 }
1978
1979 /* Make sure that the keymgmt key type matches the legacy NID */
1980 if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
1981 goto end;
1982
1983 if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
1984 goto end;
1985
1986 if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,
1987 libctx, propquery)) {
1988 evp_keymgmt_freedata(tmp_keymgmt, keydata);
1989 keydata = NULL;
1990 goto end;
1991 }
1992
1993 /*
1994 * If the dirty counter changed since last time, then clear the
1995 * operation cache. In that case, we know that |i| is zero. Just
1996 * in case this is a re-export, we increment then decrement the
1997 * keymgmt reference counter.
1998 */
1999 if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
2000 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2001 keydata = NULL;
2002 goto end;
2003 }
2004
2005 if (!CRYPTO_THREAD_write_lock(pk->lock))
2006 goto end;
2007 if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
2008 && !evp_keymgmt_util_clear_operation_cache(pk)) {
2009 CRYPTO_THREAD_unlock(pk->lock);
2010 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2011 keydata = NULL;
2012 EVP_KEYMGMT_free(tmp_keymgmt);
2013 goto end;
2014 }
2015 EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
2016
2017 /* Check to make sure some other thread didn't get there first */
2018 op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
2019 if (op != NULL && op->keymgmt != NULL) {
2020 void *tmp_keydata = op->keydata;
2021
2022 CRYPTO_THREAD_unlock(pk->lock);
2023 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2024 keydata = tmp_keydata;
2025 goto end;
2026 }
2027
2028 /* Add the new export to the operation cache */
2029 if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
2030 selection)) {
2031 CRYPTO_THREAD_unlock(pk->lock);
2032 evp_keymgmt_freedata(tmp_keymgmt, keydata);
2033 keydata = NULL;
2034 goto end;
2035 }
2036
2037 /* Synchronize the dirty count */
2038 pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
2039
2040 CRYPTO_THREAD_unlock(pk->lock);
2041 goto end;
2042 }
2043 #endif /* FIPS_MODULE */
2044
2045 keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
2046
2047 end:
2048 /*
2049 * If nothing was exported, |tmp_keymgmt| might point at a freed
2050 * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
2051 * the caller either way in that case.
2052 */
2053 if (keydata == NULL)
2054 tmp_keymgmt = NULL;
2055
2056 if (keymgmt != NULL && tmp_keymgmt != NULL) {
2057 *keymgmt = tmp_keymgmt;
2058 allocated_keymgmt = NULL;
2059 }
2060
2061 EVP_KEYMGMT_free(allocated_keymgmt);
2062 return keydata;
2063 }
2064
2065 #ifndef FIPS_MODULE
2066 int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
2067 {
2068 EVP_PKEY *allocpkey = NULL;
2069
2070 if (!ossl_assert(dest != NULL))
2071 return 0;
2072
2073 if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
2074 EVP_KEYMGMT *keymgmt = src->keymgmt;
2075 void *keydata = src->keydata;
2076 int type = src->type;
2077 const char *keytype = NULL;
2078
2079 keytype = EVP_KEYMGMT_get0_name(keymgmt);
2080
2081 /*
2082 * If the type is EVP_PKEY_NONE, then we have a problem somewhere
2083 * else in our code. If it's not one of the well known EVP_PKEY_xxx
2084 * values, it should at least be EVP_PKEY_KEYMGMT at this point.
2085 * The check is kept as a safety measure.
2086 */
2087 if (!ossl_assert(type != EVP_PKEY_NONE)) {
2088 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
2089 "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
2090 keytype);
2091 return 0;
2092 }
2093
2094 /* Prefer the legacy key type name for error reporting */
2095 if (type != EVP_PKEY_KEYMGMT)
2096 keytype = OBJ_nid2sn(type);
2097
2098 /* Make sure we have a clean slate to copy into */
2099 if (*dest == NULL) {
2100 allocpkey = *dest = EVP_PKEY_new();
2101 if (*dest == NULL) {
2102 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2103 return 0;
2104 }
2105 } else {
2106 evp_pkey_free_it(*dest);
2107 }
2108
2109 if (EVP_PKEY_set_type(*dest, type)) {
2110 /* If the key is typed but empty, we're done */
2111 if (keydata == NULL)
2112 return 1;
2113
2114 if ((*dest)->ameth->import_from == NULL) {
2115 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
2116 "key type = %s", keytype);
2117 } else {
2118 /*
2119 * We perform the export in the same libctx as the keymgmt
2120 * that we are using.
2121 */
2122 OSSL_LIB_CTX *libctx = ossl_provider_libctx(keymgmt->prov);
2123 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
2124
2125 if (pctx == NULL)
2126 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
2127
2128 if (pctx != NULL
2129 && evp_keymgmt_export(keymgmt, keydata,
2130 OSSL_KEYMGMT_SELECT_ALL,
2131 (*dest)->ameth->import_from,
2132 pctx)) {
2133 /* Synchronize the dirty count */
2134 (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
2135
2136 EVP_PKEY_CTX_free(pctx);
2137 return 1;
2138 }
2139 EVP_PKEY_CTX_free(pctx);
2140 }
2141
2142 ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
2143 "key type = %s", keytype);
2144 }
2145 }
2146
2147 if (allocpkey != NULL) {
2148 EVP_PKEY_free(allocpkey);
2149 *dest = NULL;
2150 }
2151 return 0;
2152 }
2153
2154 void *evp_pkey_get_legacy(EVP_PKEY *pk)
2155 {
2156 EVP_PKEY *tmp_copy = NULL;
2157 void *ret = NULL;
2158
2159 if (!ossl_assert(pk != NULL))
2160 return NULL;
2161
2162 /*
2163 * If this isn't an assigned provider side key, we just use any existing
2164 * origin legacy key.
2165 */
2166 if (!evp_pkey_is_assigned(pk))
2167 return NULL;
2168 if (!evp_pkey_is_provided(pk))
2169 return pk->pkey.ptr;
2170
2171 if (!CRYPTO_THREAD_read_lock(pk->lock))
2172 return NULL;
2173
2174 ret = pk->legacy_cache_pkey.ptr;
2175
2176 if (!CRYPTO_THREAD_unlock(pk->lock))
2177 return NULL;
2178
2179 if (ret != NULL)
2180 return ret;
2181
2182 if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
2183 goto err;
2184
2185 if (!CRYPTO_THREAD_write_lock(pk->lock))
2186 goto err;
2187
2188 /* Check again in case some other thread has updated it in the meantime */
2189 ret = pk->legacy_cache_pkey.ptr;
2190 if (ret == NULL) {
2191 /* Steal the legacy key reference from the temporary copy */
2192 ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
2193 tmp_copy->pkey.ptr = NULL;
2194 }
2195
2196 if (!CRYPTO_THREAD_unlock(pk->lock)) {
2197 ret = NULL;
2198 goto err;
2199 }
2200
2201 err:
2202 EVP_PKEY_free(tmp_copy);
2203
2204 return ret;
2205 }
2206 #endif /* FIPS_MODULE */
2207
2208 int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
2209 BIGNUM **bn)
2210 {
2211 int ret = 0;
2212 OSSL_PARAM params[2];
2213 unsigned char buffer[2048];
2214 unsigned char *buf = NULL;
2215 size_t buf_sz = 0;
2216
2217 if (key_name == NULL
2218 || bn == NULL)
2219 return 0;
2220
2221 memset(buffer, 0, sizeof(buffer));
2222 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
2223 params[1] = OSSL_PARAM_construct_end();
2224 if (!EVP_PKEY_get_params(pkey, params)) {
2225 if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
2226 return 0;
2227 buf_sz = params[0].return_size;
2228 /*
2229 * If it failed because the buffer was too small then allocate the
2230 * required buffer size and retry.
2231 */
2232 buf = OPENSSL_zalloc(buf_sz);
2233 if (buf == NULL)
2234 return 0;
2235 params[0].data = buf;
2236 params[0].data_size = buf_sz;
2237
2238 if (!EVP_PKEY_get_params(pkey, params))
2239 goto err;
2240 }
2241 /* Fail if the param was not found */
2242 if (!OSSL_PARAM_modified(params))
2243 goto err;
2244 ret = OSSL_PARAM_get_BN(params, bn);
2245 err:
2246 if (buf != NULL) {
2247 if (OSSL_PARAM_modified(params))
2248 OPENSSL_clear_free(buf, buf_sz);
2249 else
2250 OPENSSL_free(buf);
2251 } else if (OSSL_PARAM_modified(params)) {
2252 OPENSSL_cleanse(buffer, params[0].data_size);
2253 }
2254 return ret;
2255 }
2256
2257 int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
2258 unsigned char *buf, size_t max_buf_sz,
2259 size_t *out_len)
2260 {
2261 OSSL_PARAM params[2];
2262 int ret1 = 0, ret2 = 0;
2263
2264 if (key_name == NULL)
2265 return 0;
2266
2267 params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
2268 params[1] = OSSL_PARAM_construct_end();
2269 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2270 ret2 = OSSL_PARAM_modified(params);
2271 if (ret2 && out_len != NULL)
2272 *out_len = params[0].return_size;
2273 return ret1 && ret2;
2274 }
2275
2276 int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
2277 char *str, size_t max_buf_sz,
2278 size_t *out_len)
2279 {
2280 OSSL_PARAM params[2];
2281 int ret1 = 0, ret2 = 0;
2282
2283 if (key_name == NULL)
2284 return 0;
2285
2286 params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
2287 params[1] = OSSL_PARAM_construct_end();
2288 if ((ret1 = EVP_PKEY_get_params(pkey, params)))
2289 ret2 = OSSL_PARAM_modified(params);
2290 if (ret2 && out_len != NULL)
2291 *out_len = params[0].return_size;
2292
2293 if (ret2 && params[0].return_size == max_buf_sz)
2294 /* There was no space for a NUL byte */
2295 return 0;
2296 /* Add a terminating NUL byte for good measure */
2297 if (ret2 && str != NULL)
2298 str[params[0].return_size] = '\0';
2299
2300 return ret1 && ret2;
2301 }
2302
2303 int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
2304 int *out)
2305 {
2306 OSSL_PARAM params[2];
2307
2308 if (key_name == NULL)
2309 return 0;
2310
2311 params[0] = OSSL_PARAM_construct_int(key_name, out);
2312 params[1] = OSSL_PARAM_construct_end();
2313 return EVP_PKEY_get_params(pkey, params)
2314 && OSSL_PARAM_modified(params);
2315 }
2316
2317 int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
2318 size_t *out)
2319 {
2320 OSSL_PARAM params[2];
2321
2322 if (key_name == NULL)
2323 return 0;
2324
2325 params[0] = OSSL_PARAM_construct_size_t(key_name, out);
2326 params[1] = OSSL_PARAM_construct_end();
2327 return EVP_PKEY_get_params(pkey, params)
2328 && OSSL_PARAM_modified(params);
2329 }
2330
2331 int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
2332 {
2333 OSSL_PARAM params[2];
2334
2335 if (key_name == NULL)
2336 return 0;
2337
2338 params[0] = OSSL_PARAM_construct_int(key_name, &in);
2339 params[1] = OSSL_PARAM_construct_end();
2340 return EVP_PKEY_set_params(pkey, params);
2341 }
2342
2343 int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
2344 {
2345 OSSL_PARAM params[2];
2346
2347 if (key_name == NULL)
2348 return 0;
2349
2350 params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
2351 params[1] = OSSL_PARAM_construct_end();
2352 return EVP_PKEY_set_params(pkey, params);
2353 }
2354
2355 int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
2356 const BIGNUM *bn)
2357 {
2358 OSSL_PARAM params[2];
2359 unsigned char buffer[2048];
2360 int bsize = 0;
2361
2362 if (key_name == NULL
2363 || bn == NULL
2364 || pkey == NULL
2365 || !evp_pkey_is_assigned(pkey))
2366 return 0;
2367
2368 bsize = BN_num_bytes(bn);
2369 if (!ossl_assert(bsize <= (int)sizeof(buffer)))
2370 return 0;
2371
2372 if (BN_bn2nativepad(bn, buffer, bsize) < 0)
2373 return 0;
2374 params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
2375 params[1] = OSSL_PARAM_construct_end();
2376 return EVP_PKEY_set_params(pkey, params);
2377 }
2378
2379 int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
2380 const char *str)
2381 {
2382 OSSL_PARAM params[2];
2383
2384 if (key_name == NULL)
2385 return 0;
2386
2387 params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
2388 params[1] = OSSL_PARAM_construct_end();
2389 return EVP_PKEY_set_params(pkey, params);
2390 }
2391
2392 int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
2393 const unsigned char *buf, size_t bsize)
2394 {
2395 OSSL_PARAM params[2];
2396
2397 if (key_name == NULL)
2398 return 0;
2399
2400 params[0] = OSSL_PARAM_construct_octet_string(key_name,
2401 (unsigned char *)buf, bsize);
2402 params[1] = OSSL_PARAM_construct_end();
2403 return EVP_PKEY_set_params(pkey, params);
2404 }
2405
2406 const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
2407 {
2408 return (pkey != NULL && evp_pkey_is_provided(pkey))
2409 ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
2410 : NULL;
2411 }
2412
2413 int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
2414 {
2415 if (pkey != NULL) {
2416 if (evp_pkey_is_provided(pkey)) {
2417 pkey->dirty_cnt++;
2418 return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
2419 }
2420 #ifndef FIPS_MODULE
2421 /*
2422 * We will hopefully never find the need to set individual data in
2423 * EVP_PKEYs with a legacy internal key, but we can't be entirely
2424 * sure. This bit of code can be enabled if we find the need. If
2425 * not, it can safely be removed when #legacy support is removed.
2426 */
2427 #if 0
2428 else if (evp_pkey_is_legacy(pkey)) {
2429 return evp_pkey_set_params_to_ctrl(pkey, params);
2430 }
2431 #endif
2432 #endif
2433 }
2434 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2435 return 0;
2436 }
2437
2438 const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
2439 {
2440 return (pkey != NULL && evp_pkey_is_provided(pkey))
2441 ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
2442 : NULL;
2443 }
2444
2445 int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
2446 {
2447 if (pkey != NULL) {
2448 if (evp_pkey_is_provided(pkey))
2449 return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
2450 #ifndef FIPS_MODULE
2451 else if (evp_pkey_is_legacy(pkey))
2452 return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
2453 #endif
2454 }
2455 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
2456 return 0;
2457 }
2458
2459 #ifndef FIPS_MODULE
2460 int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
2461 {
2462 char name[80];
2463 size_t name_len;
2464
2465 if (pkey == NULL)
2466 return 0;
2467
2468 if (pkey->keymgmt == NULL
2469 || pkey->keydata == NULL) {
2470 #ifndef OPENSSL_NO_EC
2471 /* Might work through the legacy route */
2472 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2473
2474 if (ec == NULL)
2475 return 0;
2476
2477 return EC_KEY_get_conv_form(ec);
2478 #else
2479 return 0;
2480 #endif
2481 }
2482
2483 if (!EVP_PKEY_get_utf8_string_param(pkey,
2484 OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
2485 name, sizeof(name), &name_len))
2486 return 0;
2487
2488 if (strcmp(name, "uncompressed") == 0)
2489 return POINT_CONVERSION_UNCOMPRESSED;
2490
2491 if (strcmp(name, "compressed") == 0)
2492 return POINT_CONVERSION_COMPRESSED;
2493
2494 if (strcmp(name, "hybrid") == 0)
2495 return POINT_CONVERSION_HYBRID;
2496
2497 return 0;
2498 }
2499
2500 int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
2501 {
2502 char fstr[80];
2503 size_t fstrlen;
2504
2505 if (pkey == NULL)
2506 return 0;
2507
2508 if (pkey->keymgmt == NULL
2509 || pkey->keydata == NULL) {
2510 #ifndef OPENSSL_NO_EC
2511 /* Might work through the legacy route */
2512 const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
2513 const EC_GROUP *grp;
2514
2515 if (ec == NULL)
2516 return 0;
2517 grp = EC_KEY_get0_group(ec);
2518 if (grp == NULL)
2519 return 0;
2520
2521 return EC_GROUP_get_field_type(grp);
2522 #else
2523 return 0;
2524 #endif
2525 }
2526
2527 if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
2528 fstr, sizeof(fstr), &fstrlen))
2529 return 0;
2530
2531 if (strcmp(fstr, SN_X9_62_prime_field) == 0)
2532 return NID_X9_62_prime_field;
2533 else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
2534 return NID_X9_62_characteristic_two_field;
2535
2536 return 0;
2537 }
2538 #endif