-
Notifications
You must be signed in to change notification settings - Fork 16
/
transport_parameters.go
407 lines (342 loc) · 10.2 KB
/
transport_parameters.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
package minq
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"github.com/bifurcation/mint"
"github.com/bifurcation/mint/syntax"
)
const (
kQuicTransportParamtersXtn = mint.ExtensionType(0xffa5)
)
type TransportParameterId uint16
const (
kTpIdInitialMaxStreamDataBidiLocal = TransportParameterId(0x0000)
kTpIdInitialMaxData = TransportParameterId(0x0001)
kTpIdInitialMaxBidiStreams = TransportParameterId(0x0002)
kTpIdIdleTimeout = TransportParameterId(0x0003)
kTpPreferredAddress = TransportParameterId(0x0004)
kTpIdMaxPacketSize = TransportParameterId(0x0005)
kTpIdStatelessResetToken = TransportParameterId(0x0006)
kTpIdAckDelayExponent = TransportParameterId(0x0007)
kTpIdInitialMaxUniStreams = TransportParameterId(0x0008)
kTpIdDisableMigration = TransportParameterId(0x0009)
kTpIdInitialMaxStreamDataBidiRemote = TransportParameterId(0x0010)
kTpIdInitialMaxStreamDataUni = TransportParameterId(0x0011)
)
const (
kTpDefaultAckDelayExponent = byte(3)
)
type tpDef struct {
parameter TransportParameterId
val uint32
size uintptr
}
var (
kInitialMaxData = uint64(65536)
kInitialMaxStreamData = uint64(8192)
kConcurrentStreamsBidi = 16
kConcurrentStreamsUni = 16
kTransportParameterDefaults = []tpDef{
{kTpIdInitialMaxStreamDataBidiLocal, uint32(kInitialMaxStreamData), 4},
{kTpIdInitialMaxStreamDataBidiRemote, uint32(kInitialMaxStreamData), 4},
{kTpIdInitialMaxStreamDataUni, uint32(kInitialMaxStreamData), 4},
{kTpIdInitialMaxData, uint32(kInitialMaxData), 4},
{kTpIdInitialMaxBidiStreams, uint32(kConcurrentStreamsBidi), 2},
{kTpIdIdleTimeout, 5, 2},
{kTpIdInitialMaxUniStreams, uint32(kConcurrentStreamsUni), 2},
}
)
type transportParameters struct {
maxStreamDataUni uint32
maxStreamDataBidiLocal uint32
maxStreamDataBidiRemote uint32
maxData uint32
maxStreamsBidi int
maxStreamsUni int
idleTimeout uint16
ackDelayExp uint8
}
type TransportParameterList []transportParameter
type transportParameter struct {
Parameter TransportParameterId
Value []byte `tls:"head=2"`
}
type clientHelloTransportParameters struct {
InitialVersion VersionNumber
Parameters TransportParameterList `tls:"head=2"`
}
type encryptedExtensionsTransportParameters struct {
NegotiatedVersion VersionNumber
SupportedVersions []VersionNumber `tls:"head=1"`
Parameters TransportParameterList `tls:"head=2"`
}
func (tp *TransportParameterList) addUintParameter(id TransportParameterId, val uint32, size uintptr) error {
var buf bytes.Buffer
uintEncodeInt(&buf, uint64(val), size)
*tp = append(*tp, transportParameter{
id,
buf.Bytes(),
})
return nil
}
func (tp *TransportParameterList) getParameter(id TransportParameterId) []byte {
for _, ex := range *tp {
if ex.Parameter == id {
return ex.Value
}
}
return nil
}
func (tp *TransportParameterList) getUintParameter(id TransportParameterId, size uintptr) (uint32, error) {
assert(size <= 4)
b := tp.getParameter(id)
if b == nil {
logf(logTypeHandshake, "Missing transport parameter %v", id)
return 0, ErrorMissingValue
}
if len(b) != int(size) {
logf(logTypeHandshake, "Bogus transport parameter %v", id)
return 0, ErrorInvalidEncoding
}
buf := bytes.NewReader(b)
tmp, err := uintDecodeInt(buf, size)
if err != nil {
return 0, err
}
return uint32(tmp), nil
}
func (tp *TransportParameterList) getUintParameterOrDefault(id TransportParameterId, size uintptr, def uint32) (uint32, error) {
assert(size <= 4)
b := tp.getParameter(id)
if b == nil {
logf(logTypeHandshake, "Missing transport parameter %v", id)
return def, nil
}
if len(b) != int(size) {
logf(logTypeHandshake, "Bogus transport parameter %v", id)
return 0, ErrorInvalidEncoding
}
buf := bytes.NewReader(b)
tmp, err := uintDecodeInt(buf, size)
if err != nil {
return 0, err
}
return uint32(tmp), nil
}
func (tp *TransportParameterList) addOpaqueParameter(id TransportParameterId, b []byte) error {
*tp = append(*tp, transportParameter{
id,
b,
})
return nil
}
func (tp *TransportParameterList) createCommonTransportParameters() error {
for _, p := range kTransportParameterDefaults {
err := tp.addUintParameter(p.parameter, p.val, p.size)
if err != nil {
return err
}
}
return nil
}
// Implement mint.AppExtensionHandler.
type transportParametersXtnBody struct {
body []byte
}
func (t transportParametersXtnBody) Type() mint.ExtensionType {
return kQuicTransportParamtersXtn
}
func (t transportParametersXtnBody) Marshal() ([]byte, error) {
return t.body, nil
}
func (t *transportParametersXtnBody) Unmarshal(data []byte) (int, error) {
t.body = data
return len(t.body), nil
}
type transportParametersHandler struct {
log loggingFunction
role Role
version VersionNumber
peerParams *transportParameters
}
func newTransportParametersHandler(log loggingFunction, role Role, version VersionNumber) *transportParametersHandler {
return &transportParametersHandler{log, role, version, nil}
}
func (h *transportParametersHandler) setDummyPeerParams() {
h.peerParams = &transportParameters{
uint32(kInitialMaxStreamData),
uint32(kInitialMaxStreamData),
uint32(kInitialMaxStreamData),
uint32(kInitialMaxData),
kConcurrentStreamsBidi,
kConcurrentStreamsUni,
600,
uint8(1),
}
}
func (h *transportParametersHandler) Send(hs mint.HandshakeType, el *mint.ExtensionList) error {
if h.role == RoleClient {
h.log(logTypeHandshake, "Sending transport parameters")
if hs != mint.HandshakeTypeClientHello {
return nil
}
b, err := h.createClientHelloTransportParameters()
if err != nil {
return err
}
h.log(logTypeTrace, "ClientHelloTransportParameters=%s", hex.EncodeToString(b))
el.Add(&transportParametersXtnBody{b})
return nil
}
if h.peerParams == nil {
return nil
}
if hs != mint.HandshakeTypeEncryptedExtensions {
return nil
}
h.log(logTypeHandshake, "Sending transport parameters message")
b, err := h.createEncryptedExtensionsTransportParameters()
if err != nil {
return err
}
el.Add(&transportParametersXtnBody{b})
return nil
}
func (h *transportParametersHandler) Receive(hs mint.HandshakeType, el *mint.ExtensionList) error {
h.log(logTypeHandshake, "%p TransportParametersHandler message=%d", h, hs)
// First see if the other side sent the extension.
var body transportParametersXtnBody
found, err := el.Find(&body)
if err != nil {
return fmt.Errorf("Invalid transport parameters")
}
if found {
h.log(logTypeTrace, "Retrieved transport parameters len=%d %v", len(body.body), hex.EncodeToString(body.body))
}
var params *TransportParameterList
switch hs {
case mint.HandshakeTypeEncryptedExtensions:
if h.role != RoleClient {
return fmt.Errorf("EncryptedExtensions received but not a client")
}
if !found {
h.log(logTypeHandshake, "Missing transport parameters")
return fmt.Errorf("Missing transport parameters")
}
var eeParams encryptedExtensionsTransportParameters
_, err = syntax.Unmarshal(body.body, &eeParams)
if err != nil {
h.log(logTypeHandshake, "Failed to decode parameters")
return err
}
params = &eeParams.Parameters
// TODO([email protected]): Process version #s
case mint.HandshakeTypeClientHello:
if h.role != RoleServer {
return fmt.Errorf("ClientHello received but not a server")
}
if !found {
h.log(logTypeHandshake, "Missing transport parameters")
return fmt.Errorf("Missing transport parameters")
}
// TODO([email protected]): Process version #s
var chParams clientHelloTransportParameters
_, err = syntax.Unmarshal(body.body, &chParams)
if err != nil {
h.log(logTypeHandshake, "Couldn't unmarshal %v", err)
return err
}
params = &chParams.Parameters
default:
if found {
return fmt.Errorf("Received quic_transport_parameters in inappropriate message %v", hs)
}
return nil
}
// Now try to process each param.
// TODO([email protected]): Enforce that each param appears only once.
var tp transportParameters
h.log(logTypeHandshake, "Reading transport parameters values")
tp.maxStreamDataBidiLocal, err = params.getUintParameterOrDefault(kTpIdInitialMaxStreamDataBidiLocal, 4, 0)
if err != nil {
return err
}
tp.maxStreamDataBidiRemote, err = params.getUintParameterOrDefault(kTpIdInitialMaxStreamDataBidiRemote, 4, 0)
if err != nil {
return err
}
tp.maxStreamDataUni, err = params.getUintParameterOrDefault(kTpIdInitialMaxStreamDataUni, 4, 0)
if err != nil {
return err
}
tp.maxData, err = params.getUintParameterOrDefault(kTpIdInitialMaxData, 4, 0)
if err != nil {
return err
}
tmp, err := params.getUintParameterOrDefault(kTpIdInitialMaxBidiStreams, 2, 0)
if err != nil {
return err
}
tp.maxStreamsBidi = int(tmp)
if h.role == RoleClient {
tp.maxStreamsBidi++ // Allow for stream 0.
}
tmp, err = params.getUintParameterOrDefault(kTpIdInitialMaxUniStreams, 2, 0)
if err != nil {
return err
}
tp.maxStreamsUni = int(tmp)
tmp, err = params.getUintParameter(kTpIdIdleTimeout, 2)
if err != nil {
return err
}
tp.idleTimeout = uint16(tmp)
tmp, err = params.getUintParameterOrDefault(kTpIdAckDelayExponent, 1, 0)
if err != nil {
return err
}
h.peerParams = &tp
h.log(logTypeHandshake, "Finished reading transport parameters")
return nil
}
func (h *transportParametersHandler) createClientHelloTransportParameters() ([]byte, error) {
chtp := clientHelloTransportParameters{
h.version,
nil,
}
err := chtp.Parameters.createCommonTransportParameters()
if err != nil {
return nil, err
}
b, err := syntax.Marshal(chtp)
if err != nil {
return nil, err
}
return b, nil
}
func (h *transportParametersHandler) createEncryptedExtensionsTransportParameters() ([]byte, error) {
eetp := encryptedExtensionsTransportParameters{
h.version,
[]VersionNumber{
h.version,
},
nil,
}
err := eetp.Parameters.createCommonTransportParameters()
if err != nil {
return nil, err
}
b := make([]byte, 16)
_, err = rand.Read(b)
if err != nil {
return nil, err
}
eetp.Parameters.addOpaqueParameter(kTpIdStatelessResetToken, b)
b, err = syntax.Marshal(eetp)
if err != nil {
return nil, err
}
return b, nil
}