/src/openssl34/crypto/evp/asymcipher.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2006-2023 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 | | /* decrypt to new buffer of dynamic size, checking any pre-determined size */ |
299 | | int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp, |
300 | | size_t *outlenp, size_t expected_outlen, |
301 | | const unsigned char *in, size_t inlen) |
302 | 0 | { |
303 | 0 | if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0 |
304 | 0 | || (*outp = OPENSSL_malloc(*outlenp)) == NULL) |
305 | 0 | return -1; |
306 | 0 | if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0 |
307 | 0 | || *outlenp == 0 |
308 | 0 | || (expected_outlen != 0 && *outlenp != expected_outlen)) { |
309 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
310 | 0 | OPENSSL_clear_free(*outp, *outlenp); |
311 | 0 | *outp = NULL; |
312 | 0 | return 0; |
313 | 0 | } |
314 | 0 | return 1; |
315 | 0 | } |
316 | | |
317 | | static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov) |
318 | 18 | { |
319 | 18 | EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER)); |
320 | | |
321 | 18 | if (cipher == NULL) |
322 | 0 | return NULL; |
323 | | |
324 | 18 | if (!CRYPTO_NEW_REF(&cipher->refcnt, 1)) { |
325 | 0 | OPENSSL_free(cipher); |
326 | 0 | return NULL; |
327 | 0 | } |
328 | 18 | cipher->prov = prov; |
329 | 18 | ossl_provider_up_ref(prov); |
330 | | |
331 | 18 | return cipher; |
332 | 18 | } |
333 | | |
334 | | static void *evp_asym_cipher_from_algorithm(int name_id, |
335 | | const OSSL_ALGORITHM *algodef, |
336 | | OSSL_PROVIDER *prov) |
337 | 54 | { |
338 | 54 | const OSSL_DISPATCH *fns = algodef->implementation; |
339 | 54 | EVP_ASYM_CIPHER *cipher = NULL; |
340 | 54 | int ctxfncnt = 0, encfncnt = 0, decfncnt = 0; |
341 | 54 | int gparamfncnt = 0, sparamfncnt = 0; |
342 | | |
343 | 54 | if ((cipher = evp_asym_cipher_new(prov)) == NULL) { |
344 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
345 | 0 | goto err; |
346 | 0 | } |
347 | | |
348 | 54 | cipher->name_id = name_id; |
349 | 54 | if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
350 | 0 | goto err; |
351 | 54 | cipher->description = algodef->algorithm_description; |
352 | | |
353 | 648 | for (; fns->function_id != 0; fns++) { |
354 | 594 | switch (fns->function_id) { |
355 | 54 | case OSSL_FUNC_ASYM_CIPHER_NEWCTX: |
356 | 54 | if (cipher->newctx != NULL) |
357 | 0 | break; |
358 | 54 | cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns); |
359 | 54 | ctxfncnt++; |
360 | 54 | break; |
361 | 54 | case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT: |
362 | 54 | if (cipher->encrypt_init != NULL) |
363 | 0 | break; |
364 | 54 | cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns); |
365 | 54 | encfncnt++; |
366 | 54 | break; |
367 | 54 | case OSSL_FUNC_ASYM_CIPHER_ENCRYPT: |
368 | 54 | if (cipher->encrypt != NULL) |
369 | 0 | break; |
370 | 54 | cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns); |
371 | 54 | encfncnt++; |
372 | 54 | break; |
373 | 54 | case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT: |
374 | 54 | if (cipher->decrypt_init != NULL) |
375 | 0 | break; |
376 | 54 | cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns); |
377 | 54 | decfncnt++; |
378 | 54 | break; |
379 | 54 | case OSSL_FUNC_ASYM_CIPHER_DECRYPT: |
380 | 54 | if (cipher->decrypt != NULL) |
381 | 0 | break; |
382 | 54 | cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns); |
383 | 54 | decfncnt++; |
384 | 54 | break; |
385 | 54 | case OSSL_FUNC_ASYM_CIPHER_FREECTX: |
386 | 54 | if (cipher->freectx != NULL) |
387 | 0 | break; |
388 | 54 | cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns); |
389 | 54 | ctxfncnt++; |
390 | 54 | break; |
391 | 54 | case OSSL_FUNC_ASYM_CIPHER_DUPCTX: |
392 | 54 | if (cipher->dupctx != NULL) |
393 | 0 | break; |
394 | 54 | cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns); |
395 | 54 | break; |
396 | 54 | case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS: |
397 | 54 | if (cipher->get_ctx_params != NULL) |
398 | 0 | break; |
399 | 54 | cipher->get_ctx_params |
400 | 54 | = OSSL_FUNC_asym_cipher_get_ctx_params(fns); |
401 | 54 | gparamfncnt++; |
402 | 54 | break; |
403 | 54 | case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS: |
404 | 54 | if (cipher->gettable_ctx_params != NULL) |
405 | 0 | break; |
406 | 54 | cipher->gettable_ctx_params |
407 | 54 | = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns); |
408 | 54 | gparamfncnt++; |
409 | 54 | break; |
410 | 54 | case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS: |
411 | 54 | if (cipher->set_ctx_params != NULL) |
412 | 0 | break; |
413 | 54 | cipher->set_ctx_params |
414 | 54 | = OSSL_FUNC_asym_cipher_set_ctx_params(fns); |
415 | 54 | sparamfncnt++; |
416 | 54 | break; |
417 | 54 | case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS: |
418 | 54 | if (cipher->settable_ctx_params != NULL) |
419 | 0 | break; |
420 | 54 | cipher->settable_ctx_params |
421 | 54 | = OSSL_FUNC_asym_cipher_settable_ctx_params(fns); |
422 | 54 | sparamfncnt++; |
423 | 54 | break; |
424 | 594 | } |
425 | 594 | } |
426 | 54 | if (ctxfncnt != 2 |
427 | 54 | || (encfncnt != 0 && encfncnt != 2) |
428 | 54 | || (decfncnt != 0 && decfncnt != 2) |
429 | 54 | || (encfncnt != 2 && decfncnt != 2) |
430 | 54 | || (gparamfncnt != 0 && gparamfncnt != 2) |
431 | 54 | || (sparamfncnt != 0 && sparamfncnt != 2)) { |
432 | | /* |
433 | | * In order to be a consistent set of functions we must have at least |
434 | | * a set of context functions (newctx and freectx) as well as a pair of |
435 | | * "cipher" functions: (encrypt_init, encrypt) or |
436 | | * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are |
437 | | * optional, but if one of them is present then the other one must also |
438 | | * be present. The same applies to get_ctx_params and |
439 | | * gettable_ctx_params. The dupctx function is optional. |
440 | | */ |
441 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
442 | 0 | goto err; |
443 | 0 | } |
444 | | |
445 | 54 | return cipher; |
446 | 0 | err: |
447 | 0 | EVP_ASYM_CIPHER_free(cipher); |
448 | 0 | return NULL; |
449 | 54 | } |
450 | | |
451 | | void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher) |
452 | 33.9k | { |
453 | 33.9k | int i; |
454 | | |
455 | 33.9k | if (cipher == NULL) |
456 | 16.9k | return; |
457 | 17.0k | CRYPTO_DOWN_REF(&cipher->refcnt, &i); |
458 | 17.0k | if (i > 0) |
459 | 16.9k | return; |
460 | 32 | OPENSSL_free(cipher->type_name); |
461 | 32 | ossl_provider_free(cipher->prov); |
462 | 32 | CRYPTO_FREE_REF(&cipher->refcnt); |
463 | 32 | OPENSSL_free(cipher); |
464 | 32 | } |
465 | | |
466 | | int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher) |
467 | 17.0k | { |
468 | 17.0k | int ref = 0; |
469 | | |
470 | 17.0k | CRYPTO_UP_REF(&cipher->refcnt, &ref); |
471 | 17.0k | return 1; |
472 | 17.0k | } |
473 | | |
474 | | OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher) |
475 | 28.3k | { |
476 | 28.3k | return cipher->prov; |
477 | 28.3k | } |
478 | | |
479 | | EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
480 | | const char *properties) |
481 | 16.9k | { |
482 | 16.9k | return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties, |
483 | 16.9k | evp_asym_cipher_from_algorithm, |
484 | 16.9k | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
485 | 16.9k | (void (*)(void *))EVP_ASYM_CIPHER_free); |
486 | 16.9k | } |
487 | | |
488 | | EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov, |
489 | | const char *algorithm, |
490 | | const char *properties) |
491 | 0 | { |
492 | 0 | return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER, |
493 | 0 | algorithm, properties, |
494 | 0 | evp_asym_cipher_from_algorithm, |
495 | 0 | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
496 | 0 | (void (*)(void *))EVP_ASYM_CIPHER_free); |
497 | 0 | } |
498 | | |
499 | | int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name) |
500 | 0 | { |
501 | 0 | return evp_is_a(cipher->prov, cipher->name_id, NULL, name); |
502 | 0 | } |
503 | | |
504 | | int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher) |
505 | 0 | { |
506 | 0 | return cipher->name_id; |
507 | 0 | } |
508 | | |
509 | | const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher) |
510 | 0 | { |
511 | 0 | return cipher->type_name; |
512 | 0 | } |
513 | | |
514 | | const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher) |
515 | 0 | { |
516 | 0 | return cipher->description; |
517 | 0 | } |
518 | | |
519 | | void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx, |
520 | | void (*fn)(EVP_ASYM_CIPHER *cipher, |
521 | | void *arg), |
522 | | void *arg) |
523 | 8 | { |
524 | 8 | evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER, |
525 | 8 | (void (*)(void *, void *))fn, arg, |
526 | 8 | evp_asym_cipher_from_algorithm, |
527 | 8 | (int (*)(void *))EVP_ASYM_CIPHER_up_ref, |
528 | 8 | (void (*)(void *))EVP_ASYM_CIPHER_free); |
529 | 8 | } |
530 | | |
531 | | int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher, |
532 | | void (*fn)(const char *name, void *data), |
533 | | void *data) |
534 | 0 | { |
535 | 0 | if (cipher->prov != NULL) |
536 | 0 | return evp_names_do_all(cipher->prov, cipher->name_id, fn, data); |
537 | | |
538 | 0 | return 1; |
539 | 0 | } |
540 | | |
541 | | const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip) |
542 | 0 | { |
543 | 0 | void *provctx; |
544 | |
|
545 | 0 | if (cip == NULL || cip->gettable_ctx_params == NULL) |
546 | 0 | return NULL; |
547 | | |
548 | 0 | provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
549 | 0 | return cip->gettable_ctx_params(NULL, provctx); |
550 | 0 | } |
551 | | |
552 | | const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip) |
553 | 24 | { |
554 | 24 | void *provctx; |
555 | | |
556 | 24 | if (cip == NULL || cip->settable_ctx_params == NULL) |
557 | 0 | return NULL; |
558 | | |
559 | 24 | provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip)); |
560 | | return cip->settable_ctx_params(NULL, provctx); |
561 | 24 | } |