esys-evict-control-serialization.int.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 EvictControl and ESYS Serialization.
  16. *
  17. * We start by creating a primary key (Esys_CreatePrimary). Based on this
  18. * key a persistent object is created (Esys_EvictControl). The resource of
  19. * this object will be serialized and deserialized with the corresponding
  20. * ESYS functions (Esys_TR_Serialize, Esys_TR_Deserialize).
  21. * To check whether the deserialization was successful a new object will
  22. * be created with the handle returned by the deserialize function.
  23. *
  24. * Tested ESYS commands:
  25. * - Esys_Create() (M)
  26. * - Esys_CreatePrimary() (M)
  27. * - Esys_EvictControl() (M)
  28. * - Esys_FlushContext() (M)
  29. *
  30. * @param[in,out] esys_context The ESYS_CONTEXT.
  31. * @retval EXIT_FAILURE
  32. * @retval EXIT_SUCCESS
  33. */
  34. int
  35. test_esys_evict_control_serialization(ESYS_CONTEXT * esys_context)
  36. {
  37. TSS2_RC r;
  38. ESYS_TR primaryHandle = ESYS_TR_NONE;
  39. ESYS_TR persistent_handle1 = ESYS_TR_NONE;
  40. TPM2B_PUBLIC *outPublic = NULL;
  41. TPM2B_CREATION_DATA *creationData = NULL;
  42. TPM2B_DIGEST *creationHash = NULL;
  43. TPMT_TK_CREATION *creationTicket = NULL;
  44. TPM2B_PUBLIC *outPublic2 = NULL;
  45. TPM2B_PRIVATE *outPrivate2 = NULL;
  46. TPM2B_CREATION_DATA *creationData2 = NULL;
  47. TPM2B_DIGEST *creationHash2 = NULL;
  48. TPMT_TK_CREATION *creationTicket2 = NULL;
  49. TPM2B_AUTH authValuePrimary = {
  50. .size = 5,
  51. .buffer = {1, 2, 3, 4, 5}
  52. };
  53. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  54. .size = 0,
  55. .sensitive = {
  56. .userAuth = {
  57. .size = 0,
  58. .buffer = {0 },
  59. },
  60. .data = {
  61. .size = 0,
  62. .buffer = {0},
  63. },
  64. },
  65. };
  66. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  67. TPM2B_PUBLIC inPublic = {
  68. .size = 0,
  69. .publicArea = {
  70. .type = TPM2_ALG_RSA,
  71. .nameAlg = TPM2_ALG_SHA256,
  72. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  73. TPMA_OBJECT_RESTRICTED |
  74. TPMA_OBJECT_DECRYPT |
  75. TPMA_OBJECT_FIXEDTPM |
  76. TPMA_OBJECT_FIXEDPARENT |
  77. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  78. .authPolicy = {
  79. .size = 0,
  80. },
  81. .parameters.rsaDetail = {
  82. .symmetric = {
  83. .algorithm = TPM2_ALG_AES,
  84. .keyBits.aes = 128,
  85. .mode.aes = TPM2_ALG_CFB},
  86. .scheme = {
  87. .scheme = TPM2_ALG_NULL
  88. },
  89. .keyBits = 2048,
  90. .exponent = 0,
  91. },
  92. .unique.rsa = {
  93. .size = 0,
  94. .buffer = {},
  95. },
  96. },
  97. };
  98. LOG_INFO("\nRSA key will be created.");
  99. TPM2B_DATA outsideInfo = {
  100. .size = 0,
  101. .buffer = {},
  102. };
  103. TPML_PCR_SELECTION creationPCR = {
  104. .count = 0,
  105. };
  106. TPM2B_AUTH authValue = {
  107. .size = 0,
  108. .buffer = {}
  109. };
  110. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  111. goto_if_error(r, "Error: TR_SetAuth", error);
  112. RSRC_NODE_T *primaryHandle_node;
  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_if_error(r, "Error esys create primary", error);
  119. r = esys_GetResourceObject(esys_context, primaryHandle,
  120. &primaryHandle_node);
  121. goto_if_error(r, "Error Esys GetResourceObject", error);
  122. LOG_INFO("Created Primary with handle 0x%08x...",
  123. primaryHandle_node->rsrc.handle);
  124. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  125. goto_if_error(r, "Error: TR_SetAuth", error);
  126. TPM2_HANDLE permanentHandle = TPM2_PERSISTENT_FIRST;
  127. ESYS_TR persistent_handle2;
  128. r = Esys_EvictControl(esys_context, ESYS_TR_RH_OWNER, primaryHandle,
  129. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  130. permanentHandle, &persistent_handle1);
  131. goto_if_error(r, "Error Esys EvictControl", error);
  132. size_t buffer_size;
  133. uint8_t *buffer;
  134. r = Esys_TR_Serialize(esys_context, persistent_handle1, &buffer, &buffer_size);
  135. goto_if_error(r, "Error Esys_TR_Serialize", error);
  136. r = Esys_TR_Deserialize(esys_context, buffer, buffer_size, &persistent_handle2);
  137. goto_if_error(r, "Error Esys_TR_Deserialize", error);
  138. free(buffer);
  139. TPM2B_AUTH authKey2 = {
  140. .size = 6,
  141. .buffer = {6, 7, 8, 9, 10, 11}
  142. };
  143. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  144. .size = 0,
  145. .sensitive = {
  146. .userAuth = {
  147. .size = 0,
  148. .buffer = {0}
  149. },
  150. .data = {
  151. .size = 0,
  152. .buffer = {}
  153. }
  154. }
  155. };
  156. inSensitive2.sensitive.userAuth = authKey2;
  157. TPM2B_PUBLIC inPublic2 = {
  158. .size = 0,
  159. .publicArea = {
  160. .type = TPM2_ALG_RSA,
  161. .nameAlg = TPM2_ALG_SHA256,
  162. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  163. TPMA_OBJECT_RESTRICTED |
  164. TPMA_OBJECT_DECRYPT |
  165. TPMA_OBJECT_FIXEDTPM |
  166. TPMA_OBJECT_FIXEDPARENT |
  167. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  168. .authPolicy = {
  169. .size = 0,
  170. },
  171. .parameters.rsaDetail = {
  172. .symmetric = {
  173. .algorithm = TPM2_ALG_AES,
  174. .keyBits.aes = 128,
  175. .mode.aes = TPM2_ALG_CFB
  176. },
  177. .scheme = {
  178. .scheme =
  179. TPM2_ALG_NULL,
  180. },
  181. .keyBits = 2048,
  182. .exponent = 0
  183. },
  184. .unique.rsa = {
  185. .size = 0,
  186. .buffer = {}
  187. ,
  188. }
  189. }
  190. };
  191. TPM2B_DATA outsideInfo2 = {
  192. .size = 0,
  193. .buffer = {}
  194. ,
  195. };
  196. TPML_PCR_SELECTION creationPCR2 = {
  197. .count = 0,
  198. };
  199. r = Esys_TR_SetAuth(esys_context, persistent_handle2, &authValuePrimary);
  200. goto_if_error(r, "Error: TR_SetAuth", error);
  201. r = Esys_Create(esys_context,
  202. persistent_handle2,
  203. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  204. &inSensitive2,
  205. &inPublic2,
  206. &outsideInfo2,
  207. &creationPCR2,
  208. &outPrivate2,
  209. &outPublic2,
  210. &creationData2, &creationHash2, &creationTicket2);
  211. goto_if_error(r, "Error esys create with new handle from evict object",
  212. error);
  213. r = Esys_FlushContext(esys_context, primaryHandle);
  214. goto_if_error(r, "Error during FlushContext", error);
  215. r = Esys_EvictControl(esys_context, ESYS_TR_RH_OWNER, persistent_handle1,
  216. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  217. permanentHandle, &persistent_handle1);
  218. goto_if_error(r, "Error Esys EvictControl", error);
  219. Esys_Free(outPublic);
  220. Esys_Free(creationData);
  221. Esys_Free(creationHash);
  222. Esys_Free(creationTicket);
  223. Esys_Free(outPublic2);
  224. Esys_Free(outPrivate2);
  225. Esys_Free(creationData2);
  226. Esys_Free(creationHash2);
  227. Esys_Free(creationTicket2);
  228. return EXIT_SUCCESS;
  229. error:
  230. if (persistent_handle1 != ESYS_TR_NONE) {
  231. if (Esys_EvictControl(esys_context, ESYS_TR_RH_OWNER, persistent_handle1,
  232. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  233. permanentHandle, &persistent_handle1) != TSS2_RC_SUCCESS) {
  234. LOG_ERROR("Cleanup EvictControl failed");
  235. }
  236. }
  237. if (primaryHandle != ESYS_TR_NONE) {
  238. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  239. LOG_ERROR("Cleanup primaryHandle failed.");
  240. }
  241. }
  242. Esys_Free(outPublic);
  243. Esys_Free(creationData);
  244. Esys_Free(creationHash);
  245. Esys_Free(creationTicket);
  246. Esys_Free(outPublic2);
  247. Esys_Free(outPrivate2);
  248. Esys_Free(creationData2);
  249. Esys_Free(creationHash2);
  250. Esys_Free(creationTicket2);
  251. return EXIT_FAILURE;
  252. }
  253. int
  254. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  255. return test_esys_evict_control_serialization(esys_context);
  256. }