dgst.c 19 KB

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