2
0

gendsa.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright 1995-2023 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 <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_COMMON,
  24. OPT_OUT, OPT_PASSOUT, OPT_ENGINE, OPT_CIPHER, OPT_VERBOSE, OPT_QUIET,
  25. OPT_R_ENUM, OPT_PROV_ENUM
  26. } OPTION_CHOICE;
  27. const OPTIONS gendsa_options[] = {
  28. {OPT_HELP_STR, 1, '-', "Usage: %s [options] dsaparam-file\n"},
  29. OPT_SECTION("General"),
  30. {"help", OPT_HELP, '-', "Display this summary"},
  31. #ifndef OPENSSL_NO_ENGINE
  32. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  33. #endif
  34. OPT_SECTION("Output"),
  35. {"out", OPT_OUT, '>', "Output the key to the specified file"},
  36. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  37. OPT_R_OPTIONS,
  38. OPT_PROV_OPTIONS,
  39. {"", OPT_CIPHER, '-', "Encrypt the output with any supported cipher"},
  40. {"verbose", OPT_VERBOSE, '-', "Verbose output"},
  41. {"quiet", OPT_QUIET, '-', "Terse output"},
  42. OPT_PARAMETERS(),
  43. {"dsaparam-file", 0, 0, "File containing DSA parameters"},
  44. {NULL}
  45. };
  46. int gendsa_main(int argc, char **argv)
  47. {
  48. ENGINE *e = NULL;
  49. BIO *out = NULL, *in = NULL;
  50. EVP_PKEY *pkey = NULL;
  51. EVP_PKEY_CTX *ctx = NULL;
  52. EVP_CIPHER *enc = NULL;
  53. char *dsaparams = NULL, *ciphername = NULL;
  54. char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
  55. OPTION_CHOICE o;
  56. int ret = 1, private = 0, verbose = 0, nbits;
  57. opt_set_unknown_name("cipher");
  58. prog = opt_init(argc, argv, gendsa_options);
  59. while ((o = opt_next()) != OPT_EOF) {
  60. switch (o) {
  61. case OPT_EOF:
  62. case OPT_ERR:
  63. opthelp:
  64. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  65. goto end;
  66. case OPT_HELP:
  67. ret = 0;
  68. opt_help(gendsa_options);
  69. goto end;
  70. case OPT_OUT:
  71. outfile = opt_arg();
  72. break;
  73. case OPT_PASSOUT:
  74. passoutarg = opt_arg();
  75. break;
  76. case OPT_ENGINE:
  77. e = setup_engine(opt_arg(), 0);
  78. break;
  79. case OPT_R_CASES:
  80. if (!opt_rand(o))
  81. goto end;
  82. break;
  83. case OPT_PROV_CASES:
  84. if (!opt_provider(o))
  85. goto end;
  86. break;
  87. case OPT_CIPHER:
  88. ciphername = opt_unknown();
  89. break;
  90. case OPT_VERBOSE:
  91. verbose = 1;
  92. break;
  93. case OPT_QUIET:
  94. verbose = 0;
  95. break;
  96. }
  97. }
  98. /* One argument, the params file. */
  99. if (!opt_check_rest_arg("params file"))
  100. goto opthelp;
  101. argv = opt_rest();
  102. dsaparams = argv[0];
  103. if (!app_RAND_load())
  104. goto end;
  105. if (!opt_cipher(ciphername, &enc))
  106. goto end;
  107. private = 1;
  108. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  109. BIO_printf(bio_err, "Error getting password\n");
  110. goto end;
  111. }
  112. pkey = load_keyparams(dsaparams, FORMAT_UNDEF, 1, "DSA", "DSA parameters");
  113. out = bio_open_owner(outfile, FORMAT_PEM, private);
  114. if (out == NULL)
  115. goto end2;
  116. nbits = EVP_PKEY_get_bits(pkey);
  117. if (nbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  118. BIO_printf(bio_err,
  119. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  120. " Your key size is %d! Larger key size may behave not as expected.\n",
  121. OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_get_bits(pkey));
  122. ctx = EVP_PKEY_CTX_new_from_pkey(app_get0_libctx(), pkey, app_get0_propq());
  123. if (ctx == NULL) {
  124. BIO_printf(bio_err, "unable to create PKEY context\n");
  125. goto end;
  126. }
  127. EVP_PKEY_free(pkey);
  128. pkey = NULL;
  129. if (EVP_PKEY_keygen_init(ctx) <= 0) {
  130. BIO_printf(bio_err, "unable to set up for key generation\n");
  131. goto end;
  132. }
  133. pkey = app_keygen(ctx, "DSA", nbits, verbose);
  134. if (pkey == NULL)
  135. goto end;
  136. assert(private);
  137. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
  138. BIO_printf(bio_err, "unable to output generated key\n");
  139. goto end;
  140. }
  141. ret = 0;
  142. end:
  143. if (ret != 0)
  144. ERR_print_errors(bio_err);
  145. end2:
  146. BIO_free(in);
  147. BIO_free_all(out);
  148. EVP_PKEY_free(pkey);
  149. EVP_PKEY_CTX_free(ctx);
  150. EVP_CIPHER_free(enc);
  151. release_engine(e);
  152. OPENSSL_free(passout);
  153. return ret;
  154. }