fapi-nv-authorizenv-cphash.int.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "tss2_esys.h"
  16. #include "test-fapi.h"
  17. #define LOGMODULE test
  18. #include "util/log.h"
  19. #include "util/aux_util.h"
  20. static TSS2_RC
  21. check_tpm_cmd(FAPI_CONTEXT *context, TPM2_CC command_code)
  22. {
  23. TSS2_RC r;
  24. TSS2_TCTI_CONTEXT *tcti;
  25. ESYS_CONTEXT *esys;
  26. TPMS_CAPABILITY_DATA *cap_data;
  27. r = Fapi_GetTcti(context, &tcti);
  28. goto_if_error(r, "Error Fapi_GetTcti", error);
  29. r = Esys_Initialize(&esys, tcti, NULL);
  30. goto_if_error(r, "Error Fapi_GetTcti", error);
  31. r = Esys_GetCapability(esys,
  32. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  33. TPM2_CAP_COMMANDS, command_code, 1, NULL, &cap_data);
  34. Esys_Finalize(&esys);
  35. return_if_error(r, "Error: GetCapabilities");
  36. if ((cap_data->data.command.commandAttributes[0] & TPMA_CC_COMMANDINDEX_MASK) ==
  37. command_code) {
  38. free(cap_data);
  39. return TSS2_RC_SUCCESS;
  40. } else {
  41. free(cap_data);
  42. return TSS2_FAPI_RC_NOT_IMPLEMENTED;
  43. }
  44. error:
  45. return r;
  46. }
  47. /** Test the FAPI PolicyCpHash but means of AuthorizeNv.
  48. *
  49. * Tested FAPI commands:
  50. * - Fapi_GetTcti()
  51. * - Fapi_Provision()
  52. * - Fapi_Import()
  53. * - Fapi_CreateNv()
  54. * - Fapi_WriteAuthorizeNv
  55. * - Fapi_NvWrite()
  56. *
  57. * Tested Policies:
  58. * - PolicyAuthorize
  59. * - PolicyCpHash
  60. *
  61. * @param[in,out] context The FAPI_CONTEXT.
  62. * @retval EXIT_FAILURE
  63. * @retval EXIT_SUCCESS
  64. */
  65. int
  66. test_fapi_nv_authorizenv_cphash(FAPI_CONTEXT *context)
  67. {
  68. TSS2_RC r;
  69. ssize_t ret;
  70. uint8_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  71. char *policy1_name = "/policy/pol_authorize_nv";
  72. char *policy1_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_authorize_nv.json";
  73. char *policy2_name = "/policy/pol_cphash";
  74. char *policy2_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_cphash.json";
  75. FILE *stream = NULL;
  76. char json[1024];
  77. char *policy = NULL;
  78. if (check_tpm_cmd(context, TPM2_CC_PolicyAuthorizeNV) != TPM2_RC_SUCCESS) {
  79. LOG_WARNING("Command PolicyAuthorizeNV not available.");
  80. return EXIT_SKIP;
  81. }
  82. r = Fapi_Provision(context, NULL, NULL, NULL);
  83. goto_if_error(r, "Error Fapi_Provision", error);
  84. memset(&json[0], 0, sizeof(json));
  85. stream = fopen(policy1_file, "r");
  86. ret = read(fileno(stream), &json[0], sizeof(json));
  87. fclose(stream);
  88. if (ret < 0) {
  89. LOG_ERROR("IO error %s.", policy1_file);
  90. goto error;
  91. }
  92. json[ret] = '\0';
  93. r = Fapi_Import(context, policy1_name, json);
  94. goto_if_error(r, "Error Fapi_Import", error);
  95. memset(&json[0], 0, sizeof(json));
  96. stream = fopen(policy2_file, "r");
  97. ret = read(fileno(stream), &json[0], sizeof(json));
  98. fclose(stream);
  99. if (ret < 0) {
  100. LOG_ERROR("IO error %s.", policy2_file);
  101. goto error;
  102. }
  103. json[ret] = '\0';
  104. r = Fapi_Import(context, policy2_name, json);
  105. goto_if_error(r, "Error Fapi_Import", error);
  106. /* Start the test */
  107. r = Fapi_CreateNv(context, "/nv/Owner/myNV", "", 34, "", "");
  108. goto_if_error(r, "Error Fapi_CreateNv", error);
  109. r = Fapi_CreateNv(context, "/nv/Owner/myNV2", "", sizeof(data), policy1_name, "");
  110. goto_if_error(r, "Error Fapi_CreateNv", error);
  111. r = Fapi_ExportPolicy(context, "/nv/Owner/myNV2", &policy);
  112. goto_if_error(r, "Error Fapi_ExportPolicy", error);
  113. ASSERT(policy != NULL);
  114. LOG_INFO("Policy authorize nv: %s", policy);
  115. char *fields_policy_authorize[] = { "policy", "0", "type" };
  116. CHECK_JSON_FIELDS(policy, fields_policy_authorize, "POLICYAUTHORIZENV", error);
  117. r = Fapi_WriteAuthorizeNv(context, "/nv/Owner/myNV", policy2_name);
  118. goto_if_error(r, "Error Fapi_WriteAuthorizeNv", error);
  119. r = Fapi_NvWrite(context, "/nv/Owner/myNV2", &data[0], sizeof(data));
  120. goto_if_error(r, "Error Fapi_NvWrite", error);
  121. /* Cleanup */
  122. SAFE_FREE(policy);
  123. r = Fapi_Delete(context, "/nv/Owner/myNV");
  124. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  125. r = Fapi_Delete(context, "/nv/Owner/myNV2");
  126. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  127. r = Fapi_Delete(context, "/");
  128. goto_if_error(r, "Error Fapi_Delete", error);
  129. return EXIT_SUCCESS;
  130. error:
  131. SAFE_FREE(policy);
  132. Fapi_Delete(context, "/");
  133. return EXIT_FAILURE;
  134. }
  135. int
  136. test_invoke_fapi(FAPI_CONTEXT *context)
  137. {
  138. return test_fapi_nv_authorizenv_cphash(context);
  139. }