dsaparam.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 1995-2023 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. typedef enum OPTION_choice {
  24. OPT_COMMON,
  25. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
  26. OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE, OPT_QUIET,
  27. OPT_R_ENUM, OPT_PROV_ENUM
  28. } OPTION_CHOICE;
  29. const OPTIONS dsaparam_options[] = {
  30. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits] [numqbits]\n"},
  31. OPT_SECTION("General"),
  32. {"help", OPT_HELP, '-', "Display this summary"},
  33. #ifndef OPENSSL_NO_ENGINE
  34. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  35. #endif
  36. OPT_SECTION("Input"),
  37. {"in", OPT_IN, '<', "Input file"},
  38. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  39. OPT_SECTION("Output"),
  40. {"out", OPT_OUT, '>', "Output file"},
  41. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  42. {"text", OPT_TEXT, '-', "Print as text"},
  43. {"noout", OPT_NOOUT, '-', "No output"},
  44. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  45. {"quiet", OPT_QUIET, '-', "Terse 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 or key (optional)"},
  51. {"numqbits", 0, 0, "Number of bits in the subprime parameter q if generating parameters or key (optional)"},
  52. {NULL}
  53. };
  54. int dsaparam_main(int argc, char **argv)
  55. {
  56. ENGINE *e = NULL;
  57. BIO *out = NULL;
  58. EVP_PKEY *params = NULL, *pkey = NULL;
  59. EVP_PKEY_CTX *ctx = NULL;
  60. int numbits = -1, numqbits = -1, num = 0, genkey = 0;
  61. int informat = FORMAT_UNDEF, outformat = FORMAT_PEM, noout = 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_GENKEY:
  98. genkey = 1;
  99. break;
  100. case OPT_R_CASES:
  101. if (!opt_rand(o))
  102. goto end;
  103. break;
  104. case OPT_PROV_CASES:
  105. if (!opt_provider(o))
  106. goto end;
  107. break;
  108. case OPT_NOOUT:
  109. noout = 1;
  110. break;
  111. case OPT_VERBOSE:
  112. verbose = 1;
  113. break;
  114. case OPT_QUIET:
  115. verbose = 0;
  116. break;
  117. }
  118. }
  119. /* Optional args are bitsize and q bitsize. */
  120. argc = opt_num_rest();
  121. argv = opt_rest();
  122. if (argc == 2) {
  123. if (!opt_int(argv[0], &num) || num < 0)
  124. goto opthelp;
  125. if (!opt_int(argv[1], &numqbits) || numqbits < 0)
  126. goto opthelp;
  127. } else if (argc == 1) {
  128. if (!opt_int(argv[0], &num) || num < 0)
  129. goto opthelp;
  130. } else if (!opt_check_rest_arg(NULL)) {
  131. goto opthelp;
  132. }
  133. if (!app_RAND_load())
  134. goto end;
  135. /* generate a key */
  136. numbits = num;
  137. private = genkey ? 1 : 0;
  138. out = bio_open_owner(outfile, outformat, private);
  139. if (out == NULL)
  140. goto end;
  141. ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
  142. if (ctx == NULL) {
  143. BIO_printf(bio_err,
  144. "Error, DSA parameter generation context allocation failed\n");
  145. goto end;
  146. }
  147. if (numbits > 0) {
  148. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  149. BIO_printf(bio_err,
  150. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  151. " Your key size is %d! Larger key size may behave not as expected.\n",
  152. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  153. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  154. if (verbose) {
  155. EVP_PKEY_CTX_set_cb(ctx, progress_cb);
  156. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  157. num);
  158. BIO_printf(bio_err, "This could take some time\n");
  159. }
  160. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  161. BIO_printf(bio_err,
  162. "Error, DSA key generation paramgen init failed\n");
  163. goto end;
  164. }
  165. if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
  166. BIO_printf(bio_err,
  167. "Error, DSA key generation setting bit length failed\n");
  168. goto end;
  169. }
  170. if (numqbits > 0) {
  171. if (EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, numqbits) <= 0) {
  172. BIO_printf(bio_err,
  173. "Error, DSA key generation setting subprime bit length failed\n");
  174. goto end;
  175. }
  176. }
  177. params = app_paramgen(ctx, "DSA");
  178. } else {
  179. params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
  180. }
  181. if (params == NULL) {
  182. /* Error message should already have been displayed */
  183. goto end;
  184. }
  185. if (text) {
  186. EVP_PKEY_print_params(out, params, 0, NULL);
  187. }
  188. if (outformat == FORMAT_ASN1 && genkey)
  189. noout = 1;
  190. if (!noout) {
  191. if (outformat == FORMAT_ASN1)
  192. i = i2d_KeyParams_bio(out, params);
  193. else
  194. i = PEM_write_bio_Parameters(out, params);
  195. if (!i) {
  196. BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
  197. goto end;
  198. }
  199. }
  200. if (genkey) {
  201. EVP_PKEY_CTX_free(ctx);
  202. ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
  203. app_get0_propq());
  204. if (ctx == NULL) {
  205. BIO_printf(bio_err,
  206. "Error, DSA key generation context allocation failed\n");
  207. goto end;
  208. }
  209. if (EVP_PKEY_keygen_init(ctx) <= 0) {
  210. BIO_printf(bio_err,
  211. "Error, unable to initialise for key generation\n");
  212. goto end;
  213. }
  214. pkey = app_keygen(ctx, "DSA", numbits, verbose);
  215. if (pkey == NULL)
  216. goto end;
  217. assert(private);
  218. if (outformat == FORMAT_ASN1)
  219. i = i2d_PrivateKey_bio(out, pkey);
  220. else
  221. i = PEM_write_bio_PrivateKey(out, pkey, NULL, NULL, 0, NULL, NULL);
  222. }
  223. ret = 0;
  224. end:
  225. if (ret != 0)
  226. ERR_print_errors(bio_err);
  227. BIO_free_all(out);
  228. EVP_PKEY_CTX_free(ctx);
  229. EVP_PKEY_free(pkey);
  230. EVP_PKEY_free(params);
  231. release_engine(e);
  232. return ret;
  233. }