fapi-policy-or-nv-read-write.int.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <assert.h>
  17. #include "tss2_fapi.h"
  18. #include "test-fapi.h"
  19. #define LOGMODULE test
  20. #include "util/log.h"
  21. #include "util/aux_util.h"
  22. #define PASSWORD1 "abc"
  23. #define PASSWORD2 "def"
  24. #define SIGN_TEMPLATE "sign,noDa"
  25. #define NV_SIZE 10
  26. static bool cb_branch_called = false;
  27. static bool cb_auth_called = false;
  28. static bool written = false;
  29. static TSS2_RC
  30. auth_callback(
  31. char const *objectPath,
  32. char const *description,
  33. const char **auth,
  34. void *userData)
  35. {
  36. UNUSED(description);
  37. UNUSED(userData);
  38. if (!objectPath) {
  39. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  40. }
  41. if (strcmp(objectPath, "/nv/Owner/foo1") == 0)
  42. *auth = PASSWORD1;
  43. else
  44. *auth = PASSWORD2;
  45. cb_auth_called = true;
  46. return TSS2_RC_SUCCESS;
  47. }
  48. static TSS2_RC
  49. branch_callback(
  50. char const *objectPath,
  51. char const *description,
  52. char const **branchNames,
  53. size_t numBranches,
  54. size_t *selectedBranch,
  55. void *userData)
  56. {
  57. UNUSED(description);
  58. UNUSED(userData);
  59. UNUSED(branchNames);
  60. if (strcmp(objectPath, "/nv/Owner/useful") != 0) {
  61. return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path");
  62. }
  63. if (numBranches != 2) {
  64. LOG_ERROR("Wrong number of branches");
  65. return TSS2_FAPI_RC_GENERAL_FAILURE;
  66. }
  67. if (written) {
  68. *selectedBranch = 1;
  69. } else {
  70. written = true;
  71. *selectedBranch = 0;
  72. }
  73. cb_branch_called = true;
  74. return TSS2_RC_SUCCESS;
  75. }
  76. /** Test the FAPI for PolicyOr with a different policy for read and write.
  77. *
  78. * The write to NV ram will be authorized by two secrets.
  79. *
  80. * Tested FAPI commands:
  81. * - Fapi_Provision()
  82. * - Fapi_Import()
  83. * - Fapi_CreateNv()
  84. * - Fapi_NvWrite()
  85. * - Fapi_Nvread()
  86. * - Fapi_SetBranchCB()
  87. * - Fapi_SetAuthCB()
  88. * - Fapi_Delete()
  89. *
  90. * Tested Policies:
  91. * - PolicyOr
  92. * - PolicySecret
  93. * - PolicyCommandCode
  94. *
  95. * @param[in,out] context The FAPI_CONTEXT.
  96. * @retval EXIT_FAILURE
  97. * @retval EXIT_SUCCESS
  98. */
  99. int
  100. test_fapi_policy_or_nv_read_write(FAPI_CONTEXT *context)
  101. {
  102. TSS2_RC r;
  103. char *policy_name = "/policy/pol_or_read_write_secret";
  104. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_or_read_write_secret.json";
  105. FILE *stream = NULL;
  106. char *json_policy = NULL;
  107. long policy_size;
  108. uint8_t data_src[NV_SIZE];
  109. size_t dest_size = NV_SIZE;
  110. uint8_t *data_dest = NULL;
  111. for (int i = 0; i < NV_SIZE; i++) {
  112. data_src[i] = (i % 10) + 1;
  113. }
  114. r = Fapi_Provision(context, NULL, NULL, NULL);
  115. goto_if_error(r, "Error Fapi_Provision", error);
  116. r = pcr_reset(context, 16);
  117. goto_if_error(r, "Error pcr_reset", error);
  118. stream = fopen(policy_file, "r");
  119. if (!stream) {
  120. LOG_ERROR("File %s does not exist", policy_file);
  121. goto error;
  122. }
  123. fseek(stream, 0L, SEEK_END);
  124. policy_size = ftell(stream);
  125. fclose(stream);
  126. json_policy = malloc(policy_size + 1);
  127. goto_if_null(json_policy,
  128. "Could not allocate memory for the JSON policy",
  129. TSS2_FAPI_RC_MEMORY, error);
  130. stream = fopen(policy_file, "r");
  131. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  132. if (ret != policy_size) {
  133. LOG_ERROR("IO error %s.", policy_file);
  134. goto error;
  135. }
  136. json_policy[policy_size] = '\0';
  137. r = Fapi_Import(context, policy_name, json_policy);
  138. goto_if_error(r, "Error Fapi_Import", error);
  139. r = Fapi_SetBranchCB(context, branch_callback, NULL);
  140. goto_if_error(r, "Error SetPolicybranchselectioncallback", error);
  141. r = Fapi_SetAuthCB(context, auth_callback, "");
  142. goto_if_error(r, "Error Fapi_SetAuthCB", error);
  143. r = Fapi_CreateNv(context, "/nv/Owner/foo1", "noda", NV_SIZE, "", PASSWORD1);
  144. goto_if_error(r, "Error Fapi_CreateNv", error);
  145. r = Fapi_CreateNv(context, "/nv/Owner/foo2", "noda", NV_SIZE, "", PASSWORD2);
  146. goto_if_error(r, "Error Fapi_CreateNv", error);
  147. r = Fapi_CreateNv(context, "/nv/Owner/useful", "noda", NV_SIZE, policy_name, "");
  148. goto_if_error(r, "Error Fapi_CreateNv", error);
  149. r = Fapi_NvWrite(context, "/nv/Owner/useful", &data_src[0], NV_SIZE);
  150. goto_if_error(r, "Error Fapi_NvWrite", error);
  151. r = Fapi_NvRead(context, "/nv/Owner/useful", &data_dest, &dest_size, NULL);
  152. goto_if_error(r, "Error Fapi_NvRead", error);
  153. assert(data_dest != NULL);
  154. if (dest_size != NV_SIZE ||
  155. memcmp(data_src, data_dest, dest_size) != 0) {
  156. LOG_ERROR("Error: result of nv read is wrong.");
  157. goto error;
  158. }
  159. SAFE_FREE(data_dest);
  160. r = Fapi_Delete(context, "/");
  161. goto_if_error(r, "Error Fapi_Delete", error);
  162. SAFE_FREE(json_policy);
  163. if (!cb_branch_called) {
  164. LOG_ERROR("Branch selection callback was not called.");
  165. return EXIT_FAILURE;
  166. }
  167. if (!cb_auth_called) {
  168. LOG_ERROR("Auth value callback was not called.");
  169. return EXIT_FAILURE;
  170. }
  171. return EXIT_SUCCESS;
  172. error:
  173. SAFE_FREE(data_dest);
  174. Fapi_Delete(context, "/");
  175. SAFE_FREE(json_policy);
  176. return EXIT_FAILURE;
  177. }
  178. int
  179. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  180. {
  181. return test_fapi_policy_or_nv_read_write(fapi_context);
  182. }