dgst.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <stdlib.h>
  60. #include "apps.h"
  61. #include <openssl/bio.h>
  62. #include <openssl/err.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/objects.h>
  65. #include <openssl/x509.h>
  66. #include <openssl/pem.h>
  67. #include <openssl/hmac.h>
  68. #undef BUFSIZE
  69. #define BUFSIZE 1024*8
  70. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  71. EVP_PKEY *key, unsigned char *sigin, int siglen,
  72. const char *sig_name, const char *md_name,
  73. const char *file, BIO *bmd);
  74. typedef enum OPTION_choice {
  75. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  76. OPT_C, OPT_R, OPT_RAND, OPT_OUT, OPT_SIGN, OPT_PASSIN, OPT_VERIFY,
  77. OPT_PRVERIFY, OPT_SIGNATURE, OPT_KEYFORM, OPT_ENGINE, OPT_ENGINE_IMPL,
  78. OPT_HEX, OPT_BINARY, OPT_DEBUG, OPT_FIPS_FINGERPRINT,
  79. OPT_HMAC, OPT_MAC, OPT_SIGOPT, OPT_MACOPT,
  80. OPT_DIGEST
  81. } OPTION_CHOICE;
  82. OPTIONS dgst_options[] = {
  83. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [file...]\n"},
  84. {OPT_HELP_STR, 1, '-',
  85. " file... files to digest (default is stdin)\n"},
  86. {"help", OPT_HELP, '-', "Display this summary"},
  87. {"c", OPT_C, '-', "Print the digest with separating colons"},
  88. {"r", OPT_R, '-', "Print the digest in coreutils format"},
  89. {"rand", OPT_RAND, 's',
  90. "Use file(s) containing random data to seed RNG or an EGD sock"},
  91. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  92. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  93. {"sign", OPT_SIGN, 's', "Sign digest using private key"},
  94. {"verify", OPT_VERIFY, 's',
  95. "Verify a signature using public key"},
  96. {"prverify", OPT_PRVERIFY, 's',
  97. "Verify a signature using private key"},
  98. {"signature", OPT_SIGNATURE, '<', "File with signature to verify"},
  99. {"keyform", OPT_KEYFORM, 'f', "Key file format (PEM or ENGINE)"},
  100. {"hex", OPT_HEX, '-', "Print as hex dump"},
  101. {"binary", OPT_BINARY, '-', "Print in binary form"},
  102. {"d", OPT_DEBUG, '-', "Print debug info"},
  103. {"debug", OPT_DEBUG, '-', "Print debug info"},
  104. {"fips-fingerprint", OPT_FIPS_FINGERPRINT, '-',
  105. "Compute HMAC with the key used in OpenSSL-FIPS fingerprint"},
  106. {"hmac", OPT_HMAC, 's', "Create hashed MAC with key"},
  107. {"mac", OPT_MAC, 's', "Create MAC (not necessarily HMAC)"},
  108. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  109. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form or key"},
  110. {"", OPT_DIGEST, '-', "Any supported digest"},
  111. #ifndef OPENSSL_NO_ENGINE
  112. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  113. {"engine_impl", OPT_ENGINE_IMPL, '-',
  114. "Also use engine given by -engine for digest operations"},
  115. #endif
  116. {NULL}
  117. };
  118. int dgst_main(int argc, char **argv)
  119. {
  120. BIO *in = NULL, *inp, *bmd = NULL, *out = NULL;
  121. ENGINE *e = NULL, *impl = NULL;
  122. EVP_PKEY *sigkey = NULL;
  123. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
  124. char *hmac_key = NULL;
  125. char *mac_name = NULL;
  126. char *passinarg = NULL, *passin = NULL;
  127. const EVP_MD *md = NULL, *m;
  128. const char *outfile = NULL, *keyfile = NULL, *prog = NULL;
  129. const char *sigfile = NULL, *randfile = NULL;
  130. OPTION_CHOICE o;
  131. int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0;
  132. int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0;
  133. unsigned char *buf = NULL, *sigbuf = NULL;
  134. int engine_impl = 0;
  135. prog = opt_progname(argv[0]);
  136. buf = app_malloc(BUFSIZE, "I/O buffer");
  137. md = EVP_get_digestbyname(prog);
  138. prog = opt_init(argc, argv, dgst_options);
  139. while ((o = opt_next()) != OPT_EOF) {
  140. switch (o) {
  141. case OPT_EOF:
  142. case OPT_ERR:
  143. opthelp:
  144. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  145. goto end;
  146. case OPT_HELP:
  147. opt_help(dgst_options);
  148. ret = 0;
  149. goto end;
  150. case OPT_C:
  151. separator = 1;
  152. break;
  153. case OPT_R:
  154. separator = 2;
  155. break;
  156. case OPT_RAND:
  157. randfile = opt_arg();
  158. break;
  159. case OPT_OUT:
  160. outfile = opt_arg();
  161. break;
  162. case OPT_SIGN:
  163. keyfile = opt_arg();
  164. break;
  165. case OPT_PASSIN:
  166. passinarg = opt_arg();
  167. break;
  168. case OPT_VERIFY:
  169. keyfile = opt_arg();
  170. want_pub = do_verify = 1;
  171. break;
  172. case OPT_PRVERIFY:
  173. keyfile = opt_arg();
  174. do_verify = 1;
  175. break;
  176. case OPT_SIGNATURE:
  177. sigfile = opt_arg();
  178. break;
  179. case OPT_KEYFORM:
  180. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  181. goto opthelp;
  182. break;
  183. case OPT_ENGINE:
  184. e = setup_engine(opt_arg(), 0);
  185. break;
  186. case OPT_ENGINE_IMPL:
  187. engine_impl = 1;
  188. break;
  189. case OPT_HEX:
  190. out_bin = 0;
  191. break;
  192. case OPT_BINARY:
  193. out_bin = 1;
  194. break;
  195. case OPT_DEBUG:
  196. debug = 1;
  197. break;
  198. case OPT_FIPS_FINGERPRINT:
  199. hmac_key = "etaonrishdlcupfm";
  200. break;
  201. case OPT_HMAC:
  202. hmac_key = opt_arg();
  203. break;
  204. case OPT_MAC:
  205. mac_name = opt_arg();
  206. break;
  207. case OPT_SIGOPT:
  208. if (!sigopts)
  209. sigopts = sk_OPENSSL_STRING_new_null();
  210. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  211. goto opthelp;
  212. break;
  213. case OPT_MACOPT:
  214. if (!macopts)
  215. macopts = sk_OPENSSL_STRING_new_null();
  216. if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg()))
  217. goto opthelp;
  218. break;
  219. case OPT_DIGEST:
  220. if (!opt_md(opt_unknown(), &m))
  221. goto opthelp;
  222. md = m;
  223. break;
  224. }
  225. }
  226. argc = opt_num_rest();
  227. argv = opt_rest();
  228. if (do_verify && !sigfile) {
  229. BIO_printf(bio_err,
  230. "No signature to verify: use the -signature option\n");
  231. goto end;
  232. }
  233. if (engine_impl)
  234. impl = e;
  235. in = BIO_new(BIO_s_file());
  236. bmd = BIO_new(BIO_f_md());
  237. if ((in == NULL) || (bmd == NULL)) {
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. if (debug) {
  242. BIO_set_callback(in, BIO_debug_callback);
  243. /* needed for windows 3.1 */
  244. BIO_set_callback_arg(in, (char *)bio_err);
  245. }
  246. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  247. BIO_printf(bio_err, "Error getting password\n");
  248. goto end;
  249. }
  250. if (out_bin == -1) {
  251. if (keyfile)
  252. out_bin = 1;
  253. else
  254. out_bin = 0;
  255. }
  256. if (randfile)
  257. app_RAND_load_file(randfile, 0);
  258. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  259. if (out == NULL)
  260. goto end;
  261. if ((! !mac_name + ! !keyfile + ! !hmac_key) > 1) {
  262. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  263. goto end;
  264. }
  265. if (keyfile) {
  266. if (want_pub)
  267. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "key file");
  268. else
  269. sigkey = load_key(keyfile, keyform, 0, passin, e, "key file");
  270. if (!sigkey) {
  271. /*
  272. * load_[pub]key() has already printed an appropriate message
  273. */
  274. goto end;
  275. }
  276. }
  277. if (mac_name) {
  278. EVP_PKEY_CTX *mac_ctx = NULL;
  279. int r = 0;
  280. if (!init_gen_str(&mac_ctx, mac_name, impl, 0))
  281. goto mac_end;
  282. if (macopts) {
  283. char *macopt;
  284. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  285. macopt = sk_OPENSSL_STRING_value(macopts, i);
  286. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  287. BIO_printf(bio_err,
  288. "MAC parameter error \"%s\"\n", macopt);
  289. ERR_print_errors(bio_err);
  290. goto mac_end;
  291. }
  292. }
  293. }
  294. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
  295. BIO_puts(bio_err, "Error generating key\n");
  296. ERR_print_errors(bio_err);
  297. goto mac_end;
  298. }
  299. r = 1;
  300. mac_end:
  301. EVP_PKEY_CTX_free(mac_ctx);
  302. if (r == 0)
  303. goto end;
  304. }
  305. if (hmac_key) {
  306. sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, impl,
  307. (unsigned char *)hmac_key, -1);
  308. if (!sigkey)
  309. goto end;
  310. }
  311. if (sigkey) {
  312. EVP_MD_CTX *mctx = NULL;
  313. EVP_PKEY_CTX *pctx = NULL;
  314. int r;
  315. if (!BIO_get_md_ctx(bmd, &mctx)) {
  316. BIO_printf(bio_err, "Error getting context\n");
  317. ERR_print_errors(bio_err);
  318. goto end;
  319. }
  320. if (do_verify)
  321. r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  322. else
  323. r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  324. if (!r) {
  325. BIO_printf(bio_err, "Error setting context\n");
  326. ERR_print_errors(bio_err);
  327. goto end;
  328. }
  329. if (sigopts) {
  330. char *sigopt;
  331. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  332. sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  333. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  334. BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
  335. ERR_print_errors(bio_err);
  336. goto end;
  337. }
  338. }
  339. }
  340. }
  341. /* we use md as a filter, reading from 'in' */
  342. else {
  343. EVP_MD_CTX *mctx = NULL;
  344. if (!BIO_get_md_ctx(bmd, &mctx)) {
  345. BIO_printf(bio_err, "Error getting context\n");
  346. ERR_print_errors(bio_err);
  347. goto end;
  348. }
  349. if (md == NULL)
  350. md = EVP_sha256();
  351. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  352. BIO_printf(bio_err, "Error setting digest\n");
  353. ERR_print_errors(bio_err);
  354. goto end;
  355. }
  356. }
  357. if (sigfile && sigkey) {
  358. BIO *sigbio = BIO_new_file(sigfile, "rb");
  359. if (!sigbio) {
  360. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  361. ERR_print_errors(bio_err);
  362. goto end;
  363. }
  364. siglen = EVP_PKEY_size(sigkey);
  365. sigbuf = app_malloc(siglen, "signature buffer");
  366. siglen = BIO_read(sigbio, sigbuf, siglen);
  367. BIO_free(sigbio);
  368. if (siglen <= 0) {
  369. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  370. ERR_print_errors(bio_err);
  371. goto end;
  372. }
  373. }
  374. inp = BIO_push(bmd, in);
  375. if (md == NULL) {
  376. EVP_MD_CTX *tctx;
  377. BIO_get_md_ctx(bmd, &tctx);
  378. md = EVP_MD_CTX_md(tctx);
  379. }
  380. if (argc == 0) {
  381. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  382. ret = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  383. siglen, NULL, NULL, "stdin", bmd);
  384. } else {
  385. const char *md_name = NULL, *sig_name = NULL;
  386. if (!out_bin) {
  387. if (sigkey) {
  388. const EVP_PKEY_ASN1_METHOD *ameth;
  389. ameth = EVP_PKEY_get0_asn1(sigkey);
  390. if (ameth)
  391. EVP_PKEY_asn1_get0_info(NULL, NULL,
  392. NULL, NULL, &sig_name, ameth);
  393. }
  394. if (md)
  395. md_name = EVP_MD_name(md);
  396. }
  397. ret = 0;
  398. for (i = 0; i < argc; i++) {
  399. int r;
  400. if (BIO_read_filename(in, argv[i]) <= 0) {
  401. perror(argv[i]);
  402. ret++;
  403. continue;
  404. } else
  405. r = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  406. siglen, sig_name, md_name, argv[i], bmd);
  407. if (r)
  408. ret = r;
  409. (void)BIO_reset(bmd);
  410. }
  411. }
  412. end:
  413. OPENSSL_clear_free(buf, BUFSIZE);
  414. BIO_free(in);
  415. OPENSSL_free(passin);
  416. BIO_free_all(out);
  417. EVP_PKEY_free(sigkey);
  418. sk_OPENSSL_STRING_free(sigopts);
  419. sk_OPENSSL_STRING_free(macopts);
  420. OPENSSL_free(sigbuf);
  421. BIO_free(bmd);
  422. return (ret);
  423. }
  424. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  425. EVP_PKEY *key, unsigned char *sigin, int siglen,
  426. const char *sig_name, const char *md_name,
  427. const char *file, BIO *bmd)
  428. {
  429. size_t len;
  430. int i;
  431. for (;;) {
  432. i = BIO_read(bp, (char *)buf, BUFSIZE);
  433. if (i < 0) {
  434. BIO_printf(bio_err, "Read Error in %s\n", file);
  435. ERR_print_errors(bio_err);
  436. return 1;
  437. }
  438. if (i == 0)
  439. break;
  440. }
  441. if (sigin) {
  442. EVP_MD_CTX *ctx;
  443. BIO_get_md_ctx(bp, &ctx);
  444. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  445. if (i > 0)
  446. BIO_printf(out, "Verified OK\n");
  447. else if (i == 0) {
  448. BIO_printf(out, "Verification Failure\n");
  449. return 1;
  450. } else {
  451. BIO_printf(bio_err, "Error Verifying Data\n");
  452. ERR_print_errors(bio_err);
  453. return 1;
  454. }
  455. return 0;
  456. }
  457. if (key) {
  458. EVP_MD_CTX *ctx;
  459. BIO_get_md_ctx(bp, &ctx);
  460. len = BUFSIZE;
  461. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  462. BIO_printf(bio_err, "Error Signing Data\n");
  463. ERR_print_errors(bio_err);
  464. return 1;
  465. }
  466. } else {
  467. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  468. if ((int)len < 0) {
  469. ERR_print_errors(bio_err);
  470. return 1;
  471. }
  472. }
  473. if (binout)
  474. BIO_write(out, buf, len);
  475. else if (sep == 2) {
  476. for (i = 0; i < (int)len; i++)
  477. BIO_printf(out, "%02x", buf[i]);
  478. BIO_printf(out, " *%s\n", file);
  479. } else {
  480. if (sig_name) {
  481. BIO_puts(out, sig_name);
  482. if (md_name)
  483. BIO_printf(out, "-%s", md_name);
  484. BIO_printf(out, "(%s)= ", file);
  485. } else if (md_name)
  486. BIO_printf(out, "%s(%s)= ", md_name, file);
  487. else
  488. BIO_printf(out, "(%s)= ", file);
  489. for (i = 0; i < (int)len; i++) {
  490. if (sep && (i != 0))
  491. BIO_printf(out, ":");
  492. BIO_printf(out, "%02x", buf[i]);
  493. }
  494. BIO_printf(out, "\n");
  495. }
  496. return 0;
  497. }