rsa_kmgmt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * Copyright 2019-2020 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 <openssl/core_dispatch.h>
  15. #include <openssl/core_names.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/err.h>
  18. #include <openssl/rsa.h>
  19. #include <openssl/evp.h>
  20. #include "prov/implementations.h"
  21. #include "prov/providercommon.h"
  22. #include "prov/provider_ctx.h"
  23. #include "crypto/rsa.h"
  24. #include "internal/param_build_set.h"
  25. static OSSL_FUNC_keymgmt_new_fn rsa_newdata;
  26. static OSSL_FUNC_keymgmt_new_fn rsapss_newdata;
  27. static OSSL_FUNC_keymgmt_gen_init_fn rsa_gen_init;
  28. static OSSL_FUNC_keymgmt_gen_init_fn rsapss_gen_init;
  29. static OSSL_FUNC_keymgmt_gen_set_params_fn rsa_gen_set_params;
  30. static OSSL_FUNC_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
  31. static OSSL_FUNC_keymgmt_gen_settable_params_fn rsapss_gen_settable_params;
  32. static OSSL_FUNC_keymgmt_gen_fn rsa_gen;
  33. static OSSL_FUNC_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
  34. static OSSL_FUNC_keymgmt_free_fn rsa_freedata;
  35. static OSSL_FUNC_keymgmt_get_params_fn rsa_get_params;
  36. static OSSL_FUNC_keymgmt_gettable_params_fn rsa_gettable_params;
  37. static OSSL_FUNC_keymgmt_has_fn rsa_has;
  38. static OSSL_FUNC_keymgmt_match_fn rsa_match;
  39. static OSSL_FUNC_keymgmt_validate_fn rsa_validate;
  40. static OSSL_FUNC_keymgmt_import_fn rsa_import;
  41. static OSSL_FUNC_keymgmt_import_types_fn rsa_import_types;
  42. static OSSL_FUNC_keymgmt_export_fn rsa_export;
  43. static OSSL_FUNC_keymgmt_export_types_fn rsa_export_types;
  44. static OSSL_FUNC_keymgmt_query_operation_name_fn rsapss_query_operation_name;
  45. #define RSA_DEFAULT_MD "SHA256"
  46. #define RSA_PSS_DEFAULT_MD OSSL_DIGEST_NAME_SHA1
  47. #define RSA_POSSIBLE_SELECTIONS \
  48. (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
  49. DEFINE_STACK_OF(BIGNUM)
  50. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  51. static int pss_params_fromdata(RSA_PSS_PARAMS_30 *pss_params,
  52. const OSSL_PARAM params[], int rsa_type,
  53. OPENSSL_CTX *libctx)
  54. {
  55. if (!rsa_pss_params_30_fromdata(pss_params, params, libctx))
  56. return 0;
  57. /* If not a PSS type RSA, sending us PSS parameters is wrong */
  58. if (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
  59. && !rsa_pss_params_30_is_unrestricted(pss_params))
  60. return 0;
  61. return 1;
  62. }
  63. static void *rsa_newdata(void *provctx)
  64. {
  65. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  66. RSA *rsa = rsa_new_with_ctx(libctx);
  67. if (rsa != NULL) {
  68. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  69. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSA);
  70. }
  71. return rsa;
  72. }
  73. static void *rsapss_newdata(void *provctx)
  74. {
  75. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  76. RSA *rsa = rsa_new_with_ctx(libctx);
  77. if (rsa != NULL) {
  78. RSA_clear_flags(rsa, RSA_FLAG_TYPE_MASK);
  79. RSA_set_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS);
  80. }
  81. return rsa;
  82. }
  83. static void rsa_freedata(void *keydata)
  84. {
  85. RSA_free(keydata);
  86. }
  87. static int rsa_has(void *keydata, int selection)
  88. {
  89. RSA *rsa = keydata;
  90. int ok = 0;
  91. if (rsa != NULL) {
  92. if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
  93. ok = 1;
  94. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  95. /* This will change with OAEP */
  96. ok = ok && (RSA_test_flags(rsa, RSA_FLAG_TYPE_RSASSAPSS) != 0);
  97. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  98. ok = ok && (RSA_get0_e(rsa) != NULL);
  99. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  100. ok = ok && (RSA_get0_n(rsa) != NULL);
  101. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  102. ok = ok && (RSA_get0_d(rsa) != NULL);
  103. }
  104. return ok;
  105. }
  106. static int rsa_match(const void *keydata1, const void *keydata2, int selection)
  107. {
  108. const RSA *rsa1 = keydata1;
  109. const RSA *rsa2 = keydata2;
  110. int ok = 1;
  111. /* There is always an |e| */
  112. ok = ok && BN_cmp(RSA_get0_e(rsa1), RSA_get0_e(rsa2)) == 0;
  113. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  114. ok = ok && BN_cmp(RSA_get0_n(rsa1), RSA_get0_n(rsa2)) == 0;
  115. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  116. ok = ok && BN_cmp(RSA_get0_d(rsa1), RSA_get0_d(rsa2)) == 0;
  117. return ok;
  118. }
  119. static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
  120. {
  121. RSA *rsa = keydata;
  122. int rsa_type;
  123. int ok = 1;
  124. if (rsa == NULL)
  125. return 0;
  126. if ((selection & RSA_POSSIBLE_SELECTIONS) == 0)
  127. return 0;
  128. rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
  129. /* TODO(3.0) OAEP should bring on parameters as well */
  130. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  131. ok = ok && pss_params_fromdata(rsa_get0_pss_params_30(rsa), params,
  132. rsa_type, rsa_get0_libctx(rsa));
  133. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  134. ok = ok && rsa_fromdata(rsa, params);
  135. return ok;
  136. }
  137. static int rsa_export(void *keydata, int selection,
  138. OSSL_CALLBACK *param_callback, void *cbarg)
  139. {
  140. RSA *rsa = keydata;
  141. const RSA_PSS_PARAMS_30 *pss_params = rsa_get0_pss_params_30(rsa);
  142. OSSL_PARAM_BLD *tmpl;
  143. OSSL_PARAM *params = NULL;
  144. int ok = 1;
  145. if (rsa == NULL)
  146. return 0;
  147. /* TODO(3.0) OAEP should bring on parameters */
  148. tmpl = OSSL_PARAM_BLD_new();
  149. if (tmpl == NULL)
  150. return 0;
  151. if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
  152. ok = ok && (rsa_pss_params_30_is_unrestricted(pss_params)
  153. || rsa_pss_params_30_todata(pss_params, NULL, tmpl, NULL));
  154. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  155. ok = ok && rsa_todata(rsa, tmpl, NULL);
  156. if (!ok
  157. || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
  158. goto err;
  159. ok = param_callback(params, cbarg);
  160. OSSL_PARAM_BLD_free_params(params);
  161. err:
  162. OSSL_PARAM_BLD_free(tmpl);
  163. return ok;
  164. }
  165. #ifdef FIPS_MODULE
  166. /* In fips mode there are no multi-primes. */
  167. # define RSA_KEY_MP_TYPES() \
  168. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
  169. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
  170. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
  171. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
  172. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
  173. #else
  174. /*
  175. * We allow up to 10 prime factors (starting with p, q).
  176. * NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
  177. */
  178. # define RSA_KEY_MP_TYPES() \
  179. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
  180. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
  181. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0), \
  182. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0), \
  183. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0), \
  184. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0), \
  185. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0), \
  186. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0), \
  187. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0), \
  188. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0), \
  189. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
  190. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
  191. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0), \
  192. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0), \
  193. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0), \
  194. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0), \
  195. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0), \
  196. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0), \
  197. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0), \
  198. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0), \
  199. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), \
  200. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0), \
  201. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0), \
  202. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0), \
  203. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0), \
  204. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0), \
  205. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0), \
  206. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0), \
  207. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0),
  208. #endif
  209. #define RSA_KEY_TYPES() \
  210. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), \
  211. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), \
  212. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), \
  213. RSA_KEY_MP_TYPES()
  214. /*
  215. * This provider can export everything in an RSA key, so we use the exact
  216. * same type description for export as for import. Other providers might
  217. * choose to import full keys, but only export the public parts, and will
  218. * therefore have the importkey_types and importkey_types functions return
  219. * different arrays.
  220. */
  221. static const OSSL_PARAM rsa_key_types[] = {
  222. RSA_KEY_TYPES()
  223. OSSL_PARAM_END
  224. };
  225. /*
  226. * We lied about the amount of factors, exponents and coefficients, the
  227. * export and import functions can really deal with an infinite amount
  228. * of these numbers. However, RSA keys with too many primes are futile,
  229. * so we at least pretend to have some limits.
  230. */
  231. static const OSSL_PARAM *rsa_imexport_types(int selection)
  232. {
  233. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  234. return rsa_key_types;
  235. return NULL;
  236. }
  237. static const OSSL_PARAM *rsa_import_types(int selection)
  238. {
  239. return rsa_imexport_types(selection);
  240. }
  241. static const OSSL_PARAM *rsa_export_types(int selection)
  242. {
  243. return rsa_imexport_types(selection);
  244. }
  245. static int rsa_get_params(void *key, OSSL_PARAM params[])
  246. {
  247. RSA *rsa = key;
  248. const RSA_PSS_PARAMS_30 *pss_params = rsa_get0_pss_params_30(rsa);
  249. int rsa_type = RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK);
  250. OSSL_PARAM *p;
  251. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  252. && !OSSL_PARAM_set_int(p, RSA_bits(rsa)))
  253. return 0;
  254. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  255. && !OSSL_PARAM_set_int(p, RSA_security_bits(rsa)))
  256. return 0;
  257. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  258. && !OSSL_PARAM_set_int(p, RSA_size(rsa)))
  259. return 0;
  260. /*
  261. * For RSA-PSS keys, we ignore the default digest request
  262. * TODO(3.0) with RSA-OAEP keys, this may need to be amended
  263. */
  264. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  265. && rsa_type != RSA_FLAG_TYPE_RSASSAPSS) {
  266. if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
  267. return 0;
  268. }
  269. /*
  270. * For non-RSA-PSS keys, we ignore the mandatory digest request
  271. * TODO(3.0) with RSA-OAEP keys, this may need to be amended
  272. */
  273. if ((p = OSSL_PARAM_locate(params,
  274. OSSL_PKEY_PARAM_MANDATORY_DIGEST)) != NULL
  275. && rsa_type == RSA_FLAG_TYPE_RSASSAPSS) {
  276. const char *mdname = RSA_PSS_DEFAULT_MD;
  277. if (!rsa_pss_params_30_is_unrestricted(pss_params)) {
  278. mdname =
  279. rsa_oaeppss_nid2name(rsa_pss_params_30_hashalg(pss_params));
  280. if (mdname == NULL || !OSSL_PARAM_set_utf8_string(p, mdname))
  281. return 0;
  282. }
  283. }
  284. return (rsa_type != RSA_FLAG_TYPE_RSASSAPSS
  285. || rsa_pss_params_30_todata(pss_params, NULL, NULL, params))
  286. && rsa_todata(rsa, NULL, params);
  287. }
  288. static const OSSL_PARAM rsa_params[] = {
  289. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  290. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  291. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  292. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
  293. RSA_KEY_TYPES()
  294. OSSL_PARAM_END
  295. };
  296. static const OSSL_PARAM *rsa_gettable_params(void)
  297. {
  298. return rsa_params;
  299. }
  300. static int rsa_validate(void *keydata, int selection)
  301. {
  302. RSA *rsa = keydata;
  303. int ok = 0;
  304. if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
  305. ok = 1;
  306. /* If the whole key is selected, we do a pairwise validation */
  307. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
  308. == OSSL_KEYMGMT_SELECT_KEYPAIR) {
  309. ok = ok && rsa_validate_pairwise(rsa);
  310. } else {
  311. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  312. ok = ok && rsa_validate_private(rsa);
  313. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  314. ok = ok && rsa_validate_public(rsa);
  315. }
  316. return ok;
  317. }
  318. struct rsa_gen_ctx {
  319. OPENSSL_CTX *libctx;
  320. int rsa_type;
  321. size_t nbits;
  322. BIGNUM *pub_exp;
  323. size_t primes;
  324. /* For PSS */
  325. RSA_PSS_PARAMS_30 pss_params;
  326. /* For generation callback */
  327. OSSL_CALLBACK *cb;
  328. void *cbarg;
  329. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  330. /* ACVP test parameters */
  331. OSSL_PARAM *acvp_test_params;
  332. #endif
  333. };
  334. static int rsa_gencb(int p, int n, BN_GENCB *cb)
  335. {
  336. struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
  337. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  338. params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
  339. params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
  340. return gctx->cb(params, gctx->cbarg);
  341. }
  342. static void *gen_init(void *provctx, int selection, int rsa_type)
  343. {
  344. OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
  345. struct rsa_gen_ctx *gctx = NULL;
  346. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  347. return NULL;
  348. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  349. gctx->libctx = libctx;
  350. if ((gctx->pub_exp = BN_new()) == NULL
  351. || !BN_set_word(gctx->pub_exp, RSA_F4)) {
  352. BN_free(gctx->pub_exp);
  353. OPENSSL_free(gctx);
  354. gctx = NULL;
  355. } else {
  356. gctx->nbits = 2048;
  357. gctx->primes = RSA_DEFAULT_PRIME_NUM;
  358. gctx->rsa_type = rsa_type;
  359. }
  360. }
  361. return gctx;
  362. }
  363. static void *rsa_gen_init(void *provctx, int selection)
  364. {
  365. return gen_init(provctx, selection, RSA_FLAG_TYPE_RSA);
  366. }
  367. static void *rsapss_gen_init(void *provctx, int selection)
  368. {
  369. return gen_init(provctx, selection, RSA_FLAG_TYPE_RSASSAPSS);
  370. }
  371. /*
  372. * This function is common for all RSA sub-types, to detect possible
  373. * misuse, such as PSS parameters being passed when a plain RSA key
  374. * is generated.
  375. */
  376. static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
  377. {
  378. struct rsa_gen_ctx *gctx = genctx;
  379. const OSSL_PARAM *p;
  380. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
  381. && !OSSL_PARAM_get_size_t(p, &gctx->nbits))
  382. return 0;
  383. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
  384. && !OSSL_PARAM_get_size_t(p, &gctx->primes))
  385. return 0;
  386. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
  387. && !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
  388. return 0;
  389. /* Only attempt to get PSS parameters when generating an RSA-PSS key */
  390. if (gctx->rsa_type == RSA_FLAG_TYPE_RSASSAPSS
  391. && !pss_params_fromdata(&gctx->pss_params, params, gctx->rsa_type,
  392. gctx->libctx))
  393. return 0;
  394. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  395. /* Any ACVP test related parameters are copied into a params[] */
  396. if (!rsa_acvp_test_gen_params_new(&gctx->acvp_test_params, params))
  397. return 0;
  398. #endif
  399. return 1;
  400. }
  401. #define rsa_gen_basic \
  402. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL), \
  403. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL), \
  404. OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0)
  405. /*
  406. * The following must be kept in sync with rsa_pss_params_30_fromdata()
  407. * in crypto/rsa/rsa_backend.c
  408. */
  409. #define rsa_gen_pss \
  410. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_DIGEST, NULL, 0), \
  411. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MASKGENFUNC, NULL, 0), \
  412. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_RSA_MGF1_DIGEST, NULL, 0), \
  413. OSSL_PARAM_int(OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, NULL)
  414. static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
  415. {
  416. static OSSL_PARAM settable[] = {
  417. rsa_gen_basic,
  418. OSSL_PARAM_END
  419. };
  420. return settable;
  421. }
  422. static const OSSL_PARAM *rsapss_gen_settable_params(void *provctx)
  423. {
  424. static OSSL_PARAM settable[] = {
  425. rsa_gen_basic,
  426. rsa_gen_pss,
  427. OSSL_PARAM_END
  428. };
  429. return settable;
  430. }
  431. static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  432. {
  433. struct rsa_gen_ctx *gctx = genctx;
  434. RSA *rsa = NULL, *rsa_tmp = NULL;
  435. BN_GENCB *gencb = NULL;
  436. if (gctx == NULL)
  437. return NULL;
  438. switch (gctx->rsa_type) {
  439. case RSA_FLAG_TYPE_RSA:
  440. /* For plain RSA keys, PSS parameters must not be set */
  441. if (!rsa_pss_params_30_is_unrestricted(&gctx->pss_params))
  442. goto err;
  443. break;
  444. case RSA_FLAG_TYPE_RSASSAPSS:
  445. /*
  446. * For plain RSA-PSS keys, PSS parameters may be set but don't have
  447. * to, so not check.
  448. */
  449. break;
  450. default:
  451. /* Unsupported RSA key sub-type... */
  452. return NULL;
  453. }
  454. if ((rsa_tmp = rsa_new_with_ctx(gctx->libctx)) == NULL)
  455. return NULL;
  456. gctx->cb = osslcb;
  457. gctx->cbarg = cbarg;
  458. gencb = BN_GENCB_new();
  459. if (gencb != NULL)
  460. BN_GENCB_set(gencb, rsa_gencb, genctx);
  461. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  462. if (gctx->acvp_test_params != NULL) {
  463. if (!rsa_acvp_test_set_params(rsa_tmp, gctx->acvp_test_params))
  464. goto err;
  465. }
  466. #endif
  467. if (!RSA_generate_multi_prime_key(rsa_tmp,
  468. (int)gctx->nbits, (int)gctx->primes,
  469. gctx->pub_exp, gencb))
  470. goto err;
  471. if (!rsa_pss_params_30_copy(rsa_get0_pss_params_30(rsa_tmp),
  472. &gctx->pss_params))
  473. goto err;
  474. RSA_clear_flags(rsa_tmp, RSA_FLAG_TYPE_MASK);
  475. RSA_set_flags(rsa_tmp, gctx->rsa_type);
  476. rsa = rsa_tmp;
  477. rsa_tmp = NULL;
  478. err:
  479. BN_GENCB_free(gencb);
  480. RSA_free(rsa_tmp);
  481. return rsa;
  482. }
  483. static void rsa_gen_cleanup(void *genctx)
  484. {
  485. struct rsa_gen_ctx *gctx = genctx;
  486. if (gctx == NULL)
  487. return;
  488. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  489. rsa_acvp_test_gen_params_free(gctx->acvp_test_params);
  490. gctx->acvp_test_params = NULL;
  491. #endif
  492. BN_clear_free(gctx->pub_exp);
  493. OPENSSL_free(gctx);
  494. }
  495. /* For any RSA key, we use the "RSA" algorithms regardless of sub-type. */
  496. static const char *rsapss_query_operation_name(int operation_id)
  497. {
  498. return "RSA";
  499. }
  500. const OSSL_DISPATCH rsa_keymgmt_functions[] = {
  501. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
  502. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
  503. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
  504. (void (*)(void))rsa_gen_set_params },
  505. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  506. (void (*)(void))rsa_gen_settable_params },
  507. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
  508. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
  509. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
  510. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
  511. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
  512. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
  513. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))rsa_match },
  514. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
  515. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
  516. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
  517. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
  518. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
  519. { 0, NULL }
  520. };
  521. const OSSL_DISPATCH rsapss_keymgmt_functions[] = {
  522. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsapss_newdata },
  523. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsapss_gen_init },
  524. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))rsa_gen_set_params },
  525. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  526. (void (*)(void))rsapss_gen_settable_params },
  527. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
  528. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
  529. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
  530. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
  531. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
  532. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))rsa_has },
  533. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))rsa_validate },
  534. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))rsa_import },
  535. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))rsa_import_types },
  536. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))rsa_export },
  537. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))rsa_export_types },
  538. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  539. (void (*)(void))rsapss_query_operation_name },
  540. { 0, NULL }
  541. };