dh_kmgmt.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h> /* strcmp */
  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/implementations.h"
  20. #include "prov/providercommon.h"
  21. #include "prov/provider_ctx.h"
  22. #include "crypto/dh.h"
  23. #include "internal/sizes.h"
  24. static OSSL_FUNC_keymgmt_new_fn dh_newdata;
  25. static OSSL_FUNC_keymgmt_free_fn dh_freedata;
  26. static OSSL_FUNC_keymgmt_gen_init_fn dh_gen_init;
  27. static OSSL_FUNC_keymgmt_gen_init_fn dhx_gen_init;
  28. static OSSL_FUNC_keymgmt_gen_set_template_fn dh_gen_set_template;
  29. static OSSL_FUNC_keymgmt_gen_set_params_fn dh_gen_set_params;
  30. static OSSL_FUNC_keymgmt_gen_settable_params_fn dh_gen_settable_params;
  31. static OSSL_FUNC_keymgmt_gen_fn dh_gen;
  32. static OSSL_FUNC_keymgmt_gen_cleanup_fn dh_gen_cleanup;
  33. static OSSL_FUNC_keymgmt_load_fn dh_load;
  34. static OSSL_FUNC_keymgmt_get_params_fn dh_get_params;
  35. static OSSL_FUNC_keymgmt_gettable_params_fn dh_gettable_params;
  36. static OSSL_FUNC_keymgmt_set_params_fn dh_set_params;
  37. static OSSL_FUNC_keymgmt_settable_params_fn dh_settable_params;
  38. static OSSL_FUNC_keymgmt_has_fn dh_has;
  39. static OSSL_FUNC_keymgmt_match_fn dh_match;
  40. static OSSL_FUNC_keymgmt_validate_fn dh_validate;
  41. static OSSL_FUNC_keymgmt_import_fn dh_import;
  42. static OSSL_FUNC_keymgmt_import_types_fn dh_import_types;
  43. static OSSL_FUNC_keymgmt_export_fn dh_export;
  44. static OSSL_FUNC_keymgmt_export_types_fn dh_export_types;
  45. static OSSL_FUNC_keymgmt_dup_fn dh_dup;
  46. #define DH_POSSIBLE_SELECTIONS \
  47. (OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
  48. struct dh_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. /* If there is a group name then the remaining parameters are not needed */
  54. int group_nid;
  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; /* see dhtype2id */
  61. int generator; /* Used by DH_PARAMGEN_TYPE_GENERATOR in non fips mode only */
  62. int pcounter;
  63. int hindex;
  64. int priv_len;
  65. char *mdname;
  66. char *mdprops;
  67. OSSL_CALLBACK *cb;
  68. void *cbarg;
  69. int dh_type;
  70. };
  71. static int dh_gen_type_name2id_w_default(const char *name, int type)
  72. {
  73. if (strcmp(name, "default") == 0) {
  74. #ifdef FIPS_MODULE
  75. if (type == DH_FLAG_TYPE_DHX)
  76. return DH_PARAMGEN_TYPE_FIPS_186_4;
  77. return DH_PARAMGEN_TYPE_GROUP;
  78. #else
  79. if (type == DH_FLAG_TYPE_DHX)
  80. return DH_PARAMGEN_TYPE_FIPS_186_2;
  81. return DH_PARAMGEN_TYPE_GENERATOR;
  82. #endif
  83. }
  84. return ossl_dh_gen_type_name2id(name);
  85. }
  86. static void *dh_newdata(void *provctx)
  87. {
  88. DH *dh = NULL;
  89. if (ossl_prov_is_running()) {
  90. dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
  91. if (dh != NULL) {
  92. DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
  93. DH_set_flags(dh, DH_FLAG_TYPE_DH);
  94. }
  95. }
  96. return dh;
  97. }
  98. static void *dhx_newdata(void *provctx)
  99. {
  100. DH *dh = NULL;
  101. dh = ossl_dh_new_ex(PROV_LIBCTX_OF(provctx));
  102. if (dh != NULL) {
  103. DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
  104. DH_set_flags(dh, DH_FLAG_TYPE_DHX);
  105. }
  106. return dh;
  107. }
  108. static void dh_freedata(void *keydata)
  109. {
  110. DH_free(keydata);
  111. }
  112. static int dh_has(const void *keydata, int selection)
  113. {
  114. const DH *dh = keydata;
  115. int ok = 1;
  116. if (!ossl_prov_is_running() || dh == NULL)
  117. return 0;
  118. if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
  119. return 1; /* the selection is not missing */
  120. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  121. ok = ok && (DH_get0_pub_key(dh) != NULL);
  122. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  123. ok = ok && (DH_get0_priv_key(dh) != NULL);
  124. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
  125. ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
  126. return ok;
  127. }
  128. static int dh_match(const void *keydata1, const void *keydata2, int selection)
  129. {
  130. const DH *dh1 = keydata1;
  131. const DH *dh2 = keydata2;
  132. int ok = 1;
  133. if (!ossl_prov_is_running())
  134. return 0;
  135. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  136. ok = ok && BN_cmp(DH_get0_pub_key(dh1), DH_get0_pub_key(dh2)) == 0;
  137. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  138. ok = ok && BN_cmp(DH_get0_priv_key(dh1), DH_get0_priv_key(dh2)) == 0;
  139. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  140. FFC_PARAMS *dhparams1 = ossl_dh_get0_params((DH *)dh1);
  141. FFC_PARAMS *dhparams2 = ossl_dh_get0_params((DH *)dh2);
  142. ok = ok && ossl_ffc_params_cmp(dhparams1, dhparams2, 1);
  143. }
  144. return ok;
  145. }
  146. static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
  147. {
  148. DH *dh = keydata;
  149. int ok = 1;
  150. if (!ossl_prov_is_running() || dh == NULL)
  151. return 0;
  152. if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
  153. return 0;
  154. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  155. ok = ok && ossl_dh_params_fromdata(dh, params);
  156. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  157. ok = ok && ossl_dh_key_fromdata(dh, params);
  158. return ok;
  159. }
  160. static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
  161. void *cbarg)
  162. {
  163. DH *dh = keydata;
  164. OSSL_PARAM_BLD *tmpl = NULL;
  165. OSSL_PARAM *params = NULL;
  166. int ok = 1;
  167. if (!ossl_prov_is_running() || dh == NULL)
  168. return 0;
  169. tmpl = OSSL_PARAM_BLD_new();
  170. if (tmpl == NULL)
  171. return 0;
  172. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  173. ok = ok && ossl_dh_params_todata(dh, tmpl, NULL);
  174. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  175. ok = ok && ossl_dh_key_todata(dh, tmpl, NULL);
  176. if (!ok
  177. || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  178. ok = 0;
  179. goto err;
  180. }
  181. ok = param_cb(params, cbarg);
  182. OSSL_PARAM_free(params);
  183. err:
  184. OSSL_PARAM_BLD_free(tmpl);
  185. return ok;
  186. }
  187. /* IMEXPORT = IMPORT + EXPORT */
  188. # define DH_IMEXPORTABLE_PARAMETERS \
  189. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_P, NULL, 0), \
  190. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_Q, NULL, 0), \
  191. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_G, NULL, 0), \
  192. OSSL_PARAM_BN(OSSL_PKEY_PARAM_FFC_COFACTOR, NULL, 0), \
  193. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL), \
  194. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL), \
  195. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL), \
  196. OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL), \
  197. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0), \
  198. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0)
  199. # define DH_IMEXPORTABLE_PUBLIC_KEY \
  200. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0)
  201. # define DH_IMEXPORTABLE_PRIVATE_KEY \
  202. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
  203. static const OSSL_PARAM dh_all_types[] = {
  204. DH_IMEXPORTABLE_PARAMETERS,
  205. DH_IMEXPORTABLE_PUBLIC_KEY,
  206. DH_IMEXPORTABLE_PRIVATE_KEY,
  207. OSSL_PARAM_END
  208. };
  209. static const OSSL_PARAM dh_parameter_types[] = {
  210. DH_IMEXPORTABLE_PARAMETERS,
  211. OSSL_PARAM_END
  212. };
  213. static const OSSL_PARAM dh_key_types[] = {
  214. DH_IMEXPORTABLE_PUBLIC_KEY,
  215. DH_IMEXPORTABLE_PRIVATE_KEY,
  216. OSSL_PARAM_END
  217. };
  218. static const OSSL_PARAM *dh_types[] = {
  219. NULL, /* Index 0 = none of them */
  220. dh_parameter_types, /* Index 1 = parameter types */
  221. dh_key_types, /* Index 2 = key types */
  222. dh_all_types /* Index 3 = 1 + 2 */
  223. };
  224. static const OSSL_PARAM *dh_imexport_types(int selection)
  225. {
  226. int type_select = 0;
  227. if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
  228. type_select += 1;
  229. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
  230. type_select += 2;
  231. return dh_types[type_select];
  232. }
  233. static const OSSL_PARAM *dh_import_types(int selection)
  234. {
  235. return dh_imexport_types(selection);
  236. }
  237. static const OSSL_PARAM *dh_export_types(int selection)
  238. {
  239. return dh_imexport_types(selection);
  240. }
  241. static ossl_inline int dh_get_params(void *key, OSSL_PARAM params[])
  242. {
  243. DH *dh = key;
  244. OSSL_PARAM *p;
  245. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  246. && !OSSL_PARAM_set_int(p, DH_bits(dh)))
  247. return 0;
  248. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  249. && !OSSL_PARAM_set_int(p, DH_security_bits(dh)))
  250. return 0;
  251. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
  252. && !OSSL_PARAM_set_int(p, DH_size(dh)))
  253. return 0;
  254. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
  255. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  256. return 0;
  257. p->return_size = ossl_dh_key2buf(dh, (unsigned char **)&p->data,
  258. p->data_size, 0);
  259. if (p->return_size == 0)
  260. return 0;
  261. }
  262. return ossl_dh_params_todata(dh, NULL, params)
  263. && ossl_dh_key_todata(dh, NULL, params);
  264. }
  265. static const OSSL_PARAM dh_params[] = {
  266. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  267. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  268. OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
  269. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  270. DH_IMEXPORTABLE_PARAMETERS,
  271. DH_IMEXPORTABLE_PUBLIC_KEY,
  272. DH_IMEXPORTABLE_PRIVATE_KEY,
  273. OSSL_PARAM_END
  274. };
  275. static const OSSL_PARAM *dh_gettable_params(void *provctx)
  276. {
  277. return dh_params;
  278. }
  279. static const OSSL_PARAM dh_known_settable_params[] = {
  280. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  281. OSSL_PARAM_END
  282. };
  283. static const OSSL_PARAM *dh_settable_params(void *provctx)
  284. {
  285. return dh_known_settable_params;
  286. }
  287. static int dh_set_params(void *key, const OSSL_PARAM params[])
  288. {
  289. DH *dh = key;
  290. const OSSL_PARAM *p;
  291. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  292. if (p != NULL
  293. && (p->data_type != OSSL_PARAM_OCTET_STRING
  294. || !ossl_dh_buf2key(dh, p->data, p->data_size)))
  295. return 0;
  296. return 1;
  297. }
  298. static int dh_validate_public(const DH *dh, int checktype)
  299. {
  300. const BIGNUM *pub_key = NULL;
  301. int res = 0;
  302. DH_get0_key(dh, &pub_key, NULL);
  303. if (pub_key == NULL)
  304. return 0;
  305. /* The partial test is only valid for named group's with q = (p - 1) / 2 */
  306. if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK
  307. && ossl_dh_is_named_safe_prime_group(dh))
  308. return ossl_dh_check_pub_key_partial(dh, pub_key, &res);
  309. return DH_check_pub_key(dh, pub_key, &res);
  310. }
  311. static int dh_validate_private(const DH *dh)
  312. {
  313. int status = 0;
  314. const BIGNUM *priv_key = NULL;
  315. DH_get0_key(dh, NULL, &priv_key);
  316. if (priv_key == NULL)
  317. return 0;
  318. return ossl_dh_check_priv_key(dh, priv_key, &status);;
  319. }
  320. static int dh_validate(const void *keydata, int selection, int checktype)
  321. {
  322. const DH *dh = keydata;
  323. int ok = 1;
  324. if (!ossl_prov_is_running())
  325. return 0;
  326. if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
  327. return 1; /* nothing to validate */
  328. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  329. /*
  330. * Both of these functions check parameters. DH_check_params_ex()
  331. * performs a lightweight check (e.g. it does not check that p is a
  332. * safe prime)
  333. */
  334. if (checktype == OSSL_KEYMGMT_VALIDATE_QUICK_CHECK)
  335. ok = ok && DH_check_params_ex(dh);
  336. else
  337. ok = ok && DH_check_ex(dh);
  338. }
  339. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  340. ok = ok && dh_validate_public(dh, checktype);
  341. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  342. ok = ok && dh_validate_private(dh);
  343. if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR)
  344. == OSSL_KEYMGMT_SELECT_KEYPAIR)
  345. ok = ok && ossl_dh_check_pairwise(dh);
  346. return ok;
  347. }
  348. static void *dh_gen_init_base(void *provctx, int selection,
  349. const OSSL_PARAM params[], int type)
  350. {
  351. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
  352. struct dh_gen_ctx *gctx = NULL;
  353. if (!ossl_prov_is_running())
  354. return NULL;
  355. if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
  356. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
  357. return NULL;
  358. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
  359. gctx->selection = selection;
  360. gctx->libctx = libctx;
  361. gctx->pbits = 2048;
  362. gctx->qbits = 224;
  363. gctx->mdname = NULL;
  364. #ifdef FIPS_MODULE
  365. gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
  366. ? DH_PARAMGEN_TYPE_FIPS_186_4
  367. : DH_PARAMGEN_TYPE_GROUP;
  368. #else
  369. gctx->gen_type = (type == DH_FLAG_TYPE_DHX)
  370. ? DH_PARAMGEN_TYPE_FIPS_186_2
  371. : DH_PARAMGEN_TYPE_GENERATOR;
  372. #endif
  373. gctx->gindex = -1;
  374. gctx->hindex = 0;
  375. gctx->pcounter = -1;
  376. gctx->generator = DH_GENERATOR_2;
  377. gctx->dh_type = type;
  378. }
  379. if (!dh_gen_set_params(gctx, params)) {
  380. OPENSSL_free(gctx);
  381. gctx = NULL;
  382. }
  383. return gctx;
  384. }
  385. static void *dh_gen_init(void *provctx, int selection,
  386. const OSSL_PARAM params[])
  387. {
  388. return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DH);
  389. }
  390. static void *dhx_gen_init(void *provctx, int selection,
  391. const OSSL_PARAM params[])
  392. {
  393. return dh_gen_init_base(provctx, selection, params, DH_FLAG_TYPE_DHX);
  394. }
  395. static int dh_gen_set_template(void *genctx, void *templ)
  396. {
  397. struct dh_gen_ctx *gctx = genctx;
  398. DH *dh = templ;
  399. if (!ossl_prov_is_running() || gctx == NULL || dh == NULL)
  400. return 0;
  401. gctx->ffc_params = ossl_dh_get0_params(dh);
  402. return 1;
  403. }
  404. static int dh_set_gen_seed(struct dh_gen_ctx *gctx, unsigned char *seed,
  405. size_t seedlen)
  406. {
  407. OPENSSL_clear_free(gctx->seed, gctx->seedlen);
  408. gctx->seed = NULL;
  409. gctx->seedlen = 0;
  410. if (seed != NULL && seedlen > 0) {
  411. gctx->seed = OPENSSL_memdup(seed, seedlen);
  412. if (gctx->seed == NULL)
  413. return 0;
  414. gctx->seedlen = seedlen;
  415. }
  416. return 1;
  417. }
  418. static int dh_gen_set_params(void *genctx, const OSSL_PARAM params[])
  419. {
  420. struct dh_gen_ctx *gctx = genctx;
  421. const OSSL_PARAM *p;
  422. if (gctx == NULL)
  423. return 0;
  424. if (params == NULL)
  425. return 1;
  426. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_TYPE);
  427. if (p != NULL) {
  428. if (p->data_type != OSSL_PARAM_UTF8_STRING
  429. || ((gctx->gen_type =
  430. dh_gen_type_name2id_w_default(p->data, gctx->dh_type)) == -1)) {
  431. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  432. return 0;
  433. }
  434. }
  435. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  436. if (p != NULL) {
  437. const DH_NAMED_GROUP *group = NULL;
  438. if (p->data_type != OSSL_PARAM_UTF8_STRING
  439. || (group = ossl_ffc_name_to_dh_named_group(p->data)) == NULL
  440. || ((gctx->group_nid =
  441. ossl_ffc_named_group_get_uid(group)) == NID_undef)) {
  442. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  443. return 0;
  444. }
  445. gctx->gen_type = DH_PARAMGEN_TYPE_GROUP;
  446. }
  447. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_GENERATOR);
  448. if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->generator))
  449. return 0;
  450. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_GINDEX);
  451. if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->gindex))
  452. return 0;
  453. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PCOUNTER);
  454. if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->pcounter))
  455. return 0;
  456. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_H);
  457. if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->hindex))
  458. return 0;
  459. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_SEED);
  460. if (p != NULL
  461. && (p->data_type != OSSL_PARAM_OCTET_STRING
  462. || !dh_set_gen_seed(gctx, p->data, p->data_size)))
  463. return 0;
  464. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_PBITS)) != NULL
  465. && !OSSL_PARAM_get_size_t(p, &gctx->pbits))
  466. return 0;
  467. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_QBITS)) != NULL
  468. && !OSSL_PARAM_get_size_t(p, &gctx->qbits))
  469. return 0;
  470. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST);
  471. if (p != NULL) {
  472. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  473. return 0;
  474. OPENSSL_free(gctx->mdname);
  475. gctx->mdname = OPENSSL_strdup(p->data);
  476. if (gctx->mdname == NULL)
  477. return 0;
  478. }
  479. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_DIGEST_PROPS);
  480. if (p != NULL) {
  481. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  482. return 0;
  483. OPENSSL_free(gctx->mdprops);
  484. gctx->mdprops = OPENSSL_strdup(p->data);
  485. if (gctx->mdprops == NULL)
  486. return 0;
  487. }
  488. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
  489. if (p != NULL && !OSSL_PARAM_get_int(p, &gctx->priv_len))
  490. return 0;
  491. return 1;
  492. }
  493. static const OSSL_PARAM *dh_gen_settable_params(ossl_unused void *genctx,
  494. ossl_unused void *provctx)
  495. {
  496. static OSSL_PARAM settable[] = {
  497. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  498. OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_PRIV_LEN, NULL),
  499. OSSL_PARAM_int(OSSL_PKEY_PARAM_DH_GENERATOR, NULL),
  500. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE, NULL, 0),
  501. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_PBITS, NULL),
  502. OSSL_PARAM_size_t(OSSL_PKEY_PARAM_FFC_QBITS, NULL),
  503. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, NULL, 0),
  504. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS, NULL, 0),
  505. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_GINDEX, NULL),
  506. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_FFC_SEED, NULL, 0),
  507. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_PCOUNTER, NULL),
  508. OSSL_PARAM_int(OSSL_PKEY_PARAM_FFC_H, NULL),
  509. OSSL_PARAM_END
  510. };
  511. return settable;
  512. }
  513. static int dh_gencb(int p, int n, BN_GENCB *cb)
  514. {
  515. struct dh_gen_ctx *gctx = BN_GENCB_get_arg(cb);
  516. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
  517. params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
  518. params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
  519. return gctx->cb(params, gctx->cbarg);
  520. }
  521. static void *dh_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  522. {
  523. int ret = 0;
  524. struct dh_gen_ctx *gctx = genctx;
  525. DH *dh = NULL;
  526. BN_GENCB *gencb = NULL;
  527. FFC_PARAMS *ffc;
  528. if (!ossl_prov_is_running() || gctx == NULL)
  529. return NULL;
  530. /* For parameter generation - If there is a group name just create it */
  531. if (gctx->gen_type == DH_PARAMGEN_TYPE_GROUP
  532. && gctx->ffc_params == NULL) {
  533. /* Select a named group if there is not one already */
  534. if (gctx->group_nid == NID_undef)
  535. gctx->group_nid = ossl_dh_get_named_group_uid_from_size(gctx->pbits);
  536. if (gctx->group_nid == NID_undef)
  537. return NULL;
  538. dh = ossl_dh_new_by_nid_ex(gctx->libctx, gctx->group_nid);
  539. if (dh == NULL)
  540. return NULL;
  541. ffc = ossl_dh_get0_params(dh);
  542. } else {
  543. dh = ossl_dh_new_ex(gctx->libctx);
  544. if (dh == NULL)
  545. return NULL;
  546. ffc = ossl_dh_get0_params(dh);
  547. /* Copy the template value if one was passed */
  548. if (gctx->ffc_params != NULL
  549. && !ossl_ffc_params_copy(ffc, gctx->ffc_params))
  550. goto end;
  551. if (!ossl_ffc_params_set_seed(ffc, gctx->seed, gctx->seedlen))
  552. goto end;
  553. if (gctx->gindex != -1) {
  554. ossl_ffc_params_set_gindex(ffc, gctx->gindex);
  555. if (gctx->pcounter != -1)
  556. ossl_ffc_params_set_pcounter(ffc, gctx->pcounter);
  557. } else if (gctx->hindex != 0) {
  558. ossl_ffc_params_set_h(ffc, gctx->hindex);
  559. }
  560. if (gctx->mdname != NULL) {
  561. if (!ossl_ffc_set_digest(ffc, gctx->mdname, gctx->mdprops))
  562. goto end;
  563. }
  564. gctx->cb = osslcb;
  565. gctx->cbarg = cbarg;
  566. gencb = BN_GENCB_new();
  567. if (gencb != NULL)
  568. BN_GENCB_set(gencb, dh_gencb, genctx);
  569. if ((gctx->selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0) {
  570. /*
  571. * NOTE: The old safe prime generator code is not used in fips mode,
  572. * (i.e internally it ignores the generator and chooses a named
  573. * group based on pbits.
  574. */
  575. if (gctx->gen_type == DH_PARAMGEN_TYPE_GENERATOR)
  576. ret = DH_generate_parameters_ex(dh, gctx->pbits,
  577. gctx->generator, gencb);
  578. else
  579. ret = ossl_dh_generate_ffc_parameters(dh, gctx->gen_type,
  580. gctx->pbits, gctx->qbits,
  581. gencb);
  582. if (ret <= 0)
  583. goto end;
  584. }
  585. }
  586. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  587. if (ffc->p == NULL || ffc->g == NULL)
  588. goto end;
  589. if (gctx->priv_len > 0)
  590. DH_set_length(dh, (long)gctx->priv_len);
  591. ossl_ffc_params_enable_flags(ffc, FFC_PARAM_FLAG_VALIDATE_LEGACY,
  592. gctx->gen_type == DH_PARAMGEN_TYPE_FIPS_186_2);
  593. if (DH_generate_key(dh) <= 0)
  594. goto end;
  595. }
  596. DH_clear_flags(dh, DH_FLAG_TYPE_MASK);
  597. DH_set_flags(dh, gctx->dh_type);
  598. ret = 1;
  599. end:
  600. if (ret <= 0) {
  601. DH_free(dh);
  602. dh = NULL;
  603. }
  604. BN_GENCB_free(gencb);
  605. return dh;
  606. }
  607. static void dh_gen_cleanup(void *genctx)
  608. {
  609. struct dh_gen_ctx *gctx = genctx;
  610. if (gctx == NULL)
  611. return;
  612. OPENSSL_free(gctx->mdname);
  613. OPENSSL_free(gctx->mdprops);
  614. OPENSSL_clear_free(gctx->seed, gctx->seedlen);
  615. OPENSSL_free(gctx);
  616. }
  617. static void *dh_load(const void *reference, size_t reference_sz)
  618. {
  619. DH *dh = NULL;
  620. if (ossl_prov_is_running() && reference_sz == sizeof(dh)) {
  621. /* The contents of the reference is the address to our object */
  622. dh = *(DH **)reference;
  623. /* We grabbed, so we detach it */
  624. *(DH **)reference = NULL;
  625. return dh;
  626. }
  627. return NULL;
  628. }
  629. static void *dh_dup(const void *keydata_from, int selection)
  630. {
  631. if (ossl_prov_is_running())
  632. return ossl_dh_dup(keydata_from, selection);
  633. return NULL;
  634. }
  635. const OSSL_DISPATCH ossl_dh_keymgmt_functions[] = {
  636. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dh_newdata },
  637. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dh_gen_init },
  638. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
  639. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
  640. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  641. (void (*)(void))dh_gen_settable_params },
  642. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
  643. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
  644. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
  645. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
  646. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
  647. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
  648. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
  649. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
  650. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
  651. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
  652. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
  653. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
  654. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
  655. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
  656. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
  657. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
  658. { 0, NULL }
  659. };
  660. /* For any DH key, we use the "DH" algorithms regardless of sub-type. */
  661. static const char *dhx_query_operation_name(int operation_id)
  662. {
  663. return "DH";
  664. }
  665. const OSSL_DISPATCH ossl_dhx_keymgmt_functions[] = {
  666. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))dhx_newdata },
  667. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))dhx_gen_init },
  668. { OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE, (void (*)(void))dh_gen_set_template },
  669. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))dh_gen_set_params },
  670. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  671. (void (*)(void))dh_gen_settable_params },
  672. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))dh_gen },
  673. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))dh_gen_cleanup },
  674. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))dh_load },
  675. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))dh_freedata },
  676. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))dh_get_params },
  677. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))dh_gettable_params },
  678. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))dh_set_params },
  679. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))dh_settable_params },
  680. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))dh_has },
  681. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))dh_match },
  682. { OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))dh_validate },
  683. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))dh_import },
  684. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))dh_import_types },
  685. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))dh_export },
  686. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))dh_export_types },
  687. { OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
  688. (void (*)(void))dhx_query_operation_name },
  689. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))dh_dup },
  690. { 0, NULL }
  691. };