Debian Codegraph @

cel/cos_tlv.go

raw
1 // Package cel contains implementation for COS TLV event
2 package cel
3
4 import (
5 "crypto"
6 "fmt"
7 "regexp"
8 "strings"
9 "unicode/utf8"
10
11 gecel "github.com/google/go-eventlog/cel"
12 )
13
14 const (
15 // CosEventType indicates the CELR event is a COS content
16 // TODO: the value needs to be reserved in the CEL spec
17 CosEventType uint8 = 80
18 // CosEventPCR is the PCR which should be used for CosEventType events.
19 CosEventPCR = 13
20 // CosRTMR is the RTMR to be extended for COS events
21 // According to https://uefi.org/specs/UEFI/2.10/38_Confidential_Computing.html
22 // CCELMRIndex TDX Register
23 // 0 MRTD
24 // 1 RTMR[0]
25 // 2 RTMR[1]
26 // 3 RTMR[2]
27 // So:
28 // 4 RTMR[3]
29 CosRTMR = 3
30 // CosCCELMRIndex is the CCMR index to use in eventlog for COS events.
31 CosCCELMRIndex = 4
32 )
33
34 // CosType represent a COS content type in a CEL record content.
35 type CosType uint8
36
37 // Type for COS nested events
38 const (
39 ImageRefType CosType = iota
40 ImageDigestType
41 RestartPolicyType
42 ImageIDType
43 ArgType
44 EnvVarType
45 OverrideArgType
46 OverrideEnvType
47 // EventContent is empty on success, or contains an error message on failure.
48 LaunchSeparatorType
49 MemoryMonitorType
50 GpuCCModeType
51 GPUDeviceAttestationBindingType
52 )
53
54 // CosTlv is a specific event type created for the COS (Google Container-Optimized OS),
55 // used as a CEL content.
56 type CosTlv struct {
57 EventType CosType
58 EventContent []byte
59 }
60
61 // TLV returns the TLV representation of the COS TLV.
62 func (c CosTlv) TLV() (gecel.TLV, error) {
63 data, err := gecel.TLV{Type: uint8(c.EventType), Value: c.EventContent}.MarshalBinary()
64 if err != nil {
65 return gecel.TLV{}, err
66 }
67
68 return gecel.TLV{
69 Type: CosEventType,
70 Value: data,
71 }, nil
72 }
73
74 // GenerateDigest generates the digest for the given COS TLV. The whole TLV struct will
75 // be marshaled to bytes and feed into the hash algo.
76 func (c CosTlv) GenerateDigest(hashAlgo crypto.Hash) ([]byte, error) {
77 contentTLV, err := c.TLV()
78 if err != nil {
79 return nil, err
80 }
81
82 b, err := contentTLV.MarshalBinary()
83 if err != nil {
84 return nil, err
85 }
86
87 hash := hashAlgo.New()
88 if _, err = hash.Write(b); err != nil {
89 return nil, err
90 }
91 return hash.Sum(nil), nil
92 }
93
94 // ParseToCosTlv constructs a CosTlv from a TLV. It will check for the correct COS event
95 // type, and unmarshal the nested event.
96 func ParseToCosTlv(t gecel.TLV) (CosTlv, error) {
97 if !IsCosTlv(t) {
98 return CosTlv{}, fmt.Errorf("TLV type %v is not a COS event", t.Type)
99 }
100 nestedEvent := gecel.TLV{}
101 err := nestedEvent.UnmarshalBinary(t.Value)
102 if err != nil {
103 return CosTlv{}, err
104 }
105 return CosTlv{CosType(nestedEvent.Type), nestedEvent.Value}, nil
106 }
107
108 // IsCosTlv check whether a TLV is a COS TLV by its Type value.
109 func IsCosTlv(t gecel.TLV) bool {
110 return t.Type == CosEventType
111 }
112
113 // FormatEnvVar takes in an environment variable name and its value, run some checks. Concats
114 // the name and value by '=' and returns it if valid; returns an error if the name or value
115 // is invalid.
116 func FormatEnvVar(name string, value string) (string, error) {
117 if !utf8.ValidString(name) {
118 return "", fmt.Errorf("malformed env name, contains non-utf8 character: [%s]", name)
119 }
120 if !utf8.ValidString(value) {
121 return "", fmt.Errorf("malformed env value, contains non-utf8 character: [%s]", value)
122 }
123 var envVarNameRegexp = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]*$")
124 if !envVarNameRegexp.MatchString(name) {
125 return "", fmt.Errorf("malformed env name [%s], env name must start with an alpha character or '_', followed by a string of alphanumeric characters or '_' (%s)", name, envVarNameRegexp)
126 }
127 return name + "=" + value, nil
128 }
129
130 // ParseEnvVar takes in environment variable as a string (foo=bar), parses it and returns its name
131 // and value, or an error if it fails the validation check.
132 func ParseEnvVar(envvar string) (string, string, error) {
133 // switch to strings.Cut when upgrading to go 1.18
134 e := strings.SplitN(string(envvar), "=", 2)
135 if len(e) < 2 {
136 return "", "", fmt.Errorf("malformed env var, doesn't contain '=': [%s]", envvar)
137 }
138
139 if _, err := FormatEnvVar(e[0], e[1]); err != nil {
140 return "", "", err
141 }
142
143 return e[0], e[1], nil
144 }