]>
Commit | Line | Data |
---|---|---|
0abae163 RL |
1 | /* |
2 | * Copyright 2020 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 | ||
d7e498ac RL |
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 | ||
15671090 | 16 | #include <string.h> |
0abae163 RL |
17 | #include <openssl/core_names.h> |
18 | #include <openssl/params.h> | |
15671090 RL |
19 | #include <openssl/evp.h> |
20 | #include "internal/sizes.h" | |
21 | #include "internal/param_build_set.h" | |
0abae163 RL |
22 | #include "crypto/rsa.h" |
23 | ||
15671090 RL |
24 | #include "e_os.h" /* strcasecmp for Windows() */ |
25 | ||
0abae163 RL |
26 | /* |
27 | * The intention with the "backend" source file is to offer backend support | |
28 | * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider | |
29 | * implementations alike. | |
30 | */ | |
31 | ||
32 | DEFINE_STACK_OF(BIGNUM) | |
33 | ||
34 | static int collect_numbers(STACK_OF(BIGNUM) *numbers, | |
96ebe52e | 35 | const OSSL_PARAM params[], const char *names[]) |
0abae163 RL |
36 | { |
37 | const OSSL_PARAM *p = NULL; | |
96ebe52e | 38 | int i; |
0abae163 RL |
39 | |
40 | if (numbers == NULL) | |
41 | return 0; | |
42 | ||
96ebe52e SL |
43 | for (i = 0; names[i] != NULL; i++){ |
44 | p = OSSL_PARAM_locate_const(params, names[i]); | |
45 | if (p != NULL) { | |
46 | BIGNUM *tmp = NULL; | |
0abae163 | 47 | |
96ebe52e SL |
48 | if (!OSSL_PARAM_get_BN(p, &tmp) |
49 | || sk_BIGNUM_push(numbers, tmp) == 0) | |
50 | return 0; | |
51 | } | |
0abae163 RL |
52 | } |
53 | ||
54 | return 1; | |
55 | } | |
56 | ||
23b2fc0b | 57 | int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[]) |
0abae163 RL |
58 | { |
59 | const OSSL_PARAM *param_n, *param_e, *param_d; | |
60 | BIGNUM *n = NULL, *e = NULL, *d = NULL; | |
61 | STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL; | |
62 | int is_private = 0; | |
63 | ||
64 | if (rsa == NULL) | |
65 | return 0; | |
66 | ||
67 | param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N); | |
68 | param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E); | |
69 | param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D); | |
70 | ||
71 | if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n)) | |
72 | || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e)) | |
73 | || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d))) | |
74 | goto err; | |
75 | ||
76 | is_private = (d != NULL); | |
77 | ||
78 | if (!RSA_set0_key(rsa, n, e, d)) | |
79 | goto err; | |
80 | n = e = d = NULL; | |
81 | ||
82 | if (is_private) { | |
83 | if (!collect_numbers(factors = sk_BIGNUM_new_null(), params, | |
23b2fc0b | 84 | ossl_rsa_mp_factor_names) |
0abae163 | 85 | || !collect_numbers(exps = sk_BIGNUM_new_null(), params, |
23b2fc0b | 86 | ossl_rsa_mp_exp_names) |
0abae163 | 87 | || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params, |
23b2fc0b | 88 | ossl_rsa_mp_coeff_names)) |
0abae163 RL |
89 | goto err; |
90 | ||
91 | /* It's ok if this private key just has n, e and d */ | |
92 | if (sk_BIGNUM_num(factors) != 0 | |
23b2fc0b | 93 | && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs)) |
0abae163 RL |
94 | goto err; |
95 | } | |
96 | ||
4f2271d5 | 97 | |
0abae163 RL |
98 | sk_BIGNUM_free(factors); |
99 | sk_BIGNUM_free(exps); | |
100 | sk_BIGNUM_free(coeffs); | |
101 | return 1; | |
102 | ||
103 | err: | |
104 | BN_free(n); | |
105 | BN_free(e); | |
106 | BN_free(d); | |
107 | sk_BIGNUM_pop_free(factors, BN_free); | |
108 | sk_BIGNUM_pop_free(exps, BN_free); | |
109 | sk_BIGNUM_pop_free(coeffs, BN_free); | |
110 | return 0; | |
111 | } | |
112 | ||
645a541a RL |
113 | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) |
114 | ||
23b2fc0b | 115 | int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[]) |
645a541a RL |
116 | { |
117 | int ret = 0; | |
118 | const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL; | |
119 | STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null(); | |
120 | STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null(); | |
121 | STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null(); | |
122 | ||
123 | if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL) | |
124 | goto err; | |
125 | ||
126 | RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d); | |
23b2fc0b | 127 | ossl_rsa_get0_all_params(rsa, factors, exps, coeffs); |
645a541a | 128 | |
26a8f2ac RL |
129 | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n) |
130 | || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)) | |
131 | goto err; | |
132 | ||
645a541a RL |
133 | /* Check private key data integrity */ |
134 | if (rsa_d != NULL) { | |
135 | int numprimes = sk_BIGNUM_const_num(factors); | |
136 | int numexps = sk_BIGNUM_const_num(exps); | |
137 | int numcoeffs = sk_BIGNUM_const_num(coeffs); | |
138 | ||
139 | /* | |
4f2271d5 | 140 | * It's permissible to have zero primes, i.e. no CRT params. |
645a541a RL |
141 | * Otherwise, there must be at least two, as many exponents, |
142 | * and one coefficient less. | |
143 | */ | |
144 | if (numprimes != 0 | |
145 | && (numprimes < 2 || numexps < 2 || numcoeffs < 1)) | |
146 | goto err; | |
645a541a | 147 | |
26a8f2ac RL |
148 | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, |
149 | rsa_d) | |
150 | || !ossl_param_build_set_multi_key_bn(bld, params, | |
23b2fc0b P |
151 | ossl_rsa_mp_factor_names, |
152 | factors) | |
26a8f2ac | 153 | || !ossl_param_build_set_multi_key_bn(bld, params, |
23b2fc0b | 154 | ossl_rsa_mp_exp_names, exps) |
26a8f2ac | 155 | || !ossl_param_build_set_multi_key_bn(bld, params, |
23b2fc0b P |
156 | ossl_rsa_mp_coeff_names, |
157 | coeffs)) | |
645a541a | 158 | goto err; |
26a8f2ac RL |
159 | } |
160 | ||
4f2271d5 SL |
161 | #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) |
162 | /* The acvp test results are not meant for export so check for bld == NULL */ | |
163 | if (bld == NULL) | |
164 | rsa_acvp_test_get_params(rsa, params); | |
165 | #endif | |
645a541a RL |
166 | ret = 1; |
167 | err: | |
168 | sk_BIGNUM_const_free(factors); | |
169 | sk_BIGNUM_const_free(exps); | |
170 | sk_BIGNUM_const_free(coeffs); | |
171 | return ret; | |
172 | } | |
15671090 | 173 | |
23b2fc0b P |
174 | int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss, |
175 | OSSL_PARAM_BLD *bld, OSSL_PARAM params[]) | |
15671090 | 176 | { |
23b2fc0b P |
177 | if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) { |
178 | int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss); | |
179 | int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss); | |
180 | int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss); | |
181 | int saltlen = ossl_rsa_pss_params_30_saltlen(pss); | |
182 | int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL); | |
183 | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); | |
184 | int default_maskgenhashalg_nid = | |
185 | ossl_rsa_pss_params_30_maskgenhashalg(NULL); | |
15671090 RL |
186 | const char *mdname = |
187 | (hashalg_nid == default_hashalg_nid | |
23b2fc0b | 188 | ? NULL : ossl_rsa_oaeppss_nid2name(hashalg_nid)); |
15671090 RL |
189 | const char *mgfname = |
190 | (maskgenalg_nid == default_maskgenalg_nid | |
23b2fc0b | 191 | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenalg_nid)); |
15671090 RL |
192 | const char *mgf1mdname = |
193 | (maskgenhashalg_nid == default_maskgenhashalg_nid | |
23b2fc0b | 194 | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid)); |
15671090 RL |
195 | const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST; |
196 | const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC; | |
197 | const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST; | |
198 | const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN; | |
199 | ||
200 | /* | |
201 | * To ensure that the key isn't seen as unrestricted by the recipient, | |
202 | * we make sure that at least one PSS-related parameter is passed, even | |
203 | * if it has a default value; saltlen. | |
204 | */ | |
205 | if ((mdname != NULL | |
206 | && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname)) | |
207 | || (mgfname != NULL | |
208 | && !ossl_param_build_set_utf8_string(bld, params, | |
209 | key_mgf, mgfname)) | |
210 | || (mgf1mdname != NULL | |
211 | && !ossl_param_build_set_utf8_string(bld, params, | |
212 | key_mgf1_md, mgf1mdname)) | |
213 | || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen))) | |
214 | return 0; | |
215 | } | |
216 | return 1; | |
217 | } | |
218 | ||
23b2fc0b P |
219 | int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params, |
220 | const OSSL_PARAM params[], | |
b4250010 | 221 | OSSL_LIB_CTX *libctx) |
15671090 RL |
222 | { |
223 | const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen; | |
719523c7 SL |
224 | const OSSL_PARAM *param_propq; |
225 | const char *propq = NULL; | |
15671090 RL |
226 | EVP_MD *md = NULL, *mgf1md = NULL; |
227 | int saltlen; | |
228 | int ret = 0; | |
229 | ||
230 | if (pss_params == NULL) | |
231 | return 0; | |
719523c7 SL |
232 | param_propq = |
233 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS); | |
15671090 RL |
234 | param_md = |
235 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST); | |
236 | param_mgf = | |
237 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC); | |
238 | param_mgf1md = | |
239 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST); | |
240 | param_saltlen = | |
241 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN); | |
242 | ||
719523c7 SL |
243 | if (param_propq != NULL) { |
244 | if (param_propq->data_type == OSSL_PARAM_UTF8_STRING) | |
245 | propq = param_propq->data; | |
246 | } | |
15671090 RL |
247 | /* |
248 | * If we get any of the parameters, we know we have at least some | |
249 | * restrictions, so we start by setting default values, and let each | |
250 | * parameter override their specific restriction data. | |
251 | */ | |
252 | if (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL | |
253 | || param_saltlen != NULL) | |
23b2fc0b | 254 | if (!ossl_rsa_pss_params_30_set_defaults(pss_params)) |
15671090 RL |
255 | return 0; |
256 | ||
257 | if (param_mgf != NULL) { | |
23b2fc0b | 258 | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); |
15671090 RL |
259 | const char *mgfname = NULL; |
260 | ||
261 | if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING) | |
262 | mgfname = param_mgf->data; | |
263 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname)) | |
264 | return 0; | |
265 | ||
266 | /* TODO Revisit this if / when a new MGF algorithm appears */ | |
267 | if (strcasecmp(param_mgf->data, | |
23b2fc0b | 268 | ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0) |
15671090 RL |
269 | return 0; |
270 | } | |
271 | ||
272 | /* | |
273 | * We're only interested in the NIDs that correspond to the MDs, so the | |
274 | * exact propquery is unimportant in the EVP_MD_fetch() calls below. | |
275 | */ | |
276 | ||
277 | if (param_md != NULL) { | |
278 | const char *mdname = NULL; | |
279 | ||
280 | if (param_md->data_type == OSSL_PARAM_UTF8_STRING) | |
281 | mdname = param_md->data; | |
282 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname)) | |
283 | goto err; | |
284 | ||
719523c7 | 285 | if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL |
23b2fc0b P |
286 | || !ossl_rsa_pss_params_30_set_hashalg(pss_params, |
287 | ossl_rsa_oaeppss_md2nid(md))) | |
15671090 RL |
288 | goto err; |
289 | } | |
290 | ||
291 | if (param_mgf1md != NULL) { | |
292 | const char *mgf1mdname = NULL; | |
293 | ||
294 | if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING) | |
295 | mgf1mdname = param_mgf1md->data; | |
296 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname)) | |
297 | goto err; | |
298 | ||
719523c7 | 299 | if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL |
23b2fc0b P |
300 | || !ossl_rsa_pss_params_30_set_maskgenhashalg( |
301 | pss_params, ossl_rsa_oaeppss_md2nid(mgf1md))) | |
15671090 RL |
302 | goto err; |
303 | } | |
304 | ||
305 | if (param_saltlen != NULL) { | |
306 | if (!OSSL_PARAM_get_int(param_saltlen, &saltlen) | |
23b2fc0b | 307 | || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen)) |
15671090 RL |
308 | goto err; |
309 | } | |
310 | ||
311 | ret = 1; | |
312 | ||
313 | err: | |
314 | EVP_MD_free(md); | |
315 | EVP_MD_free(mgf1md); | |
316 | return ret; | |
317 | } |