1
2
3
4
5 package tls
6
7 import (
8 "context"
9 "errors"
10 "fmt"
11 "net"
12 )
13
14
15
16 type QUICEncryptionLevel int
17
18 const (
19 QUICEncryptionLevelInitial = QUICEncryptionLevel(iota)
20 QUICEncryptionLevelEarly
21 QUICEncryptionLevelHandshake
22 QUICEncryptionLevelApplication
23 )
24
25 func (l QUICEncryptionLevel) String() string {
26 switch l {
27 case QUICEncryptionLevelInitial:
28 return "Initial"
29 case QUICEncryptionLevelEarly:
30 return "Early"
31 case QUICEncryptionLevelHandshake:
32 return "Handshake"
33 case QUICEncryptionLevelApplication:
34 return "Application"
35 default:
36 return fmt.Sprintf("QUICEncryptionLevel(%v)", int(l))
37 }
38 }
39
40
41
42
43
44 type QUICConn struct {
45 conn *Conn
46
47 sessionTicketSent bool
48 }
49
50
51 type QUICConfig struct {
52 TLSConfig *Config
53
54
55
56
57
58
59 EnableSessionEvents bool
60
61
62 ClientHelloInfoConn net.Conn
63 }
64
65
66 type QUICEventKind int
67
68 const (
69
70 QUICNoEvent QUICEventKind = iota
71
72
73
74
75
76
77
78 QUICSetReadSecret
79 QUICSetWriteSecret
80
81
82
83 QUICWriteData
84
85
86
87 QUICTransportParameters
88
89
90
91
92
93
94
95
96 QUICTransportParametersRequired
97
98
99
100
101
102 QUICRejectedEarlyData
103
104
105 QUICHandshakeDone
106
107
108
109
110
111
112
113
114
115 QUICResumeSession
116
117
118
119
120
121
122
123 QUICStoreSession
124
125
126
127
128 QUICErrorEvent
129 )
130
131
132
133
134
135 type QUICEvent struct {
136 Kind QUICEventKind
137
138
139 Level QUICEncryptionLevel
140
141
142
143 Data []byte
144
145
146 Suite uint16
147
148
149 SessionState *SessionState
150
151
152
153 Err error
154 }
155
156 type quicState struct {
157 events []QUICEvent
158 nextEvent int
159
160
161
162
163
164 eventArr [8]QUICEvent
165
166 started bool
167 signalc chan struct{}
168 blockedc chan struct{}
169 ctx context.Context
170 cancel context.CancelFunc
171
172 waitingForDrain bool
173 errorReturned bool
174
175
176
177
178 readbuf []byte
179
180 transportParams []byte
181
182 enableSessionEvents bool
183 clientHelloInfoConn net.Conn
184 }
185
186
187
188 func QUICClient(config *QUICConfig) *QUICConn {
189 return newQUICConn(Client(nil, config.TLSConfig), config)
190 }
191
192
193
194 func QUICServer(config *QUICConfig) *QUICConn {
195 return newQUICConn(Server(nil, config.TLSConfig), config)
196 }
197
198 func newQUICConn(conn *Conn, config *QUICConfig) *QUICConn {
199 conn.quic = &quicState{
200 signalc: make(chan struct{}),
201 blockedc: make(chan struct{}),
202 enableSessionEvents: config.EnableSessionEvents,
203 clientHelloInfoConn: config.ClientHelloInfoConn,
204 }
205 conn.quic.events = conn.quic.eventArr[:0]
206 return &QUICConn{
207 conn: conn,
208 }
209 }
210
211
212
213
214
215 func (q *QUICConn) Start(ctx context.Context) error {
216 if q.conn.quic.started {
217 return quicError(errors.New("tls: Start called more than once"))
218 }
219 q.conn.quic.started = true
220 go q.conn.HandshakeContext(ctx)
221 if _, ok := <-q.conn.quic.blockedc; !ok {
222 return q.conn.handshakeErr
223 }
224 return nil
225 }
226
227
228
229 func (q *QUICConn) NextEvent() QUICEvent {
230 qs := q.conn.quic
231 if last := qs.nextEvent - 1; last >= 0 && len(qs.events[last].Data) > 0 {
232
233
234 qs.events[last].Data[0] = 0
235 }
236 if qs.nextEvent >= len(qs.events) && qs.waitingForDrain {
237 qs.waitingForDrain = false
238 <-qs.signalc
239 <-qs.blockedc
240 }
241 if err := q.conn.handshakeErr; err != nil {
242 if qs.errorReturned {
243 return QUICEvent{Kind: QUICNoEvent}
244 }
245 qs.errorReturned = true
246 qs.events = nil
247 qs.nextEvent = 0
248 return QUICEvent{Kind: QUICErrorEvent, Err: q.conn.handshakeErr}
249 }
250 if qs.nextEvent >= len(qs.events) {
251 qs.events = qs.events[:0]
252 qs.nextEvent = 0
253 return QUICEvent{Kind: QUICNoEvent}
254 }
255 e := qs.events[qs.nextEvent]
256 qs.events[qs.nextEvent] = QUICEvent{}
257 qs.nextEvent++
258 return e
259 }
260
261
262 func (q *QUICConn) Close() error {
263 if q.conn.quic.ctx == nil {
264 return nil
265 }
266 q.conn.quic.cancel()
267 <-q.conn.quic.signalc
268 for range q.conn.quic.blockedc {
269
270 }
271 return q.conn.handshakeErr
272 }
273
274
275
276 func (q *QUICConn) HandleData(level QUICEncryptionLevel, data []byte) error {
277 c := q.conn
278 if c.in.level != level {
279 return quicError(c.in.setErrorLocked(errors.New("tls: handshake data received at wrong level")))
280 }
281 c.quic.readbuf = data
282 <-c.quic.signalc
283 _, ok := <-c.quic.blockedc
284 if ok {
285
286 return nil
287 }
288
289 c.handshakeMutex.Lock()
290 defer c.handshakeMutex.Unlock()
291 c.handBuf().Write(c.quic.readbuf)
292 c.quic.readbuf = nil
293 for q.conn.handLen() >= 4 && q.conn.handshakeErr == nil {
294 b := q.conn.hand.Bytes()
295 n := int(b[1])<<16 | int(b[2])<<8 | int(b[3])
296 if n > maxHandshake {
297 q.conn.handshakeErr = fmt.Errorf("tls: handshake message of length %d bytes exceeds maximum of %d bytes", n, maxHandshake)
298 break
299 }
300 if len(b) < 4+n {
301 return nil
302 }
303 if err := q.conn.handlePostHandshakeMessage(); err != nil {
304 q.conn.handshakeErr = err
305 }
306 }
307 q.conn.releaseHand()
308 if q.conn.handshakeErr != nil {
309 return quicError(q.conn.handshakeErr)
310 }
311 return nil
312 }
313
314 type QUICSessionTicketOptions struct {
315
316 EarlyData bool
317 Extra [][]byte
318 }
319
320
321
322
323 func (q *QUICConn) SendSessionTicket(opts QUICSessionTicketOptions) error {
324 c := q.conn
325 if c.config.SessionTicketsDisabled {
326 return nil
327 }
328 if !c.isHandshakeComplete.Load() {
329 return quicError(errors.New("tls: SendSessionTicket called before handshake completed"))
330 }
331 if c.isClient {
332 return quicError(errors.New("tls: SendSessionTicket called on the client"))
333 }
334 if q.sessionTicketSent {
335 return quicError(errors.New("tls: SendSessionTicket called multiple times"))
336 }
337 q.sessionTicketSent = true
338 return quicError(c.sendSessionTicket(opts.EarlyData, opts.Extra))
339 }
340
341
342
343
344
345 func (q *QUICConn) StoreSession(session *SessionState) error {
346 c := q.conn
347 if !c.isClient {
348 return quicError(errors.New("tls: StoreSessionTicket called on the server"))
349 }
350 cacheKey := c.clientSessionCacheKey()
351 if cacheKey == "" {
352 return nil
353 }
354 cs := &ClientSessionState{session: session}
355 c.config.ClientSessionCache.Put(cacheKey, cs)
356 return nil
357 }
358
359
360 func (q *QUICConn) ConnectionState() ConnectionState {
361 return q.conn.ConnectionState()
362 }
363
364
365
366
367
368 func (q *QUICConn) SetTransportParameters(params []byte) {
369 if params == nil {
370 params = []byte{}
371 }
372 q.conn.quic.transportParams = params
373 if q.conn.quic.started {
374 <-q.conn.quic.signalc
375 <-q.conn.quic.blockedc
376 }
377 }
378
379
380
381 func quicError(err error) error {
382 if err == nil {
383 return nil
384 }
385 if _, ok := errors.AsType[AlertError](err); ok {
386 return err
387 }
388 a, ok := errors.AsType[alert](err)
389 if !ok {
390 a = alertInternalError
391 }
392
393
394 return fmt.Errorf("%w%.0w", err, AlertError(a))
395 }
396
397 func (c *Conn) quicReadHandshakeBytes(n int) error {
398 for c.handLen() < n {
399 if err := c.quicWaitForSignal(); err != nil {
400 return err
401 }
402 }
403 return nil
404 }
405
406 func (c *Conn) quicSetReadSecret(level QUICEncryptionLevel, suite uint16, secret []byte) error {
407
408
409
410
411 if c.handLen() != 0 {
412 c.sendAlert(alertUnexpectedMessage)
413 return errors.New("tls: handshake buffer not empty before setting read traffic secret")
414 }
415 c.quic.events = append(c.quic.events, QUICEvent{
416 Kind: QUICSetReadSecret,
417 Level: level,
418 Suite: suite,
419 Data: secret,
420 })
421 return nil
422 }
423
424 func (c *Conn) quicSetWriteSecret(level QUICEncryptionLevel, suite uint16, secret []byte) {
425 c.quic.events = append(c.quic.events, QUICEvent{
426 Kind: QUICSetWriteSecret,
427 Level: level,
428 Suite: suite,
429 Data: secret,
430 })
431 }
432
433 func (c *Conn) quicWriteCryptoData(level QUICEncryptionLevel, data []byte) {
434 var last *QUICEvent
435 if len(c.quic.events) > 0 {
436 last = &c.quic.events[len(c.quic.events)-1]
437 }
438 if last == nil || last.Kind != QUICWriteData || last.Level != level {
439 c.quic.events = append(c.quic.events, QUICEvent{
440 Kind: QUICWriteData,
441 Level: level,
442 })
443 last = &c.quic.events[len(c.quic.events)-1]
444 }
445 last.Data = append(last.Data, data...)
446 }
447
448 func (c *Conn) quicResumeSession(session *SessionState) error {
449 c.quic.events = append(c.quic.events, QUICEvent{
450 Kind: QUICResumeSession,
451 SessionState: session,
452 })
453 c.quic.waitingForDrain = true
454 for c.quic.waitingForDrain {
455 if err := c.quicWaitForSignal(); err != nil {
456 return err
457 }
458 }
459 return nil
460 }
461
462 func (c *Conn) quicStoreSession(session *SessionState) {
463 c.quic.events = append(c.quic.events, QUICEvent{
464 Kind: QUICStoreSession,
465 SessionState: session,
466 })
467 }
468
469 func (c *Conn) quicSetTransportParameters(params []byte) {
470 c.quic.events = append(c.quic.events, QUICEvent{
471 Kind: QUICTransportParameters,
472 Data: params,
473 })
474 }
475
476 func (c *Conn) quicGetTransportParameters() ([]byte, error) {
477 if c.quic.transportParams == nil {
478 c.quic.events = append(c.quic.events, QUICEvent{
479 Kind: QUICTransportParametersRequired,
480 })
481 }
482 for c.quic.transportParams == nil {
483 if err := c.quicWaitForSignal(); err != nil {
484 return nil, err
485 }
486 }
487 return c.quic.transportParams, nil
488 }
489
490 func (c *Conn) quicHandshakeComplete() {
491 c.quic.events = append(c.quic.events, QUICEvent{
492 Kind: QUICHandshakeDone,
493 })
494 }
495
496 func (c *Conn) quicRejectedEarlyData() {
497 c.quic.events = append(c.quic.events, QUICEvent{
498 Kind: QUICRejectedEarlyData,
499 })
500 }
501
502
503
504
505
506
507 func (c *Conn) quicWaitForSignal() error {
508
509
510 c.handshakeMutex.Unlock()
511 defer c.handshakeMutex.Lock()
512
513
514
515 c.quic.blockedc <- struct{}{}
516
517
518
519 c.quic.signalc <- struct{}{}
520 if c.quic.ctx.Err() != nil {
521
522 return c.sendAlertLocked(alertCloseNotify)
523 }
524 c.handBuf().Write(c.quic.readbuf)
525 c.quic.readbuf = nil
526 return nil
527 }
528
View as plain text