ike: rustfmt

pull/7713/head
Philippe Antoine 3 years ago committed by Victor Julien
parent 2b83cc799d
commit d0171d7418

@ -144,9 +144,7 @@ pub extern "C" fn rs_ike_state_get_sa_attribute(
let mut ret_code = 0; let mut ret_code = 0;
let sa_type_s: Result<_, _>; let sa_type_s: Result<_, _>;
unsafe { unsafe { sa_type_s = CStr::from_ptr(sa_type).to_str() }
sa_type_s = CStr::from_ptr(sa_type).to_str()
}
SCLogInfo!("{:#?}", sa_type_s); SCLogInfo!("{:#?}", sa_type_s);
if let Ok(sa) = sa_type_s { if let Ok(sa) = sa_type_s {
@ -159,7 +157,7 @@ pub extern "C" fn rs_ike_state_get_sa_attribute(
if let Some(numeric_value) = attr.numeric_value { if let Some(numeric_value) = attr.numeric_value {
ret_val = numeric_value; ret_val = numeric_value;
ret_code = 1; ret_code = 1;
break break;
} }
} }
} }

@ -320,8 +320,7 @@ pub unsafe extern "C" fn rs_ike_state_tx_free(state: *mut std::os::raw::c_void,
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rs_ike_parse_request( pub unsafe extern "C" fn rs_ike_parse_request(
_flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice, stream_slice: StreamSlice, _data: *const std::os::raw::c_void,
_data: *const std::os::raw::c_void,
) -> AppLayerResult { ) -> AppLayerResult {
let state = cast_pointer!(state, IKEState); let state = cast_pointer!(state, IKEState);
return state.handle_input(stream_slice.as_slice(), Direction::ToServer); return state.handle_input(stream_slice.as_slice(), Direction::ToServer);
@ -330,8 +329,7 @@ pub unsafe extern "C" fn rs_ike_parse_request(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn rs_ike_parse_response( pub unsafe extern "C" fn rs_ike_parse_response(
_flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void, _flow: *const Flow, state: *mut std::os::raw::c_void, _pstate: *mut std::os::raw::c_void,
stream_slice: StreamSlice, stream_slice: StreamSlice, _data: *const std::os::raw::c_void,
_data: *const std::os::raw::c_void,
) -> AppLayerResult { ) -> AppLayerResult {
let state = cast_pointer!(state, IKEState); let state = cast_pointer!(state, IKEState);
return state.handle_input(stream_slice.as_slice(), Direction::ToClient); return state.handle_input(stream_slice.as_slice(), Direction::ToClient);

@ -19,9 +19,9 @@ use super::ike::{IKEState, IKETransaction};
use super::ipsec_parser::IKEV2_FLAG_INITIATOR; use super::ipsec_parser::IKEV2_FLAG_INITIATOR;
use crate::ike::parser::{ExchangeType, IsakmpPayloadType, SaAttribute}; use crate::ike::parser::{ExchangeType, IsakmpPayloadType, SaAttribute};
use crate::jsonbuilder::{JsonBuilder, JsonError}; use crate::jsonbuilder::{JsonBuilder, JsonError};
use num_traits::FromPrimitive;
use std; use std;
use std::convert::TryFrom; use std::convert::TryFrom;
use num_traits::FromPrimitive;
const LOG_EXTENDED: u32 = 0x01; const LOG_EXTENDED: u32 = 0x01;

@ -261,8 +261,7 @@ pub fn parse_isakmp_header(i: &[u8]) -> IResult<&[u8], IsakmpHeader> {
let (i, flags) = be_u8(i)?; let (i, flags) = be_u8(i)?;
let (i, msg_id) = be_u32(i)?; let (i, msg_id) = be_u32(i)?;
let (i, length) = be_u32(i)?; let (i, length) = be_u32(i)?;
let hdr = let hdr = IsakmpHeader {
IsakmpHeader {
init_spi, init_spi,
resp_spi, resp_spi,
next_payload, next_payload,
@ -280,15 +279,17 @@ pub fn parse_security_association(i: &[u8]) -> IResult<&[u8], SecurityAssociatio
let start_i = i; let start_i = i;
let (i, domain_of_interpretation) = be_u32(i)?; let (i, domain_of_interpretation) = be_u32(i)?;
let (i, situation) = cond(domain_of_interpretation == 1, take(4_usize))(i)?; let (i, situation) = cond(domain_of_interpretation == 1, take(4_usize))(i)?;
let (i, data) = cond( let (i, data) = cond(domain_of_interpretation == 1 && start_i.len() >= 8, |b| {
domain_of_interpretation == 1 && start_i.len() >= 8, take(start_i.len() - 8)(b)
|b| take(start_i.len() - 8)(b) })(i)?;
)(i)?; Ok((
Ok((i, SecurityAssociationPayload { i,
SecurityAssociationPayload {
domain_of_interpretation, domain_of_interpretation,
situation, situation,
data data,
})) },
))
} }
pub fn parse_key_exchange(i: &[u8], length: u16) -> IResult<&[u8], KeyExchangePayload> { pub fn parse_key_exchange(i: &[u8], length: u16) -> IResult<&[u8], KeyExchangePayload> {
@ -303,10 +304,9 @@ pub fn parse_proposal(i: &[u8]) -> IResult<&[u8], ProposalPayload> {
let (i, spi_size) = be_u8(i)?; let (i, spi_size) = be_u8(i)?;
let (i, number_transforms) = be_u8(i)?; let (i, number_transforms) = be_u8(i)?;
let (i, spi) = take(spi_size as usize)(i)?; let (i, spi) = take(spi_size as usize)(i)?;
let (i, payload_data) = cond( let (i, payload_data) = cond((start_i.len() - 4) >= spi_size.into(), |b| {
(start_i.len() - 4) >= spi_size.into(), take((start_i.len() - 4) - spi_size as usize)(b)
|b| take((start_i.len() - 4) - spi_size as usize)(b) })(i)?;
)(i)?;
let payload = ProposalPayload { let payload = ProposalPayload {
proposal_number, proposal_number,
proposal_type, proposal_type,
@ -323,11 +323,14 @@ pub fn parse_transform(i: &[u8], length: u16) -> IResult<&[u8], TransformPayload
let (i, transform_type) = be_u8(i)?; let (i, transform_type) = be_u8(i)?;
let (i, _) = be_u16(i)?; let (i, _) = be_u16(i)?;
let (i, payload_data) = cond(length >= 4, |b| take(length - 4)(b))(i)?; let (i, payload_data) = cond(length >= 4, |b| take(length - 4)(b))(i)?;
Ok((i, TransformPayload { Ok((
i,
TransformPayload {
transform_number, transform_number,
transform_type, transform_type,
sa_attributes: payload_data.unwrap_or_default(), sa_attributes: payload_data.unwrap_or_default(),
})) },
))
} }
pub fn parse_vendor_id(i: &[u8], length: u16) -> IResult<&[u8], VendorPayload> { pub fn parse_vendor_id(i: &[u8], length: u16) -> IResult<&[u8], VendorPayload> {
@ -438,13 +441,14 @@ fn get_group_description(v: u16) -> AttributeValue {
pub fn parse_sa_attribute(i: &[u8]) -> IResult<&[u8], Vec<SaAttribute>> { pub fn parse_sa_attribute(i: &[u8]) -> IResult<&[u8], Vec<SaAttribute>> {
fn parse_attribute(i: &[u8]) -> IResult<&[u8], SaAttribute> { fn parse_attribute(i: &[u8]) -> IResult<&[u8], SaAttribute> {
let (i, b) = be_u16(i)?; let (i, b) = be_u16(i)?;
let format = ( let format = ((b >> 15) as u8, b & 0x7f_ff);
(b >> 15) as u8,
b & 0x7f_ff
);
let (i, attribute_length_or_value) = be_u16(i)?; // depends on format bit) = 1 -> value | 0 -> number of following bytes let (i, attribute_length_or_value) = be_u16(i)?; // depends on format bit) = 1 -> value | 0 -> number of following bytes
let (i, numeric_variable_value) = cond(format.0 == 0 && attribute_length_or_value == 4, be_u32) (i)?; // interpret as number let (i, numeric_variable_value) =
let (i, variable_attribute_value) = cond(format.0 == 0 && attribute_length_or_value != 4, take(attribute_length_or_value))(i)?; cond(format.0 == 0 && attribute_length_or_value == 4, be_u32)(i)?; // interpret as number
let (i, variable_attribute_value) = cond(
format.0 == 0 && attribute_length_or_value != 4,
take(attribute_length_or_value),
)(i)?;
let attr = SaAttribute { let attr = SaAttribute {
attribute_format: format.0, attribute_format: format.0,
attribute_type: get_attribute_type(format.1), attribute_type: get_attribute_type(format.1),
@ -456,23 +460,20 @@ pub fn parse_sa_attribute(i: &[u8]) -> IResult<&[u8], Vec<SaAttribute>> {
11 => match attribute_length_or_value { 11 => match attribute_length_or_value {
1 => AttributeValue::LifeTypeSeconds, 1 => AttributeValue::LifeTypeSeconds,
2 => AttributeValue::LifeTypeKilobytes, 2 => AttributeValue::LifeTypeKilobytes,
_ => AttributeValue::Unknown _ => AttributeValue::Unknown,
} },
_ => AttributeValue::Unknown _ => AttributeValue::Unknown,
}, },
numeric_value: match format.0 { numeric_value: match format.0 {
1 => Some(attribute_length_or_value as u32), 1 => Some(attribute_length_or_value as u32),
0 => { 0 => numeric_variable_value,
numeric_variable_value
},
_ => None, _ => None,
}, },
hex_value: match format.0 { hex_value: match format.0 {
0 => { 0 => variable_attribute_value
variable_attribute_value.map(|_variable_attribute_value| to_hex(_variable_attribute_value)) .map(|_variable_attribute_value| to_hex(_variable_attribute_value)),
}
_ => None, _ => None,
} },
}; };
Ok((i, attr)) Ok((i, attr))
} }
@ -489,14 +490,17 @@ pub fn parse_ikev1_payload_list(i: &[u8]) -> IResult<&[u8], Vec<IsakmpPayload>>
let (i, reserved) = be_u8(i)?; let (i, reserved) = be_u8(i)?;
let (i, payload_length) = be_u16(i)?; let (i, payload_length) = be_u16(i)?;
let (i, payload_data) = cond(payload_length >= 4, |b| take(payload_length - 4)(b))(i)?; let (i, payload_data) = cond(payload_length >= 4, |b| take(payload_length - 4)(b))(i)?;
Ok((i, IsakmpPayload { Ok((
i,
IsakmpPayload {
payload_header: IsakmpPayloadHeader { payload_header: IsakmpPayloadHeader {
next_payload, next_payload,
reserved, reserved,
payload_length payload_length,
}, },
data: payload_data.unwrap_or_default(), data: payload_data.unwrap_or_default(),
})) },
))
} }
many0(complete(parse_payload))(i) many0(complete(parse_payload))(i)
} }

Loading…
Cancel
Save