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