]>
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> | |
4f76d62f | 27 | #include <openssl/ec.h> |
b3831fbb | 28 | #include <openssl/cmac.h> |
3c27208f | 29 | #include <openssl/engine.h> |
e74bd290 | 30 | #include <openssl/params.h> |
54c1711f | 31 | #include <openssl/serializer.h> |
e74bd290 | 32 | #include <openssl/core_names.h> |
01b8b3c7 | 33 | |
25f2138b DMSP |
34 | #include "crypto/asn1.h" |
35 | #include "crypto/evp.h" | |
c2041da8 | 36 | #include "internal/evp.h" |
e74bd290 | 37 | #include "internal/provider.h" |
f6aa5774 | 38 | #include "evp_local.h" |
18e377b4 | 39 | |
4f76d62f RL |
40 | #include "crypto/ec.h" |
41 | ||
42 | /* TODO remove this when the EVP_PKEY_is_a() #legacy support hack is removed */ | |
43 | #include "e_os.h" /* strcasecmp on Windows */ | |
44 | ||
8243d8d1 RL |
45 | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
46 | int len, EVP_KEYMGMT *keymgmt); | |
e683582b SL |
47 | static void evp_pkey_free_it(EVP_PKEY *key); |
48 | ||
49 | #ifndef FIPS_MODE | |
bb2297a4 | 50 | |
8158cf20 RL |
51 | /* The type of parameters selected in key parameter functions */ |
52 | # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS | |
53 | ||
8900f3e3 | 54 | int EVP_PKEY_bits(const EVP_PKEY *pkey) |
0f113f3e | 55 | { |
6508e858 RL |
56 | if (pkey != NULL) { |
57 | if (pkey->ameth == NULL) | |
58 | return pkey->cache.bits; | |
59 | else if (pkey->ameth->pkey_bits) | |
60 | return pkey->ameth->pkey_bits(pkey); | |
61 | } | |
0f113f3e MC |
62 | return 0; |
63 | } | |
58964a49 | 64 | |
2514fa79 | 65 | int EVP_PKEY_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
66 | { |
67 | if (pkey == NULL) | |
68 | return 0; | |
6508e858 RL |
69 | if (pkey->ameth == NULL) |
70 | return pkey->cache.security_bits; | |
71 | if (pkey->ameth->pkey_security_bits == NULL) | |
0f113f3e MC |
72 | return -2; |
73 | return pkey->ameth->pkey_security_bits(pkey); | |
74 | } | |
2514fa79 | 75 | |
6b691a5c | 76 | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) |
0f113f3e | 77 | { |
e683582b | 78 | # ifndef OPENSSL_NO_DSA |
0f113f3e MC |
79 | if (pkey->type == EVP_PKEY_DSA) { |
80 | int ret = pkey->save_parameters; | |
81 | ||
82 | if (mode >= 0) | |
83 | pkey->save_parameters = mode; | |
26a7d938 | 84 | return ret; |
0f113f3e | 85 | } |
e683582b SL |
86 | # endif |
87 | # ifndef OPENSSL_NO_EC | |
0f113f3e MC |
88 | if (pkey->type == EVP_PKEY_EC) { |
89 | int ret = pkey->save_parameters; | |
90 | ||
91 | if (mode >= 0) | |
92 | pkey->save_parameters = mode; | |
26a7d938 | 93 | return ret; |
0f113f3e | 94 | } |
e683582b | 95 | # endif |
26a7d938 | 96 | return 0; |
0f113f3e | 97 | } |
d02b48c6 | 98 | |
ff1f7cde AT |
99 | int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg) |
100 | { | |
101 | return CRYPTO_set_ex_data(&key->ex_data, idx, arg); | |
102 | } | |
103 | ||
104 | void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx) | |
105 | { | |
106 | return CRYPTO_get_ex_data(&key->ex_data, idx); | |
107 | } | |
108 | ||
a8b72844 | 109 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
0f113f3e | 110 | { |
ff3b59e1 RL |
111 | /* |
112 | * TODO: clean up legacy stuff from this function when legacy support | |
113 | * is gone. | |
114 | */ | |
115 | ||
116 | /* | |
acb90ba8 RL |
117 | * If |to| is a legacy key and |from| isn't, we must downgrade |from|. |
118 | * If that fails, this function fails. | |
ff3b59e1 | 119 | */ |
acb90ba8 RL |
120 | if (to->type != EVP_PKEY_NONE && from->keymgmt != NULL) |
121 | if (!evp_pkey_downgrade((EVP_PKEY *)from)) | |
122 | return 0; | |
123 | ||
124 | /* | |
125 | * Make sure |to| is typed. Content is less important at this early | |
126 | * stage. | |
127 | * | |
128 | * 1. If |to| is untyped, assign |from|'s key type to it. | |
129 | * 2. If |to| contains a legacy key, compare its |type| to |from|'s. | |
130 | * (|from| was already downgraded above) | |
131 | * | |
132 | * If |to| is a provided key, there's nothing more to do here, functions | |
133 | * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called | |
134 | * further down help us find out if they are the same or not. | |
135 | */ | |
136 | if (to->type == EVP_PKEY_NONE && to->keymgmt == NULL) { | |
137 | if (from->type != EVP_PKEY_NONE) { | |
ff3b59e1 RL |
138 | if (EVP_PKEY_set_type(to, from->type) == 0) |
139 | return 0; | |
acb90ba8 RL |
140 | } else { |
141 | if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0) | |
142 | return 0; | |
143 | } | |
144 | } else if (to->type != EVP_PKEY_NONE) { | |
145 | if (to->type != from->type) { | |
ff3b59e1 RL |
146 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES); |
147 | goto err; | |
148 | } | |
0f113f3e MC |
149 | } |
150 | ||
151 | if (EVP_PKEY_missing_parameters(from)) { | |
152 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS); | |
153 | goto err; | |
154 | } | |
f72f00d4 DSH |
155 | |
156 | if (!EVP_PKEY_missing_parameters(to)) { | |
157 | if (EVP_PKEY_cmp_parameters(to, from) == 1) | |
158 | return 1; | |
159 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS); | |
160 | return 0; | |
161 | } | |
162 | ||
ff3b59e1 RL |
163 | /* For purely provided keys, we just call the keymgmt utility */ |
164 | if (to->keymgmt != NULL && from->keymgmt != NULL) | |
8158cf20 | 165 | return evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS); |
ff3b59e1 RL |
166 | |
167 | /* | |
168 | * If |to| is provided, we know that |from| is legacy at this point. | |
169 | * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_copy() | |
170 | * to copy the appropriate data to |to|'s keydata. | |
171 | */ | |
172 | if (to->keymgmt != NULL) { | |
173 | EVP_KEYMGMT *to_keymgmt = to->keymgmt; | |
174 | void *from_keydata = | |
175 | evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt, | |
176 | NULL); | |
177 | ||
acb90ba8 RL |
178 | /* |
179 | * If we get a NULL, it could be an internal error, or it could be | |
180 | * that there's a key mismatch. We're pretending the latter... | |
181 | */ | |
ff3b59e1 | 182 | if (from_keydata == NULL) { |
acb90ba8 | 183 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); |
ff3b59e1 RL |
184 | return 0; |
185 | } | |
186 | return evp_keymgmt_copy(to->keymgmt, to->keydata, from_keydata, | |
8158cf20 | 187 | SELECT_PARAMETERS); |
ff3b59e1 RL |
188 | } |
189 | ||
190 | /* Both keys are legacy */ | |
191 | if (from->ameth != NULL && from->ameth->param_copy != NULL) | |
0f113f3e MC |
192 | return from->ameth->param_copy(to, from); |
193 | err: | |
194 | return 0; | |
195 | } | |
d02b48c6 | 196 | |
af0f0f3e | 197 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e | 198 | { |
157ded39 RL |
199 | if (pkey != NULL) { |
200 | if (pkey->keymgmt != NULL) | |
8158cf20 | 201 | return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS); |
157ded39 RL |
202 | else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL) |
203 | return pkey->ameth->param_missing(pkey); | |
204 | } | |
0f113f3e MC |
205 | return 0; |
206 | } | |
d02b48c6 | 207 | |
1e9101c4 RL |
208 | /* |
209 | * This function is called for any mixture of keys except pure legacy pair. | |
210 | * TODO When legacy keys are gone, we replace a call to this functions with | |
211 | * a call to evp_keymgmt_util_match(). | |
212 | */ | |
213 | static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b, | |
214 | int selection) | |
215 | { | |
216 | EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL; | |
217 | void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL; | |
218 | ||
219 | /* If none of them are provided, this function shouldn't have been called */ | |
220 | if (!ossl_assert(a->keymgmt != NULL || b->keymgmt != NULL)) | |
221 | return -2; | |
222 | ||
223 | /* For purely provided keys, we just call the keymgmt utility */ | |
224 | if (a->keymgmt != NULL && b->keymgmt != NULL) | |
225 | return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection); | |
226 | ||
227 | /* | |
acb90ba8 RL |
228 | * At this point, one of them is provided, the other not. This allows |
229 | * us to compare types using legacy NIDs. | |
230 | */ | |
231 | if ((a->type != EVP_PKEY_NONE | |
232 | && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type))) | |
233 | || (b->type != EVP_PKEY_NONE | |
234 | && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))) | |
235 | return -1; /* not the same key type */ | |
236 | ||
237 | /* | |
238 | * We've determined that they both are the same keytype, so the next | |
239 | * step is to do a bit of cross export to ensure we have keydata for | |
240 | * both keys in the same keymgmt. | |
1e9101c4 RL |
241 | */ |
242 | keymgmt1 = a->keymgmt; | |
243 | keydata1 = a->keydata; | |
244 | keymgmt2 = b->keymgmt; | |
245 | keydata2 = b->keydata; | |
246 | ||
1e9101c4 RL |
247 | if (keymgmt2 != NULL && keymgmt2->match != NULL) { |
248 | tmp_keydata = | |
249 | evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL); | |
250 | if (tmp_keydata != NULL) { | |
251 | keymgmt1 = keymgmt2; | |
252 | keydata1 = tmp_keydata; | |
253 | } | |
254 | } | |
255 | if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) { | |
256 | tmp_keydata = | |
257 | evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL); | |
258 | if (tmp_keydata != NULL) { | |
259 | keymgmt2 = keymgmt1; | |
260 | keydata2 = tmp_keydata; | |
261 | } | |
262 | } | |
263 | ||
264 | /* If we still don't have matching keymgmt implementations, we give up */ | |
265 | if (keymgmt1 != keymgmt2) | |
266 | return -2; | |
267 | ||
268 | return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection); | |
269 | } | |
270 | ||
af0f0f3e | 271 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e | 272 | { |
1e9101c4 RL |
273 | /* |
274 | * TODO: clean up legacy stuff from this function when legacy support | |
275 | * is gone. | |
276 | */ | |
277 | ||
278 | if (a->keymgmt != NULL || b->keymgmt != NULL) | |
8158cf20 | 279 | return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS); |
1e9101c4 RL |
280 | |
281 | /* All legacy keys */ | |
0f113f3e MC |
282 | if (a->type != b->type) |
283 | return -1; | |
1e9101c4 | 284 | if (a->ameth != NULL && a->ameth->param_cmp != NULL) |
0f113f3e MC |
285 | return a->ameth->param_cmp(a, b); |
286 | return -2; | |
287 | } | |
58964a49 | 288 | |
af0f0f3e | 289 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e | 290 | { |
1e9101c4 RL |
291 | /* |
292 | * TODO: clean up legacy stuff from this function when legacy support | |
293 | * is gone. | |
294 | */ | |
295 | ||
296 | if (a->keymgmt != NULL || b->keymgmt != NULL) | |
8158cf20 RL |
297 | return evp_pkey_cmp_any(a, b, (SELECT_PARAMETERS |
298 | | OSSL_KEYMGMT_SELECT_PUBLIC_KEY)); | |
1e9101c4 RL |
299 | |
300 | /* All legacy keys */ | |
0f113f3e MC |
301 | if (a->type != b->type) |
302 | return -1; | |
303 | ||
1e9101c4 | 304 | if (a->ameth != NULL) { |
0f113f3e MC |
305 | int ret; |
306 | /* Compare parameters if the algorithm has them */ | |
1e9101c4 | 307 | if (a->ameth->param_cmp != NULL) { |
0f113f3e MC |
308 | ret = a->ameth->param_cmp(a, b); |
309 | if (ret <= 0) | |
310 | return ret; | |
311 | } | |
312 | ||
1e9101c4 | 313 | if (a->ameth->pub_cmp != NULL) |
0f113f3e MC |
314 | return a->ameth->pub_cmp(a, b); |
315 | } | |
316 | ||
317 | return -2; | |
318 | } | |
e6526fbf | 319 | |
f929439f MC |
320 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, |
321 | const unsigned char *priv, | |
322 | size_t len) | |
a08802ce MC |
323 | { |
324 | EVP_PKEY *ret = EVP_PKEY_new(); | |
325 | ||
326 | if (ret == NULL | |
8243d8d1 | 327 | || !pkey_set_type(ret, e, type, NULL, -1, NULL)) { |
a08802ce MC |
328 | /* EVPerr already called */ |
329 | goto err; | |
330 | } | |
331 | ||
332 | if (ret->ameth->set_priv_key == NULL) { | |
f929439f | 333 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, |
a08802ce MC |
334 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
335 | goto err; | |
336 | } | |
337 | ||
338 | if (!ret->ameth->set_priv_key(ret, priv, len)) { | |
f929439f | 339 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
340 | goto err; |
341 | } | |
342 | ||
343 | return ret; | |
344 | ||
345 | err: | |
346 | EVP_PKEY_free(ret); | |
347 | return NULL; | |
348 | } | |
349 | ||
f929439f MC |
350 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, |
351 | const unsigned char *pub, | |
352 | size_t len) | |
a08802ce MC |
353 | { |
354 | EVP_PKEY *ret = EVP_PKEY_new(); | |
355 | ||
356 | if (ret == NULL | |
8243d8d1 | 357 | || !pkey_set_type(ret, e, type, NULL, -1, NULL)) { |
a08802ce MC |
358 | /* EVPerr already called */ |
359 | goto err; | |
360 | } | |
361 | ||
362 | if (ret->ameth->set_pub_key == NULL) { | |
f929439f | 363 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, |
a08802ce MC |
364 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
365 | goto err; | |
366 | } | |
367 | ||
368 | if (!ret->ameth->set_pub_key(ret, pub, len)) { | |
f929439f | 369 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
370 | goto err; |
371 | } | |
372 | ||
373 | return ret; | |
374 | ||
375 | err: | |
376 | EVP_PKEY_free(ret); | |
377 | return NULL; | |
378 | } | |
379 | ||
0d124b0a MC |
380 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, |
381 | size_t *len) | |
382 | { | |
acb90ba8 | 383 | /* TODO(3.0) Do we need to do anything about provider side keys? */ |
0d124b0a MC |
384 | if (pkey->ameth->get_priv_key == NULL) { |
385 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, | |
386 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
387 | return 0; | |
388 | } | |
389 | ||
390 | if (!pkey->ameth->get_priv_key(pkey, priv, len)) { | |
391 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
392 | return 0; | |
393 | } | |
394 | ||
395 | return 1; | |
396 | } | |
397 | ||
398 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, | |
399 | size_t *len) | |
400 | { | |
acb90ba8 | 401 | /* TODO(3.0) Do we need to do anything about provider side keys? */ |
0d124b0a MC |
402 | if (pkey->ameth->get_pub_key == NULL) { |
403 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, | |
404 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
405 | return 0; | |
406 | } | |
407 | ||
408 | if (!pkey->ameth->get_pub_key(pkey, pub, len)) { | |
409 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
410 | return 0; | |
411 | } | |
412 | ||
413 | return 1; | |
414 | } | |
415 | ||
b3831fbb MC |
416 | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, |
417 | size_t len, const EVP_CIPHER *cipher) | |
418 | { | |
e683582b SL |
419 | # ifndef OPENSSL_NO_CMAC |
420 | # ifndef OPENSSL_NO_ENGINE | |
9a7846df | 421 | const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL; |
e683582b | 422 | # endif |
e74bd290 RL |
423 | const char *cipher_name = EVP_CIPHER_name(cipher); |
424 | const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher); | |
425 | OPENSSL_CTX *libctx = | |
426 | prov == NULL ? NULL : ossl_provider_library_context(prov); | |
b3831fbb | 427 | EVP_PKEY *ret = EVP_PKEY_new(); |
81ff9eeb | 428 | EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL); |
e74bd290 RL |
429 | EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL; |
430 | OSSL_PARAM params[4]; | |
431 | size_t paramsn = 0; | |
b3831fbb MC |
432 | |
433 | if (ret == NULL | |
8243d8d1 RL |
434 | || cmctx == NULL |
435 | || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1, NULL)) { | |
b3831fbb MC |
436 | /* EVPerr already called */ |
437 | goto err; | |
438 | } | |
439 | ||
e683582b | 440 | # ifndef OPENSSL_NO_ENGINE |
9a7846df | 441 | if (engine_id != NULL) |
e74bd290 | 442 | params[paramsn++] = |
69db3044 | 443 | OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0); |
e683582b | 444 | # endif |
3be06e0d | 445 | |
e74bd290 | 446 | params[paramsn++] = |
703170d4 | 447 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
7f588d20 | 448 | (char *)cipher_name, 0); |
e74bd290 RL |
449 | params[paramsn++] = |
450 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, | |
451 | (char *)priv, len); | |
452 | params[paramsn] = OSSL_PARAM_construct_end(); | |
453 | ||
454 | if (!EVP_MAC_CTX_set_params(cmctx, params)) { | |
b3831fbb MC |
455 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED); |
456 | goto err; | |
457 | } | |
458 | ||
459 | ret->pkey.ptr = cmctx; | |
460 | return ret; | |
461 | ||
462 | err: | |
463 | EVP_PKEY_free(ret); | |
b8d77c9b | 464 | EVP_MAC_CTX_free(cmctx); |
e74bd290 | 465 | EVP_MAC_free(cmac); |
b3831fbb | 466 | return NULL; |
e683582b | 467 | # else |
df6d51e2 MC |
468 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, |
469 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
470 | return NULL; | |
e683582b | 471 | # endif |
b3831fbb | 472 | } |
a08802ce | 473 | |
01b8b3c7 | 474 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) |
0f113f3e | 475 | { |
8243d8d1 | 476 | return pkey_set_type(pkey, NULL, type, NULL, -1, NULL); |
0f113f3e | 477 | } |
01b8b3c7 DSH |
478 | |
479 | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) | |
0f113f3e | 480 | { |
8243d8d1 | 481 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL); |
0f113f3e | 482 | } |
2f2e6b62 JL |
483 | |
484 | int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type) | |
485 | { | |
486 | if (pkey->type == type) { | |
487 | return 1; /* it already is that type */ | |
488 | } | |
489 | ||
490 | /* | |
491 | * The application is requesting to alias this to a different pkey type, | |
492 | * but not one that resolves to the base type. | |
493 | */ | |
494 | if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) { | |
495 | EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); | |
496 | return 0; | |
497 | } | |
498 | ||
499 | pkey->type = type; | |
500 | return 1; | |
501 | } | |
502 | ||
e683582b | 503 | # ifndef OPENSSL_NO_ENGINE |
d19b01ad DSH |
504 | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e) |
505 | { | |
506 | if (e != NULL) { | |
507 | if (!ENGINE_init(e)) { | |
508 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB); | |
509 | return 0; | |
510 | } | |
511 | if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { | |
512 | ENGINE_finish(e); | |
513 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM); | |
514 | return 0; | |
515 | } | |
516 | } | |
517 | ENGINE_finish(pkey->pmeth_engine); | |
518 | pkey->pmeth_engine = e; | |
519 | return 1; | |
520 | } | |
229f7b38 DB |
521 | |
522 | ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey) | |
523 | { | |
524 | return pkey->engine; | |
525 | } | |
e683582b | 526 | # endif |
01b8b3c7 | 527 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
0f113f3e | 528 | { |
f4e4382c RL |
529 | int alias = type; |
530 | ||
ad5b71be | 531 | #ifndef OPENSSL_NO_EC |
f4e4382c RL |
532 | if (EVP_PKEY_type(type) == EVP_PKEY_EC) { |
533 | const EC_GROUP *group = EC_KEY_get0_group(key); | |
534 | ||
535 | if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2) | |
536 | alias = EVP_PKEY_SM2; | |
537 | } | |
ad5b71be | 538 | #endif |
f4e4382c | 539 | |
e34c66c6 | 540 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
0f113f3e | 541 | return 0; |
f4e4382c RL |
542 | if (!EVP_PKEY_set_alias_type(pkey, alias)) |
543 | return 0; | |
0f113f3e MC |
544 | pkey->pkey.ptr = key; |
545 | return (key != NULL); | |
546 | } | |
d02b48c6 | 547 | |
3aeb9348 | 548 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) |
0f113f3e | 549 | { |
acb90ba8 RL |
550 | if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) { |
551 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY); | |
552 | return NULL; | |
553 | } | |
0f113f3e MC |
554 | return pkey->pkey.ptr; |
555 | } | |
db98bbc1 | 556 | |
ebad0b0b NM |
557 | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) |
558 | { | |
559 | ASN1_OCTET_STRING *os = NULL; | |
560 | if (pkey->type != EVP_PKEY_HMAC) { | |
561 | EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY); | |
562 | return NULL; | |
563 | } | |
564 | os = EVP_PKEY_get0(pkey); | |
565 | *len = os->length; | |
566 | return os->data; | |
567 | } | |
568 | ||
e683582b | 569 | # ifndef OPENSSL_NO_POLY1305 |
52ad5b60 TS |
570 | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) |
571 | { | |
572 | ASN1_OCTET_STRING *os = NULL; | |
573 | if (pkey->type != EVP_PKEY_POLY1305) { | |
574 | EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY); | |
575 | return NULL; | |
576 | } | |
577 | os = EVP_PKEY_get0(pkey); | |
578 | *len = os->length; | |
579 | return os->data; | |
580 | } | |
e683582b | 581 | # endif |
52ad5b60 | 582 | |
e683582b | 583 | # ifndef OPENSSL_NO_SIPHASH |
3f5616d7 TS |
584 | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) |
585 | { | |
586 | ASN1_OCTET_STRING *os = NULL; | |
587 | ||
588 | if (pkey->type != EVP_PKEY_SIPHASH) { | |
589 | EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY); | |
590 | return NULL; | |
591 | } | |
592 | os = EVP_PKEY_get0(pkey); | |
593 | *len = os->length; | |
594 | return os->data; | |
595 | } | |
e683582b | 596 | # endif |
3f5616d7 | 597 | |
e683582b | 598 | # ifndef OPENSSL_NO_RSA |
c7cb16a8 | 599 | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) |
52664f50 | 600 | { |
0f113f3e MC |
601 | int ret = EVP_PKEY_assign_RSA(pkey, key); |
602 | if (ret) | |
603 | RSA_up_ref(key); | |
604 | return ret; | |
52664f50 DSH |
605 | } |
606 | ||
9fdcc21f | 607 | RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) |
0f113f3e | 608 | { |
acb90ba8 RL |
609 | if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) { |
610 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY); | |
611 | return NULL; | |
612 | } | |
465a58b1 | 613 | if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) { |
2872dbe1 | 614 | EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY); |
0f113f3e MC |
615 | return NULL; |
616 | } | |
0f113f3e | 617 | return pkey->pkey.rsa; |
f769ce3e | 618 | } |
2872dbe1 DSH |
619 | |
620 | RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) | |
621 | { | |
622 | RSA *ret = EVP_PKEY_get0_RSA(pkey); | |
623 | if (ret != NULL) | |
624 | RSA_up_ref(ret); | |
625 | return ret; | |
626 | } | |
e683582b | 627 | # endif |
f769ce3e | 628 | |
e683582b | 629 | # ifndef OPENSSL_NO_DSA |
c7cb16a8 | 630 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) |
52664f50 | 631 | { |
0f113f3e MC |
632 | int ret = EVP_PKEY_assign_DSA(pkey, key); |
633 | if (ret) | |
634 | DSA_up_ref(key); | |
635 | return ret; | |
52664f50 DSH |
636 | } |
637 | ||
9fdcc21f | 638 | DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) |
0f113f3e | 639 | { |
acb90ba8 RL |
640 | if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) { |
641 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY); | |
642 | return NULL; | |
643 | } | |
0f113f3e | 644 | if (pkey->type != EVP_PKEY_DSA) { |
2872dbe1 | 645 | EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY); |
0f113f3e MC |
646 | return NULL; |
647 | } | |
0f113f3e | 648 | return pkey->pkey.dsa; |
f769ce3e | 649 | } |
2872dbe1 DSH |
650 | |
651 | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) | |
652 | { | |
653 | DSA *ret = EVP_PKEY_get0_DSA(pkey); | |
654 | if (ret != NULL) | |
655 | DSA_up_ref(ret); | |
656 | return ret; | |
657 | } | |
e683582b | 658 | # endif |
f769ce3e | 659 | |
e683582b | 660 | # ifndef OPENSSL_NO_EC |
4d94ae00 | 661 | |
14a7cfb3 | 662 | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) |
4d94ae00 | 663 | { |
0f113f3e MC |
664 | int ret = EVP_PKEY_assign_EC_KEY(pkey, key); |
665 | if (ret) | |
666 | EC_KEY_up_ref(key); | |
667 | return ret; | |
4d94ae00 BM |
668 | } |
669 | ||
9fdcc21f | 670 | EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) |
4d94ae00 | 671 | { |
acb90ba8 RL |
672 | if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) { |
673 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY); | |
674 | return NULL; | |
675 | } | |
f4e4382c | 676 | if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { |
2872dbe1 | 677 | EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); |
0f113f3e MC |
678 | return NULL; |
679 | } | |
0f113f3e | 680 | return pkey->pkey.ec; |
4d94ae00 | 681 | } |
2872dbe1 DSH |
682 | |
683 | EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) | |
684 | { | |
685 | EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey); | |
686 | if (ret != NULL) | |
687 | EC_KEY_up_ref(ret); | |
688 | return ret; | |
689 | } | |
e683582b | 690 | # endif |
4d94ae00 | 691 | |
e683582b | 692 | # ifndef OPENSSL_NO_DH |
52664f50 | 693 | |
c7cb16a8 | 694 | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) |
52664f50 | 695 | { |
32c869ff MC |
696 | int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX; |
697 | int ret = EVP_PKEY_assign(pkey, type, key); | |
698 | ||
0f113f3e MC |
699 | if (ret) |
700 | DH_up_ref(key); | |
701 | return ret; | |
52664f50 DSH |
702 | } |
703 | ||
9fdcc21f | 704 | DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) |
0f113f3e | 705 | { |
acb90ba8 RL |
706 | if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) { |
707 | ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY); | |
708 | return NULL; | |
709 | } | |
0f113f3e | 710 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { |
2872dbe1 | 711 | EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY); |
0f113f3e MC |
712 | return NULL; |
713 | } | |
0f113f3e | 714 | return pkey->pkey.dh; |
f769ce3e | 715 | } |
2872dbe1 DSH |
716 | |
717 | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) | |
718 | { | |
719 | DH *ret = EVP_PKEY_get0_DH(pkey); | |
720 | if (ret != NULL) | |
721 | DH_up_ref(ret); | |
722 | return ret; | |
723 | } | |
e683582b | 724 | # endif |
f769ce3e | 725 | |
6b691a5c | 726 | int EVP_PKEY_type(int type) |
0f113f3e MC |
727 | { |
728 | int ret; | |
729 | const EVP_PKEY_ASN1_METHOD *ameth; | |
730 | ENGINE *e; | |
731 | ameth = EVP_PKEY_asn1_find(&e, type); | |
732 | if (ameth) | |
733 | ret = ameth->pkey_id; | |
734 | else | |
735 | ret = NID_undef; | |
e683582b | 736 | # ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 737 | ENGINE_finish(e); |
e683582b | 738 | # endif |
0f113f3e MC |
739 | return ret; |
740 | } | |
d02b48c6 | 741 | |
7f57b076 | 742 | int EVP_PKEY_id(const EVP_PKEY *pkey) |
0f113f3e MC |
743 | { |
744 | return pkey->type; | |
745 | } | |
7f57b076 DSH |
746 | |
747 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) | |
0f113f3e MC |
748 | { |
749 | return EVP_PKEY_type(pkey->type); | |
750 | } | |
7f57b076 | 751 | |
4f76d62f RL |
752 | int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name) |
753 | { | |
754 | #ifndef FIPS_MODE | |
755 | if (pkey->keymgmt == NULL) { | |
756 | /* | |
757 | * These hard coded cases are pure hackery to get around the fact | |
758 | * that names in crypto/objects/objects.txt are a mess. There is | |
759 | * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's | |
760 | * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1, | |
761 | * the NID of which is used for EVP_PKEY_RSA. Strangely enough, | |
762 | * "DSA" is accurate... but still, better be safe and hard-code | |
763 | * names that we know. | |
764 | * TODO Clean this away along with all other #legacy support. | |
765 | */ | |
766 | int type; | |
767 | ||
768 | if (strcasecmp(name, "RSA") == 0) | |
769 | type = EVP_PKEY_RSA; | |
770 | #ifndef OPENSSL_NO_EC | |
771 | else if (strcasecmp(name, "EC") == 0) | |
772 | type = EVP_PKEY_EC; | |
773 | #endif | |
774 | #ifndef OPENSSL_NO_DSA | |
775 | else if (strcasecmp(name, "DSA") == 0) | |
776 | type = EVP_PKEY_DSA; | |
777 | #endif | |
778 | else | |
779 | type = EVP_PKEY_type(OBJ_sn2nid(name)); | |
780 | return EVP_PKEY_type(pkey->type) == type; | |
781 | } | |
782 | #endif | |
783 | return EVP_KEYMGMT_is_a(pkey->keymgmt, name); | |
784 | } | |
785 | ||
786 | int EVP_PKEY_can_sign(const EVP_PKEY *pkey) | |
787 | { | |
788 | if (pkey->keymgmt == NULL) { | |
789 | switch (EVP_PKEY_base_id(pkey)) { | |
790 | case EVP_PKEY_RSA: | |
791 | return 1; | |
792 | #ifndef OPENSSL_NO_DSA | |
793 | case EVP_PKEY_DSA: | |
794 | return 1; | |
795 | #endif | |
796 | #ifndef OPENSSL_NO_EC | |
797 | case EVP_PKEY_ED25519: | |
798 | case EVP_PKEY_ED448: | |
799 | return 1; | |
800 | case EVP_PKEY_EC: /* Including SM2 */ | |
801 | return EC_KEY_can_sign(pkey->pkey.ec); | |
802 | #endif | |
803 | default: | |
804 | break; | |
805 | } | |
806 | } else { | |
807 | const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt); | |
808 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
809 | const char *supported_sig = | |
810 | pkey->keymgmt->query_operation_name != NULL | |
811 | ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE) | |
812 | : evp_first_name(prov, pkey->keymgmt->name_id); | |
813 | EVP_SIGNATURE *signature = NULL; | |
814 | ||
815 | signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL); | |
816 | if (signature != NULL) { | |
817 | EVP_SIGNATURE_free(signature); | |
818 | return 1; | |
819 | } | |
820 | } | |
821 | return 0; | |
822 | } | |
d02b48c6 | 823 | |
c2041da8 RL |
824 | #ifndef OPENSSL_NO_EC |
825 | /* | |
826 | * TODO rewrite when we have proper data extraction functions | |
827 | * Note: an octet pointer would be desirable! | |
828 | */ | |
829 | static OSSL_CALLBACK get_ec_curve_name_cb; | |
830 | static int get_ec_curve_name_cb(const OSSL_PARAM params[], void *arg) | |
831 | { | |
832 | const OSSL_PARAM *p = NULL; | |
833 | ||
834 | if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL) | |
835 | return OSSL_PARAM_get_utf8_string(p, arg, 0); | |
836 | ||
837 | /* If there is no curve name, this is not an EC key */ | |
838 | return 0; | |
839 | } | |
840 | ||
841 | int evp_pkey_get_EC_KEY_curve_nid(const EVP_PKEY *pkey) | |
842 | { | |
843 | int ret = NID_undef; | |
844 | ||
845 | if (pkey->keymgmt == NULL) { | |
846 | if (EVP_PKEY_base_id(pkey) == EVP_PKEY_EC) { | |
847 | EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey); | |
848 | ||
849 | ret = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec)); | |
850 | } | |
851 | } else if (EVP_PKEY_is_a(pkey, "EC") || EVP_PKEY_is_a(pkey, "SM2")) { | |
852 | char *curve_name = NULL; | |
853 | ||
854 | ret = evp_keymgmt_export(pkey->keymgmt, pkey->keydata, | |
855 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, | |
856 | get_ec_curve_name_cb, &curve_name); | |
857 | if (ret) | |
858 | ret = ec_curve_name2nid(curve_name); | |
859 | OPENSSL_free(curve_name); | |
860 | } | |
861 | ||
862 | return ret; | |
863 | } | |
864 | #endif | |
865 | ||
f1299839 RL |
866 | static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent) |
867 | { | |
868 | BIO_set_indent(*out, saved_indent); | |
869 | if (pop_f_prefix) { | |
870 | BIO *next = BIO_pop(*out); | |
871 | ||
872 | BIO_free(*out); | |
873 | *out = next; | |
874 | } | |
875 | return 1; | |
876 | } | |
877 | ||
878 | static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent, | |
879 | long indent) | |
880 | { | |
881 | *pop_f_prefix = 0; | |
882 | *saved_indent = 0; | |
883 | if (indent > 0) { | |
884 | long i = BIO_get_indent(*out); | |
885 | ||
886 | *saved_indent = (i < 0 ? 0 : i); | |
887 | if (BIO_set_indent(*out, indent) <= 0) { | |
888 | if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL) | |
889 | return 0; | |
890 | *pop_f_prefix = 1; | |
891 | } | |
892 | if (BIO_set_indent(*out, indent) <= 0) { | |
893 | print_reset_indent(out, *pop_f_prefix, *saved_indent); | |
894 | return 0; | |
895 | } | |
896 | } | |
897 | return 1; | |
898 | } | |
899 | ||
35208f36 | 900 | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
0f113f3e MC |
901 | const char *kstr) |
902 | { | |
5310a4e6 P |
903 | return BIO_indent(out, indent, 128) |
904 | && BIO_printf(out, "%s algorithm \"%s\" unsupported\n", | |
905 | kstr, OBJ_nid2ln(pkey->type)) > 0; | |
0f113f3e | 906 | } |
35208f36 | 907 | |
f1299839 RL |
908 | static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent, |
909 | const char *propquery /* For provided serialization */, | |
910 | int (*legacy_print)(BIO *out, const EVP_PKEY *pkey, | |
911 | int indent, ASN1_PCTX *pctx), | |
912 | ASN1_PCTX *legacy_pctx /* For legacy print */) | |
0f113f3e | 913 | { |
f1299839 RL |
914 | int pop_f_prefix; |
915 | long saved_indent; | |
916 | OSSL_SERIALIZER_CTX *ctx = NULL; | |
917 | int ret = -2; /* default to unsupported */ | |
918 | ||
919 | if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent)) | |
920 | return 0; | |
54c1711f | 921 | |
f1299839 | 922 | ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, propquery); |
54c1711f RL |
923 | if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) |
924 | ret = OSSL_SERIALIZER_to_bio(ctx, out); | |
925 | OSSL_SERIALIZER_CTX_free(ctx); | |
926 | ||
927 | if (ret != -2) | |
f1299839 | 928 | goto end; |
54c1711f RL |
929 | |
930 | /* legacy fallback */ | |
f1299839 RL |
931 | if (legacy_print != NULL) |
932 | ret = legacy_print(out, pkey, 0, legacy_pctx); | |
933 | else | |
934 | ret = unsup_alg(out, pkey, 0, "Public Key"); | |
0f113f3e | 935 | |
f1299839 RL |
936 | end: |
937 | print_reset_indent(&out, pop_f_prefix, saved_indent); | |
938 | return ret; | |
939 | } | |
940 | ||
941 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, | |
942 | int indent, ASN1_PCTX *pctx) | |
943 | { | |
944 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ, | |
945 | (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL), | |
946 | pctx); | |
0f113f3e | 947 | } |
35208f36 DSH |
948 | |
949 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
950 | int indent, ASN1_PCTX *pctx) |
951 | { | |
f1299839 RL |
952 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ, |
953 | (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL), | |
954 | pctx); | |
0f113f3e | 955 | } |
35208f36 DSH |
956 | |
957 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
958 | int indent, ASN1_PCTX *pctx) |
959 | { | |
f1299839 RL |
960 | return print_pkey(pkey, out, indent, OSSL_SERIALIZER_Parameters_TO_TEXT_PQ, |
961 | (pkey->ameth != NULL ? pkey->ameth->param_print : NULL), | |
962 | pctx); | |
0f113f3e | 963 | } |
03919683 | 964 | |
ead0d234 RL |
965 | static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op, |
966 | int arg1, void *arg2) | |
967 | { | |
3c6ed955 | 968 | if (pkey->keymgmt == NULL) |
ead0d234 RL |
969 | return 0; |
970 | switch (op) { | |
971 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: | |
972 | { | |
973 | char mdname[80] = ""; | |
974 | int nid; | |
975 | int rv = EVP_PKEY_get_default_digest_name(pkey, mdname, | |
976 | sizeof(mdname)); | |
977 | ||
978 | if (rv <= 0) | |
979 | return rv; | |
980 | nid = OBJ_sn2nid(mdname); | |
981 | if (nid == NID_undef) | |
982 | nid = OBJ_ln2nid(mdname); | |
983 | if (nid == NID_undef) | |
984 | return 0; | |
985 | *(int *)arg2 = nid; | |
986 | return 1; | |
987 | } | |
988 | default: | |
989 | return -2; | |
990 | } | |
991 | } | |
992 | ||
5d6aaf8a | 993 | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
0f113f3e | 994 | { |
ead0d234 RL |
995 | if (pkey->ameth == NULL) |
996 | return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2); | |
997 | if (pkey->ameth->pkey_ctrl == NULL) | |
0f113f3e | 998 | return -2; |
5d6aaf8a DSH |
999 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
1000 | } | |
1001 | ||
1002 | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) | |
1003 | { | |
1004 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); | |
1005 | } | |
1006 | ||
ead0d234 RL |
1007 | int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey, |
1008 | char *mdname, size_t mdname_sz) | |
1009 | { | |
1010 | if (pkey->ameth == NULL) { | |
1011 | OSSL_PARAM params[3]; | |
1012 | char mddefault[100] = ""; | |
1013 | char mdmandatory[100] = ""; | |
1014 | ||
1015 | params[0] = | |
1016 | OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, | |
1017 | mddefault, sizeof(mddefault)); | |
1018 | params[1] = | |
1019 | OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_MANDATORY_DIGEST, | |
1020 | mdmandatory, | |
1021 | sizeof(mdmandatory)); | |
1022 | params[2] = OSSL_PARAM_construct_end(); | |
3c6ed955 | 1023 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) |
ead0d234 RL |
1024 | return 0; |
1025 | if (mdmandatory[0] != '\0') { | |
1026 | OPENSSL_strlcpy(mdname, mdmandatory, mdname_sz); | |
1027 | return 2; | |
1028 | } | |
1029 | OPENSSL_strlcpy(mdname, mddefault, mdname_sz); | |
1030 | return 1; | |
1031 | } | |
1032 | ||
1033 | { | |
1034 | int nid = NID_undef; | |
1035 | int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid); | |
1036 | const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL; | |
1037 | ||
1038 | if (rv > 0) | |
1039 | OPENSSL_strlcpy(mdname, name, mdname_sz); | |
1040 | return rv; | |
1041 | } | |
1042 | } | |
1043 | ||
ecbb2fca DW |
1044 | int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid) |
1045 | { | |
1046 | int rv, default_nid; | |
1047 | ||
1048 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL); | |
1049 | if (rv == -2) { | |
1050 | /* | |
1051 | * If there is a mandatory default digest and this isn't it, then | |
1052 | * the answer is 'no'. | |
1053 | */ | |
1054 | rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid); | |
1055 | if (rv == 2) | |
1056 | return (nid == default_nid); | |
1057 | /* zero is an error from EVP_PKEY_get_default_digest_nid() */ | |
1058 | if (rv == 0) | |
1059 | return -1; | |
1060 | } | |
1061 | return rv; | |
1062 | } | |
1063 | ||
5d6aaf8a DSH |
1064 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, |
1065 | const unsigned char *pt, size_t ptlen) | |
1066 | { | |
1067 | if (ptlen > INT_MAX) | |
1068 | return 0; | |
1069 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen, | |
1070 | (void *)pt) <= 0) | |
1071 | return 0; | |
1072 | return 1; | |
1073 | } | |
1074 | ||
1075 | size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt) | |
1076 | { | |
1077 | int rv; | |
1078 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt); | |
1079 | if (rv <= 0) | |
1080 | return 0; | |
1081 | return rv; | |
0f113f3e | 1082 | } |
e683582b SL |
1083 | |
1084 | #endif /* FIPS_MODE */ | |
1085 | ||
1086 | /*- All methods below can also be used in FIPS_MODE */ | |
1087 | ||
1088 | EVP_PKEY *EVP_PKEY_new(void) | |
1089 | { | |
1090 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); | |
1091 | ||
1092 | if (ret == NULL) { | |
1093 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
1094 | return NULL; | |
1095 | } | |
1096 | ret->type = EVP_PKEY_NONE; | |
1097 | ret->save_type = EVP_PKEY_NONE; | |
1098 | ret->references = 1; | |
1099 | ret->save_parameters = 1; | |
1100 | ret->lock = CRYPTO_THREAD_lock_new(); | |
1101 | if (ret->lock == NULL) { | |
1102 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
ff1f7cde AT |
1103 | goto err; |
1104 | } | |
1105 | #ifndef FIPS_MODE | |
1106 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) { | |
1107 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
1108 | goto err; | |
e683582b | 1109 | } |
ff1f7cde | 1110 | #endif |
e683582b | 1111 | return ret; |
ff1f7cde AT |
1112 | |
1113 | err: | |
1114 | CRYPTO_THREAD_lock_free(ret->lock); | |
1115 | OPENSSL_free(ret); | |
1116 | return NULL; | |
e683582b SL |
1117 | } |
1118 | ||
8243d8d1 RL |
1119 | /* |
1120 | * Setup a public key management method. | |
1121 | * | |
1122 | * For legacy keys, either |type| or |str| is expected to have the type | |
1123 | * information. In this case, the setup consists of finding an ASN1 method | |
1124 | * and potentially an ENGINE, and setting those fields in |pkey|. | |
1125 | * | |
1126 | * For provider side keys, |keymgmt| is expected to be non-NULL. In this | |
1127 | * case, the setup consists of setting the |keymgmt| field in |pkey|. | |
1128 | * | |
1129 | * If pkey is NULL just return 1 or 0 if the key management method exists. | |
1130 | */ | |
1131 | ||
1132 | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, | |
1133 | int len, EVP_KEYMGMT *keymgmt) | |
1134 | { | |
1135 | #ifndef FIPS_MODE | |
1136 | const EVP_PKEY_ASN1_METHOD *ameth = NULL; | |
1137 | ENGINE **eptr = (e == NULL) ? &e : NULL; | |
1138 | #endif | |
1139 | ||
1140 | /* | |
1141 | * The setups can't set both legacy and provider side methods. | |
1142 | * It is forbidden | |
1143 | */ | |
1144 | if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL) | |
1145 | || !ossl_assert(e == NULL || keymgmt == NULL)) { | |
1146 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
1147 | return 0; | |
1148 | } | |
1149 | ||
1150 | if (pkey != NULL) { | |
1151 | int free_it = 0; | |
1152 | ||
1153 | #ifndef FIPS_MODE | |
1154 | free_it = free_it || pkey->pkey.ptr != NULL; | |
1155 | #endif | |
1156 | free_it = free_it || pkey->keydata != NULL; | |
1157 | if (free_it) | |
1158 | evp_pkey_free_it(pkey); | |
1159 | #ifndef FIPS_MODE | |
1160 | /* | |
1161 | * If key type matches and a method exists then this lookup has | |
1162 | * succeeded once so just indicate success. | |
1163 | */ | |
1164 | if (pkey->type != EVP_PKEY_NONE | |
1165 | && type == pkey->save_type | |
1166 | && pkey->ameth != NULL) | |
1167 | return 1; | |
1168 | # ifndef OPENSSL_NO_ENGINE | |
1169 | /* If we have ENGINEs release them */ | |
1170 | ENGINE_finish(pkey->engine); | |
1171 | pkey->engine = NULL; | |
1172 | ENGINE_finish(pkey->pmeth_engine); | |
1173 | pkey->pmeth_engine = NULL; | |
1174 | # endif | |
1175 | #endif | |
1176 | } | |
1177 | #ifndef FIPS_MODE | |
1178 | if (str != NULL) | |
1179 | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); | |
1180 | else if (type != EVP_PKEY_NONE) | |
1181 | ameth = EVP_PKEY_asn1_find(eptr, type); | |
1182 | # ifndef OPENSSL_NO_ENGINE | |
1183 | if (pkey == NULL && eptr != NULL) | |
1184 | ENGINE_finish(e); | |
1185 | # endif | |
1186 | #endif | |
1187 | ||
1188 | ||
1189 | { | |
1190 | int check = 1; | |
1191 | ||
1192 | #ifndef FIPS_MODE | |
1193 | check = check && ameth == NULL; | |
1194 | #endif | |
1195 | check = check && keymgmt == NULL; | |
1196 | if (check) { | |
1197 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); | |
1198 | return 0; | |
1199 | } | |
1200 | } | |
1201 | if (pkey != NULL) { | |
1202 | if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) { | |
1203 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
1204 | return 0; | |
1205 | } | |
1206 | ||
1207 | pkey->keymgmt = keymgmt; | |
1208 | ||
1209 | pkey->save_type = type; | |
1210 | pkey->type = type; | |
1211 | ||
1212 | #ifndef FIPS_MODE | |
1213 | /* | |
1214 | * If the internal "origin" key is provider side, don't save |ameth|. | |
1215 | * The main reason is that |ameth| is one factor to detect that the | |
1216 | * internal "origin" key is a legacy one. | |
1217 | */ | |
1218 | if (keymgmt == NULL) | |
1219 | pkey->ameth = ameth; | |
1220 | pkey->engine = e; | |
1221 | ||
1222 | /* | |
1223 | * The EVP_PKEY_ASN1_METHOD |pkey_id| serves different purposes, | |
1224 | * depending on if we're setting this key to contain a legacy or | |
1225 | * a provider side "origin" key. For a legacy key, we assign it | |
1226 | * to the |type| field, but for a provider side key, we assign it | |
1227 | * to the |save_type| field, because |type| is supposed to be set | |
1228 | * to EVP_PKEY_NONE in that case. | |
1229 | */ | |
1230 | if (keymgmt != NULL) | |
1231 | pkey->save_type = ameth->pkey_id; | |
1232 | else if (pkey->ameth != NULL) | |
1233 | pkey->type = ameth->pkey_id; | |
1234 | #endif | |
1235 | } | |
1236 | return 1; | |
1237 | } | |
1238 | ||
1239 | #ifndef FIPS_MODE | |
1240 | static void find_ameth(const char *name, void *data) | |
1241 | { | |
1242 | const char **str = data; | |
1243 | ||
1244 | /* | |
1245 | * The error messages from pkey_set_type() are uninteresting here, | |
1246 | * and misleading. | |
1247 | */ | |
1248 | ERR_set_mark(); | |
1249 | ||
1250 | if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name), | |
1251 | NULL)) { | |
1252 | if (str[0] == NULL) | |
1253 | str[0] = name; | |
1254 | else if (str[1] == NULL) | |
1255 | str[1] = name; | |
1256 | } | |
1257 | ||
1258 | ERR_pop_to_mark(); | |
1259 | } | |
1260 | #endif | |
1261 | ||
1262 | int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt) | |
1263 | { | |
1264 | #ifndef FIPS_MODE | |
1265 | # define EVP_PKEY_TYPE_STR str[0] | |
1266 | # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0])) | |
1267 | /* | |
1268 | * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD | |
1269 | * Ideally, only one should be found. If two (or more) are found, the | |
1270 | * match is ambiguous. This should never happen, but... | |
1271 | */ | |
1272 | const char *str[2] = { NULL, NULL }; | |
1273 | ||
1274 | EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str); | |
1275 | if (str[1] != NULL) { | |
1276 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
1277 | return 0; | |
1278 | } | |
1279 | #else | |
1280 | # define EVP_PKEY_TYPE_STR NULL | |
1281 | # define EVP_PKEY_TYPE_STRLEN -1 | |
1282 | #endif | |
1283 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, | |
1284 | EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN, | |
1285 | keymgmt); | |
1286 | ||
1287 | #undef EVP_PKEY_TYPE_STR | |
1288 | #undef EVP_PKEY_TYPE_STRLEN | |
1289 | } | |
1290 | ||
e683582b SL |
1291 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) |
1292 | { | |
1293 | int i; | |
1294 | ||
1295 | if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0) | |
1296 | return 0; | |
1297 | ||
1298 | REF_PRINT_COUNT("EVP_PKEY", pkey); | |
1299 | REF_ASSERT_ISNT(i < 2); | |
1300 | return ((i > 1) ? 1 : 0); | |
1301 | } | |
1302 | ||
badf51c8 | 1303 | #ifndef FIPS_MODE |
62924755 | 1304 | void evp_pkey_free_legacy(EVP_PKEY *x) |
badf51c8 RL |
1305 | { |
1306 | if (x->ameth != NULL) { | |
ff3b59e1 | 1307 | if (x->ameth->pkey_free != NULL) |
badf51c8 RL |
1308 | x->ameth->pkey_free(x); |
1309 | x->pkey.ptr = NULL; | |
badf51c8 RL |
1310 | } |
1311 | # ifndef OPENSSL_NO_ENGINE | |
1312 | ENGINE_finish(x->engine); | |
1313 | x->engine = NULL; | |
1314 | ENGINE_finish(x->pmeth_engine); | |
1315 | x->pmeth_engine = NULL; | |
1316 | # endif | |
8243d8d1 | 1317 | x->type = EVP_PKEY_NONE; |
badf51c8 RL |
1318 | } |
1319 | #endif /* FIPS_MODE */ | |
1320 | ||
e683582b SL |
1321 | static void evp_pkey_free_it(EVP_PKEY *x) |
1322 | { | |
1323 | /* internal function; x is never NULL */ | |
1324 | ||
3c6ed955 | 1325 | evp_keymgmt_util_clear_operation_cache(x); |
badf51c8 RL |
1326 | #ifndef FIPS_MODE |
1327 | evp_pkey_free_legacy(x); | |
1328 | #endif | |
e683582b | 1329 | |
3c6ed955 RL |
1330 | if (x->keymgmt != NULL) { |
1331 | evp_keymgmt_freedata(x->keymgmt, x->keydata); | |
1332 | EVP_KEYMGMT_free(x->keymgmt); | |
1333 | x->keymgmt = NULL; | |
1334 | x->keydata = NULL; | |
1335 | } | |
e683582b SL |
1336 | } |
1337 | ||
1338 | void EVP_PKEY_free(EVP_PKEY *x) | |
1339 | { | |
1340 | int i; | |
1341 | ||
1342 | if (x == NULL) | |
1343 | return; | |
1344 | ||
1345 | CRYPTO_DOWN_REF(&x->references, &i, x->lock); | |
1346 | REF_PRINT_COUNT("EVP_PKEY", x); | |
1347 | if (i > 0) | |
1348 | return; | |
1349 | REF_ASSERT_ISNT(i < 0); | |
1350 | evp_pkey_free_it(x); | |
ff1f7cde AT |
1351 | #ifndef FIPS_MODE |
1352 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data); | |
1353 | #endif | |
e683582b SL |
1354 | CRYPTO_THREAD_lock_free(x->lock); |
1355 | #ifndef FIPS_MODE | |
1356 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); | |
1357 | #endif | |
1358 | OPENSSL_free(x); | |
1359 | } | |
1360 | ||
e683582b SL |
1361 | int EVP_PKEY_size(const EVP_PKEY *pkey) |
1362 | { | |
adc9f731 RL |
1363 | int size = 0; |
1364 | ||
6508e858 | 1365 | if (pkey != NULL) { |
adc9f731 RL |
1366 | size = pkey->cache.size; |
1367 | #ifndef FIPS_MODE | |
1368 | if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL) | |
1369 | size = pkey->ameth->pkey_size(pkey); | |
1370 | #endif | |
6508e858 | 1371 | } |
adc9f731 | 1372 | return size; |
e683582b | 1373 | } |
f6aa5774 | 1374 | |
3c6ed955 RL |
1375 | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OPENSSL_CTX *libctx, |
1376 | EVP_KEYMGMT **keymgmt, | |
1377 | const char *propquery) | |
f6aa5774 RL |
1378 | { |
1379 | EVP_KEYMGMT *allocated_keymgmt = NULL; | |
1380 | EVP_KEYMGMT *tmp_keymgmt = NULL; | |
b305452f | 1381 | void *keydata = NULL; |
adc9f731 | 1382 | int check; |
f6aa5774 RL |
1383 | |
1384 | if (pk == NULL) | |
1385 | return NULL; | |
1386 | ||
adc9f731 RL |
1387 | /* No key data => nothing to export */ |
1388 | check = 1; | |
1389 | #ifndef FIPS_MODE | |
1390 | check = check && pk->pkey.ptr == NULL; | |
1391 | #endif | |
1392 | check = check && pk->keydata == NULL; | |
1393 | if (check) | |
1394 | return NULL; | |
1395 | ||
3f7ce7f1 | 1396 | #ifndef FIPS_MODE |
3f7ce7f1 | 1397 | if (pk->pkey.ptr != NULL) { |
3f7ce7f1 | 1398 | /* |
3c6ed955 RL |
1399 | * If the legacy key doesn't have an dirty counter or export function, |
1400 | * give up | |
3f7ce7f1 | 1401 | */ |
3c6ed955 RL |
1402 | if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL) |
1403 | return NULL; | |
3f7ce7f1 RL |
1404 | } |
1405 | #endif | |
1406 | ||
3c6ed955 RL |
1407 | if (keymgmt != NULL) { |
1408 | tmp_keymgmt = *keymgmt; | |
1409 | *keymgmt = NULL; | |
1410 | } | |
1411 | ||
4b9e90f4 RL |
1412 | /* |
1413 | * If no keymgmt was given or found, get a default keymgmt. We do so by | |
1414 | * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it. | |
1415 | */ | |
f6aa5774 | 1416 | if (tmp_keymgmt == NULL) { |
2ee4a50a | 1417 | EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery); |
f6aa5774 | 1418 | |
4b9e90f4 RL |
1419 | tmp_keymgmt = ctx->keymgmt; |
1420 | ctx->keymgmt = NULL; | |
f6aa5774 RL |
1421 | EVP_PKEY_CTX_free(ctx); |
1422 | } | |
1423 | ||
3c6ed955 | 1424 | /* If there's still no keymgmt to be had, give up */ |
3f7ce7f1 RL |
1425 | if (tmp_keymgmt == NULL) |
1426 | goto end; | |
f6aa5774 | 1427 | |
3f7ce7f1 RL |
1428 | #ifndef FIPS_MODE |
1429 | if (pk->pkey.ptr != NULL) { | |
3c6ed955 | 1430 | size_t i = 0; |
3f7ce7f1 RL |
1431 | |
1432 | /* | |
3c6ed955 RL |
1433 | * If the legacy "origin" hasn't changed since last time, we try |
1434 | * to find our keymgmt in the operation cache. If it has changed, | |
1435 | * |i| remains zero, and we will clear the cache further down. | |
3f7ce7f1 | 1436 | */ |
3c6ed955 RL |
1437 | if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) { |
1438 | i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt); | |
1439 | ||
1440 | /* | |
1441 | * If |tmp_keymgmt| is present in the operation cache, it means | |
1442 | * that export doesn't need to be redone. In that case, we take | |
1443 | * token copies of the cached pointers, to have token success | |
1444 | * values to return. | |
1445 | */ | |
1446 | if (i < OSSL_NELEM(pk->operation_cache) | |
1447 | && pk->operation_cache[i].keymgmt != NULL) { | |
1448 | keydata = pk->operation_cache[i].keydata; | |
1449 | goto end; | |
1450 | } | |
3f7ce7f1 RL |
1451 | } |
1452 | ||
1453 | /* | |
3c6ed955 RL |
1454 | * TODO(3.0) Right now, we assume we have ample space. We will have |
1455 | * to think about a cache aging scheme, though, if |i| indexes outside | |
1456 | * the array. | |
3f7ce7f1 | 1457 | */ |
3c6ed955 | 1458 | if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache))) |
3f7ce7f1 RL |
1459 | goto end; |
1460 | ||
1461 | /* Make sure that the keymgmt key type matches the legacy NID */ | |
1462 | if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))) | |
1463 | goto end; | |
1464 | ||
1465 | if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL) | |
1466 | goto end; | |
1467 | ||
76e23fc5 | 1468 | if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) { |
3f7ce7f1 RL |
1469 | evp_keymgmt_freedata(tmp_keymgmt, keydata); |
1470 | keydata = NULL; | |
1471 | goto end; | |
1472 | } | |
1473 | ||
3c6ed955 RL |
1474 | /* |
1475 | * If the dirty counter changed since last time, then clear the | |
1476 | * operation cache. In that case, we know that |i| is zero. Just | |
1477 | * in case this is a re-export, we increment then decrement the | |
1478 | * keymgmt reference counter. | |
1479 | */ | |
1480 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */ | |
1481 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1482 | keydata = NULL; | |
1483 | goto end; | |
1484 | } | |
1485 | if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy) | |
1486 | evp_keymgmt_util_clear_operation_cache(pk); | |
1487 | EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */ | |
1488 | ||
1489 | /* Add the new export to the operation cache */ | |
1490 | if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) { | |
1491 | evp_keymgmt_freedata(tmp_keymgmt, keydata); | |
1492 | keydata = NULL; | |
1493 | goto end; | |
1494 | } | |
3f7ce7f1 RL |
1495 | |
1496 | /* Synchronize the dirty count */ | |
1497 | pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk); | |
1498 | goto end; | |
1499 | } | |
1500 | #endif /* FIPS_MODE */ | |
1501 | ||
1502 | keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt); | |
1503 | ||
1504 | end: | |
f6aa5774 RL |
1505 | /* |
1506 | * If nothing was exported, |tmp_keymgmt| might point at a freed | |
1507 | * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for | |
1508 | * the caller either way in that case. | |
1509 | */ | |
b305452f | 1510 | if (keydata == NULL) |
f6aa5774 RL |
1511 | tmp_keymgmt = NULL; |
1512 | ||
1513 | if (keymgmt != NULL) | |
1514 | *keymgmt = tmp_keymgmt; | |
1515 | ||
1516 | EVP_KEYMGMT_free(allocated_keymgmt); | |
b305452f | 1517 | return keydata; |
f6aa5774 | 1518 | } |
badf51c8 RL |
1519 | |
1520 | #ifndef FIPS_MODE | |
acb90ba8 | 1521 | int evp_pkey_downgrade(EVP_PKEY *pk) |
badf51c8 | 1522 | { |
acb90ba8 RL |
1523 | EVP_KEYMGMT *keymgmt = pk->keymgmt; |
1524 | void *keydata = pk->keydata; | |
1525 | int type = pk->save_type; | |
1526 | const char *keytype = NULL; | |
badf51c8 | 1527 | |
acb90ba8 RL |
1528 | /* If this isn't a provider side key, we're done */ |
1529 | if (keymgmt == NULL) | |
1530 | return 1; | |
1531 | ||
1532 | /* Get the key type name for error reporting */ | |
1533 | if (type != EVP_PKEY_NONE) | |
1534 | keytype = OBJ_nid2sn(type); | |
1535 | else | |
1536 | keytype = | |
1537 | evp_first_name(EVP_KEYMGMT_provider(keymgmt), keymgmt->name_id); | |
badf51c8 RL |
1538 | |
1539 | /* | |
acb90ba8 RL |
1540 | * |save_type| was set when any of the EVP_PKEY_set_type functions |
1541 | * was called. It was set to EVP_PKEY_NONE if the key type wasn't | |
1542 | * recognised to be any of the legacy key types, and the downgrade | |
1543 | * isn't possible. | |
badf51c8 | 1544 | */ |
acb90ba8 RL |
1545 | if (type == EVP_PKEY_NONE) { |
1546 | ERR_raise_data(ERR_LIB_EVP, EVP_R_UNKNOWN_KEY_TYPE, | |
1547 | "key type = %s, can't downgrade", keytype); | |
1548 | return 0; | |
badf51c8 RL |
1549 | } |
1550 | ||
acb90ba8 RL |
1551 | /* |
1552 | * To be able to downgrade, we steal the provider side "origin" keymgmt | |
1553 | * and keydata. We've already grabbed the pointers, so all we need to | |
1554 | * do is clear those pointers in |pk| and then call evp_pkey_free_it(). | |
1555 | * That way, we can restore |pk| if we need to. | |
1556 | */ | |
1557 | pk->keymgmt = NULL; | |
1558 | pk->keydata = NULL; | |
1559 | evp_pkey_free_it(pk); | |
1560 | if (EVP_PKEY_set_type(pk, type)) { | |
1561 | /* If the key is typed but empty, we're done */ | |
1562 | if (keydata == NULL) | |
1563 | return 1; | |
badf51c8 | 1564 | |
acb90ba8 RL |
1565 | if (pk->ameth->import_from == NULL) { |
1566 | ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION, | |
1567 | "key type = %s", keytype); | |
1568 | } else if (evp_keymgmt_export(keymgmt, keydata, | |
1569 | OSSL_KEYMGMT_SELECT_ALL, | |
1570 | pk->ameth->import_from, pk)) { | |
1571 | /* | |
1572 | * Save the provider side data in the operation cache, so they'll | |
1573 | * find it again. evp_pkey_free_it() cleared the cache, so it's | |
1574 | * safe to assume slot zero is free. | |
1575 | * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's | |
1576 | * reference count. | |
1577 | */ | |
1578 | evp_keymgmt_util_cache_keydata(pk, 0, keymgmt, keydata); | |
badf51c8 | 1579 | |
acb90ba8 RL |
1580 | /* Synchronize the dirty count */ |
1581 | pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk); | |
1582 | return 1; | |
badf51c8 RL |
1583 | } |
1584 | ||
acb90ba8 RL |
1585 | ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE, |
1586 | "key type = %s", keytype); | |
badf51c8 RL |
1587 | } |
1588 | ||
badf51c8 | 1589 | /* |
acb90ba8 RL |
1590 | * Something went wrong. This could for example happen if the keymgmt |
1591 | * turns out to be an HSM implementation that refuses to let go of some | |
1592 | * of the key data, typically the private bits. In this case, we restore | |
1593 | * the provider side internal "origin" and leave it at that. | |
badf51c8 | 1594 | */ |
acb90ba8 RL |
1595 | if (!ossl_assert(EVP_PKEY_set_type_by_keymgmt(pk, keymgmt))) { |
1596 | /* This should not be impossible */ | |
1597 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | |
1598 | return 0; | |
1599 | } | |
1600 | pk->keydata = keydata; | |
1601 | evp_keymgmt_util_cache_keyinfo(pk); | |
1602 | return 0; /* No downgrade, but at least the key is restored */ | |
badf51c8 RL |
1603 | } |
1604 | #endif /* FIPS_MODE */ | |
96ebe52e SL |
1605 | |
1606 | const OSSL_PARAM *EVP_PKEY_gettable_params(EVP_PKEY *pkey) | |
1607 | { | |
1608 | if (pkey == NULL | |
1609 | || pkey->keymgmt == NULL | |
1610 | || pkey->keydata == NULL) | |
1611 | return 0; | |
1612 | return evp_keymgmt_gettable_params(pkey->keymgmt); | |
1613 | } | |
1614 | ||
1615 | /* | |
1616 | * For the following methods param->return_size is set to a value | |
1617 | * larger than can be returned by the call to evp_keymgmt_get_params(). | |
1618 | * If it is still this value then the parameter was ignored - and in this | |
1619 | * case it returns an error.. | |
1620 | */ | |
1621 | ||
1622 | int EVP_PKEY_get_bn_param(EVP_PKEY *pkey, const char *key_name, BIGNUM **bn) | |
1623 | { | |
1624 | int ret = 0; | |
1625 | OSSL_PARAM params[2]; | |
1626 | unsigned char buffer[2048]; | |
1627 | /* | |
1628 | * Use -1 as the terminator here instead of sizeof(buffer) + 1 since | |
1629 | * -1 is less likely to be a valid value. | |
1630 | */ | |
1631 | const size_t not_set = (size_t)-1; | |
1632 | unsigned char *buf = NULL; | |
1633 | size_t buf_sz = 0; | |
1634 | ||
1635 | if (pkey == NULL | |
1636 | || pkey->keymgmt == NULL | |
1637 | || pkey->keydata == NULL | |
1638 | || key_name == NULL | |
1639 | || bn == NULL) | |
1640 | return 0; | |
1641 | ||
1642 | memset(buffer, 0, sizeof(buffer)); | |
1643 | params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer)); | |
1644 | /* If the return_size is still not_set then we know it was not found */ | |
1645 | params[0].return_size = not_set; | |
1646 | params[1] = OSSL_PARAM_construct_end(); | |
1647 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) { | |
1648 | if (params[0].return_size == not_set | |
1649 | || params[0].return_size == 0) | |
1650 | return 0; | |
1651 | buf_sz = params[0].return_size; | |
1652 | /* | |
1653 | * If it failed because the buffer was too small then allocate the | |
1654 | * required buffer size and retry. | |
1655 | */ | |
1656 | buf = OPENSSL_zalloc(buf_sz); | |
1657 | if (buf == NULL) | |
1658 | return 0; | |
1659 | params[0].data = buf; | |
1660 | params[0].data_size = buf_sz; | |
1661 | ||
1662 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) | |
1663 | goto err; | |
1664 | } | |
1665 | /* Fail if the param was not found */ | |
1666 | if (params[0].return_size == not_set) | |
1667 | goto err; | |
1668 | ret = OSSL_PARAM_get_BN(params, bn); | |
1669 | err: | |
1670 | OPENSSL_free(buf); | |
1671 | return ret; | |
1672 | } | |
1673 | ||
1674 | int EVP_PKEY_get_octet_string_param(EVP_PKEY *pkey, const char *key_name, | |
1675 | unsigned char *buf, size_t max_buf_sz, | |
1676 | size_t *out_sz) | |
1677 | { | |
1678 | OSSL_PARAM params[2]; | |
1679 | const size_t not_set = max_buf_sz + 1; | |
1680 | ||
1681 | if (pkey == NULL | |
1682 | || pkey->keymgmt == NULL | |
1683 | || pkey->keydata == NULL | |
1684 | || key_name == NULL) | |
1685 | return 0; | |
1686 | ||
1687 | params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz); | |
1688 | params[0].return_size = not_set; | |
1689 | params[1] = OSSL_PARAM_construct_end(); | |
1690 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) | |
1691 | return 0; | |
1692 | if (params[0].return_size == not_set) | |
1693 | return 0; | |
1694 | if (out_sz != NULL) | |
1695 | *out_sz = params[0].return_size; | |
1696 | return 1; | |
1697 | } | |
1698 | ||
1699 | int EVP_PKEY_get_utf8_string_param(EVP_PKEY *pkey, const char *key_name, | |
1700 | char *str, size_t max_buf_sz, | |
1701 | size_t *out_sz) | |
1702 | { | |
1703 | OSSL_PARAM params[2]; | |
1704 | const size_t not_set = max_buf_sz + 1; | |
1705 | ||
1706 | if (pkey == NULL | |
1707 | || pkey->keymgmt == NULL | |
1708 | || pkey->keydata == NULL | |
1709 | || key_name == NULL) | |
1710 | return 0; | |
1711 | ||
1712 | params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz); | |
1713 | params[0].return_size = not_set; | |
1714 | params[1] = OSSL_PARAM_construct_end(); | |
1715 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) | |
1716 | return 0; | |
1717 | if (params[0].return_size == not_set) | |
1718 | return 0; | |
1719 | if (out_sz != NULL) | |
1720 | *out_sz = params[0].return_size; | |
1721 | return 1; | |
1722 | } | |
1723 | ||
1724 | int EVP_PKEY_get_int_param(EVP_PKEY *pkey, const char *key_name, int *out) | |
1725 | { | |
1726 | OSSL_PARAM params[2]; | |
1727 | const size_t not_set = sizeof(int) + 1; | |
1728 | ||
1729 | if (pkey == NULL | |
1730 | || pkey->keymgmt == NULL | |
1731 | || pkey->keydata == NULL | |
1732 | || key_name == NULL) | |
1733 | return 0; | |
1734 | ||
1735 | params[0] = OSSL_PARAM_construct_int(key_name, out); | |
1736 | params[0].return_size = not_set; | |
1737 | params[1] = OSSL_PARAM_construct_end(); | |
1738 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) | |
1739 | return 0; | |
1740 | if (params[0].return_size == not_set) | |
1741 | return 0; | |
1742 | return 1; | |
1743 | } | |
1744 | ||
1745 | int EVP_PKEY_get_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t *out) | |
1746 | { | |
1747 | OSSL_PARAM params[2]; | |
1748 | const size_t not_set = sizeof(size_t) + 1; | |
1749 | ||
1750 | if (pkey == NULL | |
1751 | || pkey->keymgmt == NULL | |
1752 | || pkey->keydata == NULL | |
1753 | || key_name == NULL) | |
1754 | return 0; | |
1755 | ||
1756 | params[0] = OSSL_PARAM_construct_size_t(key_name, out); | |
1757 | params[0].return_size = not_set; | |
1758 | params[1] = OSSL_PARAM_construct_end(); | |
1759 | if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) | |
1760 | return 0; | |
1761 | if (params[0].return_size == not_set) | |
1762 | return 0; | |
1763 | return 1; | |
1764 | } |