fapi-key-create-sign.int.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #ifndef FAPI_PROFILE
  22. #define FAPI_PROFILE "P_ECC"
  23. #endif /* FAPI_PROFILE */
  24. json_object *
  25. get_json_hex_string(const uint8_t *buffer, size_t size)
  26. {
  27. char hex_string[size * 2 + 1];
  28. for (size_t i = 0, off = 0; i < size; i++, off += 2) {
  29. sprintf(&hex_string[off], "%02x", buffer[i]);
  30. }
  31. hex_string[(size) * 2] = '\0';
  32. json_object *jso = json_object_new_string(hex_string);
  33. return jso;
  34. }
  35. static TSS2_RC
  36. auth_callback(
  37. char const *objectPath,
  38. char const *description,
  39. const char **auth,
  40. void *userData)
  41. {
  42. UNUSED(description);
  43. UNUSED(userData);
  44. if (!objectPath) {
  45. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  46. }
  47. *auth = PASSWORD;
  48. return TSS2_RC_SUCCESS;
  49. }
  50. /** Test the FAPI functions for TpmBlobs and certificates.
  51. *
  52. * Tested FAPI commands:
  53. * - Fapi_Provision()
  54. * - Fapi_SetAuthCB()
  55. * - Fapi_CreateKey()
  56. * - Fapi_GetTpmBlobs()
  57. * - Fapi_Sign()
  58. * - Fapi_VerifySignature()
  59. * - Fapi_SetCertificate()
  60. * - Fapi_List()
  61. * - Fapi_ChangeAuth()
  62. * - Fapi_Delete()
  63. *
  64. * @param[in,out] context The FAPI_CONTEXT.
  65. * @retval EXIT_FAILURE
  66. * @retval EXIT_SUCCESS
  67. */
  68. int
  69. test_fapi_key_create_sign(FAPI_CONTEXT *context)
  70. {
  71. TSS2_RC r;
  72. char *sigscheme = NULL;
  73. const char *cert =
  74. "-----BEGIN CERTIFICATE-----\n"
  75. "MIIDBjCCAe4CCQDcvXBOEVM0UTANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJE\n"
  76. "RTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0\n"
  77. "cyBQdHkgTHRkMB4XDTE5MDIyODEwNDkyM1oXDTM1MDgyNzEwNDkyM1owRTELMAkG\n"
  78. "A1UEBhMCREUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0\n"
  79. "IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n"
  80. "AKBi+iKwkgM55iCMwXrLCJlu7TzlMu/LlkyGrm99ip2B5+/Cl6a62d8pKelg6zkH\n"
  81. "jI7+AAPteJiW4O+2qVWF8hJ5BXTjGtYbM0iZ6enCb8eyC54C7xVMc21ZIv3ob4Et\n"
  82. "50ZOuzY2pfpzE3vIaXt1CkHlfyI/hdK+mM/dVvuCz5p3AIlHrEWS3rSNgWbCsB2E\n"
  83. "TM55qSGKaLmtTbUvEKRF0TJrFLntfXkv10QD5pgn52+QV9k59OogqZOsDvkXzKPX\n"
  84. "rXF+XC0gLiGBEGAr1dv9F03xMOtO77bQTdGOeC61Tip6Nb0V3ebMckZXwdFi+Nhe\n"
  85. "FRuU33CaObtV6u5PZvSue/MCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAcamUPe8I\n"
  86. "nMOHcv9x5lVN1joihVRmKc0QqNLFc6XpJY8+U5rGkZvOcDe9Da8L97wDNXpKmU/q\n"
  87. "pprj3rT8l3v0Z5xs8Vdr8lxS6T5NhqQV0UCsn1x14gZJcE48y9/LazYi6Zcar+BX\n"
  88. "Am4vewAV3HmQ8X2EctsRhXe4wlAq4slIfEWaaofa8ai7BzO9KwpMLsGPWoNetkB9\n"
  89. "19+SFt0lFFOj/6vDw5pCpSd1nQlo1ug69mJYSX/wcGkV4t4LfGhV8jRPDsGs6I5n\n"
  90. "ETHSN5KV1XCPYJmRCjFY7sIt1x4zN7JJRO9DVw+YheIlduVfkBiF+GlQgLlFTjrJ\n"
  91. "VrpSGMIFSu301A==\n"
  92. "-----END CERTIFICATE-----\n";
  93. uint8_t *signature = NULL;
  94. char *publicKey = NULL;
  95. char *certificate = NULL;
  96. uint8_t *publicblob = NULL;
  97. uint8_t *privateblob = NULL;
  98. char *policy = NULL;
  99. char *path_list = NULL;
  100. size_t publicsize;
  101. size_t privatesize;
  102. json_object *jso = NULL;
  103. char *description = NULL;
  104. if (strcmp("P_ECC", fapi_profile) != 0)
  105. sigscheme = "RSA_PSS";
  106. /* We need to reset the passwords again, in order to not brick physical TPMs */
  107. r = Fapi_Provision(context, NULL, PASSWORD, NULL);
  108. goto_if_error(r, "Error Fapi_Provision", error);
  109. r = Fapi_SetAuthCB(context, auth_callback, NULL);
  110. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  111. #ifdef PERSISTENT
  112. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE ",0x81000004", "",
  113. PASSWORD);
  114. #else
  115. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE "", "",
  116. PASSWORD);
  117. #endif
  118. goto_if_error(r, "Error Fapi_CreateKey_Async", error);
  119. goto_if_error(r, "Error Fapi_CreateKey_Finish", error);
  120. size_t signatureSize = 0;
  121. TPM2B_DIGEST digest = {
  122. .size = 32,
  123. .buffer = {
  124. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  125. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  126. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  127. 0x67, 0x68
  128. }
  129. };
  130. r = Fapi_GetTpmBlobs(context, "HS/SRK/mySignKey", &publicblob,
  131. &publicsize,
  132. &privateblob, &privatesize, &policy);
  133. goto_if_error(r, "Error Fapi_GetTpmBlobs", error);
  134. ASSERT(publicblob != NULL);
  135. ASSERT(privateblob != NULL);
  136. ASSERT(policy != NULL);
  137. ASSERT(strlen(policy) == 0);
  138. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", cert);
  139. goto_if_error(r, "Error Fapi_SetCertificate", error);
  140. r = Fapi_Sign(context, "HS/SRK/mySignKey", sigscheme,
  141. &digest.buffer[0], digest.size, &signature, &signatureSize,
  142. &publicKey, &certificate);
  143. goto_if_error(r, "Error Fapi_Sign", error);
  144. ASSERT(signature != NULL);
  145. ASSERT(publicKey != NULL);
  146. ASSERT(certificate != NULL);
  147. ASSERT(strlen(publicKey) > ASSERT_SIZE);
  148. ASSERT(strlen(certificate) > ASSERT_SIZE);
  149. r = Fapi_VerifySignature(context, "HS/SRK/mySignKey",
  150. &digest.buffer[0], digest.size, signature, signatureSize);
  151. goto_if_error(r, "Error Fapi_VerifySignature", error);
  152. /* Create json date to import binary public and private blobs under the same
  153. parent key. */
  154. json_object * publicblobHex_jso = get_json_hex_string(publicblob, publicsize);
  155. goto_if_null2(publicblobHex_jso, "Out of memory", r, TSS2_FAPI_RC_MEMORY, error);
  156. json_object * privateblobHex_jso = get_json_hex_string(privateblob, privatesize);
  157. goto_if_null2(privateblobHex_jso, "Out of memory", r, TSS2_FAPI_RC_MEMORY, error);
  158. jso = json_object_new_object();
  159. goto_if_null2(jso, "Out of memory", r, TSS2_FAPI_RC_MEMORY, error);
  160. json_object_object_add(jso, "public", publicblobHex_jso);
  161. json_object_object_add(jso, "private", privateblobHex_jso);
  162. const char * jso_string = json_object_to_json_string_ext(jso, JSON_C_TO_STRING_PRETTY);
  163. r = Fapi_Import(context, "HS/SRK/mySignKey2", jso_string);
  164. goto_if_error(r, "Error Fapi_Import", error);
  165. r = Fapi_VerifySignature(context, "HS/SRK/mySignKey2",
  166. &digest.buffer[0], digest.size, signature, signatureSize);
  167. goto_if_error(r, "Error Fapi_VerifySignature", error);
  168. r = Fapi_List(context, "/", &path_list);
  169. goto_if_error(r, "Error Fapi_Delete", error);
  170. ASSERT(path_list != NULL);
  171. LOG_INFO("Path list: %s", path_list);
  172. char *check_path_list =
  173. "/" FAPI_PROFILE "/HS/SRK:/" FAPI_PROFILE "/HS:/" FAPI_PROFILE "/LOCKOUT:/" FAPI_PROFILE "/HE/EK:/" FAPI_PROFILE "/HE:"
  174. "/" FAPI_PROFILE "/HN:/" FAPI_PROFILE "/HS/SRK/mySignKey2:/" FAPI_PROFILE "/HS/SRK/mySignKey";
  175. ASSERT(cmp_strtokens(path_list, check_path_list, ":"));
  176. /* We need to reset the passwords again, in order to not brick physical TPMs */
  177. r = Fapi_ChangeAuth(context, "/HS", NULL);
  178. goto_if_error(r, "Error Fapi_ChangeAuth", error);
  179. r = Fapi_GetDescription(context, "/HS/SRK", &description);
  180. goto_if_error(r, "Error GetDescription", error);
  181. if (description) {
  182. LOG_INFO("SRK description: %s", description);
  183. SAFE_FREE(description);
  184. }
  185. r = Fapi_GetDescription(context, "/HE/EK", &description);
  186. goto_if_error(r, "Error GetDescription", error);
  187. if (description) {
  188. LOG_INFO("EK description: %s", description);
  189. SAFE_FREE(description);
  190. }
  191. r = Fapi_Delete(context, "/");
  192. goto_if_error(r, "Error Fapi_Delete", error);
  193. json_object_put(jso);
  194. SAFE_FREE(path_list);
  195. SAFE_FREE(publicblob);
  196. SAFE_FREE(privateblob);
  197. SAFE_FREE(policy);
  198. SAFE_FREE(publicKey);
  199. SAFE_FREE(signature);
  200. SAFE_FREE(certificate);
  201. return EXIT_SUCCESS;
  202. error:
  203. if (jso)
  204. json_object_put(jso);
  205. Fapi_Delete(context, "/");
  206. SAFE_FREE(path_list);
  207. SAFE_FREE(publicblob);
  208. SAFE_FREE(privateblob);
  209. SAFE_FREE(policy);
  210. SAFE_FREE(publicKey);
  211. SAFE_FREE(signature);
  212. SAFE_FREE(certificate);
  213. return EXIT_FAILURE;
  214. }
  215. int
  216. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  217. {
  218. return test_fapi_key_create_sign(fapi_context);
  219. }