fapi-nv-ordinary.int.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 <unistd.h>
  14. #include "tss2_fapi.h"
  15. #include "test-fapi.h"
  16. #define LOGMODULE test
  17. #include "util/log.h"
  18. #include "util/aux_util.h"
  19. #define NV_SIZE 1200
  20. #define PASSWORD "abc"
  21. #define USER_DATA "my user data"
  22. static char *password;
  23. static TSS2_RC
  24. auth_callback(
  25. char const *objectPath,
  26. char const *description,
  27. const char **auth,
  28. void *userData)
  29. {
  30. UNUSED(description);
  31. UNUSED(userData);
  32. if (!objectPath) {
  33. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  34. }
  35. *auth = PASSWORD;
  36. return TSS2_RC_SUCCESS;
  37. }
  38. static TSS2_RC
  39. action_callback(
  40. const char *objectPath,
  41. const char *action,
  42. void *userData)
  43. {
  44. UNUSED(userData);
  45. ASSERT(objectPath != NULL);
  46. ASSERT(action != NULL);
  47. ASSERT(strlen(userData) == strlen((char*)USER_DATA));
  48. if (strcmp(objectPath, "/nv/Owner/myNV") != 0) {
  49. return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path");
  50. }
  51. if (strcmp(action, "myaction")) {
  52. LOG_ERROR("Bad action: %s", action);
  53. return TSS2_FAPI_RC_GENERAL_FAILURE;
  54. }
  55. return TSS2_RC_SUCCESS;
  56. error:
  57. exit(EXIT_FAILURE);
  58. }
  59. static char *
  60. read_policy(FAPI_CONTEXT *context, char *policy_name)
  61. {
  62. FILE *stream = NULL;
  63. long policy_size;
  64. char *json_policy = NULL;
  65. char policy_file[1024];
  66. if (snprintf(&policy_file[0], 1023, TOP_SOURCEDIR "/test/data/fapi/%s.json", policy_name) < 0)
  67. return NULL;
  68. stream = fopen(policy_file, "r");
  69. if (!stream) {
  70. LOG_ERROR("File %s does not exist", policy_file);
  71. return NULL;
  72. }
  73. fseek(stream, 0L, SEEK_END);
  74. policy_size = ftell(stream);
  75. fclose(stream);
  76. json_policy = malloc(policy_size + 1);
  77. return_if_null(json_policy,
  78. "Could not allocate memory for the JSON policy",
  79. NULL);
  80. stream = fopen(policy_file, "r");
  81. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  82. if (ret != policy_size) {
  83. LOG_ERROR("IO error %s.", policy_file);
  84. return NULL;
  85. }
  86. json_policy[policy_size] = '\0';
  87. return json_policy;
  88. }
  89. /** Test the FAPI NV functions.
  90. *
  91. * Tested FAPI commands:
  92. * - Fapi_Provision()
  93. * - Fapi_Import()
  94. * - Fapi_SetPolicyActionCB()
  95. * - Fapi_CreateNv()
  96. * - Fapi_NvWrite()
  97. * - Fapi_NvRead()
  98. * - Fapi_Delete()
  99. * - Fapi_SetDescription()
  100. * - Fapi_GetDescription()
  101. * - Fapi_SetAuthCB()
  102. *
  103. * Tested Policies:
  104. * - PolicyAction
  105. *
  106. * @param[in,out] context The FAPI_CONTEXT.
  107. * @retval EXIT_FAILURE
  108. * @retval EXIT_SUCCESS
  109. */
  110. int
  111. test_fapi_nv_ordinary(FAPI_CONTEXT *context)
  112. {
  113. TSS2_RC r;
  114. char *nvPathOrdinary = "/nv/Owner/myNV";
  115. uint8_t data_src[NV_SIZE];
  116. uint8_t *data_dest = NULL;
  117. char *logData = NULL;
  118. size_t dest_size = NV_SIZE;
  119. char *description1 = "nvDescription";
  120. char *description2 = NULL;
  121. char *policy_action = "/policy/pol_action";
  122. char *policy_pcr8_0 = "/policy/pol_pcr8_0";
  123. char *json_policy = NULL;
  124. for (int i = 0; i < NV_SIZE; i++) {
  125. data_src[i] = (i % 10) + 1;
  126. }
  127. r = Fapi_Provision(context, NULL, NULL, NULL);
  128. goto_if_error(r, "Error Fapi_Provision", error);
  129. r = pcr_reset(context, 16);
  130. goto_if_error(r, "Error pcr_reset", error);
  131. json_policy = read_policy(context, policy_action);
  132. r = Fapi_Import(context, policy_action, json_policy);
  133. goto_if_error(r, "Error Fapi_Import", error);
  134. SAFE_FREE(json_policy);
  135. r = Fapi_SetPolicyActionCB(context, action_callback, USER_DATA);
  136. goto_if_error(r, "Error Fapi_SetPolicyActionCB", error);
  137. /* Test with policy action */
  138. r = Fapi_CreateNv(context, nvPathOrdinary, "noda", NV_SIZE, policy_action, "");
  139. goto_if_error(r, "Error Fapi_CreateNv", error);
  140. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  141. goto_if_error(r, "Error Fapi_NvWrite", error);
  142. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  143. goto_if_error(r, "Error Fapi_NvRead", error);
  144. ASSERT(data_dest != NULL);
  145. ASSERT(logData != NULL);
  146. ASSERT(strlen(logData) == 0);
  147. if (dest_size != NV_SIZE ||
  148. memcmp(data_src, data_dest, dest_size) != 0) {
  149. LOG_ERROR("Error: result of nv read is wrong.");
  150. goto error;
  151. }
  152. r = Fapi_Delete(context, nvPathOrdinary);
  153. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  154. SAFE_FREE(data_dest);
  155. SAFE_FREE(logData);
  156. /* Test with pcr policy with pcr 8. */
  157. uint8_t *pcr_digest = NULL;
  158. char *pcrLog = NULL;
  159. size_t pcr_digest_size = 0;
  160. uint8_t zero_digest[32];
  161. memset(&zero_digest[0], 0, 32);
  162. r = Fapi_PcrRead(context, 8, &pcr_digest,
  163. &pcr_digest_size, &pcrLog);
  164. goto_if_error(r, "Error Fapi_PcrRead", error);
  165. ASSERT(pcr_digest != NULL);
  166. ASSERT(pcrLog != NULL);
  167. if (memcmp(&pcr_digest[0], &zero_digest, pcr_digest_size) != 0) {
  168. SAFE_FREE(pcr_digest);
  169. SAFE_FREE(pcrLog);
  170. LOG_WARNING("PCR 8 is not zero. Test with pcr policy not executed.");
  171. } else {
  172. SAFE_FREE(pcr_digest);
  173. SAFE_FREE(pcrLog);
  174. json_policy = read_policy(context, policy_pcr8_0);
  175. r = Fapi_Import(context, policy_pcr8_0, json_policy);
  176. goto_if_error(r, "Error Fapi_Import", error);
  177. SAFE_FREE(json_policy);
  178. /* Test with policy action */
  179. r = Fapi_CreateNv(context, nvPathOrdinary, "noda", NV_SIZE, policy_pcr8_0, "");
  180. goto_if_error(r, "Error Fapi_CreateNv", error);
  181. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  182. goto_if_error(r, "Error Fapi_NvWrite", error);
  183. data_dest = NULL;
  184. logData = NULL;
  185. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  186. goto_if_error(r, "Error Fapi_NvRead", error);
  187. ASSERT(data_dest != NULL);
  188. ASSERT(logData != NULL);
  189. ASSERT(strlen(logData) == 0);
  190. if (dest_size != NV_SIZE ||
  191. memcmp(data_src, data_dest, dest_size) != 0) {
  192. LOG_ERROR("Error: result of nv read is wrong.");
  193. goto error;
  194. }
  195. r = Fapi_Delete(context, nvPathOrdinary);
  196. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  197. SAFE_FREE(data_dest);
  198. SAFE_FREE(logData);
  199. }
  200. /* Empty auth noda set */
  201. r = Fapi_CreateNv(context, nvPathOrdinary, "noda", NV_SIZE, "", "");
  202. goto_if_error(r, "Error Fapi_CreateNv", error);
  203. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  204. goto_if_error(r, "Error Fapi_NvWrite", error);
  205. data_dest = NULL;
  206. logData = NULL;
  207. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  208. goto_if_error(r, "Error Fapi_NvRead", error);
  209. ASSERT(data_dest != NULL);
  210. ASSERT(logData != NULL);
  211. ASSERT(strlen(logData) == 0);
  212. if (dest_size != NV_SIZE ||
  213. memcmp(data_src, data_dest, dest_size) != 0) {
  214. LOG_ERROR("Error: result of nv read is wrong.");
  215. goto error;
  216. }
  217. r = Fapi_Delete(context, nvPathOrdinary);
  218. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  219. SAFE_FREE(data_dest);
  220. SAFE_FREE(logData);
  221. r = Fapi_SetAuthCB(context, auth_callback, "");
  222. goto_if_error(r, "Error Fapi_SetAuthCB", error);
  223. /* Password set and noda set */
  224. password = PASSWORD;
  225. r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", password);
  226. goto_if_error(r, "Error Fapi_CreateNv", error);
  227. r = Fapi_SetAuthCB(context, auth_callback, "");
  228. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  229. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  230. goto_if_error(r, "Error Fapi_NvWrite", error);
  231. data_dest = NULL;
  232. logData = NULL;
  233. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  234. goto_if_error(r, "Error Fapi_NvRead", error);
  235. ASSERT(data_dest != NULL);
  236. ASSERT(logData != NULL);
  237. ASSERT(strlen(logData) == 0);
  238. if (dest_size != NV_SIZE ||
  239. memcmp(data_src, data_dest, dest_size) != 0) {
  240. LOG_ERROR("Error: result of nv read is wrong.");
  241. goto error;
  242. }
  243. r = Fapi_Delete(context, nvPathOrdinary);
  244. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  245. SAFE_FREE(data_dest);
  246. SAFE_FREE(logData);
  247. /* Empty auth noda clear */
  248. password = "";
  249. r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", "");
  250. goto_if_error(r, "Error Fapi_CreateNv", error);
  251. r = Fapi_SetDescription(context, nvPathOrdinary, description1);
  252. goto_if_error(r, "Error Fapi_SetDescription", error);
  253. r = Fapi_GetDescription(context, nvPathOrdinary, &description2);
  254. goto_if_error(r, "Error Fapi_GetDescription", error);
  255. ASSERT(description2 != NULL);
  256. if (strcmp(description1, description2) != 0) {
  257. goto_if_error(r, "Different descriptions", error);
  258. }
  259. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  260. goto_if_error(r, "Error Fapi_NvWrite", error);
  261. data_dest = NULL;
  262. logData = NULL;
  263. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  264. goto_if_error(r, "Error Fapi_NvRead", error);
  265. ASSERT(data_dest != NULL);
  266. ASSERT(logData != NULL);
  267. ASSERT(strlen(logData) == 0);
  268. if (dest_size != NV_SIZE ||
  269. memcmp(data_src, data_dest, dest_size) != 0) {
  270. LOG_ERROR("Error: result of nv read is wrong.");
  271. goto error;
  272. }
  273. r = Fapi_Delete(context, nvPathOrdinary);
  274. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  275. SAFE_FREE(data_dest);
  276. SAFE_FREE(logData);
  277. /* Password set and noda clear */
  278. password = PASSWORD;
  279. r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", password);
  280. goto_if_error(r, "Error Fapi_CreateNv", error);
  281. r = Fapi_SetAuthCB(context, auth_callback, "");
  282. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  283. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  284. goto_if_error(r, "Error Fapi_NvWrite", error);
  285. data_dest = NULL;
  286. logData = NULL;
  287. r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, &logData);
  288. goto_if_error(r, "Error Fapi_NvRead", error);
  289. ASSERT(data_dest != NULL);
  290. ASSERT(logData != NULL);
  291. ASSERT(strlen(logData) == 0);
  292. if (dest_size != NV_SIZE ||
  293. memcmp(data_src, data_dest, dest_size) != 0) {
  294. LOG_ERROR("Error: result of nv read is wrong.");
  295. goto error;
  296. }
  297. r = Fapi_Delete(context, nvPathOrdinary);
  298. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  299. r = Fapi_Delete(context, "/");
  300. goto_if_error(r, "Error Fapi_Delete", error);
  301. SAFE_FREE(data_dest);
  302. SAFE_FREE(logData);
  303. SAFE_FREE(description2);
  304. SAFE_FREE(json_policy);
  305. return EXIT_SUCCESS;
  306. error:
  307. Fapi_Delete(context, "/");
  308. SAFE_FREE(data_dest);
  309. SAFE_FREE(logData);
  310. SAFE_FREE(description2);
  311. SAFE_FREE(json_policy);
  312. return EXIT_FAILURE;
  313. }
  314. int
  315. test_invoke_fapi(FAPI_CONTEXT *context)
  316. {
  317. return test_fapi_nv_ordinary(context);
  318. }