fapi-platform-certificates.int.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "tss2_esys.h"
  11. #include "tss2_fapi.h"
  12. #include "test-fapi.h"
  13. #include "fapi_util.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. #define MIN_PLATFORM_CERT_HANDLE 0x01C08000
  18. #define CERTIFICATE_SIZE 15
  19. /** Test the FAPI functions for platform certificates.
  20. *
  21. * Tested FAPI commands:
  22. * - Fapi_Provision()
  23. * - Fapi_GetPlatformCertificates()
  24. * - Fapi_Delete()
  25. *
  26. * @param[in,out] context The FAPI_CONTEXT.
  27. * @retval EXIT_FAILURE
  28. * @retval EXIT_SUCCESS
  29. */
  30. int
  31. test_fapi_platform_certificates(FAPI_CONTEXT *context)
  32. {
  33. TSS2_RC r;
  34. ESYS_TR nvHandle = ESYS_TR_NONE;
  35. uint8_t *certs = NULL;
  36. size_t certsSize = 0;
  37. /* In case NV was already defined, do not delete it in clean up */
  38. bool nv_already_defined = false;
  39. bool nv_newly_defined = false;
  40. size_t nv_size = CERTIFICATE_SIZE;
  41. r = Fapi_Provision(context, NULL, NULL, NULL);
  42. goto_if_error(r, "Error Fapi_Provision", error);
  43. TPM2_CAP capability = TPM2_CAP_HANDLES;
  44. INT32 property = 0x1000000;
  45. UINT32 propertyCount = 254;
  46. TPMI_YES_NO moreDataAvailable;
  47. TPMS_CAPABILITY_DATA *capabilityData;
  48. capabilityData = NULL;
  49. r = Esys_GetCapability(context->esys,
  50. ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
  51. capability, property,
  52. propertyCount,
  53. &moreDataAvailable,
  54. &capabilityData);
  55. goto_if_error(r, "Error Esys_GetCapability", error);
  56. int count = capabilityData->data.handles.count;
  57. for(int i = 0; i < count; i++){
  58. if(capabilityData->data.handles.handle[i] == MIN_PLATFORM_CERT_HANDLE){
  59. nv_already_defined = true;
  60. break;
  61. }
  62. }
  63. SAFE_FREE(capabilityData);
  64. if(nv_already_defined){
  65. TPM2B_NV_PUBLIC *nvPublic = NULL;
  66. TPM2B_NAME *nvName = NULL;
  67. r = Esys_NV_ReadPublic(context->esys,
  68. nvHandle,
  69. ESYS_TR_NONE,
  70. ESYS_TR_NONE,
  71. ESYS_TR_NONE,
  72. &nvPublic,
  73. &nvName);
  74. goto_if_error(r, "Error: nv read public", error);
  75. LOG_INFO("nvPublic Size %d\n", nvPublic->nvPublic.dataSize);
  76. nv_size = nvPublic->nvPublic.dataSize;
  77. LOG_INFO("NV size: %zu", nv_size);
  78. }
  79. if(!nv_already_defined){
  80. TPM2B_AUTH auth = { 0 };
  81. TPM2B_NV_PUBLIC publicInfo = {
  82. .nvPublic = {
  83. .nameAlg = TPM2_ALG_SHA256,
  84. .attributes = TPMA_NV_PPWRITE | TPMA_NV_AUTHREAD |
  85. TPMA_NV_OWNERREAD | TPMA_NV_PLATFORMCREATE | TPMA_NV_NO_DA,
  86. .dataSize = CERTIFICATE_SIZE,
  87. .nvIndex = MIN_PLATFORM_CERT_HANDLE,
  88. },
  89. };
  90. r = Esys_NV_DefineSpace(context->esys,
  91. ESYS_TR_RH_PLATFORM,
  92. ESYS_TR_PASSWORD,
  93. ESYS_TR_NONE,
  94. ESYS_TR_NONE,
  95. &auth,
  96. &publicInfo,
  97. &nvHandle);
  98. if (number_rc(r) == TPM2_RC_BAD_AUTH ||
  99. number_rc(r) == TPM2_RC_HIERARCHY) {
  100. /* Platform authorization not possible test will be skipped */
  101. LOG_WARNING("Platform authorization not possible.");
  102. goto skip;
  103. }
  104. goto_if_error(r, "Error Esys_NV_DefineSpace", error);
  105. nv_newly_defined = true;
  106. TPM2B_MAX_NV_BUFFER nv_test_data = { .size = CERTIFICATE_SIZE,
  107. .buffer={0x61, 0x61, 0x61, 0x61, 0x61,
  108. 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
  109. 0x61, 0x61, 0x61, 0x61}};
  110. r = Esys_NV_Write(context->esys,
  111. ESYS_TR_RH_PLATFORM,
  112. nvHandle,
  113. ESYS_TR_PASSWORD,
  114. ESYS_TR_NONE,
  115. ESYS_TR_NONE,
  116. &nv_test_data,
  117. 0);
  118. goto_if_error(r, "Error Esys_NV_Write", error);
  119. }
  120. r = Fapi_GetPlatformCertificates(context, &certs, &certsSize);
  121. if (r == TSS2_FAPI_RC_NO_CERT)
  122. goto skip;
  123. goto_if_error(r, "Error Fapi_GetPlatformCertificates", error);
  124. ASSERT(certs != NULL);
  125. ASSERT(certsSize == nv_size);
  126. Fapi_Free(certs);
  127. if(nv_newly_defined){
  128. r = Esys_NV_UndefineSpace(context->esys,
  129. ESYS_TR_RH_PLATFORM,
  130. nvHandle,
  131. ESYS_TR_PASSWORD,
  132. ESYS_TR_NONE,
  133. ESYS_TR_NONE
  134. );
  135. goto_if_error(r, "Error: NV_UndefineSpace", error);
  136. }
  137. /* Cleanup */
  138. r = Fapi_Delete(context, "/");
  139. goto_if_error(r, "Error Fapi_Delete", error);
  140. return EXIT_SUCCESS;
  141. error:
  142. if(nv_newly_defined){
  143. Esys_NV_UndefineSpace(context->esys,
  144. ESYS_TR_RH_PLATFORM, nvHandle,
  145. ESYS_TR_PASSWORD, ESYS_TR_NONE,
  146. ESYS_TR_NONE);
  147. }
  148. Fapi_Delete(context, "/");
  149. return EXIT_FAILURE;
  150. skip:
  151. if(nv_newly_defined){
  152. Esys_NV_UndefineSpace(context->esys,
  153. ESYS_TR_RH_PLATFORM, nvHandle,
  154. ESYS_TR_PASSWORD, ESYS_TR_NONE,
  155. ESYS_TR_NONE);
  156. }
  157. Fapi_Delete(context, "/");
  158. return EXIT_SKIP;
  159. }
  160. int
  161. test_invoke_fapi(FAPI_CONTEXT *fapi_context)
  162. {
  163. return test_fapi_platform_certificates(fapi_context);
  164. }