esys-object-changeauth.int.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 ESYS command ObjectChangeAuth.
  16. *
  17. * We start by creating a primary key (Esys_CreatePrimary).
  18. * The auth value for this primary will be changed.
  19. *
  20. * Tested ESYS commands:
  21. * - Esys_Create() (M)
  22. * - Esys_CreatePrimary() (M)
  23. * - Esys_FlushContext() (M)
  24. * - Esys_Load() (M)
  25. * - Esys_ObjectChangeAuth() (M)
  26. *
  27. * @param[in,out] esys_context The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SUCCESS
  30. */
  31. int
  32. test_esys_object_changeauth(ESYS_CONTEXT * esys_context)
  33. {
  34. TSS2_RC r;
  35. ESYS_TR primaryHandle = ESYS_TR_NONE;
  36. ESYS_TR loadedKeyHandle = ESYS_TR_NONE;
  37. TPM2B_PUBLIC *outPublic = NULL;
  38. TPM2B_CREATION_DATA *creationData = NULL;
  39. TPM2B_DIGEST *creationHash = NULL;
  40. TPMT_TK_CREATION *creationTicket = NULL;
  41. TPM2B_PUBLIC *outPublic2 = NULL;
  42. TPM2B_PRIVATE *outPrivate2 = NULL;
  43. TPM2B_CREATION_DATA *creationData2 = NULL;
  44. TPM2B_DIGEST *creationHash2 = NULL;
  45. TPMT_TK_CREATION *creationTicket2 = NULL;
  46. TPM2B_PRIVATE *outPrivateChangeAuth = NULL;
  47. TPM2B_PUBLIC inPublic = {
  48. .size = 0,
  49. .publicArea = {
  50. .type = TPM2_ALG_RSA,
  51. .nameAlg = TPM2_ALG_SHA256,
  52. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  53. TPMA_OBJECT_RESTRICTED |
  54. TPMA_OBJECT_DECRYPT |
  55. TPMA_OBJECT_FIXEDTPM |
  56. TPMA_OBJECT_FIXEDPARENT |
  57. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  58. .authPolicy = {
  59. .size = 0,
  60. },
  61. .parameters.rsaDetail = {
  62. .symmetric = {
  63. .algorithm = TPM2_ALG_AES,
  64. .keyBits.aes = 128,
  65. .mode.aes = TPM2_ALG_CFB},
  66. .scheme = {
  67. .scheme = TPM2_ALG_NULL
  68. },
  69. .keyBits = 2048,
  70. .exponent = 0,
  71. },
  72. .unique.rsa = {
  73. .size = 0,
  74. .buffer = {},
  75. },
  76. },
  77. };
  78. TPM2B_AUTH authValuePrimary = {
  79. .size = 5,
  80. .buffer = {1, 2, 3, 4, 5}
  81. };
  82. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  83. .size = 0,
  84. .sensitive = {
  85. .userAuth = authValuePrimary,
  86. .data = {
  87. .size = 0,
  88. .buffer = {0},
  89. },
  90. },
  91. };
  92. TPM2B_DATA outsideInfo = {
  93. .size = 0,
  94. .buffer = {},
  95. };
  96. TPML_PCR_SELECTION creationPCR = {
  97. .count = 0,
  98. };
  99. TPM2B_AUTH authValue = {
  100. .size = 0,
  101. .buffer = {}
  102. };
  103. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  104. goto_if_error(r, "Error: TR_SetAuth", error);
  105. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  106. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary, &inPublic,
  107. &outsideInfo, &creationPCR, &primaryHandle,
  108. &outPublic, &creationData, &creationHash,
  109. &creationTicket);
  110. goto_if_error(r, "Error esys create primary", error);
  111. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  112. goto_if_error(r, "Error esys TR_SetAuth ", error);
  113. TPM2B_AUTH authKey2 = {
  114. .size = 6,
  115. .buffer = {6, 7, 8, 9, 10, 11}
  116. };
  117. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  118. .size = 0,
  119. .sensitive = {
  120. .userAuth = {
  121. .size = 0,
  122. .buffer = {0}
  123. },
  124. .data = {
  125. .size = 0,
  126. .buffer = {}
  127. }
  128. }
  129. };
  130. inSensitive2.sensitive.userAuth = authKey2;
  131. TPM2B_PUBLIC inPublic2 = {
  132. .size = 0,
  133. .publicArea = {
  134. .type = TPM2_ALG_RSA,
  135. .nameAlg = TPM2_ALG_SHA256,
  136. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  137. TPMA_OBJECT_RESTRICTED |
  138. TPMA_OBJECT_DECRYPT |
  139. TPMA_OBJECT_FIXEDTPM |
  140. TPMA_OBJECT_FIXEDPARENT |
  141. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  142. .authPolicy = {
  143. .size = 0,
  144. },
  145. .parameters.rsaDetail = {
  146. .symmetric = {
  147. .algorithm = TPM2_ALG_AES,
  148. .keyBits.aes = 128,
  149. .mode.aes = TPM2_ALG_CFB
  150. },
  151. .scheme = {
  152. .scheme =
  153. TPM2_ALG_NULL,
  154. },
  155. .keyBits = 2048,
  156. .exponent = 0
  157. },
  158. .unique.rsa = {
  159. .size = 0,
  160. .buffer = {}
  161. ,
  162. }
  163. }
  164. };
  165. TPM2B_DATA outsideInfo2 = {
  166. .size = 0,
  167. .buffer = {}
  168. ,
  169. };
  170. TPML_PCR_SELECTION creationPCR2 = {
  171. .count = 0,
  172. };
  173. r = Esys_Create(esys_context,
  174. primaryHandle,
  175. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  176. &inSensitive2,
  177. &inPublic2,
  178. &outsideInfo2,
  179. &creationPCR2,
  180. &outPrivate2,
  181. &outPublic2,
  182. &creationData2, &creationHash2, &creationTicket2);
  183. goto_if_error(r, "Error esys create ", error);
  184. r = Esys_Load(esys_context,
  185. primaryHandle,
  186. ESYS_TR_PASSWORD,
  187. ESYS_TR_NONE,
  188. ESYS_TR_NONE, outPrivate2, outPublic2, &loadedKeyHandle);
  189. goto_if_error(r, "Error esys load ", error);
  190. r = Esys_TR_SetAuth(esys_context, loadedKeyHandle, &authKey2);
  191. goto_if_error(r, "Error esys TR_SetAuth ", error);
  192. TPM2B_AUTH newAuth = {.size = 20,
  193. .buffer={30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  194. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49}};
  195. r = Esys_ObjectChangeAuth(esys_context,
  196. loadedKeyHandle,
  197. primaryHandle,
  198. ESYS_TR_PASSWORD,
  199. ESYS_TR_NONE,
  200. ESYS_TR_NONE,
  201. &newAuth,
  202. &outPrivateChangeAuth
  203. );
  204. goto_if_error(r, "Error: ObjectChangeAuth", error);
  205. r = Esys_FlushContext(esys_context, loadedKeyHandle);
  206. goto_if_error(r, "Error during FlushContext", error);
  207. r = Esys_FlushContext(esys_context, primaryHandle);
  208. goto_if_error(r, "Error during FlushContext", error);
  209. SAFE_FREE(outPublic);
  210. SAFE_FREE(creationData);
  211. SAFE_FREE(creationHash);
  212. SAFE_FREE(creationTicket);
  213. SAFE_FREE(outPublic2);
  214. SAFE_FREE(outPrivate2);
  215. SAFE_FREE(creationData2);
  216. SAFE_FREE(creationHash2);
  217. SAFE_FREE(creationTicket2);
  218. SAFE_FREE(outPrivateChangeAuth);
  219. return EXIT_SUCCESS;
  220. error:
  221. if (loadedKeyHandle != ESYS_TR_NONE) {
  222. if (Esys_FlushContext(esys_context, loadedKeyHandle) != TSS2_RC_SUCCESS) {
  223. LOG_ERROR("Cleanup loadedKeyHandle failed.");
  224. }
  225. }
  226. if (primaryHandle != ESYS_TR_NONE) {
  227. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  228. LOG_ERROR("Cleanup primaryHandle failed.");
  229. }
  230. }
  231. SAFE_FREE(outPublic);
  232. SAFE_FREE(creationData);
  233. SAFE_FREE(creationHash);
  234. SAFE_FREE(creationTicket);
  235. SAFE_FREE(outPublic2);
  236. SAFE_FREE(outPrivate2);
  237. SAFE_FREE(creationData2);
  238. SAFE_FREE(creationHash2);
  239. SAFE_FREE(creationTicket2);
  240. SAFE_FREE(outPrivateChangeAuth);
  241. return EXIT_FAILURE;
  242. }
  243. int
  244. test_esys_tr_setauth(ESYS_CONTEXT * esys_context)
  245. {
  246. TSS2_RC r;
  247. TPM2B_AUTH auth = {.size = 20,
  248. .buffer={30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  249. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49}};
  250. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &auth);
  251. return_if_error(r, "Error in Esys_TR_SetAuth");
  252. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, NULL);
  253. return_if_error(r, "Error in Esys_TR_SetAuth");
  254. return EXIT_SUCCESS;
  255. }
  256. int
  257. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  258. TSS2_RC r;
  259. r = test_esys_object_changeauth(esys_context);
  260. return_if_error(r, "test_esys_object_changeauth");
  261. r = test_esys_tr_setauth(esys_context);
  262. return_if_error(r, "test_esys_tr_setauth");
  263. return EXIT_SUCCESS;
  264. }