-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathflagsmap.go
227 lines (201 loc) · 6.09 KB
/
flagsmap.go
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
// Copyright 2023-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"fmt"
"github.com/veraison/corim/encoding"
"github.com/veraison/corim/extensions"
)
var True = true
var False = false
// Flag indicates whether a particular operational mode is active within the
// measured environment.
type Flag int
const (
FlagIsConfigured Flag = iota
FlagIsSecure
FlagIsRecovery
FlagIsDebug
FlagIsReplayProtected
FlagIsIntegrityProtected
FlagIsRuntimeMeasured
FlagIsImmutable
FlagIsTcb
)
// FlagsMap describes a number of boolean operational modes. If a value is nil,
// then the operational mode is unknown.
type FlagsMap struct {
// IsConfigured indicates whether the measured environment is fully
// configured for normal operation.
IsConfigured *bool `cbor:"0,keyasint,omitempty" json:"is-configured,omitempty"`
// IsSecure indicates whether the measured environment's configurable
// security settings are fully enabled.
IsSecure *bool `cbor:"1,keyasint,omitempty" json:"is-secure,omitempty"`
// IsRecovery indicates whether the measured environment is in recovery
// mode.
IsRecovery *bool `cbor:"2,keyasint,omitempty" json:"is-recovery,omitempty"`
// IsDebug indicates whether the measured environment is in a debug
// enabled mode.
IsDebug *bool `cbor:"3,keyasint,omitempty" json:"is-debug,omitempty"`
// IsReplayProtected indicates whether the measured environment is
// protected from replay by a previous image that differs from the
// current image.
IsReplayProtected *bool `cbor:"4,keyasint,omitempty" json:"is-replay-protected,omitempty"`
// IsIntegrityProtected indicates whether the measured environment is
// protected from unauthorized update.
IsIntegrityProtected *bool `cbor:"5,keyasint,omitempty" json:"is-integrity-protected,omitempty"`
// IsRuntimeMeasured indicates whether the measured environment is
// measured after being loaded into memory.
IsRuntimeMeasured *bool `cbor:"6,keyasint,omitempty" json:"is-runtime-meas,omitempty"`
// IsImmutable indicates whether the measured environment is immutable.
IsImmutable *bool `cbor:"7,keyasint,omitempty" json:"is-immutable,omitempty"`
// IsTcb indicates whether the measured environment is a trusted
// computing base.
IsTcb *bool `cbor:"8,keyasint,omitempty" json:"is-tcb,omitempty"`
Extensions
}
func NewFlagsMap() *FlagsMap {
return &FlagsMap{}
}
// nolint:gocritic
func (o FlagsMap) IsEmpty() bool {
if o.IsConfigured != nil || o.IsSecure != nil || o.IsRecovery != nil ||
o.IsDebug != nil || o.IsReplayProtected != nil || o.IsIntegrityProtected != nil ||
o.IsRuntimeMeasured != nil || o.IsImmutable != nil || o.IsTcb != nil {
return false
}
return o.Extensions.IsEmpty()
}
func (o *FlagsMap) AnySet() bool {
if o.IsConfigured != nil || o.IsSecure != nil || o.IsRecovery != nil || o.IsDebug != nil ||
o.IsReplayProtected != nil || o.IsIntegrityProtected != nil ||
o.IsRuntimeMeasured != nil || o.IsImmutable != nil || o.IsTcb != nil {
return true
}
return o.Extensions.anySet()
}
func (o *FlagsMap) setFlag(value *bool, flags ...Flag) {
for _, flag := range flags {
switch flag {
case FlagIsConfigured:
o.IsConfigured = value
case FlagIsSecure:
o.IsSecure = value
case FlagIsRecovery:
o.IsRecovery = value
case FlagIsDebug:
o.IsDebug = value
case FlagIsReplayProtected:
o.IsReplayProtected = value
case FlagIsIntegrityProtected:
o.IsIntegrityProtected = value
case FlagIsRuntimeMeasured:
o.IsRuntimeMeasured = value
case FlagIsImmutable:
o.IsImmutable = value
case FlagIsTcb:
o.IsTcb = value
default:
if value == &True {
o.Extensions.setTrue(flag)
} else {
o.Extensions.setFalse(flag)
}
}
}
}
func (o *FlagsMap) SetTrue(flags ...Flag) {
o.setFlag(&True, flags...)
}
func (o *FlagsMap) SetFalse(flags ...Flag) {
o.setFlag(&False, flags...)
}
func (o *FlagsMap) Clear(flags ...Flag) {
for _, flag := range flags {
switch flag {
case FlagIsConfigured:
o.IsConfigured = nil
case FlagIsSecure:
o.IsSecure = nil
case FlagIsRecovery:
o.IsRecovery = nil
case FlagIsDebug:
o.IsDebug = nil
case FlagIsReplayProtected:
o.IsReplayProtected = nil
case FlagIsIntegrityProtected:
o.IsIntegrityProtected = nil
case FlagIsRuntimeMeasured:
o.IsRuntimeMeasured = nil
case FlagIsImmutable:
o.IsImmutable = nil
case FlagIsTcb:
o.IsTcb = nil
default:
o.Extensions.clear(flag)
}
}
}
func (o *FlagsMap) Get(flag Flag) *bool {
switch flag {
case FlagIsConfigured:
return o.IsConfigured
case FlagIsSecure:
return o.IsSecure
case FlagIsRecovery:
return o.IsRecovery
case FlagIsDebug:
return o.IsDebug
case FlagIsReplayProtected:
return o.IsReplayProtected
case FlagIsIntegrityProtected:
return o.IsIntegrityProtected
case FlagIsRuntimeMeasured:
return o.IsRuntimeMeasured
case FlagIsImmutable:
return o.IsImmutable
case FlagIsTcb:
return o.IsTcb
default:
return o.Extensions.get(flag)
}
}
// RegisterExtensions registers a struct as a collections of extensions
func (o *FlagsMap) RegisterExtensions(exts extensions.Map) error {
for p, v := range exts {
switch p {
case ExtFlags:
o.Extensions.Register(v)
default:
return fmt.Errorf("%w: %q", extensions.ErrUnexpectedPoint, p)
}
}
return nil
}
// GetExtensions returns previously registered extension
func (o *FlagsMap) GetExtensions() extensions.IMapValue {
return o.Extensions.IMapValue
}
// UnmarshalCBOR deserializes from CBOR
func (o *FlagsMap) UnmarshalCBOR(data []byte) error {
return encoding.PopulateStructFromCBOR(dm, data, o)
}
// MarshalCBOR serializes to CBOR
// nolint:gocritic
func (o FlagsMap) MarshalCBOR() ([]byte, error) {
return encoding.SerializeStructToCBOR(em, o)
}
// UnmarshalJSON deserializes from JSON
func (o *FlagsMap) UnmarshalJSON(data []byte) error {
return encoding.PopulateStructFromJSON(data, o)
}
// MarshalJSON serializes to JSON
// nolint:gocritic
func (o FlagsMap) MarshalJSON() ([]byte, error) {
return encoding.SerializeStructToJSON(o)
}
// Valid returns an error if the FlagsMap is invalid.
// nolint:gocritic
func (o FlagsMap) Valid() error {
return o.Extensions.validFlagsMap(&o)
}