dh_kmgmt.c 29 KB

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