2
0

dgst.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. * Copyright 1995-2023 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. if (sigkey == NULL)
  296. goto end;
  297. }
  298. if (hmac_key != NULL) {
  299. if (md == NULL) {
  300. md = (EVP_MD *)EVP_sha256();
  301. digestname = SN_sha256;
  302. }
  303. sigkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, impl,
  304. (unsigned char *)hmac_key,
  305. strlen(hmac_key));
  306. if (sigkey == NULL)
  307. goto end;
  308. }
  309. if (sigkey != NULL) {
  310. EVP_MD_CTX *mctx = NULL;
  311. EVP_PKEY_CTX *pctx = NULL;
  312. int res;
  313. if (BIO_get_md_ctx(bmd, &mctx) <= 0) {
  314. BIO_printf(bio_err, "Error getting context\n");
  315. goto end;
  316. }
  317. if (do_verify)
  318. if (impl == NULL)
  319. res = EVP_DigestVerifyInit_ex(mctx, &pctx, digestname,
  320. app_get0_libctx(),
  321. app_get0_propq(), sigkey, NULL);
  322. else
  323. res = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  324. else
  325. if (impl == NULL)
  326. res = EVP_DigestSignInit_ex(mctx, &pctx, digestname,
  327. app_get0_libctx(),
  328. app_get0_propq(), sigkey, NULL);
  329. else
  330. res = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  331. if (res == 0) {
  332. BIO_printf(bio_err, "Error setting context\n");
  333. goto end;
  334. }
  335. if (sigopts != NULL) {
  336. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  337. char *sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  338. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  339. BIO_printf(bio_err, "Signature parameter error \"%s\"\n",
  340. sigopt);
  341. goto end;
  342. }
  343. }
  344. }
  345. }
  346. /* we use md as a filter, reading from 'in' */
  347. else {
  348. EVP_MD_CTX *mctx = NULL;
  349. if (BIO_get_md_ctx(bmd, &mctx) <= 0) {
  350. BIO_printf(bio_err, "Error getting context\n");
  351. goto end;
  352. }
  353. if (md == NULL)
  354. md = (EVP_MD *)EVP_sha256();
  355. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  356. BIO_printf(bio_err, "Error setting digest\n");
  357. goto end;
  358. }
  359. }
  360. if (sigfile != NULL && sigkey != NULL) {
  361. BIO *sigbio = BIO_new_file(sigfile, "rb");
  362. if (sigbio == NULL) {
  363. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  364. goto end;
  365. }
  366. siglen = EVP_PKEY_get_size(sigkey);
  367. sigbuf = app_malloc(siglen, "signature buffer");
  368. siglen = BIO_read(sigbio, sigbuf, siglen);
  369. BIO_free(sigbio);
  370. if (siglen <= 0) {
  371. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  372. goto end;
  373. }
  374. }
  375. inp = BIO_push(bmd, in);
  376. if (md == NULL) {
  377. EVP_MD_CTX *tctx;
  378. BIO_get_md_ctx(bmd, &tctx);
  379. md = EVP_MD_CTX_get1_md(tctx);
  380. }
  381. if (md != NULL)
  382. md_name = EVP_MD_get0_name(md);
  383. if (xoflen > 0) {
  384. if (!(EVP_MD_get_flags(md) & EVP_MD_FLAG_XOF)) {
  385. BIO_printf(bio_err, "Length can only be specified for XOF\n");
  386. goto end;
  387. }
  388. /*
  389. * Signing using XOF is not supported by any algorithms currently since
  390. * each algorithm only calls EVP_DigestFinal_ex() in their sign_final
  391. * and verify_final methods.
  392. */
  393. if (sigkey != NULL) {
  394. BIO_printf(bio_err, "Signing key cannot be specified for XOF\n");
  395. goto end;
  396. }
  397. }
  398. if (argc == 0) {
  399. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  400. ret = do_fp(out, buf, inp, separator, out_bin, xoflen, sigkey, sigbuf,
  401. siglen, NULL, md_name, "stdin");
  402. } else {
  403. const char *sig_name = NULL;
  404. if (out_bin == 0) {
  405. if (sigkey != NULL)
  406. sig_name = EVP_PKEY_get0_type_name(sigkey);
  407. }
  408. ret = EXIT_SUCCESS;
  409. for (i = 0; i < argc; i++) {
  410. if (BIO_read_filename(in, argv[i]) <= 0) {
  411. perror(argv[i]);
  412. ret = EXIT_FAILURE;
  413. continue;
  414. } else {
  415. if (do_fp(out, buf, inp, separator, out_bin, xoflen,
  416. sigkey, sigbuf, siglen, sig_name, md_name, argv[i]))
  417. ret = EXIT_FAILURE;
  418. }
  419. (void)BIO_reset(bmd);
  420. }
  421. }
  422. end:
  423. if (ret != EXIT_SUCCESS)
  424. ERR_print_errors(bio_err);
  425. OPENSSL_clear_free(buf, BUFSIZE);
  426. BIO_free(in);
  427. OPENSSL_free(passin);
  428. BIO_free_all(out);
  429. EVP_MD_free(md);
  430. EVP_PKEY_free(sigkey);
  431. sk_OPENSSL_STRING_free(sigopts);
  432. sk_OPENSSL_STRING_free(macopts);
  433. OPENSSL_free(sigbuf);
  434. BIO_free(bmd);
  435. release_engine(e);
  436. return ret;
  437. }
  438. static void show_digests(const OBJ_NAME *name, void *arg)
  439. {
  440. struct doall_dgst_digests *dec = (struct doall_dgst_digests *)arg;
  441. const EVP_MD *md = NULL;
  442. /* Filter out signed digests (a.k.a signature algorithms) */
  443. if (strstr(name->name, "rsa") != NULL || strstr(name->name, "RSA") != NULL)
  444. return;
  445. if (!islower((unsigned char)*name->name))
  446. return;
  447. /* Filter out message digests that we cannot use */
  448. md = EVP_MD_fetch(app_get0_libctx(), name->name, app_get0_propq());
  449. if (md == NULL) {
  450. md = EVP_get_digestbyname(name->name);
  451. if (md == NULL)
  452. return;
  453. }
  454. BIO_printf(dec->bio, "-%-25s", name->name);
  455. if (++dec->n == 3) {
  456. BIO_printf(dec->bio, "\n");
  457. dec->n = 0;
  458. } else {
  459. BIO_printf(dec->bio, " ");
  460. }
  461. }
  462. /*
  463. * The newline_escape_filename function performs newline escaping for any
  464. * filename that contains a newline. This function also takes a pointer
  465. * to backslash. The backslash pointer is a flag to indicating whether a newline
  466. * is present in the filename. If a newline is present, the backslash flag is
  467. * set and the output format will contain a backslash at the beginning of the
  468. * digest output. This output format is to replicate the output format found
  469. * in the '*sum' checksum programs. This aims to preserve backward
  470. * compatibility.
  471. */
  472. static const char *newline_escape_filename(const char *file, int *backslash)
  473. {
  474. size_t i, e = 0, length = strlen(file), newline_count = 0, mem_len = 0;
  475. char *file_cpy = NULL;
  476. for (i = 0; i < length; i++)
  477. if (file[i] == '\n')
  478. newline_count++;
  479. mem_len = length + newline_count + 1;
  480. file_cpy = app_malloc(mem_len, file);
  481. i = 0;
  482. while (e < length) {
  483. const char c = file[e];
  484. if (c == '\n') {
  485. file_cpy[i++] = '\\';
  486. file_cpy[i++] = 'n';
  487. *backslash = 1;
  488. } else {
  489. file_cpy[i++] = c;
  490. }
  491. e++;
  492. }
  493. file_cpy[i] = '\0';
  494. return (const char*)file_cpy;
  495. }
  496. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout, int xoflen,
  497. EVP_PKEY *key, unsigned char *sigin, int siglen,
  498. const char *sig_name, const char *md_name,
  499. const char *file)
  500. {
  501. size_t len = BUFSIZE;
  502. int i, backslash = 0, ret = EXIT_FAILURE;
  503. unsigned char *allocated_buf = NULL;
  504. while (BIO_pending(bp) || !BIO_eof(bp)) {
  505. i = BIO_read(bp, (char *)buf, BUFSIZE);
  506. if (i < 0) {
  507. BIO_printf(bio_err, "Read error in %s\n", file);
  508. goto end;
  509. }
  510. if (i == 0)
  511. break;
  512. }
  513. if (sigin != NULL) {
  514. EVP_MD_CTX *ctx;
  515. BIO_get_md_ctx(bp, &ctx);
  516. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  517. if (i > 0) {
  518. BIO_printf(out, "Verified OK\n");
  519. } else if (i == 0) {
  520. BIO_printf(out, "Verification failure\n");
  521. goto end;
  522. } else {
  523. BIO_printf(bio_err, "Error verifying data\n");
  524. goto end;
  525. }
  526. ret = EXIT_SUCCESS;
  527. goto end;
  528. }
  529. if (key != NULL) {
  530. EVP_MD_CTX *ctx;
  531. size_t tmplen;
  532. BIO_get_md_ctx(bp, &ctx);
  533. if (!EVP_DigestSignFinal(ctx, NULL, &tmplen)) {
  534. BIO_printf(bio_err, "Error getting maximum length of signed data\n");
  535. goto end;
  536. }
  537. if (tmplen > BUFSIZE) {
  538. len = tmplen;
  539. allocated_buf = app_malloc(len, "Signature buffer");
  540. buf = allocated_buf;
  541. }
  542. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  543. BIO_printf(bio_err, "Error signing data\n");
  544. goto end;
  545. }
  546. } else if (xoflen > 0) {
  547. EVP_MD_CTX *ctx;
  548. len = xoflen;
  549. if (len > BUFSIZE) {
  550. allocated_buf = app_malloc(len, "Digest buffer");
  551. buf = allocated_buf;
  552. }
  553. BIO_get_md_ctx(bp, &ctx);
  554. if (!EVP_DigestFinalXOF(ctx, buf, len)) {
  555. BIO_printf(bio_err, "Error Digesting Data\n");
  556. goto end;
  557. }
  558. } else {
  559. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  560. if ((int)len < 0)
  561. goto end;
  562. }
  563. if (binout) {
  564. BIO_write(out, buf, len);
  565. } else if (sep == 2) {
  566. file = newline_escape_filename(file, &backslash);
  567. if (backslash == 1)
  568. BIO_puts(out, "\\");
  569. for (i = 0; i < (int)len; i++)
  570. BIO_printf(out, "%02x", buf[i]);
  571. BIO_printf(out, " *%s\n", file);
  572. OPENSSL_free((char *)file);
  573. } else {
  574. if (sig_name != NULL) {
  575. BIO_puts(out, sig_name);
  576. if (md_name != NULL)
  577. BIO_printf(out, "-%s", md_name);
  578. BIO_printf(out, "(%s)= ", file);
  579. } else if (md_name != NULL) {
  580. BIO_printf(out, "%s(%s)= ", md_name, file);
  581. } else {
  582. BIO_printf(out, "(%s)= ", file);
  583. }
  584. for (i = 0; i < (int)len; i++) {
  585. if (sep && (i != 0))
  586. BIO_printf(out, ":");
  587. BIO_printf(out, "%02x", buf[i]);
  588. }
  589. BIO_printf(out, "\n");
  590. }
  591. ret = EXIT_SUCCESS;
  592. end:
  593. if (allocated_buf != NULL)
  594. OPENSSL_clear_free(allocated_buf, len);
  595. return ret;
  596. }