Debian Codegraph @

tpm2/test/ek_test.go

raw
1 package tpm2test
2
3 import (
4 "errors"
5 "testing"
6
7 . "github.com/google/go-tpm/tpm2"
8 "github.com/google/go-tpm/tpm2/transport"
9 "github.com/google/go-tpm/tpm2/transport/simulator"
10 )
11
12 // Test creating a sealed data blob on the standard-template EK using its policy.
13 func TestEKPolicy(t *testing.T) {
14 templates := map[string]TPMTPublic{
15 "RSA": RSAEKTemplate,
16 "ECC": ECCEKTemplate,
17 }
18
19 // Run the whole test for each of RSA and ECC EKs.
20 for name, ekTemplate := range templates {
21 t.Run(name, func(t *testing.T) {
22 ekTest(t, ekTemplate)
23 })
24 }
25 }
26
27 func ekPolicy(t transport.TPM, handle TPMISHPolicy, nonceTPM TPM2BNonce) error {
28 cmd := PolicySecret{
29 AuthHandle: TPMRHEndorsement,
30 PolicySession: handle,
31 NonceTPM: nonceTPM,
32 }
33 _, err := cmd.Execute(t)
34 return err
35 }
36
37 // This function tests a lot of combinations of authorizing the EK policy.
38 func ekTest(t *testing.T, ekTemplate TPMTPublic) {
39 type ekTestCase struct {
40 name string
41 // Use Policy instead of PolicySession, passing the callback instead of
42 // managing it ourselves?
43 jitPolicySession bool
44 // Use the policy session for decrypt? (Incompatible with decryptAnotherSession)
45 decryptPolicySession bool
46 // Use another session for decrypt? (Incompatible with decryptPolicySession)
47 decryptAnotherSession bool
48 // Use a bound session?
49 bound bool
50 // Use a salted session?
51 salted bool
52 }
53 var cases []ekTestCase
54 for jit := 0; jit < 2; jit++ {
55 for decryptPol := 0; decryptPol < 2; decryptPol++ {
56 for decryptAnother := 0; decryptAnother < 2; decryptAnother++ {
57 if decryptPol != 0 && decryptAnother != 0 {
58 continue
59 }
60 for bound := 0; bound < 2; bound++ {
61 for salted := 0; salted < 2; salted++ {
62 nextCase := ekTestCase{
63 name: "test",
64 jitPolicySession: jit != 0,
65 decryptPolicySession: decryptPol != 0,
66 decryptAnotherSession: decryptAnother != 0,
67 bound: bound != 0,
68 salted: salted != 0,
69 }
70 if nextCase.jitPolicySession {
71 nextCase.name += "-jit"
72 } else {
73 nextCase.name += "-standalone"
74 }
75 if nextCase.decryptPolicySession {
76 nextCase.name += "-decrypt-same"
77 }
78 if nextCase.decryptAnotherSession {
79 nextCase.name += "-decrypt-another"
80 }
81 if nextCase.bound {
82 nextCase.name += "-bound"
83 }
84 if nextCase.salted {
85 nextCase.name += "-salted"
86 }
87 cases = append(cases, nextCase)
88 }
89 }
90 }
91 }
92 }
93
94 thetpm, err := simulator.OpenSimulator()
95 if err != nil {
96 t.Fatalf("could not connect to TPM simulator: %v", err)
97 }
98 defer thetpm.Close()
99
100 for _, c := range cases {
101 t.Run(c.name, func(t *testing.T) {
102 // Create the EK
103 createEKCmd := CreatePrimary{
104 PrimaryHandle: TPMRHEndorsement,
105 InPublic: New2B(ekTemplate),
106 }
107 createEKRsp, err := createEKCmd.Execute(thetpm)
108 if err != nil {
109 t.Fatalf("%v", err)
110 }
111 outPub, err := createEKRsp.OutPublic.Contents()
112 if err != nil {
113 t.Fatalf("%v", err)
114 }
115 switch outPub.Type {
116 case TPMAlgRSA:
117 rsa, err := outPub.Unique.RSA()
118 if err != nil {
119 t.Fatalf("%v", err)
120 }
121 t.Logf("EK pub:\n%x\n", rsa.Buffer)
122 case TPMAlgECC:
123 ecc, err := outPub.Unique.ECC()
124 if err != nil {
125 t.Fatalf("%v", err)
126 }
127 t.Logf("EK pub:\n%x\n%x\n", ecc.X, ecc.Y)
128 }
129 t.Logf("EK name: %x", createEKRsp.Name)
130 defer func() {
131 // Flush the EK
132 flush := FlushContext{FlushHandle: createEKRsp.ObjectHandle}
133 if _, err := flush.Execute(thetpm); err != nil {
134 t.Errorf("%v", err)
135 }
136 }()
137
138 // Exercise the EK's auth policy (PolicySecret[RH_ENDORSEMENT])
139 // by creating an object under it
140 data := []byte("secrets")
141 createBlobCmd := Create{
142 ParentHandle: NamedHandle{
143 Handle: createEKRsp.ObjectHandle,
144 Name: createEKRsp.Name,
145 },
146 InSensitive: TPM2BSensitiveCreate{
147 Sensitive: &TPMSSensitiveCreate{
148 Data: NewTPMUSensitiveCreate(&TPM2BSensitiveData{
149 Buffer: data,
150 }),
151 },
152 },
153 InPublic: New2B(TPMTPublic{
154 Type: TPMAlgKeyedHash,
155 NameAlg: TPMAlgSHA256,
156 ObjectAttributes: TPMAObject{
157 FixedTPM: true,
158 FixedParent: true,
159 UserWithAuth: true,
160 NoDA: true,
161 },
162 }),
163 }
164
165 var sessions []Session
166 if c.decryptAnotherSession {
167 sessions = append(sessions, HMAC(TPMAlgSHA1, 16, AESEncryption(128, EncryptIn)))
168 }
169
170 var options []AuthOption
171 if c.decryptPolicySession {
172 options = append(options, AESEncryption(128, EncryptIn))
173 }
174 if c.bound {
175 options = append(options, Bound(createEKRsp.ObjectHandle, createEKRsp.Name, nil))
176 }
177 if c.salted {
178 options = append(options, Salted(createEKRsp.ObjectHandle, *outPub))
179 }
180
181 var s Session
182 if c.jitPolicySession {
183 // Use the convenience function to pass a policy callback.
184 s = Policy(TPMAlgSHA256, 16, ekPolicy, options...)
185 } else {
186 // Set up a session we have to execute and clean up ourselves.
187 var cleanup func() error
188 var err error
189 s, cleanup, err = PolicySession(thetpm, TPMAlgSHA256, 16, options...)
190 if err != nil {
191 t.Fatalf("creating session: %v", err)
192 }
193 // Clean up the session at the end of the test.
194 defer func() {
195 if err := cleanup(); err != nil {
196 t.Fatalf("cleaning up policy session: %v", err)
197 }
198 }()
199 // Execute the same callback ourselves.
200 if err = ekPolicy(thetpm, s.Handle(), s.NonceTPM()); err != nil {
201 t.Fatalf("executing EK policy: %v", err)
202 }
203 }
204 createBlobCmd.ParentHandle = AuthHandle{
205 Handle: createEKRsp.ObjectHandle,
206 Name: createEKRsp.Name,
207 Auth: s,
208 }
209
210 if _, err := createBlobCmd.Execute(thetpm, sessions...); err != nil {
211 t.Fatalf("%v", err)
212 }
213
214 if !c.jitPolicySession {
215 // If we're not using a "just-in-time" session with a callback,
216 // we have to re-initialize the session.
217 if err = ekPolicy(thetpm, s.Handle(), s.NonceTPM()); err != nil {
218 t.Fatalf("executing EK policy: %v", err)
219 }
220 }
221
222 // Try again and make sure it succeeds again.
223 if _, err = createBlobCmd.Execute(thetpm, sessions...); err != nil {
224 t.Fatalf("%v", err)
225 }
226
227 if !c.jitPolicySession {
228 // Finally, for non-JIT policy sessions, make sure we fail if
229 // we don't re-initialize the session.
230 // This is because after using a policy session, it's as if
231 // PolicyRestart was called.
232 _, err = createBlobCmd.Execute(thetpm, sessions...)
233 if !errors.Is(err, TPMRCPolicyFail) {
234 t.Errorf("want TPM_RC_POLICY_FAIL, got %v", err)
235 }
236 var fmt1 TPMFmt1Error
237 if !errors.As(err, &fmt1) {
238 t.Errorf("want a Fmt1Error, got %v", err)
239 } else if isSession, session := fmt1.Session(); !isSession || session != 1 {
240 t.Errorf("want TPM_RC_POLICY_FAIL on session 1, got %v", err)
241 }
242 }
243 })
244 }
245
246 }