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
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdlib.h>
12
13 #include "tss2_esys.h"
14 #include "tss2_mu.h"
15
16 #include "esys_iutil.h"
17 #include "test-esapi.h"
18 #define LOGMODULE test
19 #include "util/log.h"
20 #include "util/aux_util.h"
21
22 #define FLUSH true
23 #define NOT_FLUSH false
24
25 /*
26 * Function to compare policy digest with expected digest.
27 * The digest is computed with Esys_PolicyGetDigest.
28 */
29 bool
cmp_policy_digest(ESYS_CONTEXT * esys_context,ESYS_TR * session,TPM2B_DIGEST * expected_digest,char * comment,bool flush_session)30 cmp_policy_digest(ESYS_CONTEXT * esys_context,
31 ESYS_TR * session,
32 TPM2B_DIGEST * expected_digest,
33 char *comment, bool flush_session)
34 {
35
36 TSS2_RC r;
37 TPM2B_DIGEST *policyDigest;
38
39 r = Esys_PolicyGetDigest(esys_context,
40 *session,
41 ESYS_TR_NONE,
42 ESYS_TR_NONE, ESYS_TR_NONE, &policyDigest);
43 goto_if_error(r, "Error: PolicyGetDigest", error);
44
45 LOGBLOB_DEBUG(&policyDigest->buffer[0], policyDigest->size,
46 "POLICY DIGEST");
47
48 if (policyDigest->size != 20
49 || memcmp(&policyDigest->buffer[0], &expected_digest->buffer[0],
50 policyDigest->size)) {
51 free(policyDigest);
52 LOG_ERROR("Error: Policy%s digest did not match expected policy.",
53 comment);
54 return false;
55 }
56 free(policyDigest);
57 if (flush_session) {
58 r = Esys_FlushContext(esys_context, *session);
59 goto_if_error(r, "Error: PolicyGetDigest", error);
60 *session = ESYS_TR_NONE;
61 }
62
63 return true;
64
65 error:
66 return false;
67 }
68
69 /** This test is intended to test the ESAPI policy commands, not tested
70 * in other test cases.
71 * When possoble the commands are tested with a
72 * trial session and the policy digest is compared with the expected digest.
73 *
74 * Tested ESAPI commands:
75 * - Esys_FlushContext() (M)
76 * - Esys_NV_DefineSpace() (M)
77 * - Esys_PolicyAuthorizeNV() (F)
78 * - Esys_PolicyNV() (M)
79 *
80 * @param[in,out] esys_context The ESYS_CONTEXT.
81 * @retval EXIT_FAILURE
82 * @retval EXIT_SKIP
83 * @retval EXIT_SUCCESS
84 */
85 int
test_esys_policy_authorize_nv_opt(ESYS_CONTEXT * esys_context)86 test_esys_policy_authorize_nv_opt(ESYS_CONTEXT * esys_context)
87 {
88 TSS2_RC r;
89 int failure_return = EXIT_FAILURE;
90 ESYS_TR nvHandle = ESYS_TR_NONE;
91
92 /* Dummy parameters for trial sessoin */
93 ESYS_TR sessionTrial = ESYS_TR_NONE;
94 TPMT_SYM_DEF symmetricTrial = {.algorithm = TPM2_ALG_AES,
95 .keyBits = {.aes = 128},
96 .mode = {.aes = TPM2_ALG_CFB}
97 };
98 TPM2B_NONCE nonceCallerTrial = {
99 .size = 20,
100 .buffer = {11, 12, 13, 14, 15, 16, 17, 18, 19, 11,
101 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
102 };
103
104 /* Create valid NV handle */
105 TPM2B_AUTH auth = {.size = 20,
106 .buffer = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
107 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}
108 };
109
110 TPM2B_NV_PUBLIC publicInfo = {
111 .size = 0,
112 .nvPublic = {
113 .nvIndex = TPM2_NV_INDEX_FIRST,
114 .nameAlg = TPM2_ALG_SHA1,
115 .attributes = (TPMA_NV_OWNERWRITE |
116 TPMA_NV_AUTHWRITE |
117 TPMA_NV_WRITE_STCLEAR |
118 TPMA_NV_READ_STCLEAR |
119 TPMA_NV_AUTHREAD | TPMA_NV_OWNERREAD),
120 .authPolicy = {
121 .size = 0,
122 .buffer = {}
123 ,
124 }
125 ,
126 .dataSize = 32,
127 }
128 };
129
130 r = Esys_NV_DefineSpace(esys_context,
131 ESYS_TR_RH_OWNER,
132 ESYS_TR_PASSWORD,
133 ESYS_TR_NONE,
134 ESYS_TR_NONE, &auth, &publicInfo, &nvHandle);
135
136 goto_if_error(r, "Error esys define nv space", error);
137
138 /*
139 * Test PolicyNV
140 */
141 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
142 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
143 &nonceCallerTrial,
144 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
145 &sessionTrial);
146 goto_if_error(r, "Error: During initialization of policy trial session",
147 error);
148
149 UINT16 offset = 0;
150 TPM2_EO operation = TPM2_EO_EQ;
151 TPM2B_OPERAND operandB = {
152 .size = 8,
153 .buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09}
154 };
155
156 r = Esys_PolicyNV(esys_context,
157 ESYS_TR_RH_OWNER,
158 nvHandle,
159 sessionTrial,
160 ESYS_TR_PASSWORD,
161 ESYS_TR_NONE, ESYS_TR_NONE, &operandB, offset, operation);
162 goto_if_error(r, "Error: PolicyNV", error);
163
164 TPM2B_DIGEST expectedPolicyNV = {
165 .size = 20,
166 .buffer = {0x09, 0x92, 0x96, 0x4c, 0x18, 0x4c, 0x29, 0xde, 0x53, 0x52,
167 0xc0, 0x20, 0x86, 0x81, 0xca, 0xe9, 0x94, 0x23, 0x09, 0x24}
168 };
169
170 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyNV,
171 "NV", NOT_FLUSH))
172 goto error;
173
174 /*
175 * Test PolicyAuthorizeNV
176 */
177 r = Esys_PolicyAuthorizeNV(esys_context,
178 ESYS_TR_RH_OWNER,
179 nvHandle,
180 sessionTrial,
181 ESYS_TR_PASSWORD,
182 ESYS_TR_NONE, ESYS_TR_NONE);
183 if ((r == TPM2_RC_COMMAND_CODE) ||
184 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
185 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
186 LOG_WARNING("Command TPM2_PolicyAuthorizeNV not supported by TPM.");
187 failure_return = EXIT_SKIP;
188 goto error;
189 } else {
190 goto_if_error(r, "Error: PolicyAuthorizeNV", error);
191 }
192
193 /*
194 * Space not needed for further tests.
195 */
196
197 r = Esys_NV_UndefineSpace(esys_context,
198 ESYS_TR_RH_OWNER,
199 nvHandle,
200 ESYS_TR_PASSWORD,
201 ESYS_TR_NONE,
202 ESYS_TR_NONE
203 );
204 goto_if_error(r, "Error: NV_UndefineSpace", error);
205 nvHandle = ESYS_TR_NONE;
206
207 r = Esys_FlushContext(esys_context, sessionTrial);
208 goto_if_error(r, "Error: FlushContext", error);
209 sessionTrial = ESYS_TR_NONE;
210
211 return EXIT_SUCCESS;
212
213 error:
214
215 if (sessionTrial != ESYS_TR_NONE) {
216 if (Esys_FlushContext(esys_context, sessionTrial) != TSS2_RC_SUCCESS) {
217 LOG_ERROR("Cleanup sessionTrial failed.");
218 }
219 }
220
221 if (nvHandle != ESYS_TR_NONE) {
222 if (Esys_NV_UndefineSpace(esys_context,
223 ESYS_TR_RH_OWNER,
224 nvHandle,
225 ESYS_TR_PASSWORD,
226 ESYS_TR_NONE,
227 ESYS_TR_NONE) != TSS2_RC_SUCCESS) {
228 LOG_ERROR("Cleanup nvHandle failed.");
229 }
230 }
231
232 return failure_return;
233 }
234
235 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)236 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
237 return test_esys_policy_authorize_nv_opt(esys_context);
238 }
239