esys-save-and-load-context.int.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 context save and load.
  16. *
  17. * We start by creating a primary key (Esys_CreatePrimary).
  18. * Based in the primary a second key with an password define in the sensitive
  19. * area will be created.
  20. * This key will be loaded and saved with the ContextSave command.
  21. * After the key is flushed the key will be loaded again with ContextLoad
  22. * and will be used to create a third key
  23. *
  24. * Tested ESYS commands:
  25. * - Esys_ContextLoad() (M)
  26. * - Esys_ContextSave() (M)
  27. * - Esys_Create() (M)
  28. * - Esys_CreatePrimary() (M)
  29. * - Esys_FlushContext() (M)
  30. * - Esys_Load() (M)
  31. *
  32. * Used compiler defines: TEST_ECC
  33. *
  34. * @param[in,out] esys_context The ESYS_CONTEXT.
  35. * @retval EXIT_FAILURE
  36. * @retval EXIT_SUCCESS
  37. */
  38. int
  39. test_esys_save_and_load_context(ESYS_CONTEXT * esys_context)
  40. {
  41. TSS2_RC r;
  42. ESYS_TR primaryHandle = ESYS_TR_NONE;
  43. ESYS_TR loadedKeyHandle1 = ESYS_TR_NONE;
  44. ESYS_TR loadedKeyHandle2 = ESYS_TR_NONE;
  45. TPM2B_PUBLIC *outPublic = NULL;
  46. TPM2B_CREATION_DATA *creationData = NULL;
  47. TPM2B_DIGEST *creationHash = NULL;
  48. TPMT_TK_CREATION *creationTicket = NULL;
  49. TPM2B_PUBLIC *outPublic2 = NULL;
  50. TPM2B_PRIVATE *outPrivate2 = NULL;
  51. TPM2B_CREATION_DATA *creationData2 = NULL;
  52. TPM2B_DIGEST *creationHash2 = NULL;
  53. TPMT_TK_CREATION *creationTicket2 = NULL;
  54. TPMS_CONTEXT *context = NULL;
  55. TPM2B_AUTH authValuePrimary = {
  56. .size = 5,
  57. .buffer = {1, 2, 3, 4, 5}
  58. };
  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. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  73. #ifdef TEST_ECC
  74. TPM2B_PUBLIC inPublic = {
  75. .size = 0,
  76. .publicArea = {
  77. .type = TPM2_ALG_ECC,
  78. .nameAlg = TPM2_ALG_SHA256,
  79. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  80. TPMA_OBJECT_RESTRICTED |
  81. TPMA_OBJECT_SIGN_ENCRYPT |
  82. TPMA_OBJECT_FIXEDTPM |
  83. TPMA_OBJECT_FIXEDPARENT |
  84. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  85. .authPolicy = {
  86. .size = 0,
  87. },
  88. .parameters.eccDetail = {
  89. .symmetric = {
  90. .algorithm = TPM2_ALG_NULL,
  91. .keyBits.aes = 128,
  92. .mode.aes = TPM2_ALG_CFB,
  93. },
  94. .scheme = {
  95. .scheme = TPM2_ALG_ECDSA,
  96. .details = {
  97. .ecdsa = {.hashAlg = TPM2_ALG_SHA256}},
  98. },
  99. .curveID = TPM2_ECC_NIST_P256,
  100. .kdf = {
  101. .scheme = TPM2_ALG_NULL,
  102. .details = {}}
  103. },
  104. .unique.ecc = {
  105. .x = {.size = 0,.buffer = {}},
  106. .y = {.size = 0,.buffer = {}},
  107. },
  108. },
  109. };
  110. LOG_INFO("\nECC key will be created.");
  111. #else
  112. TPM2B_PUBLIC inPublic = {
  113. .size = 0,
  114. .publicArea = {
  115. .type = TPM2_ALG_RSA,
  116. .nameAlg = TPM2_ALG_SHA256,
  117. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  118. TPMA_OBJECT_RESTRICTED |
  119. TPMA_OBJECT_DECRYPT |
  120. TPMA_OBJECT_FIXEDTPM |
  121. TPMA_OBJECT_FIXEDPARENT |
  122. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  123. .authPolicy = {
  124. .size = 0,
  125. },
  126. .parameters.rsaDetail = {
  127. .symmetric = {
  128. .algorithm = TPM2_ALG_AES,
  129. .keyBits.aes = 128,
  130. .mode.aes = TPM2_ALG_CFB},
  131. .scheme = {
  132. .scheme = TPM2_ALG_NULL
  133. },
  134. .keyBits = 2048,
  135. .exponent = 0,
  136. },
  137. .unique.rsa = {
  138. .size = 0,
  139. .buffer = {},
  140. },
  141. },
  142. };
  143. LOG_INFO("\nRSA key will be created.");
  144. #endif /* TEST_ECC */
  145. TPM2B_DATA outsideInfo = {
  146. .size = 0,
  147. .buffer = {},
  148. };
  149. TPML_PCR_SELECTION creationPCR = {
  150. .count = 0,
  151. };
  152. TPM2B_AUTH authValue = {
  153. .size = 0,
  154. .buffer = {}
  155. };
  156. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  157. goto_if_error(r, "Error: TR_SetAuth", error);
  158. RSRC_NODE_T *primaryHandle_node;
  159. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  160. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  161. &outsideInfo, &creationPCR, &primaryHandle,
  162. &outPublic, &creationData, &creationHash,
  163. &creationTicket);
  164. goto_if_error(r, "Error esys create primary", error);
  165. r = esys_GetResourceObject(esys_context, primaryHandle,
  166. &primaryHandle_node);
  167. goto_if_error(r, "Error Esys GetResourceObject", error);
  168. LOG_INFO("Created Primary with handle 0x%08x...",
  169. primaryHandle_node->rsrc.handle);
  170. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  171. goto_if_error(r, "Error: TR_SetAuth", error);
  172. TPM2B_AUTH authKey2 = {
  173. .size = 6,
  174. .buffer = {6, 7, 8, 9, 10, 11}
  175. };
  176. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  177. .size = 0,
  178. .sensitive = {
  179. .userAuth = {
  180. .size = 0,
  181. .buffer = {0}
  182. },
  183. .data = {
  184. .size = 0,
  185. .buffer = {}
  186. }
  187. }
  188. };
  189. inSensitive2.sensitive.userAuth = authKey2;
  190. TPM2B_SENSITIVE_CREATE inSensitive3 = {
  191. .size = 0,
  192. .sensitive = {
  193. .userAuth = {
  194. .size = 0,
  195. .buffer = {}
  196. },
  197. .data = {
  198. .size = 0,
  199. .buffer = {}
  200. }
  201. }
  202. };
  203. TPM2B_PUBLIC inPublic2 = {
  204. .size = 0,
  205. .publicArea = {
  206. .type = TPM2_ALG_RSA,
  207. .nameAlg = TPM2_ALG_SHA256,
  208. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  209. TPMA_OBJECT_RESTRICTED |
  210. TPMA_OBJECT_DECRYPT |
  211. TPMA_OBJECT_FIXEDTPM |
  212. TPMA_OBJECT_FIXEDPARENT |
  213. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  214. .authPolicy = {
  215. .size = 0,
  216. },
  217. .parameters.rsaDetail = {
  218. .symmetric = {
  219. .algorithm = TPM2_ALG_AES,
  220. .keyBits.aes = 128,
  221. .mode.aes = TPM2_ALG_CFB
  222. },
  223. .scheme = {
  224. .scheme =
  225. TPM2_ALG_NULL,
  226. },
  227. .keyBits = 2048,
  228. .exponent = 0
  229. },
  230. .unique.rsa = {
  231. .size = 0,
  232. .buffer = {}
  233. ,
  234. }
  235. }
  236. };
  237. TPM2B_DATA outsideInfo2 = {
  238. .size = 0,
  239. .buffer = {}
  240. ,
  241. };
  242. TPML_PCR_SELECTION creationPCR2 = {
  243. .count = 0,
  244. };
  245. r = Esys_Create(esys_context,
  246. primaryHandle,
  247. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  248. &inSensitive2,
  249. &inPublic2,
  250. &outsideInfo2,
  251. &creationPCR2,
  252. &outPrivate2,
  253. &outPublic2,
  254. &creationData2, &creationHash2, &creationTicket2);
  255. goto_if_error(r, "Error esys create ", error);
  256. LOG_INFO("\nSecond key created.");
  257. r = Esys_Load(esys_context,
  258. primaryHandle,
  259. ESYS_TR_PASSWORD,
  260. ESYS_TR_NONE,
  261. ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle1);
  262. goto_if_error(r, "Error esys load ", error);
  263. Esys_Free(outPublic2);
  264. Esys_Free(outPrivate2);
  265. Esys_Free(creationData2);
  266. Esys_Free(creationHash2);
  267. Esys_Free(creationTicket2);
  268. LOG_INFO("\nSecond Key loaded.");
  269. r = Esys_ContextSave(esys_context, loadedKeyHandle1, &context);
  270. goto_if_error(r, "Error esys context save", error);
  271. r = Esys_FlushContext(esys_context, loadedKeyHandle1);
  272. goto_if_error(r, "Error esys flush context", error);
  273. loadedKeyHandle1 = ESYS_TR_NONE;
  274. r = Esys_ContextLoad(esys_context, context, &loadedKeyHandle2);
  275. goto_if_error(r, "Error esys context load", error);
  276. r = Esys_TR_SetAuth(esys_context, loadedKeyHandle2, &authKey2);
  277. goto_if_error(r, "Error esys TR_SetAuth ", error);
  278. r = Esys_Create(esys_context,
  279. loadedKeyHandle2,
  280. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  281. &inSensitive3,
  282. &inPublic2,
  283. &outsideInfo2,
  284. &creationPCR2,
  285. &outPrivate2,
  286. &outPublic2,
  287. &creationData2, &creationHash2, &creationTicket2);
  288. goto_if_error(r, "Error esys second create ", error);
  289. r = Esys_FlushContext(esys_context, primaryHandle);
  290. goto_if_error(r, "Error: FlushContext", error);
  291. primaryHandle = ESYS_TR_NONE;
  292. r = Esys_FlushContext(esys_context, loadedKeyHandle2);
  293. goto_if_error(r, "Error: FlushContext", error);
  294. Esys_Free(outPublic);
  295. Esys_Free(creationData);
  296. Esys_Free(creationHash);
  297. Esys_Free(creationTicket);
  298. Esys_Free(outPublic2);
  299. Esys_Free(outPrivate2);
  300. Esys_Free(creationData2);
  301. Esys_Free(creationHash2);
  302. Esys_Free(creationTicket2);
  303. Esys_Free(context);
  304. return EXIT_SUCCESS;
  305. error:
  306. if (loadedKeyHandle1 != ESYS_TR_NONE) {
  307. if (Esys_FlushContext(esys_context, loadedKeyHandle1) != TSS2_RC_SUCCESS) {
  308. LOG_ERROR("Cleanup loadedKeyHandle1 failed.");
  309. }
  310. }
  311. if (loadedKeyHandle2 != ESYS_TR_NONE) {
  312. if (Esys_FlushContext(esys_context, loadedKeyHandle2) != TSS2_RC_SUCCESS) {
  313. LOG_ERROR("Cleanup loadedKeyHandle2 failed.");
  314. }
  315. }
  316. if (primaryHandle != ESYS_TR_NONE) {
  317. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  318. LOG_ERROR("Cleanup primaryHandle failed.");
  319. }
  320. }
  321. Esys_Free(outPublic);
  322. Esys_Free(creationData);
  323. Esys_Free(creationHash);
  324. Esys_Free(creationTicket);
  325. Esys_Free(outPublic2);
  326. Esys_Free(outPrivate2);
  327. Esys_Free(creationData2);
  328. Esys_Free(creationHash2);
  329. Esys_Free(creationTicket2);
  330. Esys_Free(context);
  331. return EXIT_FAILURE;
  332. }
  333. int
  334. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  335. return test_esys_save_and_load_context(esys_context);
  336. }