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