Debian Codegraph @

attest/tpm_windows.go

raw
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 //go:build windows
16 // +build windows
17
18 package attest
19
20 import (
21 "bytes"
22 "crypto/rand"
23 "crypto/rsa"
24 "encoding/binary"
25 "errors"
26 "fmt"
27 "math/big"
28
29 tpmtbs "github.com/google/go-tpm/tpmutil/tbs"
30 "golang.org/x/sys/windows"
31 )
32
33 var wellKnownAuth [20]byte
34
35 type windowsTPM struct {
36 pcp *winPCP
37 }
38
39 func probeSystemTPMs() ([]string, error) {
40 // Initialize Tbs.dll here so that it's linked only when TPM support is required.
41 if tbs == nil {
42 tbs = windows.MustLoadDLL("Tbs.dll")
43 tbsGetDeviceInfo = tbs.MustFindProc("Tbsi_GetDeviceInfo")
44 }
45
46 // Windows systems appear to only support a single abstracted TPM.
47 // If we fail to initialize the Platform Crypto Provider, we assume
48 // a TPM is not present.
49 pcp, err := openPCP()
50 if err != nil {
51 return nil, nil
52 }
53 defer pcp.Close()
54
55 info, err := pcp.TPMInfo()
56 if err != nil {
57 return nil, fmt.Errorf("TPMInfo() failed: %v", err)
58 }
59
60 if info.TBSInfo.TPMVersion != 2 {
61 return nil, fmt.Errorf("TBSInfo.TPMVersion %d unsupported", info.TBSInfo.TPMVersion)
62 }
63
64 return []string{""}, nil
65 }
66
67 func openTPM(_ string) (*TPM, error) {
68 pcp, err := openPCP()
69 if err != nil {
70 return nil, fmt.Errorf("openPCP() failed: %v", err)
71 }
72
73 info, err := pcp.TPMInfo()
74 if err != nil {
75 return nil, fmt.Errorf("TPMInfo() failed: %v", err)
76 }
77 if info.TBSInfo.TPMVersion != 2 {
78 return nil, fmt.Errorf("TBSInfo.TPMVersion %d unsupported", info.TBSInfo.TPMVersion)
79 }
80
81 return &TPM{tpm: &windowsTPM{
82 pcp: pcp,
83 }}, nil
84 }
85
86 func (t *windowsTPM) close() error {
87 return t.pcp.Close()
88 }
89
90 func (t *windowsTPM) info() (*TPMInfo, error) {
91 tInfo := TPMInfo{
92 Interface: TPMInterfaceKernelManaged,
93 }
94 tpm, err := t.pcp.TPMCommandInterface()
95 if err != nil {
96 return nil, err
97 }
98
99 t2Info, err := readVendorAttributes(tpm)
100 if err != nil {
101 return nil, err
102 }
103 tInfo.Manufacturer = t2Info.manufacturer
104 tInfo.VendorInfo = t2Info.vendor
105 tInfo.FirmwareVersionMajor = t2Info.fwMajor
106 tInfo.FirmwareVersionMinor = t2Info.fwMinor
107
108 return &tInfo, nil
109 }
110
111 func (t *windowsTPM) ekCertificates() ([]EK, error) {
112 ekCerts, err := t.pcp.EKCerts()
113 if err != nil {
114 return nil, fmt.Errorf("could not read EKCerts: %v", err)
115 }
116 var eks []EK
117 for _, cert := range ekCerts {
118 eks = append(eks, EK{Certificate: cert, Public: cert.PublicKey})
119 }
120 return eks, nil
121 }
122
123 func (t *windowsTPM) eks() ([]EK, error) {
124 ekCerts, err := t.pcp.EKCerts()
125 if err != nil {
126 return nil, fmt.Errorf("could not read EKCerts: %v", err)
127 }
128 if len(ekCerts) > 0 {
129 var eks []EK
130 for _, cert := range ekCerts {
131 eks = append(eks, EK{Certificate: cert, Public: cert.PublicKey})
132 }
133 return eks, nil
134 }
135
136 pub, err := t.ekPub()
137 if err != nil {
138 return nil, fmt.Errorf("could not read ek public key from tpm: %v", err)
139 }
140 ek := EK{Public: pub}
141
142 i, err := t.info()
143 if err != nil {
144 return nil, err
145 }
146 ek.CertificateURL = ekCertURL(pub, i.Manufacturer.String())
147 return []EK{ek}, nil
148 }
149
150 func (t *windowsTPM) ekPub() (*rsa.PublicKey, error) {
151 p, err := t.pcp.EKPub()
152 if err != nil {
153 return nil, fmt.Errorf("could not read ekpub: %v", err)
154 }
155 ekPub, err := decodeWindowsBcryptRSABlob(p)
156 if err != nil {
157 return nil, fmt.Errorf("could not decode ekpub: %v", err)
158 }
159 return ekPub, nil
160 }
161
162 type bcryptRSABlobHeader struct {
163 Magic uint32
164 BitLength uint32
165 ExponentLen uint32
166 ModulusLen uint32
167 Prime1Len uint32
168 Prime2Len uint32
169 }
170
171 func decodeWindowsBcryptRSABlob(b []byte) (*rsa.PublicKey, error) {
172 var (
173 r = bytes.NewReader(b)
174 header = &bcryptRSABlobHeader{}
175 exp = make([]byte, 8)
176 mod = []byte("")
177 )
178 if err := binary.Read(r, binary.LittleEndian, header); err != nil {
179 return nil, err
180 }
181
182 if header.Magic != 0x31415352 { // "RSA1"
183 return nil, fmt.Errorf("invalid header magic %x", header.Magic)
184 }
185 if header.ExponentLen > 8 {
186 return nil, errors.New("exponent too large")
187 }
188
189 if _, err := r.Read(exp[8-header.ExponentLen:]); err != nil {
190 return nil, fmt.Errorf("failed to read public exponent: %v", err)
191 }
192
193 mod = make([]byte, header.ModulusLen)
194 if n, err := r.Read(mod); n != int(header.ModulusLen) || err != nil {
195 return nil, fmt.Errorf("failed to read modulus (%d, %v)", n, err)
196 }
197
198 return &rsa.PublicKey{
199 N: new(big.Int).SetBytes(mod),
200 E: int(binary.BigEndian.Uint64(exp)),
201 }, nil
202 }
203
204 func (t *windowsTPM) newAK(opts *AKConfig) (*AK, error) {
205 var name string
206 var alg Algorithm
207
208 if opts != nil && opts.Name != "" {
209 name = opts.Name
210 } else {
211 nameHex := make([]byte, 5)
212 if n, err := rand.Read(nameHex); err != nil || n != len(nameHex) {
213 return nil, fmt.Errorf("rand.Read() failed with %d/%d bytes read and error: %v", n, len(nameHex), err)
214 }
215 name = fmt.Sprintf("ak-%x", nameHex)
216 }
217 if opts != nil && opts.Algorithm != "" {
218 alg = opts.Algorithm
219 } else {
220 // Default to RSA based AK.
221 alg = RSA
222 }
223
224 kh, err := t.pcp.NewAK(name, alg)
225 if err != nil {
226 return nil, fmt.Errorf("pcp failed to mint attestation key: %v", err)
227 }
228 props, err := t.pcp.AKProperties(kh)
229 if err != nil {
230 closeNCryptObject(kh)
231 return nil, fmt.Errorf("pcp failed to read attestation key properties: %v", err)
232 }
233
234 return &AK{ak: newWindowsKey20(kh, name, props.RawPublic, props.RawCreationData, props.RawAttest, props.RawSignature)}, nil
235 }
236
237 func (t *windowsTPM) loadAK(opaqueBlob []byte) (*AK, error) {
238 sKey, err := deserializeKey(opaqueBlob)
239 if err != nil {
240 return nil, fmt.Errorf("deserializeKey() failed: %v", err)
241 }
242 if sKey.Encoding != keyEncodingOSManaged {
243 return nil, fmt.Errorf("unsupported key encoding: %x", sKey.Encoding)
244 }
245
246 hnd, err := t.pcp.LoadKeyByName(sKey.Name)
247 if err != nil {
248 return nil, fmt.Errorf("pcp failed to load key: %v", err)
249 }
250
251 return &AK{ak: newWindowsKey20(hnd, sKey.Name, sKey.Public, sKey.CreateData, sKey.CreateAttestation, sKey.CreateSignature)}, nil
252 }
253
254 func (t *windowsTPM) loadAKWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*AK, error) {
255 return nil, fmt.Errorf("not implemented")
256 }
257
258 func (t *windowsTPM) newKey(*AK, *KeyConfig) (*Key, error) {
259 return nil, fmt.Errorf("not implemented")
260 }
261
262 func (t *windowsTPM) newKeyCertifiedByKey(ck certifyingKey, opts *KeyConfig) (*Key, error) {
263 return nil, fmt.Errorf("not implemented")
264 }
265
266 func (t *windowsTPM) loadKey(opaqueBlob []byte) (*Key, error) {
267 return nil, fmt.Errorf("not implemented")
268 }
269
270 func (t *windowsTPM) loadKeyWithParent(opaqueBlob []byte, parent ParentKeyConfig) (*Key, error) {
271 return nil, fmt.Errorf("not implemented")
272 }
273
274 func (t *windowsTPM) pcrbanks() ([]HashAlg, error) {
275 tpm, err := t.pcp.TPMCommandInterface()
276 if err != nil {
277 return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
278 }
279 return pcrbanks(tpm)
280 }
281
282 func (t *windowsTPM) pcrs(alg HashAlg) ([]PCR, error) {
283 var PCRs map[uint32][]byte
284
285 tpm, err := t.pcp.TPMCommandInterface()
286 if err != nil {
287 return nil, fmt.Errorf("TPMCommandInterface() failed: %v", err)
288 }
289 PCRs, err = readAllPCRs(tpm, alg.goTPMAlg())
290 if err != nil {
291 return nil, fmt.Errorf("failed to read PCRs: %v", err)
292 }
293
294 out := make([]PCR, len(PCRs))
295 for index, digest := range PCRs {
296 digestAlg, err := alg.cryptoHash()
297 if err != nil {
298 return nil, fmt.Errorf("unknown algorithm ID %x: %v", alg, err)
299 }
300 out[int(index)] = PCR{
301 Index: int(index),
302 Digest: digest,
303 DigestAlg: digestAlg,
304 }
305 }
306
307 return out, nil
308 }
309
310 func (t *windowsTPM) measurementLog() ([]byte, error) {
311 context, err := tpmtbs.CreateContext(tpmtbs.TPMVersion20, tpmtbs.IncludeTPM20)
312 if err != nil {
313 return nil, err
314 }
315 defer context.Close()
316
317 // Run command first with nil buffer to get required buffer size.
318 logLen, err := context.GetTCGLog(nil)
319 if err != nil {
320 return nil, err
321 }
322 logBuffer := make([]byte, logLen)
323 if _, err = context.GetTCGLog(logBuffer); err != nil {
324 return nil, err
325 }
326 return logBuffer, nil
327 }