esys-get-time.int.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 GetTime command with password
  17. * authentication.
  18. *
  19. * We create a RSA primary signing key which will be used
  20. * for signing.
  21. *
  22. * Tested ESYS commands:
  23. * - Esys_CreatePrimary() (M)
  24. * - Esys_FlushContext() (M)
  25. * - Esys_GetTime() (O)
  26. *
  27. * @param[in,out] esys_context The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SKIP
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_get_time(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR signHandle = ESYS_TR_NONE;
  37. int failure_return = EXIT_FAILURE;
  38. TPM2B_PUBLIC *outPublic = NULL;
  39. TPM2B_CREATION_DATA *creationData = NULL;
  40. TPM2B_DIGEST *creationHash = NULL;
  41. TPMT_TK_CREATION *creationTicket = NULL;
  42. TPM2B_ATTEST *timeInfo = NULL;
  43. TPMT_SIGNATURE *signature = NULL;
  44. TPM2B_AUTH authValuePrimary = {
  45. .size = 5,
  46. .buffer = {1, 2, 3, 4, 5}
  47. };
  48. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  49. .size = 0,
  50. .sensitive = {
  51. .userAuth = {
  52. .size = 0,
  53. .buffer = {0},
  54. },
  55. .data = {
  56. .size = 0,
  57. .buffer = {0},
  58. },
  59. },
  60. };
  61. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  62. TPM2B_PUBLIC inPublic = {
  63. .size = 0,
  64. .publicArea = {
  65. .type = TPM2_ALG_RSA,
  66. .nameAlg = TPM2_ALG_SHA256,
  67. .objectAttributes = (
  68. TPMA_OBJECT_USERWITHAUTH |
  69. TPMA_OBJECT_RESTRICTED |
  70. TPMA_OBJECT_SIGN_ENCRYPT |
  71. TPMA_OBJECT_FIXEDTPM |
  72. TPMA_OBJECT_FIXEDPARENT |
  73. TPMA_OBJECT_SENSITIVEDATAORIGIN
  74. ),
  75. .authPolicy = {
  76. .size = 0,
  77. },
  78. .parameters.rsaDetail = {
  79. .symmetric = {
  80. .algorithm = TPM2_ALG_NULL,
  81. .keyBits.aes = 128,
  82. .mode.aes = TPM2_ALG_CFB,
  83. },
  84. .scheme = {
  85. .scheme = TPM2_ALG_RSASSA,
  86. .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA256 }},
  87. },
  88. .keyBits = 2048,
  89. .exponent = 0,
  90. },
  91. .unique.rsa = {
  92. .size = 0,
  93. .buffer = {},
  94. },
  95. },
  96. };
  97. TPM2B_AUTH authValue = {
  98. .size = 0,
  99. .buffer = {}
  100. };
  101. TPM2B_DATA outsideInfo = {
  102. .size = 0,
  103. .buffer = {},
  104. };
  105. TPML_PCR_SELECTION creationPCR = {
  106. .count = 0,
  107. };
  108. LOG_INFO("\nRSA key will be created.");
  109. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  110. goto_if_error(r, "Error: TR_SetAuth", error);
  111. RSRC_NODE_T *primaryHandle_node;
  112. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  113. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  114. &inPublic, &outsideInfo, &creationPCR,
  115. &signHandle, &outPublic, &creationData,
  116. &creationHash, &creationTicket);
  117. goto_if_error(r, "Error esys create primary", error);
  118. r = esys_GetResourceObject(esys_context, signHandle,
  119. &primaryHandle_node);
  120. goto_if_error(r, "Error Esys GetResourceObject", error);
  121. LOG_INFO("Created Primary with handle 0x%08x...",
  122. primaryHandle_node->rsrc.handle);
  123. r = Esys_TR_SetAuth(esys_context, signHandle,
  124. &authValuePrimary);
  125. goto_if_error(r, "Error: TR_SetAuth", error);
  126. ESYS_TR privacyAdminHandle= ESYS_TR_RH_ENDORSEMENT;
  127. TPMT_SIG_SCHEME inScheme = { .scheme = TPM2_ALG_NULL };
  128. TPM2B_DATA qualifyingData = {0};
  129. r = Esys_GetTime (
  130. esys_context,
  131. privacyAdminHandle,
  132. signHandle,
  133. ESYS_TR_PASSWORD,
  134. ESYS_TR_PASSWORD,
  135. ESYS_TR_NONE,
  136. &qualifyingData,
  137. &inScheme,
  138. &timeInfo,
  139. &signature);
  140. if ((r == TPM2_RC_COMMAND_CODE) ||
  141. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  142. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  143. LOG_WARNING("Command TPM2_GetTime not supported by TPM.");
  144. r = Esys_FlushContext(esys_context, signHandle);
  145. goto_if_error(r, "Flushing context", error);
  146. signHandle = ESYS_TR_NONE;
  147. failure_return = EXIT_SKIP;
  148. goto error;
  149. }
  150. goto_if_error(r, "Error: GetTime", error);
  151. r = Esys_FlushContext(esys_context, signHandle);
  152. goto_if_error(r, "Error: FlushContext", error);
  153. Esys_Free(outPublic);
  154. Esys_Free(creationData);
  155. Esys_Free(creationHash);
  156. Esys_Free(creationTicket);
  157. Esys_Free(timeInfo);
  158. Esys_Free(signature);
  159. return EXIT_SUCCESS;
  160. error:
  161. if (signHandle != ESYS_TR_NONE) {
  162. if (Esys_FlushContext(esys_context, signHandle) != TSS2_RC_SUCCESS) {
  163. LOG_ERROR("Cleanup signHandle failed.");
  164. }
  165. }
  166. Esys_Free(outPublic);
  167. Esys_Free(creationData);
  168. Esys_Free(creationHash);
  169. Esys_Free(creationTicket);
  170. Esys_Free(timeInfo);
  171. Esys_Free(signature);
  172. return failure_return;
  173. }
  174. int
  175. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  176. return test_esys_get_time(esys_context);
  177. }