fapi-key-change-auth.int.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <stdio.h>
  12. #include "tss2_fapi.h"
  13. #define LOGMODULE test
  14. #include "util/log.h"
  15. #include "util/aux_util.h"
  16. #include "test-fapi.h"
  17. #define PASSWORD "abc"
  18. #define USER_DATA "my user data"
  19. #define DESCRIPTION "my description"
  20. #define FAPI_PROFILE fapi_profile
  21. #define OBJECT_PATH "HS/SRK/mySignKey"
  22. static TSS2_RC
  23. auth_callback(
  24. char const *objectPath,
  25. char const *description,
  26. const char **auth,
  27. void *userData)
  28. {
  29. UNUSED(description);
  30. UNUSED(userData);
  31. char *profile_path;
  32. ASSERT(description != NULL);
  33. ASSERT(userData != NULL);
  34. if (!objectPath) {
  35. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  36. }
  37. int size = asprintf (&profile_path, "%s/%s", fapi_profile, OBJECT_PATH);
  38. if (size == -1)
  39. return TSS2_FAPI_RC_MEMORY;
  40. ASSERT(strlen(objectPath) == strlen(profile_path));
  41. free(profile_path);
  42. ASSERT(strlen(userData) == strlen((char*)USER_DATA));
  43. ASSERT(strlen(description) == strlen(DESCRIPTION));
  44. *auth = PASSWORD;
  45. return TSS2_RC_SUCCESS;
  46. error:
  47. exit(EXIT_FAILURE);
  48. }
  49. /** Test the FAPI function for changing key authorizations.
  50. *
  51. * The setting of the authorization callback and usage of the
  52. * key with Fapi_Sign afterwards is also tested.
  53. *
  54. * Tested FAPI commands:
  55. * - Fapi_Provision()
  56. * - Fapi_CreateKey()
  57. * - Fapi_ChangeAuth()
  58. * - Fapi_SetAuthCB()
  59. * - Fapi_Sign()
  60. * - Fapi_Delete()
  61. *
  62. * @param[in,out] context The FAPI_CONTEXT.
  63. * @retval EXIT_FAILURE
  64. * @retval EXIT_SUCCESS
  65. */
  66. int
  67. test_fapi_key_change_auth(FAPI_CONTEXT *context)
  68. {
  69. TSS2_RC r;
  70. uint8_t *signature = NULL;
  71. char *publicKey = NULL;
  72. char *certificate = NULL;
  73. r = Fapi_Provision(context, NULL, NULL, NULL);
  74. goto_if_error(r, "Error Fapi_Provision", error);
  75. r = Fapi_CreateKey(context, OBJECT_PATH, "sign,noDa", "", NULL);
  76. goto_if_error(r, "Error Fapi_CreateKey", error);
  77. r = Fapi_SetDescription(context, OBJECT_PATH, DESCRIPTION);
  78. goto_if_error(r, "Error Fapi_SetDescription", error);
  79. r = Fapi_SetCertificate(context, OBJECT_PATH, "-----BEGIN "\
  80. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  81. goto_if_error(r, "Error Fapi_CreateKey", error);
  82. size_t signatureSize = 0;
  83. TPM2B_DIGEST digest = {
  84. .size = 20,
  85. .buffer = {
  86. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  87. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  88. }
  89. };
  90. r = Fapi_ChangeAuth(context, OBJECT_PATH, PASSWORD);
  91. goto_if_error(r, "Error Fapi_Provision", error);
  92. r = Fapi_SetAuthCB(context, auth_callback, USER_DATA);
  93. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  94. r = Fapi_Sign(context, OBJECT_PATH, NULL,
  95. &digest.buffer[0], digest.size, &signature, &signatureSize,
  96. &publicKey, &certificate);
  97. goto_if_error(r, "Error Fapi_Provision", error);
  98. ASSERT(signature != NULL);
  99. ASSERT(publicKey != NULL);
  100. ASSERT(certificate != NULL);
  101. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  102. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  103. Fapi_Free(publicKey);
  104. r = Fapi_Delete(context, "/");
  105. goto_if_error(r, "Error Fapi_Delete", error);
  106. SAFE_FREE(signature);
  107. SAFE_FREE(certificate);
  108. return EXIT_SUCCESS;
  109. error:
  110. Fapi_Delete(context, "/");
  111. SAFE_FREE(signature);
  112. SAFE_FREE(certificate);
  113. return EXIT_FAILURE;
  114. }
  115. int
  116. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  117. {
  118. return test_fapi_key_change_auth(fapi_context);
  119. }