/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 | 27.3k | { |
41 | 27.3k | const OSSL_PARAM *p = NULL; |
42 | 27.3k | int i; |
43 | | |
44 | 27.3k | if (numbers == NULL) |
45 | 0 | return 0; |
46 | | |
47 | 292k | for (i = 0; names[i] != NULL; i++) { |
48 | 264k | p = OSSL_PARAM_locate_const(params, names[i]); |
49 | 264k | if (p != NULL) { |
50 | 45.6k | BIGNUM *tmp = NULL; |
51 | | |
52 | 45.6k | if (!OSSL_PARAM_get_BN(p, &tmp)) |
53 | 0 | return 0; |
54 | 45.6k | if (sk_BIGNUM_push(numbers, tmp) == 0) { |
55 | 0 | BN_clear_free(tmp); |
56 | 0 | return 0; |
57 | 0 | } |
58 | 45.6k | } |
59 | 264k | } |
60 | | |
61 | 27.3k | return 1; |
62 | 27.3k | } |
63 | | |
64 | | int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private) |
65 | 9.12k | { |
66 | 9.12k | const OSSL_PARAM *param_n, *param_e, *param_d = NULL; |
67 | 9.12k | const OSSL_PARAM *param_p, *param_q = NULL; |
68 | 9.12k | const OSSL_PARAM *param_derive = NULL; |
69 | 9.12k | BIGNUM *p = NULL, *q = NULL, *n = NULL, *e = NULL, *d = NULL; |
70 | 9.12k | STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL; |
71 | 9.12k | int is_private = 0; |
72 | 9.12k | int derive_from_pq = 0; |
73 | 9.12k | BN_CTX *ctx = NULL; |
74 | | |
75 | 9.12k | if (rsa == NULL) |
76 | 0 | return 0; |
77 | | |
78 | 9.12k | param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N); |
79 | 9.12k | param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E); |
80 | | |
81 | 9.12k | if ((param_n == NULL || !OSSL_PARAM_get_BN(param_n, &n)) |
82 | 9.12k | || (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 | 9.12k | if (include_private) { |
88 | | |
89 | 9.12k | param_derive = OSSL_PARAM_locate_const(params, |
90 | 9.12k | OSSL_PKEY_PARAM_RSA_DERIVE_FROM_PQ); |
91 | 9.12k | if ((param_derive != NULL) |
92 | 9.12k | && !OSSL_PARAM_get_int(param_derive, &derive_from_pq)) |
93 | 0 | goto err; |
94 | | |
95 | 9.12k | param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D); |
96 | 9.12k | 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 | 9.12k | 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 | 9.12k | } |
117 | | |
118 | 9.12k | is_private = (d != NULL); |
119 | | |
120 | 9.12k | if (!RSA_set0_key(rsa, n, e, d)) |
121 | 0 | goto err; |
122 | 9.12k | n = e = d = NULL; |
123 | | |
124 | 9.12k | if (is_private) { |
125 | 9.12k | if (!collect_numbers(factors = sk_BIGNUM_new_null(), params, |
126 | 9.12k | ossl_rsa_mp_factor_names) |
127 | 9.12k | || !collect_numbers(exps = sk_BIGNUM_new_null(), params, |
128 | 9.12k | ossl_rsa_mp_exp_names) |
129 | 9.12k | || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params, |
130 | 9.12k | ossl_rsa_mp_coeff_names)) |
131 | 0 | goto err; |
132 | | |
133 | 9.12k | if (derive_from_pq && sk_BIGNUM_num(exps) == 0 |
134 | 9.12k | && 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 | 9.12k | } 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 | 9.12k | if (sk_BIGNUM_num(factors) != 0 |
217 | 9.12k | && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs)) |
218 | 0 | goto err; |
219 | 9.12k | } |
220 | | /* sanity check to ensure we used everything in our stacks */ |
221 | 9.12k | if (sk_BIGNUM_num(factors) != 0 |
222 | 9.12k | || sk_BIGNUM_num(exps) != 0 |
223 | 9.12k | || 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 | 9.12k | } |
231 | | |
232 | 9.12k | BN_clear_free(p); |
233 | 9.12k | BN_clear_free(q); |
234 | 9.12k | sk_BIGNUM_free(factors); |
235 | 9.12k | sk_BIGNUM_free(exps); |
236 | 9.12k | sk_BIGNUM_free(coeffs); |
237 | 9.12k | BN_CTX_free(ctx); |
238 | 9.12k | return 1; |
239 | | |
240 | 0 | err: |
241 | 0 | BN_free(n); |
242 | 0 | BN_free(e); |
243 | 0 | BN_free(d); |
244 | 0 | sk_BIGNUM_pop_free(factors, BN_clear_free); |
245 | 0 | sk_BIGNUM_pop_free(exps, BN_clear_free); |
246 | 0 | sk_BIGNUM_pop_free(coeffs, BN_clear_free); |
247 | 0 | BN_CTX_free(ctx); |
248 | 0 | return 0; |
249 | 9.12k | } |
250 | | |
251 | | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) |
252 | | |
253 | | int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[], |
254 | | int include_private) |
255 | 18.2k | { |
256 | 18.2k | int ret = 0; |
257 | 18.2k | const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL; |
258 | 18.2k | STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null(); |
259 | 18.2k | STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null(); |
260 | 18.2k | STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null(); |
261 | | |
262 | 18.2k | if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL) |
263 | 0 | goto err; |
264 | | |
265 | 18.2k | RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d); |
266 | 18.2k | ossl_rsa_get0_all_params(rsa, factors, exps, coeffs); |
267 | | |
268 | 18.2k | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n) |
269 | 18.2k | || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)) |
270 | 0 | goto err; |
271 | | |
272 | | /* Check private key data integrity */ |
273 | 18.2k | if (include_private && rsa_d != NULL) { |
274 | | |
275 | 13.6k | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, |
276 | 13.6k | rsa_d) |
277 | 13.6k | || !ossl_param_build_set_multi_key_bn(bld, params, |
278 | 13.6k | ossl_rsa_mp_factor_names, |
279 | 13.6k | factors) |
280 | 13.6k | || !ossl_param_build_set_multi_key_bn(bld, params, |
281 | 13.6k | ossl_rsa_mp_exp_names, exps) |
282 | 13.6k | || !ossl_param_build_set_multi_key_bn(bld, params, |
283 | 13.6k | ossl_rsa_mp_coeff_names, |
284 | 13.6k | coeffs)) |
285 | 0 | goto err; |
286 | 13.6k | } |
287 | | |
288 | | #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) |
289 | | /* The acvp test results are not meant for export so check for bld == NULL */ |
290 | | if (bld == NULL) |
291 | | ossl_rsa_acvp_test_get_params(rsa, params); |
292 | | #endif |
293 | 18.2k | ret = 1; |
294 | 18.2k | err: |
295 | 18.2k | sk_BIGNUM_const_free(factors); |
296 | 18.2k | sk_BIGNUM_const_free(exps); |
297 | 18.2k | sk_BIGNUM_const_free(coeffs); |
298 | 18.2k | return ret; |
299 | 18.2k | } |
300 | | |
301 | | int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss, |
302 | | OSSL_PARAM_BLD *bld, OSSL_PARAM params[]) |
303 | 0 | { |
304 | 0 | if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) { |
305 | 0 | int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss); |
306 | 0 | int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss); |
307 | 0 | int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss); |
308 | 0 | int saltlen = ossl_rsa_pss_params_30_saltlen(pss); |
309 | 0 | int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL); |
310 | 0 | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); |
311 | 0 | int default_maskgenhashalg_nid = |
312 | 0 | ossl_rsa_pss_params_30_maskgenhashalg(NULL); |
313 | 0 | const char *mdname = |
314 | 0 | (hashalg_nid == default_hashalg_nid |
315 | 0 | ? NULL : ossl_rsa_oaeppss_nid2name(hashalg_nid)); |
316 | 0 | const char *mgfname = |
317 | 0 | (maskgenalg_nid == default_maskgenalg_nid |
318 | 0 | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenalg_nid)); |
319 | 0 | const char *mgf1mdname = |
320 | 0 | (maskgenhashalg_nid == default_maskgenhashalg_nid |
321 | 0 | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid)); |
322 | 0 | const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST; |
323 | 0 | const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC; |
324 | 0 | const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST; |
325 | 0 | const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN; |
326 | | |
327 | | /* |
328 | | * To ensure that the key isn't seen as unrestricted by the recipient, |
329 | | * we make sure that at least one PSS-related parameter is passed, even |
330 | | * if it has a default value; saltlen. |
331 | | */ |
332 | 0 | if ((mdname != NULL |
333 | 0 | && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname)) |
334 | 0 | || (mgfname != NULL |
335 | 0 | && !ossl_param_build_set_utf8_string(bld, params, |
336 | 0 | key_mgf, mgfname)) |
337 | 0 | || (mgf1mdname != NULL |
338 | 0 | && !ossl_param_build_set_utf8_string(bld, params, |
339 | 0 | key_mgf1_md, mgf1mdname)) |
340 | 0 | || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen))) |
341 | 0 | return 0; |
342 | 0 | } |
343 | 0 | return 1; |
344 | 0 | } |
345 | | |
346 | | int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params, |
347 | | int *defaults_set, |
348 | | const OSSL_PARAM params[], |
349 | | OSSL_LIB_CTX *libctx) |
350 | 4.56k | { |
351 | 4.56k | const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen; |
352 | 4.56k | const OSSL_PARAM *param_propq; |
353 | 4.56k | const char *propq = NULL; |
354 | 4.56k | EVP_MD *md = NULL, *mgf1md = NULL; |
355 | 4.56k | int saltlen; |
356 | 4.56k | int ret = 0; |
357 | | |
358 | 4.56k | if (pss_params == NULL) |
359 | 0 | return 0; |
360 | 4.56k | param_propq = |
361 | 4.56k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS); |
362 | 4.56k | param_md = |
363 | 4.56k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST); |
364 | 4.56k | param_mgf = |
365 | 4.56k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC); |
366 | 4.56k | param_mgf1md = |
367 | 4.56k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST); |
368 | 4.56k | param_saltlen = |
369 | 4.56k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN); |
370 | | |
371 | 4.56k | if (param_propq != NULL) { |
372 | 0 | if (param_propq->data_type == OSSL_PARAM_UTF8_STRING) |
373 | 0 | propq = param_propq->data; |
374 | 0 | } |
375 | | /* |
376 | | * If we get any of the parameters, we know we have at least some |
377 | | * restrictions, so we start by setting default values, and let each |
378 | | * parameter override their specific restriction data. |
379 | | */ |
380 | 4.56k | if (!*defaults_set |
381 | 4.56k | && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL |
382 | 4.56k | || param_saltlen != NULL)) { |
383 | 0 | if (!ossl_rsa_pss_params_30_set_defaults(pss_params)) |
384 | 0 | return 0; |
385 | 0 | *defaults_set = 1; |
386 | 0 | } |
387 | | |
388 | 4.56k | if (param_mgf != NULL) { |
389 | 0 | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); |
390 | 0 | const char *mgfname = NULL; |
391 | |
|
392 | 0 | if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING) |
393 | 0 | mgfname = param_mgf->data; |
394 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname)) |
395 | 0 | return 0; |
396 | | |
397 | 0 | if (OPENSSL_strcasecmp(param_mgf->data, |
398 | 0 | ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0) |
399 | 0 | return 0; |
400 | 0 | } |
401 | | |
402 | | /* |
403 | | * We're only interested in the NIDs that correspond to the MDs, so the |
404 | | * exact propquery is unimportant in the EVP_MD_fetch() calls below. |
405 | | */ |
406 | | |
407 | 4.56k | if (param_md != NULL) { |
408 | 0 | const char *mdname = NULL; |
409 | |
|
410 | 0 | if (param_md->data_type == OSSL_PARAM_UTF8_STRING) |
411 | 0 | mdname = param_md->data; |
412 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname)) |
413 | 0 | goto err; |
414 | | |
415 | 0 | if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL |
416 | 0 | || !ossl_rsa_pss_params_30_set_hashalg(pss_params, |
417 | 0 | ossl_rsa_oaeppss_md2nid(md))) |
418 | 0 | goto err; |
419 | 0 | } |
420 | | |
421 | 4.56k | if (param_mgf1md != NULL) { |
422 | 0 | const char *mgf1mdname = NULL; |
423 | |
|
424 | 0 | if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING) |
425 | 0 | mgf1mdname = param_mgf1md->data; |
426 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname)) |
427 | 0 | goto err; |
428 | | |
429 | 0 | if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL |
430 | 0 | || !ossl_rsa_pss_params_30_set_maskgenhashalg( |
431 | 0 | pss_params, ossl_rsa_oaeppss_md2nid(mgf1md))) |
432 | 0 | goto err; |
433 | 0 | } |
434 | | |
435 | 4.56k | if (param_saltlen != NULL) { |
436 | 0 | if (!OSSL_PARAM_get_int(param_saltlen, &saltlen) |
437 | 0 | || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen)) |
438 | 0 | goto err; |
439 | 0 | } |
440 | | |
441 | 4.56k | ret = 1; |
442 | | |
443 | 4.56k | err: |
444 | 4.56k | EVP_MD_free(md); |
445 | 4.56k | EVP_MD_free(mgf1md); |
446 | 4.56k | return ret; |
447 | 4.56k | } |
448 | | |
449 | | int ossl_rsa_is_foreign(const RSA *rsa) |
450 | 13.6k | { |
451 | 13.6k | #ifndef FIPS_MODULE |
452 | 13.6k | if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL()) |
453 | 0 | return 1; |
454 | 13.6k | #endif |
455 | 13.6k | return 0; |
456 | 13.6k | } |
457 | | |
458 | | static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f) |
459 | 0 | { |
460 | 0 | if (f != NULL && (*out = BN_dup(f)) == NULL) |
461 | 0 | return 0; |
462 | 0 | return 1; |
463 | 0 | } |
464 | | |
465 | | RSA *ossl_rsa_dup(const RSA *rsa, int selection) |
466 | 0 | { |
467 | 0 | RSA *dupkey = NULL; |
468 | 0 | #ifndef FIPS_MODULE |
469 | 0 | int pnum, i; |
470 | 0 | #endif |
471 | | |
472 | | /* Do not try to duplicate foreign RSA keys */ |
473 | 0 | if (ossl_rsa_is_foreign(rsa)) |
474 | 0 | return NULL; |
475 | | |
476 | 0 | if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL) |
477 | 0 | return NULL; |
478 | | |
479 | | /* public key */ |
480 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
481 | 0 | if (!rsa_bn_dup_check(&dupkey->n, rsa->n)) |
482 | 0 | goto err; |
483 | 0 | if (!rsa_bn_dup_check(&dupkey->e, rsa->e)) |
484 | 0 | goto err; |
485 | 0 | } |
486 | | |
487 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
488 | | |
489 | | /* private key */ |
490 | 0 | if (!rsa_bn_dup_check(&dupkey->d, rsa->d)) |
491 | 0 | goto err; |
492 | | |
493 | | /* factors and crt params */ |
494 | 0 | if (!rsa_bn_dup_check(&dupkey->p, rsa->p)) |
495 | 0 | goto err; |
496 | 0 | if (!rsa_bn_dup_check(&dupkey->q, rsa->q)) |
497 | 0 | goto err; |
498 | 0 | if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1)) |
499 | 0 | goto err; |
500 | 0 | if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1)) |
501 | 0 | goto err; |
502 | 0 | if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp)) |
503 | 0 | goto err; |
504 | 0 | } |
505 | | |
506 | 0 | dupkey->version = rsa->version; |
507 | 0 | dupkey->flags = rsa->flags; |
508 | | /* we always copy the PSS parameters regardless of selection */ |
509 | 0 | dupkey->pss_params = rsa->pss_params; |
510 | |
|
511 | 0 | #ifndef FIPS_MODULE |
512 | | /* multiprime */ |
513 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 |
514 | 0 | && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) { |
515 | 0 | dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); |
516 | 0 | if (dupkey->prime_infos == NULL) |
517 | 0 | goto err; |
518 | 0 | for (i = 0; i < pnum; i++) { |
519 | 0 | const RSA_PRIME_INFO *pinfo = NULL; |
520 | 0 | RSA_PRIME_INFO *duppinfo = NULL; |
521 | |
|
522 | 0 | if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL) |
523 | 0 | goto err; |
524 | | /* push first so cleanup in error case works */ |
525 | 0 | (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo); |
526 | |
|
527 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
528 | 0 | if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r)) |
529 | 0 | goto err; |
530 | 0 | if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d)) |
531 | 0 | goto err; |
532 | 0 | if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t)) |
533 | 0 | goto err; |
534 | 0 | } |
535 | 0 | if (!ossl_rsa_multip_calc_product(dupkey)) |
536 | 0 | goto err; |
537 | 0 | } |
538 | | |
539 | 0 | if (rsa->pss != NULL) { |
540 | 0 | dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss); |
541 | 0 | if (rsa->pss->maskGenAlgorithm != NULL |
542 | 0 | && dupkey->pss->maskGenAlgorithm == NULL) { |
543 | 0 | dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm); |
544 | 0 | if (dupkey->pss->maskHash == NULL) |
545 | 0 | goto err; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, |
549 | 0 | &dupkey->ex_data, &rsa->ex_data)) |
550 | 0 | goto err; |
551 | 0 | #endif |
552 | | |
553 | 0 | return dupkey; |
554 | | |
555 | 0 | err: |
556 | 0 | RSA_free(dupkey); |
557 | 0 | return NULL; |
558 | 0 | } |
559 | | |
560 | | #ifndef FIPS_MODULE |
561 | | RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg) |
562 | 0 | { |
563 | 0 | RSA_PSS_PARAMS *pss; |
564 | |
|
565 | 0 | pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS), |
566 | 0 | alg->parameter); |
567 | |
|
568 | 0 | if (pss == NULL) |
569 | 0 | return NULL; |
570 | | |
571 | 0 | if (pss->maskGenAlgorithm != NULL) { |
572 | 0 | pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm); |
573 | 0 | if (pss->maskHash == NULL) { |
574 | 0 | RSA_PSS_PARAMS_free(pss); |
575 | 0 | return NULL; |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | 0 | return pss; |
580 | 0 | } |
581 | | |
582 | | static int ossl_rsa_sync_to_pss_params_30(RSA *rsa) |
583 | 0 | { |
584 | 0 | const RSA_PSS_PARAMS *legacy_pss = NULL; |
585 | 0 | RSA_PSS_PARAMS_30 *pss = NULL; |
586 | |
|
587 | 0 | if (rsa != NULL |
588 | 0 | && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL |
589 | 0 | && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) { |
590 | 0 | const EVP_MD *md = NULL, *mgf1md = NULL; |
591 | 0 | int md_nid, mgf1md_nid, saltlen, trailerField; |
592 | 0 | RSA_PSS_PARAMS_30 pss_params; |
593 | | |
594 | | /* |
595 | | * We don't care about the validity of the fields here, we just |
596 | | * want to synchronise values. Verifying here makes it impossible |
597 | | * to even read a key with invalid values, making it hard to test |
598 | | * a bad situation. |
599 | | * |
600 | | * Other routines use ossl_rsa_pss_get_param(), so the values will |
601 | | * be checked, eventually. |
602 | | */ |
603 | 0 | if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md, |
604 | 0 | &saltlen, &trailerField)) |
605 | 0 | return 0; |
606 | 0 | md_nid = EVP_MD_get_type(md); |
607 | 0 | mgf1md_nid = EVP_MD_get_type(mgf1md); |
608 | 0 | if (!ossl_rsa_pss_params_30_set_defaults(&pss_params) |
609 | 0 | || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid) |
610 | 0 | || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params, |
611 | 0 | mgf1md_nid) |
612 | 0 | || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen) |
613 | 0 | || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params, |
614 | 0 | trailerField)) |
615 | 0 | return 0; |
616 | 0 | *pss = pss_params; |
617 | 0 | } |
618 | 0 | return 1; |
619 | 0 | } |
620 | | |
621 | | int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss, |
622 | | const EVP_MD **pmd, const EVP_MD **pmgf1md, |
623 | | int *psaltlen, int *ptrailerField) |
624 | 0 | { |
625 | 0 | RSA_PSS_PARAMS_30 pss_params; |
626 | | |
627 | | /* Get the defaults from the ONE place */ |
628 | 0 | (void)ossl_rsa_pss_params_30_set_defaults(&pss_params); |
629 | |
|
630 | 0 | if (pss == NULL) |
631 | 0 | return 0; |
632 | 0 | *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm); |
633 | 0 | if (*pmd == NULL) |
634 | 0 | return 0; |
635 | 0 | *pmgf1md = ossl_x509_algor_get_md(pss->maskHash); |
636 | 0 | if (*pmgf1md == NULL) |
637 | 0 | return 0; |
638 | 0 | if (pss->saltLength) |
639 | 0 | *psaltlen = ASN1_INTEGER_get(pss->saltLength); |
640 | 0 | else |
641 | 0 | *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params); |
642 | 0 | if (pss->trailerField) |
643 | 0 | *ptrailerField = ASN1_INTEGER_get(pss->trailerField); |
644 | 0 | else |
645 | 0 | *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params); |
646 | |
|
647 | 0 | return 1; |
648 | 0 | } |
649 | | |
650 | | int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg) |
651 | 9.12k | { |
652 | 9.12k | RSA_PSS_PARAMS *pss; |
653 | 9.12k | const ASN1_OBJECT *algoid; |
654 | 9.12k | const void *algp; |
655 | 9.12k | int algptype; |
656 | | |
657 | 9.12k | X509_ALGOR_get0(&algoid, &algptype, &algp, alg); |
658 | 9.12k | if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS) |
659 | 9.12k | return 1; |
660 | 0 | if (algptype == V_ASN1_UNDEF) |
661 | 0 | return 1; |
662 | 0 | if (algptype != V_ASN1_SEQUENCE) { |
663 | 0 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS); |
664 | 0 | return 0; |
665 | 0 | } |
666 | 0 | if ((pss = ossl_rsa_pss_decode(alg)) == NULL |
667 | 0 | || !ossl_rsa_set0_pss_params(rsa, pss)) { |
668 | 0 | RSA_PSS_PARAMS_free(pss); |
669 | 0 | return 0; |
670 | 0 | } |
671 | 0 | if (!ossl_rsa_sync_to_pss_params_30(rsa)) |
672 | 0 | return 0; |
673 | 0 | return 1; |
674 | 0 | } |
675 | | |
676 | | RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, |
677 | | OSSL_LIB_CTX *libctx, const char *propq) |
678 | 4.56k | { |
679 | 4.56k | const unsigned char *p; |
680 | 4.56k | RSA *rsa; |
681 | 4.56k | int pklen; |
682 | 4.56k | const X509_ALGOR *alg; |
683 | | |
684 | 4.56k | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf)) |
685 | 0 | return 0; |
686 | 4.56k | rsa = d2i_RSAPrivateKey(NULL, &p, pklen); |
687 | 4.56k | if (rsa == NULL) { |
688 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB); |
689 | 0 | return NULL; |
690 | 0 | } |
691 | 4.56k | if (!ossl_rsa_param_decode(rsa, alg)) { |
692 | 0 | RSA_free(rsa); |
693 | 0 | return NULL; |
694 | 0 | } |
695 | | |
696 | 4.56k | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); |
697 | 4.56k | switch (OBJ_obj2nid(alg->algorithm)) { |
698 | 4.56k | case EVP_PKEY_RSA: |
699 | 4.56k | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA); |
700 | 4.56k | break; |
701 | 0 | case EVP_PKEY_RSA_PSS: |
702 | 0 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS); |
703 | 0 | break; |
704 | 0 | default: |
705 | | /* Leave the type bits zero */ |
706 | 0 | break; |
707 | 4.56k | } |
708 | | |
709 | 4.56k | return rsa; |
710 | 4.56k | } |
711 | | #endif |