esys-certifyX509.int.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /*******************************************************************************
  3. * Copyright (c) 2020, Intel Corporation
  4. * Copyright 2020, Fraunhofer SIT sponsored by Infineon Technologies AG
  5. * All rights reserved.
  6. *******************************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <stdlib.h>
  11. #include "tss2_esys.h"
  12. #include "esys_iutil.h"
  13. #include "test-esys.h"
  14. #define LOGMODULE test
  15. #include "util/log.h"
  16. #include "util/aux_util.h"
  17. /** This test is intended to test the command Esys_CertifyX509.
  18. *
  19. * We create a RSA primary signing key which will be used as signing key
  20. * and as object for the certify command.
  21. *
  22. * Tested ESYS commands:
  23. * - Esys_CertifyX509() (M)
  24. * - Esys_CreatePrimary() (M)
  25. * - Esys_FlushContext() (M)
  26. *
  27. * @param[in,out] esys_contextx509 The ESYS_CONTEXT.
  28. * @retval EXIT_FAILURE
  29. * @retval EXIT_SKIP
  30. * @retval EXIT_SUCCESS
  31. */
  32. int
  33. test_esys_certifyx509(ESYS_CONTEXT * esys_context)
  34. {
  35. TSS2_RC r;
  36. ESYS_TR signHandle = ESYS_TR_NONE;
  37. TPM2B_PUBLIC *outPublic = NULL;
  38. TPM2B_CREATION_DATA *creationData = NULL;
  39. TPM2B_DIGEST *creationHash = NULL;
  40. TPMT_TK_CREATION *creationTicket = NULL;
  41. TPMT_SIGNATURE *signature = NULL;
  42. TPM2B_MAX_BUFFER *addedToCertificate = NULL;
  43. TPM2B_DIGEST *tbsDigest = NULL;
  44. TPM2B_DATA reserved = {0};
  45. TPM2B_AUTH authValuePrimary = {
  46. .size = 5,
  47. .buffer = {1, 2, 3, 4, 5}
  48. };
  49. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  50. .size = 0,
  51. .sensitive = {
  52. .userAuth = {
  53. .size = 0,
  54. .buffer = {0},
  55. },
  56. .data = {
  57. .size = 0,
  58. .buffer = {0},
  59. },
  60. },
  61. };
  62. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  63. TPM2B_PUBLIC inPublic = {
  64. .size = 0,
  65. .publicArea = {
  66. .type = TPM2_ALG_RSA,
  67. .nameAlg = TPM2_ALG_SHA256,
  68. .objectAttributes = (
  69. TPMA_OBJECT_FIXEDTPM |
  70. TPMA_OBJECT_USERWITHAUTH |
  71. TPMA_OBJECT_RESTRICTED |
  72. TPMA_OBJECT_SIGN_ENCRYPT |
  73. #if defined INTEGRATION_TCTI_MSSIM
  74. TPMA_OBJECT_X509SIGN | /* This requires mssim >= v1628 Other simulators
  75. that do not implement 1.59 spec return 0x000002e1
  76. tpm:parameter(2):reserved bits not set to zero as required
  77. */
  78. #endif
  79. TPMA_OBJECT_FIXEDTPM |
  80. TPMA_OBJECT_FIXEDPARENT |
  81. TPMA_OBJECT_SENSITIVEDATAORIGIN),
  82. .authPolicy = {
  83. .size = 0,
  84. .buffer = {0},
  85. },
  86. .parameters.rsaDetail = {
  87. .symmetric = {
  88. .algorithm = TPM2_ALG_NULL,
  89. .keyBits.aes = 0,
  90. .mode.aes = TPM2_ALG_NULL,
  91. },
  92. .scheme = {
  93. .scheme = TPM2_ALG_RSASSA,
  94. .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA256 }},
  95. },
  96. .keyBits = 2048,
  97. .exponent = 0,
  98. },
  99. .unique.rsa = {
  100. .size = 0,
  101. .buffer = {0},
  102. },
  103. },
  104. };
  105. TPM2B_AUTH authValue = {
  106. .size = 0,
  107. .buffer = {0}
  108. };
  109. TPM2B_DATA outsideInfo = {
  110. .size = 0,
  111. .buffer = {0},
  112. };
  113. TPML_PCR_SELECTION creationPCR = {
  114. .count = 0,
  115. };
  116. TPMT_SIG_SCHEME inScheme = {
  117. .scheme = TPM2_ALG_RSASSA,
  118. .details.rsassa = TPM2_ALG_SHA256
  119. };
  120. LOG_INFO("\nRSA key will be created.");
  121. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  122. goto_if_error(r, "Error: TR_SetAuth", error);
  123. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  124. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  125. &inPublic, &outsideInfo, &creationPCR,
  126. &signHandle, &outPublic, &creationData,
  127. &creationHash, &creationTicket);
  128. goto_if_error(r, "Error esys create primary", error);
  129. /* For the partial cert we need these fields:
  130. * 1) Signature Algorithm Identifier (optional)
  131. 2) Issuer (mandatory)
  132. 3) Validity (mandatory)
  133. 4) Subject Name (mandatory)
  134. 5) Extensions (mandatory only KeyUsage, can take tcg-tpmaObject, but it's optional)
  135. Cert data generated with tpm2-certifyX509_gen_partial_cert util from the tpm2-tools project.
  136. */
  137. TPM2B_MAX_BUFFER partialCertificate = {
  138. .size = 199,
  139. .buffer = { 0x30, 0x81, 0xc4, 0x30, 0x4d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  140. 0x55, 0x53, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x54, 0x53, 0x53,
  141. 0x20, 0x54, 0x45, 0x53, 0x54, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x07,
  142. 0x52, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03,
  143. 0x0c, 0x10, 0x54, 0x53, 0x53, 0x20, 0x54, 0x45, 0x53, 0x54, 0x20, 0x52, 0x6f, 0x6f, 0x74, 0x20,
  144. 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x37, 0x32, 0x38, 0x32, 0x31, 0x30, 0x33,
  145. 0x32, 0x32, 0x5a, 0x17, 0x0d, 0x33, 0x30, 0x30, 0x34, 0x32, 0x37, 0x32, 0x31, 0x30, 0x33, 0x32,
  146. 0x32, 0x5a, 0x30, 0x38, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  147. 0x53, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x54, 0x53, 0x53, 0x20,
  148. 0x54, 0x45, 0x53, 0x54, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0d, 0x54,
  149. 0x53, 0x53, 0x20, 0x54, 0x45, 0x53, 0x54, 0x20, 0x68, 0x6f, 0x73, 0x74, 0x30, 0x05, 0x30, 0x00,
  150. 0x03, 0x01, 0x00, 0xa3, 0x12, 0x30, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x01, 0x01,
  151. 0xff, 0x04, 0x04, 0x03, 0x02, 0x01, 0x86 }
  152. };
  153. r = Esys_CertifyX509 (
  154. esys_context,
  155. signHandle,
  156. signHandle,
  157. ESYS_TR_PASSWORD,
  158. ESYS_TR_PASSWORD,
  159. ESYS_TR_NONE,
  160. &reserved,
  161. &inScheme,
  162. &partialCertificate,
  163. &addedToCertificate,
  164. &tbsDigest,
  165. &signature);
  166. goto_if_error(r, "Error: CertifyX509", error);
  167. r = Esys_FlushContext(esys_context,signHandle);
  168. goto_if_error(r, "Error: FlushContext", error);
  169. Esys_Free(outPublic);
  170. Esys_Free(creationData);
  171. Esys_Free(creationHash);
  172. Esys_Free(creationTicket);
  173. Esys_Free(signature);
  174. Esys_Free(addedToCertificate);
  175. Esys_Free(tbsDigest);
  176. return EXIT_SUCCESS;
  177. error:
  178. if (signHandle != ESYS_TR_NONE) {
  179. if (Esys_FlushContext(esys_context, signHandle) != TSS2_RC_SUCCESS) {
  180. LOG_ERROR("Cleanup signHandle failed.");
  181. }
  182. }
  183. if (outPublic)
  184. Esys_Free(outPublic);
  185. if (creationData)
  186. Esys_Free(creationData);
  187. if (creationHash)
  188. Esys_Free(creationHash);
  189. if (creationTicket)
  190. Esys_Free(creationTicket);
  191. if (signature)
  192. Esys_Free(signature);
  193. if (addedToCertificate)
  194. Esys_Free(addedToCertificate);
  195. if (tbsDigest)
  196. Esys_Free(tbsDigest);
  197. /* If the TPM doesn't support it return skip */
  198. if (r == TPM2_RC_COMMAND_CODE)
  199. return EXIT_SKIP;
  200. else
  201. return EXIT_FAILURE;
  202. }
  203. int
  204. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  205. return test_esys_certifyx509(esys_context);
  206. }