/src/openssl/crypto/rsa/rsa_backend.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2020-2024 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  |  | /*  | 
11  |  |  * RSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <string.h>  | 
17  |  | #include <openssl/core_names.h>  | 
18  |  | #include <openssl/params.h>  | 
19  |  | #include <openssl/err.h>  | 
20  |  | #include <openssl/evp.h>  | 
21  |  | #ifndef FIPS_MODULE  | 
22  |  | # include <openssl/x509.h>  | 
23  |  | # include "crypto/asn1.h"  | 
24  |  | #endif  | 
25  |  | #include "internal/sizes.h"  | 
26  |  | #include "internal/param_build_set.h"  | 
27  |  | #include "crypto/rsa.h"  | 
28  |  | #include "rsa_local.h"  | 
29  |  |  | 
30  |  | /*  | 
31  |  |  * The intention with the "backend" source file is to offer backend support  | 
32  |  |  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider  | 
33  |  |  * implementations alike.  | 
34  |  |  */  | 
35  |  |  | 
36  |  | DEFINE_STACK_OF(BIGNUM)  | 
37  |  |  | 
38  |  | static int collect_numbers(STACK_OF(BIGNUM) *numbers,  | 
39  |  |                            const OSSL_PARAM params[], const char *names[])  | 
40  | 89.9k  | { | 
41  | 89.9k  |     const OSSL_PARAM *p = NULL;  | 
42  | 89.9k  |     int i;  | 
43  |  |  | 
44  | 89.9k  |     if (numbers == NULL)  | 
45  | 0  |         return 0;  | 
46  |  |  | 
47  | 959k  |     for (i = 0; names[i] != NULL; i++) { | 
48  | 869k  |         p = OSSL_PARAM_locate_const(params, names[i]);  | 
49  | 869k  |         if (p != NULL) { | 
50  | 149k  |             BIGNUM *tmp = NULL;  | 
51  |  |  | 
52  | 149k  |             if (!OSSL_PARAM_get_BN(p, &tmp))  | 
53  | 0  |                 return 0;  | 
54  | 149k  |             if (sk_BIGNUM_push(numbers, tmp) == 0) { | 
55  | 0  |                 BN_clear_free(tmp);  | 
56  | 0  |                 return 0;  | 
57  | 0  |             }  | 
58  | 149k  |         }  | 
59  | 869k  |     }  | 
60  |  |  | 
61  | 89.9k  |     return 1;  | 
62  | 89.9k  | }  | 
63  |  |  | 
64  |  | int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private)  | 
65  | 14.5k  | { | 
66  | 14.5k  |     const OSSL_PARAM *param_n, *param_e,  *param_d = NULL;  | 
67  | 14.5k  |     const OSSL_PARAM *param_p, *param_q = NULL;  | 
68  | 14.5k  |     const OSSL_PARAM *param_derive = NULL;  | 
69  | 14.5k  |     BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL;  | 
70  | 14.5k  |     STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;  | 
71  | 14.5k  |     int is_private = 0;  | 
72  | 14.5k  |     int derive_from_pq = 0;  | 
73  | 14.5k  |     BN_CTX *ctx = NULL;  | 
74  |  |  | 
75  | 14.5k  |     if (rsa == NULL)  | 
76  | 0  |         return 0;  | 
77  |  |  | 
78  | 14.5k  |     param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);  | 
79  | 14.5k  |     param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);  | 
80  |  |  | 
81  | 14.5k  |     if ((param_n == NULL || !OSSL_PARAM_get_BN(param_n, &n))  | 
82  | 14.5k  |         || (param_e == NULL || !OSSL_PARAM_get_BN(param_e, &e))) { | 
83  | 0  |         ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);  | 
84  | 0  |         goto err;  | 
85  | 0  |     }  | 
86  |  |  | 
87  | 14.5k  |     if (include_private) { | 
88  |  |  | 
89  | 14.5k  |         param_derive = OSSL_PARAM_locate_const(params,  | 
90  | 14.5k  |                                            OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ);  | 
91  | 14.5k  |         if ((param_derive != NULL)  | 
92  | 14.5k  |             && !OSSL_PARAM_get_int(param_derive, &derive_from_pq))  | 
93  | 0  |             goto err;  | 
94  |  |  | 
95  | 14.5k  |         param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);  | 
96  | 14.5k  |         if (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)) { | 
97  | 0  |             ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);  | 
98  | 0  |             goto err;  | 
99  | 0  |         }  | 
100  |  |  | 
101  | 14.5k  |         if (derive_from_pq) { | 
102  | 0  |             ctx = BN_CTX_new_ex(rsa->libctx);  | 
103  | 0  |             if (ctx == NULL)  | 
104  | 0  |                 goto err;  | 
105  |  |  | 
106  |  |             /* we need at minimum p, q */  | 
107  | 0  |             param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR1);  | 
108  | 0  |             param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR2);  | 
109  | 0  |             if ((param_p == NULL || !OSSL_PARAM_get_BN(param_p, &p))  | 
110  | 0  |                 || (param_q == NULL || !OSSL_PARAM_get_BN(param_q, &q))) { | 
111  | 0  |                 ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);  | 
112  | 0  |                 goto err;  | 
113  | 0  |             }  | 
114  |  | 
  | 
115  | 0  |         }  | 
116  | 14.5k  |     }  | 
117  |  |  | 
118  | 14.5k  |     is_private = (d != NULL);  | 
119  |  |  | 
120  | 14.5k  |     if (!RSA_set0_key(rsa, n, e, d))  | 
121  | 0  |         goto err;  | 
122  | 14.5k  |     n = e = d = NULL;  | 
123  |  |  | 
124  | 14.5k  |     if (is_private) { | 
125  | 14.5k  |         if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,  | 
126  | 14.5k  |                              ossl_rsa_mp_factor_names)  | 
127  | 14.5k  |             || !collect_numbers(exps = sk_BIGNUM_new_null(), params,  | 
128  | 14.5k  |                                 ossl_rsa_mp_exp_names)  | 
129  | 14.5k  |             || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,  | 
130  | 14.5k  |                                 ossl_rsa_mp_coeff_names))  | 
131  | 0  |             goto err;  | 
132  |  |  | 
133  | 14.5k  |         if (derive_from_pq && sk_BIGNUM_num(exps) == 0  | 
134  | 14.5k  |             && sk_BIGNUM_num(coeffs) == 0) { | 
135  |  |             /*  | 
136  |  |              * If we want to use crt to derive our exponents/coefficients, we  | 
137  |  |              * need to have at least 2 factors  | 
138  |  |              */  | 
139  | 0  |             if (sk_BIGNUM_num(factors) < 2) { | 
140  | 0  |                 ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);  | 
141  | 0  |                 goto err;  | 
142  | 0  |             }  | 
143  |  |  | 
144  |  |             /*  | 
145  |  |              * if we have more than two factors, n and d must also have  | 
146  |  |              * been provided  | 
147  |  |              */  | 
148  | 0  |             if (sk_BIGNUM_num(factors) > 2  | 
149  | 0  |                 && (param_n == NULL || param_d == NULL)) { | 
150  | 0  |                 ERR_raise(ERR_LIB_RSA, ERR_R_PASSED_NULL_PARAMETER);  | 
151  | 0  |                 goto err;  | 
152  | 0  |             }  | 
153  |  |  | 
154  |  |             /* build our exponents and coefficients here */  | 
155  | 0  |             if (sk_BIGNUM_num(factors) == 2) { | 
156  |  |                 /* for 2 factors we can use the sp800 functions to do this */  | 
157  | 0  |                 if (!RSA_set0_factors(rsa, sk_BIGNUM_value(factors, 0),  | 
158  | 0  |                                       sk_BIGNUM_value(factors, 1))) { | 
159  | 0  |                     ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);  | 
160  | 0  |                     goto err;  | 
161  | 0  |                 }  | 
162  |  |                 /*  | 
163  |  |                  * once consumed by RSA_set0_factors, pop those off the stack  | 
164  |  |                  * so we don't free them below  | 
165  |  |                  */  | 
166  | 0  |                 sk_BIGNUM_pop(factors);  | 
167  | 0  |                 sk_BIGNUM_pop(factors);  | 
168  |  |  | 
169  |  |                 /*  | 
170  |  |                  * Note: Because we only have 2 factors here, there will be no  | 
171  |  |                  * additional pinfo fields to hold additional factors, and  | 
172  |  |                  * since we set our key and 2 factors above we can skip  | 
173  |  |                  * the call to ossl_rsa_set0_all_params  | 
174  |  |                  */  | 
175  | 0  |                 if (!ossl_rsa_sp800_56b_derive_params_from_pq(rsa,  | 
176  | 0  |                                                               RSA_bits(rsa),  | 
177  | 0  |                                                               NULL, ctx)) { | 
178  | 0  |                     ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);  | 
179  | 0  |                     goto err;  | 
180  | 0  |                 }  | 
181  | 0  |             } else { | 
182  | 0  | #ifndef FIPS_MODULE  | 
183  |  |                 /*  | 
184  |  |                  * in the multiprime case we have to generate exps/coeffs here  | 
185  |  |                  * for each additional prime  | 
186  |  |                  */  | 
187  | 0  |                 if (!ossl_rsa_multiprime_derive(rsa, RSA_bits(rsa),  | 
188  | 0  |                                                 sk_BIGNUM_num(factors),  | 
189  | 0  |                                                 rsa->e, factors, exps,  | 
190  | 0  |                                                 coeffs)) { | 
191  | 0  |                     ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);  | 
192  | 0  |                     goto err;  | 
193  | 0  |                 }  | 
194  |  |  | 
195  |  |                 /*  | 
196  |  |                  * Now we should have all our factors, exponents and  | 
197  |  |                  * coefficients  | 
198  |  |                  */  | 
199  | 0  |                 if (!ossl_rsa_set0_all_params(rsa, factors, exps, coeffs)) { | 
200  | 0  |                     ERR_raise(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR);  | 
201  | 0  |                     goto err;  | 
202  | 0  |                 }  | 
203  |  | 
  | 
204  |  | #else  | 
205  |  |                 /* multiprime case is disallowed in FIPS mode, raise an error */  | 
206  |  |                 ERR_raise(ERR_LIB_RSA, ERR_R_UNSUPPORTED);  | 
207  |  |                 goto err;  | 
208  |  | #endif  | 
209  | 0  |             }  | 
210  |  | 
  | 
211  | 14.5k  |         } else { | 
212  |  |             /*  | 
213  |  |              * It's ok if this private key just has n, e and d  | 
214  |  |              * but only if we're not using derive_from_pq  | 
215  |  |              */  | 
216  | 14.5k  |             if (sk_BIGNUM_num(factors) != 0  | 
217  | 14.5k  |                 && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs))  | 
218  | 0  |                 goto err;  | 
219  | 14.5k  |         }  | 
220  |  |         /* sanity check to ensure we used everything in our stacks */  | 
221  | 14.5k  |         if (sk_BIGNUM_num(factors) != 0  | 
222  | 14.5k  |             || sk_BIGNUM_num(exps) != 0  | 
223  | 14.5k  |             || sk_BIGNUM_num(coeffs) != 0) { | 
224  | 0  |             ERR_raise_data(ERR_LIB_RSA, ERR_R_INTERNAL_ERROR,  | 
225  | 0  |                            "There are %d, %d, %d elements left on our factors, exps, coeffs stacks\n",  | 
226  | 0  |                            sk_BIGNUM_num(factors), sk_BIGNUM_num(exps),  | 
227  | 0  |                            sk_BIGNUM_num(coeffs));  | 
228  | 0  |             goto err;  | 
229  | 0  |         }  | 
230  | 14.5k  |     }  | 
231  |  |  | 
232  | 14.5k  |     if (!ossl_rsa_check_factors(rsa)) { | 
233  | 0  |         ERR_raise_data(ERR_LIB_RSA, RSA_R_INVALID_KEYPAIR,  | 
234  | 0  |                        "RSA factors/exponents are too big for for n-modulus\n");  | 
235  | 0  |         goto err;  | 
236  | 0  |     }  | 
237  |  |  | 
238  | 14.5k  |     BN_clear_free(p);  | 
239  | 14.5k  |     BN_clear_free(q);  | 
240  | 14.5k  |     sk_BIGNUM_free(factors);  | 
241  | 14.5k  |     sk_BIGNUM_free(exps);  | 
242  | 14.5k  |     sk_BIGNUM_free(coeffs);  | 
243  | 14.5k  |     BN_CTX_free(ctx);  | 
244  | 14.5k  |     return 1;  | 
245  |  |  | 
246  | 0  |  err:  | 
247  | 0  |     BN_free(n);  | 
248  | 0  |     BN_free(e);  | 
249  | 0  |     BN_free(d);  | 
250  | 0  |     sk_BIGNUM_pop_free(factors, BN_clear_free);  | 
251  | 0  |     sk_BIGNUM_pop_free(exps, BN_clear_free);  | 
252  | 0  |     sk_BIGNUM_pop_free(coeffs, BN_clear_free);  | 
253  | 0  |     BN_CTX_free(ctx);  | 
254  | 0  |     return 0;  | 
255  | 14.5k  | }  | 
256  |  |  | 
257  |  | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)  | 
258  |  |  | 
259  |  | int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],  | 
260  |  |                     int include_private)  | 
261  | 135k  | { | 
262  | 135k  |     int ret = 0;  | 
263  | 135k  |     const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;  | 
264  | 135k  |     STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();  | 
265  | 135k  |     STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();  | 
266  | 135k  |     STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();  | 
267  |  |  | 
268  | 135k  |     if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)  | 
269  | 0  |         goto err;  | 
270  |  |  | 
271  | 135k  |     RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);  | 
272  | 135k  |     ossl_rsa_get0_all_params(rsa, factors, exps, coeffs);  | 
273  |  |  | 
274  | 135k  |     if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)  | 
275  | 135k  |         || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e))  | 
276  | 0  |         goto err;  | 
277  |  |  | 
278  |  |     /* Check private key data integrity */  | 
279  | 135k  |     if (include_private && rsa_d != NULL) { | 
280  |  |  | 
281  | 37.0k  |         if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D,  | 
282  | 37.0k  |                                      rsa_d)  | 
283  | 37.0k  |             || !ossl_param_build_set_multi_key_bn(bld, params,  | 
284  | 37.0k  |                                                   ossl_rsa_mp_factor_names,  | 
285  | 37.0k  |                                                   factors)  | 
286  | 37.0k  |             || !ossl_param_build_set_multi_key_bn(bld, params,  | 
287  | 37.0k  |                                                   ossl_rsa_mp_exp_names, exps)  | 
288  | 37.0k  |             || !ossl_param_build_set_multi_key_bn(bld, params,  | 
289  | 37.0k  |                                                   ossl_rsa_mp_coeff_names,  | 
290  | 37.0k  |                                                   coeffs))  | 
291  | 0  |             goto err;  | 
292  | 37.0k  |     }  | 
293  |  |  | 
294  |  | #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)  | 
295  |  |     /* The acvp test results are not meant for export so check for bld == NULL */  | 
296  |  |     if (bld == NULL)  | 
297  |  |         ossl_rsa_acvp_test_get_params(rsa, params);  | 
298  |  | #endif  | 
299  | 135k  |     ret = 1;  | 
300  | 135k  |  err:  | 
301  | 135k  |     sk_BIGNUM_const_free(factors);  | 
302  | 135k  |     sk_BIGNUM_const_free(exps);  | 
303  | 135k  |     sk_BIGNUM_const_free(coeffs);  | 
304  | 135k  |     return ret;  | 
305  | 135k  | }  | 
306  |  |  | 
307  |  | int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss,  | 
308  |  |                                   OSSL_PARAM_BLD *bld, OSSL_PARAM params[])  | 
309  | 39.6k  | { | 
310  | 39.6k  |     if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) { | 
311  | 22.9k  |         int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss);  | 
312  | 22.9k  |         int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss);  | 
313  | 22.9k  |         int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss);  | 
314  | 22.9k  |         int saltlen = ossl_rsa_pss_params_30_saltlen(pss);  | 
315  | 22.9k  |         int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL);  | 
316  | 22.9k  |         int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);  | 
317  | 22.9k  |         int default_maskgenhashalg_nid =  | 
318  | 22.9k  |                 ossl_rsa_pss_params_30_maskgenhashalg(NULL);  | 
319  | 22.9k  |         const char *mdname =  | 
320  | 22.9k  |             (hashalg_nid == default_hashalg_nid  | 
321  | 22.9k  |              ? NULL : ossl_rsa_oaeppss_nid2name(hashalg_nid));  | 
322  | 22.9k  |         const char *mgfname =  | 
323  | 22.9k  |             (maskgenalg_nid == default_maskgenalg_nid  | 
324  | 22.9k  |              ? NULL : ossl_rsa_oaeppss_nid2name(maskgenalg_nid));  | 
325  | 22.9k  |         const char *mgf1mdname =  | 
326  | 22.9k  |             (maskgenhashalg_nid == default_maskgenhashalg_nid  | 
327  | 22.9k  |              ? NULL : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid));  | 
328  | 22.9k  |         const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;  | 
329  | 22.9k  |         const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;  | 
330  | 22.9k  |         const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;  | 
331  | 22.9k  |         const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;  | 
332  |  |  | 
333  |  |         /*  | 
334  |  |          * To ensure that the key isn't seen as unrestricted by the recipient,  | 
335  |  |          * we make sure that at least one PSS-related parameter is passed, even  | 
336  |  |          * if it has a default value; saltlen.  | 
337  |  |          */  | 
338  | 22.9k  |         if ((mdname != NULL  | 
339  | 22.9k  |              && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))  | 
340  | 22.9k  |             || (mgfname != NULL  | 
341  | 22.9k  |                 && !ossl_param_build_set_utf8_string(bld, params,  | 
342  | 0  |                                                      key_mgf, mgfname))  | 
343  | 22.9k  |             || (mgf1mdname != NULL  | 
344  | 22.9k  |                 && !ossl_param_build_set_utf8_string(bld, params,  | 
345  | 0  |                                                      key_mgf1_md, mgf1mdname))  | 
346  | 22.9k  |             || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))  | 
347  | 0  |             return 0;  | 
348  | 22.9k  |     }  | 
349  | 39.6k  |     return 1;  | 
350  | 39.6k  | }  | 
351  |  |  | 
352  |  | int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params,  | 
353  |  |                                     int *defaults_set,  | 
354  |  |                                     const OSSL_PARAM params[],  | 
355  |  |                                     OSSL_LIB_CTX *libctx)  | 
356  | 4.54k  | { | 
357  | 4.54k  |     const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md,  *param_saltlen;  | 
358  | 4.54k  |     const OSSL_PARAM *param_propq;  | 
359  | 4.54k  |     const char *propq = NULL;  | 
360  | 4.54k  |     EVP_MD *md = NULL, *mgf1md = NULL;  | 
361  | 4.54k  |     int saltlen;  | 
362  | 4.54k  |     int ret = 0;  | 
363  |  |  | 
364  | 4.54k  |     if (pss_params == NULL)  | 
365  | 0  |         return 0;  | 
366  | 4.54k  |     param_propq =  | 
367  | 4.54k  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS);  | 
368  | 4.54k  |     param_md =  | 
369  | 4.54k  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);  | 
370  | 4.54k  |     param_mgf =  | 
371  | 4.54k  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);  | 
372  | 4.54k  |     param_mgf1md =  | 
373  | 4.54k  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);  | 
374  | 4.54k  |     param_saltlen =  | 
375  | 4.54k  |         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);  | 
376  |  |  | 
377  | 4.54k  |     if (param_propq != NULL) { | 
378  | 0  |         if (param_propq->data_type == OSSL_PARAM_UTF8_STRING)  | 
379  | 0  |             propq = param_propq->data;  | 
380  | 0  |     }  | 
381  |  |     /*  | 
382  |  |      * If we get any of the parameters, we know we have at least some  | 
383  |  |      * restrictions, so we start by setting default values, and let each  | 
384  |  |      * parameter override their specific restriction data.  | 
385  |  |      */  | 
386  | 4.54k  |     if (!*defaults_set  | 
387  | 4.54k  |         && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL  | 
388  | 4.54k  |             || param_saltlen != NULL)) { | 
389  | 0  |         if (!ossl_rsa_pss_params_30_set_defaults(pss_params))  | 
390  | 0  |             return 0;  | 
391  | 0  |         *defaults_set = 1;  | 
392  | 0  |     }  | 
393  |  |  | 
394  | 4.54k  |     if (param_mgf != NULL) { | 
395  | 0  |         int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL);  | 
396  | 0  |         const char *mgfname = NULL;  | 
397  |  | 
  | 
398  | 0  |         if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING)  | 
399  | 0  |             mgfname = param_mgf->data;  | 
400  | 0  |         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname))  | 
401  | 0  |             return 0;  | 
402  |  |  | 
403  | 0  |         if (OPENSSL_strcasecmp(param_mgf->data,  | 
404  | 0  |                                ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0)  | 
405  | 0  |             return 0;  | 
406  | 0  |     }  | 
407  |  |  | 
408  |  |     /*  | 
409  |  |      * We're only interested in the NIDs that correspond to the MDs, so the  | 
410  |  |      * exact propquery is unimportant in the EVP_MD_fetch() calls below.  | 
411  |  |      */  | 
412  |  |  | 
413  | 4.54k  |     if (param_md != NULL) { | 
414  | 0  |         const char *mdname = NULL;  | 
415  |  | 
  | 
416  | 0  |         if (param_md->data_type == OSSL_PARAM_UTF8_STRING)  | 
417  | 0  |             mdname = param_md->data;  | 
418  | 0  |         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))  | 
419  | 0  |             goto err;  | 
420  |  |  | 
421  | 0  |         if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL  | 
422  | 0  |             || !ossl_rsa_pss_params_30_set_hashalg(pss_params,  | 
423  | 0  |                                                    ossl_rsa_oaeppss_md2nid(md)))  | 
424  | 0  |             goto err;  | 
425  | 0  |     }  | 
426  |  |  | 
427  | 4.54k  |     if (param_mgf1md != NULL) { | 
428  | 0  |         const char *mgf1mdname = NULL;  | 
429  |  | 
  | 
430  | 0  |         if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)  | 
431  | 0  |             mgf1mdname = param_mgf1md->data;  | 
432  | 0  |         else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))  | 
433  | 0  |             goto err;  | 
434  |  |  | 
435  | 0  |         if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL  | 
436  | 0  |             || !ossl_rsa_pss_params_30_set_maskgenhashalg(  | 
437  | 0  |                     pss_params, ossl_rsa_oaeppss_md2nid(mgf1md)))  | 
438  | 0  |             goto err;  | 
439  | 0  |     }  | 
440  |  |  | 
441  | 4.54k  |     if (param_saltlen != NULL) { | 
442  | 0  |         if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)  | 
443  | 0  |             || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen))  | 
444  | 0  |             goto err;  | 
445  | 0  |     }  | 
446  |  |  | 
447  | 4.54k  |     ret = 1;  | 
448  |  |  | 
449  | 4.54k  |  err:  | 
450  | 4.54k  |     EVP_MD_free(md);  | 
451  | 4.54k  |     EVP_MD_free(mgf1md);  | 
452  | 4.54k  |     return ret;  | 
453  | 4.54k  | }  | 
454  |  |  | 
455  |  | int ossl_rsa_is_foreign(const RSA *rsa)  | 
456  | 128k  | { | 
457  | 128k  | #ifndef FIPS_MODULE  | 
458  | 128k  |     if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())  | 
459  | 0  |         return 1;  | 
460  | 128k  | #endif  | 
461  | 128k  |     return 0;  | 
462  | 128k  | }  | 
463  |  |  | 
464  |  | static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)  | 
465  | 95.9k  | { | 
466  | 95.9k  |     if (f != NULL && (*out = BN_dup(f)) == NULL)  | 
467  | 0  |         return 0;  | 
468  | 95.9k  |     return 1;  | 
469  | 95.9k  | }  | 
470  |  |  | 
471  |  | RSA *ossl_rsa_dup(const RSA *rsa, int selection)  | 
472  | 1.33k  | { | 
473  | 1.33k  |     RSA *dupkey = NULL;  | 
474  | 1.33k  | #ifndef FIPS_MODULE  | 
475  | 1.33k  |     int pnum, i;  | 
476  | 1.33k  | #endif  | 
477  |  |  | 
478  |  |     /* Do not try to duplicate foreign RSA keys */  | 
479  | 1.33k  |     if (ossl_rsa_is_foreign(rsa))  | 
480  | 0  |         return NULL;  | 
481  |  |  | 
482  | 1.33k  |     if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)  | 
483  | 0  |         return NULL;  | 
484  |  |  | 
485  |  |     /* public key */  | 
486  | 1.33k  |     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { | 
487  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->n, rsa->n))  | 
488  | 0  |             goto err;  | 
489  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->e, rsa->e))  | 
490  | 0  |             goto err;  | 
491  | 1.33k  |     }  | 
492  |  |  | 
493  | 1.33k  |     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { | 
494  |  |  | 
495  |  |         /* private key */  | 
496  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->d, rsa->d))  | 
497  | 0  |             goto err;  | 
498  |  |  | 
499  |  |         /* factors and crt params */  | 
500  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->p, rsa->p))  | 
501  | 0  |             goto err;  | 
502  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->q, rsa->q))  | 
503  | 0  |             goto err;  | 
504  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))  | 
505  | 0  |             goto err;  | 
506  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))  | 
507  | 0  |             goto err;  | 
508  | 1.33k  |         if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))  | 
509  | 0  |             goto err;  | 
510  | 1.33k  |     }  | 
511  |  |  | 
512  | 1.33k  |     dupkey->version = rsa->version;  | 
513  | 1.33k  |     dupkey->flags = rsa->flags;  | 
514  |  |     /* we always copy the PSS parameters regardless of selection */  | 
515  | 1.33k  |     dupkey->pss_params = rsa->pss_params;  | 
516  |  |  | 
517  | 1.33k  | #ifndef FIPS_MODULE  | 
518  |  |     /* multiprime */  | 
519  | 1.33k  |     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0  | 
520  | 1.33k  |         && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) { | 
521  | 198  |         dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);  | 
522  | 198  |         if (dupkey->prime_infos == NULL)  | 
523  | 0  |             goto err;  | 
524  | 28.6k  |         for (i = 0; i < pnum; i++) { | 
525  | 28.4k  |             const RSA_PRIME_INFO *pinfo = NULL;  | 
526  | 28.4k  |             RSA_PRIME_INFO *duppinfo = NULL;  | 
527  |  |  | 
528  | 28.4k  |             if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL)  | 
529  | 0  |                 goto err;  | 
530  |  |             /* push first so cleanup in error case works */  | 
531  | 28.4k  |             (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);  | 
532  |  |  | 
533  | 28.4k  |             pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);  | 
534  | 28.4k  |             if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))  | 
535  | 0  |                 goto err;  | 
536  | 28.4k  |             if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))  | 
537  | 0  |                 goto err;  | 
538  | 28.4k  |             if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))  | 
539  | 0  |                 goto err;  | 
540  | 28.4k  |         }  | 
541  | 198  |         if (!ossl_rsa_multip_calc_product(dupkey))  | 
542  | 0  |             goto err;  | 
543  | 198  |     }  | 
544  |  |  | 
545  | 1.33k  |     if (rsa->pss != NULL) { | 
546  | 4  |         dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);  | 
547  | 4  |         if (rsa->pss->maskGenAlgorithm != NULL  | 
548  | 4  |             && dupkey->pss->maskGenAlgorithm == NULL) { | 
549  | 0  |             dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);  | 
550  | 0  |             if (dupkey->pss->maskHash == NULL)  | 
551  | 0  |                 goto err;  | 
552  | 0  |         }  | 
553  | 4  |     }  | 
554  | 1.33k  |     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,  | 
555  | 1.33k  |                             &dupkey->ex_data, &rsa->ex_data))  | 
556  | 0  |         goto err;  | 
557  | 1.33k  | #endif  | 
558  |  |  | 
559  | 1.33k  |     return dupkey;  | 
560  |  |  | 
561  | 0  |  err:  | 
562  | 0  |     RSA_free(dupkey);  | 
563  | 0  |     return NULL;  | 
564  | 1.33k  | }  | 
565  |  |  | 
566  |  | #ifndef FIPS_MODULE  | 
567  |  | RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)  | 
568  | 31.6k  | { | 
569  | 31.6k  |     RSA_PSS_PARAMS *pss;  | 
570  |  |  | 
571  | 31.6k  |     pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),  | 
572  | 31.6k  |                                     alg->parameter);  | 
573  |  |  | 
574  | 31.6k  |     if (pss == NULL)  | 
575  | 3.26k  |         return NULL;  | 
576  |  |  | 
577  | 28.3k  |     if (pss->maskGenAlgorithm != NULL) { | 
578  | 532  |         pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);  | 
579  | 532  |         if (pss->maskHash == NULL) { | 
580  | 167  |             RSA_PSS_PARAMS_free(pss);  | 
581  | 167  |             return NULL;  | 
582  | 167  |         }  | 
583  | 532  |     }  | 
584  |  |  | 
585  | 28.2k  |     return pss;  | 
586  | 28.3k  | }  | 
587  |  |  | 
588  |  | static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)  | 
589  | 22.9k  | { | 
590  | 22.9k  |     const RSA_PSS_PARAMS *legacy_pss = NULL;  | 
591  | 22.9k  |     RSA_PSS_PARAMS_30 *pss = NULL;  | 
592  |  |  | 
593  | 22.9k  |     if (rsa != NULL  | 
594  | 22.9k  |         && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL  | 
595  | 22.9k  |         && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) { | 
596  | 22.9k  |         const EVP_MD *md = NULL, *mgf1md = NULL;  | 
597  | 22.9k  |         int md_nid, mgf1md_nid, saltlen, trailerField;  | 
598  | 22.9k  |         RSA_PSS_PARAMS_30 pss_params;  | 
599  |  |  | 
600  |  |         /*  | 
601  |  |          * We don't care about the validity of the fields here, we just  | 
602  |  |          * want to synchronise values.  Verifying here makes it impossible  | 
603  |  |          * to even read a key with invalid values, making it hard to test  | 
604  |  |          * a bad situation.  | 
605  |  |          *  | 
606  |  |          * Other routines use ossl_rsa_pss_get_param(), so the values will  | 
607  |  |          * be checked, eventually.  | 
608  |  |          */  | 
609  | 22.9k  |         if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,  | 
610  | 22.9k  |                                                &saltlen, &trailerField))  | 
611  | 0  |             return 0;  | 
612  | 22.9k  |         md_nid = EVP_MD_get_type(md);  | 
613  | 22.9k  |         mgf1md_nid = EVP_MD_get_type(mgf1md);  | 
614  | 22.9k  |         if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)  | 
615  | 22.9k  |             || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)  | 
616  | 22.9k  |             || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,  | 
617  | 22.9k  |                                                           mgf1md_nid)  | 
618  | 22.9k  |             || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)  | 
619  | 22.9k  |             || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,  | 
620  | 22.9k  |                                                         trailerField))  | 
621  | 0  |             return 0;  | 
622  | 22.9k  |         *pss = pss_params;  | 
623  | 22.9k  |     }  | 
624  | 22.9k  |     return 1;  | 
625  | 22.9k  | }  | 
626  |  |  | 
627  |  | int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,  | 
628  |  |                                       const EVP_MD **pmd, const EVP_MD **pmgf1md,  | 
629  |  |                                       int *psaltlen, int *ptrailerField)  | 
630  | 28.0k  | { | 
631  | 28.0k  |     RSA_PSS_PARAMS_30 pss_params;  | 
632  |  |  | 
633  |  |     /* Get the defaults from the ONE place */  | 
634  | 28.0k  |     (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);  | 
635  |  |  | 
636  | 28.0k  |     if (pss == NULL)  | 
637  | 804  |         return 0;  | 
638  | 27.2k  |     *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);  | 
639  | 27.2k  |     if (*pmd == NULL)  | 
640  | 10  |         return 0;  | 
641  | 27.2k  |     *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);  | 
642  | 27.2k  |     if (*pmgf1md == NULL)  | 
643  | 8  |         return 0;  | 
644  | 27.2k  |     if (pss->saltLength)  | 
645  | 37  |         *psaltlen = ASN1_INTEGER_get(pss->saltLength);  | 
646  | 27.2k  |     else  | 
647  | 27.2k  |         *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);  | 
648  | 27.2k  |     if (pss->trailerField)  | 
649  | 7  |         *ptrailerField = ASN1_INTEGER_get(pss->trailerField);  | 
650  | 27.2k  |     else  | 
651  | 27.2k  |         *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);  | 
652  |  |  | 
653  | 27.2k  |     return 1;  | 
654  | 27.2k  | }  | 
655  |  |  | 
656  |  | int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)  | 
657  | 104k  | { | 
658  | 104k  |     RSA_PSS_PARAMS *pss;  | 
659  | 104k  |     const ASN1_OBJECT *algoid;  | 
660  | 104k  |     const void *algp;  | 
661  | 104k  |     int algptype;  | 
662  |  |  | 
663  | 104k  |     X509_ALGOR_get0(&algoid, &algptype, &algp, alg);  | 
664  | 104k  |     if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)  | 
665  | 61.8k  |         return 1;  | 
666  | 42.7k  |     if (algptype == V_ASN1_UNDEF)  | 
667  | 16.7k  |         return 1;  | 
668  | 25.9k  |     if (algptype != V_ASN1_SEQUENCE) { | 
669  | 2.58k  |         ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);  | 
670  | 2.58k  |         return 0;  | 
671  | 2.58k  |     }  | 
672  | 23.3k  |     if ((pss = ossl_rsa_pss_decode(alg)) == NULL  | 
673  | 23.3k  |         || !ossl_rsa_set0_pss_params(rsa, pss)) { | 
674  | 456  |         RSA_PSS_PARAMS_free(pss);  | 
675  | 456  |         return 0;  | 
676  | 456  |     }  | 
677  | 22.9k  |     if (!ossl_rsa_sync_to_pss_params_30(rsa))  | 
678  | 0  |         return 0;  | 
679  | 22.9k  |     return 1;  | 
680  | 22.9k  | }  | 
681  |  |  | 
682  |  | RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,  | 
683  |  |                              OSSL_LIB_CTX *libctx, const char *propq)  | 
684  | 4.63k  | { | 
685  | 4.63k  |     const unsigned char *p;  | 
686  | 4.63k  |     RSA *rsa;  | 
687  | 4.63k  |     int pklen;  | 
688  | 4.63k  |     const X509_ALGOR *alg;  | 
689  |  |  | 
690  | 4.63k  |     if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))  | 
691  | 0  |         return 0;  | 
692  | 4.63k  |     rsa = d2i_RSAPrivateKey(NULL, &p, pklen);  | 
693  | 4.63k  |     if (rsa == NULL) { | 
694  | 51  |         ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);  | 
695  | 51  |         return NULL;  | 
696  | 51  |     }  | 
697  | 4.58k  |     if (!ossl_rsa_param_decode(rsa, alg)) { | 
698  | 15  |         RSA_free(rsa);  | 
699  | 15  |         return NULL;  | 
700  | 15  |     }  | 
701  |  |  | 
702  | 4.56k  |     RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);  | 
703  | 4.56k  |     switch (OBJ_obj2nid(alg->algorithm)) { | 
704  | 4.55k  |     case EVP_PKEY_RSA:  | 
705  | 4.55k  |         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);  | 
706  | 4.55k  |         break;  | 
707  | 12  |     case EVP_PKEY_RSA_PSS:  | 
708  | 12  |         RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);  | 
709  | 12  |         break;  | 
710  | 0  |     default:  | 
711  |  |         /* Leave the type bits zero */  | 
712  | 0  |         break;  | 
713  | 4.56k  |     }  | 
714  |  |  | 
715  | 4.56k  |     return rsa;  | 
716  | 4.56k  | }  | 
717  |  | #endif  |