tpm2_gettime.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <tss2/tss2_mu.h>
  6. #include "files.h"
  7. #include "log.h"
  8. #include "tpm2.h"
  9. #include "tpm2_tool.h"
  10. #include "tpm2_alg_util.h"
  11. #include "tpm2_convert.h"
  12. #include "tpm2_hash.h"
  13. #include "tpm2_options.h"
  14. typedef struct tpm_gettime_ctx tpm_gettime_ctx;
  15. struct tpm_gettime_ctx {
  16. struct {
  17. const char *ctx_path;
  18. const char *auth_str;
  19. tpm2_loaded_object object;
  20. } signing_key;
  21. struct {
  22. const char *ctx_path;
  23. const char *auth_str;
  24. tpm2_loaded_object object;
  25. } privacy_admin;
  26. tpm2_convert_sig_fmt sig_format;
  27. TPM2B_DATA qualifying_data;
  28. TPMI_ALG_HASH halg;
  29. TPMI_ALG_SIG_SCHEME sig_scheme;
  30. TPMT_SIG_SCHEME in_scheme;
  31. const char *certify_info_path;
  32. const char *output_path;
  33. char *cp_hash_path;
  34. };
  35. static tpm_gettime_ctx ctx = {
  36. .halg = TPM2_ALG_NULL,
  37. .sig_scheme = TPM2_ALG_NULL,
  38. .privacy_admin = { .ctx_path = "endorsement" }
  39. };
  40. static tool_rc init(ESYS_CONTEXT *ectx) {
  41. if (!ctx.signing_key.ctx_path) {
  42. LOG_ERR("Expected option \"-c\"");
  43. return tool_rc_option_error;
  44. }
  45. if (ctx.cp_hash_path && (ctx.output_path || ctx.certify_info_path)) {
  46. LOG_ERR("Ignoring output options due to cpHash calculation");
  47. return tool_rc_option_error;
  48. }
  49. /* load the signing key */
  50. tool_rc rc = tpm2_util_object_load_auth(ectx, ctx.signing_key.ctx_path,
  51. ctx.signing_key.auth_str, &ctx.signing_key.object, false,
  52. TPM2_HANDLES_FLAGS_TRANSIENT|TPM2_HANDLES_FLAGS_PERSISTENT);
  53. if (rc != tool_rc_success) {
  54. LOG_ERR("Invalid key authorization");
  55. return rc;
  56. }
  57. /*
  58. * Set signature scheme for key type, or validate chosen scheme is allowed for key type.
  59. */
  60. rc = tpm2_alg_util_get_signature_scheme(ectx,
  61. ctx.signing_key.object.tr_handle, &ctx.halg, ctx.sig_scheme,
  62. &ctx.in_scheme);
  63. if (rc != tool_rc_success) {
  64. LOG_ERR("bad signature scheme for key type!");
  65. return rc;
  66. }
  67. /* set up the privacy admin (always endorsement) hard coded in ctx init */
  68. rc = tpm2_util_object_load_auth(ectx, ctx.privacy_admin.ctx_path,
  69. ctx.privacy_admin.auth_str, &ctx.privacy_admin.object, false,
  70. TPM2_HANDLE_FLAGS_E);
  71. if (rc != tool_rc_success) {
  72. return rc;
  73. }
  74. return tool_rc_success;
  75. }
  76. static bool on_option(char key, char *value) {
  77. switch (key) {
  78. case 'c':
  79. ctx.signing_key.ctx_path = value;
  80. break;
  81. case 'p':
  82. ctx.signing_key.auth_str = value;
  83. break;
  84. case 'P':
  85. ctx.privacy_admin.auth_str = value;
  86. break;
  87. case 'g':
  88. ctx.halg = tpm2_alg_util_from_optarg(value, tpm2_alg_util_flags_hash);
  89. if (ctx.halg == TPM2_ALG_ERROR) {
  90. LOG_ERR("Could not convert to number or lookup algorithm, got: "
  91. "\"%s\"", value);
  92. return false;
  93. }
  94. break;
  95. case 's': {
  96. ctx.sig_scheme = tpm2_alg_util_from_optarg(value,
  97. tpm2_alg_util_flags_sig);
  98. if (ctx.sig_scheme == TPM2_ALG_ERROR) {
  99. LOG_ERR("Unknown signing scheme, got: \"%s\"", value);
  100. return false;
  101. }
  102. }
  103. break;
  104. case 'o':
  105. ctx.output_path = value;
  106. break;
  107. case 'f':
  108. ctx.sig_format = tpm2_convert_sig_fmt_from_optarg(value);
  109. if (ctx.sig_format == signature_format_err) {
  110. return false;
  111. }
  112. break;
  113. case 'q':
  114. ctx.qualifying_data.size = sizeof(ctx.qualifying_data.buffer);
  115. return tpm2_util_bin_from_hex_or_file(value, &ctx.qualifying_data.size,
  116. ctx.qualifying_data.buffer);
  117. break;
  118. case 2:
  119. ctx.certify_info_path = value;
  120. break;
  121. case 0:
  122. ctx.cp_hash_path = value;
  123. break;
  124. /* no default */
  125. }
  126. return true;
  127. }
  128. static bool tpm2_tool_onstart(tpm2_options **opts) {
  129. static const struct option topts[] = {
  130. { "auth", required_argument, NULL, 'p' },
  131. { "endorse-auth", required_argument, NULL, 'P' },
  132. { "hash-algorithm", required_argument, NULL, 'g' },
  133. { "scheme", required_argument, NULL, 's' },
  134. { "signature", required_argument, NULL, 'o' },
  135. { "key-context", required_argument, NULL, 'c' },
  136. { "format", required_argument, NULL, 'f' },
  137. { "qualification", required_argument, NULL, 'q' },
  138. { "attestation", required_argument, NULL, 2 },
  139. { "cphash", required_argument, NULL, 0 },
  140. };
  141. *opts = tpm2_options_new("p:g:o:c:f:s:P:q:", ARRAY_LEN(topts), topts,
  142. on_option, NULL, 0);
  143. return *opts != NULL;
  144. }
  145. static tool_rc tpm2_tool_onrun(ESYS_CONTEXT *ectx, tpm2_option_flags flags) {
  146. UNUSED(flags);
  147. tool_rc rc = init(ectx);
  148. if (rc != tool_rc_success) {
  149. return rc;
  150. }
  151. TPM2B_ATTEST *time_info = NULL;
  152. TPMT_SIGNATURE *signature = NULL;
  153. if (ctx.cp_hash_path) {
  154. TPM2B_DIGEST cp_hash = { .size = 0 };
  155. tool_rc rc = tpm2_gettime(ectx, &ctx.privacy_admin.object,
  156. &ctx.signing_key.object, &ctx.qualifying_data, &ctx.in_scheme,
  157. &time_info, &signature, &cp_hash);
  158. if (rc != tool_rc_success) {
  159. return rc;
  160. }
  161. bool result = files_save_digest(&cp_hash, ctx.cp_hash_path);
  162. if (!result) {
  163. rc = tool_rc_general_error;
  164. }
  165. return rc;
  166. }
  167. rc = tpm2_gettime(ectx,
  168. &ctx.privacy_admin.object,
  169. &ctx.signing_key.object,
  170. &ctx.qualifying_data,
  171. &ctx.in_scheme,
  172. &time_info,
  173. &signature,
  174. NULL);
  175. if (rc != tool_rc_success) {
  176. return rc;
  177. }
  178. /* save the signature */
  179. if (ctx.output_path) {
  180. bool result = tpm2_convert_sig_save(signature, ctx.sig_format, ctx.output_path);
  181. if (!result) {
  182. rc = tool_rc_general_error;
  183. goto out;
  184. }
  185. }
  186. if (ctx.certify_info_path) {
  187. /* save the attestation data */
  188. bool result = files_save_bytes_to_file(ctx.certify_info_path,
  189. time_info->attestationData, time_info->size);
  190. if (!result) {
  191. rc = tool_rc_general_error;
  192. goto out;
  193. }
  194. }
  195. TPMS_ATTEST attest;
  196. rc = files_tpm2b_attest_to_tpms_attest(time_info, &attest);
  197. if (rc == tool_rc_success) {
  198. tpm2_util_print_time(&attest.attested.time.time);
  199. }
  200. out:
  201. Esys_Free(time_info);
  202. Esys_Free(signature);
  203. return rc;
  204. }
  205. static tool_rc tpm2_tool_onstop(ESYS_CONTEXT *ectx) {
  206. UNUSED(ectx);
  207. tool_rc rc = tpm2_session_close(&ctx.privacy_admin.object.session);
  208. rc |=tpm2_session_close(&ctx.signing_key.object.session);
  209. return rc;
  210. }
  211. // Register this tool with tpm2_tool.c
  212. TPM2_TOOL_REGISTER("gettime", tpm2_tool_onstart, tpm2_tool_onrun, tpm2_tool_onstop, NULL)