dsa.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * Copyright 1995-2021 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_COMMON,
  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. 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_UNDEF, 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, *ciphername = 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. ciphername = opt_unknown();
  145. break;
  146. case OPT_PROV_CASES:
  147. if (!opt_provider(o))
  148. goto end;
  149. break;
  150. }
  151. }
  152. /* No extra args. */
  153. argc = opt_num_rest();
  154. if (argc != 0)
  155. goto opthelp;
  156. if (ciphername != NULL) {
  157. if (!opt_cipher(ciphername, &enc))
  158. goto end;
  159. }
  160. private = pubin || pubout ? 0 : 1;
  161. if (text && !pubin)
  162. private = 1;
  163. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  164. BIO_printf(bio_err, "Error getting passwords\n");
  165. goto end;
  166. }
  167. BIO_printf(bio_err, "read DSA key\n");
  168. if (pubin)
  169. pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
  170. else
  171. pkey = load_key(infile, informat, 1, passin, e, "private key");
  172. if (pkey == NULL) {
  173. BIO_printf(bio_err, "unable to load Key\n");
  174. ERR_print_errors(bio_err);
  175. goto end;
  176. }
  177. if (!EVP_PKEY_is_a(pkey, "DSA")) {
  178. BIO_printf(bio_err, "Not a DSA key\n");
  179. goto end;
  180. }
  181. out = bio_open_owner(outfile, outformat, private);
  182. if (out == NULL)
  183. goto end;
  184. if (text) {
  185. assert(pubin || private);
  186. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  187. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  188. perror(outfile);
  189. ERR_print_errors(bio_err);
  190. goto end;
  191. }
  192. }
  193. if (modulus) {
  194. BIGNUM *pub_key = NULL;
  195. if (!EVP_PKEY_get_bn_param(pkey, "pub", &pub_key)) {
  196. ERR_print_errors(bio_err);
  197. goto end;
  198. }
  199. BIO_printf(out, "Public Key=");
  200. BN_print(out, pub_key);
  201. BIO_printf(out, "\n");
  202. BN_free(pub_key);
  203. }
  204. if (noout) {
  205. ret = 0;
  206. goto end;
  207. }
  208. BIO_printf(bio_err, "writing DSA key\n");
  209. if (outformat == FORMAT_ASN1) {
  210. output_type = "DER";
  211. } else if (outformat == FORMAT_PEM) {
  212. output_type = "PEM";
  213. } else if (outformat == FORMAT_MSBLOB) {
  214. output_type = "MSBLOB";
  215. } else if (outformat == FORMAT_PVK) {
  216. if (pubin) {
  217. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  218. goto end;
  219. }
  220. output_type = "PVK";
  221. } else {
  222. BIO_printf(bio_err, "bad output format specified for outfile\n");
  223. goto end;
  224. }
  225. if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
  226. if (pubout || pubin)
  227. output_structure = "SubjectPublicKeyInfo";
  228. else
  229. output_structure = "type-specific";
  230. }
  231. /* Select what you want in the output */
  232. if (pubout || pubin) {
  233. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  234. } else {
  235. assert(private);
  236. selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
  237. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  238. }
  239. /* Perform the encoding */
  240. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, output_type,
  241. output_structure, NULL);
  242. if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
  243. BIO_printf(bio_err, "%s format not supported\n", output_type);
  244. goto end;
  245. }
  246. /* Passphrase setup */
  247. if (enc != NULL)
  248. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  249. /* Default passphrase prompter */
  250. if (enc != NULL || outformat == FORMAT_PVK) {
  251. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  252. if (passout != NULL)
  253. /* When passout given, override the passphrase prompter */
  254. OSSL_ENCODER_CTX_set_passphrase(ectx,
  255. (const unsigned char *)passout,
  256. strlen(passout));
  257. }
  258. /* PVK requires a bit more */
  259. if (outformat == FORMAT_PVK) {
  260. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  261. params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
  262. if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
  263. BIO_printf(bio_err, "invalid PVK encryption level\n");
  264. goto end;
  265. }
  266. }
  267. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  268. BIO_printf(bio_err, "unable to write key\n");
  269. goto end;
  270. }
  271. ret = 0;
  272. end:
  273. if (ret != 0)
  274. ERR_print_errors(bio_err);
  275. OSSL_ENCODER_CTX_free(ectx);
  276. BIO_free_all(out);
  277. EVP_PKEY_free(pkey);
  278. EVP_CIPHER_free(enc);
  279. release_engine(e);
  280. OPENSSL_free(passin);
  281. OPENSSL_free(passout);
  282. return ret;
  283. }