-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcustom_key_info_test.go
More file actions
170 lines (140 loc) · 5.08 KB
/
custom_key_info_test.go
File metadata and controls
170 lines (140 loc) · 5.08 KB
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
package keycred_test
import (
"bytes"
"testing"
"github.com/RedTeamPentesting/keycred"
)
func TestStubCustomKeyInformation(t *testing.T) {
t.Parallel()
cki, err := keycred.ParseCustomKeyInformation([]byte{1, byte(keycred.CustomKeyInformationFlagsMFANotUsed)}, true)
if err != nil {
t.Fatalf("cannot parse stub custom key information: %v", err)
}
if cki.Version != 1 {
t.Errorf("unexpected version: %d", cki.Version)
}
if cki.Flags != keycred.CustomKeyInformationFlagsMFANotUsed {
t.Errorf("unexpected flags: %s", cki.Flags)
}
_, err = keycred.ParseCustomKeyInformation([]byte{2, 0}, false)
if err != nil {
t.Fatalf("cannot parse stub custom key information in lax mode with unexpected version: %v", err)
}
_, err = keycred.ParseCustomKeyInformation([]byte{2, 0}, true)
if err == nil {
t.Fatalf("parsing stub custom key information with unexpected version in strict mode did not fail")
}
}
func TestCustomKeyInformation(t *testing.T) {
t.Parallel()
cki := keycred.CustomKeyInformation{
Version: 1,
Flags: keycred.CustomKeyInformationFlagsAttestation | keycred.CustomKeyInformationFlagsMFANotUsed,
VolType: keycred.VolTypeOSV,
SupportsNotification: keycred.SupportsNotificationSupported,
FekKeyVersion: 0,
KeyStrength: keycred.KeyStrengthNormal,
ExtendedInfo: []*keycred.EncodedExtendedCKI{
{
Version: 0,
Size: 5,
Data: []byte{1, 2, 3, 4, 5},
},
{
Version: 0,
Size: 1,
Data: []byte{1},
},
},
FullRepresentation: true,
}
parsed, err := keycred.ParseCustomKeyInformation(cki.Bytes(), true)
if err != nil {
t.Fatalf("cannot parse custom key information: %v", err)
}
if cki.Version != parsed.Version {
t.Errorf("version mismatch: want=%v, got=%v", cki.Version, parsed.Version)
}
if cki.Flags != parsed.Flags {
t.Errorf("flags mismatch: want=%v, got=%v", cki.Flags, parsed.Flags)
}
if cki.VolType != parsed.VolType {
t.Errorf("voltype mismatch: want=%v, got=%v", cki.VolType, parsed.VolType)
}
if cki.SupportsNotification != parsed.SupportsNotification {
t.Errorf("supportsnotifications mismatch: want=%v, got=%v", cki.SupportsNotification, parsed.SupportsNotification)
}
if cki.FekKeyVersion != parsed.FekKeyVersion {
t.Errorf("fekkeyversion mismatch: want=%v, got=%v", cki.FekKeyVersion, parsed.FekKeyVersion)
}
if cki.KeyStrength != parsed.KeyStrength {
t.Errorf("keystrength mismatch: want=%v, got=%v", cki.KeyStrength, parsed.KeyStrength)
}
if len(cki.ExtendedInfo) != len(parsed.ExtendedInfo) {
t.Errorf("got %d extended info entries instead of %d", len(parsed.ExtendedInfo), len(cki.ExtendedInfo))
} else {
for i := 0; i < len(cki.ExtendedInfo); i++ {
if !bytes.Equal(cki.ExtendedInfo[i].Bytes(), parsed.ExtendedInfo[i].Bytes()) {
t.Errorf("mismatch of extended info at index %d", i)
}
}
}
}
func TestCustomKeyInformationStrictParsing(t *testing.T) {
t.Parallel()
cki := keycred.CustomKeyInformation{
Version: 3,
Flags: keycred.CustomKeyInformationFlagsAttestation | keycred.CustomKeyInformationFlagsMFANotUsed,
VolType: keycred.VolTypeOSV,
SupportsNotification: keycred.SupportsNotificationSupported,
FekKeyVersion: 0,
KeyStrength: keycred.KeyStrengthNormal,
ExtendedInfo: []*keycred.EncodedExtendedCKI{
{
Version: 0,
Size: 5,
Data: []byte{1, 2, 3, 4, 5},
},
},
FullRepresentation: true,
}
_, err := keycred.ParseCustomKeyInformation(cki.Bytes(), false)
if err != nil {
t.Fatalf("cannot parse custom key information in lax mode: %v", err)
}
_, err = keycred.ParseCustomKeyInformation(cki.Bytes(), true)
if err == nil {
t.Fatalf("parsing custom key information with invalid version in strict mode did not fail")
}
cki.Version = 1
_, err = keycred.ParseCustomKeyInformation(cki.Bytes(), true)
if err != nil {
t.Fatalf("fixing version did not make custom key information parsable in strict mode: %v", err)
}
cki.ExtendedInfo = append(cki.ExtendedInfo, &keycred.EncodedExtendedCKI{Version: 1, Size: 1, Data: []byte{0}})
_, err = keycred.ParseCustomKeyInformation(cki.Bytes(), false)
if err != nil {
t.Fatalf("cannot parse custom key information in lax mode: %v", err)
}
_, err = keycred.ParseCustomKeyInformation(cki.Bytes(), true)
if err == nil {
t.Fatalf("parsing custom key information with invalid version in strict mode did not fail")
}
}
func TestInvalidNumberOfReservedBytesFromEntra(t *testing.T) {
t.Parallel()
cki := keycred.CustomKeyInformation{
Version: 1,
Flags: keycred.CustomKeyInformationFlagsAttestation | keycred.CustomKeyInformationFlagsMFANotUsed,
VolType: keycred.VolTypeOSV,
SupportsNotification: keycred.SupportsNotificationSupported,
FekKeyVersion: 0,
KeyStrength: keycred.KeyStrengthNormal,
Reserved: make([]byte, 9),
FullRepresentation: true,
}
_, err := keycred.ParseCustomKeyInformation(cki.Bytes(), true)
if err != nil {
t.Fatalf("cannot parse custom key information with 9 reserved bytes instead of 10: %v", err)
}
}