Debian Codegraph @

tpm2/test/object_change_auth_test.go

raw
1 package tpm2test
2
3 import (
4 "bytes"
5 "errors"
6 "testing"
7
8 . "github.com/google/go-tpm/tpm2"
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 func TestObjectChangeAuth(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 SRK
20 // Put a password on the SRK to test more of the flows.
21 createSRKCmd := CreatePrimary{
22 PrimaryHandle: TPMRHOwner,
23 InSensitive: TPM2BSensitiveCreate{
24 Sensitive: &TPMSSensitiveCreate{
25 UserAuth: TPM2BAuth{
26 Buffer: []byte(nil),
27 },
28 },
29 },
30 InPublic: New2B(ECCSRKTemplate),
31 }
32 createSRKRsp, err := createSRKCmd.Execute(thetpm)
33 if err != nil {
34 t.Fatalf("%v", err)
35 }
36 defer func() {
37 // Flush the SRK
38 flushSRKCmd := FlushContext{FlushHandle: createSRKRsp.ObjectHandle}
39 if _, err := flushSRKCmd.Execute(thetpm); err != nil {
40 t.Errorf("%v", err)
41 }
42 }()
43
44 // Data we are sealing
45 data := []byte("secrets")
46
47 // Original auth for the key
48 auth := []byte("oldauth")
49
50 // New auth we are changing to
51 newauth := []byte("newauth")
52
53 // Create a sealed blob under the SRK
54 createBlobCmd := Create{
55 ParentHandle: AuthHandle{
56 Handle: createSRKRsp.ObjectHandle,
57 Name: createSRKRsp.Name,
58 Auth: PasswordAuth(nil),
59 },
60 InSensitive: TPM2BSensitiveCreate{
61 Sensitive: &TPMSSensitiveCreate{
62 UserAuth: TPM2BAuth{
63 Buffer: auth,
64 },
65 Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{
66 Buffer: data,
67 }),
68 },
69 },
70 InPublic: New2B(TPMTPublic{
71 Type: TPMAlgKeyedHash,
72 NameAlg: TPMAlgSHA256,
73 ObjectAttributes: TPMAObject{
74 FixedTPM: true,
75 FixedParent: true,
76 UserWithAuth: true,
77 NoDA: true,
78 },
79 }),
80 }
81
82 var createBlobRsp *CreateResponse
83
84 t.Run("Create", func(t *testing.T) {
85 createBlobRsp, err = createBlobCmd.Execute(thetpm)
86 if err != nil {
87 t.Fatalf("%v", err)
88 }
89 })
90
91 loadBlobCmd := Load{
92 ParentHandle: AuthHandle{
93 Handle: createSRKRsp.ObjectHandle,
94 Name: createSRKRsp.Name,
95 Auth: PasswordAuth(nil),
96 },
97 InPrivate: createBlobRsp.OutPrivate,
98 InPublic: createBlobRsp.OutPublic,
99 }
100 loadBlobRsp, err := loadBlobCmd.Execute(thetpm)
101 if err != nil {
102 t.Fatalf("%v", err)
103 }
104 defer func() {
105 // Flush the blob
106 flushBlobCmd := FlushContext{FlushHandle: loadBlobRsp.ObjectHandle}
107 if _, err := flushBlobCmd.Execute(thetpm); err != nil {
108 t.Errorf("%v", err)
109 }
110 }()
111
112 unsealCmd := Unseal{
113 ItemHandle: NamedHandle{
114 Handle: loadBlobRsp.ObjectHandle,
115 Name: loadBlobRsp.Name,
116 },
117 }
118
119 // Unseal the blob with a password session
120 t.Run("WithPassword", func(t *testing.T) {
121 unsealCmd.ItemHandle = AuthHandle{
122 Handle: loadBlobRsp.ObjectHandle,
123 Name: loadBlobRsp.Name,
124 Auth: PasswordAuth(auth),
125 }
126 unsealRsp, err := unsealCmd.Execute(thetpm)
127 if err != nil {
128 t.Errorf("%v", err)
129 }
130 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
131 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
132 }
133 })
134
135 // Change the auth of the object
136 t.Run("ObjectChangeAuth", func(t *testing.T) {
137 oca := ObjectChangeAuth{
138 ObjectHandle: AuthHandle{
139 Handle: loadBlobRsp.ObjectHandle,
140 Name: loadBlobRsp.Name,
141 Auth: PasswordAuth(auth),
142 },
143 ParentHandle: NamedHandle{
144 Handle: createSRKRsp.ObjectHandle,
145 Name: createSRKRsp.Name,
146 },
147 NewAuth: TPM2BAuth{
148 Buffer: newauth,
149 },
150 }
151
152 ocaRsp, err := oca.Execute(thetpm)
153 if err != nil {
154 t.Fatalf("failed objectchangeauthrequest: %v", err)
155 }
156
157 // Flush the old handle
158 flushBlobCmd := FlushContext{FlushHandle: loadBlobRsp.ObjectHandle}
159 if _, err := flushBlobCmd.Execute(thetpm); err != nil {
160 t.Errorf("%v", err)
161 }
162
163 // Load the new private blob, and the old public blob
164 loadBlobCmd := Load{
165 ParentHandle: AuthHandle{
166 Handle: createSRKRsp.ObjectHandle,
167 Name: createSRKRsp.Name,
168 Auth: PasswordAuth(nil),
169 },
170 InPrivate: ocaRsp.OutPrivate,
171 InPublic: createBlobRsp.OutPublic,
172 }
173 loadBlobRsp, err = loadBlobCmd.Execute(thetpm)
174 if err != nil {
175 t.Fatalf("%v", err)
176 }
177 })
178
179 // Unseal the blob with a password session
180 t.Run("WithOldPassword", func(t *testing.T) {
181 unsealCmd.ItemHandle = AuthHandle{
182 Handle: loadBlobRsp.ObjectHandle,
183 Name: loadBlobRsp.Name,
184 Auth: PasswordAuth(auth),
185 }
186 _, err := unsealCmd.Execute(thetpm)
187 if !errors.Is(err, TPMRCBadAuth) {
188 t.Errorf("want TPM_RC_BAD_AUTH, got %v", err)
189 }
190 var fmt1 TPMFmt1Error
191 if !errors.As(err, &fmt1) {
192 t.Errorf("want a Fmt1Error, got %v", err)
193 } else if isSession, session := fmt1.Session(); !isSession || session != 1 {
194 t.Errorf("want TPM_RC_BAD_AUTH on session 1, got %v", err)
195 }
196 })
197
198 t.Run("WithNewPassword", func(t *testing.T) {
199 unsealCmd.ItemHandle = AuthHandle{
200 Handle: loadBlobRsp.ObjectHandle,
201 Name: loadBlobRsp.Name,
202 Auth: PasswordAuth(newauth),
203 }
204 unsealRsp, err := unsealCmd.Execute(thetpm)
205 if err != nil {
206 t.Errorf("%v", err)
207 }
208 if !bytes.Equal(unsealRsp.OutData.Buffer, data) {
209 t.Errorf("want %x got %x", data, unsealRsp.OutData.Buffer)
210 }
211 })
212 }