/src/openssl/crypto/evp/asymcipher.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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 <stdlib.h> |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/evp.h> |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/provider.h" |
16 | | #include "internal/core.h" |
17 | | #include "crypto/evp.h" |
18 | | #include "evp_local.h" |
19 | | |
20 | | static int evp_pkey_asym_cipher_init(EVP_PKEY_CTX *ctx, int operation, |
21 | | const OSSL_PARAM params[]) |
22 | 0 | { |
23 | 0 | int ret = 0; |
24 | 0 | void *provkey = NULL; |
25 | 0 | EVP_ASYM_CIPHER *cipher = NULL; |
26 | 0 | EVP_KEYMGMT *tmp_keymgmt = NULL; |
27 | 0 | const OSSL_PROVIDER *tmp_prov = NULL; |
28 | 0 | const char *supported_ciph = NULL; |
29 | 0 | int iter; |
30 | |
|
31 | 0 | if (ctx == NULL) { |
32 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
33 | 0 | return -2; |
34 | 0 | } |
35 | | |
36 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
37 | 0 | ctx->operation = operation; |
38 | |
|
39 | 0 | ERR_set_mark(); |
40 | |
|
41 | 0 | if (evp_pkey_ctx_is_legacy(ctx)) |
42 | 0 | goto legacy; |
43 | | |
44 | 0 | if (ctx->pkey == NULL) { |
45 | 0 | ERR_clear_last_mark(); |
46 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
47 | 0 | goto err; |
48 | 0 | } |
49 | | |
50 | | /* |
51 | | * Try to derive the supported asym cipher from |ctx->keymgmt|. |
52 | | */ |
53 | 0 | if (!ossl_assert(ctx->pkey->keymgmt == NULL |
54 | 0 | || ctx->pkey->keymgmt == ctx->keymgmt)) { |
55 | 0 | ERR_clear_last_mark(); |
56 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
57 | 0 | goto err; |
58 | 0 | } |
59 | 0 | supported_ciph |
60 | 0 | = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
61 | 0 | OSSL_OP_ASYM_CIPHER); |
62 | 0 | if (supported_ciph == NULL) { |
63 | 0 | ERR_clear_last_mark(); |
64 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
65 | 0 | goto err; |
66 | 0 | } |
67 | | |
68 | | /* |
69 | | * We perform two iterations: |
70 | | * |
71 | | * 1. Do the normal asym cipher fetch, using the fetching data given by |
72 | | * the EVP_PKEY_CTX. |
73 | | * 2. Do the provider specific asym cipher fetch, from the same provider |
74 | | * as |ctx->keymgmt| |
75 | | * |
76 | | * We then try to fetch the keymgmt from the same provider as the |
77 | | * asym cipher, and try to export |ctx->pkey| to that keymgmt (when |
78 | | * this keymgmt happens to be the same as |ctx->keymgmt|, the export |
79 | | * is a no-op, but we call it anyway to not complicate the code even |
80 | | * more). |
81 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
82 | | * we're done and can perform the operation itself. If not, we perform |
83 | | * the second iteration, or jump to legacy. |
84 | | */ |
85 | 0 | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
86 | 0 | EVP_KEYMGMT *tmp_keymgmt_tofree; |
87 | | |
88 | | /* |
89 | | * If we're on the second iteration, free the results from the first. |
90 | | * They are NULL on the first iteration, so no need to check what |
91 | | * iteration we're on. |
92 | | */ |
93 | 0 | EVP_ASYM_CIPHER_free(cipher); |
94 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
95 | |
|
96 | 0 | switch (iter) { |
97 | 0 | case 1: |
98 | 0 | cipher = EVP_ASYM_CIPHER_fetch(ctx->libctx, supported_ciph, |
99 | 0 | ctx->propquery); |
100 | 0 | if (cipher != NULL) |
101 | 0 | tmp_prov = EVP_ASYM_CIPHER_get0_provider(cipher); |
102 | 0 | break; |
103 | 0 | case 2: |
104 | 0 | tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
105 | 0 | cipher = |
106 | 0 | evp_asym_cipher_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
107 | 0 | supported_ciph, ctx->propquery); |
108 | 0 | if (cipher == NULL) |
109 | 0 | goto legacy; |
110 | 0 | break; |
111 | 0 | } |
112 | 0 | if (cipher == NULL) |
113 | 0 | continue; |
114 | | |
115 | | /* |
116 | | * Ensure that the key is provided, either natively, or as a cached |
117 | | * export. We start by fetching the keymgmt with the same name as |
118 | | * |ctx->pkey|, but from the provider of the asym cipher method, using |
119 | | * the same property query as when fetching the asym cipher method. |
120 | | * With the keymgmt we found (if we did), we try to export |ctx->pkey| |
121 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
122 | | * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
123 | | */ |
124 | 0 | tmp_keymgmt_tofree = tmp_keymgmt |
125 | 0 | = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
126 | 0 | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
127 | 0 | ctx->propquery); |
128 | 0 | if (tmp_keymgmt != NULL) |
129 | 0 | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
130 | 0 | &tmp_keymgmt, ctx->propquery); |
131 | 0 | if (tmp_keymgmt == NULL) |
132 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
133 | 0 | } |
134 | | |
135 | 0 | if (provkey == NULL) { |
136 | 0 | EVP_ASYM_CIPHER_free(cipher); |
137 | 0 | goto legacy; |
138 | 0 | } |
139 | | |
140 | 0 | ERR_pop_to_mark(); |
141 | | |
142 | | /* No more legacy from here down to legacy: */ |
143 | |
|
144 | 0 | ctx->op.ciph.cipher = cipher; |
145 | 0 | ctx->op.ciph.algctx = cipher->newctx(ossl_provider_ctx(cipher->prov)); |
146 | 0 | if (ctx->op.ciph.algctx == NULL) { |
147 | | /* The provider key can stay in the cache */ |
148 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
149 | 0 | goto err; |
150 | 0 | } |
151 | | |
152 | 0 | switch (operation) { |
153 | 0 | case EVP_PKEY_OP_ENCRYPT: |
154 | 0 | if (cipher->encrypt_init == NULL) { |
155 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
156 | 0 | ret = -2; |
157 | 0 | goto err; |
158 | 0 | } |
159 | 0 | ret = cipher->encrypt_init(ctx->op.ciph.algctx, provkey, params); |
160 | 0 | break; |
161 | 0 | case EVP_PKEY_OP_DECRYPT: |
162 | 0 | if (cipher->decrypt_init == NULL) { |
163 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
164 | 0 | ret = -2; |
165 | 0 | goto err; |
166 | 0 | } |
167 | 0 | ret = cipher->decrypt_init(ctx->op.ciph.algctx, provkey, params); |
168 | 0 | break; |
169 | 0 | default: |
170 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
171 | 0 | goto err; |
172 | 0 | } |
173 | | |
174 | 0 | if (ret <= 0) |
175 | 0 | goto err; |
176 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
177 | 0 | return 1; |
178 | | |
179 | 0 | legacy: |
180 | | /* |
181 | | * If we don't have the full support we need with provided methods, |
182 | | * let's go see if legacy does. |
183 | | */ |
184 | 0 | ERR_pop_to_mark(); |
185 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
186 | 0 | tmp_keymgmt = NULL; |
187 | |
|
188 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) { |
189 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
190 | 0 | return -2; |
191 | 0 | } |
192 | 0 | switch (ctx->operation) { |
193 | 0 | case EVP_PKEY_OP_ENCRYPT: |
194 | 0 | if (ctx->pmeth->encrypt_init == NULL) |
195 | 0 | return 1; |
196 | 0 | ret = ctx->pmeth->encrypt_init(ctx); |
197 | 0 | break; |
198 | 0 | case EVP_PKEY_OP_DECRYPT: |
199 | 0 | if (ctx->pmeth->decrypt_init == NULL) |
200 | 0 | return 1; |
201 | 0 | ret = ctx->pmeth->decrypt_init(ctx); |
202 | 0 | break; |
203 | 0 | default: |
204 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
205 | 0 | ret = -1; |
206 | 0 | } |
207 | | |
208 | 0 | err: |
209 | 0 | if (ret <= 0) { |
210 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
211 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
212 | 0 | } |
213 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
214 | 0 | return ret; |
215 | 0 | } |
216 | | |
217 | | int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx) |
218 | 0 | { |
219 | 0 | return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, NULL); |
220 | 0 | } |
221 | | |
222 | | int EVP_PKEY_encrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
223 | 0 | { |
224 | 0 | return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_ENCRYPT, params); |
225 | 0 | } |
226 | | |
227 | | int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx, |
228 | | unsigned char *out, size_t *outlen, |
229 | | const unsigned char *in, size_t inlen) |
230 | 0 | { |
231 | 0 | int ret; |
232 | |
|
233 | 0 | if (ctx == NULL) { |
234 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
235 | 0 | return -2; |
236 | 0 | } |
237 | | |
238 | 0 | if (ctx->operation != EVP_PKEY_OP_ENCRYPT) { |
239 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
240 | 0 | return -1; |
241 | 0 | } |
242 | | |
243 | 0 | if (ctx->op.ciph.algctx == NULL) |
244 | 0 | goto legacy; |
245 | | |
246 | 0 | ret = ctx->op.ciph.cipher->encrypt(ctx->op.ciph.algctx, out, outlen, |
247 | 0 | (out == NULL ? 0 : *outlen), in, inlen); |
248 | 0 | return ret; |
249 | | |
250 | 0 | legacy: |
251 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) { |
252 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
253 | 0 | return -2; |
254 | 0 | } |
255 | 0 | M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT) |
256 | 0 | return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen); |
257 | 0 | } |
258 | | |
259 | | int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx) |
260 | 0 | { |
261 | 0 | return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL); |
262 | 0 | } |
263 | | |
264 | | int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
265 | 0 | { |
266 | 0 | return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params); |
267 | 0 | } |
268 | | |
269 | | int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx, |
270 | | unsigned char *out, size_t *outlen, |
271 | | const unsigned char *in, size_t inlen) |
272 | 0 | { |
273 | 0 | int ret; |
274 | |
|
275 | 0 | if (ctx == NULL) { |
276 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
277 | 0 | return -2; |
278 | 0 | } |
279 | | |
280 | 0 | if (ctx->operation != EVP_PKEY_OP_DECRYPT) { |
281 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
282 | 0 | return -1; |
283 | 0 | } |
284 | | |
285 | 0 | if (ctx->op.ciph.algctx == NULL) |
286 | 0 | goto legacy; |
287 | | |
288 | 0 | ret = ctx->op.ciph.cipher->decrypt(ctx->op.ciph.algctx, out, outlen, |
289 | 0 | (out == NULL ? 0 : *outlen), in, inlen); |
290 | 0 | return ret; |
291 | | |
292 | 0 | legacy: |
293 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) { |
294 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
295 | 0 | return -2; |
296 | 0 | } |
297 | 0 | M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT) |
298 | 0 | return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen); |
299 | 0 | } |
300 | | |
301 | | /* decrypt to new buffer of dynamic size, checking any pre-determined size */ |
302 | | int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp, |
303 | | size_t *outlenp, size_t expected_outlen, |
304 | | const unsigned char *in, size_t inlen) |
305 | 0 | { |
306 | 0 | if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0 |
307 | 0 | || (*outp = OPENSSL_malloc(*outlenp)) == NULL) |
308 | 0 | return -1; |
309 | 0 | if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0 |
310 | 0 | || *outlenp == 0 |
311 | 0 | || (expected_outlen != 0 && *outlenp != expected_outlen)) { |
312 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
313 | 0 | OPENSSL_clear_free(*outp, *outlenp); |
314 | 0 | *outp = NULL; |
315 | 0 | return 0; |
316 | 0 | } |
317 | 0 | return 1; |
318 | 0 | } |
319 | | |
320 | | static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov) |
321 | 0 | { |
322 | 0 | EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER)); |
323 | |
|
324 | 0 | if (cipher == NULL) |
325 | 0 | return NULL; |
326 | | |
327 | 0 | cipher->lock = CRYPTO_THREAD_lock_new(); |
328 | 0 | if (cipher->lock == NULL) { |
329 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
330 | 0 | OPENSSL_free(cipher); |
331 | 0 | return NULL; |
332 | 0 | } |
333 | 0 | cipher->prov = prov; |
334 | 0 | ossl_provider_up_ref(prov); |
335 | 0 | cipher->refcnt = 1; |
336 | |
|
337 | 0 | return cipher; |
338 | 0 | } |
339 | | |
340 | | static void *evp_asym_cipher_from_algorithm(int name_id, |
341 | | const OSSL_ALGORITHM *algodef, |
342 | | OSSL_PROVIDER *prov) |
343 | 0 | { |
344 | 0 | const OSSL_DISPATCH *fns = algodef->implementation; |
345 | 0 | EVP_ASYM_CIPHER *cipher = NULL; |
346 | 0 | int ctxfncnt = 0, encfncnt = 0, decfncnt = 0; |
347 | 0 | int gparamfncnt = 0, sparamfncnt = 0; |
348 | |
|
349 | 0 | if ((cipher = evp_asym_cipher_new(prov)) == NULL) { |
350 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
351 | 0 | goto err; |
352 | 0 | } |
353 | | |
354 | 0 | cipher->name_id = name_id; |
355 | 0 | if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
356 | 0 | goto err; |
357 | 0 | cipher->description = algodef->algorithm_description; |
358 | |
|
359 | 0 | for (; fns->function_id != 0; fns++) { |
360 | 0 | switch (fns->function_id) { |
361 | 0 | case OSSL_FUNC_ASYM_CIPHER_NEWCTX: |
362 | 0 | if (cipher->newctx != NULL) |
363 | 0 | break; |
364 | 0 | cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns); |
365 | 0 | ctxfncnt++; |
366 | 0 | break; |
367 | 0 | case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT: |
368 | 0 | if (cipher->encrypt_init != NULL) |
369 | 0 | break; |
370 | 0 | cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns); |
371 | 0 | encfncnt++; |
372 | 0 | break; |
373 | 0 | case OSSL_FUNC_ASYM_CIPHER_ENCRYPT: |
374 | 0 | if (cipher->encrypt != NULL) |
375 | 0 | break; |
376 | 0 | cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns); |
377 | 0 | encfncnt++; |
378 | 0 | break; |
379 | 0 | case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT: |
380 | 0 | if (cipher->decrypt_init != NULL) |
381 | 0 | break; |
382 | 0 | cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns); |
383 | 0 | decfncnt++; |
384 | 0 | break; |
385 | 0 | case OSSL_FUNC_ASYM_CIPHER_DECRYPT: |
386 | 0 | if (cipher->decrypt != NULL) |
387 | 0 | break; |
388 | 0 | cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns); |
389 | 0 | decfncnt++; |
390 | 0 | break; |
391 | 0 | case OSSL_FUNC_ASYM_CIPHER_FREECTX: |
392 | 0 | if (cipher->freectx != NULL) |
393 | 0 | break; |
394 | 0 | cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns); |
395 | 0 | ctxfncnt++; |
396 | 0 | break; |
397 | 0 | case OSSL_FUNC_ASYM_CIPHER_DUPCTX: |
398 | 0 | if (cipher->dupctx != NULL) |
399 | 0 | break; |
400 | 0 | cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns); |
401 | 0 | break; |
402 | 0 | case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS: |
403 | 0 | if (cipher->get_ctx_params != NULL) |
404 | 0 | break; |
405 | 0 | cipher->get_ctx_params |
406 | 0 | = OSSL_FUNC_asym_cipher_get_ctx_params(fns); |
407 | 0 | gparamfncnt++; |
408 | 0 | break; |
409 | 0 | case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS: |
410 | 0 | if (cipher->gettable_ctx_params != NULL) |
411 | 0 | break; |
412 | 0 | cipher->gettable_ctx_params |
413 | 0 | = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns); |
414 | 0 | gparamfncnt++; |
415 | 0 | break; |
416 | 0 | case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS: |
417 | 0 | if (cipher->set_ctx_params != NULL) |
418 | 0 | break; |
419 | 0 | cipher->set_ctx_params |
420 | 0 | = OSSL_FUNC_asym_cipher_set_ctx_params(fns); |
421 | 0 | sparamfncnt++; |
422 | 0 | break; |
423 | 0 | case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS: |
424 | 0 | if (cipher->settable_ctx_params != NULL) |
425 | 0 | break; |
426 | 0 | cipher->settable_ctx_params |
427 | 0 | = OSSL_FUNC_asym_cipher_settable_ctx_params(fns); |
428 | 0 | sparamfncnt++; |
429 | 0 | break; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | if (ctxfncnt != 2 |
433 | 0 | || (encfncnt != 0 && encfncnt != 2) |
434 | 0 | || (decfncnt != 0 && decfncnt != 2) |
435 | 0 | || (encfncnt != 2 && decfncnt != 2) |
436 | 0 | || (gparamfncnt != 0 && gparamfncnt != 2) |
437 | 0 | || (sparamfncnt != 0 && sparamfncnt != 2)) { |
438 | | /* |
439 | | * In order to be a consistent set of functions we must have at least |
440 | | * a set of context functions (newctx and freectx) as well as a pair of |
441 | | * "cipher" functions: (encrypt_init, encrypt) or |
442 | | * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are |
443 | | * optional, but if one of them is present then the other one must also |
444 | | * be present. The same applies to get_ctx_params and |
445 | | * gettable_ctx_params. The dupctx function is optional. |
446 | | */ |
447 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
448 | 0 | goto err; |
449 | 0 | } |
450 | | |
451 | 0 | return cipher; |
452 | 0 | err: |
453 | 0 | EVP_ASYM_CIPHER_free(cipher); |
454 | 0 | return NULL; |
455 | 0 | } |
456 | | |
457 | | void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher) |
458 | 0 | { |
459 | 0 | int i; |
460 | |
|
461 | 0 | if (cipher == NULL) |
462 | 0 | return; |
463 | 0 | CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock); |
464 | 0 | if (i > 0) |
465 | 0 | return; |
466 | 0 | OPENSSL_free(cipher->type_name); |
467 | 0 | ossl_provider_free(cipher->prov); |
468 | 0 | CRYPTO_THREAD_lock_free(cipher->lock); |
469 | 0 | OPENSSL_free(cipher); |
470 | 0 | } |
471 | | |
472 | | int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher) |
473 | 0 | { |
474 | 0 | int ref = 0; |
475 | |
|
476 | 0 | CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock); |
477 | 0 | return 1; |
478 | 0 | } |
479 | | |
480 | | OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher) |
481 | 0 | { |
482 | 0 | return cipher->prov; |
483 | 0 | } |
484 | | |
485 | | EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
486 | | const char *properties) |
487 | 0 | { |
488 | 0 | return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties, |
489 | 0 | evp_asym_cipher_from_algorithm, |
490 | 0 | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
491 | 0 | (void (*)(void *))EVP_ASYM_CIPHER_free); |
492 | 0 | } |
493 | | |
494 | | EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov, |
495 | | const char *algorithm, |
496 | | const char *properties) |
497 | 0 | { |
498 | 0 | return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER, |
499 | 0 | algorithm, properties, |
500 | 0 | evp_asym_cipher_from_algorithm, |
501 | 0 | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
502 | 0 | (void (*)(void *))EVP_ASYM_CIPHER_free); |
503 | 0 | } |
504 | | |
505 | | int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name) |
506 | 0 | { |
507 | 0 | return evp_is_a(cipher->prov, cipher->name_id, NULL, name); |
508 | 0 | } |
509 | | |
510 | | int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher) |
511 | 0 | { |
512 | 0 | return cipher->name_id; |
513 | 0 | } |
514 | | |
515 | | const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher) |
516 | 0 | { |
517 | 0 | return cipher->type_name; |
518 | 0 | } |
519 | | |
520 | | const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher) |
521 | 0 | { |
522 | 0 | return cipher->description; |
523 | 0 | } |
524 | | |
525 | | void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, |
526 | | void (*fn)(EVP_ASYM_CIPHER *cipher, |
527 | | void *arg), |
528 | | void *arg) |
529 | 0 | { |
530 | 0 | evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER, |
531 | 0 | (void (*)(void *, void *))fn, arg, |
532 | 0 | evp_asym_cipher_from_algorithm, |
533 | 0 | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
534 | 0 | (void (*)(void *))EVP_ASYM_CIPHER_free); |
535 | 0 | } |
536 | | |
537 | | |
538 | | int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher, |
539 | | void (*fn)(const char *name, void *data), |
540 | | void *data) |
541 | 0 | { |
542 | 0 | if (cipher->prov != NULL) |
543 | 0 | return evp_names_do_all(cipher->prov, cipher->name_id, fn, data); |
544 | | |
545 | 0 | return 1; |
546 | 0 | } |
547 | | |
548 | | const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip) |
549 | 0 | { |
550 | 0 | void *provctx; |
551 | |
|
552 | 0 | if (cip == NULL || cip->gettable_ctx_params == NULL) |
553 | 0 | return NULL; |
554 | | |
555 | 0 | provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
556 | 0 | return cip->gettable_ctx_params(NULL, provctx); |
557 | 0 | } |
558 | | |
559 | | const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip) |
560 | 0 | { |
561 | 0 | void *provctx; |
562 | |
|
563 | 0 | if (cip == NULL || cip->settable_ctx_params == NULL) |
564 | 0 | return NULL; |
565 | | |
566 | 0 | provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
567 | 0 | return cip->settable_ctx_params(NULL, provctx); |
568 | 0 | } |