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