| 1 |
// Copyright 2019 Google Inc. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 |
// use this file except in compliance with the License. You may obtain a copy of |
| 5 |
// the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
// License for the specific language governing permissions and limitations under |
| 13 |
// the License. |
| 14 |
|
| 15 |
package eventlog |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"os" |
| 20 |
"testing" |
| 21 |
|
| 22 |
"github.com/google/go-attestation/attest" |
| 23 |
"github.com/google/go-attestation/attest/attest-tool/internal" |
| 24 |
) |
| 25 |
|
| 26 |
func parseEvents(t *testing.T, testdata string) []attest.Event { |
| 27 |
data, err := os.ReadFile(testdata) |
| 28 |
if err != nil { |
| 29 |
t.Fatalf("reading test data: %v", err) |
| 30 |
} |
| 31 |
var dump internal.Dump |
| 32 |
if err := json.Unmarshal(data, &dump); err != nil { |
| 33 |
t.Fatalf("parsing test data: %v", err) |
| 34 |
} |
| 35 |
|
| 36 |
ak, err := attest.ParseAKPublic(dump.AK.Public) |
| 37 |
if err != nil { |
| 38 |
t.Fatalf("parsing AK: %v", err) |
| 39 |
} |
| 40 |
if err := ak.Verify(attest.Quote{ |
| 41 |
Quote: dump.Quote.Quote, |
| 42 |
Signature: dump.Quote.Signature, |
| 43 |
}, dump.Log.PCRs, dump.Quote.Nonce); err != nil { |
| 44 |
t.Fatalf("verifying quote: %v", err) |
| 45 |
} |
| 46 |
|
| 47 |
el, err := attest.ParseEventLog(dump.Log.Raw) |
| 48 |
if err != nil { |
| 49 |
t.Fatalf("parsing event log: %v", err) |
| 50 |
} |
| 51 |
events, err := el.Verify(dump.Log.PCRs) |
| 52 |
if err != nil { |
| 53 |
t.Fatalf("validating event log: %v", err) |
| 54 |
} |
| 55 |
return events |
| 56 |
} |
| 57 |
|
| 58 |
func notEmpty(t *testing.T, name string, field []byte) { |
| 59 |
t.Helper() |
| 60 |
if len(field) == 0 { |
| 61 |
t.Errorf("field %s wasn't populated", name) |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func isEmpty(t *testing.T, name string, field []byte) { |
| 66 |
t.Helper() |
| 67 |
if len(field) != 0 { |
| 68 |
t.Errorf("expected field %s not to be populated", name) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestParseSecureBootWindows(t *testing.T) { |
| 73 |
events := parseEvents(t, "../../../testdata/windows_gcp_shielded_vm.json") |
| 74 |
sb, err := ParseSecureBoot(events) |
| 75 |
if err != nil { |
| 76 |
t.Fatalf("parse secure boot: %v", err) |
| 77 |
} |
| 78 |
if !sb.Enabled { |
| 79 |
t.Errorf("expected secure boot to be enabled") |
| 80 |
} |
| 81 |
notEmpty(t, "db", sb.DB) |
| 82 |
notEmpty(t, "dbx", sb.DBX) |
| 83 |
notEmpty(t, "pk", sb.PK) |
| 84 |
notEmpty(t, "kek", sb.KEK) |
| 85 |
isEmpty(t, "dbt", sb.DBT) |
| 86 |
isEmpty(t, "dbr", sb.DBR) |
| 87 |
notEmpty(t, "Authority", sb.Authority) |
| 88 |
} |