Debian Codegraph @

tpm2/test/get_time_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "testing"
6
7 . "github.com/google/go-tpm/tpm2"
8 "github.com/google/go-tpm/tpm2/transport/simulator"
9 )
10
11 func TestGetTime(t *testing.T) {
12 thetpm, err := simulator.OpenSimulator()
13 if err != nil {
14 t.Fatalf("could not connect to TPM simulator: %v", err)
15 }
16 defer thetpm.Close()
17
18 createPrimary := CreatePrimary{
19 PrimaryHandle: TPMRHEndorsement,
20 InPublic: New2B(TPMTPublic{
21 Type: TPMAlgRSA,
22 NameAlg: TPMAlgSHA256,
23 ObjectAttributes: TPMAObject{
24 SignEncrypt: true,
25 FixedTPM: true,
26 FixedParent: true,
27 SensitiveDataOrigin: true,
28 UserWithAuth: true,
29 },
30 Parameters: NewTPMUPublicParms(
31 TPMAlgRSA,
32 &TPMSRSAParms{
33 Scheme: TPMTRSAScheme{
34 Scheme: TPMAlgRSASSA,
35 Details: NewTPMUAsymScheme(
36 TPMAlgRSASSA,
37 &TPMSSigSchemeRSASSA{
38 HashAlg: TPMAlgSHA256,
39 },
40 ),
41 },
42 KeyBits: 2048,
43 },
44 ),
45 }),
46 }
47
48 rspCP, err := createPrimary.Execute(thetpm)
49 if err != nil {
50 t.Fatalf("could not create key: %v", err)
51 }
52
53 flushContext := FlushContext{FlushHandle: rspCP.ObjectHandle}
54 defer flushContext.Execute(thetpm)
55
56 qualifyingData := []byte("migrationpains")
57
58 getTimeCommand := GetTime{
59 PrivacyAdminHandle: TPMRHEndorsement,
60 SignHandle: NamedHandle{Handle: rspCP.ObjectHandle, Name: rspCP.Name},
61 QualifyingData: TPM2BData{Buffer: qualifyingData},
62 }
63 getTimeResponse, err := getTimeCommand.Execute(thetpm)
64 if err != nil {
65 t.Fatalf("GetTime failed: %v", err)
66 }
67
68 tpmsAttest, err := getTimeResponse.TimeInfo.Contents()
69
70 if err != nil {
71 t.Fatalf("failed to unmarshal response: %v", err)
72 }
73
74 tpmsTimeAttestInfo, err := tpmsAttest.Attested.Time()
75
76 if err != nil {
77 t.Fatalf("union typed field did not have expected concrete type: %v", err)
78 }
79
80 if tpmsTimeAttestInfo.Time.ClockInfo.Clock != tpmsAttest.ClockInfo.Clock {
81 t.Errorf("clockInfo does not match in tpmsTimeAttestInfo (%x) vs tpmsAttest (%x)",
82 tpmsTimeAttestInfo.Time.ClockInfo.Clock, tpmsAttest.ClockInfo.Clock)
83 }
84
85 if tpmsTimeAttestInfo.Time.ClockInfo.ResetCount != tpmsAttest.ClockInfo.ResetCount {
86 t.Errorf("resetCount does not match in tpmsTimeAttestInfo (%x) vs tpmsAttest (%x)",
87 tpmsTimeAttestInfo.Time.ClockInfo.ResetCount, tpmsAttest.ClockInfo.ResetCount)
88 }
89
90 if tpmsTimeAttestInfo.Time.ClockInfo.RestartCount != tpmsAttest.ClockInfo.RestartCount {
91 t.Errorf("restartCount does not match in tpmsTimeAttestInfo (%x) vs tpmsAttest (%x)",
92 tpmsTimeAttestInfo.Time.ClockInfo.RestartCount, tpmsAttest.ClockInfo.RestartCount)
93 }
94
95 if !bytes.Equal(tpmsAttest.ExtraData.Buffer, qualifyingData) {
96 t.Errorf("extraData does not match. Saw (%v), expected (%v)",
97 tpmsAttest.ExtraData.Buffer,
98 qualifyingData)
99 }
100 }