-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcustom_key_info.go
246 lines (196 loc) · 6.81 KB
/
custom_key_info.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package keycred
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
// CustomKeyInformation represents the CUSTOM_KEY_INFORMATION structure
// (https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/701a55dc-d062-4032-a2da-dbdfc384c8cf).
type CustomKeyInformation struct {
Version uint8
Flags CustomKeyInformationFlags
VolType CustomKeyInformationVolType
SupportsNotification CustomKeyInformationSupportsNotification
FekKeyVersion uint8
KeyStrength CustomKeyInformationKeyStrengh
Reserved []byte
ExtendedInfo []*EncodedExtendedCKI
// FullRepresentation is not present in the struct, it is only used to
// distinguish between the two representations.
FullRepresentation bool
}
// Bytes returns the binary representation of the CUSTOM_KEY_INFORMATION
// structure.
func (cki *CustomKeyInformation) Bytes() []byte {
var buf bytes.Buffer
_ = writeBinary(&buf, binary.LittleEndian, cki.Version, cki.Flags)
if !cki.FullRepresentation {
return buf.Bytes()
}
reserved := cki.Reserved
if reserved == nil {
reserved = make([]byte, 10)
}
_ = writeBinary(&buf, binary.LittleEndian, cki.VolType,
cki.SupportsNotification, cki.FekKeyVersion, cki.KeyStrength, reserved)
for _, ei := range cki.ExtendedInfo {
_ = writeBinary(&buf, binary.LittleEndian, ei.Bytes())
}
return buf.Bytes()
}
// String returns a human readable string representation of the
// CUSTOM_KEY_INFORMATION structure.
func (cki *CustomKeyInformation) String() string {
if cki == nil {
return "<empty>"
}
var properties []string
if cki.Version != 1 {
properties = append(properties, fmt.Sprintf("Abnormal Version 0x%x", cki.Version))
}
if cki.Flags != 0 {
properties = append(properties, cki.Flags.String())
}
switch {
case !cki.FullRepresentation && len(properties) == 0:
properties = append(properties, "<empty stub>")
case cki.FullRepresentation:
properties = append(properties, "Volume Type: "+cki.VolType.String())
properties = append(properties, "Notifications: "+cki.SupportsNotification.String())
if cki.FekKeyVersion > 1 {
properties = append(properties, fmt.Sprintf("Abnormal FEK Key Version 0x%x", cki.FekKeyVersion))
}
properties = append(properties, "Key Strength: "+cki.KeyStrength.String())
if len(cki.ExtendedInfo) > 0 {
properties = append(properties, fmt.Sprintf("%d Extended Info Entries", len(cki.ExtendedInfo)))
}
}
return strings.Join(properties, ", ")
}
// ParseCustomKeyInformation parses the CUSTOM_KEY_INFORMATION structure from
// bytes. If strict parsing is enabled, it returns an error if a version field
// with an unexpected value is encountered.
func ParseCustomKeyInformation(data []byte, strict bool) (*CustomKeyInformation, error) {
consumer := newConsumer(data, binary.LittleEndian)
kci := &CustomKeyInformation{
Version: consumer.Uint8(),
Flags: CustomKeyInformationFlags(consumer.Uint8()),
}
if strict && kci.Version != 1 {
return nil, fmt.Errorf("custom key information version is set to %d instead of 1", kci.Version)
}
if consumer.Remaining() == 0 {
return kci, consumer.Error()
}
kci.VolType = CustomKeyInformationVolType(consumer.Uint8())
kci.SupportsNotification = CustomKeyInformationSupportsNotification(consumer.Uint8())
kci.FekKeyVersion = consumer.Uint8()
kci.KeyStrength = CustomKeyInformationKeyStrengh(consumer.Uint8())
kci.FullRepresentation = true
// Entra ID: Microsoft does not follow their own docs and only adds 0 or 9
// reserved bytes instead of 10
nReservedBytes := 10
if consumer.Remaining() == 9 || consumer.Remaining() == 0 {
nReservedBytes = consumer.Remaining()
}
kci.Reserved = consumer.Bytes(nReservedBytes)
for consumer.Remaining() > 0 {
extendedInfo := &EncodedExtendedCKI{
Version: consumer.Uint8(),
Size: consumer.Uint8(),
}
if strict && extendedInfo.Version != 0 {
return nil, fmt.Errorf("extended custom key information is set to %d instead of 0", extendedInfo.Version)
}
extendedInfo.Data = consumer.Bytes(int(extendedInfo.Size))
kci.ExtendedInfo = append(kci.ExtendedInfo, extendedInfo)
}
consumer.Bytes(consumer.Remaining())
return kci, consumer.Error()
}
// EncodedExtendedCKI holds extended custom key information in a within a
// CustomKeyInformation
// (https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-adts/b2c0cb9b-e49e-4907-9235-f9fd7eee8c13).
type EncodedExtendedCKI struct {
Version uint8
Size uint8
Data []byte
}
// Bytes returns the byte representation of the EncodedExtendedCKI.
func (e *EncodedExtendedCKI) Bytes() []byte {
return packBytes(binary.LittleEndian, e.Version, e.Size, e.Data)
}
type CustomKeyInformationFlags uint8
func (f CustomKeyInformationFlags) String() string {
var flags []string
if f&CustomKeyInformationFlagsAttestation > 0 {
flags = append(flags, "Attestation")
}
if f&CustomKeyInformationFlagsMFANotUsed > 0 {
flags = append(flags, "MFA Not Used")
}
remaining := f & ^(CustomKeyInformationFlagsAttestation | CustomKeyInformationFlagsMFANotUsed)
if remaining != 0 {
flags = append(flags, fmt.Sprintf("Unknown Flag 0x%x", uint8(remaining)))
}
return strings.Join(flags, " | ")
}
const (
CustomKeyInformationFlagsAttestation CustomKeyInformationFlags = 0x01
CustomKeyInformationFlagsMFANotUsed CustomKeyInformationFlags = 0x02
)
type CustomKeyInformationVolType uint8
func (vt CustomKeyInformationVolType) String() string {
switch vt {
case VolTypeNone:
return "Not Specified"
case VolTypeOSV:
return "OS Volume"
case VolTypeFDV:
return "Fixed Data Volume"
case VolTypeRDV:
return "Removable Data Volume"
default:
return fmt.Sprintf("Unknown Vol Type (0x%x)", int(vt))
}
}
const (
VolTypeNone CustomKeyInformationVolType = 0x00
VolTypeOSV CustomKeyInformationVolType = 0x01
VolTypeFDV CustomKeyInformationVolType = 0x02
VolTypeRDV CustomKeyInformationVolType = 0x03
)
type CustomKeyInformationSupportsNotification uint8
func (sn CustomKeyInformationSupportsNotification) String() string {
switch sn {
case SupportsNotificationNone:
return "Not Supported"
case SupportsNotificationSupported:
return "Supported"
default:
return fmt.Sprintf("Unknown Notification Support (0x%x)", int(sn))
}
}
const (
SupportsNotificationNone CustomKeyInformationSupportsNotification = 0x00
SupportsNotificationSupported CustomKeyInformationSupportsNotification = 0x01
)
type CustomKeyInformationKeyStrengh uint8
func (s CustomKeyInformationKeyStrengh) String() string {
switch s {
case KeyStrengthUnknown:
return "Unknown"
case KeyStrengthWeak:
return "Weak"
case KeyStrengthNormal:
return "Normal"
default:
return fmt.Sprintf("Unknown Key Strength (0x%x)", int(s))
}
}
const (
KeyStrengthUnknown CustomKeyInformationKeyStrengh = 0x00
KeyStrengthWeak CustomKeyInformationKeyStrengh = 0x01
KeyStrengthNormal CustomKeyInformationKeyStrengh = 0x02
)