1- use crate :: aead:: { ChaCha8PacketKey , PlaintextHeaderKey , HEADER_KEYPAIR } ;
1+ use crate :: aead:: { header_keypair , ChaCha8PacketKey , PlaintextHeaderKey } ;
22use crate :: dh:: DiffieHellman ;
33use crate :: keylog:: KeyLog ;
44use ed25519_dalek:: { Keypair , PublicKey } ;
55use quinn_proto:: crypto:: {
6- ClientConfig , ExportKeyingMaterialError , KeyPair , Keys , ServerConfig , Session ,
6+ ClientConfig , ExportKeyingMaterialError , HeaderKey , KeyPair , Keys , PacketKey , ServerConfig ,
7+ Session ,
78} ;
89use quinn_proto:: transport_parameters:: TransportParameters ;
910use quinn_proto:: { ConnectError , ConnectionId , Side , TransportError , TransportErrorCode } ;
1011use ring:: aead;
12+ use std:: any:: Any ;
1113use std:: io:: Cursor ;
1214use std:: sync:: Arc ;
1315use subtle:: ConstantTimeEq ;
@@ -80,27 +82,62 @@ pub struct NoiseConfig {
8082 supported_protocols : Option < Vec < Vec < u8 > > > ,
8183}
8284
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+ ) ) )
8697 }
98+ }
8799
100+ impl ServerConfig for NoiseConfig {
88101 fn start_session (
89- & self ,
90- _ : & str ,
102+ self : Arc < Self > ,
103+ version : u32 ,
91104 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) )
94107 }
95- }
96108
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+ } )
100122 }
101123
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
104141 }
105142}
106143
@@ -192,27 +229,8 @@ fn connection_refused(reason: &str) -> TransportError {
192229 }
193230}
194231
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 > {
216234 if !self . is_handshaking ( ) {
217235 self . xoodyak . ratchet ( ) ;
218236 }
@@ -226,7 +244,7 @@ impl Session for NoiseSession {
226244 }
227245 let client = ChaCha8PacketKey :: new ( client) ;
228246 let server = ChaCha8PacketKey :: new ( server) ;
229- let key = match self . side {
247+ match self . side {
230248 Side :: Client => KeyPair {
231249 local : client,
232250 remote : server,
@@ -235,8 +253,27 @@ impl Session for NoiseSession {
235253 local : server,
236254 remote : client,
237255 } ,
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+ } )
240277 }
241278
242279 fn read_handshake ( & mut self , handshake : & [ u8 ] ) -> Result < bool , TransportError > {
@@ -378,7 +415,7 @@ impl Session for NoiseSession {
378415 }
379416 }
380417
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 > {
382419 tracing:: trace!( "write_handshake {:?} {:?}" , self . state, self . side) ;
383420 match ( self . state , self . side ) {
384421 ( State :: Initial , Side :: Client ) => {
@@ -430,12 +467,15 @@ impl Session for NoiseSession {
430467 None
431468 }
432469 ( State :: ZeroRtt , _) => {
433- let packet = self . next_1rtt_keys ( ) ;
470+ let packet = self . next_1rtt_keys0 ( ) ;
434471 self . state = State :: Handshake ;
435472 self . zero_rtt_key = Some ( packet. local . clone ( ) ) ;
436473 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+ } ,
439479 } )
440480 }
441481 ( State :: Handshake , Side :: Server ) => {
@@ -459,18 +499,18 @@ impl Session for NoiseSession {
459499 self . xoodyak . squeeze ( & mut tag) ;
460500 handshake. extend_from_slice ( & tag) ;
461501 // 1-rtt keys
462- let packet = self . next_1rtt_keys ( ) ;
502+ let packet = self . next_1rtt_keys ( ) . unwrap ( ) ;
463503 self . state = State :: Data ;
464504 Some ( Keys {
465- header : HEADER_KEYPAIR ,
505+ header : header_keypair ( ) ,
466506 packet,
467507 } )
468508 }
469509 ( State :: OneRtt , _) => {
470- let packet = self . next_1rtt_keys ( ) ;
510+ let packet = self . next_1rtt_keys ( ) . unwrap ( ) ;
471511 self . state = State :: Data ;
472512 Some ( Keys {
473- header : HEADER_KEYPAIR ,
513+ header : header_keypair ( ) ,
474514 packet,
475515 } )
476516 }
@@ -482,8 +522,8 @@ impl Session for NoiseSession {
482522 self . state != State :: Data
483523 }
484524
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 ? ) )
487527 }
488528
489529 fn transport_parameters ( & self ) -> Result < Option < TransportParameters > , TransportError > {
@@ -494,8 +534,8 @@ impl Session for NoiseSession {
494534 }
495535 }
496536
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 ( ) ? ) )
499539 }
500540
501541 fn export_keying_material (
@@ -511,34 +551,18 @@ impl Session for NoiseSession {
511551 Ok ( ( ) )
512552 }
513553
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+ ) )
516559 }
517560
518561 fn early_data_accepted ( & self ) -> Option < bool > {
519562 Some ( true )
520563 }
521564
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 {
542566 let tag_start = match payload. len ( ) . checked_sub ( 16 ) {
543567 Some ( x) => x,
544568 None => return false ,
0 commit comments