dhparam.c 11 KB

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