dsaparam.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. argc = opt_num_rest();
  116. argv = opt_rest();
  117. if (argc == 1) {
  118. if (!opt_int(argv[0], &num) || num < 0)
  119. goto end;
  120. /* generate a key */
  121. numbits = num;
  122. }
  123. private = genkey ? 1 : 0;
  124. out = bio_open_owner(outfile, outformat, private);
  125. if (out == NULL)
  126. goto end;
  127. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
  128. if (ctx == NULL) {
  129. BIO_printf(bio_err,
  130. "Error, DSA parameter generation context allocation failed\n");
  131. goto end;
  132. }
  133. if (numbits > 0) {
  134. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  135. BIO_printf(bio_err,
  136. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  137. " Your key size is %d! Larger key size may behave not as expected.\n",
  138. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  139. EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
  140. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  141. if (verbose) {
  142. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  143. num);
  144. BIO_printf(bio_err, "This could take some time\n");
  145. }
  146. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  147. BIO_printf(bio_err,
  148. "Error, DSA key generation paramgen init failed\n");
  149. goto end;
  150. }
  151. if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
  152. BIO_printf(bio_err,
  153. "Error, DSA key generation setting bit length failed\n");
  154. goto end;
  155. }
  156. if (EVP_PKEY_paramgen(ctx, &params) <= 0) {
  157. BIO_printf(bio_err, "Error, DSA key generation failed\n");
  158. goto end;
  159. }
  160. } else {
  161. params = load_keyparams(infile, 1, "DSA", "DSA parameters");
  162. }
  163. if (params == NULL) {
  164. /* Error message should already have been displayed */
  165. goto end;
  166. }
  167. if (text) {
  168. EVP_PKEY_print_params(out, params, 0, NULL);
  169. }
  170. if (outformat == FORMAT_ASN1 && genkey)
  171. noout = 1;
  172. if (!noout) {
  173. if (outformat == FORMAT_ASN1)
  174. i = i2d_KeyParams_bio(out, params);
  175. else
  176. i = PEM_write_bio_Parameters(out, params);
  177. if (!i) {
  178. BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
  179. goto end;
  180. }
  181. }
  182. if (genkey) {
  183. EVP_PKEY_CTX_free(ctx);
  184. ctx = EVP_PKEY_CTX_new(params, NULL);
  185. if (ctx == NULL) {
  186. BIO_printf(bio_err,
  187. "Error, DSA key generation context allocation failed\n");
  188. goto end;
  189. }
  190. if (!EVP_PKEY_keygen_init(ctx)) {
  191. BIO_printf(bio_err,
  192. "Error, unable to initialise for key generation\n");
  193. goto end;
  194. }
  195. if (!EVP_PKEY_keygen(ctx, &pkey)) {
  196. BIO_printf(bio_err, "Error, unable to generate key\n");
  197. goto end;
  198. }
  199. assert(private);
  200. if (outformat == FORMAT_ASN1)
  201. i = i2d_PrivateKey_bio(out, pkey);
  202. else
  203. i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
  204. }
  205. ret = 0;
  206. end:
  207. if (ret != 0)
  208. ERR_print_errors(bio_err);
  209. BIO_free_all(out);
  210. EVP_PKEY_CTX_free(ctx);
  211. EVP_PKEY_free(pkey);
  212. EVP_PKEY_free(params);
  213. release_engine(e);
  214. return ret;
  215. }
  216. static int gendsa_cb(EVP_PKEY_CTX *ctx)
  217. {
  218. static const char symbols[] = ".+*\n";
  219. int p;
  220. char c;
  221. BIO *b;
  222. if (!verbose)
  223. return 1;
  224. b = EVP_PKEY_CTX_get_app_data(ctx);
  225. p = EVP_PKEY_CTX_get_keygen_info(ctx, 0);
  226. c = (p >= 0 && (size_t)p < sizeof(symbols) - 1) ? symbols[p] : '?';
  227. BIO_write(b, &c, 1);
  228. (void)BIO_flush(b);
  229. return 1;
  230. }