-
Notifications
You must be signed in to change notification settings - Fork 1
/
tlsparse.go
355 lines (307 loc) · 9.06 KB
/
tlsparse.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package netem
//
// References:
//
// - https://datatracker.ietf.org/doc/html/rfc8446
//
// - https://datatracker.ietf.org/doc/html/rfc6066
//
// - https://pkg.go.dev/golang.org/x/crypto/cryptobyte
//
// - https://tls13.xargs.org/#client-hello
//
import (
"crypto/tls"
"errors"
"fmt"
"golang.org/x/crypto/cryptobyte"
)
// ErrTLSParse is the error returned in case there is a TLS parse error.
var ErrTLSParse = errors.New("tlsparse: parse error")
// newErrTLSParse returns a new [ErrParse].
func newErrTLSParse(message string) error {
return fmt.Errorf("%w: %s", ErrTLSParse, message)
}
// TLSRecordHeader is a TLS TLSRecordHeader.
type TLSRecordHeader struct {
// ContentType is the type of the content.
ContentType uint8
// ProtocolVersion is the version of the TLS protocol.
ProtocolVersion uint16
// Rest contains the rest of the message.
Rest cryptobyte.String
}
// UnmarshalTLSRecordHeader unmarshals a RecordHeader.
//
// Return value:
//
// 1. the parsed RecordHeader (on success);
//
// 2. the unparsed bytes (on success), which may be empty if the
// input only contained a whole RecordHeader;
//
// 3. an error (nil on success).
func UnmarshalTLSRecordHeader(cursor cryptobyte.String) (*TLSRecordHeader, cryptobyte.String, error) {
rh := &TLSRecordHeader{}
if !cursor.ReadUint8(&rh.ContentType) {
return nil, nil, newErrTLSParse("record header: cannot read content type field")
}
if !cursor.ReadUint16(&rh.ProtocolVersion) {
return nil, nil, newErrTLSParse("record header: cannot read protocol version field")
}
if !cursor.ReadUint16LengthPrefixed(&rh.Rest) {
return nil, nil, newErrTLSParse("record header: cannot read the rest of the message")
}
switch rh.ProtocolVersion {
case tls.VersionTLS10, tls.VersionTLS11, tls.VersionTLS12, tls.VersionTLS13:
// all good
default:
return nil, nil, newErrTLSParse("record header: unknown protocol version")
}
//
// RFC 8446 defines the following content types:
//
// enum {
// invalid(0),
// change_cipher_spec(20),
// alert(21),
// handshake(22),
// application_data(23),
// (255)
// } ContentType;
//
// See https://datatracker.ietf.org/doc/html/rfc8446#section-5
//
switch rh.ContentType {
case 20, 21, 22, 23:
// all good
default:
return nil, nil, newErrTLSParse("record header: unknown content type")
}
return rh, cursor, nil
}
// TLSHandshakeMsg is the TLSHandshakeMsg message.
type TLSHandshakeMsg struct {
// HandshakeType is the type of handshake message.
HandshakeType uint8
// ClientHello is either nil or the parsed ClientHello.
ClientHello *TLSClientHello
}
// UnmarshalTLSHandshakeMsg unmarshals an Handshake message.
//
// Return value:
//
// 1. the parsed Handshake (on success);
//
// 2. an error (nil on success).
func UnmarshalTLSHandshakeMsg(cursor cryptobyte.String) (*TLSHandshakeMsg, error) {
h := &TLSHandshakeMsg{}
if !cursor.ReadUint8(&h.HandshakeType) {
return nil, newErrTLSParse("handshake: cannot read type field")
}
var rest cryptobyte.String
if !cursor.ReadUint24LengthPrefixed(&rest) {
return nil, newErrTLSParse("handshake: cannot read the rest of the message")
}
//
// RFC 8446 defines the follwing handshake types:
//
// enum {
// client_hello(1),
// server_hello(2),
// new_session_ticket(4),
// end_of_early_data(5),
// encrypted_extensions(8),
// certificate(11),
// certificate_request(13),
// certificate_verify(15),
// finished(20),
// key_update(24),
// message_hash(254),
// (255)
// } HandshakeType;
//
// See https://datatracker.ietf.org/doc/html/rfc8446#section-4
//
switch h.HandshakeType {
case 1: // client_hello
clientHello, err := unmarshalTLSClientHello(rest)
if err != nil {
return nil, err
}
h.ClientHello = clientHello
return h, nil
default:
return nil, newErrTLSParse("handshake: unsupported type")
}
}
// TLSClientHello is the TLSClientHello message.
type TLSClientHello struct {
// ProtocolVersion is the protocol version.
ProtocolVersion uint16
// Random contains exacty 32 bytes of random data.
Random []byte
// LegacySessionID is the legacy session ID.
LegacySessionID cryptobyte.String
// CipherSuites contains the client cipher suites.
CipherSuites cryptobyte.String
// LegacyCompressionMethods contains the legacy compression methods.
LegacyCompressionMethods cryptobyte.String
// Extensions contains the extensions.
Extensions cryptobyte.String
}
// unmarshalTLSClientHello unmarshals a ClientHello message.
//
// Return value:
//
// 1. the parsed ClientHello (on success);
//
// 2. an error (nil on success).
func unmarshalTLSClientHello(cursor cryptobyte.String) (*TLSClientHello, error) {
ch := &TLSClientHello{}
//
// RFC 8446 defines the ClientHello as follows:
//
// uint16 ProtocolVersion;
// opaque Random[32];
//
// uint8 CipherSuite[2]; /* Cryptographic suite selector */
// struct {
// ProtocolVersion legacy_version = 0x0303; /* TLS v1.2 */
// Random random;
// opaque legacy_session_id<0..32>;
// CipherSuite cipher_suites<2..2^16-2>;
// opaque legacy_compression_methods<1..2^8-1>;
// Extension extensions<8..2^16-1>;
// } ClientHello;
//
// See https://datatracker.ietf.org/doc/html/rfc8446#section-4.1.2
//
if !cursor.ReadUint16(&ch.ProtocolVersion) {
return nil, newErrTLSParse("client hello: cannot read protocol version field")
}
if !cursor.ReadBytes(&ch.Random, 32) {
return nil, newErrTLSParse("client hello: cannot read random field")
}
if !cursor.ReadUint8LengthPrefixed(&ch.LegacySessionID) {
return nil, newErrTLSParse("client hello: cannot read legacy session id field")
}
if !cursor.ReadUint16LengthPrefixed(&ch.CipherSuites) {
return nil, newErrTLSParse("client hello: cannot read cipher suites field")
}
if !cursor.ReadUint8LengthPrefixed(&ch.LegacyCompressionMethods) {
return nil, newErrTLSParse("client hello: cannot read legacy compression methods field")
}
if !cursor.ReadUint16LengthPrefixed(&ch.Extensions) {
return nil, newErrTLSParse("client hello: cannot read extensions field")
}
if !cursor.Empty() {
return nil, newErrTLSParse("client hello: unparsed trailing data")
}
return ch, nil
}
// TLSExtension is a TLS extension.
type TLSExtension struct {
// Type is the extension type.
Type uint16
// Data contains the extension data.
Data cryptobyte.String
}
// UnmarshalTLSExtensions unmarshals the extensions.
//
// Return value:
//
// 1. the parsed []*Extensions (on success);
//
// 2. an error (nil on success).
func UnmarshalTLSExtensions(cursor cryptobyte.String) ([]*TLSExtension, error) {
out := []*TLSExtension{}
for !cursor.Empty() {
ext := &TLSExtension{}
if !cursor.ReadUint16(&ext.Type) {
return nil, newErrTLSParse("client hello: cannot read extension type")
}
if !cursor.ReadUint16LengthPrefixed(&ext.Data) {
return nil, newErrTLSParse("client hello: cannot read extension data")
}
out = append(out, ext)
}
return out, nil
}
// FindTLSServerNameExtension returns the first ServerName extension
// in case of success or false in case of failure.
func FindTLSServerNameExtension(exts []*TLSExtension) (*TLSExtension, bool) {
for _, ext := range exts {
switch ext.Type {
case 0: // server_name
return ext, true
default:
continue
}
}
return nil, false
}
// UnmarshalTLSServerNameExtension unmarshals the server name
// from the bytes that consist of the extension value.
func UnmarshalTLSServerNameExtension(cursor cryptobyte.String) (string, error) {
var serverNameList cryptobyte.String
if !cursor.ReadUint16LengthPrefixed(&serverNameList) {
return "", newErrTLSParse("server name: cannot read server name list field")
}
if !cursor.Empty() {
return "", newErrTLSParse("server name: unparsed trailing data")
}
var (
sni string
found bool
)
for !serverNameList.Empty() {
var nameType uint8
if !serverNameList.ReadUint8(&nameType) {
return "", newErrTLSParse("server name: cannot read name type field")
}
switch nameType {
case 0: // host_name
var hostName cryptobyte.String
if !serverNameList.ReadUint16LengthPrefixed(&hostName) {
return "", newErrTLSParse("server name: cannot read host name field")
}
sni = string(hostName)
found = true
default:
continue
}
}
if !found {
return "", newErrTLSParse("server name: did not find host name entry")
}
return sni, nil
}
// ExtractTLSServerName takes in input bytes read from the network, attempts
// to determine whether this is a TLS Handshale message, and if it is a ClientHello,
// and, if affirmative, attempts to extract the server name.
func ExtractTLSServerName(rawInput []byte) (string, error) {
if len(rawInput) <= 0 {
return "", newErrTLSParse("no data")
}
rh, _, err := UnmarshalTLSRecordHeader(cryptobyte.String(rawInput))
if err != nil {
return "", err
}
hx, err := UnmarshalTLSHandshakeMsg(rh.Rest)
if err != nil {
return "", err
}
if hx.ClientHello == nil {
return "", newErrTLSParse("no client hello")
}
exts, err := UnmarshalTLSExtensions(hx.ClientHello.Extensions)
if err != nil {
return "", err
}
snext, found := FindTLSServerNameExtension(exts)
if !found {
return "", newErrTLSParse("no server name extension")
}
return UnmarshalTLSServerNameExtension(snext.Data)
}