dsaparam.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 1995-2020 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. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include "apps.h"
  15. #include "progs.h"
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <openssl/bn.h>
  19. #include <openssl/dsa.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/pem.h>
  22. static int dsa_cb(int p, int n, BN_GENCB *cb);
  23. typedef enum OPTION_choice {
  24. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  25. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
  26. OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_R_ENUM
  27. } OPTION_CHOICE;
  28. const OPTIONS dsaparam_options[] = {
  29. {"help", OPT_HELP, '-', "Display this summary"},
  30. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  31. {"in", OPT_IN, '<', "Input file"},
  32. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  33. {"out", OPT_OUT, '>', "Output file"},
  34. {"text", OPT_TEXT, '-', "Print as text"},
  35. {"C", OPT_C, '-', "Output C code"},
  36. {"noout", OPT_NOOUT, '-', "No output"},
  37. {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
  38. OPT_R_OPTIONS,
  39. #ifndef OPENSSL_NO_ENGINE
  40. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  41. #endif
  42. {NULL}
  43. };
  44. int dsaparam_main(int argc, char **argv)
  45. {
  46. ENGINE *e = NULL;
  47. DSA *dsa = NULL;
  48. BIO *in = NULL, *out = NULL;
  49. BN_GENCB *cb = NULL;
  50. int numbits = -1, num = 0, genkey = 0;
  51. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
  52. int ret = 1, i, text = 0, private = 0;
  53. char *infile = NULL, *outfile = NULL, *prog;
  54. OPTION_CHOICE o;
  55. prog = opt_init(argc, argv, dsaparam_options);
  56. while ((o = opt_next()) != OPT_EOF) {
  57. switch (o) {
  58. case OPT_EOF:
  59. case OPT_ERR:
  60. opthelp:
  61. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  62. goto end;
  63. case OPT_HELP:
  64. opt_help(dsaparam_options);
  65. ret = 0;
  66. goto end;
  67. case OPT_INFORM:
  68. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  69. goto opthelp;
  70. break;
  71. case OPT_IN:
  72. infile = opt_arg();
  73. break;
  74. case OPT_OUTFORM:
  75. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  76. goto opthelp;
  77. break;
  78. case OPT_OUT:
  79. outfile = opt_arg();
  80. break;
  81. case OPT_ENGINE:
  82. e = setup_engine(opt_arg(), 0);
  83. break;
  84. case OPT_TEXT:
  85. text = 1;
  86. break;
  87. case OPT_C:
  88. C = 1;
  89. break;
  90. case OPT_GENKEY:
  91. genkey = 1;
  92. break;
  93. case OPT_R_CASES:
  94. if (!opt_rand(o))
  95. goto end;
  96. break;
  97. case OPT_NOOUT:
  98. noout = 1;
  99. break;
  100. }
  101. }
  102. argc = opt_num_rest();
  103. argv = opt_rest();
  104. if (argc == 1) {
  105. if (!opt_int(argv[0], &num) || num < 0)
  106. goto end;
  107. /* generate a key */
  108. numbits = num;
  109. }
  110. private = genkey ? 1 : 0;
  111. in = bio_open_default(infile, 'r', informat);
  112. if (in == NULL)
  113. goto end;
  114. out = bio_open_owner(outfile, outformat, private);
  115. if (out == NULL)
  116. goto end;
  117. if (numbits > 0) {
  118. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  119. BIO_printf(bio_err,
  120. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  121. " Your key size is %d! Larger key size may behave not as expected.\n",
  122. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  123. cb = BN_GENCB_new();
  124. if (cb == NULL) {
  125. BIO_printf(bio_err, "Error allocating BN_GENCB object\n");
  126. goto end;
  127. }
  128. BN_GENCB_set(cb, dsa_cb, bio_err);
  129. dsa = DSA_new();
  130. if (dsa == NULL) {
  131. BIO_printf(bio_err, "Error allocating DSA object\n");
  132. goto end;
  133. }
  134. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  135. num);
  136. BIO_printf(bio_err, "This could take some time\n");
  137. if (!DSA_generate_parameters_ex(dsa, num, NULL, 0, NULL, NULL, cb)) {
  138. ERR_print_errors(bio_err);
  139. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  140. goto end;
  141. }
  142. } else if (informat == FORMAT_ASN1) {
  143. dsa = d2i_DSAparams_bio(in, NULL);
  144. } else {
  145. dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
  146. }
  147. if (dsa == NULL) {
  148. BIO_printf(bio_err, "unable to load DSA parameters\n");
  149. ERR_print_errors(bio_err);
  150. goto end;
  151. }
  152. if (text) {
  153. DSAparams_print(out, dsa);
  154. }
  155. if (C) {
  156. const BIGNUM *p = NULL, *q = NULL, *g = NULL;
  157. unsigned char *data;
  158. int len, bits_p;
  159. DSA_get0_pqg(dsa, &p, &q, &g);
  160. len = BN_num_bytes(p);
  161. bits_p = BN_num_bits(p);
  162. data = app_malloc(len + 20, "BN space");
  163. BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
  164. print_bignum_var(bio_out, p, "dsap", bits_p, data);
  165. print_bignum_var(bio_out, q, "dsaq", bits_p, data);
  166. print_bignum_var(bio_out, g, "dsag", bits_p, data);
  167. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  168. " BIGNUM *p, *q, *g;\n"
  169. "\n");
  170. BIO_printf(bio_out, " if (dsa == NULL)\n"
  171. " return NULL;\n");
  172. BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
  173. bits_p, bits_p);
  174. BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
  175. bits_p, bits_p);
  176. BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
  177. bits_p, bits_p);
  178. BIO_printf(bio_out, " DSA_free(dsa);\n"
  179. " BN_free(p);\n"
  180. " BN_free(q);\n"
  181. " BN_free(g);\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. static const char symbols[] = ".+*\n";
  229. char c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  230. BIO_write(BN_GENCB_get_arg(cb), &c, 1);
  231. (void)BIO_flush(BN_GENCB_get_arg(cb));
  232. return 1;
  233. }