dsaparam.c 7.3 KB

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