fapi-key-create-ckda-sign.int.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. #include "test-fapi.h"
  16. #ifdef FAPI_PASSWORD
  17. #define PASSWORD "abc"
  18. static TSS2_RC
  19. auth_callback(
  20. char const *objectPath,
  21. char const *description,
  22. const char **auth,
  23. void *userData)
  24. {
  25. UNUSED(description);
  26. UNUSED(userData);
  27. if (strcmp(objectPath, "P_RSA/HS/SRK/mySignKey") != 0) {
  28. return_error(TSS2_FAPI_RC_BAD_VALUE, "Unexpected path");
  29. }
  30. *auth = PASSWORD;
  31. return TSS2_RC_SUCCESS;
  32. }
  33. #else /*FAPI_PASSWORD */
  34. #define PASSWORD NULL
  35. #endif /* FAPI_PASSWORD */
  36. #ifdef FAPI_DA
  37. #define SIGN_TEMPLATE "sign"
  38. #else
  39. #define SIGN_TEMPLATE "sign, noDa"
  40. #endif
  41. /** Test the FAPI functions for key creation and usage with noda and da flag.
  42. *
  43. * Tested FAPI commands:
  44. * - Fapi_Provision()
  45. * - Fapi_CreateKey()
  46. * - Fapi_SetAuthCB()
  47. * - Fapi_Sign()
  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_ckda_sign(FAPI_CONTEXT *context)
  56. {
  57. TSS2_RC r;
  58. uint8_t *signature = NULL;
  59. char *publicKey = NULL;
  60. char *certificate = NULL;
  61. r = Fapi_Provision(context, NULL, NULL, NULL);
  62. goto_if_error(r, "Error Fapi_Provision", error);
  63. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE, "",
  64. PASSWORD);
  65. goto_if_error(r, "Error Fapi_CreateKey", error);
  66. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN "\
  67. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  68. goto_if_error(r, "Error Fapi_CreateKey", error);
  69. size_t signatureSize = 0;
  70. TPM2B_DIGEST digest = {
  71. .size = 20,
  72. .buffer = {
  73. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  74. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  75. }
  76. };
  77. #ifdef FAPI_PASSWORD
  78. r = Fapi_SetAuthCB(context, auth_callback, "");
  79. goto_if_error(r, "Error SetPolicyAuthCallback", error);
  80. #endif
  81. r = Fapi_Sign(context, "HS/SRK/mySignKey", NULL,
  82. &digest.buffer[0], digest.size, &signature, &signatureSize,
  83. &publicKey, &certificate);
  84. goto_if_error(r, "Error Fapi_Sign", error);
  85. ASSERT(signature != NULL);
  86. ASSERT(publicKey != NULL);
  87. ASSERT(certificate != NULL);
  88. ASSERT(strstr(publicKey, "BEGIN PUBLIC KEY"));
  89. ASSERT(strstr(certificate, "BEGIN CERTIFICATE"));
  90. r = Fapi_Delete(context, "/");
  91. goto_if_error(r, "Error Fapi_Delete", error);
  92. SAFE_FREE(signature);
  93. SAFE_FREE(publicKey);
  94. SAFE_FREE(certificate);
  95. return EXIT_SUCCESS;
  96. error:
  97. Fapi_Delete(context, "/");
  98. SAFE_FREE(signature);
  99. SAFE_FREE(publicKey);
  100. SAFE_FREE(certificate);
  101. return EXIT_FAILURE;
  102. }
  103. int
  104. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  105. {
  106. return test_fapi_key_create_ckda_sign(fapi_context);
  107. }