fapi-nv-written-policy.int.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 10
  20. #define APP_DATA_SIZE 10*1024*1024
  21. /** Test the FAPI policy PolicyNvWritten.
  22. *
  23. * Tested FAPI commands:
  24. * - Fapi_Provision()
  25. * - Fapi_CreateNv()
  26. * - Fapi_SetAppData()
  27. * - Fapi_GetAppData()
  28. * - Fapi_NvWrite()
  29. * - Fapi_Delete()
  30. *
  31. * Tested Policies:
  32. * - PolicyNvWritten
  33. *
  34. * @param[in,out] context The FAPI_CONTEXT.
  35. * @retval EXIT_FAILURE
  36. * @retval EXIT_SUCCESS
  37. */
  38. int
  39. test_fapi_nv_written_policy(FAPI_CONTEXT *context)
  40. {
  41. TSS2_RC r;
  42. char *nvPathOrdinary = "/nv/Owner/myNV";
  43. char *policy_name = "/policy/pol_nv_written";
  44. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_nv_written.json";
  45. FILE *stream = NULL;
  46. char *json_policy = NULL;
  47. long policy_size;
  48. uint8_t *appDataOut = NULL;
  49. size_t appDataOutSize, i;
  50. uint8_t *appDataIn = NULL;
  51. r = Fapi_Provision(context, NULL, NULL, NULL);
  52. goto_if_error(r, "Error Fapi_Provision", error);
  53. uint8_t data_src[NV_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  54. appDataIn = calloc(1, APP_DATA_SIZE);
  55. if (!appDataIn) {
  56. LOG_ERROR("Out of memory.");
  57. goto error;
  58. }
  59. for (i = 0; i < APP_DATA_SIZE; i++)
  60. appDataIn[i] = i % 256;
  61. stream = fopen(policy_file, "r");
  62. if (!stream) {
  63. LOG_ERROR("File %s does not exist", policy_file);
  64. goto error;
  65. }
  66. fseek(stream, 0L, SEEK_END);
  67. policy_size = ftell(stream);
  68. fclose(stream);
  69. json_policy = malloc(policy_size + 1);
  70. goto_if_null(json_policy,
  71. "Could not allocate memory for the JSON policy",
  72. TSS2_FAPI_RC_MEMORY, error);
  73. stream = fopen(policy_file, "r");
  74. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  75. if (ret != policy_size) {
  76. LOG_ERROR("IO error %s.", policy_file);
  77. goto error;
  78. }
  79. json_policy[policy_size] = '\0';
  80. r = Fapi_Import(context, policy_name, json_policy);
  81. goto_if_error(r, "Error Fapi_Import", error);
  82. /* Empty auth noda set */
  83. r = Fapi_CreateNv(context, nvPathOrdinary, "noda", 10, policy_name, "");
  84. goto_if_error(r, "Error Fapi_CreateNv", error);
  85. r = Fapi_SetAppData(context, nvPathOrdinary, appDataIn, APP_DATA_SIZE);
  86. goto_if_error(r, "Error Fapi_SetAppData", error);
  87. r = Fapi_GetAppData(context, nvPathOrdinary, &appDataOut, &appDataOutSize);
  88. goto_if_error(r, "Error Fapi_GetAppData", error);
  89. ASSERT(appDataOut != NULL);
  90. if (APP_DATA_SIZE != appDataOutSize ||
  91. memcmp(appDataOut, &appDataIn[0], appDataOutSize) != 0) {
  92. LOG_ERROR("Error: AppData equal to origin");
  93. goto error;
  94. }
  95. r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
  96. goto_if_error(r, "Error Fapi_NvWrite", error);
  97. r = Fapi_Delete(context, nvPathOrdinary);
  98. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  99. r = Fapi_Delete(context, "/");
  100. goto_if_error(r, "Error Fapi_Delete", error);
  101. SAFE_FREE(json_policy);
  102. SAFE_FREE(appDataOut);
  103. SAFE_FREE(appDataIn);
  104. return EXIT_SUCCESS;
  105. error:
  106. Fapi_Delete(context, "/");
  107. SAFE_FREE(json_policy);
  108. SAFE_FREE(appDataOut);
  109. SAFE_FREE(appDataIn);
  110. return EXIT_FAILURE;
  111. }
  112. int
  113. test_invoke_fapi(FAPI_CONTEXT *context)
  114. {
  115. return test_fapi_nv_written_policy(context);
  116. }