| 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 attest |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"crypto" |
| 20 |
"encoding/base64" |
| 21 |
"encoding/json" |
| 22 |
"os" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"github.com/google/go-tpm/legacy/tpm2" |
| 26 |
) |
| 27 |
|
| 28 |
// Dump describes the layout of serialized information from the dump command. |
| 29 |
type Dump struct { |
| 30 |
Static struct { |
| 31 |
EKPem []byte |
| 32 |
} |
| 33 |
|
| 34 |
AK AttestationParameters |
| 35 |
|
| 36 |
Quote struct { |
| 37 |
Nonce []byte |
| 38 |
Alg HashAlg |
| 39 |
Quote []byte |
| 40 |
Signature []byte |
| 41 |
} |
| 42 |
|
| 43 |
Log struct { |
| 44 |
PCRs []PCR |
| 45 |
PCRAlg tpm2.Algorithm |
| 46 |
Raw []byte // The measured boot log in binary form. |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestParseEventLogWindows(t *testing.T) { |
| 51 |
testParseEventLog(t, "testdata/windows_gcp_shielded_vm.json") |
| 52 |
} |
| 53 |
|
| 54 |
func testParseEventLog(t *testing.T, testdata string) { |
| 55 |
data, err := os.ReadFile(testdata) |
| 56 |
if err != nil { |
| 57 |
t.Fatalf("reading test data: %v", err) |
| 58 |
} |
| 59 |
var dump Dump |
| 60 |
if err := json.Unmarshal(data, &dump); err != nil { |
| 61 |
t.Fatalf("parsing test data: %v", err) |
| 62 |
} |
| 63 |
if _, err := ParseEventLog(dump.Log.Raw); err != nil { |
| 64 |
t.Fatalf("parsing event log: %v", err) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
func TestParseCryptoAgileEventLog(t *testing.T) { |
| 69 |
data, err := os.ReadFile("testdata/crypto_agile_eventlog") |
| 70 |
if err != nil { |
| 71 |
t.Fatalf("reading test data: %v", err) |
| 72 |
} |
| 73 |
if _, err := ParseEventLog(data); err != nil { |
| 74 |
t.Fatalf("parsing event log: %v", err) |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestEventLog(t *testing.T) { |
| 79 |
testEventLog(t, "testdata/windows_gcp_shielded_vm.json") |
| 80 |
} |
| 81 |
|
| 82 |
func testEventLog(t *testing.T, testdata string) { |
| 83 |
data, err := os.ReadFile(testdata) |
| 84 |
if err != nil { |
| 85 |
t.Fatalf("reading test data: %v", err) |
| 86 |
} |
| 87 |
var dump Dump |
| 88 |
if err := json.Unmarshal(data, &dump); err != nil { |
| 89 |
t.Fatalf("parsing test data: %v", err) |
| 90 |
} |
| 91 |
|
| 92 |
ak, err := ParseAKPublic(dump.AK.Public) |
| 93 |
if err != nil { |
| 94 |
t.Fatalf("parsing AK: %v", err) |
| 95 |
} |
| 96 |
if err := ak.Verify(Quote{ |
| 97 |
Quote: dump.Quote.Quote, |
| 98 |
Signature: dump.Quote.Signature, |
| 99 |
}, dump.Log.PCRs, dump.Quote.Nonce); err != nil { |
| 100 |
t.Fatalf("verifying quote: %v", err) |
| 101 |
} |
| 102 |
|
| 103 |
el, err := ParseEventLog(dump.Log.Raw) |
| 104 |
if err != nil { |
| 105 |
t.Fatalf("parsing event log: %v", err) |
| 106 |
} |
| 107 |
events, err := el.Verify(dump.Log.PCRs) |
| 108 |
if err != nil { |
| 109 |
t.Fatalf("validating event log: %v", err) |
| 110 |
} |
| 111 |
|
| 112 |
for i, e := range events { |
| 113 |
if e.sequence != i { |
| 114 |
t.Errorf("event out of order: events[%d].sequence = %d, want %d", i, e.sequence, i) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestParseEventLogEventSizeTooLarge(t *testing.T) { |
| 120 |
data := []byte{ |
| 121 |
// PCR index |
| 122 |
0x30, 0x34, 0x39, 0x33, |
| 123 |
// type |
| 124 |
0x36, 0x30, 0x30, 0x32, |
| 125 |
|
| 126 |
// Digest |
| 127 |
0x31, 0x39, 0x36, 0x33, 0x39, 0x34, 0x34, 0x37, 0x39, 0x32, |
| 128 |
0x31, 0x32, 0x32, 0x37, 0x39, 0x30, 0x34, 0x30, 0x31, 0x6d, |
| 129 |
|
| 130 |
// Event size (3.183 GB) |
| 131 |
0xbd, 0xbf, 0xef, 0x47, |
| 132 |
|
| 133 |
// "event data" |
| 134 |
0x00, 0x00, 0x00, 0x00, |
| 135 |
} |
| 136 |
|
| 137 |
// If this doesn't panic, the test passed |
| 138 |
// TODO(ericchiang): use errors.As once go-attestation switches to Go 1.13. |
| 139 |
_, err := ParseEventLog(data) |
| 140 |
if err == nil { |
| 141 |
t.Fatalf("expected parsing invalid event log to fail") |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestParseEventLogEventSizeZero(t *testing.T) { |
| 146 |
data := []byte{ |
| 147 |
// PCR index |
| 148 |
0x4, 0x0, 0x0, 0x0, |
| 149 |
|
| 150 |
// type |
| 151 |
0xd, 0x0, 0x0, 0x0, |
| 152 |
|
| 153 |
// Digest |
| 154 |
0x94, 0x2d, 0xb7, 0x4a, 0xa7, 0x37, 0x5b, 0x23, 0xea, 0x23, |
| 155 |
0x58, 0xeb, 0x3b, 0x31, 0x59, 0x88, 0x60, 0xf6, 0x90, 0x59, |
| 156 |
|
| 157 |
// Event size (0 B) |
| 158 |
0x0, 0x0, 0x0, 0x0, |
| 159 |
|
| 160 |
// no "event data" |
| 161 |
} |
| 162 |
|
| 163 |
if _, err := parseRawEvent(bytes.NewBuffer(data), nil); err != nil { |
| 164 |
t.Fatalf("parsing event log: %v", err) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestParseEventLog2EventSizeZero(t *testing.T) { |
| 169 |
data := []byte{ |
| 170 |
// PCR index |
| 171 |
0x0, 0x0, 0x0, 0x0, |
| 172 |
|
| 173 |
// type |
| 174 |
0x7, 0x0, 0x0, 0x0, |
| 175 |
|
| 176 |
// number of digests |
| 177 |
0x1, 0x0, 0x0, 0x0, |
| 178 |
|
| 179 |
// algorithm |
| 180 |
0xb, 0x0, |
| 181 |
|
| 182 |
// Digest |
| 183 |
0xc8, 0xe3, 0x88, 0xb4, 0x79, 0x12, 0x86, 0x0c, |
| 184 |
0x66, 0xa1, 0x5d, 0xad, 0xc4, 0x34, 0xf5, 0xdf, |
| 185 |
0x73, 0x6c, 0x3a, 0xb4, 0xbe, 0x52, 0x07, 0x08, |
| 186 |
0xdf, 0xac, 0x48, 0x2d, 0x71, 0xce, 0xa0, 0x73, |
| 187 |
|
| 188 |
// Event size (0 B) |
| 189 |
0x0, 0x0, 0x0, 0x0, |
| 190 |
|
| 191 |
// no "event data" |
| 192 |
} |
| 193 |
|
| 194 |
specID := &specIDEvent{ |
| 195 |
algs: []specAlgSize{ |
| 196 |
{ID: uint16(tpm2.AlgSHA256), Size: 32}, |
| 197 |
}, |
| 198 |
} |
| 199 |
|
| 200 |
if _, err := parseRawEvent2(bytes.NewBuffer(data), specID); err != nil { |
| 201 |
t.Fatalf("parsing event log: %v", err) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestParseShortNoAction(t *testing.T) { |
| 206 |
// https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClientSpecPlat_TPM_2p0_1p04_pub.pdf#page=110 |
| 207 |
// says: "For EV_NO_ACTION events other than the EFI Specification ID event |
| 208 |
// (Section 9.4.5.1) the log will ...". Thus it is concluded other |
| 209 |
// than "EFI Specification ID" events are also valid as NO_ACTION events. |
| 210 |
// |
| 211 |
// Currently we just assume that such events will have Data shorter than |
| 212 |
// "EFI Specification ID" field. |
| 213 |
|
| 214 |
data, err := os.ReadFile("testdata/short_no_action_eventlog") |
| 215 |
if err != nil { |
| 216 |
t.Fatalf("reading test data: %v", err) |
| 217 |
} |
| 218 |
if _, err := ParseEventLog(data); err != nil { |
| 219 |
t.Fatalf("parsing event log: %v", err) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestParseSpecIDEvent(t *testing.T) { |
| 224 |
tests := []struct { |
| 225 |
name string |
| 226 |
data []byte |
| 227 |
want []uint16 |
| 228 |
wantErr bool |
| 229 |
}{ |
| 230 |
{ |
| 231 |
name: "sha1", |
| 232 |
data: append( |
| 233 |
[]byte("Spec ID Event03"), 0x0, |
| 234 |
0x0, 0x0, 0x0, 0x0, // platform class |
| 235 |
0x0, // version minor |
| 236 |
0x2, // version major |
| 237 |
0x0, // errata |
| 238 |
0x8, // uintn size |
| 239 |
0x1, 0x0, 0x0, 0x0, // num algs |
| 240 |
0x04, 0x0, // SHA1 |
| 241 |
0x14, 0x0, // size |
| 242 |
0x2, // vendor info size |
| 243 |
0x0, 0x0, |
| 244 |
), |
| 245 |
want: []uint16{0x0004}, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "sha1_and_sha256", |
| 249 |
data: append( |
| 250 |
[]byte("Spec ID Event03"), 0x0, |
| 251 |
0x0, 0x0, 0x0, 0x0, // platform class |
| 252 |
0x0, // version minor |
| 253 |
0x2, // version major |
| 254 |
0x0, // errata |
| 255 |
0x8, // uintn size |
| 256 |
0x2, 0x0, 0x0, 0x0, // num algs |
| 257 |
0x04, 0x0, // SHA1 |
| 258 |
0x14, 0x0, // size |
| 259 |
0x0B, 0x0, // SHA256 |
| 260 |
0x20, 0x0, // size |
| 261 |
0x2, // vendor info size |
| 262 |
0x0, 0x0, |
| 263 |
), |
| 264 |
want: []uint16{0x0004, 0x000B}, |
| 265 |
}, |
| 266 |
{ |
| 267 |
name: "invalid_version", |
| 268 |
data: append( |
| 269 |
[]byte("Spec ID Event03"), 0x0, |
| 270 |
0x0, 0x0, 0x0, 0x0, // platform class |
| 271 |
0x2, // version minor |
| 272 |
0x1, // version major |
| 273 |
0x0, // errata |
| 274 |
0x8, // uintn size |
| 275 |
0x2, 0x0, 0x0, 0x0, // num algs |
| 276 |
0x04, 0x0, // SHA1 |
| 277 |
0x14, 0x0, // size |
| 278 |
0x0B, 0x0, // SHA256 |
| 279 |
0x20, 0x0, // size |
| 280 |
0x2, // vendor info size |
| 281 |
0x0, 0x0, |
| 282 |
), |
| 283 |
wantErr: true, |
| 284 |
}, |
| 285 |
{ |
| 286 |
name: "malicious_number_of_algs", |
| 287 |
data: append( |
| 288 |
[]byte("Spec ID Event03"), 0x0, |
| 289 |
0x0, 0x0, 0x0, 0x0, // platform class |
| 290 |
0x0, // version minor |
| 291 |
0x2, // version major |
| 292 |
0x0, // errata |
| 293 |
0x8, // uintn size |
| 294 |
0xff, 0xff, 0xff, 0xff, // num algs |
| 295 |
0x04, 0x0, // SHA1 |
| 296 |
0x14, 0x0, // size |
| 297 |
0x2, // vendor info size |
| 298 |
0x0, 0x0, |
| 299 |
), |
| 300 |
wantErr: true, |
| 301 |
}, |
| 302 |
} |
| 303 |
for _, test := range tests { |
| 304 |
t.Run(test.name, func(t *testing.T) { |
| 305 |
spec, err := parseSpecIDEvent(test.data) |
| 306 |
var algs []uint16 |
| 307 |
if (err != nil) != test.wantErr { |
| 308 |
t.Fatalf("parsing spec, wantErr=%t, got=%v", test.wantErr, err) |
| 309 |
} |
| 310 |
if err != nil { |
| 311 |
return |
| 312 |
} |
| 313 |
algsEq := func(want, got []uint16) bool { |
| 314 |
if len(got) != len(want) { |
| 315 |
return false |
| 316 |
} |
| 317 |
for i, alg := range got { |
| 318 |
if want[i] != alg { |
| 319 |
return false |
| 320 |
} |
| 321 |
} |
| 322 |
return true |
| 323 |
} |
| 324 |
|
| 325 |
for _, alg := range spec.algs { |
| 326 |
algs = append(algs, alg.ID) |
| 327 |
} |
| 328 |
|
| 329 |
if !algsEq(test.want, algs) { |
| 330 |
t.Errorf("algorithms, got=%x, want=%x", spec.algs, test.want) |
| 331 |
} |
| 332 |
}) |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
func TestEBSVerifyWorkaround(t *testing.T) { |
| 337 |
pcr5 := []PCR{ |
| 338 |
{ |
| 339 |
Index: 5, |
| 340 |
Digest: []byte{ |
| 341 |
0x31, 0x24, 0x58, 0x08, 0xd6, 0xd3, 0x58, 0x49, 0xbc, 0x39, |
| 342 |
0x4f, 0x63, 0x43, 0xf2, 0xb3, 0xff, 0x90, 0x8e, 0xd5, 0xe3, |
| 343 |
}, |
| 344 |
DigestAlg: crypto.SHA1, |
| 345 |
}, |
| 346 |
{ |
| 347 |
Index: 5, |
| 348 |
Digest: []byte{ |
| 349 |
0x6c, 0xae, 0xa1, 0x23, 0xfa, 0x61, 0x11, 0x30, 0x5e, 0xe6, 0x24, |
| 350 |
0xe4, 0x52, 0xe2, 0x69, 0xad, 0x14, 0xac, 0x52, 0x2a, 0xb8, 0xbf, |
| 351 |
0x0c, 0x88, 0xe1, 0x16, 0x16, 0xde, 0x4c, 0x22, 0x2f, 0x7d, |
| 352 |
}, |
| 353 |
DigestAlg: crypto.SHA256, |
| 354 |
}, |
| 355 |
} |
| 356 |
|
| 357 |
elr, err := os.ReadFile("testdata/ebs_event_missing_eventlog") |
| 358 |
if err != nil { |
| 359 |
t.Fatal(err) |
| 360 |
} |
| 361 |
el, err := ParseEventLog(elr) |
| 362 |
if err != nil { |
| 363 |
t.Fatalf("ParseEventLog() failed: %v", err) |
| 364 |
} |
| 365 |
if _, err := el.Verify(pcr5); err != nil { |
| 366 |
t.Errorf("Verify() failed: %v", err) |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func TestAppendEvents(t *testing.T) { |
| 371 |
base, err := os.ReadFile("testdata/ubuntu_2104_shielded_vm_no_secure_boot_eventlog") |
| 372 |
if err != nil { |
| 373 |
t.Fatalf("reading test data: %v", err) |
| 374 |
} |
| 375 |
|
| 376 |
extraLog, err := base64.StdEncoding.DecodeString(`AAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACUAAABTcGVjIElEIEV2ZW50MDMAAAAAAAACAAEC |
| 377 |
AAAABAAUAAsAIAAACAAAAAYAAAACAAAABACX3UqVWDMNeg2Hkxyy6Q35wO4yBwsAVXbW4fKD8+xm |
| 378 |
Kv75L4ecBpvSR4d6bz+A7z1prUcKPuMrAQAACAISpgJpbWFfaGFzaD1zaGEyNTYgYXBwYXJtb3I9 |
| 379 |
MSBwY2k9bm9hZXIsbm9hdHMgcHJpbnRrLmRldmttc2c9b24gc2xhYl9ub21lcmdlIGNvbnNvbGU9 |
| 380 |
dHR5UzAsMTE1MjAwbjggY29uc29sZT10dHkwIGdsaW51eC1ib290LWltYWdlPTIwMjExMDI3LjAy |
| 381 |
LjAzIHF1aWV0IHNwbGFzaCBwbHltb3V0aC5pZ25vcmUtc2VyaWFsLWNvbnNvbGVzIGxzbT1sb2Nr |
| 382 |
ZG93bix5YW1hLGxvYWRwaW4sc2FmZXNldGlkLGludGVncml0eSxhcHBhcm1vcixzZWxpbnV4LHNt |
| 383 |
YWNrLHRvbW95byxicGYgcGFuaWM9MzAgaTkxNS5lbmFibGVfcHNyPTA=`) |
| 384 |
if err != nil { |
| 385 |
t.Fatal(err) |
| 386 |
} |
| 387 |
|
| 388 |
combined, err := AppendEvents(base, extraLog) |
| 389 |
if err != nil { |
| 390 |
t.Fatalf("CombineEventLogs() failed: %v", err) |
| 391 |
} |
| 392 |
|
| 393 |
// Make sure the combined log parses successfully and has one more |
| 394 |
// event than the base log. |
| 395 |
parsedBase, err := ParseEventLog(base) |
| 396 |
if err != nil { |
| 397 |
t.Fatal(err) |
| 398 |
} |
| 399 |
parsed, err := ParseEventLog(combined) |
| 400 |
if err != nil { |
| 401 |
t.Fatalf("ParseEventLog(combined_log) failed: %v", err) |
| 402 |
} |
| 403 |
|
| 404 |
if got, want := len(parsed.rawEvents), len(parsedBase.rawEvents)+1; got != want { |
| 405 |
t.Errorf("unexpected number of events in combined log: got %d, want %d", got, want) |
| 406 |
for i, e := range parsed.rawEvents { |
| 407 |
t.Logf("logs[%d] = %+v", i, e) |
| 408 |
} |
| 409 |
} |
| 410 |
} |