2
0

dsa_kmgmt.c 20 KB

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