esys-nv-certify.int.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include "test-esys.h"
  13. #define LOGMODULE test
  14. #include "util/log.h"
  15. #include "util/aux_util.h"
  16. /** This test is intended to test the command Esys_NV_Certify.
  17. *
  18. * We create a RSA primary signing key which will be used as signing key
  19. * for the NV data.
  20. *
  21. * Tested ESYS commands:
  22. * - Esys_CreatePrimary() (M)
  23. * - Esys_FlushContext() (M)
  24. * - Esys_NV_Certify() (O)
  25. * - Esys_NV_DefineSpace() (M)
  26. * - Esys_NV_UndefineSpace() (M)
  27. * - Esys_NV_Write() (M)
  28. *
  29. * @param[in,out] esys_context The ESYS_CONTEXT.
  30. * @retval EXIT_FAILURE
  31. * @retval EXIT_SKIP
  32. * @retval EXIT_SUCCESS
  33. */
  34. int
  35. test_esys_nv_certify(ESYS_CONTEXT * esys_context)
  36. {
  37. TSS2_RC r;
  38. ESYS_TR signHandle = ESYS_TR_NONE;
  39. ESYS_TR nvHandle = ESYS_TR_NONE;
  40. int failure_return = EXIT_FAILURE;
  41. TPM2B_PUBLIC *outPublic = NULL;
  42. TPM2B_CREATION_DATA *creationData = NULL;
  43. TPM2B_DIGEST *creationHash = NULL;
  44. TPMT_TK_CREATION *creationTicket = NULL;
  45. TPM2B_ATTEST *certifyInfo = NULL;
  46. TPMT_SIGNATURE *signature = NULL;
  47. TPM2B_AUTH authValuePrimary = {
  48. .size = 5,
  49. .buffer = {1, 2, 3, 4, 5}
  50. };
  51. TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
  52. .size = 0,
  53. .sensitive = {
  54. .userAuth = {
  55. .size = 0,
  56. .buffer = {0},
  57. },
  58. .data = {
  59. .size = 0,
  60. .buffer = {0},
  61. },
  62. },
  63. };
  64. inSensitivePrimary.sensitive.userAuth = authValuePrimary;
  65. TPM2B_PUBLIC inPublic = {
  66. .size = 0,
  67. .publicArea = {
  68. .type = TPM2_ALG_RSA,
  69. .nameAlg = TPM2_ALG_SHA256,
  70. .objectAttributes = (
  71. TPMA_OBJECT_USERWITHAUTH |
  72. TPMA_OBJECT_RESTRICTED |
  73. TPMA_OBJECT_SIGN_ENCRYPT |
  74. TPMA_OBJECT_FIXEDTPM |
  75. TPMA_OBJECT_FIXEDPARENT |
  76. TPMA_OBJECT_SENSITIVEDATAORIGIN
  77. ),
  78. .authPolicy = {
  79. .size = 0,
  80. },
  81. .parameters.rsaDetail = {
  82. .symmetric = {
  83. .algorithm = TPM2_ALG_NULL,
  84. .keyBits.aes = 128,
  85. .mode.aes = TPM2_ALG_CFB,
  86. },
  87. .scheme = {
  88. .scheme = TPM2_ALG_RSASSA,
  89. .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA256 }},
  90. },
  91. .keyBits = 2048,
  92. .exponent = 0,
  93. },
  94. .unique.rsa = {
  95. .size = 0,
  96. .buffer = {},
  97. },
  98. },
  99. };
  100. TPM2B_AUTH authValue = {
  101. .size = 0,
  102. .buffer = {}
  103. };
  104. TPM2B_DATA outsideInfo = {
  105. .size = 0,
  106. .buffer = {},
  107. };
  108. TPML_PCR_SELECTION creationPCR = {
  109. .count = 0,
  110. };
  111. LOG_INFO("\nRSA key will be created.");
  112. r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
  113. goto_if_error(r, "Error: TR_SetAuth", error);
  114. r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
  115. ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
  116. &inPublic, &outsideInfo, &creationPCR,
  117. &signHandle, &outPublic, &creationData,
  118. &creationHash, &creationTicket);
  119. goto_if_error(r, "Error esys create primary", error);
  120. TPM2B_AUTH auth = {.size = 20,
  121. .buffer={10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  122. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}};
  123. TPM2B_NV_PUBLIC publicInfo = {
  124. .size = 0,
  125. .nvPublic = {
  126. .nvIndex =TPM2_NV_INDEX_FIRST,
  127. .nameAlg = TPM2_ALG_SHA256,
  128. .attributes = (
  129. TPMA_NV_OWNERWRITE |
  130. TPMA_NV_AUTHWRITE |
  131. TPMA_NV_WRITE_STCLEAR |
  132. TPMA_NV_READ_STCLEAR |
  133. TPMA_NV_AUTHREAD |
  134. TPMA_NV_OWNERREAD
  135. ),
  136. .authPolicy = {
  137. .size = 0,
  138. .buffer = {},
  139. },
  140. .dataSize = 32,
  141. }
  142. };
  143. r = Esys_NV_DefineSpace(esys_context,
  144. ESYS_TR_RH_OWNER,
  145. ESYS_TR_PASSWORD,
  146. ESYS_TR_NONE,
  147. ESYS_TR_NONE,
  148. &auth,
  149. &publicInfo,
  150. &nvHandle);
  151. goto_if_error(r, "Error esys define nv space", error);
  152. UINT16 offset = 0;
  153. TPM2B_MAX_NV_BUFFER nv_test_data = { .size = 20,
  154. .buffer={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
  155. 1, 2, 3, 4, 5, 6, 7, 8, 9}};
  156. r = Esys_NV_Write(esys_context,
  157. nvHandle,
  158. nvHandle,
  159. ESYS_TR_PASSWORD,
  160. ESYS_TR_NONE,
  161. ESYS_TR_NONE,
  162. &nv_test_data,
  163. offset);
  164. goto_if_error(r, "Error esys nv write", error);
  165. TPM2B_DATA qualifyingData = {0};
  166. TPMT_SIG_SCHEME inScheme = { .scheme = TPM2_ALG_NULL };
  167. r = Esys_NV_Certify(
  168. esys_context,
  169. signHandle,
  170. ESYS_TR_RH_OWNER,
  171. nvHandle,
  172. ESYS_TR_PASSWORD,
  173. ESYS_TR_PASSWORD,
  174. ESYS_TR_NONE,
  175. &qualifyingData,
  176. &inScheme,
  177. 20,
  178. 0,
  179. &certifyInfo,
  180. &signature);
  181. if ((r == TPM2_RC_COMMAND_CODE) ||
  182. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
  183. (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
  184. LOG_WARNING("Command TPM2_NV_Certify not supported by TPM.");
  185. failure_return = EXIT_SKIP;
  186. goto error;
  187. }
  188. goto_if_error(r, "Error: NV_Certify", error);
  189. r = Esys_NV_UndefineSpace(esys_context,
  190. ESYS_TR_RH_OWNER,
  191. nvHandle,
  192. ESYS_TR_PASSWORD,
  193. ESYS_TR_NONE,
  194. ESYS_TR_NONE
  195. );
  196. goto_if_error(r, "Error: NV_UndefineSpace", error);
  197. r = Esys_FlushContext(esys_context,signHandle);
  198. goto_if_error(r, "Error: FlushContext", error);
  199. Esys_Free(outPublic);
  200. Esys_Free(creationData);
  201. Esys_Free(creationHash);
  202. Esys_Free(creationTicket);
  203. Esys_Free(certifyInfo);
  204. Esys_Free(signature);
  205. return EXIT_SUCCESS;
  206. error:
  207. if (signHandle != ESYS_TR_NONE) {
  208. if (Esys_FlushContext(esys_context, signHandle) != TSS2_RC_SUCCESS) {
  209. LOG_ERROR("Cleanup signHandle failed.");
  210. }
  211. }
  212. if (nvHandle != ESYS_TR_NONE) {
  213. if (Esys_NV_UndefineSpace(esys_context,
  214. ESYS_TR_RH_OWNER,
  215. nvHandle,
  216. ESYS_TR_PASSWORD,
  217. ESYS_TR_NONE,
  218. ESYS_TR_NONE) != TSS2_RC_SUCCESS) {
  219. LOG_ERROR("Cleanup nvHandle failed.");
  220. }
  221. }
  222. Esys_Free(outPublic);
  223. Esys_Free(creationData);
  224. Esys_Free(creationHash);
  225. Esys_Free(creationTicket);
  226. Esys_Free(certifyInfo);
  227. Esys_Free(signature);
  228. return failure_return;
  229. }
  230. int
  231. test_invoke_esys(ESYS_CONTEXT * esys_context) {
  232. return test_esys_nv_certify(esys_context);
  233. }