2
0

dsaparam.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. cb = BN_GENCB_new();
  122. if (cb == NULL) {
  123. BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
  124. goto end;
  125. }
  126. BN_GENCB_set(cb, dsa_cb, bio_err);
  127. dsa = DSA_new();
  128. if (dsa == NULL) {
  129. BIO_printf(bio_err, "Error allocating DSA object\n");
  130. goto end;
  131. }
  132. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  133. num);
  134. BIO_printf(bio_err, "This could take some time\n");
  135. if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
  136. ERR_print_errors(bio_err);
  137. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  138. goto end;
  139. }
  140. } else if (informat == FORMAT_ASN1) {
  141. dsa = d2i_DSAparams_bio(in, NULL);
  142. } else {
  143. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  144. }
  145. if (dsa == NULL) {
  146. BIO_printf(bio_err, "unable to load DSA parameters\n");
  147. ERR_print_errors(bio_err);
  148. goto end;
  149. }
  150. if (text) {
  151. DSAparams_print(out, dsa);
  152. }
  153. if (C) {
  154. const BIGNUM *p = NULL, *q = NULL, *g = NULL;
  155. unsigned char *data;
  156. int len, bits_p;
  157. DSA_get0_pqg(dsa, &p, &q, &g);
  158. len = BN_num_bytes(p);
  159. bits_p = BN_num_bits(p);
  160. data = app_malloc(len + 20, "BN space");
  161. BIO_printf(bio_out, "DSA *get_dsa%d()\n{\n", bits_p);
  162. print_bignum_var(bio_out, p, "dsap", len, data);
  163. print_bignum_var(bio_out, q, "dsaq", len, data);
  164. print_bignum_var(bio_out, g, "dsag", len, data);
  165. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  166. "\n");
  167. BIO_printf(bio_out, " if (dsa == NULL)\n"
  168. " return NULL;\n");
  169. BIO_printf(bio_out, " dsa->p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL);\n",
  170. bits_p, bits_p);
  171. BIO_printf(bio_out, " dsa->q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL);\n",
  172. bits_p, bits_p);
  173. BIO_printf(bio_out, " dsa->g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL);\n",
  174. bits_p, bits_p);
  175. BIO_printf(bio_out, " if (!dsa->p || !dsa->q || !dsa->g) {\n"
  176. " DSA_free(dsa);\n"
  177. " return NULL;\n"
  178. " }\n"
  179. " return(dsa);\n}\n");
  180. OPENSSL_free(data);
  181. }
  182. if (outformat == FORMAT_ASN1 && genkey)
  183. noout = 1;
  184. if (!noout) {
  185. if (outformat == FORMAT_ASN1)
  186. i = i2d_DSAparams_bio(out, dsa);
  187. else
  188. i = PEM_write_bio_DSAparams(out, dsa);
  189. if (!i) {
  190. BIO_printf(bio_err, "unable to write DSA parameters\n");
  191. ERR_print_errors(bio_err);
  192. goto end;
  193. }
  194. }
  195. if (genkey) {
  196. DSA *dsakey;
  197. if ((dsakey = DSAparams_dup(dsa)) == NULL)
  198. goto end;
  199. if (!DSA_generate_key(dsakey)) {
  200. ERR_print_errors(bio_err);
  201. DSA_free(dsakey);
  202. goto end;
  203. }
  204. assert(private);
  205. if (outformat == FORMAT_ASN1)
  206. i = i2d_DSAPrivateKey_bio(out, dsakey);
  207. else
  208. i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL,
  209. NULL);
  210. DSA_free(dsakey);
  211. }
  212. ret = 0;
  213. end:
  214. BN_GENCB_free(cb);
  215. BIO_free(in);
  216. BIO_free_all(out);
  217. DSA_free(dsa);
  218. release_engine(e);
  219. return ret;
  220. }
  221. static int dsa_cb(int p, int n, BN_GENCB *cb)
  222. {
  223. char c = '*';
  224. if (p == 0)
  225. c = '.';
  226. if (p == 1)
  227. c = '+';
  228. if (p == 2)
  229. c = '*';
  230. if (p == 3)
  231. c = '\n';
  232. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  233. (void)BIO_flush(BN_GENCB_get_arg(cb));
  234. return 1;
  235. }
  236. #endif