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
11 #include <stdlib.h>
12
13 #include "tss2_tcti_mssim.h"
14 #define LOGMODULE test
15 #include "util/log.h"
16 #include "sapi-util.h"
17 #include "test.h"
18 #include "test-esapi.h"
19
20
21 /* Test copmmand cancel functionality.
22 * Create a primary object, which should pass. Then send a cancel on platform
23 * command and try to create a primary object again - this should fial with
24 * TPM_CANCEL rc. Then send a Cancel off command and try to create the object
25 * for the third time. This time it should pass again. */
26
27 int
test_invoke(TSS2_SYS_CONTEXT * sapi_context)28 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
29 {
30 TPM2_HANDLE handle = 0;
31 TSS2_TCTI_CONTEXT *tcti_context;
32 TSS2_RC rc;
33 TPM2B_SENSITIVE_CREATE in_sensitive = { 0 };
34 TPM2B_PUBLIC in_public = { 0 };
35 TPM2B_DATA outside_info = { 0 };
36 TPML_PCR_SELECTION creation_pcr = { 0 };
37 TPM2B_PUBLIC out_public = { 0 };
38 TPM2B_CREATION_DATA creation_data = { 0 };
39 TPM2B_DIGEST creation_hash = TPM2B_DIGEST_INIT;
40 TPMT_TK_CREATION creation_ticket = { 0 };
41 TPM2B_NAME name = TPM2B_NAME_INIT;
42 TSS2L_SYS_AUTH_COMMAND sessions_cmd = {
43 .auths = {{ .sessionHandle = TPM2_RS_PW }},
44 .count = 1
45 };
46 TSS2L_SYS_AUTH_RESPONSE sessions_rsp = { 0 };
47
48 in_public.publicArea.type = TPM2_ALG_RSA;
49 in_public.publicArea.nameAlg = TPM2_ALG_SHA256;
50 in_public.publicArea.objectAttributes |= TPMA_OBJECT_RESTRICTED;
51 in_public.publicArea.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
52 in_public.publicArea.objectAttributes |= TPMA_OBJECT_DECRYPT;
53 in_public.publicArea.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
54 in_public.publicArea.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
55 in_public.publicArea.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
56 in_public.publicArea.parameters.rsaDetail.symmetric.algorithm = TPM2_ALG_AES;
57 in_public.publicArea.parameters.rsaDetail.symmetric.keyBits.aes = 128;
58 in_public.publicArea.parameters.rsaDetail.symmetric.mode.aes = TPM2_ALG_CFB;
59 in_public.publicArea.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
60 in_public.publicArea.parameters.rsaDetail.keyBits = 2048;
61
62 rc = Tss2_Sys_GetTctiContext(sapi_context, &tcti_context);
63 if (rc != TPM2_RC_SUCCESS) {
64 LOG_ERROR("GetTctiContext FAILED! Response Code : 0x%x", rc);
65 exit(1);
66 }
67 LOG_DEBUG("GetTctiContext SUCCESS!");
68
69 rc = create_primary_rsa_2048_aes_128_cfb (sapi_context, &handle);
70 if (rc != TPM2_RC_SUCCESS) {
71 LOG_ERROR("CreatePrimary FAILED! Response Code : 0x%x", rc);
72 exit(1);
73 }
74 LOG_DEBUG("create_primary SUCCESS!");
75
76 rc = Tss2_Sys_FlushContext(sapi_context, handle);
77 if (rc != TPM2_RC_SUCCESS) {
78 LOG_ERROR("FlushContext FAILED! Response Code : 0x%x", rc);
79 exit(1);
80 }
81 LOG_DEBUG("FlushContext SUCCESS!");
82
83 rc = tcti_platform_command(tcti_context, MS_SIM_CANCEL_ON);
84 if (rc == TSS2_TCTI_RC_BAD_CONTEXT) {
85 LOG_DEBUG("tcti_context not suitable for command! Skipping test");
86 exit(EXIT_SKIP);
87 } else if (rc != TPM2_RC_SUCCESS) {
88 LOG_ERROR("tcti_platform_command FAILED! Response Code : 0x%x", rc);
89 exit(1);
90 }
91 LOG_DEBUG("tcti_platform_command CANCEL_ON SUCCESS!");
92
93 rc = Tss2_Sys_CreatePrimary (sapi_context,
94 TPM2_RH_OWNER,
95 &sessions_cmd,
96 &in_sensitive,
97 &in_public,
98 &outside_info,
99 &creation_pcr,
100 &handle,
101 &out_public,
102 &creation_data,
103 &creation_hash,
104 &creation_ticket,
105 &name,
106 &sessions_rsp);
107 if (rc != TPM2_RC_CANCELED) {
108 LOG_DEBUG("CreatePrimary returned unexpected rc 0x%x, expected 0x%x", rc,
109 TPM2_RC_CANCELED);
110 exit(1);
111 }
112 LOG_DEBUG("create_primary returned rc cancelled!");
113
114 rc = tcti_platform_command(tcti_context, MS_SIM_CANCEL_OFF);
115 if (rc != TPM2_RC_SUCCESS) {
116 LOG_ERROR("FlushContext FAILED! Response Code : 0x%x", rc);
117 exit(1);
118 }
119 LOG_DEBUG("tcti_platform_command CANCEL_OFF SUCCESS!");
120
121 rc = create_primary_rsa_2048_aes_128_cfb(sapi_context, &handle);
122 if (rc != TPM2_RC_SUCCESS) {
123 LOG_ERROR("create_primary FAILED! Response Code : 0x%x", rc);
124 exit(1);
125 }
126 LOG_DEBUG("create_primary SUCCESS!");
127
128 rc = Tss2_Sys_FlushContext(sapi_context, handle);
129 if (rc != TPM2_RC_SUCCESS) {
130 LOG_ERROR("FlushContext FAILED! Response Code : 0x%x", rc);
131 exit(1);
132 }
133 LOG_DEBUG("FlushContext SUCCESS!");
134 return 0;
135 }
136