gendsa.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 1995-2020 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_ERR = -1, OPT_EOF = 0, OPT_HELP,
  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. DSA *dsa = NULL;
  50. EVP_PKEY *pkey = NULL;
  51. EVP_PKEY_CTX *ctx = NULL;
  52. const EVP_CIPHER *enc = NULL;
  53. char *dsaparams = NULL;
  54. char *outfile = NULL, *passoutarg = NULL, *passout = NULL, *prog;
  55. OPTION_CHOICE o;
  56. int ret = 1, private = 0, verbose = 0;
  57. const BIGNUM *p = NULL;
  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. if (!opt_cipher(opt_unknown(), &enc))
  89. goto end;
  90. break;
  91. case OPT_VERBOSE:
  92. verbose = 1;
  93. break;
  94. }
  95. }
  96. argc = opt_num_rest();
  97. argv = opt_rest();
  98. private = 1;
  99. if (argc != 1)
  100. goto opthelp;
  101. dsaparams = *argv;
  102. if (!app_passwd(NULL, passoutarg, NULL, &passout)) {
  103. BIO_printf(bio_err, "Error getting password\n");
  104. goto end;
  105. }
  106. in = bio_open_default(dsaparams, 'r', FORMAT_PEM);
  107. if (in == NULL)
  108. goto end2;
  109. if ((dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL)) == NULL) {
  110. BIO_printf(bio_err, "unable to load DSA parameter file\n");
  111. goto end;
  112. }
  113. BIO_free(in);
  114. in = NULL;
  115. out = bio_open_owner(outfile, FORMAT_PEM, private);
  116. if (out == NULL)
  117. goto end2;
  118. DSA_get0_pqg(dsa, &p, NULL, NULL);
  119. if (BN_num_bits(p) > OPENSSL_DSA_MAX_MODULUS_BITS)
  120. BIO_printf(bio_err,
  121. "Warning: It is not recommended to use more than %d bit for DSA keys.\n"
  122. " Your key size is %d! Larger key size may behave not as expected.\n",
  123. OPENSSL_DSA_MAX_MODULUS_BITS, BN_num_bits(p));
  124. pkey = EVP_PKEY_new();
  125. if (pkey == NULL) {
  126. BIO_printf(bio_err, "unable to allocate PKEY\n");
  127. goto end;
  128. }
  129. if (!EVP_PKEY_set1_DSA(pkey, dsa)) {
  130. BIO_printf(bio_err, "unable to associate DSA parameters with PKEY\n");
  131. goto end;
  132. }
  133. ctx = EVP_PKEY_CTX_new(pkey, NULL);
  134. if (ctx == NULL) {
  135. BIO_printf(bio_err, "unable to create PKEY context\n");
  136. goto end;
  137. }
  138. EVP_PKEY_free(pkey);
  139. pkey = NULL;
  140. if (EVP_PKEY_keygen_init(ctx) <= 0) {
  141. BIO_printf(bio_err, "unable to set up for key generation\n");
  142. goto end;
  143. }
  144. if (verbose)
  145. BIO_printf(bio_err, "Generating DSA key, %d bits\n", BN_num_bits(p));
  146. if (EVP_PKEY_keygen(ctx, &pkey) <= 0) {
  147. BIO_printf(bio_err, "unable to generate key\n");
  148. goto end;
  149. }
  150. assert(private);
  151. if (!PEM_write_bio_PrivateKey(out, pkey, enc, NULL, 0, NULL, passout)) {
  152. BIO_printf(bio_err, "unable to output generated key\n");
  153. goto end;
  154. }
  155. ret = 0;
  156. end:
  157. if (ret != 0)
  158. ERR_print_errors(bio_err);
  159. end2:
  160. BIO_free(in);
  161. BIO_free_all(out);
  162. DSA_free(dsa);
  163. EVP_PKEY_free(pkey);
  164. EVP_PKEY_CTX_free(ctx);
  165. release_engine(e);
  166. OPENSSL_free(passout);
  167. return ret;
  168. }