| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
|
| 8 |
attestationpb "github.com/GoogleCloudPlatform/confidential-space/server/proto/gen/attestation" |
| 9 |
|
| 10 |
"github.com/containerd/containerd/protobuf/proto" |
| 11 |
gecel "github.com/google/go-eventlog/cel" |
| 12 |
"github.com/google/go-eventlog/extract" |
| 13 |
gepb "github.com/google/go-eventlog/proto/state" |
| 14 |
"github.com/google/go-eventlog/register" |
| 15 |
"github.com/google/go-eventlog/tpmeventlog" |
| 16 |
"github.com/google/go-tpm-tools/cel" |
| 17 |
pb "github.com/google/go-tpm-tools/proto/attest" |
| 18 |
tpmpb "github.com/google/go-tpm-tools/proto/tpm" |
| 19 |
) |
| 20 |
|
| 21 |
// parsePCClientEventLog parses a raw event log and replays the parsed event |
| 22 |
// log against the given PCR values. It returns the corresponding MachineState |
| 23 |
// containing the events verified by particular PCR indexes/digests. It returns |
| 24 |
// an error if the replay for any PCR index does not match the provided value. |
| 25 |
// |
| 26 |
// The returned MachineState may be a partial MachineState where fields can be |
| 27 |
// the zero value. In this case, an error of type MachineStateError will be |
| 28 |
// returned. Callers can inspect individual parsing errors by examining |
| 29 |
// `MachineStateError.Errors`. |
| 30 |
// |
| 31 |
// It is the caller's responsibility to ensure that the passed PCR values can be |
| 32 |
// trusted. Users can establish trust in PCR values by either calling |
| 33 |
// client.ReadPCRs() themselves or by verifying the values via a PCR quote. |
| 34 |
// |
| 35 |
// Return a grouped error for backward compatibility. |
| 36 |
func parsePCClientEventLog(rawEventLog []byte, pcrs *tpmpb.PCRs, opts VerifyOpts) (*pb.MachineState, error) { |
| 37 |
extractOpts := extract.Opts{ |
| 38 |
AllowEmptySBVar: opts.AllowEmptySBVar, |
| 39 |
AllowEFIAppBeforeCallingEvent: opts.AllowEFIAppBeforeCallingEvent, |
| 40 |
} |
| 41 |
switch opts.Loader { |
| 42 |
case GRUB: |
| 43 |
extractOpts.Loader = extract.GRUB |
| 44 |
case UnsupportedLoader: |
| 45 |
extractOpts.Loader = extract.UnsupportedLoader |
| 46 |
default: |
| 47 |
return nil, createGroupedError("", []error{fmt.Errorf("unsupported bootloader option for event log extraction: %v", opts.Loader)}) |
| 48 |
} |
| 49 |
|
| 50 |
tcghashalgo := gepb.HashAlgo(pcrs.GetHash()) |
| 51 |
cryptoHashAlg, err := tcghashalgo.CryptoHash() |
| 52 |
if err != nil { |
| 53 |
return nil, createGroupedError("", []error{err}) |
| 54 |
} |
| 55 |
|
| 56 |
pcrRegs := make([]register.PCR, 0) |
| 57 |
|
| 58 |
for pcrIndex, digest := range pcrs.GetPcrs() { |
| 59 |
pcrRegs = append(pcrRegs, register.PCR{ |
| 60 |
Index: int(pcrIndex), |
| 61 |
Digest: digest, |
| 62 |
DigestAlg: cryptoHashAlg, |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
pcrbank := register.PCRBank{ |
| 67 |
TCGHashAlgo: tcghashalgo, |
| 68 |
PCRs: pcrRegs, |
| 69 |
} |
| 70 |
|
| 71 |
tpmfirmwarestate, err := tpmeventlog.ReplayAndExtract(rawEventLog, pcrbank, extractOpts) |
| 72 |
if err != nil { |
| 73 |
return nil, err |
| 74 |
} |
| 75 |
|
| 76 |
machineState, err := ConvertToMachineState(tpmfirmwarestate) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
return machineState, nil |
| 81 |
} |
| 82 |
|
| 83 |
// ConvertToMachineState converts a go-eventlog FirmwareLogState to a MachineState. |
| 84 |
func ConvertToMachineState(tpmfirmwarestate *gepb.FirmwareLogState) (*pb.MachineState, error) { |
| 85 |
var platformstate *pb.PlatformState |
| 86 |
if tpmfirmwarestate.GetPlatform() != nil { |
| 87 |
platformstate = &pb.PlatformState{} |
| 88 |
switch tpmfirmwarestate.GetPlatform().GetTechnology() { |
| 89 |
case gepb.GCEConfidentialTechnology_AMD_SEV: |
| 90 |
platformstate.Technology = pb.GCEConfidentialTechnology_AMD_SEV |
| 91 |
case gepb.GCEConfidentialTechnology_AMD_SEV_ES: |
| 92 |
platformstate.Technology = pb.GCEConfidentialTechnology_AMD_SEV_ES |
| 93 |
case gepb.GCEConfidentialTechnology_INTEL_TDX: |
| 94 |
platformstate.Technology = pb.GCEConfidentialTechnology_INTEL_TDX |
| 95 |
case gepb.GCEConfidentialTechnology_AMD_SEV_SNP: |
| 96 |
platformstate.Technology = pb.GCEConfidentialTechnology_AMD_SEV_SNP |
| 97 |
case gepb.GCEConfidentialTechnology_NONE: |
| 98 |
platformstate.Technology = pb.GCEConfidentialTechnology_NONE |
| 99 |
default: |
| 100 |
return nil, fmt.Errorf("unsupported GCE confidential technology: %v", tpmfirmwarestate.GetPlatform().GetTechnology()) |
| 101 |
} |
| 102 |
switch fw := tpmfirmwarestate.GetPlatform().GetFirmware().(type) { |
| 103 |
case *gepb.PlatformState_GceVersion: |
| 104 |
platformstate.Firmware = &pb.PlatformState_GceVersion{GceVersion: fw.GceVersion} |
| 105 |
case *gepb.PlatformState_ScrtmVersionId: |
| 106 |
platformstate.Firmware = &pb.PlatformState_ScrtmVersionId{ScrtmVersionId: fw.ScrtmVersionId} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
var secureBootState *pb.SecureBootState |
| 111 |
if tpmfirmwarestate.GetSecureBoot() != nil { |
| 112 |
secureBootState = &pb.SecureBootState{ |
| 113 |
Enabled: tpmfirmwarestate.GetSecureBoot().GetEnabled(), |
| 114 |
Db: convertToPbDatabase(tpmfirmwarestate.GetSecureBoot().GetDb()), |
| 115 |
Dbx: convertToPbDatabase(tpmfirmwarestate.GetSecureBoot().GetDbx()), |
| 116 |
Pk: convertToPbDatabase(tpmfirmwarestate.GetSecureBoot().GetPk()), |
| 117 |
Kek: convertToPbDatabase(tpmfirmwarestate.GetSecureBoot().GetKek()), |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
var efiState *pb.EfiState |
| 122 |
if tpmfirmwarestate.GetEfi() != nil { |
| 123 |
efiState = &pb.EfiState{} |
| 124 |
efiState.Apps = make([]*pb.EfiApp, len(tpmfirmwarestate.GetEfi().GetApps())) |
| 125 |
efiState.BootServicesDrivers = make([]*pb.EfiApp, len(tpmfirmwarestate.GetEfi().GetBootServicesDrivers())) |
| 126 |
efiState.RuntimeServicesDrivers = make([]*pb.EfiApp, len(tpmfirmwarestate.GetEfi().GetRuntimeServicesDrivers())) |
| 127 |
|
| 128 |
for i, app := range tpmfirmwarestate.GetEfi().GetApps() { |
| 129 |
efiState.Apps[i] = &pb.EfiApp{Digest: app.GetDigest()} |
| 130 |
} |
| 131 |
for i, app := range tpmfirmwarestate.GetEfi().GetBootServicesDrivers() { |
| 132 |
efiState.BootServicesDrivers[i] = &pb.EfiApp{Digest: app.GetDigest()} |
| 133 |
} |
| 134 |
for i, app := range tpmfirmwarestate.GetEfi().GetRuntimeServicesDrivers() { |
| 135 |
efiState.RuntimeServicesDrivers[i] = &pb.EfiApp{Digest: app.GetDigest()} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
var rawEvents []*pb.Event |
| 140 |
if tpmfirmwarestate.GetRawEvents() != nil { |
| 141 |
rawEvents = make([]*pb.Event, len(tpmfirmwarestate.GetRawEvents())) |
| 142 |
for i, event := range tpmfirmwarestate.GetRawEvents() { |
| 143 |
rawEvents[i] = &pb.Event{ |
| 144 |
PcrIndex: uint32(event.GetPcrIndex()), |
| 145 |
UntrustedType: uint32(event.GetUntrustedType()), |
| 146 |
Data: event.GetData(), |
| 147 |
Digest: event.GetDigest(), |
| 148 |
DigestVerified: event.GetDigestVerified(), |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
var grubState *pb.GrubState |
| 154 |
if tpmfirmwarestate.GetGrub() != nil { |
| 155 |
grubState = &pb.GrubState{} |
| 156 |
files := make([]*pb.GrubFile, len(tpmfirmwarestate.GetGrub().GetFiles())) |
| 157 |
for i, file := range tpmfirmwarestate.GetGrub().GetFiles() { |
| 158 |
files[i] = &pb.GrubFile{ |
| 159 |
Digest: file.GetDigest(), |
| 160 |
UntrustedFilename: file.GetUntrustedFilename(), |
| 161 |
} |
| 162 |
} |
| 163 |
grubState.Files = files |
| 164 |
grubState.Commands = tpmfirmwarestate.GetGrub().GetCommands() |
| 165 |
} |
| 166 |
|
| 167 |
var linuxKernel *pb.LinuxKernelState |
| 168 |
if tpmfirmwarestate.GetLinuxKernel() != nil { |
| 169 |
linuxKernel = &pb.LinuxKernelState{CommandLine: tpmfirmwarestate.GetLinuxKernel().GetCommandLine()} |
| 170 |
} |
| 171 |
|
| 172 |
return &pb.MachineState{ |
| 173 |
Platform: platformstate, |
| 174 |
SecureBoot: secureBootState, |
| 175 |
Efi: efiState, |
| 176 |
RawEvents: rawEvents, |
| 177 |
Hash: tpmpb.HashAlgo(tpmfirmwarestate.GetHash()), |
| 178 |
Grub: grubState, |
| 179 |
LinuxKernel: linuxKernel, |
| 180 |
}, nil |
| 181 |
} |
| 182 |
|
| 183 |
// ConvertToFirmwareState converts a MachineState to a go-eventlog FirmwareLogState |
| 184 |
// This is essentially an inverse function for ConvertToMachineState |
| 185 |
func ConvertToFirmwareState(ms *pb.MachineState) (*gepb.FirmwareLogState, error) { |
| 186 |
if ms == nil { |
| 187 |
return nil, nil |
| 188 |
} |
| 189 |
|
| 190 |
var platformState *gepb.PlatformState |
| 191 |
if ms.GetPlatform() != nil { |
| 192 |
platformState = &gepb.PlatformState{} |
| 193 |
switch ms.GetPlatform().GetTechnology() { |
| 194 |
case pb.GCEConfidentialTechnology_AMD_SEV: |
| 195 |
platformState.Technology = gepb.GCEConfidentialTechnology_AMD_SEV |
| 196 |
case pb.GCEConfidentialTechnology_AMD_SEV_ES: |
| 197 |
platformState.Technology = gepb.GCEConfidentialTechnology_AMD_SEV_ES |
| 198 |
case pb.GCEConfidentialTechnology_INTEL_TDX: |
| 199 |
platformState.Technology = gepb.GCEConfidentialTechnology_INTEL_TDX |
| 200 |
case pb.GCEConfidentialTechnology_AMD_SEV_SNP: |
| 201 |
platformState.Technology = gepb.GCEConfidentialTechnology_AMD_SEV_SNP |
| 202 |
case pb.GCEConfidentialTechnology_NONE: |
| 203 |
platformState.Technology = gepb.GCEConfidentialTechnology_NONE |
| 204 |
default: |
| 205 |
return nil, fmt.Errorf("unsupported GCE confidential technology: %v", ms.GetPlatform().GetTechnology()) |
| 206 |
} |
| 207 |
switch fw := ms.GetPlatform().GetFirmware().(type) { |
| 208 |
case *pb.PlatformState_GceVersion: |
| 209 |
platformState.Firmware = &gepb.PlatformState_GceVersion{GceVersion: fw.GceVersion} |
| 210 |
case *pb.PlatformState_ScrtmVersionId: |
| 211 |
platformState.Firmware = &gepb.PlatformState_ScrtmVersionId{ScrtmVersionId: fw.ScrtmVersionId} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
var secureBootState *gepb.SecureBootState |
| 216 |
if ms.GetSecureBoot() != nil { |
| 217 |
secureBootState = &gepb.SecureBootState{ |
| 218 |
Enabled: ms.GetSecureBoot().GetEnabled(), |
| 219 |
Db: convertFromPbDatabase(ms.GetSecureBoot().GetDb()), |
| 220 |
Dbx: convertFromPbDatabase(ms.GetSecureBoot().GetDbx()), |
| 221 |
Pk: convertFromPbDatabase(ms.GetSecureBoot().GetPk()), |
| 222 |
Kek: convertFromPbDatabase(ms.GetSecureBoot().GetKek()), |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
var efiState *gepb.EfiState |
| 227 |
if ms.GetEfi() != nil { |
| 228 |
efiState = &gepb.EfiState{} |
| 229 |
efiState.Apps = make([]*gepb.EfiApp, len(ms.GetEfi().GetApps())) |
| 230 |
efiState.BootServicesDrivers = make([]*gepb.EfiApp, len(ms.GetEfi().GetBootServicesDrivers())) |
| 231 |
efiState.RuntimeServicesDrivers = make([]*gepb.EfiApp, len(ms.GetEfi().GetRuntimeServicesDrivers())) |
| 232 |
|
| 233 |
for i, app := range ms.GetEfi().GetApps() { |
| 234 |
efiState.Apps[i] = &gepb.EfiApp{Digest: app.GetDigest()} |
| 235 |
} |
| 236 |
for i, app := range ms.GetEfi().GetBootServicesDrivers() { |
| 237 |
efiState.BootServicesDrivers[i] = &gepb.EfiApp{Digest: app.GetDigest()} |
| 238 |
} |
| 239 |
for i, app := range ms.GetEfi().GetRuntimeServicesDrivers() { |
| 240 |
efiState.RuntimeServicesDrivers[i] = &gepb.EfiApp{Digest: app.GetDigest()} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
var rawEvents []*gepb.Event |
| 245 |
if ms.GetRawEvents() != nil { |
| 246 |
rawEvents = make([]*gepb.Event, len(ms.GetRawEvents())) |
| 247 |
for i, event := range ms.GetRawEvents() { |
| 248 |
rawEvents[i] = &gepb.Event{ |
| 249 |
PcrIndex: uint32(event.GetPcrIndex()), |
| 250 |
// UntrustedType is not present in pb.Event, so it will be 0 |
| 251 |
Data: event.GetData(), |
| 252 |
Digest: event.GetDigest(), |
| 253 |
DigestVerified: event.GetDigestVerified(), |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
var grubState *gepb.GrubState |
| 259 |
if ms.GetGrub() != nil { |
| 260 |
grubState = &gepb.GrubState{} |
| 261 |
files := make([]*gepb.GrubFile, len(ms.GetGrub().GetFiles())) |
| 262 |
for i, file := range ms.GetGrub().GetFiles() { |
| 263 |
files[i] = &gepb.GrubFile{ |
| 264 |
Digest: file.GetDigest(), |
| 265 |
// UntrustedFilename is not present in pb.GrubFile, so it will be nil |
| 266 |
} |
| 267 |
} |
| 268 |
grubState.Files = files |
| 269 |
grubState.Commands = ms.GetGrub().GetCommands() |
| 270 |
} |
| 271 |
|
| 272 |
var linuxKernel *gepb.LinuxKernelState |
| 273 |
if ms.GetLinuxKernel() != nil { |
| 274 |
linuxKernel = &gepb.LinuxKernelState{CommandLine: ms.GetLinuxKernel().GetCommandLine()} |
| 275 |
} |
| 276 |
|
| 277 |
return &gepb.FirmwareLogState{ |
| 278 |
Platform: platformState, |
| 279 |
SecureBoot: secureBootState, |
| 280 |
Efi: efiState, |
| 281 |
RawEvents: rawEvents, |
| 282 |
Hash: gepb.HashAlgo(ms.GetHash()), |
| 283 |
Grub: grubState, |
| 284 |
LinuxKernel: linuxKernel, |
| 285 |
}, nil |
| 286 |
} |
| 287 |
|
| 288 |
// convertFromPbDatabase converts a MachineState.Database to a go-eventlog.Database. |
| 289 |
func convertFromPbDatabase(pbdb *pb.Database) *gepb.Database { |
| 290 |
if pbdb == nil { |
| 291 |
return nil |
| 292 |
} |
| 293 |
gepbCerts := make([]*gepb.Certificate, 0, len(pbdb.GetCerts())) |
| 294 |
for _, c := range pbdb.GetCerts() { |
| 295 |
var gepbCert gepb.Certificate |
| 296 |
switch rep := c.GetRepresentation().(type) { |
| 297 |
case *pb.Certificate_Der: |
| 298 |
gepbCert.Representation = &gepb.Certificate_Der{Der: rep.Der} |
| 299 |
case *pb.Certificate_WellKnown: |
| 300 |
if wkEnum, ok := gepb.WellKnownCertificate_value[rep.WellKnown.String()]; ok { |
| 301 |
gepbCert.Representation = &gepb.Certificate_WellKnown{WellKnown: gepb.WellKnownCertificate(wkEnum)} |
| 302 |
} else { |
| 303 |
// If the well-known enum doesn't map directly, treat it as unknown |
| 304 |
gepbCert.Representation = &gepb.Certificate_WellKnown{WellKnown: gepb.WellKnownCertificate_UNKNOWN} |
| 305 |
} |
| 306 |
} |
| 307 |
gepbCerts = append(gepbCerts, &gepbCert) |
| 308 |
} |
| 309 |
return &gepb.Database{ |
| 310 |
Certs: gepbCerts, |
| 311 |
Hashes: pbdb.GetHashes(), |
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
// ParseCosCELPCR takes an encoded COS CEL and PCR bank, replays the CEL against the PCRs, |
| 317 |
// and returns the AttestedCosState |
| 318 |
func ParseCosCELPCR(cosEventLog []byte, p register.PCRBank) (*pb.AttestedCosState, error) { |
| 319 |
return getCosStateFromCEL(cosEventLog, p, gecel.PCRType) |
| 320 |
} |
| 321 |
|
| 322 |
// ParseCosCELRTMR takes in a raw COS CEL and a RTMR bank, validates and returns it's |
| 323 |
// COS states as parts of the MachineState. |
| 324 |
func ParseCosCELRTMR(cosEventLog []byte, r register.RTMRBank) (*pb.AttestedCosState, error) { |
| 325 |
return getCosStateFromCEL(cosEventLog, r, gecel.CCMRType) |
| 326 |
} |
| 327 |
|
| 328 |
func getCosStateFromCEL(rawCanonicalEventLog []byte, register register.MRBank, trustingRegisterType gecel.MRType) (*pb.AttestedCosState, error) { |
| 329 |
decodedCEL, err := gecel.DecodeToCEL(bytes.NewBuffer(rawCanonicalEventLog)) |
| 330 |
if err != nil { |
| 331 |
return nil, err |
| 332 |
} |
| 333 |
// Validate the COS event log first. |
| 334 |
if err := decodedCEL.Replay(register); err != nil { |
| 335 |
return nil, err |
| 336 |
} |
| 337 |
|
| 338 |
cosState, err := getVerifiedCosState(decodedCEL, trustingRegisterType) |
| 339 |
if err != nil { |
| 340 |
return nil, err |
| 341 |
} |
| 342 |
|
| 343 |
return cosState, err |
| 344 |
} |
| 345 |
|
| 346 |
// getVerifiedCosState takes in CEL and a register type (can be PCR or CCELMR), and returns the state |
| 347 |
// in the CEL. It will only include events using the correct registerType. |
| 348 |
func getVerifiedCosState(coscel gecel.CEL, registerType gecel.MRType) (*pb.AttestedCosState, error) { |
| 349 |
cosState := &pb.AttestedCosState{} |
| 350 |
cosState.Container = &pb.ContainerState{} |
| 351 |
cosState.HealthMonitoring = &pb.HealthMonitoringState{} |
| 352 |
cosState.GpuDeviceState = &pb.GpuDeviceState{} |
| 353 |
cosState.Container.Args = make([]string, 0) |
| 354 |
cosState.Container.EnvVars = make(map[string]string) |
| 355 |
cosState.Container.OverriddenEnvVars = make(map[string]string) |
| 356 |
|
| 357 |
seenSeparator := false |
| 358 |
for _, record := range coscel.Records() { |
| 359 |
if record.IndexType != registerType { |
| 360 |
return nil, fmt.Errorf("expect registerType: %d, but get %d in a CEL record", registerType, record.IndexType) |
| 361 |
} |
| 362 |
|
| 363 |
switch record.IndexType { |
| 364 |
case gecel.PCRType: |
| 365 |
if record.Index != cel.CosEventPCR { |
| 366 |
return nil, fmt.Errorf("found unexpected PCR %d in COS CEL log", record.Index) |
| 367 |
} |
| 368 |
case gecel.CCMRType: |
| 369 |
if record.Index != cel.CosCCELMRIndex { |
| 370 |
return nil, fmt.Errorf("found unexpected CCELMR %d in COS CEL log", record.Index) |
| 371 |
} |
| 372 |
default: |
| 373 |
return nil, fmt.Errorf("unknown COS CEL log index type %d", record.IndexType) |
| 374 |
} |
| 375 |
|
| 376 |
// The Content.Type is not verified at this point, so we have to fail |
| 377 |
// if we see any events that we do not understand. This ensures that |
| 378 |
// we either verify the digest of event event in this PCR, or we fail |
| 379 |
// to replay the event log. |
| 380 |
// TODO: See if we can fix this to have the Content Type be verified. |
| 381 |
cosTlv, err := cel.ParseToCosTlv(record.Content) |
| 382 |
if err != nil { |
| 383 |
return nil, err |
| 384 |
} |
| 385 |
|
| 386 |
// verify digests for the cos cel content |
| 387 |
if err := gecel.VerifyDigests(cosTlv, record.Digests); err != nil { |
| 388 |
return nil, err |
| 389 |
} |
| 390 |
|
| 391 |
// TODO: Add support for post-separator container data |
| 392 |
if seenSeparator { |
| 393 |
return nil, fmt.Errorf("found COS Event Type %v after LaunchSeparator event", cosTlv.EventType) |
| 394 |
} |
| 395 |
|
| 396 |
switch cosTlv.EventType { |
| 397 |
case cel.ImageRefType: |
| 398 |
if cosState.Container.GetImageReference() != "" { |
| 399 |
return nil, fmt.Errorf("found more than one ImageRef event") |
| 400 |
} |
| 401 |
cosState.Container.ImageReference = string(cosTlv.EventContent) |
| 402 |
|
| 403 |
case cel.ImageDigestType: |
| 404 |
if cosState.Container.GetImageDigest() != "" { |
| 405 |
return nil, fmt.Errorf("found more than one ImageDigest event") |
| 406 |
} |
| 407 |
cosState.Container.ImageDigest = string(cosTlv.EventContent) |
| 408 |
|
| 409 |
case cel.RestartPolicyType: |
| 410 |
restartPolicy, ok := pb.RestartPolicy_value[string(cosTlv.EventContent)] |
| 411 |
if !ok { |
| 412 |
return nil, fmt.Errorf("unknown restart policy in COS eventlog: %s", string(cosTlv.EventContent)) |
| 413 |
} |
| 414 |
cosState.Container.RestartPolicy = pb.RestartPolicy(restartPolicy) |
| 415 |
|
| 416 |
case cel.ImageIDType: |
| 417 |
if cosState.Container.GetImageId() != "" { |
| 418 |
return nil, fmt.Errorf("found more than one ImageId event") |
| 419 |
} |
| 420 |
cosState.Container.ImageId = string(cosTlv.EventContent) |
| 421 |
|
| 422 |
case cel.EnvVarType: |
| 423 |
envName, envVal, err := cel.ParseEnvVar(string(cosTlv.EventContent)) |
| 424 |
if err != nil { |
| 425 |
return nil, err |
| 426 |
} |
| 427 |
cosState.Container.EnvVars[envName] = envVal |
| 428 |
|
| 429 |
case cel.ArgType: |
| 430 |
cosState.Container.Args = append(cosState.Container.Args, string(cosTlv.EventContent)) |
| 431 |
|
| 432 |
case cel.OverrideArgType: |
| 433 |
cosState.Container.OverriddenArgs = append(cosState.Container.OverriddenArgs, string(cosTlv.EventContent)) |
| 434 |
|
| 435 |
case cel.OverrideEnvType: |
| 436 |
envName, envVal, err := cel.ParseEnvVar(string(cosTlv.EventContent)) |
| 437 |
if err != nil { |
| 438 |
return nil, err |
| 439 |
} |
| 440 |
cosState.Container.OverriddenEnvVars[envName] = envVal |
| 441 |
case cel.LaunchSeparatorType: |
| 442 |
seenSeparator = true |
| 443 |
case cel.MemoryMonitorType: |
| 444 |
enabled := false |
| 445 |
if len(cosTlv.EventContent) == 1 && cosTlv.EventContent[0] == uint8(1) { |
| 446 |
enabled = true |
| 447 |
} |
| 448 |
cosState.HealthMonitoring.MemoryEnabled = &enabled |
| 449 |
case cel.GpuCCModeType: |
| 450 |
ccMode, ok := pb.GPUDeviceCCMode_value[string(cosTlv.EventContent)] |
| 451 |
if !ok { |
| 452 |
return nil, fmt.Errorf("unknown GPU device CC mode in COS eventlog: %s", string(cosTlv.EventContent)) |
| 453 |
} |
| 454 |
cosState.GpuDeviceState.CcMode = pb.GPUDeviceCCMode(ccMode) |
| 455 |
case cel.GPUDeviceAttestationBindingType: |
| 456 |
report := &attestationpb.NvidiaAttestationReport{} |
| 457 |
if err := proto.Unmarshal(cosTlv.EventContent, report); err != nil { |
| 458 |
return nil, fmt.Errorf("failed to unmarshal GPU attestation report: %v", err) |
| 459 |
} |
| 460 |
|
| 461 |
default: |
| 462 |
return nil, fmt.Errorf("found unknown COS Event Type %v", cosTlv.EventType) |
| 463 |
} |
| 464 |
|
| 465 |
} |
| 466 |
return cosState, nil |
| 467 |
} |
| 468 |
|
| 469 |
func convertToPbDatabase(gedb *gepb.Database) *pb.Database { |
| 470 |
if gedb == nil { |
| 471 |
return nil |
| 472 |
} |
| 473 |
protoCerts := make([]*pb.Certificate, 0, len(gedb.GetCerts())) |
| 474 |
for _, c := range gedb.GetCerts() { |
| 475 |
var pbCert pb.Certificate |
| 476 |
if wkEnum, err := matchWellKnownCert(c); err != nil { |
| 477 |
pbCert.Representation = &pb.Certificate_Der{Der: c.GetDer()} |
| 478 |
} else { |
| 479 |
pbCert.Representation = &pb.Certificate_WellKnown{WellKnown: wkEnum} |
| 480 |
|
| 481 |
} |
| 482 |
protoCerts = append(protoCerts, &pbCert) |
| 483 |
} |
| 484 |
return &pb.Database{ |
| 485 |
Certs: protoCerts, |
| 486 |
Hashes: gedb.GetHashes(), |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
func matchWellKnownCert(cert *gepb.Certificate) (pb.WellKnownCertificate, error) { |
| 491 |
if cert.GetWellKnown() == gepb.WellKnownCertificate_UNKNOWN { |
| 492 |
return pb.WellKnownCertificate_UNKNOWN, errors.New("failed to match well known certificate") |
| 493 |
} |
| 494 |
if wkEnum, ok := pb.WellKnownCertificate_value[cert.GetWellKnown().String()]; ok { |
| 495 |
return pb.WellKnownCertificate(wkEnum), nil |
| 496 |
} |
| 497 |
return pb.WellKnownCertificate_UNKNOWN, errors.New("failed to match well known certificate") |
| 498 |
} |