2
0

dhparam.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright 1995-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. #include <openssl/opensslconf.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include "apps.h"
  15. #include "progs.h"
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/dsa.h>
  20. #include <openssl/dh.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/pem.h>
  23. #include <openssl/core_names.h>
  24. #include <openssl/core_dispatch.h>
  25. #include <openssl/param_build.h>
  26. #include <openssl/encoder.h>
  27. #include <openssl/decoder.h>
  28. #define DEFBITS 2048
  29. static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh);
  30. static int verbose = 1;
  31. typedef enum OPTION_choice {
  32. OPT_COMMON,
  33. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
  34. OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
  35. OPT_DSAPARAM, OPT_2, OPT_3, OPT_5, OPT_VERBOSE, OPT_QUIET,
  36. OPT_R_ENUM, OPT_PROV_ENUM
  37. } OPTION_CHOICE;
  38. const OPTIONS dhparam_options[] = {
  39. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
  40. OPT_SECTION("General"),
  41. {"help", OPT_HELP, '-', "Display this summary"},
  42. {"check", OPT_CHECK, '-', "Check the DH parameters"},
  43. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_DEPRECATED_3_0)
  44. {"dsaparam", OPT_DSAPARAM, '-',
  45. "Read or generate DSA parameters, convert to DH"},
  46. #endif
  47. #ifndef OPENSSL_NO_ENGINE
  48. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  49. #endif
  50. OPT_SECTION("Input"),
  51. {"in", OPT_IN, '<', "Input file"},
  52. {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
  53. OPT_SECTION("Output"),
  54. {"out", OPT_OUT, '>', "Output file"},
  55. {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
  56. {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
  57. {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
  58. {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
  59. {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
  60. {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
  61. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  62. {"quiet", OPT_QUIET, '-', "Terse output"},
  63. OPT_R_OPTIONS,
  64. OPT_PROV_OPTIONS,
  65. OPT_PARAMETERS(),
  66. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  67. {NULL}
  68. };
  69. int dhparam_main(int argc, char **argv)
  70. {
  71. BIO *in = NULL, *out = NULL;
  72. EVP_PKEY *pkey = NULL, *tmppkey = NULL;
  73. EVP_PKEY_CTX *ctx = NULL;
  74. char *infile = NULL, *outfile = NULL, *prog;
  75. ENGINE *e = NULL;
  76. int dsaparam = 0;
  77. int text = 0, ret = 1, num = 0, g = 0;
  78. int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
  79. OPTION_CHOICE o;
  80. prog = opt_init(argc, argv, dhparam_options);
  81. while ((o = opt_next()) != OPT_EOF) {
  82. switch (o) {
  83. case OPT_EOF:
  84. case OPT_ERR:
  85. opthelp:
  86. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  87. goto end;
  88. case OPT_HELP:
  89. opt_help(dhparam_options);
  90. ret = 0;
  91. goto end;
  92. case OPT_INFORM:
  93. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  94. goto opthelp;
  95. break;
  96. case OPT_OUTFORM:
  97. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  98. goto opthelp;
  99. break;
  100. case OPT_IN:
  101. infile = opt_arg();
  102. break;
  103. case OPT_OUT:
  104. outfile = opt_arg();
  105. break;
  106. case OPT_ENGINE:
  107. e = setup_engine(opt_arg(), 0);
  108. break;
  109. case OPT_CHECK:
  110. check = 1;
  111. break;
  112. case OPT_TEXT:
  113. text = 1;
  114. break;
  115. case OPT_DSAPARAM:
  116. dsaparam = 1;
  117. break;
  118. case OPT_2:
  119. g = 2;
  120. break;
  121. case OPT_3:
  122. g = 3;
  123. break;
  124. case OPT_5:
  125. g = 5;
  126. break;
  127. case OPT_NOOUT:
  128. noout = 1;
  129. break;
  130. case OPT_VERBOSE:
  131. verbose = 1;
  132. break;
  133. case OPT_QUIET:
  134. verbose = 0;
  135. break;
  136. case OPT_R_CASES:
  137. if (!opt_rand(o))
  138. goto end;
  139. break;
  140. case OPT_PROV_CASES:
  141. if (!opt_provider(o))
  142. goto end;
  143. break;
  144. }
  145. }
  146. /* One optional argument, bitsize to generate. */
  147. argc = opt_num_rest();
  148. argv = opt_rest();
  149. if (argc == 1) {
  150. if (!opt_int(argv[0], &num) || num <= 0)
  151. goto opthelp;
  152. } else if (!opt_check_rest_arg(NULL)) {
  153. goto opthelp;
  154. }
  155. if (!app_RAND_load())
  156. goto end;
  157. if (g && !num)
  158. num = DEFBITS;
  159. if (dsaparam && g) {
  160. BIO_printf(bio_err,
  161. "Error, generator may not be chosen for DSA parameters\n");
  162. goto end;
  163. }
  164. out = bio_open_default(outfile, 'w', outformat);
  165. if (out == NULL)
  166. goto end;
  167. /* DH parameters */
  168. if (num && !g)
  169. g = 2;
  170. if (num) {
  171. const char *alg = dsaparam ? "DSA" : "DH";
  172. if (infile != NULL) {
  173. BIO_printf(bio_err, "Warning, input file %s ignored\n", infile);
  174. }
  175. ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), alg, app_get0_propq());
  176. if (ctx == NULL) {
  177. BIO_printf(bio_err,
  178. "Error, %s param generation context allocation failed\n",
  179. alg);
  180. goto end;
  181. }
  182. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  183. if (verbose) {
  184. EVP_PKEY_CTX_set_cb(ctx, progress_cb);
  185. BIO_printf(bio_err,
  186. "Generating %s parameters, %d bit long %sprime\n",
  187. alg, num, dsaparam ? "" : "safe ");
  188. }
  189. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  190. BIO_printf(bio_err,
  191. "Error, unable to initialise %s parameters\n",
  192. alg);
  193. goto end;
  194. }
  195. if (dsaparam) {
  196. if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
  197. BIO_printf(bio_err, "Error, unable to set DSA prime length\n");
  198. goto end;
  199. }
  200. } else {
  201. if (EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num) <= 0) {
  202. BIO_printf(bio_err, "Error, unable to set DH prime length\n");
  203. goto end;
  204. }
  205. if (EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g) <= 0) {
  206. BIO_printf(bio_err, "Error, unable to set generator\n");
  207. goto end;
  208. }
  209. }
  210. tmppkey = app_paramgen(ctx, alg);
  211. if (tmppkey == NULL)
  212. goto end;
  213. EVP_PKEY_CTX_free(ctx);
  214. ctx = NULL;
  215. if (dsaparam) {
  216. pkey = dsa_to_dh(tmppkey);
  217. if (pkey == NULL)
  218. goto end;
  219. EVP_PKEY_free(tmppkey);
  220. } else {
  221. pkey = tmppkey;
  222. }
  223. tmppkey = NULL;
  224. } else {
  225. OSSL_DECODER_CTX *decoderctx = NULL;
  226. const char *keytype = "DH";
  227. int done;
  228. in = bio_open_default(infile, 'r', informat);
  229. if (in == NULL)
  230. goto end;
  231. do {
  232. /*
  233. * We assume we're done unless we explicitly want to retry and set
  234. * this to 0 below.
  235. */
  236. done = 1;
  237. /*
  238. * We set NULL for the keytype to allow any key type. We don't know
  239. * if we're going to get DH or DHX (or DSA in the event of dsaparam).
  240. * We check that we got one of those key types afterwards.
  241. */
  242. decoderctx
  243. = OSSL_DECODER_CTX_new_for_pkey(&tmppkey,
  244. (informat == FORMAT_ASN1)
  245. ? "DER" : "PEM",
  246. NULL,
  247. (informat == FORMAT_ASN1)
  248. ? keytype : NULL,
  249. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  250. NULL, NULL);
  251. if (decoderctx != NULL
  252. && !OSSL_DECODER_from_bio(decoderctx, in)
  253. && informat == FORMAT_ASN1
  254. && strcmp(keytype, "DH") == 0) {
  255. /*
  256. * When reading DER we explicitly state the expected keytype
  257. * because, unlike PEM, there is no header to declare what
  258. * the contents of the DER file are. The decoders just try
  259. * and guess. Unfortunately with DHX key types they may guess
  260. * wrong and think we have a DSA keytype. Therefore, we try
  261. * both DH and DHX sequentially.
  262. */
  263. keytype = "DHX";
  264. /*
  265. * BIO_reset() returns 0 for success for file BIOs only!!!
  266. * This won't work for stdin (and never has done)
  267. */
  268. if (BIO_reset(in) == 0)
  269. done = 0;
  270. }
  271. OSSL_DECODER_CTX_free(decoderctx);
  272. } while (!done);
  273. if (tmppkey == NULL) {
  274. BIO_printf(bio_err, "Error, unable to load parameters\n");
  275. goto end;
  276. }
  277. if (dsaparam) {
  278. if (!EVP_PKEY_is_a(tmppkey, "DSA")) {
  279. BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
  280. goto end;
  281. }
  282. pkey = dsa_to_dh(tmppkey);
  283. if (pkey == NULL)
  284. goto end;
  285. } else {
  286. if (!EVP_PKEY_is_a(tmppkey, "DH")
  287. && !EVP_PKEY_is_a(tmppkey, "DHX")) {
  288. BIO_printf(bio_err, "Error, unable to load DH parameters\n");
  289. goto end;
  290. }
  291. pkey = tmppkey;
  292. tmppkey = NULL;
  293. }
  294. }
  295. if (text)
  296. EVP_PKEY_print_params(out, pkey, 4, NULL);
  297. if (check) {
  298. ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey, app_get0_propq());
  299. if (ctx == NULL) {
  300. BIO_printf(bio_err, "Error, failed to check DH parameters\n");
  301. goto end;
  302. }
  303. if (EVP_PKEY_param_check(ctx) <= 0) {
  304. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  305. goto end;
  306. }
  307. BIO_printf(bio_err, "DH parameters appear to be ok.\n");
  308. }
  309. if (!noout) {
  310. OSSL_ENCODER_CTX *ectx =
  311. OSSL_ENCODER_CTX_new_for_pkey(pkey,
  312. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  313. outformat == FORMAT_ASN1
  314. ? "DER" : "PEM",
  315. NULL, NULL);
  316. if (ectx == NULL || !OSSL_ENCODER_to_bio(ectx, out)) {
  317. OSSL_ENCODER_CTX_free(ectx);
  318. BIO_printf(bio_err, "Error, unable to write DH parameters\n");
  319. goto end;
  320. }
  321. OSSL_ENCODER_CTX_free(ectx);
  322. }
  323. ret = 0;
  324. end:
  325. if (ret != 0)
  326. ERR_print_errors(bio_err);
  327. BIO_free(in);
  328. BIO_free_all(out);
  329. EVP_PKEY_free(pkey);
  330. EVP_PKEY_free(tmppkey);
  331. EVP_PKEY_CTX_free(ctx);
  332. release_engine(e);
  333. return ret;
  334. }
  335. /*
  336. * Historically we had the low-level call DSA_dup_DH() to do this.
  337. * That is now deprecated with no replacement. Since we still need to do this
  338. * for backwards compatibility reasons, we do it "manually".
  339. */
  340. static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh)
  341. {
  342. OSSL_PARAM_BLD *tmpl = NULL;
  343. OSSL_PARAM *params = NULL;
  344. BIGNUM *bn_p = NULL, *bn_q = NULL, *bn_g = NULL;
  345. EVP_PKEY_CTX *ctx = NULL;
  346. EVP_PKEY *pkey = NULL;
  347. if (!EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_P, &bn_p)
  348. || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_Q, &bn_q)
  349. || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_G, &bn_g)) {
  350. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  351. goto err;
  352. }
  353. if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
  354. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P,
  355. bn_p)
  356. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q,
  357. bn_q)
  358. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G,
  359. bn_g)
  360. || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  361. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  362. goto err;
  363. }
  364. ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DHX", app_get0_propq());
  365. if (ctx == NULL
  366. || EVP_PKEY_fromdata_init(ctx) <= 0
  367. || EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {
  368. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  369. goto err;
  370. }
  371. err:
  372. EVP_PKEY_CTX_free(ctx);
  373. OSSL_PARAM_free(params);
  374. OSSL_PARAM_BLD_free(tmpl);
  375. BN_free(bn_p);
  376. BN_free(bn_q);
  377. BN_free(bn_g);
  378. return pkey;
  379. }