File: check_test.go

package info (click to toggle)
golang-github-google-go-sev-guest 0.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 820 kB
  • sloc: asm: 9; makefile: 3
file content (501 lines) | stat: -rw-r--r-- 14,383 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package main

import (
	"bytes"
	"encoding/hex"
	"encoding/pem"
	"fmt"
	"os"
	"os/exec"
	"strconv"
	"testing"
	"time"

	"github.com/google/go-sev-guest/abi"
	"github.com/google/go-sev-guest/kds"
	checkpb "github.com/google/go-sev-guest/proto/check"
	kpb "github.com/google/go-sev-guest/proto/fakekds"
	fakesev "github.com/google/go-sev-guest/testing"
	"github.com/google/go-sev-guest/verify/testdata"
	"github.com/google/logger"
	"go.uber.org/multierr"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

// Returns true if the test should be skipped for the protobuf case since the field
// can't be set to the expected value.
type setterFn func(p *checkpb.Policy, value string, t *testing.T) bool

// Represents a test case that will set a flag or config field to a good or bad value.
// We use this data to check that
//
//   - flags alone lead to expected check success or failure,
//   - a config alone leads to expected check success or failure,
//   - a config set to a bad value and a flag set to a good value leads
//     to an expected override and success.
//   - a config set to a good value and a flag set to a bad value leads
//     to an expected override and failure.
type testCase struct {
	flag   string
	good   string
	bad    []string
	setter setterFn
}

var goodPolicy = abi.SnpPolicyToBytes(abi.SnpPolicy{
	Debug: true,
	SMT:   true,
})

const (
	goodChipID = "3ac3fe21e13fb0990eb28a802e3fb6a29483a6b0753590c951bdd3b8e53786184ca39e359669a2b76a1936776b564ea464cdce40c05f63c9b610c5068b006b5d"
	goodTcb    = 4901323769462652930
)

var check string
var kdsdatabase string

func TestMain(m *testing.M) {
	if output, err := exec.Command("go", "build", ".").CombinedOutput(); err != nil {
		die(fmt.Errorf("could not build check tool: %v, %s", err, output))
	}
	check = "./check"

	// Create a kdsdatabase using testdata for the attestation-producer's VCEK:
	dbfile, err := os.CreateTemp(".", "kdsdatabase.bin")
	if err != nil {
		die(err)
	}
	chipid, _ := hex.DecodeString(goodChipID)
	db := &kpb.Certificates{ChipCerts: []*kpb.Certificates_ChipTCBCerts{
		{
			ChipId: chipid,
			TcbCerts: map[uint64][]byte{
				goodTcb: testdata.VcekBytes,
			},
		},
	}}
	dbbytes, err := proto.Marshal(db)
	if err != nil {
		die(err)
	}
	n, err := dbfile.Write(dbbytes)
	if err != nil {
		die(err)
	}
	if n != len(dbbytes) {
		die(fmt.Errorf("kdsdatabase not fully written"))
	}
	kdsdatabase = dbfile.Name()
	logger.Init("CheckTestLog", false, false, os.Stderr)
	defer os.Remove(dbfile.Name())
	os.Exit(m.Run())
}

// Work around the fact that Golang ellipsis unpacking doesn't also pack up
// extra singular arguments prior to the unpack.
// This means given
//
//	func f(...T)
//	var a, b T
//	var c []T
//
// then
//
//	f(a, b, c...) doesn't typecheck.
//
// We'd expect the arguments to pack like f([]T{a, b, c...}...) but nope. The array
// expression []T{a, b, c...} is also invalid.
func withBaseArgs(config string, args ...string) []string {
	base := []string{
		"-in", "../../verify/testdata/attestation.bin",
		"-kdsdatabase", kdsdatabase,
	}
	if config != "" {
		base = append(base, fmt.Sprintf("-config=%s", config))
	} else {
		base = append(base, fmt.Sprintf("-guest_policy=%d", goodPolicy))
	}

	result := make([]string, len(args)+len(base))
	copy(result, base)
	copy(result[len(base):], args)
	return result
}

func setField(p *checkpb.Policy, name string, value any) {
	r := p.ProtoReflect()
	ty := r.Descriptor()
	r.Set(ty.Fields().ByName(protoreflect.Name(name)), protoreflect.ValueOf(value))
}

func bytesSetter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		v, err := hex.DecodeString(value)
		if err != nil {
			return true
		}
		setField(p, name, v)
		return false
	}
}

func stringSetter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		setField(p, name, value)
		return false
	}
}

func boolSetter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		switch value {
		case "true":
			setField(p, name, true)
		case "false":
			setField(p, name, false)
		default:
			return true
		}
		return false

	}
}

func uint64setter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		u, err := strconv.ParseUint(value, 10, 64)
		if err != nil {
			return true
		}
		setField(p, name, u)
		return false
	}
}

func uint32setter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		u, err := strconv.ParseUint(value, 10, 32)
		if err != nil {
			return true
		}
		setField(p, name, uint32(u))
		return false
	}
}

func uint32valueSetter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		u, err := strconv.ParseUint(value, 10, 32)
		if err != nil {
			return true
		}
		setField(p, name, wrapperspb.UInt32(uint32(u)).ProtoReflect())
		return false
	}
}

func uint64valueSetter(name string) setterFn {
	return func(p *checkpb.Policy, value string, _ *testing.T) bool {
		u, err := strconv.ParseUint(value, 10, 64)
		if err != nil {
			return true
		}
		setField(p, name, wrapperspb.UInt64(u).ProtoReflect())
		return false
	}
}

func testCases() []testCase {
	return []testCase{
		{
			flag: "report_data",
			good: "01020304050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
			bad: []string{
				"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
				"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100",
				"not even hex",
			},
			setter: bytesSetter("report_data"),
		},
		{
			flag: "host_data",
			good: "0000000000000000000000000000000000000000000000000000000000000000",
			bad: []string{
				"0000000000000000000000000000000000000000000000000000000000000001",   // right size
				"000000000000000000000000000000000000000000000000000000000000000001", // wrong size
			},
			setter: bytesSetter("host_data"),
		},
		{
			flag:   "family_id",
			good:   "00000000000000000000000000000000",
			bad:    []string{"00000000000000000000000000000001"},
			setter: bytesSetter("family_id"),
		},
		{
			flag:   "image_id",
			good:   "00000000000000000000000000000000",
			bad:    []string{"00000000000000000000000000000001"},
			setter: bytesSetter("image_id"),
		},
		{
			flag:   "report_id",
			good:   "8edc638e1857c555d21f6b11bda3c8b1b5a09dba4852b4c8ee7aa2f16f22cc0a",
			bad:    []string{},
			setter: bytesSetter("report_id"),
		},
		{
			flag:   "report_id_ma",
			good:   "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
			bad:    []string{},
			setter: bytesSetter("report_id_ma"),
		},
		{
			flag:   "measurement",
			good:   "b07af9620f3b839b47996422ddec6058338951d984e312115131ea82705eaf5b6bdf8a9ece31a5a608eb0cf2e4872b01",
			bad:    []string{},
			setter: bytesSetter("measurement"),
		},
		{
			flag: "chip_id",
			good: goodChipID,
			bad: []string{
				"3ac3fe21e13fb0990eb28a802e3fb6a29483a6b0753590c951bdd3b8e53786184ca39e359669a2b76a1936776b564ea464cdce40c05f63c9b610c5068b006b5d00",
				"3ac3fe21e13fb0990eb28a802e3fb6a29483a6b0753590c951bdd3b8e53786184ca39e359669a2b76a1936776b564ea464cdce40c05f63c9b610c5068b006b5e",
			},
			setter: bytesSetter("chip_id"),
		},
		{
			flag:   "minimum_tcb",
			good:   "4901323769462652930",
			bad:    []string{"4901323769462652931"},
			setter: uint64setter("minimum_tcb"),
		},
		{
			flag:   "minimum_launch_tcb",
			good:   "4901323769462652930",
			bad:    []string{"4901323769462652931"},
			setter: uint64setter("minimum_launch_tcb"),
		},
		{
			flag:   "guest_policy",
			good:   fmt.Sprintf("%d", goodPolicy),
			bad:    []string{"0", "debug"},
			setter: uint64setter("policy"),
		},
		{
			flag:   "min_build",
			good:   "0",
			bad:    []string{"257", "90"},
			setter: uint32setter("minimum_build"),
		},
		{
			flag:   "min_version",
			good:   "1.49",
			bad:    []string{"0.0.0", "1.50", "0.", ".0"},
			setter: stringSetter("minimum_version"),
		},
		{
			flag:   "provisional",
			good:   "true",
			bad:    nil, // The example doesn't have provional firmware
			setter: boolSetter("permit_provisional_firmware"),
		},
		{
			flag:   "require_author_key",
			good:   "false",
			bad:    []string{"true"},
			setter: boolSetter("require_author_key"),
		},
		{
			flag:   "require_idblock",
			good:   "false", // The example doesn't have an IDBLOCK.
			bad:    []string{"true", "yes"},
			setter: boolSetter("require_id_block"),
		},
		{
			flag:   "vmpl",
			good:   "0",
			bad:    []string{"1", "4", "wrong", "-1"},
			setter: uint32valueSetter("vmpl"),
		},
		{
			flag:   "platform_info",
			good:   "1",
			bad:    []string{"0"},
			setter: uint64valueSetter("platform_info"),
		},
	}
}

// Writes contents to a file that the runner gets a path to and can use, then deletes the file.
func withTempFile(contents []byte, t *testing.T, runner func(path string)) {
	file, err := os.CreateTemp(".", "temp")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(file.Name())

	n, err := file.Write(contents)
	if err != nil {
		t.Fatal(err)
	}
	if n != len(contents) {
		t.Fatalf("incomplete write to %q. Wrote %d, want %d", file.Name(), n, len(contents))
	}
	runner(file.Name())
}

func withTestConfig(p *checkpb.Policy, t *testing.T, runner func(path string)) {
	config := &checkpb.Config{Policy: p}

	out, err := proto.Marshal(config)
	if err != nil {
		t.Fatal(err)
	}
	withTempFile(out, t, runner)
}

func TestCheckGoodFlags(t *testing.T) {
	for _, tc := range testCases() {
		// Singular good flag
		t.Run(tc.flag, func(t *testing.T) {
			cmd := exec.Command(check, withBaseArgs("", fmt.Sprintf("-%s=%s", tc.flag, tc.good), "--product_name=Milan-B0")...)
			if output, err := cmd.CombinedOutput(); err != nil {
				t.Errorf("%s failed unexpectedly: %v (%s)", cmd, err, output)
			}
		})
	}
}

func TestCheckBadFlags(t *testing.T) {
	for _, tc := range testCases() {
		// Singular bad flags
		for i, bad := range tc.bad {
			t.Run(fmt.Sprintf("%s[%d]", tc.flag, i+1), func(t *testing.T) {
				cmd := exec.Command(check, withBaseArgs("", fmt.Sprintf("-%s=%s", tc.flag, bad), "--product_name=Milan-B0")...)
				if output, err := cmd.CombinedOutput(); err == nil {
					t.Errorf("%s succeeded unexpectedly: %s", cmd, output)
				}
			})
		}
	}
}

func TestCheckGoodFields(t *testing.T) {
	for _, tc := range testCases() {
		t.Run(tc.flag, func(t *testing.T) {
			p := &checkpb.Policy{Policy: goodPolicy}
			if tc.setter(p, tc.good, t) {
				t.Fatal("unexpected parse failure")
			}
			withTestConfig(p, t, func(path string) {
				cmd := exec.Command(check, withBaseArgs(path, "--product_name=Milan-B0")...)
				if output, err := cmd.CombinedOutput(); err != nil {
					t.Errorf("%s (%v) failed unexpectedly: %v, %s", cmd, p, err, output)
				}
			})
		})
	}
}

func TestCheckBadFields(t *testing.T) {
	for _, tc := range testCases() {
		for i, bad := range tc.bad {
			t.Run(fmt.Sprintf("%s_bad[%d]", tc.flag, i+1), func(t *testing.T) {
				p := &checkpb.Policy{Policy: goodPolicy}
				if tc.setter(p, bad, t) {
					return
				}
				withTestConfig(p, t, func(path string) {
					cmd := exec.Command(check, withBaseArgs(path, "--product_name=Milan-B0")...)
					if output, err := cmd.CombinedOutput(); err == nil {
						t.Errorf("%s (%v) succeeded unexpectedly: %s", cmd, p, output)
					}
				})
			})
		}
	}
}

func TestCheckGoodFlagOverridesBadField(t *testing.T) {
	for _, tc := range testCases() {
		for i, bad := range tc.bad {
			t.Run(fmt.Sprintf("%s_bad[%d]", tc.flag, i+1), func(t *testing.T) {
				p := &checkpb.Policy{Policy: goodPolicy}
				if tc.setter(p, bad, t) {
					return
				}
				withTestConfig(p, t, func(path string) {
					cmd := exec.Command(check, withBaseArgs(path, fmt.Sprintf("-%s=%s", tc.flag, tc.good), "--product_name=Milan-B0")...)
					if output, err := cmd.CombinedOutput(); err != nil {
						t.Errorf("%s (%v) failed unexpectedly: %v, %s", cmd, p, err, output)
					}
				})
			})
		}
	}
}

func TestCheckBadFlagOverridesGoodField(t *testing.T) {
	for _, tc := range testCases() {
		for i, bad := range tc.bad {
			t.Run(fmt.Sprintf("%s_bad[%d]", tc.flag, i+1), func(t *testing.T) {
				p := &checkpb.Policy{Policy: goodPolicy}
				if tc.setter(p, tc.good, t) {
					t.Fatal("unexpected parse failure")
				}
				withTestConfig(p, t, func(path string) {
					cmd := exec.Command(check, withBaseArgs(path, fmt.Sprintf("-%s=%s", tc.flag, bad), "--product_name=Milan-B0")...)
					if output, err := cmd.CombinedOutput(); err == nil {
						t.Errorf("%s (%v) succeeded unexpectedly: %s", cmd, p, output)
					}
				})
			})
		}
	}
}

func TestCaBundles(t *testing.T) {
	signer, err := fakesev.DefaultTestOnlyCertChain(kds.DefaultProductLine(), time.Now())
	if err != nil {
		t.Fatal(err)
	}
	fakebundle := &bytes.Buffer{}
	if err := multierr.Combine(
		pem.Encode(fakebundle, &pem.Block{Type: "CERTIFICATE", Bytes: signer.Ask.Raw}),
		pem.Encode(fakebundle, &pem.Block{Type: "CERTIFICATE", Bytes: signer.Ark.Raw}),
	); err != nil {
		t.Fatal(err)
	}
	// -product only has meaning when provided with a custom product_key_path, so test together.
	goodbad := func(n int, name string) string {
		if n == 0 {
			return fmt.Sprintf("good%s", name)
		}
		return fmt.Sprintf("bad%s[%d]", name, n+1)
	}

	withTempFile(fakebundle.Bytes(), t, func(fakePath string) {
		products := []string{"Milan", "None"}
		cabundles := []string{"../../verify/testdata/milan.testcer", fakePath, "doesNotExist"}
		for i, product := range products {
			for j, cabundle := range cabundles {
				t.Run(fmt.Sprintf("%s_%s", goodbad(i, "product"), goodbad(j, "cabundle")), func(t *testing.T) {
					cmd := exec.Command(check, withBaseArgs("", fmt.Sprintf("-product=%s", product),
						fmt.Sprintf("-product_key_path=%s", cabundle))...)
					output, err := cmd.CombinedOutput()
					// Only the first pair of the cartesian product is good.
					if i == 0 && j == 0 && err != nil {
						t.Errorf("%s errored unexpectedly: %v, %s", cmd, err, output)
					} else if !(i == 0 && j == 0) && err == nil {
						t.Errorf("%s succeeded unexpectedly: %s", cmd, output)
					}
				})
			}
		}
	})
}