/src/openssl35/crypto/evp/p_lib.c
Line | Count | Source (jump to first uncovered line) |
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 | 403k | # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS |
59 | | |
60 | | #ifndef FIPS_MODULE |
61 | | int EVP_PKEY_get_bits(const EVP_PKEY *pkey) |
62 | 28.3k | { |
63 | 28.3k | int size = 0; |
64 | | |
65 | 28.3k | if (pkey != NULL) { |
66 | 28.3k | size = pkey->cache.bits; |
67 | 28.3k | if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL) |
68 | 0 | size = pkey->ameth->pkey_bits(pkey); |
69 | 28.3k | } |
70 | 28.3k | if (size <= 0) { |
71 | 1.33k | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS); |
72 | 1.33k | return 0; |
73 | 1.33k | } |
74 | 27.0k | return size; |
75 | 28.3k | } |
76 | | |
77 | | int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey) |
78 | 159k | { |
79 | 159k | int size = 0; |
80 | | |
81 | 159k | if (pkey != NULL) { |
82 | 159k | size = pkey->cache.security_bits; |
83 | 159k | if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL) |
84 | 0 | size = pkey->ameth->pkey_security_bits(pkey); |
85 | 159k | } |
86 | 159k | if (size <= 0) { |
87 | 52 | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS); |
88 | 52 | return 0; |
89 | 52 | } |
90 | 159k | return size; |
91 | 159k | } |
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 | 161k | { |
129 | | /* |
130 | | * Clean up legacy stuff from this function when legacy support is gone. |
131 | | */ |
132 | | |
133 | 161k | EVP_PKEY *downgraded_from = NULL; |
134 | 161k | int ok = 0; |
135 | | |
136 | 161k | #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 | 161k | 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 | 161k | #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 | 161k | if (evp_pkey_is_blank(to)) { |
161 | 30.3k | #ifndef FIPS_MODULE |
162 | 30.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 | 30.3k | #endif /* !FIPS_MODULE */ |
167 | 30.3k | { |
168 | 30.3k | if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0) |
169 | 0 | goto end; |
170 | 30.3k | } |
171 | 30.3k | } |
172 | 131k | #ifndef FIPS_MODULE |
173 | 131k | 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 | 161k | #endif /* !FIPS_MODULE */ |
180 | | |
181 | 161k | 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 | 161k | if (!EVP_PKEY_missing_parameters(to)) { |
187 | 131k | if (EVP_PKEY_parameters_eq(to, from) == 1) |
188 | 131k | ok = 1; |
189 | 0 | else |
190 | 131k | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS); |
191 | 131k | goto end; |
192 | 131k | } |
193 | | |
194 | | /* For purely provided keys, we just call the keymgmt utility */ |
195 | 30.3k | if (to->keymgmt != NULL && from->keymgmt != NULL) { |
196 | 30.3k | ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS); |
197 | 30.3k | goto end; |
198 | 30.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 | 161k | end: |
232 | 161k | EVP_PKEY_free(downgraded_from); |
233 | 161k | return ok; |
234 | 0 | } |
235 | | |
236 | | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
237 | 143k | { |
238 | 143k | if (pkey != NULL) { |
239 | | #ifdef FIPS_MODULE |
240 | | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS); |
241 | | #else |
242 | 143k | if (pkey->keymgmt != NULL) |
243 | 91.5k | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS); |
244 | 52.3k | if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL) |
245 | 34.9k | return pkey->ameth->param_missing(pkey); |
246 | 52.3k | #endif /* FIPS_MODULE */ |
247 | 52.3k | } |
248 | 17.4k | return 0; |
249 | 143k | } |
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 | 281k | { |
259 | | #ifdef FIPS_MODULE |
260 | | return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection); |
261 | | #else |
262 | 281k | EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL; |
263 | 281k | void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL; |
264 | | |
265 | | /* If none of them are provided, this function shouldn't have been called */ |
266 | 281k | 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 | 281k | if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b)) |
271 | 19.6k | 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 | 262k | if (evp_pkey_is_legacy(a) |
278 | 262k | && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type))) |
279 | 0 | return -1; /* not the same key type */ |
280 | 262k | if (evp_pkey_is_legacy(b) |
281 | 262k | && !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 | 262k | keymgmt1 = a->keymgmt; |
290 | 262k | keydata1 = a->keydata; |
291 | 262k | keymgmt2 = b->keymgmt; |
292 | 262k | keydata2 = b->keydata; |
293 | | |
294 | 262k | 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 | 262k | if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) { |
303 | 262k | tmp_keydata = |
304 | 262k | evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL); |
305 | 262k | if (tmp_keydata != NULL) { |
306 | 262k | keymgmt2 = keymgmt1; |
307 | 262k | keydata2 = tmp_keydata; |
308 | 262k | } |
309 | 262k | } |
310 | | |
311 | | /* If we still don't have matching keymgmt implementations, we give up */ |
312 | 262k | if (keymgmt1 != keymgmt2) |
313 | 0 | return -2; |
314 | | |
315 | | /* If the keymgmt implementations are NULL, the export failed */ |
316 | 262k | if (keymgmt1 == NULL) |
317 | 0 | return -2; |
318 | | |
319 | 262k | return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection); |
320 | 262k | #endif /* FIPS_MODULE */ |
321 | 262k | } |
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 | 131k | { |
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 | 131k | if (a->keymgmt != NULL || b->keymgmt != NULL) |
343 | 131k | 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 | 150k | { |
365 | | /* |
366 | | * This will just call evp_keymgmt_util_match when legacy support |
367 | | * is gone. |
368 | | */ |
369 | | |
370 | | /* Trivial shortcuts */ |
371 | 150k | if (a == b) |
372 | 74 | return 1; |
373 | 150k | if (a == NULL || b == NULL) |
374 | 0 | return 0; |
375 | | |
376 | 150k | #ifndef FIPS_MODULE |
377 | 150k | if (a->keymgmt != NULL || b->keymgmt != NULL) |
378 | 150k | #endif /* !FIPS_MODULE */ |
379 | 150k | { |
380 | 150k | int selection = SELECT_PARAMETERS; |
381 | | |
382 | 150k | if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY) |
383 | 150k | && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)) |
384 | 10.2k | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
385 | 140k | else |
386 | 140k | selection |= OSSL_KEYMGMT_SELECT_KEYPAIR; |
387 | 150k | return evp_pkey_cmp_any(a, b, selection); |
388 | 150k | } |
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 | 15.0k | { |
422 | 15.0k | EVP_PKEY *pkey = NULL; |
423 | 15.0k | EVP_PKEY_CTX *ctx = NULL; |
424 | 15.0k | const EVP_PKEY_ASN1_METHOD *ameth = NULL; |
425 | 15.0k | int result = 0; |
426 | | |
427 | 15.0k | # ifndef OPENSSL_NO_ENGINE |
428 | | /* Check if there is an Engine for this type */ |
429 | 15.0k | if (e == NULL) { |
430 | 15.0k | ENGINE *tmpe = NULL; |
431 | | |
432 | 15.0k | if (strtype != NULL) |
433 | 15.0k | ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1); |
434 | 4 | 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 | 15.0k | if (tmpe == NULL) |
439 | 15.0k | ameth = NULL; |
440 | | |
441 | 15.0k | ENGINE_finish(tmpe); |
442 | 15.0k | } |
443 | 15.0k | # endif |
444 | | |
445 | 15.0k | 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 | 15.0k | ctx = EVP_PKEY_CTX_new_from_name(libctx, |
451 | 15.0k | strtype != NULL ? strtype |
452 | 15.0k | : OBJ_nid2sn(nidtype), |
453 | 15.0k | propq); |
454 | 15.0k | if (ctx == NULL) |
455 | 10 | goto err; |
456 | | /* May fail if no provider available */ |
457 | 15.0k | ERR_set_mark(); |
458 | 15.0k | if (EVP_PKEY_fromdata_init(ctx) == 1) { |
459 | 15.0k | OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
460 | | |
461 | 15.0k | ERR_clear_last_mark(); |
462 | 15.0k | params[0] = OSSL_PARAM_construct_octet_string( |
463 | 15.0k | key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY |
464 | 15.0k | : OSSL_PKEY_PARAM_PUB_KEY, |
465 | 15.0k | (void *)key, len); |
466 | | |
467 | 15.0k | if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) { |
468 | 200 | ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED); |
469 | 200 | goto err; |
470 | 200 | } |
471 | | |
472 | 14.8k | EVP_PKEY_CTX_free(ctx); |
473 | | |
474 | 14.8k | return pkey; |
475 | 15.0k | } |
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 | 210 | err: |
520 | 210 | if (!result) { |
521 | 210 | EVP_PKEY_free(pkey); |
522 | 210 | pkey = NULL; |
523 | 210 | } |
524 | 210 | EVP_PKEY_CTX_free(ctx); |
525 | 210 | 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 | 14.9k | { |
533 | 14.9k | return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv, |
534 | 14.9k | len, 1); |
535 | 14.9k | } |
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 | 117 | { |
548 | 117 | return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub, |
549 | 117 | len, 0); |
550 | 117 | } |
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.45M | { |
720 | 5.45M | return pkey_set_type(pkey, NULL, type, NULL, -1, NULL); |
721 | 5.45M | } |
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 | 690k | { |
756 | 690k | switch (pkey->type) { |
757 | 154k | case EVP_PKEY_RSA: |
758 | 182k | case EVP_PKEY_RSA_PSS: |
759 | 182k | pkey->foreign = pkey->pkey.rsa != NULL |
760 | 182k | && ossl_rsa_is_foreign(pkey->pkey.rsa); |
761 | 182k | break; |
762 | 0 | # ifndef OPENSSL_NO_EC |
763 | 5.94k | case EVP_PKEY_SM2: |
764 | 5.94k | break; |
765 | 326k | case EVP_PKEY_EC: |
766 | 326k | pkey->foreign = pkey->pkey.ec != NULL |
767 | 326k | && ossl_ec_key_is_foreign(pkey->pkey.ec); |
768 | 326k | break; |
769 | 0 | # endif |
770 | 0 | # ifndef OPENSSL_NO_DSA |
771 | 145k | case EVP_PKEY_DSA: |
772 | 145k | pkey->foreign = pkey->pkey.dsa != NULL |
773 | 145k | && ossl_dsa_is_foreign(pkey->pkey.dsa); |
774 | 145k | break; |
775 | 0 | #endif |
776 | 0 | # ifndef OPENSSL_NO_DH |
777 | 7.84k | case EVP_PKEY_DH: |
778 | 7.84k | pkey->foreign = pkey->pkey.dh != NULL |
779 | 7.84k | && ossl_dh_is_foreign(pkey->pkey.dh); |
780 | 7.84k | break; |
781 | 0 | #endif |
782 | 22.1k | default: |
783 | 22.1k | pkey->foreign = 0; |
784 | 22.1k | break; |
785 | 690k | } |
786 | 690k | } |
787 | | |
788 | | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
789 | 690k | { |
790 | 690k | # ifndef OPENSSL_NO_EC |
791 | 690k | int pktype; |
792 | | |
793 | 690k | pktype = EVP_PKEY_type(type); |
794 | 690k | if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) { |
795 | 332k | const EC_GROUP *group = EC_KEY_get0_group(key); |
796 | | |
797 | 332k | if (group != NULL) { |
798 | 332k | 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 | 332k | if (curve == NID_sm2 && pktype == EVP_PKEY_EC) |
805 | 5.94k | type = EVP_PKEY_SM2; |
806 | 326k | else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2) |
807 | 0 | type = EVP_PKEY_EC; |
808 | 332k | } |
809 | 332k | } |
810 | 690k | # endif |
811 | | |
812 | 690k | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
813 | 0 | return 0; |
814 | | |
815 | 690k | pkey->pkey.ptr = key; |
816 | 690k | detect_foreign_key(pkey); |
817 | | |
818 | 690k | return (key != NULL); |
819 | 690k | } |
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 | 101k | { |
886 | 101k | 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 | 101k | return evp_pkey_get_legacy((EVP_PKEY *)pkey); |
891 | 101k | } |
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 | 40.1k | { |
914 | 40.1k | DSA *ret = evp_pkey_get0_DSA_int(pkey); |
915 | | |
916 | 40.1k | if (ret != NULL && !DSA_up_ref(ret)) |
917 | 0 | return NULL; |
918 | | |
919 | 40.1k | return ret; |
920 | 40.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 | 3.38k | { |
926 | 3.38k | 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 | 3.38k | return evp_pkey_get_legacy((EVP_PKEY *)pkey); |
931 | 3.38k | } |
932 | | |
933 | | static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type) |
934 | 3.38k | { |
935 | 3.38k | ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type); |
936 | | |
937 | 3.38k | if (ret != NULL && !ossl_ecx_key_up_ref(ret)) |
938 | 0 | ret = NULL; |
939 | 3.38k | return ret; |
940 | 3.38k | } |
941 | | |
942 | | # define IMPLEMENT_ECX_VARIANT(NAME) \ |
943 | | ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \ |
944 | 3.38k | { \ |
945 | 3.38k | return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \ |
946 | 3.38k | } ossl_evp_pkey_get1_X25519 Line | Count | Source | 944 | 495 | { \ | 945 | 495 | return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \ | 946 | 495 | } |
Line | Count | Source | 944 | 2.19k | { \ | 945 | 2.19k | return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \ | 946 | 2.19k | } |
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 | 458 | { \ | 945 | 458 | return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \ | 946 | 458 | } |
|
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 | 26.5k | { |
997 | 26.5k | 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 | 26.5k | return evp_pkey_get_legacy((EVP_PKEY *)pkey); |
1002 | 26.5k | } |
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 | 7.54k | { |
1011 | 7.54k | DH *ret = evp_pkey_get0_DH_int(pkey); |
1012 | | |
1013 | 7.54k | if (ret != NULL && !DH_up_ref(ret)) |
1014 | 0 | ret = NULL; |
1015 | | |
1016 | 7.54k | return ret; |
1017 | 7.54k | } |
1018 | | # endif |
1019 | | |
1020 | | int EVP_PKEY_type(int type) |
1021 | 3.33M | { |
1022 | 3.33M | int ret; |
1023 | 3.33M | const EVP_PKEY_ASN1_METHOD *ameth; |
1024 | 3.33M | ENGINE *e; |
1025 | 3.33M | ameth = EVP_PKEY_asn1_find(&e, type); |
1026 | 3.33M | if (ameth) |
1027 | 2.17M | ret = ameth->pkey_id; |
1028 | 1.16M | else |
1029 | 1.16M | ret = NID_undef; |
1030 | 3.33M | # ifndef OPENSSL_NO_ENGINE |
1031 | 3.33M | ENGINE_finish(e); |
1032 | 3.33M | # endif |
1033 | 3.33M | return ret; |
1034 | 3.33M | } |
1035 | | |
1036 | | int EVP_PKEY_get_id(const EVP_PKEY *pkey) |
1037 | 330k | { |
1038 | 330k | return pkey->type; |
1039 | 330k | } |
1040 | | |
1041 | | int EVP_PKEY_get_base_id(const EVP_PKEY *pkey) |
1042 | 297k | { |
1043 | 297k | return EVP_PKEY_type(pkey->type); |
1044 | 297k | } |
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.30M | { |
1075 | 2.30M | int type; |
1076 | 2.30M | size_t i; |
1077 | | |
1078 | 25.3M | for (i = 0; i < OSSL_NELEM(standard_name2type); i++) { |
1079 | 23.6M | if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0) |
1080 | 657k | return (int)standard_name2type[i].id; |
1081 | 23.6M | } |
1082 | | |
1083 | 1.64M | if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef) |
1084 | 948k | return type; |
1085 | 698k | 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.59M | { |
1102 | 1.59M | if (pkey == NULL) |
1103 | 0 | return 0; |
1104 | 1.59M | if (pkey->keymgmt == NULL) |
1105 | 568k | return pkey->type == evp_pkey_name2type(name); |
1106 | 1.02M | return EVP_KEYMGMT_is_a(pkey->keymgmt, name); |
1107 | 1.59M | } |
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 | 43.7k | { |
1127 | 43.7k | 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 | 43.7k | } else { |
1147 | 43.7k | const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt); |
1148 | 43.7k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
1149 | 43.7k | const char *supported_sig = |
1150 | 43.7k | pkey->keymgmt->query_operation_name != NULL |
1151 | 43.7k | ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE) |
1152 | 43.7k | : EVP_KEYMGMT_get0_name(pkey->keymgmt); |
1153 | 43.7k | EVP_SIGNATURE *signature = NULL; |
1154 | | |
1155 | 43.7k | signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL); |
1156 | 43.7k | if (signature != NULL) { |
1157 | 43.7k | EVP_SIGNATURE_free(signature); |
1158 | 43.7k | return 1; |
1159 | 43.7k | } |
1160 | 43.7k | } |
1161 | 0 | return 0; |
1162 | 43.7k | } |
1163 | | |
1164 | | static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent) |
1165 | 76.8k | { |
1166 | 76.8k | BIO_set_indent(*out, saved_indent); |
1167 | 76.8k | if (pop_f_prefix) { |
1168 | 66.8k | BIO *next = BIO_pop(*out); |
1169 | | |
1170 | 66.8k | BIO_free(*out); |
1171 | 66.8k | *out = next; |
1172 | 66.8k | } |
1173 | 76.8k | return 1; |
1174 | 76.8k | } |
1175 | | |
1176 | | static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent, |
1177 | | long indent) |
1178 | 76.8k | { |
1179 | 76.8k | *pop_f_prefix = 0; |
1180 | 76.8k | *saved_indent = 0; |
1181 | 76.8k | if (indent > 0) { |
1182 | 66.8k | long i = BIO_get_indent(*out); |
1183 | | |
1184 | 66.8k | *saved_indent = (i < 0 ? 0 : i); |
1185 | 66.8k | if (BIO_set_indent(*out, indent) <= 0) { |
1186 | 66.8k | BIO *prefbio = BIO_new(BIO_f_prefix()); |
1187 | | |
1188 | 66.8k | if (prefbio == NULL) |
1189 | 0 | return 0; |
1190 | 66.8k | *out = BIO_push(prefbio, *out); |
1191 | 66.8k | *pop_f_prefix = 1; |
1192 | 66.8k | } |
1193 | 66.8k | if (BIO_set_indent(*out, indent) <= 0) { |
1194 | 0 | print_reset_indent(out, *pop_f_prefix, *saved_indent); |
1195 | 0 | return 0; |
1196 | 0 | } |
1197 | 66.8k | } |
1198 | 76.8k | return 1; |
1199 | 76.8k | } |
1200 | | |
1201 | | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
1202 | | const char *kstr) |
1203 | 0 | { |
1204 | 0 | return BIO_indent(out, indent, 128) |
1205 | 0 | && BIO_printf(out, "%s algorithm \"%s\" unsupported\n", |
1206 | 0 | kstr, OBJ_nid2ln(pkey->type)) > 0; |
1207 | 0 | } |
1208 | | |
1209 | | static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent, |
1210 | | int selection /* For provided encoding */, |
1211 | | const char *propquery /* For provided encoding */, |
1212 | | int (*legacy_print)(BIO *out, const EVP_PKEY *pkey, |
1213 | | int indent, ASN1_PCTX *pctx), |
1214 | | ASN1_PCTX *legacy_pctx /* For legacy print */) |
1215 | 76.8k | { |
1216 | 76.8k | int pop_f_prefix; |
1217 | 76.8k | long saved_indent; |
1218 | 76.8k | OSSL_ENCODER_CTX *ctx = NULL; |
1219 | 76.8k | int ret = -2; /* default to unsupported */ |
1220 | | |
1221 | 76.8k | if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent)) |
1222 | 0 | return 0; |
1223 | | |
1224 | 76.8k | ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL, |
1225 | 76.8k | propquery); |
1226 | 76.8k | if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0) |
1227 | 75.3k | ret = OSSL_ENCODER_to_bio(ctx, out); |
1228 | 76.8k | OSSL_ENCODER_CTX_free(ctx); |
1229 | | |
1230 | 76.8k | if (ret != -2) |
1231 | 75.3k | goto end; |
1232 | | |
1233 | | /* legacy fallback */ |
1234 | 1.51k | if (legacy_print != NULL) |
1235 | 1.51k | ret = legacy_print(out, pkey, 0, legacy_pctx); |
1236 | 0 | else |
1237 | 0 | ret = unsup_alg(out, pkey, 0, "Public Key"); |
1238 | | |
1239 | 76.8k | end: |
1240 | 76.8k | print_reset_indent(&out, pop_f_prefix, saved_indent); |
1241 | 76.8k | return ret; |
1242 | 1.51k | } |
1243 | | |
1244 | | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, |
1245 | | int indent, ASN1_PCTX *pctx) |
1246 | 28.3k | { |
1247 | 28.3k | return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL, |
1248 | 28.3k | (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL), |
1249 | 28.3k | pctx); |
1250 | 28.3k | } |
1251 | | |
1252 | | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, |
1253 | | int indent, ASN1_PCTX *pctx) |
1254 | 29.2k | { |
1255 | 29.2k | return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL, |
1256 | 29.2k | (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL), |
1257 | 29.2k | pctx); |
1258 | 29.2k | } |
1259 | | |
1260 | | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, |
1261 | | int indent, ASN1_PCTX *pctx) |
1262 | 19.2k | { |
1263 | 19.2k | return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL, |
1264 | 19.2k | (pkey->ameth != NULL ? pkey->ameth->param_print : NULL), |
1265 | 19.2k | pctx); |
1266 | 19.2k | } |
1267 | | |
1268 | | # ifndef OPENSSL_NO_STDIO |
1269 | | int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey, |
1270 | | int indent, ASN1_PCTX *pctx) |
1271 | 0 | { |
1272 | 0 | int ret; |
1273 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
1274 | |
|
1275 | 0 | if (b == NULL) |
1276 | 0 | return 0; |
1277 | 0 | ret = EVP_PKEY_print_public(b, pkey, indent, pctx); |
1278 | 0 | BIO_free(b); |
1279 | 0 | return ret; |
1280 | 0 | } |
1281 | | |
1282 | | int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey, |
1283 | | int indent, ASN1_PCTX *pctx) |
1284 | 0 | { |
1285 | 0 | int ret; |
1286 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
1287 | |
|
1288 | 0 | if (b == NULL) |
1289 | 0 | return 0; |
1290 | 0 | ret = EVP_PKEY_print_private(b, pkey, indent, pctx); |
1291 | 0 | BIO_free(b); |
1292 | 0 | return ret; |
1293 | 0 | } |
1294 | | |
1295 | | int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey, |
1296 | | int indent, ASN1_PCTX *pctx) |
1297 | 0 | { |
1298 | 0 | int ret; |
1299 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
1300 | |
|
1301 | 0 | if (b == NULL) |
1302 | 0 | return 0; |
1303 | 0 | ret = EVP_PKEY_print_params(b, pkey, indent, pctx); |
1304 | 0 | BIO_free(b); |
1305 | 0 | return ret; |
1306 | 0 | } |
1307 | | # endif |
1308 | | |
1309 | | static void mdname2nid(const char *mdname, void *data) |
1310 | 0 | { |
1311 | 0 | int *nid = (int *)data; |
1312 | |
|
1313 | 0 | if (*nid != NID_undef) |
1314 | 0 | return; |
1315 | | |
1316 | 0 | *nid = OBJ_sn2nid(mdname); |
1317 | 0 | if (*nid == NID_undef) |
1318 | 0 | *nid = OBJ_ln2nid(mdname); |
1319 | 0 | } |
1320 | | |
1321 | | static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op, |
1322 | | int arg1, void *arg2) |
1323 | 0 | { |
1324 | 0 | if (pkey->keymgmt == NULL) |
1325 | 0 | return 0; |
1326 | 0 | switch (op) { |
1327 | 0 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
1328 | 0 | { |
1329 | 0 | char mdname[80] = ""; |
1330 | 0 | int rv = EVP_PKEY_get_default_digest_name(pkey, mdname, |
1331 | 0 | sizeof(mdname)); |
1332 | |
|
1333 | 0 | if (rv > 0) { |
1334 | 0 | int mdnum; |
1335 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov); |
1336 | | /* Make sure the MD is in the namemap if available */ |
1337 | 0 | EVP_MD *md; |
1338 | 0 | OSSL_NAMEMAP *namemap; |
1339 | 0 | int nid = NID_undef; |
1340 | |
|
1341 | 0 | (void)ERR_set_mark(); |
1342 | 0 | md = EVP_MD_fetch(libctx, mdname, NULL); |
1343 | 0 | (void)ERR_pop_to_mark(); |
1344 | 0 | namemap = ossl_namemap_stored(libctx); |
1345 | | |
1346 | | /* |
1347 | | * The only reason to fetch the MD was to make sure it is in the |
1348 | | * namemap. We can immediately free it. |
1349 | | */ |
1350 | 0 | EVP_MD_free(md); |
1351 | 0 | mdnum = ossl_namemap_name2num(namemap, mdname); |
1352 | 0 | if (mdnum == 0) |
1353 | 0 | return 0; |
1354 | | |
1355 | | /* |
1356 | | * We have the namemap number - now we need to find the |
1357 | | * associated nid |
1358 | | */ |
1359 | 0 | if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid)) |
1360 | 0 | return 0; |
1361 | 0 | *(int *)arg2 = nid; |
1362 | 0 | } |
1363 | 0 | return rv; |
1364 | 0 | } |
1365 | 0 | default: |
1366 | 0 | return -2; |
1367 | 0 | } |
1368 | 0 | } |
1369 | | |
1370 | | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
1371 | 0 | { |
1372 | 0 | if (pkey->ameth == NULL) |
1373 | 0 | return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2); |
1374 | 0 | if (pkey->ameth->pkey_ctrl == NULL) |
1375 | 0 | return -2; |
1376 | 0 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
1377 | 0 | } |
1378 | | |
1379 | | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) |
1380 | 0 | { |
1381 | 0 | if (pkey == NULL) |
1382 | 0 | return 0; |
1383 | 0 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); |
1384 | 0 | } |
1385 | | |
1386 | | int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey, |
1387 | | char *mdname, size_t mdname_sz) |
1388 | 0 | { |
1389 | 0 | if (pkey->ameth == NULL) |
1390 | 0 | return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt, |
1391 | 0 | pkey->keydata, |
1392 | 0 | mdname, mdname_sz); |
1393 | | |
1394 | 0 | { |
1395 | 0 | int nid = NID_undef; |
1396 | 0 | int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid); |
1397 | 0 | const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL; |
1398 | |
|
1399 | 0 | if (rv > 0) |
1400 | 0 | OPENSSL_strlcpy(mdname, name, mdname_sz); |
1401 | 0 | return rv; |
1402 | 0 | } |
1403 | 0 | } |
1404 | | |
1405 | | int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz, |
1406 | | size_t *gname_len) |
1407 | 26.4k | { |
1408 | 26.4k | return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME, |
1409 | 26.4k | gname, gname_sz, gname_len); |
1410 | 26.4k | } |
1411 | | |
1412 | | int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx, |
1413 | | const char *name, const char *propq) |
1414 | 40.2k | { |
1415 | 40.2k | int rv; |
1416 | 40.2k | EVP_MD_CTX *ctx = NULL; |
1417 | | |
1418 | 40.2k | if ((ctx = EVP_MD_CTX_new()) == NULL) |
1419 | 0 | return -1; |
1420 | | |
1421 | 40.2k | ERR_set_mark(); |
1422 | 40.2k | rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx, |
1423 | 40.2k | propq, pkey, NULL); |
1424 | 40.2k | ERR_pop_to_mark(); |
1425 | | |
1426 | 40.2k | EVP_MD_CTX_free(ctx); |
1427 | 40.2k | return rv; |
1428 | 40.2k | } |
1429 | | #endif /* !FIPS_MODULE */ |
1430 | | |
1431 | | int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub, |
1432 | | size_t publen) |
1433 | 43.2k | { |
1434 | 43.2k | if (pkey == NULL) |
1435 | 0 | return 0; |
1436 | 43.2k | #ifndef FIPS_MODULE |
1437 | 43.2k | if (evp_pkey_is_provided(pkey)) |
1438 | 43.2k | #endif /* !FIPS_MODULE */ |
1439 | 43.2k | return |
1440 | 43.2k | EVP_PKEY_set_octet_string_param(pkey, |
1441 | 43.2k | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
1442 | 43.2k | (unsigned char *)pub, publen); |
1443 | | |
1444 | 0 | #ifndef FIPS_MODULE |
1445 | 0 | if (publen > INT_MAX) |
1446 | 0 | return 0; |
1447 | | /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */ |
1448 | 0 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen, |
1449 | 0 | (void *)pub) <= 0) |
1450 | 0 | return 0; |
1451 | 0 | return 1; |
1452 | 0 | #endif /* !FIPS_MODULE */ |
1453 | 0 | } |
1454 | | |
1455 | | size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub) |
1456 | 138k | { |
1457 | 138k | if (pkey == NULL) |
1458 | 0 | return 0; |
1459 | 138k | #ifndef FIPS_MODULE |
1460 | 138k | if (evp_pkey_is_provided(pkey)) |
1461 | 138k | #endif |
1462 | 138k | { |
1463 | 138k | size_t return_size = OSSL_PARAM_UNMODIFIED; |
1464 | 138k | unsigned char *buf; |
1465 | | |
1466 | | /* |
1467 | | * We know that this is going to fail, but it will give us a size |
1468 | | * to allocate. |
1469 | | */ |
1470 | 138k | EVP_PKEY_get_octet_string_param(pkey, |
1471 | 138k | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
1472 | 138k | NULL, 0, &return_size); |
1473 | 138k | if (return_size == OSSL_PARAM_UNMODIFIED) |
1474 | 0 | return 0; |
1475 | | |
1476 | 138k | *ppub = NULL; |
1477 | 138k | buf = OPENSSL_malloc(return_size); |
1478 | 138k | if (buf == NULL) |
1479 | 0 | return 0; |
1480 | | |
1481 | 138k | if (!EVP_PKEY_get_octet_string_param(pkey, |
1482 | 138k | OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, |
1483 | 138k | buf, return_size, NULL)) { |
1484 | 0 | OPENSSL_free(buf); |
1485 | 0 | return 0; |
1486 | 0 | } |
1487 | 138k | *ppub = buf; |
1488 | 138k | return return_size; |
1489 | 138k | } |
1490 | | |
1491 | 0 | #ifndef FIPS_MODULE |
1492 | 0 | { |
1493 | 0 | int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub); |
1494 | 0 | if (rv <= 0) |
1495 | 0 | return 0; |
1496 | 0 | return rv; |
1497 | 0 | } |
1498 | 0 | #endif /* !FIPS_MODULE */ |
1499 | 0 | } |
1500 | | |
1501 | | /*- All methods below can also be used in FIPS_MODULE */ |
1502 | | |
1503 | | EVP_PKEY *EVP_PKEY_new(void) |
1504 | 1.78M | { |
1505 | 1.78M | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); |
1506 | | |
1507 | 1.78M | if (ret == NULL) |
1508 | 0 | return NULL; |
1509 | | |
1510 | 1.78M | ret->type = EVP_PKEY_NONE; |
1511 | 1.78M | ret->save_type = EVP_PKEY_NONE; |
1512 | | |
1513 | 1.78M | if (!CRYPTO_NEW_REF(&ret->references, 1)) |
1514 | 0 | goto err; |
1515 | | |
1516 | 1.78M | ret->lock = CRYPTO_THREAD_lock_new(); |
1517 | 1.78M | if (ret->lock == NULL) { |
1518 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
1519 | 0 | goto err; |
1520 | 0 | } |
1521 | | |
1522 | 1.78M | #ifndef FIPS_MODULE |
1523 | 1.78M | ret->save_parameters = 1; |
1524 | 1.78M | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) { |
1525 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
1526 | 0 | goto err; |
1527 | 0 | } |
1528 | 1.78M | #endif |
1529 | 1.78M | return ret; |
1530 | | |
1531 | 0 | err: |
1532 | 0 | CRYPTO_FREE_REF(&ret->references); |
1533 | 0 | CRYPTO_THREAD_lock_free(ret->lock); |
1534 | 0 | OPENSSL_free(ret); |
1535 | 0 | return NULL; |
1536 | 1.78M | } |
1537 | | |
1538 | | /* |
1539 | | * Setup a public key management method. |
1540 | | * |
1541 | | * For legacy keys, either |type| or |str| is expected to have the type |
1542 | | * information. In this case, the setup consists of finding an ASN1 method |
1543 | | * and potentially an ENGINE, and setting those fields in |pkey|. |
1544 | | * |
1545 | | * For provider side keys, |keymgmt| is expected to be non-NULL. In this |
1546 | | * case, the setup consists of setting the |keymgmt| field in |pkey|. |
1547 | | * |
1548 | | * If pkey is NULL just return 1 or 0 if the key management method exists. |
1549 | | */ |
1550 | | |
1551 | | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
1552 | | int len, EVP_KEYMGMT *keymgmt) |
1553 | 9.63M | { |
1554 | 9.63M | #ifndef FIPS_MODULE |
1555 | 9.63M | const EVP_PKEY_ASN1_METHOD *ameth = NULL; |
1556 | 9.63M | ENGINE **eptr = (e == NULL) ? &e : NULL; |
1557 | 9.63M | #endif |
1558 | | |
1559 | | /* |
1560 | | * The setups can't set both legacy and provider side methods. |
1561 | | * It is forbidden |
1562 | | */ |
1563 | 9.63M | if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL) |
1564 | 9.63M | || !ossl_assert(e == NULL || keymgmt == NULL)) { |
1565 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1566 | 0 | return 0; |
1567 | 0 | } |
1568 | | |
1569 | 9.63M | if (pkey != NULL) { |
1570 | 6.33M | int free_it = 0; |
1571 | | |
1572 | 6.33M | #ifndef FIPS_MODULE |
1573 | 6.33M | free_it = free_it || pkey->pkey.ptr != NULL; |
1574 | 6.33M | #endif |
1575 | 6.33M | free_it = free_it || pkey->keydata != NULL; |
1576 | 6.33M | if (free_it) |
1577 | 0 | evp_pkey_free_it(pkey); |
1578 | 6.33M | #ifndef FIPS_MODULE |
1579 | | /* |
1580 | | * If key type matches and a method exists then this lookup has |
1581 | | * succeeded once so just indicate success. |
1582 | | */ |
1583 | 6.33M | if (pkey->type != EVP_PKEY_NONE |
1584 | 6.33M | && type == pkey->save_type |
1585 | 6.33M | && pkey->ameth != NULL) |
1586 | 3.30M | return 1; |
1587 | 3.02M | # ifndef OPENSSL_NO_ENGINE |
1588 | | /* If we have ENGINEs release them */ |
1589 | 3.02M | ENGINE_finish(pkey->engine); |
1590 | 3.02M | pkey->engine = NULL; |
1591 | 3.02M | ENGINE_finish(pkey->pmeth_engine); |
1592 | 3.02M | pkey->pmeth_engine = NULL; |
1593 | 3.02M | # endif |
1594 | 3.02M | #endif |
1595 | 3.02M | } |
1596 | 6.33M | #ifndef FIPS_MODULE |
1597 | 6.33M | if (str != NULL) |
1598 | 4.09M | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); |
1599 | 2.24M | else if (type != EVP_PKEY_NONE) |
1600 | 2.15M | ameth = EVP_PKEY_asn1_find(eptr, type); |
1601 | 6.33M | # ifndef OPENSSL_NO_ENGINE |
1602 | 6.33M | if (pkey == NULL && eptr != NULL) |
1603 | 3.30M | ENGINE_finish(e); |
1604 | 6.33M | # endif |
1605 | 6.33M | #endif |
1606 | | |
1607 | | |
1608 | 6.33M | { |
1609 | 6.33M | int check = 1; |
1610 | | |
1611 | 6.33M | #ifndef FIPS_MODULE |
1612 | 6.33M | check = check && ameth == NULL; |
1613 | 6.33M | #endif |
1614 | 6.33M | check = check && keymgmt == NULL; |
1615 | 6.33M | if (check) { |
1616 | 2.52M | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
1617 | 2.52M | return 0; |
1618 | 2.52M | } |
1619 | 6.33M | } |
1620 | 3.81M | if (pkey != NULL) { |
1621 | 3.02M | if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) { |
1622 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1623 | 0 | return 0; |
1624 | 0 | } |
1625 | | |
1626 | 3.02M | pkey->keymgmt = keymgmt; |
1627 | | |
1628 | 3.02M | pkey->save_type = type; |
1629 | 3.02M | pkey->type = type; |
1630 | | |
1631 | 3.02M | #ifndef FIPS_MODULE |
1632 | | /* |
1633 | | * If the internal "origin" key is provider side, don't save |ameth|. |
1634 | | * The main reason is that |ameth| is one factor to detect that the |
1635 | | * internal "origin" key is a legacy one. |
1636 | | */ |
1637 | 3.02M | if (keymgmt == NULL) |
1638 | 2.15M | pkey->ameth = ameth; |
1639 | | |
1640 | | /* |
1641 | | * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose |
1642 | | * for any key type that has a legacy implementation, regardless of |
1643 | | * if the internal key is a legacy or a provider side one. When |
1644 | | * there is no legacy implementation for the key, the type becomes |
1645 | | * EVP_PKEY_KEYMGMT, which indicates that one should be cautious |
1646 | | * with functions that expect legacy internal keys. |
1647 | | */ |
1648 | 3.02M | if (ameth != NULL) { |
1649 | 2.93M | if (type == EVP_PKEY_NONE) |
1650 | 787k | pkey->type = ameth->pkey_id; |
1651 | 2.93M | } else { |
1652 | 88.0k | pkey->type = EVP_PKEY_KEYMGMT; |
1653 | 88.0k | } |
1654 | 3.02M | # ifndef OPENSSL_NO_ENGINE |
1655 | 3.02M | if (eptr == NULL && e != NULL && !ENGINE_init(e)) { |
1656 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
1657 | 0 | return 0; |
1658 | 0 | } |
1659 | 3.02M | # endif |
1660 | 3.02M | pkey->engine = e; |
1661 | 3.02M | #endif |
1662 | 3.02M | } |
1663 | 3.81M | return 1; |
1664 | 3.81M | } |
1665 | | |
1666 | | #ifndef FIPS_MODULE |
1667 | | static void find_ameth(const char *name, void *data) |
1668 | 3.30M | { |
1669 | 3.30M | const char **str = data; |
1670 | | |
1671 | | /* |
1672 | | * The error messages from pkey_set_type() are uninteresting here, |
1673 | | * and misleading. |
1674 | | */ |
1675 | 3.30M | ERR_set_mark(); |
1676 | | |
1677 | 3.30M | if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name), |
1678 | 3.30M | NULL)) { |
1679 | 787k | if (str[0] == NULL) |
1680 | 787k | str[0] = name; |
1681 | 0 | else if (str[1] == NULL) |
1682 | 0 | str[1] = name; |
1683 | 787k | } |
1684 | | |
1685 | 3.30M | ERR_pop_to_mark(); |
1686 | 3.30M | } |
1687 | | #endif |
1688 | | |
1689 | | int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt) |
1690 | 875k | { |
1691 | 875k | #ifndef FIPS_MODULE |
1692 | 875k | # define EVP_PKEY_TYPE_STR str[0] |
1693 | 875k | # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0])) |
1694 | | /* |
1695 | | * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD |
1696 | | * Ideally, only one should be found. If two (or more) are found, the |
1697 | | * match is ambiguous. This should never happen, but... |
1698 | | */ |
1699 | 875k | const char *str[2] = { NULL, NULL }; |
1700 | | |
1701 | 875k | if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str) |
1702 | 875k | || str[1] != NULL) { |
1703 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
1704 | 0 | return 0; |
1705 | 0 | } |
1706 | | #else |
1707 | | # define EVP_PKEY_TYPE_STR NULL |
1708 | | # define EVP_PKEY_TYPE_STRLEN -1 |
1709 | | #endif |
1710 | 875k | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, |
1711 | 875k | EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN, |
1712 | 875k | keymgmt); |
1713 | | |
1714 | 875k | #undef EVP_PKEY_TYPE_STR |
1715 | 875k | #undef EVP_PKEY_TYPE_STRLEN |
1716 | 875k | } |
1717 | | |
1718 | | int EVP_PKEY_up_ref(EVP_PKEY *pkey) |
1719 | 5.75M | { |
1720 | 5.75M | int i; |
1721 | | |
1722 | 5.75M | if (CRYPTO_UP_REF(&pkey->references, &i) <= 0) |
1723 | 0 | return 0; |
1724 | | |
1725 | 5.75M | REF_PRINT_COUNT("EVP_PKEY", i, pkey); |
1726 | 5.75M | REF_ASSERT_ISNT(i < 2); |
1727 | 5.75M | return ((i > 1) ? 1 : 0); |
1728 | 5.75M | } |
1729 | | |
1730 | | EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) |
1731 | 19.2k | { |
1732 | 19.2k | EVP_PKEY *dup_pk; |
1733 | | |
1734 | 19.2k | if (pkey == NULL) { |
1735 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER); |
1736 | 0 | return NULL; |
1737 | 0 | } |
1738 | | |
1739 | 19.2k | if ((dup_pk = EVP_PKEY_new()) == NULL) |
1740 | 0 | return NULL; |
1741 | | |
1742 | 19.2k | if (evp_pkey_is_blank(pkey)) |
1743 | 0 | goto done; |
1744 | | |
1745 | 19.2k | #ifndef FIPS_MODULE |
1746 | 19.2k | if (evp_pkey_is_provided(pkey)) |
1747 | 19.2k | #endif /* !FIPS_MODULE */ |
1748 | 19.2k | { |
1749 | 19.2k | if (!evp_keymgmt_util_copy(dup_pk, pkey, |
1750 | 19.2k | OSSL_KEYMGMT_SELECT_ALL)) |
1751 | 0 | goto err; |
1752 | 19.2k | goto done; |
1753 | 19.2k | } |
1754 | | |
1755 | 0 | #ifndef FIPS_MODULE |
1756 | 0 | if (evp_pkey_is_legacy(pkey)) { |
1757 | 0 | const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth; |
1758 | |
|
1759 | 0 | if (ameth == NULL || ameth->copy == NULL) { |
1760 | 0 | if (pkey->pkey.ptr == NULL /* empty key, just set type */ |
1761 | 0 | && EVP_PKEY_set_type(dup_pk, pkey->type) != 0) |
1762 | 0 | goto done; |
1763 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE); |
1764 | 0 | goto err; |
1765 | 0 | } |
1766 | 0 | if (!ameth->copy(dup_pk, pkey)) |
1767 | 0 | goto err; |
1768 | 0 | goto done; |
1769 | 0 | } |
1770 | 0 | #endif /* !FIPS_MODULE */ |
1771 | | |
1772 | 0 | goto err; |
1773 | 19.2k | done: |
1774 | 19.2k | #ifndef FIPS_MODULE |
1775 | | /* copy auxiliary data */ |
1776 | 19.2k | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, |
1777 | 19.2k | &dup_pk->ex_data, &pkey->ex_data)) |
1778 | 0 | goto err; |
1779 | | |
1780 | 19.2k | if (pkey->attributes != NULL) { |
1781 | 0 | if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL) |
1782 | 0 | goto err; |
1783 | 0 | } |
1784 | 19.2k | #endif /* !FIPS_MODULE */ |
1785 | 19.2k | return dup_pk; |
1786 | 0 | err: |
1787 | 0 | EVP_PKEY_free(dup_pk); |
1788 | 0 | return NULL; |
1789 | 19.2k | } |
1790 | | |
1791 | | #ifndef FIPS_MODULE |
1792 | | void evp_pkey_free_legacy(EVP_PKEY *x) |
1793 | 2.35M | { |
1794 | 2.35M | const EVP_PKEY_ASN1_METHOD *ameth = x->ameth; |
1795 | 2.35M | ENGINE *tmpe = NULL; |
1796 | | |
1797 | 2.35M | if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL) |
1798 | 101k | ameth = EVP_PKEY_asn1_find(&tmpe, x->type); |
1799 | | |
1800 | 2.35M | if (ameth != NULL) { |
1801 | 1.36M | if (x->legacy_cache_pkey.ptr != NULL) { |
1802 | | /* |
1803 | | * We should never have both a legacy origin key, and a key in the |
1804 | | * legacy cache. |
1805 | | */ |
1806 | 101k | assert(x->pkey.ptr == NULL); |
1807 | | /* |
1808 | | * For the purposes of freeing we make the legacy cache look like |
1809 | | * a legacy origin key. |
1810 | | */ |
1811 | 101k | x->pkey = x->legacy_cache_pkey; |
1812 | 101k | x->legacy_cache_pkey.ptr = NULL; |
1813 | 101k | } |
1814 | 1.36M | if (ameth->pkey_free != NULL) |
1815 | 1.36M | ameth->pkey_free(x); |
1816 | 1.36M | x->pkey.ptr = NULL; |
1817 | 1.36M | } |
1818 | 2.35M | # ifndef OPENSSL_NO_ENGINE |
1819 | 2.35M | ENGINE_finish(tmpe); |
1820 | 2.35M | ENGINE_finish(x->engine); |
1821 | 2.35M | x->engine = NULL; |
1822 | 2.35M | ENGINE_finish(x->pmeth_engine); |
1823 | 2.35M | x->pmeth_engine = NULL; |
1824 | 2.35M | # endif |
1825 | 2.35M | } |
1826 | | #endif /* FIPS_MODULE */ |
1827 | | |
1828 | | static void evp_pkey_free_it(EVP_PKEY *x) |
1829 | 2.13M | { |
1830 | | /* internal function; x is never NULL */ |
1831 | 2.13M | evp_keymgmt_util_clear_operation_cache(x); |
1832 | 2.13M | #ifndef FIPS_MODULE |
1833 | 2.13M | evp_pkey_free_legacy(x); |
1834 | 2.13M | #endif |
1835 | | |
1836 | 2.13M | if (x->keymgmt != NULL) { |
1837 | 875k | evp_keymgmt_freedata(x->keymgmt, x->keydata); |
1838 | 875k | EVP_KEYMGMT_free(x->keymgmt); |
1839 | 875k | x->keymgmt = NULL; |
1840 | 875k | x->keydata = NULL; |
1841 | 875k | } |
1842 | 2.13M | x->type = EVP_PKEY_NONE; |
1843 | 2.13M | } |
1844 | | |
1845 | | void EVP_PKEY_free(EVP_PKEY *x) |
1846 | 21.1M | { |
1847 | 21.1M | int i; |
1848 | | |
1849 | 21.1M | if (x == NULL) |
1850 | 13.2M | return; |
1851 | | |
1852 | 7.89M | CRYPTO_DOWN_REF(&x->references, &i); |
1853 | 7.89M | REF_PRINT_COUNT("EVP_PKEY", i, x); |
1854 | 7.89M | if (i > 0) |
1855 | 5.75M | return; |
1856 | 2.13M | REF_ASSERT_ISNT(i < 0); |
1857 | 2.13M | evp_pkey_free_it(x); |
1858 | 2.13M | #ifndef FIPS_MODULE |
1859 | 2.13M | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data); |
1860 | 2.13M | #endif |
1861 | 2.13M | CRYPTO_THREAD_lock_free(x->lock); |
1862 | 2.13M | CRYPTO_FREE_REF(&x->references); |
1863 | 2.13M | #ifndef FIPS_MODULE |
1864 | 2.13M | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); |
1865 | 2.13M | #endif |
1866 | 2.13M | OPENSSL_free(x); |
1867 | 2.13M | } |
1868 | | |
1869 | | int EVP_PKEY_get_size(const EVP_PKEY *pkey) |
1870 | 4.34k | { |
1871 | 4.34k | int size = 0; |
1872 | | |
1873 | 4.34k | if (pkey != NULL) { |
1874 | 4.34k | size = pkey->cache.size; |
1875 | 4.34k | #ifndef FIPS_MODULE |
1876 | 4.34k | if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL) |
1877 | 1.16k | size = pkey->ameth->pkey_size(pkey); |
1878 | 4.34k | #endif |
1879 | 4.34k | } |
1880 | 4.34k | if (size <= 0) { |
1881 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE); |
1882 | 0 | return 0; |
1883 | 0 | } |
1884 | 4.34k | return size; |
1885 | 4.34k | } |
1886 | | |
1887 | | const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey) |
1888 | 0 | { |
1889 | 0 | if (!evp_pkey_is_assigned(pkey)) |
1890 | 0 | return NULL; |
1891 | | |
1892 | 0 | if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL) |
1893 | 0 | return pkey->keymgmt->description; |
1894 | 0 | #ifndef FIPS_MODULE |
1895 | 0 | if (pkey->ameth != NULL) |
1896 | 0 | return pkey->ameth->info; |
1897 | 0 | #endif |
1898 | 0 | return NULL; |
1899 | 0 | } |
1900 | | |
1901 | | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx, |
1902 | | EVP_KEYMGMT **keymgmt, |
1903 | | const char *propquery) |
1904 | 592k | { |
1905 | 592k | EVP_KEYMGMT *allocated_keymgmt = NULL; |
1906 | 592k | EVP_KEYMGMT *tmp_keymgmt = NULL; |
1907 | 592k | int selection = OSSL_KEYMGMT_SELECT_ALL; |
1908 | 592k | void *keydata = NULL; |
1909 | 592k | int check; |
1910 | | |
1911 | 592k | if (pk == NULL) |
1912 | 0 | return NULL; |
1913 | | |
1914 | | /* No key data => nothing to export */ |
1915 | 592k | check = 1; |
1916 | 592k | #ifndef FIPS_MODULE |
1917 | 592k | check = check && pk->pkey.ptr == NULL; |
1918 | 592k | #endif |
1919 | 592k | check = check && pk->keydata == NULL; |
1920 | 592k | if (check) |
1921 | 0 | return NULL; |
1922 | | |
1923 | 592k | #ifndef FIPS_MODULE |
1924 | 592k | if (pk->pkey.ptr != NULL) { |
1925 | | /* |
1926 | | * If the legacy key doesn't have an dirty counter or export function, |
1927 | | * give up |
1928 | | */ |
1929 | 322k | if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL) |
1930 | 0 | return NULL; |
1931 | 322k | } |
1932 | 592k | #endif |
1933 | | |
1934 | 592k | if (keymgmt != NULL) { |
1935 | 592k | tmp_keymgmt = *keymgmt; |
1936 | 592k | *keymgmt = NULL; |
1937 | 592k | } |
1938 | | |
1939 | | /* |
1940 | | * If no keymgmt was given or found, get a default keymgmt. We do so by |
1941 | | * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it. |
1942 | | */ |
1943 | 592k | if (tmp_keymgmt == NULL) { |
1944 | 0 | EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery); |
1945 | |
|
1946 | 0 | if (ctx == NULL) |
1947 | 0 | goto end; |
1948 | 0 | allocated_keymgmt = tmp_keymgmt = ctx->keymgmt; |
1949 | 0 | ctx->keymgmt = NULL; |
1950 | 0 | EVP_PKEY_CTX_free(ctx); |
1951 | 0 | } |
1952 | | |
1953 | | /* If there's still no keymgmt to be had, give up */ |
1954 | 592k | if (tmp_keymgmt == NULL) |
1955 | 0 | goto end; |
1956 | | |
1957 | 592k | #ifndef FIPS_MODULE |
1958 | 592k | if (pk->pkey.ptr != NULL) { |
1959 | 322k | OP_CACHE_ELEM *op; |
1960 | | |
1961 | | /* |
1962 | | * If the legacy "origin" hasn't changed since last time, we try |
1963 | | * to find our keymgmt in the operation cache. If it has changed, |
1964 | | * |i| remains zero, and we will clear the cache further down. |
1965 | | */ |
1966 | 322k | if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) { |
1967 | 220k | if (!CRYPTO_THREAD_read_lock(pk->lock)) |
1968 | 0 | goto end; |
1969 | 220k | op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, |
1970 | 220k | selection); |
1971 | | |
1972 | | /* |
1973 | | * If |tmp_keymgmt| is present in the operation cache, it means |
1974 | | * that export doesn't need to be redone. In that case, we take |
1975 | | * token copies of the cached pointers, to have token success |
1976 | | * values to return. It is possible (e.g. in a no-cached-fetch |
1977 | | * build), for op->keymgmt to be a different pointer to tmp_keymgmt |
1978 | | * even though the name/provider must be the same. In other words |
1979 | | * the keymgmt instance may be different but still equivalent, i.e. |
1980 | | * same algorithm/provider instance - but we make the simplifying |
1981 | | * assumption that the keydata can be used with either keymgmt |
1982 | | * instance. Not doing so introduces significant complexity and |
1983 | | * probably requires refactoring - since we would have to ripple |
1984 | | * the change in keymgmt instance up the call chain. |
1985 | | */ |
1986 | 220k | if (op != NULL && op->keymgmt != NULL) { |
1987 | 191k | keydata = op->keydata; |
1988 | 191k | CRYPTO_THREAD_unlock(pk->lock); |
1989 | 191k | goto end; |
1990 | 191k | } |
1991 | 29.1k | CRYPTO_THREAD_unlock(pk->lock); |
1992 | 29.1k | } |
1993 | | |
1994 | | /* Make sure that the keymgmt key type matches the legacy NID */ |
1995 | 131k | if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))) |
1996 | 0 | goto end; |
1997 | | |
1998 | 131k | if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL) |
1999 | 0 | goto end; |
2000 | | |
2001 | 131k | if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import, |
2002 | 131k | libctx, propquery)) { |
2003 | 0 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
2004 | 0 | keydata = NULL; |
2005 | 0 | goto end; |
2006 | 0 | } |
2007 | | |
2008 | | /* |
2009 | | * If the dirty counter changed since last time, then clear the |
2010 | | * operation cache. In that case, we know that |i| is zero. Just |
2011 | | * in case this is a re-export, we increment then decrement the |
2012 | | * keymgmt reference counter. |
2013 | | */ |
2014 | 131k | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */ |
2015 | 0 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
2016 | 0 | keydata = NULL; |
2017 | 0 | goto end; |
2018 | 0 | } |
2019 | | |
2020 | 131k | if (!CRYPTO_THREAD_write_lock(pk->lock)) |
2021 | 0 | goto end; |
2022 | 131k | if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy |
2023 | 131k | && !evp_keymgmt_util_clear_operation_cache(pk)) { |
2024 | 0 | CRYPTO_THREAD_unlock(pk->lock); |
2025 | 0 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
2026 | 0 | keydata = NULL; |
2027 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
2028 | 0 | goto end; |
2029 | 0 | } |
2030 | 131k | EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */ |
2031 | | |
2032 | | /* Check to make sure some other thread didn't get there first */ |
2033 | 131k | op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection); |
2034 | 131k | if (op != NULL && op->keymgmt != NULL) { |
2035 | 0 | void *tmp_keydata = op->keydata; |
2036 | |
|
2037 | 0 | CRYPTO_THREAD_unlock(pk->lock); |
2038 | 0 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
2039 | 0 | keydata = tmp_keydata; |
2040 | 0 | goto end; |
2041 | 0 | } |
2042 | | |
2043 | | /* Add the new export to the operation cache */ |
2044 | 131k | if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata, |
2045 | 131k | selection)) { |
2046 | 0 | CRYPTO_THREAD_unlock(pk->lock); |
2047 | 0 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
2048 | 0 | keydata = NULL; |
2049 | 0 | goto end; |
2050 | 0 | } |
2051 | | |
2052 | | /* Synchronize the dirty count */ |
2053 | 131k | pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk); |
2054 | | |
2055 | 131k | CRYPTO_THREAD_unlock(pk->lock); |
2056 | 131k | goto end; |
2057 | 131k | } |
2058 | 270k | #endif /* FIPS_MODULE */ |
2059 | | |
2060 | 270k | keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection); |
2061 | | |
2062 | 592k | end: |
2063 | | /* |
2064 | | * If nothing was exported, |tmp_keymgmt| might point at a freed |
2065 | | * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for |
2066 | | * the caller either way in that case. |
2067 | | */ |
2068 | 592k | if (keydata == NULL) |
2069 | 0 | tmp_keymgmt = NULL; |
2070 | | |
2071 | 592k | if (keymgmt != NULL && tmp_keymgmt != NULL) { |
2072 | 592k | *keymgmt = tmp_keymgmt; |
2073 | 592k | allocated_keymgmt = NULL; |
2074 | 592k | } |
2075 | | |
2076 | 592k | EVP_KEYMGMT_free(allocated_keymgmt); |
2077 | 592k | return keydata; |
2078 | 270k | } |
2079 | | |
2080 | | #ifndef FIPS_MODULE |
2081 | | int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src) |
2082 | 101k | { |
2083 | 101k | EVP_PKEY *allocpkey = NULL; |
2084 | | |
2085 | 101k | if (!ossl_assert(dest != NULL)) |
2086 | 0 | return 0; |
2087 | | |
2088 | 101k | if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) { |
2089 | 101k | EVP_KEYMGMT *keymgmt = src->keymgmt; |
2090 | 101k | void *keydata = src->keydata; |
2091 | 101k | int type = src->type; |
2092 | 101k | const char *keytype = NULL; |
2093 | | |
2094 | 101k | keytype = EVP_KEYMGMT_get0_name(keymgmt); |
2095 | | |
2096 | | /* |
2097 | | * If the type is EVP_PKEY_NONE, then we have a problem somewhere |
2098 | | * else in our code. If it's not one of the well known EVP_PKEY_xxx |
2099 | | * values, it should at least be EVP_PKEY_KEYMGMT at this point. |
2100 | | * The check is kept as a safety measure. |
2101 | | */ |
2102 | 101k | if (!ossl_assert(type != EVP_PKEY_NONE)) { |
2103 | 0 | ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR, |
2104 | 0 | "keymgmt key type = %s but legacy type = EVP_PKEY_NONE", |
2105 | 0 | keytype); |
2106 | 0 | return 0; |
2107 | 0 | } |
2108 | | |
2109 | | /* Prefer the legacy key type name for error reporting */ |
2110 | 101k | if (type != EVP_PKEY_KEYMGMT) |
2111 | 101k | keytype = OBJ_nid2sn(type); |
2112 | | |
2113 | | /* Make sure we have a clean slate to copy into */ |
2114 | 101k | if (*dest == NULL) { |
2115 | 101k | allocpkey = *dest = EVP_PKEY_new(); |
2116 | 101k | if (*dest == NULL) { |
2117 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
2118 | 0 | return 0; |
2119 | 0 | } |
2120 | 101k | } else { |
2121 | 0 | evp_pkey_free_it(*dest); |
2122 | 0 | } |
2123 | | |
2124 | 101k | if (EVP_PKEY_set_type(*dest, type)) { |
2125 | | /* If the key is typed but empty, we're done */ |
2126 | 101k | if (keydata == NULL) |
2127 | 0 | return 1; |
2128 | | |
2129 | 101k | if ((*dest)->ameth->import_from == NULL) { |
2130 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION, |
2131 | 0 | "key type = %s", keytype); |
2132 | 101k | } else { |
2133 | | /* |
2134 | | * We perform the export in the same libctx as the keymgmt |
2135 | | * that we are using. |
2136 | | */ |
2137 | 101k | OSSL_LIB_CTX *libctx = |
2138 | 101k | ossl_provider_libctx(keymgmt->prov); |
2139 | 101k | EVP_PKEY_CTX *pctx = |
2140 | 101k | EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL); |
2141 | | |
2142 | 101k | if (pctx == NULL) |
2143 | 101k | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
2144 | | |
2145 | 101k | if (pctx != NULL |
2146 | 101k | && evp_keymgmt_export(keymgmt, keydata, |
2147 | 101k | OSSL_KEYMGMT_SELECT_ALL, |
2148 | 101k | (*dest)->ameth->import_from, |
2149 | 101k | pctx)) { |
2150 | | /* Synchronize the dirty count */ |
2151 | 101k | (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest); |
2152 | | |
2153 | 101k | EVP_PKEY_CTX_free(pctx); |
2154 | 101k | return 1; |
2155 | 101k | } |
2156 | 0 | EVP_PKEY_CTX_free(pctx); |
2157 | 0 | } |
2158 | | |
2159 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE, |
2160 | 0 | "key type = %s", keytype); |
2161 | 0 | } |
2162 | 101k | } |
2163 | | |
2164 | 0 | if (allocpkey != NULL) { |
2165 | 0 | EVP_PKEY_free(allocpkey); |
2166 | 0 | *dest = NULL; |
2167 | 0 | } |
2168 | 0 | return 0; |
2169 | 101k | } |
2170 | | |
2171 | | void *evp_pkey_get_legacy(EVP_PKEY *pk) |
2172 | 560k | { |
2173 | 560k | EVP_PKEY *tmp_copy = NULL; |
2174 | 560k | void *ret = NULL; |
2175 | | |
2176 | 560k | if (!ossl_assert(pk != NULL)) |
2177 | 0 | return NULL; |
2178 | | |
2179 | | /* |
2180 | | * If this isn't an assigned provider side key, we just use any existing |
2181 | | * origin legacy key. |
2182 | | */ |
2183 | 560k | if (!evp_pkey_is_assigned(pk)) |
2184 | 0 | return NULL; |
2185 | 560k | if (!evp_pkey_is_provided(pk)) |
2186 | 458k | return pk->pkey.ptr; |
2187 | | |
2188 | 101k | if (!CRYPTO_THREAD_read_lock(pk->lock)) |
2189 | 0 | return NULL; |
2190 | | |
2191 | 101k | ret = pk->legacy_cache_pkey.ptr; |
2192 | | |
2193 | 101k | if (!CRYPTO_THREAD_unlock(pk->lock)) |
2194 | 0 | return NULL; |
2195 | | |
2196 | 101k | if (ret != NULL) |
2197 | 0 | return ret; |
2198 | | |
2199 | 101k | if (!evp_pkey_copy_downgraded(&tmp_copy, pk)) |
2200 | 0 | goto err; |
2201 | | |
2202 | 101k | if (!CRYPTO_THREAD_write_lock(pk->lock)) |
2203 | 0 | goto err; |
2204 | | |
2205 | | /* Check again in case some other thread has updated it in the meantime */ |
2206 | 101k | ret = pk->legacy_cache_pkey.ptr; |
2207 | 101k | if (ret == NULL) { |
2208 | | /* Steal the legacy key reference from the temporary copy */ |
2209 | 101k | ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr; |
2210 | 101k | tmp_copy->pkey.ptr = NULL; |
2211 | 101k | } |
2212 | | |
2213 | 101k | if (!CRYPTO_THREAD_unlock(pk->lock)) { |
2214 | 0 | ret = NULL; |
2215 | 0 | goto err; |
2216 | 0 | } |
2217 | | |
2218 | 101k | err: |
2219 | 101k | EVP_PKEY_free(tmp_copy); |
2220 | | |
2221 | 101k | return ret; |
2222 | 101k | } |
2223 | | #endif /* FIPS_MODULE */ |
2224 | | |
2225 | | int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name, |
2226 | | BIGNUM **bn) |
2227 | 0 | { |
2228 | 0 | int ret = 0; |
2229 | 0 | OSSL_PARAM params[2]; |
2230 | 0 | unsigned char buffer[2048]; |
2231 | 0 | unsigned char *buf = NULL; |
2232 | 0 | size_t buf_sz = 0; |
2233 | |
|
2234 | 0 | if (key_name == NULL |
2235 | 0 | || bn == NULL) |
2236 | 0 | return 0; |
2237 | | |
2238 | 0 | memset(buffer, 0, sizeof(buffer)); |
2239 | 0 | params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer)); |
2240 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2241 | 0 | if (!EVP_PKEY_get_params(pkey, params)) { |
2242 | 0 | if (!OSSL_PARAM_modified(params) || params[0].return_size == 0) |
2243 | 0 | return 0; |
2244 | 0 | buf_sz = params[0].return_size; |
2245 | | /* |
2246 | | * If it failed because the buffer was too small then allocate the |
2247 | | * required buffer size and retry. |
2248 | | */ |
2249 | 0 | buf = OPENSSL_zalloc(buf_sz); |
2250 | 0 | if (buf == NULL) |
2251 | 0 | return 0; |
2252 | 0 | params[0].data = buf; |
2253 | 0 | params[0].data_size = buf_sz; |
2254 | |
|
2255 | 0 | if (!EVP_PKEY_get_params(pkey, params)) |
2256 | 0 | goto err; |
2257 | 0 | } |
2258 | | /* Fail if the param was not found */ |
2259 | 0 | if (!OSSL_PARAM_modified(params)) |
2260 | 0 | goto err; |
2261 | 0 | ret = OSSL_PARAM_get_BN(params, bn); |
2262 | 0 | err: |
2263 | 0 | if (buf != NULL) { |
2264 | 0 | if (OSSL_PARAM_modified(params)) |
2265 | 0 | OPENSSL_clear_free(buf, buf_sz); |
2266 | 0 | else |
2267 | 0 | OPENSSL_free(buf); |
2268 | 0 | } else if (OSSL_PARAM_modified(params)) { |
2269 | 0 | OPENSSL_cleanse(buffer, params[0].data_size); |
2270 | 0 | } |
2271 | 0 | return ret; |
2272 | 0 | } |
2273 | | |
2274 | | int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name, |
2275 | | unsigned char *buf, size_t max_buf_sz, |
2276 | | size_t *out_len) |
2277 | 276k | { |
2278 | 276k | OSSL_PARAM params[2]; |
2279 | 276k | int ret1 = 0, ret2 = 0; |
2280 | | |
2281 | 276k | if (key_name == NULL) |
2282 | 0 | return 0; |
2283 | | |
2284 | 276k | params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz); |
2285 | 276k | params[1] = OSSL_PARAM_construct_end(); |
2286 | 276k | if ((ret1 = EVP_PKEY_get_params(pkey, params))) |
2287 | 276k | ret2 = OSSL_PARAM_modified(params); |
2288 | 276k | if (ret2 && out_len != NULL) |
2289 | 138k | *out_len = params[0].return_size; |
2290 | 276k | return ret1 && ret2; |
2291 | 276k | } |
2292 | | |
2293 | | int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name, |
2294 | | char *str, size_t max_buf_sz, |
2295 | | size_t *out_len) |
2296 | 50.6k | { |
2297 | 50.6k | OSSL_PARAM params[2]; |
2298 | 50.6k | int ret1 = 0, ret2 = 0; |
2299 | | |
2300 | 50.6k | if (key_name == NULL) |
2301 | 0 | return 0; |
2302 | | |
2303 | 50.6k | params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz); |
2304 | 50.6k | params[1] = OSSL_PARAM_construct_end(); |
2305 | 50.6k | if ((ret1 = EVP_PKEY_get_params(pkey, params))) |
2306 | 50.6k | ret2 = OSSL_PARAM_modified(params); |
2307 | 50.6k | if (ret2 && out_len != NULL) |
2308 | 24.2k | *out_len = params[0].return_size; |
2309 | | |
2310 | 50.6k | if (ret2 && params[0].return_size == max_buf_sz) |
2311 | | /* There was no space for a NUL byte */ |
2312 | 0 | return 0; |
2313 | | /* Add a terminating NUL byte for good measure */ |
2314 | 50.6k | if (ret2 && str != NULL) |
2315 | 50.6k | str[params[0].return_size] = '\0'; |
2316 | | |
2317 | 50.6k | return ret1 && ret2; |
2318 | 50.6k | } |
2319 | | |
2320 | | int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name, |
2321 | | int *out) |
2322 | 2 | { |
2323 | 2 | OSSL_PARAM params[2]; |
2324 | | |
2325 | 2 | if (key_name == NULL) |
2326 | 0 | return 0; |
2327 | | |
2328 | 2 | params[0] = OSSL_PARAM_construct_int(key_name, out); |
2329 | 2 | params[1] = OSSL_PARAM_construct_end(); |
2330 | 2 | return EVP_PKEY_get_params(pkey, params) |
2331 | 2 | && OSSL_PARAM_modified(params); |
2332 | 2 | } |
2333 | | |
2334 | | int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name, |
2335 | | size_t *out) |
2336 | 0 | { |
2337 | 0 | OSSL_PARAM params[2]; |
2338 | |
|
2339 | 0 | if (key_name == NULL) |
2340 | 0 | return 0; |
2341 | | |
2342 | 0 | params[0] = OSSL_PARAM_construct_size_t(key_name, out); |
2343 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2344 | 0 | return EVP_PKEY_get_params(pkey, params) |
2345 | 0 | && OSSL_PARAM_modified(params); |
2346 | 0 | } |
2347 | | |
2348 | | int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in) |
2349 | 0 | { |
2350 | 0 | OSSL_PARAM params[2]; |
2351 | |
|
2352 | 0 | if (key_name == NULL) |
2353 | 0 | return 0; |
2354 | | |
2355 | 0 | params[0] = OSSL_PARAM_construct_int(key_name, &in); |
2356 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2357 | 0 | return EVP_PKEY_set_params(pkey, params); |
2358 | 0 | } |
2359 | | |
2360 | | int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in) |
2361 | 0 | { |
2362 | 0 | OSSL_PARAM params[2]; |
2363 | |
|
2364 | 0 | if (key_name == NULL) |
2365 | 0 | return 0; |
2366 | | |
2367 | 0 | params[0] = OSSL_PARAM_construct_size_t(key_name, &in); |
2368 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2369 | 0 | return EVP_PKEY_set_params(pkey, params); |
2370 | 0 | } |
2371 | | |
2372 | | int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name, |
2373 | | const BIGNUM *bn) |
2374 | 0 | { |
2375 | 0 | OSSL_PARAM params[2]; |
2376 | 0 | unsigned char buffer[2048]; |
2377 | 0 | int bsize = 0; |
2378 | |
|
2379 | 0 | if (key_name == NULL |
2380 | 0 | || bn == NULL |
2381 | 0 | || pkey == NULL |
2382 | 0 | || !evp_pkey_is_assigned(pkey)) |
2383 | 0 | return 0; |
2384 | | |
2385 | 0 | bsize = BN_num_bytes(bn); |
2386 | 0 | if (!ossl_assert(bsize <= (int)sizeof(buffer))) |
2387 | 0 | return 0; |
2388 | | |
2389 | 0 | if (BN_bn2nativepad(bn, buffer, bsize) < 0) |
2390 | 0 | return 0; |
2391 | 0 | params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize); |
2392 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2393 | 0 | return EVP_PKEY_set_params(pkey, params); |
2394 | 0 | } |
2395 | | |
2396 | | int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name, |
2397 | | const char *str) |
2398 | 0 | { |
2399 | 0 | OSSL_PARAM params[2]; |
2400 | |
|
2401 | 0 | if (key_name == NULL) |
2402 | 0 | return 0; |
2403 | | |
2404 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0); |
2405 | 0 | params[1] = OSSL_PARAM_construct_end(); |
2406 | 0 | return EVP_PKEY_set_params(pkey, params); |
2407 | 0 | } |
2408 | | |
2409 | | int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name, |
2410 | | const unsigned char *buf, size_t bsize) |
2411 | 43.2k | { |
2412 | 43.2k | OSSL_PARAM params[2]; |
2413 | | |
2414 | 43.2k | if (key_name == NULL) |
2415 | 0 | return 0; |
2416 | | |
2417 | 43.2k | params[0] = OSSL_PARAM_construct_octet_string(key_name, |
2418 | 43.2k | (unsigned char *)buf, bsize); |
2419 | 43.2k | params[1] = OSSL_PARAM_construct_end(); |
2420 | 43.2k | return EVP_PKEY_set_params(pkey, params); |
2421 | 43.2k | } |
2422 | | |
2423 | | const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey) |
2424 | 0 | { |
2425 | 0 | return (pkey != NULL && evp_pkey_is_provided(pkey)) |
2426 | 0 | ? EVP_KEYMGMT_settable_params(pkey->keymgmt) |
2427 | 0 | : NULL; |
2428 | 0 | } |
2429 | | |
2430 | | int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[]) |
2431 | 43.2k | { |
2432 | 43.2k | if (pkey != NULL) { |
2433 | 43.2k | if (evp_pkey_is_provided(pkey)) { |
2434 | 43.2k | pkey->dirty_cnt++; |
2435 | 43.2k | return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params); |
2436 | 43.2k | } |
2437 | 43.2k | #ifndef FIPS_MODULE |
2438 | | /* |
2439 | | * We will hopefully never find the need to set individual data in |
2440 | | * EVP_PKEYs with a legacy internal key, but we can't be entirely |
2441 | | * sure. This bit of code can be enabled if we find the need. If |
2442 | | * not, it can safely be removed when #legacy support is removed. |
2443 | | */ |
2444 | | # if 0 |
2445 | | else if (evp_pkey_is_legacy(pkey)) { |
2446 | | return evp_pkey_set_params_to_ctrl(pkey, params); |
2447 | | } |
2448 | | # endif |
2449 | 43.2k | #endif |
2450 | 43.2k | } |
2451 | 43.2k | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); |
2452 | 0 | return 0; |
2453 | 43.2k | } |
2454 | | |
2455 | | const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey) |
2456 | 0 | { |
2457 | 0 | return (pkey != NULL && evp_pkey_is_provided(pkey)) |
2458 | 0 | ? EVP_KEYMGMT_gettable_params(pkey->keymgmt) |
2459 | 0 | : NULL; |
2460 | 0 | } |
2461 | | |
2462 | | int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[]) |
2463 | 327k | { |
2464 | 327k | if (pkey != NULL) { |
2465 | 327k | if (evp_pkey_is_provided(pkey)) |
2466 | 324k | return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0; |
2467 | 2.93k | #ifndef FIPS_MODULE |
2468 | 2.93k | else if (evp_pkey_is_legacy(pkey)) |
2469 | 2.93k | return evp_pkey_get_params_to_ctrl(pkey, params) > 0; |
2470 | 327k | #endif |
2471 | 327k | } |
2472 | 327k | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY); |
2473 | 0 | return 0; |
2474 | 327k | } |
2475 | | |
2476 | | #ifndef FIPS_MODULE |
2477 | | int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey) |
2478 | 24.1k | { |
2479 | 24.1k | char name[80]; |
2480 | 24.1k | size_t name_len; |
2481 | | |
2482 | 24.1k | if (pkey == NULL) |
2483 | 0 | return 0; |
2484 | | |
2485 | 24.1k | if (pkey->keymgmt == NULL |
2486 | 24.1k | || pkey->keydata == NULL) { |
2487 | 0 | # ifndef OPENSSL_NO_EC |
2488 | | /* Might work through the legacy route */ |
2489 | 0 | const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
2490 | |
|
2491 | 0 | if (ec == NULL) |
2492 | 0 | return 0; |
2493 | | |
2494 | 0 | return EC_KEY_get_conv_form(ec); |
2495 | | # else |
2496 | | return 0; |
2497 | | # endif |
2498 | 0 | } |
2499 | | |
2500 | 24.1k | if (!EVP_PKEY_get_utf8_string_param(pkey, |
2501 | 24.1k | OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT, |
2502 | 24.1k | name, sizeof(name), &name_len)) |
2503 | 0 | return 0; |
2504 | | |
2505 | 24.1k | if (strcmp(name, "uncompressed") == 0) |
2506 | 24.0k | return POINT_CONVERSION_UNCOMPRESSED; |
2507 | | |
2508 | 88 | if (strcmp(name, "compressed") == 0) |
2509 | 15 | return POINT_CONVERSION_COMPRESSED; |
2510 | | |
2511 | 73 | if (strcmp(name, "hybrid") == 0) |
2512 | 73 | return POINT_CONVERSION_HYBRID; |
2513 | | |
2514 | 0 | return 0; |
2515 | 73 | } |
2516 | | |
2517 | | int EVP_PKEY_get_field_type(const EVP_PKEY *pkey) |
2518 | 88 | { |
2519 | 88 | char fstr[80]; |
2520 | 88 | size_t fstrlen; |
2521 | | |
2522 | 88 | if (pkey == NULL) |
2523 | 0 | return 0; |
2524 | | |
2525 | 88 | if (pkey->keymgmt == NULL |
2526 | 88 | || pkey->keydata == NULL) { |
2527 | 0 | # ifndef OPENSSL_NO_EC |
2528 | | /* Might work through the legacy route */ |
2529 | 0 | const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); |
2530 | 0 | const EC_GROUP *grp; |
2531 | |
|
2532 | 0 | if (ec == NULL) |
2533 | 0 | return 0; |
2534 | 0 | grp = EC_KEY_get0_group(ec); |
2535 | 0 | if (grp == NULL) |
2536 | 0 | return 0; |
2537 | | |
2538 | 0 | return EC_GROUP_get_field_type(grp); |
2539 | | # else |
2540 | | return 0; |
2541 | | # endif |
2542 | 0 | } |
2543 | | |
2544 | 88 | if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE, |
2545 | 88 | fstr, sizeof(fstr), &fstrlen)) |
2546 | 0 | return 0; |
2547 | | |
2548 | 88 | if (strcmp(fstr, SN_X9_62_prime_field) == 0) |
2549 | 82 | return NID_X9_62_prime_field; |
2550 | 6 | else if (strcmp(fstr, SN_X9_62_characteristic_two_field)) |
2551 | 0 | return NID_X9_62_characteristic_two_field; |
2552 | | |
2553 | 6 | return 0; |
2554 | 88 | } |
2555 | | #endif |