esys-auto-session-flags.int.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright 2017-2019, Intel Corporation
  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 "esys_iutil.h"
  12. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This test is intended to test auto adjust and restore session flags in ESYS
  16. *
  17. * Tested ESYS commands:
  18. * - Esys_FlushContext() (M)
  19. * - Esys_NV_DefineSpace() (M)
  20. * - Esys_NV_Read() (M)
  21. * - Esys_NV_Write() (M)
  22. * - Esys_NV_UndefineSpace() (M)
  23. * - Esys_StartAuthSession() (M)
  24. *
  25. * @param[in,out] esys_context The ESYS_CONTEXT.
  26. * @retval EXIT_FAILURE
  27. * @retval EXIT_SUCCESS
  28. */
  29. int
  30. test_esys_auto_flags(ESYS_CONTEXT * esys_context)
  31. {
  32. TSS2_RC r;
  33. int test_ret = EXIT_SUCCESS ;
  34. ESYS_TR nvHandle = ESYS_TR_NONE;
  35. ESYS_TR session_auth = ESYS_TR_NONE;
  36. ESYS_TR session_enc = ESYS_TR_NONE;
  37. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_AES,
  38. .keyBits = {.aes = 128},
  39. .mode = {.aes = TPM2_ALG_CFB}
  40. };
  41. TPM2B_NONCE nonceCaller = {
  42. .size = 20,
  43. .buffer = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12, 13, 14, 15, 16, 17,
  44. 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
  45. };
  46. /* Auth session */
  47. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  48. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  49. &nonceCaller,
  50. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  51. &session_auth);
  52. goto_if_error(r, "Error: During initialization of session_auth", error);
  53. /* Enc param session */
  54. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  55. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  56. &nonceCaller,
  57. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  58. &session_enc);
  59. goto_if_error(r, "Error: During initialization of session_enc", error);
  60. /* Set both ENC and DEC flags for the enc session */
  61. TPMA_SESSION sessionAttributes = TPMA_SESSION_DECRYPT |
  62. TPMA_SESSION_ENCRYPT |
  63. TPMA_SESSION_CONTINUESESSION;
  64. r = Esys_TRSess_SetAttributes(esys_context, session_enc, sessionAttributes, 0xFF);
  65. goto_if_error(r, "Error: During SetAttributes", error);
  66. TPM2B_AUTH auth = {.size = 20,
  67. .buffer={10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  68. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}};
  69. TPM2B_NV_PUBLIC publicInfo = {
  70. .size = 0,
  71. .nvPublic = {
  72. .nvIndex =TPM2_NV_INDEX_FIRST,
  73. .nameAlg = TPM2_ALG_SHA256,
  74. .attributes = (
  75. TPMA_NV_OWNERWRITE |
  76. TPMA_NV_AUTHWRITE |
  77. TPMA_NV_WRITE_STCLEAR |
  78. TPMA_NV_READ_STCLEAR |
  79. TPMA_NV_AUTHREAD |
  80. TPMA_NV_OWNERREAD
  81. ),
  82. .authPolicy = {
  83. .size = 0,
  84. .buffer = {},
  85. },
  86. .dataSize = 20,
  87. }
  88. };
  89. r = Esys_NV_DefineSpace (
  90. esys_context,
  91. ESYS_TR_RH_OWNER,
  92. session_auth,
  93. ESYS_TR_NONE,
  94. ESYS_TR_NONE,
  95. &auth,
  96. &publicInfo,
  97. &nvHandle);
  98. goto_if_error(r, "Error esys define nv space", error);
  99. TPM2B_MAX_NV_BUFFER nv_test_data = { .size = 20,
  100. .buffer={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
  101. 1, 2, 3, 4, 5, 6, 7, 8, 9}};
  102. const TPM2B_MAX_NV_BUFFER *nv_test_data_ptr = &nv_test_data;
  103. /* NV_Write cmd does not support TPMA_SESSION_ENCRYPT - the flag should
  104. * be auto cleared by ESYS */
  105. r = Esys_NV_Write(
  106. esys_context,
  107. nvHandle,
  108. nvHandle,
  109. session_enc,
  110. ESYS_TR_NONE,
  111. ESYS_TR_NONE,
  112. nv_test_data_ptr,
  113. 0);
  114. goto_if_error(r, "Error esys nv write", error);
  115. /* Verify that the same session flags are still set after the test */
  116. TPMA_SESSION sessionAttributesVerify;
  117. r = Esys_TRSess_GetAttributes(esys_context, session_enc,
  118. &sessionAttributesVerify);
  119. goto_if_error(r, "Error: During GetAttributes", error);
  120. if (sessionAttributes != sessionAttributesVerify) {
  121. LOG_ERROR("Session flags not equal after write %x, %x",
  122. sessionAttributes, sessionAttributesVerify);
  123. r = TSS2_ESYS_RC_GENERAL_FAILURE;
  124. goto_if_error(r, "Error esys nv write", error);
  125. }
  126. TPM2B_MAX_NV_BUFFER *data;
  127. /* NV_Read cmd does not support TPMA_SESSION_DECRYPT - the flags should
  128. * be auto cleared by ESYS */
  129. r = Esys_NV_Read(
  130. esys_context,
  131. nvHandle,
  132. nvHandle,
  133. session_enc,
  134. ESYS_TR_NONE,
  135. ESYS_TR_NONE,
  136. 20, 0, &data);
  137. goto_if_error(r, "Error: nv read", error);
  138. free(data);
  139. /* Verify that the same session flags are still set after the test */
  140. r = Esys_TRSess_GetAttributes(esys_context, session_enc,
  141. &sessionAttributesVerify);
  142. goto_if_error(r, "Error: During GetAttributes", error);
  143. if (sessionAttributes != sessionAttributesVerify) {
  144. LOG_ERROR("Session flags not equal after read %x, %x",
  145. sessionAttributes, sessionAttributesVerify);
  146. r = TSS2_ESYS_RC_GENERAL_FAILURE;
  147. }
  148. error:
  149. if (r)
  150. test_ret = EXIT_FAILURE;
  151. if (nvHandle != ESYS_TR_NONE) {
  152. if (Esys_NV_UndefineSpace(esys_context,
  153. ESYS_TR_RH_OWNER,
  154. nvHandle,
  155. session_auth,
  156. ESYS_TR_NONE,
  157. ESYS_TR_NONE) != TSS2_RC_SUCCESS) {
  158. LOG_ERROR("Cleanup nvHandle failed.");
  159. }
  160. }
  161. if (session_auth != ESYS_TR_NONE) {
  162. if (Esys_FlushContext(esys_context, session_auth) != TSS2_RC_SUCCESS) {
  163. LOG_ERROR("Cleanup session_auth failed.");
  164. }
  165. }
  166. if (session_enc != ESYS_TR_NONE) {
  167. if (Esys_FlushContext(esys_context, session_enc) != TSS2_RC_SUCCESS) {
  168. LOG_ERROR("Cleanup session_enc failed.");
  169. }
  170. }
  171. return test_ret;
  172. }
  173. int
  174. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  175. return test_esys_auto_flags(esys_context);
  176. }