dgst.c 20 KB

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