@@ -74,6 +74,8 @@ const HRR_RANDOM: [u8; 32] = [
7474 0xC2 , 0xA2 , 0x11 , 0x16 , 0x7A , 0xBB , 0x8C , 0x5E , 0x07 , 0x9E , 0x09 , 0xE2 , 0xC8 , 0xA8 , 0x33 , 0x9C ,
7575] ;
7676
77+ const MAX_RETAINED_CLIENT_HELLO : usize = 64 ;
78+
7779/// DTLS 1.3 server
7880pub struct Server {
7981 /// Current server state.
@@ -143,7 +145,7 @@ pub struct Server {
143145
144146 /// Raw packets buffered during auto-sense so they can be replayed
145147 /// to a DTLS 1.2 server on fallback.
146- auto_packets : Vec < Vec < u8 > > ,
148+ retained_hello : VecDeque < Buf > ,
147149}
148150
149151#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
@@ -179,7 +181,7 @@ impl Server {
179181 Self :: new_with_engine ( engine, now, true )
180182 }
181183
182- pub ( crate ) fn new_with_engine ( mut engine : Engine , now : Instant , auto_mode : bool ) -> Server {
184+ pub fn new_with_engine ( mut engine : Engine , now : Instant , auto_mode : bool ) -> Server {
183185 let cookie_secret = engine. random_arr ( ) ;
184186
185187 Server {
@@ -204,7 +206,7 @@ impl Server {
204206 cookie_secret,
205207 pending_key_update_response : false ,
206208 auto_mode,
207- auto_packets : Vec :: new ( ) ,
209+ retained_hello : VecDeque :: with_capacity ( 10 ) ,
208210 }
209211 }
210212
@@ -217,9 +219,15 @@ impl Server {
217219 self . auto_mode
218220 }
219221
220- /// The last time instant seen by this server.
221- pub fn last_now ( & self ) -> Instant {
222- self . last_now
222+ /// Take all relevant config from this server instance.
223+ ///
224+ /// This is used in two cases:
225+ ///
226+ /// 1. Switching a server pending (auto-mode) to dtls12 server
227+ /// 2. set_active(true), turning a server pending (auto-mode) to a ClientPending
228+ pub fn into_parts ( self ) -> ( Arc < Config > , DtlsCertificate , Instant , VecDeque < Buf > ) {
229+ let ( config, cert) = self . engine . into_fallback ( ) ;
230+ ( config, cert, self . last_now , self . retained_hello )
223231 }
224232
225233 pub ( crate ) fn state_name ( & self ) -> & ' static str {
@@ -231,36 +239,24 @@ impl Server {
231239 // the ClientHello so they can be replayed to Server12 on fallback.
232240 if self . auto_mode && self . state == State :: AwaitClientHello {
233241 // Cap buffered fragments to prevent unbounded growth from malicious traffic
234- if self . auto_packets . len ( ) >= 64 {
235- return Err ( Error :: SecurityError (
236- "too many fragmented packets during auto-sense" . to_string ( ) ,
237- ) ) ;
242+ if self . retained_hello . len ( ) >= MAX_RETAINED_CLIENT_HELLO {
243+ return Err ( Error :: TooManyClientHelloFragments ) ;
238244 }
239- self . auto_packets . push ( packet. to_vec ( ) ) ;
245+ self . retained_hello . push_back ( packet. to_buf ( ) ) ;
240246 }
241247
242248 self . engine . parse_packet ( packet) ?;
243249 self . make_progress ( ) ?;
244250
245251 // Once past AwaitClientHello, DTLS 1.3 is committed — free the buffer.
246252 if self . auto_mode && self . state != State :: AwaitClientHello {
247- self . auto_packets . clear ( ) ;
253+ self . retained_hello . clear ( ) ;
248254 self . auto_mode = false ;
249255 }
250256
251257 Ok ( ( ) )
252258 }
253259
254- /// Take the buffered raw packets for DTLS 1.2 fallback replay.
255- pub fn take_auto_packets ( & mut self ) -> Vec < Vec < u8 > > {
256- std:: mem:: take ( & mut self . auto_packets )
257- }
258-
259- /// Number of buffered auto-sense packets.
260- pub fn auto_packet_count ( & self ) -> usize {
261- self . auto_packets . len ( )
262- }
263-
264260 pub fn poll_output < ' a > ( & mut self , buf : & ' a mut [ u8 ] ) -> Output < ' a > {
265261 if let Some ( event) = self . local_events . pop_front ( ) {
266262 return event. into_output ( buf, & self . client_certificates ) ;
0 commit comments