Skip to content

Commit 7b469a0

Browse files
committed
Change der::positive_integer to strip leading zero from its result.
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.
1 parent 3fca40e commit 7b469a0

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

‎src/der.rs‎

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,35 @@ pub fn nested<'a, F, R, E: Copy>(input: &mut Reader<'a>, tag: Tag, error: E,
9797

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

102101
// Empty encodings are not allowed.
103-
if bytes.len() == 0 {
104-
return Err(());
105-
}
102+
read_all(value, (), |input| {
103+
let first_byte = try!(input.read_byte());
106104

107-
// Negative values are not allowed.
108-
if bytes[0] & 0x80 != 0 {
109-
return Err(());
110-
}
105+
if first_byte == 0 {
106+
if input.at_end() {
107+
// The valid encoding of zero.
108+
return Ok(value);
109+
}
111110

112-
// Over-long encodings are not allowed.
113-
if bytes.len() > 1 && bytes[0] == 0 && (bytes[1] & 0x80 == 0) {
114-
return Err(());
115-
}
111+
let after_leading_zero = input.mark();
112+
let second_byte = try!(input.read_byte());
113+
if (second_byte & 0x80) == 0 {
114+
// A leading zero is only allowed when the value's high bit is
115+
// set.
116+
return Err(());
117+
}
118+
let _ = input.skip_to_end();
119+
return input.get_input_between_marks(after_leading_zero,
120+
input.mark());
121+
}
122+
123+
// Negative values are not allowed.
124+
if (first_byte & 0x80) != 0 {
125+
return Err(());
126+
}
116127

117-
Ok(value)
128+
let _ = input.skip_to_end();
129+
Ok(value)
130+
})
118131
}

0 commit comments

Comments
 (0)