]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: add SecBlobError custom error type for the Kerberos parser
authorPierre Chifflier <chifflier@wzdftpd.net>
Wed, 30 Oct 2019 16:17:52 +0000 (17:17 +0100)
committerVictor Julien <victor@inliniac.net>
Mon, 2 Mar 2020 16:16:42 +0000 (17:16 +0100)
rust/src/kerberos.rs
rust/src/nfs/nfs4.rs
rust/src/smb/auth.rs

index 8d5d13c5013ed64cac925b5b3ef948fd729d9485..ff1370e1f2b38e1f8d75e18427d737ee90a106eb 100644 (file)
@@ -19,15 +19,36 @@ use kerberos_parser::krb5_parser::parse_ap_req;
 use kerberos_parser::krb5::{ApReq,Realm,PrincipalName};
 use nom;
 use nom::IResult;
-use nom::error::ErrorKind;
+use nom::error::{ErrorKind, ParseError};
 use nom::number::complete::le_u16;
 use der_parser;
+use der_parser::error::BerError;
 use der_parser::der::parse_der_oid;
 
 use crate::log::*;
 
-pub const SECBLOB_NOT_SPNEGO :  u32 = 128;
-pub const SECBLOB_KRB_FMT_ERR : u32 = 129;
+#[derive(Debug)]
+pub enum SecBlobError {
+    NotSpNego,
+    KrbFmtError,
+    Ber(BerError),
+    NomError(ErrorKind),
+}
+
+impl From<BerError> for SecBlobError {
+    fn from(error: BerError) -> Self {
+        SecBlobError::Ber(error)
+    }
+}
+
+impl<I> ParseError<I> for SecBlobError {
+    fn from_error_kind(_input: I, kind: ErrorKind) -> Self {
+        SecBlobError::NomError(kind)
+    }
+    fn append(_input: I, kind: ErrorKind, _other: Self) -> Self {
+        SecBlobError::NomError(kind)
+    }
+}
 
 #[derive(Debug,PartialEq)]
 pub struct Kerberos5Ticket {
@@ -35,11 +56,11 @@ pub struct Kerberos5Ticket {
     pub sname: PrincipalName,
 }
 
-fn parse_kerberos5_request_do(blob: &[u8]) -> IResult<&[u8], ApReq>
+fn parse_kerberos5_request_do(blob: &[u8]) -> IResult<&[u8], ApReq, SecBlobError>
 {
-    let (_,b) = der_parser::parse_der(blob)?;
+    let (_,b) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?;
     let blob = b.as_slice().or(
-        Err(nom::Err::Error(error_position!(blob, ErrorKind::Custom(SECBLOB_KRB_FMT_ERR))))
+        Err(nom::Err::Error(SecBlobError::KrbFmtError))
     )?;
     do_parse!(
         blob,
@@ -52,9 +73,10 @@ fn parse_kerberos5_request_do(blob: &[u8]) -> IResult<&[u8], ApReq>
             ap_req
         })
     )
+    .map_err(|e| nom::Err::convert(e))
 }
 
-pub fn parse_kerberos5_request(blob: &[u8]) -> IResult<&[u8], Kerberos5Ticket>
+pub fn parse_kerberos5_request(blob: &[u8]) -> IResult<&[u8], Kerberos5Ticket, SecBlobError>
 {
     let (rem, req) = parse_kerberos5_request_do(blob)?;
     let t = Kerberos5Ticket {
index 7aa1d500d747bc8f04d04b03e0ef98eeec6fc301..cfc9b4697e6551ebbc30a02f30bf2a2fbc4f98bd 100644 (file)
@@ -29,12 +29,12 @@ use crate::nfs::rpc_records::*;
 use crate::nfs::nfs_records::*;
 use crate::nfs::nfs4_records::*;
 
-use crate::kerberos;
+use crate::kerberos::{parse_kerberos5_request, Kerberos5Ticket, SecBlobError};
 
-named!(parse_req_gssapi<kerberos::Kerberos5Ticket>,
+named!(parse_req_gssapi<&[u8], Kerberos5Ticket, SecBlobError>,
    do_parse!(
         len: be_u32
-    >>  ap: flat_map!(take!(len), call!(kerberos::parse_kerberos5_request))
+    >>  ap: flat_map!(take!(len), parse_kerberos5_request)
     >> ( ap )
 ));
 
index 69729ec3de1f0498a1bff58842a435ac700c6741..5b245d9332205e043c26a3aad0c5328a9d5c6e7d 100644 (file)
@@ -23,25 +23,24 @@ use crate::smb::smb::*;
 
 use nom;
 use nom::IResult;
-use nom::error::ErrorKind;
 use der_parser::ber::BerObjectContent;
 use der_parser::der::{parse_der_oid, parse_der_sequence};
 
-fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8]>
+fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8], SecBlobError>
 {
-    let (rem, base_o) = der_parser::parse_der(blob)?;
+    let (rem, base_o) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?;
     SCLogDebug!("parse_secblob_get_spnego: base_o {:?}", base_o);
     let d = match base_o.content.as_slice() {
-        Err(_) => { return Err(nom::Err::Error(error_position!(blob,ErrorKind::Custom(SECBLOB_NOT_SPNEGO)))); },
+        Err(_) => { return Err(nom::Err::Error(SecBlobError::NotSpNego)); },
         Ok(d) => d,
     };
-    let (next, o) = parse_der_oid(d)?;
+    let (next, o) = parse_der_oid(d).map_err(|e| nom::Err::convert(e))?;
     SCLogDebug!("parse_secblob_get_spnego: sub_o {:?}", o);
 
     let oid = match o.content.as_oid() {
         Ok(oid) => oid,
         Err(_) => {
-            return Err(nom::Err::Error(error_position!(blob,ErrorKind::Custom(SECBLOB_NOT_SPNEGO))));
+            return Err(nom::Err::Error(SecBlobError::NotSpNego));
         },
     };
     SCLogDebug!("oid {}", oid.to_string());
@@ -51,7 +50,7 @@ fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8]>
             SCLogDebug!("SPNEGO {}", oid);
         },
         _ => {
-            return Err(nom::Err::Error(error_position!(blob,ErrorKind::Custom(SECBLOB_NOT_SPNEGO))));
+            return Err(nom::Err::Error(SecBlobError::NotSpNego));
         },
     }
 
@@ -60,16 +59,16 @@ fn parse_secblob_get_spnego(blob: &[u8]) -> IResult<&[u8], &[u8]>
     Ok((rem, next))
 }
 
-fn parse_secblob_spnego_start(blob: &[u8]) -> IResult<&[u8], &[u8]>
+fn parse_secblob_spnego_start(blob: &[u8]) -> IResult<&[u8], &[u8], SecBlobError>
 {
-    let (rem, o) = der_parser::parse_der(blob)?;
+    let (rem, o) = der_parser::parse_der(blob).map_err(|e| nom::Err::convert(e))?;
     let d = match o.content.as_slice() {
         Ok(d) => {
             SCLogDebug!("d: next data len {}",d.len());
             d
         },
         _ => {
-            return Err(nom::Err::Error(error_position!(blob,ErrorKind::Custom(SECBLOB_NOT_SPNEGO))));
+            return Err(nom::Err::Error(SecBlobError::NotSpNego));
         },
     };
     Ok((rem, d))