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