dsa_kmgmt.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Copyright 2019-2024 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. * DSA 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 "prov/securitycheck.h"
  19. #include "prov/providercommon.h"
  20. #include "prov/implementations.h"
  21. #include "prov/provider_ctx.h"
  22. #include "crypto/dsa.h"
  23. #include "internal/sizes.h"
  24. #include "internal/nelem.h"
  25. #include "internal/param_build_set.h"
  26. static OSSL_FUNC_keymgmt_new_fn dsa_newdata;
  27. static OSSL_FUNC_keymgmt_free_fn dsa_freedata;
  28. static OSSL_FUNC_keymgmt_gen_init_fn dsa_gen_init;
  29. static OSSL_FUNC_keymgmt_gen_set_template_fn dsa_gen_set_template;
  30. static OSSL_FUNC_keymgmt_gen_set_params_fn dsa_gen_set_params;
  31. static OSSL_FUNC_keymgmt_gen_settable_params_fn dsa_gen_settable_params;
  32. static OSSL_FUNC_keymgmt_gen_get_params_fn dsa_gen_get_params;
  33. static OSSL_FUNC_keymgmt_gen_gettable_params_fn dsa_gen_gettable_params;
  34. static OSSL_FUNC_keymgmt_gen_fn dsa_gen;
  35. static OSSL_FUNC_keymgmt_gen_cleanup_fn dsa_gen_cleanup;
  36. static OSSL_FUNC_keymgmt_load_fn dsa_load;
  37. static OSSL_FUNC_keymgmt_get_params_fn dsa_get_params;
  38. static OSSL_FUNC_keymgmt_gettable_params_fn dsa_gettable_params;
  39. static OSSL_FUNC_keymgmt_has_fn dsa_has;
  40. static OSSL_FUNC_keymgmt_match_fn dsa_match;
  41. static OSSL_FUNC_keymgmt_validate_fn dsa_validate;
  42. static OSSL_FUNC_keymgmt_import_fn dsa_import;
  43. static OSSL_FUNC_keymgmt_import_types_fn dsa_import_types;
  44. static OSSL_FUNC_keymgmt_export_fn dsa_export;
  45. static OSSL_FUNC_keymgmt_export_types_fn dsa_export_types;
  46. static OSSL_FUNC_keymgmt_dup_fn dsa_dup;
  47. #define DSA_DEFAULT_MD "SHA256"
  48. #define DSA_POSSIBLE_SELECTIONS \
  49. (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
  50. struct dsa_gen_ctx {
  51. OSSL_LIB_CTX *libctx;
  52. FFC_PARAMS *ffc_params;
  53. int selection;
  54. /* All these parameters are used for parameter generation only */
  55. size_t pbits;
  56. size_t qbits;
  57. unsigned char *seed; /* optional FIPS186-4 param for testing */
  58. size_t seedlen;
  59. int gindex; /* optional FIPS186-4 generator index (ignored if -1) */
  60. int gen_type; /* DSA_PARAMGEN_TYPE_FIPS_186_2 or DSA_PARAMGEN_TYPE_FIPS_186_4 */
  61. int pcounter;
  62. int hindex;
  63. char *mdname;
  64. char *mdprops;
  65. OSSL_CALLBACK *cb;
  66. void *cbarg;
  67. OSSL_FIPS_IND_DECLARE
  68. };
  69. typedef struct dh_name2id_st{
  70. const char *name;
  71. int id;
  72. } DSA_GENTYPE_NAME2ID;
  73. static const DSA_GENTYPE_NAME2ID dsatype2id[] = {
  74. #ifdef FIPS_MODULE
  75. { "default", DSA_PARAMGEN_TYPE_FIPS_186_4 },
  76. #else
  77. { "default", DSA_PARAMGEN_TYPE_FIPS_DEFAULT },
  78. #endif
  79. { "fips186_4", DSA_PARAMGEN_TYPE_FIPS_186_4 },
  80. { "fips186_2", DSA_PARAMGEN_TYPE_FIPS_186_2 },
  81. };
  82. static int dsa_gen_type_name2id(const char *name)
  83. {
  84. size_t i;
  85. for (i = 0; i < OSSL_NELEM(dsatype2id); ++i) {
  86. if (OPENSSL_strcasecmp(dsatype2id[i].name, name) == 0)
  87. return dsatype2id[i].id;
  88. }
  89. return -1;
  90. }
  91. static int dsa_key_todata(DSA *dsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
  92. int include_private)
  93. {
  94. const BIGNUM *priv = NULL, *pub = NULL;
  95. if (dsa == NULL)
  96. return 0;
  97. DSA_get0_key(dsa, &pub, &priv);
  98. if (include_private
  99. && priv != NULL
  100. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  101. return 0;
  102. if (pub != NULL
  103. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
  104. return 0;
  105. return 1;
  106. }
  107. static void *dsa_newdata(void *provctx)
  108. {
  109. if (!ossl_prov_is_running())
  110. return NULL;
  111. return ossl_dsa_new(PROV_LIBCTX_OF(provctx));
  112. }
  113. static void dsa_freedata(void *keydata)
  114. {
  115. DSA_free(keydata);
  116. }
  117. static int dsa_has(const void *keydata, int selection)
  118. {
  119. const DSA *dsa = keydata;
  120. int ok = 1;
  121. if (!ossl_prov_is_running() || dsa == NULL)
  122. return 0;
  123. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  124. return 1; /* the selection is not missing */
  125. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  126. ok = ok && (DSA_get0_pub_key(dsa) != NULL);
  127. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  128. ok = ok && (DSA_get0_priv_key(dsa) != NULL);
  129. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  130. ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
  131. return ok;
  132. }
  133. static int dsa_match(const void *keydata1, const void *keydata2, int selection)
  134. {
  135. const DSA *dsa1 = keydata1;
  136. const DSA *dsa2 = keydata2;
  137. int ok = 1;
  138. if (!ossl_prov_is_running())
  139. return 0;
  140. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  141. int key_checked = 0;
  142. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  143. const BIGNUM *pa = DSA_get0_pub_key(dsa1);
  144. const BIGNUM *pb = DSA_get0_pub_key(dsa2);
  145. if (pa != NULL && pb != NULL) {
  146. ok = ok && BN_cmp(pa, pb) == 0;
  147. key_checked = 1;
  148. }
  149. }
  150. if (!key_checked
  151. && (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  152. const BIGNUM *pa = DSA_get0_priv_key(dsa1);
  153. const BIGNUM *pb = DSA_get0_priv_key(dsa2);
  154. if (pa != NULL && pb != NULL) {
  155. ok = ok && BN_cmp(pa, pb) == 0;
  156. key_checked = 1;
  157. }
  158. }
  159. ok = ok && key_checked;
  160. }
  161. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  162. FFC_PARAMS *dsaparams1 = ossl_dsa_get0_params((DSA *)dsa1);
  163. FFC_PARAMS *dsaparams2 = ossl_dsa_get0_params((DSA *)dsa2);
  164. ok = ok && ossl_ffc_params_cmp(dsaparams1, dsaparams2, 1);
  165. }
  166. return ok;
  167. }
  168. static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
  169. {
  170. DSA *dsa = keydata;
  171. int ok = 1;
  172. if (!ossl_prov_is_running() || dsa == NULL)
  173. return 0;
  174. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  175. return 0;
  176. /* a key without parameters is meaningless */
  177. ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params);
  178. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  179. int include_private =
  180. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  181. ok = ok && ossl_dsa_key_fromdata(dsa, params, include_private);
  182. }
  183. return ok;
  184. }
  185. static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
  186. void *cbarg)
  187. {
  188. DSA *dsa = keydata;
  189. OSSL_PARAM_BLD *tmpl;
  190. OSSL_PARAM *params = NULL;
  191. int ok = 1;
  192. if (!ossl_prov_is_running() || dsa == NULL)
  193. return 0;
  194. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  195. return 0;
  196. tmpl = OSSL_PARAM_BLD_new();
  197. if (tmpl == NULL)
  198. return 0;
  199. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  200. ok = ok && ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), tmpl, NULL);
  201. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  202. int include_private =
  203. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  204. ok = ok && dsa_key_todata(dsa, tmpl, NULL, include_private);
  205. }
  206. if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  207. ok = 0;
  208. goto err;
  209. }
  210. ok = param_cb(params, cbarg);
  211. OSSL_PARAM_free(params);
  212. err:
  213. OSSL_PARAM_BLD_free(tmpl);
  214. return ok;
  215. }
  216. /* IMEXPORT = IMPORT + EXPORT */
  217. # define DSA_IMEXPORTABLE_PARAMETERS \
  218. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
  219. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
  220. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
  221. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
  222. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
  223. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
  224. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
  225. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0)
  226. # define DSA_IMEXPORTABLE_PUBLIC_KEY \
  227. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
  228. # define DSA_IMEXPORTABLE_PRIVATE_KEY \
  229. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  230. static const OSSL_PARAM dsa_all_types[] = {
  231. DSA_IMEXPORTABLE_PARAMETERS,
  232. DSA_IMEXPORTABLE_PUBLIC_KEY,
  233. DSA_IMEXPORTABLE_PRIVATE_KEY,
  234. OSSL_PARAM_END
  235. };
  236. static const OSSL_PARAM dsa_parameter_types[] = {
  237. DSA_IMEXPORTABLE_PARAMETERS,
  238. OSSL_PARAM_END
  239. };
  240. static const OSSL_PARAM dsa_key_types[] = {
  241. DSA_IMEXPORTABLE_PUBLIC_KEY,
  242. DSA_IMEXPORTABLE_PRIVATE_KEY,
  243. OSSL_PARAM_END
  244. };
  245. static const OSSL_PARAM *dsa_types[] = {
  246. NULL, /* Index 0 = none of them */
  247. dsa_parameter_types, /* Index 1 = parameter types */
  248. dsa_key_types, /* Index 2 = key types */
  249. dsa_all_types /* Index 3 = 1 + 2 */
  250. };
  251. static const OSSL_PARAM *dsa_imexport_types(int selection)
  252. {
  253. int type_select = 0;
  254. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  255. type_select += 1;
  256. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  257. type_select += 2;
  258. return dsa_types[type_select];
  259. }
  260. static const OSSL_PARAM *dsa_import_types(int selection)
  261. {
  262. return dsa_imexport_types(selection);
  263. }
  264. static const OSSL_PARAM *dsa_export_types(int selection)
  265. {
  266. return dsa_imexport_types(selection);
  267. }
  268. static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
  269. {
  270. DSA *dsa = key;
  271. OSSL_PARAM *p;
  272. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  273. && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
  274. return 0;
  275. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  276. && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
  277. return 0;
  278. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  279. && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
  280. return 0;
  281. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  282. && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
  283. return 0;
  284. return ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), NULL, params)
  285. && dsa_key_todata(dsa, NULL, params, 1);
  286. }
  287. static const OSSL_PARAM dsa_params[] = {
  288. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  289. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  290. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  291. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
  292. DSA_IMEXPORTABLE_PARAMETERS,
  293. DSA_IMEXPORTABLE_PUBLIC_KEY,
  294. DSA_IMEXPORTABLE_PRIVATE_KEY,
  295. OSSL_PARAM_END
  296. };
  297. static const OSSL_PARAM *dsa_gettable_params(void *provctx)
  298. {
  299. return dsa_params;
  300. }
  301. static int dsa_validate_domparams(const DSA *dsa, int checktype)
  302. {
  303. int status = 0;
  304. return ossl_dsa_check_params(dsa, checktype, &status);
  305. }
  306. static int dsa_validate_public(const DSA *dsa)
  307. {
  308. int status = 0;
  309. const BIGNUM *pub_key = NULL;
  310. DSA_get0_key(dsa, &pub_key, NULL);
  311. if (pub_key == NULL)
  312. return 0;
  313. return ossl_dsa_check_pub_key(dsa, pub_key, &status);
  314. }
  315. static int dsa_validate_private(const DSA *dsa)
  316. {
  317. int status = 0;
  318. const BIGNUM *priv_key = NULL;
  319. DSA_get0_key(dsa, NULL, &priv_key);
  320. if (priv_key == NULL)
  321. return 0;
  322. return ossl_dsa_check_priv_key(dsa, priv_key, &status);
  323. }
  324. static int dsa_validate(const void *keydata, int selection, int checktype)
  325. {
  326. const DSA *dsa = keydata;
  327. int ok = 1;
  328. if (!ossl_prov_is_running())
  329. return 0;
  330. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  331. return 1; /* nothing to validate */
  332. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  333. ok = ok && dsa_validate_domparams(dsa, checktype);
  334. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  335. ok = ok && dsa_validate_public(dsa);
  336. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  337. ok = ok && dsa_validate_private(dsa);
  338. /* If the whole key is selected, we do a pairwise validation */
  339. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
  340. == OSSL_KEYMGMT_SELECT_KEYPAIR)
  341. ok = ok && ossl_dsa_check_pairwise(dsa);
  342. return ok;
  343. }
  344. static void *dsa_gen_init(void *provctx, int selection,
  345. const OSSL_PARAM params[])
  346. {
  347. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  348. struct dsa_gen_ctx *gctx = NULL;
  349. if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0)
  350. return NULL;
  351. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  352. gctx->selection = selection;
  353. gctx->libctx = libctx;
  354. gctx->pbits = 2048;
  355. gctx->qbits = 224;
  356. #ifdef FIPS_MODULE
  357. gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
  358. #else
  359. gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_DEFAULT;
  360. #endif
  361. gctx->gindex = -1;
  362. gctx->pcounter = -1;
  363. gctx->hindex = 0;
  364. OSSL_FIPS_IND_INIT(gctx)
  365. }
  366. if (!dsa_gen_set_params(gctx, params)) {
  367. dsa_gen_cleanup(gctx);
  368. gctx = NULL;
  369. }
  370. return gctx;
  371. }
  372. static int dsa_gen_set_template(void *genctx, void *templ)
  373. {
  374. struct dsa_gen_ctx *gctx = genctx;
  375. DSA *dsa = templ;
  376. if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL)
  377. return 0;
  378. gctx->ffc_params = ossl_dsa_get0_params(dsa);
  379. return 1;
  380. }
  381. static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
  382. size_t seedlen)
  383. {
  384. OPENSSL_clear_free(gctx->seed, gctx->seedlen);
  385. gctx->seed = NULL;
  386. gctx->seedlen = 0;
  387. if (seed != NULL && seedlen > 0) {
  388. gctx->seed = OPENSSL_memdup(seed, seedlen);
  389. if (gctx->seed == NULL)
  390. return 0;
  391. gctx->seedlen = seedlen;
  392. }
  393. return 1;
  394. }
  395. static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
  396. {
  397. struct dsa_gen_ctx *gctx = genctx;
  398. const OSSL_PARAM *p;
  399. int gen_type = -1;
  400. if (gctx == NULL)
  401. return 0;
  402. if (ossl_param_is_empty(params))
  403. return 1;
  404. if (!OSSL_FIPS_IND_SET_CTX_PARAM(gctx, OSSL_FIPS_IND_SETTABLE0, params,
  405. OSSL_PKEY_PARAM_FIPS_SIGN_CHECK))
  406. return 0;
  407. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
  408. if (p != NULL) {
  409. if (p->data_type != OSSL_PARAM_UTF8_STRING
  410. || ((gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
  411. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  412. return 0;
  413. }
  414. /*
  415. * Only assign context gen_type if it was set by dsa_gen_type_name2id
  416. * must be in range:
  417. * DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT
  418. */
  419. if (gen_type != -1)
  420. gctx->gen_type = gen_type;
  421. }
  422. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
  423. if (p != NULL
  424. && !OSSL_PARAM_get_int(p, &gctx->gindex))
  425. return 0;
  426. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
  427. if (p != NULL
  428. && !OSSL_PARAM_get_int(p, &gctx->pcounter))
  429. return 0;
  430. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
  431. if (p != NULL
  432. && !OSSL_PARAM_get_int(p, &gctx->hindex))
  433. return 0;
  434. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
  435. if (p != NULL
  436. && (p->data_type != OSSL_PARAM_OCTET_STRING
  437. || !dsa_set_gen_seed(gctx, p->data, p->data_size)))
  438. return 0;
  439. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
  440. && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
  441. return 0;
  442. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
  443. && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
  444. return 0;
  445. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
  446. if (p != NULL) {
  447. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  448. return 0;
  449. OPENSSL_free(gctx->mdname);
  450. gctx->mdname = OPENSSL_strdup(p->data);
  451. if (gctx->mdname == NULL)
  452. return 0;
  453. }
  454. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
  455. if (p != NULL) {
  456. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  457. return 0;
  458. OPENSSL_free(gctx->mdprops);
  459. gctx->mdprops = OPENSSL_strdup(p->data);
  460. if (gctx->mdprops == NULL)
  461. return 0;
  462. }
  463. return 1;
  464. }
  465. static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx,
  466. ossl_unused void *provctx)
  467. {
  468. static OSSL_PARAM settable[] = {
  469. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
  470. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
  471. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
  472. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
  473. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
  474. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
  475. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
  476. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
  477. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
  478. OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_PKEY_PARAM_FIPS_SIGN_CHECK)
  479. OSSL_PARAM_END
  480. };
  481. return settable;
  482. }
  483. static int dsa_gen_get_params(void *genctx, OSSL_PARAM *params)
  484. {
  485. struct dsa_gen_ctx *gctx = genctx;
  486. if (gctx == NULL)
  487. return 0;
  488. if (ossl_param_is_empty(params))
  489. return 1;
  490. if (!OSSL_FIPS_IND_GET_CTX_PARAM(gctx, params))
  491. return 0;
  492. return 1;
  493. }
  494. static const OSSL_PARAM *dsa_gen_gettable_params(ossl_unused void *ctx,
  495. ossl_unused void *provctx)
  496. {
  497. static const OSSL_PARAM dsa_gen_gettable_params_table[] = {
  498. OSSL_FIPS_IND_GETTABLE_CTX_PARAM()
  499. OSSL_PARAM_END
  500. };
  501. return dsa_gen_gettable_params_table;
  502. }
  503. static int dsa_gencb(int p, int n, BN_GENCB *cb)
  504. {
  505. struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
  506. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  507. params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
  508. params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
  509. return gctx->cb(params, gctx->cbarg);
  510. }
  511. static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  512. {
  513. struct dsa_gen_ctx *gctx = genctx;
  514. DSA *dsa = NULL;
  515. BN_GENCB *gencb = NULL;
  516. int ret = 0;
  517. FFC_PARAMS *ffc;
  518. if (!ossl_prov_is_running() || gctx == NULL)
  519. return NULL;
  520. #ifdef FIPS_MODULE
  521. /*
  522. * DSA signing is not approved in FIPS 140-3, so there is no
  523. * need for DSA keygen either.
  524. */
  525. if (!OSSL_FIPS_IND_ON_UNAPPROVED(gctx, OSSL_FIPS_IND_SETTABLE0,
  526. gctx->libctx, "DSA", "Keygen",
  527. ossl_fips_config_dsa_sign_disallowed))
  528. return 0;
  529. #endif
  530. dsa = ossl_dsa_new(gctx->libctx);
  531. if (dsa == NULL)
  532. return NULL;
  533. if (gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_DEFAULT)
  534. gctx->gen_type = (gctx->pbits >= 2048 ? DSA_PARAMGEN_TYPE_FIPS_186_4 :
  535. DSA_PARAMGEN_TYPE_FIPS_186_2);
  536. /*
  537. * Do a bounds check on context gen_type. Must be in range:
  538. * DSA_PARAMGEN_TYPE_FIPS_186_4 <= gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT
  539. * Noted here as this needs to be adjusted if a new type is
  540. * added.
  541. */
  542. if (!ossl_assert((gctx->gen_type >= DSA_PARAMGEN_TYPE_FIPS_186_4)
  543. && (gctx->gen_type <= DSA_PARAMGEN_TYPE_FIPS_DEFAULT))) {
  544. ERR_raise_data(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR,
  545. "gen_type set to unsupported value %d", gctx->gen_type);
  546. goto end;
  547. }
  548. gctx->cb = osslcb;
  549. gctx->cbarg = cbarg;
  550. gencb = BN_GENCB_new();
  551. if (gencb != NULL)
  552. BN_GENCB_set(gencb, dsa_gencb, genctx);
  553. ffc = ossl_dsa_get0_params(dsa);
  554. /* Copy the template value if one was passed */
  555. if (gctx->ffc_params != NULL
  556. && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
  557. goto end;
  558. if (gctx->seed != NULL
  559. && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
  560. goto end;
  561. if (gctx->gindex != -1) {
  562. ossl_ffc_params_set_gindex(ffc, gctx->gindex);
  563. if (gctx->pcounter != -1)
  564. ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
  565. } else if (gctx->hindex != 0) {
  566. ossl_ffc_params_set_h(ffc, gctx->hindex);
  567. }
  568. if (gctx->mdname != NULL)
  569. ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
  570. if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  571. if (ossl_dsa_generate_ffc_parameters(dsa, gctx->gen_type,
  572. gctx->pbits, gctx->qbits,
  573. gencb) <= 0)
  574. goto end;
  575. }
  576. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
  577. gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_186_2);
  578. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  579. if (ffc->p == NULL
  580. || ffc->q == NULL
  581. || ffc->g == NULL)
  582. goto end;
  583. if (DSA_generate_key(dsa) <= 0)
  584. goto end;
  585. }
  586. ret = 1;
  587. end:
  588. if (ret <= 0) {
  589. DSA_free(dsa);
  590. dsa = NULL;
  591. }
  592. BN_GENCB_free(gencb);
  593. return dsa;
  594. }
  595. static void dsa_gen_cleanup(void *genctx)
  596. {
  597. struct dsa_gen_ctx *gctx = genctx;
  598. if (gctx == NULL)
  599. return;
  600. OPENSSL_free(gctx->mdname);
  601. OPENSSL_free(gctx->mdprops);
  602. OPENSSL_clear_free(gctx->seed, gctx->seedlen);
  603. OPENSSL_free(gctx);
  604. }
  605. static void *dsa_load(const void *reference, size_t reference_sz)
  606. {
  607. DSA *dsa = NULL;
  608. if (ossl_prov_is_running() && reference_sz == sizeof(dsa)) {
  609. /* The contents of the reference is the address to our object */
  610. dsa = *(DSA **)reference;
  611. /* We grabbed, so we detach it */
  612. *(DSA **)reference = NULL;
  613. return dsa;
  614. }
  615. return NULL;
  616. }
  617. static void *dsa_dup(const void *keydata_from, int selection)
  618. {
  619. if (ossl_prov_is_running())
  620. return ossl_dsa_dup(keydata_from, selection);
  621. return NULL;
  622. }
  623. const OSSL_DISPATCH ossl_dsa_keymgmt_functions[] = {
  624. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dsa_newdata },
  625. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dsa_gen_init },
  626. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dsa_gen_set_template },
  627. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dsa_gen_set_params },
  628. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  629. (void (*)(void))dsa_gen_settable_params },
  630. { OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))dsa_gen_get_params },
  631. { OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
  632. (void (*)(void))dsa_gen_gettable_params },
  633. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dsa_gen },
  634. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dsa_gen_cleanup },
  635. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dsa_load },
  636. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dsa_freedata },
  637. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dsa_get_params },
  638. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dsa_gettable_params },
  639. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dsa_has },
  640. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dsa_match },
  641. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dsa_validate },
  642. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dsa_import },
  643. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dsa_import_types },
  644. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dsa_export },
  645. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dsa_export_types },
  646. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dsa_dup },
  647. OSSL_DISPATCH_END
  648. };