dgst.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. OPTION_CHOICE o;
  84. int separator = 0, debug = 0, keyform = FORMAT_PEM, siglen = 0;
  85. int i, ret = 1, out_bin = -1, want_pub = 0, do_verify = 0;
  86. unsigned char *buf = NULL, *sigbuf = NULL;
  87. int engine_impl = 0;
  88. prog = opt_progname(argv[0]);
  89. buf = app_malloc(BUFSIZE, "I/O buffer");
  90. md = EVP_get_digestbyname(prog);
  91. prog = opt_init(argc, argv, dgst_options);
  92. while ((o = opt_next()) != OPT_EOF) {
  93. switch (o) {
  94. case OPT_EOF:
  95. case OPT_ERR:
  96. opthelp:
  97. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  98. goto end;
  99. case OPT_HELP:
  100. opt_help(dgst_options);
  101. ret = 0;
  102. goto end;
  103. case OPT_C:
  104. separator = 1;
  105. break;
  106. case OPT_R:
  107. separator = 2;
  108. break;
  109. case OPT_R_CASES:
  110. if (!opt_rand(o))
  111. goto end;
  112. break;
  113. case OPT_OUT:
  114. outfile = opt_arg();
  115. break;
  116. case OPT_SIGN:
  117. keyfile = opt_arg();
  118. break;
  119. case OPT_PASSIN:
  120. passinarg = opt_arg();
  121. break;
  122. case OPT_VERIFY:
  123. keyfile = opt_arg();
  124. want_pub = do_verify = 1;
  125. break;
  126. case OPT_PRVERIFY:
  127. keyfile = opt_arg();
  128. do_verify = 1;
  129. break;
  130. case OPT_SIGNATURE:
  131. sigfile = opt_arg();
  132. break;
  133. case OPT_KEYFORM:
  134. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyform))
  135. goto opthelp;
  136. break;
  137. case OPT_ENGINE:
  138. e = setup_engine(opt_arg(), 0);
  139. break;
  140. case OPT_ENGINE_IMPL:
  141. engine_impl = 1;
  142. break;
  143. case OPT_HEX:
  144. out_bin = 0;
  145. break;
  146. case OPT_BINARY:
  147. out_bin = 1;
  148. break;
  149. case OPT_DEBUG:
  150. debug = 1;
  151. break;
  152. case OPT_FIPS_FINGERPRINT:
  153. hmac_key = "etaonrishdlcupfm";
  154. break;
  155. case OPT_HMAC:
  156. hmac_key = opt_arg();
  157. break;
  158. case OPT_MAC:
  159. mac_name = opt_arg();
  160. break;
  161. case OPT_SIGOPT:
  162. if (!sigopts)
  163. sigopts = sk_OPENSSL_STRING_new_null();
  164. if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg()))
  165. goto opthelp;
  166. break;
  167. case OPT_MACOPT:
  168. if (!macopts)
  169. macopts = sk_OPENSSL_STRING_new_null();
  170. if (!macopts || !sk_OPENSSL_STRING_push(macopts, opt_arg()))
  171. goto opthelp;
  172. break;
  173. case OPT_DIGEST:
  174. if (!opt_md(opt_unknown(), &m))
  175. goto opthelp;
  176. md = m;
  177. break;
  178. }
  179. }
  180. argc = opt_num_rest();
  181. argv = opt_rest();
  182. if (keyfile != NULL && argc > 1) {
  183. BIO_printf(bio_err, "%s: Can only sign or verify one file.\n", prog);
  184. goto end;
  185. }
  186. if (do_verify && sigfile == NULL) {
  187. BIO_printf(bio_err,
  188. "No signature to verify: use the -signature option\n");
  189. goto end;
  190. }
  191. if (engine_impl)
  192. impl = e;
  193. in = BIO_new(BIO_s_file());
  194. bmd = BIO_new(BIO_f_md());
  195. if ((in == NULL) || (bmd == NULL)) {
  196. ERR_print_errors(bio_err);
  197. goto end;
  198. }
  199. if (debug) {
  200. BIO_set_callback(in, BIO_debug_callback);
  201. /* needed for windows 3.1 */
  202. BIO_set_callback_arg(in, (char *)bio_err);
  203. }
  204. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  205. BIO_printf(bio_err, "Error getting password\n");
  206. goto end;
  207. }
  208. if (out_bin == -1) {
  209. if (keyfile != NULL)
  210. out_bin = 1;
  211. else
  212. out_bin = 0;
  213. }
  214. out = bio_open_default(outfile, 'w', out_bin ? FORMAT_BINARY : FORMAT_TEXT);
  215. if (out == NULL)
  216. goto end;
  217. if ((!(mac_name == NULL) + !(keyfile == NULL) + !(hmac_key == NULL)) > 1) {
  218. BIO_printf(bio_err, "MAC and Signing key cannot both be specified\n");
  219. goto end;
  220. }
  221. if (keyfile != NULL) {
  222. int type;
  223. if (want_pub)
  224. sigkey = load_pubkey(keyfile, keyform, 0, NULL, e, "key file");
  225. else
  226. sigkey = load_key(keyfile, keyform, 0, passin, e, "key file");
  227. if (sigkey == NULL) {
  228. /*
  229. * load_[pub]key() has already printed an appropriate message
  230. */
  231. goto end;
  232. }
  233. type = EVP_PKEY_id(sigkey);
  234. if (type == EVP_PKEY_ED25519 || type == EVP_PKEY_ED448) {
  235. /*
  236. * We implement PureEdDSA for these which doesn't have a separate
  237. * digest, and only supports one shot.
  238. */
  239. BIO_printf(bio_err, "Key type not supported for this operation\n");
  240. goto end;
  241. }
  242. }
  243. if (mac_name != NULL) {
  244. EVP_PKEY_CTX *mac_ctx = NULL;
  245. int r = 0;
  246. if (!init_gen_str(&mac_ctx, mac_name, impl, 0))
  247. goto mac_end;
  248. if (macopts != NULL) {
  249. char *macopt;
  250. for (i = 0; i < sk_OPENSSL_STRING_num(macopts); i++) {
  251. macopt = sk_OPENSSL_STRING_value(macopts, i);
  252. if (pkey_ctrl_string(mac_ctx, macopt) <= 0) {
  253. BIO_printf(bio_err,
  254. "MAC parameter error \"%s\"\n", macopt);
  255. ERR_print_errors(bio_err);
  256. goto mac_end;
  257. }
  258. }
  259. }
  260. if (EVP_PKEY_keygen(mac_ctx, &sigkey) <= 0) {
  261. BIO_puts(bio_err, "Error generating key\n");
  262. ERR_print_errors(bio_err);
  263. goto mac_end;
  264. }
  265. r = 1;
  266. mac_end:
  267. EVP_PKEY_CTX_free(mac_ctx);
  268. if (r == 0)
  269. goto end;
  270. }
  271. if (hmac_key != NULL) {
  272. sigkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, impl,
  273. (unsigned char *)hmac_key, -1);
  274. if (sigkey == NULL)
  275. goto end;
  276. }
  277. if (sigkey != NULL) {
  278. EVP_MD_CTX *mctx = NULL;
  279. EVP_PKEY_CTX *pctx = NULL;
  280. int r;
  281. if (!BIO_get_md_ctx(bmd, &mctx)) {
  282. BIO_printf(bio_err, "Error getting context\n");
  283. ERR_print_errors(bio_err);
  284. goto end;
  285. }
  286. if (do_verify)
  287. r = EVP_DigestVerifyInit(mctx, &pctx, md, impl, sigkey);
  288. else
  289. r = EVP_DigestSignInit(mctx, &pctx, md, impl, sigkey);
  290. if (!r) {
  291. BIO_printf(bio_err, "Error setting context\n");
  292. ERR_print_errors(bio_err);
  293. goto end;
  294. }
  295. if (sigopts != NULL) {
  296. char *sigopt;
  297. for (i = 0; i < sk_OPENSSL_STRING_num(sigopts); i++) {
  298. sigopt = sk_OPENSSL_STRING_value(sigopts, i);
  299. if (pkey_ctrl_string(pctx, sigopt) <= 0) {
  300. BIO_printf(bio_err, "parameter error \"%s\"\n", sigopt);
  301. ERR_print_errors(bio_err);
  302. goto end;
  303. }
  304. }
  305. }
  306. }
  307. /* we use md as a filter, reading from 'in' */
  308. else {
  309. EVP_MD_CTX *mctx = NULL;
  310. if (!BIO_get_md_ctx(bmd, &mctx)) {
  311. BIO_printf(bio_err, "Error getting context\n");
  312. ERR_print_errors(bio_err);
  313. goto end;
  314. }
  315. if (md == NULL)
  316. md = EVP_sha256();
  317. if (!EVP_DigestInit_ex(mctx, md, impl)) {
  318. BIO_printf(bio_err, "Error setting digest\n");
  319. ERR_print_errors(bio_err);
  320. goto end;
  321. }
  322. }
  323. if (sigfile != NULL && sigkey != NULL) {
  324. BIO *sigbio = BIO_new_file(sigfile, "rb");
  325. if (sigbio == NULL) {
  326. BIO_printf(bio_err, "Error opening signature file %s\n", sigfile);
  327. ERR_print_errors(bio_err);
  328. goto end;
  329. }
  330. siglen = EVP_PKEY_size(sigkey);
  331. sigbuf = app_malloc(siglen, "signature buffer");
  332. siglen = BIO_read(sigbio, sigbuf, siglen);
  333. BIO_free(sigbio);
  334. if (siglen <= 0) {
  335. BIO_printf(bio_err, "Error reading signature file %s\n", sigfile);
  336. ERR_print_errors(bio_err);
  337. goto end;
  338. }
  339. }
  340. inp = BIO_push(bmd, in);
  341. if (md == NULL) {
  342. EVP_MD_CTX *tctx;
  343. BIO_get_md_ctx(bmd, &tctx);
  344. md = EVP_MD_CTX_md(tctx);
  345. }
  346. if (argc == 0) {
  347. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  348. ret = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  349. siglen, NULL, NULL, "stdin");
  350. } else {
  351. const char *md_name = NULL, *sig_name = NULL;
  352. if (!out_bin) {
  353. if (sigkey != NULL) {
  354. const EVP_PKEY_ASN1_METHOD *ameth;
  355. ameth = EVP_PKEY_get0_asn1(sigkey);
  356. if (ameth)
  357. EVP_PKEY_asn1_get0_info(NULL, NULL,
  358. NULL, NULL, &sig_name, ameth);
  359. }
  360. if (md != NULL)
  361. md_name = EVP_MD_name(md);
  362. }
  363. ret = 0;
  364. for (i = 0; i < argc; i++) {
  365. int r;
  366. if (BIO_read_filename(in, argv[i]) <= 0) {
  367. perror(argv[i]);
  368. ret++;
  369. continue;
  370. } else {
  371. r = do_fp(out, buf, inp, separator, out_bin, sigkey, sigbuf,
  372. siglen, sig_name, md_name, argv[i]);
  373. }
  374. if (r)
  375. ret = r;
  376. (void)BIO_reset(bmd);
  377. }
  378. }
  379. end:
  380. OPENSSL_clear_free(buf, BUFSIZE);
  381. BIO_free(in);
  382. OPENSSL_free(passin);
  383. BIO_free_all(out);
  384. EVP_PKEY_free(sigkey);
  385. sk_OPENSSL_STRING_free(sigopts);
  386. sk_OPENSSL_STRING_free(macopts);
  387. OPENSSL_free(sigbuf);
  388. BIO_free(bmd);
  389. release_engine(e);
  390. return ret;
  391. }
  392. int do_fp(BIO *out, unsigned char *buf, BIO *bp, int sep, int binout,
  393. EVP_PKEY *key, unsigned char *sigin, int siglen,
  394. const char *sig_name, const char *md_name,
  395. const char *file)
  396. {
  397. size_t len;
  398. int i;
  399. for (;;) {
  400. i = BIO_read(bp, (char *)buf, BUFSIZE);
  401. if (i < 0) {
  402. BIO_printf(bio_err, "Read Error in %s\n", file);
  403. ERR_print_errors(bio_err);
  404. return 1;
  405. }
  406. if (i == 0)
  407. break;
  408. }
  409. if (sigin != NULL) {
  410. EVP_MD_CTX *ctx;
  411. BIO_get_md_ctx(bp, &ctx);
  412. i = EVP_DigestVerifyFinal(ctx, sigin, (unsigned int)siglen);
  413. if (i > 0) {
  414. BIO_printf(out, "Verified OK\n");
  415. } else if (i == 0) {
  416. BIO_printf(out, "Verification Failure\n");
  417. return 1;
  418. } else {
  419. BIO_printf(bio_err, "Error Verifying Data\n");
  420. ERR_print_errors(bio_err);
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. if (key != NULL) {
  426. EVP_MD_CTX *ctx;
  427. BIO_get_md_ctx(bp, &ctx);
  428. len = BUFSIZE;
  429. if (!EVP_DigestSignFinal(ctx, buf, &len)) {
  430. BIO_printf(bio_err, "Error Signing Data\n");
  431. ERR_print_errors(bio_err);
  432. return 1;
  433. }
  434. } else {
  435. len = BIO_gets(bp, (char *)buf, BUFSIZE);
  436. if ((int)len < 0) {
  437. ERR_print_errors(bio_err);
  438. return 1;
  439. }
  440. }
  441. if (binout) {
  442. BIO_write(out, buf, len);
  443. } else if (sep == 2) {
  444. for (i = 0; i < (int)len; i++)
  445. BIO_printf(out, "%02x", buf[i]);
  446. BIO_printf(out, " *%s\n", file);
  447. } else {
  448. if (sig_name != NULL) {
  449. BIO_puts(out, sig_name);
  450. if (md_name != NULL)
  451. BIO_printf(out, "-%s", md_name);
  452. BIO_printf(out, "(%s)= ", file);
  453. } else if (md_name != NULL) {
  454. BIO_printf(out, "%s(%s)= ", md_name, file);
  455. } else {
  456. BIO_printf(out, "(%s)= ", file);
  457. }
  458. for (i = 0; i < (int)len; i++) {
  459. if (sep && (i != 0))
  460. BIO_printf(out, ":");
  461. BIO_printf(out, "%02x", buf[i]);
  462. }
  463. BIO_printf(out, "\n");
  464. }
  465. return 0;
  466. }