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