Debian Codegraph @

server/policy_test.go

raw
1 package server
2
3 import (
4 "testing"
5
6 pb "github.com/google/go-tpm-tools/proto/attest"
7 )
8
9 var defaultGcePolicy = pb.Policy{
10 Platform: &pb.PlatformPolicy{
11 MinimumGceFirmwareVersion: 1,
12 MinimumTechnology: pb.GCEConfidentialTechnology_NONE,
13 },
14 }
15
16 func TestNilPolicyAlwaysPasses(t *testing.T) {
17 subtests := []struct {
18 name string
19 state *pb.MachineState
20 }{
21 {"NilState", nil},
22 {"PlatformState", &pb.MachineState{
23 Platform: &pb.PlatformState{
24 Firmware: &pb.PlatformState_GceVersion{GceVersion: 1},
25 Technology: pb.GCEConfidentialTechnology_AMD_SEV,
26 },
27 }},
28 }
29 for _, subtest := range subtests {
30 t.Run(subtest.name, func(t *testing.T) {
31 if err := EvaluatePolicy(subtest.state, nil); err != nil {
32 t.Errorf("nil policy should always succeed: %v", err)
33 }
34 })
35 }
36 }
37
38 func TestGCEFirmwareVersionSimple(t *testing.T) {
39 zero := ConvertGCEFirmwareVersionToSCRTMVersion(0)
40 if len(zero) != 0 {
41 t.Errorf("expected empty SCRTM version, got %x", zero)
42 }
43 ver, err := ConvertSCRTMVersionToGCEFirmwareVersion(
44 ConvertGCEFirmwareVersionToSCRTMVersion(23),
45 )
46 if ver != 23 {
47 t.Errorf("convert functions aren't inverses, got %d: %v", ver, err)
48 }
49 }
50
51 func TestEvaluatePolicy(t *testing.T) {
52 tests := []struct {
53 name string
54 log eventLog
55 policy *pb.Policy
56 }{
57 {"Debian10-SHA1", Debian10GCE, &defaultGcePolicy},
58 {"RHEL8-CryptoAgile", Rhel8GCE, &defaultGcePolicy},
59 {"Ubuntu1804AmdSev-CryptoAgile", UbuntuAmdSevGCE, &defaultGcePolicy},
60 // TODO: add the tests below back once go-attestation has releases:
61 // https://github.com/google/go-attestation/pull/222/
62 // {"Ubuntu2104NoDbx-CryptoAgile", Ubuntu2104NoDbxGCE, &defaultGcePolicy},
63 // {"Ubuntu2104NoSecureBoot-CryptoAgile", Ubuntu2104NoSecureBootGCE, &defaultGcePolicy},
64 }
65
66 for _, test := range tests {
67 t.Run(test.name, func(t *testing.T) {
68 machineState, err := parsePCClientEventLog(test.log.RawLog, test.log.Banks[0], VerifyOpts{Loader: UnsupportedLoader})
69 if err != nil {
70 t.Fatalf("failed to get machine state: %v", err)
71 }
72 if err := EvaluatePolicy(machineState, test.policy); err != nil {
73 t.Errorf("failed to apply policy: %v", err)
74 }
75 })
76 }
77 }
78
79 func TestEvaluatePolicySCRTM(t *testing.T) {
80 archLinuxWorkstationSCRTMPolicy := pb.Policy{
81 Platform: &pb.PlatformPolicy{
82 AllowedScrtmVersionIds: [][]byte{{0x1e, 0xfb, 0x6b, 0x54, 0x0c, 0x1d, 0x55, 0x40, 0xa4, 0xad,
83 0x4e, 0xf4, 0xbf, 0x17, 0xb8, 0x3a}},
84 },
85 }
86 machineState, err := parsePCClientEventLog(ArchLinuxWorkstation.RawLog, ArchLinuxWorkstation.Banks[0],
87 VerifyOpts{Loader: UnsupportedLoader, AllowEFIAppBeforeCallingEvent: true, AllowEmptySBVar: true})
88 if err != nil {
89 t.Fatalf("failed to get machine state: %v", err)
90 }
91 if err := EvaluatePolicy(machineState, &archLinuxWorkstationSCRTMPolicy); err != nil {
92 t.Errorf("failed to apply policy: %v", err)
93 }
94 }
95
96 func TestEvaluatePolicyFailure(t *testing.T) {
97 badGcePolicyVersion := pb.Policy{
98 Platform: &pb.PlatformPolicy{
99 MinimumGceFirmwareVersion: 2,
100 MinimumTechnology: pb.GCEConfidentialTechnology_NONE,
101 },
102 }
103 badGcePolicySEVES := pb.Policy{
104 Platform: &pb.PlatformPolicy{
105 MinimumGceFirmwareVersion: 0,
106 MinimumTechnology: pb.GCEConfidentialTechnology_AMD_SEV_ES,
107 },
108 }
109 badGcePolicySEV := pb.Policy{
110 Platform: &pb.PlatformPolicy{
111 MinimumGceFirmwareVersion: 0,
112 MinimumTechnology: pb.GCEConfidentialTechnology_AMD_SEV_ES,
113 },
114 }
115 badPhysicalPolicy := pb.Policy{
116 Platform: &pb.PlatformPolicy{
117 AllowedScrtmVersionIds: [][]byte{{0x00}},
118 },
119 }
120 tests := []struct {
121 name string
122 log eventLog
123 policy *pb.Policy
124 opts VerifyOpts
125 }{
126 {"Debian10-SHA1", Debian10GCE, &badGcePolicyVersion, VerifyOpts{Loader: UnsupportedLoader}},
127 {"Debian10-SHA1", Debian10GCE, &badGcePolicySEV, VerifyOpts{Loader: UnsupportedLoader}},
128 {"Ubuntu1804AmdSev-CryptoAgile", UbuntuAmdSevGCE, &badGcePolicySEVES,
129 VerifyOpts{Loader: UnsupportedLoader}},
130 {"ArchLinuxWorkstation-CryptoAgile", ArchLinuxWorkstation,
131 &badPhysicalPolicy,
132 VerifyOpts{Loader: UnsupportedLoader, AllowEFIAppBeforeCallingEvent: true, AllowEmptySBVar: true}},
133 }
134
135 for _, test := range tests {
136 t.Run(test.name, func(t *testing.T) {
137 machineState, err := parsePCClientEventLog(test.log.RawLog, test.log.Banks[0], test.opts)
138 if err != nil {
139 t.Fatalf("failed to get machine state: %v", err)
140 }
141 if err := EvaluatePolicy(machineState, test.policy); err == nil {
142 t.Errorf("expected policy failure; got success")
143 }
144 })
145 }
146 }