Skip to content
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
18 changes: 18 additions & 0 deletions src/crl/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,4 +1268,22 @@ mod tests {
include_bytes!("../../tests/client_auth_revocation/ee_revoked_crl_ku_ee_depth.crl.der");
assert!(OwnedCertRevocationList::from_der(crl).is_ok())
}

#[test]
fn test_crl_issuing_distribution_point_illegal_bit_string() {
let crl = &[
0x30, 0x65, 0x30, 0x50, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0c, 0x31, 0x0a, 0x30, 0x08,
0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x01, 0x41, 0x17, 0x0d, 0x32, 0x30, 0x30, 0x31,
0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x32, 0x31, 0x30,
0x31, 0x30, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a, 0xa0, 0x10, 0x30, 0x0e,
0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x1c, 0x04, 0x05, 0x30, 0x03, 0x83, 0x01, 0x00,
0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
0x00, 0x03, 0x02, 0x00, 0x00,
];
assert_eq!(
BorrowedCertRevocationList::from_der(crl).err(),
Some(Error::UnsupportedRevocationReasonsPartitioning)
);
}
}
49 changes: 37 additions & 12 deletions src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,20 +378,19 @@ pub(crate) fn bit_string_flags(input: untrusted::Input<'_>) -> Result<BitStringF
let padding_bits = bit_string.read_byte().map_err(|_| Error::BadDer)?;
let raw_bits = bit_string.read_bytes_to_end().as_slice_less_safe();

// It's illegal to have more than 7 bits of padding. Similarly, if the raw bitflags
// are empty there should be no padding.
if padding_bits > 7 || (raw_bits.is_empty() && padding_bits != 0) {
return Err(Error::BadDer);
}
match (padding_bits, raw_bits.last()) {
// It's illegal to have more than 7 bits of padding.
(8.., _) => Err(Error::BadDer),

// If the raw bitflags are empty there should be no padding.
(0, None) => Ok(BitStringFlags { raw_bits }),
(_, None) => Err(Error::BadDer),

// If there are padding bits then the last bit of the last raw byte must be 0 or the
// distinguished encoding rules are not being followed.
let last_byte = raw_bits[raw_bits.len() - 1];
let padding_mask = (1 << padding_bits) - 1;
// If there are padding bits then the last bit of the last raw byte must be 0 or the
// distinguished encoding rules are not being followed.
(1..=7, Some(last)) if last & ((1 << padding_bits) - 1) != 0 => Err(Error::BadDer),

match padding_bits > 0 && (last_byte & padding_mask) != 0 {
true => Err(Error::BadDer),
false => Ok(BitStringFlags { raw_bits }),
(_, Some(_)) => Ok(BitStringFlags { raw_bits }),
}
})
}
Expand Down Expand Up @@ -764,6 +763,14 @@ mod tests {
bit_string_flags(bad_padding_example),
Err(Error::BadDer)
));

// invalid padding for empty set
for pad in 1..=255 {
assert_eq!(
bit_string_flags(untrusted::Input::from(&[pad])).err(),
Some(Error::BadDer)
);
}
}

#[test]
Expand All @@ -790,6 +797,24 @@ mod tests {
assert!(!res.bit_set(256));
}

#[test]
fn empty_bit_string_flags() {
let bs = super::bit_string_flags(untrusted::Input::from(&[0x00])).unwrap();

// all bits are unset
for b in 0..256 {
assert!(!bs.bit_set(b));
}
}

#[test]
fn mispadded_bit_string_flags() {
assert_eq!(
super::bit_string_flags(untrusted::Input::from(&[0x04, 0xff])).err(),
Some(super::Error::BadDer)
);
}

#[test]
fn test_small_nonnegative_integer() {
use super::{Error, FromDer, Tag};
Expand Down
Loading