@@ -804,8 +804,8 @@ impl PacketVersion<Self> for Ospfv2 {
804
804
fn decode_auth_validate (
805
805
data : & [ u8 ] ,
806
806
pkt_len : u16 ,
807
- hdr_auth : PacketHdrAuth ,
808
- auth : Option < AuthDecodeCtx < ' _ > > ,
807
+ hdr_auth : & PacketHdrAuth ,
808
+ auth : Option < & AuthDecodeCtx < ' _ > > ,
809
809
) -> DecodeResult < Option < u64 > > {
810
810
// Discard the packet if its authentication type doesn't match the
811
811
// interface's configured authentication type.
@@ -824,49 +824,17 @@ impl PacketVersion<Self> for Ospfv2 {
824
824
auth_len,
825
825
seqno,
826
826
} => {
827
- // Get authentication key.
828
- let auth = auth. as_ref ( ) . unwrap ( ) ;
829
- let auth_key = match auth. method {
830
- AuthMethod :: ManualKey ( key) => {
831
- // Check if the Key ID matches.
832
- if key. id != key_id as u64 {
833
- return Err ( DecodeError :: AuthKeyIdNotFound (
834
- key_id as u32 ,
835
- ) ) ;
836
- }
837
- key
838
- }
839
- AuthMethod :: Keychain ( keychain) => keychain
840
- . key_lookup_accept ( key_id as u64 )
841
- . ok_or ( DecodeError :: AuthKeyIdNotFound ( key_id as u32 ) ) ?,
842
- } ;
843
-
844
- // Sanity check.
845
- if auth_key. algo . digest_size ( ) != auth_len {
846
- return Err ( DecodeError :: AuthLenError ( auth_len as u16 ) ) ;
847
- }
848
-
849
827
// Get the authentication trailer.
850
828
let auth_trailer = & data
851
- [ pkt_len as usize ..pkt_len as usize + auth_len as usize ] ;
829
+ [ pkt_len as usize ..pkt_len as usize + * auth_len as usize ] ;
852
830
853
831
// Compute message digest.
854
832
let data = & data[ ..pkt_len as usize ] ;
855
- let digest = auth:: message_digest (
856
- data,
857
- auth_key. algo ,
858
- & auth_key. string ,
859
- None ,
860
- None ,
861
- ) ;
862
833
863
- // Check if the received message digest is valid.
864
- if * auth_trailer != digest {
865
- return Err ( DecodeError :: AuthError ) ;
866
- }
834
+ validate_digest ( * key_id, * auth_len, auth, auth_trailer, data) ?;
867
835
868
836
// Authentication succeeded.
869
- Ok ( Some ( seqno. into ( ) ) )
837
+ Ok ( Some ( ( * seqno) . into ( ) ) )
870
838
}
871
839
}
872
840
}
@@ -881,4 +849,62 @@ impl PacketVersion<Self> for Ospfv2 {
881
849
) ;
882
850
buf. put_slice ( & digest) ;
883
851
}
852
+
853
+ fn packet_options ( data : & [ u8 ] ) -> Option < Options > {
854
+ let pkt_type = PacketType :: from_u8 ( data[ 1 ] ) . unwrap ( ) ;
855
+ match pkt_type {
856
+ PacketType :: Hello => {
857
+ let options = & data[ PacketHdr :: LENGTH as usize + 6 ..] ;
858
+ Some ( Options :: from_bits_truncate ( options[ 0 ] ) )
859
+ }
860
+ PacketType :: DbDesc => {
861
+ let options = & data[ PacketHdr :: LENGTH as usize + 2 ..] ;
862
+ Some ( Options :: from_bits_truncate ( options[ 0 ] ) )
863
+ }
864
+ PacketType :: LsRequest
865
+ | PacketType :: LsUpdate
866
+ | PacketType :: LsAck => None ,
867
+ }
868
+ }
869
+ }
870
+
871
+ // ===== helper functions =====
872
+
873
+ pub ( crate ) fn validate_digest (
874
+ key_id : u8 ,
875
+ auth_len : u8 ,
876
+ auth : Option < & AuthDecodeCtx < ' _ > > ,
877
+ digest_rx : & [ u8 ] ,
878
+ data : & [ u8 ] ,
879
+ ) -> DecodeResult < ( ) > {
880
+ // Get authentication key.
881
+ let auth = auth. as_ref ( ) . unwrap ( ) ;
882
+ let auth_key = match auth. method {
883
+ AuthMethod :: ManualKey ( key) => {
884
+ // Check if the Key ID matches.
885
+ if key. id != key_id as u64 {
886
+ return Err ( DecodeError :: AuthKeyIdNotFound ( key_id as u32 ) ) ;
887
+ }
888
+ key
889
+ }
890
+ AuthMethod :: Keychain ( keychain) => keychain
891
+ . key_lookup_accept ( key_id as u64 )
892
+ . ok_or ( DecodeError :: AuthKeyIdNotFound ( key_id as u32 ) ) ?,
893
+ } ;
894
+
895
+ // Sanity check.
896
+ if auth_key. algo . digest_size ( ) != auth_len {
897
+ return Err ( DecodeError :: AuthLenError ( auth_len as u16 ) ) ;
898
+ }
899
+
900
+ // Compute message digest.
901
+ let digest =
902
+ auth:: message_digest ( data, auth_key. algo , & auth_key. string , None , None ) ;
903
+
904
+ // Check if the received message digest is valid.
905
+ if * digest_rx != digest {
906
+ return Err ( DecodeError :: AuthError ) ;
907
+ }
908
+
909
+ Ok ( ( ) )
884
910
}
0 commit comments