dsaparam.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_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, "static DSA *get_dsa%d(void)\n{\n", bits_p);
  167. print_bignum_var(bio_out, p, "dsap", bits_p, data);
  168. print_bignum_var(bio_out, q, "dsaq", bits_p, data);
  169. print_bignum_var(bio_out, g, "dsag", bits_p, data);
  170. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  171. " BIGNUM *p, *q, *g;\n"
  172. "\n");
  173. BIO_printf(bio_out, " if (dsa == NULL)\n"
  174. " return NULL;\n");
  175. BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
  176. bits_p, bits_p);
  177. BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
  178. bits_p, bits_p);
  179. BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
  180. bits_p, bits_p);
  181. BIO_printf(bio_out, " DSA_free(dsa);\n"
  182. " BN_free(p);\n"
  183. " BN_free(q);\n"
  184. " BN_free(g);\n"
  185. " return NULL;\n"
  186. " }\n"
  187. " return dsa;\n}\n");
  188. OPENSSL_free(data);
  189. }
  190. if (outformat == FORMAT_ASN1 && genkey)
  191. noout = 1;
  192. if (!noout) {
  193. if (outformat == FORMAT_ASN1)
  194. i = i2d_DSAparams_bio(out, dsa);
  195. else
  196. i = PEM_write_bio_DSAparams(out, dsa);
  197. if (!i) {
  198. BIO_printf(bio_err, "unable to write DSA parameters\n");
  199. ERR_print_errors(bio_err);
  200. goto end;
  201. }
  202. }
  203. if (genkey) {
  204. DSA *dsakey;
  205. if ((dsakey = DSAparams_dup(dsa)) == NULL)
  206. goto end;
  207. if (!DSA_generate_key(dsakey)) {
  208. ERR_print_errors(bio_err);
  209. DSA_free(dsakey);
  210. goto end;
  211. }
  212. assert(private);
  213. if (outformat == FORMAT_ASN1)
  214. i = i2d_DSAPrivateKey_bio(out, dsakey);
  215. else
  216. i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
  217. NULL);
  218. DSA_free(dsakey);
  219. }
  220. ret = 0;
  221. end:
  222. BN_GENCB_free(cb);
  223. BIO_free(in);
  224. BIO_free_all(out);
  225. DSA_free(dsa);
  226. release_engine(e);
  227. return ret;
  228. }
  229. static int dsa_cb(int p, int n, BN_GENCB *cb)
  230. {
  231. static const char symbols[] = ".+*\n";
  232. char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  233. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  234. (void)BIO_flush(BN_GENCB_get_arg(cb));
  235. return 1;
  236. }
  237. #endif