@@ -9,7 +9,8 @@ pub(super) type SymmetricKey = [u8; 32];
9
9
/// Maximum Lightning message data length according to
10
10
/// [BOLT-8](https://github.com/lightningnetwork/lightning-rfc/blob/v1.0/08-transport.md#lightning-message-specification)
11
11
/// and [BOLT-1](https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md#lightning-message-format):
12
- const LN_MAX_MSG_LEN : usize = :: std:: u16:: MAX as usize ; // Must be equal to 65535
12
+ const LN_MAX_MSG_LEN : usize = 65535 ;
13
+ const LN_MAX_PACKET_LENGTH : usize = MESSAGE_LENGTH_HEADER_SIZE + chacha:: TAG_SIZE + LN_MAX_MSG_LEN + chacha:: TAG_SIZE ;
13
14
14
15
const MESSAGE_LENGTH_HEADER_SIZE : usize = 2 ;
15
16
const TAGGED_MESSAGE_LENGTH_HEADER_SIZE : usize = MESSAGE_LENGTH_HEADER_SIZE + chacha:: TAG_SIZE ;
@@ -76,7 +77,7 @@ impl Iterator for Decryptor {
76
77
impl Encryptor {
77
78
pub fn encrypt ( & mut self , buffer : & [ u8 ] ) -> Vec < u8 > {
78
79
if buffer. len ( ) > LN_MAX_MSG_LEN {
79
- panic ! ( "Attempted to encrypt message longer than 65535 bytes!" ) ;
80
+ panic ! ( "Attempted to encrypt message longer than {} bytes!" , LN_MAX_MSG_LEN ) ;
80
81
}
81
82
82
83
let length = buffer. len ( ) as u16 ;
@@ -131,16 +132,19 @@ impl Decryptor {
131
132
}
132
133
}
133
134
135
+ // If we ever get to the end of the decryption phase and have more data in the read buffer
136
+ // than is possible for a valid message something has gone wrong. An error with a mismatched
137
+ // length and payload should result an error from the decryption code before we get here.
138
+ if self . read_buffer . as_ref ( ) . unwrap ( ) . len ( ) > LN_MAX_PACKET_LENGTH {
139
+ panic ! ( "Encrypted message data longer than {}" , LN_MAX_PACKET_LENGTH ) ;
140
+ }
141
+
134
142
Ok ( ( ) )
135
143
}
136
144
137
145
// Decrypt the next payload from the slice returning the number of bytes consumed during the
138
146
// operation. This will always be (None, 0) if no payload could be decrypted.
139
147
fn decrypt_next ( & mut self , buffer : & [ u8 ] ) -> Result < ( Option < Vec < u8 > > , usize ) , String > {
140
- if buffer. len ( ) > LN_MAX_MSG_LEN + 16 {
141
- panic ! ( "Attempted to decrypt message longer than 65535 + 16 bytes!" ) ;
142
- }
143
-
144
148
let message_length = if let Some ( length) = self . pending_message_length {
145
149
// we have already decrypted the header
146
150
length
@@ -360,9 +364,10 @@ mod tests {
360
364
}
361
365
362
366
#[ test]
367
+ // https://github.com/lightningnetwork/lightning-rfc/blob/v1.0/08-transport.md#lightning-message-specification
363
368
fn max_msg_len_limit_value ( ) {
364
369
assert_eq ! ( LN_MAX_MSG_LEN , 65535 ) ;
365
- assert_eq ! ( LN_MAX_MSG_LEN , :: std :: u16 :: MAX as usize ) ;
370
+ assert_eq ! ( LN_MAX_PACKET_LENGTH , 65569 ) ;
366
371
}
367
372
368
373
#[ test]
@@ -373,13 +378,28 @@ mod tests {
373
378
let _should_panic = connected_encryptor. encrypt ( & msg) ;
374
379
}
375
380
381
+ // Test that the decryptor can handle multiple partial reads() that result in a total size
382
+ // larger than LN_MAX_PACKET_LENGTH and still decrypt the messages.
376
383
#[ test]
377
- #[ should_panic( expected = "Attempted to decrypt message longer than 65535 + 16 bytes!" ) ]
378
- fn max_message_len_decryption ( ) {
379
- let ( _, ( _, mut remote_decryptor) ) = setup_peers ( ) ;
384
+ fn read_buffer_can_grow_over_max_payload_len ( ) {
385
+ let ( ( mut connected_encryptor, _) , ( _, mut remote_decryptor) ) = setup_peers ( ) ;
386
+ let msg1 = [ 1u8 ; LN_MAX_MSG_LEN ] ;
387
+ let msg2 = [ 2u8 ; LN_MAX_MSG_LEN ] ;
388
+
389
+ let encrypted1 = connected_encryptor. encrypt ( & msg1) ;
390
+ let encrypted2 = connected_encryptor. encrypt ( & msg2) ;
391
+
392
+ let read1 = & encrypted1[ ..1 ] ;
393
+ let mut read2 = vec ! [ ] ;
394
+ read2. extend_from_slice ( & encrypted1[ 1 ..] ) ;
395
+ read2. extend_from_slice ( & encrypted2) ;
396
+
397
+ remote_decryptor. read ( read1) . unwrap ( ) ;
398
+ assert_eq ! ( remote_decryptor. next( ) , None ) ;
399
+
400
+ remote_decryptor. read ( & read2[ ..] ) . unwrap ( ) ;
380
401
381
- // MSG should not exceed LN_MAX_MSG_LEN + 16
382
- let msg = [ 4u8 ; LN_MAX_MSG_LEN + 17 ] ;
383
- remote_decryptor. read ( & msg) . unwrap ( ) ;
402
+ assert_eq ! ( remote_decryptor. next( ) , Some ( msg1. to_vec( ) ) ) ;
403
+ assert_eq ! ( remote_decryptor. next( ) , Some ( msg2. to_vec( ) ) ) ;
384
404
}
385
405
}
0 commit comments