|
|
@@ -14,6 +14,7 @@ type ClientHello struct {
|
|
14
|
14
|
Time time.Time
|
|
15
|
15
|
Random [RandomLen]byte
|
|
16
|
16
|
SessionID []byte
|
|
|
17
|
+ Host string
|
|
17
|
18
|
CipherSuite uint16
|
|
18
|
19
|
}
|
|
19
|
20
|
|
|
|
@@ -28,6 +29,15 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
|
|
28
|
29
|
return hello, fmt.Errorf("unknown handshake type %#x", handshake[0])
|
|
29
|
30
|
}
|
|
30
|
31
|
|
|
|
32
|
+ handshakeSizeBytes := [4]byte{0, handshake[1], handshake[2], handshake[3]}
|
|
|
33
|
+ handshakeLength := binary.BigEndian.Uint32(handshakeSizeBytes[:])
|
|
|
34
|
+
|
|
|
35
|
+ if len(handshake)-4 != int(handshakeLength) {
|
|
|
36
|
+ return hello,
|
|
|
37
|
+ fmt.Errorf("incorrect handshake size. manifested=%d, real=%d",
|
|
|
38
|
+ handshakeLength, len(handshake)-4) // nolint: gomnd
|
|
|
39
|
+ }
|
|
|
40
|
+
|
|
31
|
41
|
copy(hello.Random[:], handshake[ClientHelloRandomOffset:])
|
|
32
|
42
|
|
|
33
|
43
|
for i := ClientHelloRandomOffset; i < ClientHelloRandomOffset+RandomLen; i++ {
|
|
|
@@ -61,11 +71,48 @@ func ParseClientHello(secret, handshake []byte) (ClientHello, error) {
|
|
61
|
71
|
timestamp := int64(binary.LittleEndian.Uint32(computedRandom[RandomLen-4:]))
|
|
62
|
72
|
hello.Time = time.Unix(timestamp, 0)
|
|
63
|
73
|
|
|
|
74
|
+ parseSessionID(&hello, handshake)
|
|
|
75
|
+ parseCipherSuite(&hello, handshake)
|
|
|
76
|
+ parseSNI(&hello, handshake)
|
|
|
77
|
+
|
|
|
78
|
+ return hello, nil
|
|
|
79
|
+}
|
|
|
80
|
+
|
|
|
81
|
+func parseSessionID(hello *ClientHello, handshake []byte) {
|
|
64
|
82
|
hello.SessionID = make([]byte, handshake[ClientHelloSessionIDOffset])
|
|
65
|
83
|
copy(hello.SessionID, handshake[ClientHelloSessionIDOffset+1:])
|
|
|
84
|
+}
|
|
66
|
85
|
|
|
|
86
|
+func parseCipherSuite(hello *ClientHello, handshake []byte) {
|
|
67
|
87
|
cipherSuiteOffset := ClientHelloSessionIDOffset + len(hello.SessionID) + 3 // nolint: gomnd
|
|
68
|
88
|
hello.CipherSuite = binary.BigEndian.Uint16(handshake[cipherSuiteOffset : cipherSuiteOffset+2])
|
|
|
89
|
+}
|
|
69
|
90
|
|
|
70
|
|
- return hello, nil
|
|
|
91
|
+func parseSNI(hello *ClientHello, handshake []byte) {
|
|
|
92
|
+ cipherSuiteOffset := ClientHelloSessionIDOffset + len(hello.SessionID) + 1
|
|
|
93
|
+ handshake = handshake[cipherSuiteOffset:]
|
|
|
94
|
+
|
|
|
95
|
+ cipherSuiteLength := binary.BigEndian.Uint16(handshake[:2])
|
|
|
96
|
+ handshake = handshake[2+cipherSuiteLength:]
|
|
|
97
|
+
|
|
|
98
|
+ compressionMethodsLength := int(handshake[0])
|
|
|
99
|
+ handshake = handshake[1+compressionMethodsLength:]
|
|
|
100
|
+
|
|
|
101
|
+ extensionsLength := binary.BigEndian.Uint16(handshake[:2])
|
|
|
102
|
+ handshake = handshake[2 : 2+extensionsLength]
|
|
|
103
|
+
|
|
|
104
|
+ for len(handshake) > 0 {
|
|
|
105
|
+ if binary.BigEndian.Uint16(handshake[:2]) != ExtensionSNI {
|
|
|
106
|
+ extensionsLength := binary.BigEndian.Uint16(handshake[2:4])
|
|
|
107
|
+ handshake = handshake[4+extensionsLength:]
|
|
|
108
|
+
|
|
|
109
|
+ continue
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ hostnameLength := binary.BigEndian.Uint16(handshake[7:9])
|
|
|
113
|
+ handshake = handshake[9:]
|
|
|
114
|
+ hello.Host = string(handshake[:int(hostnameLength)])
|
|
|
115
|
+
|
|
|
116
|
+ return
|
|
|
117
|
+ }
|
|
71
|
118
|
}
|