fapi-get-esys-blobs.int.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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_fapi.h"
  11. #include "tss2_esys.h"
  12. #include "test-fapi.h"
  13. #include "fapi_util.h"
  14. #include "fapi_int.h"
  15. #include "esys_iutil.h"
  16. #define LOGMODULE test
  17. #include "util/log.h"
  18. #include "util/aux_util.h"
  19. #include "tss2_mu.h"
  20. #define PASSWORD "abc"
  21. #define SIGN_TEMPLATE "sign,noDa"
  22. json_object *
  23. get_json_hex_string(const uint8_t *buffer, size_t size)
  24. {
  25. char hex_string[size * 2 + 1];
  26. for (size_t i = 0, off = 0; i < size; i++, off += 2) {
  27. sprintf(&hex_string[off], "%02x", buffer[i]);
  28. }
  29. hex_string[(size) * 2] = '\0';
  30. json_object *jso = json_object_new_string(hex_string);
  31. return jso;
  32. }
  33. static TSS2_RC
  34. auth_callback(
  35. char const *objectPath,
  36. char const *description,
  37. const char **auth,
  38. void *userData)
  39. {
  40. UNUSED(description);
  41. UNUSED(userData);
  42. if (!objectPath) {
  43. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  44. }
  45. *auth = PASSWORD;
  46. return TSS2_RC_SUCCESS;
  47. }
  48. /** Test the FAPI functions for TpmBlobs and certificates.
  49. *
  50. * Tested FAPI commands:
  51. * - Fapi_Provision()
  52. * - Fapi_SetAuthCB()
  53. * - Fapi_CreateKey()
  54. * - Fapi_GetEsysBlob()
  55. * - Fapi_ChangeAuth()
  56. * - Fapi_Delete()
  57. *
  58. * @param[in,out] context The FAPI_CONTEXT.
  59. * @retval EXIT_FAILURE
  60. * @retval EXIT_SUCCESS
  61. */
  62. int
  63. test_fapi_get_esys_blobs(FAPI_CONTEXT *context)
  64. {
  65. TSS2_RC r;
  66. char *publicKey = NULL;
  67. uint8_t *publicblob = NULL;
  68. uint8_t *privateblob = NULL;
  69. char *path_list = NULL;
  70. json_object *jso = NULL;
  71. char *nvPath = "/nv/Owner/myNV";
  72. uint8_t *data = NULL;
  73. size_t data_size;
  74. TPMS_CONTEXT key_context;
  75. size_t offset = 0;
  76. ESYS_TR esys_handle;
  77. uint8_t type;
  78. /* We need to reset the passwords again, in order to not brick physical TPMs */
  79. r = Fapi_Provision(context, NULL, NULL, NULL);
  80. goto_if_error(r, "Error Fapi_Provision", error);
  81. r = Fapi_SetAuthCB(context, auth_callback, NULL);
  82. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  83. /* Password set and noda set */
  84. r = Fapi_CreateNv(context, nvPath, "", 10, "", PASSWORD);
  85. goto_if_error(r, "Error Fapi_CreateNv", error);
  86. /* Create ESAPI NV object from path */
  87. r = Fapi_GetEsysBlob(context,nvPath, &type,
  88. &data, &data_size);
  89. goto_if_error(r, "Error Fapi_GetEsysBlob", error);
  90. ASSERT(data != NULL);
  91. if (type != FAPI_ESYSBLOB_DESERIALIZE) {
  92. LOG_ERROR("Invalid type");
  93. goto error;
  94. }
  95. r = Esys_TR_Deserialize(context->esys, data, data_size, &esys_handle);
  96. goto_if_error(r, "Object deserializs", error);
  97. SAFE_FREE(data);
  98. r = Fapi_Delete(context, nvPath);
  99. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  100. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE, "",
  101. PASSWORD);
  102. goto_if_error(r, "Error Fapi_CreateKey_Async", error);
  103. /* Create ESAPI key object from path */
  104. data = NULL;
  105. r = Fapi_GetEsysBlob(context, "HS/SRK/mySignKey", &type,
  106. &data, &data_size);
  107. goto_if_error(r, "Error Fapi_GetEsysBlob", error);
  108. ASSERT(data != NULL);
  109. if (type != FAPI_ESYSBLOB_CONTEXTLOAD) {
  110. LOG_ERROR("Invalid type");
  111. goto error;
  112. }
  113. r = Tss2_MU_TPMS_CONTEXT_Unmarshal(data, data_size, &offset, &key_context);
  114. goto_if_error(r, "Context unmarshal", error);
  115. r = Esys_ContextLoad(context->esys, &key_context, &esys_handle);
  116. goto_if_error(r, "Context load", error);
  117. /* Variables for signing test */
  118. TPM2B_DIGEST pcr_digest_zero = {
  119. .size = 32,
  120. .buffer = { 0 }
  121. };
  122. TPMT_SIG_SCHEME inScheme = { .scheme = TPM2_ALG_ECDSA,
  123. .details.ecdsa = TPM2_ALG_SHA256 };
  124. TPMT_TK_HASHCHECK hash_validation = {
  125. .tag = TPM2_ST_HASHCHECK,
  126. .hierarchy = TPM2_RH_OWNER,
  127. .digest = {0}
  128. };
  129. TPM2B_AUTH authValue = {
  130. .size = sizeof(PASSWORD),
  131. .buffer = { 0 }
  132. };
  133. memcpy(&authValue.buffer[0], PASSWORD, sizeof(PASSWORD));
  134. TPMT_SIGNATURE *signature = NULL;
  135. r = Esys_TR_SetAuth(context->esys, esys_handle, &authValue);
  136. goto_if_error(r, "Error: TR_SetAuth", error);
  137. /* Check whether ESYS key can be used for signing. */
  138. r = Esys_Sign(
  139. context->esys,
  140. esys_handle,
  141. ESYS_TR_PASSWORD,
  142. ESYS_TR_NONE,
  143. ESYS_TR_NONE,
  144. &pcr_digest_zero,
  145. &inScheme,
  146. &hash_validation,
  147. &signature);
  148. goto_if_error(r, "Error: Sign", error);
  149. SAFE_FREE(signature);
  150. r = Esys_FlushContext(context->esys, esys_handle);
  151. goto_if_error(r, "Flush Context", error);
  152. SAFE_FREE(data);
  153. /* Create ESAPI persistent key object from path */
  154. data = NULL;
  155. r = Fapi_GetEsysBlob(context, "HS/SRK", &type,
  156. &data, &data_size);
  157. goto_if_error(r, "Error Fapi_GetEsysBlob", error);
  158. ASSERT(data != NULL);
  159. if (type != FAPI_ESYSBLOB_DESERIALIZE) {
  160. LOG_ERROR("Invalid type");
  161. goto error;
  162. }
  163. r = Esys_TR_Deserialize(context->esys, data, data_size, &esys_handle);
  164. goto_if_error(r, "Object deserializs", error);
  165. TPM2B_PUBLIC inPublic = {
  166. .size = 0,
  167. .publicArea = {
  168. .type = TPM2_ALG_ECC,
  169. .nameAlg = TPM2_ALG_SHA256,
  170. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  171. TPMA_OBJECT_RESTRICTED |
  172. TPMA_OBJECT_SIGN_ENCRYPT |
  173. TPMA_OBJECT_FIXEDTPM |
  174. TPMA_OBJECT_FIXEDPARENT |
  175. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  176. .authPolicy = {
  177. .size = 0,
  178. },
  179. .parameters.eccDetail = {
  180. .symmetric = {
  181. .algorithm = TPM2_ALG_NULL,
  182. .keyBits.aes = 128,
  183. .mode.aes = TPM2_ALG_CFB,
  184. },
  185. .scheme = {
  186. .scheme = TPM2_ALG_ECDSA,
  187. .details = {
  188. .ecdsa = {.hashAlg = TPM2_ALG_SHA256}},
  189. },
  190. .curveID = TPM2_ECC_NIST_P256,
  191. .kdf = {
  192. .scheme = TPM2_ALG_NULL,
  193. .details = {}}
  194. },
  195. .unique.ecc = {
  196. .x = {.size = 0,.buffer = {}},
  197. .y = {.size = 0,.buffer = {}},
  198. },
  199. },
  200. };
  201. TPM2B_SENSITIVE_CREATE inSensitive = {
  202. .size = 0,
  203. .sensitive = {
  204. .userAuth = {
  205. .size = 0,
  206. .buffer = {0}
  207. },
  208. .data = {
  209. .size = 0,
  210. .buffer = {}
  211. }
  212. }
  213. };
  214. TPM2B_DATA outsideInfo = {
  215. .size = 0,
  216. .buffer = {}
  217. ,
  218. };
  219. TPML_PCR_SELECTION creationPCR = {
  220. .count = 0,
  221. };
  222. TPM2B_PUBLIC *outPublic = NULL;
  223. TPM2B_PRIVATE *outPrivate = NULL;
  224. r = Esys_Create(context->esys,
  225. esys_handle,
  226. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  227. &inSensitive,
  228. &inPublic,
  229. &outsideInfo,
  230. &creationPCR,
  231. &outPrivate,
  232. &outPublic,
  233. NULL,NULL, NULL);
  234. goto_if_error(r, "Error esys create ", error);
  235. SAFE_FREE(outPublic);
  236. SAFE_FREE(outPrivate);
  237. r = Fapi_Delete(context, "/");
  238. goto_if_error(r, "Error Fapi_Delete", error);
  239. json_object_put(jso);
  240. SAFE_FREE(data);
  241. SAFE_FREE(path_list);
  242. SAFE_FREE(publicblob);
  243. SAFE_FREE(privateblob);
  244. SAFE_FREE(publicKey);
  245. return EXIT_SUCCESS;
  246. error:
  247. if (jso)
  248. json_object_put(jso);
  249. SAFE_FREE(data);
  250. Fapi_Delete(context, "/");
  251. SAFE_FREE(path_list);
  252. SAFE_FREE(publicblob);
  253. SAFE_FREE(privateblob);
  254. SAFE_FREE(publicKey);
  255. return EXIT_FAILURE;
  256. }
  257. int
  258. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  259. {
  260. return test_fapi_get_esys_blobs(fapi_context);
  261. }