dsaparam.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 1995-2022 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]\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 (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_UNDEF, 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. case OPT_QUIET:
  114. verbose = 0;
  115. break;
  116. }
  117. }
  118. /* Optional arg is bitsize. */
  119. argc = opt_num_rest();
  120. argv = opt_rest();
  121. if (argc == 1) {
  122. if (!opt_int(argv[0], &num) || num < 0)
  123. goto opthelp;
  124. } else if (!opt_check_rest_arg(NULL)) {
  125. goto opthelp;
  126. }
  127. if (!app_RAND_load())
  128. goto end;
  129. /* generate a key */
  130. numbits = num;
  131. private = genkey ? 1 : 0;
  132. out = bio_open_owner(outfile, outformat, private);
  133. if (out == NULL)
  134. goto end;
  135. ctx = EVP_PKEY_CTX_new_from_name(app_get0_libctx(), "DSA", app_get0_propq());
  136. if (ctx == NULL) {
  137. BIO_printf(bio_err,
  138. "Error, DSA parameter generation context allocation failed\n");
  139. goto end;
  140. }
  141. if (numbits > 0) {
  142. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  143. BIO_printf(bio_err,
  144. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  145. " Your key size is %d! Larger key size may behave not as expected.\n",
  146. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  147. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  148. if (verbose) {
  149. EVP_PKEY_CTX_set_cb(ctx, progress_cb);
  150. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  151. num);
  152. BIO_printf(bio_err, "This could take some time\n");
  153. }
  154. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  155. BIO_printf(bio_err,
  156. "Error, DSA key generation paramgen init failed\n");
  157. goto end;
  158. }
  159. if (EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num) <= 0) {
  160. BIO_printf(bio_err,
  161. "Error, DSA key generation setting bit length failed\n");
  162. goto end;
  163. }
  164. params = app_paramgen(ctx, "DSA");
  165. } else {
  166. params = load_keyparams(infile, informat, 1, "DSA", "DSA parameters");
  167. }
  168. if (params == NULL) {
  169. /* Error message should already have been displayed */
  170. goto end;
  171. }
  172. if (text) {
  173. EVP_PKEY_print_params(out, params, 0, NULL);
  174. }
  175. if (outformat == FORMAT_ASN1 && genkey)
  176. noout = 1;
  177. if (!noout) {
  178. if (outformat == FORMAT_ASN1)
  179. i = i2d_KeyParams_bio(out, params);
  180. else
  181. i = PEM_write_bio_Parameters(out, params);
  182. if (!i) {
  183. BIO_printf(bio_err, "Error, unable to write DSA parameters\n");
  184. goto end;
  185. }
  186. }
  187. if (genkey) {
  188. EVP_PKEY_CTX_free(ctx);
  189. ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), params,
  190. app_get0_propq());
  191. if (ctx == NULL) {
  192. BIO_printf(bio_err,
  193. "Error, DSA key generation context allocation failed\n");
  194. goto end;
  195. }
  196. if (EVP_PKEY_keygen_init(ctx) <= 0) {
  197. BIO_printf(bio_err,
  198. "Error, unable to initialise for key generation\n");
  199. goto end;
  200. }
  201. pkey = app_keygen(ctx, "DSA", numbits, verbose);
  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. }