Skip to content

Commit

Permalink
Change der::positive_integer to strip leading zero from its result.
Browse files Browse the repository at this point in the history
This way, callers don't have to strip it itself, which simplifies them.
Callers shouldn't need to know about the DER requirements for leading
zeros.
  • Loading branch information
briansmith committed Jun 2, 2016
1 parent 3fca40e commit 7b469a0
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,35 @@ pub fn nested<'a, F, R, E: Copy>(input: &mut Reader<'a>, tag: Tag, error: E,

pub fn positive_integer<'a>(input: &mut Reader<'a>) -> Result<Input<'a>, ()> {
let value = try!(expect_tag_and_get_value(input, Tag::Integer));
let bytes = value.as_slice_less_safe();

// Empty encodings are not allowed.
if bytes.len() == 0 {
return Err(());
}
read_all(value, (), |input| {
let first_byte = try!(input.read_byte());

// Negative values are not allowed.
if bytes[0] & 0x80 != 0 {
return Err(());
}
if first_byte == 0 {
if input.at_end() {
// The valid encoding of zero.
return Ok(value);
}

// Over-long encodings are not allowed.
if bytes.len() > 1 && bytes[0] == 0 && (bytes[1] & 0x80 == 0) {
return Err(());
}
let after_leading_zero = input.mark();
let second_byte = try!(input.read_byte());
if (second_byte & 0x80) == 0 {
// A leading zero is only allowed when the value's high bit is
// set.
return Err(());
}
let _ = input.skip_to_end();
return input.get_input_between_marks(after_leading_zero,
input.mark());
}

// Negative values are not allowed.
if (first_byte & 0x80) != 0 {
return Err(());
}

Ok(value)
let _ = input.skip_to_end();
Ok(value)
})
}

0 comments on commit 7b469a0

Please sign in to comment.