fapi-key-create-policy-secret-nv-sign.int.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <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 NV_SIZE 34
  22. #define PASSWORD "abc"
  23. #define SIGN_TEMPLATE "sign"
  24. static TSS2_RC
  25. auth_callback(
  26. char const *objectPath,
  27. char const *description,
  28. const char **auth,
  29. void *userData)
  30. {
  31. UNUSED(description);
  32. UNUSED(userData);
  33. if (!objectPath) {
  34. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  35. }
  36. *auth = PASSWORD;
  37. return TSS2_RC_SUCCESS;
  38. }
  39. static char *
  40. read_policy(FAPI_CONTEXT *context, char *policy_name)
  41. {
  42. FILE *stream = NULL;
  43. long policy_size;
  44. char *json_policy = NULL;
  45. char policy_file[1024];
  46. if (snprintf(&policy_file[0], 1023, TOP_SOURCEDIR "/test/data/fapi/%s.json", policy_name) < 0)
  47. return NULL;
  48. stream = fopen(policy_file, "r");
  49. if (!stream) {
  50. LOG_ERROR("File %s does not exist", policy_file);
  51. return NULL;
  52. }
  53. fseek(stream, 0L, SEEK_END);
  54. policy_size = ftell(stream);
  55. fclose(stream);
  56. json_policy = malloc(policy_size + 1);
  57. return_if_null(json_policy,
  58. "Could not allocate memory for the JSON policy",
  59. NULL);
  60. stream = fopen(policy_file, "r");
  61. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  62. if (ret != policy_size) {
  63. LOG_ERROR("IO error %s.", policy_file);
  64. return NULL;
  65. }
  66. json_policy[policy_size] = '\0';
  67. return json_policy;
  68. }
  69. /** Test the FAPI PolicySecret and PolicyAuthValue handling.
  70. *
  71. * Tested FAPI commands:
  72. * - Fapi_Provision()
  73. * - Fapi_Import()
  74. * - Fapi_CreateNv()
  75. * - Fapi_CreateKey()
  76. * - Fapi_Sign()
  77. * - Fapi_SetAuthCB()
  78. * - Fapi_Delete()
  79. *
  80. * Tested Policies:
  81. * - PolicySecret
  82. * - PolicyAuthValue
  83. *
  84. * @param[in,out] context The FAPI_CONTEXT.
  85. * @retval EXIT_FAILURE
  86. * @retval EXIT_SUCCESS
  87. */
  88. int
  89. test_fapi_key_create_policy_secret_nv_sign(FAPI_CONTEXT *context)
  90. {
  91. TSS2_RC r;
  92. char *nv_path_auth_object = "/nv/Owner/myNV";
  93. char *policy_nv = "/policy/pol_auth_value";
  94. char *policy_secret = "/policy/pol_secret";
  95. char *sign_key = "/HS/SRK/mySignkey";
  96. char *json_policy = NULL;
  97. uint8_t *signature = NULL;
  98. char *publicKey = NULL;
  99. char *certificate = NULL;
  100. r = Fapi_Provision(context, NULL, NULL, NULL);
  101. goto_if_error(r, "Error Fapi_Provision", error);
  102. json_policy = read_policy(context, policy_nv);
  103. if (!json_policy)
  104. goto error;
  105. r = Fapi_Import(context, policy_nv, json_policy);
  106. goto_if_error(r, "Error Fapi_Import", error);
  107. /* Create NV Object with policy which will be used for key authorization */
  108. r = Fapi_CreateNv(context, nv_path_auth_object, "noda", 34, policy_nv, PASSWORD);
  109. goto_if_error(r, "Error Fapi_CreateNv", error);
  110. SAFE_FREE(json_policy);
  111. json_policy = read_policy(context, policy_secret);
  112. if (!json_policy)
  113. goto error;
  114. r = Fapi_Import(context, policy_secret, json_policy);
  115. goto_if_error(r, "Error Fapi_Import", error);
  116. r = Fapi_CreateKey(context, sign_key, SIGN_TEMPLATE,
  117. policy_secret, "");
  118. goto_if_error(r, "Error Fapi_CreateKey", error);
  119. r = Fapi_SetCertificate(context, sign_key, "-----BEGIN "\
  120. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  121. goto_if_error(r, "Error Fapi_CreateKey", error);
  122. size_t signatureSize = 0;
  123. TPM2B_DIGEST digest = {
  124. .size = 32,
  125. .buffer = {
  126. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  127. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  128. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  129. 0x41, 0x42
  130. }
  131. };
  132. LOG_ERROR("***** START TEST ERROR ******");
  133. r = Fapi_Sign(context, sign_key, NULL,
  134. &digest.buffer[0], digest.size, &signature, &signatureSize,
  135. &publicKey, &certificate);
  136. LOG_ERROR("***** END TEST ERROR ******");
  137. if (r == TSS2_RC_SUCCESS)
  138. goto error;
  139. ASSERT(signature == NULL);
  140. ASSERT(publicKey == NULL);
  141. ASSERT(certificate == NULL);
  142. r = Fapi_SetAuthCB(context, auth_callback, "");
  143. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  144. signature = NULL;
  145. publicKey = NULL;
  146. certificate = NULL;
  147. r = Fapi_Sign(context, sign_key, NULL,
  148. &digest.buffer[0], digest.size, &signature, &signatureSize,
  149. &publicKey, &certificate);
  150. goto_if_error(r, "Error Fapi_Sign", error);
  151. ASSERT(signature != NULL);
  152. ASSERT(publicKey != NULL);
  153. ASSERT(certificate != NULL);
  154. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  155. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  156. r = Fapi_Delete(context, nv_path_auth_object);
  157. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  158. r = Fapi_Delete(context, "/");
  159. goto_if_error(r, "Error Fapi_Delete", error);
  160. SAFE_FREE(signature);
  161. SAFE_FREE(publicKey);
  162. SAFE_FREE(certificate);
  163. SAFE_FREE(json_policy);
  164. return EXIT_SUCCESS;
  165. error:
  166. Fapi_Delete(context, "/");
  167. SAFE_FREE(signature);
  168. SAFE_FREE(publicKey);
  169. SAFE_FREE(certificate);
  170. SAFE_FREE(json_policy);
  171. return EXIT_FAILURE;
  172. }
  173. int
  174. test_invoke_fapi(FAPI_CONTEXT *context)
  175. {
  176. return test_fapi_key_create_policy_secret_nv_sign(context);
  177. }