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