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