esys-tr-getTpmHandle-nv.int.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "esys_iutil.h"
  12. #define LOGMODULE test
  13. #include "util/log.h"
  14. #include "util/aux_util.h"
  15. /** This tests the Esys_TR_ToTPMPublic function by
  16. * creating an NV index object and then attempting to retrieve
  17. * the TPM2_HANDLE for it and validating that the handle is correct for the
  18. * expected object type.
  19. *
  20. * Tested ESYS commands:
  21. * - Esys_NV_DefineSpace() (M)
  22. * - Esys_NV_UndefineSpace() (M)
  23. * - Esys_TR_ToTPMPublic() (M)
  24. *
  25. * @param[in,out] ectx The ESYS_CONTEXT.
  26. * @retval EXIT_FAILURE
  27. * @retval EXIT_SUCCESS
  28. */
  29. int
  30. test_esys_tr_toTpmPublic_nv(ESYS_CONTEXT * ectx)
  31. {
  32. int rc = EXIT_FAILURE;
  33. TSS2_RC r;
  34. ESYS_TR nvHandle = ESYS_TR_NONE;
  35. TPM2B_AUTH auth = {.size = 20,
  36. .buffer={10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  37. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}};
  38. TPM2B_NV_PUBLIC publicInfo = {
  39. .size = 0,
  40. .nvPublic = {
  41. .nvIndex =TPM2_NV_INDEX_FIRST,
  42. .nameAlg = TPM2_ALG_SHA256,
  43. .attributes = TPMA_NV_AUTHWRITE | TPMA_NV_AUTHREAD,
  44. .authPolicy = {
  45. .size = 0,
  46. .buffer = {},
  47. },
  48. .dataSize = 1,
  49. }
  50. };
  51. r = Esys_NV_DefineSpace(ectx, ESYS_TR_RH_OWNER,
  52. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  53. &auth, &publicInfo, &nvHandle);
  54. goto_if_error(r, "NV define space", out);
  55. /* the handle should be NV */
  56. TPM2_HANDLE tpmHandle = ESYS_TR_NONE;
  57. r = Esys_TR_GetTpmHandle(ectx, nvHandle, &tpmHandle);
  58. goto_if_error(r, "Esys_TR_ToTPMPublic", error);
  59. if (!(tpmHandle & TPM2_HR_NV_INDEX)) {
  60. LOG_ERROR("Retrieved handle should be NV, got: 0x%x", tpmHandle);
  61. goto error;
  62. }
  63. rc = EXIT_SUCCESS;
  64. error:
  65. r = Esys_NV_UndefineSpace(ectx, ESYS_TR_RH_OWNER, nvHandle,
  66. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE);
  67. if (r != TSS2_RC_SUCCESS) {
  68. LOG_ERROR("NV UndefineSpace");
  69. rc = EXIT_FAILURE;
  70. }
  71. out:
  72. return rc;
  73. }
  74. int
  75. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  76. return test_esys_tr_toTpmPublic_nv(esys_context);
  77. }