fapi-key-create-policy-authorize-pem-sign.int.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 OBJECT_PATH "HS/SRK/mySignKey"
  22. #define USER_DATA "my user data"
  23. #define DESCRIPTION "PolicyAuthorize"
  24. /** Test the FAPI functions for PolicyAuthoirze with signing.
  25. *
  26. * Tested FAPI commands:
  27. * - Fapi_Provision()
  28. * - Fapi_SetBranchCB()
  29. * - Fapi_Import()
  30. * - Fapi_CreateKey()
  31. * - Fapi_Sign()
  32. * - Fapi_List()
  33. * - Fapi_Delete()
  34. *
  35. * Tested Policies:
  36. * - PolicyNameHash
  37. * - PolicyAuthorize
  38. * - PolicyCpHash (Not entered, only as alternative branch)
  39. *
  40. * @param[in,out] context The FAPI_CONTEXT.
  41. * @retval EXIT_FAILURE
  42. * @retval EXIT_SUCCESS
  43. */
  44. int
  45. test_fapi_key_create_policy_authorize_pem_sign(FAPI_CONTEXT *context)
  46. {
  47. TSS2_RC r;
  48. char *policy_pcr = "/policy/pol_pcr";
  49. #ifdef TEST_ECC
  50. char *policy_file_pcr = TOP_SOURCEDIR "/test/data/fapi/policy/pol_pcr16_0_ecc_authorized.json";
  51. char *policy_file_authorize = TOP_SOURCEDIR "/test/data/fapi/policy/pol_authorize_ecc_pem.json";
  52. #else
  53. char *policy_file_pcr = TOP_SOURCEDIR "/test/data/fapi/policy/pol_pcr16_0_rsa_authorized.json";
  54. char *policy_file_authorize = TOP_SOURCEDIR "/test/data/fapi/policy/pol_authorize_rsa_pem.json";
  55. #endif
  56. char *policy_name_authorize = "/policy/pol_authorize";
  57. // uint8_t policyRef[] = { 1, 2, 3, 4, 5 };
  58. FILE *stream = NULL;
  59. char *json_policy = NULL;
  60. long policy_size;
  61. uint8_t *signature = NULL;
  62. char *publicKey = NULL;
  63. char *pathList = NULL;
  64. r = Fapi_Provision(context, NULL, NULL, NULL);
  65. goto_if_error(r, "Error Fapi_Provision", error);
  66. r = pcr_reset(context, 16);
  67. goto_if_error(r, "Error pcr_reset", error);
  68. /* Read in the first policy */
  69. stream = fopen(policy_file_pcr, "r");
  70. if (!stream) {
  71. LOG_ERROR("File %s does not exist", policy_file_pcr);
  72. goto error;
  73. }
  74. fseek(stream, 0L, SEEK_END);
  75. policy_size = ftell(stream);
  76. fclose(stream);
  77. json_policy = malloc(policy_size + 1);
  78. goto_if_null(json_policy,
  79. "Could not allocate memory for the JSON policy",
  80. TSS2_FAPI_RC_MEMORY, error);
  81. stream = fopen(policy_file_pcr, "r");
  82. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  83. if (ret != policy_size) {
  84. LOG_ERROR("IO error %s.", policy_file_pcr);
  85. goto error;
  86. }
  87. json_policy[policy_size] = '\0';
  88. r = Fapi_Import(context, policy_pcr, json_policy);
  89. SAFE_FREE(json_policy);
  90. goto_if_error(r, "Error Fapi_List", error);
  91. /* Read in the authorize policy */
  92. stream = fopen(policy_file_authorize, "r");
  93. if (!stream) {
  94. LOG_ERROR("File %s does not exist", policy_file_authorize);
  95. goto error;
  96. }
  97. fseek(stream, 0L, SEEK_END);
  98. policy_size = ftell(stream);
  99. fclose(stream);
  100. json_policy = malloc(policy_size + 1);
  101. goto_if_null(json_policy,
  102. "Could not allocate memory for the JSON policy",
  103. TSS2_FAPI_RC_MEMORY, error);
  104. stream = fopen(policy_file_authorize, "r");
  105. ret = read(fileno(stream), json_policy, policy_size);
  106. if (ret != policy_size) {
  107. LOG_ERROR("IO error %s.", policy_file_authorize);
  108. goto error;
  109. }
  110. json_policy[policy_size] = '\0';
  111. r = Fapi_Import(context, policy_name_authorize, json_policy);
  112. SAFE_FREE(json_policy);
  113. goto_if_error(r, "Error Fapi_Import", error);
  114. /* Create key and use them to authorize the policy */
  115. r = Fapi_CreateKey(context, "HS/SRK/myPolicySignKey", "sign,noDa",
  116. "", NULL);
  117. goto_if_error(r, "Error Fapi_CreateKey", error);
  118. /* Create the actual key */
  119. r = Fapi_CreateKey(context, OBJECT_PATH, "sign, noda",
  120. policy_name_authorize, NULL);
  121. goto_if_error(r, "Error Fapi_CreateKey", error);
  122. /* Use the key */
  123. size_t signatureSize = 0;
  124. TPM2B_DIGEST digest = {
  125. .size = 32,
  126. .buffer = {
  127. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  128. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  129. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  130. 0x41, 0x42
  131. }
  132. };
  133. r = Fapi_Sign(context, OBJECT_PATH, NULL,
  134. &digest.buffer[0], digest.size, &signature, &signatureSize,
  135. &publicKey, NULL);
  136. goto_if_error(r, "Error Fapi_Sign", error);
  137. ASSERT(signature != NULL);
  138. ASSERT(publicKey != NULL);
  139. LOG_INFO("PublicKey: %s", publicKey);
  140. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  141. /* Cleanup */
  142. r = Fapi_Delete(context, "/");
  143. goto_if_error(r, "Error Fapi_Delete", error);
  144. SAFE_FREE(signature);
  145. SAFE_FREE(publicKey);
  146. return EXIT_SUCCESS;
  147. error:
  148. Fapi_Delete(context, "/");
  149. SAFE_FREE(json_policy);
  150. SAFE_FREE(signature);
  151. SAFE_FREE(publicKey);
  152. SAFE_FREE(pathList);
  153. Fapi_Delete(context, "/");
  154. return EXIT_FAILURE;
  155. }
  156. int
  157. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  158. {
  159. return test_fapi_key_create_policy_authorize_pem_sign(fapi_context);
  160. }