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