dgst.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. 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 = EXIT_SUCCESS;
  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 = EXIT_SUCCESS;
  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. digestname = opt_unknown();
  200. break;
  201. case OPT_PROV_CASES:
  202. if (!opt_provider(o))
  203. goto end;
  204. break;
  205. }
  206. }
  207. /* Remaining args are files to digest. */
  208. argc = opt_num_rest();
  209. argv = opt_rest();
  210. if (keyfile != NULL && argc > 1) {
  211. BIO_printf(bio_err, "%s: Can only sign or verify one file.\n", prog);
  212. goto end;
  213. }
  214. if (!app_RAND_load())
  215. goto end;
  216. if (digestname != NULL) {
  217. if (!opt_md(digestname, &md))
  218. goto opthelp;
  219. }
  220. if (do_verify && sigfile == NULL) {
  221. BIO_printf(bio_err,
  222. "No signature to verify: use the -signature option\n");
  223. goto end;
  224. }
  225. if (engine_impl)
  226. impl = e;
  227. in = BIO_new(BIO_s_file());
  228. bmd = BIO_new(BIO_f_md());
  229. if (in == NULL || bmd == NULL)
  230. goto end;
  231. if (debug) {
  232. BIO_set_callback_ex(in, BIO_debug_callback_ex);
  233. /* needed for windows 3.1 */
  234. BIO_set_callback_arg(in, (char *)bio_err);
  235. }
  236. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  237. BIO_printf(bio_err, "Error getting password\n");
  238. goto end;
  239. }
  240. if (out_bin == -1) {
  241. if (keyfile != NULL)
  242. out_bin = 1;
  243. else
  244. out_bin = 0;
  245. }
  246. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  247. if (out == NULL)
  248. goto end;
  249. if ((!(mac_name == NULL) + !(keyfile == NULL) + !(hmac_key == NULL)) > 1) {
  250. BIO_printf(bio_err, "MAC and signing key cannot both be specified\n");
  251. goto end;
  252. }
  253. if (keyfile != NULL) {
  254. int type;
  255. if (want_pub)
  256. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "public key");
  257. else
  258. sigkey = load_key(keyfile, keyform, 0, passin, e, "private key");
  259. if (sigkey == NULL) {
  260. /*
  261. * load_[pub]key() has already printed an appropriate message
  262. */
  263. goto end;
  264. }
  265. type = EVP_PKEY_get_id(sigkey);
  266. if (type == EVP_PKEY_ED25519 || type == EVP_PKEY_ED448) {
  267. /*
  268. * We implement PureEdDSA for these which doesn't have a separate
  269. * digest, and only supports one shot.
  270. */
  271. BIO_printf(bio_err, "Key type not supported for this operation\n");
  272. goto end;
  273. }
  274. }
  275. if (mac_name != NULL) {
  276. EVP_PKEY_CTX *mac_ctx = NULL;
  277. if (!init_gen_str(&mac_ctx, mac_name, impl, 0, NULL, NULL))
  278. goto end;
  279. if (macopts != NULL) {
  280. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  281. char *macopt = sk_OPENSSL_STRING_value(macopts, i);
  282. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  283. EVP_PKEY_CTX_free(mac_ctx);
  284. BIO_printf(bio_err, "MAC parameter error \"%s\"\n", macopt);
  285. goto end;
  286. }
  287. }
  288. }
  289. sigkey = app_keygen(mac_ctx, mac_name, 0, 0 /* not verbose */);
  290. /* Verbose output would make external-tests gost-engine fail */
  291. EVP_PKEY_CTX_free(mac_ctx);
  292. }
  293. if (hmac_key != NULL) {
  294. if (md == NULL)
  295. md = (EVP_MD *)EVP_sha256();
  296. sigkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, impl,
  297. (unsigned char *)hmac_key,
  298. strlen(hmac_key));
  299. if (sigkey == NULL)
  300. goto end;
  301. }
  302. if (sigkey != NULL) {
  303. EVP_MD_CTX *mctx = NULL;
  304. EVP_PKEY_CTX *pctx = NULL;
  305. int res;
  306. if (!BIO_get_md_ctx(bmd, &mctx)) {
  307. BIO_printf(bio_err, "Error getting context\n");
  308. goto end;
  309. }
  310. if (do_verify)
  311. res = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  312. else
  313. res = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  314. if (res == 0) {
  315. BIO_printf(bio_err, "Error setting context\n");
  316. goto end;
  317. }
  318. if (sigopts != NULL) {
  319. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  320. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  321. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  322. BIO_printf(bio_err, "Signature parameter error \"%s\"\n",
  323. sigopt);
  324. goto end;
  325. }
  326. }
  327. }
  328. }
  329. /* we use md as a filter, reading from 'in' */
  330. else {
  331. EVP_MD_CTX *mctx = NULL;
  332. if (!BIO_get_md_ctx(bmd, &mctx)) {
  333. BIO_printf(bio_err, "Error getting context\n");
  334. goto end;
  335. }
  336. if (md == NULL)
  337. md = (EVP_MD *)EVP_sha256();
  338. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  339. BIO_printf(bio_err, "Error setting digest\n");
  340. goto end;
  341. }
  342. }
  343. if (sigfile != NULL && sigkey != NULL) {
  344. BIO *sigbio = BIO_new_file(sigfile, "rb");
  345. if (sigbio == NULL) {
  346. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  347. goto end;
  348. }
  349. siglen = EVP_PKEY_get_size(sigkey);
  350. sigbuf = app_malloc(siglen, "signature buffer");
  351. siglen = BIO_read(sigbio, sigbuf, siglen);
  352. BIO_free(sigbio);
  353. if (siglen <= 0) {
  354. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  355. goto end;
  356. }
  357. }
  358. inp = BIO_push(bmd, in);
  359. if (md == NULL) {
  360. EVP_MD_CTX *tctx;
  361. BIO_get_md_ctx(bmd, &tctx);
  362. md = EVP_MD_CTX_get1_md(tctx);
  363. }
  364. if (md != NULL)
  365. md_name = EVP_MD_get0_name(md);
  366. if (xoflen > 0) {
  367. if (!(EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF)) {
  368. BIO_printf(bio_err, "Length can only be specified for XOF\n");
  369. goto end;
  370. }
  371. if (sigkey != NULL) {
  372. BIO_printf(bio_err, "Signing key cannot be specified for XOF\n");
  373. goto end;
  374. }
  375. }
  376. if (argc == 0) {
  377. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  378. ret = do_fp(out, buf, inp, separator, out_bin, xoflen, sigkey, sigbuf,
  379. siglen, NULL, md_name, "stdin");
  380. } else {
  381. const char *sig_name = NULL;
  382. if (out_bin == 0) {
  383. if (sigkey != NULL)
  384. sig_name = EVP_PKEY_get0_type_name(sigkey);
  385. }
  386. ret = EXIT_SUCCESS;
  387. for (i = 0; i < argc; i++) {
  388. if (BIO_read_filename(in, argv[i]) <= 0) {
  389. perror(argv[i]);
  390. ret = EXIT_FAILURE;
  391. continue;
  392. } else {
  393. if (do_fp(out, buf, inp, separator, out_bin, xoflen,
  394. sigkey, sigbuf, siglen, sig_name, md_name, argv[i]))
  395. ret = EXIT_FAILURE;
  396. }
  397. (void)BIO_reset(bmd);
  398. }
  399. }
  400. end:
  401. if (ret != EXIT_SUCCESS)
  402. ERR_print_errors(bio_err);
  403. OPENSSL_clear_free(buf, BUFSIZE);
  404. BIO_free(in);
  405. OPENSSL_free(passin);
  406. BIO_free_all(out);
  407. EVP_MD_free(md);
  408. EVP_PKEY_free(sigkey);
  409. sk_OPENSSL_STRING_free(sigopts);
  410. sk_OPENSSL_STRING_free(macopts);
  411. OPENSSL_free(sigbuf);
  412. BIO_free(bmd);
  413. release_engine(e);
  414. return ret;
  415. }
  416. static void show_digests(const OBJ_NAME *name, void *arg)
  417. {
  418. struct doall_dgst_digests *dec = (struct doall_dgst_digests *)arg;
  419. const EVP_MD *md = NULL;
  420. /* Filter out signed digests (a.k.a signature algorithms) */
  421. if (strstr(name->name, "rsa") != NULL || strstr(name->name, "RSA") != NULL)
  422. return;
  423. if (!islower((unsigned char)*name->name))
  424. return;
  425. /* Filter out message digests that we cannot use */
  426. md = EVP_get_digestbyname(name->name);
  427. if (md == NULL)
  428. return;
  429. BIO_printf(dec->bio, "-%-25s", name->name);
  430. if (++dec->n == 3) {
  431. BIO_printf(dec->bio, "\n");
  432. dec->n = 0;
  433. } else {
  434. BIO_printf(dec->bio, " ");
  435. }
  436. }
  437. /*
  438. * The newline_escape_filename function performs newline escaping for any
  439. * filename that contains a newline. This function also takes a pointer
  440. * to backslash. The backslash pointer is a flag to indicating whether a newline
  441. * is present in the filename. If a newline is present, the backslash flag is
  442. * set and the output format will contain a backslash at the beginning of the
  443. * digest output. This output format is to replicate the output format found
  444. * in the '*sum' checksum programs. This aims to preserve backward
  445. * compatibility.
  446. */
  447. static const char *newline_escape_filename(const char *file, int * backslash)
  448. {
  449. size_t i, e = 0, length = strlen(file), newline_count = 0, mem_len = 0;
  450. char *file_cpy = NULL;
  451. for (i = 0; i < length; i++)
  452. if (file[i] == '\n')
  453. newline_count++;
  454. mem_len = length + newline_count + 1;
  455. file_cpy = app_malloc(mem_len, file);
  456. i = 0;
  457. while(e < length) {
  458. const char c = file[e];
  459. if (c == '\n') {
  460. file_cpy[i++] = '\\';
  461. file_cpy[i++] = 'n';
  462. *backslash = 1;
  463. } else {
  464. file_cpy[i++] = c;
  465. }
  466. e++;
  467. }
  468. file_cpy[i] = '\0';
  469. return (const char*)file_cpy;
  470. }
  471. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, int xoflen,
  472. EVP_PKEY *key, unsigned char *sigin, int siglen,
  473. const char *sig_name, const char *md_name,
  474. const char *file)
  475. {
  476. size_t len = BUFSIZE;
  477. int i, backslash = 0, ret = EXIT_FAILURE;
  478. unsigned char *allocated_buf = NULL;
  479. while (BIO_pending(bp) || !BIO_eof(bp)) {
  480. i = BIO_read(bp, (char *)buf, BUFSIZE);
  481. if (i < 0) {
  482. BIO_printf(bio_err, "Read error in %s\n", file);
  483. goto end;
  484. }
  485. if (i == 0)
  486. break;
  487. }
  488. if (sigin != NULL) {
  489. EVP_MD_CTX *ctx;
  490. BIO_get_md_ctx(bp, &ctx);
  491. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  492. if (i > 0) {
  493. BIO_printf(out, "Verified OK\n");
  494. } else if (i == 0) {
  495. BIO_printf(out, "Verification failure\n");
  496. goto end;
  497. } else {
  498. BIO_printf(bio_err, "Error verifying data\n");
  499. goto end;
  500. }
  501. ret = EXIT_SUCCESS;
  502. goto end;
  503. }
  504. if (key != NULL) {
  505. EVP_MD_CTX *ctx;
  506. size_t tmplen;
  507. BIO_get_md_ctx(bp, &ctx);
  508. if (!EVP_DigestSignFinal(ctx, NULL, &tmplen)) {
  509. BIO_printf(bio_err, "Error getting maximum length of signed data\n");
  510. goto end;
  511. }
  512. if (tmplen > BUFSIZE) {
  513. len = tmplen;
  514. allocated_buf = app_malloc(len, "Signature buffer");
  515. buf = allocated_buf;
  516. }
  517. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  518. BIO_printf(bio_err, "Error signing data\n");
  519. goto end;
  520. }
  521. } else if (xoflen > 0) {
  522. EVP_MD_CTX *ctx;
  523. len = xoflen;
  524. if (len > BUFSIZE) {
  525. allocated_buf = app_malloc(len, "Digest buffer");
  526. buf = allocated_buf;
  527. }
  528. BIO_get_md_ctx(bp, &ctx);
  529. if (!EVP_DigestFinalXOF(ctx, buf, len)) {
  530. BIO_printf(bio_err, "Error Digesting Data\n");
  531. goto end;
  532. }
  533. } else {
  534. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  535. if ((int)len < 0)
  536. goto end;
  537. }
  538. if (binout) {
  539. BIO_write(out, buf, len);
  540. } else if (sep == 2) {
  541. file = newline_escape_filename(file, &backslash);
  542. if (backslash == 1)
  543. BIO_puts(out, "\\");
  544. for (i = 0; i < (int)len; i++)
  545. BIO_printf(out, "%02x", buf[i]);
  546. BIO_printf(out, " *%s\n", file);
  547. OPENSSL_free((char *)file);
  548. } else {
  549. if (sig_name != NULL) {
  550. BIO_puts(out, sig_name);
  551. if (md_name != NULL)
  552. BIO_printf(out, "-%s", md_name);
  553. BIO_printf(out, "(%s)= ", file);
  554. } else if (md_name != NULL) {
  555. BIO_printf(out, "%s(%s)= ", md_name, file);
  556. } else {
  557. BIO_printf(out, "(%s)= ", file);
  558. }
  559. for (i = 0; i < (int)len; i++) {
  560. if (sep && (i != 0))
  561. BIO_printf(out, ":");
  562. BIO_printf(out, "%02x", buf[i]);
  563. }
  564. BIO_printf(out, "\n");
  565. }
  566. ret = EXIT_SUCCESS;
  567. end:
  568. if (allocated_buf != NULL)
  569. OPENSSL_clear_free(allocated_buf, len);
  570. return ret;
  571. }