@@ -7,6 +7,10 @@ use std::error::Error;
7
7
8
8
// Default is 32 bytes to decrypt the TLS protocol messages.
9
9
const 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 ;
10
14
11
15
// Current version that is running.
12
16
static VERSION : Lazy < Version > = Lazy :: new ( || {
@@ -21,12 +25,18 @@ static VERSION: Lazy<Version> = Lazy::new(|| {
21
25
pub struct ProtocolConfig {
22
26
/// Maximum number of bytes that can be sent.
23
27
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 > ,
24
31
/// Maximum number of bytes that can be decrypted online, i.e. while the
25
32
/// MPC-TLS connection is active.
26
33
#[ builder( default = "DEFAULT_MAX_RECV_ONLINE" ) ]
27
34
max_recv_data_online : usize ,
28
35
/// Maximum number of bytes that can be received.
29
36
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 > ,
30
40
/// Version that is being run by prover/verifier.
31
41
#[ builder( setter( skip) , default = "VERSION.clone()" ) ]
32
42
version : Version ,
@@ -54,6 +64,12 @@ impl ProtocolConfig {
54
64
self . max_sent_data
55
65
}
56
66
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
+
57
73
/// Returns the maximum number of bytes that can be decrypted online.
58
74
pub fn max_recv_data_online ( & self ) -> usize {
59
75
self . max_recv_data_online
@@ -63,6 +79,12 @@ impl ProtocolConfig {
63
79
pub fn max_recv_data ( & self ) -> usize {
64
80
self . max_recv_data
65
81
}
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
+ }
66
88
}
67
89
68
90
/// Protocol configuration validator used by checker (i.e. verifier) to perform
@@ -71,8 +93,14 @@ impl ProtocolConfig {
71
93
pub struct ProtocolConfigValidator {
72
94
/// Maximum number of bytes that can be sent.
73
95
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 ,
74
99
/// Maximum number of bytes that can be received.
75
100
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 ,
76
104
/// Version that is being run by checker.
77
105
#[ builder( setter( skip) , default = "VERSION.clone()" ) ]
78
106
version : Version ,
@@ -89,15 +117,28 @@ impl ProtocolConfigValidator {
89
117
self . max_sent_data
90
118
}
91
119
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
+
92
126
/// Returns the maximum number of bytes that can be received.
93
127
pub fn max_recv_data ( & self ) -> usize {
94
128
self . max_recv_data
95
129
}
96
130
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
+
97
137
/// Performs compatibility check of the protocol configuration between
98
138
/// prover and verifier.
99
139
pub fn validate ( & self , config : & ProtocolConfig ) -> Result < ( ) , ProtocolConfigError > {
100
140
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 ) ?;
101
142
self . check_version ( & config. version ) ?;
102
143
Ok ( ( ) )
103
144
}
@@ -125,6 +166,32 @@ impl ProtocolConfigValidator {
125
166
Ok ( ( ) )
126
167
}
127
168
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
+
128
195
// Checks if both versions are the same (might support check for different but
129
196
// compatible versions in the future).
130
197
fn check_version ( & self , peer_version : & Version ) -> Result < ( ) , ProtocolConfigError > {
@@ -165,6 +232,13 @@ impl ProtocolConfigError {
165
232
}
166
233
}
167
234
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
+
168
242
fn version ( msg : impl Into < String > ) -> Self {
169
243
Self {
170
244
kind : ErrorKind :: Version ,
@@ -176,7 +250,8 @@ impl ProtocolConfigError {
176
250
impl fmt:: Display for ProtocolConfigError {
177
251
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
178
252
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" ) ?,
180
255
ErrorKind :: Version => write ! ( f, "version error" ) ?,
181
256
}
182
257
@@ -191,6 +266,7 @@ impl fmt::Display for ProtocolConfigError {
191
266
#[ derive( Debug ) ]
192
267
enum ErrorKind {
193
268
MaxTranscriptSize ,
269
+ MaxRecordCount ,
194
270
Version ,
195
271
}
196
272
0 commit comments