dgst.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*
  2. * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include "apps.h"
  13. #include "progs.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/evp.h>
  17. #include <openssl/objects.h>
  18. #include <openssl/x509.h>
  19. #include <openssl/pem.h>
  20. #include <openssl/hmac.h>
  21. #include <ctype.h>
  22. #undef BUFSIZE
  23. #define BUFSIZE 1024*8
  24. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, int xoflen,
  25. EVP_PKEY *key, unsigned char *sigin, int siglen,
  26. const char *sig_name, const char *md_name,
  27. const char *file);
  28. static void show_digests(const OBJ_NAME *name, void *bio_);
  29. struct doall_dgst_digests {
  30. BIO *bio;
  31. int n;
  32. };
  33. typedef enum OPTION_choice {
  34. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, OPT_LIST,
  35. OPT_C, OPT_R, OPT_OUT, OPT_SIGN, OPT_PASSIN, OPT_VERIFY,
  36. OPT_PRVERIFY, OPT_SIGNATURE, OPT_KEYFORM, OPT_ENGINE, OPT_ENGINE_IMPL,
  37. OPT_HEX, OPT_BINARY, OPT_DEBUG, OPT_FIPS_FINGERPRINT,
  38. OPT_HMAC, OPT_MAC, OPT_SIGOPT, OPT_MACOPT, OPT_XOFLEN,
  39. OPT_DIGEST,
  40. OPT_R_ENUM, OPT_PROV_ENUM
  41. } OPTION_CHOICE;
  42. const OPTIONS dgst_options[] = {
  43. {OPT_HELP_STR, 1, '-', "Usage: %s [options] [file...]\n"},
  44. OPT_SECTION("General"),
  45. {"help", OPT_HELP, '-', "Display this summary"},
  46. {"list", OPT_LIST, '-', "List digests"},
  47. #ifndef OPENSSL_NO_ENGINE
  48. {"engine", OPT_ENGINE, 's', "Use engine e, possibly a hardware device"},
  49. {"engine_impl", OPT_ENGINE_IMPL, '-',
  50. "Also use engine given by -engine for digest operations"},
  51. #endif
  52. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  53. OPT_SECTION("Output"),
  54. {"c", OPT_C, '-', "Print the digest with separating colons"},
  55. {"r", OPT_R, '-', "Print the digest in coreutils format"},
  56. {"out", OPT_OUT, '>', "Output to filename rather than stdout"},
  57. {"keyform", OPT_KEYFORM, 'f', "Key file format (ENGINE, other values ignored)"},
  58. {"hex", OPT_HEX, '-', "Print as hex dump"},
  59. {"binary", OPT_BINARY, '-', "Print in binary form"},
  60. {"xoflen", OPT_XOFLEN, 'p', "Output length for XOF algorithms"},
  61. {"d", OPT_DEBUG, '-', "Print debug info"},
  62. {"debug", OPT_DEBUG, '-', "Print debug info"},
  63. OPT_SECTION("Signing"),
  64. {"sign", OPT_SIGN, 's', "Sign digest using private key"},
  65. {"verify", OPT_VERIFY, 's', "Verify a signature using public key"},
  66. {"prverify", OPT_PRVERIFY, 's', "Verify a signature using private key"},
  67. {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"},
  68. {"signature", OPT_SIGNATURE, '<', "File with signature to verify"},
  69. {"hmac", OPT_HMAC, 's', "Create hashed MAC with key"},
  70. {"mac", OPT_MAC, 's', "Create MAC (not necessarily HMAC)"},
  71. {"macopt", OPT_MACOPT, 's', "MAC algorithm parameters in n:v form or key"},
  72. {"", OPT_DIGEST, '-', "Any supported digest"},
  73. {"fips-fingerprint", OPT_FIPS_FINGERPRINT, '-',
  74. "Compute HMAC with the key used in OpenSSL-FIPS fingerprint"},
  75. OPT_R_OPTIONS,
  76. OPT_PROV_OPTIONS,
  77. OPT_PARAMETERS(),
  78. {"file", 0, 0, "Files to digest (optional; default is stdin)"},
  79. {NULL}
  80. };
  81. int dgst_main(int argc, char **argv)
  82. {
  83. BIO *in = NULL, *inp, *bmd = NULL, *out = NULL;
  84. ENGINE *e = NULL, *impl = NULL;
  85. EVP_PKEY *sigkey = NULL;
  86. STACK_OF(OPENSSL_STRING) *sigopts = NULL, *macopts = NULL;
  87. char *hmac_key = NULL;
  88. char *mac_name = NULL;
  89. char *passinarg = NULL, *passin = NULL;
  90. const EVP_MD *md = NULL, *m;
  91. const char *outfile = NULL, *keyfile = NULL, *prog = NULL;
  92. const char *sigfile = NULL;
  93. const char *md_name = NULL;
  94. OPTION_CHOICE o;
  95. int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0;
  96. int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0;
  97. int xoflen = 0;
  98. unsigned char *buf = NULL, *sigbuf = NULL;
  99. int engine_impl = 0;
  100. struct doall_dgst_digests dec;
  101. prog = opt_progname(argv[0]);
  102. buf = app_malloc(BUFSIZE, "I/O buffer");
  103. md = EVP_get_digestbyname(prog);
  104. prog = opt_init(argc, argv, dgst_options);
  105. while ((o = opt_next()) != OPT_EOF) {
  106. switch (o) {
  107. case OPT_EOF:
  108. case OPT_ERR:
  109. opthelp:
  110. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  111. goto end;
  112. case OPT_HELP:
  113. opt_help(dgst_options);
  114. ret = 0;
  115. goto end;
  116. case OPT_LIST:
  117. BIO_printf(bio_out, "Supported digests:\n");
  118. dec.bio = bio_out;
  119. dec.n = 0;
  120. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH,
  121. show_digests, &dec);
  122. BIO_printf(bio_out, "\n");
  123. ret = 0;
  124. goto end;
  125. case OPT_C:
  126. separator = 1;
  127. break;
  128. case OPT_R:
  129. separator = 2;
  130. break;
  131. case OPT_R_CASES:
  132. if (!opt_rand(o))
  133. goto end;
  134. break;
  135. case OPT_OUT:
  136. outfile = opt_arg();
  137. break;
  138. case OPT_SIGN:
  139. keyfile = opt_arg();
  140. break;
  141. case OPT_PASSIN:
  142. passinarg = opt_arg();
  143. break;
  144. case OPT_VERIFY:
  145. keyfile = opt_arg();
  146. want_pub = do_verify = 1;
  147. break;
  148. case OPT_PRVERIFY:
  149. keyfile = opt_arg();
  150. do_verify = 1;
  151. break;
  152. case OPT_SIGNATURE:
  153. sigfile = opt_arg();
  154. break;
  155. case OPT_KEYFORM:
  156. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  157. goto opthelp;
  158. break;
  159. case OPT_ENGINE:
  160. e = setup_engine(opt_arg(), 0);
  161. break;
  162. case OPT_ENGINE_IMPL:
  163. engine_impl = 1;
  164. break;
  165. case OPT_HEX:
  166. out_bin = 0;
  167. break;
  168. case OPT_BINARY:
  169. out_bin = 1;
  170. break;
  171. case OPT_XOFLEN:
  172. xoflen = atoi(opt_arg());
  173. break;
  174. case OPT_DEBUG:
  175. debug = 1;
  176. break;
  177. case OPT_FIPS_FINGERPRINT:
  178. hmac_key = "etaonrishdlcupfm";
  179. break;
  180. case OPT_HMAC:
  181. hmac_key = opt_arg();
  182. break;
  183. case OPT_MAC:
  184. mac_name = opt_arg();
  185. break;
  186. case OPT_SIGOPT:
  187. if (!sigopts)
  188. sigopts = sk_OPENSSL_STRING_new_null();
  189. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  190. goto opthelp;
  191. break;
  192. case OPT_MACOPT:
  193. if (!macopts)
  194. macopts = sk_OPENSSL_STRING_new_null();
  195. if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg()))
  196. goto opthelp;
  197. break;
  198. case OPT_DIGEST:
  199. if (!opt_md(opt_unknown(), &m))
  200. goto opthelp;
  201. md = m;
  202. break;
  203. case OPT_PROV_CASES:
  204. if (!opt_provider(o))
  205. goto end;
  206. break;
  207. }
  208. }
  209. /* Remaining args are files to digest. */
  210. argc = opt_num_rest();
  211. argv = opt_rest();
  212. if (keyfile != NULL && argc > 1) {
  213. BIO_printf(bio_err, "%s: Can only sign or verify one file.\n", prog);
  214. goto end;
  215. }
  216. if (do_verify && sigfile == NULL) {
  217. BIO_printf(bio_err,
  218. "No signature to verify: use the -signature option\n");
  219. goto end;
  220. }
  221. if (engine_impl)
  222. impl = e;
  223. in = BIO_new(BIO_s_file());
  224. bmd = BIO_new(BIO_f_md());
  225. if ((in == NULL) || (bmd == NULL)) {
  226. ERR_print_errors(bio_err);
  227. goto end;
  228. }
  229. if (debug) {
  230. BIO_set_callback(in, BIO_debug_callback);
  231. /* needed for windows 3.1 */
  232. BIO_set_callback_arg(in, (char *)bio_err);
  233. }
  234. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  235. BIO_printf(bio_err, "Error getting password\n");
  236. goto end;
  237. }
  238. if (out_bin == -1) {
  239. if (keyfile != NULL)
  240. out_bin = 1;
  241. else
  242. out_bin = 0;
  243. }
  244. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  245. if (out == NULL)
  246. goto end;
  247. if ((!(mac_name == NULL) + !(keyfile == NULL) + !(hmac_key == NULL)) > 1) {
  248. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  249. goto end;
  250. }
  251. if (keyfile != NULL) {
  252. int type;
  253. if (want_pub)
  254. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
  255. else
  256. sigkey = load_key(keyfile, keyform, 0, passin, e, "private key");
  257. if (sigkey == NULL) {
  258. /*
  259. * load_[pub]key() has already printed an appropriate message
  260. */
  261. goto end;
  262. }
  263. type = EVP_PKEY_id(sigkey);
  264. if (type == EVP_PKEY_ED25519 || type == EVP_PKEY_ED448) {
  265. /*
  266. * We implement PureEdDSA for these which doesn't have a separate
  267. * digest, and only supports one shot.
  268. */
  269. BIO_printf(bio_err, "Key type not supported for this operation\n");
  270. goto end;
  271. }
  272. }
  273. if (mac_name != NULL) {
  274. EVP_PKEY_CTX *mac_ctx = NULL;
  275. int r = 0;
  276. if (!init_gen_str(&mac_ctx, mac_name, impl, 0, NULL, NULL))
  277. goto mac_end;
  278. if (macopts != NULL) {
  279. char *macopt;
  280. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  281. macopt = sk_OPENSSL_STRING_value(macopts, i);
  282. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  283. BIO_printf(bio_err,
  284. "MAC parameter error \"%s\"\n", macopt);
  285. ERR_print_errors(bio_err);
  286. goto mac_end;
  287. }
  288. }
  289. }
  290. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
  291. BIO_puts(bio_err, "Error generating key\n");
  292. ERR_print_errors(bio_err);
  293. goto mac_end;
  294. }
  295. r = 1;
  296. mac_end:
  297. EVP_PKEY_CTX_free(mac_ctx);
  298. if (r == 0)
  299. goto end;
  300. }
  301. if (hmac_key != NULL) {
  302. sigkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, impl,
  303. (unsigned char *)hmac_key,
  304. strlen(hmac_key));
  305. if (sigkey == NULL)
  306. goto end;
  307. }
  308. if (sigkey != NULL) {
  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 != NULL) {
  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 != NULL && sigkey != NULL) {
  355. BIO *sigbio = BIO_new_file(sigfile, "rb");
  356. if (sigbio == NULL) {
  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 (md != NULL)
  378. md_name = EVP_MD_name(md);
  379. if (xoflen > 0) {
  380. if (!(EVP_MD_flags(md) & EVP_MD_FLAG_XOF)) {
  381. BIO_printf(bio_err, "Length can only be specified for XOF\n");
  382. goto end;
  383. }
  384. if (sigkey != NULL) {
  385. BIO_printf(bio_err, "Signing key cannot be specified for XOF\n");
  386. goto end;
  387. }
  388. }
  389. if (argc == 0) {
  390. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  391. ret = do_fp(out, buf, inp, separator, out_bin, xoflen, sigkey, sigbuf,
  392. siglen, NULL, md_name, "stdin");
  393. } else {
  394. const char *sig_name = NULL;
  395. if (!out_bin) {
  396. if (sigkey != NULL)
  397. sig_name = EVP_PKEY_get0_first_alg_name(sigkey);
  398. }
  399. ret = 0;
  400. for (i = 0; i < argc; i++) {
  401. int r;
  402. if (BIO_read_filename(in, argv[i]) <= 0) {
  403. perror(argv[i]);
  404. ret++;
  405. continue;
  406. } else {
  407. r = do_fp(out, buf, inp, separator, out_bin, xoflen,
  408. sigkey, sigbuf, siglen, sig_name, md_name, argv[i]);
  409. }
  410. if (r)
  411. ret = r;
  412. (void)BIO_reset(bmd);
  413. }
  414. }
  415. end:
  416. OPENSSL_clear_free(buf, BUFSIZE);
  417. BIO_free(in);
  418. OPENSSL_free(passin);
  419. BIO_free_all(out);
  420. EVP_PKEY_free(sigkey);
  421. sk_OPENSSL_STRING_free(sigopts);
  422. sk_OPENSSL_STRING_free(macopts);
  423. OPENSSL_free(sigbuf);
  424. BIO_free(bmd);
  425. release_engine(e);
  426. return ret;
  427. }
  428. static void show_digests(const OBJ_NAME *name, void *arg)
  429. {
  430. struct doall_dgst_digests *dec = (struct doall_dgst_digests *)arg;
  431. const EVP_MD *md = NULL;
  432. /* Filter out signed digests (a.k.a signature algorithms) */
  433. if (strstr(name->name, "rsa") != NULL || strstr(name->name, "RSA") != NULL)
  434. return;
  435. if (!islower((unsigned char)*name->name))
  436. return;
  437. /* Filter out message digests that we cannot use */
  438. md = EVP_get_digestbyname(name->name);
  439. if (md == NULL)
  440. return;
  441. BIO_printf(dec->bio, "-%-25s", name->name);
  442. if (++dec->n == 3) {
  443. BIO_printf(dec->bio, "\n");
  444. dec->n = 0;
  445. } else {
  446. BIO_printf(dec->bio, " ");
  447. }
  448. }
  449. /*
  450. * The newline_escape_filename function performs newline escaping for any
  451. * filename that contains a newline. This function also takes a pointer
  452. * to backslash. The backslash pointer is a flag to indicating whether a newline
  453. * is present in the filename. If a newline is present, the backslash flag is
  454. * set and the output format will contain a backslash at the beginning of the
  455. * digest output. This output format is to replicate the output format found
  456. * in the '*sum' checksum programs. This aims to preserve backward
  457. * compatibility.
  458. */
  459. static const char *newline_escape_filename(const char *file, int * backslash)
  460. {
  461. size_t i, e = 0, length = strlen(file), newline_count = 0, mem_len = 0;
  462. char *file_cpy = NULL;
  463. for (i = 0; i < length; i++)
  464. if (file[i] == '\n')
  465. newline_count++;
  466. mem_len = length + newline_count + 1;
  467. file_cpy = app_malloc(mem_len, file);
  468. i = 0;
  469. while(e < length) {
  470. const char c = file[e];
  471. if (c == '\n') {
  472. file_cpy[i++] = '\\';
  473. file_cpy[i++] = 'n';
  474. *backslash = 1;
  475. } else {
  476. file_cpy[i++] = c;
  477. }
  478. e++;
  479. }
  480. file_cpy[i] = '\0';
  481. return (const char*)file_cpy;
  482. }
  483. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, int xoflen,
  484. EVP_PKEY *key, unsigned char *sigin, int siglen,
  485. const char *sig_name, const char *md_name,
  486. const char *file)
  487. {
  488. size_t len = BUFSIZE;
  489. int i, backslash = 0, ret = 1;
  490. unsigned char *allocated_buf = NULL;
  491. while (BIO_pending(bp) || !BIO_eof(bp)) {
  492. i = BIO_read(bp, (char *)buf, BUFSIZE);
  493. if (i < 0) {
  494. BIO_printf(bio_err, "Read Error in %s\n", file);
  495. ERR_print_errors(bio_err);
  496. goto end;
  497. }
  498. if (i == 0)
  499. break;
  500. }
  501. if (sigin != NULL) {
  502. EVP_MD_CTX *ctx;
  503. BIO_get_md_ctx(bp, &ctx);
  504. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  505. if (i > 0) {
  506. BIO_printf(out, "Verified OK\n");
  507. } else if (i == 0) {
  508. BIO_printf(out, "Verification Failure\n");
  509. goto end;
  510. } else {
  511. BIO_printf(bio_err, "Error Verifying Data\n");
  512. ERR_print_errors(bio_err);
  513. goto end;
  514. }
  515. ret = 0;
  516. goto end;
  517. }
  518. if (key != NULL) {
  519. EVP_MD_CTX *ctx;
  520. size_t tmplen;
  521. BIO_get_md_ctx(bp, &ctx);
  522. if (!EVP_DigestSignFinal(ctx, NULL, &tmplen)) {
  523. BIO_printf(bio_err, "Error Signing Data\n");
  524. ERR_print_errors(bio_err);
  525. goto end;
  526. }
  527. if (tmplen > BUFSIZE) {
  528. len = tmplen;
  529. allocated_buf = app_malloc(len, "Signature buffer");
  530. buf = allocated_buf;
  531. }
  532. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  533. BIO_printf(bio_err, "Error Signing Data\n");
  534. ERR_print_errors(bio_err);
  535. goto end;
  536. }
  537. } else if (xoflen > 0) {
  538. EVP_MD_CTX *ctx;
  539. len = xoflen;
  540. if (len > BUFSIZE) {
  541. allocated_buf = app_malloc(len, "Digest buffer");
  542. buf = allocated_buf;
  543. }
  544. BIO_get_md_ctx(bp, &ctx);
  545. if (!EVP_DigestFinalXOF(ctx, buf, len)) {
  546. BIO_printf(bio_err, "Error Digesting Data\n");
  547. ERR_print_errors(bio_err);
  548. goto end;
  549. }
  550. } else {
  551. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  552. if ((int)len < 0) {
  553. ERR_print_errors(bio_err);
  554. goto end;
  555. }
  556. }
  557. if (binout) {
  558. BIO_write(out, buf, len);
  559. } else if (sep == 2) {
  560. file = newline_escape_filename(file, &backslash);
  561. if (backslash == 1)
  562. BIO_puts(out, "\\");
  563. for (i = 0; i < (int)len; i++)
  564. BIO_printf(out, "%02x", buf[i]);
  565. BIO_printf(out, " *%s\n", file);
  566. OPENSSL_free((char *)file);
  567. } else {
  568. if (sig_name != NULL) {
  569. BIO_puts(out, sig_name);
  570. if (md_name != NULL)
  571. BIO_printf(out, "-%s", md_name);
  572. BIO_printf(out, "(%s)= ", file);
  573. } else if (md_name != NULL) {
  574. BIO_printf(out, "%s(%s)= ", md_name, file);
  575. } else {
  576. BIO_printf(out, "(%s)= ", file);
  577. }
  578. for (i = 0; i < (int)len; i++) {
  579. if (sep && (i != 0))
  580. BIO_printf(out, ":");
  581. BIO_printf(out, "%02x", buf[i]);
  582. }
  583. BIO_printf(out, "\n");
  584. }
  585. ret = 0;
  586. end:
  587. if (allocated_buf != NULL)
  588. OPENSSL_clear_free(allocated_buf, len);
  589. return ret;
  590. }