xref: /freebsd-src/crypto/openssl/providers/implementations/asymciphers/rsa_enc.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1b077aed3SPierre Pronchery /*
2b077aed3SPierre Pronchery  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /*
11b077aed3SPierre Pronchery  * RSA low level APIs are deprecated for public use, but still ok for
12b077aed3SPierre Pronchery  * internal use.
13b077aed3SPierre Pronchery  */
14b077aed3SPierre Pronchery #include "internal/deprecated.h"
15b077aed3SPierre Pronchery 
16b077aed3SPierre Pronchery #include <openssl/crypto.h>
17b077aed3SPierre Pronchery #include <openssl/evp.h>
18b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
19b077aed3SPierre Pronchery #include <openssl/core_names.h>
20b077aed3SPierre Pronchery #include <openssl/rsa.h>
21b077aed3SPierre Pronchery #include <openssl/params.h>
22b077aed3SPierre Pronchery #include <openssl/err.h>
23b077aed3SPierre Pronchery #include <openssl/proverr.h>
24b077aed3SPierre Pronchery /* Just for SSL_MAX_MASTER_KEY_LENGTH */
25b077aed3SPierre Pronchery #include <openssl/prov_ssl.h>
26b077aed3SPierre Pronchery #include "internal/constant_time.h"
27b077aed3SPierre Pronchery #include "internal/sizes.h"
28b077aed3SPierre Pronchery #include "crypto/rsa.h"
29b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
30b077aed3SPierre Pronchery #include "prov/implementations.h"
31b077aed3SPierre Pronchery #include "prov/providercommon.h"
32b077aed3SPierre Pronchery #include "prov/securitycheck.h"
33b077aed3SPierre Pronchery 
34b077aed3SPierre Pronchery #include <stdlib.h>
35b077aed3SPierre Pronchery 
36b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_newctx_fn rsa_newctx;
37b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_encrypt_init_fn rsa_encrypt_init;
38b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_encrypt_fn rsa_encrypt;
39b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_decrypt_init_fn rsa_decrypt_init;
40b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_decrypt_fn rsa_decrypt;
41b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_freectx_fn rsa_freectx;
42b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_dupctx_fn rsa_dupctx;
43b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_get_ctx_params_fn rsa_get_ctx_params;
44b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_gettable_ctx_params_fn rsa_gettable_ctx_params;
45b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_set_ctx_params_fn rsa_set_ctx_params;
46b077aed3SPierre Pronchery static OSSL_FUNC_asym_cipher_settable_ctx_params_fn rsa_settable_ctx_params;
47b077aed3SPierre Pronchery 
48b077aed3SPierre Pronchery static OSSL_ITEM padding_item[] = {
49b077aed3SPierre Pronchery     { RSA_PKCS1_PADDING,        OSSL_PKEY_RSA_PAD_MODE_PKCSV15 },
50b077aed3SPierre Pronchery     { RSA_NO_PADDING,           OSSL_PKEY_RSA_PAD_MODE_NONE },
51b077aed3SPierre Pronchery     { RSA_PKCS1_OAEP_PADDING,   OSSL_PKEY_RSA_PAD_MODE_OAEP }, /* Correct spelling first */
52b077aed3SPierre Pronchery     { RSA_PKCS1_OAEP_PADDING,   "oeap"   },
53b077aed3SPierre Pronchery     { RSA_X931_PADDING,         OSSL_PKEY_RSA_PAD_MODE_X931 },
54b077aed3SPierre Pronchery     { 0,                        NULL     }
55b077aed3SPierre Pronchery };
56b077aed3SPierre Pronchery 
57b077aed3SPierre Pronchery /*
58b077aed3SPierre Pronchery  * What's passed as an actual key is defined by the KEYMGMT interface.
59b077aed3SPierre Pronchery  * We happen to know that our KEYMGMT simply passes RSA structures, so
60b077aed3SPierre Pronchery  * we use that here too.
61b077aed3SPierre Pronchery  */
62b077aed3SPierre Pronchery 
63b077aed3SPierre Pronchery typedef struct {
64b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx;
65b077aed3SPierre Pronchery     RSA *rsa;
66b077aed3SPierre Pronchery     int pad_mode;
67b077aed3SPierre Pronchery     int operation;
68b077aed3SPierre Pronchery     /* OAEP message digest */
69b077aed3SPierre Pronchery     EVP_MD *oaep_md;
70b077aed3SPierre Pronchery     /* message digest for MGF1 */
71b077aed3SPierre Pronchery     EVP_MD *mgf1_md;
72b077aed3SPierre Pronchery     /* OAEP label */
73b077aed3SPierre Pronchery     unsigned char *oaep_label;
74b077aed3SPierre Pronchery     size_t oaep_labellen;
75b077aed3SPierre Pronchery     /* TLS padding */
76b077aed3SPierre Pronchery     unsigned int client_version;
77b077aed3SPierre Pronchery     unsigned int alt_version;
78b077aed3SPierre Pronchery } PROV_RSA_CTX;
79b077aed3SPierre Pronchery 
rsa_newctx(void * provctx)80b077aed3SPierre Pronchery static void *rsa_newctx(void *provctx)
81b077aed3SPierre Pronchery {
82b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx;
83b077aed3SPierre Pronchery 
84b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
85b077aed3SPierre Pronchery         return NULL;
86b077aed3SPierre Pronchery     prsactx = OPENSSL_zalloc(sizeof(PROV_RSA_CTX));
87b077aed3SPierre Pronchery     if (prsactx == NULL)
88b077aed3SPierre Pronchery         return NULL;
89b077aed3SPierre Pronchery     prsactx->libctx = PROV_LIBCTX_OF(provctx);
90b077aed3SPierre Pronchery 
91b077aed3SPierre Pronchery     return prsactx;
92b077aed3SPierre Pronchery }
93b077aed3SPierre Pronchery 
rsa_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[],int operation)94b077aed3SPierre Pronchery static int rsa_init(void *vprsactx, void *vrsa, const OSSL_PARAM params[],
95b077aed3SPierre Pronchery                     int operation)
96b077aed3SPierre Pronchery {
97b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
98b077aed3SPierre Pronchery 
99b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || prsactx == NULL || vrsa == NULL)
100b077aed3SPierre Pronchery         return 0;
101b077aed3SPierre Pronchery 
102b077aed3SPierre Pronchery     if (!ossl_rsa_check_key(prsactx->libctx, vrsa, operation))
103b077aed3SPierre Pronchery         return 0;
104b077aed3SPierre Pronchery 
105b077aed3SPierre Pronchery     if (!RSA_up_ref(vrsa))
106b077aed3SPierre Pronchery         return 0;
107b077aed3SPierre Pronchery     RSA_free(prsactx->rsa);
108b077aed3SPierre Pronchery     prsactx->rsa = vrsa;
109b077aed3SPierre Pronchery     prsactx->operation = operation;
110b077aed3SPierre Pronchery 
111b077aed3SPierre Pronchery     switch (RSA_test_flags(prsactx->rsa, RSA_FLAG_TYPE_MASK)) {
112b077aed3SPierre Pronchery     case RSA_FLAG_TYPE_RSA:
113b077aed3SPierre Pronchery         prsactx->pad_mode = RSA_PKCS1_PADDING;
114b077aed3SPierre Pronchery         break;
115b077aed3SPierre Pronchery     default:
116b077aed3SPierre Pronchery         /* This should not happen due to the check above */
117b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
118b077aed3SPierre Pronchery         return 0;
119b077aed3SPierre Pronchery     }
120b077aed3SPierre Pronchery     return rsa_set_ctx_params(prsactx, params);
121b077aed3SPierre Pronchery }
122b077aed3SPierre Pronchery 
rsa_encrypt_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[])123b077aed3SPierre Pronchery static int rsa_encrypt_init(void *vprsactx, void *vrsa,
124b077aed3SPierre Pronchery                             const OSSL_PARAM params[])
125b077aed3SPierre Pronchery {
126b077aed3SPierre Pronchery     return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_ENCRYPT);
127b077aed3SPierre Pronchery }
128b077aed3SPierre Pronchery 
rsa_decrypt_init(void * vprsactx,void * vrsa,const OSSL_PARAM params[])129b077aed3SPierre Pronchery static int rsa_decrypt_init(void *vprsactx, void *vrsa,
130b077aed3SPierre Pronchery                             const OSSL_PARAM params[])
131b077aed3SPierre Pronchery {
132b077aed3SPierre Pronchery     return rsa_init(vprsactx, vrsa, params, EVP_PKEY_OP_DECRYPT);
133b077aed3SPierre Pronchery }
134b077aed3SPierre Pronchery 
rsa_encrypt(void * vprsactx,unsigned char * out,size_t * outlen,size_t outsize,const unsigned char * in,size_t inlen)135b077aed3SPierre Pronchery static int rsa_encrypt(void *vprsactx, unsigned char *out, size_t *outlen,
136b077aed3SPierre Pronchery                        size_t outsize, const unsigned char *in, size_t inlen)
137b077aed3SPierre Pronchery {
138b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
139b077aed3SPierre Pronchery     int ret;
140b077aed3SPierre Pronchery 
141b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
142b077aed3SPierre Pronchery         return 0;
143b077aed3SPierre Pronchery 
144b077aed3SPierre Pronchery     if (out == NULL) {
145b077aed3SPierre Pronchery         size_t len = RSA_size(prsactx->rsa);
146b077aed3SPierre Pronchery 
147b077aed3SPierre Pronchery         if (len == 0) {
148b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
149b077aed3SPierre Pronchery             return 0;
150b077aed3SPierre Pronchery         }
151b077aed3SPierre Pronchery         *outlen = len;
152b077aed3SPierre Pronchery         return 1;
153b077aed3SPierre Pronchery     }
154b077aed3SPierre Pronchery 
155b077aed3SPierre Pronchery     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
156b077aed3SPierre Pronchery         int rsasize = RSA_size(prsactx->rsa);
157b077aed3SPierre Pronchery         unsigned char *tbuf;
158b077aed3SPierre Pronchery 
159b077aed3SPierre Pronchery         if ((tbuf = OPENSSL_malloc(rsasize)) == NULL) {
160b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
161b077aed3SPierre Pronchery             return 0;
162b077aed3SPierre Pronchery         }
163b077aed3SPierre Pronchery         if (prsactx->oaep_md == NULL) {
164b077aed3SPierre Pronchery             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
165b077aed3SPierre Pronchery             if (prsactx->oaep_md == NULL) {
166b077aed3SPierre Pronchery                 OPENSSL_free(tbuf);
167b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
168b077aed3SPierre Pronchery                 return 0;
169b077aed3SPierre Pronchery             }
170b077aed3SPierre Pronchery         }
171b077aed3SPierre Pronchery         ret =
172b077aed3SPierre Pronchery             ossl_rsa_padding_add_PKCS1_OAEP_mgf1_ex(prsactx->libctx, tbuf,
173b077aed3SPierre Pronchery                                                     rsasize, in, inlen,
174b077aed3SPierre Pronchery                                                     prsactx->oaep_label,
175b077aed3SPierre Pronchery                                                     prsactx->oaep_labellen,
176b077aed3SPierre Pronchery                                                     prsactx->oaep_md,
177b077aed3SPierre Pronchery                                                     prsactx->mgf1_md);
178b077aed3SPierre Pronchery 
179b077aed3SPierre Pronchery         if (!ret) {
180b077aed3SPierre Pronchery             OPENSSL_free(tbuf);
181b077aed3SPierre Pronchery             return 0;
182b077aed3SPierre Pronchery         }
183b077aed3SPierre Pronchery         ret = RSA_public_encrypt(rsasize, tbuf, out, prsactx->rsa,
184b077aed3SPierre Pronchery                                  RSA_NO_PADDING);
185b077aed3SPierre Pronchery         OPENSSL_free(tbuf);
186b077aed3SPierre Pronchery     } else {
187b077aed3SPierre Pronchery         ret = RSA_public_encrypt(inlen, in, out, prsactx->rsa,
188b077aed3SPierre Pronchery                                  prsactx->pad_mode);
189b077aed3SPierre Pronchery     }
190b077aed3SPierre Pronchery     /* A ret value of 0 is not an error */
191b077aed3SPierre Pronchery     if (ret < 0)
192b077aed3SPierre Pronchery         return ret;
193b077aed3SPierre Pronchery     *outlen = ret;
194b077aed3SPierre Pronchery     return 1;
195b077aed3SPierre Pronchery }
196b077aed3SPierre Pronchery 
rsa_decrypt(void * vprsactx,unsigned char * out,size_t * outlen,size_t outsize,const unsigned char * in,size_t inlen)197b077aed3SPierre Pronchery static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
198b077aed3SPierre Pronchery                        size_t outsize, const unsigned char *in, size_t inlen)
199b077aed3SPierre Pronchery {
200b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
201b077aed3SPierre Pronchery     int ret;
202b077aed3SPierre Pronchery     size_t len = RSA_size(prsactx->rsa);
203b077aed3SPierre Pronchery 
204b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
205b077aed3SPierre Pronchery         return 0;
206b077aed3SPierre Pronchery 
207b077aed3SPierre Pronchery     if (prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
208b077aed3SPierre Pronchery         if (out == NULL) {
209b077aed3SPierre Pronchery             *outlen = SSL_MAX_MASTER_KEY_LENGTH;
210b077aed3SPierre Pronchery             return 1;
211b077aed3SPierre Pronchery         }
212b077aed3SPierre Pronchery         if (outsize < SSL_MAX_MASTER_KEY_LENGTH) {
213b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
214b077aed3SPierre Pronchery             return 0;
215b077aed3SPierre Pronchery         }
216b077aed3SPierre Pronchery     } else {
217b077aed3SPierre Pronchery         if (out == NULL) {
218b077aed3SPierre Pronchery             if (len == 0) {
219b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
220b077aed3SPierre Pronchery                 return 0;
221b077aed3SPierre Pronchery             }
222b077aed3SPierre Pronchery             *outlen = len;
223b077aed3SPierre Pronchery             return 1;
224b077aed3SPierre Pronchery         }
225b077aed3SPierre Pronchery 
226b077aed3SPierre Pronchery         if (outsize < len) {
227b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH);
228b077aed3SPierre Pronchery             return 0;
229b077aed3SPierre Pronchery         }
230b077aed3SPierre Pronchery     }
231b077aed3SPierre Pronchery 
232b077aed3SPierre Pronchery     if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING
233b077aed3SPierre Pronchery             || prsactx->pad_mode == RSA_PKCS1_WITH_TLS_PADDING) {
234b077aed3SPierre Pronchery         unsigned char *tbuf;
235b077aed3SPierre Pronchery 
236b077aed3SPierre Pronchery         if ((tbuf = OPENSSL_malloc(len)) == NULL) {
237b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
238b077aed3SPierre Pronchery             return 0;
239b077aed3SPierre Pronchery         }
240b077aed3SPierre Pronchery         ret = RSA_private_decrypt(inlen, in, tbuf, prsactx->rsa,
241b077aed3SPierre Pronchery                                   RSA_NO_PADDING);
242b077aed3SPierre Pronchery         /*
243b077aed3SPierre Pronchery          * With no padding then, on success ret should be len, otherwise an
244b077aed3SPierre Pronchery          * error occurred (non-constant time)
245b077aed3SPierre Pronchery          */
246b077aed3SPierre Pronchery         if (ret != (int)len) {
247b077aed3SPierre Pronchery             OPENSSL_free(tbuf);
248b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_DECRYPT);
249b077aed3SPierre Pronchery             return 0;
250b077aed3SPierre Pronchery         }
251b077aed3SPierre Pronchery         if (prsactx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
252b077aed3SPierre Pronchery             if (prsactx->oaep_md == NULL) {
253b077aed3SPierre Pronchery                 prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA-1", NULL);
254b077aed3SPierre Pronchery                 if (prsactx->oaep_md == NULL) {
255b077aed3SPierre Pronchery                     OPENSSL_free(tbuf);
256b077aed3SPierre Pronchery                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
257b077aed3SPierre Pronchery                     return 0;
258b077aed3SPierre Pronchery                 }
259b077aed3SPierre Pronchery             }
260b077aed3SPierre Pronchery             ret = RSA_padding_check_PKCS1_OAEP_mgf1(out, outsize, tbuf,
261b077aed3SPierre Pronchery                                                     len, len,
262b077aed3SPierre Pronchery                                                     prsactx->oaep_label,
263b077aed3SPierre Pronchery                                                     prsactx->oaep_labellen,
264b077aed3SPierre Pronchery                                                     prsactx->oaep_md,
265b077aed3SPierre Pronchery                                                     prsactx->mgf1_md);
266b077aed3SPierre Pronchery         } else {
267b077aed3SPierre Pronchery             /* RSA_PKCS1_WITH_TLS_PADDING */
268b077aed3SPierre Pronchery             if (prsactx->client_version <= 0) {
269b077aed3SPierre Pronchery                 ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
270b077aed3SPierre Pronchery                 OPENSSL_free(tbuf);
271b077aed3SPierre Pronchery                 return 0;
272b077aed3SPierre Pronchery             }
273b077aed3SPierre Pronchery             ret = ossl_rsa_padding_check_PKCS1_type_2_TLS(
274b077aed3SPierre Pronchery                         prsactx->libctx, out, outsize, tbuf, len,
275b077aed3SPierre Pronchery                         prsactx->client_version, prsactx->alt_version);
276b077aed3SPierre Pronchery         }
277b077aed3SPierre Pronchery         OPENSSL_free(tbuf);
278b077aed3SPierre Pronchery     } else {
279b077aed3SPierre Pronchery         ret = RSA_private_decrypt(inlen, in, out, prsactx->rsa,
280b077aed3SPierre Pronchery                                   prsactx->pad_mode);
281b077aed3SPierre Pronchery     }
282b077aed3SPierre Pronchery     *outlen = constant_time_select_s(constant_time_msb_s(ret), *outlen, ret);
283b077aed3SPierre Pronchery     ret = constant_time_select_int(constant_time_msb(ret), 0, 1);
284b077aed3SPierre Pronchery     return ret;
285b077aed3SPierre Pronchery }
286b077aed3SPierre Pronchery 
rsa_freectx(void * vprsactx)287b077aed3SPierre Pronchery static void rsa_freectx(void *vprsactx)
288b077aed3SPierre Pronchery {
289b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
290b077aed3SPierre Pronchery 
291b077aed3SPierre Pronchery     RSA_free(prsactx->rsa);
292b077aed3SPierre Pronchery 
293b077aed3SPierre Pronchery     EVP_MD_free(prsactx->oaep_md);
294b077aed3SPierre Pronchery     EVP_MD_free(prsactx->mgf1_md);
295b077aed3SPierre Pronchery     OPENSSL_free(prsactx->oaep_label);
296b077aed3SPierre Pronchery 
297b077aed3SPierre Pronchery     OPENSSL_free(prsactx);
298b077aed3SPierre Pronchery }
299b077aed3SPierre Pronchery 
rsa_dupctx(void * vprsactx)300b077aed3SPierre Pronchery static void *rsa_dupctx(void *vprsactx)
301b077aed3SPierre Pronchery {
302b077aed3SPierre Pronchery     PROV_RSA_CTX *srcctx = (PROV_RSA_CTX *)vprsactx;
303b077aed3SPierre Pronchery     PROV_RSA_CTX *dstctx;
304b077aed3SPierre Pronchery 
305b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
306b077aed3SPierre Pronchery         return NULL;
307b077aed3SPierre Pronchery 
308b077aed3SPierre Pronchery     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
309b077aed3SPierre Pronchery     if (dstctx == NULL)
310b077aed3SPierre Pronchery         return NULL;
311b077aed3SPierre Pronchery 
312b077aed3SPierre Pronchery     *dstctx = *srcctx;
313b077aed3SPierre Pronchery     if (dstctx->rsa != NULL && !RSA_up_ref(dstctx->rsa)) {
314b077aed3SPierre Pronchery         OPENSSL_free(dstctx);
315b077aed3SPierre Pronchery         return NULL;
316b077aed3SPierre Pronchery     }
317b077aed3SPierre Pronchery 
318b077aed3SPierre Pronchery     if (dstctx->oaep_md != NULL && !EVP_MD_up_ref(dstctx->oaep_md)) {
319b077aed3SPierre Pronchery         RSA_free(dstctx->rsa);
320b077aed3SPierre Pronchery         OPENSSL_free(dstctx);
321b077aed3SPierre Pronchery         return NULL;
322b077aed3SPierre Pronchery     }
323b077aed3SPierre Pronchery 
324b077aed3SPierre Pronchery     if (dstctx->mgf1_md != NULL && !EVP_MD_up_ref(dstctx->mgf1_md)) {
325b077aed3SPierre Pronchery         RSA_free(dstctx->rsa);
326b077aed3SPierre Pronchery         EVP_MD_free(dstctx->oaep_md);
327b077aed3SPierre Pronchery         OPENSSL_free(dstctx);
328b077aed3SPierre Pronchery         return NULL;
329b077aed3SPierre Pronchery     }
330b077aed3SPierre Pronchery 
331b077aed3SPierre Pronchery     return dstctx;
332b077aed3SPierre Pronchery }
333b077aed3SPierre Pronchery 
rsa_get_ctx_params(void * vprsactx,OSSL_PARAM * params)334b077aed3SPierre Pronchery static int rsa_get_ctx_params(void *vprsactx, OSSL_PARAM *params)
335b077aed3SPierre Pronchery {
336b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
337b077aed3SPierre Pronchery     OSSL_PARAM *p;
338b077aed3SPierre Pronchery 
339b077aed3SPierre Pronchery     if (prsactx == NULL)
340b077aed3SPierre Pronchery         return 0;
341b077aed3SPierre Pronchery 
342b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
343b077aed3SPierre Pronchery     if (p != NULL)
344b077aed3SPierre Pronchery         switch (p->data_type) {
345b077aed3SPierre Pronchery         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
346b077aed3SPierre Pronchery             if (!OSSL_PARAM_set_int(p, prsactx->pad_mode))
347b077aed3SPierre Pronchery                 return 0;
348b077aed3SPierre Pronchery             break;
349b077aed3SPierre Pronchery         case OSSL_PARAM_UTF8_STRING:
350b077aed3SPierre Pronchery             {
351b077aed3SPierre Pronchery                 int i;
352b077aed3SPierre Pronchery                 const char *word = NULL;
353b077aed3SPierre Pronchery 
354b077aed3SPierre Pronchery                 for (i = 0; padding_item[i].id != 0; i++) {
355b077aed3SPierre Pronchery                     if (prsactx->pad_mode == (int)padding_item[i].id) {
356b077aed3SPierre Pronchery                         word = padding_item[i].ptr;
357b077aed3SPierre Pronchery                         break;
358b077aed3SPierre Pronchery                     }
359b077aed3SPierre Pronchery                 }
360b077aed3SPierre Pronchery 
361b077aed3SPierre Pronchery                 if (word != NULL) {
362b077aed3SPierre Pronchery                     if (!OSSL_PARAM_set_utf8_string(p, word))
363b077aed3SPierre Pronchery                         return 0;
364b077aed3SPierre Pronchery                 } else {
365b077aed3SPierre Pronchery                     ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
366b077aed3SPierre Pronchery                 }
367b077aed3SPierre Pronchery             }
368b077aed3SPierre Pronchery             break;
369b077aed3SPierre Pronchery         default:
370b077aed3SPierre Pronchery             return 0;
371b077aed3SPierre Pronchery         }
372b077aed3SPierre Pronchery 
373b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
374b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, prsactx->oaep_md == NULL
375b077aed3SPierre Pronchery                                                     ? ""
376b077aed3SPierre Pronchery                                                     : EVP_MD_get0_name(prsactx->oaep_md)))
377b077aed3SPierre Pronchery         return 0;
378b077aed3SPierre Pronchery 
379b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
380b077aed3SPierre Pronchery     if (p != NULL) {
381b077aed3SPierre Pronchery         EVP_MD *mgf1_md = prsactx->mgf1_md == NULL ? prsactx->oaep_md
382b077aed3SPierre Pronchery                                                    : prsactx->mgf1_md;
383b077aed3SPierre Pronchery 
384b077aed3SPierre Pronchery         if (!OSSL_PARAM_set_utf8_string(p, mgf1_md == NULL
385b077aed3SPierre Pronchery                                            ? ""
386b077aed3SPierre Pronchery                                            : EVP_MD_get0_name(mgf1_md)))
387b077aed3SPierre Pronchery         return 0;
388b077aed3SPierre Pronchery     }
389b077aed3SPierre Pronchery 
390b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
391b077aed3SPierre Pronchery     if (p != NULL &&
392b077aed3SPierre Pronchery         !OSSL_PARAM_set_octet_ptr(p, prsactx->oaep_label,
393b077aed3SPierre Pronchery                                   prsactx->oaep_labellen))
394b077aed3SPierre Pronchery         return 0;
395b077aed3SPierre Pronchery 
396b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
397b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->client_version))
398b077aed3SPierre Pronchery         return 0;
399b077aed3SPierre Pronchery 
400b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
401b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_uint(p, prsactx->alt_version))
402b077aed3SPierre Pronchery         return 0;
403b077aed3SPierre Pronchery 
404b077aed3SPierre Pronchery     return 1;
405b077aed3SPierre Pronchery }
406b077aed3SPierre Pronchery 
407b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
408b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
409b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
410b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
411b077aed3SPierre Pronchery     OSSL_PARAM_DEFN(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR,
412b077aed3SPierre Pronchery                     NULL, 0),
413b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
414b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
415b077aed3SPierre Pronchery     OSSL_PARAM_END
416b077aed3SPierre Pronchery };
417b077aed3SPierre Pronchery 
rsa_gettable_ctx_params(ossl_unused void * vprsactx,ossl_unused void * provctx)418b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_gettable_ctx_params(ossl_unused void *vprsactx,
419b077aed3SPierre Pronchery                                                  ossl_unused void *provctx)
420b077aed3SPierre Pronchery {
421b077aed3SPierre Pronchery     return known_gettable_ctx_params;
422b077aed3SPierre Pronchery }
423b077aed3SPierre Pronchery 
rsa_set_ctx_params(void * vprsactx,const OSSL_PARAM params[])424b077aed3SPierre Pronchery static int rsa_set_ctx_params(void *vprsactx, const OSSL_PARAM params[])
425b077aed3SPierre Pronchery {
426b077aed3SPierre Pronchery     PROV_RSA_CTX *prsactx = (PROV_RSA_CTX *)vprsactx;
427b077aed3SPierre Pronchery     const OSSL_PARAM *p;
428b077aed3SPierre Pronchery     char mdname[OSSL_MAX_NAME_SIZE];
429b077aed3SPierre Pronchery     char mdprops[OSSL_MAX_PROPQUERY_SIZE] = { '\0' };
430b077aed3SPierre Pronchery     char *str = NULL;
431b077aed3SPierre Pronchery 
432b077aed3SPierre Pronchery     if (prsactx == NULL)
433b077aed3SPierre Pronchery         return 0;
434b077aed3SPierre Pronchery     if (params == NULL)
435b077aed3SPierre Pronchery         return 1;
436b077aed3SPierre Pronchery 
437b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST);
438b077aed3SPierre Pronchery     if (p != NULL) {
439b077aed3SPierre Pronchery         str = mdname;
440b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
441b077aed3SPierre Pronchery             return 0;
442b077aed3SPierre Pronchery 
443b077aed3SPierre Pronchery         p = OSSL_PARAM_locate_const(params,
444b077aed3SPierre Pronchery                                     OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS);
445b077aed3SPierre Pronchery         if (p != NULL) {
446b077aed3SPierre Pronchery             str = mdprops;
447b077aed3SPierre Pronchery             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
448b077aed3SPierre Pronchery                 return 0;
449b077aed3SPierre Pronchery         }
450b077aed3SPierre Pronchery 
451b077aed3SPierre Pronchery         EVP_MD_free(prsactx->oaep_md);
452b077aed3SPierre Pronchery         prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, mdname, mdprops);
453b077aed3SPierre Pronchery 
454b077aed3SPierre Pronchery         if (prsactx->oaep_md == NULL)
455b077aed3SPierre Pronchery             return 0;
456b077aed3SPierre Pronchery     }
457b077aed3SPierre Pronchery 
458b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_PAD_MODE);
459b077aed3SPierre Pronchery     if (p != NULL) {
460b077aed3SPierre Pronchery         int pad_mode = 0;
461b077aed3SPierre Pronchery 
462b077aed3SPierre Pronchery         switch (p->data_type) {
463b077aed3SPierre Pronchery         case OSSL_PARAM_INTEGER: /* Support for legacy pad mode number */
464b077aed3SPierre Pronchery             if (!OSSL_PARAM_get_int(p, &pad_mode))
465b077aed3SPierre Pronchery                 return 0;
466b077aed3SPierre Pronchery             break;
467b077aed3SPierre Pronchery         case OSSL_PARAM_UTF8_STRING:
468b077aed3SPierre Pronchery             {
469b077aed3SPierre Pronchery                 int i;
470b077aed3SPierre Pronchery 
471b077aed3SPierre Pronchery                 if (p->data == NULL)
472b077aed3SPierre Pronchery                     return 0;
473b077aed3SPierre Pronchery 
474b077aed3SPierre Pronchery                 for (i = 0; padding_item[i].id != 0; i++) {
475b077aed3SPierre Pronchery                     if (strcmp(p->data, padding_item[i].ptr) == 0) {
476b077aed3SPierre Pronchery                         pad_mode = padding_item[i].id;
477b077aed3SPierre Pronchery                         break;
478b077aed3SPierre Pronchery                     }
479b077aed3SPierre Pronchery                 }
480b077aed3SPierre Pronchery             }
481b077aed3SPierre Pronchery             break;
482b077aed3SPierre Pronchery         default:
483b077aed3SPierre Pronchery             return 0;
484b077aed3SPierre Pronchery         }
485b077aed3SPierre Pronchery 
486b077aed3SPierre Pronchery         /*
487b077aed3SPierre Pronchery          * PSS padding is for signatures only so is not compatible with
488b077aed3SPierre Pronchery          * asymmetric cipher use.
489b077aed3SPierre Pronchery          */
490b077aed3SPierre Pronchery         if (pad_mode == RSA_PKCS1_PSS_PADDING)
491b077aed3SPierre Pronchery             return 0;
492b077aed3SPierre Pronchery         if (pad_mode == RSA_PKCS1_OAEP_PADDING && prsactx->oaep_md == NULL) {
493b077aed3SPierre Pronchery             prsactx->oaep_md = EVP_MD_fetch(prsactx->libctx, "SHA1", mdprops);
494b077aed3SPierre Pronchery             if (prsactx->oaep_md == NULL)
495b077aed3SPierre Pronchery                 return 0;
496b077aed3SPierre Pronchery         }
497b077aed3SPierre Pronchery         prsactx->pad_mode = pad_mode;
498b077aed3SPierre Pronchery     }
499b077aed3SPierre Pronchery 
500b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST);
501b077aed3SPierre Pronchery     if (p != NULL) {
502b077aed3SPierre Pronchery         str = mdname;
503b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdname)))
504b077aed3SPierre Pronchery             return 0;
505b077aed3SPierre Pronchery 
506b077aed3SPierre Pronchery         p = OSSL_PARAM_locate_const(params,
507b077aed3SPierre Pronchery                                     OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS);
508b077aed3SPierre Pronchery         if (p != NULL) {
509b077aed3SPierre Pronchery             str = mdprops;
510b077aed3SPierre Pronchery             if (!OSSL_PARAM_get_utf8_string(p, &str, sizeof(mdprops)))
511b077aed3SPierre Pronchery                 return 0;
512b077aed3SPierre Pronchery         } else {
513b077aed3SPierre Pronchery             str = NULL;
514b077aed3SPierre Pronchery         }
515b077aed3SPierre Pronchery 
516b077aed3SPierre Pronchery         EVP_MD_free(prsactx->mgf1_md);
517b077aed3SPierre Pronchery         prsactx->mgf1_md = EVP_MD_fetch(prsactx->libctx, mdname, str);
518b077aed3SPierre Pronchery 
519b077aed3SPierre Pronchery         if (prsactx->mgf1_md == NULL)
520b077aed3SPierre Pronchery             return 0;
521b077aed3SPierre Pronchery     }
522b077aed3SPierre Pronchery 
523b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL);
524b077aed3SPierre Pronchery     if (p != NULL) {
525b077aed3SPierre Pronchery         void *tmp_label = NULL;
526b077aed3SPierre Pronchery         size_t tmp_labellen;
527b077aed3SPierre Pronchery 
528b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_octet_string(p, &tmp_label, 0, &tmp_labellen))
529b077aed3SPierre Pronchery             return 0;
530b077aed3SPierre Pronchery         OPENSSL_free(prsactx->oaep_label);
531b077aed3SPierre Pronchery         prsactx->oaep_label = (unsigned char *)tmp_label;
532b077aed3SPierre Pronchery         prsactx->oaep_labellen = tmp_labellen;
533b077aed3SPierre Pronchery     }
534b077aed3SPierre Pronchery 
535b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION);
536b077aed3SPierre Pronchery     if (p != NULL) {
537b077aed3SPierre Pronchery         unsigned int client_version;
538b077aed3SPierre Pronchery 
539b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &client_version))
540b077aed3SPierre Pronchery             return 0;
541b077aed3SPierre Pronchery         prsactx->client_version = client_version;
542b077aed3SPierre Pronchery     }
543b077aed3SPierre Pronchery 
544b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION);
545b077aed3SPierre Pronchery     if (p != NULL) {
546b077aed3SPierre Pronchery         unsigned int alt_version;
547b077aed3SPierre Pronchery 
548b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_uint(p, &alt_version))
549b077aed3SPierre Pronchery             return 0;
550b077aed3SPierre Pronchery         prsactx->alt_version = alt_version;
551b077aed3SPierre Pronchery     }
552b077aed3SPierre Pronchery 
553b077aed3SPierre Pronchery     return 1;
554b077aed3SPierre Pronchery }
555b077aed3SPierre Pronchery 
556b077aed3SPierre Pronchery static const OSSL_PARAM known_settable_ctx_params[] = {
557b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, NULL, 0),
558*e0c4386eSCy Schubert     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, NULL, 0),
559b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, NULL, 0),
560b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST, NULL, 0),
561b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS, NULL, 0),
562b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, NULL, 0),
563b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, NULL),
564b077aed3SPierre Pronchery     OSSL_PARAM_uint(OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, NULL),
565b077aed3SPierre Pronchery     OSSL_PARAM_END
566b077aed3SPierre Pronchery };
567b077aed3SPierre Pronchery 
rsa_settable_ctx_params(ossl_unused void * vprsactx,ossl_unused void * provctx)568b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_settable_ctx_params(ossl_unused void *vprsactx,
569b077aed3SPierre Pronchery                                                  ossl_unused void *provctx)
570b077aed3SPierre Pronchery {
571b077aed3SPierre Pronchery     return known_settable_ctx_params;
572b077aed3SPierre Pronchery }
573b077aed3SPierre Pronchery 
574b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_rsa_asym_cipher_functions[] = {
575b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))rsa_newctx },
576b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))rsa_encrypt_init },
577b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))rsa_encrypt },
578b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_DECRYPT_INIT, (void (*)(void))rsa_decrypt_init },
579b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_DECRYPT, (void (*)(void))rsa_decrypt },
580b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_FREECTX, (void (*)(void))rsa_freectx },
581b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_DUPCTX, (void (*)(void))rsa_dupctx },
582b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_GET_CTX_PARAMS,
583b077aed3SPierre Pronchery       (void (*)(void))rsa_get_ctx_params },
584b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_GETTABLE_CTX_PARAMS,
585b077aed3SPierre Pronchery       (void (*)(void))rsa_gettable_ctx_params },
586b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_SET_CTX_PARAMS,
587b077aed3SPierre Pronchery       (void (*)(void))rsa_set_ctx_params },
588b077aed3SPierre Pronchery     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
589b077aed3SPierre Pronchery       (void (*)(void))rsa_settable_ctx_params },
590b077aed3SPierre Pronchery     { 0, NULL }
591b077aed3SPierre Pronchery };
592