esys-audit.int.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "esys_iutil.h"
  12. #include "test-esys.h"
  13. #define LOGMODULE test
  14. #include "util/log.h"
  15. #include "util/aux_util.h"
  16. /** This test is intended to test the ESYS audit commands.
  17. *
  18. * First a key for signing the audit digest is computed.
  19. * A audit session is started, and for the command GetCapability the
  20. * command audit digest and the session audit digest is computed.
  21. * (Esys_GetCommandAuditDigest, Esys_GetSessionAuditDigest). In the
  22. * last test the audit hash alg is changed with Esys_SetCommandCodeAuditStatus.
  23. *
  24. *\b Note: platform authorization needed.
  25. *
  26. * Tested ESYS commands:
  27. * - Esys_CreatePrimary() (M)
  28. * - Esys_FlushContext() (M)
  29. * - Esys_GetCapability() (M)
  30. * - Esys_GetCommandAuditDigest() (O)
  31. * - Esys_GetSessionAuditDigest() (M)
  32. * - Esys_SetCommandCodeAuditStatus() (O)
  33. * - Esys_StartAuthSession() (M)
  34. * - Esys_StartAuthSession() (M)
  35. *
  36. * @param[in,out] esys_context The ESYS_CONTEXT.
  37. * @retval EXIT_FAILURE
  38. * @retval EXIT_SKIP
  39. * @retval EXIT_SUCCESS
  40. */
  41. int
  42. test_esys_audit(ESYS_CONTEXT * esys_context)
  43. {
  44. TSS2_RC r;
  45. ESYS_TR signHandle = ESYS_TR_NONE;
  46. ESYS_TR session = ESYS_TR_NONE;
  47. int failure_return = EXIT_FAILURE;
  48. TPM2B_PUBLIC *outPublic = NULL;
  49. TPM2B_CREATION_DATA *creationData = NULL;
  50. TPM2B_DIGEST *creationHash = NULL;
  51. TPMT_TK_CREATION *creationTicket = NULL;
  52. TPMS_CAPABILITY_DATA *capabilityData = NULL;
  53. TPM2B_ATTEST *auditInfo = NULL;
  54. TPMT_SIGNATURE *signature = NULL;
  55. /* Compute a signing key */
  56. TPM2B_AUTH authValuePrimary = {
  57. .size = 5,
  58. .buffer = {1, 2, 3, 4, 5}
  59. };
  60. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  61. .size = 0,
  62. .sensitive = {
  63. .userAuth = {
  64. .size = 0,
  65. .buffer = {0},
  66. },
  67. .data = {
  68. .size = 0,
  69. .buffer = {0},
  70. },
  71. },
  72. };
  73. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  74. TPM2B_PUBLIC inPublic = {
  75. .size = 0,
  76. .publicArea = {
  77. .type = TPM2_ALG_RSA,
  78. .nameAlg = TPM2_ALG_SHA256,
  79. .objectAttributes = (
  80. TPMA_OBJECT_USERWITHAUTH |
  81. TPMA_OBJECT_RESTRICTED |
  82. TPMA_OBJECT_SIGN_ENCRYPT |
  83. TPMA_OBJECT_FIXEDTPM |
  84. TPMA_OBJECT_FIXEDPARENT |
  85. TPMA_OBJECT_SENSITIVEDATAORIGIN
  86. ),
  87. .authPolicy = {
  88. .size = 0,
  89. },
  90. .parameters.rsaDetail = {
  91. .symmetric = {
  92. .algorithm = TPM2_ALG_NULL,
  93. .keyBits.aes = 128,
  94. .mode.aes = TPM2_ALG_CFB,
  95. },
  96. .scheme = {
  97. .scheme = TPM2_ALG_RSASSA,
  98. .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA256 }},
  99. },
  100. .keyBits = 2048,
  101. .exponent = 0,
  102. },
  103. .unique.rsa = {
  104. .size = 0,
  105. .buffer = {},
  106. },
  107. },
  108. };
  109. TPM2B_AUTH authValue = {
  110. .size = 0,
  111. .buffer = {}
  112. };
  113. TPM2B_DATA outsideInfo = {
  114. .size = 0,
  115. .buffer = {},
  116. };
  117. TPML_PCR_SELECTION creationPCR = {
  118. .count = 0,
  119. };
  120. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  121. goto_if_error(r, "Error: TR_SetAuth", error);
  122. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  123. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  124. &inPublic, &outsideInfo, &creationPCR,
  125. &signHandle, &outPublic, &creationData,
  126. &creationHash, &creationTicket);
  127. goto_if_error(r, "Error esys create primary", error);
  128. /* Start a audit session */
  129. TPMA_SESSION sessionAttributes = TPMA_SESSION_CONTINUESESSION |
  130. TPMA_SESSION_AUDIT;
  131. TPM2_SE sessionType = TPM2_SE_HMAC;
  132. TPMI_ALG_HASH authHash = TPM2_ALG_SHA256;
  133. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_NULL };
  134. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  135. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  136. NULL,
  137. sessionType, &symmetric, authHash, &session);
  138. goto_if_error(r, "Error Esys_StartAuthSession", error);
  139. r = Esys_TRSess_SetAttributes(esys_context, session, sessionAttributes,
  140. 0xff);
  141. goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
  142. /* Execute one command to be audited */
  143. TPM2_CAP capability = TPM2_CAP_TPM_PROPERTIES;
  144. UINT32 property = TPM2_PT_LOCKOUT_COUNTER;
  145. UINT32 propertyCount = 1;
  146. TPMI_YES_NO moreData;
  147. r = Esys_GetCapability(esys_context,
  148. session, ESYS_TR_NONE, ESYS_TR_NONE,
  149. capability, property, propertyCount,
  150. &moreData, &capabilityData);
  151. goto_if_error(r, "Error esys get capability", error);
  152. ESYS_TR privacyHandle = ESYS_TR_RH_ENDORSEMENT;
  153. TPM2B_DATA qualifyingData = {0};
  154. TPMT_SIG_SCHEME inScheme = { .scheme = TPM2_ALG_NULL };
  155. /* Test the audit commands */
  156. r = Esys_GetCommandAuditDigest(
  157. esys_context,
  158. privacyHandle,
  159. signHandle,
  160. ESYS_TR_PASSWORD,
  161. ESYS_TR_PASSWORD,
  162. ESYS_TR_NONE,
  163. &qualifyingData,
  164. &inScheme,
  165. &auditInfo,
  166. &signature);
  167. if ((r == TPM2_RC_COMMAND_CODE) ||
  168. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  169. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  170. LOG_WARNING("Command TPM2_GetCommandAuditDigest not supported by TPM.");
  171. failure_return = EXIT_SKIP;
  172. goto error;
  173. }
  174. Esys_Free(auditInfo);
  175. Esys_Free(signature);
  176. goto_if_error(r, "Error: GetCommandAuditDigest", error);
  177. r = Esys_GetSessionAuditDigest(
  178. esys_context,
  179. privacyHandle,
  180. signHandle,
  181. session,
  182. ESYS_TR_PASSWORD,
  183. ESYS_TR_PASSWORD,
  184. ESYS_TR_NONE,
  185. &qualifyingData,
  186. &inScheme,
  187. &auditInfo,
  188. &signature);
  189. goto_if_error(r, "Error: GetSessionAuditDigest", error);
  190. TPMI_ALG_HASH auditAlg = TPM2_ALG_SHA256;
  191. TPML_CC clearList = {0};
  192. TPML_CC setList = {0};
  193. r = Esys_SetCommandCodeAuditStatus(
  194. esys_context,
  195. ESYS_TR_RH_PLATFORM,
  196. ESYS_TR_PASSWORD,
  197. ESYS_TR_NONE,
  198. ESYS_TR_NONE,
  199. auditAlg,
  200. &setList,
  201. &clearList);
  202. if (number_rc(r) == TPM2_RC_BAD_AUTH) {
  203. /* Platform authorization not possible test will be skipped */
  204. LOG_WARNING("Platform authorization not possible.");
  205. failure_return = EXIT_SKIP;
  206. goto error;
  207. }
  208. goto_if_error(r, "Error: SetCommandCodeAuditStatus", error);
  209. r = Esys_FlushContext(esys_context, signHandle);
  210. goto_if_error(r, "Error: FlushContext", error);
  211. signHandle = ESYS_TR_NONE;
  212. r = Esys_FlushContext(esys_context, session);
  213. goto_if_error(r, "Error during FlushContext", error);
  214. Esys_Free(outPublic);
  215. Esys_Free(creationData);
  216. Esys_Free(creationHash);
  217. Esys_Free(creationTicket);
  218. Esys_Free(capabilityData);
  219. Esys_Free(auditInfo);
  220. Esys_Free(signature);
  221. return EXIT_SUCCESS;
  222. error:
  223. if (session != ESYS_TR_NONE) {
  224. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  225. LOG_ERROR("Cleanup session failed.");
  226. }
  227. }
  228. if (signHandle != ESYS_TR_NONE) {
  229. if (Esys_FlushContext(esys_context, signHandle) != TSS2_RC_SUCCESS) {
  230. LOG_ERROR("Cleanup signHandle failed.");
  231. }
  232. }
  233. Esys_Free(outPublic);
  234. Esys_Free(creationData);
  235. Esys_Free(creationHash);
  236. Esys_Free(creationTicket);
  237. Esys_Free(capabilityData);
  238. Esys_Free(auditInfo);
  239. Esys_Free(signature);
  240. return failure_return;
  241. }
  242. int
  243. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  244. return test_esys_audit(esys_context);
  245. }