| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/google/go-sev-guest/verify/trust" |
| 10 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 11 |
) |
| 12 |
|
| 13 |
// EvaluatePolicy succeeds if the provided MachineState complies with the |
| 14 |
// provided policy. If the state does not pass the policy, the returned error |
| 15 |
// will describe in what way the state failed. See the Policy documentation for |
| 16 |
// more information about the specifics of different policies. |
| 17 |
func EvaluatePolicy(state *pb.MachineState, policy *pb.Policy) error { |
| 18 |
if err := evaluatePlatformPolicy(state.GetPlatform(), policy.GetPlatform()); err != nil { |
| 19 |
return err |
| 20 |
} |
| 21 |
return nil |
| 22 |
} |
| 23 |
|
| 24 |
// PolicyOptions provides extra options for evaluating policy. |
| 25 |
type PolicyOptions struct { |
| 26 |
// Getter allows the policy evaluator to download reference materials if needed. |
| 27 |
Getter trust.HTTPSGetter |
| 28 |
// Now is the time to evaluate time-based constraints against. |
| 29 |
Now time.Time |
| 30 |
} |
| 31 |
|
| 32 |
// DefaultPolicyOptions returns a useful default for PolicyOptions. |
| 33 |
func DefaultPolicyOptions() *PolicyOptions { |
| 34 |
return &PolicyOptions{ |
| 35 |
Getter: trust.DefaultHTTPSGetter(), |
| 36 |
Now: time.Now(), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
func evaluatePlatformPolicy(state *pb.PlatformState, policy *pb.PlatformPolicy) error { |
| 41 |
allowedVersions := policy.GetAllowedScrtmVersionIds() |
| 42 |
if len(allowedVersions) > 0 { |
| 43 |
if err := hasAllowedVersion(state, allowedVersions); err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
minGceVersion := policy.GetMinimumGceFirmwareVersion() |
| 49 |
gceVersion := state.GetGceVersion() |
| 50 |
if minGceVersion > gceVersion { |
| 51 |
return fmt.Errorf("expected GCE Version %d or later, got %d", minGceVersion, gceVersion) |
| 52 |
} |
| 53 |
minTech := policy.GetMinimumTechnology() |
| 54 |
tech := state.GetTechnology() |
| 55 |
if minTech > tech { |
| 56 |
return fmt.Errorf("expected a GCE Confidential Technology of %d or later, got %d", minTech, tech) |
| 57 |
} |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
func hasAllowedVersion(state *pb.PlatformState, allowedVersions [][]byte) error { |
| 62 |
firmware := state.GetFirmware() |
| 63 |
|
| 64 |
// We want the version check to work even for a GCE VM. |
| 65 |
var version []byte |
| 66 |
if scrtm, ok := firmware.(*pb.PlatformState_ScrtmVersionId); ok { |
| 67 |
version = scrtm.ScrtmVersionId |
| 68 |
} else if gce, ok := firmware.(*pb.PlatformState_GceVersion); ok { |
| 69 |
version = ConvertGCEFirmwareVersionToSCRTMVersion(gce.GceVersion) |
| 70 |
} else { |
| 71 |
return errors.New("missing SCRTM version in PlatformState") |
| 72 |
} |
| 73 |
for _, allowed := range allowedVersions { |
| 74 |
if bytes.Equal(version, allowed) { |
| 75 |
return nil |
| 76 |
} |
| 77 |
} |
| 78 |
return fmt.Errorf("provided SCRTM version (%x) not allowed", version) |
| 79 |
} |