gendsa.c 3.9 KB

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