tpm2_quote.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "files.h"
  7. #include "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_alg_util.h"
  10. #include "tpm2_convert.h"
  11. #include "tpm2_openssl.h"
  12. #include "tpm2_systemdeps.h"
  13. #include "tpm2_tool.h"
  14. typedef struct tpm_quote_ctx tpm_quote_ctx;
  15. struct tpm_quote_ctx {
  16. struct {
  17. const char *ctx_path;
  18. const char *auth_str;
  19. tpm2_loaded_object object;
  20. } key;
  21. char *signature_path;
  22. char *message_path;
  23. char *pcr_path;
  24. FILE *pcr_output;
  25. tpm2_convert_sig_fmt sig_format;
  26. TPMI_ALG_HASH sig_hash_algorithm;
  27. tpm2_algorithm algs;
  28. TPM2B_DATA qualification_data;
  29. TPML_PCR_SELECTION pcr_selections;
  30. TPMS_CAPABILITY_DATA cap_data;
  31. tpm2_pcrs pcrs;
  32. char *cp_hash_path;
  33. };
  34. static tpm_quote_ctx ctx = {
  35. .sig_hash_algorithm = TPM2_ALG_NULL,
  36. .qualification_data = TPM2B_EMPTY_INIT,
  37. };
  38. // write all PCR banks according to g_pcrSelection & g_pcrs->
  39. static bool write_pcr_values() {
  40. UINT32 count;
  41. // PCR output to file wasn't requested
  42. if (ctx.pcr_output == NULL) {
  43. return true;
  44. }
  45. // Make sure the file content is written in little endian format
  46. ctx.pcr_selections.count = htole32(ctx.pcr_selections.count);
  47. UINT32 i;
  48. for (i = 0; i < le32toh(ctx.pcr_selections.count); i++)
  49. ctx.pcr_selections.pcrSelections[i].hash = htole16(ctx.pcr_selections.pcrSelections[i].hash);
  50. // Export TPML_PCR_SELECTION structure to pcr outfile
  51. if (fwrite(&ctx.pcr_selections, sizeof(TPML_PCR_SELECTION), 1,
  52. ctx.pcr_output) != 1) {
  53. LOG_ERR("write to output file failed: %s", strerror(errno));
  54. return false;
  55. }
  56. count = htole32(ctx.pcrs.count);
  57. // Export PCR digests to pcr outfile
  58. if (fwrite(&count, sizeof(UINT32), 1, ctx.pcr_output) != 1) {
  59. LOG_ERR("write to output file failed: %s", strerror(errno));
  60. return false;
  61. }
  62. UINT32 j;
  63. for (j = 0; j < ctx.pcrs.count; j++) {
  64. ctx.pcrs.pcr_values[j].count = htole32(ctx.pcrs.pcr_values[j].count);
  65. UINT32 k;
  66. for(k = 0; k < le32toh(ctx.pcrs.pcr_values[j].count); k++)
  67. ctx.pcrs.pcr_values[j].digests[k].size = htole16(ctx.pcrs.pcr_values[j].digests[k].size);
  68. if (fwrite(&ctx.pcrs.pcr_values[j], sizeof(TPML_DIGEST), 1,
  69. ctx.pcr_output) != 1) {
  70. LOG_ERR("write to output file failed: %s", strerror(errno));
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. static bool write_output_files(TPM2B_ATTEST *quoted, TPMT_SIGNATURE *signature) {
  77. bool res = true;
  78. if (ctx.signature_path) {
  79. res &= tpm2_convert_sig_save(signature, ctx.sig_format,
  80. ctx.signature_path);
  81. }
  82. if (ctx.message_path) {
  83. res &= files_save_bytes_to_file(ctx.message_path,
  84. (UINT8*) quoted->attestationData, quoted->size);
  85. }
  86. res &= write_pcr_values();
  87. return res;
  88. }
  89. static tool_rc quote(ESYS_CONTEXT *ectx, TPML_PCR_SELECTION *pcr_selection) {
  90. TPM2B_ATTEST *quoted = NULL;
  91. TPMT_SIGNATURE *signature = NULL;
  92. TPMT_SIG_SCHEME in_scheme = { .scheme = TPM2_ALG_NULL };
  93. tool_rc rc = tpm2_alg_util_get_signature_scheme(ectx,
  94. ctx.key.object.tr_handle, &ctx.sig_hash_algorithm, TPM2_ALG_NULL,
  95. &in_scheme);
  96. if (rc != tool_rc_success) {
  97. return rc;
  98. }
  99. if (ctx.cp_hash_path) {
  100. TPM2B_DIGEST cp_hash = { .size = 0 };
  101. rc = tpm2_quote(ectx, &ctx.key.object, &in_scheme,
  102. &ctx.qualification_data, pcr_selection, &quoted, &signature, &cp_hash);
  103. if (rc != tool_rc_success) {
  104. return rc;
  105. }
  106. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  107. if (!result) {
  108. rc = tool_rc_general_error;
  109. }
  110. return rc;
  111. }
  112. rc = tpm2_quote(ectx, &ctx.key.object, &in_scheme, &ctx.qualification_data,
  113. pcr_selection, &quoted, &signature, NULL);
  114. if (rc != tool_rc_success) {
  115. return rc;
  116. }
  117. tpm2_tool_output("quoted: ");
  118. tpm2_util_print_tpm2b(quoted);
  119. tpm2_tool_output("\nsignature:\n");
  120. tpm2_tool_output(" alg: %s\n",
  121. tpm2_alg_util_algtostr(signature->sigAlg, tpm2_alg_util_flags_sig));
  122. UINT16 size;
  123. BYTE *sig = tpm2_convert_sig(&size, signature);
  124. if (!sig) {
  125. return tool_rc_general_error;
  126. }
  127. tpm2_tool_output(" sig: ");
  128. tpm2_util_hexdump(sig, size);
  129. tpm2_tool_output("\n");
  130. free(sig);
  131. if (ctx.pcr_output) {
  132. // Filter out invalid/unavailable PCR selections
  133. if (!pcr_check_pcr_selection(&ctx.cap_data, &ctx.pcr_selections)) {
  134. LOG_ERR("Failed to filter unavailable PCR values for quote!");
  135. return tool_rc_general_error;
  136. }
  137. // Gather PCR values from the TPM (the quote doesn't have them!)
  138. rc = pcr_read_pcr_values(ectx, &ctx.pcr_selections, &ctx.pcrs);
  139. if (rc != tool_rc_success) {
  140. LOG_ERR("Failed to retrieve PCR values related to quote!");
  141. return rc;
  142. }
  143. // Grab the digest from the quote
  144. TPMS_ATTEST attest;
  145. rc = files_tpm2b_attest_to_tpms_attest(quoted, &attest);
  146. if (rc != tool_rc_success) {
  147. return rc;
  148. }
  149. // Print out PCR values as output
  150. if (!pcr_print_pcr_struct(&ctx.pcr_selections, &ctx.pcrs)) {
  151. LOG_ERR("Failed to print PCR values related to quote!");
  152. return tool_rc_general_error;
  153. }
  154. // Calculate the digest from our selected PCR values (to ensure correctness)
  155. TPM2B_DIGEST pcr_digest = TPM2B_TYPE_INIT(TPM2B_DIGEST, buffer);
  156. if (!tpm2_openssl_hash_pcr_banks(ctx.sig_hash_algorithm,
  157. &ctx.pcr_selections, &ctx.pcrs, &pcr_digest)) {
  158. LOG_ERR("Failed to hash PCR values related to quote!");
  159. return tool_rc_general_error;
  160. }
  161. tpm2_tool_output("calcDigest: ");
  162. tpm2_util_hexdump(pcr_digest.buffer, pcr_digest.size);
  163. tpm2_tool_output("\n");
  164. // Make sure digest from quote matches calculated PCR digest
  165. if (!tpm2_util_verify_digests(&attest.attested.quote.pcrDigest, &pcr_digest)) {
  166. LOG_ERR("Error validating calculated PCR composite with quote");
  167. return tool_rc_general_error;
  168. }
  169. }
  170. // Write everything out
  171. bool res = write_output_files(quoted, signature);
  172. free(quoted);
  173. free(signature);
  174. return res ? tool_rc_success : tool_rc_general_error;
  175. }
  176. static bool on_option(char key, char *value) {
  177. switch (key) {
  178. case 'c':
  179. ctx.key.ctx_path = value;
  180. break;
  181. case 'p':
  182. ctx.key.auth_str = value;
  183. break;
  184. case 'l':
  185. if (!pcr_parse_selections(value, &ctx.pcr_selections)) {
  186. LOG_ERR("Could not parse pcr selections, got: \"%s\"", value);
  187. return false;
  188. }
  189. break;
  190. case 'q':
  191. ctx.qualification_data.size = sizeof(ctx.qualification_data.buffer);
  192. return tpm2_util_bin_from_hex_or_file(value, &ctx.qualification_data.size,
  193. ctx.qualification_data.buffer);
  194. break;
  195. case 's':
  196. ctx.signature_path = value;
  197. break;
  198. case 'm':
  199. ctx.message_path = value;
  200. break;
  201. case 'o':
  202. ctx.pcr_path = value;
  203. break;
  204. case 'f':
  205. ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);
  206. if (ctx.sig_format == signature_format_err) {
  207. return false;
  208. }
  209. break;
  210. case 'g':
  211. ctx.sig_hash_algorithm = tpm2_alg_util_from_optarg(value,
  212. tpm2_alg_util_flags_hash);
  213. if (ctx.sig_hash_algorithm == TPM2_ALG_ERROR) {
  214. LOG_ERR(
  215. "Could not convert signature hash algorithm selection, got: \"%s\"",
  216. value);
  217. return false;
  218. }
  219. break;
  220. case 0:
  221. ctx.cp_hash_path = value;
  222. break;
  223. }
  224. return true;
  225. }
  226. static bool tpm2_tool_onstart(tpm2_options **opts) {
  227. static const struct option topts[] = {
  228. { "key-context", required_argument, NULL, 'c' },
  229. { "auth", required_argument, NULL, 'p' },
  230. { "pcr-list", required_argument, NULL, 'l' },
  231. { "qualification", required_argument, NULL, 'q' },
  232. { "signature", required_argument, NULL, 's' },
  233. { "message", required_argument, NULL, 'm' },
  234. { "pcr", required_argument, NULL, 'o' },
  235. { "format", required_argument, NULL, 'f' },
  236. { "hash-algorithm", required_argument, NULL, 'g' },
  237. { "cphash", required_argument, NULL, 0 }
  238. };
  239. *opts = tpm2_options_new("c:p:l:q:s:m:o:f:g:", ARRAY_LEN(topts), topts,
  240. on_option, NULL, 0);
  241. return *opts != NULL;
  242. }
  243. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  244. UNUSED(flags);
  245. /* TODO this whole file needs to be re-done, especially the option validation */
  246. if (!ctx.pcr_selections.count) {
  247. LOG_ERR("Expected -l to be specified.");
  248. return tool_rc_option_error;
  249. }
  250. if (ctx.cp_hash_path && (ctx.signature_path || ctx.message_path)) {
  251. LOG_ERR("Cannot produce output when calculating cpHash");
  252. return tool_rc_option_error;
  253. }
  254. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.key.ctx_path,
  255. ctx.key.auth_str, &ctx.key.object, false, TPM2_HANDLE_ALL_W_NV);
  256. if (rc != tool_rc_success) {
  257. LOG_ERR("Invalid key authorization");
  258. return rc;
  259. }
  260. if (ctx.pcr_path) {
  261. ctx.pcr_output = fopen(ctx.pcr_path, "wb+");
  262. if (!ctx.pcr_output) {
  263. LOG_ERR("Could not open PCR output file \"%s\" error: \"%s\"",
  264. ctx.pcr_path, strerror(errno));
  265. return tool_rc_general_error;
  266. }
  267. }
  268. rc = pcr_get_banks(ectx, &ctx.cap_data, &ctx.algs);
  269. if (rc != tool_rc_success) {
  270. return rc;
  271. }
  272. return quote(ectx, &ctx.pcr_selections);
  273. }
  274. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  275. UNUSED(ectx);
  276. if (ctx.pcr_output) {
  277. fclose(ctx.pcr_output);
  278. }
  279. return tpm2_session_close(&ctx.key.object.session);
  280. }
  281. // Register this tool with tpm2_tool.c
  282. TPM2_TOOL_REGISTER("quote", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)