tpm_extendpcr.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (C) 2017 Andreas Steffen
  3. * HSR Hochschule fuer Technik Rapperswil
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <tpm_tss.h>
  16. #include <library.h>
  17. #include <crypto/hashers/hasher.h>
  18. #include <utils/debug.h>
  19. #include <syslog.h>
  20. #include <getopt.h>
  21. #include <errno.h>
  22. /* logging */
  23. static bool log_to_stderr = TRUE;
  24. static bool log_to_syslog = TRUE;
  25. static level_t default_loglevel = 1;
  26. /* global variables */
  27. tpm_tss_t *tpm;
  28. chunk_t digest;
  29. chunk_t pcr_value;
  30. /**
  31. * logging function for tpm_extendpcr
  32. */
  33. static void tpm_extendpcr_dbg(debug_t group, level_t level, char *fmt, ...)
  34. {
  35. char buffer[8192];
  36. char *current = buffer, *next;
  37. va_list args;
  38. if (level <= default_loglevel)
  39. {
  40. if (log_to_stderr)
  41. {
  42. va_start(args, fmt);
  43. vfprintf(stderr, fmt, args);
  44. va_end(args);
  45. fprintf(stderr, "\n");
  46. }
  47. if (log_to_syslog)
  48. {
  49. /* write in memory buffer first */
  50. va_start(args, fmt);
  51. vsnprintf(buffer, sizeof(buffer), fmt, args);
  52. va_end(args);
  53. /* do a syslog with every line */
  54. while (current)
  55. {
  56. next = strchr(current, '\n');
  57. if (next)
  58. {
  59. *(next++) = '\0';
  60. }
  61. syslog(LOG_INFO, "%s\n", current);
  62. current = next;
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. * Initialize logging to stderr/syslog
  69. */
  70. static void init_log(const char *program)
  71. {
  72. dbg = tpm_extendpcr_dbg;
  73. if (log_to_stderr)
  74. {
  75. setbuf(stderr, NULL);
  76. }
  77. if (log_to_syslog)
  78. {
  79. openlog(program, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_AUTHPRIV);
  80. }
  81. }
  82. /**
  83. * @brief exit tpm_extendpcr
  84. *
  85. * @param status 0 = OK, -1 = general discomfort
  86. */
  87. static void exit_tpm_extendpcr(err_t message, ...)
  88. {
  89. int status = 0;
  90. DESTROY_IF(tpm);
  91. chunk_free(&digest);
  92. chunk_free(&pcr_value);
  93. /* print any error message to stderr */
  94. if (message != NULL && *message != '\0')
  95. {
  96. va_list args;
  97. char m[8192];
  98. va_start(args, message);
  99. vsnprintf(m, sizeof(m), message, args);
  100. va_end(args);
  101. fprintf(stderr, "tpm_extendpcr error: %s\n", m);
  102. status = -1;
  103. }
  104. library_deinit();
  105. exit(status);
  106. }
  107. /**
  108. * @brief prints the usage of the program to the stderr output
  109. *
  110. * If message is set, program is exited with 1 (error)
  111. * @param message message in case of an error
  112. */
  113. static void usage(const char *message)
  114. {
  115. fprintf(stderr,
  116. "Usage: tpm_extendpcr [--alg <name>] --pcr <nr> --digest <hex>|--in"
  117. " <file>\n"
  118. " [--hash] [--out <file>] [--quiet]"
  119. " [--debug <level>]\n"
  120. " tpm_extendpcr --help\n"
  121. "\n"
  122. "Options:\n"
  123. " --alg (-a) hash algorithm (sha1|sha256)\n"
  124. " --pcr (-p) platform configuration register (0..23)\n"
  125. " --digest (-d) digest in hex format to be extended\n"
  126. " --in (-i) binary input file with digest to be extended\n"
  127. " --hash (-x) prehash the input file to create digest\n"
  128. " --out (-o) binary output file with updated PCR value\n"
  129. " --help (-h) show usage and exit\n"
  130. "\n"
  131. "Debugging output:\n"
  132. " --debug (-l) changes the log level (-1..4, default: 1)\n"
  133. " --quiet (-q) do not write log output to stderr\n"
  134. );
  135. exit_tpm_extendpcr(message);
  136. }
  137. /**
  138. * @brief main of tpm_extendpcr which extends digest into a PCR
  139. *
  140. * @param argc number of arguments
  141. * @param argv pointer to the argument values
  142. */
  143. int main(int argc, char *argv[])
  144. {
  145. hash_algorithm_t alg = HASH_SHA1;
  146. hasher_t *hasher = NULL;
  147. char *infile = NULL, *outfile = NULL;
  148. uint32_t pcr = 16;
  149. bool hash = FALSE;
  150. atexit(library_deinit);
  151. if (!library_init(NULL, "tpm_extendpcr"))
  152. {
  153. exit(SS_RC_LIBSTRONGSWAN_INTEGRITY);
  154. }
  155. if (lib->integrity &&
  156. !lib->integrity->check_file(lib->integrity, "tpm_extendpcr", argv[0]))
  157. {
  158. fprintf(stderr, "integrity check of tpm_extendpcr failed\n");
  159. exit(SS_RC_DAEMON_INTEGRITY);
  160. }
  161. for (;;)
  162. {
  163. static const struct option long_opts[] = {
  164. /* name, has_arg, flag, val */
  165. { "help", no_argument, NULL, 'h' },
  166. { "alg", required_argument, NULL, 'a' },
  167. { "pcr", required_argument, NULL, 'p' },
  168. { "digest", required_argument, NULL, 'd' },
  169. { "in", required_argument, NULL, 'i' },
  170. { "hash", no_argument, NULL, 'x' },
  171. { "out", required_argument, NULL, 'o' },
  172. { "quiet", no_argument, NULL, 'q' },
  173. { "debug", required_argument, NULL, 'l' },
  174. { 0,0,0,0 }
  175. };
  176. /* parse next option */
  177. int c = getopt_long(argc, argv, "ha:p:d:i:xo:ql:", long_opts, NULL);
  178. switch (c)
  179. {
  180. case EOF: /* end of flags */
  181. break;
  182. case 'h': /* --help */
  183. usage(NULL);
  184. case 'a': /* --alg <name> */
  185. if (!enum_from_name(hash_algorithm_short_names, optarg, &alg))
  186. {
  187. usage("unsupported hash algorithm");
  188. }
  189. continue;
  190. case 'p': /* --pcr <nr> */
  191. pcr = atoi(optarg);
  192. continue;
  193. case 'd': /* --digest <hex> */
  194. digest = chunk_from_hex(chunk_from_str(optarg), NULL);
  195. continue;
  196. case 'i': /* --in <file> */
  197. infile = optarg;
  198. continue;
  199. case 'x': /* --hash */
  200. hash = TRUE;
  201. continue;
  202. case 'o': /* --out <file> */
  203. outfile = optarg;
  204. continue;
  205. case 'q': /* --quiet */
  206. log_to_stderr = FALSE;
  207. continue;
  208. case 'l': /* --debug <level> */
  209. default_loglevel = atoi(optarg);
  210. continue;
  211. default:
  212. usage("unknown option");
  213. }
  214. /* break from loop */
  215. break;
  216. }
  217. init_log("tpm_extendpcr");
  218. if (!lib->plugins->load(lib->plugins,
  219. lib->settings->get_str(lib->settings, "tpm_extendpcr.load",
  220. "tpm sha1 sha2")))
  221. {
  222. exit_tpm_extendpcr("plugin loading failed");
  223. }
  224. /* try to find a TPM */
  225. tpm = tpm_tss_probe(TPM_VERSION_ANY);
  226. if (!tpm)
  227. {
  228. exit_tpm_extendpcr("no TPM found");
  229. }
  230. /* read digest from file */
  231. if (digest.len == 0)
  232. {
  233. chunk_t *chunk;
  234. if (!infile)
  235. {
  236. exit_tpm_extendpcr("--digest or --in option required");
  237. }
  238. chunk = chunk_map(infile, FALSE);
  239. if (!chunk)
  240. {
  241. exit_tpm_extendpcr("reading input file failed");
  242. }
  243. if (hash)
  244. {
  245. hasher = lib->crypto->create_hasher(lib->crypto, alg);
  246. if (!hasher || !hasher->allocate_hash(hasher, *chunk, &digest))
  247. {
  248. DESTROY_IF(hasher);
  249. chunk_unmap(chunk);
  250. exit_tpm_extendpcr("prehashing infile failed");
  251. }
  252. hasher->destroy(hasher);
  253. }
  254. else
  255. {
  256. digest = chunk_clone(*chunk);
  257. }
  258. chunk_unmap(chunk);
  259. }
  260. DBG1(DBG_PTS, "Digest: %#B", &digest);
  261. /* extend digest into PCR */
  262. if (!tpm->extend_pcr(tpm, pcr, &pcr_value, digest, alg))
  263. {
  264. exit_tpm_extendpcr("extending PCR failed");
  265. }
  266. DBG1(DBG_PTS, "PCR %02u: %#B", pcr, &pcr_value);
  267. /* write PCR value to file */
  268. if (outfile)
  269. {
  270. if (!chunk_write(pcr_value, outfile, 022, TRUE))
  271. {
  272. DBG1(DBG_PTS, "writing '%s' failed", outfile);
  273. }
  274. }
  275. chunk_free(&pcr_value);
  276. exit_tpm_extendpcr(NULL);
  277. return -1; /* should never be reached */
  278. }