/src/openssl111/crypto/evp/p_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include "internal/refcount.h" |
13 | | #include <openssl/bn.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/objects.h> |
16 | | #include <openssl/evp.h> |
17 | | #include <openssl/x509.h> |
18 | | #include <openssl/rsa.h> |
19 | | #include <openssl/dsa.h> |
20 | | #include <openssl/dh.h> |
21 | | #include <openssl/cmac.h> |
22 | | #include <openssl/engine.h> |
23 | | |
24 | | #include "crypto/asn1.h" |
25 | | #include "crypto/evp.h" |
26 | | |
27 | | static void EVP_PKEY_free_it(EVP_PKEY *x); |
28 | | |
29 | | int EVP_PKEY_bits(const EVP_PKEY *pkey) |
30 | 0 | { |
31 | 0 | if (pkey && pkey->ameth && pkey->ameth->pkey_bits) |
32 | 0 | return pkey->ameth->pkey_bits(pkey); |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | | int EVP_PKEY_security_bits(const EVP_PKEY *pkey) |
37 | 0 | { |
38 | 0 | if (pkey == NULL) |
39 | 0 | return 0; |
40 | 0 | if (!pkey->ameth || !pkey->ameth->pkey_security_bits) |
41 | 0 | return -2; |
42 | 0 | return pkey->ameth->pkey_security_bits(pkey); |
43 | 0 | } |
44 | | |
45 | | int EVP_PKEY_size(const EVP_PKEY *pkey) |
46 | 0 | { |
47 | 0 | if (pkey && pkey->ameth && pkey->ameth->pkey_size) |
48 | 0 | return pkey->ameth->pkey_size(pkey); |
49 | 0 | return 0; |
50 | 0 | } |
51 | | |
52 | | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) |
53 | 0 | { |
54 | 0 | #ifndef OPENSSL_NO_DSA |
55 | 0 | if (pkey->type == EVP_PKEY_DSA) { |
56 | 0 | int ret = pkey->save_parameters; |
57 | |
|
58 | 0 | if (mode >= 0) |
59 | 0 | pkey->save_parameters = mode; |
60 | 0 | return ret; |
61 | 0 | } |
62 | 0 | #endif |
63 | 0 | #ifndef OPENSSL_NO_EC |
64 | 0 | if (pkey->type == EVP_PKEY_EC) { |
65 | 0 | int ret = pkey->save_parameters; |
66 | |
|
67 | 0 | if (mode >= 0) |
68 | 0 | pkey->save_parameters = mode; |
69 | 0 | return ret; |
70 | 0 | } |
71 | 0 | #endif |
72 | 0 | return 0; |
73 | 0 | } |
74 | | |
75 | | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
76 | 0 | { |
77 | 0 | if (to->type == EVP_PKEY_NONE) { |
78 | 0 | if (EVP_PKEY_set_type(to, from->type) == 0) |
79 | 0 | return 0; |
80 | 0 | } else if (to->type != from->type) { |
81 | 0 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES); |
82 | 0 | goto err; |
83 | 0 | } |
84 | | |
85 | 0 | if (EVP_PKEY_missing_parameters(from)) { |
86 | 0 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS); |
87 | 0 | goto err; |
88 | 0 | } |
89 | | |
90 | 0 | if (!EVP_PKEY_missing_parameters(to)) { |
91 | 0 | if (EVP_PKEY_cmp_parameters(to, from) == 1) |
92 | 0 | return 1; |
93 | 0 | EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS); |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | 0 | if (from->ameth && from->ameth->param_copy) |
98 | 0 | return from->ameth->param_copy(to, from); |
99 | 0 | err: |
100 | 0 | return 0; |
101 | 0 | } |
102 | | |
103 | | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) |
104 | 0 | { |
105 | 0 | if (pkey != NULL && pkey->ameth && pkey->ameth->param_missing) |
106 | 0 | return pkey->ameth->param_missing(pkey); |
107 | 0 | return 0; |
108 | 0 | } |
109 | | |
110 | | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
111 | 0 | { |
112 | 0 | if (a->type != b->type) |
113 | 0 | return -1; |
114 | 0 | if (a->ameth && a->ameth->param_cmp) |
115 | 0 | return a->ameth->param_cmp(a, b); |
116 | 0 | return -2; |
117 | 0 | } |
118 | | |
119 | | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
120 | 0 | { |
121 | 0 | if (a->type != b->type) |
122 | 0 | return -1; |
123 | | |
124 | 0 | if (a->ameth) { |
125 | 0 | int ret; |
126 | | /* Compare parameters if the algorithm has them */ |
127 | 0 | if (a->ameth->param_cmp) { |
128 | 0 | ret = a->ameth->param_cmp(a, b); |
129 | 0 | if (ret <= 0) |
130 | 0 | return ret; |
131 | 0 | } |
132 | | |
133 | 0 | if (a->ameth->pub_cmp) |
134 | 0 | return a->ameth->pub_cmp(a, b); |
135 | 0 | } |
136 | | |
137 | 0 | return -2; |
138 | 0 | } |
139 | | |
140 | | EVP_PKEY *EVP_PKEY_new(void) |
141 | 0 | { |
142 | 0 | EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret)); |
143 | |
|
144 | 0 | if (ret == NULL) { |
145 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); |
146 | 0 | return NULL; |
147 | 0 | } |
148 | 0 | ret->type = EVP_PKEY_NONE; |
149 | 0 | ret->save_type = EVP_PKEY_NONE; |
150 | 0 | ret->references = 1; |
151 | 0 | ret->save_parameters = 1; |
152 | 0 | ret->lock = CRYPTO_THREAD_lock_new(); |
153 | 0 | if (ret->lock == NULL) { |
154 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE); |
155 | 0 | OPENSSL_free(ret); |
156 | 0 | return NULL; |
157 | 0 | } |
158 | 0 | return ret; |
159 | 0 | } |
160 | | |
161 | | int EVP_PKEY_up_ref(EVP_PKEY *pkey) |
162 | 0 | { |
163 | 0 | int i; |
164 | |
|
165 | 0 | if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0) |
166 | 0 | return 0; |
167 | | |
168 | 0 | REF_PRINT_COUNT("EVP_PKEY", pkey); |
169 | 0 | REF_ASSERT_ISNT(i < 2); |
170 | 0 | return ((i > 1) ? 1 : 0); |
171 | 0 | } |
172 | | |
173 | | /* |
174 | | * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey |
175 | | * is NULL just return 1 or 0 if the algorithm exists. |
176 | | */ |
177 | | |
178 | | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str, |
179 | | int len) |
180 | 0 | { |
181 | 0 | const EVP_PKEY_ASN1_METHOD *ameth; |
182 | 0 | ENGINE **eptr = (e == NULL) ? &e : NULL; |
183 | |
|
184 | 0 | if (pkey) { |
185 | 0 | if (pkey->pkey.ptr) |
186 | 0 | EVP_PKEY_free_it(pkey); |
187 | | /* |
188 | | * If key type matches and a method exists then this lookup has |
189 | | * succeeded once so just indicate success. |
190 | | */ |
191 | 0 | if ((type == pkey->save_type) && pkey->ameth) |
192 | 0 | return 1; |
193 | 0 | #ifndef OPENSSL_NO_ENGINE |
194 | | /* If we have ENGINEs release them */ |
195 | 0 | ENGINE_finish(pkey->engine); |
196 | 0 | pkey->engine = NULL; |
197 | 0 | ENGINE_finish(pkey->pmeth_engine); |
198 | 0 | pkey->pmeth_engine = NULL; |
199 | 0 | #endif |
200 | 0 | } |
201 | 0 | if (str) |
202 | 0 | ameth = EVP_PKEY_asn1_find_str(eptr, str, len); |
203 | 0 | else |
204 | 0 | ameth = EVP_PKEY_asn1_find(eptr, type); |
205 | 0 | #ifndef OPENSSL_NO_ENGINE |
206 | 0 | if (pkey == NULL && eptr != NULL) |
207 | 0 | ENGINE_finish(e); |
208 | 0 | #endif |
209 | 0 | if (ameth == NULL) { |
210 | 0 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | if (pkey) { |
214 | 0 | pkey->ameth = ameth; |
215 | 0 | pkey->type = pkey->ameth->pkey_id; |
216 | 0 | pkey->save_type = type; |
217 | 0 | # ifndef OPENSSL_NO_ENGINE |
218 | 0 | if (eptr == NULL && e != NULL && !ENGINE_init(e)) { |
219 | 0 | EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_INITIALIZATION_ERROR); |
220 | 0 | return 0; |
221 | 0 | } |
222 | 0 | # endif |
223 | 0 | pkey->engine = e; |
224 | 0 | } |
225 | 0 | return 1; |
226 | 0 | } |
227 | | |
228 | | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e, |
229 | | const unsigned char *priv, |
230 | | size_t len) |
231 | 0 | { |
232 | 0 | EVP_PKEY *ret = EVP_PKEY_new(); |
233 | |
|
234 | 0 | if (ret == NULL |
235 | 0 | || !pkey_set_type(ret, e, type, NULL, -1)) { |
236 | | /* EVPerr already called */ |
237 | 0 | goto err; |
238 | 0 | } |
239 | | |
240 | 0 | if (ret->ameth->set_priv_key == NULL) { |
241 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, |
242 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
243 | 0 | goto err; |
244 | 0 | } |
245 | | |
246 | 0 | if (!ret->ameth->set_priv_key(ret, priv, len)) { |
247 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED); |
248 | 0 | goto err; |
249 | 0 | } |
250 | | |
251 | 0 | return ret; |
252 | | |
253 | 0 | err: |
254 | 0 | EVP_PKEY_free(ret); |
255 | 0 | return NULL; |
256 | 0 | } |
257 | | |
258 | | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e, |
259 | | const unsigned char *pub, |
260 | | size_t len) |
261 | 0 | { |
262 | 0 | EVP_PKEY *ret = EVP_PKEY_new(); |
263 | |
|
264 | 0 | if (ret == NULL |
265 | 0 | || !pkey_set_type(ret, e, type, NULL, -1)) { |
266 | | /* EVPerr already called */ |
267 | 0 | goto err; |
268 | 0 | } |
269 | | |
270 | 0 | if (ret->ameth->set_pub_key == NULL) { |
271 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, |
272 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
273 | 0 | goto err; |
274 | 0 | } |
275 | | |
276 | 0 | if (!ret->ameth->set_pub_key(ret, pub, len)) { |
277 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED); |
278 | 0 | goto err; |
279 | 0 | } |
280 | | |
281 | 0 | return ret; |
282 | | |
283 | 0 | err: |
284 | 0 | EVP_PKEY_free(ret); |
285 | 0 | return NULL; |
286 | 0 | } |
287 | | |
288 | | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv, |
289 | | size_t *len) |
290 | 0 | { |
291 | 0 | if (pkey->ameth->get_priv_key == NULL) { |
292 | 0 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, |
293 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
294 | 0 | return 0; |
295 | 0 | } |
296 | | |
297 | 0 | if (!pkey->ameth->get_priv_key(pkey, priv, len)) { |
298 | 0 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED); |
299 | 0 | return 0; |
300 | 0 | } |
301 | | |
302 | 0 | return 1; |
303 | 0 | } |
304 | | |
305 | | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub, |
306 | | size_t *len) |
307 | 0 | { |
308 | 0 | if (pkey->ameth->get_pub_key == NULL) { |
309 | 0 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, |
310 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
311 | 0 | return 0; |
312 | 0 | } |
313 | | |
314 | 0 | if (!pkey->ameth->get_pub_key(pkey, pub, len)) { |
315 | 0 | EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED); |
316 | 0 | return 0; |
317 | 0 | } |
318 | | |
319 | 0 | return 1; |
320 | 0 | } |
321 | | |
322 | | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv, |
323 | | size_t len, const EVP_CIPHER *cipher) |
324 | 0 | { |
325 | 0 | #ifndef OPENSSL_NO_CMAC |
326 | 0 | EVP_PKEY *ret = EVP_PKEY_new(); |
327 | 0 | CMAC_CTX *cmctx = CMAC_CTX_new(); |
328 | |
|
329 | 0 | if (ret == NULL |
330 | 0 | || cmctx == NULL |
331 | 0 | || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) { |
332 | | /* EVPerr already called */ |
333 | 0 | goto err; |
334 | 0 | } |
335 | | |
336 | 0 | if (!CMAC_Init(cmctx, priv, len, cipher, e)) { |
337 | 0 | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED); |
338 | 0 | goto err; |
339 | 0 | } |
340 | | |
341 | 0 | ret->pkey.ptr = cmctx; |
342 | 0 | return ret; |
343 | | |
344 | 0 | err: |
345 | 0 | EVP_PKEY_free(ret); |
346 | 0 | CMAC_CTX_free(cmctx); |
347 | 0 | return NULL; |
348 | | #else |
349 | | EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, |
350 | | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
351 | | return NULL; |
352 | | #endif |
353 | 0 | } |
354 | | |
355 | | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) |
356 | 0 | { |
357 | 0 | return pkey_set_type(pkey, NULL, type, NULL, -1); |
358 | 0 | } |
359 | | |
360 | | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len) |
361 | 0 | { |
362 | 0 | return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len); |
363 | 0 | } |
364 | | |
365 | | int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type) |
366 | 0 | { |
367 | 0 | if (pkey->type == type) { |
368 | 0 | return 1; /* it already is that type */ |
369 | 0 | } |
370 | | |
371 | | /* |
372 | | * The application is requesting to alias this to a different pkey type, |
373 | | * but not one that resolves to the base type. |
374 | | */ |
375 | 0 | if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) { |
376 | 0 | EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM); |
377 | 0 | return 0; |
378 | 0 | } |
379 | | |
380 | 0 | pkey->type = type; |
381 | 0 | return 1; |
382 | 0 | } |
383 | | |
384 | | #ifndef OPENSSL_NO_ENGINE |
385 | | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e) |
386 | 0 | { |
387 | 0 | if (e != NULL) { |
388 | 0 | if (!ENGINE_init(e)) { |
389 | 0 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB); |
390 | 0 | return 0; |
391 | 0 | } |
392 | 0 | if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { |
393 | 0 | ENGINE_finish(e); |
394 | 0 | EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM); |
395 | 0 | return 0; |
396 | 0 | } |
397 | 0 | } |
398 | 0 | ENGINE_finish(pkey->pmeth_engine); |
399 | 0 | pkey->pmeth_engine = e; |
400 | 0 | return 1; |
401 | 0 | } |
402 | | |
403 | | ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey) |
404 | 0 | { |
405 | 0 | return pkey->engine; |
406 | 0 | } |
407 | | #endif |
408 | | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) |
409 | 0 | { |
410 | 0 | if (pkey == NULL || !EVP_PKEY_set_type(pkey, type)) |
411 | 0 | return 0; |
412 | 0 | pkey->pkey.ptr = key; |
413 | 0 | return (key != NULL); |
414 | 0 | } |
415 | | |
416 | | void *EVP_PKEY_get0(const EVP_PKEY *pkey) |
417 | 0 | { |
418 | 0 | return pkey->pkey.ptr; |
419 | 0 | } |
420 | | |
421 | | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len) |
422 | 0 | { |
423 | 0 | ASN1_OCTET_STRING *os = NULL; |
424 | 0 | if (pkey->type != EVP_PKEY_HMAC) { |
425 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY); |
426 | 0 | return NULL; |
427 | 0 | } |
428 | 0 | os = EVP_PKEY_get0(pkey); |
429 | 0 | *len = os->length; |
430 | 0 | return os->data; |
431 | 0 | } |
432 | | |
433 | | #ifndef OPENSSL_NO_POLY1305 |
434 | | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len) |
435 | 0 | { |
436 | 0 | ASN1_OCTET_STRING *os = NULL; |
437 | 0 | if (pkey->type != EVP_PKEY_POLY1305) { |
438 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY); |
439 | 0 | return NULL; |
440 | 0 | } |
441 | 0 | os = EVP_PKEY_get0(pkey); |
442 | 0 | *len = os->length; |
443 | 0 | return os->data; |
444 | 0 | } |
445 | | #endif |
446 | | |
447 | | #ifndef OPENSSL_NO_SIPHASH |
448 | | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len) |
449 | 0 | { |
450 | 0 | ASN1_OCTET_STRING *os = NULL; |
451 | |
|
452 | 0 | if (pkey->type != EVP_PKEY_SIPHASH) { |
453 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY); |
454 | 0 | return NULL; |
455 | 0 | } |
456 | 0 | os = EVP_PKEY_get0(pkey); |
457 | 0 | *len = os->length; |
458 | 0 | return os->data; |
459 | 0 | } |
460 | | #endif |
461 | | |
462 | | #ifndef OPENSSL_NO_RSA |
463 | | int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) |
464 | 0 | { |
465 | 0 | int ret = EVP_PKEY_assign_RSA(pkey, key); |
466 | 0 | if (ret) |
467 | 0 | RSA_up_ref(key); |
468 | 0 | return ret; |
469 | 0 | } |
470 | | |
471 | | RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) |
472 | 0 | { |
473 | 0 | if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) { |
474 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY); |
475 | 0 | return NULL; |
476 | 0 | } |
477 | 0 | return pkey->pkey.rsa; |
478 | 0 | } |
479 | | |
480 | | RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) |
481 | 0 | { |
482 | 0 | RSA *ret = EVP_PKEY_get0_RSA(pkey); |
483 | 0 | if (ret != NULL) |
484 | 0 | RSA_up_ref(ret); |
485 | 0 | return ret; |
486 | 0 | } |
487 | | #endif |
488 | | |
489 | | #ifndef OPENSSL_NO_DSA |
490 | | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) |
491 | 0 | { |
492 | 0 | int ret = EVP_PKEY_assign_DSA(pkey, key); |
493 | 0 | if (ret) |
494 | 0 | DSA_up_ref(key); |
495 | 0 | return ret; |
496 | 0 | } |
497 | | |
498 | | DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) |
499 | 0 | { |
500 | 0 | if (pkey->type != EVP_PKEY_DSA) { |
501 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY); |
502 | 0 | return NULL; |
503 | 0 | } |
504 | 0 | return pkey->pkey.dsa; |
505 | 0 | } |
506 | | |
507 | | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) |
508 | 0 | { |
509 | 0 | DSA *ret = EVP_PKEY_get0_DSA(pkey); |
510 | 0 | if (ret != NULL) |
511 | 0 | DSA_up_ref(ret); |
512 | 0 | return ret; |
513 | 0 | } |
514 | | #endif |
515 | | |
516 | | #ifndef OPENSSL_NO_EC |
517 | | |
518 | | int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) |
519 | 0 | { |
520 | 0 | int ret = EVP_PKEY_assign_EC_KEY(pkey, key); |
521 | 0 | if (ret) |
522 | 0 | EC_KEY_up_ref(key); |
523 | 0 | return ret; |
524 | 0 | } |
525 | | |
526 | | EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) |
527 | 0 | { |
528 | 0 | if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) { |
529 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); |
530 | 0 | return NULL; |
531 | 0 | } |
532 | 0 | return pkey->pkey.ec; |
533 | 0 | } |
534 | | |
535 | | EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) |
536 | 0 | { |
537 | 0 | EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey); |
538 | 0 | if (ret != NULL) |
539 | 0 | EC_KEY_up_ref(ret); |
540 | 0 | return ret; |
541 | 0 | } |
542 | | #endif |
543 | | |
544 | | #ifndef OPENSSL_NO_DH |
545 | | |
546 | | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) |
547 | 0 | { |
548 | 0 | int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX; |
549 | 0 | int ret = EVP_PKEY_assign(pkey, type, key); |
550 | |
|
551 | 0 | if (ret) |
552 | 0 | DH_up_ref(key); |
553 | 0 | return ret; |
554 | 0 | } |
555 | | |
556 | | DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey) |
557 | 0 | { |
558 | 0 | if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { |
559 | 0 | EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY); |
560 | 0 | return NULL; |
561 | 0 | } |
562 | 0 | return pkey->pkey.dh; |
563 | 0 | } |
564 | | |
565 | | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) |
566 | 0 | { |
567 | 0 | DH *ret = EVP_PKEY_get0_DH(pkey); |
568 | 0 | if (ret != NULL) |
569 | 0 | DH_up_ref(ret); |
570 | 0 | return ret; |
571 | 0 | } |
572 | | #endif |
573 | | |
574 | | int EVP_PKEY_type(int type) |
575 | 0 | { |
576 | 0 | int ret; |
577 | 0 | const EVP_PKEY_ASN1_METHOD *ameth; |
578 | 0 | ENGINE *e; |
579 | 0 | ameth = EVP_PKEY_asn1_find(&e, type); |
580 | 0 | if (ameth) |
581 | 0 | ret = ameth->pkey_id; |
582 | 0 | else |
583 | 0 | ret = NID_undef; |
584 | 0 | #ifndef OPENSSL_NO_ENGINE |
585 | 0 | ENGINE_finish(e); |
586 | 0 | #endif |
587 | 0 | return ret; |
588 | 0 | } |
589 | | |
590 | | int EVP_PKEY_id(const EVP_PKEY *pkey) |
591 | 0 | { |
592 | 0 | return pkey->type; |
593 | 0 | } |
594 | | |
595 | | int EVP_PKEY_base_id(const EVP_PKEY *pkey) |
596 | 0 | { |
597 | 0 | return EVP_PKEY_type(pkey->type); |
598 | 0 | } |
599 | | |
600 | | void EVP_PKEY_free(EVP_PKEY *x) |
601 | 0 | { |
602 | 0 | int i; |
603 | |
|
604 | 0 | if (x == NULL) |
605 | 0 | return; |
606 | | |
607 | 0 | CRYPTO_DOWN_REF(&x->references, &i, x->lock); |
608 | 0 | REF_PRINT_COUNT("EVP_PKEY", x); |
609 | 0 | if (i > 0) |
610 | 0 | return; |
611 | 0 | REF_ASSERT_ISNT(i < 0); |
612 | 0 | EVP_PKEY_free_it(x); |
613 | 0 | CRYPTO_THREAD_lock_free(x->lock); |
614 | 0 | sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); |
615 | 0 | OPENSSL_free(x); |
616 | 0 | } |
617 | | |
618 | | static void EVP_PKEY_free_it(EVP_PKEY *x) |
619 | 0 | { |
620 | | /* internal function; x is never NULL */ |
621 | 0 | if (x->ameth && x->ameth->pkey_free) { |
622 | 0 | x->ameth->pkey_free(x); |
623 | 0 | x->pkey.ptr = NULL; |
624 | 0 | } |
625 | 0 | #ifndef OPENSSL_NO_ENGINE |
626 | 0 | ENGINE_finish(x->engine); |
627 | 0 | x->engine = NULL; |
628 | 0 | ENGINE_finish(x->pmeth_engine); |
629 | 0 | x->pmeth_engine = NULL; |
630 | 0 | #endif |
631 | 0 | } |
632 | | |
633 | | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent, |
634 | | const char *kstr) |
635 | 0 | { |
636 | 0 | BIO_indent(out, indent, 128); |
637 | 0 | BIO_printf(out, "%s algorithm \"%s\" unsupported\n", |
638 | 0 | kstr, OBJ_nid2ln(pkey->type)); |
639 | 0 | return 1; |
640 | 0 | } |
641 | | |
642 | | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, |
643 | | int indent, ASN1_PCTX *pctx) |
644 | 0 | { |
645 | 0 | if (pkey->ameth && pkey->ameth->pub_print) |
646 | 0 | return pkey->ameth->pub_print(out, pkey, indent, pctx); |
647 | | |
648 | 0 | return unsup_alg(out, pkey, indent, "Public Key"); |
649 | 0 | } |
650 | | |
651 | | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, |
652 | | int indent, ASN1_PCTX *pctx) |
653 | 0 | { |
654 | 0 | if (pkey->ameth && pkey->ameth->priv_print) |
655 | 0 | return pkey->ameth->priv_print(out, pkey, indent, pctx); |
656 | | |
657 | 0 | return unsup_alg(out, pkey, indent, "Private Key"); |
658 | 0 | } |
659 | | |
660 | | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, |
661 | | int indent, ASN1_PCTX *pctx) |
662 | 0 | { |
663 | 0 | if (pkey->ameth && pkey->ameth->param_print) |
664 | 0 | return pkey->ameth->param_print(out, pkey, indent, pctx); |
665 | 0 | return unsup_alg(out, pkey, indent, "Parameters"); |
666 | 0 | } |
667 | | |
668 | | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2) |
669 | 0 | { |
670 | 0 | if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL) |
671 | 0 | return -2; |
672 | 0 | return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2); |
673 | 0 | } |
674 | | |
675 | | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid) |
676 | 0 | { |
677 | 0 | return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid); |
678 | 0 | } |
679 | | |
680 | | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, |
681 | | const unsigned char *pt, size_t ptlen) |
682 | 0 | { |
683 | 0 | if (ptlen > INT_MAX) |
684 | 0 | return 0; |
685 | 0 | if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen, |
686 | 0 | (void *)pt) <= 0) |
687 | 0 | return 0; |
688 | 0 | return 1; |
689 | 0 | } |
690 | | |
691 | | size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt) |
692 | 0 | { |
693 | 0 | int rv; |
694 | 0 | rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt); |
695 | 0 | if (rv <= 0) |
696 | 0 | return 0; |
697 | 0 | return rv; |
698 | 0 | } |