dh_kmgmt.c 30 KB

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