gendsa.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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,
  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. {NULL}
  42. };
  43. int gendsa_main(int argc, char **argv)
  44. {
  45. ENGINE *e = NULL;
  46. BIO *out = NULL, *in = NULL;
  47. DSA *dsa = NULL;
  48. const EVP_CIPHER *enc = NULL;
  49. char *dsaparams = NULL;
  50. char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
  51. OPTION_CHOICE o;
  52. int ret = 1, private = 0;
  53. const BIGNUM *p = NULL;
  54. prog = opt_init(argc, argv, gendsa_options);
  55. while ((o = opt_next()) != OPT_EOF) {
  56. switch (o) {
  57. case OPT_EOF:
  58. case OPT_ERR:
  59. opthelp:
  60. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  61. goto end;
  62. case OPT_HELP:
  63. ret = 0;
  64. opt_help(gendsa_options);
  65. goto end;
  66. case OPT_OUT:
  67. outfile = opt_arg();
  68. break;
  69. case OPT_PASSOUT:
  70. passoutarg = opt_arg();
  71. break;
  72. case OPT_ENGINE:
  73. e = setup_engine(opt_arg(), 0);
  74. break;
  75. case OPT_R_CASES:
  76. if (!opt_rand(o))
  77. goto end;
  78. break;
  79. case OPT_CIPHER:
  80. if (!opt_cipher(opt_unknown(), &enc))
  81. goto end;
  82. break;
  83. }
  84. }
  85. argc = opt_num_rest();
  86. argv = opt_rest();
  87. private = 1;
  88. if (argc != 1)
  89. goto opthelp;
  90. dsaparams = *argv;
  91. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  92. BIO_printf(bio_err, "Error getting password\n");
  93. goto end;
  94. }
  95. in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
  96. if (in == NULL)
  97. goto end2;
  98. if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
  99. BIO_printf(bio_err, "unable to load DSA parameter file\n");
  100. goto end;
  101. }
  102. BIO_free(in);
  103. in = NULL;
  104. out = bio_open_owner(outfile, FORMAT_PEM, private);
  105. if (out == NULL)
  106. goto end2;
  107. DSA_get0_pqg(dsa, &p, NULL, NULL);
  108. if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
  109. BIO_printf(bio_err,
  110. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  111. " Your key size is %d! Larger key size may behave not as expected.\n",
  112. OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
  113. BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
  114. if (!DSA_generate_key(dsa))
  115. goto end;
  116. assert(private);
  117. if (!PEM_write_bio_DSAPrivateKey(out, dsa, enc, NULL, 0, NULL, passout))
  118. goto end;
  119. ret = 0;
  120. end:
  121. if (ret != 0)
  122. ERR_print_errors(bio_err);
  123. end2:
  124. BIO_free(in);
  125. BIO_free_all(out);
  126. DSA_free(dsa);
  127. release_engine(e);
  128. OPENSSL_free(passout);
  129. return ret;
  130. }
  131. #endif