/test/integration/fapi-quote-destructive.int.c

https://github.com/tpm2-software/tpm2-tss · C · 147 lines · 104 code · 23 blank · 20 comment · 5 complexity · a51e570cbac71e8df73eda911f4a7c39 MD5 · raw file

  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 <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <unistd.h>
  13. #include <errno.h>
  14. #include <string.h>
  15. #include <assert.h>
  16. #include "tss2_fapi.h"
  17. #include "test-fapi.h"
  18. #define LOGMODULE test
  19. #include "util/log.h"
  20. #include "util/aux_util.h"
  21. #define EVENT_SIZE 10
  22. /** Test the FAPI functions for quote commands.
  23. *
  24. * Tested FAPI commands:
  25. * - Fapi_Provision()
  26. * - Fapi_CreateKey()
  27. * - Fapi_PcrExtend()
  28. * - Fapi_Quote()
  29. * - Fapi_VerifyQuote()
  30. * - Fapi_List()
  31. * - Fapi_Delete()
  32. *
  33. * @param[in,out] context The FAPI_CONTEXT.
  34. * @retval EXIT_FAILURE
  35. * @retval EXIT_SUCCESS
  36. */
  37. int
  38. test_fapi_quote_destructive(FAPI_CONTEXT *context)
  39. {
  40. TSS2_RC r;
  41. char *pubkey_pem = NULL;
  42. uint8_t *signature = NULL;
  43. char *quoteInfo = NULL;
  44. char *pcrEventLog = NULL;
  45. char *certificate = NULL;
  46. char *export_data = NULL;
  47. uint8_t *pcr_digest = NULL;
  48. char *log = NULL;
  49. char *pathlist = NULL;
  50. uint8_t data[EVENT_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  51. size_t signatureSize = 0;
  52. uint32_t pcrList[2] = { 11, 16 };
  53. r = Fapi_Provision(context, NULL, NULL, NULL);
  54. goto_if_error(r, "Error Fapi_Provision", error);
  55. r = Fapi_CreateKey(context, "HS/SRK/mySignKey", "sign,noDa", "", NULL);
  56. goto_if_error(r, "Error Fapi_CreateKey", error);
  57. r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", "-----BEGIN " \
  58. "CERTIFICATE-----[...]-----END CERTIFICATE-----");
  59. goto_if_error(r, "Error Fapi_SetCertificate", error);
  60. uint8_t qualifyingData[20] = {
  61. 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
  62. 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
  63. };
  64. r = pcr_reset(context, 16);
  65. goto_if_error(r, "Error pcr_reset", error);
  66. r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
  67. goto_if_error(r, "Error Fapi_PcrExtend", error);
  68. r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
  69. goto_if_error(r, "Error Fapi_PcrExtend", error);
  70. r = Fapi_PcrExtend(context, 16, data, EVENT_SIZE, "{ \"test\": \"myfile\" }");
  71. goto_if_error(r, "Error Fapi_PcrExtend", error);
  72. r = Fapi_Quote(context, pcrList, 2, "HS/SRK/mySignKey",
  73. "TPM-Quote",
  74. qualifyingData, 20,
  75. &quoteInfo,
  76. &signature, &signatureSize,
  77. &pcrEventLog, &certificate);
  78. goto_if_error(r, "Error Fapi_Quote", error);
  79. assert(quoteInfo != NULL);
  80. assert(signature != NULL);
  81. assert(pcrEventLog != NULL);
  82. assert(certificate != NULL);
  83. assert(strlen(quoteInfo) > ASSERT_SIZE);
  84. assert(strlen(pcrEventLog) > ASSERT_SIZE);
  85. assert(strlen(certificate) > ASSERT_SIZE);
  86. LOG_INFO("\npcrEventLog: %s\n", pcrEventLog);
  87. r = Fapi_VerifyQuote(context, "HS/SRK/mySignKey",
  88. qualifyingData, 20, quoteInfo,
  89. signature, signatureSize, pcrEventLog);
  90. goto_if_error(r, "Error Fapi_Verfiy_Quote", error);
  91. r = Fapi_List(context, "/", &pathlist);
  92. goto_if_error(r, "Pathlist", error);
  93. assert(pathlist != NULL);
  94. assert(strlen(pathlist) > ASSERT_SIZE);
  95. r = Fapi_Delete(context, "/");
  96. goto_if_error(r, "Error Fapi_Delete", error);
  97. SAFE_FREE(pubkey_pem);
  98. SAFE_FREE(signature);
  99. SAFE_FREE(quoteInfo);
  100. SAFE_FREE(pcrEventLog);
  101. SAFE_FREE(certificate);
  102. SAFE_FREE(export_data);
  103. SAFE_FREE(pcr_digest);
  104. SAFE_FREE(log);
  105. SAFE_FREE(pathlist);
  106. return EXIT_SUCCESS;
  107. error:
  108. Fapi_Delete(context, "/");
  109. SAFE_FREE(pubkey_pem);
  110. SAFE_FREE(signature);
  111. SAFE_FREE(quoteInfo);
  112. SAFE_FREE(pcrEventLog);
  113. SAFE_FREE(certificate);
  114. SAFE_FREE(export_data);
  115. SAFE_FREE(pcr_digest);
  116. SAFE_FREE(log);
  117. SAFE_FREE(pathlist);
  118. return EXIT_FAILURE;
  119. }
  120. int
  121. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  122. {
  123. return test_fapi_quote_destructive(fapi_context);
  124. }