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