rsa_backend.c 24 KB

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