fapi-quote-destructive.int.c 4.2 KB

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