dsaparam.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 1995-2021 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 "apps.h"
  13. #include <time.h>
  14. #include <string.h>
  15. #include "apps.h"
  16. #include "progs.h"
  17. #include <openssl/bio.h>
  18. #include <openssl/err.h>
  19. #include <openssl/bn.h>
  20. #include <openssl/dsa.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/pem.h>
  23. static int verbose = 0;
  24. static int gendsa_cb(EVP_PKEY_CTX *ctx);
  25. typedef enum OPTION_choice {
  26. OPT_COMMON,
  27. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_TEXT,
  28. OPT_NOOUT, OPT_GENKEY, OPT_ENGINE, OPT_VERBOSE,
  29. OPT_R_ENUM, OPT_PROV_ENUM
  30. } OPTION_CHOICE;
  31. const OPTIONS dsaparam_options[] = {
  32. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [numbits]\n"},
  33. OPT_SECTION("General"),
  34. {"help", OPT_HELP, '-', "Display this summary"},
  35. #ifndef OPENSSL_NO_ENGINE
  36. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  37. #endif
  38. OPT_SECTION("Input"),
  39. {"in", OPT_IN, '<', "Input file"},
  40. {"inform", OPT_INFORM, 'F', "Input format - DER or PEM"},
  41. OPT_SECTION("Output"),
  42. {"out", OPT_OUT, '>', "Output file"},
  43. {"outform", OPT_OUTFORM, 'F', "Output format - DER or PEM"},
  44. {"text", OPT_TEXT, '-', "Print as text"},
  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 *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_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. }
  115. }
  116. /* Optional arg is bitsize. */
  117. argc = opt_num_rest();
  118. argv = opt_rest();
  119. if (argc == 1) {
  120. if (!opt_int(argv[0], &num) || num < 0)
  121. goto opthelp;
  122. } else if (argc != 0) {
  123. goto opthelp;
  124. }
  125. if (!app_RAND_load())
  126. goto end;
  127. /* generate a key */
  128. numbits = num;
  129. private = genkey ? 1 : 0;
  130. out = bio_open_owner(outfile, outformat, private);
  131. if (out == NULL)
  132. goto end;
  133. ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL);
  134. if (ctx == NULL) {
  135. BIO_printf(bio_err,
  136. "Error, DSA parameter generation context allocation failed\n");
  137. goto end;
  138. }
  139. if (numbits > 0) {
  140. if (numbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  141. BIO_printf(bio_err,
  142. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  143. " Your key size is %d! Larger key size may behave not as expected.\n",
  144. OPENSSL_DSA_MAX_MODULUS_BITS, numbits);
  145. EVP_PKEY_CTX_set_cb(ctx, gendsa_cb);
  146. EVP_PKEY_CTX_set_app_data(ctx, bio_err);
  147. if (verbose) {
  148. BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n",
  149. num);
  150. BIO_printf(bio_err, "This could take some time\n");
  151. }
  152. if (EVP_PKEY_paramgen_init(ctx) <= 0) {
  153. BIO_printf(bio_err,
  154. "Error, DSA key generation paramgen init failed\n");
  155. goto end;
  156. }
  157. if (!EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, num)) {
  158. BIO_printf(bio_err,
  159. "Error, DSA key generation setting bit length failed\n");
  160. goto end;
  161. }
  162. params = app_paramgen(ctx, "DSA");
  163. } else {
  164. params = load_keyparams(infile, informat, 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. pkey = app_keygen(ctx, "DSA", numbits, verbose);
  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. }