Debian Codegraph @

attest/example_test.go

raw
1 package attest_test
2
3 import (
4 "crypto/subtle"
5 "flag"
6 "log"
7 "testing"
8
9 "github.com/google/go-attestation/attest"
10 )
11
12 var (
13 testExamples = flag.Bool("test-examples", false, "Enable tests for examples.")
14 )
15
16 func ExampleAK() {
17 tpm, err := attest.OpenTPM(nil)
18 if err != nil {
19 log.Fatalf("Failed to open the TPM: %v", err)
20 }
21 defer tpm.Close()
22
23 // Create a new AK.
24 ak, err := tpm.NewAK(nil)
25 if err != nil {
26 log.Fatalf("Failed to create AK: %v", err)
27 }
28 // Save a re-loadable representation to blob.
29 blob, err := ak.Marshal()
30 if err != nil {
31 log.Fatalf("Failed to marshal AK: %v", err)
32 }
33 // Close our handle to the AK.
34 if err := ak.Close(tpm); err != nil {
35 log.Fatalf("Failed to close AK: %v", err)
36 }
37
38 // Re-load the created AK from the blob.
39 ak, err = tpm.LoadAK(blob)
40 if err != nil {
41 log.Fatalf("Failed to load AK: %v", err)
42 }
43 if err := ak.Close(tpm); err != nil {
44 log.Fatalf("Failed to close AK: %v", err)
45 }
46 }
47
48 func ExampleAK_credentialActivation() {
49 tpm, err := attest.OpenTPM(nil)
50 if err != nil {
51 log.Fatalf("Failed to open TPM: %v", err)
52 }
53 defer tpm.Close()
54
55 // Create a new AK.
56 ak, err := tpm.NewAK(nil)
57 if err != nil {
58 log.Fatalf("Failed to create AK: %v", err)
59 }
60 defer ak.Close(tpm)
61
62 // Read the EK.
63 ek, err := tpm.EKs()
64 if err != nil {
65 log.Fatalf("Failed to enumerate EKs: %v", err)
66 }
67
68 // Read parameters necessary to generate a challenge.
69 ap := ak.AttestationParameters()
70
71 // Generate a credential activation challenge (usually done on the server).
72 activation := attest.ActivationParameters{
73 EK: ek[0].Public,
74 AK: ap,
75 }
76 secret, challenge, err := activation.Generate()
77 if err != nil {
78 log.Fatalf("Failed to generate activation challenge: %v", err)
79 }
80
81 // Challenge the AK & EK properties to recieve the decrypted secret.
82 decrypted, err := ak.ActivateCredential(tpm, *challenge)
83 if err != nil {
84 log.Fatalf("Failed to activate credential: %v", err)
85 }
86
87 // Check that the AK completed the challenge (usually done on the server).
88 if subtle.ConstantTimeCompare(secret, decrypted) == 0 {
89 log.Fatal("Activation response did not match secret")
90 }
91 }
92
93 func ExampleAK_credentialActivationWithEK() {
94 tpm, err := attest.OpenTPM(nil)
95 if err != nil {
96 log.Fatalf("Failed to open TPM: %v", err)
97 }
98 defer tpm.Close()
99
100 // Create a new AK.
101 ak, err := tpm.NewAK(nil)
102 if err != nil {
103 log.Fatalf("Failed to create AK: %v", err)
104 }
105 defer ak.Close(tpm)
106
107 // Read the EK certificates.
108 ekCerts, err := tpm.EKCertificates()
109 if err != nil {
110 log.Fatalf("Failed to enumerate EKs: %v", err)
111 }
112
113 // Read parameters necessary to generate a challenge.
114 ap := ak.AttestationParameters()
115
116 // Try activating with each EK certificate.
117 for _, ek := range ekCerts {
118 // Generate a credential activation challenge (usually done on the server).
119 activation := attest.ActivationParameters{
120 EK: ek.Public,
121 AK: ap,
122 }
123 secret, challenge, err := activation.Generate()
124 if err != nil {
125 log.Fatalf("Failed to generate activation challenge: %v", err)
126 }
127
128 // Challenge the AK & EK properties to recieve the decrypted secret.
129 decrypted, err := ak.ActivateCredentialWithEK(tpm, *challenge, ek)
130 if err != nil {
131 log.Fatalf("Failed to activate credential: %v", err)
132 }
133
134 // Check that the AK completed the challenge (usually done on the server).
135 if subtle.ConstantTimeCompare(secret, decrypted) == 0 {
136 log.Fatal("Activation response did not match secret")
137 }
138 }
139 }
140
141 func TestExampleAK(t *testing.T) {
142 if !*testExamples {
143 t.SkipNow()
144 }
145 ExampleAK()
146 ExampleAK_credentialActivation()
147 ExampleAK_credentialActivationWithEK()
148 }
149
150 func TestExampleTPM(t *testing.T) {
151 if !*testExamples {
152 t.SkipNow()
153 }
154 ExampleTPM_AttestPlatform()
155 }
156
157 func ExampleTPM_AttestPlatform() {
158 tpm, err := attest.OpenTPM(nil)
159 if err != nil {
160 log.Fatalf("Failed to open TPM: %v", err)
161 }
162 defer tpm.Close()
163
164 // Create a new AK.
165 ak, err := tpm.NewAK(nil)
166 if err != nil {
167 log.Fatalf("Failed to create AK: %v", err)
168 }
169 defer ak.Close(tpm)
170
171 // The nonce would typically be provided by the server.
172 nonce := []byte{1, 2, 3, 4, 5, 6, 7, 8}
173
174 // Perform an attestation against the state of the plaform. Usually, you
175 // would pass a nil config, and the event log would be read from the
176 // platform. To ensure this example runs on platforms without event logs,
177 // we pass a fake EventLog value.
178 att, err := tpm.AttestPlatform(ak, nonce, &attest.PlatformAttestConfig{
179 EventLog: []byte{0},
180 })
181 if err != nil {
182 log.Fatalf("Failed to attest the platform state: %v", err)
183 }
184
185 // Construct an AKPublic struct from the parameters of the key. This
186 // will be used to verify the quote signatures.
187 pub, err := attest.ParseAKPublic(ak.AttestationParameters().Public)
188 if err != nil {
189 log.Fatalf("Failed to parse AK public: %v", err)
190 }
191
192 for i, q := range att.Quotes {
193 if err := pub.Verify(q, att.PCRs, nonce); err != nil {
194 log.Fatalf("quote[%d] verification failed: %v", i, err)
195 }
196 }
197 }