2
0

rsa_kmgmt.c 26 KB

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