Debian Codegraph @

tpm2/test/audit_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "crypto/sha256"
6 "testing"
7
8 . "github.com/google/go-tpm/tpm2"
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 func TestAuditSession(t *testing.T) {
13 thetpm, err := simulator.OpenSimulator()
14 if err != nil {
15 t.Fatalf("could not connect to TPM simulator: %v", err)
16 }
17 defer thetpm.Close()
18
19 // Create the audit session
20 sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA256, 16, Audit())
21 if err != nil {
22 t.Fatalf("%v", err)
23 }
24 defer cleanup()
25
26 // Create the AK for audit
27 createAKCmd := CreatePrimary{
28 PrimaryHandle: TPMRHOwner,
29 InPublic: New2B(TPMTPublic{
30 Type: TPMAlgECC,
31 NameAlg: TPMAlgSHA256,
32 ObjectAttributes: TPMAObject{
33 FixedTPM: true,
34 STClear: false,
35 FixedParent: true,
36 SensitiveDataOrigin: true,
37 UserWithAuth: true,
38 AdminWithPolicy: false,
39 NoDA: true,
40 EncryptedDuplication: false,
41 Restricted: true,
42 Decrypt: false,
43 SignEncrypt: true,
44 },
45 Parameters: NewTPMUPublicParms(
46 TPMAlgECC,
47 &TPMSECCParms{
48 Scheme: TPMTECCScheme{
49 Scheme: TPMAlgECDSA,
50 Details: NewTPMUAsymScheme(
51 TPMAlgECDSA,
52 &TPMSSigSchemeECDSA{
53 HashAlg: TPMAlgSHA256,
54 },
55 ),
56 },
57 CurveID: TPMECCNistP256,
58 },
59 ),
60 },
61 ),
62 }
63 createAKRsp, err := createAKCmd.Execute(thetpm)
64 if err != nil {
65 t.Fatalf("%v", err)
66 }
67 defer func() {
68 // Flush the AK
69 flush := FlushContext{FlushHandle: createAKRsp.ObjectHandle}
70 if _, err := flush.Execute(thetpm); err != nil {
71 t.Errorf("%v", err)
72 }
73 }()
74
75 audit, err := NewAudit(TPMAlgSHA256)
76 if err != nil {
77 t.Fatalf("%v", err)
78 }
79 audit2, err := NewAudit(TPMAlgSHA256)
80 if err != nil {
81 t.Fatalf("%v", err)
82 }
83 var accumulatedDigest []byte
84 if h, err := TPMAlgSHA256.Hash(); err == nil {
85 accumulatedDigest = make([]byte, h.Size())
86 } else {
87 t.Fatalf("TPMAlgSHA256.Hash(): %v", err)
88 }
89 // Call GetCapability a bunch of times with the audit session and make sure it extends like
90 // we expect it to.
91 props := []TPMPT{
92 TPMPTFamilyIndicator,
93 TPMPTLevel,
94 TPMPTRevision,
95 TPMPTDayofYear,
96 TPMPTYear,
97 TPMPTManufacturer,
98 }
99 for _, prop := range props {
100 getCmd := GetCapability{
101 Capability: TPMCapTPMProperties,
102 Property: uint32(prop),
103 PropertyCount: 1,
104 }
105 getRsp, err := getCmd.Execute(thetpm, sess)
106 if err != nil {
107 t.Fatalf("%v", err)
108 }
109 if err := AuditCommand(audit, getCmd, getRsp); err != nil {
110 t.Fatalf("%v", err)
111 }
112
113 // mimic an external audit log
114 auditLog := struct {
115 command []byte
116 response []byte
117 }{}
118
119 cmdBytes, err := MarshalCommand(getCmd)
120 if err != nil {
121 t.Fatalf("MarshalCommand: %v", err)
122 }
123 rspBytes, err := MarshalResponse(getCmd, getRsp)
124 if err != nil {
125 t.Fatalf("MarshalResponse: %v", err)
126 }
127 auditLog.command = cmdBytes
128 auditLog.response = rspBytes
129
130 // Get the audit digest signed by the AK
131 getAuditCmd := GetSessionAuditDigest{
132 PrivacyAdminHandle: TPMRHEndorsement,
133 SignHandle: NamedHandle{
134 Handle: createAKRsp.ObjectHandle,
135 Name: createAKRsp.Name,
136 },
137 SessionHandle: sess.Handle(),
138 QualifyingData: TPM2BData{Buffer: []byte("foobar")},
139 }
140 getAuditRsp, err := getAuditCmd.Execute(thetpm)
141 if err != nil {
142 t.Fatalf("%v", err)
143 }
144 // TODO check the signature with the AK pub
145 attest, err := getAuditRsp.AuditInfo.Contents()
146 if err != nil {
147 t.Fatalf("%v", err)
148 }
149 aud, err := attest.Attested.SessionAudit()
150 if err != nil {
151 t.Fatalf("%v", err)
152 }
153 want := audit.Digest()
154 got := aud.SessionDigest.Buffer
155 if !bytes.Equal(want, got) {
156 t.Errorf("unexpected audit value:\ngot %x\nwant %x", got, want)
157 }
158
159 // This demonstrates that audit value can be replayed from an audit log
160 cmd, err := UnmarshalCommand[GetCapability](auditLog.command)
161 if err != nil {
162 t.Fatalf("UnmarshalCommand: %v", err)
163 }
164 rsp, err := UnmarshalResponse[GetCapabilityResponse](auditLog.response)
165 if err != nil {
166 t.Fatalf("UnmarshalResponse: %v", err)
167 }
168 if err := AuditCommand(audit2, cmd, rsp); err != nil {
169 t.Fatalf("AuditCommand: %v", err)
170 }
171 got2 := audit2.Digest()
172 if !bytes.Equal(want, got2) {
173 t.Errorf("unexpected audit value from replay:\ngot %x\nwant %x", got2, want)
174 }
175
176 // This demonstrates that MarshalCommand/MarshalResponse provide everything needed
177 cpHashFromBytes := sha256.Sum256(cmdBytes)
178 rpHashFromBytes := sha256.Sum256(rspBytes)
179
180 h := sha256.New()
181 h.Write(accumulatedDigest)
182 h.Write(cpHashFromBytes[:])
183 h.Write(rpHashFromBytes[:])
184 accumulatedDigest = h.Sum(nil)
185
186 if !bytes.Equal(want, accumulatedDigest) {
187 t.Errorf("unexpected audit value from direct hash reconstruction:\ngot %x\nwant %x", want, accumulatedDigest)
188 }
189 }
190
191 }
192
193 // TestAuditSessionWithCertify tests audit session with a more complex command (Certify)
194 // which has two AuthHandles
195 func TestAuditSessionWithCertify(t *testing.T) {
196 thetpm, err := simulator.OpenSimulator()
197 if err != nil {
198 t.Fatalf("could not connect to TPM simulator: %v", err)
199 }
200 defer thetpm.Close()
201
202 // Create the audit session
203 sess, cleanup, err := HMACSession(thetpm, TPMAlgSHA256, 16, Audit())
204 if err != nil {
205 t.Fatalf("%v", err)
206 }
207 defer cleanup()
208
209 Auth := []byte("password")
210
211 // Create the AK for audit
212 createAKCmd := CreatePrimary{
213 PrimaryHandle: TPMRHOwner,
214 InPublic: New2B(TPMTPublic{
215 Type: TPMAlgECC,
216 NameAlg: TPMAlgSHA256,
217 ObjectAttributes: TPMAObject{
218 FixedTPM: true,
219 STClear: false,
220 FixedParent: true,
221 SensitiveDataOrigin: true,
222 UserWithAuth: true,
223 AdminWithPolicy: false,
224 NoDA: true,
225 EncryptedDuplication: false,
226 Restricted: true,
227 Decrypt: false,
228 SignEncrypt: true,
229 },
230
231 Parameters: NewTPMUPublicParms(
232 TPMAlgECC,
233 &TPMSECCParms{
234 Scheme: TPMTECCScheme{
235 Scheme: TPMAlgECDSA,
236 Details: NewTPMUAsymScheme(
237 TPMAlgECDSA,
238 &TPMSSigSchemeECDSA{
239 HashAlg: TPMAlgSHA256,
240 },
241 ),
242 },
243 CurveID: TPMECCNistP256,
244 },
245 ),
246 }),
247 InSensitive: TPM2BSensitiveCreate{
248 Sensitive: &TPMSSensitiveCreate{
249 UserAuth: TPM2BAuth{
250 Buffer: Auth,
251 },
252 },
253 },
254 }
255
256 createAKRsp, err := createAKCmd.Execute(thetpm)
257 if err != nil {
258 t.Fatalf("%v", err)
259 }
260 defer func() {
261 // Flush the AK
262 flush := FlushContext{FlushHandle: createAKRsp.ObjectHandle}
263 if _, err := flush.Execute(thetpm); err != nil {
264 t.Errorf("%v", err)
265 }
266 }()
267
268 // Create a key to certify
269 createKeyCmd := CreatePrimary{
270 PrimaryHandle: TPMRHOwner,
271 InPublic: New2B(TPMTPublic{
272 Type: TPMAlgRSA,
273 NameAlg: TPMAlgSHA256,
274 ObjectAttributes: TPMAObject{
275 FixedTPM: true,
276 FixedParent: true,
277 SensitiveDataOrigin: true,
278 UserWithAuth: true,
279 Decrypt: true,
280 SignEncrypt: true,
281 },
282 Parameters: NewTPMUPublicParms(
283 TPMAlgRSA,
284 &TPMSRSAParms{
285 KeyBits: 2048,
286 },
287 ),
288 }),
289 }
290 createKeyRsp, err := createKeyCmd.Execute(thetpm)
291 if err != nil {
292 t.Fatalf("%v", err)
293 }
294 defer func() {
295 flush := FlushContext{FlushHandle: createKeyRsp.ObjectHandle}
296 if _, err := flush.Execute(thetpm); err != nil {
297 t.Errorf("%v", err)
298 }
299 }()
300
301 originalCmd := Certify{
302 ObjectHandle: AuthHandle{
303 Handle: createKeyRsp.ObjectHandle,
304 Name: createKeyRsp.Name,
305 Auth: PasswordAuth(nil),
306 },
307 SignHandle: AuthHandle{
308 Handle: createAKRsp.ObjectHandle,
309 Name: createAKRsp.Name,
310 Auth: PasswordAuth(Auth),
311 },
312 QualifyingData: TPM2BData{Buffer: []byte("test")},
313 InScheme: TPMTSigScheme{
314 Scheme: TPMAlgECDSA,
315 Details: NewTPMUSigScheme(
316 TPMAlgECDSA,
317 &TPMSSchemeHash{
318 HashAlg: TPMAlgSHA256,
319 },
320 ),
321 },
322 }
323 t.Logf("=== ORIGINAL COMMAND ===")
324 t.Logf("ObjectHandle: %#v", originalCmd.ObjectHandle)
325 t.Logf("SignHandle: %#v", originalCmd.SignHandle)
326
327 // Execute the command with audit session
328 originalRsp, err := originalCmd.Execute(thetpm, sess)
329 if err != nil {
330 t.Fatalf("%v", err)
331 }
332
333 // Calculate audit digest with the command/response
334 audit, err := NewAudit(TPMAlgSHA256)
335 if err != nil {
336 t.Fatalf("%v", err)
337 }
338 if err := AuditCommand(audit, originalCmd, originalRsp); err != nil {
339 t.Fatalf("AuditCommand: %v", err)
340 }
341
342 // Get the audit digest signed by the AK
343 getAuditCmd := GetSessionAuditDigest{
344 PrivacyAdminHandle: TPMRHEndorsement,
345 SignHandle: AuthHandle{
346 Handle: createAKRsp.ObjectHandle,
347 Name: createAKRsp.Name,
348 Auth: PasswordAuth(Auth),
349 },
350 SessionHandle: sess.Handle(),
351 QualifyingData: TPM2BData{Buffer: []byte("foobar")},
352 }
353 getAuditRsp, err := getAuditCmd.Execute(thetpm)
354 if err != nil {
355 t.Fatalf("%v", err)
356 }
357
358 // Verify the TPM's audit digest matches our calculated digest
359 attest, err := getAuditRsp.AuditInfo.Contents()
360 if err != nil {
361 t.Fatalf("%v", err)
362 }
363 aud, err := attest.Attested.SessionAudit()
364 if err != nil {
365 t.Fatalf("%v", err)
366 }
367 want := aud.SessionDigest.Buffer
368 got := audit.Digest()
369 if !bytes.Equal(want, got) {
370 t.Errorf("TPM audit digest doesn't match calculated digest:\nTPM: %x\ncalculated: %x", want, got)
371 }
372
373 // Marshal the command and response
374 cmdBytes, err := MarshalCommand(originalCmd)
375 if err != nil {
376 t.Fatalf("MarshalCommand: %v", err)
377 }
378
379 rspBytes, err := MarshalResponse(originalCmd, originalRsp)
380 if err != nil {
381 t.Fatalf("MarshalResponse: %v", err)
382 }
383
384 // Unmarshal the command
385 unmarshalledCmd, err := UnmarshalCommand[Certify](cmdBytes)
386 if err != nil {
387 t.Fatalf("UnmarshalCommand: %v", err)
388 }
389
390 t.Logf("=== UNMARSHALLED COMMAND ===")
391 t.Logf("ObjectHandle: %#v", unmarshalledCmd.ObjectHandle)
392 t.Logf("SignHandle: %#v", unmarshalledCmd.SignHandle)
393
394 // Unmarshal the response
395 unmarshalledRsp, err := UnmarshalResponse[CertifyResponse](rspBytes)
396 if err != nil {
397 t.Fatalf("UnmarshalResponse: %v", err)
398 }
399
400 // Calculate audit digest with unmarshalled command/response
401 audit2, err := NewAudit(TPMAlgSHA256)
402 if err != nil {
403 t.Fatalf("%v", err)
404 }
405 if err := AuditCommand(audit2, unmarshalledCmd, unmarshalledRsp); err != nil {
406 t.Fatalf("AuditCommand (unmarshalled): %v", err)
407 }
408 got2 := audit2.Digest()
409
410 // Verify unmarshalled digest matches the original calculated digest
411 if !bytes.Equal(want, got2) {
412 t.Errorf("unmarshalled audit digest doesn't match original:\noriginal: %x\nunmarshalled: %x", want, got2)
413 }
414 }