dhparam.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright 1995-2020 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 gendh_cb(EVP_PKEY_CTX *ctx);
  31. typedef enum OPTION_choice {
  32. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  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,
  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. OPT_R_OPTIONS,
  62. OPT_PROV_OPTIONS,
  63. OPT_PARAMETERS(),
  64. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  65. {NULL}
  66. };
  67. int dhparam_main(int argc, char **argv)
  68. {
  69. BIO *in = NULL, *out = NULL;
  70. EVP_PKEY *pkey = NULL, *tmppkey = NULL;
  71. EVP_PKEY_CTX *ctx = NULL;
  72. char *infile = NULL, *outfile = NULL, *prog;
  73. ENGINE *e = NULL;
  74. int dsaparam = 0;
  75. int text = 0, ret = 1, num = 0, g = 0;
  76. int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
  77. OPTION_CHOICE o;
  78. prog = opt_init(argc, argv, dhparam_options);
  79. while ((o = opt_next()) != OPT_EOF) {
  80. switch (o) {
  81. case OPT_EOF:
  82. case OPT_ERR:
  83. opthelp:
  84. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  85. goto end;
  86. case OPT_HELP:
  87. opt_help(dhparam_options);
  88. ret = 0;
  89. goto end;
  90. case OPT_INFORM:
  91. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  92. goto opthelp;
  93. break;
  94. case OPT_OUTFORM:
  95. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  96. goto opthelp;
  97. break;
  98. case OPT_IN:
  99. infile = opt_arg();
  100. break;
  101. case OPT_OUT:
  102. outfile = opt_arg();
  103. break;
  104. case OPT_ENGINE:
  105. e = setup_engine(opt_arg(), 0);
  106. break;
  107. case OPT_CHECK:
  108. check = 1;
  109. break;
  110. case OPT_TEXT:
  111. text = 1;
  112. break;
  113. case OPT_DSAPARAM:
  114. dsaparam = 1;
  115. break;
  116. case OPT_2:
  117. g = 2;
  118. break;
  119. case OPT_3:
  120. g = 3;
  121. break;
  122. case OPT_5:
  123. g = 5;
  124. break;
  125. case OPT_NOOUT:
  126. noout = 1;
  127. break;
  128. case OPT_R_CASES:
  129. if (!opt_rand(o))
  130. goto end;
  131. break;
  132. case OPT_PROV_CASES:
  133. if (!opt_provider(o))
  134. goto end;
  135. break;
  136. }
  137. }
  138. /* One optional argument, bitsize to generate. */
  139. argc = opt_num_rest();
  140. argv = opt_rest();
  141. if (argc == 1) {
  142. if (!opt_int(argv[0], &num) || num <= 0)
  143. goto opthelp;
  144. } else if (argc != 0) {
  145. goto opthelp;
  146. }
  147. if (g && !num)
  148. num = DEFBITS;
  149. if (dsaparam && g) {
  150. BIO_printf(bio_err,
  151. "Error, generator may not be chosen for DSA parameters\n");
  152. goto end;
  153. }
  154. out = bio_open_default(outfile, 'w', outformat);
  155. if (out == NULL)
  156. goto end;
  157. /* DH parameters */
  158. if (num && !g)
  159. g = 2;
  160. if (num) {
  161. const char *alg = dsaparam ? "DSA" : "DH";
  162. ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL);
  163. if (ctx == NULL) {
  164. BIO_printf(bio_err,
  165. "Error, %s param generation context allocation failed\n",
  166. alg);
  167. goto end;
  168. }
  169. EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
  170. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  171. BIO_printf(bio_err,
  172. "Generating %s parameters, %d bit long %sprime\n",
  173. alg, num, dsaparam ? "" : "safe ");
  174. if (!EVP_PKEY_paramgen_init(ctx)) {
  175. BIO_printf(bio_err,
  176. "Error, unable to initialise %s parameters\n",
  177. alg);
  178. goto end;
  179. }
  180. if (dsaparam) {
  181. if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
  182. BIO_printf(bio_err, "Error, unable to set DSA prime length\n");
  183. goto end;
  184. }
  185. } else {
  186. if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
  187. BIO_printf(bio_err, "Error, unable to set DH prime length\n");
  188. goto end;
  189. }
  190. if (!EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, g)) {
  191. BIO_printf(bio_err, "Error, unable to set generator\n");
  192. goto end;
  193. }
  194. }
  195. if (!EVP_PKEY_paramgen(ctx, &tmppkey)) {
  196. BIO_printf(bio_err, "Error, %s generation failed\n", alg);
  197. goto end;
  198. }
  199. EVP_PKEY_CTX_free(ctx);
  200. ctx = NULL;
  201. if (dsaparam) {
  202. pkey = dsa_to_dh(tmppkey);
  203. if (pkey == NULL)
  204. goto end;
  205. EVP_PKEY_free(tmppkey);
  206. } else {
  207. pkey = tmppkey;
  208. }
  209. tmppkey = NULL;
  210. } else {
  211. OSSL_DECODER_CTX *decoderctx = NULL;
  212. const char *keytype = "DH";
  213. int done;
  214. in = bio_open_default(infile, 'r', informat);
  215. if (in == NULL)
  216. goto end;
  217. do {
  218. /*
  219. * We assume we're done unless we explicitly want to retry and set
  220. * this to 0 below.
  221. */
  222. done = 1;
  223. /*
  224. * We set NULL for the keytype to allow any key type. We don't know
  225. * if we're going to get DH or DHX (or DSA in the event of dsaparam).
  226. * We check that we got one of those key types afterwards.
  227. */
  228. decoderctx
  229. = OSSL_DECODER_CTX_new_by_EVP_PKEY(&tmppkey,
  230. (informat == FORMAT_ASN1)
  231. ? "DER" : "PEM",
  232. NULL,
  233. (informat == FORMAT_ASN1)
  234. ? keytype : NULL,
  235. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  236. NULL, NULL);
  237. if (decoderctx != NULL
  238. && !OSSL_DECODER_from_bio(decoderctx, in)
  239. && informat == FORMAT_ASN1
  240. && strcmp(keytype, "DH") == 0) {
  241. /*
  242. * When reading DER we explicitly state the expected keytype
  243. * because, unlike PEM, there is no header to declare what
  244. * the contents of the DER file are. The decoders just try
  245. * and guess. Unfortunately with DHX key types they may guess
  246. * wrong and think we have a DSA keytype. Therefore we try
  247. * both DH and DHX sequentially.
  248. */
  249. keytype = "DHX";
  250. /*
  251. * BIO_reset() returns 0 for success for file BIOs only!!!
  252. * This won't work for stdin (and never has done)
  253. * TODO: We should fix this at some point
  254. */
  255. if (BIO_reset(in) == 0)
  256. done = 0;
  257. }
  258. OSSL_DECODER_CTX_free(decoderctx);
  259. } while (!done);
  260. if (tmppkey == NULL) {
  261. BIO_printf(bio_err, "Error, unable to load parameters\n");
  262. goto end;
  263. }
  264. if (dsaparam) {
  265. if (!EVP_PKEY_is_a(tmppkey, "DSA")) {
  266. BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
  267. goto end;
  268. }
  269. pkey = dsa_to_dh(tmppkey);
  270. if (pkey == NULL)
  271. goto end;
  272. } else {
  273. if (!EVP_PKEY_is_a(tmppkey, "DH")
  274. && !EVP_PKEY_is_a(tmppkey, "DHX")) {
  275. BIO_printf(bio_err, "Error, unable to load DH parameters\n");
  276. goto end;
  277. }
  278. pkey = tmppkey;
  279. tmppkey = NULL;
  280. }
  281. }
  282. if (text)
  283. EVP_PKEY_print_params(out, pkey, 4, NULL);
  284. if (check) {
  285. ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
  286. if (ctx == NULL) {
  287. BIO_printf(bio_err, "Error, failed to check DH parameters\n");
  288. goto end;
  289. }
  290. if (!EVP_PKEY_param_check(ctx)) {
  291. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  292. goto end;
  293. }
  294. BIO_printf(bio_err, "DH parameters appear to be ok.\n");
  295. }
  296. if (!noout) {
  297. OSSL_ENCODER_CTX *ectx =
  298. OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey,
  299. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  300. outformat == FORMAT_ASN1
  301. ? "DER" : "PEM",
  302. NULL, NULL);
  303. if (ectx == NULL || !OSSL_ENCODER_to_bio(ectx, out)) {
  304. OSSL_ENCODER_CTX_free(ectx);
  305. BIO_printf(bio_err, "Error, unable to write DH parameters\n");
  306. goto end;
  307. }
  308. OSSL_ENCODER_CTX_free(ectx);
  309. }
  310. ret = 0;
  311. end:
  312. if (ret != 0)
  313. ERR_print_errors(bio_err);
  314. BIO_free(in);
  315. BIO_free_all(out);
  316. EVP_PKEY_free(pkey);
  317. EVP_PKEY_free(tmppkey);
  318. EVP_PKEY_CTX_free(ctx);
  319. release_engine(e);
  320. return ret;
  321. }
  322. /*
  323. * Historically we had the low level call DSA_dup_DH() to do this.
  324. * That is now deprecated with no replacement. Since we still need to do this
  325. * for backwards compatibility reasons, we do it "manually".
  326. */
  327. static EVP_PKEY *dsa_to_dh(EVP_PKEY *dh)
  328. {
  329. OSSL_PARAM_BLD *tmpl = NULL;
  330. OSSL_PARAM *params = NULL;
  331. BIGNUM *bn_p = NULL, *bn_q = NULL, *bn_g = NULL;
  332. EVP_PKEY_CTX *ctx = NULL;
  333. EVP_PKEY *pkey = NULL;
  334. if (!EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_P, &bn_p)
  335. || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_Q, &bn_q)
  336. || !EVP_PKEY_get_bn_param(dh, OSSL_PKEY_PARAM_FFC_G, &bn_g)) {
  337. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  338. goto err;
  339. }
  340. if ((tmpl = OSSL_PARAM_BLD_new()) == NULL
  341. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P,
  342. bn_p)
  343. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q,
  344. bn_q)
  345. || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G,
  346. bn_g)
  347. || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
  348. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  349. goto err;
  350. }
  351. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DHX", NULL);
  352. if (ctx == NULL
  353. || !EVP_PKEY_param_fromdata_init(ctx)
  354. || !EVP_PKEY_fromdata(ctx, &pkey, params)) {
  355. BIO_printf(bio_err, "Error, failed to set DH parameters\n");
  356. goto err;
  357. }
  358. err:
  359. EVP_PKEY_CTX_free(ctx);
  360. OSSL_PARAM_BLD_free_params(params);
  361. OSSL_PARAM_BLD_free(tmpl);
  362. BN_free(bn_p);
  363. BN_free(bn_q);
  364. BN_free(bn_g);
  365. return pkey;
  366. }
  367. static int gendh_cb(EVP_PKEY_CTX *ctx)
  368. {
  369. int p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  370. BIO *b = EVP_PKEY_CTX_get_app_data(ctx);
  371. static const char symbols[] = ".+*\n";
  372. char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  373. BIO_write(b, &c, 1);
  374. (void)BIO_flush(b);
  375. return 1;
  376. }