Debian Codegraph @

tpm/structures.go

raw
1 // Copyright (c) 2014, Google LLC All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package tpm
16
17 import (
18 "bytes"
19 "crypto"
20 "crypto/rsa"
21 "encoding/binary"
22 "errors"
23 "fmt"
24 "io"
25 "math/big"
26
27 "github.com/google/go-tpm/tpmutil"
28 )
29
30 // A pcrValue is the fixed-size value of a PCR.
31 type pcrValue [20]byte
32
33 // PCRSize gives the fixed size (20 bytes) of a PCR.
34 const PCRSize int = 20
35
36 // A pcrMask represents a set of PCR choices, one bit per PCR out of the 24
37 // possible PCR values.
38 type pcrMask [3]byte
39
40 // A pcrSelection is the first element in the input a PCR composition, which is
41 // A pcrSelection, followed by the combined length of the PCR values,
42 // followed by the PCR values, all hashed under SHA-1.
43 type pcrSelection struct {
44 Size uint16
45 Mask pcrMask
46 }
47
48 // pcrInfoLong stores detailed information about PCRs.
49 type pcrInfoLong struct {
50 Tag uint16
51 LocAtCreation Locality
52 LocAtRelease Locality
53 PCRsAtCreation pcrSelection
54 PCRsAtRelease pcrSelection
55 DigestAtCreation Digest
56 DigestAtRelease Digest
57 }
58
59 // pcrInfoShort stores detailed information about PCRs.
60 type pcrInfoShort struct {
61 PCRsAtRelease pcrSelection
62 LocAtRelease Locality
63 DigestAtRelease Digest
64 }
65
66 type pcrInfo struct {
67 PcrSelection pcrSelection
68 DigestAtRelease Digest
69 DigestAtCreation Digest
70 }
71
72 type capVersionByte byte
73
74 // A capVersionInfo contains information about the TPM itself. Note that this
75 // is deserialized specially, since it has a variable-length byte array but no
76 // length. It is preceded with a length in the response to the Quote2 command.
77
78 type capVersion struct {
79 Major capVersionByte
80 Minor capVersionByte
81 RevMajor byte
82 RevMinor byte
83 }
84
85 // CapVersionInfo implements TPM_CAP_VERSION_INFO from spec. Part 2 - Page 174
86 type CapVersionInfo struct {
87 Tag tpmutil.Tag
88 Version capVersion
89 SpecLevel uint16
90 ErrataRev byte
91 TPMVendorID [4]byte
92 VendorSpecific tpmutil.U16Bytes
93 }
94
95 // Decode reads TPM_CAP_VERSION_INFO into CapVersionInfo.
96 func (c *CapVersionInfo) Decode(data []byte) error {
97 var cV capVersion
98 buf := bytes.NewReader(data)
99 err := binary.Read(buf, binary.LittleEndian, &c.Tag)
100 if err != nil {
101 return err
102 }
103 err = binary.Read(buf, binary.LittleEndian, &cV.Major)
104 if err != nil {
105 return err
106 }
107 err = binary.Read(buf, binary.LittleEndian, &cV.Minor)
108 if err != nil {
109 return err
110 }
111 err = binary.Read(buf, binary.LittleEndian, &cV.RevMajor)
112 if err != nil {
113 return err
114 }
115 err = binary.Read(buf, binary.LittleEndian, &cV.RevMinor)
116 if err != nil {
117 return err
118 }
119
120 c.Version = cV
121
122 err = binary.Read(buf, binary.LittleEndian, &c.SpecLevel)
123 if err != nil {
124 return err
125 }
126 err = binary.Read(buf, binary.LittleEndian, &c.ErrataRev)
127 if err != nil {
128 return err
129 }
130 err = binary.Read(buf, binary.LittleEndian, &c.TPMVendorID)
131 if err != nil {
132 return err
133 }
134 var venspecificLen uint16
135 err = binary.Read(buf, binary.LittleEndian, &venspecificLen)
136 if err != nil {
137 return err
138 }
139 venSpecData := make([]byte, venspecificLen)
140 err = binary.Read(buf, binary.LittleEndian, &venSpecData)
141 if err != nil {
142 return err
143 }
144 c.VendorSpecific = venSpecData
145
146 return nil
147
148 }
149
150 // PermanentFlags contains persistent TPM properties
151 type PermanentFlags struct {
152 Tag uint16
153 Disable bool
154 Ownership bool
155 Deactivated bool
156 ReadPubEK bool
157 DisableOwnerClear bool
158 AllowMaintenance bool
159 PhysicalPresenceLifetimeLock bool
160 PhysicalPresenceHWEnable bool
161 PhysicalPresenceCMDEnable bool
162 CEKPUsed bool
163 TPMPost bool
164 TPMPostLock bool
165 FIPS bool
166 Operator bool
167 EnableRevokeEK bool
168 NVLocked bool
169 ReadSRKPub bool
170 TPMEstablished bool
171 MaintenanceDone bool
172 DisableFullDALogicInfo bool
173 }
174
175 // nvAttributes implements struct of TPM_NV_ATTRIBUTES
176 // See: TPM-Main-Part-2-TPM-Structures_v1.2_rev116_01032011, P.140
177 type nvAttributes struct {
178 Tag uint16
179 Attributes Permission
180 }
181
182 // NVDataPublic implements the structure of TPM_NV_DATA_PUBLIC
183 // as described in TPM-Main-Part-2-TPM-Structures_v1.2_rev116_01032011, P. 142
184 type NVDataPublic struct {
185 Tag uint16
186 NVIndex uint32
187 PCRInfoRead pcrInfoShort
188 PCRInfoWrite pcrInfoShort
189 Permission nvAttributes
190 ReadSTClear bool
191 WriteSTClear bool
192 WriteDefine bool
193 Size uint32
194 }
195
196 // CloseKey flushes the key associated with the tpmutil.Handle.
197 func CloseKey(rw io.ReadWriter, h tpmutil.Handle) error {
198 return flushSpecific(rw, h, rtKey)
199 }
200
201 // A Nonce is a 20-byte value.
202 type Nonce [20]byte
203
204 // An oiapResponse is a response to an OIAP command.
205 type oiapResponse struct {
206 AuthHandle tpmutil.Handle
207 NonceEven Nonce
208 }
209
210 // String returns a string representation of an oiapResponse.
211 func (opr oiapResponse) String() string {
212 return fmt.Sprintf("oiapResponse{AuthHandle: %x, NonceEven: % x}", opr.AuthHandle, opr.NonceEven)
213 }
214
215 // Close flushes the auth handle associated with an OIAP session.
216 func (opr *oiapResponse) Close(rw io.ReadWriter) error {
217 return flushSpecific(rw, opr.AuthHandle, rtAuth)
218 }
219
220 // An osapCommand is a command sent for OSAP authentication.
221 type osapCommand struct {
222 EntityType uint16
223 EntityValue tpmutil.Handle
224 OddOSAP Nonce
225 }
226
227 // String returns a string representation of an osapCommand.
228 func (opc osapCommand) String() string {
229 return fmt.Sprintf("osapCommand{EntityType: %x, EntityValue: %x, OddOSAP: % x}", opc.EntityType, opc.EntityValue, opc.OddOSAP)
230 }
231
232 // An osapResponse is a TPM reply to an osapCommand.
233 type osapResponse struct {
234 AuthHandle tpmutil.Handle
235 NonceEven Nonce
236 EvenOSAP Nonce
237 }
238
239 // String returns a string representation of an osapResponse.
240 func (opr osapResponse) String() string {
241 return fmt.Sprintf("osapResponse{AuthHandle: %x, NonceEven: % x, EvenOSAP: % x}", opr.AuthHandle, opr.NonceEven, opr.EvenOSAP)
242 }
243
244 // Close flushes the AuthHandle associated with an OSAP session.
245 func (opr *osapResponse) Close(rw io.ReadWriter) error {
246 return flushSpecific(rw, opr.AuthHandle, rtAuth)
247 }
248
249 // A Digest is a 20-byte SHA1 value.
250 type Digest [20]byte
251
252 // An AuthValue is a 20-byte value used for authentication.
253 type authValue [20]byte
254
255 const authSize uint32 = 20
256
257 // A sealCommand is the command sent to the TPM to seal data.
258 type sealCommand struct {
259 KeyHandle tpmutil.Handle
260 EncAuth authValue
261 }
262
263 // String returns a string representation of a sealCommand.
264 func (sc sealCommand) String() string {
265 return fmt.Sprintf("sealCommand{KeyHandle: %x, EncAuth: % x}", sc.KeyHandle, sc.EncAuth)
266 }
267
268 // commandAuth stores the auth information sent with a command. Commands with
269 // tagRQUAuth1Command tags use one of these auth structures, and commands with
270 // tagRQUAuth2Command use two.
271 type commandAuth struct {
272 AuthHandle tpmutil.Handle
273 NonceOdd Nonce
274 ContSession byte
275 Auth authValue
276 }
277
278 // String returns a string representation of a sealCommandAuth.
279 func (ca commandAuth) String() string {
280 return fmt.Sprintf("commandAuth{AuthHandle: %x, NonceOdd: % x, ContSession: %x, Auth: % x}", ca.AuthHandle, ca.NonceOdd, ca.ContSession, ca.Auth)
281 }
282
283 // responseAuth contains the auth information returned from a command.
284 type responseAuth struct {
285 NonceEven Nonce
286 ContSession byte
287 Auth authValue
288 }
289
290 // String returns a string representation of a responseAuth.
291 func (ra responseAuth) String() string {
292 return fmt.Sprintf("responseAuth{NonceEven: % x, ContSession: %x, Auth: % x}", ra.NonceEven, ra.ContSession, ra.Auth)
293 }
294
295 // These are the parameters of a TPM key.
296 type keyParams struct {
297 AlgID Algorithm
298 EncScheme uint16
299 SigScheme uint16
300 Params tpmutil.U32Bytes // Serialized rsaKeyParams or symmetricKeyParams.
301 }
302
303 // An rsaKeyParams encodes the length of the RSA prime in bits, the number of
304 // primes in its factored form, and the exponent used for public-key
305 // encryption.
306 type rsaKeyParams struct {
307 KeyLength uint32
308 NumPrimes uint32
309 Exponent tpmutil.U32Bytes
310 }
311
312 type symmetricKeyParams struct {
313 KeyLength uint32
314 BlockSize uint32
315 IV tpmutil.U32Bytes
316 }
317
318 // A key is a TPM representation of a key.
319 type key struct {
320 Version uint32
321 KeyUsage uint16
322 KeyFlags KeyFlags
323 AuthDataUsage byte
324 AlgorithmParams keyParams
325 PCRInfo tpmutil.U32Bytes
326 PubKey tpmutil.U32Bytes
327 EncData tpmutil.U32Bytes
328 }
329
330 // A key12 is a newer TPM representation of a key.
331 type key12 struct {
332 Tag uint16
333 Zero uint16 // Always all 0.
334 KeyUsage uint16
335 KeyFlags uint32
336 AuthDataUsage byte
337 AlgorithmParams keyParams
338 PCRInfo tpmutil.U32Bytes // This must be a serialization of a pcrInfoLong.
339 PubKey tpmutil.U32Bytes
340 EncData tpmutil.U32Bytes
341 }
342
343 // A pubKey represents a public key known to the TPM.
344 type pubKey struct {
345 AlgorithmParams keyParams
346 Key tpmutil.U32Bytes
347 }
348
349 // A migrationKeyAuth represents the target of a migration.
350 type migrationKeyAuth struct {
351 MigrationKey pubKey
352 MigrationScheme MigrationScheme
353 Digest Digest
354 }
355
356 // A symKey is a TPM representation of a symmetric key.
357 type symKey struct {
358 AlgID Algorithm
359 EncScheme uint16
360 Key tpmutil.U16Bytes // TPM_SYMMETRIC_KEY uses a 16-bit header for Key data
361 }
362
363 // A tpmStoredData holds sealed data from the TPM.
364 type tpmStoredData struct {
365 Version uint32
366 Info tpmutil.U32Bytes
367 Enc tpmutil.U32Bytes
368 }
369
370 // String returns a string representation of a tpmStoredData.
371 func (tsd tpmStoredData) String() string {
372 return fmt.Sprintf("tpmStoreddata{Version: %x, Info: % x, Enc: % x\n", tsd.Version, tsd.Info, tsd.Enc)
373 }
374
375 // A quoteInfo structure is the structure signed by the TPM.
376 type quoteInfo struct {
377 // The Version must be 0x01010000
378 Version uint32
379
380 // Fixed is always 'QUOT'.
381 Fixed [4]byte
382
383 // The CompositeDigest is computed by ComputePCRComposite.
384 CompositeDigest Digest
385
386 // The Nonce is either a random Nonce or the SHA1 hash of data to sign.
387 Nonce Nonce
388 }
389
390 // A pcrComposite stores a selection of PCRs with the selected PCR values.
391 type pcrComposite struct {
392 Selection pcrSelection
393 Values tpmutil.U32Bytes
394 }
395
396 // convertPubKey converts a public key into TPM form. Currently, this function
397 // only supports 2048-bit RSA keys.
398 func convertPubKey(pk crypto.PublicKey) (*pubKey, error) {
399 pkRSA, ok := pk.(*rsa.PublicKey)
400 if !ok {
401 return nil, errors.New("the provided Privacy CA public key was not an RSA key")
402 }
403 if pkRSA.N.BitLen() != 2048 {
404 return nil, errors.New("The provided Privacy CA RSA public key was not a 2048-bit key")
405 }
406
407 rsakp := rsaKeyParams{
408 KeyLength: 2048,
409 NumPrimes: 2,
410 Exponent: big.NewInt(int64(pkRSA.E)).Bytes(),
411 }
412 rsakpb, err := tpmutil.Pack(rsakp)
413 if err != nil {
414 return nil, err
415 }
416 kp := keyParams{
417 AlgID: AlgRSA,
418 EncScheme: esNone,
419 SigScheme: ssRSASaPKCS1v15SHA1,
420 Params: rsakpb,
421 }
422 pubKey := pubKey{
423 AlgorithmParams: kp,
424 Key: pkRSA.N.Bytes(),
425 }
426
427 return &pubKey, nil
428 }