dhparam.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. #include <openssl/opensslconf.h>
  111. #ifdef OPENSSL_NO_DH
  112. NON_EMPTY_TRANSLATION_UNIT
  113. #else
  114. # include <stdio.h>
  115. # include <stdlib.h>
  116. # include <time.h>
  117. # include <string.h>
  118. # include "apps.h"
  119. # include <openssl/bio.h>
  120. # include <openssl/err.h>
  121. # include <openssl/bn.h>
  122. # include <openssl/dh.h>
  123. # include <openssl/x509.h>
  124. # include <openssl/pem.h>
  125. # ifndef OPENSSL_NO_DSA
  126. # include <openssl/dsa.h>
  127. # endif
  128. # define DEFBITS 2048
  129. static int dh_cb(int p, int n, BN_GENCB *cb);
  130. typedef enum OPTION_choice {
  131. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  132. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
  133. OPT_ENGINE, OPT_CHECK, OPT_TEXT, OPT_NOOUT,
  134. OPT_RAND, OPT_DSAPARAM, OPT_C, OPT_2, OPT_5
  135. } OPTION_CHOICE;
  136. OPTIONS dhparam_options[] = {
  137. {OPT_HELP_STR, 1, '-', "Usage: %s [flags] [numbits]\n"},
  138. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  139. {"help", OPT_HELP, '-', "Display this summary"},
  140. {"in", OPT_IN, '<', "Input file"},
  141. {"inform", OPT_INFORM, 'F', "Input format, DER or PEM"},
  142. {"outform", OPT_OUTFORM, 'F', "Output format, DER or PEM"},
  143. {"out", OPT_OUT, '>', "Output file"},
  144. {"check", OPT_CHECK, '-', "Check the DH parameters"},
  145. {"text", OPT_TEXT, '-', "Print a text form of the DH parameters"},
  146. {"noout", OPT_NOOUT, '-'},
  147. {"rand", OPT_RAND, 's',
  148. "Load the file(s) into the random number generator"},
  149. {"C", OPT_C, '-', "Print C code"},
  150. {"2", OPT_2, '-', "Generate parameters using 2 as the generator value"},
  151. {"5", OPT_5, '-', "Generate parameters using 5 as the generator value"},
  152. # ifndef OPENSSL_NO_DSA
  153. {"dsaparam", OPT_DSAPARAM, '-',
  154. "Read or generate DSA parameters, convert to DH"},
  155. # endif
  156. # ifndef OPENSSL_NO_ENGINE
  157. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  158. # endif
  159. {NULL}
  160. };
  161. int dhparam_main(int argc, char **argv)
  162. {
  163. BIO *in = NULL, *out = NULL;
  164. DH *dh = NULL;
  165. char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;
  166. int dsaparam = 0, i, text = 0, C = 0, ret = 1, num = 0, g = 0;
  167. int informat = FORMAT_PEM, outformat = FORMAT_PEM, check = 0, noout = 0;
  168. OPTION_CHOICE o;
  169. prog = opt_init(argc, argv, dhparam_options);
  170. while ((o = opt_next()) != OPT_EOF) {
  171. switch (o) {
  172. case OPT_EOF:
  173. case OPT_ERR:
  174. opthelp:
  175. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  176. goto end;
  177. case OPT_HELP:
  178. opt_help(dhparam_options);
  179. ret = 0;
  180. goto end;
  181. case OPT_INFORM:
  182. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  183. goto opthelp;
  184. break;
  185. case OPT_OUTFORM:
  186. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  187. goto opthelp;
  188. break;
  189. case OPT_IN:
  190. infile = opt_arg();
  191. break;
  192. case OPT_OUT:
  193. outfile = opt_arg();
  194. break;
  195. case OPT_ENGINE:
  196. (void)setup_engine(opt_arg(), 0);
  197. break;
  198. case OPT_CHECK:
  199. check = 1;
  200. break;
  201. case OPT_TEXT:
  202. text = 1;
  203. break;
  204. case OPT_DSAPARAM:
  205. dsaparam = 1;
  206. break;
  207. case OPT_C:
  208. C = 1;
  209. break;
  210. case OPT_2:
  211. g = 2;
  212. break;
  213. case OPT_5:
  214. g = 5;
  215. break;
  216. case OPT_NOOUT:
  217. noout = 1;
  218. break;
  219. case OPT_RAND:
  220. inrand = opt_arg();
  221. break;
  222. }
  223. }
  224. argc = opt_num_rest();
  225. argv = opt_rest();
  226. if (argv[0] && (!opt_int(argv[0], &num) || num <= 0))
  227. goto end;
  228. if (g && !num)
  229. num = DEFBITS;
  230. # ifndef OPENSSL_NO_DSA
  231. if (dsaparam && g) {
  232. BIO_printf(bio_err,
  233. "generator may not be chosen for DSA parameters\n");
  234. goto end;
  235. }
  236. # endif
  237. /* DH parameters */
  238. if (num && !g)
  239. g = 2;
  240. if (num) {
  241. BN_GENCB *cb;
  242. cb = BN_GENCB_new();
  243. if (cb == NULL) {
  244. ERR_print_errors(bio_err);
  245. goto end;
  246. }
  247. BN_GENCB_set(cb, dh_cb, bio_err);
  248. if (!app_RAND_load_file(NULL, 1) && inrand == NULL) {
  249. BIO_printf(bio_err,
  250. "warning, not much extra random data, consider using the -rand option\n");
  251. }
  252. if (inrand != NULL)
  253. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  254. app_RAND_load_files(inrand));
  255. # ifndef OPENSSL_NO_DSA
  256. if (dsaparam) {
  257. DSA *dsa = DSA_new();
  258. BIO_printf(bio_err,
  259. "Generating DSA parameters, %d bit long prime\n", num);
  260. if (dsa == NULL
  261. || !DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL,
  262. cb)) {
  263. DSA_free(dsa);
  264. BN_GENCB_free(cb);
  265. ERR_print_errors(bio_err);
  266. goto end;
  267. }
  268. dh = DSA_dup_DH(dsa);
  269. DSA_free(dsa);
  270. if (dh == NULL) {
  271. BN_GENCB_free(cb);
  272. ERR_print_errors(bio_err);
  273. goto end;
  274. }
  275. } else
  276. # endif
  277. {
  278. dh = DH_new();
  279. BIO_printf(bio_err,
  280. "Generating DH parameters, %d bit long safe prime, generator %d\n",
  281. num, g);
  282. BIO_printf(bio_err, "This is going to take a long time\n");
  283. if (dh == NULL || !DH_generate_parameters_ex(dh, num, g, cb)) {
  284. BN_GENCB_free(cb);
  285. ERR_print_errors(bio_err);
  286. goto end;
  287. }
  288. }
  289. BN_GENCB_free(cb);
  290. app_RAND_write_file(NULL);
  291. } else {
  292. in = bio_open_default(infile, 'r', informat);
  293. if (in == NULL)
  294. goto end;
  295. # ifndef OPENSSL_NO_DSA
  296. if (dsaparam) {
  297. DSA *dsa;
  298. if (informat == FORMAT_ASN1)
  299. dsa = d2i_DSAparams_bio(in, NULL);
  300. else /* informat == FORMAT_PEM */
  301. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  302. if (dsa == NULL) {
  303. BIO_printf(bio_err, "unable to load DSA parameters\n");
  304. ERR_print_errors(bio_err);
  305. goto end;
  306. }
  307. dh = DSA_dup_DH(dsa);
  308. DSA_free(dsa);
  309. if (dh == NULL) {
  310. ERR_print_errors(bio_err);
  311. goto end;
  312. }
  313. } else
  314. # endif
  315. {
  316. if (informat == FORMAT_ASN1)
  317. dh = d2i_DHparams_bio(in, NULL);
  318. else /* informat == FORMAT_PEM */
  319. dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL);
  320. if (dh == NULL) {
  321. BIO_printf(bio_err, "unable to load DH parameters\n");
  322. ERR_print_errors(bio_err);
  323. goto end;
  324. }
  325. }
  326. /* dh != NULL */
  327. }
  328. out = bio_open_default(outfile, 'w', outformat);
  329. if (out == NULL)
  330. goto end;
  331. if (text) {
  332. DHparams_print(out, dh);
  333. }
  334. if (check) {
  335. if (!DH_check(dh, &i)) {
  336. ERR_print_errors(bio_err);
  337. goto end;
  338. }
  339. if (i & DH_CHECK_P_NOT_PRIME)
  340. printf("p value is not prime\n");
  341. if (i & DH_CHECK_P_NOT_SAFE_PRIME)
  342. printf("p value is not a safe prime\n");
  343. if (i & DH_UNABLE_TO_CHECK_GENERATOR)
  344. printf("unable to check the generator value\n");
  345. if (i & DH_NOT_SUITABLE_GENERATOR)
  346. printf("the g value is not a generator\n");
  347. if (i == 0)
  348. printf("DH parameters appear to be ok.\n");
  349. }
  350. if (C) {
  351. unsigned char *data;
  352. int len, bits;
  353. len = BN_num_bytes(dh->p);
  354. bits = BN_num_bits(dh->p);
  355. data = app_malloc(len, "print a BN");
  356. BIO_printf(out, "#ifndef HEADER_DH_H\n"
  357. "# include <openssl/dh.h>\n"
  358. "#endif\n"
  359. "\n");
  360. BIO_printf(out, "DH *get_dh%d()\n{\n", bits);
  361. print_bignum_var(out, dh->p, "dhp", bits, data);
  362. print_bignum_var(out, dh->g, "dhg", bits, data);
  363. BIO_printf(out, " DH *dh = DN_new();\n"
  364. "\n"
  365. " if (dh == NULL)\n"
  366. " return NULL;\n");
  367. BIO_printf(out, " dh->p = BN_bin2bn(dhp_%d, sizeof (dhp_%d), NULL);\n",
  368. bits, bits);
  369. BIO_printf(out, " dh->g = BN_bin2bn(dhg_%d, sizeof (dhg_%d), NULL);\n",
  370. bits, bits);
  371. BIO_printf(out, " if (!dh->p || !dh->g) {\n"
  372. " DH_free(dh);\n"
  373. " return NULL;\n"
  374. " }\n");
  375. if (dh->length)
  376. BIO_printf(out,
  377. " dh->length = %ld;\n", dh->length);
  378. BIO_printf(out, " return dh;\n}\n");
  379. OPENSSL_free(data);
  380. }
  381. if (!noout) {
  382. if (outformat == FORMAT_ASN1)
  383. i = i2d_DHparams_bio(out, dh);
  384. else if (dh->q)
  385. i = PEM_write_bio_DHxparams(out, dh);
  386. else
  387. i = PEM_write_bio_DHparams(out, dh);
  388. if (!i) {
  389. BIO_printf(bio_err, "unable to write DH parameters\n");
  390. ERR_print_errors(bio_err);
  391. goto end;
  392. }
  393. }
  394. ret = 0;
  395. end:
  396. BIO_free(in);
  397. BIO_free_all(out);
  398. DH_free(dh);
  399. return (ret);
  400. }
  401. static int dh_cb(int p, int n, BN_GENCB *cb)
  402. {
  403. char c = '*';
  404. if (p == 0)
  405. c = '.';
  406. if (p == 1)
  407. c = '+';
  408. if (p == 2)
  409. c = '*';
  410. if (p == 3)
  411. c = '\n';
  412. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  413. (void)BIO_flush(BN_GENCB_get_arg(cb));
  414. return 1;
  415. }
  416. #endif