| 1 |
package client_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/sha256" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/google/go-tpm-tools/client" |
| 11 |
"github.com/google/go-tpm-tools/internal/test" |
| 12 |
tpb "github.com/google/go-tpm-tools/proto/tpm" |
| 13 |
"github.com/google/go-tpm-tools/server" |
| 14 |
"github.com/google/go-tpm/tpm2" |
| 15 |
"github.com/google/go-tpm/tpm2/transport" |
| 16 |
) |
| 17 |
|
| 18 |
func TestCreateCertifiedAKBlob(t *testing.T) { |
| 19 |
rwc := test.GetTPM(t) |
| 20 |
tpm := transport.FromReadWriter(rwc) |
| 21 |
defer client.CheckedClose(t, rwc) |
| 22 |
|
| 23 |
ek, err := client.EndorsementKeyRSA(rwc) |
| 24 |
if err != nil { |
| 25 |
t.Fatalf("failed to get rsa ek: %v", ek) |
| 26 |
} |
| 27 |
defer ek.Close() |
| 28 |
|
| 29 |
pubBytes, err := ek.PublicArea().Encode() |
| 30 |
if err != nil { |
| 31 |
t.Fatalf("ek public area encode failed: %v", err) |
| 32 |
} |
| 33 |
pub, err := tpm2.Unmarshal[tpm2.TPMTPublic](pubBytes) |
| 34 |
if err != nil { |
| 35 |
t.Fatalf("Unmarshal public key failed: %v", err) |
| 36 |
} |
| 37 |
|
| 38 |
testcases := []struct { |
| 39 |
name string |
| 40 |
keyAlgo tpm2.TPMAlgID |
| 41 |
}{ |
| 42 |
{"RSA", tpm2.TPMAlgRSA}, |
| 43 |
{"ECC", tpm2.TPMAlgECC}, |
| 44 |
} |
| 45 |
for _, tc := range testcases { |
| 46 |
t.Run(tc.name, func(t *testing.T) { |
| 47 |
challenge, secret, err := server.CreateRestrictedHMACBlob(pub) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("server.CreateChallenge failed: %v", err) |
| 50 |
} |
| 51 |
|
| 52 |
response, err := client.CreateCertifiedAKBlob(tpm, challenge, tc.keyAlgo) |
| 53 |
if err != nil { |
| 54 |
t.Fatalf("SolveChallengeImportCertify failed: %v", err) |
| 55 |
} |
| 56 |
if err := server.VerifyCertifiedAKBlob(response, secret); err != nil { |
| 57 |
t.Errorf("server.VerifyCertifiedAKBlob failed: %v", err) |
| 58 |
} |
| 59 |
}) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestVerifyCertifiedAKBlobErrors(t *testing.T) { |
| 64 |
rwc := test.GetTPM(t) |
| 65 |
defer client.CheckedClose(t, rwc) |
| 66 |
tpm := transport.FromReadWriter(rwc) |
| 67 |
|
| 68 |
ek, err := client.EndorsementKeyRSA(rwc) |
| 69 |
if err != nil { |
| 70 |
t.Fatalf("failed to get rsa ek: %v", ek) |
| 71 |
} |
| 72 |
defer ek.Close() |
| 73 |
|
| 74 |
pubBytes, err := ek.PublicArea().Encode() |
| 75 |
if err != nil { |
| 76 |
t.Fatalf("ek public area encode failed: %v", err) |
| 77 |
} |
| 78 |
ekPub, err := tpm2.Unmarshal[tpm2.TPMTPublic](pubBytes) |
| 79 |
if err != nil { |
| 80 |
t.Fatalf("Unmarshal public key failed: %v", err) |
| 81 |
} |
| 82 |
challenge, secret, err := server.CreateRestrictedHMACBlob(ekPub) |
| 83 |
if err != nil { |
| 84 |
t.Fatalf("server.CreateChallenge failed: %v", err) |
| 85 |
} |
| 86 |
|
| 87 |
response, err := client.CreateCertifiedAKBlob(tpm, challenge, tpm2.TPMAlgECC) |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("SolveChallengeImportCertify failed: %v", err) |
| 90 |
} |
| 91 |
|
| 92 |
// Make a copy of the valid data to tamper with |
| 93 |
goodAkPub := bytes.Clone(response.GetPubArea()) |
| 94 |
goodCertifyInfo := bytes.Clone(response.GetCertifyInfo()) |
| 95 |
goodSignature := bytes.Clone(response.GetRawSig()) |
| 96 |
|
| 97 |
// helper to re-sign a modified certifyInfo |
| 98 |
reSign := func(certifyInfo []byte) []byte { |
| 99 |
sig, err := tpm2.Unmarshal[tpm2.TPMTSignature](goodSignature) |
| 100 |
if err != nil { |
| 101 |
t.Fatalf("unmarshaling good signature: %v", err) |
| 102 |
} |
| 103 |
hmacVal, err := sig.Signature.HMAC() |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("getting hmac from signature: %v", err) |
| 106 |
} |
| 107 |
digest := sha256.Sum256(certifyInfo) |
| 108 |
h := hmac.New(sha256.New, secret) |
| 109 |
h.Write(digest[:]) |
| 110 |
hmacVal.Digest = h.Sum(nil) |
| 111 |
return tpm2.Marshal(sig) |
| 112 |
} |
| 113 |
|
| 114 |
testcases := []struct { |
| 115 |
name string |
| 116 |
getReq func(t *testing.T) *tpb.CertifiedBlob |
| 117 |
secret []byte |
| 118 |
wantErrString string |
| 119 |
}{ |
| 120 |
{ |
| 121 |
name: "Bad Secret", |
| 122 |
getReq: func(_ *testing.T) *tpb.CertifiedBlob { |
| 123 |
return response |
| 124 |
}, |
| 125 |
secret: []byte("bad secret"), |
| 126 |
wantErrString: "invalid HMAC", |
| 127 |
}, |
| 128 |
{ |
| 129 |
name: "Wrong HMAC Hash Alg", |
| 130 |
getReq: func(t *testing.T) *tpb.CertifiedBlob { |
| 131 |
sig, err := tpm2.Unmarshal[tpm2.TPMTSignature](goodSignature) |
| 132 |
if err != nil { |
| 133 |
t.Fatalf("unmarshaling good signature: %v", err) |
| 134 |
} |
| 135 |
hmacVal, err := sig.Signature.HMAC() |
| 136 |
if err != nil { |
| 137 |
t.Fatalf("getting hmac from signature: %v", err) |
| 138 |
} |
| 139 |
hmacVal.HashAlg = tpm2.TPMAlgSHA1 |
| 140 |
return &tpb.CertifiedBlob{ |
| 141 |
PubArea: goodAkPub, |
| 142 |
CertifyInfo: goodCertifyInfo, |
| 143 |
RawSig: tpm2.Marshal(sig), |
| 144 |
} |
| 145 |
}, |
| 146 |
secret: secret, |
| 147 |
wantErrString: "wrong hash algorithm", |
| 148 |
}, |
| 149 |
{ |
| 150 |
name: "Bad HMAC Digest", |
| 151 |
getReq: func(t *testing.T) *tpb.CertifiedBlob { |
| 152 |
sig, err := tpm2.Unmarshal[tpm2.TPMTSignature](goodSignature) |
| 153 |
if err != nil { |
| 154 |
t.Fatalf("unmarshaling good signature: %v", err) |
| 155 |
} |
| 156 |
hmacVal, err := sig.Signature.HMAC() |
| 157 |
if err != nil { |
| 158 |
t.Fatalf("getting hmac from signature: %v", err) |
| 159 |
} |
| 160 |
hmacVal.Digest[0] ^= 0xff |
| 161 |
return &tpb.CertifiedBlob{ |
| 162 |
PubArea: goodAkPub, |
| 163 |
CertifyInfo: goodCertifyInfo, |
| 164 |
RawSig: tpm2.Marshal(sig), |
| 165 |
} |
| 166 |
}, |
| 167 |
secret: secret, |
| 168 |
wantErrString: "invalid HMAC", |
| 169 |
}, |
| 170 |
{ |
| 171 |
name: "Tampered CertifyInfo", |
| 172 |
getReq: func(_ *testing.T) *tpb.CertifiedBlob { |
| 173 |
badCertifyInfo := bytes.Clone(goodCertifyInfo) |
| 174 |
badCertifyInfo[0] ^= 0xff |
| 175 |
return &tpb.CertifiedBlob{ |
| 176 |
PubArea: goodAkPub, |
| 177 |
CertifyInfo: badCertifyInfo, |
| 178 |
RawSig: goodSignature, |
| 179 |
} |
| 180 |
}, |
| 181 |
secret: secret, |
| 182 |
wantErrString: "invalid HMAC", |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "Bad Attest Magic", |
| 186 |
getReq: func(t *testing.T) *tpb.CertifiedBlob { |
| 187 |
attest, err := tpm2.Unmarshal[tpm2.TPMSAttest](goodCertifyInfo) |
| 188 |
if err != nil { |
| 189 |
t.Fatalf("unmarshaling good certify info: %v", err) |
| 190 |
} |
| 191 |
attest.Magic = 0 |
| 192 |
badCertifyInfo := tpm2.Marshal(attest) |
| 193 |
return &tpb.CertifiedBlob{ |
| 194 |
PubArea: goodAkPub, |
| 195 |
CertifyInfo: badCertifyInfo, |
| 196 |
RawSig: reSign(badCertifyInfo), |
| 197 |
} |
| 198 |
}, |
| 199 |
secret: secret, |
| 200 |
wantErrString: "attestation statement was invalid", |
| 201 |
}, |
| 202 |
{ |
| 203 |
name: "Bad Certified Name", |
| 204 |
getReq: func(t *testing.T) *tpb.CertifiedBlob { |
| 205 |
attest, err := tpm2.Unmarshal[tpm2.TPMSAttest](goodCertifyInfo) |
| 206 |
if err != nil { |
| 207 |
t.Fatalf("unmarshaling good certify info: %v", err) |
| 208 |
} |
| 209 |
certify, err := attest.Attested.Certify() |
| 210 |
if err != nil { |
| 211 |
t.Fatalf("getting certify from attest: %v", err) |
| 212 |
} |
| 213 |
certify.Name.Buffer[0] ^= 0xff |
| 214 |
badCertifyInfo := tpm2.Marshal(attest) |
| 215 |
|
| 216 |
return &tpb.CertifiedBlob{ |
| 217 |
PubArea: goodAkPub, |
| 218 |
CertifyInfo: badCertifyInfo, |
| 219 |
RawSig: reSign(badCertifyInfo), |
| 220 |
} |
| 221 |
}, |
| 222 |
secret: secret, |
| 223 |
wantErrString: "incorrect name", |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "QualifiedName matches Name", |
| 227 |
getReq: func(t *testing.T) *tpb.CertifiedBlob { |
| 228 |
attest, err := tpm2.Unmarshal[tpm2.TPMSAttest](goodCertifyInfo) |
| 229 |
if err != nil { |
| 230 |
t.Fatalf("unmarshaling good certify info: %v", err) |
| 231 |
} |
| 232 |
certify, err := attest.Attested.Certify() |
| 233 |
if err != nil { |
| 234 |
t.Fatalf("getting certify from attest: %v", err) |
| 235 |
} |
| 236 |
certify.QualifiedName.Buffer = bytes.Clone(certify.Name.Buffer) |
| 237 |
badCertifyInfo := tpm2.Marshal(attest) |
| 238 |
return &tpb.CertifiedBlob{ |
| 239 |
PubArea: goodAkPub, |
| 240 |
CertifyInfo: badCertifyInfo, |
| 241 |
RawSig: reSign(badCertifyInfo), |
| 242 |
} |
| 243 |
}, |
| 244 |
secret: secret, |
| 245 |
wantErrString: "incorrect name", |
| 246 |
}, |
| 247 |
} |
| 248 |
|
| 249 |
for _, tc := range testcases { |
| 250 |
t.Run(tc.name, func(t *testing.T) { |
| 251 |
err := server.VerifyCertifiedAKBlob(tc.getReq(t), tc.secret) |
| 252 |
if err == nil || !strings.Contains(err.Error(), tc.wantErrString) { |
| 253 |
t.Errorf("got err: %v, want err containing: %q", err, tc.wantErrString) |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
} |