Debian Codegraph @

attest/storage.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 package attest
16
17 import (
18 "encoding/json"
19 "fmt"
20 )
21
22 // serializedKey represents a loadable, TPM-backed key.
23 type serializedKey struct {
24 // Encoding describes the strategy by which the key should be
25 // loaded/unloaded.
26 Encoding keyEncoding `json:"KeyEncoding"`
27
28 // Unused and retained for backwards compatibility with existing keys.
29 TPMVersion uint8
30
31 // Public represents the public key, in a TPM-specific format. This
32 // field is populated on all platforms and TPM versions.
33 Public []byte
34 // The following fields are only valid for TPM 2.0 hardware, holding
35 // information returned as the result to a TPM2_CertifyCreation command.
36 // These are stored alongside the key for later use, as the certification
37 // can only be obtained immediately after the key is generated.
38 CreateData []byte
39 CreateAttestation []byte
40 CreateSignature []byte
41
42 // Name is only valid for KeyEncodingOSManaged, which is only used
43 // on Windows.
44 Name string
45 // Blob represents the key material for KeyEncodingEncrypted keys. This
46 // is only used on Linux.
47 Blob []byte `json:"KeyBlob"`
48 }
49
50 // Serialize represents the key in a persistent format which may be
51 // loaded at a later time using deserializeKey().
52 func (k *serializedKey) Serialize() ([]byte, error) {
53 return json.Marshal(k)
54 }
55
56 func deserializeKey(b []byte) (*serializedKey, error) {
57 var k serializedKey
58 var err error
59 if err = json.Unmarshal(b, &k); err != nil {
60 return nil, fmt.Errorf("json.Unmarshal() failed: %v", err)
61 }
62
63 if k.TPMVersion != 2 {
64 return nil, fmt.Errorf("key for different TPM version: %v", k.TPMVersion)
65 }
66
67 return &k, nil
68 }