fapi-key-create-policy-authorize-sign.int.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 <stdbool.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include "tss2_fapi.h"
  17. #include "test-fapi.h"
  18. #define LOGMODULE test
  19. #include "util/log.h"
  20. #include "util/aux_util.h"
  21. static bool cb_called = false;
  22. #define OBJECT_PATH "HS/SRK/mySignKey"
  23. #define USER_DATA "my user data"
  24. #define DESCRIPTION "PolicyAuthorize"
  25. static TSS2_RC
  26. branch_callback(
  27. char const *objectPath,
  28. char const *description MAYBE_UNUSED,
  29. char const **branchNames MAYBE_UNUSED,
  30. size_t numBranches,
  31. size_t *selectedBranch,
  32. void *userData MAYBE_UNUSED)
  33. {
  34. char *profile_path;
  35. ASSERT(description != NULL);
  36. ASSERT(userData != NULL);
  37. ASSERT(branchNames != NULL);
  38. if (!objectPath) {
  39. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  40. }
  41. int size = asprintf (&profile_path, "%s/%s", fapi_profile, OBJECT_PATH);
  42. if (size == -1)
  43. return TSS2_FAPI_RC_MEMORY;
  44. ASSERT(strlen(objectPath) == strlen(profile_path));
  45. free(profile_path);
  46. ASSERT(strlen(userData) == strlen((char*)USER_DATA));
  47. ASSERT(strlen(description) == strlen(DESCRIPTION));
  48. if (numBranches != 2) {
  49. LOG_ERROR("Wrong number of branches");
  50. return TSS2_FAPI_RC_GENERAL_FAILURE;
  51. }
  52. if (!strcmp(branchNames[0], "/policy/pol_name_hash"))
  53. *selectedBranch = 0;
  54. else if (!strcmp(branchNames[1], "/policy/pol_name_hash"))
  55. *selectedBranch = 1;
  56. else {
  57. LOG_ERROR("BranchName not found. Got \"%s\" and \"%s\"",
  58. branchNames[0], branchNames[1]);
  59. return TSS2_FAPI_RC_GENERAL_FAILURE;
  60. }
  61. cb_called = true;
  62. return TSS2_RC_SUCCESS;
  63. error:
  64. exit(EXIT_FAILURE);
  65. }
  66. /** Test the FAPI functions for PolicyAuthoirze with signing.
  67. *
  68. * Tested FAPI commands:
  69. * - Fapi_Provision()
  70. * - Fapi_SetBranchCB()
  71. * - Fapi_Import()
  72. * - Fapi_CreateKey()
  73. * - Fapi_AuthorizePolicy()
  74. * - Fapi_Sign()
  75. * - Fapi_List()
  76. * - Fapi_Delete()
  77. *
  78. * Tested Policies:
  79. * - PolicyNameHash
  80. * - PolicyAuthorize
  81. * - PolicyCpHash (Not entered, only as alternative branch)
  82. *
  83. * @param[in,out] context The FAPI_CONTEXT.
  84. * @retval EXIT_FAILURE
  85. * @retval EXIT_SUCCESS
  86. */
  87. int
  88. test_fapi_key_create_policy_authorize_sign(FAPI_CONTEXT *context)
  89. {
  90. TSS2_RC r;
  91. char *policy_name_hash = "/policy/pol_name_hash";
  92. char *policy_file_name_hash = TOP_SOURCEDIR "/test/data/fapi/policy/pol_name_hash.json";
  93. /* This policy cannot succeed, but that's the intention. We authorize it but then choose
  94. the other policy from branch selection. */
  95. char *policy_cphash = "/policy/pol_cphash";
  96. char *policy_file_cphash = TOP_SOURCEDIR "/test/data/fapi/policy/pol_cphash.json";
  97. char *policy_name_authorize = "/policy/pol_authorize";
  98. char *policy_file_authorize = TOP_SOURCEDIR "/test/data/fapi/policy/pol_authorize.json";
  99. char *policy_name_authorize_outer = "/policy/pol_authorize_outer";
  100. char *policy_file_authorize_outer = TOP_SOURCEDIR
  101. "/test/data/fapi/policy/pol_authorize_outer.json";
  102. uint8_t policyRef[] = { 1, 2, 3, 4, 5 };
  103. FILE *stream = NULL;
  104. char *json_policy = NULL;
  105. long policy_size;
  106. uint8_t *signature = NULL;
  107. char *publicKey = NULL;
  108. char *certificate = NULL;
  109. char *pathList = NULL;
  110. r = Fapi_List(context, "/", &pathList);
  111. if (r != TSS2_FAPI_RC_NOT_PROVISIONED) {
  112. LOG_ERROR("It was not detected that provisioning was not made");
  113. goto error;
  114. }
  115. r = Fapi_Provision(context, NULL, NULL, NULL);
  116. goto_if_error(r, "Error Fapi_Provision", error);
  117. r = Fapi_List(context, "/P_DOES_NOT_EXIST", &pathList);
  118. if (r != TSS2_FAPI_RC_NOT_PROVISIONED) {
  119. LOG_ERROR("It was not detected that provisioning was not made");
  120. goto error;
  121. }
  122. r = Fapi_List(context, "/SRK/DOES_NOT_EXIST", &pathList);
  123. if (r != TSS2_FAPI_RC_PATH_NOT_FOUND) {
  124. LOG_ERROR("It was not detected that provisioning was not made");
  125. goto error;
  126. }
  127. r = pcr_reset(context, 16);
  128. goto_if_error(r, "Error pcr_reset", error);
  129. r = Fapi_SetBranchCB(context, branch_callback, USER_DATA);
  130. goto_if_error(r, "Error SetPolicybranchselectioncallback", error);
  131. /* Read in the first policy */
  132. stream = fopen(policy_file_name_hash, "r");
  133. if (!stream) {
  134. LOG_ERROR("File %s does not exist", policy_file_name_hash);
  135. goto error;
  136. }
  137. fseek(stream, 0L, SEEK_END);
  138. policy_size = ftell(stream);
  139. fclose(stream);
  140. json_policy = malloc(policy_size + 1);
  141. goto_if_null(json_policy,
  142. "Could not allocate memory for the JSON policy",
  143. TSS2_FAPI_RC_MEMORY, error);
  144. stream = fopen(policy_file_name_hash, "r");
  145. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  146. if (ret != policy_size) {
  147. LOG_ERROR("IO error %s.", policy_file_name_hash);
  148. goto error;
  149. }
  150. json_policy[policy_size] = '\0';
  151. r = Fapi_Import(context, policy_name_hash, json_policy);
  152. SAFE_FREE(json_policy);
  153. goto_if_error(r, "Error Fapi_List", error);
  154. /* Read in the second policy */
  155. stream = fopen(policy_file_cphash, "r");
  156. if (!stream) {
  157. LOG_ERROR("File %s does not exist", policy_file_name_hash);
  158. goto error;
  159. }
  160. fseek(stream, 0L, SEEK_END);
  161. policy_size = ftell(stream);
  162. fclose(stream);
  163. json_policy = malloc(policy_size + 1);
  164. goto_if_null(json_policy,
  165. "Could not allocate memory for the JSON policy",
  166. TSS2_FAPI_RC_MEMORY, error);
  167. stream = fopen(policy_file_cphash, "r");
  168. ret = read(fileno(stream), json_policy, policy_size);
  169. if (ret != policy_size) {
  170. LOG_ERROR("IO error %s.", policy_file_name_hash);
  171. goto error;
  172. }
  173. json_policy[policy_size] = '\0';
  174. r = Fapi_Import(context, policy_cphash, json_policy);
  175. SAFE_FREE(json_policy);
  176. goto_if_error(r, "Error Fapi_List", error);
  177. /* Read in the third policy */
  178. stream = fopen(policy_file_authorize, "r");
  179. if (!stream) {
  180. LOG_ERROR("File %s does not exist", policy_file_authorize);
  181. goto error;
  182. }
  183. fseek(stream, 0L, SEEK_END);
  184. policy_size = ftell(stream);
  185. fclose(stream);
  186. json_policy = malloc(policy_size + 1);
  187. goto_if_null(json_policy,
  188. "Could not allocate memory for the JSON policy",
  189. TSS2_FAPI_RC_MEMORY, error);
  190. stream = fopen(policy_file_authorize, "r");
  191. ret = read(fileno(stream), json_policy, policy_size);
  192. if (ret != policy_size) {
  193. LOG_ERROR("IO error %s.", policy_file_authorize);
  194. goto error;
  195. }
  196. json_policy[policy_size] = '\0';
  197. r = Fapi_Import(context, policy_name_authorize, json_policy);
  198. SAFE_FREE(json_policy);
  199. goto_if_error(r, "Error Fapi_Import", error);
  200. /* Read in the fourth policy */
  201. stream = fopen(policy_file_authorize_outer, "r");
  202. if (!stream) {
  203. LOG_ERROR("File %s does not exist", policy_file_authorize_outer);
  204. goto error;
  205. }
  206. fseek(stream, 0L, SEEK_END);
  207. policy_size = ftell(stream);
  208. fclose(stream);
  209. json_policy = malloc(policy_size + 1);
  210. goto_if_null(json_policy,
  211. "Could not allocate memory for the JSON policy",
  212. TSS2_FAPI_RC_MEMORY, error);
  213. stream = fopen(policy_file_authorize_outer, "r");
  214. ret = read(fileno(stream), json_policy, policy_size);
  215. if (ret != policy_size) {
  216. LOG_ERROR("IO error %s.", policy_file_authorize_outer);
  217. goto error;
  218. }
  219. json_policy[policy_size] = '\0';
  220. r = Fapi_Import(context, policy_name_authorize_outer, json_policy);
  221. SAFE_FREE(json_policy);
  222. goto_if_error(r, "Error Fapi_Import", error);
  223. /* Create keys and use them to authorize the authorize policy */
  224. r = Fapi_CreateKey(context, "HS/SRK/myPolicySignKeyOuter", "sign,noDa",
  225. "", NULL);
  226. goto_if_error(r, "Error Fapi_CreateKey", error);
  227. /* Create keys and use them to authorize policies */
  228. r = Fapi_CreateKey(context, "HS/SRK/myPolicySignKey", "sign,noDa",
  229. "", NULL);
  230. goto_if_error(r, "Error Fapi_CreateKey", error);
  231. /* Create the actual key */
  232. r = Fapi_CreateKey(context, OBJECT_PATH, "sign, noda",
  233. policy_name_authorize_outer, NULL);
  234. goto_if_error(r, "Error Fapi_CreateKey", error);
  235. /* Authorize the policies in sequence. */
  236. r = Fapi_AuthorizePolicy(context, policy_name_authorize,
  237. "HS/SRK/myPolicySignKeyOuter", NULL, 0);
  238. goto_if_error(r, "Authorize policy", error);
  239. r = Fapi_AuthorizePolicy(context, policy_name_hash,
  240. "HS/SRK/myPolicySignKey", policyRef, sizeof(policyRef));
  241. goto_if_error(r, "Authorize policy", error);
  242. r = Fapi_AuthorizePolicy(context, policy_cphash,
  243. "HS/SRK/myPolicySignKey", policyRef, sizeof(policyRef));
  244. goto_if_error(r, "Authorize policy", error);
  245. /* The policy is authorized twice with idfferent keys in order to test the code that
  246. stores multiple authorizations inside the policy statements. */
  247. r = Fapi_CreateKey(context, "HS/SRK/myPolicySignKey2", "sign,noDa",
  248. "", NULL);
  249. goto_if_error(r, "Error Fapi_CreateKey", error);
  250. r = Fapi_SetCertificate(context, OBJECT_PATH, "-----BEGIN "\
  251. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  252. goto_if_error(r, "Error Fapi_CreateKey", error);
  253. /* Use the key */
  254. size_t signatureSize = 0;
  255. TPM2B_DIGEST digest = {
  256. .size = 32,
  257. .buffer = {
  258. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  259. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  260. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  261. 0x41, 0x42
  262. }
  263. };
  264. r = Fapi_Sign(context, OBJECT_PATH, NULL,
  265. &digest.buffer[0], digest.size, &signature, &signatureSize,
  266. &publicKey, &certificate);
  267. goto_if_error(r, "Error Fapi_Sign", error);
  268. ASSERT(signature != NULL);
  269. ASSERT(publicKey != NULL);
  270. ASSERT(certificate != NULL);
  271. LOG_INFO("Public key: %s", publicKey);
  272. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  273. LOG_INFO("Certificate: %s", certificate);
  274. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  275. r = Fapi_List(context, "/", &pathList);
  276. goto_if_error(r, "Error Fapi_List", error);
  277. ASSERT(pathList != NULL);
  278. LOG_INFO("Pathlist: %s", pathList);
  279. char *check_pathList1 =
  280. "/" FAPI_PROFILE "/HS/SRK:/" FAPI_PROFILE "/HS:/" FAPI_PROFILE "/LOCKOUT:/"
  281. FAPI_PROFILE "/HE/EK:/" FAPI_PROFILE "/HE:/" FAPI_PROFILE "/HN:/policy/pol_name_hash:"
  282. "/policy/pol_cphash:/policy/pol_authorize_outer:/policy/pol_authorize:/" FAPI_PROFILE
  283. "/HS/SRK/myPolicySignKey2:/" FAPI_PROFILE "/HS/SRK/myPolicySignKey:/" FAPI_PROFILE
  284. "/HS/SRK/mySignKey:/" FAPI_PROFILE "/HS/SRK/myPolicySignKeyOuter";
  285. ASSERT(cmp_strtokens(pathList, check_pathList1, ":"));
  286. SAFE_FREE(pathList);
  287. pathList = NULL;
  288. r = Fapi_List(context, "/SRK/", &pathList);
  289. goto_if_error(r, "Error Fapi_List", error);
  290. ASSERT(pathList != NULL);
  291. LOG_INFO("Pathlist: %s", pathList);
  292. char *check_pathList2 =
  293. "/" FAPI_PROFILE "/HS/SRK:/" FAPI_PROFILE "/HS/SRK/myPolicySignKey2:/" FAPI_PROFILE
  294. "/HS/SRK/myPolicySignKey:/" FAPI_PROFILE "/HS/SRK/mySignKey:/" FAPI_PROFILE
  295. "/HS/SRK/myPolicySignKeyOuter";
  296. ASSERT(cmp_strtokens(pathList, check_pathList2, ":"));
  297. SAFE_FREE(pathList);
  298. pathList = NULL;
  299. r = Fapi_List(context, "/HS/", &pathList);
  300. goto_if_error(r, "Error Fapi_List", error);
  301. ASSERT(pathList != NULL);
  302. LOG_INFO("Pathlist: %s", pathList);
  303. char *check_pathList3 =
  304. "/" FAPI_PROFILE "/HS/SRK:/" FAPI_PROFILE "/HS:/" FAPI_PROFILE"/HS/SRK/myPolicySignKey2:/"
  305. FAPI_PROFILE "/HS/SRK/myPolicySignKey:/" FAPI_PROFILE "/HS/SRK/mySignKey:/" FAPI_PROFILE
  306. "/HS/SRK/myPolicySignKeyOuter";
  307. ASSERT(cmp_strtokens(pathList, check_pathList3, ":"));
  308. SAFE_FREE(pathList);
  309. LOG_WARNING("Next is a failure-test, and we expect errors in the log");
  310. pathList = NULL;
  311. r = Fapi_List(context, "XXX", &pathList);
  312. if (r == TSS2_RC_SUCCESS) {
  313. LOG_ERROR("Path XXX was found");
  314. goto error;
  315. }
  316. ASSERT(pathList == NULL);
  317. SAFE_FREE(pathList);
  318. pathList = NULL;
  319. r = Fapi_List(context, "/policy/", &pathList);
  320. goto_if_error(r, "Error Fapi_List", error);
  321. ASSERT(pathList != NULL);
  322. ASSERT(strlen(pathList) > ASSERT_SIZE);
  323. LOG_INFO("Pathlist: %s", pathList);
  324. char *check_pathList4 =
  325. "/policy/pol_name_hash:/policy/pol_cphash:/policy/pol_authorize_outer:/policy/pol_authorize";
  326. ASSERT(cmp_strtokens(pathList, check_pathList4, ":"));
  327. SAFE_FREE(pathList);
  328. /* Cleanup */
  329. r = Fapi_Delete(context, "/");
  330. goto_if_error(r, "Error Fapi_Delete", error);
  331. SAFE_FREE(signature);
  332. SAFE_FREE(publicKey);
  333. SAFE_FREE(certificate);
  334. if (!cb_called) {
  335. LOG_ERROR("Branch selection callback was not called.");
  336. return EXIT_FAILURE;
  337. }
  338. return EXIT_SUCCESS;
  339. error:
  340. Fapi_Delete(context, "/");
  341. SAFE_FREE(json_policy);
  342. SAFE_FREE(signature);
  343. SAFE_FREE(publicKey);
  344. SAFE_FREE(certificate);
  345. SAFE_FREE(pathList);
  346. Fapi_Delete(context, "/");
  347. return EXIT_FAILURE;
  348. }
  349. int
  350. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  351. {
  352. return test_fapi_key_create_policy_authorize_sign(fapi_context);
  353. }