fapi-export-policy.int.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <stdio.h>
  11. #include <inttypes.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <json-c/json.h>
  17. #include <json-c/json_util.h>
  18. #include "tss2_fapi.h"
  19. #include "tss2_esys.h"
  20. #include "tss2_tcti.h"
  21. #include "test-fapi.h"
  22. #define LOGMODULE test
  23. #include "util/log.h"
  24. #include "util/aux_util.h"
  25. typedef struct {
  26. char *path;
  27. char *sha1;
  28. char *sha256;
  29. } policy_digests;
  30. /** Check the digest values from result table for sha1 and sha256. */
  31. static bool
  32. check_policy(char *policy, policy_digests *digests) {
  33. json_object *jso = NULL;
  34. json_object *jso_digest_struct = NULL;
  35. json_object *jso_digest_list = NULL;
  36. json_object *jso_digest = NULL;
  37. const char *digest_str= NULL;
  38. bool check_sha1 = false;
  39. bool check_sha256 = false;
  40. int i, n;
  41. jso = json_tokener_parse(policy);
  42. if (!jso) {
  43. LOG_ERROR("JSON error in policy");
  44. goto error;
  45. }
  46. if (!json_object_object_get_ex(jso, "policyDigests", &jso_digest_list)) {
  47. LOG_ERROR("Policy error");
  48. goto error;
  49. }
  50. n = json_object_array_length(jso_digest_list);
  51. if (n > 0) {
  52. if (!digests->sha1 || !digests->sha256) {
  53. LOG_ERROR("Digest computation for %s should not be possible.", digests->path);
  54. goto error;
  55. }
  56. }
  57. for (i = 0; i < n; i++) {
  58. jso_digest_struct = json_object_array_get_idx(jso_digest_list, i);
  59. if (!jso_digest_struct) {
  60. LOG_ERROR("Policy error");
  61. goto error;
  62. }
  63. if (!json_object_object_get_ex(jso_digest_struct, "digest", &jso_digest)) {
  64. LOG_ERROR("Policy error2");
  65. goto error;
  66. }
  67. digest_str = json_object_get_string(jso_digest);
  68. if (strlen(digest_str) == 40) {
  69. LOG_INFO("Digest SHA1: %s", digest_str);
  70. if (strcmp(digest_str, digests->sha1) == 0) {
  71. check_sha1 = true;
  72. }
  73. } else if (strlen(digest_str) == 64) {
  74. LOG_INFO("Digest SHA256: %s", digest_str);
  75. if (strcmp(digest_str, digests->sha256) == 0) {
  76. check_sha256 = true;
  77. }
  78. } else {
  79. LOG_WARNING("Hash alg not in result table.");
  80. }
  81. }
  82. json_object_put(jso);
  83. if (n > 0 && (!check_sha1 || !check_sha256)) {
  84. LOG_ERROR("Policy check failed for: %s", digests->path);
  85. goto error;
  86. }
  87. return true;
  88. error:
  89. if (jso)
  90. json_object_put(jso);
  91. return false;
  92. }
  93. static char *
  94. read_policy(FAPI_CONTEXT *context, char *policy_name)
  95. {
  96. FILE *stream = NULL;
  97. long policy_size;
  98. char *json_policy = NULL;
  99. char policy_file[1024];
  100. if (snprintf(&policy_file[0], 1023, TOP_SOURCEDIR "/test/data/fapi/%s.json", policy_name) < 0)
  101. return NULL;
  102. stream = fopen(policy_file, "r");
  103. if (!stream) {
  104. LOG_ERROR("File %s does not exist", policy_file);
  105. return NULL;
  106. }
  107. fseek(stream, 0L, SEEK_END);
  108. policy_size = ftell(stream);
  109. fclose(stream);
  110. json_policy = malloc(policy_size + 1);
  111. stream = fopen(policy_file, "r");
  112. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  113. if (ret != policy_size) {
  114. LOG_ERROR("IO error %s.", policy_file);
  115. return NULL;
  116. }
  117. json_policy[policy_size] = '\0';
  118. return json_policy;
  119. }
  120. /** Test the FAPI key signing with PolicyAuthorizeNV.
  121. *
  122. * Tested FAPI commands:
  123. * - Fapi_Provision()
  124. * - Fapi_Import()
  125. * - Fapi_ExportPolicy()
  126. *
  127. * @param[in,out] context The FAPI_CONTEXT.
  128. * @retval EXIT_FAILURE
  129. * @retval EXIT_SUCCESS
  130. */
  131. int
  132. test_fapi_export_policy(FAPI_CONTEXT *context)
  133. {
  134. TSS2_RC r;
  135. size_t i;
  136. /*
  137. * Table with expected policy digests.
  138. * If computation is not possible sha1 and sha256 has to be set to NULL.
  139. * If a policy digest will be computed for these cases an error will be signalled.
  140. */
  141. static policy_digests policies[] = {
  142. { .path = "/policy/pol_action",
  143. .sha1 = "0000000000000000000000000000000000000000",
  144. .sha256 = "0000000000000000000000000000000000000000000000000000000000000000" },
  145. { .path = "/policy/pol_pcr16_0_ecc_authorized",
  146. .sha1 = "eab0d71ae6088009cbd0b50729fde69eb453649c",
  147. .sha256 = "bff2d58e9813f97cefc14f72ad8133bc7092d652b7c877959254af140c841f36" },
  148. { .path = "/policy/pol_authorize_ecc_pem",
  149. .sha1 = "bf566648f8842b5eda9b1900804c7b151b01df28",
  150. .sha256 = "a1eecf1da5fe2f66457af9fd06e6b23b10440da0d593914ff3f38533cafe218e" },
  151. { .path = "/policy/pol_nv_counter", .sha1 = NULL, .sha256 = NULL },
  152. { .path = "/policy/pol_authorize_rsa_pem",
  153. .sha1 = "47ae64686921c18c71c97704d9d728618c9a3ffc",
  154. .sha256 = "a91f9dce9c7a913ffa21bfdf70ff3ccd62231ca1041722fad4ac89f789458207" },
  155. { .path = "/policy/pol_locality",
  156. .sha1 = "9d2af7c7235047d90719bb07e699bc266554997f",
  157. .sha256 = "ddee6af14bf3c4e8127ced87bcf9a57e1c0c8ddb5e67735c8505f96f07b8dbb8" },
  158. { .path = "/policy/pol_nv_change_auth",
  159. .sha1 = "9ebf6fd0f5547da6c57280ae4032c2de62b773da",
  160. .sha256 = "363ac945b6457c47c31f3355dba0db27de8db213d6250c6bf79685003f9fe7ab" },
  161. { .path = "/policy/pol_password",
  162. .sha1 = "af6038c78c5c962d37127e319124e3a8dc582e9b",
  163. .sha256 = "8fcd2169ab92694e0c633f1ab772842b8241bbc20288981fc7ac1eddc1fddb0e" },
  164. { .path = "/policy/pol_pcr16_0_or",
  165. .sha1 = "be5ea4b5e7de56f883968667f1f12f02b7c3c99b",
  166. .sha256 = "04b01d728fc1ea060d943b3ca6e3e5ea9d3bbb61126542677ad7591c092eafba" },
  167. { .path = "/policy/pol_physical_presence",
  168. .sha1 = "9acb06395f831f88e89eeac29442cb0ebe9485ab",
  169. .sha256 = "0d7c6747b1b9facbba03492097aa9d5af792e5efc07346e05f9daa8b3d9e13b5" },
  170. { .path = "/policy/pol_secret", .sha1 = NULL, .sha256 = NULL },
  171. { .path = "/policy/pol_authorize", .sha1 = NULL, .sha256 = NULL },
  172. { .path = "/policy/pol_authorize_nv", .sha1 = NULL, .sha256 = NULL },
  173. { .path = "/policy/pol_auth_value",
  174. .sha1 = "af6038c78c5c962d37127e319124e3a8dc582e9b",
  175. .sha256 = "8fcd2169ab92694e0c633f1ab772842b8241bbc20288981fc7ac1eddc1fddb0e" },
  176. { .path = "/policy/pol_command_code",
  177. .sha1 = "2a2a1493809bbc1b4b46fc325dc54a815cbb980e",
  178. .sha256 = "cc6918b226273b08f5bd406d7f10cf160f0a7d13dfd83b7770ccbcd1aa80d811" },
  179. { .path = "/policy/pol_duplicate", .sha1 = NULL, .sha256 = NULL },
  180. { .path = "/policy/pol_pcr16_0",
  181. .sha1 = "eab0d71ae6088009cbd0b50729fde69eb453649c",
  182. .sha256 = "bff2d58e9813f97cefc14f72ad8133bc7092d652b7c877959254af140c841f36" },
  183. { .path = "/policy/pol_nv", .sha1 = NULL, .sha256 = NULL },
  184. { .path = "/policy/pol_authorize_outer", .sha1 = NULL, .sha256 = NULL },
  185. { .path = "/policy/pol_countertimer",
  186. .sha1 = "969a04fb820cc4a3aa4436e02cd5a71a87fd95b9",
  187. .sha256 = "7c67802209683d17c1d94f3fc9df7afb2a0d7955c3c5d0fa3f602d58ffdaf984" },
  188. { .path = "/policy/pol_cphash",
  189. .sha1 = "b2b2763b9a638a8e4f38897a47468e09fe0a0853",
  190. .sha256 = "2d7038734b12258ae7108ab70d0e7ee36f4e64c64d53f8adb6c2bed602c95d09" },
  191. { .path = "/policy/pol_name_hash", .sha1 = NULL, .sha256 = NULL },
  192. { .path = "/policy/pol_nv_written",
  193. .sha1 = "5a91e7105386bd547a15aad40369b1e25e462873",
  194. .sha256 = "3c326323670e28ad37bd57f63b4cc34d26ab205ef22f275c58d47fab2485466e" },
  195. { .path = "/policy/pol_pcr16_0_fail",
  196. .sha1 = "904fbcef1d6cb3ecaf085259b40b55fa3025232f",
  197. .sha256 = "b740077197d46009b9c18f5ad181b7a3ac5bef1d9a881cc5dde808f1a6b8c787" },
  198. { .path = "/policy/pol_pcr16_read",
  199. .sha1 = "eab0d71ae6088009cbd0b50729fde69eb453649c",
  200. .sha256 = "bff2d58e9813f97cefc14f72ad8133bc7092d652b7c877959254af140c841f36" },
  201. { .path = "/policy/pol_pcr8_0",
  202. .sha1 = "d6756ffbe88b1d082ee7048500ff9cc1a718de40",
  203. .sha256 = "2a90ac03196573f129e70a9e04485bff581d2890fe5882d3c2667290d84b497b" },
  204. { .path = "/policy/pol_signed_ecc",
  205. .sha1 = "c18fc6f175bd17e41c257184443a81c99ced9225",
  206. .sha256 = "07aefc36fb098f5f59f2f74d8854235a29d1f93b4ddd488f6ec667d9c1d716b6" },
  207. { .path = "/policy/pol_signed",
  208. .sha1 = "85bf0403e87d3c29c3daa5c87efb111cb717875d",
  209. .sha256 = "b1969529af7796c5b4f5e4781713f4525049f36cb12ec63f996dad1c4401c068" },
  210. };
  211. char *json_policy = NULL;
  212. char *policy = NULL;
  213. r = Fapi_Provision(context, NULL, NULL, NULL);
  214. goto_if_error(r, "Error Fapi_Provision", error);
  215. r = pcr_reset(context, 16);
  216. goto_if_error(r, "Error pcr_reset", error);
  217. for (i = 0; i < sizeof(policies) / sizeof(policies[0]); i++) {
  218. fprintf(stderr, "\nTest policy: %s\n", policies[i].path);
  219. json_policy = read_policy(context, policies[i].path);
  220. if (!json_policy)
  221. goto error;
  222. r = Fapi_Import(context, policies[i].path, json_policy);
  223. goto_if_error(r, "Error Fapi_Import", error);
  224. policy = NULL;
  225. r = Fapi_ExportPolicy(context, policies[i].path, &policy);
  226. goto_if_error(r, "Error Fapi_ExportPolicy", error);
  227. ASSERT(policy != NULL);
  228. ASSERT(strlen(policy) > ASSERT_SIZE);
  229. if (!check_policy(policy, &policies[i])) {
  230. goto error;
  231. }
  232. fprintf(stderr, "\nPolicy from policy file:\n%s\n%s\n", policies[i].path, policy);
  233. SAFE_FREE(json_policy);
  234. SAFE_FREE(policy);
  235. }
  236. r = Fapi_Delete(context, "/");
  237. goto_if_error(r, "Error Fapi_Delete", error);
  238. return EXIT_SUCCESS;
  239. error:
  240. Fapi_Delete(context, "/");
  241. SAFE_FREE(json_policy);
  242. SAFE_FREE(policy);
  243. return EXIT_FAILURE;
  244. }
  245. int
  246. test_invoke_fapi(FAPI_CONTEXT *context)
  247. {
  248. return test_fapi_export_policy(context);
  249. }