fapi-quote.int.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <json-c/json.h>
  16. #include <json-c/json_util.h>
  17. #include <json-c/json_tokener.h>
  18. #include "tss2_fapi.h"
  19. #include "test-fapi.h"
  20. #define LOGMODULE test
  21. #include "util/log.h"
  22. #include "util/aux_util.h"
  23. #define EVENT_SIZE 10
  24. #ifndef FAPI_PROFILE
  25. #define FAPI_PROFILE "P_ECC"
  26. #endif /* FAPI_PROFILE */
  27. /** Test the FAPI functions for quote commands.
  28. *
  29. * Tested FAPI commands:
  30. * - Fapi_Provision()
  31. * - Fapi_CreateKey()
  32. * - Fapi_PcrExtend()
  33. * - Fapi_Quote()
  34. * - Fapi_ExportKey()
  35. * - Fapi_Import()
  36. * - Fapi_PcrRead()
  37. * - Fapi_VerifyQuote()
  38. * - Fapi_List()
  39. * - Fapi_Delete()
  40. *
  41. * @param[in,out] context The FAPI_CONTEXT.
  42. * @retval EXIT_FAILURE
  43. * @retval EXIT_SUCCESS
  44. */
  45. int
  46. test_fapi_quote(FAPI_CONTEXT *context)
  47. {
  48. TSS2_RC r;
  49. json_object *jso = NULL;
  50. char *pubkey_pem = NULL;
  51. uint8_t *signature = NULL;
  52. char *quoteInfo = NULL;
  53. char *pcrEventLog = NULL;
  54. char *certificate = NULL;
  55. char *export_data = NULL;
  56. json_object *jso_public = NULL;
  57. uint8_t *pcr_digest = NULL;
  58. char *log = NULL;
  59. char *pathlist = NULL;
  60. uint8_t data[EVENT_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  61. size_t signatureSize = 0;
  62. uint32_t pcrList[1] = { 16 };
  63. size_t pcr_digest_size = 0;
  64. r = Fapi_Provision(context, NULL, NULL, NULL);
  65. goto_if_error(r, "Error Fapi_Provision", error);
  66. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", "sign,noDa", "", NULL);
  67. goto_if_error(r, "Error Fapi_CreateKey", error);
  68. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN " \
  69. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  70. goto_if_error(r, "Error Fapi_SetCertificate", error);
  71. uint8_t qualifyingData[20] = {
  72. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  73. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  74. };
  75. r = pcr_reset(context, 16);
  76. goto_if_error(r, "Error pcr_reset", error);
  77. r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
  78. goto_if_error(r, "Error Fapi_PcrExtend", error);
  79. r = Fapi_Quote(context, pcrList, 1, "HS/SRK/mySignKey",
  80. "TPM-Quote",
  81. qualifyingData, 20,
  82. &quoteInfo,
  83. &signature, &signatureSize,
  84. &pcrEventLog, &certificate);
  85. goto_if_error(r, "Error Fapi_Quote", error);
  86. ASSERT(quoteInfo != NULL);
  87. ASSERT(signature != NULL);
  88. ASSERT(pcrEventLog != NULL);
  89. ASSERT(certificate != NULL);
  90. ASSERT(strlen(quoteInfo) > ASSERT_SIZE);
  91. ASSERT(strlen(pcrEventLog) > ASSERT_SIZE);
  92. ASSERT(strlen(certificate) > ASSERT_SIZE);
  93. LOG_INFO("\npcrEventLog: %s\n", pcrEventLog);
  94. LOG_INFO("Quote Info:\n%s\n", quoteInfo);
  95. char *field_list_quote_info[] = { "attest", "attested", "pcrDigest" };
  96. CHECK_JSON_FIELDS(quoteInfo, field_list_quote_info, "", error);
  97. r = Fapi_ExportKey(context, "HS/SRK/mySignKey", NULL, &export_data);
  98. goto_if_error(r, "Export.", error);
  99. ASSERT(export_data != NULL);
  100. ASSERT(strlen(export_data) > ASSERT_SIZE);
  101. jso = json_tokener_parse(export_data);
  102. LOG_INFO("\nExported: %s\n", export_data);
  103. char *fields_export[] = { "pem_ext_public" };
  104. CHECK_JSON_FIELDS(export_data, fields_export, "BEGIN PUBLIC KEY", error);
  105. if (!jso || !json_object_object_get_ex(jso, "pem_ext_public", &jso_public)) {
  106. LOG_ERROR("No public key eyported.");
  107. goto error;
  108. }
  109. pubkey_pem = strdup(json_object_get_string(jso_public));
  110. if (!pubkey_pem) {
  111. LOG_ERROR("Out of memory.");
  112. goto error;
  113. }
  114. r = Fapi_Import(context, "/ext/myExtPubKey", pubkey_pem);
  115. goto_if_error(r, "Error Fapi_Import", error);
  116. r = Fapi_PcrRead(context, 16, &pcr_digest,
  117. &pcr_digest_size, &log);
  118. goto_if_error(r, "Error Fapi_PcrRead", error);
  119. ASSERT(pcr_digest != NULL);
  120. ASSERT(log != NULL);
  121. ASSERT(strlen(log) > ASSERT_SIZE);
  122. LOG_INFO("\nTEST_JSON\nLog:\n%s\nEND_JSON", log);
  123. LOG_INFO("Quote Info:\n%s\n", quoteInfo);
  124. const char *log_check_list[] =
  125. {
  126. "["
  127. " {"
  128. " \"recnum\":1,"
  129. " \"pcr\":16,"
  130. " \"digests\":["
  131. " {"
  132. " \"hashAlg\":\"SHA1\","
  133. " \"digest\":\"494179714a6cd627239dfededf2de9ef994caf03\""
  134. " },"
  135. " {"
  136. " \"hashAlg\":\"SHA256\","
  137. " \"digest\":\"1f825aa2f0020ef7cf91dfa30da4668d791c5d4824fc8e41354b89ec05795ab3\""
  138. " },"
  139. " {"
  140. " \"hashAlg\":\"SHA384\","
  141. " \"digest\":\"182e95266adff49059e706c61483478fe0688150c8d08b95fab5cfde961f12d903aaf44104af4ce72ba6a4bf20302b2e\""
  142. " },"
  143. " {"
  144. " \"hashAlg\":\"SHA512\","
  145. " \"digest\":\"0f89ee1fcb7b0a4f7809d1267a029719004c5a5e5ec323a7c3523a20974f9a3f202f56fadba4cd9e8d654ab9f2e96dc5c795ea176fa20ede8d854c342f903533\""
  146. " },"
  147. " {"
  148. " \"hashAlg\":\"SM3_256\","
  149. " \"digest\":\"24c898bdb4d258f9bebb2e820d4ed478a7c013b37bd9e5006515730c18a70416\""
  150. " }"
  151. " ],"
  152. " \"type\":\"tss2\","
  153. " \"sub_event\":{"
  154. " \"data\":\"00010203040506070809\","
  155. " \"event\":{"
  156. " \"test\":\"myfile\""
  157. " }"
  158. " }"
  159. " }"
  160. "]",
  161. "["
  162. " {"
  163. " \"recnum\":1,"
  164. " \"pcr\":16,"
  165. " \"digests\":["
  166. " {"
  167. " \"hashAlg\":\"SHA1\","
  168. " \"digest\":\"494179714a6cd627239dfededf2de9ef994caf03\""
  169. " },"
  170. " {"
  171. " \"hashAlg\":\"SHA256\","
  172. " \"digest\":\"1f825aa2f0020ef7cf91dfa30da4668d791c5d4824fc8e41354b89ec05795ab3\""
  173. " },"
  174. " {"
  175. " \"hashAlg\":\"SHA384\","
  176. " \"digest\":\"182e95266adff49059e706c61483478fe0688150c8d08b95fab5cfde961f12d903aaf44104af4ce72ba6a4bf20302b2e\""
  177. " },"
  178. " {"
  179. " \"hashAlg\":\"SHA512\","
  180. " \"digest\":\"0f89ee1fcb7b0a4f7809d1267a029719004c5a5e5ec323a7c3523a20974f9a3f202f56fadba4cd9e8d654ab9f2e96dc5c795ea176fa20ede8d854c342f903533\""
  181. " }"
  182. " ],"
  183. " \"type\":\"tss2\","
  184. " \"sub_event\":{"
  185. " \"data\":\"00010203040506070809\","
  186. " \"event\":{"
  187. " \"test\":\"myfile\""
  188. " }"
  189. " }"
  190. " }"
  191. "]",
  192. "["
  193. " {"
  194. " \"recnum\":1,"
  195. " \"pcr\":16,"
  196. " \"digests\":["
  197. " {"
  198. " \"hashAlg\":\"SHA1\","
  199. " \"digest\":\"494179714a6cd627239dfededf2de9ef994caf03\""
  200. " },"
  201. " {"
  202. " \"hashAlg\":\"SHA256\","
  203. " \"digest\":\"1f825aa2f0020ef7cf91dfa30da4668d791c5d4824fc8e41354b89ec05795ab3\""
  204. " },"
  205. " {"
  206. " \"hashAlg\":\"SHA384\","
  207. " \"digest\":\"182e95266adff49059e706c61483478fe0688150c8d08b95fab5cfde961f12d903aaf44104af4ce72ba6a4bf20302b2e\""
  208. " }"
  209. " ],"
  210. " \"type\":\"tss2\","
  211. " \"sub_event\":{"
  212. " \"data\":\"00010203040506070809\","
  213. " \"event\":{"
  214. " \"test\":\"myfile\""
  215. " }"
  216. " }"
  217. " }"
  218. "]",
  219. "["
  220. " {"
  221. " \"recnum\":1,"
  222. " \"pcr\":16,"
  223. " \"digests\":["
  224. " {"
  225. " \"hashAlg\":\"SHA1\","
  226. " \"digest\":\"494179714a6cd627239dfededf2de9ef994caf03\""
  227. " },"
  228. " {"
  229. " \"hashAlg\":\"SHA256\","
  230. " \"digest\":\"1f825aa2f0020ef7cf91dfa30da4668d791c5d4824fc8e41354b89ec05795ab3\""
  231. " }"
  232. " ],"
  233. " \"type\":\"tss2\","
  234. " \"sub_event\":{"
  235. " \"data\":\"00010203040506070809\","
  236. " \"event\":{"
  237. " \"test\":\"myfile\""
  238. " }"
  239. " }"
  240. " }"
  241. "]",
  242. "["
  243. " {"
  244. " \"recnum\":1,"
  245. " \"pcr\":16,"
  246. " \"digests\":["
  247. " {"
  248. " \"hashAlg\":\"SHA1\","
  249. " \"digest\":\"494179714a6cd627239dfededf2de9ef994caf03\""
  250. " }"
  251. " ],"
  252. " \"type\":\"tss2\","
  253. " \"sub_event\":{"
  254. " \"data\":\"00010203040506070809\","
  255. " \"event\":{"
  256. " \"test\":\"myfile\""
  257. " }"
  258. " }"
  259. " }"
  260. "]"
  261. };
  262. CHECK_JSON_LIST(log_check_list, log, error);
  263. r = Fapi_VerifyQuote(context, "HS/SRK/mySignKey",
  264. qualifyingData, 20, quoteInfo,
  265. signature, signatureSize, log);
  266. goto_if_error(r, "Error Fapi_Verfiy_Quote", error);
  267. LOG_INFO("\nVerifyQuote log: %s\n", log);
  268. CHECK_JSON_LIST(log_check_list, log, error);
  269. r = Fapi_List(context, "/", &pathlist);
  270. goto_if_error(r, "Pathlist", error);
  271. ASSERT(pathlist != NULL);
  272. ASSERT(strlen(pathlist) > ASSERT_SIZE);
  273. LOG_INFO("\nPathlist: %s\n", pathlist);
  274. char *check_pathlist =
  275. "/" FAPI_PROFILE "/HS/SRK:/" FAPI_PROFILE "/HS:/" FAPI_PROFILE "/LOCKOUT:/"
  276. FAPI_PROFILE "/HE/EK:/" FAPI_PROFILE "/HE:/" FAPI_PROFILE "/HN:/" FAPI_PROFILE
  277. "/HS/SRK/mySignKey:/ext/myExtPubKey";
  278. ASSERT(cmp_strtokens(pathlist, check_pathlist, ":"));
  279. LOG_INFO("\nPathlist: %s\n", check_pathlist);
  280. /* Invalidate qualifying data */
  281. qualifyingData[0] = 0;
  282. r = Fapi_VerifyQuote(context, "HS/SRK/mySignKey",
  283. qualifyingData, 20, quoteInfo,
  284. signature, signatureSize, log);
  285. if (r == TPM2_RC_SUCCESS) {
  286. LOG_ERROR("Invalid qualifying data was not detected.");
  287. goto error;
  288. }
  289. r = Fapi_Delete(context, "/");
  290. goto_if_error(r, "Error Fapi_Delete", error);
  291. json_object_put(jso);
  292. SAFE_FREE(pubkey_pem);
  293. SAFE_FREE(signature);
  294. SAFE_FREE(quoteInfo);
  295. SAFE_FREE(pcrEventLog);
  296. SAFE_FREE(certificate);
  297. SAFE_FREE(export_data);
  298. SAFE_FREE(pcr_digest);
  299. SAFE_FREE(log);
  300. SAFE_FREE(pathlist);
  301. return EXIT_SUCCESS;
  302. error:
  303. Fapi_Delete(context, "/");
  304. if (jso)
  305. json_object_put(jso);
  306. SAFE_FREE(pubkey_pem);
  307. SAFE_FREE(signature);
  308. SAFE_FREE(quoteInfo);
  309. SAFE_FREE(pcrEventLog);
  310. SAFE_FREE(certificate);
  311. SAFE_FREE(export_data);
  312. SAFE_FREE(pcr_digest);
  313. SAFE_FREE(log);
  314. SAFE_FREE(pathlist);
  315. return EXIT_FAILURE;
  316. }
  317. int
  318. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  319. {
  320. return test_fapi_quote(fapi_context);
  321. }