@@ -7,6 +7,10 @@ use std::error::Error;
77
88// Default is 32 bytes to decrypt the TLS protocol messages.
99const DEFAULT_MAX_RECV_ONLINE : usize = 32 ;
10+ // Default maximum number of TLS records to allow.
11+ //
12+ // This would allow for up to 50Mb upload from prover to verifier.
13+ const DEFAULT_RECORDS_LIMIT : usize = 256 ;
1014
1115// Current version that is running.
1216static VERSION : Lazy < Version > = Lazy :: new ( || {
@@ -21,12 +25,18 @@ static VERSION: Lazy<Version> = Lazy::new(|| {
2125pub struct ProtocolConfig {
2226 /// Maximum number of bytes that can be sent.
2327 max_sent_data : usize ,
28+ /// Maximum number of application data records that can be sent.
29+ #[ builder( setter( strip_option) , default ) ]
30+ max_sent_records : Option < usize > ,
2431 /// Maximum number of bytes that can be decrypted online, i.e. while the
2532 /// MPC-TLS connection is active.
2633 #[ builder( default = "DEFAULT_MAX_RECV_ONLINE" ) ]
2734 max_recv_data_online : usize ,
2835 /// Maximum number of bytes that can be received.
2936 max_recv_data : usize ,
37+ /// Maximum number of application data records that can be received.
38+ #[ builder( setter( strip_option) , default ) ]
39+ max_recv_records : Option < usize > ,
3040 /// Version that is being run by prover/verifier.
3141 #[ builder( setter( skip) , default = "VERSION.clone()" ) ]
3242 version : Version ,
@@ -54,6 +64,12 @@ impl ProtocolConfig {
5464 self . max_sent_data
5565 }
5666
67+ /// Returns the maximum number of application data records that can
68+ /// be sent.
69+ pub fn max_sent_records ( & self ) -> Option < usize > {
70+ self . max_sent_records
71+ }
72+
5773 /// Returns the maximum number of bytes that can be decrypted online.
5874 pub fn max_recv_data_online ( & self ) -> usize {
5975 self . max_recv_data_online
@@ -63,6 +79,12 @@ impl ProtocolConfig {
6379 pub fn max_recv_data ( & self ) -> usize {
6480 self . max_recv_data
6581 }
82+
83+ /// Returns the maximum number of application data records that can
84+ /// be received.
85+ pub fn max_recv_records ( & self ) -> Option < usize > {
86+ self . max_recv_records
87+ }
6688}
6789
6890/// Protocol configuration validator used by checker (i.e. verifier) to perform
@@ -71,8 +93,14 @@ impl ProtocolConfig {
7193pub struct ProtocolConfigValidator {
7294 /// Maximum number of bytes that can be sent.
7395 max_sent_data : usize ,
96+ /// Maximum number of application data records that can be sent.
97+ #[ builder( default = "DEFAULT_RECORDS_LIMIT" ) ]
98+ max_sent_records : usize ,
7499 /// Maximum number of bytes that can be received.
75100 max_recv_data : usize ,
101+ /// Maximum number of application data records that can be received.
102+ #[ builder( default = "DEFAULT_RECORDS_LIMIT" ) ]
103+ max_recv_records : usize ,
76104 /// Version that is being run by checker.
77105 #[ builder( setter( skip) , default = "VERSION.clone()" ) ]
78106 version : Version ,
@@ -89,15 +117,28 @@ impl ProtocolConfigValidator {
89117 self . max_sent_data
90118 }
91119
120+ /// Returns the maximum number of application data records that can
121+ /// be sent.
122+ pub fn max_sent_records ( & self ) -> usize {
123+ self . max_sent_records
124+ }
125+
92126 /// Returns the maximum number of bytes that can be received.
93127 pub fn max_recv_data ( & self ) -> usize {
94128 self . max_recv_data
95129 }
96130
131+ /// Returns the maximum number of application data records that can
132+ /// be received.
133+ pub fn max_recv_records ( & self ) -> usize {
134+ self . max_recv_records
135+ }
136+
97137 /// Performs compatibility check of the protocol configuration between
98138 /// prover and verifier.
99139 pub fn validate ( & self , config : & ProtocolConfig ) -> Result < ( ) , ProtocolConfigError > {
100140 self . check_max_transcript_size ( config. max_sent_data , config. max_recv_data ) ?;
141+ self . check_max_records ( config. max_sent_records , config. max_recv_records ) ?;
101142 self . check_version ( & config. version ) ?;
102143 Ok ( ( ) )
103144 }
@@ -125,6 +166,32 @@ impl ProtocolConfigValidator {
125166 Ok ( ( ) )
126167 }
127168
169+ fn check_max_records (
170+ & self ,
171+ max_sent_records : Option < usize > ,
172+ max_recv_records : Option < usize > ,
173+ ) -> Result < ( ) , ProtocolConfigError > {
174+ if let Some ( max_sent_records) = max_sent_records {
175+ if max_sent_records > self . max_sent_records {
176+ return Err ( ProtocolConfigError :: max_record_count ( format ! (
177+ "max_sent_records {} is greater than the configured limit {}" ,
178+ max_sent_records, self . max_sent_records,
179+ ) ) ) ;
180+ }
181+ }
182+
183+ if let Some ( max_recv_records) = max_recv_records {
184+ if max_recv_records > self . max_recv_records {
185+ return Err ( ProtocolConfigError :: max_record_count ( format ! (
186+ "max_recv_records {} is greater than the configured limit {}" ,
187+ max_recv_records, self . max_recv_records,
188+ ) ) ) ;
189+ }
190+ }
191+
192+ Ok ( ( ) )
193+ }
194+
128195 // Checks if both versions are the same (might support check for different but
129196 // compatible versions in the future).
130197 fn check_version ( & self , peer_version : & Version ) -> Result < ( ) , ProtocolConfigError > {
@@ -165,6 +232,13 @@ impl ProtocolConfigError {
165232 }
166233 }
167234
235+ fn max_record_count ( msg : impl Into < String > ) -> Self {
236+ Self {
237+ kind : ErrorKind :: MaxRecordCount ,
238+ source : Some ( msg. into ( ) . into ( ) ) ,
239+ }
240+ }
241+
168242 fn version ( msg : impl Into < String > ) -> Self {
169243 Self {
170244 kind : ErrorKind :: Version ,
@@ -176,7 +250,8 @@ impl ProtocolConfigError {
176250impl fmt:: Display for ProtocolConfigError {
177251 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
178252 match self . kind {
179- ErrorKind :: MaxTranscriptSize => write ! ( f, "max transcript size error" ) ?,
253+ ErrorKind :: MaxTranscriptSize => write ! ( f, "max transcript size exceeded" ) ?,
254+ ErrorKind :: MaxRecordCount => write ! ( f, "max record count exceeded" ) ?,
180255 ErrorKind :: Version => write ! ( f, "version error" ) ?,
181256 }
182257
@@ -191,6 +266,7 @@ impl fmt::Display for ProtocolConfigError {
191266#[ derive( Debug ) ]
192267enum ErrorKind {
193268 MaxTranscriptSize ,
269+ MaxRecordCount ,
194270 Version ,
195271}
196272
0 commit comments