dsaparam.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 1995-2018 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. #ifdef OPENSSL_NO_DSA
  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/dsa.h>
  23. # include <openssl/x509.h>
  24. # include <openssl/pem.h>
  25. static int dsa_cb(int p, int n, BN_GENCB *cb);
  26. typedef enum OPTION_choice {
  27. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  28. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
  29. OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
  30. } OPTION_CHOICE;
  31. const OPTIONS dsaparam_options[] = {
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  34. {"in", OPT_IN, '<', "Input file"},
  35. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  36. {"out", OPT_OUT, '>', "Output file"},
  37. {"text", OPT_TEXT, '-', "Print as text"},
  38. {"C", OPT_C, '-', "Output C code"},
  39. {"noout", OPT_NOOUT, '-', "No output"},
  40. {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
  41. OPT_R_OPTIONS,
  42. # ifndef OPENSSL_NO_ENGINE
  43. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  44. # endif
  45. {NULL}
  46. };
  47. int dsaparam_main(int argc, char **argv)
  48. {
  49. ENGINE *e = NULL;
  50. DSA *dsa = NULL;
  51. BIO *in = NULL, *out = NULL;
  52. BN_GENCB *cb = NULL;
  53. int numbits = -1, num = 0, genkey = 0;
  54. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
  55. int ret = 1, i, text = 0, private = 0;
  56. char *infile = NULL, *outfile = NULL, *prog;
  57. OPTION_CHOICE o;
  58. prog = opt_init(argc, argv, dsaparam_options);
  59. while ((o = opt_next()) != OPT_EOF) {
  60. switch (o) {
  61. case OPT_EOF:
  62. case OPT_ERR:
  63. opthelp:
  64. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  65. goto end;
  66. case OPT_HELP:
  67. opt_help(dsaparam_options);
  68. ret = 0;
  69. goto end;
  70. case OPT_INFORM:
  71. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  72. goto opthelp;
  73. break;
  74. case OPT_IN:
  75. infile = opt_arg();
  76. break;
  77. case OPT_OUTFORM:
  78. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  79. goto opthelp;
  80. break;
  81. case OPT_OUT:
  82. outfile = opt_arg();
  83. break;
  84. case OPT_ENGINE:
  85. e = setup_engine(opt_arg(), 0);
  86. break;
  87. case OPT_TEXT:
  88. text = 1;
  89. break;
  90. case OPT_C:
  91. C = 1;
  92. break;
  93. case OPT_GENKEY:
  94. genkey = 1;
  95. break;
  96. case OPT_R_CASES:
  97. if (!opt_rand(o))
  98. goto end;
  99. break;
  100. case OPT_NOOUT:
  101. noout = 1;
  102. break;
  103. }
  104. }
  105. argc = opt_num_rest();
  106. argv = opt_rest();
  107. if (argc == 1) {
  108. if (!opt_int(argv[0], &num) || num < 0)
  109. goto end;
  110. /* generate a key */
  111. numbits = num;
  112. }
  113. private = genkey ? 1 : 0;
  114. in = bio_open_default(infile, 'r', informat);
  115. if (in == NULL)
  116. goto end;
  117. out = bio_open_owner(outfile, outformat, private);
  118. if (out == NULL)
  119. goto end;
  120. if (numbits > 0) {
  121. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  122. BIO_printf(bio_err,
  123. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  124. " Your key size is %d! Larger key size may behave not as expected.\n",
  125. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  126. cb = BN_GENCB_new();
  127. if (cb == NULL) {
  128. BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
  129. goto end;
  130. }
  131. BN_GENCB_set(cb, dsa_cb, bio_err);
  132. dsa = DSA_new();
  133. if (dsa == NULL) {
  134. BIO_printf(bio_err, "Error allocating DSA object\n");
  135. goto end;
  136. }
  137. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  138. num);
  139. BIO_printf(bio_err, "This could take some time\n");
  140. if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
  141. ERR_print_errors(bio_err);
  142. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  143. goto end;
  144. }
  145. } else if (informat == FORMAT_ASN1) {
  146. dsa = d2i_DSAparams_bio(in, NULL);
  147. } else {
  148. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  149. }
  150. if (dsa == NULL) {
  151. BIO_printf(bio_err, "unable to load DSA parameters\n");
  152. ERR_print_errors(bio_err);
  153. goto end;
  154. }
  155. if (text) {
  156. DSAparams_print(out, dsa);
  157. }
  158. if (C) {
  159. const BIGNUM *p = NULL, *q = NULL, *g = NULL;
  160. unsigned char *data;
  161. int len, bits_p;
  162. DSA_get0_pqg(dsa, &p, &q, &g);
  163. len = BN_num_bytes(p);
  164. bits_p = BN_num_bits(p);
  165. data = app_malloc(len + 20, "BN space");
  166. BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
  167. print_bignum_var(bio_out, p, "dsap", len, data);
  168. print_bignum_var(bio_out, q, "dsaq", len, data);
  169. print_bignum_var(bio_out, g, "dsag", len, data);
  170. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  171. "\n");
  172. BIO_printf(bio_out, " if (dsa == NULL)\n"
  173. " return NULL;\n");
  174. BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL);\n",
  175. bits_p, bits_p);
  176. BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL);\n",
  177. bits_p, bits_p);
  178. BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL);\n",
  179. bits_p, bits_p);
  180. BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
  181. " DSA_free(dsa);\n"
  182. " return NULL;\n"
  183. " }\n"
  184. " return(dsa);\n}\n");
  185. OPENSSL_free(data);
  186. }
  187. if (outformat == FORMAT_ASN1 && genkey)
  188. noout = 1;
  189. if (!noout) {
  190. if (outformat == FORMAT_ASN1)
  191. i = i2d_DSAparams_bio(out, dsa);
  192. else
  193. i = PEM_write_bio_DSAparams(out, dsa);
  194. if (!i) {
  195. BIO_printf(bio_err, "unable to write DSA parameters\n");
  196. ERR_print_errors(bio_err);
  197. goto end;
  198. }
  199. }
  200. if (genkey) {
  201. DSA *dsakey;
  202. if ((dsakey = DSAparams_dup(dsa)) == NULL)
  203. goto end;
  204. if (!DSA_generate_key(dsakey)) {
  205. ERR_print_errors(bio_err);
  206. DSA_free(dsakey);
  207. goto end;
  208. }
  209. assert(private);
  210. if (outformat == FORMAT_ASN1)
  211. i = i2d_DSAPrivateKey_bio(out, dsakey);
  212. else
  213. i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
  214. NULL);
  215. DSA_free(dsakey);
  216. }
  217. ret = 0;
  218. end:
  219. BN_GENCB_free(cb);
  220. BIO_free(in);
  221. BIO_free_all(out);
  222. DSA_free(dsa);
  223. release_engine(e);
  224. return ret;
  225. }
  226. static int dsa_cb(int p, int n, BN_GENCB *cb)
  227. {
  228. char c = '*';
  229. if (p == 0)
  230. c = '.';
  231. if (p == 1)
  232. c = '+';
  233. if (p == 2)
  234. c = '*';
  235. if (p == 3)
  236. c = '\n';
  237. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  238. (void)BIO_flush(BN_GENCB_get_arg(cb));
  239. return 1;
  240. }
  241. #endif