]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
b0edda11 | 2 | * Copyright 1995-2018 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 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
cd420b0b | 18 | #include "internal/refcount.h" |
4d94ae00 BM |
19 | #include <openssl/bn.h> |
20 | #include <openssl/err.h> | |
ec577822 BM |
21 | #include <openssl/objects.h> |
22 | #include <openssl/evp.h> | |
ec577822 | 23 | #include <openssl/x509.h> |
3c27208f RS |
24 | #include <openssl/rsa.h> |
25 | #include <openssl/dsa.h> | |
26 | #include <openssl/dh.h> | |
b3831fbb | 27 | #include <openssl/cmac.h> |
3c27208f | 28 | #include <openssl/engine.h> |
e74bd290 | 29 | #include <openssl/params.h> |
54c1711f | 30 | #include <openssl/serializer.h> |
e74bd290 | 31 | #include <openssl/core_names.h> |
01b8b3c7 | 32 | |
25f2138b DMSP |
33 | #include "crypto/asn1.h" |
34 | #include "crypto/evp.h" | |
e74bd290 | 35 | #include "internal/provider.h" |
f6aa5774 | 36 | #include "evp_local.h" |
18e377b4 | 37 | |
e683582b SL |
38 | static void evp_pkey_free_it(EVP_PKEY *key); |
39 | ||
40 | #ifndef FIPS_MODE | |
bb2297a4 | 41 | |
8900f3e3 | 42 | int EVP_PKEY_bits(const EVP_PKEY *pkey) |
0f113f3e | 43 | { |
6508e858 RL |
44 | if (pkey != NULL) { |
45 | if (pkey->ameth == NULL) | |
46 | return pkey->cache.bits; | |
47 | else if (pkey->ameth->pkey_bits) | |
48 | return pkey->ameth->pkey_bits(pkey); | |
49 | } | |
0f113f3e MC |
50 | return 0; |
51 | } | |
58964a49 | 52 | |
2514fa79 | 53 | int EVP_PKEY_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
54 | { |
55 | if (pkey == NULL) | |
56 | return 0; | |
6508e858 RL |
57 | if (pkey->ameth == NULL) |
58 | return pkey->cache.security_bits; | |
59 | if (pkey->ameth->pkey_security_bits == NULL) | |
0f113f3e MC |
60 | return -2; |
61 | return pkey->ameth->pkey_security_bits(pkey); | |
62 | } | |
2514fa79 | 63 | |
6b691a5c | 64 | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) |
0f113f3e | 65 | { |
e683582b | 66 | # ifndef OPENSSL_NO_DSA |
0f113f3e MC |
67 | if (pkey->type == EVP_PKEY_DSA) { |
68 | int ret = pkey->save_parameters; | |
69 | ||
70 | if (mode >= 0) | |
71 | pkey->save_parameters = mode; | |
26a7d938 | 72 | return ret; |
0f113f3e | 73 | } |
e683582b SL |
74 | # endif |
75 | # ifndef OPENSSL_NO_EC | |
0f113f3e MC |
76 | if (pkey->type == EVP_PKEY_EC) { |
77 | int ret = pkey->save_parameters; | |
78 | ||
79 | if (mode >= 0) | |
80 | pkey->save_parameters = mode; | |
26a7d938 | 81 | return ret; |
0f113f3e | 82 | } |
e683582b | 83 | # endif |
26a7d938 | 84 | return 0; |
0f113f3e | 85 | } |
d02b48c6 | 86 | |
a8b72844 | 87 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
0f113f3e | 88 | { |
2986ecdc DSH |
89 | if (to->type == EVP_PKEY_NONE) { |
90 | if (EVP_PKEY_set_type(to, from->type) == 0) | |
91 | return 0; | |
92 | } else if (to->type != from->type) { | |
0f113f3e MC |
93 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES); |
94 | goto err; | |
95 | } | |
96 | ||
97 | if (EVP_PKEY_missing_parameters(from)) { | |
98 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS); | |
99 | goto err; | |
100 | } | |
f72f00d4 DSH |
101 | |
102 | if (!EVP_PKEY_missing_parameters(to)) { | |
103 | if (EVP_PKEY_cmp_parameters(to, from) == 1) | |
104 | return 1; | |
105 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS); | |
106 | return 0; | |
107 | } | |
108 | ||
0f113f3e MC |
109 | if (from->ameth && from->ameth->param_copy) |
110 | return from->ameth->param_copy(to, from); | |
111 | err: | |
112 | return 0; | |
113 | } | |
d02b48c6 | 114 | |
af0f0f3e | 115 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e | 116 | { |
157ded39 RL |
117 | if (pkey != NULL) { |
118 | if (pkey->keymgmt != NULL) | |
119 | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, | |
120 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS); | |
121 | else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL) | |
122 | return pkey->ameth->param_missing(pkey); | |
123 | } | |
0f113f3e MC |
124 | return 0; |
125 | } | |
d02b48c6 | 126 | |
af0f0f3e | 127 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
128 | { |
129 | if (a->type != b->type) | |
130 | return -1; | |
131 | if (a->ameth && a->ameth->param_cmp) | |
132 | return a->ameth->param_cmp(a, b); | |
133 | return -2; | |
134 | } | |
58964a49 | 135 | |
af0f0f3e | 136 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
137 | { |
138 | if (a->type != b->type) | |
139 | return -1; | |
140 | ||
141 | if (a->ameth) { | |
142 | int ret; | |
143 | /* Compare parameters if the algorithm has them */ | |
144 | if (a->ameth->param_cmp) { | |
145 | ret = a->ameth->param_cmp(a, b); | |
146 | if (ret <= 0) | |
147 | return ret; | |
148 | } | |
149 | ||
150 | if (a->ameth->pub_cmp) | |
151 | return a->ameth->pub_cmp(a, b); | |
152 | } | |
153 | ||
154 | return -2; | |
155 | } | |
e6526fbf | 156 | |
2872dbe1 | 157 | |
0f113f3e MC |
158 | /* |
159 | * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey | |
160 | * is NULL just return 1 or 0 if the algorithm exists. | |
01b8b3c7 DSH |
161 | */ |
162 | ||
a08802ce MC |
163 | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
164 | int len) | |
0f113f3e MC |
165 | { |
166 | const EVP_PKEY_ASN1_METHOD *ameth; | |
a08802ce MC |
167 | ENGINE **eptr = (e == NULL) ? &e : NULL; |
168 | ||
0f113f3e MC |
169 | if (pkey) { |
170 | if (pkey->pkey.ptr) | |
e683582b | 171 | evp_pkey_free_it(pkey); |
0f113f3e MC |
172 | /* |
173 | * If key type matches and a method exists then this lookup has | |
174 | * succeeded once so just indicate success. | |
175 | */ | |
176 | if ((type == pkey->save_type) && pkey->ameth) | |
177 | return 1; | |
e683582b | 178 | # ifndef OPENSSL_NO_ENGINE |
d19b01ad | 179 | /* If we have ENGINEs release them */ |
7c96dbcd RS |
180 | ENGINE_finish(pkey->engine); |
181 | pkey->engine = NULL; | |
d19b01ad DSH |
182 | ENGINE_finish(pkey->pmeth_engine); |
183 | pkey->pmeth_engine = NULL; | |
e683582b | 184 | # endif |
0f113f3e MC |
185 | } |
186 | if (str) | |
a08802ce | 187 | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); |
0f113f3e | 188 | else |
a08802ce | 189 | ameth = EVP_PKEY_asn1_find(eptr, type); |
e683582b | 190 | # ifndef OPENSSL_NO_ENGINE |
a08802ce | 191 | if (pkey == NULL && eptr != NULL) |
0f113f3e | 192 | ENGINE_finish(e); |
e683582b | 193 | # endif |
7c96dbcd | 194 | if (ameth == NULL) { |
0f113f3e MC |
195 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); |
196 | return 0; | |
197 | } | |
198 | if (pkey) { | |
199 | pkey->ameth = ameth; | |
200 | pkey->engine = e; | |
201 | ||
202 | pkey->type = pkey->ameth->pkey_id; | |
203 | pkey->save_type = type; | |
204 | } | |
205 | return 1; | |
206 | } | |
01b8b3c7 | 207 | |
f929439f MC |
208 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, |
209 | const unsigned char *priv, | |
210 | size_t len) | |
a08802ce MC |
211 | { |
212 | EVP_PKEY *ret = EVP_PKEY_new(); | |
213 | ||
214 | if (ret == NULL | |
215 | || !pkey_set_type(ret, e, type, NULL, -1)) { | |
216 | /* EVPerr already called */ | |
217 | goto err; | |
218 | } | |
219 | ||
220 | if (ret->ameth->set_priv_key == NULL) { | |
f929439f | 221 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, |
a08802ce MC |
222 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
223 | goto err; | |
224 | } | |
225 | ||
226 | if (!ret->ameth->set_priv_key(ret, priv, len)) { | |
f929439f | 227 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
228 | goto err; |
229 | } | |
230 | ||
231 | return ret; | |
232 | ||
233 | err: | |
234 | EVP_PKEY_free(ret); | |
235 | return NULL; | |
236 | } | |
237 | ||
f929439f MC |
238 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, |
239 | const unsigned char *pub, | |
240 | size_t len) | |
a08802ce MC |
241 | { |
242 | EVP_PKEY *ret = EVP_PKEY_new(); | |
243 | ||
244 | if (ret == NULL | |
245 | || !pkey_set_type(ret, e, type, NULL, -1)) { | |
246 | /* EVPerr already called */ | |
247 | goto err; | |
248 | } | |
249 | ||
250 | if (ret->ameth->set_pub_key == NULL) { | |
f929439f | 251 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, |
a08802ce MC |
252 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
253 | goto err; | |
254 | } | |
255 | ||
256 | if (!ret->ameth->set_pub_key(ret, pub, len)) { | |
f929439f | 257 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
258 | goto err; |
259 | } | |
260 | ||
261 | return ret; | |
262 | ||
263 | err: | |
264 | EVP_PKEY_free(ret); | |
265 | return NULL; | |
266 | } | |
267 | ||
0d124b0a MC |
268 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, |
269 | size_t *len) | |
270 | { | |
271 | if (pkey->ameth->get_priv_key == NULL) { | |
272 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, | |
273 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | if (!pkey->ameth->get_priv_key(pkey, priv, len)) { | |
278 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
279 | return 0; | |
280 | } | |
281 | ||
282 | return 1; | |
283 | } | |
284 | ||
285 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, | |
286 | size_t *len) | |
287 | { | |
288 | if (pkey->ameth->get_pub_key == NULL) { | |
289 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, | |
290 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
291 | return 0; | |
292 | } | |
293 | ||
294 | if (!pkey->ameth->get_pub_key(pkey, pub, len)) { | |
295 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
296 | return 0; | |
297 | } | |
298 | ||
299 | return 1; | |
300 | } | |
301 | ||
b3831fbb MC |
302 | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, |
303 | size_t len, const EVP_CIPHER *cipher) | |
304 | { | |
e683582b SL |
305 | # ifndef OPENSSL_NO_CMAC |
306 | # ifndef OPENSSL_NO_ENGINE | |
9a7846df | 307 | const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL; |
e683582b | 308 | # endif |
e74bd290 RL |
309 | const char *cipher_name = EVP_CIPHER_name(cipher); |
310 | const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher); | |
311 | OPENSSL_CTX *libctx = | |
312 | prov == NULL ? NULL : ossl_provider_library_context(prov); | |
b3831fbb | 313 | EVP_PKEY *ret = EVP_PKEY_new(); |
81ff9eeb | 314 | EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL); |
e74bd290 RL |
315 | EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL; |
316 | OSSL_PARAM params[4]; | |
317 | size_t paramsn = 0; | |
b3831fbb MC |
318 | |
319 | if (ret == NULL | |
320 | || cmctx == NULL | |
321 | || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) { | |
322 | /* EVPerr already called */ | |
323 | goto err; | |
324 | } | |
325 | ||
e683582b | 326 | # ifndef OPENSSL_NO_ENGINE |
9a7846df | 327 | if (engine_id != NULL) |
e74bd290 | 328 | params[paramsn++] = |
69db3044 | 329 | OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0); |
e683582b | 330 | # endif |
3be06e0d | 331 | |
e74bd290 | 332 | params[paramsn++] = |
703170d4 | 333 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
7f588d20 | 334 | (char *)cipher_name, 0); |
e74bd290 RL |
335 | params[paramsn++] = |
336 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, | |
337 | (char *)priv, len); | |
338 | params[paramsn] = OSSL_PARAM_construct_end(); | |
339 | ||
340 | if (!EVP_MAC_CTX_set_params(cmctx, params)) { | |
b3831fbb MC |
341 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED); |
342 | goto err; | |
343 | } | |
344 | ||
345 | ret->pkey.ptr = cmctx; | |
346 | return ret; | |
347 | ||
348 | err: | |
349 | EVP_PKEY_free(ret); | |
b8d77c9b | 350 | EVP_MAC_CTX_free(cmctx); |
e74bd290 | 351 | EVP_MAC_free(cmac); |
b3831fbb | 352 | return NULL; |
e683582b | 353 | # else |
df6d51e2 MC |
354 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, |
355 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
356 | return NULL; | |
e683582b | 357 | # endif |
b3831fbb | 358 | } |
a08802ce | 359 | |
01b8b3c7 | 360 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) |
0f113f3e | 361 | { |
a08802ce | 362 | return pkey_set_type(pkey, NULL, type, NULL, -1); |
0f113f3e | 363 | } |
01b8b3c7 DSH |
364 | |
365 | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) | |
0f113f3e | 366 | { |
a08802ce | 367 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len); |
0f113f3e | 368 | } |
2f2e6b62 JL |
369 | |
370 | int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type) | |
371 | { | |
372 | if (pkey->type == type) { | |
373 | return 1; /* it already is that type */ | |
374 | } | |
375 | ||
376 | /* | |
377 | * The application is requesting to alias this to a different pkey type, | |
378 | * but not one that resolves to the base type. | |
379 | */ | |
380 | if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) { | |
381 | EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | pkey->type = type; | |
386 | return 1; | |
387 | } | |
388 | ||
e683582b | 389 | # ifndef OPENSSL_NO_ENGINE |
d19b01ad DSH |
390 | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e) |
391 | { | |
392 | if (e != NULL) { | |
393 | if (!ENGINE_init(e)) { | |
394 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB); | |
395 | return 0; | |
396 | } | |
397 | if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { | |
398 | ENGINE_finish(e); | |
399 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM); | |
400 | return 0; | |
401 | } | |
402 | } | |
403 | ENGINE_finish(pkey->pmeth_engine); | |
404 | pkey->pmeth_engine = e; | |
405 | return 1; | |
406 | } | |
229f7b38 DB |
407 | |
408 | ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey) | |
409 | { | |
410 | return pkey->engine; | |
411 | } | |
e683582b | 412 | # endif |
01b8b3c7 | 413 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
0f113f3e | 414 | { |
f4e4382c RL |
415 | int alias = type; |
416 | ||
ad5b71be | 417 | #ifndef OPENSSL_NO_EC |
f4e4382c RL |
418 | if (EVP_PKEY_type(type) == EVP_PKEY_EC) { |
419 | const EC_GROUP *group = EC_KEY_get0_group(key); | |
420 | ||
421 | if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2) | |
422 | alias = EVP_PKEY_SM2; | |
423 | } | |
ad5b71be | 424 | #endif |
f4e4382c | 425 | |
e34c66c6 | 426 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
0f113f3e | 427 | return 0; |
f4e4382c RL |
428 | if (!EVP_PKEY_set_alias_type(pkey, alias)) |
429 | return 0; | |
0f113f3e MC |
430 | pkey->pkey.ptr = key; |
431 | return (key != NULL); | |
432 | } | |
d02b48c6 | 433 | |
3aeb9348 | 434 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) |
0f113f3e MC |
435 | { |
436 | return pkey->pkey.ptr; | |
437 | } | |
db98bbc1 | 438 | |
ebad0b0b NM |
439 | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) |
440 | { | |
441 | ASN1_OCTET_STRING *os = NULL; | |
442 | if (pkey->type != EVP_PKEY_HMAC) { | |
443 | EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY); | |
444 | return NULL; | |
445 | } | |
446 | os = EVP_PKEY_get0(pkey); | |
447 | *len = os->length; | |
448 | return os->data; | |
449 | } | |
450 | ||
e683582b | 451 | # ifndef OPENSSL_NO_POLY1305 |
52ad5b60 TS |
452 | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) |
453 | { | |
454 | ASN1_OCTET_STRING *os = NULL; | |
455 | if (pkey->type != EVP_PKEY_POLY1305) { | |
456 | EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY); | |
457 | return NULL; | |
458 | } | |
459 | os = EVP_PKEY_get0(pkey); | |
460 | *len = os->length; | |
461 | return os->data; | |
462 | } | |
e683582b | 463 | # endif |
52ad5b60 | 464 | |
e683582b | 465 | # ifndef OPENSSL_NO_SIPHASH |
3f5616d7 TS |
466 | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) |
467 | { | |
468 | ASN1_OCTET_STRING *os = NULL; | |
469 | ||
470 | if (pkey->type != EVP_PKEY_SIPHASH) { | |
471 | EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY); | |
472 | return NULL; | |
473 | } | |
474 | os = EVP_PKEY_get0(pkey); | |
475 | *len = os->length; | |
476 | return os->data; | |
477 | } | |
e683582b | 478 | # endif |
3f5616d7 | 479 | |
e683582b | 480 | # ifndef OPENSSL_NO_RSA |
c7cb16a8 | 481 | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) |
52664f50 | 482 | { |
0f113f3e MC |
483 | int ret = EVP_PKEY_assign_RSA(pkey, key); |
484 | if (ret) | |
485 | RSA_up_ref(key); | |
486 | return ret; | |
52664f50 DSH |
487 | } |
488 | ||
9fdcc21f | 489 | RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) |
0f113f3e | 490 | { |
465a58b1 | 491 | if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) { |
2872dbe1 | 492 | EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY); |
0f113f3e MC |
493 | return NULL; |
494 | } | |
0f113f3e | 495 | return pkey->pkey.rsa; |
f769ce3e | 496 | } |
2872dbe1 DSH |
497 | |
498 | RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) | |
499 | { | |
500 | RSA *ret = EVP_PKEY_get0_RSA(pkey); | |
501 | if (ret != NULL) | |
502 | RSA_up_ref(ret); | |
503 | return ret; | |
504 | } | |
e683582b | 505 | # endif |
f769ce3e | 506 | |
e683582b | 507 | # ifndef OPENSSL_NO_DSA |
c7cb16a8 | 508 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) |
52664f50 | 509 | { |
0f113f3e MC |
510 | int ret = EVP_PKEY_assign_DSA(pkey, key); |
511 | if (ret) | |
512 | DSA_up_ref(key); | |
513 | return ret; | |
52664f50 DSH |
514 | } |
515 | ||
9fdcc21f | 516 | DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) |
0f113f3e MC |
517 | { |
518 | if (pkey->type != EVP_PKEY_DSA) { | |
2872dbe1 | 519 | EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY); |
0f113f3e MC |
520 | return NULL; |
521 | } | |
0f113f3e | 522 | return pkey->pkey.dsa; |
f769ce3e | 523 | } |
2872dbe1 DSH |
524 | |
525 | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) | |
526 | { | |
527 | DSA *ret = EVP_PKEY_get0_DSA(pkey); | |
528 | if (ret != NULL) | |
529 | DSA_up_ref(ret); | |
530 | return ret; | |
531 | } | |
e683582b | 532 | # endif |
f769ce3e | 533 | |
e683582b | 534 | # ifndef OPENSSL_NO_EC |
4d94ae00 | 535 | |
14a7cfb3 | 536 | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) |
4d94ae00 | 537 | { |
0f113f3e MC |
538 | int ret = EVP_PKEY_assign_EC_KEY(pkey, key); |
539 | if (ret) | |
540 | EC_KEY_up_ref(key); | |
541 | return ret; | |
4d94ae00 BM |
542 | } |
543 | ||
9fdcc21f | 544 | EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) |
4d94ae00 | 545 | { |
f4e4382c | 546 | if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { |
2872dbe1 | 547 | EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); |
0f113f3e MC |
548 | return NULL; |
549 | } | |
0f113f3e | 550 | return pkey->pkey.ec; |
4d94ae00 | 551 | } |
2872dbe1 DSH |
552 | |
553 | EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) | |
554 | { | |
555 | EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey); | |
556 | if (ret != NULL) | |
557 | EC_KEY_up_ref(ret); | |
558 | return ret; | |
559 | } | |
e683582b | 560 | # endif |
4d94ae00 | 561 | |
e683582b | 562 | # ifndef OPENSSL_NO_DH |
52664f50 | 563 | |
c7cb16a8 | 564 | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) |
52664f50 | 565 | { |
32c869ff MC |
566 | int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX; |
567 | int ret = EVP_PKEY_assign(pkey, type, key); | |
568 | ||
0f113f3e MC |
569 | if (ret) |
570 | DH_up_ref(key); | |
571 | return ret; | |
52664f50 DSH |
572 | } |
573 | ||
9fdcc21f | 574 | DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) |
0f113f3e MC |
575 | { |
576 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { | |
2872dbe1 | 577 | EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY); |
0f113f3e MC |
578 | return NULL; |
579 | } | |
0f113f3e | 580 | return pkey->pkey.dh; |
f769ce3e | 581 | } |
2872dbe1 DSH |
582 | |
583 | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) | |
584 | { | |
585 | DH *ret = EVP_PKEY_get0_DH(pkey); | |
586 | if (ret != NULL) | |
587 | DH_up_ref(ret); | |
588 | return ret; | |
589 | } | |
e683582b | 590 | # endif |
f769ce3e | 591 | |
6b691a5c | 592 | int EVP_PKEY_type(int type) |
0f113f3e MC |
593 | { |
594 | int ret; | |
595 | const EVP_PKEY_ASN1_METHOD *ameth; | |
596 | ENGINE *e; | |
597 | ameth = EVP_PKEY_asn1_find(&e, type); | |
598 | if (ameth) | |
599 | ret = ameth->pkey_id; | |
600 | else | |
601 | ret = NID_undef; | |
e683582b | 602 | # ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 603 | ENGINE_finish(e); |
e683582b | 604 | # endif |
0f113f3e MC |
605 | return ret; |
606 | } | |
d02b48c6 | 607 | |
7f57b076 | 608 | int EVP_PKEY_id(const EVP_PKEY *pkey) |
0f113f3e MC |
609 | { |
610 | return pkey->type; | |
611 | } | |
7f57b076 DSH |
612 | |
613 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) | |
0f113f3e MC |
614 | { |
615 | return EVP_PKEY_type(pkey->type); | |
616 | } | |
7f57b076 | 617 | |
d02b48c6 | 618 | |
f1299839 RL |
619 | static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent) |
620 | { | |
621 | BIO_set_indent(*out, saved_indent); | |
622 | if (pop_f_prefix) { | |
623 | BIO *next = BIO_pop(*out); | |
624 | ||
625 | BIO_free(*out); | |
626 | *out = next; | |
627 | } | |
628 | return 1; | |
629 | } | |
630 | ||
631 | static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent, | |
632 | long indent) | |
633 | { | |
634 | *pop_f_prefix = 0; | |
635 | *saved_indent = 0; | |
636 | if (indent > 0) { | |
637 | long i = BIO_get_indent(*out); | |
638 | ||
639 | *saved_indent = (i < 0 ? 0 : i); | |
640 | if (BIO_set_indent(*out, indent) <= 0) { | |
641 | if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL) | |
642 | return 0; | |
643 | *pop_f_prefix = 1; | |
644 | } | |
645 | if (BIO_set_indent(*out, indent) <= 0) { | |
646 | print_reset_indent(out, *pop_f_prefix, *saved_indent); | |
647 | return 0; | |
648 | } | |
649 | } | |
650 | return 1; | |
651 | } | |
652 | ||
35208f36 | 653 | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
0f113f3e MC |
654 | const char *kstr) |
655 | { | |
5310a4e6 P |
656 | return BIO_indent(out, indent, 128) |
657 | && BIO_printf(out, "%s algorithm \"%s\" unsupported\n", | |
658 | kstr, OBJ_nid2ln(pkey->type)) > 0; | |
0f113f3e | 659 | } |
35208f36 | 660 | |
f1299839 RL |
661 | static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent, |
662 | const char *propquery /* For provided serialization */, | |
663 | int (*legacy_print)(BIO *out, const EVP_PKEY *pkey, | |
664 | int indent, ASN1_PCTX *pctx), | |
665 | ASN1_PCTX *legacy_pctx /* For legacy print */) | |
0f113f3e | 666 | { |
f1299839 RL |
667 | int pop_f_prefix; |
668 | long saved_indent; | |
669 | OSSL_SERIALIZER_CTX *ctx = NULL; | |
670 | int ret = -2; /* default to unsupported */ | |
671 | ||
672 | if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent)) | |
673 | return 0; | |
54c1711f | 674 | |
f1299839 | 675 | ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery); |
54c1711f RL |
676 | if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) |
677 | ret = OSSL_SERIALIZER_to_bio(ctx, out); | |
678 | OSSL_SERIALIZER_CTX_free(ctx); | |
679 | ||
680 | if (ret != -2) | |
f1299839 | 681 | goto end; |
54c1711f RL |
682 | |
683 | /* legacy fallback */ | |
f1299839 RL |
684 | if (legacy_print != NULL) |
685 | ret = legacy_print(out, pkey, 0, legacy_pctx); | |
686 | else | |
687 | ret = unsup_alg(out, pkey, 0, "Public Key"); | |
0f113f3e | 688 | |
f1299839 RL |
689 | end: |
690 | print_reset_indent(&out, pop_f_prefix, saved_indent); | |
691 | return ret; | |
692 | } | |
693 | ||
694 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, | |
695 | int indent, ASN1_PCTX *pctx) | |
696 | { | |
697 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ, | |
698 | (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL), | |
699 | pctx); | |
0f113f3e | 700 | } |
35208f36 DSH |
701 | |
702 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
703 | int indent, ASN1_PCTX *pctx) |
704 | { | |
f1299839 RL |
705 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ, |
706 | (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL), | |
707 | pctx); | |
0f113f3e | 708 | } |
35208f36 DSH |
709 | |
710 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
711 | int indent, ASN1_PCTX *pctx) |
712 | { | |
f1299839 RL |
713 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ, |
714 | (pkey->ameth != NULL ? pkey->ameth->param_print : NULL), | |
715 | pctx); | |
0f113f3e | 716 | } |
03919683 | 717 | |
ead0d234 RL |
718 | static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op, |
719 | int arg1, void *arg2) | |
720 | { | |
3c6ed955 | 721 | if (pkey->keymgmt == NULL) |
ead0d234 RL |
722 | return 0; |
723 | switch (op) { | |
724 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: | |
725 | { | |
726 | char mdname[80] = ""; | |
727 | int nid; | |
728 | int rv = EVP_PKEY_get_default_digest_name(pkey, mdname, | |
729 | sizeof(mdname)); | |
730 | ||
731 | if (rv <= 0) | |
732 | return rv; | |
733 | nid = OBJ_sn2nid(mdname); | |
734 | if (nid == NID_undef) | |
735 | nid = OBJ_ln2nid(mdname); | |
736 | if (nid == NID_undef) | |
737 | return 0; | |
738 | *(int *)arg2 = nid; | |
739 | return 1; | |
740 | } | |
741 | default: | |
742 | return -2; | |
743 | } | |
744 | } | |
745 | ||
5d6aaf8a | 746 | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
0f113f3e | 747 | { |
ead0d234 RL |
748 | if (pkey->ameth == NULL) |
749 | return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2); | |
750 | if (pkey->ameth->pkey_ctrl == NULL) | |
0f113f3e | 751 | return -2; |
5d6aaf8a DSH |
752 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
753 | } | |
754 | ||
755 | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) | |
756 | { | |
757 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); | |
758 | } | |
759 | ||
ead0d234 RL |
760 | int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey, |
761 | char *mdname, size_t mdname_sz) | |
762 | { | |
763 | if (pkey->ameth == NULL) { | |
764 | OSSL_PARAM params[3]; | |
765 | char mddefault[100] = ""; | |
766 | char mdmandatory[100] = ""; | |
767 | ||
768 | params[0] = | |
769 | OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, | |
770 | mddefault, sizeof(mddefault)); | |
771 | params[1] = | |
772 | OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, | |
773 | mdmandatory, | |
774 | sizeof(mdmandatory)); | |
775 | params[2] = OSSL_PARAM_construct_end(); | |
3c6ed955 | 776 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) |
ead0d234 RL |
777 | return 0; |
778 | if (mdmandatory[0] != '\0') { | |
779 | OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz); | |
780 | return 2; | |
781 | } | |
782 | OPENSSL_strlcpy(mdname, mddefault, mdname_sz); | |
783 | return 1; | |
784 | } | |
785 | ||
786 | { | |
787 | int nid = NID_undef; | |
788 | int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid); | |
789 | const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL; | |
790 | ||
791 | if (rv > 0) | |
792 | OPENSSL_strlcpy(mdname, name, mdname_sz); | |
793 | return rv; | |
794 | } | |
795 | } | |
796 | ||
ecbb2fca DW |
797 | int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid) |
798 | { | |
799 | int rv, default_nid; | |
800 | ||
801 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL); | |
802 | if (rv == -2) { | |
803 | /* | |
804 | * If there is a mandatory default digest and this isn't it, then | |
805 | * the answer is 'no'. | |
806 | */ | |
807 | rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid); | |
808 | if (rv == 2) | |
809 | return (nid == default_nid); | |
810 | /* zero is an error from EVP_PKEY_get_default_digest_nid() */ | |
811 | if (rv == 0) | |
812 | return -1; | |
813 | } | |
814 | return rv; | |
815 | } | |
816 | ||
5d6aaf8a DSH |
817 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, |
818 | const unsigned char *pt, size_t ptlen) | |
819 | { | |
820 | if (ptlen > INT_MAX) | |
821 | return 0; | |
822 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen, | |
823 | (void *)pt) <= 0) | |
824 | return 0; | |
825 | return 1; | |
826 | } | |
827 | ||
828 | size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt) | |
829 | { | |
830 | int rv; | |
831 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt); | |
832 | if (rv <= 0) | |
833 | return 0; | |
834 | return rv; | |
0f113f3e | 835 | } |
e683582b SL |
836 | |
837 | #endif /* FIPS_MODE */ | |
838 | ||
839 | /*- All methods below can also be used in FIPS_MODE */ | |
840 | ||
841 | EVP_PKEY *EVP_PKEY_new(void) | |
842 | { | |
843 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); | |
844 | ||
845 | if (ret == NULL) { | |
846 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
847 | return NULL; | |
848 | } | |
849 | ret->type = EVP_PKEY_NONE; | |
850 | ret->save_type = EVP_PKEY_NONE; | |
851 | ret->references = 1; | |
852 | ret->save_parameters = 1; | |
853 | ret->lock = CRYPTO_THREAD_lock_new(); | |
854 | if (ret->lock == NULL) { | |
855 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
856 | OPENSSL_free(ret); | |
857 | return NULL; | |
858 | } | |
859 | return ret; | |
860 | } | |
861 | ||
862 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) | |
863 | { | |
864 | int i; | |
865 | ||
866 | if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0) | |
867 | return 0; | |
868 | ||
869 | REF_PRINT_COUNT("EVP_PKEY", pkey); | |
870 | REF_ASSERT_ISNT(i < 2); | |
871 | return ((i > 1) ? 1 : 0); | |
872 | } | |
873 | ||
badf51c8 RL |
874 | #ifndef FIPS_MODE |
875 | static void evp_pkey_free_legacy(EVP_PKEY *x) | |
876 | { | |
877 | if (x->ameth != NULL) { | |
878 | if (x->ameth->pkey_free) | |
879 | x->ameth->pkey_free(x); | |
880 | x->pkey.ptr = NULL; | |
881 | x->ameth = NULL; | |
882 | } | |
883 | # ifndef OPENSSL_NO_ENGINE | |
884 | ENGINE_finish(x->engine); | |
885 | x->engine = NULL; | |
886 | ENGINE_finish(x->pmeth_engine); | |
887 | x->pmeth_engine = NULL; | |
888 | # endif | |
889 | x->type = x->save_type = EVP_PKEY_NONE; | |
890 | } | |
891 | #endif /* FIPS_MODE */ | |
892 | ||
e683582b SL |
893 | static void evp_pkey_free_it(EVP_PKEY *x) |
894 | { | |
895 | /* internal function; x is never NULL */ | |
896 | ||
3c6ed955 | 897 | evp_keymgmt_util_clear_operation_cache(x); |
badf51c8 RL |
898 | #ifndef FIPS_MODE |
899 | evp_pkey_free_legacy(x); | |
900 | #endif | |
e683582b | 901 | |
3c6ed955 RL |
902 | if (x->keymgmt != NULL) { |
903 | evp_keymgmt_freedata(x->keymgmt, x->keydata); | |
904 | EVP_KEYMGMT_free(x->keymgmt); | |
905 | x->keymgmt = NULL; | |
906 | x->keydata = NULL; | |
907 | } | |
e683582b SL |
908 | } |
909 | ||
910 | void EVP_PKEY_free(EVP_PKEY *x) | |
911 | { | |
912 | int i; | |
913 | ||
914 | if (x == NULL) | |
915 | return; | |
916 | ||
917 | CRYPTO_DOWN_REF(&x->references, &i, x->lock); | |
918 | REF_PRINT_COUNT("EVP_PKEY", x); | |
919 | if (i > 0) | |
920 | return; | |
921 | REF_ASSERT_ISNT(i < 0); | |
922 | evp_pkey_free_it(x); | |
923 | CRYPTO_THREAD_lock_free(x->lock); | |
924 | #ifndef FIPS_MODE | |
925 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); | |
926 | #endif | |
927 | OPENSSL_free(x); | |
928 | } | |
929 | ||
e683582b SL |
930 | int EVP_PKEY_size(const EVP_PKEY *pkey) |
931 | { | |
6508e858 RL |
932 | if (pkey != NULL) { |
933 | if (pkey->ameth == NULL) | |
934 | return pkey->cache.size; | |
935 | else if (pkey->ameth->pkey_size != NULL) | |
936 | return pkey->ameth->pkey_size(pkey); | |
937 | } | |
e683582b SL |
938 | return 0; |
939 | } | |
f6aa5774 | 940 | |
3c6ed955 RL |
941 | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx, |
942 | EVP_KEYMGMT **keymgmt, | |
943 | const char *propquery) | |
f6aa5774 RL |
944 | { |
945 | EVP_KEYMGMT *allocated_keymgmt = NULL; | |
946 | EVP_KEYMGMT *tmp_keymgmt = NULL; | |
b305452f | 947 | void *keydata = NULL; |
f6aa5774 RL |
948 | |
949 | if (pk == NULL) | |
950 | return NULL; | |
951 | ||
3f7ce7f1 | 952 | #ifndef FIPS_MODE |
3f7ce7f1 | 953 | if (pk->pkey.ptr != NULL) { |
3f7ce7f1 | 954 | /* |
3c6ed955 RL |
955 | * If the legacy key doesn't have an dirty counter or export function, |
956 | * give up | |
3f7ce7f1 | 957 | */ |
3c6ed955 RL |
958 | if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL) |
959 | return NULL; | |
3f7ce7f1 RL |
960 | } |
961 | #endif | |
962 | ||
3c6ed955 RL |
963 | if (keymgmt != NULL) { |
964 | tmp_keymgmt = *keymgmt; | |
965 | *keymgmt = NULL; | |
966 | } | |
967 | ||
3f7ce7f1 | 968 | /* If no keymgmt was given or found, get a default keymgmt */ |
f6aa5774 | 969 | if (tmp_keymgmt == NULL) { |
2ee4a50a | 970 | EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery); |
f6aa5774 RL |
971 | |
972 | if (ctx != NULL && ctx->keytype != NULL) | |
973 | tmp_keymgmt = allocated_keymgmt = | |
974 | EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery); | |
975 | EVP_PKEY_CTX_free(ctx); | |
976 | } | |
977 | ||
3c6ed955 | 978 | /* If there's still no keymgmt to be had, give up */ |
3f7ce7f1 RL |
979 | if (tmp_keymgmt == NULL) |
980 | goto end; | |
f6aa5774 | 981 | |
3f7ce7f1 RL |
982 | #ifndef FIPS_MODE |
983 | if (pk->pkey.ptr != NULL) { | |
3c6ed955 | 984 | size_t i = 0; |
3f7ce7f1 RL |
985 | |
986 | /* | |
3c6ed955 RL |
987 | * If the legacy "origin" hasn't changed since last time, we try |
988 | * to find our keymgmt in the operation cache. If it has changed, | |
989 | * |i| remains zero, and we will clear the cache further down. | |
3f7ce7f1 | 990 | */ |
3c6ed955 RL |
991 | if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) { |
992 | i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt); | |
993 | ||
994 | /* | |
995 | * If |tmp_keymgmt| is present in the operation cache, it means | |
996 | * that export doesn't need to be redone. In that case, we take | |
997 | * token copies of the cached pointers, to have token success | |
998 | * values to return. | |
999 | */ | |
1000 | if (i < OSSL_NELEM(pk->operation_cache) | |
1001 | && pk->operation_cache[i].keymgmt != NULL) { | |
1002 | keydata = pk->operation_cache[i].keydata; | |
1003 | goto end; | |
1004 | } | |
3f7ce7f1 RL |
1005 | } |
1006 | ||
1007 | /* | |
3c6ed955 RL |
1008 | * TODO(3.0) Right now, we assume we have ample space. We will have |
1009 | * to think about a cache aging scheme, though, if |i| indexes outside | |
1010 | * the array. | |
3f7ce7f1 | 1011 | */ |
3c6ed955 | 1012 | if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache))) |
3f7ce7f1 RL |
1013 | goto end; |
1014 | ||
1015 | /* Make sure that the keymgmt key type matches the legacy NID */ | |
1016 | if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))) | |
1017 | goto end; | |
1018 | ||
1019 | if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL) | |
1020 | goto end; | |
1021 | ||
1022 | if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt)) { | |
1023 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1024 | keydata = NULL; | |
1025 | goto end; | |
1026 | } | |
1027 | ||
3c6ed955 RL |
1028 | /* |
1029 | * If the dirty counter changed since last time, then clear the | |
1030 | * operation cache. In that case, we know that |i| is zero. Just | |
1031 | * in case this is a re-export, we increment then decrement the | |
1032 | * keymgmt reference counter. | |
1033 | */ | |
1034 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */ | |
1035 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1036 | keydata = NULL; | |
1037 | goto end; | |
1038 | } | |
1039 | if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy) | |
1040 | evp_keymgmt_util_clear_operation_cache(pk); | |
1041 | EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */ | |
1042 | ||
1043 | /* Add the new export to the operation cache */ | |
1044 | if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) { | |
1045 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1046 | keydata = NULL; | |
1047 | goto end; | |
1048 | } | |
3f7ce7f1 RL |
1049 | |
1050 | /* Synchronize the dirty count */ | |
1051 | pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk); | |
1052 | goto end; | |
1053 | } | |
1054 | #endif /* FIPS_MODE */ | |
1055 | ||
1056 | keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt); | |
1057 | ||
1058 | end: | |
f6aa5774 RL |
1059 | /* |
1060 | * If nothing was exported, |tmp_keymgmt| might point at a freed | |
1061 | * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for | |
1062 | * the caller either way in that case. | |
1063 | */ | |
b305452f | 1064 | if (keydata == NULL) |
f6aa5774 RL |
1065 | tmp_keymgmt = NULL; |
1066 | ||
1067 | if (keymgmt != NULL) | |
1068 | *keymgmt = tmp_keymgmt; | |
1069 | ||
1070 | EVP_KEYMGMT_free(allocated_keymgmt); | |
b305452f | 1071 | return keydata; |
f6aa5774 | 1072 | } |
badf51c8 RL |
1073 | |
1074 | #ifndef FIPS_MODE | |
1075 | /* | |
1076 | * This differs from exporting in that it releases the legacy key and assigns | |
1077 | * the export keymgmt and keydata to the "origin" provider side key instead | |
1078 | * of the operation cache. | |
1079 | */ | |
1080 | void *evp_pkey_upgrade_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx, | |
1081 | EVP_KEYMGMT **keymgmt, | |
1082 | const char *propquery) | |
1083 | { | |
1084 | EVP_KEYMGMT *allocated_keymgmt = NULL; | |
1085 | EVP_KEYMGMT *tmp_keymgmt = NULL; | |
1086 | void *keydata = NULL; | |
1087 | ||
1088 | if (pk == NULL) | |
1089 | return NULL; | |
1090 | ||
1091 | /* | |
1092 | * If this key is already "upgraded", this function shouldn't have been | |
1093 | * called. | |
1094 | */ | |
1095 | if (!ossl_assert(pk->keymgmt == NULL)) | |
1096 | return NULL; | |
1097 | ||
1098 | if (keymgmt != NULL) { | |
1099 | tmp_keymgmt = *keymgmt; | |
1100 | *keymgmt = NULL; | |
1101 | } | |
1102 | ||
1103 | /* If the key isn't a legacy one, bail out, but with proper values */ | |
1104 | if (pk->pkey.ptr == NULL) { | |
1105 | tmp_keymgmt = pk->keymgmt; | |
1106 | keydata = pk->keydata; | |
1107 | } else { | |
1108 | /* If the legacy key doesn't have an export function, give up */ | |
1109 | if (pk->ameth->export_to == NULL) | |
1110 | return NULL; | |
1111 | ||
1112 | /* If no keymgmt was given, get a default keymgmt */ | |
1113 | if (tmp_keymgmt == NULL) { | |
1114 | EVP_PKEY_CTX *ctx = | |
1115 | EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery); | |
1116 | ||
1117 | if (ctx != NULL && ctx->keytype != NULL) | |
1118 | tmp_keymgmt = allocated_keymgmt = | |
1119 | EVP_KEYMGMT_fetch(ctx->libctx, ctx->keytype, propquery); | |
1120 | EVP_PKEY_CTX_free(ctx); | |
1121 | } | |
1122 | ||
1123 | /* If we still don't have a keymgmt, give up */ | |
1124 | if (tmp_keymgmt == NULL) | |
1125 | goto end; | |
1126 | ||
1127 | /* Make sure that the keymgmt key type matches the legacy NID */ | |
1128 | if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))) | |
1129 | goto end; | |
1130 | ||
1131 | if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL) | |
1132 | goto end; | |
1133 | ||
1134 | if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt) | |
1135 | || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) { | |
1136 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1137 | keydata = NULL; | |
1138 | goto end; | |
1139 | } | |
1140 | ||
1141 | /* | |
1142 | * Clear the operation cache, all the legacy data, as well as the | |
1143 | * dirty counters | |
1144 | */ | |
1145 | evp_pkey_free_legacy(pk); | |
1146 | pk->dirty_cnt_copy = 0; | |
1147 | ||
1148 | evp_keymgmt_util_clear_operation_cache(pk); | |
1149 | pk->keymgmt = tmp_keymgmt; | |
1150 | pk->keydata = keydata; | |
1151 | evp_keymgmt_util_cache_keyinfo(pk); | |
1152 | } | |
1153 | ||
1154 | end: | |
1155 | /* | |
1156 | * If nothing was upgraded, |tmp_keymgmt| might point at a freed | |
1157 | * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for | |
1158 | * the caller either way in that case. | |
1159 | */ | |
1160 | if (keydata == NULL) | |
1161 | tmp_keymgmt = NULL; | |
1162 | ||
1163 | if (keymgmt != NULL) | |
1164 | *keymgmt = tmp_keymgmt; | |
1165 | ||
1166 | EVP_KEYMGMT_free(allocated_keymgmt); | |
1167 | return keydata; | |
1168 | } | |
1169 | #endif /* FIPS_MODE */ |