Skip to content
Merged
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
32 changes: 31 additions & 1 deletion der/src/asn1/real.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,29 @@ impl<'a> DecodeValue<'a> for f64 {
return Err(reader.error(Tag::Real.value_error()));
}
};
// Real related error: exponent outside the range of an IEEE-754 double
let exponent_value =
i16::from_be_bytes([exponent.to_be_bytes()[6], exponent.to_be_bytes()[7]]);
if !(-1023..=1023).contains(&exponent_value) {
return Err(reader.error(Tag::Real.value_error()));
}
// Section 8.5.7.5: Read the remaining bytes for the mantissa
let mantissa_bytes = &bytes[mantissa_start..];
if mantissa_bytes.len() > 8 {
// Real related error: mantissa does not fit in a u64
return Err(reader.error(Tag::Real.value_error()));
}
let mut n_bytes = [0x0; 8];
for (pos, byte) in bytes[mantissa_start..].iter().rev().enumerate() {
for (pos, byte) in mantissa_bytes.iter().rev().enumerate() {
n_bytes[7 - pos] = *byte;
}
let n = u64::from_be_bytes(n_bytes);
// Multiply byte 2^F corresponds to just a left shift
let mantissa = n << scaling_factor;
if mantissa == 0 {
// Section 8.5.2: zero is encoded with no contents octets
return Err(reader.error(Tag::Real.value_error()));
}
// Create the f64
Ok(encode_f64(sign, exponent, mantissa))
} else if is_nth_bit_one::<6>(bytes) {
Expand Down Expand Up @@ -898,6 +913,21 @@ mod tests {
}
}

#[test]
fn decode_malformed_binary_encoding_is_an_error() {
// A mantissa longer than eight octets does not fit in a u64.
assert!(f64::from_der(&[0x09, 0x0b, 0x80, 0x00, 1, 1, 1, 1, 1, 1, 1, 1, 1]).is_err());
// A zero mantissa, with no octets or a zero octet: zero has no contents octets.
assert!(f64::from_der(&[0x09, 0x02, 0x80, 0x00]).is_err());
assert!(f64::from_der(&[0x09, 0x03, 0x80, 0x00, 0x00]).is_err());
// A mantissa that the scaling factor (F = 1) shifts to zero.
assert!(f64::from_der(&[0x09, 0x0a, 0x84, 0x00, 0x80, 0, 0, 0, 0, 0, 0, 0]).is_err());
// Exponents outside the range of an IEEE-754 double.
assert!(f64::from_der(&[0x09, 0x04, 0x81, 0x7f, 0xff, 0x01]).is_err());
assert!(f64::from_der(&[0x09, 0x04, 0x81, 0x04, 0x00, 0x01]).is_err());
assert!(f64::from_der(&[0x09, 0x04, 0x81, 0x80, 0x00, 0x01]).is_err());
}

#[test]
fn decode_truncated_binary_encoding_is_an_error() {
// GHSA-27m6-7r55-vwr2: the binary-encoding branch reads the exponent
Expand Down
Loading