1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * RSA low level APIs are deprecated for public use, but still ok for
12*b077aed3SPierre Pronchery  * internal use.
13*b077aed3SPierre Pronchery  */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery 
16*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
17*b077aed3SPierre Pronchery #include <openssl/core_names.h>
18*b077aed3SPierre Pronchery #include <openssl/bn.h>
19*b077aed3SPierre Pronchery #include <openssl/err.h>
20*b077aed3SPierre Pronchery #include <openssl/rsa.h>
21*b077aed3SPierre Pronchery #include <openssl/evp.h>
22*b077aed3SPierre Pronchery #include <openssl/proverr.h>
23*b077aed3SPierre Pronchery #include "prov/implementations.h"
24*b077aed3SPierre Pronchery #include "prov/providercommon.h"
25*b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
26*b077aed3SPierre Pronchery #include "crypto/rsa.h"
27*b077aed3SPierre Pronchery #include "crypto/cryptlib.h"
28*b077aed3SPierre Pronchery #include "internal/param_build_set.h"
29*b077aed3SPierre Pronchery 
30*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_new_fn rsa_newdata;
31*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_new_fn rsapss_newdata;
32*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_init_fn rsa_gen_init;
33*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_init_fn rsapss_gen_init;
34*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_set_params_fn rsa_gen_set_params;
35*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
36*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_settable_params_fn rsapss_gen_settable_params;
37*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_fn rsa_gen;
38*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
39*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_load_fn rsa_load;
40*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_load_fn rsapss_load;
41*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_free_fn rsa_freedata;
42*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_get_params_fn rsa_get_params;
43*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_gettable_params_fn rsa_gettable_params;
44*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_has_fn rsa_has;
45*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_match_fn rsa_match;
46*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_validate_fn rsa_validate;
47*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_import_fn rsa_import;
48*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_import_types_fn rsa_import_types;
49*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_export_fn rsa_export;
50*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_export_types_fn rsa_export_types;
51*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_query_operation_name_fn rsa_query_operation_name;
52*b077aed3SPierre Pronchery static OSSL_FUNC_keymgmt_dup_fn rsa_dup;
53*b077aed3SPierre Pronchery 
54*b077aed3SPierre Pronchery #define RSA_DEFAULT_MD "SHA256"
55*b077aed3SPierre Pronchery #define RSA_PSS_DEFAULT_MD OSSL_DIGEST_NAME_SHA1
56*b077aed3SPierre Pronchery #define RSA_POSSIBLE_SELECTIONS                                        \
57*b077aed3SPierre Pronchery     (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
58*b077aed3SPierre Pronchery 
59*b077aed3SPierre Pronchery DEFINE_STACK_OF(BIGNUM)
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const,BIGNUM)60*b077aed3SPierre Pronchery DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
61*b077aed3SPierre Pronchery 
62*b077aed3SPierre Pronchery static int pss_params_fromdata(RSA_PSS_PARAMS_30 *pss_params, int *defaults_set,
63*b077aed3SPierre Pronchery                                const OSSL_PARAM params[], int rsa_type,
64*b077aed3SPierre Pronchery                                OSSL_LIB_CTX *libctx)
65*b077aed3SPierre Pronchery {
66*b077aed3SPierre Pronchery     if (!ossl_rsa_pss_params_30_fromdata(pss_params, defaults_set,
67*b077aed3SPierre Pronchery                                          params, libctx))
68*b077aed3SPierre Pronchery         return 0;
69*b077aed3SPierre Pronchery 
70*b077aed3SPierre Pronchery     /* If not a PSS type RSA, sending us PSS parameters is wrong */
71*b077aed3SPierre Pronchery     if (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
72*b077aed3SPierre Pronchery         && !ossl_rsa_pss_params_30_is_unrestricted(pss_params))
73*b077aed3SPierre Pronchery         return 0;
74*b077aed3SPierre Pronchery 
75*b077aed3SPierre Pronchery     return 1;
76*b077aed3SPierre Pronchery }
77*b077aed3SPierre Pronchery 
rsa_newdata(void * provctx)78*b077aed3SPierre Pronchery static void *rsa_newdata(void *provctx)
79*b077aed3SPierre Pronchery {
80*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
81*b077aed3SPierre Pronchery     RSA *rsa;
82*b077aed3SPierre Pronchery 
83*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
84*b077aed3SPierre Pronchery         return NULL;
85*b077aed3SPierre Pronchery 
86*b077aed3SPierre Pronchery     rsa = ossl_rsa_new_with_ctx(libctx);
87*b077aed3SPierre Pronchery     if (rsa != NULL) {
88*b077aed3SPierre Pronchery         RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
89*b077aed3SPierre Pronchery         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
90*b077aed3SPierre Pronchery     }
91*b077aed3SPierre Pronchery     return rsa;
92*b077aed3SPierre Pronchery }
93*b077aed3SPierre Pronchery 
rsapss_newdata(void * provctx)94*b077aed3SPierre Pronchery static void *rsapss_newdata(void *provctx)
95*b077aed3SPierre Pronchery {
96*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
97*b077aed3SPierre Pronchery     RSA *rsa;
98*b077aed3SPierre Pronchery 
99*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
100*b077aed3SPierre Pronchery         return NULL;
101*b077aed3SPierre Pronchery 
102*b077aed3SPierre Pronchery     rsa = ossl_rsa_new_with_ctx(libctx);
103*b077aed3SPierre Pronchery     if (rsa != NULL) {
104*b077aed3SPierre Pronchery         RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
105*b077aed3SPierre Pronchery         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
106*b077aed3SPierre Pronchery     }
107*b077aed3SPierre Pronchery     return rsa;
108*b077aed3SPierre Pronchery }
109*b077aed3SPierre Pronchery 
rsa_freedata(void * keydata)110*b077aed3SPierre Pronchery static void rsa_freedata(void *keydata)
111*b077aed3SPierre Pronchery {
112*b077aed3SPierre Pronchery     RSA_free(keydata);
113*b077aed3SPierre Pronchery }
114*b077aed3SPierre Pronchery 
rsa_has(const void * keydata,int selection)115*b077aed3SPierre Pronchery static int rsa_has(const void *keydata, int selection)
116*b077aed3SPierre Pronchery {
117*b077aed3SPierre Pronchery     const RSA *rsa = keydata;
118*b077aed3SPierre Pronchery     int ok = 1;
119*b077aed3SPierre Pronchery 
120*b077aed3SPierre Pronchery     if (rsa == NULL || !ossl_prov_is_running())
121*b077aed3SPierre Pronchery         return 0;
122*b077aed3SPierre Pronchery     if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
123*b077aed3SPierre Pronchery         return 1; /* the selection is not missing */
124*b077aed3SPierre Pronchery 
125*b077aed3SPierre Pronchery     /* OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS are always available even if empty */
126*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
127*b077aed3SPierre Pronchery         ok = ok && (RSA_get0_n(rsa) != NULL);
128*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
129*b077aed3SPierre Pronchery         ok = ok && (RSA_get0_e(rsa) != NULL);
130*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
131*b077aed3SPierre Pronchery         ok = ok && (RSA_get0_d(rsa) != NULL);
132*b077aed3SPierre Pronchery     return ok;
133*b077aed3SPierre Pronchery }
134*b077aed3SPierre Pronchery 
rsa_match(const void * keydata1,const void * keydata2,int selection)135*b077aed3SPierre Pronchery static int rsa_match(const void *keydata1, const void *keydata2, int selection)
136*b077aed3SPierre Pronchery {
137*b077aed3SPierre Pronchery     const RSA *rsa1 = keydata1;
138*b077aed3SPierre Pronchery     const RSA *rsa2 = keydata2;
139*b077aed3SPierre Pronchery     int ok = 1;
140*b077aed3SPierre Pronchery 
141*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
142*b077aed3SPierre Pronchery         return 0;
143*b077aed3SPierre Pronchery 
144*b077aed3SPierre Pronchery     /* There is always an |e| */
145*b077aed3SPierre Pronchery     ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
146*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
147*b077aed3SPierre Pronchery         int key_checked = 0;
148*b077aed3SPierre Pronchery 
149*b077aed3SPierre Pronchery         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
150*b077aed3SPierre Pronchery             const BIGNUM *pa = RSA_get0_n(rsa1);
151*b077aed3SPierre Pronchery             const BIGNUM *pb = RSA_get0_n(rsa2);
152*b077aed3SPierre Pronchery 
153*b077aed3SPierre Pronchery             if (pa != NULL && pb != NULL) {
154*b077aed3SPierre Pronchery                 ok = ok && BN_cmp(pa, pb) == 0;
155*b077aed3SPierre Pronchery                 key_checked = 1;
156*b077aed3SPierre Pronchery             }
157*b077aed3SPierre Pronchery         }
158*b077aed3SPierre Pronchery         if (!key_checked
159*b077aed3SPierre Pronchery             && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
160*b077aed3SPierre Pronchery             const BIGNUM *pa = RSA_get0_d(rsa1);
161*b077aed3SPierre Pronchery             const BIGNUM *pb = RSA_get0_d(rsa2);
162*b077aed3SPierre Pronchery 
163*b077aed3SPierre Pronchery             if (pa != NULL && pb != NULL) {
164*b077aed3SPierre Pronchery                 ok = ok && BN_cmp(pa, pb) == 0;
165*b077aed3SPierre Pronchery                 key_checked = 1;
166*b077aed3SPierre Pronchery             }
167*b077aed3SPierre Pronchery         }
168*b077aed3SPierre Pronchery         ok = ok && key_checked;
169*b077aed3SPierre Pronchery     }
170*b077aed3SPierre Pronchery     return ok;
171*b077aed3SPierre Pronchery }
172*b077aed3SPierre Pronchery 
rsa_import(void * keydata,int selection,const OSSL_PARAM params[])173*b077aed3SPierre Pronchery static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
174*b077aed3SPierre Pronchery {
175*b077aed3SPierre Pronchery     RSA *rsa = keydata;
176*b077aed3SPierre Pronchery     int rsa_type;
177*b077aed3SPierre Pronchery     int ok = 1;
178*b077aed3SPierre Pronchery     int pss_defaults_set = 0;
179*b077aed3SPierre Pronchery 
180*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || rsa == NULL)
181*b077aed3SPierre Pronchery         return 0;
182*b077aed3SPierre Pronchery 
183*b077aed3SPierre Pronchery     if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
184*b077aed3SPierre Pronchery         return 0;
185*b077aed3SPierre Pronchery 
186*b077aed3SPierre Pronchery     rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
187*b077aed3SPierre Pronchery 
188*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
189*b077aed3SPierre Pronchery         ok = ok && pss_params_fromdata(ossl_rsa_get0_pss_params_30(rsa),
190*b077aed3SPierre Pronchery                                        &pss_defaults_set,
191*b077aed3SPierre Pronchery                                        params, rsa_type,
192*b077aed3SPierre Pronchery                                        ossl_rsa_get0_libctx(rsa));
193*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
194*b077aed3SPierre Pronchery         int include_private =
195*b077aed3SPierre Pronchery             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
196*b077aed3SPierre Pronchery 
197*b077aed3SPierre Pronchery         ok = ok && ossl_rsa_fromdata(rsa, params, include_private);
198*b077aed3SPierre Pronchery     }
199*b077aed3SPierre Pronchery 
200*b077aed3SPierre Pronchery     return ok;
201*b077aed3SPierre Pronchery }
202*b077aed3SPierre Pronchery 
rsa_export(void * keydata,int selection,OSSL_CALLBACK * param_callback,void * cbarg)203*b077aed3SPierre Pronchery static int rsa_export(void *keydata, int selection,
204*b077aed3SPierre Pronchery                       OSSL_CALLBACK *param_callback, void *cbarg)
205*b077aed3SPierre Pronchery {
206*b077aed3SPierre Pronchery     RSA *rsa = keydata;
207*b077aed3SPierre Pronchery     const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30(rsa);
208*b077aed3SPierre Pronchery     OSSL_PARAM_BLD *tmpl;
209*b077aed3SPierre Pronchery     OSSL_PARAM *params = NULL;
210*b077aed3SPierre Pronchery     int ok = 1;
211*b077aed3SPierre Pronchery 
212*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || rsa == NULL)
213*b077aed3SPierre Pronchery         return 0;
214*b077aed3SPierre Pronchery 
215*b077aed3SPierre Pronchery     if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
216*b077aed3SPierre Pronchery         return 0;
217*b077aed3SPierre Pronchery 
218*b077aed3SPierre Pronchery     tmpl = OSSL_PARAM_BLD_new();
219*b077aed3SPierre Pronchery     if (tmpl == NULL)
220*b077aed3SPierre Pronchery         return 0;
221*b077aed3SPierre Pronchery 
222*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
223*b077aed3SPierre Pronchery         ok = ok && (ossl_rsa_pss_params_30_is_unrestricted(pss_params)
224*b077aed3SPierre Pronchery                     || ossl_rsa_pss_params_30_todata(pss_params, tmpl, NULL));
225*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
226*b077aed3SPierre Pronchery         int include_private =
227*b077aed3SPierre Pronchery             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
228*b077aed3SPierre Pronchery 
229*b077aed3SPierre Pronchery         ok = ok && ossl_rsa_todata(rsa, tmpl, NULL, include_private);
230*b077aed3SPierre Pronchery     }
231*b077aed3SPierre Pronchery 
232*b077aed3SPierre Pronchery     if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
233*b077aed3SPierre Pronchery         ok = 0;
234*b077aed3SPierre Pronchery         goto err;
235*b077aed3SPierre Pronchery     }
236*b077aed3SPierre Pronchery 
237*b077aed3SPierre Pronchery     ok = param_callback(params, cbarg);
238*b077aed3SPierre Pronchery     OSSL_PARAM_free(params);
239*b077aed3SPierre Pronchery err:
240*b077aed3SPierre Pronchery     OSSL_PARAM_BLD_free(tmpl);
241*b077aed3SPierre Pronchery     return ok;
242*b077aed3SPierre Pronchery }
243*b077aed3SPierre Pronchery 
244*b077aed3SPierre Pronchery #ifdef FIPS_MODULE
245*b077aed3SPierre Pronchery /* In fips mode there are no multi-primes. */
246*b077aed3SPierre Pronchery # define RSA_KEY_MP_TYPES()                                                    \
247*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),                           \
248*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),                           \
249*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),                         \
250*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),                         \
251*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
252*b077aed3SPierre Pronchery #else
253*b077aed3SPierre Pronchery /*
254*b077aed3SPierre Pronchery  * We allow up to 10 prime factors (starting with p, q).
255*b077aed3SPierre Pronchery  * NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
256*b077aed3SPierre Pronchery  */
257*b077aed3SPierre Pronchery # define RSA_KEY_MP_TYPES()                                                    \
258*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0),                           \
259*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0),                           \
260*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0),                           \
261*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0),                           \
262*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0),                           \
263*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0),                           \
264*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0),                           \
265*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0),                           \
266*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0),                           \
267*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0),                          \
268*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0),                         \
269*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0),                         \
270*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0),                         \
271*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0),                         \
272*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0),                         \
273*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0),                         \
274*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0),                         \
275*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0),                         \
276*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0),                         \
277*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0),                        \
278*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),                      \
279*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0),                      \
280*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0),                      \
281*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0),                      \
282*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0),                      \
283*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0),                      \
284*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0),                      \
285*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0),                      \
286*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0),
287*b077aed3SPierre Pronchery #endif
288*b077aed3SPierre Pronchery 
289*b077aed3SPierre Pronchery #define RSA_KEY_TYPES()                                                        \
290*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),                                 \
291*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),                                 \
292*b077aed3SPierre Pronchery OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),                                 \
293*b077aed3SPierre Pronchery RSA_KEY_MP_TYPES()
294*b077aed3SPierre Pronchery 
295*b077aed3SPierre Pronchery /*
296*b077aed3SPierre Pronchery  * This provider can export everything in an RSA key, so we use the exact
297*b077aed3SPierre Pronchery  * same type description for export as for import.  Other providers might
298*b077aed3SPierre Pronchery  * choose to import full keys, but only export the public parts, and will
299*b077aed3SPierre Pronchery  * therefore have the importkey_types and importkey_types functions return
300*b077aed3SPierre Pronchery  * different arrays.
301*b077aed3SPierre Pronchery  */
302*b077aed3SPierre Pronchery static const OSSL_PARAM rsa_key_types[] = {
303*b077aed3SPierre Pronchery     RSA_KEY_TYPES()
304*b077aed3SPierre Pronchery     OSSL_PARAM_END
305*b077aed3SPierre Pronchery };
306*b077aed3SPierre Pronchery /*
307*b077aed3SPierre Pronchery  * We lied about the amount of factors, exponents and coefficients, the
308*b077aed3SPierre Pronchery  * export and import functions can really deal with an infinite amount
309*b077aed3SPierre Pronchery  * of these numbers.  However, RSA keys with too many primes are futile,
310*b077aed3SPierre Pronchery  * so we at least pretend to have some limits.
311*b077aed3SPierre Pronchery  */
312*b077aed3SPierre Pronchery 
rsa_imexport_types(int selection)313*b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_imexport_types(int selection)
314*b077aed3SPierre Pronchery {
315*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
316*b077aed3SPierre Pronchery         return rsa_key_types;
317*b077aed3SPierre Pronchery     return NULL;
318*b077aed3SPierre Pronchery }
319*b077aed3SPierre Pronchery 
rsa_import_types(int selection)320*b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_import_types(int selection)
321*b077aed3SPierre Pronchery {
322*b077aed3SPierre Pronchery     return rsa_imexport_types(selection);
323*b077aed3SPierre Pronchery }
324*b077aed3SPierre Pronchery 
rsa_export_types(int selection)325*b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_export_types(int selection)
326*b077aed3SPierre Pronchery {
327*b077aed3SPierre Pronchery     return rsa_imexport_types(selection);
328*b077aed3SPierre Pronchery }
329*b077aed3SPierre Pronchery 
rsa_get_params(void * key,OSSL_PARAM params[])330*b077aed3SPierre Pronchery static int rsa_get_params(void *key, OSSL_PARAM params[])
331*b077aed3SPierre Pronchery {
332*b077aed3SPierre Pronchery     RSA *rsa = key;
333*b077aed3SPierre Pronchery     const RSA_PSS_PARAMS_30 *pss_params = ossl_rsa_get0_pss_params_30(rsa);
334*b077aed3SPierre Pronchery     int rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
335*b077aed3SPierre Pronchery     OSSL_PARAM *p;
336*b077aed3SPierre Pronchery     int empty = RSA_get0_n(rsa) == NULL;
337*b077aed3SPierre Pronchery 
338*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
339*b077aed3SPierre Pronchery         && (empty || !OSSL_PARAM_set_int(p, RSA_bits(rsa))))
340*b077aed3SPierre Pronchery         return 0;
341*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
342*b077aed3SPierre Pronchery         && (empty || !OSSL_PARAM_set_int(p, RSA_security_bits(rsa))))
343*b077aed3SPierre Pronchery         return 0;
344*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
345*b077aed3SPierre Pronchery         && (empty || !OSSL_PARAM_set_int(p, RSA_size(rsa))))
346*b077aed3SPierre Pronchery         return 0;
347*b077aed3SPierre Pronchery 
348*b077aed3SPierre Pronchery     /*
349*b077aed3SPierre Pronchery      * For restricted RSA-PSS keys, we ignore the default digest request.
350*b077aed3SPierre Pronchery      * With RSA-OAEP keys, this may need to be amended.
351*b077aed3SPierre Pronchery      */
352*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
353*b077aed3SPierre Pronchery         && (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
354*b077aed3SPierre Pronchery             || ossl_rsa_pss_params_30_is_unrestricted(pss_params))) {
355*b077aed3SPierre Pronchery         if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
356*b077aed3SPierre Pronchery             return 0;
357*b077aed3SPierre Pronchery     }
358*b077aed3SPierre Pronchery 
359*b077aed3SPierre Pronchery     /*
360*b077aed3SPierre Pronchery      * For non-RSA-PSS keys, we ignore the mandatory digest request.
361*b077aed3SPierre Pronchery      * With RSA-OAEP keys, this may need to be amended.
362*b077aed3SPierre Pronchery      */
363*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate(params,
364*b077aed3SPierre Pronchery                                OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
365*b077aed3SPierre Pronchery         && rsa_type == RSA_FLAG_TYPE_RSASSAPSS
366*b077aed3SPierre Pronchery         && !ossl_rsa_pss_params_30_is_unrestricted(pss_params)) {
367*b077aed3SPierre Pronchery         const char *mdname =
368*b077aed3SPierre Pronchery             ossl_rsa_oaeppss_nid2name(ossl_rsa_pss_params_30_hashalg(pss_params));
369*b077aed3SPierre Pronchery 
370*b077aed3SPierre Pronchery         if (mdname == NULL || !OSSL_PARAM_set_utf8_string(p, mdname))
371*b077aed3SPierre Pronchery             return 0;
372*b077aed3SPierre Pronchery     }
373*b077aed3SPierre Pronchery     return (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
374*b077aed3SPierre Pronchery             || ossl_rsa_pss_params_30_todata(pss_params, NULL, params))
375*b077aed3SPierre Pronchery         && ossl_rsa_todata(rsa, NULL, params, 1);
376*b077aed3SPierre Pronchery }
377*b077aed3SPierre Pronchery 
378*b077aed3SPierre Pronchery static const OSSL_PARAM rsa_params[] = {
379*b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
380*b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
381*b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
382*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
383*b077aed3SPierre Pronchery     RSA_KEY_TYPES()
384*b077aed3SPierre Pronchery     OSSL_PARAM_END
385*b077aed3SPierre Pronchery };
386*b077aed3SPierre Pronchery 
rsa_gettable_params(void * provctx)387*b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_gettable_params(void *provctx)
388*b077aed3SPierre Pronchery {
389*b077aed3SPierre Pronchery     return rsa_params;
390*b077aed3SPierre Pronchery }
391*b077aed3SPierre Pronchery 
rsa_validate(const void * keydata,int selection,int checktype)392*b077aed3SPierre Pronchery static int rsa_validate(const void *keydata, int selection, int checktype)
393*b077aed3SPierre Pronchery {
394*b077aed3SPierre Pronchery     const RSA *rsa = keydata;
395*b077aed3SPierre Pronchery     int ok = 1;
396*b077aed3SPierre Pronchery 
397*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
398*b077aed3SPierre Pronchery         return 0;
399*b077aed3SPierre Pronchery 
400*b077aed3SPierre Pronchery     if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
401*b077aed3SPierre Pronchery         return 1; /* nothing to validate */
402*b077aed3SPierre Pronchery 
403*b077aed3SPierre Pronchery     /* If the whole key is selected, we do a pairwise validation */
404*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
405*b077aed3SPierre Pronchery         == OSSL_KEYMGMT_SELECT_KEYPAIR) {
406*b077aed3SPierre Pronchery         ok = ok && ossl_rsa_validate_pairwise(rsa);
407*b077aed3SPierre Pronchery     } else {
408*b077aed3SPierre Pronchery         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
409*b077aed3SPierre Pronchery             ok = ok && ossl_rsa_validate_private(rsa);
410*b077aed3SPierre Pronchery         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
411*b077aed3SPierre Pronchery             ok = ok && ossl_rsa_validate_public(rsa);
412*b077aed3SPierre Pronchery     }
413*b077aed3SPierre Pronchery     return ok;
414*b077aed3SPierre Pronchery }
415*b077aed3SPierre Pronchery 
416*b077aed3SPierre Pronchery struct rsa_gen_ctx {
417*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx;
418*b077aed3SPierre Pronchery     const char *propq;
419*b077aed3SPierre Pronchery 
420*b077aed3SPierre Pronchery     int rsa_type;
421*b077aed3SPierre Pronchery 
422*b077aed3SPierre Pronchery     size_t nbits;
423*b077aed3SPierre Pronchery     BIGNUM *pub_exp;
424*b077aed3SPierre Pronchery     size_t primes;
425*b077aed3SPierre Pronchery 
426*b077aed3SPierre Pronchery     /* For PSS */
427*b077aed3SPierre Pronchery     RSA_PSS_PARAMS_30 pss_params;
428*b077aed3SPierre Pronchery     int pss_defaults_set;
429*b077aed3SPierre Pronchery 
430*b077aed3SPierre Pronchery     /* For generation callback */
431*b077aed3SPierre Pronchery     OSSL_CALLBACK *cb;
432*b077aed3SPierre Pronchery     void *cbarg;
433*b077aed3SPierre Pronchery 
434*b077aed3SPierre Pronchery #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
435*b077aed3SPierre Pronchery     /* ACVP test parameters */
436*b077aed3SPierre Pronchery     OSSL_PARAM *acvp_test_params;
437*b077aed3SPierre Pronchery #endif
438*b077aed3SPierre Pronchery };
439*b077aed3SPierre Pronchery 
rsa_gencb(int p,int n,BN_GENCB * cb)440*b077aed3SPierre Pronchery static int rsa_gencb(int p, int n, BN_GENCB *cb)
441*b077aed3SPierre Pronchery {
442*b077aed3SPierre Pronchery     struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
443*b077aed3SPierre Pronchery     OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
444*b077aed3SPierre Pronchery 
445*b077aed3SPierre Pronchery     params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
446*b077aed3SPierre Pronchery     params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
447*b077aed3SPierre Pronchery     return gctx->cb(params, gctx->cbarg);
448*b077aed3SPierre Pronchery }
449*b077aed3SPierre Pronchery 
gen_init(void * provctx,int selection,int rsa_type,const OSSL_PARAM params[])450*b077aed3SPierre Pronchery static void *gen_init(void *provctx, int selection, int rsa_type,
451*b077aed3SPierre Pronchery                       const OSSL_PARAM params[])
452*b077aed3SPierre Pronchery {
453*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
454*b077aed3SPierre Pronchery     struct rsa_gen_ctx *gctx = NULL;
455*b077aed3SPierre Pronchery 
456*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
457*b077aed3SPierre Pronchery         return NULL;
458*b077aed3SPierre Pronchery 
459*b077aed3SPierre Pronchery     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
460*b077aed3SPierre Pronchery         return NULL;
461*b077aed3SPierre Pronchery 
462*b077aed3SPierre Pronchery     if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
463*b077aed3SPierre Pronchery         gctx->libctx = libctx;
464*b077aed3SPierre Pronchery         if ((gctx->pub_exp = BN_new()) == NULL
465*b077aed3SPierre Pronchery             || !BN_set_word(gctx->pub_exp, RSA_F4)) {
466*b077aed3SPierre Pronchery             goto err;
467*b077aed3SPierre Pronchery         }
468*b077aed3SPierre Pronchery         gctx->nbits = 2048;
469*b077aed3SPierre Pronchery         gctx->primes = RSA_DEFAULT_PRIME_NUM;
470*b077aed3SPierre Pronchery         gctx->rsa_type = rsa_type;
471*b077aed3SPierre Pronchery     } else {
472*b077aed3SPierre Pronchery         goto err;
473*b077aed3SPierre Pronchery     }
474*b077aed3SPierre Pronchery 
475*b077aed3SPierre Pronchery     if (!rsa_gen_set_params(gctx, params))
476*b077aed3SPierre Pronchery         goto err;
477*b077aed3SPierre Pronchery     return gctx;
478*b077aed3SPierre Pronchery 
479*b077aed3SPierre Pronchery err:
480*b077aed3SPierre Pronchery     if (gctx != NULL)
481*b077aed3SPierre Pronchery         BN_free(gctx->pub_exp);
482*b077aed3SPierre Pronchery     OPENSSL_free(gctx);
483*b077aed3SPierre Pronchery     return NULL;
484*b077aed3SPierre Pronchery }
485*b077aed3SPierre Pronchery 
rsa_gen_init(void * provctx,int selection,const OSSL_PARAM params[])486*b077aed3SPierre Pronchery static void *rsa_gen_init(void *provctx, int selection,
487*b077aed3SPierre Pronchery                           const OSSL_PARAM params[])
488*b077aed3SPierre Pronchery {
489*b077aed3SPierre Pronchery     return gen_init(provctx, selection, RSA_FLAG_TYPE_RSA, params);
490*b077aed3SPierre Pronchery }
491*b077aed3SPierre Pronchery 
rsapss_gen_init(void * provctx,int selection,const OSSL_PARAM params[])492*b077aed3SPierre Pronchery static void *rsapss_gen_init(void *provctx, int selection,
493*b077aed3SPierre Pronchery                              const OSSL_PARAM params[])
494*b077aed3SPierre Pronchery {
495*b077aed3SPierre Pronchery     return gen_init(provctx, selection, RSA_FLAG_TYPE_RSASSAPSS, params);
496*b077aed3SPierre Pronchery }
497*b077aed3SPierre Pronchery 
498*b077aed3SPierre Pronchery /*
499*b077aed3SPierre Pronchery  * This function is common for all RSA sub-types, to detect possible
500*b077aed3SPierre Pronchery  * misuse, such as PSS parameters being passed when a plain RSA key
501*b077aed3SPierre Pronchery  * is generated.
502*b077aed3SPierre Pronchery  */
rsa_gen_set_params(void * genctx,const OSSL_PARAM params[])503*b077aed3SPierre Pronchery static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
504*b077aed3SPierre Pronchery {
505*b077aed3SPierre Pronchery     struct rsa_gen_ctx *gctx = genctx;
506*b077aed3SPierre Pronchery     const OSSL_PARAM *p;
507*b077aed3SPierre Pronchery 
508*b077aed3SPierre Pronchery     if (params == NULL)
509*b077aed3SPierre Pronchery         return 1;
510*b077aed3SPierre Pronchery 
511*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL) {
512*b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_size_t(p, &gctx->nbits))
513*b077aed3SPierre Pronchery             return 0;
514*b077aed3SPierre Pronchery         if (gctx->nbits < RSA_MIN_MODULUS_BITS) {
515*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_KEY_SIZE_TOO_SMALL);
516*b077aed3SPierre Pronchery             return 0;
517*b077aed3SPierre Pronchery         }
518*b077aed3SPierre Pronchery     }
519*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
520*b077aed3SPierre Pronchery         && !OSSL_PARAM_get_size_t(p, &gctx->primes))
521*b077aed3SPierre Pronchery         return 0;
522*b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
523*b077aed3SPierre Pronchery         && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
524*b077aed3SPierre Pronchery         return 0;
525*b077aed3SPierre Pronchery     /* Only attempt to get PSS parameters when generating an RSA-PSS key */
526*b077aed3SPierre Pronchery     if (gctx->rsa_type == RSA_FLAG_TYPE_RSASSAPSS
527*b077aed3SPierre Pronchery         && !pss_params_fromdata(&gctx->pss_params, &gctx->pss_defaults_set, params,
528*b077aed3SPierre Pronchery                                 gctx->rsa_type, gctx->libctx))
529*b077aed3SPierre Pronchery         return 0;
530*b077aed3SPierre Pronchery #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
531*b077aed3SPierre Pronchery     /* Any ACVP test related parameters are copied into a params[] */
532*b077aed3SPierre Pronchery     if (!ossl_rsa_acvp_test_gen_params_new(&gctx->acvp_test_params, params))
533*b077aed3SPierre Pronchery         return 0;
534*b077aed3SPierre Pronchery #endif
535*b077aed3SPierre Pronchery     return 1;
536*b077aed3SPierre Pronchery }
537*b077aed3SPierre Pronchery 
538*b077aed3SPierre Pronchery #define rsa_gen_basic                                           \
539*b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL),          \
540*b077aed3SPierre Pronchery     OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL),        \
541*b077aed3SPierre Pronchery     OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0)
542*b077aed3SPierre Pronchery 
543*b077aed3SPierre Pronchery /*
544*b077aed3SPierre Pronchery  * The following must be kept in sync with ossl_rsa_pss_params_30_fromdata()
545*b077aed3SPierre Pronchery  * in crypto/rsa/rsa_backend.c
546*b077aed3SPierre Pronchery  */
547*b077aed3SPierre Pronchery #define rsa_gen_pss                                                     \
548*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST, NULL, 0),        \
549*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, NULL, 0),  \
550*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MASKGENFUNC, NULL, 0),   \
551*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MGF1_DIGEST, NULL, 0),   \
552*b077aed3SPierre Pronchery     OSSL_PARAM_int(OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, NULL)
553*b077aed3SPierre Pronchery 
rsa_gen_settable_params(ossl_unused void * genctx,ossl_unused void * provctx)554*b077aed3SPierre Pronchery static const OSSL_PARAM *rsa_gen_settable_params(ossl_unused void *genctx,
555*b077aed3SPierre Pronchery                                                  ossl_unused void *provctx)
556*b077aed3SPierre Pronchery {
557*b077aed3SPierre Pronchery     static OSSL_PARAM settable[] = {
558*b077aed3SPierre Pronchery         rsa_gen_basic,
559*b077aed3SPierre Pronchery         OSSL_PARAM_END
560*b077aed3SPierre Pronchery     };
561*b077aed3SPierre Pronchery 
562*b077aed3SPierre Pronchery     return settable;
563*b077aed3SPierre Pronchery }
564*b077aed3SPierre Pronchery 
rsapss_gen_settable_params(ossl_unused void * genctx,ossl_unused void * provctx)565*b077aed3SPierre Pronchery static const OSSL_PARAM *rsapss_gen_settable_params(ossl_unused void *genctx,
566*b077aed3SPierre Pronchery                                                     ossl_unused void *provctx)
567*b077aed3SPierre Pronchery {
568*b077aed3SPierre Pronchery     static OSSL_PARAM settable[] = {
569*b077aed3SPierre Pronchery         rsa_gen_basic,
570*b077aed3SPierre Pronchery         rsa_gen_pss,
571*b077aed3SPierre Pronchery         OSSL_PARAM_END
572*b077aed3SPierre Pronchery     };
573*b077aed3SPierre Pronchery 
574*b077aed3SPierre Pronchery     return settable;
575*b077aed3SPierre Pronchery }
576*b077aed3SPierre Pronchery 
rsa_gen(void * genctx,OSSL_CALLBACK * osslcb,void * cbarg)577*b077aed3SPierre Pronchery static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
578*b077aed3SPierre Pronchery {
579*b077aed3SPierre Pronchery     struct rsa_gen_ctx *gctx = genctx;
580*b077aed3SPierre Pronchery     RSA *rsa = NULL, *rsa_tmp = NULL;
581*b077aed3SPierre Pronchery     BN_GENCB *gencb = NULL;
582*b077aed3SPierre Pronchery 
583*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || gctx == NULL)
584*b077aed3SPierre Pronchery         return NULL;
585*b077aed3SPierre Pronchery 
586*b077aed3SPierre Pronchery     switch (gctx->rsa_type) {
587*b077aed3SPierre Pronchery     case RSA_FLAG_TYPE_RSA:
588*b077aed3SPierre Pronchery         /* For plain RSA keys, PSS parameters must not be set */
589*b077aed3SPierre Pronchery         if (!ossl_rsa_pss_params_30_is_unrestricted(&gctx->pss_params))
590*b077aed3SPierre Pronchery             goto err;
591*b077aed3SPierre Pronchery         break;
592*b077aed3SPierre Pronchery     case RSA_FLAG_TYPE_RSASSAPSS:
593*b077aed3SPierre Pronchery         /*
594*b077aed3SPierre Pronchery          * For plain RSA-PSS keys, PSS parameters may be set but don't have
595*b077aed3SPierre Pronchery          * to, so not check.
596*b077aed3SPierre Pronchery          */
597*b077aed3SPierre Pronchery         break;
598*b077aed3SPierre Pronchery     default:
599*b077aed3SPierre Pronchery         /* Unsupported RSA key sub-type... */
600*b077aed3SPierre Pronchery         return NULL;
601*b077aed3SPierre Pronchery     }
602*b077aed3SPierre Pronchery 
603*b077aed3SPierre Pronchery     if ((rsa_tmp = ossl_rsa_new_with_ctx(gctx->libctx)) == NULL)
604*b077aed3SPierre Pronchery         return NULL;
605*b077aed3SPierre Pronchery 
606*b077aed3SPierre Pronchery     gctx->cb = osslcb;
607*b077aed3SPierre Pronchery     gctx->cbarg = cbarg;
608*b077aed3SPierre Pronchery     gencb = BN_GENCB_new();
609*b077aed3SPierre Pronchery     if (gencb != NULL)
610*b077aed3SPierre Pronchery         BN_GENCB_set(gencb, rsa_gencb, genctx);
611*b077aed3SPierre Pronchery 
612*b077aed3SPierre Pronchery #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
613*b077aed3SPierre Pronchery     if (gctx->acvp_test_params != NULL) {
614*b077aed3SPierre Pronchery         if (!ossl_rsa_acvp_test_set_params(rsa_tmp, gctx->acvp_test_params))
615*b077aed3SPierre Pronchery             goto err;
616*b077aed3SPierre Pronchery     }
617*b077aed3SPierre Pronchery #endif
618*b077aed3SPierre Pronchery 
619*b077aed3SPierre Pronchery     if (!RSA_generate_multi_prime_key(rsa_tmp,
620*b077aed3SPierre Pronchery                                       (int)gctx->nbits, (int)gctx->primes,
621*b077aed3SPierre Pronchery                                       gctx->pub_exp, gencb))
622*b077aed3SPierre Pronchery         goto err;
623*b077aed3SPierre Pronchery 
624*b077aed3SPierre Pronchery     if (!ossl_rsa_pss_params_30_copy(ossl_rsa_get0_pss_params_30(rsa_tmp),
625*b077aed3SPierre Pronchery                                      &gctx->pss_params))
626*b077aed3SPierre Pronchery         goto err;
627*b077aed3SPierre Pronchery 
628*b077aed3SPierre Pronchery     RSA_clear_flags(rsa_tmp, RSA_FLAG_TYPE_MASK);
629*b077aed3SPierre Pronchery     RSA_set_flags(rsa_tmp, gctx->rsa_type);
630*b077aed3SPierre Pronchery 
631*b077aed3SPierre Pronchery     rsa = rsa_tmp;
632*b077aed3SPierre Pronchery     rsa_tmp = NULL;
633*b077aed3SPierre Pronchery  err:
634*b077aed3SPierre Pronchery     BN_GENCB_free(gencb);
635*b077aed3SPierre Pronchery     RSA_free(rsa_tmp);
636*b077aed3SPierre Pronchery     return rsa;
637*b077aed3SPierre Pronchery }
638*b077aed3SPierre Pronchery 
rsa_gen_cleanup(void * genctx)639*b077aed3SPierre Pronchery static void rsa_gen_cleanup(void *genctx)
640*b077aed3SPierre Pronchery {
641*b077aed3SPierre Pronchery     struct rsa_gen_ctx *gctx = genctx;
642*b077aed3SPierre Pronchery 
643*b077aed3SPierre Pronchery     if (gctx == NULL)
644*b077aed3SPierre Pronchery         return;
645*b077aed3SPierre Pronchery #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
646*b077aed3SPierre Pronchery     ossl_rsa_acvp_test_gen_params_free(gctx->acvp_test_params);
647*b077aed3SPierre Pronchery     gctx->acvp_test_params = NULL;
648*b077aed3SPierre Pronchery #endif
649*b077aed3SPierre Pronchery     BN_clear_free(gctx->pub_exp);
650*b077aed3SPierre Pronchery     OPENSSL_free(gctx);
651*b077aed3SPierre Pronchery }
652*b077aed3SPierre Pronchery 
common_load(const void * reference,size_t reference_sz,int expected_rsa_type)653*b077aed3SPierre Pronchery static void *common_load(const void *reference, size_t reference_sz,
654*b077aed3SPierre Pronchery                          int expected_rsa_type)
655*b077aed3SPierre Pronchery {
656*b077aed3SPierre Pronchery     RSA *rsa = NULL;
657*b077aed3SPierre Pronchery 
658*b077aed3SPierre Pronchery     if (ossl_prov_is_running() && reference_sz == sizeof(rsa)) {
659*b077aed3SPierre Pronchery         /* The contents of the reference is the address to our object */
660*b077aed3SPierre Pronchery         rsa = *(RSA **)reference;
661*b077aed3SPierre Pronchery 
662*b077aed3SPierre Pronchery         if (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK) != expected_rsa_type)
663*b077aed3SPierre Pronchery             return NULL;
664*b077aed3SPierre Pronchery 
665*b077aed3SPierre Pronchery         /* We grabbed, so we detach it */
666*b077aed3SPierre Pronchery         *(RSA **)reference = NULL;
667*b077aed3SPierre Pronchery         return rsa;
668*b077aed3SPierre Pronchery     }
669*b077aed3SPierre Pronchery     return NULL;
670*b077aed3SPierre Pronchery }
671*b077aed3SPierre Pronchery 
rsa_load(const void * reference,size_t reference_sz)672*b077aed3SPierre Pronchery static void *rsa_load(const void *reference, size_t reference_sz)
673*b077aed3SPierre Pronchery {
674*b077aed3SPierre Pronchery     return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSA);
675*b077aed3SPierre Pronchery }
676*b077aed3SPierre Pronchery 
rsapss_load(const void * reference,size_t reference_sz)677*b077aed3SPierre Pronchery static void *rsapss_load(const void *reference, size_t reference_sz)
678*b077aed3SPierre Pronchery {
679*b077aed3SPierre Pronchery     return common_load(reference, reference_sz, RSA_FLAG_TYPE_RSASSAPSS);
680*b077aed3SPierre Pronchery }
681*b077aed3SPierre Pronchery 
rsa_dup(const void * keydata_from,int selection)682*b077aed3SPierre Pronchery static void *rsa_dup(const void *keydata_from, int selection)
683*b077aed3SPierre Pronchery {
684*b077aed3SPierre Pronchery     if (ossl_prov_is_running()
685*b077aed3SPierre Pronchery         /* do not allow creating empty keys by duplication */
686*b077aed3SPierre Pronchery         && (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
687*b077aed3SPierre Pronchery         return ossl_rsa_dup(keydata_from, selection);
688*b077aed3SPierre Pronchery     return NULL;
689*b077aed3SPierre Pronchery }
690*b077aed3SPierre Pronchery 
691*b077aed3SPierre Pronchery /* For any RSA key, we use the "RSA" algorithms regardless of sub-type. */
rsa_query_operation_name(int operation_id)692*b077aed3SPierre Pronchery static const char *rsa_query_operation_name(int operation_id)
693*b077aed3SPierre Pronchery {
694*b077aed3SPierre Pronchery     return "RSA";
695*b077aed3SPierre Pronchery }
696*b077aed3SPierre Pronchery 
697*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_rsa_keymgmt_functions[] = {
698*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
699*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
700*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
701*b077aed3SPierre Pronchery       (void (*)(void))rsa_gen_set_params },
702*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
703*b077aed3SPierre Pronchery       (void (*)(void))rsa_gen_settable_params },
704*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
705*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
706*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsa_load },
707*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
708*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
709*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
710*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
711*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
712*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
713*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
714*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
715*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
716*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
717*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
718*b077aed3SPierre Pronchery     { 0, NULL }
719*b077aed3SPierre Pronchery };
720*b077aed3SPierre Pronchery 
721*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_rsapss_keymgmt_functions[] = {
722*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsapss_newdata },
723*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsapss_gen_init },
724*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))rsa_gen_set_params },
725*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
726*b077aed3SPierre Pronchery       (void (*)(void))rsapss_gen_settable_params },
727*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
728*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
729*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))rsapss_load },
730*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
731*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
732*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
733*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
734*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
735*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
736*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
737*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
738*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
739*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
740*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
741*b077aed3SPierre Pronchery       (void (*)(void))rsa_query_operation_name },
742*b077aed3SPierre Pronchery     { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))rsa_dup },
743*b077aed3SPierre Pronchery     { 0, NULL }
744*b077aed3SPierre Pronchery };
745