/src/openssl30/crypto/rsa/rsa_backend.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2020-2022 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 | 0 | { |
41 | 0 | const OSSL_PARAM *p = NULL; |
42 | 0 | int i; |
43 | |
|
44 | 0 | if (numbers == NULL) |
45 | 0 | return 0; |
46 | | |
47 | 0 | for (i = 0; names[i] != NULL; i++){ |
48 | 0 | p = OSSL_PARAM_locate_const(params, names[i]); |
49 | 0 | if (p != NULL) { |
50 | 0 | BIGNUM *tmp = NULL; |
51 | |
|
52 | 0 | if (!OSSL_PARAM_get_BN(p, &tmp)) |
53 | 0 | return 0; |
54 | 0 | if (sk_BIGNUM_push(numbers, tmp) == 0) { |
55 | 0 | BN_clear_free(tmp); |
56 | 0 | return 0; |
57 | 0 | } |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | 0 | return 1; |
62 | 0 | } |
63 | | |
64 | | int ossl_rsa_fromdata(RSA *rsa, const OSSL_PARAM params[], int include_private) |
65 | 0 | { |
66 | 0 | const OSSL_PARAM *param_n, *param_e, *param_d = NULL; |
67 | 0 | BIGNUM *n = NULL, *e = NULL, *d = NULL; |
68 | 0 | STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL; |
69 | 0 | int is_private = 0; |
70 | |
|
71 | 0 | if (rsa == NULL) |
72 | 0 | return 0; |
73 | | |
74 | 0 | param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N); |
75 | 0 | param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E); |
76 | 0 | if (include_private) |
77 | 0 | param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D); |
78 | |
|
79 | 0 | if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n)) |
80 | 0 | || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e)) |
81 | 0 | || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d))) |
82 | 0 | goto err; |
83 | | |
84 | 0 | is_private = (d != NULL); |
85 | |
|
86 | 0 | if (!RSA_set0_key(rsa, n, e, d)) |
87 | 0 | goto err; |
88 | 0 | n = e = d = NULL; |
89 | |
|
90 | 0 | if (is_private) { |
91 | 0 | if (!collect_numbers(factors = sk_BIGNUM_new_null(), params, |
92 | 0 | ossl_rsa_mp_factor_names) |
93 | 0 | || !collect_numbers(exps = sk_BIGNUM_new_null(), params, |
94 | 0 | ossl_rsa_mp_exp_names) |
95 | 0 | || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params, |
96 | 0 | ossl_rsa_mp_coeff_names)) |
97 | 0 | goto err; |
98 | | |
99 | | /* It's ok if this private key just has n, e and d */ |
100 | 0 | if (sk_BIGNUM_num(factors) != 0 |
101 | 0 | && !ossl_rsa_set0_all_params(rsa, factors, exps, coeffs)) |
102 | 0 | goto err; |
103 | 0 | } |
104 | | |
105 | | |
106 | 0 | sk_BIGNUM_free(factors); |
107 | 0 | sk_BIGNUM_free(exps); |
108 | 0 | sk_BIGNUM_free(coeffs); |
109 | 0 | return 1; |
110 | | |
111 | 0 | err: |
112 | 0 | BN_free(n); |
113 | 0 | BN_free(e); |
114 | 0 | BN_free(d); |
115 | 0 | sk_BIGNUM_pop_free(factors, BN_free); |
116 | 0 | sk_BIGNUM_pop_free(exps, BN_free); |
117 | 0 | sk_BIGNUM_pop_free(coeffs, BN_free); |
118 | 0 | return 0; |
119 | 0 | } |
120 | | |
121 | | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) |
122 | | |
123 | | int ossl_rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[], |
124 | | int include_private) |
125 | 3.76k | { |
126 | 3.76k | int ret = 0; |
127 | 3.76k | const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL; |
128 | 3.76k | STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null(); |
129 | 3.76k | STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null(); |
130 | 3.76k | STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null(); |
131 | | |
132 | 3.76k | if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL) |
133 | 0 | goto err; |
134 | | |
135 | 3.76k | RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d); |
136 | 3.76k | ossl_rsa_get0_all_params(rsa, factors, exps, coeffs); |
137 | | |
138 | 3.76k | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n) |
139 | 3.76k | || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)) |
140 | 0 | goto err; |
141 | | |
142 | | /* Check private key data integrity */ |
143 | 3.76k | if (include_private && rsa_d != NULL) { |
144 | 0 | int numprimes = sk_BIGNUM_const_num(factors); |
145 | 0 | int numexps = sk_BIGNUM_const_num(exps); |
146 | 0 | int numcoeffs = sk_BIGNUM_const_num(coeffs); |
147 | | |
148 | | /* |
149 | | * It's permissible to have zero primes, i.e. no CRT params. |
150 | | * Otherwise, there must be at least two, as many exponents, |
151 | | * and one coefficient less. |
152 | | */ |
153 | 0 | if (numprimes != 0 |
154 | 0 | && (numprimes < 2 || numexps < 2 || numcoeffs < 1)) |
155 | 0 | goto err; |
156 | | |
157 | 0 | if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, |
158 | 0 | rsa_d) |
159 | 0 | || !ossl_param_build_set_multi_key_bn(bld, params, |
160 | 0 | ossl_rsa_mp_factor_names, |
161 | 0 | factors) |
162 | 0 | || !ossl_param_build_set_multi_key_bn(bld, params, |
163 | 0 | ossl_rsa_mp_exp_names, exps) |
164 | 0 | || !ossl_param_build_set_multi_key_bn(bld, params, |
165 | 0 | ossl_rsa_mp_coeff_names, |
166 | 0 | coeffs)) |
167 | 0 | goto err; |
168 | 0 | } |
169 | | |
170 | | #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) |
171 | | /* The acvp test results are not meant for export so check for bld == NULL */ |
172 | | if (bld == NULL) |
173 | | ossl_rsa_acvp_test_get_params(rsa, params); |
174 | | #endif |
175 | 3.76k | ret = 1; |
176 | 3.76k | err: |
177 | 3.76k | sk_BIGNUM_const_free(factors); |
178 | 3.76k | sk_BIGNUM_const_free(exps); |
179 | 3.76k | sk_BIGNUM_const_free(coeffs); |
180 | 3.76k | return ret; |
181 | 3.76k | } |
182 | | |
183 | | int ossl_rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss, |
184 | | OSSL_PARAM_BLD *bld, OSSL_PARAM params[]) |
185 | 3.42k | { |
186 | 3.42k | if (!ossl_rsa_pss_params_30_is_unrestricted(pss)) { |
187 | 1.05k | int hashalg_nid = ossl_rsa_pss_params_30_hashalg(pss); |
188 | 1.05k | int maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(pss); |
189 | 1.05k | int maskgenhashalg_nid = ossl_rsa_pss_params_30_maskgenhashalg(pss); |
190 | 1.05k | int saltlen = ossl_rsa_pss_params_30_saltlen(pss); |
191 | 1.05k | int default_hashalg_nid = ossl_rsa_pss_params_30_hashalg(NULL); |
192 | 1.05k | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); |
193 | 1.05k | int default_maskgenhashalg_nid = |
194 | 1.05k | ossl_rsa_pss_params_30_maskgenhashalg(NULL); |
195 | 1.05k | const char *mdname = |
196 | 1.05k | (hashalg_nid == default_hashalg_nid |
197 | 1.05k | ? NULL : ossl_rsa_oaeppss_nid2name(hashalg_nid)); |
198 | 1.05k | const char *mgfname = |
199 | 1.05k | (maskgenalg_nid == default_maskgenalg_nid |
200 | 1.05k | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenalg_nid)); |
201 | 1.05k | const char *mgf1mdname = |
202 | 1.05k | (maskgenhashalg_nid == default_maskgenhashalg_nid |
203 | 1.05k | ? NULL : ossl_rsa_oaeppss_nid2name(maskgenhashalg_nid)); |
204 | 1.05k | const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST; |
205 | 1.05k | const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC; |
206 | 1.05k | const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST; |
207 | 1.05k | const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN; |
208 | | |
209 | | /* |
210 | | * To ensure that the key isn't seen as unrestricted by the recipient, |
211 | | * we make sure that at least one PSS-related parameter is passed, even |
212 | | * if it has a default value; saltlen. |
213 | | */ |
214 | 1.05k | if ((mdname != NULL |
215 | 1.05k | && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname)) |
216 | 1.05k | || (mgfname != NULL |
217 | 1.05k | && !ossl_param_build_set_utf8_string(bld, params, |
218 | 0 | key_mgf, mgfname)) |
219 | 1.05k | || (mgf1mdname != NULL |
220 | 1.05k | && !ossl_param_build_set_utf8_string(bld, params, |
221 | 0 | key_mgf1_md, mgf1mdname)) |
222 | 1.05k | || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen))) |
223 | 0 | return 0; |
224 | 1.05k | } |
225 | 3.42k | return 1; |
226 | 3.42k | } |
227 | | |
228 | | int ossl_rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params, |
229 | | int *defaults_set, |
230 | | const OSSL_PARAM params[], |
231 | | OSSL_LIB_CTX *libctx) |
232 | 0 | { |
233 | 0 | const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen; |
234 | 0 | const OSSL_PARAM *param_propq; |
235 | 0 | const char *propq = NULL; |
236 | 0 | EVP_MD *md = NULL, *mgf1md = NULL; |
237 | 0 | int saltlen; |
238 | 0 | int ret = 0; |
239 | |
|
240 | 0 | if (pss_params == NULL) |
241 | 0 | return 0; |
242 | 0 | param_propq = |
243 | 0 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST_PROPS); |
244 | 0 | param_md = |
245 | 0 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST); |
246 | 0 | param_mgf = |
247 | 0 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC); |
248 | 0 | param_mgf1md = |
249 | 0 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST); |
250 | 0 | param_saltlen = |
251 | 0 | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN); |
252 | |
|
253 | 0 | if (param_propq != NULL) { |
254 | 0 | if (param_propq->data_type == OSSL_PARAM_UTF8_STRING) |
255 | 0 | propq = param_propq->data; |
256 | 0 | } |
257 | | /* |
258 | | * If we get any of the parameters, we know we have at least some |
259 | | * restrictions, so we start by setting default values, and let each |
260 | | * parameter override their specific restriction data. |
261 | | */ |
262 | 0 | if (!*defaults_set |
263 | 0 | && (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL |
264 | 0 | || param_saltlen != NULL)) { |
265 | 0 | if (!ossl_rsa_pss_params_30_set_defaults(pss_params)) |
266 | 0 | return 0; |
267 | 0 | *defaults_set = 1; |
268 | 0 | } |
269 | | |
270 | 0 | if (param_mgf != NULL) { |
271 | 0 | int default_maskgenalg_nid = ossl_rsa_pss_params_30_maskgenalg(NULL); |
272 | 0 | const char *mgfname = NULL; |
273 | |
|
274 | 0 | if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING) |
275 | 0 | mgfname = param_mgf->data; |
276 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname)) |
277 | 0 | return 0; |
278 | | |
279 | 0 | if (OPENSSL_strcasecmp(param_mgf->data, |
280 | 0 | ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0) |
281 | 0 | return 0; |
282 | 0 | } |
283 | | |
284 | | /* |
285 | | * We're only interested in the NIDs that correspond to the MDs, so the |
286 | | * exact propquery is unimportant in the EVP_MD_fetch() calls below. |
287 | | */ |
288 | | |
289 | 0 | if (param_md != NULL) { |
290 | 0 | const char *mdname = NULL; |
291 | |
|
292 | 0 | if (param_md->data_type == OSSL_PARAM_UTF8_STRING) |
293 | 0 | mdname = param_md->data; |
294 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname)) |
295 | 0 | goto err; |
296 | | |
297 | 0 | if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL |
298 | 0 | || !ossl_rsa_pss_params_30_set_hashalg(pss_params, |
299 | 0 | ossl_rsa_oaeppss_md2nid(md))) |
300 | 0 | goto err; |
301 | 0 | } |
302 | | |
303 | 0 | if (param_mgf1md != NULL) { |
304 | 0 | const char *mgf1mdname = NULL; |
305 | |
|
306 | 0 | if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING) |
307 | 0 | mgf1mdname = param_mgf1md->data; |
308 | 0 | else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname)) |
309 | 0 | goto err; |
310 | | |
311 | 0 | if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL |
312 | 0 | || !ossl_rsa_pss_params_30_set_maskgenhashalg( |
313 | 0 | pss_params, ossl_rsa_oaeppss_md2nid(mgf1md))) |
314 | 0 | goto err; |
315 | 0 | } |
316 | | |
317 | 0 | if (param_saltlen != NULL) { |
318 | 0 | if (!OSSL_PARAM_get_int(param_saltlen, &saltlen) |
319 | 0 | || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen)) |
320 | 0 | goto err; |
321 | 0 | } |
322 | | |
323 | 0 | ret = 1; |
324 | |
|
325 | 0 | err: |
326 | 0 | EVP_MD_free(md); |
327 | 0 | EVP_MD_free(mgf1md); |
328 | 0 | return ret; |
329 | 0 | } |
330 | | |
331 | | int ossl_rsa_is_foreign(const RSA *rsa) |
332 | 341 | { |
333 | 341 | #ifndef FIPS_MODULE |
334 | 341 | if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL()) |
335 | 0 | return 1; |
336 | 341 | #endif |
337 | 341 | return 0; |
338 | 341 | } |
339 | | |
340 | | static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f) |
341 | 0 | { |
342 | 0 | if (f != NULL && (*out = BN_dup(f)) == NULL) |
343 | 0 | return 0; |
344 | 0 | return 1; |
345 | 0 | } |
346 | | |
347 | | RSA *ossl_rsa_dup(const RSA *rsa, int selection) |
348 | 0 | { |
349 | 0 | RSA *dupkey = NULL; |
350 | 0 | #ifndef FIPS_MODULE |
351 | 0 | int pnum, i; |
352 | 0 | #endif |
353 | | |
354 | | /* Do not try to duplicate foreign RSA keys */ |
355 | 0 | if (ossl_rsa_is_foreign(rsa)) |
356 | 0 | return NULL; |
357 | | |
358 | 0 | if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL) |
359 | 0 | return NULL; |
360 | | |
361 | | /* public key */ |
362 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) { |
363 | 0 | if (!rsa_bn_dup_check(&dupkey->n, rsa->n)) |
364 | 0 | goto err; |
365 | 0 | if (!rsa_bn_dup_check(&dupkey->e, rsa->e)) |
366 | 0 | goto err; |
367 | 0 | } |
368 | | |
369 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
370 | | |
371 | | /* private key */ |
372 | 0 | if (!rsa_bn_dup_check(&dupkey->d, rsa->d)) |
373 | 0 | goto err; |
374 | | |
375 | | /* factors and crt params */ |
376 | 0 | if (!rsa_bn_dup_check(&dupkey->p, rsa->p)) |
377 | 0 | goto err; |
378 | 0 | if (!rsa_bn_dup_check(&dupkey->q, rsa->q)) |
379 | 0 | goto err; |
380 | 0 | if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1)) |
381 | 0 | goto err; |
382 | 0 | if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1)) |
383 | 0 | goto err; |
384 | 0 | if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp)) |
385 | 0 | goto err; |
386 | 0 | } |
387 | | |
388 | 0 | dupkey->version = rsa->version; |
389 | 0 | dupkey->flags = rsa->flags; |
390 | | /* we always copy the PSS parameters regardless of selection */ |
391 | 0 | dupkey->pss_params = rsa->pss_params; |
392 | |
|
393 | 0 | #ifndef FIPS_MODULE |
394 | | /* multiprime */ |
395 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 |
396 | 0 | && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) { |
397 | 0 | dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); |
398 | 0 | if (dupkey->prime_infos == NULL) |
399 | 0 | goto err; |
400 | 0 | for (i = 0; i < pnum; i++) { |
401 | 0 | const RSA_PRIME_INFO *pinfo = NULL; |
402 | 0 | RSA_PRIME_INFO *duppinfo = NULL; |
403 | |
|
404 | 0 | if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL) { |
405 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE); |
406 | 0 | goto err; |
407 | 0 | } |
408 | | /* push first so cleanup in error case works */ |
409 | 0 | (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo); |
410 | |
|
411 | 0 | pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i); |
412 | 0 | if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r)) |
413 | 0 | goto err; |
414 | 0 | if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d)) |
415 | 0 | goto err; |
416 | 0 | if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t)) |
417 | 0 | goto err; |
418 | 0 | } |
419 | 0 | if (!ossl_rsa_multip_calc_product(dupkey)) |
420 | 0 | goto err; |
421 | 0 | } |
422 | | |
423 | 0 | if (rsa->pss != NULL) { |
424 | 0 | dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss); |
425 | 0 | if (rsa->pss->maskGenAlgorithm != NULL |
426 | 0 | && dupkey->pss->maskGenAlgorithm == NULL) { |
427 | 0 | dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm); |
428 | 0 | if (dupkey->pss->maskHash == NULL) |
429 | 0 | goto err; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA, |
433 | 0 | &dupkey->ex_data, &rsa->ex_data)) |
434 | 0 | goto err; |
435 | 0 | #endif |
436 | | |
437 | 0 | return dupkey; |
438 | | |
439 | 0 | err: |
440 | 0 | RSA_free(dupkey); |
441 | 0 | return NULL; |
442 | 0 | } |
443 | | |
444 | | #ifndef FIPS_MODULE |
445 | | RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg) |
446 | 1.05k | { |
447 | 1.05k | RSA_PSS_PARAMS *pss; |
448 | | |
449 | 1.05k | pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS), |
450 | 1.05k | alg->parameter); |
451 | | |
452 | 1.05k | if (pss == NULL) |
453 | 1 | return NULL; |
454 | | |
455 | 1.05k | if (pss->maskGenAlgorithm != NULL) { |
456 | 0 | pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm); |
457 | 0 | if (pss->maskHash == NULL) { |
458 | 0 | RSA_PSS_PARAMS_free(pss); |
459 | 0 | return NULL; |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | 1.05k | return pss; |
464 | 1.05k | } |
465 | | |
466 | | static int ossl_rsa_sync_to_pss_params_30(RSA *rsa) |
467 | 1.05k | { |
468 | 1.05k | const RSA_PSS_PARAMS *legacy_pss = NULL; |
469 | 1.05k | RSA_PSS_PARAMS_30 *pss = NULL; |
470 | | |
471 | 1.05k | if (rsa != NULL |
472 | 1.05k | && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL |
473 | 1.05k | && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) { |
474 | 1.05k | const EVP_MD *md = NULL, *mgf1md = NULL; |
475 | 1.05k | int md_nid, mgf1md_nid, saltlen, trailerField; |
476 | 1.05k | RSA_PSS_PARAMS_30 pss_params; |
477 | | |
478 | | /* |
479 | | * We don't care about the validity of the fields here, we just |
480 | | * want to synchronise values. Verifying here makes it impossible |
481 | | * to even read a key with invalid values, making it hard to test |
482 | | * a bad situation. |
483 | | * |
484 | | * Other routines use ossl_rsa_pss_get_param(), so the values will |
485 | | * be checked, eventually. |
486 | | */ |
487 | 1.05k | if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md, |
488 | 1.05k | &saltlen, &trailerField)) |
489 | 0 | return 0; |
490 | 1.05k | md_nid = EVP_MD_get_type(md); |
491 | 1.05k | mgf1md_nid = EVP_MD_get_type(mgf1md); |
492 | 1.05k | if (!ossl_rsa_pss_params_30_set_defaults(&pss_params) |
493 | 1.05k | || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid) |
494 | 1.05k | || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params, |
495 | 1.05k | mgf1md_nid) |
496 | 1.05k | || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen) |
497 | 1.05k | || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params, |
498 | 1.05k | trailerField)) |
499 | 0 | return 0; |
500 | 1.05k | *pss = pss_params; |
501 | 1.05k | } |
502 | 1.05k | return 1; |
503 | 1.05k | } |
504 | | |
505 | | int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss, |
506 | | const EVP_MD **pmd, const EVP_MD **pmgf1md, |
507 | | int *psaltlen, int *ptrailerField) |
508 | 1.05k | { |
509 | 1.05k | RSA_PSS_PARAMS_30 pss_params; |
510 | | |
511 | | /* Get the defaults from the ONE place */ |
512 | 1.05k | (void)ossl_rsa_pss_params_30_set_defaults(&pss_params); |
513 | | |
514 | 1.05k | if (pss == NULL) |
515 | 0 | return 0; |
516 | 1.05k | *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm); |
517 | 1.05k | if (*pmd == NULL) |
518 | 0 | return 0; |
519 | 1.05k | *pmgf1md = ossl_x509_algor_get_md(pss->maskHash); |
520 | 1.05k | if (*pmgf1md == NULL) |
521 | 0 | return 0; |
522 | 1.05k | if (pss->saltLength) |
523 | 0 | *psaltlen = ASN1_INTEGER_get(pss->saltLength); |
524 | 1.05k | else |
525 | 1.05k | *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params); |
526 | 1.05k | if (pss->trailerField) |
527 | 0 | *ptrailerField = ASN1_INTEGER_get(pss->trailerField); |
528 | 1.05k | else |
529 | 1.05k | *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);; |
530 | | |
531 | 1.05k | return 1; |
532 | 1.05k | } |
533 | | |
534 | | int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg) |
535 | 3.94k | { |
536 | 3.94k | RSA_PSS_PARAMS *pss; |
537 | 3.94k | const ASN1_OBJECT *algoid; |
538 | 3.94k | const void *algp; |
539 | 3.94k | int algptype; |
540 | | |
541 | 3.94k | X509_ALGOR_get0(&algoid, &algptype, &algp, alg); |
542 | 3.94k | if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS) |
543 | 341 | return 1; |
544 | 3.60k | if (algptype == V_ASN1_UNDEF) |
545 | 2.37k | return 1; |
546 | 1.22k | if (algptype != V_ASN1_SEQUENCE) { |
547 | 176 | ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS); |
548 | 176 | return 0; |
549 | 176 | } |
550 | 1.05k | if ((pss = ossl_rsa_pss_decode(alg)) == NULL |
551 | 1.05k | || !ossl_rsa_set0_pss_params(rsa, pss)) { |
552 | 1 | RSA_PSS_PARAMS_free(pss); |
553 | 1 | return 0; |
554 | 1 | } |
555 | 1.05k | if (!ossl_rsa_sync_to_pss_params_30(rsa)) |
556 | 0 | return 0; |
557 | 1.05k | return 1; |
558 | 1.05k | } |
559 | | |
560 | | RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, |
561 | | OSSL_LIB_CTX *libctx, const char *propq) |
562 | 0 | { |
563 | 0 | const unsigned char *p; |
564 | 0 | RSA *rsa; |
565 | 0 | int pklen; |
566 | 0 | const X509_ALGOR *alg; |
567 | |
|
568 | 0 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf)) |
569 | 0 | return 0; |
570 | 0 | rsa = d2i_RSAPrivateKey(NULL, &p, pklen); |
571 | 0 | if (rsa == NULL) { |
572 | 0 | ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB); |
573 | 0 | return NULL; |
574 | 0 | } |
575 | 0 | if (!ossl_rsa_param_decode(rsa, alg)) { |
576 | 0 | RSA_free(rsa); |
577 | 0 | return NULL; |
578 | 0 | } |
579 | | |
580 | 0 | RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK); |
581 | 0 | switch (OBJ_obj2nid(alg->algorithm)) { |
582 | 0 | case EVP_PKEY_RSA: |
583 | 0 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA); |
584 | 0 | break; |
585 | 0 | case EVP_PKEY_RSA_PSS: |
586 | 0 | RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS); |
587 | 0 | break; |
588 | 0 | default: |
589 | | /* Leave the type bits zero */ |
590 | 0 | break; |
591 | 0 | } |
592 | | |
593 | 0 | return rsa; |
594 | 0 | } |
595 | | #endif |