forked from dreadl0ck/tlsx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserverHello.go
321 lines (277 loc) · 10.1 KB
/
serverHello.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package tlsx
import (
"errors"
"fmt"
"golang.org/x/crypto/cryptobyte"
)
const (
ServerHelloRandomLen = 32
)
// CurveID is the type of a TLS identifier for an elliptic curve. See
// https://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8.
type CurveID uint16
// TLS 1.3 Key Share. See RFC 8446, Section 4.2.8.
type keyShare struct {
group CurveID
data []byte
}
// readUint8LengthPrefixed acts like s.ReadUint8LengthPrefixed, but targets a
// []byte instead of a cryptobyte.String.
func readUint8LengthPrefixed(s *cryptobyte.String, out *[]byte) bool {
return s.ReadUint8LengthPrefixed((*cryptobyte.String)(out))
}
// readUint16LengthPrefixed acts like s.ReadUint16LengthPrefixed, but targets a
// []byte instead of a cryptobyte.String.
func readUint16LengthPrefixed(s *cryptobyte.String, out *[]byte) bool {
return s.ReadUint16LengthPrefixed((*cryptobyte.String)(out))
}
// TLS extension numbers
const (
extensionServerName uint16 = 0
extensionStatusRequest uint16 = 5
extensionSupportedCurves uint16 = 10 // supported_groups in TLS 1.3, see RFC 8446, Section 4.2.7
extensionSupportedPoints uint16 = 11
extensionSignatureAlgorithms uint16 = 13
extensionALPN uint16 = 16
extensionSCT uint16 = 18
extensionSessionTicket uint16 = 35
extensionPreSharedKey uint16 = 41
extensionEarlyData uint16 = 42
extensionSupportedVersions uint16 = 43
extensionCookie uint16 = 44
extensionPSKModes uint16 = 45
extensionCertificateAuthorities uint16 = 47
extensionSignatureAlgorithmsCert uint16 = 50
extensionKeyShare uint16 = 51
extensionNextProtoNeg uint16 = 13172 // not IANA assigned
extensionRenegotiationInfo uint16 = 0xff01
)
type ServerHello struct {
ServerHelloBasic
NextProtoNeg bool
NextProtos []string
OCSPStapling bool
TicketSupported bool
SecureRenegotiationSupported bool
SecureRenegotiation []byte
AlpnProtocol string
Ems bool
Scts [][]byte
SupportedVersion uint16
ServerShare keyShare
SelectedIdentityPresent bool
SelectedIdentity uint16
// HelloRetryRequest extensions
Cookie []byte
}
func (m *ServerHello) Unmarshal(data []byte) error {
if len(data) < 5+4 {
return errors.New("Server returned short message")
}
// buf contains a TLS record, with a 5 byte record header and a 4 byte
// handshake header. The length of the ServerHello is taken from the
// handshake header.
serverHelloLen := int(data[6])<<16 | int(data[7])<<8 | int(data[8])
if serverHelloLen >= len(data) {
return errors.New("invalid serverHelloLen")
}
// prevent slice bounds out of range [:19] with capacity 11
if len(data) < 9+serverHelloLen {
return errors.New("invalid data length for server hello")
}
data = data[5 : 9+serverHelloLen]
*m = ServerHello{}
s := cryptobyte.String(data)
if !s.Skip(4) || // message type and uint24 length field
!s.ReadUint16(&m.Vers) || !s.ReadBytes(&m.Random, 32) ||
!readUint8LengthPrefixed(&s, &m.SessionID) ||
!s.ReadUint16(&m.CipherSuite) ||
!s.ReadUint8(&m.CompressionMethod) {
return errors.New("invalid message type")
}
if s.Empty() {
// ServerHello is optionally followed by extension data
return nil
}
var extensions cryptobyte.String
if !s.ReadUint16LengthPrefixed(&extensions) || !s.Empty() {
return errors.New("failed to read extensions")
}
for !extensions.Empty() {
var extension uint16
var extData cryptobyte.String
if !extensions.ReadUint16(&extension) ||
!extensions.ReadUint16LengthPrefixed(&extData) {
return errors.New("failed to read extension data")
}
m.Extensions = append(m.Extensions, extension)
switch extension {
case extensionNextProtoNeg:
m.NextProtoNeg = true
for !extData.Empty() {
var proto cryptobyte.String
if !extData.ReadUint8LengthPrefixed(&proto) ||
proto.Empty() {
return errors.New("failed to read extensionNextProtoNeg")
}
m.NextProtos = append(m.NextProtos, string(proto))
}
case extensionStatusRequest:
m.OCSPStapling = true
case extensionSessionTicket:
m.TicketSupported = true
case extensionRenegotiationInfo:
if !readUint8LengthPrefixed(&extData, &m.SecureRenegotiation) {
return errors.New("failed to read extensionRenegotiationInfo")
}
m.SecureRenegotiationSupported = true
case extensionALPN:
var protoList cryptobyte.String
if !extData.ReadUint16LengthPrefixed(&protoList) || protoList.Empty() {
return errors.New("failed to read extensionALPN protoList")
}
var proto cryptobyte.String
if !protoList.ReadUint8LengthPrefixed(&proto) ||
proto.Empty() || !protoList.Empty() {
return errors.New("failed to read extensionRenegotiationInfo proto")
}
m.AlpnProtocol = string(proto)
case extensionSCT:
var sctList cryptobyte.String
if !extData.ReadUint16LengthPrefixed(&sctList) || sctList.Empty() {
return errors.New("failed to read extensionSCT sctList")
}
for !sctList.Empty() {
var sct []byte
if !readUint16LengthPrefixed(&sctList, &sct) ||
len(sct) == 0 {
return errors.New("failed to read extensionSCT sctList sct")
}
m.Scts = append(m.Scts, sct)
}
case extensionSupportedVersions:
if !extData.ReadUint16(&m.SupportedVersion) {
return errors.New("failed to read extensionSupportedVersions")
}
case extensionCookie:
if !readUint16LengthPrefixed(&extData, &m.Cookie) ||
len(m.Cookie) == 0 {
return errors.New("failed to read extensionCookie")
}
case extensionKeyShare:
// This extension has different formats in SH and HRR, accept either
// and let the handshake logic decide. See RFC 8446, Section 4.2.8.
if len(extData) == 2 {
if !extData.ReadUint16((*uint16)(&m.SelectedGroup)) {
return errors.New("failed to read extensionKeyShare")
}
} else {
if !extData.ReadUint16((*uint16)(&m.ServerShare.group)) ||
!readUint16LengthPrefixed(&extData, &m.ServerShare.data) {
return errors.New("failed to read extensionKeyShare")
}
}
case extensionPreSharedKey:
m.SelectedIdentityPresent = true
if !extData.ReadUint16(&m.SelectedIdentity) {
return errors.New("failed to read extensionPreSharedKey")
}
default:
// Ignore unknown extensions.
continue
}
if !extData.Empty() {
return errors.New("failed to read extension data")
}
}
return nil
}
func (ch ServerHello) String() string {
str := fmt.Sprintln("Version:", ch.Vers)
str += fmt.Sprintln("Random:", ch.Random)
str += fmt.Sprintf("SessionId: %#v\n", ch.SessionID)
str += fmt.Sprintf("CipherSuite (%d): %v\n", 1, ch.CipherSuite)
str += fmt.Sprintf("CompressionMethod: %v\n", ch.CompressionMethod)
str += fmt.Sprintln("NextProtoNeg:", ch.NextProtoNeg)
str += fmt.Sprintf("NextProtos: %q\n", ch.NextProtos)
str += fmt.Sprintf("OcspStapling: %#v\n", ch.OCSPStapling)
str += fmt.Sprintf("Scts: %#v\n", ch.Scts)
str += fmt.Sprintf("Ems: %#v\n", ch.Ems)
str += fmt.Sprintf("TicketSupported: %v\n", ch.TicketSupported)
str += fmt.Sprintf("SecureRenegotiation: %v\n", ch.SecureRenegotiation)
str += fmt.Sprintf("SecureRenegotiationSupported: %v\n", ch.SecureRenegotiationSupported)
str += fmt.Sprintf("AlpnProtocol: %v\n", ch.AlpnProtocol)
str += fmt.Sprintf("Extensions: %v\n", ch.Extensions)
str += fmt.Sprintf("SupportedVersion: %v\n", ch.SupportedVersion)
str += fmt.Sprintf("ServerShare: %v\n", ch.ServerShare)
str += fmt.Sprintf("SelectedIdentityPresent: %v\n", ch.SelectedIdentityPresent)
str += fmt.Sprintf("SelectedIdentity: %v\n", ch.SelectedIdentity)
str += fmt.Sprintf("Cookie: %v\n", ch.Cookie)
str += fmt.Sprintf("SelectedGroup: %v\n", ch.SelectedGroup)
return str
}
type ServerHelloBasic struct {
Vers uint16
Random []byte
SessionID []byte
CipherSuite uint16
CompressionMethod uint8
SelectedGroup CurveID
Extensions []uint16
}
// Unmarshal only parses the fields needed for JA3 fingerprinting
// to avoids unnecessary allocations
func (m *ServerHelloBasic) Unmarshal(data []byte) error {
if len(data) < 5+4 {
return errors.New("server returned short message")
}
// buf contains a TLS record, with a 5 byte record header and a 4 byte
// handshake header. The length of the ServerHello is taken from the
// handshake header.
serverHelloLen := int(data[6])<<16 | int(data[7])<<8 | int(data[8])
if serverHelloLen >= len(data) {
return errors.New("invalid serverHelloLen")
}
// prevent slice bounds out of range [:19] with capacity 11
if len(data) < 9+serverHelloLen {
return errors.New("invalid data length for server hello")
}
data = data[5 : 9+serverHelloLen]
*m = ServerHelloBasic{}
s := cryptobyte.String(data)
if !s.Skip(4) || // message type and uint24 length field
!s.ReadUint16(&m.Vers) || !s.ReadBytes(&m.Random, 32) ||
!readUint8LengthPrefixed(&s, &m.SessionID) ||
!s.ReadUint16(&m.CipherSuite) ||
!s.ReadUint8(&m.CompressionMethod) {
return errors.New("invalid message type")
}
if s.Empty() {
// ServerHello is optionally followed by extension data
return nil
}
var extensions cryptobyte.String
if !s.ReadUint16LengthPrefixed(&extensions) || !s.Empty() {
return errors.New("failed to read extensions")
}
for !extensions.Empty() {
var extension uint16
var extData cryptobyte.String
if !extensions.ReadUint16(&extension) ||
!extensions.ReadUint16LengthPrefixed(&extData) {
return errors.New("failed to read extension data")
}
m.Extensions = append(m.Extensions, extension)
}
return nil
}
func (ch ServerHelloBasic) String() string {
str := fmt.Sprintln("Version:", ch.Vers)
str += fmt.Sprintln("Random:", ch.Random)
str += fmt.Sprintf("SessionId: %#v\n", ch.SessionID)
str += fmt.Sprintf("CipherSuite (%d): %v\n", 1, ch.CipherSuite)
str += fmt.Sprintf("CompressionMethod: %v\n", ch.CompressionMethod)
str += fmt.Sprintf("Extensions: %v\n", ch.Extensions)
str += fmt.Sprintf("SelectedGroup: %v\n", ch.SelectedGroup)
return str
}