dsaparam.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include <openssl/opensslconf.h> /* for OPENSSL_NO_DSA */
  58. #ifndef OPENSSL_NO_DSA
  59. # include <stdio.h>
  60. # include <stdlib.h>
  61. # include <time.h>
  62. # include <string.h>
  63. # include "apps.h"
  64. # include <openssl/bio.h>
  65. # include <openssl/err.h>
  66. # include <openssl/bn.h>
  67. # include <openssl/dsa.h>
  68. # include <openssl/x509.h>
  69. # include <openssl/pem.h>
  70. # ifdef GENCB_TEST
  71. static int stop_keygen_flag = 0;
  72. static void timebomb_sigalarm(int foo)
  73. {
  74. stop_keygen_flag = 1;
  75. }
  76. # endif
  77. static int dsa_cb(int p, int n, BN_GENCB *cb);
  78. typedef enum OPTION_choice {
  79. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  80. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
  81. OPT_NOOUT, OPT_GENKEY, OPT_RAND, OPT_ENGINE,
  82. OPT_TIMEBOMB
  83. } OPTION_CHOICE;
  84. OPTIONS dsaparam_options[] = {
  85. {"help", OPT_HELP, '-', "Display this summary"},
  86. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  87. {"in", OPT_IN, '<', "Input file"},
  88. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  89. {"out", OPT_OUT, '>', "Output file"},
  90. {"text", OPT_TEXT, '-', "Print as text"},
  91. {"C", OPT_C, '-', "Output C code"},
  92. {"noout", OPT_NOOUT, '-', "No output"},
  93. {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
  94. {"rand", OPT_RAND, 's', "Files to use for random number input"},
  95. # ifdef GENCB_TEST
  96. {"timebomb", OPT_TIMEBOMB, 'p', "Interrupt keygen after 'pnum' seconds"},
  97. # endif
  98. # ifndef OPENSSL_NO_ENGINE
  99. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  100. # endif
  101. {NULL}
  102. };
  103. int dsaparam_main(int argc, char **argv)
  104. {
  105. DSA *dsa = NULL;
  106. BIO *in = NULL, *out = NULL;
  107. BN_GENCB *cb = NULL;
  108. int numbits = -1, num = 0, genkey = 0, need_rand = 0;
  109. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
  110. int ret = 1, i, text = 0, private = 0;
  111. # ifdef GENCB_TEST
  112. int timebomb = 0;
  113. # endif
  114. char *infile = NULL, *outfile = NULL, *prog, *inrand = NULL;
  115. OPTION_CHOICE o;
  116. prog = opt_init(argc, argv, dsaparam_options);
  117. while ((o = opt_next()) != OPT_EOF) {
  118. switch (o) {
  119. case OPT_EOF:
  120. case OPT_ERR:
  121. opthelp:
  122. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  123. goto end;
  124. case OPT_HELP:
  125. opt_help(dsaparam_options);
  126. ret = 0;
  127. goto end;
  128. case OPT_INFORM:
  129. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  130. goto opthelp;
  131. break;
  132. case OPT_IN:
  133. infile = opt_arg();
  134. break;
  135. case OPT_OUTFORM:
  136. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  137. goto opthelp;
  138. break;
  139. case OPT_OUT:
  140. outfile = opt_arg();
  141. break;
  142. case OPT_ENGINE:
  143. (void)setup_engine(opt_arg(), 0);
  144. break;
  145. case OPT_TIMEBOMB:
  146. # ifdef GENCB_TEST
  147. timebomb = atoi(opt_arg());
  148. break;
  149. # endif
  150. case OPT_TEXT:
  151. text = 1;
  152. break;
  153. case OPT_C:
  154. C = 1;
  155. break;
  156. case OPT_GENKEY:
  157. genkey = need_rand = 1;
  158. break;
  159. case OPT_RAND:
  160. inrand = opt_arg();
  161. need_rand = 1;
  162. break;
  163. case OPT_NOOUT:
  164. noout = 1;
  165. break;
  166. }
  167. }
  168. argc = opt_num_rest();
  169. argv = opt_rest();
  170. if (argc == 1) {
  171. if (!opt_int(argv[0], &num) || num < 0)
  172. goto end;
  173. /* generate a key */
  174. numbits = num;
  175. need_rand = 1;
  176. }
  177. private = genkey ? 1 : 0;
  178. in = bio_open_default(infile, 'r', informat);
  179. if (in == NULL)
  180. goto end;
  181. out = bio_open_owner(outfile, outformat, private);
  182. if (out == NULL)
  183. goto end;
  184. if (need_rand) {
  185. app_RAND_load_file(NULL, (inrand != NULL));
  186. if (inrand != NULL)
  187. BIO_printf(bio_err, "%ld semi-random bytes loaded\n",
  188. app_RAND_load_files(inrand));
  189. }
  190. if (numbits > 0) {
  191. cb = BN_GENCB_new();
  192. if (cb == NULL) {
  193. BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
  194. goto end;
  195. }
  196. BN_GENCB_set(cb, dsa_cb, bio_err);
  197. assert(need_rand);
  198. dsa = DSA_new();
  199. if (dsa == NULL) {
  200. BIO_printf(bio_err, "Error allocating DSA object\n");
  201. goto end;
  202. }
  203. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  204. num);
  205. BIO_printf(bio_err, "This could take some time\n");
  206. # ifdef GENCB_TEST
  207. if (timebomb > 0) {
  208. struct sigaction act;
  209. act.sa_handler = timebomb_sigalarm;
  210. act.sa_flags = 0;
  211. BIO_printf(bio_err,
  212. "(though I'll stop it if not done within %d secs)\n",
  213. timebomb);
  214. if (sigaction(SIGALRM, &act, NULL) != 0) {
  215. BIO_printf(bio_err, "Error, couldn't set SIGALRM handler\n");
  216. goto end;
  217. }
  218. alarm(timebomb);
  219. }
  220. # endif
  221. if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
  222. # ifdef GENCB_TEST
  223. if (stop_keygen_flag) {
  224. BIO_printf(bio_err, "DSA key generation time-stopped\n");
  225. /* This is an asked-for behaviour! */
  226. ret = 0;
  227. goto end;
  228. }
  229. # endif
  230. ERR_print_errors(bio_err);
  231. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  232. goto end;
  233. }
  234. } else if (informat == FORMAT_ASN1)
  235. dsa = d2i_DSAparams_bio(in, NULL);
  236. else
  237. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  238. if (dsa == NULL) {
  239. BIO_printf(bio_err, "unable to load DSA parameters\n");
  240. ERR_print_errors(bio_err);
  241. goto end;
  242. }
  243. if (text) {
  244. DSAparams_print(out, dsa);
  245. }
  246. if (C) {
  247. int len = BN_num_bytes(dsa->p);
  248. int bits_p = BN_num_bits(dsa->p);
  249. unsigned char *data = app_malloc(len + 20, "BN space");
  250. BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
  251. print_bignum_var(bio_out, dsa->p, "dsap", len, data);
  252. print_bignum_var(bio_out, dsa->q, "dsaq", len, data);
  253. print_bignum_var(bio_out, dsa->g, "dsag", len, data);
  254. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  255. "\n");
  256. BIO_printf(bio_out, " if (dsa == NULL)\n"
  257. " return NULL;\n");
  258. BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof (dsap_%d), NULL);\n",
  259. bits_p, bits_p);
  260. BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof (dsaq_%d), NULL);\n",
  261. bits_p, bits_p);
  262. BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof (dsag_%d), NULL);\n",
  263. bits_p, bits_p);
  264. BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
  265. " DSA_free(dsa);\n"
  266. " return NULL;\n"
  267. " }\n"
  268. " return(dsa);\n}\n");
  269. }
  270. if (!noout) {
  271. if (outformat == FORMAT_ASN1)
  272. i = i2d_DSAparams_bio(out, dsa);
  273. else
  274. i = PEM_write_bio_DSAparams(out, dsa);
  275. if (!i) {
  276. BIO_printf(bio_err, "unable to write DSA parameters\n");
  277. ERR_print_errors(bio_err);
  278. goto end;
  279. }
  280. }
  281. if (genkey) {
  282. DSA *dsakey;
  283. assert(need_rand);
  284. if ((dsakey = DSAparams_dup(dsa)) == NULL)
  285. goto end;
  286. if (!DSA_generate_key(dsakey)) {
  287. ERR_print_errors(bio_err);
  288. DSA_free(dsakey);
  289. goto end;
  290. }
  291. assert(private);
  292. if (outformat == FORMAT_ASN1)
  293. i = i2d_DSAPrivateKey_bio(out, dsakey);
  294. else
  295. i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
  296. NULL);
  297. DSA_free(dsakey);
  298. }
  299. if (need_rand)
  300. app_RAND_write_file(NULL);
  301. ret = 0;
  302. end:
  303. BN_GENCB_free(cb);
  304. BIO_free(in);
  305. BIO_free_all(out);
  306. DSA_free(dsa);
  307. return (ret);
  308. }
  309. static int dsa_cb(int p, int n, BN_GENCB *cb)
  310. {
  311. char c = '*';
  312. if (p == 0)
  313. c = '.';
  314. if (p == 1)
  315. c = '+';
  316. if (p == 2)
  317. c = '*';
  318. if (p == 3)
  319. c = '\n';
  320. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  321. (void)BIO_flush(BN_GENCB_get_arg(cb));
  322. # ifdef GENCB_TEST
  323. if (stop_keygen_flag)
  324. return 0;
  325. # endif
  326. return 1;
  327. }
  328. #else /* !OPENSSL_NO_DSA */
  329. # if PEDANTIC
  330. static void *dummy = &dummy;
  331. # endif
  332. #endif