fapi-key-create-policy-or-sign.int.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <stdbool.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include "tss2_fapi.h"
  17. #include "test-fapi.h"
  18. #define LOGMODULE test
  19. #include "util/log.h"
  20. #include "util/aux_util.h"
  21. #define PASSWORD NULL
  22. #define SIGN_TEMPLATE "sign,noDa"
  23. static bool cb_called = false;
  24. static TSS2_RC
  25. branch_callback(
  26. char const *objectPath,
  27. char const *description,
  28. char const **branchNames,
  29. size_t numBranches,
  30. size_t *selectedBranch,
  31. void *userData)
  32. {
  33. UNUSED(description);
  34. UNUSED(userData);
  35. if (strcmp(objectPath, "P_ECC/HS/SRK/mySignKey") != 0) {
  36. return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path");
  37. }
  38. if (numBranches != 2) {
  39. LOG_ERROR("Wrong number of branches");
  40. return TSS2_FAPI_RC_GENERAL_FAILURE;
  41. }
  42. if (!strcmp(branchNames[0], "branch0"))
  43. *selectedBranch = 0;
  44. else if (!strcmp(branchNames[1], "branch0"))
  45. *selectedBranch = 1;
  46. else {
  47. LOG_ERROR("BranchName not found. Got \"%s\" and \"%s\"",
  48. branchNames[0], branchNames[1]);
  49. return TSS2_FAPI_RC_GENERAL_FAILURE;
  50. }
  51. cb_called = true;
  52. return TSS2_RC_SUCCESS;
  53. }
  54. /** Test the FAPI for PolicyOr using signing.
  55. *
  56. * Tested FAPI commands:
  57. * - Fapi_Provision()
  58. * - Fapi_Import()
  59. * - Fapi_CreateKey()
  60. * - Fapi_SetBranchCB()
  61. * - Fapi_Sign()
  62. * - Fapi_Delete()
  63. *
  64. * Tested Policies:
  65. * - PolicyOr
  66. * - PolicyPcr
  67. *
  68. * @param[in,out] context The FAPI_CONTEXT.
  69. * @retval EXIT_FAILURE
  70. * @retval EXIT_SUCCESS
  71. */
  72. int
  73. test_fapi_key_create_policy_or_sign(FAPI_CONTEXT *context)
  74. {
  75. TSS2_RC r;
  76. char *policy_name = "/policy/pol_pcr16_0_or";
  77. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_pcr16_0_or.json";
  78. FILE *stream = NULL;
  79. char *json_policy = NULL;
  80. uint8_t *signature = NULL;
  81. char *publicKey = NULL;
  82. char *certificate = NULL;
  83. long policy_size;
  84. r = Fapi_Provision(context, NULL, NULL, NULL);
  85. goto_if_error(r, "Error Fapi_Provision", error);
  86. r = pcr_reset(context, 16);
  87. goto_if_error(r, "Error pcr_reset", error);
  88. stream = fopen(policy_file, "r");
  89. if (!stream) {
  90. LOG_ERROR("File %s does not exist", policy_file);
  91. goto error;
  92. }
  93. fseek(stream, 0L, SEEK_END);
  94. policy_size = ftell(stream);
  95. fclose(stream);
  96. json_policy = malloc(policy_size + 1);
  97. goto_if_null(json_policy,
  98. "Could not allocate memory for the JSON policy",
  99. TSS2_FAPI_RC_MEMORY, error);
  100. stream = fopen(policy_file, "r");
  101. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  102. if (ret != policy_size) {
  103. LOG_ERROR("IO error %s.", policy_file);
  104. goto error;
  105. }
  106. json_policy[policy_size] = '\0';
  107. r = Fapi_Import(context, policy_name, json_policy);
  108. goto_if_error(r, "Error Fapi_Import", error);
  109. r = Fapi_CreateKey(context, "/HS/SRK/mySignKey", SIGN_TEMPLATE,
  110. policy_name, PASSWORD);
  111. goto_if_error(r, "Error Fapi_CreateKey", error);
  112. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  113. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  114. goto_if_error(r, "Error Fapi_CreateKey", error);
  115. size_t signatureSize = 0;
  116. TPM2B_DIGEST digest = {
  117. .size = 20,
  118. .buffer = {
  119. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  120. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  121. }
  122. };
  123. r = Fapi_SetBranchCB(context, branch_callback, NULL);
  124. goto_if_error(r, "Error SetPolicybranchselectioncallback", error);
  125. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  126. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  127. goto_if_error(r, "Error Fapi_CreateKey", error);
  128. r = Fapi_Sign(context, "/HS/SRK/mySignKey", NULL,
  129. &digest.buffer[0], digest.size, &signature, &signatureSize,
  130. &publicKey, &certificate);
  131. goto_if_error(r, "Error Fapi_Sign", error);
  132. ASSERT(signature != NULL);
  133. ASSERT(publicKey != NULL);
  134. ASSERT(certificate != NULL);
  135. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  136. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  137. r = Fapi_Delete(context, "/");
  138. goto_if_error(r, "Error Fapi_Delete", error);
  139. SAFE_FREE(json_policy);
  140. SAFE_FREE(signature);
  141. SAFE_FREE(publicKey);
  142. SAFE_FREE(certificate);
  143. if (!cb_called) {
  144. LOG_ERROR("Branch selection callback was not called.");
  145. return EXIT_FAILURE;
  146. }
  147. return EXIT_SUCCESS;
  148. error:
  149. Fapi_Delete(context, "/");
  150. SAFE_FREE(json_policy);
  151. SAFE_FREE(signature);
  152. SAFE_FREE(publicKey);
  153. SAFE_FREE(certificate);
  154. return EXIT_FAILURE;
  155. }
  156. int
  157. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  158. {
  159. return test_fapi_key_create_policy_or_sign(fapi_context);
  160. }