dgst.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  91. {"passin", OPT_PASSIN, 's'},
  92. {"sign", OPT_SIGN, '<', "Sign digest using private key in file"},
  93. {"verify", OPT_VERIFY, '<',
  94. "Verify a signature using public key in file"},
  95. {"prverify", OPT_PRVERIFY, '<',
  96. "Verify a signature using private key in file"},
  97. {"signature", OPT_SIGNATURE, '<', "File with signature to verify"},
  98. {"keyform", OPT_KEYFORM, 'f', "Key file format (PEM or ENGINE)"},
  99. {"hex", OPT_HEX, '-', "Print as hex dump"},
  100. {"binary", OPT_BINARY, '-', "Print in binary form"},
  101. {"d", OPT_DEBUG, '-', "Print debug info"},
  102. {"debug", OPT_DEBUG, '-'},
  103. {"fips-fingerprint", OPT_FIPS_FINGERPRINT, '-'},
  104. {"hmac", OPT_HMAC, 's', "Create hashed MAC with key"},
  105. {"mac", OPT_MAC, 's', "Create MAC (not neccessarily HMAC)"},
  106. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  107. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form or key"},
  108. {"", OPT_DIGEST, '-', "Any supported digest"},
  109. #ifndef OPENSSL_NO_ENGINE
  110. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  111. {"engine_impl", OPT_ENGINE_IMPL, '-'},
  112. #endif
  113. {NULL}
  114. };
  115. int dgst_main(int argc, char **argv)
  116. {
  117. BIO *in = NULL, *inp, *bmd = NULL, *out = NULL;
  118. ENGINE *e = NULL, *impl = NULL;
  119. EVP_PKEY *sigkey = NULL;
  120. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
  121. char *hmac_key = NULL;
  122. char *mac_name = NULL;
  123. char *passinarg = NULL, *passin = NULL;
  124. const EVP_MD *md = NULL, *m;
  125. const char *outfile = NULL, *keyfile = NULL, *prog = NULL;
  126. const char *sigfile = NULL, *randfile = NULL;
  127. OPTION_CHOICE o;
  128. int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0;
  129. int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0;
  130. unsigned char *buf = NULL, *sigbuf = NULL;
  131. int engine_impl = 0;
  132. prog = opt_progname(argv[0]);
  133. buf = app_malloc(BUFSIZE, "I/O buffer");
  134. md = EVP_get_digestbyname(prog);
  135. prog = opt_init(argc, argv, dgst_options);
  136. while ((o = opt_next()) != OPT_EOF) {
  137. switch (o) {
  138. case OPT_EOF:
  139. case OPT_ERR:
  140. opthelp:
  141. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  142. goto end;
  143. case OPT_HELP:
  144. opt_help(dgst_options);
  145. ret = 0;
  146. goto end;
  147. case OPT_C:
  148. separator = 1;
  149. break;
  150. case OPT_R:
  151. separator = 2;
  152. break;
  153. case OPT_RAND:
  154. randfile = opt_arg();
  155. break;
  156. case OPT_OUT:
  157. outfile = opt_arg();
  158. break;
  159. case OPT_SIGN:
  160. keyfile = opt_arg();
  161. break;
  162. case OPT_PASSIN:
  163. passinarg = opt_arg();
  164. break;
  165. case OPT_VERIFY:
  166. keyfile = opt_arg();
  167. want_pub = do_verify = 1;
  168. break;
  169. case OPT_PRVERIFY:
  170. keyfile = opt_arg();
  171. do_verify = 1;
  172. break;
  173. case OPT_SIGNATURE:
  174. sigfile = opt_arg();
  175. break;
  176. case OPT_KEYFORM:
  177. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  178. goto opthelp;
  179. break;
  180. case OPT_ENGINE:
  181. e = setup_engine(opt_arg(), 0);
  182. break;
  183. case OPT_ENGINE_IMPL:
  184. engine_impl = 1;
  185. break;
  186. case OPT_HEX:
  187. out_bin = 0;
  188. break;
  189. case OPT_BINARY:
  190. out_bin = 1;
  191. break;
  192. case OPT_DEBUG:
  193. debug = 1;
  194. break;
  195. case OPT_FIPS_FINGERPRINT:
  196. hmac_key = "etaonrishdlcupfm";
  197. break;
  198. case OPT_HMAC:
  199. hmac_key = opt_arg();
  200. break;
  201. case OPT_MAC:
  202. mac_name = opt_arg();
  203. break;
  204. case OPT_SIGOPT:
  205. if (!sigopts)
  206. sigopts = sk_OPENSSL_STRING_new_null();
  207. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  208. goto opthelp;
  209. break;
  210. case OPT_MACOPT:
  211. if (!macopts)
  212. macopts = sk_OPENSSL_STRING_new_null();
  213. if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg()))
  214. goto opthelp;
  215. break;
  216. case OPT_DIGEST:
  217. if (!opt_md(opt_unknown(), &m))
  218. goto opthelp;
  219. md = m;
  220. break;
  221. }
  222. }
  223. argc = opt_num_rest();
  224. argv = opt_rest();
  225. if (do_verify && !sigfile) {
  226. BIO_printf(bio_err,
  227. "No signature to verify: use the -signature option\n");
  228. goto end;
  229. }
  230. if (engine_impl)
  231. impl = e;
  232. in = BIO_new(BIO_s_file());
  233. bmd = BIO_new(BIO_f_md());
  234. if ((in == NULL) || (bmd == NULL)) {
  235. ERR_print_errors(bio_err);
  236. goto end;
  237. }
  238. if (debug) {
  239. BIO_set_callback(in, BIO_debug_callback);
  240. /* needed for windows 3.1 */
  241. BIO_set_callback_arg(in, (char *)bio_err);
  242. }
  243. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  244. BIO_printf(bio_err, "Error getting password\n");
  245. goto end;
  246. }
  247. if (out_bin == -1) {
  248. if (keyfile)
  249. out_bin = 1;
  250. else
  251. out_bin = 0;
  252. }
  253. if (randfile)
  254. app_RAND_load_file(randfile, 0);
  255. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  256. if (out == NULL)
  257. goto end;
  258. if ((! !mac_name + ! !keyfile + ! !hmac_key) > 1) {
  259. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  260. goto end;
  261. }
  262. if (keyfile) {
  263. if (want_pub)
  264. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "key file");
  265. else
  266. sigkey = load_key(keyfile, keyform, 0, passin, e, "key file");
  267. if (!sigkey) {
  268. /*
  269. * load_[pub]key() has already printed an appropriate message
  270. */
  271. goto end;
  272. }
  273. }
  274. if (mac_name) {
  275. EVP_PKEY_CTX *mac_ctx = NULL;
  276. int r = 0;
  277. if (!init_gen_str(&mac_ctx, mac_name, impl, 0))
  278. goto mac_end;
  279. if (macopts) {
  280. char *macopt;
  281. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  282. macopt = sk_OPENSSL_STRING_value(macopts, i);
  283. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  284. BIO_printf(bio_err,
  285. "MAC parameter error \"%s\"\n", macopt);
  286. ERR_print_errors(bio_err);
  287. goto mac_end;
  288. }
  289. }
  290. }
  291. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
  292. BIO_puts(bio_err, "Error generating key\n");
  293. ERR_print_errors(bio_err);
  294. goto mac_end;
  295. }
  296. r = 1;
  297. mac_end:
  298. EVP_PKEY_CTX_free(mac_ctx);
  299. if (r == 0)
  300. goto end;
  301. }
  302. if (hmac_key) {
  303. sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, impl,
  304. (unsigned char *)hmac_key, -1);
  305. if (!sigkey)
  306. goto end;
  307. }
  308. if (sigkey) {
  309. EVP_MD_CTX *mctx = NULL;
  310. EVP_PKEY_CTX *pctx = NULL;
  311. int r;
  312. if (!BIO_get_md_ctx(bmd, &mctx)) {
  313. BIO_printf(bio_err, "Error getting context\n");
  314. ERR_print_errors(bio_err);
  315. goto end;
  316. }
  317. if (do_verify)
  318. r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  319. else
  320. r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  321. if (!r) {
  322. BIO_printf(bio_err, "Error setting context\n");
  323. ERR_print_errors(bio_err);
  324. goto end;
  325. }
  326. if (sigopts) {
  327. char *sigopt;
  328. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  329. sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  330. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  331. BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
  332. ERR_print_errors(bio_err);
  333. goto end;
  334. }
  335. }
  336. }
  337. }
  338. /* we use md as a filter, reading from 'in' */
  339. else {
  340. EVP_MD_CTX *mctx = NULL;
  341. if (!BIO_get_md_ctx(bmd, &mctx)) {
  342. BIO_printf(bio_err, "Error getting context\n");
  343. ERR_print_errors(bio_err);
  344. goto end;
  345. }
  346. if (md == NULL)
  347. md = EVP_sha256();
  348. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  349. BIO_printf(bio_err, "Error setting digest\n");
  350. ERR_print_errors(bio_err);
  351. goto end;
  352. }
  353. }
  354. if (sigfile && sigkey) {
  355. BIO *sigbio = BIO_new_file(sigfile, "rb");
  356. if (!sigbio) {
  357. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  358. ERR_print_errors(bio_err);
  359. goto end;
  360. }
  361. siglen = EVP_PKEY_size(sigkey);
  362. sigbuf = app_malloc(siglen, "signature buffer");
  363. siglen = BIO_read(sigbio, sigbuf, siglen);
  364. BIO_free(sigbio);
  365. if (siglen <= 0) {
  366. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  367. ERR_print_errors(bio_err);
  368. goto end;
  369. }
  370. }
  371. inp = BIO_push(bmd, in);
  372. if (md == NULL) {
  373. EVP_MD_CTX *tctx;
  374. BIO_get_md_ctx(bmd, &tctx);
  375. md = EVP_MD_CTX_md(tctx);
  376. }
  377. if (argc == 0) {
  378. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  379. ret = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  380. siglen, NULL, NULL, "stdin", bmd);
  381. } else {
  382. const char *md_name = NULL, *sig_name = NULL;
  383. if (!out_bin) {
  384. if (sigkey) {
  385. const EVP_PKEY_ASN1_METHOD *ameth;
  386. ameth = EVP_PKEY_get0_asn1(sigkey);
  387. if (ameth)
  388. EVP_PKEY_asn1_get0_info(NULL, NULL,
  389. NULL, NULL, &sig_name, ameth);
  390. }
  391. if (md)
  392. md_name = EVP_MD_name(md);
  393. }
  394. ret = 0;
  395. for (i = 0; i < argc; i++) {
  396. int r;
  397. if (BIO_read_filename(in, argv[i]) <= 0) {
  398. perror(argv[i]);
  399. ret++;
  400. continue;
  401. } else
  402. r = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  403. siglen, sig_name, md_name, argv[i], bmd);
  404. if (r)
  405. ret = r;
  406. (void)BIO_reset(bmd);
  407. }
  408. }
  409. end:
  410. OPENSSL_clear_free(buf, BUFSIZE);
  411. BIO_free(in);
  412. OPENSSL_free(passin);
  413. BIO_free_all(out);
  414. EVP_PKEY_free(sigkey);
  415. sk_OPENSSL_STRING_free(sigopts);
  416. sk_OPENSSL_STRING_free(macopts);
  417. OPENSSL_free(sigbuf);
  418. BIO_free(bmd);
  419. return (ret);
  420. }
  421. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  422. EVP_PKEY *key, unsigned char *sigin, int siglen,
  423. const char *sig_name, const char *md_name,
  424. const char *file, BIO *bmd)
  425. {
  426. size_t len;
  427. int i;
  428. for (;;) {
  429. i = BIO_read(bp, (char *)buf, BUFSIZE);
  430. if (i < 0) {
  431. BIO_printf(bio_err, "Read Error in %s\n", file);
  432. ERR_print_errors(bio_err);
  433. return 1;
  434. }
  435. if (i == 0)
  436. break;
  437. }
  438. if (sigin) {
  439. EVP_MD_CTX *ctx;
  440. BIO_get_md_ctx(bp, &ctx);
  441. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  442. if (i > 0)
  443. BIO_printf(out, "Verified OK\n");
  444. else if (i == 0) {
  445. BIO_printf(out, "Verification Failure\n");
  446. return 1;
  447. } else {
  448. BIO_printf(bio_err, "Error Verifying Data\n");
  449. ERR_print_errors(bio_err);
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. if (key) {
  455. EVP_MD_CTX *ctx;
  456. BIO_get_md_ctx(bp, &ctx);
  457. len = BUFSIZE;
  458. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  459. BIO_printf(bio_err, "Error Signing Data\n");
  460. ERR_print_errors(bio_err);
  461. return 1;
  462. }
  463. } else {
  464. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  465. if ((int)len < 0) {
  466. ERR_print_errors(bio_err);
  467. return 1;
  468. }
  469. }
  470. if (binout)
  471. BIO_write(out, buf, len);
  472. else if (sep == 2) {
  473. for (i = 0; i < (int)len; i++)
  474. BIO_printf(out, "%02x", buf[i]);
  475. BIO_printf(out, " *%s\n", file);
  476. } else {
  477. if (sig_name) {
  478. BIO_puts(out, sig_name);
  479. if (md_name)
  480. BIO_printf(out, "-%s", md_name);
  481. BIO_printf(out, "(%s)= ", file);
  482. } else if (md_name)
  483. BIO_printf(out, "%s(%s)= ", md_name, file);
  484. else
  485. BIO_printf(out, "(%s)= ", file);
  486. for (i = 0; i < (int)len; i++) {
  487. if (sep && (i != 0))
  488. BIO_printf(out, ":");
  489. BIO_printf(out, "%02x", buf[i]);
  490. }
  491. BIO_printf(out, "\n");
  492. }
  493. return 0;
  494. }