esys-certify-creation.int.c 5.0 KB

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