dgst.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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, *impl = 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. int engine_impl = 0;
  121. #endif
  122. char *hmac_key=NULL;
  123. char *mac_name=NULL;
  124. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
  125. apps_startup();
  126. if ((buf=(unsigned char *)OPENSSL_malloc(BUFSIZE)) == NULL)
  127. {
  128. BIO_printf(bio_err,"out of memory\n");
  129. goto end;
  130. }
  131. if (bio_err == NULL)
  132. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  133. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  134. if (!load_config(bio_err, NULL))
  135. goto end;
  136. /* first check the program name */
  137. program_name(argv[0],pname,sizeof pname);
  138. md=EVP_get_digestbyname(pname);
  139. argc--;
  140. argv++;
  141. while (argc > 0)
  142. {
  143. if ((*argv)[0] != '-') break;
  144. if (strcmp(*argv,"-c") == 0)
  145. separator=1;
  146. else if (strcmp(*argv,"-r") == 0)
  147. separator=2;
  148. else if (strcmp(*argv,"-rand") == 0)
  149. {
  150. if (--argc < 1) break;
  151. randfile=*(++argv);
  152. }
  153. else if (strcmp(*argv,"-out") == 0)
  154. {
  155. if (--argc < 1) break;
  156. outfile=*(++argv);
  157. }
  158. else if (strcmp(*argv,"-sign") == 0)
  159. {
  160. if (--argc < 1) break;
  161. keyfile=*(++argv);
  162. }
  163. else if (!strcmp(*argv,"-passin"))
  164. {
  165. if (--argc < 1)
  166. break;
  167. passargin=*++argv;
  168. }
  169. else if (strcmp(*argv,"-verify") == 0)
  170. {
  171. if (--argc < 1) break;
  172. keyfile=*(++argv);
  173. want_pub = 1;
  174. do_verify = 1;
  175. }
  176. else if (strcmp(*argv,"-prverify") == 0)
  177. {
  178. if (--argc < 1) break;
  179. keyfile=*(++argv);
  180. do_verify = 1;
  181. }
  182. else if (strcmp(*argv,"-signature") == 0)
  183. {
  184. if (--argc < 1) break;
  185. sigfile=*(++argv);
  186. }
  187. else if (strcmp(*argv,"-keyform") == 0)
  188. {
  189. if (--argc < 1) break;
  190. keyform=str2fmt(*(++argv));
  191. }
  192. #ifndef OPENSSL_NO_ENGINE
  193. else if (strcmp(*argv,"-engine") == 0)
  194. {
  195. if (--argc < 1) break;
  196. engine= *(++argv);
  197. e = setup_engine(bio_err, engine, 0);
  198. }
  199. else if (strcmp(*argv,"-engine_impl") == 0)
  200. engine_impl = 1;
  201. #endif
  202. else if (strcmp(*argv,"-hex") == 0)
  203. out_bin = 0;
  204. else if (strcmp(*argv,"-binary") == 0)
  205. out_bin = 1;
  206. else if (strcmp(*argv,"-d") == 0)
  207. debug=1;
  208. else if (!strcmp(*argv,"-fips-fingerprint"))
  209. hmac_key = "etaonrishdlcupfm";
  210. else if (!strcmp(*argv,"-hmac"))
  211. {
  212. if (--argc < 1)
  213. break;
  214. hmac_key=*++argv;
  215. }
  216. else if (!strcmp(*argv,"-mac"))
  217. {
  218. if (--argc < 1)
  219. break;
  220. mac_name=*++argv;
  221. }
  222. else if (strcmp(*argv,"-sigopt") == 0)
  223. {
  224. if (--argc < 1)
  225. break;
  226. if (!sigopts)
  227. sigopts = sk_OPENSSL_STRING_new_null();
  228. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, *(++argv)))
  229. break;
  230. }
  231. else if (strcmp(*argv,"-macopt") == 0)
  232. {
  233. if (--argc < 1)
  234. break;
  235. if (!macopts)
  236. macopts = sk_OPENSSL_STRING_new_null();
  237. if (!macopts || !sk_OPENSSL_STRING_push(macopts, *(++argv)))
  238. break;
  239. }
  240. else if ((m=EVP_get_digestbyname(&((*argv)[1]))) != NULL)
  241. md=m;
  242. else
  243. break;
  244. argc--;
  245. argv++;
  246. }
  247. if(do_verify && !sigfile) {
  248. BIO_printf(bio_err, "No signature to verify: use the -signature option\n");
  249. goto end;
  250. }
  251. if ((argc > 0) && (argv[0][0] == '-')) /* bad option */
  252. {
  253. BIO_printf(bio_err,"unknown option '%s'\n",*argv);
  254. BIO_printf(bio_err,"options are\n");
  255. BIO_printf(bio_err,"-c to output the digest with separating colons\n");
  256. BIO_printf(bio_err,"-r to output the digest in coreutils format\n");
  257. BIO_printf(bio_err,"-d to output debug info\n");
  258. BIO_printf(bio_err,"-hex output as hex dump\n");
  259. BIO_printf(bio_err,"-binary output in binary form\n");
  260. BIO_printf(bio_err,"-sign file sign digest using private key in file\n");
  261. BIO_printf(bio_err,"-verify file verify a signature using public key in file\n");
  262. BIO_printf(bio_err,"-prverify file verify a signature using private key in file\n");
  263. BIO_printf(bio_err,"-keyform arg key file format (PEM or ENGINE)\n");
  264. BIO_printf(bio_err,"-out filename output to filename rather than stdout\n");
  265. BIO_printf(bio_err,"-signature file signature to verify\n");
  266. BIO_printf(bio_err,"-sigopt nm:v signature parameter\n");
  267. BIO_printf(bio_err,"-hmac key create hashed MAC with key\n");
  268. BIO_printf(bio_err,"-mac algorithm create MAC (not neccessarily HMAC)\n");
  269. BIO_printf(bio_err,"-macopt nm:v MAC algorithm parameters or key\n");
  270. #ifndef OPENSSL_NO_ENGINE
  271. BIO_printf(bio_err,"-engine e use engine e, possibly a hardware device.\n");
  272. #endif
  273. EVP_MD_do_all_sorted(list_md_fn, bio_err);
  274. goto end;
  275. }
  276. #ifndef OPENSSL_NO_ENGINE
  277. if (engine_impl)
  278. impl = e;
  279. #endif
  280. in=BIO_new(BIO_s_file());
  281. bmd=BIO_new(BIO_f_md());
  282. if (debug)
  283. {
  284. BIO_set_callback(in,BIO_debug_callback);
  285. /* needed for windows 3.1 */
  286. BIO_set_callback_arg(in,(char *)bio_err);
  287. }
  288. if(!app_passwd(bio_err, passargin, NULL, &passin, NULL))
  289. {
  290. BIO_printf(bio_err, "Error getting password\n");
  291. goto end;
  292. }
  293. if ((in == NULL) || (bmd == NULL))
  294. {
  295. ERR_print_errors(bio_err);
  296. goto end;
  297. }
  298. if(out_bin == -1) {
  299. if(keyfile)
  300. out_bin = 1;
  301. else
  302. out_bin = 0;
  303. }
  304. if(randfile)
  305. app_RAND_load_file(randfile, bio_err, 0);
  306. if(outfile) {
  307. if(out_bin)
  308. out = BIO_new_file(outfile, "wb");
  309. else out = BIO_new_file(outfile, "w");
  310. } else {
  311. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  312. #ifdef OPENSSL_SYS_VMS
  313. {
  314. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  315. out = BIO_push(tmpbio, out);
  316. }
  317. #endif
  318. }
  319. if(!out) {
  320. BIO_printf(bio_err, "Error opening output file %s\n",
  321. outfile ? outfile : "(stdout)");
  322. ERR_print_errors(bio_err);
  323. goto end;
  324. }
  325. if ((!!mac_name + !!keyfile + !!hmac_key) > 1)
  326. {
  327. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  328. goto end;
  329. }
  330. if(keyfile)
  331. {
  332. if (want_pub)
  333. sigkey = load_pubkey(bio_err, keyfile, keyform, 0, NULL,
  334. e, "key file");
  335. else
  336. sigkey = load_key(bio_err, keyfile, keyform, 0, passin,
  337. e, "key file");
  338. if (!sigkey)
  339. {
  340. /* load_[pub]key() has already printed an appropriate
  341. message */
  342. goto end;
  343. }
  344. }
  345. if (mac_name)
  346. {
  347. EVP_PKEY_CTX *mac_ctx = NULL;
  348. int r = 0;
  349. if (!init_gen_str(bio_err, &mac_ctx, mac_name, impl, 0))
  350. goto mac_end;
  351. if (macopts)
  352. {
  353. char *macopt;
  354. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++)
  355. {
  356. macopt = sk_OPENSSL_STRING_value(macopts, i);
  357. if (pkey_ctrl_string(mac_ctx, macopt) <= 0)
  358. {
  359. BIO_printf(bio_err,
  360. "MAC parameter error \"%s\"\n",
  361. macopt);
  362. ERR_print_errors(bio_err);
  363. goto mac_end;
  364. }
  365. }
  366. }
  367. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0)
  368. {
  369. BIO_puts(bio_err, "Error generating key\n");
  370. ERR_print_errors(bio_err);
  371. goto mac_end;
  372. }
  373. r = 1;
  374. mac_end:
  375. if (mac_ctx)
  376. EVP_PKEY_CTX_free(mac_ctx);
  377. if (r == 0)
  378. goto end;
  379. }
  380. if (hmac_key)
  381. {
  382. sigkey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, impl,
  383. (unsigned char *)hmac_key, -1);
  384. if (!sigkey)
  385. goto end;
  386. }
  387. if (sigkey)
  388. {
  389. EVP_MD_CTX *mctx = NULL;
  390. EVP_PKEY_CTX *pctx = NULL;
  391. int r;
  392. if (!BIO_get_md_ctx(bmd, &mctx))
  393. {
  394. BIO_printf(bio_err, "Error getting context\n");
  395. ERR_print_errors(bio_err);
  396. goto end;
  397. }
  398. if (do_verify)
  399. r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  400. else
  401. r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  402. if (!r)
  403. {
  404. BIO_printf(bio_err, "Error setting context\n");
  405. ERR_print_errors(bio_err);
  406. goto end;
  407. }
  408. if (sigopts)
  409. {
  410. char *sigopt;
  411. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++)
  412. {
  413. sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  414. if (pkey_ctrl_string(pctx, sigopt) <= 0)
  415. {
  416. BIO_printf(bio_err,
  417. "parameter error \"%s\"\n",
  418. sigopt);
  419. ERR_print_errors(bio_err);
  420. goto end;
  421. }
  422. }
  423. }
  424. }
  425. /* we use md as a filter, reading from 'in' */
  426. else
  427. {
  428. EVP_MD_CTX *mctx = NULL;
  429. if (!BIO_get_md_ctx(bmd, &mctx))
  430. {
  431. BIO_printf(bio_err, "Error getting context\n");
  432. ERR_print_errors(bio_err);
  433. goto end;
  434. }
  435. if (md == NULL)
  436. md = EVP_md5();
  437. if (!EVP_DigestInit_ex(mctx, md, impl))
  438. {
  439. BIO_printf(bio_err, "Error setting digest %s\n", pname);
  440. ERR_print_errors(bio_err);
  441. goto end;
  442. }
  443. }
  444. if(sigfile && sigkey) {
  445. BIO *sigbio;
  446. sigbio = BIO_new_file(sigfile, "rb");
  447. siglen = EVP_PKEY_size(sigkey);
  448. sigbuf = OPENSSL_malloc(siglen);
  449. if(!sigbio) {
  450. BIO_printf(bio_err, "Error opening signature file %s\n",
  451. sigfile);
  452. ERR_print_errors(bio_err);
  453. goto end;
  454. }
  455. siglen = BIO_read(sigbio, sigbuf, siglen);
  456. BIO_free(sigbio);
  457. if(siglen <= 0) {
  458. BIO_printf(bio_err, "Error reading signature file %s\n",
  459. sigfile);
  460. ERR_print_errors(bio_err);
  461. goto end;
  462. }
  463. }
  464. inp=BIO_push(bmd,in);
  465. if (md == NULL)
  466. {
  467. EVP_MD_CTX *tctx;
  468. BIO_get_md_ctx(bmd, &tctx);
  469. md = EVP_MD_CTX_md(tctx);
  470. }
  471. if (argc == 0)
  472. {
  473. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  474. err=do_fp(out, buf,inp,separator, out_bin, sigkey, sigbuf,
  475. siglen,NULL,NULL,"stdin",bmd);
  476. }
  477. else
  478. {
  479. const char *md_name = NULL, *sig_name = NULL;
  480. if(!out_bin)
  481. {
  482. if (sigkey)
  483. {
  484. const EVP_PKEY_ASN1_METHOD *ameth;
  485. ameth = EVP_PKEY_get0_asn1(sigkey);
  486. if (ameth)
  487. EVP_PKEY_asn1_get0_info(NULL, NULL,
  488. NULL, NULL, &sig_name, ameth);
  489. }
  490. md_name = EVP_MD_name(md);
  491. }
  492. err = 0;
  493. for (i=0; i<argc; i++)
  494. {
  495. int r;
  496. if (BIO_read_filename(in,argv[i]) <= 0)
  497. {
  498. perror(argv[i]);
  499. err++;
  500. continue;
  501. }
  502. else
  503. r=do_fp(out,buf,inp,separator,out_bin,sigkey,sigbuf,
  504. siglen,sig_name,md_name, argv[i],bmd);
  505. if(r)
  506. err=r;
  507. (void)BIO_reset(bmd);
  508. }
  509. }
  510. end:
  511. if (buf != NULL)
  512. {
  513. OPENSSL_cleanse(buf,BUFSIZE);
  514. OPENSSL_free(buf);
  515. }
  516. if (in != NULL) BIO_free(in);
  517. if (passin)
  518. OPENSSL_free(passin);
  519. BIO_free_all(out);
  520. EVP_PKEY_free(sigkey);
  521. if (sigopts)
  522. sk_OPENSSL_STRING_free(sigopts);
  523. if (macopts)
  524. sk_OPENSSL_STRING_free(macopts);
  525. if(sigbuf) OPENSSL_free(sigbuf);
  526. if (bmd != NULL) BIO_free(bmd);
  527. apps_shutdown();
  528. OPENSSL_EXIT(err);
  529. }
  530. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  531. EVP_PKEY *key, unsigned char *sigin, int siglen,
  532. const char *sig_name, const char *md_name,
  533. const char *file,BIO *bmd)
  534. {
  535. size_t len;
  536. int i;
  537. for (;;)
  538. {
  539. i=BIO_read(bp,(char *)buf,BUFSIZE);
  540. if(i < 0)
  541. {
  542. BIO_printf(bio_err, "Read Error in %s\n",file);
  543. ERR_print_errors(bio_err);
  544. return 1;
  545. }
  546. if (i == 0) break;
  547. }
  548. if(sigin)
  549. {
  550. EVP_MD_CTX *ctx;
  551. BIO_get_md_ctx(bp, &ctx);
  552. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  553. if(i > 0)
  554. BIO_printf(out, "Verified OK\n");
  555. else if(i == 0)
  556. {
  557. BIO_printf(out, "Verification Failure\n");
  558. return 1;
  559. }
  560. else
  561. {
  562. BIO_printf(bio_err, "Error Verifying Data\n");
  563. ERR_print_errors(bio_err);
  564. return 1;
  565. }
  566. return 0;
  567. }
  568. if(key)
  569. {
  570. EVP_MD_CTX *ctx;
  571. BIO_get_md_ctx(bp, &ctx);
  572. len = BUFSIZE;
  573. if(!EVP_DigestSignFinal(ctx, buf, &len))
  574. {
  575. BIO_printf(bio_err, "Error Signing Data\n");
  576. ERR_print_errors(bio_err);
  577. return 1;
  578. }
  579. }
  580. else
  581. {
  582. len=BIO_gets(bp,(char *)buf,BUFSIZE);
  583. if ((int)len <0)
  584. {
  585. ERR_print_errors(bio_err);
  586. return 1;
  587. }
  588. }
  589. if(binout) BIO_write(out, buf, len);
  590. else if (sep == 2)
  591. {
  592. for (i=0; i<(int)len; i++)
  593. BIO_printf(out, "%02x",buf[i]);
  594. BIO_printf(out, " *%s\n", file);
  595. }
  596. else
  597. {
  598. if (sig_name)
  599. BIO_printf(out, "%s-%s(%s)= ", sig_name, md_name, file);
  600. else if (md_name)
  601. BIO_printf(out, "%s(%s)= ", md_name, file);
  602. else
  603. BIO_printf(out, "(%s)= ", file);
  604. for (i=0; i<(int)len; i++)
  605. {
  606. if (sep && (i != 0))
  607. BIO_printf(out, ":");
  608. BIO_printf(out, "%02x",buf[i]);
  609. }
  610. BIO_printf(out, "\n");
  611. }
  612. return 0;
  613. }