dgst.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright 1995-2018 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 <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pem.h>
  20. #include <openssl/hmac.h>
  21. #undef BUFSIZE
  22. #define BUFSIZE 1024*8
  23. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  24. EVP_PKEY *key, unsigned char *sigin, int siglen,
  25. const char *sig_name, const char *md_name,
  26. const char *file);
  27. typedef enum OPTION_choice {
  28. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  29. OPT_C, OPT_R, OPT_OUT, OPT_SIGN, OPT_PASSIN, OPT_VERIFY,
  30. OPT_PRVERIFY, OPT_SIGNATURE, OPT_KEYFORM, OPT_ENGINE, OPT_ENGINE_IMPL,
  31. OPT_HEX, OPT_BINARY, OPT_DEBUG, OPT_FIPS_FINGERPRINT,
  32. OPT_HMAC, OPT_MAC, OPT_SIGOPT, OPT_MACOPT,
  33. OPT_DIGEST,
  34. OPT_R_ENUM
  35. } OPTION_CHOICE;
  36. const OPTIONS dgst_options[] = {
  37. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [file...]\n"},
  38. {OPT_HELP_STR, 1, '-',
  39. " file... files to digest (default is stdin)\n"},
  40. {"help", OPT_HELP, '-', "Display this summary"},
  41. {"c", OPT_C, '-', "Print the digest with separating colons"},
  42. {"r", OPT_R, '-', "Print the digest in coreutils format"},
  43. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  44. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  45. {"sign", OPT_SIGN, 's', "Sign digest using private key"},
  46. {"verify", OPT_VERIFY, 's',
  47. "Verify a signature using public key"},
  48. {"prverify", OPT_PRVERIFY, 's',
  49. "Verify a signature using private key"},
  50. {"signature", OPT_SIGNATURE, '<', "File with signature to verify"},
  51. {"keyform", OPT_KEYFORM, 'f', "Key file format (PEM or ENGINE)"},
  52. {"hex", OPT_HEX, '-', "Print as hex dump"},
  53. {"binary", OPT_BINARY, '-', "Print in binary form"},
  54. {"d", OPT_DEBUG, '-', "Print debug info"},
  55. {"debug", OPT_DEBUG, '-', "Print debug info"},
  56. {"fips-fingerprint", OPT_FIPS_FINGERPRINT, '-',
  57. "Compute HMAC with the key used in OpenSSL-FIPS fingerprint"},
  58. {"hmac", OPT_HMAC, 's', "Create hashed MAC with key"},
  59. {"mac", OPT_MAC, 's', "Create MAC (not necessarily HMAC)"},
  60. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  61. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form or key"},
  62. {"", OPT_DIGEST, '-', "Any supported digest"},
  63. OPT_R_OPTIONS,
  64. #ifndef OPENSSL_NO_ENGINE
  65. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  66. {"engine_impl", OPT_ENGINE_IMPL, '-',
  67. "Also use engine given by -engine for digest operations"},
  68. #endif
  69. {NULL}
  70. };
  71. int dgst_main(int argc, char **argv)
  72. {
  73. BIO *in = NULL, *inp, *bmd = NULL, *out = NULL;
  74. ENGINE *e = NULL, *impl = NULL;
  75. EVP_PKEY *sigkey = NULL;
  76. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
  77. char *hmac_key = NULL;
  78. char *mac_name = NULL;
  79. char *passinarg = NULL, *passin = NULL;
  80. const EVP_MD *md = NULL, *m;
  81. const char *outfile = NULL, *keyfile = NULL, *prog = NULL;
  82. const char *sigfile = NULL;
  83. OPTION_CHOICE o;
  84. int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0;
  85. int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0;
  86. unsigned char *buf = NULL, *sigbuf = NULL;
  87. int engine_impl = 0;
  88. prog = opt_progname(argv[0]);
  89. buf = app_malloc(BUFSIZE, "I/O buffer");
  90. md = EVP_get_digestbyname(prog);
  91. prog = opt_init(argc, argv, dgst_options);
  92. while ((o = opt_next()) != OPT_EOF) {
  93. switch (o) {
  94. case OPT_EOF:
  95. case OPT_ERR:
  96. opthelp:
  97. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  98. goto end;
  99. case OPT_HELP:
  100. opt_help(dgst_options);
  101. ret = 0;
  102. goto end;
  103. case OPT_C:
  104. separator = 1;
  105. break;
  106. case OPT_R:
  107. separator = 2;
  108. break;
  109. case OPT_R_CASES:
  110. if (!opt_rand(o))
  111. goto end;
  112. break;
  113. case OPT_OUT:
  114. outfile = opt_arg();
  115. break;
  116. case OPT_SIGN:
  117. keyfile = opt_arg();
  118. break;
  119. case OPT_PASSIN:
  120. passinarg = opt_arg();
  121. break;
  122. case OPT_VERIFY:
  123. keyfile = opt_arg();
  124. want_pub = do_verify = 1;
  125. break;
  126. case OPT_PRVERIFY:
  127. keyfile = opt_arg();
  128. do_verify = 1;
  129. break;
  130. case OPT_SIGNATURE:
  131. sigfile = opt_arg();
  132. break;
  133. case OPT_KEYFORM:
  134. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  135. goto opthelp;
  136. break;
  137. case OPT_ENGINE:
  138. e = setup_engine(opt_arg(), 0);
  139. break;
  140. case OPT_ENGINE_IMPL:
  141. engine_impl = 1;
  142. break;
  143. case OPT_HEX:
  144. out_bin = 0;
  145. break;
  146. case OPT_BINARY:
  147. out_bin = 1;
  148. break;
  149. case OPT_DEBUG:
  150. debug = 1;
  151. break;
  152. case OPT_FIPS_FINGERPRINT:
  153. hmac_key = "etaonrishdlcupfm";
  154. break;
  155. case OPT_HMAC:
  156. hmac_key = opt_arg();
  157. break;
  158. case OPT_MAC:
  159. mac_name = opt_arg();
  160. break;
  161. case OPT_SIGOPT:
  162. if (!sigopts)
  163. sigopts = sk_OPENSSL_STRING_new_null();
  164. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  165. goto opthelp;
  166. break;
  167. case OPT_MACOPT:
  168. if (!macopts)
  169. macopts = sk_OPENSSL_STRING_new_null();
  170. if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg()))
  171. goto opthelp;
  172. break;
  173. case OPT_DIGEST:
  174. if (!opt_md(opt_unknown(), &m))
  175. goto opthelp;
  176. md = m;
  177. break;
  178. }
  179. }
  180. argc = opt_num_rest();
  181. argv = opt_rest();
  182. if (keyfile != NULL && argc > 1) {
  183. BIO_printf(bio_err, "%s: Can only sign or verify one file.\n", prog);
  184. goto end;
  185. }
  186. if (do_verify && sigfile == NULL) {
  187. BIO_printf(bio_err,
  188. "No signature to verify: use the -signature option\n");
  189. goto end;
  190. }
  191. if (engine_impl)
  192. impl = e;
  193. in = BIO_new(BIO_s_file());
  194. bmd = BIO_new(BIO_f_md());
  195. if ((in == NULL) || (bmd == NULL)) {
  196. ERR_print_errors(bio_err);
  197. goto end;
  198. }
  199. if (debug) {
  200. BIO_set_callback(in, BIO_debug_callback);
  201. /* needed for windows 3.1 */
  202. BIO_set_callback_arg(in, (char *)bio_err);
  203. }
  204. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  205. BIO_printf(bio_err, "Error getting password\n");
  206. goto end;
  207. }
  208. if (out_bin == -1) {
  209. if (keyfile != NULL)
  210. out_bin = 1;
  211. else
  212. out_bin = 0;
  213. }
  214. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  215. if (out == NULL)
  216. goto end;
  217. if ((!(mac_name == NULL) + !(keyfile == NULL) + !(hmac_key == NULL)) > 1) {
  218. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  219. goto end;
  220. }
  221. if (keyfile != NULL) {
  222. if (want_pub)
  223. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "key file");
  224. else
  225. sigkey = load_key(keyfile, keyform, 0, passin, e, "key file");
  226. if (sigkey == NULL) {
  227. /*
  228. * load_[pub]key() has already printed an appropriate message
  229. */
  230. goto end;
  231. }
  232. }
  233. if (mac_name != NULL) {
  234. EVP_PKEY_CTX *mac_ctx = NULL;
  235. int r = 0;
  236. if (!init_gen_str(&mac_ctx, mac_name, impl, 0))
  237. goto mac_end;
  238. if (macopts != NULL) {
  239. char *macopt;
  240. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  241. macopt = sk_OPENSSL_STRING_value(macopts, i);
  242. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  243. BIO_printf(bio_err,
  244. "MAC parameter error \"%s\"\n", macopt);
  245. ERR_print_errors(bio_err);
  246. goto mac_end;
  247. }
  248. }
  249. }
  250. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
  251. BIO_puts(bio_err, "Error generating key\n");
  252. ERR_print_errors(bio_err);
  253. goto mac_end;
  254. }
  255. r = 1;
  256. mac_end:
  257. EVP_PKEY_CTX_free(mac_ctx);
  258. if (r == 0)
  259. goto end;
  260. }
  261. if (hmac_key != NULL) {
  262. sigkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, impl,
  263. (unsigned char *)hmac_key, -1);
  264. if (sigkey == NULL)
  265. goto end;
  266. }
  267. if (sigkey != NULL) {
  268. EVP_MD_CTX *mctx = NULL;
  269. EVP_PKEY_CTX *pctx = NULL;
  270. int r;
  271. if (!BIO_get_md_ctx(bmd, &mctx)) {
  272. BIO_printf(bio_err, "Error getting context\n");
  273. ERR_print_errors(bio_err);
  274. goto end;
  275. }
  276. if (do_verify)
  277. r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  278. else
  279. r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  280. if (!r) {
  281. BIO_printf(bio_err, "Error setting context\n");
  282. ERR_print_errors(bio_err);
  283. goto end;
  284. }
  285. if (sigopts != NULL) {
  286. char *sigopt;
  287. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  288. sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  289. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  290. BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
  291. ERR_print_errors(bio_err);
  292. goto end;
  293. }
  294. }
  295. }
  296. }
  297. /* we use md as a filter, reading from 'in' */
  298. else {
  299. EVP_MD_CTX *mctx = NULL;
  300. if (!BIO_get_md_ctx(bmd, &mctx)) {
  301. BIO_printf(bio_err, "Error getting context\n");
  302. ERR_print_errors(bio_err);
  303. goto end;
  304. }
  305. if (md == NULL)
  306. md = EVP_sha256();
  307. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  308. BIO_printf(bio_err, "Error setting digest\n");
  309. ERR_print_errors(bio_err);
  310. goto end;
  311. }
  312. }
  313. if (sigfile != NULL && sigkey != NULL) {
  314. BIO *sigbio = BIO_new_file(sigfile, "rb");
  315. if (sigbio == NULL) {
  316. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  317. ERR_print_errors(bio_err);
  318. goto end;
  319. }
  320. siglen = EVP_PKEY_size(sigkey);
  321. sigbuf = app_malloc(siglen, "signature buffer");
  322. siglen = BIO_read(sigbio, sigbuf, siglen);
  323. BIO_free(sigbio);
  324. if (siglen <= 0) {
  325. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  326. ERR_print_errors(bio_err);
  327. goto end;
  328. }
  329. }
  330. inp = BIO_push(bmd, in);
  331. if (md == NULL) {
  332. EVP_MD_CTX *tctx;
  333. BIO_get_md_ctx(bmd, &tctx);
  334. md = EVP_MD_CTX_md(tctx);
  335. }
  336. if (argc == 0) {
  337. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  338. ret = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  339. siglen, NULL, NULL, "stdin");
  340. } else {
  341. const char *md_name = NULL, *sig_name = NULL;
  342. if (!out_bin) {
  343. if (sigkey != NULL) {
  344. const EVP_PKEY_ASN1_METHOD *ameth;
  345. ameth = EVP_PKEY_get0_asn1(sigkey);
  346. if (ameth)
  347. EVP_PKEY_asn1_get0_info(NULL, NULL,
  348. NULL, NULL, &sig_name, ameth);
  349. }
  350. if (md != NULL)
  351. md_name = EVP_MD_name(md);
  352. }
  353. ret = 0;
  354. for (i = 0; i < argc; i++) {
  355. int r;
  356. if (BIO_read_filename(in, argv[i]) <= 0) {
  357. perror(argv[i]);
  358. ret++;
  359. continue;
  360. } else {
  361. r = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  362. siglen, sig_name, md_name, argv[i]);
  363. }
  364. if (r)
  365. ret = r;
  366. (void)BIO_reset(bmd);
  367. }
  368. }
  369. end:
  370. OPENSSL_clear_free(buf, BUFSIZE);
  371. BIO_free(in);
  372. OPENSSL_free(passin);
  373. BIO_free_all(out);
  374. EVP_PKEY_free(sigkey);
  375. sk_OPENSSL_STRING_free(sigopts);
  376. sk_OPENSSL_STRING_free(macopts);
  377. OPENSSL_free(sigbuf);
  378. BIO_free(bmd);
  379. release_engine(e);
  380. return ret;
  381. }
  382. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  383. EVP_PKEY *key, unsigned char *sigin, int siglen,
  384. const char *sig_name, const char *md_name,
  385. const char *file)
  386. {
  387. size_t len;
  388. int i;
  389. for (;;) {
  390. i = BIO_read(bp, (char *)buf, BUFSIZE);
  391. if (i < 0) {
  392. BIO_printf(bio_err, "Read Error in %s\n", file);
  393. ERR_print_errors(bio_err);
  394. return 1;
  395. }
  396. if (i == 0)
  397. break;
  398. }
  399. if (sigin != NULL) {
  400. EVP_MD_CTX *ctx;
  401. BIO_get_md_ctx(bp, &ctx);
  402. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  403. if (i > 0) {
  404. BIO_printf(out, "Verified OK\n");
  405. } else if (i == 0) {
  406. BIO_printf(out, "Verification Failure\n");
  407. return 1;
  408. } else {
  409. BIO_printf(bio_err, "Error Verifying Data\n");
  410. ERR_print_errors(bio_err);
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. if (key != NULL) {
  416. EVP_MD_CTX *ctx;
  417. BIO_get_md_ctx(bp, &ctx);
  418. len = BUFSIZE;
  419. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  420. BIO_printf(bio_err, "Error Signing Data\n");
  421. ERR_print_errors(bio_err);
  422. return 1;
  423. }
  424. } else {
  425. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  426. if ((int)len < 0) {
  427. ERR_print_errors(bio_err);
  428. return 1;
  429. }
  430. }
  431. if (binout) {
  432. BIO_write(out, buf, len);
  433. } else if (sep == 2) {
  434. for (i = 0; i < (int)len; i++)
  435. BIO_printf(out, "%02x", buf[i]);
  436. BIO_printf(out, " *%s\n", file);
  437. } else {
  438. if (sig_name != NULL) {
  439. BIO_puts(out, sig_name);
  440. if (md_name != NULL)
  441. BIO_printf(out, "-%s", md_name);
  442. BIO_printf(out, "(%s)= ", file);
  443. } else if (md_name != NULL) {
  444. BIO_printf(out, "%s(%s)= ", md_name, file);
  445. } else {
  446. BIO_printf(out, "(%s)= ", file);
  447. }
  448. for (i = 0; i < (int)len; i++) {
  449. if (sep && (i != 0))
  450. BIO_printf(out, ":");
  451. BIO_printf(out, "%02x", buf[i]);
  452. }
  453. BIO_printf(out, "\n");
  454. }
  455. return 0;
  456. }