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 "tss2_esys.h"
12 #include "tss2_mu.h"
13
14 #include "esys_iutil.h"
15 #include "test-esapi.h"
16 #define LOGMODULE test
17 #include "util/log.h"
18 #include "util/aux_util.h"
19
20 #define FLUSH true
21 #define NOT_FLUSH false
22
23 /*
24 * Function to compare policy digest with expected digest.
25 * The digest is computed with Esys_PolicyGetDigest.
26 */
27 bool
cmp_policy_digest(ESYS_CONTEXT * esys_context,ESYS_TR * session,TPM2B_DIGEST * expected_digest,char * comment,bool flush_session)28 cmp_policy_digest(ESYS_CONTEXT * esys_context,
29 ESYS_TR * session,
30 TPM2B_DIGEST * expected_digest,
31 char *comment, bool flush_session)
32 {
33
34 TSS2_RC r;
35 TPM2B_DIGEST *policyDigest;
36
37 r = Esys_PolicyGetDigest(esys_context,
38 *session,
39 ESYS_TR_NONE,
40 ESYS_TR_NONE, ESYS_TR_NONE, &policyDigest);
41 goto_if_error(r, "Error: PolicyGetDigest", error);
42
43 LOGBLOB_DEBUG(&policyDigest->buffer[0], policyDigest->size,
44 "POLICY DIGEST");
45
46 if (policyDigest->size != 20
47 || memcmp(&policyDigest->buffer[0], &expected_digest->buffer[0],
48 policyDigest->size)) {
49 free(policyDigest);
50 LOG_ERROR("Error: Policy%s digest did not match expected policy.",
51 comment);
52 return false;
53 }
54 free(policyDigest);
55 if (flush_session) {
56 r = Esys_FlushContext(esys_context, *session);
57 goto_if_error(r, "Error: PolicyGetDigest", error);
58 *session = ESYS_TR_NONE;
59 }
60
61 return true;
62
63 error:
64 return false;
65 }
66
67 /** This test is intended to test the ESAPI policy commands, not tested
68 * in other test cases.
69 * When possoble the commands are tested with a
70 * trial session and the policy digest is compared with the expected digest.
71 *
72 * Tested ESAPI commands:
73 * - Esys_FlushContext() (M)
74 * - Esys_NV_DefineSpace() (M)
75 * - Esys_NV_UndefineSpace() (M)
76 * - Esys_PolicyCounterTimer() (M)
77 * - Esys_PolicyDuplicationSelect() (M)
78 * - Esys_PolicyGetDigest() (M)
79 * - Esys_PolicyNV() (M)
80 * - Esys_PolicyNameHash() (M)
81 * - Esys_PolicyNvWritten() (M)
82 * - Esys_PolicyOR() (M)
83 * - Esys_PolicyPCR() (M)
84 * - Esys_PolicyPhysicalPresence() (O)
85 * - Esys_PolicyRestart() (M)
86 * - Esys_SetPrimaryPolicy() (M)
87 * - Esys_StartAuthSession() (M)
88 *
89 * @param[in,out] esys_context The ESYS_CONTEXT.
90 * @retval EXIT_FAILURE
91 * @retval EXIT_SKIP
92 * @retval EXIT_SUCCESS
93 */
94 int
test_esys_policy_regression(ESYS_CONTEXT * esys_context)95 test_esys_policy_regression(ESYS_CONTEXT * esys_context)
96 {
97 TSS2_RC r;
98 int failure_return = EXIT_FAILURE;
99 ESYS_TR nvHandle = ESYS_TR_NONE;
100 ESYS_TR sessionTrialNV = ESYS_TR_NONE;
101 ESYS_TR sessionTrialPCR = ESYS_TR_NONE;
102
103 /* Dummy parameters for trial sessoin */
104 ESYS_TR sessionTrial = ESYS_TR_NONE;
105 TPMT_SYM_DEF symmetricTrial = {.algorithm = TPM2_ALG_AES,
106 .keyBits = {.aes = 128},
107 .mode = {.aes = TPM2_ALG_CFB}
108 };
109 TPM2B_NONCE nonceCallerTrial = {
110 .size = 20,
111 .buffer = {11, 12, 13, 14, 15, 16, 17, 18, 19, 11,
112 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
113 };
114
115 /*
116 * Test PolicyPCR
117 */
118 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
119 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
120 &nonceCallerTrial,
121 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
122 &sessionTrial);
123 goto_if_error(r, "Error: During initialization of policy trial session",
124 error);
125
126 sessionTrialPCR = sessionTrial;
127 TPML_PCR_SELECTION pcrSelection = {
128 .count = 1,
129 .pcrSelections = {
130 {.hash = TPM2_ALG_SHA1,
131 .sizeofSelect = 3,
132 .pcrSelect = {00, 00, 01},
133 },
134 }
135 };
136 /* SHA1 digest for PCR register with zeros */
137 TPM2B_DIGEST pcr_digest_zero = {
138 .size = 20,
139 .buffer = {0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
140 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f}
141 };
142
143 r = Esys_PolicyPCR(esys_context,
144 sessionTrial,
145 ESYS_TR_NONE,
146 ESYS_TR_NONE,
147 ESYS_TR_NONE, &pcr_digest_zero, &pcrSelection);
148 goto_if_error(r, "Error: pcr digest can not be computed.", error);
149
150 TPM2B_DIGEST expectedPolicyPCR = {
151 .size = 20,
152 .buffer = {0x85, 0x33, 0x11, 0x83, 0x19, 0x03, 0x12, 0xf5, 0xe8, 0x3c,
153 0x60, 0x43, 0x34, 0x6f, 0x9f, 0x37, 0x21, 0x04, 0x76, 0x8e},
154 };
155
156 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyPCR,
157 "PCR", NOT_FLUSH))
158 goto error;
159
160 /* Create valid NV handle */
161 TPM2B_AUTH auth = {.size = 20,
162 .buffer = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
163 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}
164 };
165
166 TPM2B_NV_PUBLIC publicInfo = {
167 .size = 0,
168 .nvPublic = {
169 .nvIndex = TPM2_NV_INDEX_FIRST,
170 .nameAlg = TPM2_ALG_SHA1,
171 .attributes = (TPMA_NV_OWNERWRITE |
172 TPMA_NV_AUTHWRITE |
173 TPMA_NV_WRITE_STCLEAR |
174 TPMA_NV_READ_STCLEAR |
175 TPMA_NV_AUTHREAD | TPMA_NV_OWNERREAD),
176 .authPolicy = {
177 .size = 0,
178 .buffer = {}
179 ,
180 }
181 ,
182 .dataSize = 32,
183 }
184 };
185
186 r = Esys_NV_DefineSpace(esys_context,
187 ESYS_TR_RH_OWNER,
188 ESYS_TR_PASSWORD,
189 ESYS_TR_NONE,
190 ESYS_TR_NONE, &auth, &publicInfo, &nvHandle);
191
192 goto_if_error(r, "Error esys define nv space", error);
193
194 /*
195 * Test PolicyNV
196 */
197 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
198 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
199 &nonceCallerTrial,
200 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
201 &sessionTrial);
202 goto_if_error(r, "Error: During initialization of policy trial session",
203 error);
204
205 sessionTrialNV = sessionTrial;
206 UINT16 offset = 0;
207 TPM2_EO operation = TPM2_EO_EQ;
208 TPM2B_OPERAND operandB = {
209 .size = 8,
210 .buffer = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09}
211 };
212
213 r = Esys_PolicyNV(esys_context,
214 ESYS_TR_RH_OWNER,
215 nvHandle,
216 sessionTrial,
217 ESYS_TR_PASSWORD,
218 ESYS_TR_NONE, ESYS_TR_NONE, &operandB, offset, operation);
219 goto_if_error(r, "Error: PolicyNV", error);
220
221 TPM2B_DIGEST expectedPolicyNV = {
222 .size = 20,
223 .buffer = {0x09, 0x92, 0x96, 0x4c, 0x18, 0x4c, 0x29, 0xde, 0x53, 0x52,
224 0xc0, 0x20, 0x86, 0x81, 0xca, 0xe9, 0x94, 0x23, 0x09, 0x24}
225 };
226
227 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyNV,
228 "NV", NOT_FLUSH))
229 goto error;
230
231 /*
232 * Test PolicyOR
233 */
234
235 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
236 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
237 &nonceCallerTrial,
238 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
239 &sessionTrial);
240 goto_if_error(r, "Error: During initialization of policy trial session",
241 error);
242
243 TPML_DIGEST pHashList = {
244 .count = 2,
245 .digests = {
246 expectedPolicyPCR,
247 expectedPolicyNV}
248 };
249
250 r = Esys_PolicyOR(esys_context,
251 sessionTrial,
252 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE, &pHashList);
253
254 goto_if_error(r, "Error: PolicyOR", error);
255
256 TPM2B_DIGEST expectedPolicyOR = {
257 .size = 20,
258 .buffer = {0x5d, 0x34, 0x38, 0xd3, 0xb8, 0xf0, 0x82, 0xef, 0x22, 0x12,
259 0xeb, 0x29, 0x54, 0xde, 0x66, 0x37, 0x06, 0x2f, 0xd8, 0x70}
260 };
261
262 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyOR,
263 "OR", FLUSH))
264 goto error;
265
266 r = Esys_FlushContext(esys_context, sessionTrialPCR);
267 goto_if_error(r, "Error: FlushContext", error);
268 sessionTrialPCR = ESYS_TR_NONE;
269
270 r = Esys_FlushContext(esys_context, sessionTrialNV);
271 goto_if_error(r, "Error: FlushContext", error);
272 sessionTrial = ESYS_TR_NONE;
273
274 /*
275 * Test PolicyCounterTimer
276 */
277 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
278 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
279 &nonceCallerTrial,
280 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
281 &sessionTrial);
282 goto_if_error(r, "Error: During initialization of policy trial session",
283 error);
284
285 r = Esys_PolicyCounterTimer(esys_context,
286 sessionTrial,
287 ESYS_TR_NONE,
288 ESYS_TR_NONE,
289 ESYS_TR_NONE, &operandB, offset, operation);
290 goto_if_error(r, "Error: PolicyCounterTimer", error);
291
292 TPM2B_DIGEST expectedPolicyCounterTimer = {
293 .size = 20,
294 .buffer = {0x93, 0x8a, 0x9c, 0x7f, 0x60, 0xd4, 0xdd, 0xb5, 0x5a, 0x13,
295 0xc1, 0x7e, 0x3c, 0xe4, 0x62, 0x10, 0x82, 0xd0, 0x11, 0xad}
296 };
297
298 if (!cmp_policy_digest
299 (esys_context, &sessionTrial, &expectedPolicyCounterTimer,
300 "CounterTimter", NOT_FLUSH))
301 goto error;
302
303 /*
304 * Test PolicyRestart
305 */
306 r = Esys_PolicyRestart(esys_context,
307 sessionTrial,
308 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE);
309 goto_if_error(r, "Error: PolicyRestart", error);
310
311 TPM2B_DIGEST expectedPolicyRestart = {
312 .size = 0,
313 .buffer = {0}
314 };
315
316 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyRestart,
317 "Restart", FLUSH))
318 goto error;
319
320 /*
321 * Test PolicyNameHash
322 */
323 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
324 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
325 &nonceCallerTrial,
326 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
327 &sessionTrial);
328 goto_if_error(r, "Error: During initialization of policy trial session",
329 error);
330
331 TPM2B_DIGEST nameHash = {
332 .size = 20,
333 .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
334 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
335 };
336
337 r = Esys_PolicyNameHash(esys_context,
338 sessionTrial,
339 ESYS_TR_NONE,
340 ESYS_TR_NONE, ESYS_TR_NONE, &nameHash);
341 goto_if_error(r, "Error: PolicyNameHash", error);
342
343 TPM2B_DIGEST expectedPolicyNameHash = {
344 .size = 20,
345 .buffer = {0xb4, 0x00, 0x9a, 0xb0, 0x80, 0x49, 0x55, 0x5f, 0x8c, 0xf2,
346 0x7b, 0x2e, 0x55, 0x7e, 0x74, 0x7d, 0x44, 0x39, 0x68, 0x7f}
347 };
348
349 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyNameHash,
350 "NameHash", FLUSH))
351 goto error;
352
353 /*
354 * Test PolicyDuplicationSelect
355 */
356 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
357 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
358 &nonceCallerTrial,
359 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
360 &sessionTrial);
361 goto_if_error(r, "Error: During initialization of policy trial session",
362 error);
363
364 TPM2B_NAME name1 = {
365 .size = 20,
366 .name = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
367 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
368 };
369
370 TPM2B_NAME name2 = {
371 .size = 20,
372 .name = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
373 31, 32, 33, 34, 35, 36, 37, 38, 39, 30}
374 };
375
376 r = Esys_PolicyDuplicationSelect(esys_context,
377 sessionTrial,
378 ESYS_TR_NONE,
379 ESYS_TR_NONE,
380 ESYS_TR_NONE, &name1, &name2, 0);
381 goto_if_error(r, "Error: PolicyDuplicationSelect", error);
382
383 TPM2B_DIGEST expectedPolicyDuplicationSelect = {
384 .size = 20,
385 .buffer = {0x53, 0x77, 0xf5, 0x82, 0x83, 0x18, 0xa9, 0xee, 0x76, 0x70,
386 0xa8, 0x31, 0x5d, 0x5c, 0x04, 0xb4, 0xaa, 0xaf, 0x96, 0x8f}
387 };
388
389 if (!cmp_policy_digest
390 (esys_context, &sessionTrial, &expectedPolicyDuplicationSelect,
391 "PolicyDuplicationSelect", FLUSH))
392 goto error;
393
394 goto_if_error(r, "Error: FlushContext", error);
395
396 /*
397 * Test PolicyNvWritten
398 */
399 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
400 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
401 &nonceCallerTrial,
402 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
403 &sessionTrial);
404 goto_if_error(r, "Error: During initialization of policy trial session",
405 error);
406
407 r = Esys_PolicyNvWritten(esys_context,
408 sessionTrial,
409 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE, 0);
410 goto_if_error(r, "Error: PolicyNvWritten", error);
411
412 TPM2B_DIGEST expectedPolicyNvWritten = {
413 .size = 20,
414 .buffer = {0x5a, 0x91, 0xe7, 0x10, 0x53, 0x86, 0xbd, 0x54, 0x7a, 0x15,
415 0xaa, 0xd4, 0x03, 0x69, 0xb1, 0xe2, 0x5e, 0x46, 0x28, 0x73}
416 };
417
418 if (!cmp_policy_digest(esys_context, &sessionTrial, &expectedPolicyNvWritten,
419 "NvWritten", FLUSH))
420 goto error;
421
422 /*
423 * Space not needed for further tests.
424 */
425
426 r = Esys_NV_UndefineSpace(esys_context,
427 ESYS_TR_RH_OWNER,
428 nvHandle,
429 ESYS_TR_PASSWORD,
430 ESYS_TR_NONE,
431 ESYS_TR_NONE
432 );
433 goto_if_error(r, "Error: NV_UndefineSpace", error);
434 nvHandle = ESYS_TR_NONE;
435
436 /*
437 * Test PolicySetPrimaryPolicy
438 */
439
440 TPM2B_DIGEST authPolicy = {
441 .size = 20,
442 .buffer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
443 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
444 };
445
446 r = Esys_SetPrimaryPolicy(esys_context,
447 ESYS_TR_RH_OWNER,
448 ESYS_TR_PASSWORD,
449 ESYS_TR_NONE, ESYS_TR_NONE,
450 &authPolicy,
451 TPM2_ALG_SHA1);
452
453 if ((r == TPM2_RC_COMMAND_CODE) ||
454 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
455 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
456 LOG_WARNING("Command TPM2_SetPrimaryPolicy not supported by TPM.");
457 } else {
458 goto_if_error(r, "Error: SetPrimaryPolicy", error);
459 }
460
461 return EXIT_SUCCESS;
462
463 error:
464
465 if (sessionTrial != ESYS_TR_NONE) {
466 if (Esys_FlushContext(esys_context, sessionTrial) != TSS2_RC_SUCCESS) {
467 LOG_ERROR("Cleanup sessionTrial failed.");
468 }
469 }
470
471 if (sessionTrialPCR != ESYS_TR_NONE) {
472 if (Esys_FlushContext(esys_context, sessionTrialPCR) != TSS2_RC_SUCCESS) {
473 LOG_ERROR("Cleanup sessionTrialPCR failed.");
474 }
475 }
476
477 if (nvHandle != ESYS_TR_NONE) {
478 if (Esys_NV_UndefineSpace(esys_context,
479 ESYS_TR_RH_OWNER,
480 nvHandle,
481 ESYS_TR_PASSWORD,
482 ESYS_TR_NONE,
483 ESYS_TR_NONE) != TSS2_RC_SUCCESS) {
484 LOG_ERROR("Cleanup nvHandle failed.");
485 }
486 }
487
488 return failure_return;
489 }
490
491 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)492 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
493 return test_esys_policy_regression(esys_context);
494 }
495