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-03-28 20:21:16 +00:00
|
|
|
package attest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto"
|
2019-04-05 21:49:36 +00:00
|
|
|
"flag"
|
2019-12-09 18:58:36 +00:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2019-03-28 20:21:16 +00:00
|
|
|
"testing"
|
2019-04-05 21:49:36 +00:00
|
|
|
)
|
2019-03-28 20:21:16 +00:00
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
var (
|
|
|
|
testLocal = flag.Bool("testLocal", false, "run tests against local hardware")
|
2019-03-28 20:21:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOpen(t *testing.T) {
|
2019-04-05 21:49:36 +00:00
|
|
|
if !*testLocal {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
tpm, err := OpenTPM(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenTPM() failed: %v", err)
|
|
|
|
}
|
|
|
|
if tpm == nil {
|
|
|
|
t.Fatalf("Expected non-nil tpm struct")
|
|
|
|
}
|
|
|
|
defer tpm.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInfo(t *testing.T) {
|
2019-04-05 21:49:36 +00:00
|
|
|
if !*testLocal {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
tpm, err := OpenTPM(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenTPM() failed: %v", err)
|
|
|
|
}
|
|
|
|
defer tpm.Close()
|
|
|
|
|
|
|
|
info, err := tpm.Info()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("tpm.Info() failed: %v", err)
|
|
|
|
}
|
|
|
|
if info.Manufacturer.String() == "" {
|
|
|
|
t.Error("Expected info.Manufacturer.String() != ''")
|
|
|
|
}
|
|
|
|
t.Logf("TPM Info = %+v", info)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEKs(t *testing.T) {
|
2019-04-05 21:49:36 +00:00
|
|
|
if !*testLocal {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
tpm, err := OpenTPM(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenTPM() failed: %v", err)
|
|
|
|
}
|
|
|
|
defer tpm.Close()
|
|
|
|
|
|
|
|
eks, err := tpm.EKs()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("EKs() failed: %v", err)
|
|
|
|
}
|
|
|
|
if len(eks) == 0 {
|
|
|
|
t.Log("EKs() did not return anything. This could be an issue if an EK is present.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func TestAKCreateAndLoad(t *testing.T) {
|
2019-04-05 21:49:36 +00:00
|
|
|
if !*testLocal {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
2019-03-28 20:21:16 +00:00
|
|
|
tpm, err := OpenTPM(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenTPM() failed: %v", err)
|
|
|
|
}
|
|
|
|
defer tpm.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)
|
|
|
|
|
2020-05-05 23:56:57 +00:00
|
|
|
k1, k2 := ak.ak.(*wrappedKey20), loaded.ak.(*wrappedKey20)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chooseEK selects the EK public which will be activated against.
|
2019-08-21 17:26:55 +00:00
|
|
|
func chooseEK(t *testing.T, eks []EK) crypto.PublicKey {
|
2019-03-28 20:21:16 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for _, ek := range eks {
|
2019-08-21 17:26:55 +00:00
|
|
|
return ek.Public
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
2019-08-21 17:26:55 +00:00
|
|
|
t.Fatalf("No suitable EK found")
|
2019-03-28 20:21:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-05 21:49:36 +00:00
|
|
|
func TestPCRs(t *testing.T) {
|
|
|
|
if !*testLocal {
|
|
|
|
t.SkipNow()
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
tpm, err := OpenTPM(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("OpenTPM() failed: %v", err)
|
|
|
|
}
|
|
|
|
defer tpm.Close()
|
|
|
|
|
2019-08-20 18:52:12 +00:00
|
|
|
PCRs, err := tpm.PCRs(HashSHA1)
|
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)
|
|
|
|
}
|
|
|
|
for i, pcr := range PCRs {
|
|
|
|
if pcr.Index != i {
|
|
|
|
t.Errorf("PCR index %d does not match map index %d", pcr.Index, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-04 22:35:21 +00:00
|
|
|
|
|
|
|
func TestBug139(t *testing.T) {
|
|
|
|
// Tests ParseAKPublic() with known parseable but non-spec-compliant blobs, and ensure
|
|
|
|
// an error is returned rather than a segfault.
|
|
|
|
// https://github.com/google/go-attestation/issues/139
|
|
|
|
badBlob := []byte{0, 1, 0, 4, 0, 1, 0, 0, 0, 0, 0, 6, 0, 128, 0, 67, 0, 16, 8, 0, 0, 1, 0, 1, 0, 0}
|
|
|
|
msg := "parsing public key: missing rsa signature scheme"
|
|
|
|
if _, err := ParseAKPublic(TPMVersion20, badBlob); err == nil || err.Error() != msg {
|
|
|
|
t.Errorf("ParseAKPublic() err = %v, want %v", err, msg)
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 18:58:36 +00:00
|
|
|
|
|
|
|
func TestBug142(t *testing.T) {
|
|
|
|
// Tests ParseEKCertificate() with a malformed size prefix which would overflow
|
|
|
|
// an int16, ensuring an error is returned rather than a panic occurring.
|
|
|
|
input := []byte{0x10, 0x01, 0x00, 0xff, 0xff, 0x20}
|
|
|
|
wantErr := fmt.Errorf("parsing nvram header: ekCert size %d smaller than specified cert length %d", len(input), 65535)
|
|
|
|
if _, err := ParseEKCertificate(input); !reflect.DeepEqual(err, wantErr) {
|
|
|
|
t.Errorf("ParseEKCertificate() = %v, want %v", err, wantErr)
|
|
|
|
}
|
|
|
|
}
|