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