esys-session-attributes.int.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. #define LOGDEFAULT LOGLEVEL_INFO
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. extern TSS2_RC
  18. (*transmit_hook) (const uint8_t *command_buffer, size_t command_size);
  19. size_t handles;
  20. TPMA_SESSION session1_attributes;
  21. static TSS2_RC
  22. hookcheck_session1 (const uint8_t *command_buffer, size_t command_size);
  23. /** Test encrypt / decrypt session flags propagation
  24. *
  25. * Testing that the command decrypt and response encrypt session flags that are
  26. * set in Esys are actually propagated to the TPM command's session flags, if
  27. * the command allows this. Using TPM2_CreatePrimary as a candidate.
  28. *
  29. * @param[in,out] esys_context The ESYS_CONTEXT.
  30. * @retval EXIT_FAILURE
  31. * @retval EXIT_SUCCESS
  32. */
  33. int
  34. test_esys_session_attributes(ESYS_CONTEXT * esys_context)
  35. {
  36. TSS2_RC r;
  37. ESYS_TR objectHandle = ESYS_TR_NONE;
  38. ESYS_TR session = ESYS_TR_NONE;
  39. TPM2B_DIGEST *rdata = NULL;
  40. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_XOR,
  41. .keyBits = { .exclusiveOr = TPM2_ALG_SHA256 },
  42. .mode = {.aes = TPM2_ALG_CFB}};
  43. TPM2B_SENSITIVE_CREATE inSensitive = {
  44. .size = 0,
  45. .sensitive = {
  46. .userAuth = {
  47. .size = 0,
  48. .buffer = {0}
  49. ,
  50. },
  51. .data = {
  52. .size = 0,
  53. .buffer = {0}
  54. }
  55. }
  56. };
  57. TPM2B_PUBLIC inPublic = {
  58. .size = 0,
  59. .publicArea = {
  60. .type = TPM2_ALG_RSA,
  61. .nameAlg = TPM2_ALG_SHA256,
  62. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  63. TPMA_OBJECT_RESTRICTED |
  64. TPMA_OBJECT_DECRYPT |
  65. TPMA_OBJECT_FIXEDTPM |
  66. TPMA_OBJECT_FIXEDPARENT |
  67. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  68. .authPolicy = {
  69. .size = 0,
  70. },
  71. .parameters.rsaDetail = {
  72. .symmetric = {
  73. .algorithm = TPM2_ALG_AES,
  74. .keyBits.aes = 128,
  75. .mode.aes = TPM2_ALG_CFB,
  76. },
  77. .scheme = {
  78. .scheme =
  79. TPM2_ALG_NULL,
  80. },
  81. .keyBits = 2048,
  82. .exponent = 0,
  83. },
  84. .unique.rsa = {
  85. .size = 0,
  86. .buffer = {}
  87. ,
  88. }
  89. }
  90. };
  91. TPM2B_DATA outsideInfo = {
  92. .size = 0,
  93. .buffer = {}
  94. ,
  95. };
  96. TPML_PCR_SELECTION creationPCR = {
  97. .count = 0,
  98. };
  99. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  100. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  101. NULL,
  102. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  103. &session);
  104. goto_if_error(r, "Error: During initialization of session", error);
  105. /* Testing Encrypt and Decrypt, both set */
  106. r = Esys_TRSess_SetAttributes(esys_context, session,
  107. TPMA_SESSION_DECRYPT | TPMA_SESSION_ENCRYPT,
  108. TPMA_SESSION_DECRYPT | TPMA_SESSION_ENCRYPT);
  109. goto_if_error(r, "Error: During initialization of attributes", error);
  110. handles = 1;
  111. session1_attributes = TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_DECRYPT |
  112. TPMA_SESSION_ENCRYPT;
  113. transmit_hook = hookcheck_session1;
  114. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, session,
  115. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitive, &inPublic,
  116. &outsideInfo, &creationPCR, &objectHandle,
  117. NULL, NULL, NULL, NULL);
  118. transmit_hook = NULL;
  119. goto_if_error(r, "Error esys create primary", error);
  120. r = Esys_FlushContext(esys_context, objectHandle);
  121. goto_if_error(r, "Error during FlushContext", error);
  122. r = Esys_FlushContext(esys_context, session);
  123. goto_if_error(r, "Flushing context", error);
  124. /* Testing only Encrypt, i.e. responses, set */
  125. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  126. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  127. NULL,
  128. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  129. &session);
  130. goto_if_error(r, "Error: During initialization of session", error);
  131. r = Esys_TRSess_SetAttributes(esys_context, session,
  132. TPMA_SESSION_ENCRYPT,
  133. TPMA_SESSION_DECRYPT | TPMA_SESSION_ENCRYPT);
  134. goto_if_error(r, "Error: During initialization of attributes", error);
  135. handles = 0;
  136. session1_attributes = TPMA_SESSION_CONTINUESESSION | TPMA_SESSION_ENCRYPT;
  137. transmit_hook = hookcheck_session1;
  138. r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE,
  139. 10, &rdata);
  140. Esys_Free(rdata);
  141. transmit_hook = NULL;
  142. goto_if_error(r, "Error esys create primary", error);
  143. transmit_hook = hookcheck_session1;
  144. r = Esys_GetRandom(esys_context, session, ESYS_TR_NONE, ESYS_TR_NONE,
  145. 10, &rdata);
  146. transmit_hook = NULL;
  147. goto_if_error(r, "Error esys create primary", error);
  148. LOGBLOB_INFO(&rdata->buffer[0], rdata->size, "rdata");
  149. /* Cleanup */
  150. r = Esys_FlushContext(esys_context, session);
  151. goto_if_error(r, "Flushing context", error);
  152. Esys_Free(rdata);
  153. return EXIT_SUCCESS;
  154. error:
  155. LOG_ERROR("\nError Code: %x\n", r);
  156. if (session != ESYS_TR_NONE) {
  157. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  158. LOG_ERROR("Cleanup session failed.");
  159. }
  160. }
  161. if (objectHandle != ESYS_TR_NONE) {
  162. if (Esys_FlushContext(esys_context, objectHandle) != TSS2_RC_SUCCESS) {
  163. LOG_ERROR("Cleanup objectHandle failed.");
  164. }
  165. }
  166. Esys_Free(rdata);
  167. return EXIT_FAILURE;
  168. }
  169. int
  170. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  171. return test_esys_session_attributes(esys_context);
  172. }
  173. static TSS2_RC
  174. hookcheck_session1 (const uint8_t *command_buffer, size_t command_size)
  175. {
  176. TSS2_RC r;
  177. size_t offset = 10; /* header */;
  178. TPM2_ST tag;
  179. TPMS_AUTH_COMMAND session1;
  180. LOGBLOB_INFO(command_buffer, command_size, "command");
  181. r = Tss2_MU_UINT16_Unmarshal(command_buffer, command_size, NULL, &tag);
  182. return_if_error(r, "Unmarshalling AuthSize failed");
  183. if (tag != TPM2_ST_SESSIONS) {
  184. LOG_ERROR("Bad Tag. Expected TPM2_ST_SESSION Got: 0x%04x", tag);
  185. return TSS2_TCTI_RC_BAD_VALUE;
  186. }
  187. offset += sizeof(TPM2_HANDLE) * handles;
  188. /* TPM2_AUTHORIZATION_SIZE authorizationSize */
  189. r = Tss2_MU_UINT32_Unmarshal(command_buffer, command_size, &offset, NULL);
  190. return_if_error(r, "Unmarshalling AuthSize failed");
  191. r = Tss2_MU_TPMS_AUTH_COMMAND_Unmarshal(command_buffer, command_size, &offset,
  192. &session1);
  193. return_if_error(r, "Unmarshalling first session failed");
  194. if (session1.sessionAttributes != session1_attributes) {
  195. LOG_ERROR("Session Attribute mismatch. Expected: 0x%08x Got: 0x%08x",
  196. session1_attributes, session1.sessionAttributes);
  197. return TSS2_TCTI_RC_BAD_VALUE;
  198. }
  199. return TSS2_RC_SUCCESS;
  200. }