Skip to content

Commit 1bbba0b

Browse files
authored
Minor fixes and additions (#207)
* replace ReadPublic() by DecodePublic() when creating and loading keys: the current implementation calls ReadPublic() even if public data is already accessible * drop handle() from the ak interface: it is unnecessary * add Blobs() to attest.Key: to allow agnostic key marshaling
1 parent 611c659 commit 1bbba0b

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

attest/application_key.go

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type key interface {
2828
certificationParameters() CertificationParameters
2929
sign(tpmBase, []byte) ([]byte, error)
3030
decrypt(tpmBase, []byte) ([]byte, error)
31+
blobs() ([]byte, []byte, error)
3132
}
3233

3334
// Key represents a key which can be used for signing and decrypting
@@ -101,3 +102,8 @@ func (k *Key) Marshal() ([]byte, error) {
101102
func (k *Key) CertificationParameters() CertificationParameters {
102103
return k.key.certificationParameters()
103104
}
105+
106+
// Blobs returns public and private blobs to be used by tpm2.Load().
107+
func (k *Key) Blobs() (pub, priv []byte, err error) {
108+
return k.key.blobs()
109+
}

attest/attest.go

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"github.com/google/certificate-transparency-go/x509"
2525
"github.com/google/go-tpm/tpm"
2626
"github.com/google/go-tpm/tpm2"
27-
"github.com/google/go-tpm/tpmutil"
2827
)
2928

3029
// TPMVersion is used to configure a preference in
@@ -104,7 +103,6 @@ type ak interface {
104103
activateCredential(tpm tpmBase, in EncryptedCredential) ([]byte, error)
105104
quote(t tpmBase, nonce []byte, alg HashAlg) (*Quote, error)
106105
attestationParameters() AttestationParameters
107-
handle() (tpmutil.Handle, error)
108106
}
109107

110108
// AK represents a key which can be used for attestation.

attest/key_linux.go

-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package attest
1919
import (
2020
"fmt"
2121

22-
"github.com/google/go-tpm/tpmutil"
2322
"github.com/google/go-tspi/attestation"
2423
)
2524

@@ -92,7 +91,3 @@ func (k *trousersKey12) attestationParameters() AttestationParameters {
9291
UseTCSDActivationFormat: true,
9392
}
9493
}
95-
96-
func (k *trousersKey12) handle() (tpmutil.Handle, error) {
97-
return 0, fmt.Errorf("not implemented")
98-
}

attest/key_windows.go

-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121

2222
tpm1 "github.com/google/go-tpm/tpm"
23-
"github.com/google/go-tpm/tpmutil"
2423
)
2524

2625
// windowsKey12 represents a Windows-managed key on a TPM1.2 TPM.
@@ -112,10 +111,6 @@ func (k *windowsKey12) attestationParameters() AttestationParameters {
112111
}
113112
}
114113

115-
func (k *windowsKey12) handle() (tpmutil.Handle, error) {
116-
return 0, fmt.Errorf("not implemented")
117-
}
118-
119114
// windowsKey20 represents a key bound to a TPM 2.0.
120115
type windowsKey20 struct {
121116
hnd uintptr
@@ -189,7 +184,3 @@ func (k *windowsKey20) attestationParameters() AttestationParameters {
189184
CreateSignature: k.createSignature,
190185
}
191186
}
192-
193-
func (k *windowsKey20) handle() (tpmutil.Handle, error) {
194-
return 0, fmt.Errorf("not implemented")
195-
}

attest/wrapped_tpm20.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,9 @@ func (t *wrappedTPM20) newAK(opts *AKConfig) (*AK, error) {
159159

160160
func (t *wrappedTPM20) newKey(ak *AK, opts *KeyConfig) (*Key, error) {
161161
// TODO(szp): TODO(jsonp): Abstract choice of hierarchy & parent.
162-
certifierHandle, err := ak.ak.handle()
163-
if err != nil {
164-
return nil, fmt.Errorf("cannot get AK's handle: %v", err)
162+
k, ok := ak.ak.(*wrappedKey20)
163+
if !ok {
164+
return nil, fmt.Errorf("expected *wrappedKey20, got: %T", k)
165165
}
166166

167167
srk, _, err := t.getPrimaryKeyHandle(commonSrkEquivalentHandle)
@@ -185,7 +185,7 @@ func (t *wrappedTPM20) newKey(ak *AK, opts *KeyConfig) (*Key, error) {
185185
}()
186186

187187
// Certify application key by AK
188-
attestation, sig, err := tpm2.CertifyCreation(t.rwc, "", keyHandle, certifierHandle, nil, creationHash, tpm2.SigScheme{tpm2.AlgRSASSA, tpm2.AlgSHA256, 0}, tix)
188+
attestation, sig, err := tpm2.CertifyCreation(t.rwc, "", keyHandle, k.hnd, nil, creationHash, tpm2.SigScheme{tpm2.AlgRSASSA, tpm2.AlgSHA256, 0}, tix)
189189
if err != nil {
190190
return nil, fmt.Errorf("CertifyCreation failed: %v", err)
191191
}
@@ -195,13 +195,13 @@ func (t *wrappedTPM20) newKey(ak *AK, opts *KeyConfig) (*Key, error) {
195195
return nil, fmt.Errorf("failed to pack TPMT_SIGNATURE: %v", err)
196196
}
197197

198-
tpmPub, _, _, err := tpm2.ReadPublic(t.rwc, keyHandle)
198+
tpmPub, err := tpm2.DecodePublic(pub)
199199
if err != nil {
200-
return nil, fmt.Errorf("read public blob: %v", err)
200+
return nil, fmt.Errorf("decode public key: %v", err)
201201
}
202202
pubKey, err := tpmPub.Key()
203203
if err != nil {
204-
return nil, fmt.Errorf("decode public key: %v", err)
204+
return nil, fmt.Errorf("access public key: %v", err)
205205
}
206206
return &Key{key: newWrappedKey20(keyHandle, blob, pub, creationData, attestation, signature), pub: pubKey, tpm: t}, nil
207207
}
@@ -239,13 +239,13 @@ func (t *wrappedTPM20) loadKey(opaqueBlob []byte) (*Key, error) {
239239
if err != nil {
240240
return nil, fmt.Errorf("cannot load signing key: %v", err)
241241
}
242-
tpmPub, _, _, err := tpm2.ReadPublic(t.rwc, hnd)
242+
tpmPub, err := tpm2.DecodePublic(sKey.Public)
243243
if err != nil {
244-
return nil, fmt.Errorf("read public blob: %v", err)
244+
return nil, fmt.Errorf("decode public blob: %v", err)
245245
}
246246
pub, err := tpmPub.Key()
247247
if err != nil {
248-
return nil, fmt.Errorf("decode public key: %v", err)
248+
return nil, fmt.Errorf("access public key: %v", err)
249249
}
250250
return &Key{key: newWrappedKey20(hnd, sKey.Blob, sKey.Public, sKey.CreateData, sKey.CreateAttestation, sKey.CreateSignature), pub: pub, tpm: t}, nil
251251
}
@@ -396,10 +396,6 @@ func (k *wrappedKey20) certificationParameters() CertificationParameters {
396396
}
397397
}
398398

399-
func (k *wrappedKey20) handle() (tpmutil.Handle, error) {
400-
return k.hnd, nil
401-
}
402-
403399
func (k *wrappedKey20) sign(tb tpmBase, digest []byte) ([]byte, error) {
404400
t, ok := tb.(*wrappedTPM20)
405401
if !ok {
@@ -424,3 +420,7 @@ func (k *wrappedKey20) sign(tb tpmBase, digest []byte) ([]byte, error) {
424420
func (k *wrappedKey20) decrypt(tb tpmBase, ctxt []byte) ([]byte, error) {
425421
return nil, fmt.Errorf("not implemented")
426422
}
423+
424+
func (k *wrappedKey20) blobs() ([]byte, []byte, error) {
425+
return k.public, k.blob, nil
426+
}

0 commit comments

Comments
 (0)