dgst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #undef BUFSIZE
  69. #define BUFSIZE 1024*8
  70. #undef PROG
  71. #define PROG dgst_main
  72. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  73. EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
  74. const char *file);
  75. int MAIN(int, char **);
  76. int MAIN(int argc, char **argv)
  77. {
  78. ENGINE *e = NULL;
  79. unsigned char *buf=NULL;
  80. int i,err=0;
  81. const EVP_MD *md=NULL,*m;
  82. BIO *in=NULL,*inp;
  83. BIO *bmd=NULL;
  84. BIO *out = NULL;
  85. const char *name;
  86. #define PROG_NAME_SIZE 39
  87. char pname[PROG_NAME_SIZE+1];
  88. int separator=0;
  89. int debug=0;
  90. int keyform=FORMAT_PEM;
  91. const char *outfile = NULL, *keyfile = NULL;
  92. const char *sigfile = NULL, *randfile = NULL;
  93. int out_bin = -1, want_pub = 0, do_verify = 0;
  94. EVP_PKEY *sigkey = NULL;
  95. unsigned char *sigbuf = NULL;
  96. int siglen = 0;
  97. char *engine=NULL;
  98. apps_startup();
  99. if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL)
  100. {
  101. BIO_printf(bio_err,"out of memory\n");
  102. goto end;
  103. }
  104. if (bio_err == NULL)
  105. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  106. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  107. if (!load_config(bio_err, NULL))
  108. goto end;
  109. /* first check the program name */
  110. program_name(argv[0],pname,sizeof pname);
  111. md=EVP_get_digestbyname(pname);
  112. argc--;
  113. argv++;
  114. while (argc > 0)
  115. {
  116. if ((*argv)[0] != '-') break;
  117. if (strcmp(*argv,"-c") == 0)
  118. separator=1;
  119. else if (strcmp(*argv,"-rand") == 0)
  120. {
  121. if (--argc < 1) break;
  122. randfile=*(++argv);
  123. }
  124. else if (strcmp(*argv,"-out") == 0)
  125. {
  126. if (--argc < 1) break;
  127. outfile=*(++argv);
  128. }
  129. else if (strcmp(*argv,"-sign") == 0)
  130. {
  131. if (--argc < 1) break;
  132. keyfile=*(++argv);
  133. }
  134. else if (strcmp(*argv,"-verify") == 0)
  135. {
  136. if (--argc < 1) break;
  137. keyfile=*(++argv);
  138. want_pub = 1;
  139. do_verify = 1;
  140. }
  141. else if (strcmp(*argv,"-prverify") == 0)
  142. {
  143. if (--argc < 1) break;
  144. keyfile=*(++argv);
  145. do_verify = 1;
  146. }
  147. else if (strcmp(*argv,"-signature") == 0)
  148. {
  149. if (--argc < 1) break;
  150. sigfile=*(++argv);
  151. }
  152. else if (strcmp(*argv,"-keyform") == 0)
  153. {
  154. if (--argc < 1) break;
  155. keyform=str2fmt(*(++argv));
  156. }
  157. else if (strcmp(*argv,"-engine") == 0)
  158. {
  159. if (--argc < 1) break;
  160. engine= *(++argv);
  161. }
  162. else if (strcmp(*argv,"-hex") == 0)
  163. out_bin = 0;
  164. else if (strcmp(*argv,"-binary") == 0)
  165. out_bin = 1;
  166. else if (strcmp(*argv,"-d") == 0)
  167. debug=1;
  168. else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
  169. md=m;
  170. else
  171. break;
  172. argc--;
  173. argv++;
  174. }
  175. if (md == NULL)
  176. md=EVP_md5();
  177. if(do_verify && !sigfile) {
  178. BIO_printf(bio_err, "No signature to verify: use the -signature option\n");
  179. err = 1;
  180. goto end;
  181. }
  182. if ((argc > 0) && (argv[0][0] == '-')) /* bad option */
  183. {
  184. BIO_printf(bio_err,"unknown option '%s'\n",*argv);
  185. BIO_printf(bio_err,"options are\n");
  186. BIO_printf(bio_err,"-c to output the digest with separating colons\n");
  187. BIO_printf(bio_err,"-d to output debug info\n");
  188. BIO_printf(bio_err,"-hex output as hex dump\n");
  189. BIO_printf(bio_err,"-binary output in binary form\n");
  190. BIO_printf(bio_err,"-sign file sign digest using private key in file\n");
  191. BIO_printf(bio_err,"-verify file verify a signature using public key in file\n");
  192. BIO_printf(bio_err,"-prverify file verify a signature using private key in file\n");
  193. BIO_printf(bio_err,"-keyform arg key file format (PEM or ENGINE)\n");
  194. BIO_printf(bio_err,"-signature file signature to verify\n");
  195. BIO_printf(bio_err,"-binary output in binary form\n");
  196. BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n");
  197. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm (default)\n",
  198. LN_md5,LN_md5);
  199. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  200. LN_md4,LN_md4);
  201. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  202. LN_md2,LN_md2);
  203. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  204. LN_sha1,LN_sha1);
  205. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  206. LN_sha,LN_sha);
  207. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  208. LN_mdc2,LN_mdc2);
  209. BIO_printf(bio_err,"-%3s to use the %s message digest algorithm\n",
  210. LN_ripemd160,LN_ripemd160);
  211. err=1;
  212. goto end;
  213. }
  214. e = setup_engine(bio_err, engine, 0);
  215. in=BIO_new(BIO_s_file());
  216. bmd=BIO_new(BIO_f_md());
  217. if (debug)
  218. {
  219. BIO_set_callback(in,BIO_debug_callback);
  220. /* needed for windows 3.1 */
  221. BIO_set_callback_arg(in,bio_err);
  222. }
  223. if ((in == NULL) || (bmd == NULL))
  224. {
  225. ERR_print_errors(bio_err);
  226. goto end;
  227. }
  228. if(out_bin == -1) {
  229. if(keyfile) out_bin = 1;
  230. else out_bin = 0;
  231. }
  232. if(randfile)
  233. app_RAND_load_file(randfile, bio_err, 0);
  234. if(outfile) {
  235. if(out_bin)
  236. out = BIO_new_file(outfile, "wb");
  237. else out = BIO_new_file(outfile, "w");
  238. } else {
  239. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  240. #ifdef OPENSSL_SYS_VMS
  241. {
  242. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  243. out = BIO_push(tmpbio, out);
  244. }
  245. #endif
  246. }
  247. if(!out) {
  248. BIO_printf(bio_err, "Error opening output file %s\n",
  249. outfile ? outfile : "(stdout)");
  250. ERR_print_errors(bio_err);
  251. goto end;
  252. }
  253. if(keyfile)
  254. {
  255. if (want_pub)
  256. sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
  257. e, "key file");
  258. else
  259. sigkey = load_key(bio_err, keyfile, keyform, 0, NULL,
  260. e, "key file");
  261. if (!sigkey)
  262. {
  263. /* load_[pub]key() has already printed an appropriate
  264. message */
  265. goto end;
  266. }
  267. }
  268. if(sigfile && sigkey) {
  269. BIO *sigbio;
  270. sigbio = BIO_new_file(sigfile, "rb");
  271. siglen = EVP_PKEY_size(sigkey);
  272. sigbuf = OPENSSL_malloc(siglen);
  273. if(!sigbio) {
  274. BIO_printf(bio_err, "Error opening signature file %s\n",
  275. sigfile);
  276. ERR_print_errors(bio_err);
  277. goto end;
  278. }
  279. siglen = BIO_read(sigbio, sigbuf, siglen);
  280. BIO_free(sigbio);
  281. if(siglen <= 0) {
  282. BIO_printf(bio_err, "Error reading signature file %s\n",
  283. sigfile);
  284. ERR_print_errors(bio_err);
  285. goto end;
  286. }
  287. }
  288. /* we use md as a filter, reading from 'in' */
  289. BIO_set_md(bmd,md);
  290. inp=BIO_push(bmd,in);
  291. if (argc == 0)
  292. {
  293. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  294. err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
  295. siglen,"","(stdin)");
  296. }
  297. else
  298. {
  299. name=OBJ_nid2sn(md->type);
  300. for (i=0; i<argc; i++)
  301. {
  302. char *tmp,*tofree=NULL;
  303. int r;
  304. if (BIO_read_filename(in,argv[i]) <= 0)
  305. {
  306. perror(argv[i]);
  307. err++;
  308. continue;
  309. }
  310. if(!out_bin)
  311. {
  312. tmp=tofree=OPENSSL_malloc(strlen(name)+strlen(argv[i])+5);
  313. sprintf(tmp,"%s(%s)= ",name,argv[i]);
  314. }
  315. else
  316. tmp="";
  317. r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
  318. siglen,tmp,argv[i]);
  319. if(r)
  320. err=r;
  321. if(tofree)
  322. OPENSSL_free(tofree);
  323. (void)BIO_reset(bmd);
  324. }
  325. }
  326. end:
  327. if (buf != NULL)
  328. {
  329. OPENSSL_cleanse(buf,BUFSIZE);
  330. OPENSSL_free(buf);
  331. }
  332. if (in != NULL) BIO_free(in);
  333. BIO_free_all(out);
  334. EVP_PKEY_free(sigkey);
  335. if(sigbuf) OPENSSL_free(sigbuf);
  336. if (bmd != NULL) BIO_free(bmd);
  337. apps_shutdown();
  338. EXIT(err);
  339. }
  340. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  341. EVP_PKEY *key, unsigned char *sigin, int siglen, const char *title,
  342. const char *file)
  343. {
  344. int len;
  345. int i;
  346. for (;;)
  347. {
  348. i=BIO_read(bp,(char *)buf,BUFSIZE);
  349. if(i < 0)
  350. {
  351. BIO_printf(bio_err, "Read Error in %s\n",file);
  352. ERR_print_errors(bio_err);
  353. return 1;
  354. }
  355. if (i == 0) break;
  356. }
  357. if(sigin)
  358. {
  359. EVP_MD_CTX *ctx;
  360. BIO_get_md_ctx(bp, &ctx);
  361. i = EVP_VerifyFinal(ctx, sigin, (unsigned int)siglen, key);
  362. if(i > 0)
  363. BIO_printf(out, "Verified OK\n");
  364. else if(i == 0)
  365. {
  366. BIO_printf(out, "Verification Failure\n");
  367. return 1;
  368. }
  369. else
  370. {
  371. BIO_printf(bio_err, "Error Verifying Data\n");
  372. ERR_print_errors(bio_err);
  373. return 1;
  374. }
  375. return 0;
  376. }
  377. if(key)
  378. {
  379. EVP_MD_CTX *ctx;
  380. BIO_get_md_ctx(bp, &ctx);
  381. if(!EVP_SignFinal(ctx, buf, (unsigned int *)&len, key))
  382. {
  383. BIO_printf(bio_err, "Error Signing Data\n");
  384. ERR_print_errors(bio_err);
  385. return 1;
  386. }
  387. }
  388. else
  389. len=BIO_gets(bp,(char *)buf,BUFSIZE);
  390. if(binout) BIO_write(out, buf, len);
  391. else
  392. {
  393. BIO_write(out,title,strlen(title));
  394. for (i=0; i<len; i++)
  395. {
  396. if (sep && (i != 0))
  397. BIO_printf(out, ":");
  398. BIO_printf(out, "%02x",buf[i]);
  399. }
  400. BIO_printf(out, "\n");
  401. }
  402. return 0;
  403. }