-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.go
444 lines (383 loc) · 11.4 KB
/
packet.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
package minq
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
)
// Encode a QUIC packet.
/*
Long header
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|1| Type (7) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|DCIL(4)|SCIL(4)|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// Initial Header: same as long header but with Token
+-+-+-+-+-+-+-+-+
|1| 0x7f |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Version (32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|DCIL(4)|SCIL(4)|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Connection ID (0/32..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Token Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Token (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Length (i) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+
|0|K|1|1|0|R R R|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Connection ID (0..144) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Packet Number (8/16/32) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Protected Payload (*) ...
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
const (
packetFlagLongHeader = byte(0x80)
packetFlagK = byte(0x40)
packetFlagShortHeader = byte(0x30)
)
// This packet type differs considerably from the spec. It includes both
// long and short headers in the same value space. Long headers are from
// 0-0x7f (inclusive); short headers are always represented as 0xff.
type packetType byte
const (
packetTypeInitial = packetType(0x7f)
packetTypeRetry = packetType(0x7e)
packetTypeHandshake = packetType(0x7d)
packetType0RTTProtected = packetType(0x7c)
packetTypeProtectedShort = packetType(0x00) // Not a real type
)
func (pt packetType) isLongHeader() bool {
return pt&packetType(packetFlagLongHeader) != 0
}
func (pt packetType) isProtected() bool {
if !pt.isLongHeader() {
return true
}
switch pt & 0x7f {
case packetTypeInitial, packetTypeHandshake, packetTypeRetry:
return false
}
return true
}
func (pt packetType) String() string {
switch pt {
case packetTypeInitial:
return "Initial"
case packetTypeRetry:
return "Retry"
case packetTypeHandshake:
return "Handshake"
case packetType0RTTProtected:
return "0-RTT"
case packetTypeProtectedShort:
return "1-RTT"
default:
return fmt.Sprintf("%x", uint8(pt))
}
}
// kCidDefaultLength is the length of connection ID we generate.
// TODO: make this configurable.
const kCidDefaultLength = 5
// ConnectionId identifies the connection that a packet belongs to.
type ConnectionId []byte
// String stringifies a connection ID in the natural way.
func (c ConnectionId) String() string {
return hex.EncodeToString(c)
}
// EncodeLength produces the length encoding used in the long packet header.
func (c ConnectionId) EncodeLength() byte {
if len(c) == 0 {
return 0
}
assert(len(c) >= 4 && len(c) <= 18)
return byte(len(c) - 3)
}
// The PDU definition for the header.
// These types are capitalized so that |codec| can use them.
type packetHeader struct {
// Type is the on-the-wire form of the packet type.
// Consult getHeaderType if you want a value that corresponds to the
// definition of packetType.
Type packetType
Version VersionNumber
ConnectionIDLengths byte
DestinationConnectionID ConnectionId
SourceConnectionID ConnectionId
TokenLength uint8
Token []byte
PayloadLength uint64 `tls:"varint"`
// In order to decode a short header, the length of the connection
// ID must be set in |shortCidLength| before decoding.
shortCidLength uintptr
}
func (p packetHeader) String() string {
ht := "SHORT"
if p.Type.isLongHeader() {
ht = "LONG"
}
return fmt.Sprintf("%s PT=%v", ht, p.getHeaderType())
}
func (p *packetHeader) getHeaderType() packetType {
if p.Type.isLongHeader() {
return p.Type & 0x7f
}
return packetTypeProtectedShort
}
type packet struct {
packetHeader
PacketNumber uint64 // Never more than 32 bits on the wire.
payload []byte
}
// This reads from p.ConnectionIDLengths.
func (p packetHeader) ConnectionIDLengths__length() uintptr {
if p.Type.isLongHeader() {
return 1
}
return 0
}
func (p packetHeader) TokenLength__length() uintptr {
if p.getHeaderType() != packetTypeInitial {
assert(len(p.Token) == 0)
return 0
}
return 1
}
func (p packetHeader) Token__length() uintptr {
if p.getHeaderType() != packetTypeInitial {
assert(len(p.Token) == 0)
return 0
}
return uintptr(p.TokenLength)
}
func (p packetHeader) DestinationConnectionID__length() uintptr {
if !p.Type.isLongHeader() {
return p.shortCidLength
}
l := p.ConnectionIDLengths >> 4
if l != 0 {
l += 3
}
return uintptr(l)
}
func (p packetHeader) SourceConnectionID__length() uintptr {
if !p.Type.isLongHeader() {
return 0
}
l := p.ConnectionIDLengths & 0xf
if l != 0 {
l += 3
}
return uintptr(l)
}
func (p packetHeader) PayloadLength__length() uintptr {
if p.Type.isLongHeader() {
return codecDefaultSize
}
return 0
}
func (p packetHeader) Version__length() uintptr {
if p.Type.isLongHeader() {
return 4
}
return 0
}
func newPacket(pt packetType, destCid ConnectionId, srcCid ConnectionId, ver VersionNumber, pn uint64, payload []byte, aeadOverhead int) *packet {
if pt == packetTypeProtectedShort {
// Only support writing the 32-bit packet number.
pt = packetType(0x2 | packetFlagShortHeader)
srcCid = nil
} else {
pt = pt | packetType(packetFlagLongHeader)
}
lengths := (destCid.EncodeLength() << 4) | srcCid.EncodeLength()
return &packet{
packetHeader: packetHeader{
Type: pt,
ConnectionIDLengths: lengths,
DestinationConnectionID: destCid,
SourceConnectionID: srcCid,
Version: ver,
PayloadLength: uint64(len(payload) + 4 + aeadOverhead),
},
PacketNumber: pn,
payload: payload,
}
}
type versionNegotiationPacket struct {
Versions []byte
}
func newVersionNegotiationPacket(versions []VersionNumber) *versionNegotiationPacket {
var buf bytes.Buffer
for _, v := range versions {
buf.Write(encodeArgs(v))
}
return &versionNegotiationPacket{buf.Bytes()}
}
/*
We don't use these.
func encodePacket(c ConnectionState, aead Aead, p *Packet) ([]byte, error) {
hdr, err := encode(&p.packetHeader)
if err != nil {
return nil, err
}
b, err := aead.protect(p.packetHeader.PacketNumber, hdr, p.payload)
if err != nil {
return nil, err
}
return encodeArgs(hdr, b), nil
}
func decodePacket(c ConnectionState, aead Aead, b []byte) (*Packet, error) {
// Parse the header
var hdr packetHeader
br, err := decode(&hdr, b)
if err != nil {
return nil, err
}
hdr.PacketNumber = c.expandPacketNumber(hdr.PacketNumber)
pt, err := aead.unprotect(hdr.PacketNumber, b[0:br], b[br:])
if err != nil {
return nil, err
}
return &Packet{hdr, pt}, nil
}
*/
func dumpPacket(payload []byte) string {
first := true
ret := fmt.Sprintf("%d=[", len(payload))
for len(payload) > 0 {
if !first {
ret += ", "
}
first = false
n, f, err := decodeFrame(payload)
if err != nil {
ret += fmt.Sprintf("Undecoded: [%x]", payload)
break
}
payload = payload[n:]
// TODO([email protected]): Not sure why %v doesn't work
ret += f.String()
}
ret += "]"
return ret
}
type pneCipherFactory interface {
create(sample []byte) cipher.Stream
}
type pneCipherFactoryAES struct {
block cipher.Block
}
func newPneCipherFactoryAES(key []byte) pneCipherFactory {
inner, err := aes.NewCipher(key)
assert(err == nil)
if err != nil {
return nil
}
return &pneCipherFactoryAES{block: inner}
}
func (f *pneCipherFactoryAES) create(sample []byte) cipher.Stream {
if len(sample) != 16 {
return nil
}
return cipher.NewCTR(f.block, sample)
}
func xorPacketNumber(hdr *packetHeader, hdrlen int, pnbuf []byte, p []byte, factory pneCipherFactory) error {
logf(logTypeTrace, "PNE Operation: hdrlen=%v, hdr=%x, payload=%x", hdrlen, p[:hdrlen], p)
// The packet must be at least long enough to contain
// the header, plus a minimum 1-byte PN, plus the sample.
sample_length := 16
if sample_length > len(p)-(hdrlen+1) {
logf(logTypePacket, "Packet too short")
return nil
}
// Now compute the offset
sample_offset := hdrlen + 4
if sample_offset+sample_length > len(p) {
sample_offset = len(p) - sample_length
}
sample := p[sample_offset : sample_offset+sample_length]
logf(logTypeTrace, "PNE sample_offset=%d sample=%x", sample_offset, sample)
stream := factory.create(sample)
stream.XORKeyStream(pnbuf, p[hdrlen:hdrlen+len(pnbuf)])
return nil
}
var pnPatterns = []struct {
prefix byte
mask byte
length int
}{
{
0, 0x80, 1,
},
{
0x80, 0xc0, 2,
},
{
0xc0, 0xc0, 4,
},
}
const ()
func encodePacketNumber(pn uint64, l int) []byte {
var buf bytes.Buffer
i := 0
for i, _ = range pnPatterns {
if pnPatterns[i].length == l {
break
}
}
uintEncodeInt(&buf, pn, uintptr(l))
b := buf.Bytes()
b[0] &= ^pnPatterns[i].mask
b[0] |= pnPatterns[i].prefix
return b
}
func decodePacketNumber(buf []byte) (uint64, int, error) {
if len(buf) < 1 {
return 0, 0, fmt.Errorf("Zero-length packet number")
}
i := 0
for i, _ = range pnPatterns {
if pnPatterns[i].mask&buf[0] == pnPatterns[i].prefix {
break
}
}
pat := &pnPatterns[i]
if len(buf) < pat.length {
return 0, 0, fmt.Errorf("Buffer too short for packet number (%v < %v)", len(buf), pat.length)
}
buf = dup(buf[:pat.length])
buf[0] &= ^pat.mask
return uintDecodeIntBuf(buf), pat.length, nil
}