gendsa.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <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,
  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. OPT_PARAMETERS(),
  42. {"dsaparam-file", 0, 0, "File containing DSA parameters"},
  43. {NULL}
  44. };
  45. int gendsa_main(int argc, char **argv)
  46. {
  47. ENGINE *e = NULL;
  48. BIO *out = NULL, *in = NULL;
  49. EVP_PKEY *pkey = NULL;
  50. EVP_PKEY_CTX *ctx = NULL;
  51. EVP_CIPHER *enc = NULL;
  52. char *dsaparams = NULL, *ciphername = NULL;
  53. char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
  54. OPTION_CHOICE o;
  55. int ret = 1, private = 0, verbose = 0, nbits;
  56. prog = opt_init(argc, argv, gendsa_options);
  57. while ((o = opt_next()) != OPT_EOF) {
  58. switch (o) {
  59. case OPT_EOF:
  60. case OPT_ERR:
  61. opthelp:
  62. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  63. goto end;
  64. case OPT_HELP:
  65. ret = 0;
  66. opt_help(gendsa_options);
  67. goto end;
  68. case OPT_OUT:
  69. outfile = opt_arg();
  70. break;
  71. case OPT_PASSOUT:
  72. passoutarg = opt_arg();
  73. break;
  74. case OPT_ENGINE:
  75. e = setup_engine(opt_arg(), 0);
  76. break;
  77. case OPT_R_CASES:
  78. if (!opt_rand(o))
  79. goto end;
  80. break;
  81. case OPT_PROV_CASES:
  82. if (!opt_provider(o))
  83. goto end;
  84. break;
  85. case OPT_CIPHER:
  86. ciphername = opt_unknown();
  87. break;
  88. case OPT_VERBOSE:
  89. verbose = 1;
  90. break;
  91. }
  92. }
  93. /* One argument, the params file. */
  94. argc = opt_num_rest();
  95. argv = opt_rest();
  96. if (argc != 1)
  97. goto opthelp;
  98. dsaparams = argv[0];
  99. if (!app_RAND_load())
  100. goto end;
  101. if (ciphername != NULL) {
  102. if (!opt_cipher(ciphername, &enc))
  103. goto end;
  104. }
  105. private = 1;
  106. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  107. BIO_printf(bio_err, "Error getting password\n");
  108. goto end;
  109. }
  110. pkey = load_keyparams(dsaparams, FORMAT_UNDEF, 1, "DSA", "DSA parameters");
  111. out = bio_open_owner(outfile, FORMAT_PEM, private);
  112. if (out == NULL)
  113. goto end2;
  114. nbits = EVP_PKEY_get_bits(pkey);
  115. if (nbits > OPENSSL_DSA_MAX_MODULUS_BITS)
  116. BIO_printf(bio_err,
  117. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  118. " Your key size is %d! Larger key size may behave not as expected.\n",
  119. OPENSSL_DSA_MAX_MODULUS_BITS, EVP_PKEY_get_bits(pkey));
  120. ctx = EVP_PKEY_CTX_new(pkey, NULL);
  121. if (ctx == NULL) {
  122. BIO_printf(bio_err, "unable to create PKEY context\n");
  123. goto end;
  124. }
  125. EVP_PKEY_free(pkey);
  126. pkey = NULL;
  127. if (EVP_PKEY_keygen_init(ctx) <= 0) {
  128. BIO_printf(bio_err, "unable to set up for key generation\n");
  129. goto end;
  130. }
  131. pkey = app_keygen(ctx, "DSA", nbits, verbose);
  132. assert(private);
  133. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
  134. BIO_printf(bio_err, "unable to output generated key\n");
  135. goto end;
  136. }
  137. ret = 0;
  138. end:
  139. if (ret != 0)
  140. ERR_print_errors(bio_err);
  141. end2:
  142. BIO_free(in);
  143. BIO_free_all(out);
  144. EVP_PKEY_free(pkey);
  145. EVP_PKEY_CTX_free(ctx);
  146. EVP_CIPHER_free(enc);
  147. release_engine(e);
  148. OPENSSL_free(passout);
  149. return ret;
  150. }