blob: 1f9d801efef4c75df076e50b9ea7b41cd9be17c8 [file] [log] [blame]
Juergen Repp607317b2018-04-12 10:34:57 +02001/*******************************************************************************
2 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 *******************************************************************************/
27
28#include "tss2_esys.h"
29
30#include "esys_iutil.h"
31#define LOGMODULE test
32#include "util/log.h"
33
34/*
35 * This test is intended to test the ESAPI audit commands.
36 * First a key for signing the audit digest is computed.
37 * A audit session is started, and for the command GetCapability the
38 * command audit digest and the session audit digest is computed.
39 * (Esys_GetCommandAuditDigest, Esys_GetSessionAuditDigest). In the
40 * last test the audit hash alg is changed with Esys_SetCommandCodeAuditStatus.
41 */
42
43int
44test_invoke_esapi(ESYS_CONTEXT * esys_context)
45{
46 uint32_t r = 0;
47
48 /* Compute a signing key */
49 TPM2B_AUTH authValuePrimary = {
50 .size = 5,
51 .buffer = {1, 2, 3, 4, 5}
52 };
53
54 TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
55 .size = 4,
56 .sensitive = {
57 .userAuth = {
58 .size = 0,
59 .buffer = {0},
60 },
61 .data = {
62 .size = 0,
63 .buffer = {0},
64 },
65 },
66 };
67
68 inSensitivePrimary.sensitive.userAuth = authValuePrimary;
69
70 TPM2B_PUBLIC inPublic = {
71 .size = 0,
72 .publicArea = {
73 .type = TPM2_ALG_RSA,
74 .nameAlg = TPM2_ALG_SHA1,
75 .objectAttributes = (
76 TPMA_OBJECT_USERWITHAUTH |
77 TPMA_OBJECT_RESTRICTED |
78 TPMA_OBJECT_SIGN_ENCRYPT |
79 TPMA_OBJECT_FIXEDTPM |
80 TPMA_OBJECT_FIXEDPARENT |
81 TPMA_OBJECT_SENSITIVEDATAORIGIN
82 ),
83 .authPolicy = {
84 .size = 0,
85 },
86 .parameters.rsaDetail = {
87 .symmetric = {
88 .algorithm = TPM2_ALG_NULL,
89 .keyBits.aes = 128,
90 .mode.aes = TPM2_ALG_ECB,
91 },
92 .scheme = {
93 .scheme = TPM2_ALG_RSASSA,
94 .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA1 }},
95
96 },
97 .keyBits = 2048,
98 .exponent = 0,
99 },
100 .unique.rsa = {
101 .size = 0,
102 .buffer = {},
103 },
104 },
105 };
106
107 TPM2B_AUTH authValue = {
108 .size = 0,
109 .buffer = {}
110 };
111
112 TPM2B_DATA outsideInfo = {
113 .size = 0,
114 .buffer = {},
115 };
116
117 TPML_PCR_SELECTION creationPCR = {
118 .count = 0,
119 };
120
121 r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
122 goto_if_error(r, "Error: TR_SetAuth", error);
123
124 ESYS_TR signHandle;
125 TPM2B_PUBLIC *outPublic;
126 TPM2B_CREATION_DATA *creationData;
127 TPM2B_DIGEST *creationHash;
128 TPMT_TK_CREATION *creationTicket;
129
130 r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
131 ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
132 &inPublic, &outsideInfo, &creationPCR,
133 &signHandle, &outPublic, &creationData,
134 &creationHash, &creationTicket);
135 goto_if_error(r, "Error esys create primary", error);
136
137 /* Start a audit session */
138 TPMA_SESSION sessionAttributes = TPMA_SESSION_CONTINUESESSION |
139 TPMA_SESSION_AUDIT;
140 TPM2_SE sessionType = TPM2_SE_HMAC;
141 TPMI_ALG_HASH authHash = TPM2_ALG_SHA256;
142 TPM2B_NONCE *nonceTpm;
143 TPMT_SYM_DEF symmetric = {.algorithm = TPM2_ALG_NULL };
144 ESYS_TR session;
145
146 r = Esys_StartAuthSession(esys_context, ESYS_TR_NONE, ESYS_TR_NONE,
147 ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
148 NULL,
149 sessionType, &symmetric, authHash, &session,
150 &nonceTpm);
151
152 goto_if_error(r, "Error Esys_StartAuthSessiony", error);
153 r = Esys_TRSess_SetAttributes(esys_context, session, sessionAttributes,
154 0xff);
155 goto_if_error(r, "Error Esys_TRSess_SetAttributes", error);
156
157 /* Execute one command to be audited */
158 TPM2_CAP capability = TPM2_CAP_TPM_PROPERTIES;
159 UINT32 property = TPM2_PT_LOCKOUT_COUNTER;
160 UINT32 propertyCount = 1;
161 TPMS_CAPABILITY_DATA *capabilityData;
162 TPMI_YES_NO moreData;
163
164 r = Esys_GetCapability(esys_context,
165 session, ESYS_TR_NONE, ESYS_TR_NONE,
166 capability, property, propertyCount,
167 &moreData, &capabilityData);
168
169 goto_if_error(r, "Error esys get capability", error);
170
171 ESYS_TR privacyHandle = ESYS_TR_RH_ENDORSEMENT;
172 TPM2B_DATA qualifyingData = {0};
173 TPMT_SIG_SCHEME inScheme = { .scheme = TPM2_ALG_NULL };
174 TPM2B_ATTEST *auditInfo;
175 TPMT_SIGNATURE *signature;
176
177 /* Test the audit commands */
178 r = Esys_GetCommandAuditDigest(
179 esys_context,
180 privacyHandle,
181 signHandle,
182 ESYS_TR_PASSWORD,
183 ESYS_TR_PASSWORD,
184 ESYS_TR_NONE,
185 &qualifyingData,
186 &inScheme,
187 &auditInfo,
188 &signature);
189 goto_if_error(r, "Error: GetCommandAuditDigest", error);
190
191 r = Esys_GetSessionAuditDigest(
192 esys_context,
193 privacyHandle,
194 signHandle,
195 session,
196 ESYS_TR_PASSWORD,
197 ESYS_TR_PASSWORD,
198 ESYS_TR_NONE,
199 &qualifyingData,
200 &inScheme,
201 &auditInfo,
202 &signature);
203 goto_if_error(r, "Error: GetSessionAuditDigest", error);
204
205 TPMI_ALG_HASH auditAlg = TPM2_ALG_SHA1;
206 TPML_CC clearList = {0};
207 TPML_CC setList = {0};
208
209 r = Esys_SetCommandCodeAuditStatus(
210 esys_context,
211 ESYS_TR_RH_PLATFORM,
212 ESYS_TR_PASSWORD,
213 ESYS_TR_NONE,
214 ESYS_TR_NONE,
215 auditAlg,
216 &setList,
217 &clearList);
218 goto_if_error(r, "Error: SetCommandCodeAuditStatus", error);
219
220 r = Esys_FlushContext(esys_context, signHandle);
221 goto_if_error(r, "Error: FlushContext", error);
222
223 r = Esys_FlushContext(esys_context, session);
224 goto_if_error(r, "Error during FlushContext", error);
225
226 return 0;
227
228 error:
229 return 1;
230}