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