dsa.c 7.5 KB

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