rsa_backend.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. if (strcasecmp(param_mgf->data,
  236. ossl_rsa_mgf_nid2name(default_maskgenalg_nid)) != 0)
  237. return 0;
  238. }
  239. /*
  240. * We're only interested in the NIDs that correspond to the MDs, so the
  241. * exact propquery is unimportant in the EVP_MD_fetch() calls below.
  242. */
  243. if (param_md != NULL) {
  244. const char *mdname = NULL;
  245. if (param_md->data_type == OSSL_PARAM_UTF8_STRING)
  246. mdname = param_md->data;
  247. else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))
  248. goto err;
  249. if ((md = EVP_MD_fetch(libctx, mdname, propq)) == NULL
  250. || !ossl_rsa_pss_params_30_set_hashalg(pss_params,
  251. ossl_rsa_oaeppss_md2nid(md)))
  252. goto err;
  253. }
  254. if (param_mgf1md != NULL) {
  255. const char *mgf1mdname = NULL;
  256. if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)
  257. mgf1mdname = param_mgf1md->data;
  258. else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))
  259. goto err;
  260. if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, propq)) == NULL
  261. || !ossl_rsa_pss_params_30_set_maskgenhashalg(
  262. pss_params, ossl_rsa_oaeppss_md2nid(mgf1md)))
  263. goto err;
  264. }
  265. if (param_saltlen != NULL) {
  266. if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)
  267. || !ossl_rsa_pss_params_30_set_saltlen(pss_params, saltlen))
  268. goto err;
  269. }
  270. ret = 1;
  271. err:
  272. EVP_MD_free(md);
  273. EVP_MD_free(mgf1md);
  274. return ret;
  275. }
  276. int ossl_rsa_is_foreign(const RSA *rsa)
  277. {
  278. #ifndef FIPS_MODULE
  279. if (rsa->engine != NULL || RSA_get_method(rsa) != RSA_PKCS1_OpenSSL())
  280. return 1;
  281. #endif
  282. return 0;
  283. }
  284. static ossl_inline int rsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
  285. {
  286. if (f != NULL && (*out = BN_dup(f)) == NULL)
  287. return 0;
  288. return 1;
  289. }
  290. RSA *ossl_rsa_dup(const RSA *rsa, int selection)
  291. {
  292. RSA *dupkey = NULL;
  293. #ifndef FIPS_MODULE
  294. int pnum, i;
  295. #endif
  296. /* Do not try to duplicate foreign RSA keys */
  297. if (ossl_rsa_is_foreign(rsa))
  298. return NULL;
  299. if ((dupkey = ossl_rsa_new_with_ctx(rsa->libctx)) == NULL)
  300. return NULL;
  301. /* public key */
  302. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  303. if (!rsa_bn_dup_check(&dupkey->n, rsa->n))
  304. goto err;
  305. if (!rsa_bn_dup_check(&dupkey->e, rsa->e))
  306. goto err;
  307. }
  308. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  309. /* private key */
  310. if (!rsa_bn_dup_check(&dupkey->d, rsa->d))
  311. goto err;
  312. /* factors and crt params */
  313. if (!rsa_bn_dup_check(&dupkey->p, rsa->p))
  314. goto err;
  315. if (!rsa_bn_dup_check(&dupkey->q, rsa->q))
  316. goto err;
  317. if (!rsa_bn_dup_check(&dupkey->dmp1, rsa->dmp1))
  318. goto err;
  319. if (!rsa_bn_dup_check(&dupkey->dmq1, rsa->dmq1))
  320. goto err;
  321. if (!rsa_bn_dup_check(&dupkey->iqmp, rsa->iqmp))
  322. goto err;
  323. }
  324. dupkey->version = rsa->version;
  325. dupkey->flags = rsa->flags;
  326. /* we always copy the PSS parameters regardless of selection */
  327. dupkey->pss_params = rsa->pss_params;
  328. #ifndef FIPS_MODULE
  329. /* multiprime */
  330. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  331. && (pnum = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) > 0) {
  332. dupkey->prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  333. for (i = 0; i < pnum; i++) {
  334. const RSA_PRIME_INFO *pinfo = NULL;
  335. RSA_PRIME_INFO *duppinfo = NULL;
  336. if ((duppinfo = OPENSSL_zalloc(sizeof(*duppinfo))) == NULL) {
  337. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  338. goto err;
  339. }
  340. /* push first so cleanup in error case works */
  341. (void)sk_RSA_PRIME_INFO_push(dupkey->prime_infos, duppinfo);
  342. pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);
  343. if (!rsa_bn_dup_check(&duppinfo->r, pinfo->r))
  344. goto err;
  345. if (!rsa_bn_dup_check(&duppinfo->d, pinfo->d))
  346. goto err;
  347. if (!rsa_bn_dup_check(&duppinfo->t, pinfo->t))
  348. goto err;
  349. }
  350. if (!ossl_rsa_multip_calc_product(dupkey))
  351. goto err;
  352. }
  353. if (rsa->pss != NULL) {
  354. dupkey->pss = RSA_PSS_PARAMS_dup(rsa->pss);
  355. if (rsa->pss->maskGenAlgorithm != NULL
  356. && dupkey->pss->maskGenAlgorithm == NULL) {
  357. dupkey->pss->maskHash = ossl_x509_algor_mgf1_decode(rsa->pss->maskGenAlgorithm);
  358. if (dupkey->pss->maskHash == NULL)
  359. goto err;
  360. }
  361. }
  362. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_RSA,
  363. &dupkey->ex_data, &rsa->ex_data))
  364. goto err;
  365. #endif
  366. return dupkey;
  367. err:
  368. RSA_free(dupkey);
  369. return NULL;
  370. }
  371. #ifndef FIPS_MODULE
  372. RSA_PSS_PARAMS *ossl_rsa_pss_decode(const X509_ALGOR *alg)
  373. {
  374. RSA_PSS_PARAMS *pss;
  375. pss = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_PSS_PARAMS),
  376. alg->parameter);
  377. if (pss == NULL)
  378. return NULL;
  379. if (pss->maskGenAlgorithm != NULL) {
  380. pss->maskHash = ossl_x509_algor_mgf1_decode(pss->maskGenAlgorithm);
  381. if (pss->maskHash == NULL) {
  382. RSA_PSS_PARAMS_free(pss);
  383. return NULL;
  384. }
  385. }
  386. return pss;
  387. }
  388. static int ossl_rsa_sync_to_pss_params_30(RSA *rsa)
  389. {
  390. const RSA_PSS_PARAMS *legacy_pss = NULL;
  391. RSA_PSS_PARAMS_30 *pss = NULL;
  392. if (rsa != NULL
  393. && (legacy_pss = RSA_get0_pss_params(rsa)) != NULL
  394. && (pss = ossl_rsa_get0_pss_params_30(rsa)) != NULL) {
  395. const EVP_MD *md = NULL, *mgf1md = NULL;
  396. int md_nid, mgf1md_nid, saltlen, trailerField;
  397. RSA_PSS_PARAMS_30 pss_params;
  398. /*
  399. * We don't care about the validity of the fields here, we just
  400. * want to synchronise values. Verifying here makes it impossible
  401. * to even read a key with invalid values, making it hard to test
  402. * a bad situation.
  403. *
  404. * Other routines use ossl_rsa_pss_get_param(), so the values will
  405. * be checked, eventually.
  406. */
  407. if (!ossl_rsa_pss_get_param_unverified(legacy_pss, &md, &mgf1md,
  408. &saltlen, &trailerField))
  409. return 0;
  410. md_nid = EVP_MD_get_type(md);
  411. mgf1md_nid = EVP_MD_get_type(mgf1md);
  412. if (!ossl_rsa_pss_params_30_set_defaults(&pss_params)
  413. || !ossl_rsa_pss_params_30_set_hashalg(&pss_params, md_nid)
  414. || !ossl_rsa_pss_params_30_set_maskgenhashalg(&pss_params,
  415. mgf1md_nid)
  416. || !ossl_rsa_pss_params_30_set_saltlen(&pss_params, saltlen)
  417. || !ossl_rsa_pss_params_30_set_trailerfield(&pss_params,
  418. trailerField))
  419. return 0;
  420. *pss = pss_params;
  421. }
  422. return 1;
  423. }
  424. int ossl_rsa_pss_get_param_unverified(const RSA_PSS_PARAMS *pss,
  425. const EVP_MD **pmd, const EVP_MD **pmgf1md,
  426. int *psaltlen, int *ptrailerField)
  427. {
  428. RSA_PSS_PARAMS_30 pss_params;
  429. /* Get the defaults from the ONE place */
  430. (void)ossl_rsa_pss_params_30_set_defaults(&pss_params);
  431. if (pss == NULL)
  432. return 0;
  433. *pmd = ossl_x509_algor_get_md(pss->hashAlgorithm);
  434. if (*pmd == NULL)
  435. return 0;
  436. *pmgf1md = ossl_x509_algor_get_md(pss->maskHash);
  437. if (*pmgf1md == NULL)
  438. return 0;
  439. if (pss->saltLength)
  440. *psaltlen = ASN1_INTEGER_get(pss->saltLength);
  441. else
  442. *psaltlen = ossl_rsa_pss_params_30_saltlen(&pss_params);
  443. if (pss->trailerField)
  444. *ptrailerField = ASN1_INTEGER_get(pss->trailerField);
  445. else
  446. *ptrailerField = ossl_rsa_pss_params_30_trailerfield(&pss_params);;
  447. return 1;
  448. }
  449. int ossl_rsa_param_decode(RSA *rsa, const X509_ALGOR *alg)
  450. {
  451. RSA_PSS_PARAMS *pss;
  452. const ASN1_OBJECT *algoid;
  453. const void *algp;
  454. int algptype;
  455. X509_ALGOR_get0(&algoid, &algptype, &algp, alg);
  456. if (OBJ_obj2nid(algoid) != EVP_PKEY_RSA_PSS)
  457. return 1;
  458. if (algptype == V_ASN1_UNDEF)
  459. return 1;
  460. if (algptype != V_ASN1_SEQUENCE) {
  461. ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_PSS_PARAMETERS);
  462. return 0;
  463. }
  464. if ((pss = ossl_rsa_pss_decode(alg)) == NULL
  465. || !ossl_rsa_set0_pss_params(rsa, pss)) {
  466. RSA_PSS_PARAMS_free(pss);
  467. return 0;
  468. }
  469. if (!ossl_rsa_sync_to_pss_params_30(rsa))
  470. return 0;
  471. return 1;
  472. }
  473. RSA *ossl_rsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  474. OSSL_LIB_CTX *libctx, const char *propq)
  475. {
  476. const unsigned char *p;
  477. RSA *rsa;
  478. int pklen;
  479. const X509_ALGOR *alg;
  480. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &alg, p8inf))
  481. return 0;
  482. rsa = d2i_RSAPrivateKey(NULL, &p, pklen);
  483. if (rsa == NULL) {
  484. ERR_raise(ERR_LIB_RSA, ERR_R_RSA_LIB);
  485. return NULL;
  486. }
  487. if (!ossl_rsa_param_decode(rsa, alg)) {
  488. RSA_free(rsa);
  489. return NULL;
  490. }
  491. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  492. switch (OBJ_obj2nid(alg->algorithm)) {
  493. case EVP_PKEY_RSA:
  494. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
  495. break;
  496. case EVP_PKEY_RSA_PSS:
  497. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
  498. break;
  499. default:
  500. /* Leave the type bits zero */
  501. break;
  502. }
  503. return rsa;
  504. }
  505. #endif