-
Notifications
You must be signed in to change notification settings - Fork 8
/
extension.go
457 lines (376 loc) · 9.37 KB
/
extension.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package dissector
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)
const (
extensionHeaderLen = 4
)
const (
ExtServerName uint16 = 0x00
ExtSupportedGroups uint16 = 0x0a
ExtECPointFormats uint16 = 0x0b
ExtSignatureAlgorithms uint16 = 0x0d
ExtALPN uint16 = 0x10
ExtEncryptThenMac uint16 = 0x16
ExtExtendedMasterSecret uint16 = 0x17
ExtSessionTicket uint16 = 0x23
ExtSupportedVersions uint16 = 0x2b
ExtRenegotiationInfo uint16 = 0xff01
)
var (
ErrShortBuffer = errors.New("short buffer")
ErrTypeMismatch = errors.New("type mismatch")
)
type Extension interface {
Type() uint16
Encode() ([]byte, error)
Decode([]byte) error
}
func NewExtension(t uint16, data []byte) (ext Extension, err error) {
switch t {
case ExtServerName:
ext = new(ServerNameExtension)
case ExtSupportedGroups:
ext = new(SupportedGroupsExtension)
case ExtECPointFormats:
ext = new(ECPointFormatsExtension)
case ExtSignatureAlgorithms:
ext = new(SignatureAlgorithmsExtension)
case ExtALPN:
ext = new(ALPNExtension)
case ExtEncryptThenMac:
ext = new(EncryptThenMacExtension)
case ExtExtendedMasterSecret:
ext = new(ExtendedMasterSecretExtension)
case ExtSessionTicket:
ext = new(SessionTicketExtension)
case ExtSupportedVersions:
ext = new(SupportedVersionsExtension)
case ExtRenegotiationInfo:
ext = new(RenegotiationInfoExtension)
default:
ext = &unknownExtension{
types: t,
}
}
if len(data) > 0 {
err = ext.Decode(data)
}
return
}
func ReadExtension(r io.Reader) (Extension, error) {
b := make([]byte, extensionHeaderLen)
if _, err := io.ReadFull(r, b); err != nil {
return nil, err
}
t := binary.BigEndian.Uint16(b[:2])
bb := make([]byte, int(binary.BigEndian.Uint16(b[2:4])))
if _, err := io.ReadFull(r, bb); err != nil {
return nil, err
}
return NewExtension(t, bb)
}
func readExtensions(b []byte) (exts []Extension, err error) {
if len(b) == 0 {
return
}
br := bytes.NewReader(b)
for br.Len() > 0 {
var ext Extension
ext, err = ReadExtension(br)
if err != nil {
return
}
exts = append(exts, ext)
}
return
}
type unknownExtension struct {
types uint16
raw []byte
}
func (ext *unknownExtension) Type() uint16 {
return ext.types
}
func (ext *unknownExtension) Encode() ([]byte, error) {
return ext.raw, nil
}
func (ext *unknownExtension) Decode(b []byte) error {
ext.raw = make([]byte, len(b))
copy(ext.raw, b)
return nil
}
type ServerNameExtension struct {
NameType uint8
Name string
}
func (ext *ServerNameExtension) Type() uint16 {
return ExtServerName
}
func (ext *ServerNameExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint16(1+2+len(ext.Name)))
buf.WriteByte(ext.NameType)
binary.Write(buf, binary.BigEndian, uint16(len(ext.Name)))
buf.WriteString(ext.Name)
return buf.Bytes(), nil
}
func (ext *ServerNameExtension) Decode(b []byte) error {
if len(b) < 5 {
return fmt.Errorf("server_name: %w", ErrShortBuffer)
}
ext.NameType = b[2]
n := int(binary.BigEndian.Uint16(b[3:]))
if len(b[5:]) < n {
return fmt.Errorf("server_name: %w", ErrShortBuffer)
}
ext.Name = string(b[5 : 5+n])
return nil
}
type SessionTicketExtension struct {
Data []byte
}
func (ext *SessionTicketExtension) Type() uint16 {
return ExtSessionTicket
}
func (ext *SessionTicketExtension) Encode() ([]byte, error) {
return ext.Data, nil
}
func (ext *SessionTicketExtension) Decode(b []byte) error {
ext.Data = make([]byte, len(b))
copy(ext.Data, b)
return nil
}
type ECPointFormatsExtension struct {
Formats []uint8
}
func (ext *ECPointFormatsExtension) Type() uint16 {
return ExtECPointFormats
}
func (ext *ECPointFormatsExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
buf.WriteByte(uint8(len(ext.Formats)))
buf.Write(ext.Formats)
return buf.Bytes(), nil
}
func (ext *ECPointFormatsExtension) Decode(b []byte) error {
if len(b) < 1 {
return fmt.Errorf("ec_point_formats: %w", ErrShortBuffer)
}
n := int(b[0])
if len(b[1:]) < n {
return fmt.Errorf("ec_point_formats: %w", ErrShortBuffer)
}
ext.Formats = make([]byte, n)
copy(ext.Formats, b[1:])
return nil
}
type SupportedGroupsExtension struct {
Groups []uint16
}
func (ext *SupportedGroupsExtension) Type() uint16 {
return ExtSupportedGroups
}
func (ext *SupportedGroupsExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint16(len(ext.Groups)*2))
for _, group := range ext.Groups {
binary.Write(buf, binary.BigEndian, group)
}
return buf.Bytes(), nil
}
func (ext *SupportedGroupsExtension) Decode(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("supported_groups: %w", ErrShortBuffer)
}
n := int(binary.BigEndian.Uint16(b)) / 2 * 2 //make it even
if len(b[2:]) < n {
return fmt.Errorf("supported_groups: %w", ErrShortBuffer)
}
for i := 0; i < n; i += 2 {
ext.Groups = append(ext.Groups, binary.BigEndian.Uint16(b[2+i:]))
}
return nil
}
type SignatureAlgorithmsExtension struct {
Algorithms []uint16
}
func (ext *SignatureAlgorithmsExtension) Type() uint16 {
return ExtSignatureAlgorithms
}
func (ext *SignatureAlgorithmsExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
binary.Write(buf, binary.BigEndian, uint16(len(ext.Algorithms)*2))
for _, alg := range ext.Algorithms {
binary.Write(buf, binary.BigEndian, alg)
}
return buf.Bytes(), nil
}
func (ext *SignatureAlgorithmsExtension) Decode(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("signature_algorithms: %w", ErrShortBuffer)
}
n := int(binary.BigEndian.Uint16(b))
if len(b[2:]) < n {
return fmt.Errorf("signature_algorithms: %w", ErrShortBuffer)
}
for i := 0; i < n; i += 2 {
ext.Algorithms = append(ext.Algorithms, binary.BigEndian.Uint16(b[2+i:]))
}
return nil
}
type EncryptThenMacExtension struct {
Data []byte
}
func (ext *EncryptThenMacExtension) Type() uint16 {
return ExtEncryptThenMac
}
func (ext *EncryptThenMacExtension) Encode() ([]byte, error) {
return ext.Data, nil
}
func (ext *EncryptThenMacExtension) Decode(b []byte) error {
ext.Data = make([]byte, len(b))
copy(ext.Data, b)
return nil
}
type ExtendedMasterSecretExtension struct {
Data []byte
}
func (ext *ExtendedMasterSecretExtension) Type() uint16 {
return ExtExtendedMasterSecret
}
func (ext *ExtendedMasterSecretExtension) Encode() ([]byte, error) {
return ext.Data, nil
}
func (ext *ExtendedMasterSecretExtension) Decode(b []byte) error {
ext.Data = make([]byte, len(b))
copy(ext.Data, b)
return nil
}
type RenegotiationInfoExtension struct {
Data []byte
}
func (ext *RenegotiationInfoExtension) Type() uint16 {
return ExtRenegotiationInfo
}
func (ext *RenegotiationInfoExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
buf.WriteByte(uint8(len(ext.Data)))
buf.Write(ext.Data)
return buf.Bytes(), nil
}
func (ext *RenegotiationInfoExtension) Decode(b []byte) error {
if len(b) < 1 {
return fmt.Errorf("renegotiation_info: %w", ErrShortBuffer)
}
n := int(b[0])
if len(b[1:]) < n {
return fmt.Errorf("renegotiation_info: %w", ErrShortBuffer)
}
ext.Data = make([]byte, n)
copy(ext.Data, b[1:])
return nil
}
type ALPNExtension struct {
Protos []string
}
func (ext *ALPNExtension) Type() uint16 {
return ExtALPN
}
func (ext *ALPNExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
buf.Write([]byte{0, 0}) // reserved 2-byte length
for _, proto := range ext.Protos {
if proto == "" {
continue
}
buf.WriteByte(uint8(len(proto))) // proto value length
buf.WriteString(proto)
}
data := buf.Bytes()
binary.BigEndian.PutUint16(data[:2], uint16(len(data)-2))
return data, nil
}
func (ext *ALPNExtension) Decode(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("application_layer_protocol_negotiation: %w", ErrShortBuffer)
}
alpnLen := int(binary.BigEndian.Uint16(b[:2]))
b = b[2:]
if len(b) < alpnLen {
return fmt.Errorf("application_layer_protocol_negotiation: %w", ErrShortBuffer)
}
b = b[:alpnLen]
for {
n := int(b[0])
if len(b[1:]) < n {
return fmt.Errorf("application_layer_protocol_negotiation: %w", ErrShortBuffer)
}
if proto := string(b[1 : 1+n]); proto != "" {
ext.Protos = append(ext.Protos, proto)
}
b = b[1+n:]
if len(b) == 0 {
break
}
}
return nil
}
type SupportedVersionsExtension struct {
Versions []uint16
Server bool
}
func (ext *SupportedVersionsExtension) Type() uint16 {
return ExtSupportedVersions
}
func (ext *SupportedVersionsExtension) Encode() ([]byte, error) {
buf := &bytes.Buffer{}
if len(ext.Versions) == 0 {
return nil, nil
}
if ext.Server {
var ver [2]byte
binary.BigEndian.PutUint16(ver[:], ext.Versions[0])
buf.Write(ver[:])
return buf.Bytes(), nil
}
buf.WriteByte(uint8(len(ext.Versions) * 2))
for _, version := range ext.Versions {
if version == 0 {
continue
}
var ver [2]byte
binary.BigEndian.PutUint16(ver[:], version)
buf.Write(ver[:])
}
return buf.Bytes(), nil
}
func (ext *SupportedVersionsExtension) Decode(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("supported_versions: %w", ErrShortBuffer)
}
if len(b) == 2 {
ext.Versions = append(ext.Versions, binary.BigEndian.Uint16(b))
ext.Server = true
return nil
}
verLen := int(b[0])
b = b[1:]
if len(b) < verLen {
return fmt.Errorf("supported_versions: %w", ErrShortBuffer)
}
b = b[:verLen]
for {
ver := binary.BigEndian.Uint16(b[:2])
ext.Versions = append(ext.Versions, ver)
b = b[2:]
if len(b) == 0 {
break
}
}
return nil
}