2
0

gendsa.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 1995-2018 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. #ifdef OPENSSL_NO_DSA
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. # include <stdio.h>
  14. # include <string.h>
  15. # include <sys/types.h>
  16. # include <sys/stat.h>
  17. # include "apps.h"
  18. # include "progs.h"
  19. # include <openssl/bio.h>
  20. # include <openssl/err.h>
  21. # include <openssl/bn.h>
  22. # include <openssl/dsa.h>
  23. # include <openssl/x509.h>
  24. # include <openssl/pem.h>
  25. typedef enum OPTION_choice {
  26. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  27. OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE,
  28. OPT_R_ENUM
  29. } OPTION_CHOICE;
  30. const OPTIONS gendsa_options[] = {
  31. {OPT_HELP_STR, 1, '-', "Usage: %s [args] dsaparam-file\n"},
  32. {OPT_HELP_STR, 1, '-', "Valid options are:\n"},
  33. {"help", OPT_HELP, '-', "Display this summary"},
  34. {"out", OPT_OUT, '>', "Output the key to the specified file"},
  35. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  36. OPT_R_OPTIONS,
  37. {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
  38. # ifndef OPENSSL_NO_ENGINE
  39. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  40. # endif
  41. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  42. {NULL}
  43. };
  44. int gendsa_main(int argc, char **argv)
  45. {
  46. ENGINE *e = NULL;
  47. BIO *out = NULL, *in = NULL;
  48. DSA *dsa = NULL;
  49. const EVP_CIPHER *enc = NULL;
  50. char *dsaparams = NULL;
  51. char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
  52. OPTION_CHOICE o;
  53. int ret = 1, private = 0, verbose = 0;
  54. const BIGNUM *p = NULL;
  55. prog = opt_init(argc, argv, gendsa_options);
  56. while ((o = opt_next()) != OPT_EOF) {
  57. switch (o) {
  58. case OPT_EOF:
  59. case OPT_ERR:
  60. opthelp:
  61. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  62. goto end;
  63. case OPT_HELP:
  64. ret = 0;
  65. opt_help(gendsa_options);
  66. goto end;
  67. case OPT_OUT:
  68. outfile = opt_arg();
  69. break;
  70. case OPT_PASSOUT:
  71. passoutarg = opt_arg();
  72. break;
  73. case OPT_ENGINE:
  74. e = setup_engine(opt_arg(), 0);
  75. break;
  76. case OPT_R_CASES:
  77. if (!opt_rand(o))
  78. goto end;
  79. break;
  80. case OPT_CIPHER:
  81. if (!opt_cipher(opt_unknown(), &enc))
  82. goto end;
  83. break;
  84. case OPT_VERBOSE:
  85. verbose = 1;
  86. break;
  87. }
  88. }
  89. argc = opt_num_rest();
  90. argv = opt_rest();
  91. private = 1;
  92. if (argc != 1)
  93. goto opthelp;
  94. dsaparams = *argv;
  95. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  96. BIO_printf(bio_err, "Error getting password\n");
  97. goto end;
  98. }
  99. in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
  100. if (in == NULL)
  101. goto end2;
  102. if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
  103. BIO_printf(bio_err, "unable to load DSA parameter file\n");
  104. goto end;
  105. }
  106. BIO_free(in);
  107. in = NULL;
  108. out = bio_open_owner(outfile, FORMAT_PEM, private);
  109. if (out == NULL)
  110. goto end2;
  111. DSA_get0_pqg(dsa, &p, NULL, NULL);
  112. if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
  113. BIO_printf(bio_err,
  114. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  115. " Your key size is %d! Larger key size may behave not as expected.\n",
  116. OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
  117. if (verbose)
  118. BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
  119. if (!DSA_generate_key(dsa))
  120. goto end;
  121. assert(private);
  122. if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
  123. goto end;
  124. ret = 0;
  125. end:
  126. if (ret != 0)
  127. ERR_print_errors(bio_err);
  128. end2:
  129. BIO_free(in);
  130. BIO_free_all(out);
  131. DSA_free(dsa);
  132. release_engine(e);
  133. OPENSSL_free(passout);
  134. return ret;
  135. }
  136. #endif