dsaparam.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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,
  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. {"noout", OPT_NOOUT, '-', "No output"},
  45. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  46. {"genkey", OPT_GENKEY, '-', "Generate a DSA key"},
  47. OPT_R_OPTIONS,
  48. OPT_PROV_OPTIONS,
  49. OPT_PARAMETERS(),
  50. {"numbits", 0, 0, "Number of bits if generating parameters (optional)"},
  51. {NULL}
  52. };
  53. int dsaparam_main(int argc, char **argv)
  54. {
  55. ENGINE *e = NULL;
  56. BIO *out = NULL;
  57. EVP_PKEY *params = NULL, *pkey = NULL;
  58. EVP_PKEY_CTX *ctx = NULL;
  59. int numbits = -1, num = 0, genkey = 0;
  60. int informat = FORMAT_PEM, outformat = FORMAT_PEM, noout = 0;
  61. int ret = 1, i, text = 0, private = 0;
  62. char *infile = NULL, *outfile = NULL, *prog;
  63. OPTION_CHOICE o;
  64. prog = opt_init(argc, argv, dsaparam_options);
  65. while ((o = opt_next()) != OPT_EOF) {
  66. switch (o) {
  67. case OPT_EOF:
  68. case OPT_ERR:
  69. opthelp:
  70. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  71. goto end;
  72. case OPT_HELP:
  73. opt_help(dsaparam_options);
  74. ret = 0;
  75. goto end;
  76. case OPT_INFORM:
  77. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  78. goto opthelp;
  79. break;
  80. case OPT_IN:
  81. infile = opt_arg();
  82. break;
  83. case OPT_OUTFORM:
  84. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &outformat))
  85. goto opthelp;
  86. break;
  87. case OPT_OUT:
  88. outfile = opt_arg();
  89. break;
  90. case OPT_ENGINE:
  91. e = setup_engine(opt_arg(), 0);
  92. break;
  93. case OPT_TEXT:
  94. text = 1;
  95. break;
  96. case OPT_GENKEY:
  97. genkey = 1;
  98. break;
  99. case OPT_R_CASES:
  100. if (!opt_rand(o))
  101. goto end;
  102. break;
  103. case OPT_PROV_CASES:
  104. if (!opt_provider(o))
  105. goto end;
  106. break;
  107. case OPT_NOOUT:
  108. noout = 1;
  109. break;
  110. case OPT_VERBOSE:
  111. verbose = 1;
  112. break;
  113. }
  114. }
  115. /* Optional arg is bitsize. */
  116. argc = opt_num_rest();
  117. argv = opt_rest();
  118. if (argc == 1) {
  119. if (!opt_int(argv[0], &num) || num < 0)
  120. goto opthelp;
  121. } else if (argc != 0) {
  122. goto opthelp;
  123. }
  124. /* generate a key */
  125. numbits = num;
  126. private = genkey ? 1 : 0;
  127. out = bio_open_owner(outfile, outformat, private);
  128. if (out == NULL)
  129. goto end;
  130. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
  131. if (ctx == NULL) {
  132. BIO_printf(bio_err,
  133. "Error, DSA parameter generation context allocation failed\n");
  134. goto end;
  135. }
  136. if (numbits > 0) {
  137. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  138. BIO_printf(bio_err,
  139. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  140. " Your key size is %d! Larger key size may behave not as expected.\n",
  141. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  142. EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
  143. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  144. if (verbose) {
  145. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  146. num);
  147. BIO_printf(bio_err, "This could take some time\n");
  148. }
  149. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  150. BIO_printf(bio_err,
  151. "Error, DSA key generation paramgen init failed\n");
  152. goto end;
  153. }
  154. if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
  155. BIO_printf(bio_err,
  156. "Error, DSA key generation setting bit length failed\n");
  157. goto end;
  158. }
  159. if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
  160. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  161. goto end;
  162. }
  163. } else {
  164. params = load_keyparams(infile, 1, "DSA", "DSA parameters");
  165. }
  166. if (params == NULL) {
  167. /* Error message should already have been displayed */
  168. goto end;
  169. }
  170. if (text) {
  171. EVP_PKEY_print_params(out, params, 0, NULL);
  172. }
  173. if (outformat == FORMAT_ASN1 && genkey)
  174. noout = 1;
  175. if (!noout) {
  176. if (outformat == FORMAT_ASN1)
  177. i = i2d_KeyParams_bio(out, params);
  178. else
  179. i = PEM_write_bio_Parameters(out, params);
  180. if (!i) {
  181. BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
  182. goto end;
  183. }
  184. }
  185. if (genkey) {
  186. EVP_PKEY_CTX_free(ctx);
  187. ctx = EVP_PKEY_CTX_new(params, NULL);
  188. if (ctx == NULL) {
  189. BIO_printf(bio_err,
  190. "Error, DSA key generation context allocation failed\n");
  191. goto end;
  192. }
  193. if (!EVP_PKEY_keygen_init(ctx)) {
  194. BIO_printf(bio_err,
  195. "Error, unable to initialise for key generation\n");
  196. goto end;
  197. }
  198. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  199. BIO_printf(bio_err, "Error, unable to generate key\n");
  200. goto end;
  201. }
  202. assert(private);
  203. if (outformat == FORMAT_ASN1)
  204. i = i2d_PrivateKey_bio(out, pkey);
  205. else
  206. i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
  207. }
  208. ret = 0;
  209. end:
  210. if (ret != 0)
  211. ERR_print_errors(bio_err);
  212. BIO_free_all(out);
  213. EVP_PKEY_CTX_free(ctx);
  214. EVP_PKEY_free(pkey);
  215. EVP_PKEY_free(params);
  216. release_engine(e);
  217. return ret;
  218. }
  219. static int gendsa_cb(EVP_PKEY_CTX *ctx)
  220. {
  221. static const char symbols[] = ".+*\n";
  222. int p;
  223. char c;
  224. BIO *b;
  225. if (!verbose)
  226. return 1;
  227. b = EVP_PKEY_CTX_get_app_data(ctx);
  228. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  229. c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  230. BIO_write(b, &c, 1);
  231. (void)BIO_flush(b);
  232. return 1;
  233. }