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/rand"
|
|
|
|
"flag"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testTPM12 = flag.Bool("testTPM12", false, "run tests for TPM1.2")
|
|
|
|
tpm12config = &OpenConfig{TPMVersion12}
|
|
|
|
)
|
|
|
|
|
2019-07-30 17:27:57 +00:00
|
|
|
func openTPM12(t *testing.T) *TPM {
|
2019-03-28 20:21:16 +00:00
|
|
|
if !*testTPM12 {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
tpm, err := OpenTPM(tpm12config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to open tpm 1.2: %v", err)
|
|
|
|
}
|
2019-07-30 17:27:57 +00:00
|
|
|
return tpm
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTPM12Info(t *testing.T) {
|
|
|
|
tpm := openTPM12(t)
|
2019-03-28 20:21:16 +00:00
|
|
|
defer tpm.Close()
|
|
|
|
|
|
|
|
Info, err := tpm.Info()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get Vendor info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Vendor info: %s\n", Info.VendorInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTPM12PCRs(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
tpm := openTPM12(t)
|
2019-03-28 20:21:16 +00:00
|
|
|
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("Failed to get PCR values: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var indices []int
|
|
|
|
for i, PCR := range PCRs {
|
|
|
|
if i != PCR.Index {
|
|
|
|
t.Errorf("Index %d does not match the PCRindex %d\n", i, PCR.Index)
|
|
|
|
}
|
|
|
|
indices = append(indices, i)
|
|
|
|
}
|
|
|
|
sort.Ints(indices)
|
|
|
|
for i := range indices {
|
|
|
|
PCR := PCRs[i]
|
|
|
|
t.Logf("PCR %v contains value 0x%x, which was caculated using alg %v\n", PCR.Index, bytes.NewBuffer(PCR.Digest), PCR.DigestAlg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTPM12EKs(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
tpm := openTPM12(t)
|
2019-03-28 20:21:16 +00:00
|
|
|
defer tpm.Close()
|
|
|
|
|
2019-08-21 17:26:55 +00:00
|
|
|
eks, err := tpm.EKs()
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to get EKs: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:26:55 +00:00
|
|
|
if len(eks) == 0 {
|
2019-03-28 20:21:16 +00:00
|
|
|
t.Fatalf("EKs returned nothing")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func TestNewAK(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
tpm := openTPM12(t)
|
2019-03-28 20:21:16 +00:00
|
|
|
defer tpm.Close()
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
if _, err := tpm.NewAK(nil); err != nil {
|
|
|
|
t.Fatalf("NewAK failed: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTPMQuote(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
tpm := openTPM12(t)
|
|
|
|
defer tpm.Close()
|
2019-03-28 20:21:16 +00:00
|
|
|
|
2019-07-30 17:27:57 +00:00
|
|
|
nonce := make([]byte, 20)
|
|
|
|
if _, err := rand.Read(nonce); err != nil {
|
|
|
|
t.Fatalf("reading nonce: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
quote, err := ak.Quote(tpm, nonce, HashSHA1)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Quote failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Logf("Quote{version: %v, quote: %x, signature: %x}\n", quote.Version, quote.Quote, quote.Signature)
|
|
|
|
}
|
|
|
|
|
2019-10-03 19:32:50 +00:00
|
|
|
func TestParseAKPublic12(t *testing.T) {
|
2019-07-30 17:27:57 +00:00
|
|
|
tpm := openTPM12(t)
|
|
|
|
defer tpm.Close()
|
2019-03-28 20:21:16 +00:00
|
|
|
|
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-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(TPMVersion12, params.Public); err != nil {
|
|
|
|
t.Errorf("parsing AK public blob: %v", err)
|
2019-03-28 20:21:16 +00:00
|
|
|
}
|
2019-07-30 17:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTPMActivateCredential(t *testing.T) {
|
|
|
|
tpm := openTPM12(t)
|
2019-03-28 20:21:16 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
EKs, err := tpm.EKs()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to read EKs: %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: TPMVersion12,
|
2019-10-10 21:29:46 +00:00
|
|
|
AK: ak.AttestationParameters(),
|
2019-07-23 22:22:53 +00:00
|
|
|
EK: ek,
|
|
|
|
}
|
|
|
|
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
|
|
|
validation, err := ak.ActivateCredential(tpm, *challenge)
|
2019-03-28 20:21:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("ActivateCredential failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-07-23 22:22:53 +00:00
|
|
|
if !bytes.Equal(validation, secret) {
|
|
|
|
t.Errorf("secret mismatch: expected %x, got %x", secret, validation)
|
2019-03-28 20:29:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 20:21:16 +00:00
|
|
|
t.Logf("validation: %x", validation)
|
|
|
|
}
|