Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minimum MAC size this fixes a deserialization error #1159

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 0 additions & 42 deletions ntp-proto/src/packet/extension_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,48 +1253,6 @@ mod tests {
assert_eq!(slice.len(), 28 + 16);
}

#[test]
fn deserialize_without_cipher_invalid_length() {
// the message will be smaller than the cutoff value of 28
let cookie = ExtensionField::NtsCookie(Cow::Borrowed(&[0; 4]));

let data = ExtensionFieldData {
authenticated: vec![],
encrypted: vec![cookie],
untrusted: vec![],
};

let nonce_length = 11;
let cipher = crate::packet::crypto::IdentityCipher::new(nonce_length);

let mut w = [0u8; 128];
let mut cursor = Cursor::new(w.as_mut_slice());
data.serialize(&mut cursor, &cipher, ExtensionHeaderVersion::V4)
.unwrap();

let n = cursor.position() as usize;
let slice = &w.as_slice()[..n];

let cipher = crate::packet::crypto::NoCipher;

let result =
ExtensionFieldData::deserialize(slice, 0, &cipher, ExtensionHeaderVersion::V4).unwrap();

let DeserializedExtensionField {
efdata,
remaining_bytes,
cookie,
} = result;

assert_eq!(efdata.authenticated, &[]);
assert_eq!(efdata.encrypted, &[]);
assert_eq!(efdata.untrusted, &[]);

assert_eq!(remaining_bytes, slice);

assert!(cookie.is_none());
}

#[test]
fn deserialize_without_cipher() {
let cookie = ExtensionField::NtsCookie(Cow::Borrowed(&[0; 32]));
Expand Down
6 changes: 5 additions & 1 deletion ntp-proto/src/packet/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ pub(super) struct Mac<'a> {
}

impl<'a> Mac<'a> {
pub(super) const MAXIMUM_SIZE: usize = 28;
// As per RFC7822:
// If a MAC is used, it resides at the end of the packet. This field
// can be either 24 octets long, 20 octets long, or a 4-octet
// crypto-NAK.
pub(super) const MAXIMUM_SIZE: usize = 24;

pub(super) fn into_owned(self) -> Mac<'static> {
Mac {
Expand Down
22 changes: 22 additions & 0 deletions ntp-proto/src/packet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,28 @@ mod tests {
assert!(NtpPacket::deserialize(&input, &NoCipher).is_err());
}

#[test]
fn round_trip_with_ef() {
let (mut p, _) = NtpPacket::poll_message(PollInterval::default());
p.efdata.untrusted.push(ExtensionField::Unknown {
type_id: 0x42,
data: vec![].into(),
});

let serialized = p.serialize_without_encryption_vec().unwrap();

let (mut out, _) = NtpPacket::deserialize(&serialized, &NoCipher).unwrap();

// Strip any padding
let ExtensionField::Unknown { data, .. } = &mut out.efdata.untrusted[0] else {
panic!("wrong ef");
};
assert!(data.iter().all(|&e| e == 0));
*data = vec![].into();

assert_eq!(p, out);
}

#[cfg(feature = "ntpv5")]
#[test]
fn ef_with_missing_padding_v5() {
Expand Down
Loading