Skip to content

Commit 4538bcb

Browse files
fixed some bugs with csuites parsing
1 parent a82c532 commit 4538bcb

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

layers/tls.go

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
)
88

99
const (
10-
headerSizeTLS = 5
10+
headerSizeTLS = 5
11+
HandshakeTLSVal = 0x16 // 22
12+
ClientHelloTLSVal = 0x01
1113
)
1214

1315
type Record struct {
@@ -19,11 +21,38 @@ type Record struct {
1921
Data []byte
2022
}
2123

24+
func (r *Record) String() string {
25+
return fmt.Sprintf(` - Content Type: %s (%d)
26+
- Version: %s (%#04x)
27+
- Length: %d`,
28+
r.ContentTypeDesc,
29+
r.ContentType,
30+
r.VersionDesc,
31+
r.Version,
32+
r.Length)
33+
}
34+
35+
type HSTLSParser interface {
36+
ParseHS(data []byte) error
37+
}
38+
39+
func HSTLSParserByType(hstype uint8) HSTLSParser {
40+
switch hstype {
41+
case 1:
42+
return &TLSClientHello{}
43+
}
44+
return nil
45+
}
46+
2247
type CipherSuite struct {
2348
Value uint16
2449
Desc string
2550
}
2651

52+
func (cs *CipherSuite) String() string {
53+
return fmt.Sprintf("%s (%#x)", cs.Desc, cs.Value)
54+
}
55+
2756
type ServerName struct {
2857
Type uint16
2958
Length uint16
@@ -56,27 +85,35 @@ type TLSClientHello struct {
5685
CmprMethodsLength uint8 // usually 0x01
5786
CmprMethods []byte // usually 0x00
5887
ExtensionLength uint16
59-
ServerName ServerName
88+
ServerName *ServerName
6089
}
6190

62-
func (tch *TLSClientHello) Parse(data []byte) error {
63-
// TODO: add ParseHandshake and type dispatcher
91+
func (tch *TLSClientHello) ParseHS(data []byte) error {
6492
// offset 7 bytes
93+
if len(data) < 4 {
94+
return fmt.Errorf("message should be at least 4 bytes, got %d bytes", len(data))
95+
}
6596
tch.Length = int(uint(data[3]) | uint(data[2])<<8 | uint(data[1])<<16) // 6 - 8 bytes data[1:4]
66-
tch.Version = binary.BigEndian.Uint16(data[4:6]) // 9 - 10 bytes data[4:6]
97+
if len(data)-4 < tch.Length {
98+
return fmt.Errorf("message should be at least %d bytes, got %d bytes", tch.Length, len(data)-4)
99+
}
100+
tch.Version = binary.BigEndian.Uint16(data[4:6]) // 9 - 10 bytes data[4:6]
67101
tch.VersionDesc = verdesc(tch.Version)
68102
tch.Random = data[6:38] // 11-42 data[6:38]
69103
tch.SessionIDLength = data[38] // 43 data[38] 32 bytes
70104
sid := tch.SessionIDLength + 39 // 70
71105
tch.SessionID = data[39:sid] // data[39:71]
72106
csl := binary.BigEndian.Uint16(data[sid : sid+2]) // data[71:73] suites count * 2 bytes
73107
tch.CipherSuitesLength = csl
74-
cmproffset := csl + 73 // 107
75-
css := make([]*CipherSuite, 0, csl/2+1)
76-
for i := range len(data[73:cmproffset]) {
77-
val := binary.BigEndian.Uint16(data[i : i+2])
108+
offset := uint16(sid + 2) //73
109+
cmproffset := csl + offset // 107
110+
css := make([]*CipherSuite, 0, csl/2)
111+
var i uint16
112+
for i < csl {
113+
val := binary.BigEndian.Uint16(data[i+offset : i+offset+2])
78114
valdesc := csuitedesc(val)
79115
css = append(css, &CipherSuite{Value: val, Desc: valdesc})
116+
i += 2
80117
}
81118
tch.CipherSuites = css
82119
cml := data[cmproffset] // 107
@@ -85,13 +122,13 @@ func (tch *TLSClientHello) Parse(data []byte) error {
85122
tch.CmprMethods = data[cmproffset+1 : extoffset] // data[108:109]
86123
extlen := binary.BigEndian.Uint16(data[extoffset : extoffset+2]) // data[109:111]
87124
tch.ExtensionLength = extlen
88-
var i = extoffset + 2
125+
i = extoffset + 2
89126
for i < extoffset+extlen {
90127
typ := binary.BigEndian.Uint16(data[i : i+2])
91128
length := binary.BigEndian.Uint16(data[i+2 : i+4])
92129
switch typ {
93130
case 0: // TODO: add more extensions
94-
sn := ServerName{}
131+
sn := &ServerName{}
95132
err := sn.Parse(data[i : i+length+4])
96133
if err != nil {
97134
return err
@@ -118,17 +155,6 @@ type TLSServerHello struct {
118155
ExtensionLength uint16
119156
}
120157

121-
func (r *Record) String() string {
122-
return fmt.Sprintf(` - Content Type: %s (%d)
123-
- Version: %s (%#04x)
124-
- Length: %d`,
125-
r.ContentTypeDesc,
126-
r.ContentType,
127-
r.VersionDesc,
128-
r.Version,
129-
r.Length)
130-
}
131-
132158
// port 443
133159
// https://tls12.xargs.org/#client-hello/annotated
134160
// https://tls13.xargs.org/#client-hello/annotated

0 commit comments

Comments
 (0)