fapi-nv-increment.int.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 PASSWORD "abc"
  20. static TSS2_RC
  21. auth_callback(
  22. char const *objectPath,
  23. char const *description,
  24. const char **auth,
  25. void *userData)
  26. {
  27. UNUSED(description);
  28. UNUSED(userData);
  29. if (!objectPath) {
  30. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  31. }
  32. *auth = PASSWORD;
  33. return TSS2_RC_SUCCESS;
  34. }
  35. /** Test the FAPI function FAPI_NvIncrement.
  36. *
  37. * Tested FAPI commands:
  38. * - Fapi_Provision()
  39. * - Fapi_Import()
  40. * - Fapi_CreateNv()
  41. * - Fapi_SetAuthCB()
  42. * - Fapi_ChangeAuth()
  43. * - Fapi_Delete()
  44. * - Fapi_NvIncrement()
  45. *
  46. * Tested Policies:
  47. * - PolicyAuthValue
  48. * - PolicyCommandCode
  49. *
  50. * @param[in,out] context The FAPI_CONTEXT.
  51. * @retval EXIT_FAILURE
  52. * @retval EXIT_SUCCESS
  53. */
  54. int
  55. test_fapi_nv_increment(FAPI_CONTEXT *context)
  56. {
  57. TSS2_RC r;
  58. char *nvPathCounter = "nv/Owner/myNV_Counter";
  59. char *policy_name = "/policy/pol_nv_change_auth";
  60. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_nv_change_auth.json";
  61. FILE *stream = NULL;
  62. char *json_policy = NULL;
  63. long policy_size;
  64. r = Fapi_Provision(context, NULL, NULL, NULL);
  65. goto_if_error(r, "Error Fapi_Provision", error);
  66. r = pcr_reset(context, 16);
  67. goto_if_error(r, "Error pcr_reset", error);
  68. stream = fopen(policy_file, "r");
  69. if (!stream) {
  70. LOG_ERROR("File %s does not exist", policy_file);
  71. goto error;
  72. }
  73. fseek(stream, 0L, SEEK_END);
  74. policy_size = ftell(stream);
  75. fclose(stream);
  76. json_policy = malloc(policy_size + 1);
  77. goto_if_null(json_policy,
  78. "Could not allocate memory for the JSON policy",
  79. TSS2_FAPI_RC_MEMORY, error);
  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. goto error;
  85. }
  86. json_policy[policy_size] = '\0';
  87. r = Fapi_Import(context, policy_name, json_policy);
  88. goto_if_error(r, "Error Fapi_Import", error);
  89. /* Test no password, noda set */
  90. r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, policy_name, "abc");
  91. goto_if_error(r, "Error Fapi_CreateNv", error);
  92. r = Fapi_SetAuthCB(context, auth_callback, "");
  93. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  94. r = Fapi_ChangeAuth(context, nvPathCounter, "abc");
  95. goto_if_error(r, "Error Fapi_CreateNv", error);
  96. r = Fapi_Delete(context, nvPathCounter);
  97. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  98. /* Test no password, noda set */
  99. r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, "", "");
  100. goto_if_error(r, "Error Fapi_CreateNv", error);
  101. r = Fapi_NvIncrement(context, nvPathCounter);
  102. goto_if_error(r, "Error Fapi_CreateNv", error);
  103. r = Fapi_Delete(context, nvPathCounter);
  104. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  105. /* Test with password noda set */
  106. r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, "", PASSWORD);
  107. goto_if_error(r, "Error Fapi_CreateNv", error);
  108. r = Fapi_SetAuthCB(context, auth_callback, "");
  109. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  110. r = Fapi_NvIncrement(context, nvPathCounter);
  111. goto_if_error(r, "Error Fapi_CreateNv", error);
  112. r = Fapi_Delete(context, nvPathCounter);
  113. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  114. /* Test no password, noda clear */
  115. r = Fapi_CreateNv(context, nvPathCounter, "counter", 0, "", "");
  116. goto_if_error(r, "Error Fapi_CreateNv", error);
  117. r = Fapi_NvIncrement(context, nvPathCounter);
  118. goto_if_error(r, "Error Fapi_CreateNv", error);
  119. r = Fapi_Delete(context, nvPathCounter);
  120. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  121. /* Test with password noda clear */
  122. r = Fapi_CreateNv(context, nvPathCounter, "counter", 0, "", PASSWORD);
  123. goto_if_error(r, "Error Fapi_CreateNv", error);
  124. r = Fapi_SetAuthCB(context, auth_callback, "");
  125. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  126. r = Fapi_NvIncrement(context, nvPathCounter);
  127. goto_if_error(r, "Error Fapi_CreateNv", error);
  128. r = Fapi_Delete(context, nvPathCounter);
  129. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  130. r = Fapi_Delete(context, "/");
  131. goto_if_error(r, "Error Fapi_Delete", error);
  132. SAFE_FREE(json_policy);
  133. return EXIT_SUCCESS;
  134. error:
  135. Fapi_Delete(context, "/");
  136. SAFE_FREE(json_policy);
  137. return EXIT_FAILURE;
  138. }
  139. int
  140. test_invoke_fapi(FAPI_CONTEXT *context)
  141. {
  142. return test_fapi_nv_increment(context);
  143. }