esys-create-password-auth.int.c 11 KB

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