dsa.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright 1995-2022 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. opt_set_unknown_name("cipher");
  86. prog = opt_init(argc, argv, dsa_options);
  87. while ((o = opt_next()) != OPT_EOF) {
  88. switch (o) {
  89. case OPT_EOF:
  90. case OPT_ERR:
  91. opthelp:
  92. ret = 0;
  93. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  94. goto end;
  95. case OPT_HELP:
  96. opt_help(dsa_options);
  97. ret = 0;
  98. goto end;
  99. case OPT_INFORM:
  100. if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat))
  101. goto opthelp;
  102. break;
  103. case OPT_IN:
  104. infile = opt_arg();
  105. break;
  106. case OPT_OUTFORM:
  107. if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat))
  108. goto opthelp;
  109. break;
  110. case OPT_OUT:
  111. outfile = opt_arg();
  112. break;
  113. case OPT_ENGINE:
  114. e = setup_engine(opt_arg(), 0);
  115. break;
  116. case OPT_PASSIN:
  117. passinarg = opt_arg();
  118. break;
  119. case OPT_PASSOUT:
  120. passoutarg = opt_arg();
  121. break;
  122. case OPT_PVK_STRONG: /* pvk_encr:= 2 */
  123. case OPT_PVK_WEAK: /* pvk_encr:= 1 */
  124. case OPT_PVK_NONE: /* pvk_encr:= 0 */
  125. #ifndef OPENSSL_NO_RC4
  126. pvk_encr = (o - OPT_PVK_NONE);
  127. #endif
  128. break;
  129. case OPT_NOOUT:
  130. noout = 1;
  131. break;
  132. case OPT_TEXT:
  133. text = 1;
  134. break;
  135. case OPT_MODULUS:
  136. modulus = 1;
  137. break;
  138. case OPT_PUBIN:
  139. pubin = 1;
  140. break;
  141. case OPT_PUBOUT:
  142. pubout = 1;
  143. break;
  144. case OPT_CIPHER:
  145. ciphername = opt_unknown();
  146. break;
  147. case OPT_PROV_CASES:
  148. if (!opt_provider(o))
  149. goto end;
  150. break;
  151. }
  152. }
  153. /* No extra args. */
  154. if (!opt_check_rest_arg(NULL))
  155. goto opthelp;
  156. if (!opt_cipher(ciphername, &enc))
  157. goto end;
  158. private = pubin || pubout ? 0 : 1;
  159. if (text && !pubin)
  160. private = 1;
  161. if (!app_passwd(passinarg, passoutarg, &passin, &passout)) {
  162. BIO_printf(bio_err, "Error getting passwords\n");
  163. goto end;
  164. }
  165. BIO_printf(bio_err, "read DSA key\n");
  166. if (pubin)
  167. pkey = load_pubkey(infile, informat, 1, passin, e, "public key");
  168. else
  169. pkey = load_key(infile, informat, 1, passin, e, "private key");
  170. if (pkey == NULL) {
  171. BIO_printf(bio_err, "unable to load Key\n");
  172. ERR_print_errors(bio_err);
  173. goto end;
  174. }
  175. if (!EVP_PKEY_is_a(pkey, "DSA")) {
  176. BIO_printf(bio_err, "Not a DSA key\n");
  177. goto end;
  178. }
  179. out = bio_open_owner(outfile, outformat, private);
  180. if (out == NULL)
  181. goto end;
  182. if (text) {
  183. assert(pubin || private);
  184. if ((pubin && EVP_PKEY_print_public(out, pkey, 0, NULL) <= 0)
  185. || (!pubin && EVP_PKEY_print_private(out, pkey, 0, NULL) <= 0)) {
  186. perror(outfile);
  187. ERR_print_errors(bio_err);
  188. goto end;
  189. }
  190. }
  191. if (modulus) {
  192. BIGNUM *pub_key = NULL;
  193. if (!EVP_PKEY_get_bn_param(pkey, "pub", &pub_key)) {
  194. ERR_print_errors(bio_err);
  195. goto end;
  196. }
  197. BIO_printf(out, "Public Key=");
  198. BN_print(out, pub_key);
  199. BIO_printf(out, "\n");
  200. BN_free(pub_key);
  201. }
  202. if (noout) {
  203. ret = 0;
  204. goto end;
  205. }
  206. BIO_printf(bio_err, "writing DSA key\n");
  207. if (outformat == FORMAT_ASN1) {
  208. output_type = "DER";
  209. } else if (outformat == FORMAT_PEM) {
  210. output_type = "PEM";
  211. } else if (outformat == FORMAT_MSBLOB) {
  212. output_type = "MSBLOB";
  213. } else if (outformat == FORMAT_PVK) {
  214. if (pubin) {
  215. BIO_printf(bio_err, "PVK form impossible with public key input\n");
  216. goto end;
  217. }
  218. output_type = "PVK";
  219. } else {
  220. BIO_printf(bio_err, "bad output format specified for outfile\n");
  221. goto end;
  222. }
  223. if (outformat == FORMAT_ASN1 || outformat == FORMAT_PEM) {
  224. if (pubout || pubin)
  225. output_structure = "SubjectPublicKeyInfo";
  226. else
  227. output_structure = "type-specific";
  228. }
  229. /* Select what you want in the output */
  230. if (pubout || pubin) {
  231. selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  232. } else {
  233. assert(private);
  234. selection = (OSSL_KEYMGMT_SELECT_KEYPAIR
  235. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
  236. }
  237. /* Perform the encoding */
  238. ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, output_type,
  239. output_structure, NULL);
  240. if (OSSL_ENCODER_CTX_get_num_encoders(ectx) == 0) {
  241. BIO_printf(bio_err, "%s format not supported\n", output_type);
  242. goto end;
  243. }
  244. /* Passphrase setup */
  245. if (enc != NULL)
  246. OSSL_ENCODER_CTX_set_cipher(ectx, EVP_CIPHER_get0_name(enc), NULL);
  247. /* Default passphrase prompter */
  248. if (enc != NULL || outformat == FORMAT_PVK) {
  249. OSSL_ENCODER_CTX_set_passphrase_ui(ectx, get_ui_method(), NULL);
  250. if (passout != NULL)
  251. /* When passout given, override the passphrase prompter */
  252. OSSL_ENCODER_CTX_set_passphrase(ectx,
  253. (const unsigned char *)passout,
  254. strlen(passout));
  255. }
  256. /* PVK requires a bit more */
  257. if (outformat == FORMAT_PVK) {
  258. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  259. params[0] = OSSL_PARAM_construct_int("encrypt-level", &pvk_encr);
  260. if (!OSSL_ENCODER_CTX_set_params(ectx, params)) {
  261. BIO_printf(bio_err, "invalid PVK encryption level\n");
  262. goto end;
  263. }
  264. }
  265. if (!OSSL_ENCODER_to_bio(ectx, out)) {
  266. BIO_printf(bio_err, "unable to write key\n");
  267. goto end;
  268. }
  269. ret = 0;
  270. end:
  271. if (ret != 0)
  272. ERR_print_errors(bio_err);
  273. OSSL_ENCODER_CTX_free(ectx);
  274. BIO_free_all(out);
  275. EVP_PKEY_free(pkey);
  276. EVP_CIPHER_free(enc);
  277. release_engine(e);
  278. OPENSSL_free(passin);
  279. OPENSSL_free(passout);
  280. return ret;
  281. }