/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_free(data);  | 
23  | 0  | }  | 
24  |  |  | 
25  |  | static int evp_asym_cipher_up_ref(void *data)  | 
26  | 0  | { | 
27  | 0  |     return EVP_ASYM_CIPHER_up_ref(data);  | 
28  | 0  | }  | 
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  |     ERR_set_mark();  | 
265  | 0  |     ret = cipher->encrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);  | 
266  | 0  |     if (ret <= 0 && ERR_count_to_mark() == 0)  | 
267  | 0  |         ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,  | 
268  | 0  |                        "%s encrypt:%s", cipher->type_name, desc);  | 
269  | 0  |     ERR_clear_last_mark();  | 
270  | 0  |     return ret;  | 
271  |  |  | 
272  | 0  |  legacy:  | 
273  | 0  |     if (ctx->pmeth == NULL || ctx->pmeth->encrypt == NULL) { | 
274  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
275  | 0  |         return -2;  | 
276  | 0  |     }  | 
277  | 0  |     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)  | 
278  | 0  |         return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);  | 
279  | 0  | }  | 
280  |  |  | 
281  |  | int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)  | 
282  | 0  | { | 
283  | 0  |     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, NULL);  | 
284  | 0  | }  | 
285  |  |  | 
286  |  | int EVP_PKEY_decrypt_init_ex(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])  | 
287  | 0  | { | 
288  | 0  |     return evp_pkey_asym_cipher_init(ctx, EVP_PKEY_OP_DECRYPT, params);  | 
289  | 0  | }  | 
290  |  |  | 
291  |  | int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,  | 
292  |  |                      unsigned char *out, size_t *outlen,  | 
293  |  |                      const unsigned char *in, size_t inlen)  | 
294  | 0  | { | 
295  | 0  |     EVP_ASYM_CIPHER *cipher;  | 
296  | 0  |     const char *desc;  | 
297  | 0  |     int ret;  | 
298  |  | 
  | 
299  | 0  |     if (ctx == NULL) { | 
300  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
301  | 0  |         return -2;  | 
302  | 0  |     }  | 
303  |  |  | 
304  | 0  |     if (ctx->operation != EVP_PKEY_OP_DECRYPT) { | 
305  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);  | 
306  | 0  |         return -1;  | 
307  | 0  |     }  | 
308  |  |  | 
309  | 0  |     if (ctx->op.ciph.algctx == NULL)  | 
310  | 0  |         goto legacy;  | 
311  |  |  | 
312  | 0  |     cipher = ctx->op.ciph.cipher;  | 
313  | 0  |     desc = cipher->description != NULL ? cipher->description : "";  | 
314  | 0  |     ERR_set_mark();  | 
315  | 0  |     ret = cipher->decrypt(ctx->op.ciph.algctx, out, outlen, (out == NULL ? 0 : *outlen), in, inlen);  | 
316  | 0  |     if (ret <= 0 && ERR_count_to_mark() == 0)  | 
317  | 0  |         ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_ASYM_CIPHER_FAILURE,  | 
318  | 0  |                        "%s decrypt:%s", cipher->type_name, desc);  | 
319  | 0  |     ERR_clear_last_mark();  | 
320  |  | 
  | 
321  | 0  |     return ret;  | 
322  |  |  | 
323  | 0  |  legacy:  | 
324  | 0  |     if (ctx->pmeth == NULL || ctx->pmeth->decrypt == NULL) { | 
325  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
326  | 0  |         return -2;  | 
327  | 0  |     }  | 
328  | 0  |     M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)  | 
329  | 0  |         return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);  | 
330  | 0  | }  | 
331  |  |  | 
332  |  | /* decrypt to new buffer of dynamic size, checking any pre-determined size */  | 
333  |  | int evp_pkey_decrypt_alloc(EVP_PKEY_CTX *ctx, unsigned char **outp,  | 
334  |  |                            size_t *outlenp, size_t expected_outlen,  | 
335  |  |                            const unsigned char *in, size_t inlen)  | 
336  | 0  | { | 
337  | 0  |     if (EVP_PKEY_decrypt(ctx, NULL, outlenp, in, inlen) <= 0  | 
338  | 0  |             || (*outp = OPENSSL_malloc(*outlenp)) == NULL)  | 
339  | 0  |         return -1;  | 
340  | 0  |     if (EVP_PKEY_decrypt(ctx, *outp, outlenp, in, inlen) <= 0  | 
341  | 0  |             || *outlenp == 0  | 
342  | 0  |             || (expected_outlen != 0 && *outlenp != expected_outlen)) { | 
343  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);  | 
344  | 0  |         OPENSSL_clear_free(*outp, *outlenp);  | 
345  | 0  |         *outp = NULL;  | 
346  | 0  |         return 0;  | 
347  | 0  |     }  | 
348  | 0  |     return 1;  | 
349  | 0  | }  | 
350  |  |  | 
351  |  | static EVP_ASYM_CIPHER *evp_asym_cipher_new(OSSL_PROVIDER *prov)  | 
352  | 0  | { | 
353  | 0  |     EVP_ASYM_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_ASYM_CIPHER));  | 
354  |  | 
  | 
355  | 0  |     if (cipher == NULL)  | 
356  | 0  |         return NULL;  | 
357  |  |  | 
358  | 0  |     if (!CRYPTO_NEW_REF(&cipher->refcnt, 1)  | 
359  | 0  |             || !ossl_provider_up_ref(prov)) { | 
360  | 0  |         CRYPTO_FREE_REF(&cipher->refcnt);  | 
361  | 0  |         OPENSSL_free(cipher);  | 
362  | 0  |         return NULL;  | 
363  | 0  |     }  | 
364  | 0  |     cipher->prov = prov;  | 
365  |  | 
  | 
366  | 0  |     return cipher;  | 
367  | 0  | }  | 
368  |  |  | 
369  |  | static void *evp_asym_cipher_from_algorithm(int name_id,  | 
370  |  |                                             const OSSL_ALGORITHM *algodef,  | 
371  |  |                                             OSSL_PROVIDER *prov)  | 
372  | 0  | { | 
373  | 0  |     const OSSL_DISPATCH *fns = algodef->implementation;  | 
374  | 0  |     EVP_ASYM_CIPHER *cipher = NULL;  | 
375  | 0  |     int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;  | 
376  | 0  |     int gparamfncnt = 0, sparamfncnt = 0;  | 
377  |  | 
  | 
378  | 0  |     if ((cipher = evp_asym_cipher_new(prov)) == NULL) { | 
379  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);  | 
380  | 0  |         goto err;  | 
381  | 0  |     }  | 
382  |  |  | 
383  | 0  |     cipher->name_id = name_id;  | 
384  | 0  |     if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)  | 
385  | 0  |         goto err;  | 
386  | 0  |     cipher->description = algodef->algorithm_description;  | 
387  |  | 
  | 
388  | 0  |     for (; fns->function_id != 0; fns++) { | 
389  | 0  |         switch (fns->function_id) { | 
390  | 0  |         case OSSL_FUNC_ASYM_CIPHER_NEWCTX:  | 
391  | 0  |             if (cipher->newctx != NULL)  | 
392  | 0  |                 break;  | 
393  | 0  |             cipher->newctx = OSSL_FUNC_asym_cipher_newctx(fns);  | 
394  | 0  |             ctxfncnt++;  | 
395  | 0  |             break;  | 
396  | 0  |         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT:  | 
397  | 0  |             if (cipher->encrypt_init != NULL)  | 
398  | 0  |                 break;  | 
399  | 0  |             cipher->encrypt_init = OSSL_FUNC_asym_cipher_encrypt_init(fns);  | 
400  | 0  |             encfncnt++;  | 
401  | 0  |             break;  | 
402  | 0  |         case OSSL_FUNC_ASYM_CIPHER_ENCRYPT:  | 
403  | 0  |             if (cipher->encrypt != NULL)  | 
404  | 0  |                 break;  | 
405  | 0  |             cipher->encrypt = OSSL_FUNC_asym_cipher_encrypt(fns);  | 
406  | 0  |             encfncnt++;  | 
407  | 0  |             break;  | 
408  | 0  |         case OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT:  | 
409  | 0  |             if (cipher->decrypt_init != NULL)  | 
410  | 0  |                 break;  | 
411  | 0  |             cipher->decrypt_init = OSSL_FUNC_asym_cipher_decrypt_init(fns);  | 
412  | 0  |             decfncnt++;  | 
413  | 0  |             break;  | 
414  | 0  |         case OSSL_FUNC_ASYM_CIPHER_DECRYPT:  | 
415  | 0  |             if (cipher->decrypt != NULL)  | 
416  | 0  |                 break;  | 
417  | 0  |             cipher->decrypt = OSSL_FUNC_asym_cipher_decrypt(fns);  | 
418  | 0  |             decfncnt++;  | 
419  | 0  |             break;  | 
420  | 0  |         case OSSL_FUNC_ASYM_CIPHER_FREECTX:  | 
421  | 0  |             if (cipher->freectx != NULL)  | 
422  | 0  |                 break;  | 
423  | 0  |             cipher->freectx = OSSL_FUNC_asym_cipher_freectx(fns);  | 
424  | 0  |             ctxfncnt++;  | 
425  | 0  |             break;  | 
426  | 0  |         case OSSL_FUNC_ASYM_CIPHER_DUPCTX:  | 
427  | 0  |             if (cipher->dupctx != NULL)  | 
428  | 0  |                 break;  | 
429  | 0  |             cipher->dupctx = OSSL_FUNC_asym_cipher_dupctx(fns);  | 
430  | 0  |             break;  | 
431  | 0  |         case OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS:  | 
432  | 0  |             if (cipher->get_ctx_params != NULL)  | 
433  | 0  |                 break;  | 
434  | 0  |             cipher->get_ctx_params  | 
435  | 0  |                 = OSSL_FUNC_asym_cipher_get_ctx_params(fns);  | 
436  | 0  |             gparamfncnt++;  | 
437  | 0  |             break;  | 
438  | 0  |         case OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS:  | 
439  | 0  |             if (cipher->gettable_ctx_params != NULL)  | 
440  | 0  |                 break;  | 
441  | 0  |             cipher->gettable_ctx_params  | 
442  | 0  |                 = OSSL_FUNC_asym_cipher_gettable_ctx_params(fns);  | 
443  | 0  |             gparamfncnt++;  | 
444  | 0  |             break;  | 
445  | 0  |         case OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS:  | 
446  | 0  |             if (cipher->set_ctx_params != NULL)  | 
447  | 0  |                 break;  | 
448  | 0  |             cipher->set_ctx_params  | 
449  | 0  |                 = OSSL_FUNC_asym_cipher_set_ctx_params(fns);  | 
450  | 0  |             sparamfncnt++;  | 
451  | 0  |             break;  | 
452  | 0  |         case OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS:  | 
453  | 0  |             if (cipher->settable_ctx_params != NULL)  | 
454  | 0  |                 break;  | 
455  | 0  |             cipher->settable_ctx_params  | 
456  | 0  |                 = OSSL_FUNC_asym_cipher_settable_ctx_params(fns);  | 
457  | 0  |             sparamfncnt++;  | 
458  | 0  |             break;  | 
459  | 0  |         }  | 
460  | 0  |     }  | 
461  | 0  |     if (ctxfncnt != 2  | 
462  | 0  |         || (encfncnt != 0 && encfncnt != 2)  | 
463  | 0  |         || (decfncnt != 0 && decfncnt != 2)  | 
464  | 0  |         || (encfncnt != 2 && decfncnt != 2)  | 
465  | 0  |         || (gparamfncnt != 0 && gparamfncnt != 2)  | 
466  | 0  |         || (sparamfncnt != 0 && sparamfncnt != 2)) { | 
467  |  |         /*  | 
468  |  |          * In order to be a consistent set of functions we must have at least  | 
469  |  |          * a set of context functions (newctx and freectx) as well as a pair of  | 
470  |  |          * "cipher" functions: (encrypt_init, encrypt) or  | 
471  |  |          * (decrypt_init decrypt). set_ctx_params and settable_ctx_params are  | 
472  |  |          * optional, but if one of them is present then the other one must also  | 
473  |  |          * be present. The same applies to get_ctx_params and  | 
474  |  |          * gettable_ctx_params. The dupctx function is optional.  | 
475  |  |          */  | 
476  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);  | 
477  | 0  |         goto err;  | 
478  | 0  |     }  | 
479  |  |  | 
480  | 0  |     return cipher;  | 
481  | 0  |  err:  | 
482  | 0  |     EVP_ASYM_CIPHER_free(cipher);  | 
483  | 0  |     return NULL;  | 
484  | 0  | }  | 
485  |  |  | 
486  |  | void EVP_ASYM_CIPHER_free(EVP_ASYM_CIPHER *cipher)  | 
487  | 0  | { | 
488  | 0  |     int i;  | 
489  |  | 
  | 
490  | 0  |     if (cipher == NULL)  | 
491  | 0  |         return;  | 
492  | 0  |     CRYPTO_DOWN_REF(&cipher->refcnt, &i);  | 
493  | 0  |     if (i > 0)  | 
494  | 0  |         return;  | 
495  | 0  |     OPENSSL_free(cipher->type_name);  | 
496  | 0  |     ossl_provider_free(cipher->prov);  | 
497  | 0  |     CRYPTO_FREE_REF(&cipher->refcnt);  | 
498  | 0  |     OPENSSL_free(cipher);  | 
499  | 0  | }  | 
500  |  |  | 
501  |  | int EVP_ASYM_CIPHER_up_ref(EVP_ASYM_CIPHER *cipher)  | 
502  | 0  | { | 
503  | 0  |     int ref = 0;  | 
504  |  | 
  | 
505  | 0  |     CRYPTO_UP_REF(&cipher->refcnt, &ref);  | 
506  | 0  |     return 1;  | 
507  | 0  | }  | 
508  |  |  | 
509  |  | OSSL_PROVIDER *EVP_ASYM_CIPHER_get0_provider(const EVP_ASYM_CIPHER *cipher)  | 
510  | 0  | { | 
511  | 0  |     return cipher->prov;  | 
512  | 0  | }  | 
513  |  |  | 
514  |  | EVP_ASYM_CIPHER *EVP_ASYM_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,  | 
515  |  |                                        const char *properties)  | 
516  | 0  | { | 
517  | 0  |     return evp_generic_fetch(ctx, OSSL_OP_ASYM_CIPHER, algorithm, properties,  | 
518  | 0  |                              evp_asym_cipher_from_algorithm,  | 
519  | 0  |                              evp_asym_cipher_up_ref,  | 
520  | 0  |                              evp_asym_cipher_free);  | 
521  | 0  | }  | 
522  |  |  | 
523  |  | EVP_ASYM_CIPHER *evp_asym_cipher_fetch_from_prov(OSSL_PROVIDER *prov,  | 
524  |  |                                                  const char *algorithm,  | 
525  |  |                                                  const char *properties)  | 
526  | 0  | { | 
527  | 0  |     return evp_generic_fetch_from_prov(prov, OSSL_OP_ASYM_CIPHER,  | 
528  | 0  |                                        algorithm, properties,  | 
529  | 0  |                                        evp_asym_cipher_from_algorithm,  | 
530  | 0  |                                        evp_asym_cipher_up_ref,  | 
531  | 0  |                                        evp_asym_cipher_free);  | 
532  | 0  | }  | 
533  |  |  | 
534  |  | int EVP_ASYM_CIPHER_is_a(const EVP_ASYM_CIPHER *cipher, const char *name)  | 
535  | 0  | { | 
536  | 0  |     return evp_is_a(cipher->prov, cipher->name_id, NULL, name);  | 
537  | 0  | }  | 
538  |  |  | 
539  |  | int evp_asym_cipher_get_number(const EVP_ASYM_CIPHER *cipher)  | 
540  | 0  | { | 
541  | 0  |     return cipher->name_id;  | 
542  | 0  | }  | 
543  |  |  | 
544  |  | const char *EVP_ASYM_CIPHER_get0_name(const EVP_ASYM_CIPHER *cipher)  | 
545  | 0  | { | 
546  | 0  |     return cipher->type_name;  | 
547  | 0  | }  | 
548  |  |  | 
549  |  | const char *EVP_ASYM_CIPHER_get0_description(const EVP_ASYM_CIPHER *cipher)  | 
550  | 0  | { | 
551  | 0  |     return cipher->description;  | 
552  | 0  | }  | 
553  |  |  | 
554  |  | void EVP_ASYM_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,  | 
555  |  |                                      void (*fn)(EVP_ASYM_CIPHER *cipher,  | 
556  |  |                                                 void *arg),  | 
557  |  |                                      void *arg)  | 
558  | 0  | { | 
559  | 0  |     evp_generic_do_all(libctx, OSSL_OP_ASYM_CIPHER,  | 
560  | 0  |                        (void (*)(void *, void *))fn, arg,  | 
561  | 0  |                        evp_asym_cipher_from_algorithm,  | 
562  | 0  |                        evp_asym_cipher_up_ref,  | 
563  | 0  |                        evp_asym_cipher_free);  | 
564  | 0  | }  | 
565  |  |  | 
566  |  |  | 
567  |  | int EVP_ASYM_CIPHER_names_do_all(const EVP_ASYM_CIPHER *cipher,  | 
568  |  |                                  void (*fn)(const char *name, void *data),  | 
569  |  |                                  void *data)  | 
570  | 0  | { | 
571  | 0  |     if (cipher->prov != NULL)  | 
572  | 0  |         return evp_names_do_all(cipher->prov, cipher->name_id, fn, data);  | 
573  |  |  | 
574  | 0  |     return 1;  | 
575  | 0  | }  | 
576  |  |  | 
577  |  | const OSSL_PARAM *EVP_ASYM_CIPHER_gettable_ctx_params(const EVP_ASYM_CIPHER *cip)  | 
578  | 0  | { | 
579  | 0  |     void *provctx;  | 
580  |  | 
  | 
581  | 0  |     if (cip == NULL || cip->gettable_ctx_params == NULL)  | 
582  | 0  |         return NULL;  | 
583  |  |  | 
584  | 0  |     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));  | 
585  | 0  |     return cip->gettable_ctx_params(NULL, provctx);  | 
586  | 0  | }  | 
587  |  |  | 
588  |  | const OSSL_PARAM *EVP_ASYM_CIPHER_settable_ctx_params(const EVP_ASYM_CIPHER *cip)  | 
589  | 0  | { | 
590  | 0  |     void *provctx;  | 
591  |  | 
  | 
592  | 0  |     if (cip == NULL || cip->settable_ctx_params == NULL)  | 
593  | 0  |         return NULL;  | 
594  |  |  | 
595  | 0  |     provctx = ossl_provider_ctx(EVP_ASYM_CIPHER_get0_provider(cip));  | 
596  |  |     return cip->settable_ctx_params(NULL, provctx);  | 
597  | 0  | }  |