fapi-key-create-policy-authorize-nv-sign.int.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 "tss2_esys.h"
  18. #include "tss2_tcti.h"
  19. #include "test-fapi.h"
  20. #define LOGMODULE test
  21. #include "util/log.h"
  22. #include "util/aux_util.h"
  23. #define NV_SIZE 34
  24. #define PASSWORD ""
  25. #define SIGN_TEMPLATE "sign"
  26. static TSS2_RC
  27. check_tpm_cmd(FAPI_CONTEXT *context, TPM2_CC command_code)
  28. {
  29. TSS2_RC r;
  30. TSS2_TCTI_CONTEXT *tcti;
  31. ESYS_CONTEXT *esys;
  32. TPMS_CAPABILITY_DATA *cap_data;
  33. r = Fapi_GetTcti(context, &tcti);
  34. goto_if_error(r, "Error Fapi_GetTcti", error);
  35. r = Esys_Initialize(&esys, tcti, NULL);
  36. goto_if_error(r, "Error Fapi_GetTcti", error);
  37. r = Esys_GetCapability(esys,
  38. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  39. TPM2_CAP_COMMANDS, command_code, 1, NULL, &cap_data);
  40. Esys_Finalize(&esys);
  41. return_if_error(r, "Error: GetCapabilities");
  42. if ((cap_data->data.command.commandAttributes[0] & TPMA_CC_COMMANDINDEX_MASK) ==
  43. command_code) {
  44. free(cap_data);
  45. return TSS2_RC_SUCCESS;
  46. } else {
  47. free(cap_data);
  48. return TSS2_FAPI_RC_NOT_IMPLEMENTED;
  49. }
  50. error:
  51. return r;
  52. }
  53. static char *
  54. read_policy(FAPI_CONTEXT *context, char *policy_name)
  55. {
  56. FILE *stream = NULL;
  57. long policy_size;
  58. char *json_policy = NULL;
  59. char policy_file[1024];
  60. if (snprintf(&policy_file[0], 1023, TOP_SOURCEDIR "/test/data/fapi/%s.json", policy_name) < 0)
  61. return NULL;
  62. stream = fopen(policy_file, "r");
  63. if (!stream) {
  64. LOG_ERROR("File %s does not exist", policy_file);
  65. return NULL;
  66. }
  67. fseek(stream, 0L, SEEK_END);
  68. policy_size = ftell(stream);
  69. fclose(stream);
  70. json_policy = malloc(policy_size + 1);
  71. stream = fopen(policy_file, "r");
  72. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  73. if (ret != policy_size) {
  74. LOG_ERROR("IO error %s.", policy_file);
  75. return NULL;
  76. }
  77. json_policy[policy_size] = '\0';
  78. return json_policy;
  79. }
  80. /** Test the FAPI key signing with PolicyAuthorizeNV.
  81. *
  82. * Tested FAPI commands:
  83. * - Fapi_GetTcti()
  84. * - Fapi_Provision()
  85. * - Fapi_CreateNv()
  86. * - Fapi_Import()
  87. * - Fapi_WriteAuthorizeNv()
  88. * - Fapi_CreateKey()
  89. * - Fapi_Sign()
  90. * - Fapi_Delete()
  91. *
  92. * Tested Policies:
  93. * - PolicyAuthorizeNv
  94. *
  95. * @param[in,out] context The FAPI_CONTEXT.
  96. * @retval EXIT_FAILURE
  97. * @retval EXIT_SUCCESS
  98. */
  99. int
  100. test_fapi_key_create_policy_authorize_nv(FAPI_CONTEXT *context)
  101. {
  102. TSS2_RC r;
  103. char *nvPathPolicy = "/nv/Owner/myNV";
  104. char *policy_authorize_nv = "/policy/pol_authorize_nv";
  105. char *policy_pcr2 = "/policy/pol_pcr16_0";
  106. char *json_policy = NULL;
  107. uint8_t *signature = NULL;
  108. char *publicKey = NULL;
  109. char *certificate = NULL;
  110. if (check_tpm_cmd(context, TPM2_CC_PolicyAuthorizeNV) != TPM2_RC_SUCCESS) {
  111. LOG_WARNING("Command PolicyAuthorizeNV not available.");
  112. return EXIT_SKIP;
  113. }
  114. r = Fapi_Provision(context, NULL, NULL, NULL);
  115. goto_if_error(r, "Error Fapi_Provision", error);
  116. /* Create NV object for storing the policy */
  117. r = Fapi_CreateNv(context, nvPathPolicy, "noda", 34, "", "");
  118. goto_if_error(r, "Error Fapi_CreateNv", error);
  119. r = pcr_reset(context, 16);
  120. goto_if_error(r, "Error pcr_reset", error);
  121. json_policy = read_policy(context, policy_authorize_nv);
  122. if (!json_policy)
  123. goto error;
  124. r = Fapi_Import(context, policy_authorize_nv, json_policy);
  125. goto_if_error(r, "Error Fapi_Import", error);
  126. SAFE_FREE(json_policy);
  127. json_policy = read_policy(context, policy_pcr2);
  128. if (!json_policy)
  129. goto error;
  130. r = Fapi_Import(context, policy_pcr2, json_policy);
  131. goto_if_error(r, "Error Fapi_Import", error);
  132. r = Fapi_WriteAuthorizeNv(context,nvPathPolicy, policy_pcr2);
  133. goto_if_error(r, "Error Fapi_WriteAuthorizeNv", error);
  134. r = Fapi_CreateKey(context, "/HS/SRK/myPolicySignKey", "sign",
  135. "", PASSWORD);
  136. goto_if_error(r, "Error Fapi_CreateKey", error);
  137. r = Fapi_CreateKey(context, "/HS/SRK/mySignKey", SIGN_TEMPLATE,
  138. policy_authorize_nv, PASSWORD);
  139. goto_if_error(r, "Error Fapi_CreateKey", error);
  140. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  141. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  142. goto_if_error(r, "Error Fapi_CreateKey", error);
  143. size_t signatureSize = 0;
  144. TPM2B_DIGEST digest = {
  145. .size = 20,
  146. .buffer = {
  147. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  148. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  149. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  150. 0x41, 0x42
  151. }
  152. };
  153. r = Fapi_Sign(context, "/HS/SRK/mySignKey", NULL,
  154. &digest.buffer[0], digest.size, &signature, &signatureSize,
  155. &publicKey, &certificate);
  156. goto_if_error(r, "Error Fapi_Sign", error);
  157. ASSERT(signature);
  158. ASSERT(publicKey);
  159. ASSERT(certificate);
  160. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  161. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  162. r = Fapi_Delete(context, policy_authorize_nv);
  163. goto_if_error(r, "Error Fapi_Delete", error);
  164. r = Fapi_Delete(context, nvPathPolicy);
  165. goto_if_error(r, "Error Fapi_NV_Undefine", error);
  166. r = Fapi_Delete(context, "/");
  167. goto_if_error(r, "Error Fapi_Delete", error);
  168. SAFE_FREE(json_policy);
  169. SAFE_FREE(signature);
  170. SAFE_FREE(publicKey);
  171. SAFE_FREE(certificate);
  172. return EXIT_SUCCESS;
  173. error:
  174. Fapi_Delete(context, "/");
  175. SAFE_FREE(json_policy);
  176. SAFE_FREE(signature);
  177. SAFE_FREE(publicKey);
  178. SAFE_FREE(certificate);
  179. return EXIT_FAILURE;
  180. }
  181. int
  182. test_invoke_fapi(FAPI_CONTEXT *context)
  183. {
  184. return test_fapi_key_create_policy_authorize_nv(context);
  185. }