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_PolicyPhysicalPresence() (O)
76 *
77 * @param[in,out] esys_context The ESYS_CONTEXT.
78 * @retval EXIT_FAILURE
79 * @retval EXIT_SKIP
80 * @retval EXIT_SUCCESS
81 */
82 int
test_esys_policy_physical_presence_opt(ESYS_CONTEXT * esys_context)83 test_esys_policy_physical_presence_opt(ESYS_CONTEXT * esys_context)
84 {
85 TSS2_RC r;
86 int failure_return = EXIT_FAILURE;
87
88 /* Dummy parameters for trial sessoin */
89 ESYS_TR sessionTrial = ESYS_TR_NONE;
90 TPMT_SYM_DEF symmetricTrial = {.algorithm = TPM2_ALG_AES,
91 .keyBits = {.aes = 128},
92 .mode = {.aes = TPM2_ALG_CFB}
93 };
94 TPM2B_NONCE nonceCallerTrial = {
95 .size = 20,
96 .buffer = {11, 12, 13, 14, 15, 16, 17, 18, 19, 11,
97 21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
98 };
99
100 /*
101 * Test PolicyPhysicalPresence
102 */
103 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
104 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
105 &nonceCallerTrial,
106 TPM2_SE_TRIAL, &symmetricTrial, TPM2_ALG_SHA1,
107 &sessionTrial);
108 goto_if_error(r, "Error: During initialization of policy trial session",
109 error);
110
111 r = Esys_PolicyPhysicalPresence(esys_context,
112 sessionTrial,
113 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE);
114 if ((r == TPM2_RC_COMMAND_CODE) ||
115 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_RC_LAYER)) ||
116 (r == (TPM2_RC_COMMAND_CODE | TSS2_RESMGR_TPM_RC_LAYER))) {
117 LOG_WARNING("Command TPM2_PolicyPhysicalPresence not supported by TPM.");
118 failure_return = EXIT_SKIP;
119 goto error;
120 } else {
121 goto_if_error(r, "Error: PolicyPhysicalPresence", error);
122 }
123
124 TPM2B_DIGEST expectedPolicyPhysicalPresence = {
125 .size = 20,
126 .buffer = {0x9a, 0xcb, 0x06, 0x39, 0x5f, 0x83, 0x1f, 0x88, 0xe8, 0x9e,
127 0xea, 0xc2, 0x94, 0x42, 0xcb, 0x0e, 0xbe, 0x94, 0x85, 0xab}
128 };
129
130 if (!cmp_policy_digest
131 (esys_context, &sessionTrial, &expectedPolicyPhysicalPresence,
132 "PhysicalPresence", FLUSH))
133 goto error;
134
135 return EXIT_SUCCESS;
136
137 error:
138
139 if (sessionTrial != ESYS_TR_NONE) {
140 if (Esys_FlushContext(esys_context, sessionTrial) != TSS2_RC_SUCCESS) {
141 LOG_ERROR("Cleanup sessionTrial failed.");
142 }
143 }
144
145 return failure_return;
146 }
147
148 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)149 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
150 return test_esys_policy_physical_presence_opt(esys_context);
151 }
152