Skip to content

[WIP] der: clarify writer draft #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions der/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ pem-rfc7468 = { version = "1.0.0-rc.3", optional = true, features = ["alloc"] }
time = { version = "0.3.4", optional = true, default-features = false }
zeroize = { version = "1.8", optional = true, default-features = false }
heapless = { version = "0.8", optional = true, default-features = false }
tynm = { version = "0.2", optional = true, default-features = false }

[dev-dependencies]
hex-literal = "1"
proptest = "1"

[features]
default = ["clarify"]
alloc = ["zeroize?/alloc"]
std = ["alloc"]

Expand All @@ -41,6 +43,7 @@ derive = ["dep:der_derive"]
oid = ["dep:const-oid"]
pem = ["dep:pem-rfc7468", "alloc", "zeroize"]
real = []
clarify = ["std", "pem", "dep:tynm", "derive"]

[package.metadata.docs.rs]
all-features = true
Expand Down
12 changes: 8 additions & 4 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ impl<'a> AnyRef<'a> {
pub fn value(self) -> &'a [u8] {
self.value.as_slice()
}
/// Returns [`Tag`] and [`Length`] of self.
pub fn header(&self) -> Header {
Header {
tag: self.tag,
length: self.value.len(),
}
}

/// Attempt to decode this [`AnyRef`] type into the inner value.
pub fn decode_as<T>(self) -> Result<T, <T as DecodeValue<'a>>::Error>
Expand All @@ -66,10 +73,7 @@ impl<'a> AnyRef<'a> {
return Err(self.tag.unexpected_error(None).to_error().into());
}

let header = Header {
tag: self.tag,
length: self.value.len(),
};
let header = self.header();

let mut decoder = SliceReader::new(self.value())?;
let result = T::decode_value(&mut decoder, header)?;
Expand Down
8 changes: 8 additions & 0 deletions der/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Writer trait.

#[cfg(feature = "clarify")]
pub mod clarify;
#[cfg(feature = "pem")]
pub(crate) mod pem;
pub(crate) mod slice;
Expand All @@ -18,6 +20,12 @@ pub trait Writer {
fn write_byte(&mut self, byte: u8) -> Result<()> {
self.write(&[byte])
}

#[cfg(feature = "clarify")]
/// Should return Some(clarifier) for clarify writers
fn clarifier(&mut self) -> Option<&mut clarify::Clarifier> {
None
}
}

#[cfg(feature = "std")]
Expand Down
Loading
Loading