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