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