esys-tr-getTpmHandle-key.int.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. #ifdef HAVE_CONFIG_H
  3. #include <config.h>
  4. #endif
  5. #include <stdlib.h>
  6. #include "tss2_esys.h"
  7. #include "esys_iutil.h"
  8. #define LOGMODULE test
  9. #include "util/log.h"
  10. #include "util/aux_util.h"
  11. /** This tests the Esys_TR_ToTPMPublic function by
  12. * creating a Primary Object Key and then attempting to retrieve
  13. * the TPM2_HANDLE for it and validating that the handle is correct for the
  14. * expected object type.
  15. *
  16. * Tested ESYS commands:
  17. * - Esys_CreatePrimary() (M)
  18. * - Esys_EvictControl() (M)
  19. * - Esys_FlushContext() (M)
  20. * - Esys_TR_ToTPMPublic() (M)
  21. *
  22. * @param[in,out] ectx The ESYS_CONTEXT.
  23. * @retval EXIT_FAILURE
  24. * @retval EXIT_SUCCESS
  25. */
  26. int
  27. test_esys_tr_toTpmPublic_key(ESYS_CONTEXT * ectx)
  28. {
  29. int rc = EXIT_FAILURE;
  30. TSS2_RC r;
  31. ESYS_TR primaryHandle = ESYS_TR_NONE;
  32. ESYS_TR keyHandle = ESYS_TR_NONE;
  33. TPM2B_AUTH authValuePrimary = {
  34. .size = 5,
  35. .buffer = {1, 2, 3, 4, 5}
  36. };
  37. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  38. .size = 0,
  39. .sensitive = {
  40. .userAuth = {
  41. .size = 0,
  42. .buffer = {0 },
  43. },
  44. .data = {
  45. .size = 0,
  46. .buffer = {0},
  47. },
  48. },
  49. };
  50. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  51. TPM2B_PUBLIC inPublic = {
  52. .size = 0,
  53. .publicArea = {
  54. .type = TPM2_ALG_RSA,
  55. .nameAlg = TPM2_ALG_SHA256,
  56. .objectAttributes = (TPMA_OBJECT_USERWITHAUTH |
  57. TPMA_OBJECT_RESTRICTED |
  58. TPMA_OBJECT_DECRYPT |
  59. TPMA_OBJECT_FIXEDTPM |
  60. TPMA_OBJECT_FIXEDPARENT |
  61. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  62. .authPolicy = {
  63. .size = 0,
  64. },
  65. .parameters.rsaDetail = {
  66. .symmetric = {
  67. .algorithm = TPM2_ALG_AES,
  68. .keyBits.aes = 128,
  69. .mode.aes = TPM2_ALG_CFB},
  70. .scheme = {
  71. .scheme = TPM2_ALG_NULL
  72. },
  73. .keyBits = 2048,
  74. .exponent = 0,
  75. },
  76. .unique.rsa = {
  77. .size = 0,
  78. .buffer = {},
  79. },
  80. },
  81. };
  82. LOG_INFO("\nRSA key will be created.");
  83. TPM2B_DATA outsideInfo = {
  84. .size = 0,
  85. .buffer = {},
  86. };
  87. TPML_PCR_SELECTION creationPCR = {
  88. .count = 0,
  89. };
  90. /* create a key */
  91. r = Esys_CreatePrimary(ectx, ESYS_TR_RH_OWNER,
  92. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  93. &inSensitivePrimary, &inPublic, &outsideInfo,
  94. &creationPCR,
  95. &primaryHandle, NULL, NULL, NULL, NULL);
  96. goto_if_error(r, "Create primary", out);
  97. /* the handle should be transient */
  98. TPM2_HANDLE tpmHandle = ESYS_TR_NONE;
  99. r = Esys_TR_GetTpmHandle(ectx, primaryHandle, &tpmHandle);
  100. goto_if_error(r, "Esys_TR_ToTPMPublic", error);
  101. if (!(tpmHandle & TPM2_HR_TRANSIENT)) {
  102. LOG_ERROR("Retrieved handle should be transient, got: 0x%x", tpmHandle);
  103. goto error;
  104. }
  105. /* make it persistent */
  106. r = Esys_EvictControl(ectx, ESYS_TR_RH_OWNER, primaryHandle,
  107. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  108. TPM2_PERSISTENT_FIRST, &keyHandle);
  109. goto_if_error(r, "EvictControl make persistent", error);
  110. /* handle should be persistent */
  111. r = Esys_TR_GetTpmHandle(ectx, keyHandle, &tpmHandle);
  112. goto_if_error(r, "Esys_TR_ToTPMPublic", error);
  113. if (!(tpmHandle & TPM2_HR_PERSISTENT)) {
  114. LOG_ERROR("Retrieved handle should be transient, got: 0x%x", tpmHandle);
  115. goto error;
  116. }
  117. rc = EXIT_SUCCESS;
  118. error:
  119. r = Esys_FlushContext(ectx, primaryHandle);
  120. if (r != TSS2_RC_SUCCESS) {
  121. rc = EXIT_FAILURE;
  122. LOG_ERROR("TR close on key object");
  123. }
  124. r = Esys_EvictControl(ectx, ESYS_TR_RH_OWNER, keyHandle,
  125. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  126. TPM2_PERSISTENT_FIRST, &keyHandle);
  127. if (r != TSS2_RC_SUCCESS) {
  128. rc = EXIT_FAILURE;
  129. LOG_ERROR("Esys_EvictControl");
  130. }
  131. out:
  132. return rc;
  133. }
  134. int
  135. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  136. return test_esys_tr_toTpmPublic_key(esys_context);
  137. }