| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/rsa" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/google/go-attestation/attest" |
| 12 |
"github.com/google/go-tpm-tools/client" |
| 13 |
"github.com/google/go-tpm-tools/internal/test" |
| 14 |
"github.com/google/go-tpm/legacy/tpm2" |
| 15 |
) |
| 16 |
|
| 17 |
func TestQuote(t *testing.T) { |
| 18 |
rwc := test.GetTPM(t) |
| 19 |
defer client.CheckedClose(t, rwc) |
| 20 |
|
| 21 |
keys := []struct { |
| 22 |
name string |
| 23 |
getKey func(io.ReadWriter) (*client.Key, error) |
| 24 |
}{ |
| 25 |
{"AK-ECC", client.AttestationKeyECC}, |
| 26 |
{"AK-RSA", client.AttestationKeyRSA}, |
| 27 |
} |
| 28 |
|
| 29 |
pcrSels := []tpm2.PCRSelection{ |
| 30 |
{ |
| 31 |
Hash: tpm2.AlgSHA256, |
| 32 |
PCRs: []int{7}, |
| 33 |
}, |
| 34 |
client.FullPcrSel(tpm2.AlgSHA256), |
| 35 |
} |
| 36 |
|
| 37 |
for _, key := range keys { |
| 38 |
for _, sel := range pcrSels { |
| 39 |
name := fmt.Sprintf("%s-%d", key.name, len(sel.PCRs)) |
| 40 |
t.Run(name, func(t *testing.T) { |
| 41 |
ak, err := key.getKey(rwc) |
| 42 |
if err != nil { |
| 43 |
t.Errorf("failed to generate AK: %v", err) |
| 44 |
} |
| 45 |
defer ak.Close() |
| 46 |
|
| 47 |
quoted, err := ak.Quote(sel, []byte("test")) |
| 48 |
if err != nil { |
| 49 |
t.Errorf("failed to quote: %v", err) |
| 50 |
} |
| 51 |
sig, err := tpm2.DecodeSignature(bytes.NewBuffer(quoted.GetRawSig())) |
| 52 |
if err != nil { |
| 53 |
t.Errorf("signature decoding failed: %v", err) |
| 54 |
} |
| 55 |
|
| 56 |
switch pub := ak.PublicKey().(type) { |
| 57 |
case *ecdsa.PublicKey: |
| 58 |
hash, err := sig.ECC.HashAlg.Hash() |
| 59 |
if err != nil { |
| 60 |
t.Fatalf("not a valid hash type: %v", sig.ECC.HashAlg) |
| 61 |
} |
| 62 |
|
| 63 |
hashCon := hash.New() |
| 64 |
hashCon.Write(quoted.GetQuote()) |
| 65 |
if !ecdsa.Verify(pub, hashCon.Sum(nil)[:], sig.ECC.R, sig.ECC.S) { |
| 66 |
t.Errorf("ECC signature verification failed") |
| 67 |
} |
| 68 |
case *rsa.PublicKey: |
| 69 |
hash, err := sig.RSA.HashAlg.Hash() |
| 70 |
if err != nil { |
| 71 |
t.Fatalf("not a valid hash type: %v", sig.RSA.HashAlg) |
| 72 |
} |
| 73 |
|
| 74 |
hashCon := hash.New() |
| 75 |
hashCon.Write(quoted.GetQuote()) |
| 76 |
if err = rsa.VerifyPKCS1v15(pub, hash, hashCon.Sum(nil), []byte(sig.RSA.Signature)); err != nil { |
| 77 |
t.Errorf("RSA signature verification failed: %v", err) |
| 78 |
} |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
func TestQuoteShouldFailWithNonSigningKey(t *testing.T) { |
| 87 |
rwc := test.GetTPM(t) |
| 88 |
defer client.CheckedClose(t, rwc) |
| 89 |
|
| 90 |
srk, err := client.StorageRootKeyRSA(rwc) |
| 91 |
if err != nil { |
| 92 |
t.Errorf("failed to generate SRK: %v", err) |
| 93 |
} |
| 94 |
defer srk.Close() |
| 95 |
|
| 96 |
selpcr := tpm2.PCRSelection{ |
| 97 |
Hash: tpm2.AlgSHA1, |
| 98 |
PCRs: []int{7}, |
| 99 |
} |
| 100 |
_, err = srk.Quote(selpcr, []byte("test")) |
| 101 |
if err == nil { |
| 102 |
t.Errorf("Quote with a non-signing key should fail") |
| 103 |
} |
| 104 |
t.Log(err) |
| 105 |
} |
| 106 |
|
| 107 |
// Basic tests of Key.Attest, more advanced methods are in server package |
| 108 |
func TestAttest(t *testing.T) { |
| 109 |
rwc := test.GetTPM(t) |
| 110 |
defer client.CheckedClose(t, rwc) |
| 111 |
|
| 112 |
keys := []struct { |
| 113 |
name string |
| 114 |
getKey func(io.ReadWriter) (*client.Key, error) |
| 115 |
shouldSucceed bool |
| 116 |
}{ |
| 117 |
{"AK-ECC", client.AttestationKeyECC, true}, |
| 118 |
{"AK-RSA", client.AttestationKeyRSA, true}, |
| 119 |
{"EK-ECC", client.EndorsementKeyECC, false}, |
| 120 |
{"EK-RSA", client.EndorsementKeyRSA, false}, |
| 121 |
} |
| 122 |
for _, key := range keys { |
| 123 |
t.Run(key.name, func(t *testing.T) { |
| 124 |
ak, err := key.getKey(rwc) |
| 125 |
if err != nil { |
| 126 |
t.Fatalf("failed to generate AK: %v", err) |
| 127 |
} |
| 128 |
defer ak.Close() |
| 129 |
|
| 130 |
attestation, err := ak.Attest(client.AttestOpts{Nonce: []byte("some nonce")}) |
| 131 |
if !key.shouldSucceed { |
| 132 |
if err == nil { |
| 133 |
t.Error("expected failure when calling Attest") |
| 134 |
} |
| 135 |
return |
| 136 |
} |
| 137 |
if err != nil { |
| 138 |
t.Fatalf("failed to attest: %v", err) |
| 139 |
} |
| 140 |
|
| 141 |
// Basic check, make sure we got multiple banks, and fields parse |
| 142 |
if _, err = tpm2.DecodePublic(attestation.AkPub); err != nil { |
| 143 |
t.Errorf("failed to decode AkPub: %v", err) |
| 144 |
} |
| 145 |
if len(attestation.Quotes) <= 1 { |
| 146 |
t.Error("expected multiple quotes") |
| 147 |
} |
| 148 |
if _, err = attest.ParseEventLog(attestation.EventLog); err != nil { |
| 149 |
t.Errorf("failed to parse event log: %v", err) |
| 150 |
} |
| 151 |
}) |
| 152 |
|
| 153 |
} |
| 154 |
} |