2
0

dgst.c 17 KB

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