-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransient.go
264 lines (221 loc) · 6.34 KB
/
transient.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Package sks implements the Secure Key Store for Go
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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 sks
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/sha256"
"fmt"
"io"
attestIf "github.com/facebookincubator/sks/attest"
"github.com/google/go-attestation/attest"
)
const (
tpmHandleLabel = "attest_tpm_handle"
transientAkHandleLabel = "transient_ak_handle"
transientKeyHandleLabel = "transient_key_handle"
)
// newTransientKey creates a new transient key, which is TPM2.0 certified by an AK (attestation key)
func newTransientKey(tpm *attest.TPM, config *attest.KeyConfig) (*attest.Key, *attest.AK, error) {
if tpm == nil {
return nil, nil, fmt.Errorf(ErrInvalidTPMHandle, tpmHandleLabel)
}
ak, err := tpm.NewAK(nil)
if err != nil {
return nil, nil, err
}
key, err := tpm.NewKey(ak, config)
if err != nil {
return nil, nil, err
}
return key, ak, nil
}
// loadTransientKey loads the transient key, the transientAK Key based on the TPM 2.0 encrypted blobs
func loadTransientKey(tpm *attest.TPM, transientAKBlob, transientKeyBlob []byte) (*attest.Key, *attest.AK, error) {
if tpm == nil {
return nil, nil, fmt.Errorf(ErrInvalidTPMHandle, tpmHandleLabel)
}
if transientAKBlob == nil {
return nil, nil, fmt.Errorf(ErrEmptyTPM20EncryptedBlob, transientAkHandleLabel)
}
if transientKeyBlob == nil {
return nil, nil, fmt.Errorf(ErrEmptyTPM20EncryptedBlob, transientKeyHandleLabel)
}
ak, err := tpm.LoadAK(transientAKBlob)
if err != nil {
return nil, nil, err
}
key, err := tpm.LoadKey(transientKeyBlob)
if err != nil {
return nil, nil, err
}
return key, ak, nil
}
type attestkey struct {
handle *attest.TPM
transient *attest.Key
akTransient *attest.AK
attestor attestIf.Attestor
label string
tag string
}
// Config is the configuration for the transient key
type Config struct {
TransientAKKeyBlob []byte
TransientSKSKeyBlob []byte
Attestor attestIf.Attestor
}
// Option is a list of options for the transient key
type Option func(config *Config) *Config
// newConfig creates a Config for the shared CLI from a list of Options.
func newConfig(options ...Option) *Config {
config := &Config{
TransientAKKeyBlob: nil,
TransientSKSKeyBlob: nil,
}
for _, option := range options {
config = option(config)
}
return config
}
// WithAKKeyBlob sets the AK blob for the transient key
func WithAKKeyBlob(akBlob []byte) Option {
return func(config *Config) *Config {
config.TransientAKKeyBlob = akBlob
return config
}
}
// WithSKSKeyBlob sets the SKS blob for the transient key
func WithSKSKeyBlob(sksBlob []byte) Option {
return func(config *Config) *Config {
config.TransientSKSKeyBlob = sksBlob
return config
}
}
// WithAttestor sets the attestor for the transient key
func WithAttestor(attestor attestIf.Attestor) Option {
return func(config *Config) *Config {
config.Attestor = attestor
return config
}
}
// New creates (or) loads a transient key, which is TPM2.0 certified by an AK (attestation key)
func New(label string, tag string, options ...Option) (Key, error) {
config := newConfig(options...)
var (
handle *attest.TPM
transient *attest.Key
akTransient *attest.AK
attestor attestIf.Attestor
err error
)
handle, err = attest.OpenTPM(nil)
if err != nil {
return nil, err
}
attestor = config.Attestor
if config.TransientAKKeyBlob == nil || config.TransientSKSKeyBlob == nil {
transient, akTransient, err = newTransientKey(handle, nil)
if err != nil {
return nil, err
}
} else {
transient, akTransient, err = loadTransientKey(handle, config.TransientAKKeyBlob, config.TransientSKSKeyBlob)
if err != nil {
return nil, err
}
}
return &attestkey{
transient: transient,
akTransient: akTransient,
attestor: attestor,
handle: handle,
label: label,
tag: tag}, nil
}
func (k *attestkey) Public() crypto.PublicKey {
return k.transient.Public()
}
func (k *attestkey) Attest() error {
_, err := k.attestor.Attest(&attestIf.Req{
AttestTPMHandle: k.handle,
TransientAKHandle: k.akTransient,
TransientKeyHandle: k.transient,
})
if err != nil {
return fmt.Errorf("attestation failure: %w", err)
}
return nil
}
func (k *attestkey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
pkey, err := k.transient.Private(k.transient.Public())
if err != nil {
return nil, err
}
signer, ok := pkey.(crypto.Signer)
if !ok {
return nil, fmt.Errorf(ErrInvalidCryptoSigner)
}
return signer.Sign(rand, digest, opts)
}
// Hash returns the hash of the public key
func (k *attestkey) Hash() []byte {
pubkey, ok := k.transient.Public().(*ecdsa.PublicKey)
if !ok {
return nil
}
rawKey := elliptic.Marshal(pubkey.Curve, pubkey.X, pubkey.Y)
h := sha256.Sum256(rawKey)
return h[:]
}
// Label returns the label of the key
func (k *attestkey) Label() string {
return k.label
}
// Tag returns the tag of the key
func (k *attestkey) Tag() string {
return k.tag
}
// Remote returns an error as key creation is handled outside of this library
func (k *attestkey) Remove() error {
// not supported as key creation is handled outside of this library
return fmt.Errorf(ErrNotSupported)
}
// Close closes the transient key, the transient AK and the TPM attestation handle
func (k *attestkey) Close() error {
if k.transient != nil {
k.transient.Close()
}
if k.akTransient != nil {
k.akTransient.Close(k.handle)
}
if k.handle != nil {
k.handle.Close()
}
return nil
}
// EncryptedBlob returns the blobs of the transient key and transient AK
func (k *attestkey) EncryptedBlob() ([]byte, []byte, error) {
akBlob, err := k.akTransient.Marshal()
if err != nil {
return nil, nil, err
}
keyBlob, err := k.transient.Marshal()
if err != nil {
return nil, nil, err
}
return akBlob, keyBlob, nil
}