fapi-duplicate.int.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 <unistd.h>
  12. #include <string.h>
  13. #include "tss2_fapi.h"
  14. #include "test-fapi.h"
  15. #define LOGMODULE test
  16. #include "util/log.h"
  17. #include "util/aux_util.h"
  18. #define SIZE 2000
  19. /** Test the FAPI functions for key duplication.
  20. *
  21. * Tested FAPI commands:
  22. * - Fapi_Provision()
  23. * - Fapi_Import()
  24. * - Fapi_CreateKey()
  25. * - Fapi_ExportKey()
  26. * - Fapi_Delete()
  27. *
  28. * Tested Policies:
  29. * - PolicyDuplicationSelect
  30. *
  31. * @param[in,out] context The FAPI_CONTEXT.
  32. * @retval EXIT_FAILURE
  33. * @retval EXIT_SUCCESS
  34. */
  35. int
  36. test_fapi_duplicate(FAPI_CONTEXT *context)
  37. {
  38. TSS2_RC r;
  39. char *policy_name = "/policy/pol_duplicate";
  40. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_duplicate.json";
  41. FILE *stream = NULL;
  42. char *json_policy = NULL;
  43. long policy_size;
  44. char *json_duplicate = NULL;
  45. char *json_string_pub_key = NULL;
  46. r = Fapi_Provision(context, NULL, NULL, NULL);
  47. goto_if_error(r, "Error Fapi_Provision", error);
  48. r = pcr_reset(context, 16);
  49. goto_if_error(r, "Error pcr_reset", error);
  50. stream = fopen(policy_file, "r");
  51. if (!stream) {
  52. LOG_ERROR("File %s does not exist", policy_file);
  53. goto error;
  54. }
  55. fseek(stream, 0L, SEEK_END);
  56. policy_size = ftell(stream);
  57. fclose(stream);
  58. json_policy = malloc(policy_size + 1);
  59. goto_if_null(json_policy,
  60. "Could not allocate memory for the JSON policy",
  61. TSS2_FAPI_RC_MEMORY, error);
  62. stream = fopen(policy_file, "r");
  63. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  64. if (ret != policy_size) {
  65. LOG_ERROR("IO error %s.", policy_file);
  66. goto error;
  67. }
  68. json_policy[policy_size] = '\0';
  69. r = Fapi_Import(context, policy_name, json_policy);
  70. goto_if_error(r, "Error Fapi_List", error);
  71. r = Fapi_CreateKey(context, "HS/SRK/myCryptKey", "restricted,decrypt,noDa",
  72. "", NULL);
  73. goto_if_error(r, "Error Fapi_CreateKey", error);
  74. r = Fapi_ExportKey(context, "HS/SRK/myCryptKey", NULL, &json_string_pub_key);
  75. goto_if_error(r, "Error Fapi_CreateKey", error);
  76. ASSERT(json_string_pub_key != NULL);
  77. ASSERT(strlen(json_string_pub_key) > ASSERT_SIZE);
  78. r = Fapi_Import(context, "ext/myNewParent", json_string_pub_key);
  79. goto_if_error(r, "Error Fapi_Import", error);
  80. r = Fapi_CreateKey(context, "HS/SRK/myCryptKey/myCryptKey2",
  81. "exportable,decrypt,noDa", policy_name, NULL);
  82. goto_if_error(r, "Error Fapi_CreateKey", error);
  83. r = Fapi_ExportKey(context, "HS/SRK/myCryptKey/myCryptKey2",
  84. "ext/myNewParent", &json_duplicate);
  85. goto_if_error(r, "Error Fapi_CreateKey", error);
  86. ASSERT(json_duplicate != NULL);
  87. ASSERT(strlen(json_duplicate) > ASSERT_SIZE);
  88. LOG_INFO("\nTEST_JSON\nExport Data:\n%s\nEND_JSON", json_duplicate);
  89. char *duplicate_check[] = { "duplicate" };
  90. CHECK_JSON_FIELDS(json_duplicate, duplicate_check, "", error);
  91. r = Fapi_Import(context, "importedKey", json_duplicate);
  92. goto_if_error(r, "Error Fapi_Import", error);
  93. fprintf(stderr, "Duplicate:\n%s\n", json_duplicate);
  94. r = Fapi_Delete(context, "/");
  95. goto_if_error(r, "Error Fapi_Delete", error);
  96. SAFE_FREE(json_string_pub_key);
  97. SAFE_FREE(json_duplicate);
  98. SAFE_FREE(json_policy);
  99. return EXIT_SUCCESS;
  100. error:
  101. Fapi_Delete(context, "/");
  102. SAFE_FREE(json_string_pub_key);
  103. SAFE_FREE(json_duplicate);
  104. SAFE_FREE(json_policy);
  105. return EXIT_FAILURE;
  106. }
  107. int
  108. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  109. {
  110. return test_fapi_duplicate(fapi_context);
  111. }