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