esys-unseal-password-auth.int.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdint.h>
  13. #include <tss2_esys.h>
  14. #include "test.h"
  15. #include "esys_types.h"
  16. #include "esys_iutil.h"
  17. #define LOGMODULE test
  18. #include "util/log.h"
  19. #include "util/aux_util.h"
  20. /** This test is intended to test the unseal operation for the ESYS command
  21. * Unseal.
  22. *
  23. * We start by creating a primary key (Esys_CreatePrimary).
  24. * Based on the primary key a second key with a password and the to be sealed
  25. * data defined in the sensitive area will be created (Esys_Create).
  26. * This key will be loaded and the unseal command (Esys_Unseal) will be used
  27. * to retrieve the sealed data.
  28. *
  29. * Tested ESYS commands:
  30. * - Esys_Create() (M)
  31. * - Esys_CreatePrimary() (M)
  32. * - Esys_FlushContext() (M)
  33. * - Esys_Load() (M)
  34. * - Esys_Unseal() (M)
  35. *
  36. * @param[in,out] esys_context The ESYS_CONTEXT.
  37. * @retval EXIT_FAILURE
  38. * @retval EXIT_SUCCESS
  39. */
  40. int
  41. test_esys_unseal_password_auth(ESYS_CONTEXT * esys_context)
  42. {
  43. /*
  44. * 1. Create Primary
  45. */
  46. TSS2_RC r;
  47. ESYS_TR primaryHandle = ESYS_TR_NONE;
  48. ESYS_TR loadedKeyHandle = ESYS_TR_NONE;
  49. TPM2B_PUBLIC *outPublic = NULL;
  50. TPM2B_CREATION_DATA *creationData = NULL;
  51. TPM2B_DIGEST *creationHash = NULL;
  52. TPMT_TK_CREATION *creationTicket = NULL;
  53. TPM2B_PUBLIC *outPublic2 = NULL;
  54. TPM2B_PRIVATE *outPrivate2 = NULL;
  55. TPM2B_CREATION_DATA *creationData2 = NULL;
  56. TPM2B_DIGEST *creationHash2 = NULL;
  57. TPMT_TK_CREATION *creationTicket2 = NULL;
  58. TPM2B_SENSITIVE_DATA *outData = NULL;
  59. TPM2B_AUTH authValuePrimary = {
  60. .size = 5,
  61. .buffer = {1, 2, 3, 4, 5}
  62. };
  63. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  64. .size = 0,
  65. .sensitive = {
  66. .userAuth = {
  67. .size = 0,
  68. .buffer = {0 },
  69. },
  70. .data = {
  71. .size = 0,
  72. .buffer = {0},
  73. },
  74. },
  75. };
  76. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  77. TPM2B_PUBLIC inPublic = {
  78. .size = 0,
  79. .publicArea = {
  80. .type = TPM2_ALG_RSA,
  81. .nameAlg = TPM2_ALG_SHA256,
  82. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  83. TPMA_OBJECT_RESTRICTED |
  84. TPMA_OBJECT_DECRYPT |
  85. TPMA_OBJECT_FIXEDTPM |
  86. TPMA_OBJECT_FIXEDPARENT |
  87. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  88. .authPolicy = {
  89. .size = 0,
  90. },
  91. .parameters.rsaDetail = {
  92. .symmetric = {
  93. .algorithm = TPM2_ALG_AES,
  94. .keyBits.aes = 128,
  95. .mode.aes = TPM2_ALG_CFB},
  96. .scheme = {
  97. .scheme = TPM2_ALG_NULL
  98. },
  99. .keyBits = 2048,
  100. .exponent = 0,
  101. },
  102. .unique.rsa = {
  103. .size = 0,
  104. .buffer = {},
  105. },
  106. },
  107. };
  108. TPM2B_DATA outsideInfo = {
  109. .size = 0,
  110. .buffer = {},
  111. };
  112. TPML_PCR_SELECTION creationPCR = {
  113. .count = 0,
  114. };
  115. TPM2B_AUTH authValue = {
  116. .size = 0,
  117. .buffer = {}
  118. };
  119. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  120. goto_if_error(r, "Error: TR_SetAuth", error);
  121. RSRC_NODE_T *primaryHandle_node;
  122. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  123. ESYS_TR_NONE, ESYS_TR_NONE,
  124. &inSensitivePrimary, &inPublic,
  125. &outsideInfo, &creationPCR, &primaryHandle,
  126. &outPublic, &creationData, &creationHash,
  127. &creationTicket);
  128. goto_if_error(r, "Error esys create primary", error);
  129. r = esys_GetResourceObject(esys_context, primaryHandle,
  130. &primaryHandle_node);
  131. goto_if_error(r, "Error Esys GetResourceObject", error);
  132. LOG_INFO("Created Primary with handle 0x%08x...",
  133. primaryHandle_node->rsrc.handle);
  134. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  135. goto_if_error(r, "Error: TR_SetAuth", error);
  136. /*
  137. * 2. Create second key with sealed data
  138. */
  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 = 8,
  152. .buffer = {3,2,3,2,3,2,3,2}
  153. }
  154. }
  155. };
  156. inSensitive2.sensitive.userAuth = authKey2;
  157. TPM2B_PUBLIC inPublic2 = {
  158. .size = 0,
  159. .publicArea = {
  160. /* type = TPM2_ALG_RSA, */
  161. .type = TPM2_ALG_KEYEDHASH,
  162. .nameAlg = TPM2_ALG_SHA256,
  163. .objectAttributes = (
  164. TPMA_OBJECT_USERWITHAUTH |
  165. /* TPMA_OBJECT_RESTRICTED | */
  166. /* TPMA_OBJECT_DECRYPT | */
  167. TPMA_OBJECT_FIXEDTPM |
  168. TPMA_OBJECT_FIXEDPARENT
  169. /* TPMA_OBJECT_SENSITIVEDATAORIGIN */
  170. ),
  171. .authPolicy = {
  172. .size = 0,
  173. },
  174. /*
  175. .parameters.rsaDetail = {
  176. .symmetric = {
  177. .algorithm = TPM2_ALG_AES,
  178. .keyBits.aes = 128,
  179. .mode.aes = TPM2_ALG_CFB
  180. },
  181. .scheme = {
  182. .scheme = TPM2_ALG_NULL,
  183. },
  184. .keyBits = 2048,
  185. .exponent = 0
  186. },
  187. .unique.rsa = {
  188. .size = 0,
  189. .buffer = {}
  190. ,
  191. }
  192. */
  193. .parameters.keyedHashDetail = {
  194. .scheme = {
  195. .scheme = TPM2_ALG_NULL,
  196. .details = {
  197. .hmac = {
  198. .hashAlg = TPM2_ALG_SHA256
  199. }
  200. }
  201. }
  202. },
  203. .unique.keyedHash = {
  204. .size = 0,
  205. .buffer = {},
  206. },
  207. }
  208. };
  209. TPM2B_DATA outsideInfo2 = {
  210. .size = 0,
  211. .buffer = {}
  212. ,
  213. };
  214. TPML_PCR_SELECTION creationPCR2 = {
  215. .count = 0,
  216. };
  217. r = Esys_Create(esys_context,
  218. primaryHandle,
  219. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  220. &inSensitive2,
  221. &inPublic2,
  222. &outsideInfo2,
  223. &creationPCR2,
  224. &outPrivate2,
  225. &outPublic2,
  226. &creationData2, &creationHash2, &creationTicket2);
  227. goto_if_error(r, "Error esys create ", error);
  228. LOG_INFO("\nSecond key created.");
  229. /*
  230. * 3. Load second key
  231. */
  232. r = Esys_Load(esys_context,
  233. primaryHandle,
  234. ESYS_TR_PASSWORD,
  235. ESYS_TR_NONE,
  236. ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle);
  237. goto_if_error(r, "Error esys load ", error);
  238. LOG_INFO("\nSecond Key loaded.");
  239. r = Esys_TR_SetAuth(esys_context, loadedKeyHandle, &authKey2);
  240. goto_if_error(r, "Error esys TR_SetAuth ", error);
  241. /*
  242. * 4. Unseal key
  243. */
  244. r = Esys_Unseal(esys_context, loadedKeyHandle, ESYS_TR_PASSWORD,
  245. ESYS_TR_NONE, ESYS_TR_NONE, &outData);
  246. goto_if_error(r, "Error esys Unseal ", error);
  247. if(memcmp(&(outData->buffer), &(inSensitive2.sensitive.data.buffer),
  248. inSensitive2.sensitive.data.size)!=0){
  249. LOG_ERROR("Error: Unsealed Data is unequal.");
  250. goto error;
  251. }
  252. LOG_INFO("\nData successfully unsealed.");
  253. /*
  254. * 5. Flush Context
  255. */
  256. r = Esys_FlushContext(esys_context, primaryHandle);
  257. goto_if_error(r, "Error during FlushContext", error);
  258. primaryHandle = ESYS_TR_NONE;
  259. r = Esys_FlushContext(esys_context, loadedKeyHandle);
  260. goto_if_error(r, "Error during FlushContext", error);
  261. Esys_Free(outPublic);
  262. Esys_Free(creationData);
  263. Esys_Free(creationHash);
  264. Esys_Free(creationTicket);
  265. Esys_Free(outPublic2);
  266. Esys_Free(outPrivate2);
  267. Esys_Free(creationData2);
  268. Esys_Free(creationHash2);
  269. Esys_Free(creationTicket2);
  270. Esys_Free(outData);
  271. return EXIT_SUCCESS;
  272. error:
  273. if (loadedKeyHandle != ESYS_TR_NONE) {
  274. if (Esys_FlushContext(esys_context, loadedKeyHandle) != TSS2_RC_SUCCESS) {
  275. LOG_ERROR("Cleanup loadedKeyHandle failed.");
  276. }
  277. }
  278. if (primaryHandle != ESYS_TR_NONE) {
  279. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  280. LOG_ERROR("Cleanup primaryHandle failed.");
  281. }
  282. }
  283. Esys_Free(outPublic);
  284. Esys_Free(creationData);
  285. Esys_Free(creationHash);
  286. Esys_Free(creationTicket);
  287. Esys_Free(outPublic2);
  288. Esys_Free(outPrivate2);
  289. Esys_Free(creationData2);
  290. Esys_Free(creationHash2);
  291. Esys_Free(creationTicket2);
  292. Esys_Free(outData);
  293. return EXIT_FAILURE;
  294. }
  295. int
  296. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  297. return test_esys_unseal_password_auth(esys_context);
  298. }