sys-policy-authorizeNV.int.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. /***********************************************************************
  3. * Copyright (c) 2017-2018, Intel Corporation
  4. *
  5. * All rights reserved.
  6. ***********************************************************************/
  7. #ifdef HAVE_CONFIG_H
  8. #include <config.h>
  9. #endif
  10. #include <inttypes.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <openssl/rand.h>
  15. #include "tss2_mu.h"
  16. #include "tss2_sys.h"
  17. #define LOGMODULE test
  18. #include "util/log.h"
  19. #include "test-esys.h"
  20. #include "test.h"
  21. #include "sys-util.h"
  22. int
  23. test_invoke (TSS2_SYS_CONTEXT *sys_context)
  24. {
  25. TSS2_RC rc;
  26. TPM2B_ENCRYPTED_SECRET encrypted_salt = { 0 };
  27. TPMI_SH_AUTH_SESSION session_handle = 0;
  28. TPMT_SYM_DEF symmetric = { .algorithm = TPM2_ALG_NULL };
  29. TSS2L_SYS_AUTH_COMMAND cmd_auth = {
  30. .count = 1,
  31. .auths = {{
  32. .sessionHandle = TPM2_RH_PW,
  33. }},
  34. };
  35. TSS2L_SYS_AUTH_RESPONSE rsp_auth = { 0 };
  36. TPM2B_NONCE nonce_caller = {
  37. .size = TPM2_SHA256_DIGEST_SIZE,
  38. .buffer = { 0 }
  39. };
  40. TPM2B_NONCE nonce_tpm = {
  41. .size = TPM2_SHA256_DIGEST_SIZE,
  42. .buffer = { 0 }
  43. };
  44. /* First start a policy session */
  45. LOG_INFO("Calling StartAuthSession policy session");
  46. rc = Tss2_Sys_StartAuthSession (sys_context,
  47. TPM2_RH_NULL, /* tpmKey */
  48. TPM2_RH_NULL, /* bind */
  49. NULL, /* cmdAuthsArray */
  50. &nonce_caller, /* nonceCaller */
  51. &encrypted_salt, /* encryptedSalt */
  52. TPM2_SE_POLICY, /* sessionType */
  53. &symmetric, /* symmetric */
  54. TPM2_ALG_SHA256, /* authHash */
  55. &session_handle, /* sessionHandle */
  56. &nonce_tpm, /* nonceTPM */
  57. NULL /* rspAuthsArray */
  58. );
  59. if (rc != TSS2_RC_SUCCESS) {
  60. LOG_ERROR("StartAuthSession failed: 0x%" PRIx32, rc);
  61. exit(1);
  62. }
  63. LOG_INFO("StartAuthSession TPM2_SE_POLICY success! Session handle: "
  64. "0x%" PRIx32, session_handle);
  65. /*
  66. * Then create an NV space where the digest required for PolicyAuthorizeNV
  67. * will be stored.
  68. */
  69. TPM2B_NV_PUBLIC nv_public = { 0 };
  70. TPM2B_AUTH nv_auth = {
  71. .size = TPM2_SHA256_DIGEST_SIZE,
  72. };
  73. TPMI_RH_NV_INDEX nv_index = TPM2_HR_NV_INDEX | 0x01;
  74. nv_public.nvPublic.nvIndex = nv_index;
  75. nv_public.nvPublic.nameAlg = TPM2_ALG_SHA256;
  76. nv_public.nvPublic.attributes = TPMA_NV_PPREAD;
  77. nv_public.nvPublic.attributes |= TPMA_NV_PPWRITE;
  78. nv_public.nvPublic.attributes |= TPMA_NV_WRITE_STCLEAR;
  79. nv_public.nvPublic.attributes |= TPMA_NV_ORDERLY;
  80. nv_public.nvPublic.attributes |= TPMA_NV_OWNERREAD;
  81. nv_public.nvPublic.attributes |= TPMA_NV_OWNERWRITE;
  82. nv_public.nvPublic.authPolicy.size = 0;
  83. nv_public.nvPublic.dataSize = sizeof(TPMT_HA);
  84. cmd_auth.count = 1;
  85. cmd_auth.auths[0].sessionHandle = TPM2_RH_PW;
  86. cmd_auth.auths[0].hmac.size = 0;
  87. LOG_INFO("Calling NV_DefineSpace");
  88. rc = Tss2_Sys_NV_DefineSpace (sys_context,
  89. TPM2_RH_OWNER,
  90. &cmd_auth,
  91. &nv_auth,
  92. &nv_public,
  93. &rsp_auth);
  94. if (rc != TSS2_RC_SUCCESS) {
  95. LOG_ERROR("NV_DefineSpace failed: 0x%" PRIx32, rc);
  96. exit(1);
  97. }
  98. LOG_INFO("NV_DefineSpace success!, NV_index: 0x%x", nv_index);
  99. TPM2B_MAX_NV_BUFFER nv_data = { .size = 32 + 2, };
  100. rc = Tss2_MU_INT16_Marshal(TPM2_ALG_SHA256, nv_data.buffer,
  101. TPM2_MAX_DIGEST_BUFFER, NULL);
  102. if (rc != TSS2_RC_SUCCESS) {
  103. LOG_ERROR("Tss2_MU_INT16_Marshal failed: 0x%" PRIx32, rc);
  104. exit(1);
  105. }
  106. /* Write the empty policy session to the NV index */
  107. LOG_INFO("Calling NV_Write");
  108. rc = Tss2_Sys_NV_Write(sys_context,
  109. TPM2_RH_OWNER,
  110. nv_index,
  111. &cmd_auth,
  112. &nv_data,
  113. 0,
  114. &rsp_auth);
  115. if (rc != TPM2_RC_SUCCESS) {
  116. LOG_ERROR("Sys_NV_Write FAILED! Response Code : 0x%x", rc);
  117. exit(1);
  118. }
  119. LOG_INFO("Sys_NV_Write success!");
  120. /* Authorize the policy using the NV index */
  121. LOG_INFO("Calling PolicyAuthorizeNV");
  122. rc = Tss2_Sys_PolicyAuthorizeNV(sys_context,
  123. TPM2_RH_OWNER,
  124. nv_index,
  125. session_handle,
  126. &cmd_auth,
  127. &rsp_auth);
  128. if (rc != TPM2_RC_SUCCESS) {
  129. LOG_ERROR("PolicyAuthorizeNV FAILED! Response Code : 0x%x", rc);
  130. exit(1);
  131. }
  132. LOG_INFO("PolicyAuthorizeNV success!");
  133. /* Create a symmetric encryption key using the password session */
  134. TPM2B_SENSITIVE_CREATE in_sensitive = { 0 };
  135. TPMT_PUBLIC in_public = { 0 };
  136. TPM2B_TEMPLATE public_template = { 0 };
  137. TPM2B_PRIVATE out_private = { 0 };
  138. TPM2B_PUBLIC out_public = { 0 };
  139. TPM2B_NAME name = TPM2B_NAME_INIT;
  140. TPM2_HANDLE object_handle = 0;
  141. /* Use precomputed authPolicy for simplicity */
  142. char auth[] = {0x06, 0x3a, 0x24, 0xdc, 0x2f, 0xc9, 0x32, 0xc3,
  143. 0xb8, 0xa0, 0x85, 0xca, 0x67, 0x27, 0x3c, 0x03,
  144. 0xa6, 0x7c, 0x11, 0x39, 0x8f, 0x2a, 0x4a, 0x13,
  145. 0xbd, 0x05, 0x37, 0xf8, 0x5f, 0x47, 0x56, 0xcb};
  146. memcpy(in_public.authPolicy.buffer, auth, TPM2_SHA256_DIGEST_SIZE);
  147. in_public.type = TPM2_ALG_SYMCIPHER;
  148. in_public.nameAlg = TPM2_ALG_SHA256;
  149. in_public.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
  150. in_public.objectAttributes |= TPMA_OBJECT_DECRYPT;
  151. in_public.objectAttributes |= TPMA_OBJECT_SIGN_ENCRYPT;
  152. in_public.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
  153. in_public.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
  154. in_public.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
  155. in_public.parameters.symDetail.sym.algorithm = TPM2_ALG_AES;
  156. in_public.parameters.symDetail.sym.keyBits.sym = 128;
  157. in_public.parameters.symDetail.sym.mode.sym = TPM2_ALG_CFB;
  158. in_public.authPolicy.size = TPM2_SHA256_DIGEST_SIZE;
  159. uint8_t public_buf[sizeof(in_public)] = {0};
  160. size_t offset = 0;
  161. rc = Tss2_MU_TPMT_PUBLIC_Marshal(&in_public, public_buf,
  162. sizeof(in_public), &offset);
  163. if (rc != TPM2_RC_SUCCESS) {
  164. LOG_ERROR("Tss2_MU_TPMT_PUBLIC_Marshal FAILED! Response Code: 0x%x", rc);
  165. exit(1);
  166. }
  167. public_template.size = offset;
  168. memcpy(public_template.buffer, public_buf, offset);
  169. cmd_auth.count = 1;
  170. cmd_auth.auths[0].sessionHandle = TPM2_RH_PW;
  171. cmd_auth.auths[0].hmac.size = TPM2_SHA256_DIGEST_SIZE;
  172. /* Create a symmetric encryption key using the password session */
  173. LOG_INFO("Calling CreateLoaded");
  174. rc = Tss2_Sys_CreateLoaded (sys_context,
  175. TPM2_RH_OWNER,
  176. &cmd_auth,
  177. &in_sensitive,
  178. &public_template,
  179. &object_handle,
  180. &out_private,
  181. &out_public,
  182. &name,
  183. &rsp_auth);
  184. if (rc != TPM2_RC_SUCCESS) {
  185. LOG_ERROR("CreateLoaded FAILED! Response Code: 0x%x", rc);
  186. exit(1);
  187. }
  188. LOG_INFO("success object handle: 0x%x", object_handle);
  189. TPM2B_MAX_BUFFER data_in = TPM2B_MAX_BUFFER_INIT;
  190. TPM2B_MAX_BUFFER data_out = TPM2B_MAX_BUFFER_INIT;
  191. TPM2B_IV iv_in = TPM2B_IV_INIT;
  192. TPM2B_IV iv_out = TPM2B_IV_INIT;
  193. if (RAND_bytes(data_in.buffer, TPM2_MAX_DIGEST_BUFFER) != 1) {
  194. LOG_ERROR("RAND_bytes FAILED!");
  195. exit(1);
  196. }
  197. /* Call encrypt using the key object using the password session */
  198. LOG_INFO("Calling EncryptDecrypt using password session 0x%x", TPM2_RH_PW);
  199. LOGBLOB_DEBUG(data_in.buffer, 32, "%s", "First 32 bytes of plain text:");
  200. rc = TSS2_RETRY_EXP(Tss2_Sys_EncryptDecrypt (sys_context,
  201. object_handle,
  202. &cmd_auth,
  203. NO, /* encrypt */
  204. TPM2_ALG_NULL,
  205. &iv_in,
  206. &data_in,
  207. &data_out,
  208. &iv_out,
  209. &rsp_auth));
  210. if (rc == TPM2_RC_COMMAND_CODE) {
  211. LOG_WARNING("Encrypt/Decrypt not supported by TPM");
  212. rc = Tss2_Sys_NV_UndefineSpace(sys_context,
  213. TPM2_RH_OWNER,
  214. nv_index,
  215. &cmd_auth,
  216. &rsp_auth);
  217. if (rc != TSS2_RC_SUCCESS) {
  218. LOG_ERROR("Tss2_Sys_NV_UndefineSpace failed: 0x%"PRIx32, rc);
  219. return 99; /* fatal error */
  220. }
  221. rc = Tss2_Sys_FlushContext(sys_context, object_handle);
  222. if (rc != TSS2_RC_SUCCESS) {
  223. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  224. return 99; /* fatal error */
  225. }
  226. rc = Tss2_Sys_FlushContext(sys_context, session_handle);
  227. if (rc != TSS2_RC_SUCCESS) {
  228. LOG_ERROR("Tss2_Sys_FlushContext failed with 0x%"PRIx32, rc);
  229. return 99; /* fatal error */
  230. }
  231. return EXIT_SKIP;
  232. }
  233. if (rc != TPM2_RC_SUCCESS) {
  234. LOG_ERROR("EncryptDecrypt FAILED! Response Code : 0x%x", rc);
  235. exit(1);
  236. }
  237. LOG_INFO("EncryptDecrypt success!");
  238. LOGBLOB_DEBUG(data_out.buffer, 32, "%s", "First 32 bytes of cypher text:");
  239. cmd_auth.auths[0].sessionAttributes |= TPMA_SESSION_CONTINUESESSION;
  240. cmd_auth.auths[0].sessionHandle = session_handle;
  241. cmd_auth.auths[0].hmac.size = 0;
  242. memset(data_out.buffer, '\0', TPM2_MAX_DIGEST_BUFFER);
  243. /* Call encrypt using the key object using the policy session
  244. * This should pass because we allowed it with the PolicyAuthorizeNV call */
  245. LOG_INFO("Calling EncryptDecrypt using policy session 0x%x", session_handle);
  246. rc = TSS2_RETRY_EXP(Tss2_Sys_EncryptDecrypt (sys_context,
  247. object_handle,
  248. &cmd_auth,
  249. NO, /* encrypt */
  250. TPM2_ALG_NULL,
  251. &iv_in,
  252. &data_in,
  253. &data_out,
  254. &iv_out,
  255. &rsp_auth));
  256. if (rc != TPM2_RC_SUCCESS) {
  257. LOG_INFO("EncryptDecrypt Failed rc: 0x%x", rc);
  258. exit(1);
  259. }
  260. LOG_INFO("EncryptDecrypt success!");
  261. cmd_auth.auths[0].sessionHandle = TPM2_RH_PW;
  262. cmd_auth.auths[0].hmac.size = 0;
  263. /* Kill the NV index - this should invalidate the policy */
  264. rc = Tss2_Sys_NV_UndefineSpace(sys_context,
  265. TPM2_RH_OWNER,
  266. nv_index,
  267. &cmd_auth,
  268. &rsp_auth);
  269. if (rc != TSS2_RC_SUCCESS) {
  270. LOG_ERROR("Tss2_Sys_NV_UndefineSpace failed: 0x%" PRIx32, rc);
  271. exit(1);
  272. }
  273. LOG_INFO("Tss2_Sys_NV_UndefineSpace for NV index 0x%" PRIx32 " success!",
  274. nv_index);
  275. /* Call encrypt using the key object using the policy session again.
  276. * This should fail because the NV index is destroyed */
  277. cmd_auth.auths[0].sessionHandle = session_handle;
  278. memset(data_out.buffer, '\0', TPM2_MAX_DIGEST_BUFFER);
  279. LOG_INFO("Calling EncryptDecrypt again with policy session after destroying"
  280. " the NV index This should fail with RC_POLICY_FAIL");
  281. rc = TSS2_RETRY_EXP(Tss2_Sys_EncryptDecrypt(sys_context,
  282. object_handle,
  283. &cmd_auth,
  284. NO, /* encrypt */
  285. TPM2_ALG_NULL,
  286. &iv_in,
  287. &data_in,
  288. &data_out,
  289. &iv_out,
  290. &rsp_auth));
  291. if (rc != TPM2_RC_1 + TPM2_RC_S + TPM2_RC_POLICY_FAIL) {
  292. LOG_INFO("EncryptDecrypt passes unexpectedly rc: 0x%x", rc);
  293. exit(1);
  294. }
  295. LOG_INFO("EncryptDecrypt failed as expected!");
  296. /* Clean up the session and key*/
  297. rc = Tss2_Sys_FlushContext (sys_context, object_handle);
  298. if (rc != TSS2_RC_SUCCESS) {
  299. LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
  300. exit(1);
  301. }
  302. LOG_INFO("Flushed context for object handle: 0x%" PRIx32 " success!",
  303. object_handle);
  304. rc = Tss2_Sys_FlushContext (sys_context, session_handle);
  305. if (rc != TSS2_RC_SUCCESS) {
  306. LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
  307. exit(1);
  308. }
  309. LOG_INFO("Flushed context for session handle: 0x%" PRIx32 " success!",
  310. session_handle);
  311. return 0;
  312. }