esys-hierarchychangeauth.int.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This test is intended to test the change of an authorization value of
  16. * a hierarchy.
  17. *
  18. * To check whether the change was successful a primary key is created
  19. * with the handle of this hierarchy and the new authorization.
  20. * Also second primary is created after a call of Esys_TR_SetAuth with
  21. * the new auth value.
  22. *
  23. * Tested ESYS commands:
  24. * - Esys_CreatePrimary() (M)
  25. * - Esys_FlushContext() (M)
  26. * - Esys_HierarchyChangeAuth() (M)
  27. *
  28. * @param[in,out] esys_context The ESYS_CONTEXT.
  29. * @retval EXIT_FAILURE
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_hierarchychangeauth(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR primaryHandle = ESYS_TR_NONE;
  37. bool auth_changed = false;
  38. ESYS_TR authHandle_handle = ESYS_TR_RH_OWNER;
  39. TPM2B_PUBLIC *outPublic = NULL;
  40. TPM2B_CREATION_DATA *creationData = NULL;
  41. TPM2B_DIGEST *creationHash = NULL;
  42. TPMT_TK_CREATION *creationTicket = NULL;
  43. TPM2B_AUTH newAuth = {
  44. .size = 5,
  45. .buffer = {1, 2, 3, 4, 5}
  46. };
  47. TPM2B_AUTH emptyAuth = {
  48. .size = 0,
  49. .buffer = {}
  50. };
  51. r = Esys_HierarchyChangeAuth(esys_context,
  52. authHandle_handle,
  53. ESYS_TR_PASSWORD,
  54. ESYS_TR_NONE,
  55. ESYS_TR_NONE,
  56. &newAuth);
  57. goto_if_error(r, "Error: HierarchyChangeAuth", error);
  58. auth_changed = true;
  59. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  60. .size = 0,
  61. .sensitive = {
  62. .userAuth = {
  63. .size = 0,
  64. .buffer = {0 },
  65. },
  66. .data = {
  67. .size = 0,
  68. .buffer = {0},
  69. },
  70. },
  71. };
  72. TPM2B_PUBLIC inPublic = {
  73. .size = 0,
  74. .publicArea = {
  75. .type = TPM2_ALG_RSA,
  76. .nameAlg = TPM2_ALG_SHA256,
  77. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  78. TPMA_OBJECT_RESTRICTED |
  79. TPMA_OBJECT_DECRYPT |
  80. TPMA_OBJECT_FIXEDTPM |
  81. TPMA_OBJECT_FIXEDPARENT |
  82. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  83. .authPolicy = {
  84. .size = 0,
  85. },
  86. .parameters.rsaDetail = {
  87. .symmetric = {
  88. .algorithm = TPM2_ALG_AES,
  89. .keyBits.aes = 128,
  90. .mode.aes = TPM2_ALG_CFB},
  91. .scheme = {
  92. .scheme = TPM2_ALG_NULL
  93. },
  94. .keyBits = 2048,
  95. .exponent = 0,
  96. },
  97. .unique.rsa = {
  98. .size = 0,
  99. .buffer = {},
  100. },
  101. },
  102. };
  103. LOG_INFO("\nRSA key will be created.");
  104. TPM2B_DATA outsideInfo = {
  105. .size = 0,
  106. .buffer = {},
  107. };
  108. TPML_PCR_SELECTION creationPCR = {
  109. .count = 0,
  110. };
  111. goto_if_error(r, "Error: TR_SetAuth", error);
  112. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  113. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  114. &outsideInfo, &creationPCR, &primaryHandle,
  115. &outPublic, &creationData, &creationHash,
  116. &creationTicket);
  117. goto_if_error(r, "Error esys create primary", error);
  118. r = Esys_FlushContext(esys_context, primaryHandle);
  119. goto_if_error(r, "Flushing context", error);
  120. primaryHandle = ESYS_TR_NONE;
  121. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &newAuth);
  122. goto_if_error(r, "Error SetAuth", error);
  123. Esys_Free(outPublic);
  124. Esys_Free(creationData);
  125. Esys_Free(creationHash);
  126. Esys_Free(creationTicket);
  127. /* Check whether HierarchyChangeAuth with auth equal NULL works */
  128. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  129. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  130. &outsideInfo, &creationPCR, &primaryHandle,
  131. &outPublic, &creationData, &creationHash,
  132. &creationTicket);
  133. goto_if_error(r, "Error esys create primary", error);
  134. r = Esys_FlushContext(esys_context, primaryHandle);
  135. goto_if_error(r, "Flushing context", error);
  136. r = Esys_HierarchyChangeAuth(esys_context,
  137. authHandle_handle,
  138. ESYS_TR_PASSWORD,
  139. ESYS_TR_NONE,
  140. ESYS_TR_NONE,
  141. NULL);
  142. goto_if_error(r, "Error: HierarchyChangeAuth", error);
  143. Esys_Free(outPublic);
  144. Esys_Free(creationData);
  145. Esys_Free(creationHash);
  146. Esys_Free(creationTicket);
  147. return EXIT_SUCCESS;
  148. error:
  149. if (primaryHandle != ESYS_TR_NONE) {
  150. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  151. LOG_ERROR("Cleanup primaryHandle failed.");
  152. }
  153. }
  154. if (auth_changed) {
  155. if (Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &newAuth) != TSS2_RC_SUCCESS) {
  156. LOG_ERROR("Error SetAuth");
  157. }
  158. if (Esys_HierarchyChangeAuth(esys_context,
  159. authHandle_handle,
  160. ESYS_TR_PASSWORD,
  161. ESYS_TR_NONE,
  162. ESYS_TR_NONE,
  163. &emptyAuth) != TSS2_RC_SUCCESS) {
  164. LOG_ERROR("Error: HierarchyChangeAuth");
  165. }
  166. }
  167. Esys_Free(outPublic);
  168. Esys_Free(creationData);
  169. Esys_Free(creationHash);
  170. Esys_Free(creationTicket);
  171. return EXIT_FAILURE;
  172. }
  173. int
  174. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  175. return test_esys_hierarchychangeauth(esys_context);
  176. }