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