-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquic_tls.go
404 lines (361 loc) · 13.4 KB
/
quic_tls.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
package quic
import (
"bytes"
"crypto/tls"
"fmt"
)
func NewQuicClientHello() (ecdheKey ECDHEKeys, clientHelloPacket []byte) {
handshake := ClientHello{
HandshakeType: []byte{HandshakeTypeClientHello},
Length: []byte{0x00, 0x00, 0x00},
Version: TLS1_2,
Random: noRandomByte(32),
SessionIDLength: []byte{0x00},
//CipherSuitesLength: []byte{0x00, 0x06},
// TLS_RSA_WITH_AES_128_GCM_SHA256
// TLS_RSA_WITH_AES_128_GCM_SHA384
// TLS_CHACHA20_POLY1305_SHA256
//CipherSuites: []byte{0x13, 0x01, 0x13, 0x02, 0x13, 0x03},
// quic-goのCipherSuite
CipherSuitesLength: []byte{0x00, 0x26},
CipherSuites: []byte{
0xc0, 0x2b, 0xc0, 0x2f, 0xc0, 0x2c, 0xc0, 0x30,
0xcc, 0xa9, 0xcc, 0xa8, 0xc0, 0x09, 0xc0, 0x13,
0xc0, 0x0a, 0xc0, 0x14, 0x00, 0x9c, 0x00, 0x9d,
0x00, 0x2f, 0x00, 0x35, 0xc0, 0x12, 0x00, 0x0a,
0x13, 0x01, 0x13, 0x02, 0x13, 0x03,
},
CompressionLength: []byte{0x01},
CompressionMethod: []byte{0x00},
}
// TLS1.3のextensionをセット
handshake.Extensions, ecdheKey = setQuicTLSExtension(true)
// Quic transport parameterを追加
handshake.Extensions = append(handshake.Extensions, setQuicTransportParameters()...)
// ExtensionLengthをセット
handshake.ExtensionLength = UintTo2byte(uint16(len(handshake.Extensions)))
// Typeの1byteとLengthの3byteを合計から引く
handshake.Length = UintTo3byte(uint32(toByteLen(handshake) - 4))
// byteにする
clientHelloPacket = toByteArr(handshake)
//var hello []byte
//hello = append(hello, NewTLSRecordHeader("Handshake", toByteLen(handshake))...)
//hello = append(hello, handshakebyte...)
// ClientHelloを保存しておく
//tlsinfo.HandshakeMessages = handshakebyte
return ecdheKey, clientHelloPacket
}
// quic-goが送っていたのをセットする
func setQuicTransportParameters() []byte {
var quicParams []byte
var quicParamsBytes []byte
// GREASE Bit
quicParams = append(quicParams, []byte{0x42, 0x0b, 0x04, 0x1f, 0xad, 0x78, 0x8e}...)
quicParams = append(quicParams, initialMaxStreamDataBidiLocalParamByte...)
quicParams = append(quicParams, initialMaxStreamDataBidiRemoteParamByte...)
quicParams = append(quicParams, initialMaxStreamDataUniParamByte...)
quicParams = append(quicParams, initialMaxDataParamByte...)
quicParams = append(quicParams, initialMaxStreamsBidiParamByte...)
quicParams = append(quicParams, initialMaxStreamsUniParamByte...)
quicParams = append(quicParams, maxIdleTimeoutParamByte...)
quicParams = append(quicParams, maxUdpPayloadSizeParamByte...)
// GREASE Bit
quicParams = append(quicParams, []byte{0x0b, 0x01, 0x1a}...)
quicParams = append(quicParams, disableActiveMigrationParamByte...)
quicParams = append(quicParams, activeConnectionIdLimitParamByte...)
// Set source connection id Length
//initialSourceConnectionId = append(initialSourceConnectionId, byte(len(sourceConnID)))
// Set source connection id
//initialSourceConnectionId = append(initialSourceConnectionId, sourceConnID...)
quicParams = append(quicParams, initialSourceConnectionIdParamByte...)
quicParams = append(quicParams, maxDatagramFrameSizeParamByte...)
// tls extensionのType = 57 をセット
quicParamsBytes = append(quicParamsBytes, []byte{0x00, 0x39}...)
// Lengthをセット
quicParamsBytes = append(quicParamsBytes, UintTo2byte(uint16(len(quicParams)))...)
quicParamsBytes = append(quicParamsBytes, quicParams...)
return quicParamsBytes
}
// golangのclientのをキャプチャしてそのままセットする
func setQuicTLSExtension(http3 bool) ([]byte, ECDHEKeys) {
var tlsExtension []byte
// server_name
tlsExtension = append(tlsExtension, setServerNameExt([]byte(`localhost`))...)
// status_reqeust
tlsExtension = append(tlsExtension, []byte{0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00}...)
// supported_groups
tlsExtension = append(tlsExtension, []byte{0x00, 0x0a, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x1d,
0x00, 0x17, 0x00, 0x18, 0x00, 0x19}...)
// ec_point_formats
tlsExtension = append(tlsExtension, []byte{0x00, 0x0b, 0x00, 0x02, 0x01, 0x00}...)
// signature_algorithms
tlsExtension = append(tlsExtension, []byte{
0x00, 0x0d, 0x00, 0x1a, 0x00, 0x18, 0x08, 0x04,
0x04, 0x03, 0x08, 0x07, 0x08, 0x05, 0x08, 0x06,
0x04, 0x01, 0x05, 0x01, 0x06, 0x01, 0x05, 0x03,
0x06, 0x03, 0x02, 0x01, 0x02, 0x03,
}...)
// renagotiation_info
tlsExtension = append(tlsExtension, []byte{0xff, 0x01, 0x00, 0x01, 0x00}...)
// Application Layer Protocol Negotiation for HTTP3
if http3 {
tlsExtension = append(tlsExtension, []byte{0x00, 0x10, 0x00, 0x05, 0x00, 0x03, 0x02, 0x68, 0x33}...)
}
// quic-echo-example
//tlsExtension = append(tlsExtension, []byte{
// 0x00, 0x10, 0x00, 0x14, 0x00, 0x12, 0x11, 0x71,
// 0x75, 0x69, 0x63, 0x2d, 0x65, 0x63, 0x68, 0x6f,
// 0x2d, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65}...)
// signed_certificate_timestamp
tlsExtension = append(tlsExtension, []byte{0x00, 0x12, 0x00, 0x00}...)
// supported_versions
tlsExtension = append(tlsExtension, []byte{0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04}...)
// 共通鍵を生成する
clientkey := genrateClientECDHEKey()
// key_share, DHEの公開鍵を送る
// Type=0x0033, Length=0x0026, ClientKeyShareLength=0x0024
tlsExtension = append(tlsExtension, []byte{0x00, 0x33, 0x00, 0x26, 0x00, 0x24}...)
// Group=0x001d
tlsExtension = append(tlsExtension, UintTo2byte(uint16(tls.X25519))...)
// keyのLength = 32byte
tlsExtension = append(tlsExtension, []byte{0x00, 0x20}...)
// 公開鍵を追加()
tlsExtension = append(tlsExtension, clientkey.PublicKey...)
//tlsExtension = append(tlsExtension, strtoByte("2fe57da347cd62431528daac5fbb290730fff684afc4cfc2ed90995f58cb3b74")...)
// set length
//tlsExtensionByte = append(tlsExtensionByte, UintTo2byte(uint16(len(tlsExtension)))...)
//tlsExtensionByte = append(tlsExtensionByte, tlsExtension...)
return tlsExtension, clientkey
}
func ParseTLSHandshake(packet []byte) ([]interface{}, bool) {
var handshake []interface{}
for i := 0; i < len(packet); i++ {
switch packet[0] {
case HandshakeTypeServerHello:
hello := ServerHello{
HandshakeType: packet[0:1],
Length: packet[1:4],
Version: packet[4:6],
Random: packet[6:38],
SessionIDLength: packet[38:39],
CipherSuites: packet[39:41],
CompressionMethod: packet[41:42],
ExtensionLength: packet[42:44],
TLSExtensions: ParseTLSExtensions(packet[44:]),
}
handshake = append(handshake, hello)
// packetを縮める, TLSレコードヘッダの4byte+Length
packet = packet[4+sum3BytetoLength(hello.Length):]
i = 0
case HandshakeTypeEncryptedExtensions:
encExt := EncryptedExtensions{
HandshakeType: packet[0:1],
Length: packet[1:4],
ExtensionLength: packet[4:6],
TLSExtensions: ParseTLSExtensions(packet[6:]),
}
handshake = append(handshake, encExt)
// packetを縮める, TLSレコードヘッダの4byte+Length
packet = packet[4+sum3BytetoLength(encExt.Length):]
i = 0
case HandshakeTypeCertificate:
cert := ServerCertificate{
HandshakeType: packet[0:1],
Length: packet[1:4],
CertificatesRequestContextLength: packet[4:5],
CertificatesLength: packet[5:8],
}
// Certificate PacketのLengthよりPacketの残りのLengthが小さいときはTLSパケットが分割されてるので、
// 読み込みをやめて前半のパケットを保存しておく
if len(packet) < int(sum3BytetoLength(cert.Length)) {
fragPacket := FragmentTLSPacket{
Type: HandshakeTypeCertificate,
Packet: packet[:],
// 残りのPacketのLength, 最後に足している4byteはTLSレコードヘッダの4byte(Typeの1byte + Lengthの3byte)
RemainPacketLength: int(sum3BytetoLength(cert.Length)) - len(packet) + 4,
}
handshake = append(handshake, fragPacket)
// packetを縮める
packet = packet[len(packet):]
i = 0
} else {
cert.Certificates = readCertificates(packet[8:])
handshake = append(handshake, cert)
// packetを縮める
packet = packet[4+sum3BytetoLength(cert.Length):]
i = 0
}
case HandshakeTypeCertificateVerify:
verify := CertificateVerify{
HandshakeType: packet[0:1],
Length: packet[1:4],
SignatureHashAlgorithms: packet[4:6],
SignatureLength: packet[6:8],
Signature: packet[8:],
}
handshake = append(handshake, verify)
// packetを縮める, TLSレコードヘッダの4byte+Length
packet = packet[4+sum3BytetoLength(verify.Length):]
i = 0
case HandshakeTypeFinished:
finish := FinishedMessage{
HandshakeType: packet[0:1],
Length: packet[1:4],
VerifyData: packet[4:],
}
handshake = append(handshake, finish)
// packetを縮める, TLSレコードヘッダの4byte+Length
packet = packet[4+sum3BytetoLength(finish.Length):]
i = 0
case HandshakeTypeNewSessionTicket:
//ticketLength := binary.BigEndian.Uint16(packet[21:23]) + 23
session := SessionTicket{
HandshakeType: packet[0:1],
Length: packet[1:4],
TicketLifeTime: packet[4:8],
TicketAgeAdd: packet[8:12],
TicketNonceLength: packet[12:13],
}
offset := 13
if !bytes.Equal(session.TicketNonceLength, []byte{0x00}) {
session.TicketNonce = packet[offset : offset+int(sumByteArr(session.TicketNonceLength))]
offset += int(sumByteArr(session.TicketNonceLength))
}
// set Ticket Length
session.TicketLength = packet[offset : offset+2]
offset += 2
// set Ticket
session.Ticket = packet[offset : offset+int(sumByteArr(session.TicketLength))]
offset += int(sumByteArr(session.TicketLength))
// set Extensions
session.TicketExtensionLength = packet[offset : offset+2]
offset += 2
session.TicketExtensions = packet[offset:]
handshake = append(handshake, session)
// packetを縮める, TLSレコードヘッダの4byte+Length
packet = packet[4+sum3BytetoLength(session.Length):]
i = 0
}
}
return handshake, isPacketFragment(handshake)
}
func setServerNameExt(hostname []byte) []byte {
sname := ServerNameIndicationExtension{
ServerNameListLength: UintTo2byte(uint16(len(hostname) + 3)),
ServerNameType: []byte{0x00},
ServerNameLength: UintTo2byte(uint16(len(hostname))),
ServerName: hostname,
}
// type
extbyte := []byte{0x00, 0x00}
// set packet length
extbyte = append(extbyte, UintTo2byte(toByteLen(sname))...)
extbyte = append(extbyte, toByteArr(sname)...)
return extbyte
}
func ParseTLSExtensions(extPacket []byte) (tlsEx []TLSExtensions) {
for i := 0; i < len(extPacket); i++ {
switch extPacket[1] {
case TLSExtSupportedVersions:
tlsEx = append(tlsEx, TLSExtensions{
Type: extPacket[0:2],
Length: extPacket[2:4],
Value: SupportedVersions{
Version: extPacket[4:6],
},
})
// packetを縮める
extPacket = extPacket[6:]
i = 0
case TLSExtKeyShare:
tlsEx = append(tlsEx, TLSExtensions{
Type: extPacket[0:2],
Length: extPacket[2:4],
Value: KeyShareExtension{
Group: extPacket[4:6],
KeyExchangeLength: extPacket[6:8],
KeyExchange: extPacket[8:40],
},
})
// packetを縮める
extPacket = extPacket[40:]
i = 0
case TLSExtALPN:
alpnExt := TLSExtensions{
Type: extPacket[0:2],
Length: extPacket[2:4],
}
alpn := ALPNProtocol{
ALPNExtLength: extPacket[4:6],
StringLength: extPacket[6:7],
}
alpn.NextProtocol = extPacket[7 : 7+alpn.StringLength[0]]
alpnExt.Value = alpn
tlsEx = append(tlsEx, alpnExt)
// packetを縮める
extPacket = extPacket[7+alpn.StringLength[0]:]
i = 0
case TLSExtQuicTP:
quicTps := TLSExtensions{
Type: extPacket[0:2],
Length: extPacket[2:4],
}
quicTps.Value = ParseQuicTransportPrameters(extPacket[4 : 4+sumByteArr(quicTps.Length)])
tlsEx = append(tlsEx, quicTps)
// packetを縮める
extPacket = extPacket[4+sumByteArr(quicTps.Length):]
i = 0
}
}
return tlsEx
}
func checkQuicTransportPram(id byte) bool {
for _, v := range quicTransportPrameterMaps {
if int(id) == v {
//fmt.Printf("quic transport parameter type is %s\n", k)
return true
}
}
return false
}
func ParseQuicTransportPrameters(packet []byte) (quicPrams []QuicParameters) {
for i := 0; i < len(packet); i++ {
if checkQuicTransportPram(packet[0]) {
param := QuicParameters{
Type: packet[0:1],
Length: packet[1:2],
}
param.Value = packet[2 : 2+param.Length[0]]
quicPrams = append(quicPrams, param)
// packetを縮める
packet = packet[2+int(param.Length[0]):]
i = 0
} else {
// GREASEを処理する
// typeで定義されてなければGREASEとみなす(面倒くさいから)
// https://datatracker.ietf.org/doc/html/draft-ietf-quic-bit-grease#section-3
// The QUIC Bit is defined as the second-to-most significant bit of the first
// byte of QUIC packets (that is, the value 0x40).
// と書いてあるから0byte目が0x40以上ならGREASEと判断し、可変長整数デコードしてチェックするのが正しそう?(面倒くさい)
param := QuicParameters{
Type: packet[0:2],
Length: packet[2:3],
}
param.Value = packet[3 : 3+param.Length[0]]
quicPrams = append(quicPrams, param)
// packetを縮める
packet = packet[3+int(param.Length[0]):]
i = 0
}
}
return quicPrams
}
func isPacketFragment(packet []interface{}) bool {
for _, v := range packet {
t := fmt.Sprintf("%T", v)
if t == "quic.FragmentTLSPacket" {
return true
}
}
return false
}