esys-hierarchy-control.int.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. /** Test the ESYS function Esys_HierarchyControl.
  17. *
  18. * The owner hierarchy will be disabled and with Esys_CreatePrimary it will
  19. * be checked whether the owner hierarchy is disabled.
  20. *
  21. *\b Note: platform authorization needed.
  22. *
  23. * Tested ESYS commands:
  24. * - Esys_CreatePrimary() (M)
  25. * - Esys_FlushContext() (M)
  26. * - Esys_HierarchyControl() (M)
  27. *
  28. * @param[in,out] esys_context The ESYS_CONTEXT.
  29. * @param[in] enable The hierarchy to enable or disable.
  30. * @retval EXIT_FAILURE
  31. * @retval EXIT_SKIP
  32. * @retval EXIT_SUCCESS
  33. */
  34. int
  35. test_esys_hierarchy_control(ESYS_CONTEXT * esys_context, ESYS_TR enable)
  36. {
  37. TSS2_RC r;
  38. ESYS_TR authHandle_handle = ESYS_TR_RH_PLATFORM;
  39. TPMI_YES_NO state = TPM2_NO;
  40. ESYS_TR primaryHandle = ESYS_TR_NONE;
  41. int failure_return = EXIT_FAILURE;
  42. TPM2B_PUBLIC *outPublic = NULL;
  43. TPM2B_CREATION_DATA *creationData = NULL;
  44. TPM2B_DIGEST *creationHash = NULL;
  45. TPMT_TK_CREATION *creationTicket = NULL;
  46. r = Esys_HierarchyControl(
  47. esys_context,
  48. authHandle_handle,
  49. ESYS_TR_PASSWORD,
  50. ESYS_TR_NONE,
  51. ESYS_TR_NONE,
  52. enable,
  53. state);
  54. if (number_rc(r) == TPM2_RC_BAD_AUTH) {
  55. /* Platform authorization not possible test will be skipped */
  56. LOG_WARNING("Platform authorization not possible.");
  57. return EXIT_SKIP;
  58. }
  59. goto_if_error(r, "Error: HierarchyControl", error);
  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. TPM2B_PUBLIC inPublic = {
  74. .size = 0,
  75. .publicArea = {
  76. .type = TPM2_ALG_RSA,
  77. .nameAlg = TPM2_ALG_SHA256,
  78. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  79. TPMA_OBJECT_RESTRICTED |
  80. TPMA_OBJECT_DECRYPT |
  81. TPMA_OBJECT_FIXEDTPM |
  82. TPMA_OBJECT_FIXEDPARENT |
  83. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  84. .authPolicy = {
  85. .size = 0,
  86. },
  87. .parameters.rsaDetail = {
  88. .symmetric = {
  89. .algorithm = TPM2_ALG_AES,
  90. .keyBits.aes = 128,
  91. .mode.aes = TPM2_ALG_CFB},
  92. .scheme = {
  93. .scheme = TPM2_ALG_NULL
  94. },
  95. .keyBits = 2048,
  96. .exponent = 0,
  97. },
  98. .unique.rsa = {
  99. .size = 0,
  100. .buffer = {},
  101. },
  102. },
  103. };
  104. LOG_INFO("\nRSA key will be created.");
  105. TPM2B_DATA outsideInfo = {
  106. .size = 0,
  107. .buffer = {},
  108. };
  109. TPML_PCR_SELECTION creationPCR = {
  110. .count = 0,
  111. };
  112. goto_if_error(r, "Error: TR_SetAuth", error);
  113. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  114. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  115. &outsideInfo, &creationPCR, &primaryHandle,
  116. &outPublic, &creationData, &creationHash,
  117. &creationTicket);
  118. goto_error_if_not_failed(r, "Error: Create Primary", error);
  119. state = TPM2_YES;
  120. r = Esys_HierarchyControl(
  121. esys_context,
  122. authHandle_handle,
  123. ESYS_TR_PASSWORD,
  124. ESYS_TR_NONE,
  125. ESYS_TR_NONE,
  126. enable,
  127. state);
  128. goto_if_error(r, "Error: HierarchyControl", error);
  129. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  130. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  131. &outsideInfo, &creationPCR, &primaryHandle,
  132. &outPublic, &creationData, &creationHash,
  133. &creationTicket);
  134. goto_if_error(r, "Error esys create primary", error);
  135. r = Esys_FlushContext(esys_context, primaryHandle);
  136. goto_if_error(r, "Error: FlushContext", error);
  137. Esys_Free(outPublic);
  138. Esys_Free(creationData);
  139. Esys_Free(creationHash);
  140. Esys_Free(creationTicket);
  141. return EXIT_SUCCESS;
  142. error:
  143. if (primaryHandle != ESYS_TR_NONE) {
  144. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  145. LOG_ERROR("Cleanup primaryHandle failed.");
  146. }
  147. }
  148. Esys_Free(outPublic);
  149. Esys_Free(creationData);
  150. Esys_Free(creationHash);
  151. Esys_Free(creationTicket);
  152. return failure_return;
  153. }
  154. int
  155. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  156. int rc = test_esys_hierarchy_control(esys_context, ESYS_TR_RH_OWNER);
  157. if (rc)
  158. return rc;
  159. /*
  160. * Test that backwards compat API change is still working, see:
  161. * - https://github.com/tpm2-software/tpm2-tss/issues/1750
  162. */
  163. return test_esys_hierarchy_control(esys_context, TPM2_RH_OWNER);
  164. }