fapi-key-create-sign-password-provision.int.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <string.h>
  11. #include "tss2_fapi.h"
  12. #include "test-fapi.h"
  13. #include "fapi_util.h"
  14. #include "fapi_int.h"
  15. #include "esys_iutil.h"
  16. #define LOGMODULE test
  17. #include "util/log.h"
  18. #include "util/aux_util.h"
  19. #define PASSWORD "abc"
  20. #define SIGN_TEMPLATE "sign,noDa"
  21. static TSS2_RC
  22. auth_callback(
  23. char const *objectPath,
  24. char const *description,
  25. const char **auth,
  26. void *userData)
  27. {
  28. UNUSED(description);
  29. UNUSED(userData);
  30. if (!objectPath) {
  31. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  32. }
  33. char *pw = PASSWORD;
  34. if (!pw)
  35. return TSS2_FAPI_RC_GENERAL_FAILURE;
  36. *auth = pw;
  37. return TSS2_RC_SUCCESS;
  38. }
  39. /** Test the FAPI functions for key creation and usage with an SH password.
  40. *
  41. * Tested FAPI commands:
  42. * - Fapi_Provision()
  43. * - Fapi_SetAuthCB()
  44. * - Fapi_CreateKey()
  45. * - Fapi_GetTpmBlobs()
  46. * - Fapi_Sign()
  47. * - Fapi_SetCertificate()
  48. * - Fapi_List()
  49. * - Fapi_ChangeAuth()
  50. * - Fapi_Delete()
  51. *
  52. * @param[in,out] context The FAPI_CONTEXT.
  53. * @retval EXIT_FAILURE
  54. * @retval EXIT_SUCCESS
  55. */
  56. int
  57. test_fapi_key_create_sign_password_provision(FAPI_CONTEXT *context)
  58. {
  59. TSS2_RC r;
  60. char *sigscheme = NULL;
  61. uint8_t *publicblob = NULL;
  62. uint8_t *privateblob = NULL;
  63. char *policy = NULL;
  64. uint8_t *signature = NULL;
  65. char *publicKey = NULL;
  66. char *certificate = NULL;
  67. char *path_list = NULL;
  68. char *policy_name = "/policy/pol_pcr16_0";
  69. char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_pcr16_0.json";
  70. FILE *stream = NULL;
  71. char *json_policy = NULL;
  72. long policy_size;
  73. size_t publicsize;
  74. size_t privatesize;
  75. const char *cert =
  76. "-----BEGIN CERTIFICATE-----\n"
  77. "MIIDBjCCAe4CCQDcvXBOEVM0UTANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJE\n"
  78. "RTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\n"
  79. "cyBQdHkgTHRkMB4XDTE5MDIyODEwNDkyM1oXDTM1MDgyNzEwNDkyM1owRTELMAkG\n"
  80. "A1UEBhMCREUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\n"
  81. "IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n"
  82. "AKBi+iKwkgM55iCMwXrLCJlu7TzlMu/LlkyGrm99ip2B5+/Cl6a62d8pKelg6zkH\n"
  83. "jI7+AAPteJiW4O+2qVWF8hJ5BXTjGtYbM0iZ6enCb8eyC54C7xVMc21ZIv3ob4Et\n"
  84. "50ZOuzY2pfpzE3vIaXt1CkHlfyI/hdK+mM/dVvuCz5p3AIlHrEWS3rSNgWbCsB2E\n"
  85. "TM55qSGKaLmtTbUvEKRF0TJrFLntfXkv10QD5pgn52+QV9k59OogqZOsDvkXzKPX\n"
  86. "rXF+XC0gLiGBEGAr1dv9F03xMOtO77bQTdGOeC61Tip6Nb0V3ebMckZXwdFi+Nhe\n"
  87. "FRuU33CaObtV6u5PZvSue/MCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAcamUPe8I\n"
  88. "nMOHcv9x5lVN1joihVRmKc0QqNLFc6XpJY8+U5rGkZvOcDe9Da8L97wDNXpKmU/q\n"
  89. "pprj3rT8l3v0Z5xs8Vdr8lxS6T5NhqQV0UCsn1x14gZJcE48y9/LazYi6Zcar+BX\n"
  90. "Am4vewAV3HmQ8X2EctsRhXe4wlAq4slIfEWaaofa8ai7BzO9KwpMLsGPWoNetkB9\n"
  91. "19+SFt0lFFOj/6vDw5pCpSd1nQlo1ug69mJYSX/wcGkV4t4LfGhV8jRPDsGs6I5n\n"
  92. "ETHSN5KV1XCPYJmRCjFY7sIt1x4zN7JJRO9DVw+YheIlduVfkBiF+GlQgLlFTjrJ\n"
  93. "VrpSGMIFSu301A==\n"
  94. "-----END CERTIFICATE-----\n";
  95. if (strcmp("P_ECC", fapi_profile) != 0)
  96. sigscheme = "RSA_PSS";
  97. /* We need to reset the passwords again, in order to not brick physical TPMs */
  98. r = Fapi_Provision(context, NULL, PASSWORD, 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. r = Fapi_SetAuthCB(context, auth_callback, NULL);
  103. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  104. stream = fopen(policy_file, "r");
  105. if (!stream) {
  106. LOG_ERROR("File %s does not exist", policy_file);
  107. goto error;
  108. }
  109. fseek(stream, 0L, SEEK_END);
  110. policy_size = ftell(stream);
  111. fclose(stream);
  112. json_policy = malloc(policy_size + 1);
  113. goto_if_null(json_policy,
  114. "Could not allocate memory for the JSON policy",
  115. TSS2_FAPI_RC_MEMORY, error);
  116. stream = fopen(policy_file, "r");
  117. ssize_t ret = read(fileno(stream), json_policy, policy_size);
  118. if (ret != policy_size) {
  119. LOG_ERROR("IO error %s.", policy_file);
  120. goto error;
  121. }
  122. json_policy[policy_size] = '\0';
  123. r = Fapi_Import(context, policy_name, json_policy);
  124. goto_if_error(r, "Error Fapi_Import", error);
  125. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE, policy_name,
  126. PASSWORD);
  127. goto_if_error(r, "Error Fapi_CreateKey", error);
  128. size_t signatureSize = 0;
  129. TPM2B_DIGEST digest = {
  130. .size = 20,
  131. .buffer = {
  132. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  133. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  134. }
  135. };
  136. r = Fapi_GetTpmBlobs(context, "HS/SRK/mySignKey", &publicblob,
  137. &publicsize,
  138. &privateblob, &privatesize, &policy);
  139. goto_if_error(r, "Error Fapi_GetTpmBlobs", error);
  140. ASSERT(publicblob != NULL);
  141. ASSERT(privateblob != NULL);
  142. ASSERT(policy != NULL);
  143. ASSERT(strlen(policy) > ASSERT_SIZE);
  144. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", cert);
  145. goto_if_error(r, "Error Fapi_SetCertificate", error);
  146. r = Fapi_Sign(context, "HS/SRK/mySignKey", sigscheme,
  147. &digest.buffer[0], digest.size, &signature, &signatureSize,
  148. &publicKey, &certificate);
  149. goto_if_error(r, "Error Fapi_Sign", error);
  150. ASSERT(signature != NULL);
  151. ASSERT(publicKey != NULL);
  152. ASSERT(certificate != NULL);
  153. LOG_INFO("Public key: %s", publicKey);
  154. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  155. LOG_INFO("Certificate: %s", certificate);
  156. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  157. r = Fapi_List(context, "/", &path_list);
  158. goto_if_error(r, "Error Fapi_Delete", error);
  159. ASSERT(path_list != NULL);
  160. ASSERT(strlen(path_list) > ASSERT_SIZE);
  161. fprintf(stderr, "\nPathList:\n%s\n", path_list);
  162. /* We need to reset the passwords again, in order to not brick physical TPMs */
  163. r = Fapi_ChangeAuth(context, "/HS", NULL);
  164. goto_if_error(r, "Error Fapi_ChangeAuth", error);
  165. r = Fapi_Delete(context, "/");
  166. goto_if_error(r, "Error Fapi_Delete", error);
  167. SAFE_FREE(publicblob);
  168. SAFE_FREE(privateblob);
  169. SAFE_FREE(policy);
  170. SAFE_FREE(signature);
  171. SAFE_FREE(publicKey);
  172. SAFE_FREE(certificate);
  173. SAFE_FREE(path_list);
  174. SAFE_FREE(json_policy);
  175. return EXIT_SUCCESS;
  176. error:
  177. Fapi_Delete(context, "/");
  178. SAFE_FREE(publicblob);
  179. SAFE_FREE(privateblob);
  180. SAFE_FREE(policy);
  181. SAFE_FREE(signature);
  182. SAFE_FREE(publicKey);
  183. SAFE_FREE(certificate);
  184. SAFE_FREE(path_list);
  185. SAFE_FREE(json_policy);
  186. return EXIT_FAILURE;
  187. }
  188. int
  189. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  190. {
  191. return test_fapi_key_create_sign_password_provision(fapi_context);
  192. }