esys-create-session-auth.int.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 parameter encryption/decryption, session management,
  16. * hmac computation, and session key generation.
  17. *
  18. * We start by creating a primary key (Esys_CreatePrimary).
  19. * The primary key will be used as tpmKey for Esys_StartAuthSession. Parameter
  20. * encryption and decryption will be activated for the session.
  21. * The session will be used to Create a second key by Eys_Create (with password)
  22. * This key will be Loaded to and a third key will be created with the second
  23. * key as parent key (Esys_Create).
  24. * The type of encryptin can be selected by the compiler variables (-D option):
  25. * TEST_XOR_OBFUSCATION or TEST_AES_ENCRYPTION.
  26. * Secret exchange with a ECC key can be activated with the compiler variable
  27. * -D TEST_ECC.
  28. *
  29. * Tested ESYS commands:
  30. * - Esys_ContextLoad() (M)
  31. * - Esys_ContextSave() (M)
  32. * - Esys_Create() (M)
  33. * - Esys_CreatePrimary() (M)
  34. * - Esys_FlushContext() (M)
  35. * - Esys_Load() (M)
  36. * - Esys_StartAuthSession() (M)
  37. *
  38. * Used compiler defines: TEST_ECC, TEST_AES_ENCRYPTION, TEST_BOUND_SESSION
  39. * TEST_XOR_OBFUSCATION
  40. *
  41. * @param[in,out] esys_context The ESYS_CONTEXT.
  42. * @retval EXIT_FAILURE
  43. * @retval EXIT_SUCCESS
  44. */
  45. int
  46. test_esys_create_session_auth(ESYS_CONTEXT * esys_context)
  47. {
  48. TSS2_RC r;
  49. ESYS_TR primaryHandle = ESYS_TR_NONE;
  50. ESYS_TR loadedKeyHandle = ESYS_TR_NONE;
  51. #if defined(TEST_ECC) || !defined(TEST_NULL_BIND_NO_TPM_KEY)
  52. ESYS_TR primaryHandle_AuthSession = ESYS_TR_NONE;
  53. #endif
  54. ESYS_TR session = ESYS_TR_NONE;
  55. ESYS_TR outerSession = ESYS_TR_NONE;
  56. TPM2B_PUBLIC *outPublic = NULL;
  57. TPM2B_CREATION_DATA *creationData = NULL;
  58. TPM2B_DIGEST *creationHash = NULL;
  59. TPMT_TK_CREATION *creationTicket = NULL;
  60. #ifdef TEST_ECC
  61. TPM2B_PUBLIC *outPublicEcc = NULL;
  62. TPM2B_CREATION_DATA *creationDataEcc = NULL;
  63. TPM2B_DIGEST *creationHashEcc = NULL;
  64. TPMT_TK_CREATION *creationTicketEcc = NULL;
  65. #endif
  66. TPM2B_PUBLIC *outPublic2 = NULL;
  67. TPM2B_PRIVATE *outPrivate2 = NULL;
  68. TPM2B_CREATION_DATA *creationData2 = NULL;
  69. TPM2B_DIGEST *creationHash2 = NULL;
  70. TPMT_TK_CREATION *creationTicket2 = NULL;
  71. TPM2B_AUTH authValuePrimary = {
  72. .size = 5,
  73. .buffer = {1, 2, 3, 4, 5}
  74. };
  75. #ifdef TEST_LARGE_AUTH
  76. for (int i = 0; i < 33; i++)
  77. authValuePrimary.buffer[i] = i;
  78. authValuePrimary.size = 33;
  79. #endif
  80. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  81. .size = 0,
  82. .sensitive = {
  83. .userAuth = {
  84. .size = 0,
  85. .buffer = {0 },
  86. },
  87. .data = {
  88. .size = 0,
  89. .buffer = {0},
  90. },
  91. },
  92. };
  93. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  94. #ifdef TEST_ECC
  95. TPM2B_PUBLIC inPublicEcc = {
  96. .size = 0,
  97. .publicArea = {
  98. .type = TPM2_ALG_ECC,
  99. .nameAlg = TPM2_ALG_SHA256,
  100. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  101. TPMA_OBJECT_DECRYPT |
  102. TPMA_OBJECT_FIXEDTPM |
  103. TPMA_OBJECT_FIXEDPARENT |
  104. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  105. .authPolicy = {
  106. .size = 0,
  107. },
  108. .parameters.eccDetail = {
  109. .symmetric = {
  110. .algorithm = TPM2_ALG_NULL,
  111. .keyBits.aes = 128,
  112. .mode.aes = TPM2_ALG_CFB,
  113. },
  114. .scheme = {
  115. .scheme = TPM2_ALG_ECDH,
  116. .details = {
  117. .ecdh = {.hashAlg = TPM2_ALG_SHA256}},
  118. },
  119. .curveID = TPM2_ECC_NIST_P256,
  120. .kdf = {
  121. .scheme = TPM2_ALG_NULL,
  122. .details = {}}
  123. },
  124. .unique.ecc = {
  125. .x = {.size = 0,.buffer = {}},
  126. .y = {.size = 0,.buffer = {}},
  127. },
  128. },
  129. };
  130. LOG_INFO("\nECC key will be created.");
  131. #endif
  132. TPM2B_PUBLIC inPublic = {
  133. .size = 0,
  134. .publicArea = {
  135. .type = TPM2_ALG_RSA,
  136. .nameAlg = TPM2_ALG_SHA256,
  137. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  138. TPMA_OBJECT_RESTRICTED |
  139. TPMA_OBJECT_DECRYPT |
  140. TPMA_OBJECT_FIXEDTPM |
  141. TPMA_OBJECT_FIXEDPARENT |
  142. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  143. .authPolicy = {
  144. .size = 0,
  145. },
  146. .parameters.rsaDetail = {
  147. .symmetric = {
  148. .algorithm = TPM2_ALG_AES,
  149. .keyBits.aes = 128,
  150. .mode.aes = TPM2_ALG_CFB},
  151. .scheme = {
  152. .scheme = TPM2_ALG_NULL
  153. },
  154. .keyBits = 2048,
  155. .exponent = 0,
  156. },
  157. .unique.rsa = {
  158. .size = 0,
  159. .buffer = {},
  160. },
  161. },
  162. };
  163. TPM2B_DATA outsideInfo = {
  164. .size = 0,
  165. .buffer = {},
  166. };
  167. TPML_PCR_SELECTION creationPCR = {
  168. .count = 0,
  169. };
  170. TPM2B_AUTH authValue = {
  171. .size = 0,
  172. .buffer = {}
  173. };
  174. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  175. goto_if_error(r, "Error: TR_SetAuth", error);
  176. RSRC_NODE_T *primaryHandle_node;
  177. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  178. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  179. &outsideInfo, &creationPCR, &primaryHandle,
  180. &outPublic, &creationData, &creationHash,
  181. &creationTicket);
  182. goto_if_error(r, "Error esys create primary", error);
  183. r = esys_GetResourceObject(esys_context, primaryHandle,
  184. &primaryHandle_node);
  185. goto_if_error(r, "Error Esys GetResourceObject", error);
  186. LOG_INFO("Created Primary with handle 0x%08x...",
  187. primaryHandle_node->rsrc.handle);
  188. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  189. goto_if_error(r, "Error: TR_SetAuth", error);
  190. #ifdef TEST_ECC
  191. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  192. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublicEcc,
  193. &outsideInfo, &creationPCR, &primaryHandle_AuthSession,
  194. &outPublicEcc, &creationDataEcc, &creationHashEcc,
  195. &creationTicketEcc);
  196. goto_if_error(r, "Error esys create primary", error);
  197. r = Esys_TR_SetAuth(esys_context, primaryHandle_AuthSession, &authValuePrimary);
  198. goto_if_error(r, "Error: TR_SetAuth", error);
  199. #elif defined(TEST_BOUND_SESSION) || !defined(TEST_NULL_BIND_NO_TPM_KEY)
  200. primaryHandle_AuthSession = primaryHandle;
  201. #endif /* TEST_ECC */
  202. #if TEST_XOR_OBFUSCATION
  203. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_XOR,
  204. .keyBits = { .exclusiveOr = TPM2_ALG_SHA256 },
  205. .mode = {.aes = TPM2_ALG_CFB}};
  206. #elif TEST_AES_ENCRYPTION
  207. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_AES,
  208. .keyBits = {.aes = 128},
  209. .mode = {.aes = TPM2_ALG_CFB}};
  210. #else
  211. #error "TEST_XOR_OBFUSCATION or TEST_PARAM_ENCRYPTION not set"
  212. #endif
  213. TPMA_SESSION sessionAttributes;
  214. TPMA_SESSION sessionAttributes2;
  215. sessionAttributes = (TPMA_SESSION_DECRYPT |
  216. TPMA_SESSION_ENCRYPT |
  217. TPMA_SESSION_CONTINUESESSION);
  218. TPM2_SE sessionType = TPM2_SE_HMAC;
  219. TPMI_ALG_HASH authHash = TPM2_ALG_SHA256;
  220. r = Esys_StartAuthSession(esys_context,
  221. ESYS_TR_NONE, ESYS_TR_NONE,
  222. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  223. NULL,
  224. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  225. &outerSession);
  226. goto_if_error(r, "Error during Esys_StartAuthSession", error);
  227. r = Esys_TRSess_SetAttributes(esys_context, outerSession, TPMA_SESSION_AUDIT,
  228. TPMA_SESSION_AUDIT);
  229. goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
  230. #if defined(TEST_BOUND_SESSION)
  231. r = Esys_StartAuthSession(esys_context,
  232. primaryHandle_AuthSession,
  233. primaryHandle_AuthSession,
  234. outerSession, ESYS_TR_NONE, ESYS_TR_NONE,
  235. NULL,
  236. sessionType, &symmetric, authHash, &session);
  237. #elif defined(TEST_NULL_BIND_TPMKEY)
  238. r = Esys_StartAuthSession(esys_context,
  239. primaryHandle_AuthSession,
  240. ESYS_TR_RH_NULL,
  241. outerSession, ESYS_TR_NONE, ESYS_TR_NONE,
  242. NULL,
  243. sessionType, &symmetric, authHash, &session);
  244. #elif defined(TEST_NULL_BIND_NO_TPM_KEY)
  245. r = Esys_StartAuthSession(esys_context,
  246. ESYS_TR_NONE,
  247. ESYS_TR_RH_NULL,
  248. outerSession, ESYS_TR_NONE, ESYS_TR_NONE,
  249. NULL,
  250. sessionType, &symmetric, authHash, &session);
  251. #else
  252. r = Esys_StartAuthSession(esys_context,
  253. primaryHandle_AuthSession,
  254. ESYS_TR_NONE,
  255. outerSession, ESYS_TR_NONE, ESYS_TR_NONE,
  256. NULL,
  257. sessionType, &symmetric, authHash, &session);
  258. #endif
  259. Esys_FlushContext(esys_context, outerSession);
  260. goto_if_error(r, "Error during Esys_StartAuthSession", error);
  261. #ifdef TEST_ECC
  262. r = Esys_FlushContext(esys_context, primaryHandle_AuthSession);
  263. goto_if_error(r, "Error during FlushContext", error);
  264. #endif
  265. goto_if_error(r, "Error Esys_StartAuthSessiony", error);
  266. r = Esys_TRSess_SetAttributes(esys_context, session, sessionAttributes,
  267. 0xff);
  268. goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
  269. r = Esys_TRSess_GetAttributes(esys_context, session, &sessionAttributes2);
  270. goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
  271. if (sessionAttributes != sessionAttributes2) {
  272. LOG_ERROR("Session Attributes differ");
  273. goto error;
  274. }
  275. /* Save and load the session and test if the attributes are still OK. */
  276. TPMS_CONTEXT *contextBlob;
  277. r = Esys_ContextSave(esys_context, session, &contextBlob);
  278. goto_if_error(r, "Error during ContextSave", error);
  279. session = ESYS_TR_NONE;
  280. r = Esys_ContextLoad(esys_context, contextBlob, &session);
  281. goto_if_error(r, "Error during ContextLoad", error);
  282. free(contextBlob);
  283. r = Esys_TRSess_GetAttributes(esys_context, session, &sessionAttributes2);
  284. goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
  285. if (sessionAttributes != sessionAttributes2) {
  286. LOG_ERROR("Session Attributes differ");
  287. goto error;
  288. }
  289. TPM2B_AUTH authKey2 = {
  290. .size = 6,
  291. .buffer = {6, 7, 8, 9, 10, 11}
  292. };
  293. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  294. .size = 0,
  295. .sensitive = {
  296. .userAuth = {
  297. .size = 0,
  298. .buffer = {0}
  299. },
  300. .data = {
  301. .size = 0,
  302. .buffer = {}
  303. }
  304. }
  305. };
  306. inSensitive2.sensitive.userAuth = authKey2;
  307. TPM2B_SENSITIVE_CREATE inSensitive3 = {
  308. .size = 0,
  309. .sensitive = {
  310. .userAuth = {
  311. .size = 0,
  312. .buffer = {}
  313. },
  314. .data = {
  315. .size = 0,
  316. .buffer = {}
  317. }
  318. }
  319. };
  320. TPM2B_PUBLIC inPublic2 = {
  321. .size = 0,
  322. .publicArea = {
  323. .type = TPM2_ALG_RSA,
  324. .nameAlg = TPM2_ALG_SHA256,
  325. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  326. TPMA_OBJECT_RESTRICTED |
  327. TPMA_OBJECT_DECRYPT |
  328. TPMA_OBJECT_FIXEDTPM |
  329. TPMA_OBJECT_FIXEDPARENT |
  330. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  331. .authPolicy = {
  332. .size = 0,
  333. },
  334. .parameters.rsaDetail = {
  335. .symmetric = {
  336. .algorithm = TPM2_ALG_AES,
  337. .keyBits.aes = 128,
  338. .mode.aes = TPM2_ALG_CFB
  339. },
  340. .scheme = {
  341. .scheme =
  342. TPM2_ALG_NULL,
  343. },
  344. .keyBits = 2048,
  345. .exponent = 0
  346. },
  347. .unique.rsa = {
  348. .size = 0,
  349. .buffer = {}
  350. ,
  351. }
  352. }
  353. };
  354. TPM2B_DATA outsideInfo2 = {
  355. .size = 0,
  356. .buffer = {}
  357. ,
  358. };
  359. TPML_PCR_SELECTION creationPCR2 = {
  360. .count = 0,
  361. };
  362. r = Esys_Create(esys_context,
  363. primaryHandle,
  364. session, ESYS_TR_NONE, ESYS_TR_NONE,
  365. &inSensitive2,
  366. &inPublic2,
  367. &outsideInfo2,
  368. &creationPCR2,
  369. &outPrivate2,
  370. &outPublic2,
  371. &creationData2, &creationHash2, &creationTicket2);
  372. goto_if_error(r, "Error esys create ", error);
  373. LOG_INFO("\nSecond key created.");
  374. r = Esys_Load(esys_context,
  375. primaryHandle,
  376. session,
  377. ESYS_TR_NONE,
  378. ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle);
  379. goto_if_error(r, "Error esys load ", error);
  380. LOG_INFO("\nSecond Key loaded.");
  381. r = Esys_TR_SetAuth(esys_context, loadedKeyHandle, &authKey2);
  382. goto_if_error(r, "Error esys TR_SetAuth ", error);
  383. Esys_Free(outPublic2);
  384. Esys_Free(outPrivate2);
  385. Esys_Free(creationData2);
  386. Esys_Free(creationHash2);
  387. Esys_Free(creationTicket2);
  388. r = Esys_Create(esys_context,
  389. loadedKeyHandle,
  390. session, ESYS_TR_NONE, ESYS_TR_NONE,
  391. &inSensitive3,
  392. &inPublic2,
  393. &outsideInfo2,
  394. &creationPCR2,
  395. &outPrivate2,
  396. &outPublic2,
  397. &creationData2, &creationHash2, &creationTicket2);
  398. goto_if_error(r, "Error esys second create ", error);
  399. r = Esys_FlushContext(esys_context, primaryHandle);
  400. goto_if_error(r, "Error during FlushContext", error);
  401. r = Esys_FlushContext(esys_context, loadedKeyHandle);
  402. goto_if_error(r, "Error during FlushContext", error);
  403. r = Esys_FlushContext(esys_context, session);
  404. goto_if_error(r, "Flushing context", error);
  405. Esys_Free(outPublic);
  406. Esys_Free(creationData);
  407. Esys_Free(creationHash);
  408. Esys_Free(creationTicket);
  409. #ifdef TEST_ECC
  410. Esys_Free(outPublicEcc);
  411. Esys_Free(creationDataEcc);
  412. Esys_Free(creationHashEcc);
  413. Esys_Free(creationTicketEcc);
  414. #endif
  415. Esys_Free(outPublic2);
  416. Esys_Free(outPrivate2);
  417. Esys_Free(creationData2);
  418. Esys_Free(creationHash2);
  419. Esys_Free(creationTicket2);
  420. return EXIT_SUCCESS;
  421. error:
  422. if (session != ESYS_TR_NONE) {
  423. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  424. LOG_ERROR("Cleanup session failed.");
  425. }
  426. }
  427. if (loadedKeyHandle != ESYS_TR_NONE) {
  428. if (Esys_FlushContext(esys_context, loadedKeyHandle) != TSS2_RC_SUCCESS) {
  429. LOG_ERROR("Cleanup loadedKeyHandle failed.");
  430. }
  431. }
  432. if (primaryHandle != ESYS_TR_NONE) {
  433. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  434. LOG_ERROR("Cleanup primaryHandle failed.");
  435. }
  436. }
  437. #ifdef TEST_ECC
  438. if (primaryHandle_AuthSession != ESYS_TR_NONE) {
  439. if (Esys_FlushContext(esys_context, primaryHandle_AuthSession) != TSS2_RC_SUCCESS) {
  440. LOG_ERROR("Cleanup primaryHandle_AuthSession failed.");
  441. }
  442. }
  443. #endif
  444. Esys_Free(outPublic);
  445. Esys_Free(creationData);
  446. Esys_Free(creationHash);
  447. Esys_Free(creationTicket);
  448. #ifdef TEST_ECC
  449. Esys_Free(outPublicEcc);
  450. Esys_Free(creationDataEcc);
  451. Esys_Free(creationHashEcc);
  452. Esys_Free(creationTicketEcc);
  453. #endif
  454. Esys_Free(outPublic2);
  455. Esys_Free(outPrivate2);
  456. Esys_Free(creationData2);
  457. Esys_Free(creationHash2);
  458. Esys_Free(creationTicket2);
  459. return EXIT_FAILURE;
  460. }
  461. int
  462. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  463. return test_esys_create_session_auth(esys_context);
  464. }