esys-quote.int.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 test is intended to test the quote command with password
  16. * authentication.
  17. *
  18. * We create a RSA primary signing key which will be used
  19. * for signing.
  20. *
  21. * Tested ESYS commands:
  22. * - Esys_CreatePrimary() (M)
  23. * - Esys_FlushContext() (M)
  24. * - Esys_Quote() (M)
  25. *
  26. * @param[in,out] esys_context The ESYS_CONTEXT.
  27. * @retval EXIT_FAILURE
  28. * @retval EXIT_SUCCESS
  29. */
  30. int
  31. test_esys_quote(ESYS_CONTEXT * esys_context)
  32. {
  33. TSS2_RC r;
  34. ESYS_TR primaryHandle = ESYS_TR_NONE;
  35. TPM2B_PUBLIC *outPublic = NULL;
  36. TPM2B_CREATION_DATA *creationData = NULL;
  37. TPM2B_DIGEST *creationHash = NULL;
  38. TPMT_TK_CREATION *creationTicket = NULL;
  39. TPM2B_ATTEST *attest = NULL;
  40. TPMT_SIGNATURE *signature = NULL;
  41. TPM2B_AUTH authValuePrimary = {
  42. .size = 5,
  43. .buffer = {1, 2, 3, 4, 5}
  44. };
  45. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  46. .size = 0,
  47. .sensitive = {
  48. .userAuth = {
  49. .size = 0,
  50. .buffer = {0},
  51. },
  52. .data = {
  53. .size = 0,
  54. .buffer = {0},
  55. },
  56. },
  57. };
  58. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  59. TPM2B_PUBLIC inPublic = {
  60. .size = 0,
  61. .publicArea = {
  62. .type = TPM2_ALG_RSA,
  63. .nameAlg = TPM2_ALG_SHA256,
  64. .objectAttributes = (
  65. TPMA_OBJECT_USERWITHAUTH |
  66. TPMA_OBJECT_RESTRICTED |
  67. TPMA_OBJECT_SIGN_ENCRYPT |
  68. TPMA_OBJECT_FIXEDTPM |
  69. TPMA_OBJECT_FIXEDPARENT |
  70. TPMA_OBJECT_SENSITIVEDATAORIGIN
  71. ),
  72. .authPolicy = {
  73. .size = 0,
  74. },
  75. .parameters.rsaDetail = {
  76. .symmetric = {
  77. .algorithm = TPM2_ALG_NULL,
  78. .keyBits.aes = 128,
  79. .mode.aes = TPM2_ALG_CFB,
  80. },
  81. .scheme = {
  82. .scheme = TPM2_ALG_RSASSA,
  83. .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA256 }},
  84. },
  85. .keyBits = 2048,
  86. .exponent = 0,
  87. },
  88. .unique.rsa = {
  89. .size = 0,
  90. .buffer = {},
  91. },
  92. },
  93. };
  94. TPM2B_AUTH authValue = {
  95. .size = 0,
  96. .buffer = {}
  97. };
  98. TPM2B_DATA outsideInfo = {
  99. .size = 0,
  100. .buffer = {},
  101. };
  102. TPML_PCR_SELECTION creationPCR = {
  103. .count = 0,
  104. };
  105. LOG_INFO("\nRSA key will be created.");
  106. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  107. goto_if_error(r, "Error: TR_SetAuth", error);
  108. RSRC_NODE_T *primaryHandle_node;
  109. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  110. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  111. &inPublic, &outsideInfo, &creationPCR,
  112. &primaryHandle, &outPublic, &creationData,
  113. &creationHash, &creationTicket);
  114. goto_if_error(r, "Error esys create primary", error);
  115. r = esys_GetResourceObject(esys_context, primaryHandle,
  116. &primaryHandle_node);
  117. goto_if_error(r, "Error Esys GetResourceObject", error);
  118. LOG_INFO("Created Primary with handle 0x%08x...",
  119. primaryHandle_node->rsrc.handle);
  120. r = Esys_TR_SetAuth(esys_context, primaryHandle,
  121. &authValuePrimary);
  122. goto_if_error(r, "Error: TR_SetAuth", error);
  123. TPM2B_DATA qualifyingData = { .size = 0, .buffer = {}};
  124. TPMT_SIG_SCHEME sig_scheme = { .scheme = TPM2_ALG_NULL };
  125. TPML_PCR_SELECTION pcr_selection = {
  126. .count = 2,
  127. .pcrSelections = {
  128. {
  129. .hash = TPM2_ALG_SHA256,
  130. .sizeofSelect = 3,
  131. .pcrSelect = { 0,4,0 } },
  132. {
  133. .hash = TPM2_ALG_SHA256,
  134. .sizeofSelect = 3,
  135. .pcrSelect = { 0,4,0 } },
  136. }};
  137. r = Esys_Quote(esys_context, primaryHandle,
  138. ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
  139. &qualifyingData, &sig_scheme, &pcr_selection,
  140. &attest, &signature);
  141. goto_if_error(r, "Error Esys Quote", error);
  142. r = Esys_FlushContext(esys_context, primaryHandle);
  143. goto_if_error(r, "Error: FlushContext", error);
  144. Esys_Free(outPublic);
  145. Esys_Free(creationData);
  146. Esys_Free(creationHash);
  147. Esys_Free(creationTicket);
  148. Esys_Free(attest);
  149. Esys_Free(signature);
  150. return EXIT_SUCCESS;
  151. error:
  152. if (primaryHandle != ESYS_TR_NONE) {
  153. if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
  154. LOG_ERROR("Cleanup primaryHandle failed.");
  155. }
  156. }
  157. return EXIT_FAILURE;
  158. }
  159. int
  160. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  161. return test_esys_quote(esys_context);
  162. }