dsa.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include "apps.h"
  15. #include "progs.h"
  16. #include <openssl/bio.h>
  17. #include <openssl/err.h>
  18. #include <openssl/dsa.h>
  19. #include <openssl/evp.h>
  20. #include <openssl/x509.h>
  21. #include <openssl/pem.h>
  22. #include <openssl/bn.h>
  23. typedef enum OPTION_choice {
  24. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  25. OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT, OPT_ENGINE,
  26. /* Do not change the order here; see case statements below */
  27. OPT_PVK_NONE, OPT_PVK_WEAK, OPT_PVK_STRONG,
  28. OPT_NOOUT, OPT_TEXT, OPT_MODULUS, OPT_PUBIN,
  29. OPT_PUBOUT, OPT_CIPHER, OPT_PASSIN, OPT_PASSOUT,
  30. OPT_PROV_ENUM
  31. } OPTION_CHOICE;
  32. const OPTIONS dsa_options[] = {
  33. OPT_SECTION("General"),
  34. {"help", OPT_HELP, '-', "Display this summary"},
  35. {"", OPT_CIPHER, '-', "Any supported cipher"},
  36. #ifndef OPENSSL_NO_RC4
  37. {"pvk-strong", OPT_PVK_STRONG, '-', "Enable 'Strong' PVK encoding level (default)"},
  38. {"pvk-weak", OPT_PVK_WEAK, '-', "Enable 'Weak' PVK encoding level"},
  39. {"pvk-none", OPT_PVK_NONE, '-', "Don't enforce PVK encoding"},
  40. #endif
  41. #ifndef OPENSSL_NO_ENGINE
  42. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  43. #endif
  44. OPT_SECTION("Input"),
  45. {"in", OPT_IN, 's', "Input key"},
  46. {"inform", OPT_INFORM, 'f', "Input format (DER/PEM/PVK); has no effect"},
  47. {"pubin", OPT_PUBIN, '-', "Expect a public key in input file"},
  48. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  49. OPT_SECTION("Output"),
  50. {"out", OPT_OUT, '>', "Output file"},
  51. {"outform", OPT_OUTFORM, 'f', "Output format, DER PEM PVK"},
  52. {"noout", OPT_NOOUT, '-', "Don't print key out"},
  53. {"text", OPT_TEXT, '-', "Print the key in text"},
  54. {"modulus", OPT_MODULUS, '-', "Print the DSA public value"},
  55. {"pubout", OPT_PUBOUT, '-', "Output public key, not private"},
  56. {"passout", OPT_PASSOUT, 's', "Output file pass phrase source"},
  57. OPT_PROV_OPTIONS,
  58. {NULL}
  59. };
  60. int dsa_main(int argc, char **argv)
  61. {
  62. BIO *out = NULL;
  63. DSA *dsa = NULL;
  64. ENGINE *e = NULL;
  65. EVP_PKEY *pkey = NULL;
  66. const EVP_CIPHER *enc = NULL;
  67. char *infile = NULL, *outfile = NULL, *prog;
  68. char *passin = NULL, *passout = NULL, *passinarg = NULL, *passoutarg = NULL;
  69. OPTION_CHOICE o;
  70. int informat = FORMAT_PEM, outformat = FORMAT_PEM, text = 0, noout = 0;
  71. int i, modulus = 0, pubin = 0, pubout = 0, ret = 1;
  72. #ifndef OPENSSL_NO_RC4
  73. int pvk_encr = 2;
  74. #endif
  75. int private = 0;
  76. prog = opt_init(argc, argv, dsa_options);
  77. while ((o = opt_next()) != OPT_EOF) {
  78. switch (o) {
  79. case OPT_EOF:
  80. case OPT_ERR:
  81. opthelp:
  82. ret = 0;
  83. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  84. goto end;
  85. case OPT_HELP:
  86. opt_help(dsa_options);
  87. ret = 0;
  88. goto end;
  89. case OPT_INFORM:
  90. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  91. goto opthelp;
  92. break;
  93. case OPT_IN:
  94. infile = opt_arg();
  95. break;
  96. case OPT_OUTFORM:
  97. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  98. goto opthelp;
  99. break;
  100. case OPT_OUT:
  101. outfile = opt_arg();
  102. break;
  103. case OPT_ENGINE:
  104. e = setup_engine(opt_arg(), 0);
  105. break;
  106. case OPT_PASSIN:
  107. passinarg = opt_arg();
  108. break;
  109. case OPT_PASSOUT:
  110. passoutarg = opt_arg();
  111. break;
  112. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  113. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  114. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  115. #ifndef OPENSSL_NO_RC4
  116. pvk_encr = (o - OPT_PVK_NONE);
  117. #endif
  118. break;
  119. case OPT_NOOUT:
  120. noout = 1;
  121. break;
  122. case OPT_TEXT:
  123. text = 1;
  124. break;
  125. case OPT_MODULUS:
  126. modulus = 1;
  127. break;
  128. case OPT_PUBIN:
  129. pubin = 1;
  130. break;
  131. case OPT_PUBOUT:
  132. pubout = 1;
  133. break;
  134. case OPT_CIPHER:
  135. if (!opt_cipher(opt_unknown(), &enc))
  136. goto end;
  137. break;
  138. case OPT_PROV_CASES:
  139. if (!opt_provider(o))
  140. goto end;
  141. break;
  142. }
  143. }
  144. argc = opt_num_rest();
  145. if (argc != 0)
  146. goto opthelp;
  147. private = pubin || pubout ? 0 : 1;
  148. if (text && !pubin)
  149. private = 1;
  150. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  151. BIO_printf(bio_err, "Error getting passwords\n");
  152. goto end;
  153. }
  154. BIO_printf(bio_err, "read DSA key\n");
  155. if (pubin)
  156. pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
  157. else
  158. pkey = load_key(infile, informat, 1, passin, e, "private key");
  159. if (pkey != NULL)
  160. dsa = EVP_PKEY_get1_DSA(pkey);
  161. if (dsa == NULL) {
  162. BIO_printf(bio_err, "unable to load Key\n");
  163. ERR_print_errors(bio_err);
  164. goto end;
  165. }
  166. out = bio_open_owner(outfile, outformat, private);
  167. if (out == NULL)
  168. goto end;
  169. if (text) {
  170. assert(pubin || private);
  171. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  172. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  173. perror(outfile);
  174. ERR_print_errors(bio_err);
  175. goto end;
  176. }
  177. }
  178. if (modulus) {
  179. const BIGNUM *pub_key = NULL;
  180. DSA_get0_key(dsa, &pub_key, NULL);
  181. BIO_printf(out, "Public Key=");
  182. BN_print(out, pub_key);
  183. BIO_printf(out, "\n");
  184. }
  185. if (noout) {
  186. ret = 0;
  187. goto end;
  188. }
  189. BIO_printf(bio_err, "writing DSA key\n");
  190. if (outformat == FORMAT_ASN1) {
  191. if (pubin || pubout) {
  192. i = i2d_DSA_PUBKEY_bio(out, dsa);
  193. } else {
  194. assert(private);
  195. i = i2d_DSAPrivateKey_bio(out, dsa);
  196. }
  197. } else if (outformat == FORMAT_PEM) {
  198. if (pubin || pubout) {
  199. i = PEM_write_bio_DSA_PUBKEY(out, dsa);
  200. } else {
  201. assert(private);
  202. i = PEM_write_bio_DSAPrivateKey(out, dsa, enc,
  203. NULL, 0, NULL, passout);
  204. }
  205. #ifndef OPENSSL_NO_RSA
  206. } else if (outformat == FORMAT_MSBLOB || outformat == FORMAT_PVK) {
  207. EVP_PKEY *pk;
  208. pk = EVP_PKEY_new();
  209. if (pk == NULL)
  210. goto end;
  211. EVP_PKEY_set1_DSA(pk, dsa);
  212. if (outformat == FORMAT_PVK) {
  213. if (pubin) {
  214. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  215. EVP_PKEY_free(pk);
  216. goto end;
  217. }
  218. assert(private);
  219. # ifdef OPENSSL_NO_RC4
  220. BIO_printf(bio_err, "PVK format not supported\n");
  221. EVP_PKEY_free(pk);
  222. goto end;
  223. # else
  224. i = i2b_PVK_bio(out, pk, pvk_encr, 0, passout);
  225. # endif
  226. } else if (pubin || pubout) {
  227. i = i2b_PublicKey_bio(out, pk);
  228. } else {
  229. assert(private);
  230. i = i2b_PrivateKey_bio(out, pk);
  231. }
  232. EVP_PKEY_free(pk);
  233. #endif
  234. } else {
  235. BIO_printf(bio_err, "bad output format specified for outfile\n");
  236. goto end;
  237. }
  238. if (i <= 0) {
  239. BIO_printf(bio_err, "unable to write private key\n");
  240. ERR_print_errors(bio_err);
  241. goto end;
  242. }
  243. ret = 0;
  244. end:
  245. BIO_free_all(out);
  246. EVP_PKEY_free(pkey);
  247. DSA_free(dsa);
  248. release_engine(e);
  249. OPENSSL_free(passin);
  250. OPENSSL_free(passout);
  251. return ret;
  252. }