dhparam.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #ifndef OPENSSL_NO_DEPRECATED_3_0
  10. /* We need to use some deprecated APIs */
  11. # define OPENSSL_SUPPRESS_DEPRECATED
  12. #endif
  13. #include <openssl/opensslconf.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <string.h>
  18. #include "apps.h"
  19. #include "progs.h"
  20. #include <openssl/bio.h>
  21. #include <openssl/err.h>
  22. #include <openssl/bn.h>
  23. #include <openssl/dh.h>
  24. #include <openssl/x509.h>
  25. #include <openssl/pem.h>
  26. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  27. # include <openssl/dsa.h>
  28. #endif
  29. #define DEFBITS 2048
  30. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  31. static int dh_cb(int p, int n, BN_GENCB *cb);
  32. #endif
  33. static int gendh_cb(EVP_PKEY_CTX *ctx);
  34. typedef enum OPTION_choice {
  35. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  36. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
  37. OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
  38. OPT_DSAPARAM, OPT_2, OPT_3, OPT_5,
  39. OPT_R_ENUM, OPT_PROV_ENUM
  40. } OPTION_CHOICE;
  41. const OPTIONS dhparam_options[] = {
  42. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
  43. OPT_SECTION("General"),
  44. {"help", OPT_HELP, '-', "Display this summary"},
  45. {"check", OPT_CHECK, '-', "Check the DH parameters"},
  46. #ifndef OPENSSL_NO_DSA
  47. {"dsaparam", OPT_DSAPARAM, '-',
  48. "Read or generate DSA parameters, convert to DH"},
  49. #endif
  50. #ifndef OPENSSL_NO_ENGINE
  51. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  52. #endif
  53. OPT_SECTION("Input"),
  54. {"in", OPT_IN, '<', "Input file"},
  55. {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
  56. OPT_SECTION("Output"),
  57. {"out", OPT_OUT, '>', "Output file"},
  58. {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
  59. {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
  60. {"noout", OPT_NOOUT, '-', "Don't output any DH parameters"},
  61. {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
  62. {"3", OPT_3, '-', "Generate parameters using 3 as the generator value"},
  63. {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
  64. OPT_R_OPTIONS,
  65. OPT_PROV_OPTIONS,
  66. OPT_PARAMETERS(),
  67. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  68. {NULL}
  69. };
  70. int dhparam_main(int argc, char **argv)
  71. {
  72. BIO *in = NULL, *out = NULL;
  73. DH *dh = NULL, *alloc_dh = NULL;
  74. EVP_PKEY *pkey = NULL;
  75. EVP_PKEY_CTX *ctx = NULL;
  76. char *infile = NULL, *outfile = NULL, *prog;
  77. ENGINE *e = NULL;
  78. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  79. int dsaparam = 0;
  80. #endif
  81. int i, text = 0, ret = 1, num = 0, g = 0;
  82. int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
  83. OPTION_CHOICE o;
  84. prog = opt_init(argc, argv, dhparam_options);
  85. while ((o = opt_next()) != OPT_EOF) {
  86. switch (o) {
  87. case OPT_EOF:
  88. case OPT_ERR:
  89. opthelp:
  90. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  91. goto end;
  92. case OPT_HELP:
  93. opt_help(dhparam_options);
  94. ret = 0;
  95. goto end;
  96. case OPT_INFORM:
  97. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  98. goto opthelp;
  99. break;
  100. case OPT_OUTFORM:
  101. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  102. goto opthelp;
  103. break;
  104. case OPT_IN:
  105. infile = opt_arg();
  106. break;
  107. case OPT_OUT:
  108. outfile = opt_arg();
  109. break;
  110. case OPT_ENGINE:
  111. e = setup_engine(opt_arg(), 0);
  112. break;
  113. case OPT_CHECK:
  114. check = 1;
  115. break;
  116. case OPT_TEXT:
  117. text = 1;
  118. break;
  119. case OPT_DSAPARAM:
  120. #ifndef OPENSSL_NO_DSA
  121. # ifdef OPENSSL_NO_DEPRECATED_3_0
  122. BIO_printf(bio_err, "The dsaparam option is deprecated.\n");
  123. # else
  124. dsaparam = 1;
  125. # endif
  126. #endif
  127. break;
  128. case OPT_2:
  129. g = 2;
  130. break;
  131. case OPT_3:
  132. g = 3;
  133. break;
  134. case OPT_5:
  135. g = 5;
  136. break;
  137. case OPT_NOOUT:
  138. noout = 1;
  139. break;
  140. case OPT_R_CASES:
  141. if (!opt_rand(o))
  142. goto end;
  143. break;
  144. case OPT_PROV_CASES:
  145. if (!opt_provider(o))
  146. goto end;
  147. break;
  148. }
  149. }
  150. argc = opt_num_rest();
  151. argv = opt_rest();
  152. if (argv[0] != NULL && (!opt_int(argv[0], &num) || num <= 0))
  153. goto end;
  154. if (g && !num)
  155. num = DEFBITS;
  156. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  157. if (dsaparam && g) {
  158. BIO_printf(bio_err,
  159. "Error, generator may not be chosen for DSA parameters\n");
  160. goto end;
  161. }
  162. #endif
  163. out = bio_open_default(outfile, 'w', outformat);
  164. if (out == NULL)
  165. goto end;
  166. /* DH parameters */
  167. if (num && !g)
  168. g = 2;
  169. if (num) {
  170. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  171. if (dsaparam) {
  172. DSA *dsa = DSA_new();
  173. BN_GENCB *cb = BN_GENCB_new();
  174. if (cb == NULL)
  175. goto end;
  176. BN_GENCB_set(cb, dh_cb, bio_err);
  177. BIO_printf(bio_err,
  178. "Generating DSA parameters, %d bit long prime\n", num);
  179. if (dsa == NULL
  180. || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
  181. cb)) {
  182. DSA_free(dsa);
  183. BN_GENCB_free(cb);
  184. BIO_printf(bio_err, "Error, unable to generate DSA parameters\n");
  185. goto end;
  186. }
  187. dh = alloc_dh = DSA_dup_DH(dsa);
  188. DSA_free(dsa);
  189. BN_GENCB_free(cb);
  190. if (dh == NULL)
  191. goto end;
  192. } else
  193. #endif
  194. {
  195. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL);
  196. if (ctx == NULL) {
  197. BIO_printf(bio_err,
  198. "Error, DH key generation context allocation failed\n");
  199. goto end;
  200. }
  201. EVP_PKEY_CTX_set_cb(ctx, gendh_cb);
  202. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  203. BIO_printf(bio_err,
  204. "Generating DH parameters, %d bit long safe prime, generator %d\n",
  205. num, g);
  206. BIO_printf(bio_err, "This is going to take a long time\n");
  207. if (!EVP_PKEY_paramgen_init(ctx)) {
  208. BIO_printf(bio_err,
  209. "Error, unable to initialise DH param generation\n");
  210. goto end;
  211. }
  212. if (!EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, num)) {
  213. BIO_printf(bio_err, "Error, unable to set DH prime length\n");
  214. goto end;
  215. }
  216. if (!EVP_PKEY_paramgen(ctx, &pkey)) {
  217. BIO_printf(bio_err, "Error, DH generation failed\n");
  218. goto end;
  219. }
  220. dh = EVP_PKEY_get0_DH(pkey);
  221. }
  222. } else {
  223. in = bio_open_default(infile, 'r', informat);
  224. if (in == NULL)
  225. goto end;
  226. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  227. if (dsaparam) {
  228. DSA *dsa;
  229. if (informat == FORMAT_ASN1)
  230. dsa = d2i_DSAparams_bio(in, NULL);
  231. else /* informat == FORMAT_PEM */
  232. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  233. if (dsa == NULL) {
  234. BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
  235. goto end;
  236. }
  237. dh = alloc_dh = DSA_dup_DH(dsa);
  238. DSA_free(dsa);
  239. if (dh == NULL)
  240. goto end;
  241. } else
  242. #endif
  243. {
  244. if (informat == FORMAT_ASN1) {
  245. /*
  246. * We have no PEM header to determine what type of DH params it
  247. * is. We'll just try both.
  248. */
  249. dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHparams, in, NULL);
  250. /* BIO_reset() returns 0 for success for file BIOs only!!! */
  251. if (dh == NULL && BIO_reset(in) == 0)
  252. dh = alloc_dh = ASN1_d2i_bio_of(DH, DH_new, d2i_DHxparams, in, NULL);
  253. } else {
  254. /* informat == FORMAT_PEM */
  255. dh = alloc_dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
  256. }
  257. if (dh == NULL) {
  258. BIO_printf(bio_err, "Error, unable to load DH parameters\n");
  259. goto end;
  260. }
  261. }
  262. /* dh != NULL */
  263. }
  264. if (text)
  265. EVP_PKEY_print_params(out, pkey, 4, NULL);
  266. if (check) {
  267. if (!EVP_PKEY_param_check(ctx) /* DH_check(dh, &i) */) {
  268. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  269. goto end;
  270. }
  271. BIO_printf(bio_err, "DH parameters appear to be ok.\n");
  272. if (num != 0) {
  273. /*
  274. * We have generated parameters but DH_check() indicates they are
  275. * invalid! This should never happen!
  276. */
  277. BIO_printf(bio_err, "Error, invalid parameters generated\n");
  278. goto end;
  279. }
  280. }
  281. if (!noout) {
  282. const BIGNUM *q;
  283. DH_get0_pqg(dh, NULL, &q, NULL);
  284. if (outformat == FORMAT_ASN1) {
  285. if (q != NULL)
  286. i = ASN1_i2d_bio_of(DH, i2d_DHxparams, out, dh);
  287. else
  288. i = ASN1_i2d_bio_of(DH, i2d_DHparams, out, dh);
  289. } else if (q != NULL) {
  290. i = PEM_write_bio_DHxparams(out, dh);
  291. } else {
  292. i = PEM_write_bio_DHparams(out, dh);
  293. }
  294. if (!i) {
  295. BIO_printf(bio_err, "Error, unable to write DH parameters\n");
  296. goto end;
  297. }
  298. }
  299. ret = 0;
  300. end:
  301. if (ret != 0)
  302. ERR_print_errors(bio_err);
  303. DH_free(alloc_dh);
  304. BIO_free(in);
  305. BIO_free_all(out);
  306. EVP_PKEY_free(pkey);
  307. EVP_PKEY_CTX_free(ctx);
  308. release_engine(e);
  309. return ret;
  310. }
  311. static int common_dh_cb(int p, BIO *b)
  312. {
  313. static const char symbols[] = ".+*\n";
  314. char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  315. BIO_write(b, &c, 1);
  316. (void)BIO_flush(b);
  317. return 1;
  318. }
  319. #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  320. static int dh_cb(int p, int n, BN_GENCB *cb)
  321. {
  322. return common_dh_cb(p, BN_GENCB_get_arg(cb));
  323. }
  324. #endif
  325. static int gendh_cb(EVP_PKEY_CTX *ctx)
  326. {
  327. return common_dh_cb(EVP_PKEY_CTX_get_keygen_info(ctx, 0),
  328. EVP_PKEY_CTX_get_app_data(ctx));
  329. }