fapi-nv-extend.int.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "tss2_fapi.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. #include "test-fapi.h"
  18. #define NV_SIZE 32
  19. #define PASSWORD "abc"
  20. static char *password;
  21. static TSS2_RC
  22. auth_callback(
  23. char const *objectPath,
  24. char const *description,
  25. const char **auth,
  26. void *userData)
  27. {
  28. UNUSED(description);
  29. UNUSED(userData);
  30. if (!objectPath) {
  31. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  32. }
  33. *auth = password;
  34. return TSS2_RC_SUCCESS;
  35. }
  36. /** Test the FAPI function FAPI_NvExtend.
  37. *
  38. * Tested FAPI commands:
  39. * - Fapi_Provision()
  40. * - Fapi_CreateNv()
  41. * - Fapi_NvExtend()
  42. * - Fapi_Delete()
  43. * - Fapi_SetAuthCB()
  44. *
  45. * @param[in,out] context The FAPI_CONTEXT.
  46. * @retval EXIT_FAILURE
  47. * @retval EXIT_SUCCESS
  48. */
  49. int
  50. test_fapi_nv_extend(FAPI_CONTEXT *context)
  51. {
  52. TSS2_RC r;
  53. char *nvPathExtend = "/nv/Owner/myNVextend";
  54. uint8_t *data_dest = NULL;
  55. char *log = NULL;
  56. size_t dest_size;
  57. r = Fapi_Provision(context, NULL, NULL, NULL);
  58. goto_if_error(r, "Error Fapi_Provision", error);
  59. /* Test no password, noda set */
  60. r = Fapi_CreateNv(context, nvPathExtend, "pcr, noda", 0, "", "");
  61. goto_if_error(r, "Error Fapi_CreateNv", error);
  62. uint8_t data_src[NV_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  63. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  64. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  65. 0, 1
  66. };
  67. r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
  68. goto_if_error(r, "Error Fapi_NV_EXTEND", error);
  69. r = Fapi_NvRead(context, nvPathExtend, &data_dest, &dest_size, &log);
  70. goto_if_error(r, "Error Fapi_NvRead", error);
  71. ASSERT(data_dest != NULL);
  72. ASSERT(log != NULL);
  73. LOG_INFO("\nTEST_JSON\nLog:\n%s\nEND_JSON", log);
  74. char *fields_log1[] = { "0", "digests", "0", "digest" };
  75. CHECK_JSON_FIELDS(log, fields_log1,
  76. "dcb1ac4a5de370cad091c13f13aee2f936c278fa05d264653c0c1321852a35e8",
  77. error);
  78. ASSERT(strlen(log) > ASSERT_SIZE);
  79. fprintf(stderr, "\nLog:\n%s\n", log);
  80. SAFE_FREE(data_dest);
  81. r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
  82. goto_if_error(r, "Error Fapi_NV_EXTEND", error);
  83. SAFE_FREE(log);
  84. data_dest = NULL;
  85. log = NULL;
  86. r = Fapi_NvRead(context, nvPathExtend, &data_dest, &dest_size, &log);
  87. goto_if_error(r, "Error Fapi_NvRead", error);
  88. ASSERT(data_dest != NULL);
  89. ASSERT(log != NULL);
  90. ASSERT(strlen(log) > ASSERT_SIZE);
  91. LOG_INFO("\nTEST_JSON\nLog:\n%s\nEND_JSON", log);
  92. char *fields_log2[] = { "1", "digests", "0", "digest" };
  93. CHECK_JSON_FIELDS(log, fields_log2,
  94. "dcb1ac4a5de370cad091c13f13aee2f936c278fa05d264653c0c1321852a35e8",
  95. error);
  96. fprintf(stderr, "\nLog:\n%s\n", log);
  97. r = Fapi_Delete(context, nvPathExtend);
  98. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  99. r = Fapi_SetAuthCB(context, auth_callback, "");
  100. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  101. /* Test with password noda set */
  102. password = PASSWORD;
  103. r = Fapi_CreateNv(context, nvPathExtend, "pcr, noda", 0, "", PASSWORD);
  104. goto_if_error(r, "Error Fapi_CreateNv", error);
  105. r = Fapi_SetAuthCB(context, auth_callback, "");
  106. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  107. r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
  108. goto_if_error(r, "Error Fapi_NV_EXTEN", error);
  109. r = Fapi_Delete(context, nvPathExtend);
  110. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  111. /* Test no password, noda clear */
  112. password = "";
  113. r = Fapi_CreateNv(context, nvPathExtend, "pcr", 0, "", "");
  114. goto_if_error(r, "Error Fapi_CreateNv", error);
  115. r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
  116. goto_if_error(r, "Error Fapi_NV_EXTEN", error);
  117. r = Fapi_Delete(context, nvPathExtend);
  118. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  119. /* Test with password noda clear */
  120. password = PASSWORD;
  121. r = Fapi_CreateNv(context, nvPathExtend, "pcr", 0, "", PASSWORD);
  122. goto_if_error(r, "Error Fapi_CreateNv", error);
  123. r = Fapi_SetAuthCB(context, auth_callback, "");
  124. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  125. r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
  126. goto_if_error(r, "Error Fapi_NV_EXTEN", error);
  127. r = Fapi_Delete(context, nvPathExtend);
  128. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  129. r = Fapi_Delete(context, "/");
  130. goto_if_error(r, "Error Fapi_Delete", error);
  131. SAFE_FREE(log);
  132. SAFE_FREE(data_dest);
  133. return EXIT_SUCCESS;
  134. error:
  135. Fapi_Delete(context, "/");
  136. SAFE_FREE(log);
  137. SAFE_FREE(data_dest);
  138. return EXIT_FAILURE;
  139. }
  140. int
  141. test_invoke_fapi(FAPI_CONTEXT *context)
  142. {
  143. return test_fapi_nv_extend(context);
  144. }