sys-read-clock.int.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2020, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <inttypes.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "tss2_sys.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "test.h"
  17. #define EXIT_SKIP 77
  18. /*
  19. * This is an incredibly simple test to create the most simple session
  20. * (which ends up being a trial policy) and then just tear it down.
  21. */
  22. int
  23. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  24. {
  25. TSS2_RC rc, rc2;
  26. TPM2B_NONCE nonce_caller = {
  27. .size = TPM2_SHA1_DIGEST_SIZE,
  28. .buffer = {
  29. 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
  30. 0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef,
  31. 0xde, 0xad, 0xbe, 0xef,
  32. }
  33. };
  34. TPM2B_NONCE nonce_tpm = {
  35. .size = TPM2_SHA1_DIGEST_SIZE,
  36. .buffer = { 0 }
  37. };
  38. TPM2B_ENCRYPTED_SECRET encrypted_salt = { 0 };
  39. TPMI_SH_AUTH_SESSION session = 0;
  40. TPMT_SYM_DEF symmetric = { .algorithm = TPM2_ALG_NULL };
  41. LOG_INFO("StartAuthSession for TPM2_SE_POLICY (policy session)");
  42. rc = Tss2_Sys_StartAuthSession (sys_context,
  43. TPM2_RH_NULL,
  44. TPM2_RH_NULL,
  45. NULL,
  46. &nonce_caller,
  47. &encrypted_salt,
  48. TPM2_SE_HMAC,
  49. &symmetric,
  50. TPM2_ALG_SHA1,
  51. &session,
  52. &nonce_tpm,
  53. NULL);
  54. if (rc != TSS2_RC_SUCCESS) {
  55. LOG_ERROR("Tss2_Sys_StartAuthSession failed: 0x%" PRIx32, rc);
  56. exit(1);
  57. }
  58. LOG_INFO("StartAuthSession for TPM2_SE_POLICY success! Session handle: "
  59. "0x%" PRIx32, session);
  60. rc = Tss2_Sys_ReadClock_Prepare(sys_context);
  61. if (rc != TSS2_RC_SUCCESS) {
  62. LOG_ERROR("Tss2_Sys_ReadClock_Prepare failed: 0x%" PRIx32, rc);
  63. goto error;
  64. }
  65. TSS2L_SYS_AUTH_COMMAND auths = {0};
  66. auths.auths[0].sessionHandle = session;
  67. auths.auths[0].sessionAttributes = TPMA_SESSION_AUDIT |
  68. TPMA_SESSION_CONTINUESESSION;
  69. auths.count = 1;
  70. rc = Tss2_Sys_SetCmdAuths(sys_context, &auths);
  71. if (rc != TSS2_RC_SUCCESS) {
  72. LOG_ERROR("Tss2_Sys_SetCmdAuths failed: 0x%" PRIx32, rc);
  73. goto error;
  74. }
  75. rc = Tss2_Sys_Execute(sys_context);
  76. /* TPMs before Revision 1.38 might not support session usage*/
  77. if ((rc == TPM2_RC_AUTH_CONTEXT ) ||
  78. (rc == (TPM2_RC_AUTH_CONTEXT | TSS2_RESMGR_RC_LAYER)) ||
  79. (rc == (TPM2_RC_AUTH_CONTEXT | TSS2_RESMGR_TPM_RC_LAYER))) {
  80. LOG_WARNING("Session usage not supported by TPM.");
  81. rc = EXIT_SKIP;
  82. goto error;
  83. }
  84. if (rc != TSS2_RC_SUCCESS) {
  85. LOG_ERROR("Tss2_Sys_ExecuteAsync failed: 0x%" PRIx32, rc);
  86. goto error;
  87. }
  88. TPMS_TIME_INFO time;
  89. rc = Tss2_Sys_ReadClock_Complete(sys_context, &time);
  90. if (rc != TSS2_RC_SUCCESS) {
  91. LOG_ERROR("Tss2_Sys_ReadClock_Complete failed: 0x%" PRIx32, rc);
  92. goto error;
  93. }
  94. error:
  95. rc2 = Tss2_Sys_FlushContext (sys_context, session);
  96. if (rc2 != TSS2_RC_SUCCESS) {
  97. LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
  98. return rc2;
  99. }
  100. return rc;
  101. }