@@ -20,6 +20,8 @@ pub enum DecodeError {
20
20
UnknownRealmByte ,
21
21
/// Failed to decode a public key (ie it's invalid)
22
22
BadPublicKey ,
23
+ /// Failed to decode a signature (ie it's invalid)
24
+ BadSignature ,
23
25
/// Buffer not of right length (either too short or too long)
24
26
WrongLength ,
25
27
}
@@ -408,6 +410,7 @@ impl Error for DecodeError {
408
410
match * self {
409
411
DecodeError :: UnknownRealmByte => "Unknown realm byte in Onion packet" ,
410
412
DecodeError :: BadPublicKey => "Invalid public key in packet" ,
413
+ DecodeError :: BadSignature => "Invalid signature in packet" ,
411
414
DecodeError :: WrongLength => "Data was wrong length for packet" ,
412
415
}
413
416
}
@@ -433,11 +436,20 @@ macro_rules! secp_pubkey {
433
436
} ;
434
437
}
435
438
439
+ macro_rules! secp_signature {
440
+ ( $ctx: expr, $slice: expr ) => {
441
+ match Signature :: from_compact( $ctx, $slice) {
442
+ Ok ( sig) => sig,
443
+ Err ( _) => return Err ( DecodeError :: BadSignature )
444
+ }
445
+ } ;
446
+ }
447
+
436
448
impl MsgDecodable for LocalFeatures {
437
449
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
438
450
if v. len ( ) < 3 { return Err ( DecodeError :: WrongLength ) ; }
439
451
let len = byte_utils:: slice_to_be16 ( & v[ 0 ..2 ] ) as usize ;
440
- if v. len ( ) != len + 2 { return Err ( DecodeError :: WrongLength ) ; }
452
+ if v. len ( ) < len + 2 { return Err ( DecodeError :: WrongLength ) ; }
441
453
let mut flags = Vec :: with_capacity ( len) ;
442
454
flags. extend_from_slice ( & v[ 2 ..] ) ;
443
455
Ok ( Self {
@@ -458,7 +470,7 @@ impl MsgDecodable for GlobalFeatures {
458
470
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
459
471
if v. len ( ) < 3 { return Err ( DecodeError :: WrongLength ) ; }
460
472
let len = byte_utils:: slice_to_be16 ( & v[ 0 ..2 ] ) as usize ;
461
- if v. len ( ) != len + 2 { return Err ( DecodeError :: WrongLength ) ; }
473
+ if v. len ( ) < len + 2 { return Err ( DecodeError :: WrongLength ) ; }
462
474
let mut flags = Vec :: with_capacity ( len) ;
463
475
flags. extend_from_slice ( & v[ 2 ..] ) ;
464
476
Ok ( Self {
@@ -478,13 +490,10 @@ impl MsgEncodable for GlobalFeatures {
478
490
impl MsgDecodable for Init {
479
491
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
480
492
let global_features = GlobalFeatures :: decode ( v) ?;
481
- if global_features . flags . len ( ) + 4 <= v . len ( ) {
493
+ if v . len ( ) < global_features . flags . len ( ) + 4 {
482
494
return Err ( DecodeError :: WrongLength ) ;
483
495
}
484
496
let local_features = LocalFeatures :: decode ( & v[ global_features. flags . len ( ) + 2 ..] ) ?;
485
- if global_features. flags . len ( ) + local_features. flags . len ( ) + 4 != v. len ( ) {
486
- return Err ( DecodeError :: WrongLength ) ;
487
- }
488
497
Ok ( Self {
489
498
global_features : global_features,
490
499
local_features : local_features,
@@ -502,24 +511,20 @@ impl MsgEncodable for Init {
502
511
503
512
impl MsgDecodable for OpenChannel {
504
513
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
505
- if v. len ( ) != 2 * 32 +6 * 8 +4 +2 * 2 +6 * 33 +1 {
514
+ if v. len ( ) < 2 * 32 +6 * 8 +4 +2 * 2 +6 * 33 +1 {
506
515
return Err ( DecodeError :: WrongLength ) ;
507
516
}
508
517
let ctx = Secp256k1 :: without_caps ( ) ;
509
- let funding_pubkey = secp_pubkey ! ( & ctx, & v[ 120 ..153 ] ) ;
510
- let revocation_basepoint = secp_pubkey ! ( & ctx, & v[ 153 ..186 ] ) ;
511
- let payment_basepoint = secp_pubkey ! ( & ctx, & v[ 186 ..219 ] ) ;
512
- let delayed_payment_basepoint = secp_pubkey ! ( & ctx, & v[ 219 ..252 ] ) ;
513
- let htlc_basepoint = secp_pubkey ! ( & ctx, & v[ 252 ..285 ] ) ;
514
- let first_per_commitment_point = secp_pubkey ! ( & ctx, & v[ 285 ..318 ] ) ;
515
518
516
519
let mut shutdown_scriptpubkey = None ;
517
520
if v. len ( ) >= 321 {
518
521
let len = byte_utils:: slice_to_be16 ( & v[ 319 ..321 ] ) as usize ;
519
- if v. len ( ) != 321 +len {
522
+ if v. len ( ) < 321 +len {
520
523
return Err ( DecodeError :: WrongLength ) ;
521
524
}
522
525
shutdown_scriptpubkey = Some ( Script :: from ( v[ 321 ..321 +len] . to_vec ( ) ) ) ;
526
+ } else if v. len ( ) != 2 * 32 +6 * 8 +4 +2 * 2 +6 * 33 +1 { // Message cant have 1 extra byte
527
+ return Err ( DecodeError :: WrongLength ) ;
523
528
}
524
529
525
530
Ok ( OpenChannel {
@@ -534,12 +539,12 @@ impl MsgDecodable for OpenChannel {
534
539
feerate_per_kw : byte_utils:: slice_to_be32 ( & v[ 112 ..116 ] ) ,
535
540
to_self_delay : byte_utils:: slice_to_be16 ( & v[ 116 ..118 ] ) ,
536
541
max_accepted_htlcs : byte_utils:: slice_to_be16 ( & v[ 118 ..120 ] ) ,
537
- funding_pubkey : funding_pubkey ,
538
- revocation_basepoint : revocation_basepoint ,
539
- payment_basepoint : payment_basepoint ,
540
- delayed_payment_basepoint : delayed_payment_basepoint ,
541
- htlc_basepoint : htlc_basepoint ,
542
- first_per_commitment_point : first_per_commitment_point ,
542
+ funding_pubkey : secp_pubkey ! ( & ctx , & v [ 120 .. 153 ] ) ,
543
+ revocation_basepoint : secp_pubkey ! ( & ctx , & v [ 153 .. 186 ] ) ,
544
+ payment_basepoint : secp_pubkey ! ( & ctx , & v [ 186 .. 219 ] ) ,
545
+ delayed_payment_basepoint : secp_pubkey ! ( & ctx , & v [ 219 .. 252 ] ) ,
546
+ htlc_basepoint : secp_pubkey ! ( & ctx , & v [ 252 .. 285 ] ) ,
547
+ first_per_commitment_point : secp_pubkey ! ( & ctx , & v [ 285 .. 318 ] ) ,
543
548
channel_flags : v[ 318 ] ,
544
549
shutdown_scriptpubkey : shutdown_scriptpubkey
545
550
} )
@@ -551,10 +556,41 @@ impl MsgEncodable for OpenChannel {
551
556
}
552
557
}
553
558
554
-
555
559
impl MsgDecodable for AcceptChannel {
556
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
557
- unimplemented ! ( ) ;
560
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
561
+ if v. len ( ) < 32 +4 * 8 +4 +2 * 2 +6 * 33 {
562
+ return Err ( DecodeError :: WrongLength ) ;
563
+ }
564
+ let ctx = Secp256k1 :: without_caps ( ) ;
565
+
566
+ let mut shutdown_scriptpubkey = None ;
567
+ if v. len ( ) >= 272 {
568
+ let len = byte_utils:: slice_to_be16 ( & v[ 270 ..272 ] ) as usize ;
569
+ if v. len ( ) < 272 +len {
570
+ return Err ( DecodeError :: WrongLength ) ;
571
+ }
572
+ shutdown_scriptpubkey = Some ( Script :: from ( v[ 272 ..272 +len] . to_vec ( ) ) ) ;
573
+ } else if v. len ( ) != 32 +4 * 8 +4 +2 * 2 +6 * 33 { // Message cant have 1 extra byte
574
+ return Err ( DecodeError :: WrongLength ) ;
575
+ }
576
+
577
+ Ok ( Self {
578
+ temporary_channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
579
+ dust_limit_satoshis : byte_utils:: slice_to_be64 ( & v[ 32 ..40 ] ) ,
580
+ max_htlc_value_in_flight_msat : byte_utils:: slice_to_be64 ( & v[ 40 ..48 ] ) ,
581
+ channel_reserve_satoshis : byte_utils:: slice_to_be64 ( & v[ 48 ..56 ] ) ,
582
+ htlc_minimum_msat : byte_utils:: slice_to_be64 ( & v[ 56 ..64 ] ) ,
583
+ minimum_depth : byte_utils:: slice_to_be32 ( & v[ 64 ..68 ] ) ,
584
+ to_self_delay : byte_utils:: slice_to_be16 ( & v[ 68 ..70 ] ) ,
585
+ max_accepted_htlcs : byte_utils:: slice_to_be16 ( & v[ 70 ..72 ] ) ,
586
+ funding_pubkey : secp_pubkey ! ( & ctx, & v[ 72 ..105 ] ) ,
587
+ revocation_basepoint : secp_pubkey ! ( & ctx, & v[ 105 ..138 ] ) ,
588
+ payment_basepoint : secp_pubkey ! ( & ctx, & v[ 138 ..171 ] ) ,
589
+ delayed_payment_basepoint : secp_pubkey ! ( & ctx, & v[ 171 ..204 ] ) ,
590
+ htlc_basepoint : secp_pubkey ! ( & ctx, & v[ 204 ..237 ] ) ,
591
+ first_per_commitment_point : secp_pubkey ! ( & ctx, & v[ 237 ..270 ] ) ,
592
+ shutdown_scriptpubkey : shutdown_scriptpubkey
593
+ } )
558
594
}
559
595
}
560
596
impl MsgEncodable for AcceptChannel {
@@ -564,8 +600,17 @@ impl MsgEncodable for AcceptChannel {
564
600
}
565
601
566
602
impl MsgDecodable for FundingCreated {
567
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
568
- unimplemented ! ( ) ;
603
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
604
+ if v. len ( ) < 32 +32 +2 +64 {
605
+ return Err ( DecodeError :: WrongLength ) ;
606
+ }
607
+ let ctx = Secp256k1 :: without_caps ( ) ;
608
+ Ok ( Self {
609
+ temporary_channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
610
+ funding_txid : deserialize ( & v[ 32 ..64 ] ) . unwrap ( ) ,
611
+ funding_output_index : byte_utils:: slice_to_be16 ( & v[ 64 ..66 ] ) ,
612
+ signature : secp_signature ! ( & ctx, & v[ 66 ..130 ] ) ,
613
+ } )
569
614
}
570
615
}
571
616
impl MsgEncodable for FundingCreated {
@@ -575,8 +620,15 @@ impl MsgEncodable for FundingCreated {
575
620
}
576
621
577
622
impl MsgDecodable for FundingSigned {
578
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
579
- unimplemented ! ( ) ;
623
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
624
+ if v. len ( ) < 32 +64 {
625
+ return Err ( DecodeError :: WrongLength ) ;
626
+ }
627
+ let ctx = Secp256k1 :: without_caps ( ) ;
628
+ Ok ( Self {
629
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
630
+ signature : secp_signature ! ( & ctx, & v[ 32 ..96 ] ) ,
631
+ } )
580
632
}
581
633
}
582
634
impl MsgEncodable for FundingSigned {
@@ -586,8 +638,15 @@ impl MsgEncodable for FundingSigned {
586
638
}
587
639
588
640
impl MsgDecodable for FundingLocked {
589
- fn decode ( _v : & [ u8 ] ) -> Result < Self , DecodeError > {
590
- unimplemented ! ( ) ;
641
+ fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
642
+ if v. len ( ) < 32 +33 {
643
+ return Err ( DecodeError :: WrongLength ) ;
644
+ }
645
+ let ctx = Secp256k1 :: without_caps ( ) ;
646
+ Ok ( Self {
647
+ channel_id : deserialize ( & v[ 0 ..32 ] ) . unwrap ( ) ,
648
+ next_per_commitment_point : secp_pubkey ! ( & ctx, & v[ 32 ..65 ] ) ,
649
+ } )
591
650
}
592
651
}
593
652
impl MsgEncodable for FundingLocked {
@@ -839,7 +898,7 @@ impl MsgEncodable for ChannelUpdate {
839
898
840
899
impl MsgDecodable for OnionRealm0HopData {
841
900
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
842
- if v. len ( ) != 32 {
901
+ if v. len ( ) < 32 {
843
902
return Err ( DecodeError :: WrongLength ) ;
844
903
}
845
904
Ok ( OnionRealm0HopData {
@@ -862,7 +921,7 @@ impl MsgEncodable for OnionRealm0HopData {
862
921
863
922
impl MsgDecodable for OnionHopData {
864
923
fn decode ( v : & [ u8 ] ) -> Result < Self , DecodeError > {
865
- if v. len ( ) != 65 {
924
+ if v. len ( ) < 65 {
866
925
return Err ( DecodeError :: WrongLength ) ;
867
926
}
868
927
let realm = v[ 0 ] ;
0 commit comments