dsa.c 8.5 KB

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