forked from orangecms/txt-suite
/
tpm.go
203 lines (172 loc) · 4.74 KB
/
tpm.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
197
198
199
200
201
202
203
package test
import (
"bytes"
"fmt"
"io"
tpm1 "github.com/google/go-tpm/tpm"
"github.com/google/go-tpm/tpm2"
"github.com/9elements/txt-suite/pkg/api"
)
const (
psIndex = 0x50000001
auxIndex = 0x50000003
)
var (
tpm12Connection *io.ReadWriteCloser = nil
tpm20Connection *io.ReadWriteCloser = nil
test16tpmispresent = Test{
Name: "TPM 1.2 present",
Required: true,
function: Test16TPMPresent,
}
test17tpmislocked = Test{
Name: "TPM in production mode",
function: Test17TPMIsLocked,
Required: false,
dependencies: []*Test{&test16tpmispresent},
}
test18psindexisset = Test{
Name: "PS index is set in NVRAM",
function: Test18PSIndexIsSet,
Required: true,
dependencies: []*Test{&test16tpmispresent},
}
test19auxindexisset = Test{
Name: "AUX index is set in NVRAM",
function: Test19AUXIndexIsSet,
Required: true,
dependencies: []*Test{&test16tpmispresent},
}
test20lcppolicyisvalid = Test{
Name: "PS index contains a valid LCP Policy",
function: Test20LCPPolicyIsValid,
Required: true,
dependencies: []*Test{&test16tpmispresent, &test18psindexisset},
}
TestsTPM = [...]*Test{
&test16tpmispresent,
&test17tpmislocked,
&test18psindexisset,
&test19auxindexisset,
&test20lcppolicyisvalid,
}
)
// Connects to a TPM device (virtual or real) at the given path
func ConnectTPM(tpmPath string) error {
conn, err := tpm2.OpenTPM(tpmPath)
if err != nil {
conn, err = tpm1.OpenTPM(tpmPath)
if err != nil {
return err
}
tpm12Connection = &conn
return nil
}
tpm20Connection = &conn
return nil
}
// Checks whether a TPM is present and answers to GetCapability
func Test16TPMPresent() (bool, error) {
if tpm12Connection != nil {
vid, err := tpm1.GetManufacturer(*tpm12Connection)
return vid != nil && err == nil, nil
} else if tpm20Connection != nil {
ca, _, err := tpm2.GetCapability(*tpm20Connection, tpm2.CapabilityTPMProperties, 1, uint32(tpm2.Manufacturer))
return ca != nil && err == nil, nil
} else {
return false, fmt.Errorf("No TPM connection")
}
}
// TPM NVRAM is locked
func Test17TPMIsLocked() (bool, error) {
if tpm12Connection != nil {
flags, err := tpm1.GetPermanentFlags(*tpm12Connection)
return flags.NvLocked, err
} else if tpm20Connection != nil {
return false, fmt.Errorf("Unimplemented: TPM 2.0")
} else {
return false, fmt.Errorf("No TPM connection")
}
}
// TPM NVRAM has a valid PS index
func Test18PSIndexIsSet() (bool, error) {
if tpm12Connection != nil {
data, err := tpm1.NVReadValueNoAuth(*tpm12Connection, psIndex, 0, 54)
if err != nil {
return false, err
}
return len(data) == 54, err
} else if tpm20Connection != nil {
meta, err := tpm2.NVReadPublic(*tpm20Connection, psIndex)
if err != nil {
return false, err
}
rc := true
rc = rc && meta.NVIndex == psIndex
rc = rc && (meta.Attributes&tpm2.KeyProp(tpm2.AttrWriteLocked) != 0)
return rc, nil
} else {
return false, fmt.Errorf("Not connected to TPM")
}
}
// TPM NVRAM has a valid AUX index
func Test19AUXIndexIsSet() (bool, error) {
if tpm12Connection != nil {
buf, err := tpm1.NVReadValueNoAuth(*tpm12Connection, auxIndex, 0, 1)
return len(buf) == 1, err
} else if tpm20Connection != nil {
meta, err := tpm2.NVReadPublic(*tpm20Connection, auxIndex)
if err != nil {
return false, err
}
return meta.NVIndex == auxIndex, nil
} else {
return false, fmt.Errorf("Not connected to TPM")
}
}
// PS index contains a valid LCP policy
func Test20LCPPolicyIsValid() (bool, error) {
var data []byte
var err error
if tpm12Connection != nil {
data, err = tpm1.NVReadValueNoAuth(*tpm12Connection, psIndex, 0, 54)
if err != nil {
return false, err
}
} else if tpm20Connection != nil {
data, err = tpm2.NVRead(*tpm20Connection, psIndex)
if err != nil {
return false, err
}
} else {
return false, fmt.Errorf("Not connected to TPM")
}
lcp, err := api.ParsePolicy(data)
if err != nil {
return false, err
}
return lcp.Version < 0x300, nil
}
// Reads PCR-00 and checks whether if it's not the EmptyDigest
func Test21PCR0IsSet() (bool, error) {
if tpm12Connection != nil {
pcr, err := tpm1.ReadPCR(*tpm12Connection, 0)
return bytes.Equal(pcr, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) && err == nil, err
} else if tpm20Connection != nil {
ca, _, err := tpm2.GetCapability(*tpm20Connection, tpm2.CapabilityPCRs, 1, 0)
if ca == nil || err != nil {
return false, err
}
for i := 0; i < 4; i++ {
pcr, _ := tpm2.ReadPCRs(*tpm20Connection, ca[i].(tpm2.PCRSelection))
for j := 0; j < len(pcr[0]); j++ {
if pcr[0][j] != 0 {
return false, nil
}
}
}
return true, nil
} else {
return false, fmt.Errorf("Not connected to TPM")
}
}