dsa.c 7.6 KB

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