2
0

dsa_kmgmt.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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. /* a key without parameters is meaningless */
  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. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  192. return 0;
  193. tmpl = OSSL_PARAM_BLD_new();
  194. if (tmpl == NULL)
  195. return 0;
  196. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  197. ok = ok && ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), tmpl, NULL);
  198. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  199. int include_private =
  200. selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
  201. ok = ok && dsa_key_todata(dsa, tmpl, NULL, include_private);
  202. }
  203. if (!ok || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  204. ok = 0;
  205. goto err;
  206. }
  207. ok = param_cb(params, cbarg);
  208. OSSL_PARAM_free(params);
  209. err:
  210. OSSL_PARAM_BLD_free(tmpl);
  211. return ok;
  212. }
  213. /* IMEXPORT = IMPORT + EXPORT */
  214. # define DSA_IMEXPORTABLE_PARAMETERS \
  215. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
  216. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
  217. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
  218. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
  219. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
  220. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
  221. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
  222. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0)
  223. # define DSA_IMEXPORTABLE_PUBLIC_KEY \
  224. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
  225. # define DSA_IMEXPORTABLE_PRIVATE_KEY \
  226. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  227. static const OSSL_PARAM dsa_all_types[] = {
  228. DSA_IMEXPORTABLE_PARAMETERS,
  229. DSA_IMEXPORTABLE_PUBLIC_KEY,
  230. DSA_IMEXPORTABLE_PRIVATE_KEY,
  231. OSSL_PARAM_END
  232. };
  233. static const OSSL_PARAM dsa_parameter_types[] = {
  234. DSA_IMEXPORTABLE_PARAMETERS,
  235. OSSL_PARAM_END
  236. };
  237. static const OSSL_PARAM dsa_key_types[] = {
  238. DSA_IMEXPORTABLE_PUBLIC_KEY,
  239. DSA_IMEXPORTABLE_PRIVATE_KEY,
  240. OSSL_PARAM_END
  241. };
  242. static const OSSL_PARAM *dsa_types[] = {
  243. NULL, /* Index 0 = none of them */
  244. dsa_parameter_types, /* Index 1 = parameter types */
  245. dsa_key_types, /* Index 2 = key types */
  246. dsa_all_types /* Index 3 = 1 + 2 */
  247. };
  248. static const OSSL_PARAM *dsa_imexport_types(int selection)
  249. {
  250. int type_select = 0;
  251. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  252. type_select += 1;
  253. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  254. type_select += 2;
  255. return dsa_types[type_select];
  256. }
  257. static const OSSL_PARAM *dsa_import_types(int selection)
  258. {
  259. return dsa_imexport_types(selection);
  260. }
  261. static const OSSL_PARAM *dsa_export_types(int selection)
  262. {
  263. return dsa_imexport_types(selection);
  264. }
  265. static ossl_inline int dsa_get_params(void *key, OSSL_PARAM params[])
  266. {
  267. DSA *dsa = key;
  268. OSSL_PARAM *p;
  269. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  270. && !OSSL_PARAM_set_int(p, DSA_bits(dsa)))
  271. return 0;
  272. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  273. && !OSSL_PARAM_set_int(p, DSA_security_bits(dsa)))
  274. return 0;
  275. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  276. && !OSSL_PARAM_set_int(p, DSA_size(dsa)))
  277. return 0;
  278. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_DEFAULT_DIGEST)) != NULL
  279. && !OSSL_PARAM_set_utf8_string(p, DSA_DEFAULT_MD))
  280. return 0;
  281. return ossl_ffc_params_todata(ossl_dsa_get0_params(dsa), NULL, params)
  282. && dsa_key_todata(dsa, NULL, params, 1);
  283. }
  284. static const OSSL_PARAM dsa_params[] = {
  285. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  286. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  287. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  288. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
  289. DSA_IMEXPORTABLE_PARAMETERS,
  290. DSA_IMEXPORTABLE_PUBLIC_KEY,
  291. DSA_IMEXPORTABLE_PRIVATE_KEY,
  292. OSSL_PARAM_END
  293. };
  294. static const OSSL_PARAM *dsa_gettable_params(void *provctx)
  295. {
  296. return dsa_params;
  297. }
  298. static int dsa_validate_domparams(const DSA *dsa, int checktype)
  299. {
  300. int status = 0;
  301. return ossl_dsa_check_params(dsa, checktype, &status);
  302. }
  303. static int dsa_validate_public(const DSA *dsa)
  304. {
  305. int status = 0;
  306. const BIGNUM *pub_key = NULL;
  307. DSA_get0_key(dsa, &pub_key, NULL);
  308. if (pub_key == NULL)
  309. return 0;
  310. return ossl_dsa_check_pub_key(dsa, pub_key, &status);
  311. }
  312. static int dsa_validate_private(const DSA *dsa)
  313. {
  314. int status = 0;
  315. const BIGNUM *priv_key = NULL;
  316. DSA_get0_key(dsa, NULL, &priv_key);
  317. if (priv_key == NULL)
  318. return 0;
  319. return ossl_dsa_check_priv_key(dsa, priv_key, &status);
  320. }
  321. static int dsa_validate(const void *keydata, int selection, int checktype)
  322. {
  323. const DSA *dsa = keydata;
  324. int ok = 1;
  325. if (!ossl_prov_is_running())
  326. return 0;
  327. if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
  328. return 1; /* nothing to validate */
  329. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  330. ok = ok && dsa_validate_domparams(dsa, checktype);
  331. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  332. ok = ok && dsa_validate_public(dsa);
  333. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  334. ok = ok && dsa_validate_private(dsa);
  335. /* If the whole key is selected, we do a pairwise validation */
  336. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
  337. == OSSL_KEYMGMT_SELECT_KEYPAIR)
  338. ok = ok && ossl_dsa_check_pairwise(dsa);
  339. return ok;
  340. }
  341. static void *dsa_gen_init(void *provctx, int selection,
  342. const OSSL_PARAM params[])
  343. {
  344. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  345. struct dsa_gen_ctx *gctx = NULL;
  346. if (!ossl_prov_is_running() || (selection & DSA_POSSIBLE_SELECTIONS) == 0)
  347. return NULL;
  348. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  349. gctx->selection = selection;
  350. gctx->libctx = libctx;
  351. gctx->pbits = 2048;
  352. gctx->qbits = 224;
  353. #ifdef FIPS_MODULE
  354. gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_186_4;
  355. #else
  356. gctx->gen_type = DSA_PARAMGEN_TYPE_FIPS_DEFAULT;
  357. #endif
  358. gctx->gindex = -1;
  359. gctx->pcounter = -1;
  360. gctx->hindex = 0;
  361. }
  362. if (!dsa_gen_set_params(gctx, params)) {
  363. OPENSSL_free(gctx);
  364. gctx = NULL;
  365. }
  366. return gctx;
  367. }
  368. static int dsa_gen_set_template(void *genctx, void *templ)
  369. {
  370. struct dsa_gen_ctx *gctx = genctx;
  371. DSA *dsa = templ;
  372. if (!ossl_prov_is_running() || gctx == NULL || dsa == NULL)
  373. return 0;
  374. gctx->ffc_params = ossl_dsa_get0_params(dsa);
  375. return 1;
  376. }
  377. static int dsa_set_gen_seed(struct dsa_gen_ctx *gctx, unsigned char *seed,
  378. size_t seedlen)
  379. {
  380. OPENSSL_clear_free(gctx->seed, gctx->seedlen);
  381. gctx->seed = NULL;
  382. gctx->seedlen = 0;
  383. if (seed != NULL && seedlen > 0) {
  384. gctx->seed = OPENSSL_memdup(seed, seedlen);
  385. if (gctx->seed == NULL)
  386. return 0;
  387. gctx->seedlen = seedlen;
  388. }
  389. return 1;
  390. }
  391. static int dsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
  392. {
  393. struct dsa_gen_ctx *gctx = genctx;
  394. const OSSL_PARAM *p;
  395. if (gctx == NULL)
  396. return 0;
  397. if (params == NULL)
  398. return 1;
  399. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
  400. if (p != NULL) {
  401. if (p->data_type != OSSL_PARAM_UTF8_STRING
  402. || ((gctx->gen_type = dsa_gen_type_name2id(p->data)) == -1)) {
  403. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  404. return 0;
  405. }
  406. }
  407. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
  408. if (p != NULL
  409. && !OSSL_PARAM_get_int(p, &gctx->gindex))
  410. return 0;
  411. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
  412. if (p != NULL
  413. && !OSSL_PARAM_get_int(p, &gctx->pcounter))
  414. return 0;
  415. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
  416. if (p != NULL
  417. && !OSSL_PARAM_get_int(p, &gctx->hindex))
  418. return 0;
  419. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
  420. if (p != NULL
  421. && (p->data_type != OSSL_PARAM_OCTET_STRING
  422. || !dsa_set_gen_seed(gctx, p->data, p->data_size)))
  423. return 0;
  424. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
  425. && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
  426. return 0;
  427. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
  428. && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
  429. return 0;
  430. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
  431. if (p != NULL) {
  432. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  433. return 0;
  434. OPENSSL_free(gctx->mdname);
  435. gctx->mdname = OPENSSL_strdup(p->data);
  436. if (gctx->mdname == NULL)
  437. return 0;
  438. }
  439. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
  440. if (p != NULL) {
  441. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  442. return 0;
  443. OPENSSL_free(gctx->mdprops);
  444. gctx->mdprops = OPENSSL_strdup(p->data);
  445. if (gctx->mdprops == NULL)
  446. return 0;
  447. }
  448. return 1;
  449. }
  450. static const OSSL_PARAM *dsa_gen_settable_params(ossl_unused void *genctx,
  451. ossl_unused void *provctx)
  452. {
  453. static OSSL_PARAM settable[] = {
  454. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
  455. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
  456. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
  457. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
  458. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
  459. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
  460. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
  461. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
  462. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
  463. OSSL_PARAM_END
  464. };
  465. return settable;
  466. }
  467. static int dsa_gencb(int p, int n, BN_GENCB *cb)
  468. {
  469. struct dsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
  470. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  471. params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
  472. params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
  473. return gctx->cb(params, gctx->cbarg);
  474. }
  475. static void *dsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  476. {
  477. struct dsa_gen_ctx *gctx = genctx;
  478. DSA *dsa = NULL;
  479. BN_GENCB *gencb = NULL;
  480. int ret = 0;
  481. FFC_PARAMS *ffc;
  482. if (!ossl_prov_is_running() || gctx == NULL)
  483. return NULL;
  484. dsa = ossl_dsa_new(gctx->libctx);
  485. if (dsa == NULL)
  486. return NULL;
  487. if (gctx->gen_type == DSA_PARAMGEN_TYPE_FIPS_DEFAULT)
  488. gctx->gen_type = (gctx->pbits >= 2048 ? DSA_PARAMGEN_TYPE_FIPS_186_4 :
  489. DSA_PARAMGEN_TYPE_FIPS_186_2);
  490. gctx->cb = osslcb;
  491. gctx->cbarg = cbarg;
  492. gencb = BN_GENCB_new();
  493. if (gencb != NULL)
  494. BN_GENCB_set(gencb, dsa_gencb, genctx);
  495. ffc = ossl_dsa_get0_params(dsa);
  496. /* Copy the template value if one was passed */
  497. if (gctx->ffc_params != NULL
  498. && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
  499. goto end;
  500. if (gctx->seed != NULL
  501. && !ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
  502. goto end;
  503. if (gctx->gindex != -1) {
  504. ossl_ffc_params_set_gindex(ffc, gctx->gindex);
  505. if (gctx->pcounter != -1)
  506. ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
  507. } else if (gctx->hindex != 0) {
  508. ossl_ffc_params_set_h(ffc, gctx->hindex);
  509. }
  510. if (gctx->mdname != NULL)
  511. ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops);
  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. OSSL_DISPATCH_END
  587. };