rsa_backend.c 19 KB

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