dhparam.c 11 KB

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