/
test_helpers_test.go
193 lines (162 loc) · 4.75 KB
/
test_helpers_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
/*
Copyright (c) 2020 GMO GlobalSign, Inc.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tpmkeys_test
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"io"
"math/big"
"testing"
"time"
"github.com/google/go-tpm-tools/simulator"
"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpmutil"
)
const (
keyPassword = "xyzzy"
)
func rsaDecryptTemplate(scheme *tpm2.SigScheme) tpm2.Public {
return tpm2.Public{
Type: tpm2.AlgRSA,
NameAlg: tpm2.AlgSHA256,
Attributes: tpm2.FlagStorageDefault & ^tpm2.FlagRestricted,
RSAParameters: &tpm2.RSAParams{
Sign: scheme,
KeyBits: 2048,
},
}
}
func rsaSigningTemplate(scheme *tpm2.SigScheme) tpm2.Public {
return tpm2.Public{
Type: tpm2.AlgRSA,
NameAlg: tpm2.AlgSHA256,
Attributes: tpm2.FlagSignerDefault & ^tpm2.FlagRestricted,
RSAParameters: &tpm2.RSAParams{
Sign: scheme,
KeyBits: 2048,
},
}
}
func eccSigningTemplate(scheme *tpm2.SigScheme) tpm2.Public {
return tpm2.Public{
Type: tpm2.AlgECC,
NameAlg: tpm2.AlgSHA256,
Attributes: tpm2.FlagSignerDefault & ^tpm2.FlagRestricted,
ECCParameters: &tpm2.ECCParams{
Sign: scheme,
CurveID: tpm2.CurveNISTP256,
},
}
}
func createTLSCerts(t *testing.T, key crypto.PublicKey) (*x509.Certificate, *x509.Certificate) {
t.Helper()
now := time.Now()
cakey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("failed to generate CA key: %v", err)
}
catmpl := &x509.Certificate{
SerialNumber: big.NewInt(1983),
NotBefore: now,
NotAfter: now.Add(time.Hour * 24),
Subject: pkix.Name{CommonName: "TPM Test Issuer"},
KeyUsage: x509.KeyUsageCertSign,
BasicConstraintsValid: true,
IsCA: true,
}
caDER, err := x509.CreateCertificate(rand.Reader, catmpl, catmpl, cakey.Public(), cakey)
if err != nil {
t.Fatalf("failed to create CA certificate: %v", err)
}
cacert, err := x509.ParseCertificate(caDER)
if err != nil {
t.Fatalf("failed to parse CA certificate: %v", err)
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(1984),
NotBefore: now,
NotAfter: now.Add(time.Hour * 24),
Subject: pkix.Name{CommonName: "TPM Test Client"},
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, cacert, key, cakey)
if err != nil {
t.Fatalf("failed to create certificate: %v", err)
}
cert, err := x509.ParseCertificate(der)
if err != nil {
t.Fatalf("failed to parse certificate: %v", err)
}
return cert, cacert
}
func createAndLoadKey(t *testing.T, pub tpm2.Public) (io.ReadWriter, uint32, func()) {
t.Helper()
// Get simulated TPM.
tpm, err := simulator.Get()
if err != nil {
t.Fatalf("failed to get TPM simulator: %v", err)
}
var storagePub = tpm2.Public{
Type: tpm2.AlgRSA,
NameAlg: tpm2.AlgSHA256,
Attributes: tpm2.FlagStorageDefault,
RSAParameters: &tpm2.RSAParams{
Symmetric: &tpm2.SymScheme{
Alg: tpm2.AlgAES,
KeyBits: 128,
Mode: tpm2.AlgCFB,
},
KeyBits: 2048,
},
}
// Create storage key.
storage, _, _, _, _, _, err := tpm2.CreatePrimaryEx(tpm, tpm2.HandleOwner,
tpm2.PCRSelection{}, "", "", storagePub)
if err != nil {
flushTPM(t, tpm, 0, 0)
t.Fatalf("failed to create storage key: %v", err)
}
// Create and load key.
private, public, _, _, _, err := tpm2.CreateKey(tpm, storage,
tpm2.PCRSelection{}, "", keyPassword, pub)
if err != nil {
flushTPM(t, tpm, storage, 0)
t.Fatalf("failed to create key: %v", err)
}
handle, _, err := tpm2.Load(tpm, storage, "", public, private)
if err != nil {
flushTPM(t, tpm, storage, 0)
t.Fatalf("failed to load key: %v", err)
}
return tpm, uint32(handle), func() { flushTPM(t, tpm, storage, handle) }
}
func flushTPM(t *testing.T, tpm io.ReadWriteCloser, storage, handle tpmutil.Handle) {
t.Helper()
if handle != 0 {
if err := tpm2.FlushContext(tpm, handle); err != nil {
t.Errorf("failed to flush key: %v", err)
}
}
if storage != 0 {
if err := tpm2.FlushContext(tpm, storage); err != nil {
t.Errorf("failed to flush storage key: %v", err)
}
}
if err := tpm.Close(); err != nil {
t.Errorf("failed to close TPM: %v", err)
}
}