fapi-key-create-policies-sign.int.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include "tss2_fapi.h"
  16. #define LOGMODULE test
  17. #include "util/log.h"
  18. #include "util/aux_util.h"
  19. #include "test-fapi.h"
  20. #ifdef TEST_PASSWORD
  21. #define PASSWORD "abc"
  22. #else
  23. #define PASSWORD ""
  24. #endif
  25. static TSS2_RC
  26. auth_callback(
  27. char const *objectPath,
  28. char const *description,
  29. const char **auth,
  30. void *userData)
  31. {
  32. UNUSED(description);
  33. UNUSED(userData);
  34. if (!objectPath) {
  35. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  36. }
  37. *auth = PASSWORD;
  38. return TSS2_RC_SUCCESS;
  39. }
  40. #define SIGN_TEMPLATE "sign,noDa"
  41. /** Test several FAPI policies by usage of signing key.
  42. *
  43. * Which test case will be executed is determined by the compiler switches:
  44. * TEST_POLICY_PASSWORD, TEST_POLICY_AUTH_VALUE, TEST_POLICY_LOCALITY
  45. * TEST_POLICY_PHYSICAL_PRESENCE, TEST_POLICY_COMMAND_CODE, TEST_POLICY_COUNTERTIMER.
  46. *
  47. * Tested FAPI commands:
  48. * - Fapi_Provision()
  49. * - Fapi_Import()
  50. * - Fapi_CreateKey()
  51. * - Fapi_SetAuthCB()
  52. * - Fapi_Sign()
  53. * - Fapi_Delete()
  54. *
  55. * Tested Policies:
  56. * - PolicyPassword
  57. * - PolicyAuthValue
  58. * - PolicyLocality
  59. * - PolicyPhysicalPresence
  60. * - PolicyCommandCode
  61. * - PolicyCounterTimer
  62. *
  63. * @param[in,out] context The FAPI_CONTEXT.
  64. * @retval EXIT_FAILURE
  65. * @retval EXIT_SUCCESS
  66. */
  67. int
  68. test_fapi_key_create_policies_sign(FAPI_CONTEXT *context)
  69. {
  70. TSS2_RC r;
  71. #if defined(TEST_POLICY_PASSWORD)
  72. char *policy_name = "/policy/pol_password";
  73. char *policy_file = FAPI_POLICIES "/policy/pol_password.json";
  74. #elif defined(TEST_POLICY_AUTH_VALUE)
  75. char *policy_name = "/policy/pol_auth_value";
  76. char *policy_file = FAPI_POLICIES "/policy/pol_auth_value.json";
  77. #elif defined(TEST_POLICY_LOCALITY)
  78. char *policy_name = "/policy/pol_locality";
  79. char *policy_file = FAPI_POLICIES "/policy/pol_locality.json";
  80. #elif defined(TEST_POLICY_PHYSICAL_PRESENCE)
  81. char *policy_name = "/policy/pol_physical_presence";
  82. char *policy_file = FAPI_POLICIES "/policy/pol_physical_presence.json";
  83. #elif defined(TEST_POLICY_COMMAND_CODE)
  84. char *policy_name = "/policy/pol_command_code";
  85. char *policy_file = FAPI_POLICIES "/policy/pol_command_code.json";
  86. #elif defined(TEST_POLICY_COUNTERTIMER)
  87. char *policy_name = "/policy/pol_countertimer";
  88. char *policy_file = FAPI_POLICIES "/policy/pol_countertimer.json";
  89. #else
  90. #error "Please define POLICY_PASSWORD,_AUTH_VALUE,_LOCALITY,_PHYSICAL_PRESENCE,_COMMAND_CODE,_COUNTERTIMER"
  91. #endif
  92. FILE *stream = NULL;
  93. uint8_t *signature =NULL;
  94. char *publicKey = NULL;
  95. char *certificate = NULL;
  96. char *json_policy = NULL;
  97. long policy_size;
  98. r = Fapi_Provision(context, NULL, NULL, NULL);
  99. goto_if_error(r, "Error Fapi_Provision", error);
  100. r = pcr_reset(context, 16);
  101. goto_if_error(r, "Error pcr_reset", error);
  102. stream = fopen(policy_file, "r");
  103. if (!stream) {
  104. LOG_ERROR("File %s does not exist", policy_file);
  105. goto error;
  106. }
  107. fseek(stream, 0L, SEEK_END);
  108. policy_size = ftell(stream);
  109. fclose(stream);
  110. json_policy = malloc(policy_size + 1);
  111. goto_if_null(json_policy,
  112. "Could not allocate memory for the JSON policy",
  113. TSS2_FAPI_RC_MEMORY, error);
  114. stream = fopen(policy_file, "r");
  115. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  116. if (ret != policy_size) {
  117. LOG_ERROR("IO error %s.", policy_file);
  118. goto error;
  119. }
  120. json_policy[policy_size] = '\0';
  121. r = Fapi_Import(context, policy_name, json_policy);
  122. goto_if_error(r, "Error Fapi_Import", error);
  123. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE,
  124. policy_name, PASSWORD);
  125. goto_if_error(r, "Error Fapi_CreateKey", error);
  126. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  127. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  128. goto_if_error(r, "Error Fapi_CreateKey", error);
  129. size_t signatureSize = 0;
  130. TPM2B_DIGEST digest = {
  131. .size = 20,
  132. .buffer = {
  133. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  134. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  135. }
  136. };
  137. r = Fapi_SetAuthCB(context, auth_callback, "");
  138. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  139. r = Fapi_Sign(context, "HS/SRK/mySignKey", NULL,
  140. &digest.buffer[0], digest.size, &signature, &signatureSize,
  141. &publicKey, &certificate);
  142. #if defined(TEST_POLICY_PHYSICAL_PRESENCE)
  143. if (number_rc(r) == TPM2_RC_PP) {
  144. LOG_WARNING("Test requires physical presence.");
  145. goto skip;
  146. } else if (r == TPM2_RC_COMMAND_CODE) {
  147. LOG_WARNING("Command not supported, probably PolicyPhysicalPresence");
  148. goto skip;
  149. }
  150. #endif /* TEST_POLICY_PHYSICAL_PRESENCE */
  151. goto_if_error(r, "Error Fapi_Sign", error);
  152. ASSERT(signature != NULL);
  153. ASSERT(publicKey != NULL);
  154. ASSERT(certificate != NULL);
  155. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  156. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  157. r = Fapi_Delete(context, "/HS/SRK/mySignKey");
  158. goto_if_error(r, "Error Fapi_Delete", error);
  159. r = Fapi_Delete(context, "/");
  160. goto_if_error(r, "Error Fapi_Delete", error);
  161. SAFE_FREE(json_policy);
  162. SAFE_FREE(signature);
  163. SAFE_FREE(publicKey);
  164. SAFE_FREE(certificate);
  165. return EXIT_SUCCESS;
  166. #if defined(TEST_POLICY_PHYSICAL_PRESENCE)
  167. r = Fapi_Delete(context, "/HS/SRK/mySignKey");
  168. goto_if_error(r, "Error Fapi_Delete", error);
  169. r = Fapi_Delete(context, "/");
  170. goto_if_error(r, "Error Fapi_Delete", error);
  171. skip:
  172. Fapi_Delete(context, "/");
  173. SAFE_FREE(json_policy);
  174. SAFE_FREE(signature);
  175. SAFE_FREE(publicKey);
  176. SAFE_FREE(certificate);
  177. return EXIT_SKIP;
  178. #endif /* TEST_POLICY_PHYSICAL_PRESENCE */
  179. error:
  180. Fapi_Delete(context, "/");
  181. SAFE_FREE(json_policy);
  182. SAFE_FREE(signature);
  183. SAFE_FREE(publicKey);
  184. SAFE_FREE(certificate);
  185. return EXIT_FAILURE;
  186. }
  187. int
  188. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  189. {
  190. return test_fapi_key_create_policies_sign(fapi_context);
  191. }