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