esys-make-credential.int.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 the function Esys_MakeCredential
  16. * We start by creating a primary key (Esys_CreatePrimary).
  17. *
  18. * Based in the primary a second key will be created.
  19. * The public part of the key will be loaded by the function
  20. * Esys_LoadExternal. A credential will be encrypted with this
  21. * key with the command Esys_MakeCredential. The credential
  22. * will be activated with Esys_ActivateCredential.
  23. *
  24. * Tested ESYS commands:
  25. * - Esys_ActivateCredential() (M)
  26. * - Esys_Create() (M)
  27. * - Esys_CreatePrimary() (M)
  28. * - Esys_FlushContext() (M)
  29. * - Esys_Load() (M)
  30. * - Esys_LoadExternal() (M)
  31. * - Esys_MakeCredential() (M)
  32. * - Esys_ReadPublic() (M)
  33. * - Esys_StartAuthSession() (M)
  34. *
  35. * Used compiler defines: TEST_SESSION
  36. *
  37. * @param[in,out] esys_context The ESYS_CONTEXT.
  38. * @retval EXIT_FAILURE
  39. * @retval EXIT_SUCCESS
  40. */
  41. int
  42. test_esys_make_credential(ESYS_CONTEXT * esys_context)
  43. {
  44. TSS2_RC r;
  45. ESYS_TR primaryHandle = ESYS_TR_NONE;
  46. ESYS_TR loadedKeyHandle = ESYS_TR_NONE;
  47. TPM2B_PUBLIC *outPublic = NULL;
  48. TPM2B_CREATION_DATA *creationData = NULL;
  49. TPM2B_DIGEST *creationHash = NULL;
  50. TPMT_TK_CREATION *creationTicket = NULL;
  51. TPM2B_PUBLIC *outPublic2 = NULL;
  52. TPM2B_PRIVATE *outPrivate2 = NULL;
  53. TPM2B_CREATION_DATA *creationData2 = NULL;
  54. TPM2B_DIGEST *creationHash2 = NULL;
  55. TPMT_TK_CREATION *creationTicket2 = NULL;
  56. TPM2B_PUBLIC *primaryKeyPublic = NULL;
  57. TPM2B_NAME *primaryKeyName = NULL;
  58. TPM2B_NAME *primaryKeyQualifiedName = NULL;
  59. TPM2B_ID_OBJECT *credentialBlob = NULL;
  60. TPM2B_ENCRYPTED_SECRET *secret = NULL;
  61. TPM2B_DIGEST *certInfo = NULL;
  62. #ifdef TEST_SESSION
  63. ESYS_TR session = ESYS_TR_NONE;
  64. ESYS_TR session2 = ESYS_TR_NONE;
  65. TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_AES,
  66. .keyBits = {.aes = 128},
  67. .mode = {.aes = TPM2_ALG_CFB}
  68. };
  69. TPMA_SESSION sessionAttributes;
  70. TPM2B_NONCE nonceCaller = {
  71. .size = 20,
  72. .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  73. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
  74. };
  75. memset(&sessionAttributes, 0, sizeof sessionAttributes);
  76. RSRC_NODE_T *session_node;
  77. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  78. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  79. &nonceCaller,
  80. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  81. &session);
  82. goto_if_error(r, "Error: During initialization of session", error);
  83. r = esys_GetResourceObject(esys_context, session,
  84. &session_node);
  85. goto_if_error(r, "Error Esys GetResourceObject", error);
  86. LOG_INFO("Created session with handle 0x%08x...",
  87. session_node->rsrc.handle);
  88. RSRC_NODE_T *session2_node;
  89. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  90. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  91. &nonceCaller,
  92. TPM2_SE_HMAC, &symmetric, TPM2_ALG_SHA256,
  93. &session2);
  94. goto_if_error(r, "Error: During initialization of session", error);
  95. r = esys_GetResourceObject(esys_context, session2,
  96. &session2_node);
  97. goto_if_error(r, "Error Esys GetResourceObject", error);
  98. LOG_INFO("Created session2 with handle 0x%08x...",
  99. session2_node->rsrc.handle);
  100. #endif /* TEST_SESSION */
  101. TPM2B_AUTH authValuePrimary = {
  102. .size = 5,
  103. .buffer = {1, 2, 3, 4, 5}
  104. };
  105. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  106. .size = 0,
  107. .sensitive = {
  108. .userAuth = {
  109. .size = 0,
  110. .buffer = {0 },
  111. },
  112. .data = {
  113. .size = 0,
  114. .buffer = {0},
  115. },
  116. },
  117. };
  118. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  119. TPM2B_PUBLIC inPublic = {
  120. .size = 0,
  121. .publicArea = {
  122. .type = TPM2_ALG_RSA,
  123. .nameAlg = TPM2_ALG_SHA256,
  124. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  125. TPMA_OBJECT_RESTRICTED |
  126. TPMA_OBJECT_DECRYPT |
  127. TPMA_OBJECT_FIXEDTPM |
  128. TPMA_OBJECT_FIXEDPARENT |
  129. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  130. .authPolicy = {
  131. .size = 0,
  132. },
  133. .parameters.rsaDetail = {
  134. .symmetric = {
  135. .algorithm = TPM2_ALG_AES,
  136. .keyBits.aes = 128,
  137. .mode.aes = TPM2_ALG_CFB},
  138. .scheme = {
  139. .scheme = TPM2_ALG_NULL
  140. },
  141. .keyBits = 2048,
  142. .exponent = 0,
  143. },
  144. .unique.rsa = {
  145. .size = 0,
  146. .buffer = {},
  147. },
  148. },
  149. };
  150. LOG_INFO("\nRSA key will be created.");
  151. TPM2B_DATA outsideInfo = {
  152. .size = 0,
  153. .buffer = {},
  154. };
  155. TPML_PCR_SELECTION creationPCR = {
  156. .count = 0,
  157. };
  158. TPM2B_AUTH authValue = {
  159. .size = 0,
  160. .buffer = {}
  161. };
  162. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  163. goto_if_error(r, "Error: TR_SetAuth", error);
  164. RSRC_NODE_T *primaryHandle_node;
  165. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  166. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  167. &outsideInfo, &creationPCR, &primaryHandle,
  168. &outPublic, &creationData, &creationHash,
  169. &creationTicket);
  170. goto_if_error(r, "Error esys create primary", error);
  171. r = esys_GetResourceObject(esys_context, primaryHandle,
  172. &primaryHandle_node);
  173. goto_if_error(r, "Error Esys GetResourceObject", error);
  174. LOG_INFO("Created Primary with handle 0x%08x...",
  175. primaryHandle_node->rsrc.handle);
  176. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  177. goto_if_error(r, "Error: TR_SetAuth", error);
  178. TPM2B_AUTH authKey2 = {
  179. .size = 6,
  180. .buffer = {6, 7, 8, 9, 10, 11}
  181. };
  182. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  183. .size = 0,
  184. .sensitive = {
  185. .userAuth = {
  186. .size = 0,
  187. .buffer = {0}
  188. },
  189. .data = {
  190. .size = 0,
  191. .buffer = {}
  192. }
  193. }
  194. };
  195. inSensitive2.sensitive.userAuth = authKey2;
  196. TPM2B_PUBLIC inPublic2 = {
  197. .size = 0,
  198. .publicArea = {
  199. .type = TPM2_ALG_RSA,
  200. .nameAlg = TPM2_ALG_SHA256,
  201. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  202. TPMA_OBJECT_RESTRICTED |
  203. TPMA_OBJECT_DECRYPT |
  204. TPMA_OBJECT_FIXEDTPM |
  205. TPMA_OBJECT_FIXEDPARENT |
  206. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  207. .authPolicy = {
  208. .size = 0,
  209. },
  210. .parameters.rsaDetail = {
  211. .symmetric = {
  212. .algorithm = TPM2_ALG_AES,
  213. .keyBits.aes = 128,
  214. .mode.aes = TPM2_ALG_CFB
  215. },
  216. .scheme = {
  217. .scheme =
  218. TPM2_ALG_NULL,
  219. },
  220. .keyBits = 2048,
  221. .exponent = 0
  222. },
  223. .unique.rsa = {
  224. .size = 0,
  225. .buffer = {}
  226. ,
  227. }
  228. }
  229. };
  230. TPM2B_DATA outsideInfo2 = {
  231. .size = 0,
  232. .buffer = {}
  233. ,
  234. };
  235. TPML_PCR_SELECTION creationPCR2 = {
  236. .count = 0,
  237. };
  238. r = Esys_Create(esys_context,
  239. primaryHandle,
  240. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  241. &inSensitive2,
  242. &inPublic2,
  243. &outsideInfo2,
  244. &creationPCR2,
  245. &outPrivate2,
  246. &outPublic2,
  247. &creationData2, &creationHash2, &creationTicket2);
  248. goto_if_error(r, "Error esys create ", error);
  249. LOG_INFO("\nSecond key created.");
  250. /*
  251. * Their is no individual stand-alone test for Esys_LoadExternal, so modify
  252. * a single Esys_LoadExternal call to test that the backwards compat change
  253. * from TPM2_RH to ESYS_TR works as expected. Their are other Esys_LoadExternal
  254. * calls that use the expected ESYS_TR type.
  255. *
  256. * For more details, see:
  257. * - https://github.com/tpm2-software/tpm2-tss/issues/1750
  258. */
  259. r = Esys_LoadExternal(esys_context,
  260. ESYS_TR_NONE,
  261. ESYS_TR_NONE,
  262. ESYS_TR_NONE,
  263. NULL,
  264. outPublic2,
  265. TPM2_RH_OWNER,
  266. &loadedKeyHandle);
  267. goto_if_error(r, "Error esys load external", error);
  268. r = Esys_ReadPublic(esys_context,
  269. primaryHandle,
  270. ESYS_TR_NONE,
  271. ESYS_TR_NONE,
  272. ESYS_TR_NONE,
  273. &primaryKeyPublic,
  274. &primaryKeyName,
  275. &primaryKeyQualifiedName);
  276. goto_if_error(r, "Error esys read public", error);
  277. TPM2B_DIGEST credential = {
  278. .size = 20,
  279. .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  280. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}};
  281. r = Esys_MakeCredential(esys_context,
  282. loadedKeyHandle,
  283. ESYS_TR_NONE,
  284. ESYS_TR_NONE,
  285. ESYS_TR_NONE,
  286. &credential,
  287. primaryKeyName,
  288. &credentialBlob,
  289. &secret
  290. );
  291. goto_if_error(r, "Error: MakeCredential", error);
  292. r = Esys_FlushContext(esys_context, loadedKeyHandle);
  293. goto_if_error(r, "Error esys flush context", error);
  294. r = Esys_Load(esys_context,
  295. primaryHandle,
  296. #ifdef TEST_SESSION
  297. session,
  298. #else
  299. ESYS_TR_PASSWORD,
  300. #endif
  301. ESYS_TR_NONE,
  302. ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle);
  303. goto_if_error(r, "Error esys load ", error);
  304. LOG_INFO("\nSecond Key loaded.");
  305. r = Esys_TR_SetAuth(esys_context, loadedKeyHandle, &authKey2);
  306. goto_if_error(r, "Error: TR_SetAuth", error);
  307. r = Esys_ActivateCredential(esys_context,
  308. primaryHandle,
  309. loadedKeyHandle,
  310. #ifdef TEST_SESSION
  311. session,
  312. #else
  313. ESYS_TR_PASSWORD,
  314. #endif
  315. #ifdef TEST_SESSION
  316. session2,
  317. #else
  318. ESYS_TR_PASSWORD,
  319. #endif
  320. ESYS_TR_NONE,
  321. credentialBlob,
  322. secret,
  323. &certInfo
  324. );
  325. goto_if_error(r, "Error: ActivateCredential", error);
  326. r = Esys_FlushContext(esys_context, primaryHandle);
  327. goto_if_error(r, "Error during FlushContext", error);
  328. r = Esys_FlushContext(esys_context, loadedKeyHandle);
  329. goto_if_error(r, "Error esys flush context", error);
  330. #ifdef TEST_SESSION
  331. r = Esys_FlushContext(esys_context, session);
  332. goto_if_error(r, "Flushing context", error);
  333. r = Esys_FlushContext(esys_context, session2);
  334. goto_if_error(r, "Flushing context", error);
  335. #endif
  336. Esys_Free(outPublic);
  337. Esys_Free(creationData);
  338. Esys_Free(creationHash);
  339. Esys_Free(creationTicket);
  340. Esys_Free(outPublic2);
  341. Esys_Free(outPrivate2);
  342. Esys_Free(creationData2);
  343. Esys_Free(creationHash2);
  344. Esys_Free(creationTicket2);
  345. Esys_Free(primaryKeyPublic);
  346. Esys_Free(primaryKeyName);
  347. Esys_Free(primaryKeyQualifiedName);
  348. Esys_Free(credentialBlob);
  349. Esys_Free(secret);
  350. Esys_Free(certInfo);
  351. return EXIT_SUCCESS;
  352. error:
  353. #ifdef TEST_SESSION
  354. if (session != ESYS_TR_NONE) {
  355. if (Esys_FlushContext(esys_context, session) != TSS2_RC_SUCCESS) {
  356. LOG_ERROR("Cleanup session failed.");
  357. }
  358. }
  359. if (session2 != ESYS_TR_NONE) {
  360. if (Esys_FlushContext(esys_context, session2) != TSS2_RC_SUCCESS) {
  361. LOG_ERROR("Cleanup session2 failed.");
  362. }
  363. }
  364. #endif
  365. if (loadedKeyHandle != ESYS_TR_NONE) {
  366. if (Esys_FlushContext(esys_context, loadedKeyHandle) != TSS2_RC_SUCCESS) {
  367. LOG_ERROR("Cleanup loadedKeyHandle failed.");
  368. }
  369. }
  370. if (primaryHandle != ESYS_TR_NONE) {
  371. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  372. LOG_ERROR("Cleanup primaryHandle failed.");
  373. }
  374. }
  375. Esys_Free(outPublic);
  376. Esys_Free(creationData);
  377. Esys_Free(creationHash);
  378. Esys_Free(creationTicket);
  379. Esys_Free(outPublic2);
  380. Esys_Free(outPrivate2);
  381. Esys_Free(creationData2);
  382. Esys_Free(creationHash2);
  383. Esys_Free(creationTicket2);
  384. Esys_Free(primaryKeyPublic);
  385. Esys_Free(primaryKeyName);
  386. Esys_Free(primaryKeyQualifiedName);
  387. Esys_Free(credentialBlob);
  388. Esys_Free(secret);
  389. Esys_Free(certInfo);
  390. return EXIT_FAILURE;
  391. }
  392. int
  393. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  394. return test_esys_make_credential(esys_context);
  395. }