dh_kmgmt.c 30 KB

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