esys-create-policy-auth.int.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. #ifdef HAVE_CONFIG_H
  3. #include <config.h>
  4. #endif
  5. #include <stdlib.h>
  6. #include "tss2_esys.h"
  7. #include "esys_iutil.h"
  8. #define LOGMODULE test
  9. #include "util/log.h"
  10. #include "util/aux_util.h"
  11. /** This test is intended to test policy authentication for the ESYS command
  12. * Create.
  13. *
  14. * We start by creating a primary key with a password and policy.
  15. * Based in the primary a second key will be created using the prinary key's
  16. * policy for authorization.
  17. *
  18. * Tested ESYS commands:
  19. * - Esys_StartAuthSession() (M)
  20. * - Esys_PolicyCommandCode() (M)
  21. * - Esys_PolicyGetDigest() (M)
  22. * - Esys_FlushContext() (M)
  23. * - Esys_CreatePrimary() (M)
  24. * - Esys_Create() (M)
  25. *
  26. * Used compiler defines: TEST_ECC
  27. *
  28. * @param[in,out] esys_context The ESYS_CONTEXT.
  29. * @retval EXIT_FAILURE
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_create_policy_auth(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR primaryHandle = ESYS_TR_NONE;
  37. ESYS_TR trialHandle = ESYS_TR_NONE;
  38. ESYS_TR policyHandle = ESYS_TR_NONE;
  39. TPM2B_DIGEST *trialDigest = NULL;
  40. TPM2B_PUBLIC *outPublic = NULL;
  41. TPM2B_PUBLIC *outPublic2 = NULL;
  42. TPM2B_PRIVATE *outPrivate2 = NULL;
  43. TPMT_SYM_DEF policyAlgo = {
  44. .algorithm = TPM2_ALG_AES,
  45. .keyBits.aes = 128,
  46. .mode.aes = TPM2_ALG_CFB,
  47. };
  48. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  49. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  50. NULL, TPM2_SE_TRIAL, &policyAlgo,
  51. TPM2_ALG_SHA256, &trialHandle);
  52. goto_if_error(r, "Error esys start trial session", error);
  53. r = Esys_PolicyCommandCode(esys_context, trialHandle, ESYS_TR_NONE,
  54. ESYS_TR_NONE, ESYS_TR_NONE, TPM2_CC_Create);
  55. goto_if_error(r, "Error esys policy command code", error);
  56. r = Esys_PolicyGetDigest(esys_context, trialHandle, ESYS_TR_NONE,
  57. ESYS_TR_NONE, ESYS_TR_NONE, &trialDigest);
  58. goto_if_error(r, "Error esys policy get digest", error);
  59. r = Esys_FlushContext(esys_context, trialHandle);
  60. goto_if_error(r, "Error esys flush context", error);
  61. TPM2B_AUTH authValuePrimary = {
  62. .size = 5,
  63. .buffer = {1, 2, 3, 4, 5}
  64. };
  65. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  66. .size = 0,
  67. .sensitive = {
  68. .userAuth = {
  69. .size = 0,
  70. .buffer = {0},
  71. },
  72. .data = {
  73. .size = 0,
  74. .buffer = {0},
  75. },
  76. },
  77. };
  78. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  79. #ifdef TEST_ECC
  80. TPM2B_PUBLIC inPublic = {
  81. .size = 0,
  82. .publicArea = {
  83. .type = TPM2_ALG_ECC,
  84. .nameAlg = TPM2_ALG_SHA256,
  85. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  86. TPMA_OBJECT_RESTRICTED |
  87. TPMA_OBJECT_DECRYPT |
  88. TPMA_OBJECT_FIXEDTPM |
  89. TPMA_OBJECT_FIXEDPARENT |
  90. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  91. .authPolicy = *trialDigest,
  92. .parameters.eccDetail = {
  93. .symmetric = {
  94. .algorithm = TPM2_ALG_NULL,
  95. .keyBits.aes = 128,
  96. .mode.aes = TPM2_ALG_CFB,
  97. },
  98. .scheme = {
  99. .scheme = TPM2_ALG_ECDSA,
  100. .details = {
  101. .ecdsa = {.hashAlg = TPM2_ALG_SHA256}},
  102. },
  103. .curveID = TPM2_ECC_NIST_P256,
  104. .kdf = {
  105. .scheme = TPM2_ALG_NULL,
  106. .details = {}}
  107. },
  108. .unique.ecc = {
  109. .x = {.size = 0,.buffer = {}},
  110. .y = {.size = 0,.buffer = {}},
  111. },
  112. },
  113. };
  114. LOG_INFO("\nECC key will be created.");
  115. #else
  116. TPM2B_PUBLIC inPublic = {
  117. .size = 0,
  118. .publicArea = {
  119. .type = TPM2_ALG_RSA,
  120. .nameAlg = TPM2_ALG_SHA256,
  121. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  122. TPMA_OBJECT_RESTRICTED |
  123. TPMA_OBJECT_DECRYPT |
  124. TPMA_OBJECT_FIXEDTPM |
  125. TPMA_OBJECT_FIXEDPARENT |
  126. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  127. .authPolicy = *trialDigest,
  128. .parameters.rsaDetail = {
  129. .symmetric = {
  130. .algorithm = TPM2_ALG_AES,
  131. .keyBits.aes = 128,
  132. .mode.aes = TPM2_ALG_CFB},
  133. .scheme = {
  134. .scheme = TPM2_ALG_NULL
  135. },
  136. .keyBits = 2048,
  137. .exponent = 0,
  138. },
  139. .unique.rsa = {
  140. .size = 0,
  141. .buffer = {},
  142. },
  143. },
  144. };
  145. LOG_INFO("\nRSA key will be created.");
  146. #endif /* TEST_ECC */
  147. TPM2B_DATA outsideInfo = {
  148. .size = 0,
  149. .buffer = {},
  150. };
  151. TPML_PCR_SELECTION creationPCR = {
  152. .count = 0,
  153. };
  154. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  155. ESYS_TR_NONE, ESYS_TR_NONE,
  156. &inSensitivePrimary, &inPublic,
  157. &outsideInfo, &creationPCR, &primaryHandle,
  158. &outPublic, NULL, NULL, NULL);
  159. goto_if_error(r, "Error esys create primary", error);
  160. LOG_INFO("Created Primary Key");
  161. r = Esys_TR_SetAuth(esys_context, primaryHandle, &authValuePrimary);
  162. goto_if_error(r, "Error: TR_SetAuth", error);
  163. r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
  164. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  165. NULL, TPM2_SE_POLICY, &policyAlgo,
  166. TPM2_ALG_SHA256, &policyHandle);
  167. goto_if_error(r, "Error esys start policy session", error);
  168. r = Esys_PolicyCommandCode(esys_context, policyHandle, ESYS_TR_NONE,
  169. ESYS_TR_NONE, ESYS_TR_NONE, TPM2_CC_Create);
  170. goto_if_error(r, "Error esys policy command code", error);
  171. TPM2B_SENSITIVE_CREATE inSensitive2 = {
  172. .size = 0,
  173. .sensitive = {
  174. .userAuth = {
  175. .size = 0,
  176. .buffer = {0}
  177. },
  178. .data = {
  179. .size = 0,
  180. .buffer = {0}
  181. }
  182. }
  183. };
  184. TPM2B_PUBLIC inPublic2 = {
  185. .size = 0,
  186. .publicArea = {
  187. .type = TPM2_ALG_RSA,
  188. .nameAlg = TPM2_ALG_SHA256,
  189. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  190. TPMA_OBJECT_RESTRICTED |
  191. TPMA_OBJECT_DECRYPT |
  192. TPMA_OBJECT_FIXEDTPM |
  193. TPMA_OBJECT_FIXEDPARENT |
  194. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  195. .authPolicy = {
  196. .size = 0,
  197. },
  198. .parameters.rsaDetail = {
  199. .symmetric = {
  200. .algorithm = TPM2_ALG_AES,
  201. .keyBits.aes = 128,
  202. .mode.aes = TPM2_ALG_CFB
  203. },
  204. .scheme = {
  205. .scheme =
  206. TPM2_ALG_NULL,
  207. },
  208. .keyBits = 2048,
  209. .exponent = 0
  210. },
  211. .unique.rsa = {
  212. .size = 0,
  213. .buffer = {}
  214. ,
  215. }
  216. }
  217. };
  218. TPM2B_DATA outsideInfo2 = {
  219. .size = 0,
  220. .buffer = {}
  221. ,
  222. };
  223. TPML_PCR_SELECTION creationPCR2 = {
  224. .count = 0,
  225. };
  226. r = Esys_Create(esys_context, primaryHandle, policyHandle, ESYS_TR_NONE,
  227. ESYS_TR_NONE, &inSensitive2, &inPublic2, &outsideInfo2,
  228. &creationPCR2, &outPrivate2, &outPublic2, NULL, NULL, NULL);
  229. goto_if_error(r, "Error esys create ", error);
  230. LOG_INFO("\nSecond key created.");
  231. r = Esys_FlushContext(esys_context, policyHandle);
  232. policyHandle = ESYS_TR_NONE;
  233. goto_if_error(r, "Error during FlushContext", error);
  234. r = Esys_FlushContext(esys_context, primaryHandle);
  235. primaryHandle = ESYS_TR_NONE;
  236. goto_if_error(r, "Error during FlushContext", error);
  237. Esys_Free(trialDigest);
  238. Esys_Free(outPublic);
  239. Esys_Free(outPublic2);
  240. Esys_Free(outPrivate2);
  241. return EXIT_SUCCESS;
  242. error:
  243. if (trialHandle != ESYS_TR_NONE) {
  244. if (Esys_FlushContext(esys_context, trialHandle) != TSS2_RC_SUCCESS) {
  245. LOG_ERROR("Cleanup trialHandle failed.");
  246. }
  247. }
  248. if (policyHandle != ESYS_TR_NONE) {
  249. if (Esys_FlushContext(esys_context, policyHandle) != TSS2_RC_SUCCESS) {
  250. LOG_ERROR("Cleanup policyHandle failed.");
  251. }
  252. }
  253. if (primaryHandle != ESYS_TR_NONE) {
  254. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  255. LOG_ERROR("Cleanup primaryHandle failed.");
  256. }
  257. }
  258. Esys_Free(trialDigest);
  259. Esys_Free(outPublic);
  260. Esys_Free(outPublic2);
  261. Esys_Free(outPrivate2);
  262. return EXIT_FAILURE;
  263. }
  264. int
  265. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  266. return test_esys_create_policy_auth(esys_context);
  267. }