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