Skip to content

Commit 2cb8754

Browse files
authored
Fix a bug that KeyConfig.QualifyingData is ignored by tpm.NewKey...() (#477)
... when the algorithm and size are not set.
1 parent 713b503 commit 2cb8754

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

attest/application_key_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ import (
3030
"encoding/asn1"
3131
"math/big"
3232
"testing"
33+
34+
"github.com/google/go-cmp/cmp"
3335
)
3436

3537
func TestSimKeyCreateAndLoad(t *testing.T) {
@@ -678,3 +680,28 @@ func testKeyOpts(t *testing.T, tpm *TPM) {
678680
})
679681
}
680682
}
683+
684+
func TestOptsSetDefault(t *testing.T) {
685+
if optsSetDefault(nil) != defaultConfig {
686+
t.Errorf("expecting optsSetDefault(nil) to return the defaultConfig")
687+
}
688+
689+
normalOpts := KeyConfig{
690+
Algorithm: RSA,
691+
Size: 2048,
692+
}
693+
if optsSetDefault(&normalOpts) != &normalOpts {
694+
t.Errorf("expecting optsSetDefault() to return the same pointer for normal options")
695+
}
696+
697+
optsMissingAlgorithmAndSize := KeyConfig{
698+
QualifyingData: []byte("hello"),
699+
}
700+
optsAfter := optsSetDefault(&optsMissingAlgorithmAndSize)
701+
if optsAfter == &optsMissingAlgorithmAndSize {
702+
t.Errorf("expecting optsSetDefault() to return a pointer to a copy")
703+
}
704+
if diff := cmp.Diff(optsAfter, &KeyConfig{Algorithm: ECDSA, Size: 256, QualifyingData: []byte("hello")}); diff != "" {
705+
t.Errorf("optsSetDefault() mismatch (-want +got):\n%s", diff)
706+
}
707+
}

attest/tpm.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -488,15 +488,25 @@ func (t *TPM) NewAK(opts *AKConfig) (*AK, error) {
488488
return t.tpm.newAK(opts)
489489
}
490490

491-
// NewKey creates an application key certified by the attestation key. If opts is nil
492-
// then DefaultConfig is used.
493-
func (t *TPM) NewKey(ak *AK, opts *KeyConfig) (*Key, error) {
491+
// Return a KeyConfig with default values if appropriate. It never modifies the
492+
// incoming pointer.
493+
func optsSetDefault(opts *KeyConfig) *KeyConfig {
494494
if opts == nil {
495-
opts = defaultConfig
495+
return defaultConfig
496496
}
497497
if opts.Algorithm == "" && opts.Size == 0 {
498-
opts = defaultConfig
498+
optsCopy := *opts
499+
optsCopy.Algorithm = defaultConfig.Algorithm
500+
optsCopy.Size = defaultConfig.Size
501+
return &optsCopy
499502
}
503+
return opts
504+
}
505+
506+
// NewKey creates an application key certified by the attestation key. If opts is nil
507+
// then DefaultConfig is used.
508+
func (t *TPM) NewKey(ak *AK, opts *KeyConfig) (*Key, error) {
509+
opts = optsSetDefault(opts)
500510
return t.tpm.newKey(ak, opts)
501511
}
502512

@@ -506,12 +516,7 @@ func (t *TPM) NewKey(ak *AK, opts *KeyConfig) (*Key, error) {
506516
// Thus it can be used in cases where the attestation key was not created
507517
// by go-attestation library. If opts is nil then DefaultConfig is used.
508518
func (t *TPM) NewKeyCertifiedByKey(akHandle tpmutil.Handle, akAlg Algorithm, opts *KeyConfig) (*Key, error) {
509-
if opts == nil {
510-
opts = defaultConfig
511-
}
512-
if opts.Algorithm == "" && opts.Size == 0 {
513-
opts = defaultConfig
514-
}
519+
opts = optsSetDefault(opts)
515520
ck := certifyingKey{handle: akHandle, alg: akAlg}
516521
return t.tpm.newKeyCertifiedByKey(ck, opts)
517522
}

0 commit comments

Comments
 (0)