]>
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 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
cd420b0b | 12 | #include "internal/refcount.h" |
4d94ae00 BM |
13 | #include <openssl/bn.h> |
14 | #include <openssl/err.h> | |
ec577822 BM |
15 | #include <openssl/objects.h> |
16 | #include <openssl/evp.h> | |
ec577822 | 17 | #include <openssl/x509.h> |
3c27208f RS |
18 | #include <openssl/rsa.h> |
19 | #include <openssl/dsa.h> | |
20 | #include <openssl/dh.h> | |
b3831fbb | 21 | #include <openssl/cmac.h> |
3c27208f | 22 | #include <openssl/engine.h> |
e74bd290 | 23 | #include <openssl/params.h> |
54c1711f | 24 | #include <openssl/serializer.h> |
e74bd290 | 25 | #include <openssl/core_names.h> |
01b8b3c7 | 26 | |
25f2138b DMSP |
27 | #include "crypto/asn1.h" |
28 | #include "crypto/evp.h" | |
e74bd290 | 29 | #include "internal/provider.h" |
18e377b4 | 30 | |
d02b48c6 | 31 | static void EVP_PKEY_free_it(EVP_PKEY *x); |
bb2297a4 | 32 | |
8900f3e3 | 33 | int EVP_PKEY_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
34 | { |
35 | if (pkey && pkey->ameth && pkey->ameth->pkey_bits) | |
36 | return pkey->ameth->pkey_bits(pkey); | |
37 | return 0; | |
38 | } | |
58964a49 | 39 | |
2514fa79 | 40 | int EVP_PKEY_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
41 | { |
42 | if (pkey == NULL) | |
43 | return 0; | |
12a765a5 | 44 | if (pkey->ameth == NULL || pkey->ameth->pkey_security_bits == NULL) |
0f113f3e MC |
45 | return -2; |
46 | return pkey->ameth->pkey_security_bits(pkey); | |
47 | } | |
2514fa79 | 48 | |
47ec2367 | 49 | int EVP_PKEY_size(const EVP_PKEY *pkey) |
0f113f3e MC |
50 | { |
51 | if (pkey && pkey->ameth && pkey->ameth->pkey_size) | |
52 | return pkey->ameth->pkey_size(pkey); | |
53 | return 0; | |
54 | } | |
d02b48c6 | 55 | |
6b691a5c | 56 | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) |
0f113f3e | 57 | { |
cf1b7d96 | 58 | #ifndef OPENSSL_NO_DSA |
0f113f3e MC |
59 | if (pkey->type == EVP_PKEY_DSA) { |
60 | int ret = pkey->save_parameters; | |
61 | ||
62 | if (mode >= 0) | |
63 | pkey->save_parameters = mode; | |
26a7d938 | 64 | return ret; |
0f113f3e | 65 | } |
4d94ae00 | 66 | #endif |
5488bb61 | 67 | #ifndef OPENSSL_NO_EC |
0f113f3e MC |
68 | if (pkey->type == EVP_PKEY_EC) { |
69 | int ret = pkey->save_parameters; | |
70 | ||
71 | if (mode >= 0) | |
72 | pkey->save_parameters = mode; | |
26a7d938 | 73 | return ret; |
0f113f3e | 74 | } |
d02b48c6 | 75 | #endif |
26a7d938 | 76 | return 0; |
0f113f3e | 77 | } |
d02b48c6 | 78 | |
a8b72844 | 79 | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
0f113f3e | 80 | { |
2986ecdc DSH |
81 | if (to->type == EVP_PKEY_NONE) { |
82 | if (EVP_PKEY_set_type(to, from->type) == 0) | |
83 | return 0; | |
84 | } else if (to->type != from->type) { | |
0f113f3e MC |
85 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES); |
86 | goto err; | |
87 | } | |
88 | ||
89 | if (EVP_PKEY_missing_parameters(from)) { | |
90 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS); | |
91 | goto err; | |
92 | } | |
f72f00d4 DSH |
93 | |
94 | if (!EVP_PKEY_missing_parameters(to)) { | |
95 | if (EVP_PKEY_cmp_parameters(to, from) == 1) | |
96 | return 1; | |
97 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS); | |
98 | return 0; | |
99 | } | |
100 | ||
0f113f3e MC |
101 | if (from->ameth && from->ameth->param_copy) |
102 | return from->ameth->param_copy(to, from); | |
103 | err: | |
104 | return 0; | |
105 | } | |
d02b48c6 | 106 | |
af0f0f3e | 107 | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e | 108 | { |
ab5c77b4 | 109 | if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing) |
0f113f3e MC |
110 | return pkey->ameth->param_missing(pkey); |
111 | return 0; | |
112 | } | |
d02b48c6 | 113 | |
af0f0f3e | 114 | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
115 | { |
116 | if (a->type != b->type) | |
117 | return -1; | |
118 | if (a->ameth && a->ameth->param_cmp) | |
119 | return a->ameth->param_cmp(a, b); | |
120 | return -2; | |
121 | } | |
58964a49 | 122 | |
af0f0f3e | 123 | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e MC |
124 | { |
125 | if (a->type != b->type) | |
126 | return -1; | |
127 | ||
128 | if (a->ameth) { | |
129 | int ret; | |
130 | /* Compare parameters if the algorithm has them */ | |
131 | if (a->ameth->param_cmp) { | |
132 | ret = a->ameth->param_cmp(a, b); | |
133 | if (ret <= 0) | |
134 | return ret; | |
135 | } | |
136 | ||
137 | if (a->ameth->pub_cmp) | |
138 | return a->ameth->pub_cmp(a, b); | |
139 | } | |
140 | ||
141 | return -2; | |
142 | } | |
e6526fbf | 143 | |
6b691a5c | 144 | EVP_PKEY *EVP_PKEY_new(void) |
0f113f3e | 145 | { |
a2d0baa2 | 146 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); |
0f113f3e | 147 | |
0f113f3e MC |
148 | if (ret == NULL) { |
149 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
03273d61 | 150 | return NULL; |
0f113f3e MC |
151 | } |
152 | ret->type = EVP_PKEY_NONE; | |
153 | ret->save_type = EVP_PKEY_NONE; | |
154 | ret->references = 1; | |
0f113f3e | 155 | ret->save_parameters = 1; |
03273d61 AG |
156 | ret->lock = CRYPTO_THREAD_lock_new(); |
157 | if (ret->lock == NULL) { | |
158 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); | |
159 | OPENSSL_free(ret); | |
160 | return NULL; | |
161 | } | |
162 | return ret; | |
0f113f3e MC |
163 | } |
164 | ||
c5ebfcab | 165 | int EVP_PKEY_up_ref(EVP_PKEY *pkey) |
2872dbe1 | 166 | { |
03273d61 | 167 | int i; |
c5ebfcab | 168 | |
2f545ae4 | 169 | if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0) |
c5ebfcab F |
170 | return 0; |
171 | ||
172 | REF_PRINT_COUNT("EVP_PKEY", pkey); | |
173 | REF_ASSERT_ISNT(i < 2); | |
174 | return ((i > 1) ? 1 : 0); | |
2872dbe1 DSH |
175 | } |
176 | ||
0f113f3e MC |
177 | /* |
178 | * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey | |
179 | * is NULL just return 1 or 0 if the algorithm exists. | |
01b8b3c7 DSH |
180 | */ |
181 | ||
a08802ce MC |
182 | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
183 | int len) | |
0f113f3e MC |
184 | { |
185 | const EVP_PKEY_ASN1_METHOD *ameth; | |
a08802ce MC |
186 | ENGINE **eptr = (e == NULL) ? &e : NULL; |
187 | ||
0f113f3e MC |
188 | if (pkey) { |
189 | if (pkey->pkey.ptr) | |
190 | EVP_PKEY_free_it(pkey); | |
191 | /* | |
192 | * If key type matches and a method exists then this lookup has | |
193 | * succeeded once so just indicate success. | |
194 | */ | |
195 | if ((type == pkey->save_type) && pkey->ameth) | |
196 | return 1; | |
01b8b3c7 | 197 | #ifndef OPENSSL_NO_ENGINE |
d19b01ad | 198 | /* If we have ENGINEs release them */ |
7c96dbcd RS |
199 | ENGINE_finish(pkey->engine); |
200 | pkey->engine = NULL; | |
d19b01ad DSH |
201 | ENGINE_finish(pkey->pmeth_engine); |
202 | pkey->pmeth_engine = NULL; | |
01b8b3c7 | 203 | #endif |
0f113f3e MC |
204 | } |
205 | if (str) | |
a08802ce | 206 | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); |
0f113f3e | 207 | else |
a08802ce | 208 | ameth = EVP_PKEY_asn1_find(eptr, type); |
01b8b3c7 | 209 | #ifndef OPENSSL_NO_ENGINE |
a08802ce | 210 | if (pkey == NULL && eptr != NULL) |
0f113f3e | 211 | ENGINE_finish(e); |
01b8b3c7 | 212 | #endif |
7c96dbcd | 213 | if (ameth == NULL) { |
0f113f3e MC |
214 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); |
215 | return 0; | |
216 | } | |
217 | if (pkey) { | |
218 | pkey->ameth = ameth; | |
219 | pkey->engine = e; | |
220 | ||
221 | pkey->type = pkey->ameth->pkey_id; | |
222 | pkey->save_type = type; | |
223 | } | |
224 | return 1; | |
225 | } | |
01b8b3c7 | 226 | |
f929439f MC |
227 | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, |
228 | const unsigned char *priv, | |
229 | size_t len) | |
a08802ce MC |
230 | { |
231 | EVP_PKEY *ret = EVP_PKEY_new(); | |
232 | ||
233 | if (ret == NULL | |
234 | || !pkey_set_type(ret, e, type, NULL, -1)) { | |
235 | /* EVPerr already called */ | |
236 | goto err; | |
237 | } | |
238 | ||
239 | if (ret->ameth->set_priv_key == NULL) { | |
f929439f | 240 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, |
a08802ce MC |
241 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
242 | goto err; | |
243 | } | |
244 | ||
245 | if (!ret->ameth->set_priv_key(ret, priv, len)) { | |
f929439f | 246 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
247 | goto err; |
248 | } | |
249 | ||
250 | return ret; | |
251 | ||
252 | err: | |
253 | EVP_PKEY_free(ret); | |
254 | return NULL; | |
255 | } | |
256 | ||
f929439f MC |
257 | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, |
258 | const unsigned char *pub, | |
259 | size_t len) | |
a08802ce MC |
260 | { |
261 | EVP_PKEY *ret = EVP_PKEY_new(); | |
262 | ||
263 | if (ret == NULL | |
264 | || !pkey_set_type(ret, e, type, NULL, -1)) { | |
265 | /* EVPerr already called */ | |
266 | goto err; | |
267 | } | |
268 | ||
269 | if (ret->ameth->set_pub_key == NULL) { | |
f929439f | 270 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, |
a08802ce MC |
271 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
272 | goto err; | |
273 | } | |
274 | ||
275 | if (!ret->ameth->set_pub_key(ret, pub, len)) { | |
f929439f | 276 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED); |
a08802ce MC |
277 | goto err; |
278 | } | |
279 | ||
280 | return ret; | |
281 | ||
282 | err: | |
283 | EVP_PKEY_free(ret); | |
284 | return NULL; | |
285 | } | |
286 | ||
0d124b0a MC |
287 | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, |
288 | size_t *len) | |
289 | { | |
290 | if (pkey->ameth->get_priv_key == NULL) { | |
291 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, | |
292 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
293 | return 0; | |
294 | } | |
295 | ||
296 | if (!pkey->ameth->get_priv_key(pkey, priv, len)) { | |
297 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
298 | return 0; | |
299 | } | |
300 | ||
301 | return 1; | |
302 | } | |
303 | ||
304 | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, | |
305 | size_t *len) | |
306 | { | |
307 | if (pkey->ameth->get_pub_key == NULL) { | |
308 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, | |
309 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
310 | return 0; | |
311 | } | |
312 | ||
313 | if (!pkey->ameth->get_pub_key(pkey, pub, len)) { | |
314 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED); | |
315 | return 0; | |
316 | } | |
317 | ||
318 | return 1; | |
319 | } | |
320 | ||
b3831fbb MC |
321 | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, |
322 | size_t len, const EVP_CIPHER *cipher) | |
323 | { | |
df6d51e2 | 324 | #ifndef OPENSSL_NO_CMAC |
3be06e0d | 325 | # ifndef OPENSSL_NO_ENGINE |
9a7846df | 326 | const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL; |
3be06e0d | 327 | # endif |
e74bd290 RL |
328 | const char *cipher_name = EVP_CIPHER_name(cipher); |
329 | const OSSL_PROVIDER *prov = EVP_CIPHER_provider(cipher); | |
330 | OPENSSL_CTX *libctx = | |
331 | prov == NULL ? NULL : ossl_provider_library_context(prov); | |
b3831fbb | 332 | EVP_PKEY *ret = EVP_PKEY_new(); |
81ff9eeb | 333 | EVP_MAC *cmac = EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, NULL); |
e74bd290 RL |
334 | EVP_MAC_CTX *cmctx = cmac != NULL ? EVP_MAC_CTX_new(cmac) : NULL; |
335 | OSSL_PARAM params[4]; | |
336 | size_t paramsn = 0; | |
b3831fbb MC |
337 | |
338 | if (ret == NULL | |
339 | || cmctx == NULL | |
340 | || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) { | |
341 | /* EVPerr already called */ | |
342 | goto err; | |
343 | } | |
344 | ||
3be06e0d | 345 | # ifndef OPENSSL_NO_ENGINE |
9a7846df | 346 | if (engine_id != NULL) |
e74bd290 | 347 | params[paramsn++] = |
69db3044 | 348 | OSSL_PARAM_construct_utf8_string("engine", (char *)engine_id, 0); |
3be06e0d MC |
349 | # endif |
350 | ||
e74bd290 | 351 | params[paramsn++] = |
703170d4 | 352 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
7f588d20 | 353 | (char *)cipher_name, 0); |
e74bd290 RL |
354 | params[paramsn++] = |
355 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, | |
356 | (char *)priv, len); | |
357 | params[paramsn] = OSSL_PARAM_construct_end(); | |
358 | ||
359 | if (!EVP_MAC_CTX_set_params(cmctx, params)) { | |
b3831fbb MC |
360 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED); |
361 | goto err; | |
362 | } | |
363 | ||
364 | ret->pkey.ptr = cmctx; | |
365 | return ret; | |
366 | ||
367 | err: | |
368 | EVP_PKEY_free(ret); | |
b8d77c9b | 369 | EVP_MAC_CTX_free(cmctx); |
e74bd290 | 370 | EVP_MAC_free(cmac); |
b3831fbb | 371 | return NULL; |
df6d51e2 MC |
372 | #else |
373 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, | |
374 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
375 | return NULL; | |
376 | #endif | |
b3831fbb | 377 | } |
a08802ce | 378 | |
01b8b3c7 | 379 | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) |
0f113f3e | 380 | { |
a08802ce | 381 | return pkey_set_type(pkey, NULL, type, NULL, -1); |
0f113f3e | 382 | } |
01b8b3c7 DSH |
383 | |
384 | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) | |
0f113f3e | 385 | { |
a08802ce | 386 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len); |
0f113f3e | 387 | } |
2f2e6b62 JL |
388 | |
389 | int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type) | |
390 | { | |
391 | if (pkey->type == type) { | |
392 | return 1; /* it already is that type */ | |
393 | } | |
394 | ||
395 | /* | |
396 | * The application is requesting to alias this to a different pkey type, | |
397 | * but not one that resolves to the base type. | |
398 | */ | |
399 | if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) { | |
400 | EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); | |
401 | return 0; | |
402 | } | |
403 | ||
404 | pkey->type = type; | |
405 | return 1; | |
406 | } | |
407 | ||
d19b01ad DSH |
408 | #ifndef OPENSSL_NO_ENGINE |
409 | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e) | |
410 | { | |
411 | if (e != NULL) { | |
412 | if (!ENGINE_init(e)) { | |
413 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB); | |
414 | return 0; | |
415 | } | |
416 | if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { | |
417 | ENGINE_finish(e); | |
418 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM); | |
419 | return 0; | |
420 | } | |
421 | } | |
422 | ENGINE_finish(pkey->pmeth_engine); | |
423 | pkey->pmeth_engine = e; | |
424 | return 1; | |
425 | } | |
229f7b38 DB |
426 | |
427 | ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey) | |
428 | { | |
429 | return pkey->engine; | |
430 | } | |
d19b01ad | 431 | #endif |
01b8b3c7 | 432 | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
0f113f3e | 433 | { |
e34c66c6 | 434 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
0f113f3e MC |
435 | return 0; |
436 | pkey->pkey.ptr = key; | |
437 | return (key != NULL); | |
438 | } | |
d02b48c6 | 439 | |
3aeb9348 | 440 | void *EVP_PKEY_get0(const EVP_PKEY *pkey) |
0f113f3e MC |
441 | { |
442 | return pkey->pkey.ptr; | |
443 | } | |
db98bbc1 | 444 | |
ebad0b0b NM |
445 | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) |
446 | { | |
447 | ASN1_OCTET_STRING *os = NULL; | |
448 | if (pkey->type != EVP_PKEY_HMAC) { | |
449 | EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY); | |
450 | return NULL; | |
451 | } | |
452 | os = EVP_PKEY_get0(pkey); | |
453 | *len = os->length; | |
454 | return os->data; | |
455 | } | |
456 | ||
52ad5b60 TS |
457 | #ifndef OPENSSL_NO_POLY1305 |
458 | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) | |
459 | { | |
460 | ASN1_OCTET_STRING *os = NULL; | |
461 | if (pkey->type != EVP_PKEY_POLY1305) { | |
462 | EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY); | |
463 | return NULL; | |
464 | } | |
465 | os = EVP_PKEY_get0(pkey); | |
466 | *len = os->length; | |
467 | return os->data; | |
468 | } | |
469 | #endif | |
470 | ||
3f5616d7 TS |
471 | #ifndef OPENSSL_NO_SIPHASH |
472 | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) | |
473 | { | |
474 | ASN1_OCTET_STRING *os = NULL; | |
475 | ||
476 | if (pkey->type != EVP_PKEY_SIPHASH) { | |
477 | EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY); | |
478 | return NULL; | |
479 | } | |
480 | os = EVP_PKEY_get0(pkey); | |
481 | *len = os->length; | |
482 | return os->data; | |
483 | } | |
484 | #endif | |
485 | ||
cf1b7d96 | 486 | #ifndef OPENSSL_NO_RSA |
c7cb16a8 | 487 | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) |
52664f50 | 488 | { |
0f113f3e MC |
489 | int ret = EVP_PKEY_assign_RSA(pkey, key); |
490 | if (ret) | |
491 | RSA_up_ref(key); | |
492 | return ret; | |
52664f50 DSH |
493 | } |
494 | ||
9fdcc21f | 495 | RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) |
0f113f3e | 496 | { |
465a58b1 | 497 | if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) { |
2872dbe1 | 498 | EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY); |
0f113f3e MC |
499 | return NULL; |
500 | } | |
0f113f3e | 501 | return pkey->pkey.rsa; |
f769ce3e | 502 | } |
2872dbe1 DSH |
503 | |
504 | RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) | |
505 | { | |
506 | RSA *ret = EVP_PKEY_get0_RSA(pkey); | |
507 | if (ret != NULL) | |
508 | RSA_up_ref(ret); | |
509 | return ret; | |
510 | } | |
f769ce3e DSH |
511 | #endif |
512 | ||
cf1b7d96 | 513 | #ifndef OPENSSL_NO_DSA |
c7cb16a8 | 514 | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) |
52664f50 | 515 | { |
0f113f3e MC |
516 | int ret = EVP_PKEY_assign_DSA(pkey, key); |
517 | if (ret) | |
518 | DSA_up_ref(key); | |
519 | return ret; | |
52664f50 DSH |
520 | } |
521 | ||
9fdcc21f | 522 | DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey) |
0f113f3e MC |
523 | { |
524 | if (pkey->type != EVP_PKEY_DSA) { | |
2872dbe1 | 525 | EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY); |
0f113f3e MC |
526 | return NULL; |
527 | } | |
0f113f3e | 528 | return pkey->pkey.dsa; |
f769ce3e | 529 | } |
2872dbe1 DSH |
530 | |
531 | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) | |
532 | { | |
533 | DSA *ret = EVP_PKEY_get0_DSA(pkey); | |
534 | if (ret != NULL) | |
535 | DSA_up_ref(ret); | |
536 | return ret; | |
537 | } | |
f769ce3e DSH |
538 | #endif |
539 | ||
14a7cfb3 | 540 | #ifndef OPENSSL_NO_EC |
4d94ae00 | 541 | |
14a7cfb3 | 542 | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) |
4d94ae00 | 543 | { |
0f113f3e MC |
544 | int ret = EVP_PKEY_assign_EC_KEY(pkey, key); |
545 | if (ret) | |
546 | EC_KEY_up_ref(key); | |
547 | return ret; | |
4d94ae00 BM |
548 | } |
549 | ||
9fdcc21f | 550 | EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey) |
4d94ae00 | 551 | { |
0f113f3e | 552 | if (pkey->type != EVP_PKEY_EC) { |
2872dbe1 | 553 | EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); |
0f113f3e MC |
554 | return NULL; |
555 | } | |
0f113f3e | 556 | return pkey->pkey.ec; |
4d94ae00 | 557 | } |
2872dbe1 DSH |
558 | |
559 | EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) | |
560 | { | |
561 | EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey); | |
562 | if (ret != NULL) | |
563 | EC_KEY_up_ref(ret); | |
564 | return ret; | |
565 | } | |
4d94ae00 BM |
566 | #endif |
567 | ||
cf1b7d96 | 568 | #ifndef OPENSSL_NO_DH |
52664f50 | 569 | |
c7cb16a8 | 570 | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) |
52664f50 | 571 | { |
32c869ff MC |
572 | int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX; |
573 | int ret = EVP_PKEY_assign(pkey, type, key); | |
574 | ||
0f113f3e MC |
575 | if (ret) |
576 | DH_up_ref(key); | |
577 | return ret; | |
52664f50 DSH |
578 | } |
579 | ||
9fdcc21f | 580 | DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey) |
0f113f3e MC |
581 | { |
582 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { | |
2872dbe1 | 583 | EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY); |
0f113f3e MC |
584 | return NULL; |
585 | } | |
0f113f3e | 586 | return pkey->pkey.dh; |
f769ce3e | 587 | } |
2872dbe1 DSH |
588 | |
589 | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) | |
590 | { | |
591 | DH *ret = EVP_PKEY_get0_DH(pkey); | |
592 | if (ret != NULL) | |
593 | DH_up_ref(ret); | |
594 | return ret; | |
595 | } | |
f769ce3e DSH |
596 | #endif |
597 | ||
6b691a5c | 598 | int EVP_PKEY_type(int type) |
0f113f3e MC |
599 | { |
600 | int ret; | |
601 | const EVP_PKEY_ASN1_METHOD *ameth; | |
602 | ENGINE *e; | |
603 | ameth = EVP_PKEY_asn1_find(&e, type); | |
604 | if (ameth) | |
605 | ret = ameth->pkey_id; | |
606 | else | |
607 | ret = NID_undef; | |
01b8b3c7 | 608 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 609 | ENGINE_finish(e); |
01b8b3c7 | 610 | #endif |
0f113f3e MC |
611 | return ret; |
612 | } | |
d02b48c6 | 613 | |
7f57b076 | 614 | int EVP_PKEY_id(const EVP_PKEY *pkey) |
0f113f3e MC |
615 | { |
616 | return pkey->type; | |
617 | } | |
7f57b076 DSH |
618 | |
619 | int EVP_PKEY_base_id(const EVP_PKEY *pkey) | |
0f113f3e MC |
620 | { |
621 | return EVP_PKEY_type(pkey->type); | |
622 | } | |
7f57b076 | 623 | |
6b691a5c | 624 | void EVP_PKEY_free(EVP_PKEY *x) |
0f113f3e MC |
625 | { |
626 | int i; | |
d02b48c6 | 627 | |
0f113f3e MC |
628 | if (x == NULL) |
629 | return; | |
d02b48c6 | 630 | |
2f545ae4 | 631 | CRYPTO_DOWN_REF(&x->references, &i, x->lock); |
f3f1cf84 | 632 | REF_PRINT_COUNT("EVP_PKEY", x); |
0f113f3e MC |
633 | if (i > 0) |
634 | return; | |
f3f1cf84 | 635 | REF_ASSERT_ISNT(i < 0); |
0f113f3e | 636 | EVP_PKEY_free_it(x); |
d65c3615 | 637 | CRYPTO_THREAD_lock_free(x->lock); |
222561fe | 638 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); |
0f113f3e MC |
639 | OPENSSL_free(x); |
640 | } | |
d02b48c6 | 641 | |
6b691a5c | 642 | static void EVP_PKEY_free_it(EVP_PKEY *x) |
0f113f3e | 643 | { |
c5ba2d99 | 644 | /* internal function; x is never NULL */ |
4cae07fe RL |
645 | |
646 | evp_keymgmt_clear_pkey_cache(x); | |
647 | ||
0f113f3e MC |
648 | if (x->ameth && x->ameth->pkey_free) { |
649 | x->ameth->pkey_free(x); | |
650 | x->pkey.ptr = NULL; | |
651 | } | |
01b8b3c7 | 652 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd RS |
653 | ENGINE_finish(x->engine); |
654 | x->engine = NULL; | |
d19b01ad DSH |
655 | ENGINE_finish(x->pmeth_engine); |
656 | x->pmeth_engine = NULL; | |
01b8b3c7 | 657 | #endif |
0f113f3e | 658 | } |
d02b48c6 | 659 | |
35208f36 | 660 | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
0f113f3e MC |
661 | const char *kstr) |
662 | { | |
663 | BIO_indent(out, indent, 128); | |
664 | BIO_printf(out, "%s algorithm \"%s\" unsupported\n", | |
665 | kstr, OBJ_nid2ln(pkey->type)); | |
666 | return 1; | |
667 | } | |
35208f36 DSH |
668 | |
669 | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
670 | int indent, ASN1_PCTX *pctx) |
671 | { | |
54c1711f RL |
672 | const char *pq = OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ; |
673 | OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq); | |
674 | int ret = -2; /* mark as unsupported */ | |
675 | ||
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) | |
681 | return ret; | |
682 | ||
683 | /* legacy fallback */ | |
0f113f3e MC |
684 | if (pkey->ameth && pkey->ameth->pub_print) |
685 | return pkey->ameth->pub_print(out, pkey, indent, pctx); | |
686 | ||
687 | return unsup_alg(out, pkey, indent, "Public Key"); | |
688 | } | |
35208f36 DSH |
689 | |
690 | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
691 | int indent, ASN1_PCTX *pctx) |
692 | { | |
54c1711f RL |
693 | const char *pq = OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ; |
694 | OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq); | |
695 | int ret = -2; /* mark as unsupported */ | |
696 | ||
697 | if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) | |
698 | ret = OSSL_SERIALIZER_to_bio(ctx, out); | |
699 | OSSL_SERIALIZER_CTX_free(ctx); | |
700 | ||
701 | if (ret != -2) | |
702 | return ret; | |
703 | ||
704 | /* legacy fallback */ | |
0f113f3e MC |
705 | if (pkey->ameth && pkey->ameth->priv_print) |
706 | return pkey->ameth->priv_print(out, pkey, indent, pctx); | |
707 | ||
708 | return unsup_alg(out, pkey, indent, "Private Key"); | |
709 | } | |
35208f36 DSH |
710 | |
711 | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, | |
0f113f3e MC |
712 | int indent, ASN1_PCTX *pctx) |
713 | { | |
54c1711f RL |
714 | const char *pq = OSSL_SERIALIZER_Parameters_TO_TEXT_PQ; |
715 | OSSL_SERIALIZER_CTX *ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, pq); | |
716 | int ret = -2; /* mark as unsupported */ | |
717 | ||
718 | if (OSSL_SERIALIZER_CTX_get_serializer(ctx) != NULL) | |
719 | ret = OSSL_SERIALIZER_to_bio(ctx, out); | |
720 | OSSL_SERIALIZER_CTX_free(ctx); | |
721 | ||
722 | if (ret != -2) | |
723 | return ret; | |
724 | ||
725 | /* legacy fallback */ | |
0f113f3e MC |
726 | if (pkey->ameth && pkey->ameth->param_print) |
727 | return pkey->ameth->param_print(out, pkey, indent, pctx); | |
54c1711f | 728 | |
0f113f3e MC |
729 | return unsup_alg(out, pkey, indent, "Parameters"); |
730 | } | |
03919683 | 731 | |
5d6aaf8a | 732 | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
0f113f3e | 733 | { |
5d6aaf8a | 734 | if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
0f113f3e | 735 | return -2; |
5d6aaf8a DSH |
736 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
737 | } | |
738 | ||
739 | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) | |
740 | { | |
741 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); | |
742 | } | |
743 | ||
ecbb2fca DW |
744 | int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid) |
745 | { | |
746 | int rv, default_nid; | |
747 | ||
748 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL); | |
749 | if (rv == -2) { | |
750 | /* | |
751 | * If there is a mandatory default digest and this isn't it, then | |
752 | * the answer is 'no'. | |
753 | */ | |
754 | rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid); | |
755 | if (rv == 2) | |
756 | return (nid == default_nid); | |
757 | /* zero is an error from EVP_PKEY_get_default_digest_nid() */ | |
758 | if (rv == 0) | |
759 | return -1; | |
760 | } | |
761 | return rv; | |
762 | } | |
763 | ||
5d6aaf8a DSH |
764 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, |
765 | const unsigned char *pt, size_t ptlen) | |
766 | { | |
767 | if (ptlen > INT_MAX) | |
768 | return 0; | |
769 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen, | |
770 | (void *)pt) <= 0) | |
771 | return 0; | |
772 | return 1; | |
773 | } | |
774 | ||
775 | size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt) | |
776 | { | |
777 | int rv; | |
778 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt); | |
779 | if (rv <= 0) | |
780 | return 0; | |
781 | return rv; | |
0f113f3e | 782 | } |