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