-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkey.go
341 lines (285 loc) · 9 KB
/
key.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
Copyright (c) 2020 GMO GlobalSign, Inc.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
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 tpmkeys
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"encoding/asn1"
"errors"
"fmt"
"io"
"math/big"
"github.com/google/go-tpm/tpm2"
"github.com/google/go-tpm/tpmutil"
)
// PrivateKey represents a private key resident on a TPM 2.0 device. RSA and
// ECC private keys are supported for signing, and only RSA keys are supported
// for encryption.
type PrivateKey struct {
tpmRW io.ReadWriter
tpmPath string
password string
parentPassword string
activeHandle tpmutil.Handle
persistentHandle tpmutil.Handle
parentHandle tpmutil.Handle
publicBlob []byte
privateBlob []byte
pubKey crypto.PublicKey
scheme *tpm2.SigScheme
}
// Public returns the public key corresponding to the opaque private key.
func (k *PrivateKey) Public() crypto.PublicKey {
return k.pubKey
}
// Sign signs digest with the private key.
func (k *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
scheme, err := k.sigScheme(opts)
if err != nil {
return nil, err
}
// Sign digest.
rw, handle, closeFunc, err := k.getHandle()
if err != nil {
return nil, err
}
defer closeFunc()
sig, err := tpm2.Sign(rw, handle, k.password, digest, nil, &scheme)
if err != nil {
return nil, err
}
// Return signature based on key type.
var sigBytes []byte
switch {
case sig.RSA != nil:
sigBytes = sig.RSA.Signature
case sig.ECC != nil:
tmp := struct {
R *big.Int
S *big.Int
}{
R: sig.ECC.R,
S: sig.ECC.S,
}
sigBytes, err = asn1.Marshal(tmp)
if err != nil {
return nil, fmt.Errorf("failed to marshal EC signature: %v", err)
}
}
return sigBytes, nil
}
// Decrypt decrypts msg with the private key.
func (k *PrivateKey) Decrypt(rand io.Reader, msg []byte, opts crypto.DecrypterOpts) ([]byte, error) {
var scheme tpm2.AsymScheme
var label string
switch o := opts.(type) {
case *rsa.OAEPOptions:
scheme.Alg = tpm2.AlgOAEP
// tpm2.RSADecrypt appends a null octet to the label passed in, but
// rsa.EncryptOAEP does not, so the label provided will likely include
// it. To avoid including it twice, we check here if the last byte in
// the provided label is a null octet, and we remove it if it is.
if l := len(o.Label); l > 0 && o.Label[l-1] == 0 {
label = string(o.Label[:l-1])
} else {
label = string(o.Label)
}
var err error
scheme.Hash, err = tpmHash(o.Hash)
if err != nil {
return nil, err
}
default:
scheme.Alg = tpm2.AlgRSAES
scheme.Hash = tpm2.AlgNull
}
// Decrypt message.
rw, handle, closeFunc, err := k.getHandle()
if err != nil {
return nil, err
}
defer closeFunc()
return tpm2.RSADecrypt(rw, handle, k.password, msg, &scheme, label)
}
// getHandle returns an io.ReadWriter for a TPM, a key handle, and a cleanup
// function. The return values vary based on which constructor function was
// used to create the private key.
func (k *PrivateKey) getHandle() (io.ReadWriter, tpmutil.Handle, func(), error) {
if k.activeHandle != 0 {
return k.tpmRW, k.activeHandle, func() {}, nil
}
tpm, err := openTPM(k.tpmPath)
if err != nil {
return nil, 0, nil, fmt.Errorf("failed to open TPM: %v", err)
}
if k.persistentHandle != 0 {
return tpm, k.persistentHandle, func() { tpm.Close() }, nil
}
handle, _, err := tpm2.Load(tpm, k.parentHandle, k.parentPassword, k.publicBlob, k.privateBlob)
if err != nil {
tpm.Close()
return nil, 0, nil, fmt.Errorf("failed to load key: %v", err)
}
return tpm, handle, func() {
tpm2.FlushContext(tpm, handle)
tpm.Close()
}, nil
}
// sigScheme returns a signature scheme appropriate for the key and the
// provided signer options.
func (k *PrivateKey) sigScheme(opts crypto.SignerOpts) (tpm2.SigScheme, error) {
var scheme tpm2.SigScheme
// Use the signature algorithm specified by the key, or choose an
// appropriate one based on the key type and the signer options.
if k.scheme == nil || k.scheme.Alg == tpm2.AlgNull {
switch t := k.pubKey.(type) {
case *rsa.PublicKey:
switch opts.(type) {
case *rsa.PSSOptions:
scheme.Alg = tpm2.AlgRSAPSS
default:
scheme.Alg = tpm2.AlgRSASSA
}
case *ecdsa.PublicKey:
scheme.Alg = tpm2.AlgECDSA
default:
return tpm2.SigScheme{}, fmt.Errorf("unsupported public key type: %T", t)
}
} else {
scheme.Alg = k.scheme.Alg
}
// Select a hash algorithm based on the signer options.
var err error
scheme.Hash, err = tpmHash(opts.HashFunc())
if err != nil {
return tpm2.SigScheme{}, nil
}
return scheme, nil
}
// tpmHash returns the appropriate tpm2.Algorithm for the provided hash, or
// an error if the hash is not supported.
func tpmHash(h crypto.Hash) (tpm2.Algorithm, error) {
switch h {
case crypto.SHA1:
return tpm2.AlgSHA1, nil
case crypto.SHA256:
return tpm2.AlgSHA256, nil
case crypto.SHA384:
return tpm2.AlgSHA384, nil
case crypto.SHA512:
return tpm2.AlgSHA512, nil
}
return 0, fmt.Errorf("unsupported hash function: %d", h)
}
// NewFromActiveHandle returns a private key object representing the key
// referred to by the specified active handle. The caller is responsible for
// ensuring that the handle for the key is not changed, and the io.ReadWriter
// is not closed, until the returned key will no longer be used. Since this
// function accepts an io.ReadWriter, is it also suitable for connecting to
// a TPM simulator.
func NewFromActiveHandle(rw io.ReadWriter, handle uint32, password string) (*PrivateKey, error) {
pubKey, scheme, err := publicKeyAndScheme(rw, tpmutil.Handle(handle))
if err != nil {
return nil, err
}
return &PrivateKey{
tpmRW: rw,
password: password,
activeHandle: tpmutil.Handle(handle),
pubKey: pubKey,
scheme: scheme,
}, nil
}
// NewFromPersistentHandle returns a private key object representing the key
// referred to by the specified persistent handle, using the TPM at the specified
// path. A connection to the TPM is opened and closed with each use of the key,
// so the returned key is usable for as long as the key remains at that
// persistent handle.
func NewFromPersistentHandle(path string, handle uint32, password string) (*PrivateKey, error) {
tpm, err := openTPM(path)
if err != nil {
return nil, fmt.Errorf("failed to open TPM: %v", err)
}
defer tpm.Close()
pubKey, scheme, err := publicKeyAndScheme(tpm, tpmutil.Handle(handle))
if err != nil {
return nil, err
}
return &PrivateKey{
tpmPath: path,
password: password,
persistentHandle: tpmutil.Handle(handle),
pubKey: pubKey,
scheme: scheme,
}, nil
}
// NewFromBlobs returns a private key object representing the key referred to
// by the provided public and private area blobs. A connection to the TPM is
// opened and closed, and the key loaded and flushed, with each use of the key,
// so the returned key is usable for as long as the parent key remains at the
// specified persistent handle.
func NewFromBlobs(
path string,
parent uint32,
parentPassword string,
pubBlob, privBlob []byte,
password string,
) (*PrivateKey, error) {
tpm, err := openTPM(path)
if err != nil {
return nil, fmt.Errorf("failed to open TPM: %v", err)
}
defer tpm.Close()
handle, _, err := tpm2.Load(tpm, tpmutil.Handle(parent), parentPassword, pubBlob, privBlob)
if err != nil {
return nil, fmt.Errorf("failed to load key: %v", err)
}
defer tpm2.FlushContext(tpm, handle)
pubKey, scheme, err := publicKeyAndScheme(tpm, handle)
if err != nil {
return nil, err
}
return &PrivateKey{
tpmPath: path,
password: password,
parentPassword: parentPassword,
parentHandle: tpmutil.Handle(parent),
publicBlob: pubBlob,
privateBlob: privBlob,
pubKey: pubKey,
scheme: scheme,
}, nil
}
// publicKeyAndScheme reads a public area from an active handle and returns the
// public key and signature scheme, if any.
func publicKeyAndScheme(rw io.ReadWriter, handle tpmutil.Handle) (crypto.PublicKey, *tpm2.SigScheme, error) {
pub, _, _, err := tpm2.ReadPublic(rw, handle)
if err != nil {
return nil, nil, fmt.Errorf("failed to read public area from TPM: %v", err)
}
pubKey, err := pub.Key()
if err != nil {
return nil, nil, fmt.Errorf("failed to get public key from public area: %v", err)
}
// If the object specifies a signature scheme, store it.
var scheme *tpm2.SigScheme
switch {
case pub.RSAParameters != nil:
scheme = pub.RSAParameters.Sign
case pub.ECCParameters != nil:
scheme = pub.ECCParameters.Sign
default:
return nil, nil, errors.New("only RSA and ECC keys supported")
}
return pubKey, scheme, nil
}