1
- use crate :: aead:: { ChaCha8PacketKey , PlaintextHeaderKey , HEADER_KEYPAIR } ;
1
+ use crate :: aead:: { header_keypair , ChaCha8PacketKey , PlaintextHeaderKey } ;
2
2
use crate :: dh:: DiffieHellman ;
3
3
use crate :: keylog:: KeyLog ;
4
4
use ed25519_dalek:: { Keypair , PublicKey } ;
5
5
use quinn_proto:: crypto:: {
6
- ClientConfig , ExportKeyingMaterialError , KeyPair , Keys , ServerConfig , Session ,
6
+ ClientConfig , ExportKeyingMaterialError , HeaderKey , KeyPair , Keys , PacketKey , ServerConfig ,
7
+ Session ,
7
8
} ;
8
9
use quinn_proto:: transport_parameters:: TransportParameters ;
9
10
use quinn_proto:: { ConnectError , ConnectionId , Side , TransportError , TransportErrorCode } ;
10
11
use ring:: aead;
12
+ use std:: any:: Any ;
11
13
use std:: io:: Cursor ;
12
14
use std:: sync:: Arc ;
13
15
use subtle:: ConstantTimeEq ;
@@ -80,27 +82,62 @@ pub struct NoiseConfig {
80
82
supported_protocols : Option < Vec < Vec < u8 > > > ,
81
83
}
82
84
83
- impl ClientConfig < NoiseSession > for NoiseConfig {
84
- fn new ( ) -> Self {
85
- Default :: default ( )
85
+ impl ClientConfig for NoiseConfig {
86
+ fn start_session (
87
+ self : Arc < Self > ,
88
+ version : u32 ,
89
+ server_name : & str ,
90
+ params : & TransportParameters ,
91
+ ) -> Result < Box < dyn Session > , ConnectError > {
92
+ Ok ( Box :: new ( NoiseConfig :: start_session (
93
+ & self ,
94
+ Side :: Client ,
95
+ params,
96
+ ) ) )
86
97
}
98
+ }
87
99
100
+ impl ServerConfig for NoiseConfig {
88
101
fn start_session (
89
- & self ,
90
- _ : & str ,
102
+ self : Arc < Self > ,
103
+ version : u32 ,
91
104
params : & TransportParameters ,
92
- ) -> Result < NoiseSession , ConnectError > {
93
- Ok ( NoiseConfig :: start_session ( self , Side :: Client , params) )
105
+ ) -> Box < dyn Session > {
106
+ Box :: new ( NoiseConfig :: start_session ( & self , Side :: Server , params) )
94
107
}
95
- }
96
108
97
- impl ServerConfig < NoiseSession > for Arc < NoiseConfig > {
98
- fn new ( ) -> Self {
99
- Default :: default ( )
109
+ fn initial_keys (
110
+ & self ,
111
+ version : u32 ,
112
+ dst_cid : & ConnectionId ,
113
+ side : Side ,
114
+ ) -> Result < Keys , quinn_proto:: crypto:: UnsupportedVersion > {
115
+ Ok ( Keys {
116
+ header : header_keypair ( ) ,
117
+ packet : KeyPair {
118
+ local : Box :: new ( ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ) ,
119
+ remote : Box :: new ( ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ) ,
120
+ } ,
121
+ } )
100
122
}
101
123
102
- fn start_session ( & self , params : & TransportParameters ) -> NoiseSession {
103
- NoiseConfig :: start_session ( self , Side :: Server , params)
124
+ fn retry_tag ( & self , version : u32 , orig_dst_cid : & ConnectionId , packet : & [ u8 ] ) -> [ u8 ; 16 ] {
125
+ let mut pseudo_packet = Vec :: with_capacity ( packet. len ( ) + orig_dst_cid. len ( ) + 1 ) ;
126
+ pseudo_packet. push ( orig_dst_cid. len ( ) as u8 ) ;
127
+ pseudo_packet. extend_from_slice ( orig_dst_cid) ;
128
+ pseudo_packet. extend_from_slice ( packet) ;
129
+
130
+ let nonce = aead:: Nonce :: assume_unique_for_key ( RETRY_INTEGRITY_NONCE ) ;
131
+ let key = aead:: LessSafeKey :: new (
132
+ aead:: UnboundKey :: new ( & aead:: AES_128_GCM , & RETRY_INTEGRITY_KEY ) . unwrap ( ) ,
133
+ ) ;
134
+
135
+ let tag = key
136
+ . seal_in_place_separate_tag ( nonce, aead:: Aad :: from ( pseudo_packet) , & mut [ ] )
137
+ . unwrap ( ) ;
138
+ let mut result = [ 0 ; 16 ] ;
139
+ result. copy_from_slice ( tag. as_ref ( ) ) ;
140
+ result
104
141
}
105
142
}
106
143
@@ -192,27 +229,8 @@ fn connection_refused(reason: &str) -> TransportError {
192
229
}
193
230
}
194
231
195
- impl Session for NoiseSession {
196
- type HandshakeData = Vec < u8 > ;
197
- type Identity = PublicKey ;
198
- type ClientConfig = NoiseConfig ;
199
- type ServerConfig = Arc < NoiseConfig > ;
200
- type HmacKey = ring:: hmac:: Key ;
201
- type HandshakeTokenKey = ring:: hkdf:: Prk ;
202
- type HeaderKey = PlaintextHeaderKey ;
203
- type PacketKey = ChaCha8PacketKey ;
204
-
205
- fn initial_keys ( _: & ConnectionId , _: Side ) -> Keys < Self > {
206
- Keys {
207
- header : HEADER_KEYPAIR ,
208
- packet : KeyPair {
209
- local : ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ,
210
- remote : ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ,
211
- } ,
212
- }
213
- }
214
-
215
- fn next_1rtt_keys ( & mut self ) -> KeyPair < Self :: PacketKey > {
232
+ impl NoiseSession {
233
+ fn next_1rtt_keys0 ( & mut self ) -> KeyPair < ChaCha8PacketKey > {
216
234
if !self . is_handshaking ( ) {
217
235
self . xoodyak . ratchet ( ) ;
218
236
}
@@ -226,7 +244,7 @@ impl Session for NoiseSession {
226
244
}
227
245
let client = ChaCha8PacketKey :: new ( client) ;
228
246
let server = ChaCha8PacketKey :: new ( server) ;
229
- let key = match self . side {
247
+ match self . side {
230
248
Side :: Client => KeyPair {
231
249
local : client,
232
250
remote : server,
@@ -235,8 +253,27 @@ impl Session for NoiseSession {
235
253
local : server,
236
254
remote : client,
237
255
} ,
238
- } ;
239
- key
256
+ }
257
+ }
258
+ }
259
+
260
+ impl Session for NoiseSession {
261
+ fn initial_keys ( & self , _: & ConnectionId , _: Side ) -> Keys {
262
+ Keys {
263
+ header : header_keypair ( ) ,
264
+ packet : KeyPair {
265
+ local : Box :: new ( ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ) ,
266
+ remote : Box :: new ( ChaCha8PacketKey :: new ( [ 0 ; 32 ] ) ) ,
267
+ } ,
268
+ }
269
+ }
270
+
271
+ fn next_1rtt_keys ( & mut self ) -> Option < KeyPair < Box < dyn PacketKey > > > {
272
+ let key = self . next_1rtt_keys0 ( ) ;
273
+ Some ( KeyPair {
274
+ local : Box :: new ( key. local ) ,
275
+ remote : Box :: new ( key. remote ) ,
276
+ } )
240
277
}
241
278
242
279
fn read_handshake ( & mut self , handshake : & [ u8 ] ) -> Result < bool , TransportError > {
@@ -378,7 +415,7 @@ impl Session for NoiseSession {
378
415
}
379
416
}
380
417
381
- fn write_handshake ( & mut self , handshake : & mut Vec < u8 > ) -> Option < Keys < Self > > {
418
+ fn write_handshake ( & mut self , handshake : & mut Vec < u8 > ) -> Option < Keys > {
382
419
tracing:: trace!( "write_handshake {:?} {:?}" , self . state, self . side) ;
383
420
match ( self . state , self . side ) {
384
421
( State :: Initial , Side :: Client ) => {
@@ -430,12 +467,15 @@ impl Session for NoiseSession {
430
467
None
431
468
}
432
469
( State :: ZeroRtt , _) => {
433
- let packet = self . next_1rtt_keys ( ) ;
470
+ let packet = self . next_1rtt_keys0 ( ) ;
434
471
self . state = State :: Handshake ;
435
472
self . zero_rtt_key = Some ( packet. local . clone ( ) ) ;
436
473
Some ( Keys {
437
- header : HEADER_KEYPAIR ,
438
- packet,
474
+ header : header_keypair ( ) ,
475
+ packet : KeyPair {
476
+ local : Box :: new ( packet. local ) ,
477
+ remote : Box :: new ( packet. remote ) ,
478
+ } ,
439
479
} )
440
480
}
441
481
( State :: Handshake , Side :: Server ) => {
@@ -459,18 +499,18 @@ impl Session for NoiseSession {
459
499
self . xoodyak . squeeze ( & mut tag) ;
460
500
handshake. extend_from_slice ( & tag) ;
461
501
// 1-rtt keys
462
- let packet = self . next_1rtt_keys ( ) ;
502
+ let packet = self . next_1rtt_keys ( ) . unwrap ( ) ;
463
503
self . state = State :: Data ;
464
504
Some ( Keys {
465
- header : HEADER_KEYPAIR ,
505
+ header : header_keypair ( ) ,
466
506
packet,
467
507
} )
468
508
}
469
509
( State :: OneRtt , _) => {
470
- let packet = self . next_1rtt_keys ( ) ;
510
+ let packet = self . next_1rtt_keys ( ) . unwrap ( ) ;
471
511
self . state = State :: Data ;
472
512
Some ( Keys {
473
- header : HEADER_KEYPAIR ,
513
+ header : header_keypair ( ) ,
474
514
packet,
475
515
} )
476
516
}
@@ -482,8 +522,8 @@ impl Session for NoiseSession {
482
522
self . state != State :: Data
483
523
}
484
524
485
- fn peer_identity ( & self ) -> Option < Self :: Identity > {
486
- self . remote_s
525
+ fn peer_identity ( & self ) -> Option < Box < dyn Any > > {
526
+ Some ( Box :: new ( self . remote_s ? ) )
487
527
}
488
528
489
529
fn transport_parameters ( & self ) -> Result < Option < TransportParameters > , TransportError > {
@@ -494,8 +534,8 @@ impl Session for NoiseSession {
494
534
}
495
535
}
496
536
497
- fn handshake_data ( & self ) -> Option < Self :: HandshakeData > {
498
- self . alpn . clone ( )
537
+ fn handshake_data ( & self ) -> Option < Box < dyn Any > > {
538
+ Some ( Box :: new ( self . alpn . clone ( ) ? ) )
499
539
}
500
540
501
541
fn export_keying_material (
@@ -511,34 +551,18 @@ impl Session for NoiseSession {
511
551
Ok ( ( ) )
512
552
}
513
553
514
- fn early_crypto ( & self ) -> Option < ( Self :: HeaderKey , Self :: PacketKey ) > {
515
- Some ( ( PlaintextHeaderKey , self . zero_rtt_key . clone ( ) . unwrap ( ) ) )
554
+ fn early_crypto ( & self ) -> Option < ( Box < dyn HeaderKey > , Box < dyn PacketKey > ) > {
555
+ Some ( (
556
+ Box :: new ( PlaintextHeaderKey ) ,
557
+ Box :: new ( self . zero_rtt_key . clone ( ) ?) ,
558
+ ) )
516
559
}
517
560
518
561
fn early_data_accepted ( & self ) -> Option < bool > {
519
562
Some ( true )
520
563
}
521
564
522
- fn retry_tag ( orig_dst_cid : & ConnectionId , packet : & [ u8 ] ) -> [ u8 ; 16 ] {
523
- let mut pseudo_packet = Vec :: with_capacity ( packet. len ( ) + orig_dst_cid. len ( ) + 1 ) ;
524
- pseudo_packet. push ( orig_dst_cid. len ( ) as u8 ) ;
525
- pseudo_packet. extend_from_slice ( orig_dst_cid) ;
526
- pseudo_packet. extend_from_slice ( packet) ;
527
-
528
- let nonce = aead:: Nonce :: assume_unique_for_key ( RETRY_INTEGRITY_NONCE ) ;
529
- let key = aead:: LessSafeKey :: new (
530
- aead:: UnboundKey :: new ( & aead:: AES_128_GCM , & RETRY_INTEGRITY_KEY ) . unwrap ( ) ,
531
- ) ;
532
-
533
- let tag = key
534
- . seal_in_place_separate_tag ( nonce, aead:: Aad :: from ( pseudo_packet) , & mut [ ] )
535
- . unwrap ( ) ;
536
- let mut result = [ 0 ; 16 ] ;
537
- result. copy_from_slice ( tag. as_ref ( ) ) ;
538
- result
539
- }
540
-
541
- fn is_valid_retry ( orig_dst_cid : & ConnectionId , header : & [ u8 ] , payload : & [ u8 ] ) -> bool {
565
+ fn is_valid_retry ( & self , orig_dst_cid : & ConnectionId , header : & [ u8 ] , payload : & [ u8 ] ) -> bool {
542
566
let tag_start = match payload. len ( ) . checked_sub ( 16 ) {
543
567
Some ( x) => x,
544
568
None => return false ,
0 commit comments