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