fapi-key-create-he-sign.int.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. static TSS2_RC
  25. auth_callback(
  26. char const *objectPath,
  27. char const *description,
  28. const char **auth,
  29. void *userData)
  30. {
  31. UNUSED(description);
  32. UNUSED(userData);
  33. if (!objectPath) {
  34. return_error(TSS2_FAPI_RC_BAD_VALUE, "No path.");
  35. }
  36. *auth = PASSWORD;
  37. return TSS2_RC_SUCCESS;
  38. }
  39. /** Test creation of a signing key in the endorsement hierarchy.
  40. *
  41. * Tested FAPI commands:
  42. * - Fapi_Provision()
  43. * - Fapi_SetAuthCB()
  44. * - Fapi_CreateKey()
  45. * - Fapi_Sign()
  46. * - Fapi_VerifySignature()
  47. * - Fapi_List()
  48. * - Fapi_Delete()
  49. *
  50. * @param[in,out] context The FAPI_CONTEXT.
  51. * @retval EXIT_FAILURE
  52. * @retval EXIT_SUCCESS
  53. */
  54. int
  55. test_fapi_key_create_he_sign(FAPI_CONTEXT *context)
  56. {
  57. TSS2_RC r;
  58. char *sigscheme = NULL;
  59. uint8_t *signature = NULL;
  60. char *publicKey = NULL;
  61. char *path_list = NULL;
  62. if (strcmp("P_ECC", fapi_profile) != 0)
  63. sigscheme = "RSA_PSS";
  64. /* We need to reset the passwords again, in order to not brick physical TPMs */
  65. r = Fapi_Provision(context, NULL, NULL, NULL);
  66. goto_if_error(r, "Error Fapi_Provision", error);
  67. r = Fapi_SetAuthCB(context, auth_callback, NULL);
  68. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  69. r = Fapi_CreateKey(context, "HE/EK/mySignKey", SIGN_TEMPLATE , "",
  70. PASSWORD);
  71. goto_if_error(r, "Error Fapi_CreateKey", error);
  72. size_t signatureSize = 0;
  73. TPM2B_DIGEST digest = {
  74. .size = 32,
  75. .buffer = {
  76. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  77. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  78. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f,
  79. 0x67, 0x68
  80. }
  81. };
  82. r = Fapi_Sign(context, "HE/EK/mySignKey", sigscheme,
  83. &digest.buffer[0], digest.size, &signature, &signatureSize,
  84. &publicKey, NULL);
  85. goto_if_error(r, "Error Fapi_Sign", error);
  86. ASSERT(signature != NULL);
  87. ASSERT(publicKey != NULL);
  88. ASSERT(strlen(publicKey) > ASSERT_SIZE);
  89. r = Fapi_VerifySignature(context, "HE/EK/mySignKey",
  90. &digest.buffer[0], digest.size, signature, signatureSize);
  91. goto_if_error(r, "Error Fapi_VerifySignature", error);
  92. r = Fapi_Delete(context, "/");
  93. goto_if_error(r, "Error Fapi_Delete", error);
  94. SAFE_FREE(path_list);
  95. SAFE_FREE(publicKey);
  96. SAFE_FREE(signature);
  97. return EXIT_SUCCESS;
  98. error:
  99. Fapi_Delete(context, "/");
  100. SAFE_FREE(path_list);
  101. SAFE_FREE(publicKey);
  102. SAFE_FREE(signature);
  103. return EXIT_FAILURE;
  104. }
  105. int
  106. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  107. {
  108. return test_fapi_key_create_he_sign(fapi_context);
  109. }