dsaparam.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright 1995-2020 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. #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 verbose = 0;
  23. static int gendsa_cb(EVP_PKEY_CTX *ctx);
  24. typedef enum OPTION_choice {
  25. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  26. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT, OPT_C,
  27. OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
  28. OPT_R_ENUM, OPT_PROV_ENUM
  29. } OPTION_CHOICE;
  30. const OPTIONS dsaparam_options[] = {
  31. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
  32. OPT_SECTION("General"),
  33. {"help", OPT_HELP, '-', "Display this summary"},
  34. #ifndef OPENSSL_NO_ENGINE
  35. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  36. #endif
  37. OPT_SECTION("Input"),
  38. {"in", OPT_IN, '<', "Input file"},
  39. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  40. OPT_SECTION("Output"),
  41. {"out", OPT_OUT, '>', "Output file"},
  42. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  43. {"text", OPT_TEXT, '-', "Print as text"},
  44. {"C", OPT_C, '-', "Output C code"},
  45. {"noout", OPT_NOOUT, '-', "No output"},
  46. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  47. {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
  48. OPT_R_OPTIONS,
  49. OPT_PROV_OPTIONS,
  50. OPT_PARAMETERS(),
  51. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  52. {NULL}
  53. };
  54. int dsaparam_main(int argc, char **argv)
  55. {
  56. ENGINE *e = NULL;
  57. BIO *in = NULL, *out = NULL;
  58. EVP_PKEY *params = NULL, *pkey = NULL;
  59. EVP_PKEY_CTX *ctx = NULL;
  60. int numbits = -1, num = 0, genkey = 0;
  61. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0, C = 0;
  62. int ret = 1, i, text = 0, private = 0;
  63. char *infile = NULL, *outfile = NULL, *prog;
  64. OPTION_CHOICE o;
  65. prog = opt_init(argc, argv, dsaparam_options);
  66. while ((o = opt_next()) != OPT_EOF) {
  67. switch (o) {
  68. case OPT_EOF:
  69. case OPT_ERR:
  70. opthelp:
  71. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  72. goto end;
  73. case OPT_HELP:
  74. opt_help(dsaparam_options);
  75. ret = 0;
  76. goto end;
  77. case OPT_INFORM:
  78. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  79. goto opthelp;
  80. break;
  81. case OPT_IN:
  82. infile = opt_arg();
  83. break;
  84. case OPT_OUTFORM:
  85. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  86. goto opthelp;
  87. break;
  88. case OPT_OUT:
  89. outfile = opt_arg();
  90. break;
  91. case OPT_ENGINE:
  92. e = setup_engine(opt_arg(), 0);
  93. break;
  94. case OPT_TEXT:
  95. text = 1;
  96. break;
  97. case OPT_C:
  98. C = 1;
  99. break;
  100. case OPT_GENKEY:
  101. genkey = 1;
  102. break;
  103. case OPT_R_CASES:
  104. if (!opt_rand(o))
  105. goto end;
  106. break;
  107. case OPT_PROV_CASES:
  108. if (!opt_provider(o))
  109. goto end;
  110. break;
  111. case OPT_NOOUT:
  112. noout = 1;
  113. break;
  114. case OPT_VERBOSE:
  115. verbose = 1;
  116. break;
  117. }
  118. }
  119. argc = opt_num_rest();
  120. argv = opt_rest();
  121. if (argc == 1) {
  122. if (!opt_int(argv[0], &num) || num < 0)
  123. goto end;
  124. /* generate a key */
  125. numbits = num;
  126. }
  127. private = genkey ? 1 : 0;
  128. in = bio_open_default(infile, 'r', informat);
  129. if (in == NULL)
  130. goto end;
  131. out = bio_open_owner(outfile, outformat, private);
  132. if (out == NULL)
  133. goto end;
  134. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
  135. if (ctx == NULL) {
  136. BIO_printf(bio_err,
  137. "Error, DSA parameter generation context allocation failed\n");
  138. goto end;
  139. }
  140. if (numbits > 0) {
  141. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  142. BIO_printf(bio_err,
  143. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  144. " Your key size is %d! Larger key size may behave not as expected.\n",
  145. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  146. EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
  147. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  148. if (verbose) {
  149. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  150. num);
  151. BIO_printf(bio_err, "This could take some time\n");
  152. }
  153. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  154. BIO_printf(bio_err,
  155. "Error, DSA key generation paramgen init failed\n");
  156. goto end;
  157. }
  158. if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
  159. BIO_printf(bio_err,
  160. "Error, DSA key generation setting bit length failed\n");
  161. goto end;
  162. }
  163. if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
  164. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  165. goto end;
  166. }
  167. } else if (informat == FORMAT_ASN1) {
  168. params = d2i_KeyParams_bio(EVP_PKEY_DSA, NULL, in);
  169. } else {
  170. params = PEM_read_bio_Parameters(in, NULL);
  171. }
  172. if (params == NULL) {
  173. BIO_printf(bio_err, "Error, unable to load DSA parameters\n");
  174. goto end;
  175. }
  176. if (text) {
  177. EVP_PKEY_print_params(out, params, 0, NULL);
  178. }
  179. if (C) {
  180. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  181. unsigned char *data;
  182. int len, bits_p;
  183. EVP_PKEY_get_bn_param(params, "p", &p);
  184. EVP_PKEY_get_bn_param(params, "q", &q);
  185. EVP_PKEY_get_bn_param(params, "g", &g);
  186. len = BN_num_bytes(p);
  187. bits_p = BN_num_bits(p);
  188. data = app_malloc(len + 20, "BN space");
  189. BIO_printf(bio_out, "static DSA *get_dsa%d(void)\n{\n", bits_p);
  190. print_bignum_var(bio_out, p, "dsap", bits_p, data);
  191. print_bignum_var(bio_out, q, "dsaq", bits_p, data);
  192. print_bignum_var(bio_out, g, "dsag", bits_p, data);
  193. BN_free(p);
  194. BN_free(q);
  195. BN_free(g);
  196. BIO_printf(bio_out, " DSA *dsa = DSA_new();\n"
  197. " BIGNUM *p, *q, *g;\n"
  198. "\n");
  199. BIO_printf(bio_out, " if (dsa == NULL)\n"
  200. " return NULL;\n");
  201. BIO_printf(bio_out, " if (!DSA_set0_pqg(dsa, p = BN_bin2bn(dsap_%d, sizeof(dsap_%d), NULL),\n",
  202. bits_p, bits_p);
  203. BIO_printf(bio_out, " q = BN_bin2bn(dsaq_%d, sizeof(dsaq_%d), NULL),\n",
  204. bits_p, bits_p);
  205. BIO_printf(bio_out, " g = BN_bin2bn(dsag_%d, sizeof(dsag_%d), NULL))) {\n",
  206. bits_p, bits_p);
  207. BIO_printf(bio_out, " DSA_free(dsa);\n"
  208. " BN_free(p);\n"
  209. " BN_free(q);\n"
  210. " BN_free(g);\n"
  211. " return NULL;\n"
  212. " }\n"
  213. " return dsa;\n}\n");
  214. OPENSSL_free(data);
  215. }
  216. if (outformat == FORMAT_ASN1 && genkey)
  217. noout = 1;
  218. if (!noout) {
  219. if (outformat == FORMAT_ASN1)
  220. i = i2d_KeyParams_bio(out, params);
  221. else
  222. i = PEM_write_bio_Parameters(out, params);
  223. if (!i) {
  224. BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
  225. goto end;
  226. }
  227. }
  228. if (genkey) {
  229. EVP_PKEY_CTX_free(ctx);
  230. ctx = EVP_PKEY_CTX_new(params, NULL);
  231. if (ctx == NULL) {
  232. BIO_printf(bio_err,
  233. "Error, DSA key generation context allocation failed\n");
  234. goto end;
  235. }
  236. if (!EVP_PKEY_keygen_init(ctx)) {
  237. BIO_printf(bio_err,
  238. "Error, unable to initialise for key generation\n");
  239. goto end;
  240. }
  241. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  242. BIO_printf(bio_err, "Error, unable to generate key\n");
  243. goto end;
  244. }
  245. assert(private);
  246. if (outformat == FORMAT_ASN1)
  247. i = i2d_PrivateKey_bio(out, pkey);
  248. else
  249. i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
  250. }
  251. ret = 0;
  252. end:
  253. if (ret != 0)
  254. ERR_print_errors(bio_err);
  255. BIO_free(in);
  256. BIO_free_all(out);
  257. EVP_PKEY_CTX_free(ctx);
  258. EVP_PKEY_free(pkey);
  259. EVP_PKEY_free(params);
  260. release_engine(e);
  261. return ret;
  262. }
  263. static int gendsa_cb(EVP_PKEY_CTX *ctx)
  264. {
  265. static const char symbols[] = ".+*\n";
  266. int p;
  267. char c;
  268. BIO *b;
  269. if (!verbose)
  270. return 1;
  271. b = EVP_PKEY_CTX_get_app_data(ctx);
  272. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  273. c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  274. BIO_write(b, &c, 1);
  275. (void)BIO_flush(b);
  276. return 1;
  277. }