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