-
Notifications
You must be signed in to change notification settings - Fork 19
/
clientHello.go
289 lines (226 loc) · 6.14 KB
/
clientHello.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package tlsx
import "fmt"
const (
ClientHelloRandomLen = 32
)
type ClientHello struct {
TLSMessage
HandshakeType uint8
HandshakeLen uint32
HandshakeVersion Version
Random []byte
SessionIDLen uint32
SessionID []byte
CipherSuiteLen uint16
CipherSuites []CipherSuite
CompressMethods []uint8
ExtensionLen uint16
Extensions map[Extension]uint16 // [Type]Length
SNI string
SignatureAlgs []uint16
SupportedGroups []uint16
SupportedPoints []uint8
OSCP bool
ALPNs []string
}
func (ch ClientHello) String() string {
str := fmt.Sprintln("Version:", ch.Version)
str += fmt.Sprintln("Handshake Type:", ch.HandshakeType)
str += fmt.Sprintln("Handshake Version:", ch.HandshakeVersion)
str += fmt.Sprintf("SessionID: %#v\n", ch.SessionID)
str += fmt.Sprintf("Cipher Suites (%d): %v\n", ch.CipherSuiteLen, ch.CipherSuites)
str += fmt.Sprintf("Compression Methods: %v\n", ch.CompressMethods)
str += fmt.Sprintln("Extensions:", ch.Extensions)
str += fmt.Sprintf("SNI: %q\n", ch.SNI)
str += fmt.Sprintf("Signature Algorithms: %#v\n", ch.SignatureAlgs)
str += fmt.Sprintf("Groups: %#v\n", ch.SupportedGroups)
str += fmt.Sprintf("Points: %#v\n", ch.SupportedPoints)
str += fmt.Sprintf("OSCP: %v\n", ch.OSCP)
str += fmt.Sprintf("ALPNs: %v", ch.ALPNs)
return str
}
func (ch *ClientHello) Unmarshall(payload []byte) error {
ch.Raw = payload
ch.Type = uint8(payload[0])
ch.Version = Version(payload[1])<<8 | Version(payload[2])
ch.MessageLen = uint16(payload[3])<<8 | uint16(payload[4])
if ch.Type != uint8(22) {
return ErrHandshakeWrongType
}
hs := payload[5:]
if len(hs) < 6 {
return ErrHandshakeBadLength
}
ch.HandshakeType = uint8(hs[0])
if ch.HandshakeType != 1 {
return ErrHandshakeWrongType
}
ch.HandshakeLen = uint32(hs[1])<<16 | uint32(hs[2])<<8 | uint32(hs[3])
ch.HandshakeVersion = Version(hs[4])<<8 | Version(hs[5])
hs = hs[6:]
if len(hs) < ClientHelloRandomLen {
return ErrHandshakeBadLength
}
// Get random data
ch.Random = hs[:ClientHelloRandomLen]
hs = hs[ClientHelloRandomLen:]
if len(hs) < 1 {
return ErrHandshakeBadLength
}
// Get SessionID
ch.SessionIDLen = uint32(hs[0])
hs = hs[1:]
if len(hs) < int(ch.SessionIDLen) {
return ErrHandshakeBadLength
}
if ch.SessionIDLen != 0 {
ch.SessionID = hs[:ch.SessionIDLen]
}
hs = hs[ch.SessionIDLen:]
if len(hs) < 2 {
return ErrHandshakeBadLength
}
// Cipher Suite
ch.CipherSuiteLen = uint16(hs[0])<<8 | uint16(hs[1])
numCiphers := ch.CipherSuiteLen / 2
if len(hs) < int(ch.CipherSuiteLen) {
return ErrHandshakeBadLength
}
ch.CipherSuites = make([]CipherSuite, numCiphers)
for i := 0; i < int(numCiphers); i++ {
ch.CipherSuites[i] = CipherSuite(hs[2+2*i])<<8 | CipherSuite(hs[3+2*i])
}
hs = hs[2+ch.CipherSuiteLen:]
if len(hs) < 1 {
return ErrHandshakeBadLength
}
// Compression Methods
numCompressMethods := int(hs[0])
if len(hs) < 1+numCompressMethods {
return ErrHandshakeBadLength
}
ch.CompressMethods = make([]uint8, numCompressMethods)
for i := 0; i < int(numCompressMethods); i++ {
ch.CompressMethods[i] = uint8(hs[1+1*i])
}
hs = hs[1+numCompressMethods:]
if len(hs) < 2 {
// No extensions or malformed length
return ErrHandshakeBadLength
}
// Extensions
ch.ExtensionLen = uint16(hs[0])<<8 | uint16(hs[1])
if len(hs) < int(ch.ExtensionLen) {
return ErrHandshakeExtBadLength
}
hs = hs[2:]
ch.Extensions = make(map[Extension]uint16)
for len(hs) > 0 {
if len(hs) < 4 {
return ErrHandshakeExtBadLength
}
extType := Extension(hs[0])<<8 | Extension(hs[1])
length := uint16(hs[2])<<8 | uint16(hs[3])
if len(hs) < 4+int(length) {
return ErrHandshakeExtBadLength
}
data := hs[4 : 4+length]
hs = hs[4+length:]
switch extType {
case ExtServerName:
if len(data) < 2 {
return ErrHandshakeExtBadLength
}
sniLen := int(data[0])<<8 | int(data[0])
data = data[2:]
if len(data) < sniLen {
// Malformed SNI data
return ErrHandshakeExtBadLength
}
for len(data) > 0 {
nameType := data[0]
if len(data) < 3 {
// Malformed ServerName
return ErrHandshakeExtBadLength
}
nameLen := int(data[1])<<8 | int(data[2])
data = data[3:]
switch nameType {
case SNINameTypeDNS:
ch.SNI = string(data)
default:
// Unknown Name Type
}
data = data[nameLen:]
}
case ExtSignatureAlgs:
if len(data) < 2 {
return ErrHandshakeExtBadLength
}
sigLen := int(data[0])<<8 | int(data[1])
data = data[2:]
if len(data) < sigLen {
return ErrHandshakeExtBadLength
}
ch.SignatureAlgs = make([]uint16, sigLen/2)
for i := 0; i < sigLen/2; i++ {
ch.SignatureAlgs[i] = uint16(data[i*2])<<8 | uint16(data[i*2+1])
}
case ExtSupportedGroups:
if len(data) < 2 {
return ErrHandshakeExtBadLength
}
groupLen := int(data[0])<<8 | int(data[1])
data = data[2:]
if len(data) < groupLen {
// Malformed length
return ErrHandshakeExtBadLength
}
ch.SupportedGroups = make([]uint16, groupLen/2)
for i := 0; i < groupLen/2; i++ {
ch.SupportedGroups[i] = uint16(data[i*2])<<8 | uint16(data[i*2+1])
}
case ExtECPointFormats:
if len(data) < 1 {
return ErrHandshakeExtBadLength
}
pointLen := int(data[0])
data = data[1:]
if len(data) < pointLen {
return ErrHandshakeExtBadLength
}
ch.SupportedPoints = make([]uint8, pointLen)
for i := 0; i < pointLen; i++ {
ch.SupportedPoints[i] = uint8(data[i])
}
case ExtStatusRequest:
if len(data) < 1 {
return ErrHandshakeExtBadLength
}
switch data[0] {
case OCSPStatusRequest:
ch.OSCP = true
}
case ExtALPN:
if len(data) < 2 {
return ErrHandshakeExtBadLength
}
alpnLen := int(data[0])<<8 | int(data[1])
data = data[2:]
if len(data) != alpnLen {
return ErrHandshakeExtBadLength
}
for len(data) > 0 {
stringLen := int(data[0])
data = data[1:]
ch.ALPNs = append(ch.ALPNs, string(data[:stringLen]))
data = data[stringLen:]
}
default:
// Other extension where we only care about presence, or presence
// and length or unknown extension
ch.Extensions[extType] = length
}
}
return nil
}