forked from canonical/secboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecboot_test.go
196 lines (170 loc) · 5.4 KB
/
secboot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package secboot_test
import (
"bytes"
"crypto"
"crypto/x509"
"flag"
"fmt"
"math/rand"
"os"
"testing"
"time"
"github.com/canonical/go-tpm2"
. "github.com/snapcore/secboot"
"github.com/snapcore/secboot/internal/testutil"
"golang.org/x/xerrors"
. "gopkg.in/check.v1"
)
var (
testCACert []byte
testEkCert []byte
testAuth = []byte("1234")
)
func Test(t *testing.T) { TestingT(t) }
// resetTPMSimulator executes reset sequence of the TPM (Shutdown(CLEAR) -> reset -> Startup(CLEAR)) and the re-initializes the
// TPMConnection.
func resetTPMSimulator(t *testing.T, tpm *TPMConnection, tcti *tpm2.TctiMssim) (*TPMConnection, *tpm2.TctiMssim) {
tpm, tcti, err := testutil.ResetTPMSimulator(tpm, tcti)
if err != nil {
t.Fatalf("%v", err)
}
return tpm, tcti
}
func openTPMSimulatorForTesting(t *testing.T) (*TPMConnection, *tpm2.TctiMssim) {
tpm, tcti, err := testutil.OpenTPMSimulatorForTesting()
if err != nil {
t.Fatalf("%v", err)
}
if tpm == nil {
t.SkipNow()
}
return tpm, tcti
}
func openTPMForTesting(t *testing.T) *TPMConnection {
tpm, err := testutil.OpenTPMForTesting()
if err != nil {
t.Fatalf("%v", err)
}
if tpm == nil {
t.SkipNow()
}
return tpm
}
// Flush a handle context. Fails the test if it doesn't succeed.
func flushContext(t *testing.T, tpm *TPMConnection, context tpm2.HandleContext) {
if err := tpm.FlushContext(context); err != nil {
t.Errorf("FlushContext failed: %v", err)
}
}
// Set the hierarchy auth to testAuth. Fatal on failure
func setHierarchyAuthForTest(t *testing.T, tpm *TPMConnection, hierarchy tpm2.ResourceContext) {
if err := tpm.HierarchyChangeAuth(hierarchy, testAuth, nil); err != nil {
t.Fatalf("HierarchyChangeAuth failed: %v", err)
}
}
// Reset the hierarchy auth to nil.
func resetHierarchyAuth(t *testing.T, tpm *TPMConnection, hierarchy tpm2.ResourceContext) {
if err := tpm.HierarchyChangeAuth(hierarchy, nil, nil); err != nil {
t.Errorf("HierarchyChangeAuth failed: %v", err)
}
}
// Undefine a NV index set by a test. Fails the test if it doesn't succeed.
func undefineNVSpace(t *testing.T, tpm *TPMConnection, context, authHandle tpm2.ResourceContext) {
if err := tpm.NVUndefineSpace(authHandle, context, nil); err != nil {
t.Errorf("NVUndefineSpace failed: %v", err)
}
}
func undefineKeyNVSpace(t *testing.T, tpm *TPMConnection, path string) {
k, err := ReadSealedKeyObject(path)
if err != nil {
t.Fatalf("ReadSealedKeyObject failed: %v", err)
}
h := k.PCRPolicyCounterHandle()
if h == tpm2.HandleNull {
return
}
rc, err := tpm.CreateResourceContextFromTPM(h)
if tpm2.IsResourceUnavailableError(err, h) {
return
}
if err != nil {
t.Fatalf("CreateResourceContextFromTPM failed: %v", err)
}
undefineNVSpace(t, tpm, rc, tpm.OwnerHandleContext())
}
// clearTPMWithPlatformAuth clears the TPM with platform hierarchy authorization - something that we can only do on the simulator
func clearTPMWithPlatformAuth(t *testing.T, tpm *TPMConnection) {
if err := tpm.ClearControl(tpm.PlatformHandleContext(), false, nil); err != nil {
t.Fatalf("ClearControl failed: %v", err)
}
if err := tpm.Clear(tpm.PlatformHandleContext(), nil); err != nil {
t.Fatalf("Clear failed: %v", err)
}
}
func closeTPM(t *testing.T, tpm *TPMConnection) {
if err := tpm.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
func TestMain(m *testing.M) {
flag.Parse()
rand.Seed(time.Now().UnixNano())
os.Exit(func() int {
if testutil.UseMssim {
simulatorCleanup, err := testutil.LaunchTPMSimulator(nil)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot launch TPM simulator: %v\n", err)
return 1
}
defer simulatorCleanup()
var caKey crypto.PrivateKey
testCACert, caKey, err = testutil.CreateTestCA()
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create test TPM CA certificate and private key: %v\n", err)
return 1
}
restoreCAHashes := testutil.TrustCA(testCACert)
defer restoreCAHashes()
if err := func() error {
tpm, _, err := testutil.OpenTPMSimulatorForTesting()
if err != nil {
return xerrors.Errorf("cannot open connection: %w", err)
}
defer tpm.Close()
testEkCert, err = testutil.CreateTestEKCert(tpm.TPMContext, testCACert, caKey)
if err != nil {
return xerrors.Errorf("cannot create test EK certificate: %w", err)
}
return testutil.CertifyTPM(tpm.TPMContext, testEkCert)
}(); err != nil {
fmt.Fprintf(os.Stderr, "Cannot certify TPM simulator: %v\n", err)
return 1
}
caCert, _ := x509.ParseCertificate(testCACert)
b := new(bytes.Buffer)
if err := EncodeEKCertificateChain(nil, []*x509.Certificate{caCert}, b); err != nil {
fmt.Fprintf(os.Stderr, "Cannot encode EK certificate chain: %v\n", err)
return 1
}
testutil.EncodedTPMSimulatorEKCertChain = b.Bytes()
}
return m.Run()
}())
}