esys-policy-physical-presence-opt.int.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
  4. * All rights reserved.
  5. *******************************************************************************/
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif
  9. #include <stdlib.h>
  10. #include "tss2_esys.h"
  11. #include "tss2_mu.h"
  12. #include "esys_iutil.h"
  13. #include "test-esys.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. #define FLUSH true
  18. #define NOT_FLUSH false
  19. /*
  20. * Function to compare policy digest with expected digest.
  21. * The digest is computed with Esys_PolicyGetDigest.
  22. */
  23. bool
  24. cmp_policy_digest(ESYS_CONTEXT * esys_context,
  25. ESYS_TR * session,
  26. TPM2B_DIGEST * expected_digest,
  27. char *comment, bool flush_session)
  28. {
  29. TSS2_RC r;
  30. TPM2B_DIGEST *policyDigest;
  31. r = Esys_PolicyGetDigest(esys_context,
  32. *session,
  33. ESYS_TR_NONE,
  34. ESYS_TR_NONE, ESYS_TR_NONE, &policyDigest);
  35. goto_if_error(r, "Error: PolicyGetDigest", error);
  36. LOGBLOB_DEBUG(&policyDigest->buffer[0], policyDigest->size,
  37. "POLICY DIGEST");
  38. if (policyDigest->size != 32
  39. || memcmp(&policyDigest->buffer[0], &expected_digest->buffer[0],
  40. policyDigest->size)) {
  41. free(policyDigest);
  42. LOG_ERROR("Error: Policy%s digest did not match expected policy.",
  43. comment);
  44. return false;
  45. }
  46. free(policyDigest);
  47. if (flush_session) {
  48. r = Esys_FlushContext(esys_context, *session);
  49. goto_if_error(r, "Error: PolicyGetDigest", error);
  50. *session = ESYS_TR_NONE;
  51. }
  52. return true;
  53. error:
  54. return false;
  55. }
  56. /** This test is intended to test the ESYS policy commands, not tested
  57. * in other test cases.
  58. * When possoble the commands are tested with a
  59. * trial session and the policy digest is compared with the expected digest.
  60. *
  61. * Tested ESYS commands:
  62. * - Esys_PolicyPhysicalPresence() (O)
  63. *
  64. * @param[in,out] esys_context The ESYS_CONTEXT.
  65. * @retval EXIT_FAILURE
  66. * @retval EXIT_SKIP
  67. * @retval EXIT_SUCCESS
  68. */
  69. int
  70. test_esys_policy_physical_presence_opt(ESYS_CONTEXT * esys_context)
  71. {
  72. TSS2_RC r;
  73. int failure_return = EXIT_FAILURE;
  74. /* Dummy parameters for trial sessoin */
  75. ESYS_TR sessionTrial = ESYS_TR_NONE;
  76. TPMT_SYM_DEF symmetricTrial = {.algorithm = TPM2_ALG_AES,
  77. .keyBits = {.aes = 128},
  78. .mode = {.aes = TPM2_ALG_CFB}
  79. };
  80. TPM2B_NONCE nonceCallerTrial = {
  81. .size = 32,
  82. .buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14, 15, 16, 17,
  83. 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 }
  84. };
  85. /*
  86. * Test PolicyPhysicalPresence
  87. */
  88. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  89. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  90. &nonceCallerTrial,
  91. TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA256,
  92. &sessionTrial);
  93. goto_if_error(r, "Error: During initialization of policy trial session",
  94. error);
  95. r = Esys_PolicyPhysicalPresence(esys_context,
  96. sessionTrial,
  97. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE);
  98. if ((r == TPM2_RC_COMMAND_CODE) ||
  99. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  100. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  101. LOG_WARNING("Command TPM2_PolicyPhysicalPresence not supported by TPM.");
  102. failure_return = EXIT_SKIP;
  103. goto error;
  104. } else {
  105. goto_if_error(r, "Error: PolicyPhysicalPresence", error);
  106. }
  107. TPM2B_DIGEST expectedPolicyPhysicalPresence = {
  108. .size = 20,
  109. .buffer = { 0x0d, 0x7c, 0x67, 0x47, 0xb1, 0xb9, 0xfa, 0xcb, 0xba, 0x03, 0x49,
  110. 0x20, 0x97, 0xaa, 0x9d, 0x5a, 0xf7, 0x92, 0xe5, 0xef, 0xc0, 0x73,
  111. 0x46, 0xe0, 0x5f, 0x9d, 0xaa, 0x8b, 0x3d, 0x9e, 0x13, 0xb5
  112. }
  113. };
  114. if (!cmp_policy_digest
  115. (esys_context, &sessionTrial, &expectedPolicyPhysicalPresence,
  116. "PhysicalPresence", FLUSH))
  117. goto error;
  118. return EXIT_SUCCESS;
  119. error:
  120. if (sessionTrial != ESYS_TR_NONE) {
  121. if (Esys_FlushContext(esys_context, sessionTrial) != TSS2_RC_SUCCESS) {
  122. LOG_ERROR("Cleanup sessionTrial failed.");
  123. }
  124. }
  125. return failure_return;
  126. }
  127. int
  128. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  129. return test_esys_policy_physical_presence_opt(esys_context);
  130. }