rsa_backend.c 19 KB

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