dsa_kmgmt.c 21 KB

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