dgst.c 19 KB

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