2019-03-28 20:21:16 +00:00
|
|
|
// Copyright 2019 Google Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
// use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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.
|
2019-03-28 20:29:24 +00:00
|
|
|
|
2019-04-01 18:06:07 +00:00
|
|
|
// +build !localtest !tpm12
|
2019-09-19 20:13:11 +00:00
|
|
|
// +build cgo
|
|
|
|
|
|
|
|
// NOTE: simulator requires cgo, hence the build tag.
|
2019-04-01 18:06:07 +00:00
|
|
|
|
2019-03-28 20:21:16 +00:00
|
|
|
package attest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-tpm-tools/simulator"
|
|
|
|
)
|
|
|
|
|
|
|
|
func setupSimulatedTPM(t *testing.T) (*simulator.Simulator, *TPM) {
|
|
|
|
t.Helper()
|
|
|
|
tpm, err := simulator.Get()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-26 22:11:31 +00:00
|
|
|
return tpm, &TPM{tpm: &platformTPM{
|
2019-03-28 20:21:16 +00:00
|
|
|
version: TPMVersion20,
|
|
|
|
interf: TPMInterfaceKernelManaged,
|
|
|
|
sysPath: "/dev/tpmrm0",
|
|
|
|
rwc: tpm,
|
2019-09-19 20:13:11 +00:00
|
|
|
}}
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestSimTPM20EK(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
|
|
|
eks, err := tpm.EKs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("EKs() failed: %v", err)
|
|
|
|
}
|
2019-08-21 17:26:55 +00:00
|
|
|
if len(eks) == 0 || (eks[0].Public == nil) {
|
2019-03-28 20:21:16 +00:00
|
|
|
t.Errorf("EKs() = %v, want at least 1 EK with populated fields", eks)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestSimTPM20Info(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-08-21 17:28:55 +00:00
|
|
|
if _, err := tpm.Info(); err != nil {
|
2019-03-28 20:21:16 +00:00
|
|
|
t.Errorf("tpm.Info() failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func TestSimTPM20AKCreateAndLoad(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
ak, err := tpm.NewAK(nil)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("NewAK() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
enc, err := ak.Marshal()
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
ak.Close(tpm)
|
|
|
|
t.Fatalf("ak.Marshal() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-10-03 19:32:50 +00:00
|
|
|
if err := ak.Close(tpm); err != nil {
|
|
|
|
t.Fatalf("ak.Close() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
loaded, err := tpm.LoadAK(enc)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("LoadKey() failed: %v", err)
|
|
|
|
}
|
|
|
|
defer loaded.Close(tpm)
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
k1, k2 := ak.ak.(*key20), loaded.ak.(*key20)
|
2019-07-19 20:05:18 +00:00
|
|
|
|
|
|
|
if !bytes.Equal(k1.public, k2.public) {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Error("Original & loaded AK public blobs did not match.")
|
2019-07-19 20:05:18 +00:00
|
|
|
t.Logf("Original = %v", k1.public)
|
|
|
|
t.Logf("Loaded = %v", k2.public)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestSimTPM20ActivateCredential(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
ak, err := tpm.NewAK(nil)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("NewAK() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-10-03 19:32:50 +00:00
|
|
|
defer ak.Close(tpm)
|
2019-03-28 20:21:16 +00:00
|
|
|
|
|
|
|
EKs, err := tpm.EKs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("EKs() failed: %v", err)
|
|
|
|
}
|
2019-08-21 17:26:55 +00:00
|
|
|
ek := chooseEK(t, EKs)
|
2019-03-28 20:21:16 +00:00
|
|
|
|
2019-07-23 22:22:53 +00:00
|
|
|
ap := ActivationParameters{
|
|
|
|
TPMVersion: TPMVersion20,
|
2019-10-10 21:29:46 +00:00
|
|
|
AK: ak.AttestationParameters(),
|
2019-07-23 22:22:53 +00:00
|
|
|
EK: ek,
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-07-23 22:22:53 +00:00
|
|
|
secret, challenge, err := ap.Generate()
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-07-23 22:22:53 +00:00
|
|
|
t.Fatalf("Generate() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
decryptedSecret, err := ak.ActivateCredential(tpm, *challenge)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Errorf("ak.ActivateCredential() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(secret, decryptedSecret) {
|
|
|
|
t.Error("secret does not match decrypted secret")
|
|
|
|
t.Logf("Secret = %v", secret)
|
|
|
|
t.Logf("Decrypted secret = %v", decryptedSecret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func TestParseAKPublic20(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
ak, err := tpm.NewAK(nil)
|
2019-07-30 17:27:57 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("NewAK() failed: %v", err)
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
2019-10-03 19:32:50 +00:00
|
|
|
defer ak.Close(tpm)
|
|
|
|
params := ak.AttestationParameters()
|
|
|
|
if _, err := ParseAKPublic(TPMVersion20, params.Public); err != nil {
|
|
|
|
t.Errorf("parsing AK public blob: %v", err)
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 22:11:31 +00:00
|
|
|
func TestSimTPM20QuoteAndVerify(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
ak, err := tpm.NewAK(nil)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("NewAK() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-10-03 19:32:50 +00:00
|
|
|
defer ak.Close(tpm)
|
2019-03-28 20:21:16 +00:00
|
|
|
|
|
|
|
nonce := []byte{1, 2, 3, 4, 5, 6, 7, 8}
|
2019-10-03 19:32:50 +00:00
|
|
|
quote, err := ak.Quote(tpm, nonce, HashSHA256)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("ak.Quote() failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-09-26 22:11:31 +00:00
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
// Providing both PCR banks to AKPublic.Verify() ensures we can handle
|
2019-09-26 22:11:31 +00:00
|
|
|
// the case where extra PCRs of a different digest algorithm are provided.
|
|
|
|
var pcrs []PCR
|
|
|
|
for _, alg := range []HashAlg{HashSHA256, HashSHA1} {
|
|
|
|
p, err := tpm.PCRs(alg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tpm.PCRs(%v) failed: %v", alg, err)
|
|
|
|
}
|
|
|
|
pcrs = append(pcrs, p...)
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
pub, err := ParseAKPublic(tpm.Version(), ak.AttestationParameters().Public)
|
2019-09-26 22:11:31 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("ParseAKPublic() failed: %v", err)
|
2019-09-26 22:11:31 +00:00
|
|
|
}
|
|
|
|
if err := pub.Verify(*quote, pcrs, nonce); err != nil {
|
|
|
|
t.Errorf("quote verification failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSimTPM20AttestPlatform(t *testing.T) {
|
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
ak, err := tpm.NewAK(nil)
|
2019-09-26 22:11:31 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("NewAK() failed: %v", err)
|
2019-09-26 22:11:31 +00:00
|
|
|
}
|
2019-10-03 19:32:50 +00:00
|
|
|
defer ak.Close(tpm)
|
2019-09-26 22:11:31 +00:00
|
|
|
|
|
|
|
nonce := []byte{1, 2, 3, 4, 5, 6, 7, 8}
|
2019-10-03 19:32:50 +00:00
|
|
|
attestation, err := tpm.attestPlatform(ak, nonce, nil)
|
2019-09-26 22:11:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("AttestPlatform() failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
pub, err := ParseAKPublic(attestation.TPMVersion, attestation.Public)
|
2019-09-26 22:11:31 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:32:50 +00:00
|
|
|
t.Fatalf("ParseAKPublic() failed: %v", err)
|
2019-09-26 22:11:31 +00:00
|
|
|
}
|
|
|
|
for i, q := range attestation.Quotes {
|
|
|
|
if err := pub.Verify(q, attestation.PCRs, nonce); err != nil {
|
|
|
|
t.Errorf("quote[%d] verification failed: %v", i, err)
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestSimTPM20PCRs(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-08-20 18:52:12 +00:00
|
|
|
PCRs, err := tpm.PCRs(HashSHA256)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("PCRs() failed: %v", err)
|
|
|
|
}
|
|
|
|
if len(PCRs) != 24 {
|
|
|
|
t.Errorf("len(PCRs) = %d, want %d", len(PCRs), 24)
|
|
|
|
}
|
2019-08-20 18:52:12 +00:00
|
|
|
|
2019-03-28 20:21:16 +00:00
|
|
|
for i, pcr := range PCRs {
|
|
|
|
if len(pcr.Digest) != pcr.DigestAlg.Size() {
|
|
|
|
t.Errorf("PCR %d len(digest) = %d, expected match with digest algorithm size (%d)", pcr.Index, len(pcr.Digest), pcr.DigestAlg.Size())
|
|
|
|
}
|
|
|
|
if pcr.Index != i {
|
|
|
|
t.Errorf("PCR index %d does not match map index %d", pcr.Index, i)
|
|
|
|
}
|
|
|
|
if pcr.DigestAlg != crypto.SHA256 {
|
|
|
|
t.Errorf("pcr.DigestAlg = %v, expected crypto.SHA256", pcr.DigestAlg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestSimTPM20Persistence(t *testing.T) {
|
2019-03-28 20:21:16 +00:00
|
|
|
sim, tpm := setupSimulatedTPM(t)
|
|
|
|
defer sim.Close()
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
ekHnd, _, err := tpm.tpm.getPrimaryKeyHandle(commonEkEquivalentHandle)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("getPrimaryKeyHandle() failed: %v", err)
|
|
|
|
}
|
|
|
|
if ekHnd != commonEkEquivalentHandle {
|
|
|
|
t.Fatalf("bad EK-equivalent handle: got 0x%x, wanted 0x%x", ekHnd, commonEkEquivalentHandle)
|
|
|
|
}
|
|
|
|
|
2019-09-19 20:13:11 +00:00
|
|
|
ekHnd, p, err := tpm.tpm.getPrimaryKeyHandle(commonEkEquivalentHandle)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("second getPrimaryKeyHandle() failed: %v", err)
|
|
|
|
}
|
|
|
|
if ekHnd != commonEkEquivalentHandle {
|
|
|
|
t.Fatalf("bad EK-equivalent handle: got 0x%x, wanted 0x%x", ekHnd, commonEkEquivalentHandle)
|
|
|
|
}
|
|
|
|
if p {
|
|
|
|
t.Fatalf("generated a new key the second time; that shouldn't happen")
|
|
|
|
}
|
|
|
|
}
|