esys-create-fail.int.c 9.1 KB

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