Skip to content

Commit

Permalink
Implement Integer type binding for DER Reader and Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
kellerkindt committed Jan 20, 2024
1 parent 68d5113 commit c74917f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/rw/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ impl<W: BasicWrite> Writer for BasicWriter<W> {

fn write_number<T: Number, C: crate::descriptor::numbers::Constraint<T>>(
&mut self,
_value: T,
value: T,
) -> Result<(), Self::Error> {
todo!()
self.write.write_identifier(C::TAG)?;
let value = value.to_i64();
let offset = value.leading_zeros() / u8::BITS;
let len = value.to_be_bytes().len() as u64 - offset as u64;
self.write.write_length(len)?;
self.write.write_integer_i64(value)?;
Ok(())
}

fn write_utf8string<C: crate::descriptor::utf8string::Constraint>(
Expand Down Expand Up @@ -227,7 +233,12 @@ impl<R: BasicRead> Reader for BasicReader<R> {
fn read_number<T: Number, C: crate::descriptor::numbers::Constraint<T>>(
&mut self,
) -> Result<T, Self::Error> {
todo!()
let identifier = self.read.read_identifier()?;
if identifier.value() != Tag::DEFAULT_INTEGER.value() {
return Err(Error::unexpected_tag(Tag::DEFAULT_INTEGER, identifier));
}
let len = self.read.read_length()?;
self.read.read_integer_i64(len as u32).map(T::from_i64)
}

fn read_utf8string<C: crate::descriptor::utf8string::Constraint>(
Expand Down
50 changes: 50 additions & 0 deletions tests/der_basic_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use asn1rs::descriptor::numbers::NoConstraint;
use asn1rs::descriptor::numbers::Number;
use asn1rs::descriptor::{Integer, ReadableType, WritableType};
use asn1rs::prelude::basic::DER;
use std::fmt::Debug;

fn write_read_integer<T: Number + Debug + PartialEq>(len: T) {
let mut buffer = Vec::new();
let mut writer = DER::writer(&mut buffer);

Integer::<T, NoConstraint>::write_value(&mut writer, &len).unwrap();

let mut reader = DER::reader(&buffer[..]);
let read = Integer::<T, NoConstraint>::read_value(&mut reader).unwrap();

assert_eq!(len, read);
}

#[test]
pub fn test_length_bounds() {
write_read_integer(0);
write_read_integer(u8::MAX as u64 - 1);
write_read_integer(u8::MAX as u64);
write_read_integer(u8::MAX as u64 + 1);
write_read_integer(u16::MAX as u64 - 1);
write_read_integer(u16::MAX as u64);
write_read_integer(u16::MAX as u64 + 1);
write_read_integer(u32::MAX as u64 - 1);
write_read_integer(u32::MAX as u64);
write_read_integer(u32::MAX as u64 + 1);
write_read_integer(u64::MAX - 1);
write_read_integer(u64::MAX);
}

#[inline]
pub fn test_letsencrypt_point_numbers() {
const BYTES: &'static [u8] = &[0x80, 0x01, 0x09, 0x81, 0x01, 0x09];

let mut reader = DER::reader(BYTES);

assert_eq!(
9,
Integer::<i64, NoConstraint>::read_value(&mut reader).unwrap()
);

assert_eq!(
9,
Integer::<i64, NoConstraint>::read_value(&mut reader).unwrap()
);
}

0 comments on commit c74917f

Please sign in to comment.