dsa.c 9.1 KB

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