-
Notifications
You must be signed in to change notification settings - Fork 8
/
dissector.go
109 lines (95 loc) · 2.28 KB
/
dissector.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
package dissector
import (
"crypto/tls"
"fmt"
"io"
)
type ClientHelloInfo struct {
SessionID []byte
CipherSuites []uint16
CompressionMethods []uint8
SupportedProtos []string
SupportedVersions []uint16
ServerName string
}
func ParseClientHello(r io.Reader) (*ClientHelloInfo, error) {
record, err := ReadRecord(r)
if err != nil {
return nil, err
}
if record.Type != Handshake {
return nil, ErrBadType
}
msg := &ClientHelloMsg{}
if err := msg.Decode(record.Opaque); err != nil {
return nil, err
}
info := &ClientHelloInfo{
SessionID: msg.SessionID,
CipherSuites: msg.CipherSuites,
CompressionMethods: msg.CompressionMethods,
}
for _, ext := range msg.Extensions {
switch ext.Type() {
case ExtServerName:
sniExt := ext.(*ServerNameExtension)
info.ServerName = sniExt.Name
case ExtSupportedVersions:
verExt := ext.(*SupportedVersionsExtension)
info.SupportedVersions = verExt.Versions
case ExtALPN:
alpnExt := ext.(*ALPNExtension)
info.SupportedProtos = alpnExt.Protos
}
}
return info, nil
}
type ServerHelloInfo struct {
SessionID []byte
CipherSuite uint16
CompressionMethod uint8
Proto string
Version uint16
}
func ParseServerHello(r io.Reader) (*ServerHelloInfo, error) {
record, err := ReadRecord(r)
if err != nil {
return nil, err
}
switch record.Type {
case Handshake:
case EncryptedAlert:
msg := &AlertMsg{}
if err := msg.Decode(record.Opaque); err != nil {
return nil, err
}
return nil, fmt.Errorf("%w: %s", ErrAlert, msg.String())
default:
return nil, fmt.Errorf("%w %d", ErrBadType, record.Type)
}
msg := &ServerHelloMsg{}
if err := msg.Decode(record.Opaque); err != nil {
return nil, err
}
info := &ServerHelloInfo{
SessionID: msg.SessionID,
CipherSuite: msg.CipherSuite,
CompressionMethod: msg.CompressionMethod,
Version: tls.VersionTLS12,
}
for _, ext := range msg.Extensions {
switch ext.Type() {
case ExtSupportedVersions:
verExt := ext.(*SupportedVersionsExtension)
if len(verExt.Versions) > 0 {
info.Version = verExt.Versions[0]
}
case ExtALPN:
alpnExt := ext.(*ALPNExtension)
if len(alpnExt.Protos) > 0 {
info.Proto = alpnExt.Protos[0]
}
}
}
return info, nil
}