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