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