Skip to content

der: clarify writer: add pretty-printing DER hex with comments #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

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/der.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
toolchain: ${{ matrix.rust }}
targets: ${{ matrix.target }}
- uses: RustCrypto/actions/cargo-hack-install@master
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features arbitrary,std
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --exclude-features arbitrary,std,clarify

minimal-versions:
if: false # TODO: temp disabled due to unpublished prerelease dependencies
Expand Down
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 = []
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", "oid"]

[package.metadata.docs.rs]
all-features = true
Expand Down
1 change: 1 addition & 0 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl<'a> AnyRef<'a> {

let mut decoder = SliceReader::new_with_encoding_rules(self.value(), encoding)?;
let result = T::decode_value(&mut decoder, self.header())?;

decoder.finish()?;
Ok(result)
}
Expand Down
21 changes: 20 additions & 1 deletion der/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,26 @@ where
/// Encode this value as ASN.1 DER using the provided [`Writer`].
fn encode(&self, writer: &mut impl Writer) -> Result<()> {
self.header()?.encode(writer)?;
self.encode_value(writer)
clarify_start_value_type::<T>(writer);
let result = self.encode_value(writer);
clarify_end_value_type::<T>(writer);
result
}
}

#[allow(unused_variables)]
fn clarify_start_value_type<T: ?Sized>(writer: &mut impl Writer) {
#[cfg(feature = "clarify")]
if let Some(clarifier) = writer.clarifier() {
clarifier.clarify_start_value_type::<T>();
}
}

#[allow(unused_variables)]
fn clarify_end_value_type<T: ?Sized>(writer: &mut impl Writer) {
#[cfg(feature = "clarify")]
if let Some(clarifier) = writer.clarifier() {
clarifier.clarify_end_value_type::<T>();
}
}

Expand Down
21 changes: 20 additions & 1 deletion der/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,27 @@ impl Encode for Header {
}

fn encode(&self, writer: &mut impl Writer) -> Result<()> {
clarify_start_tag(writer, &self.tag);
self.tag.encode(writer)?;
self.length.encode(writer)
let result = self.length.encode(writer);
clarify_end_length(writer, &self.tag, self.length);
result
}
}

#[allow(unused_variables)]
fn clarify_start_tag(writer: &mut impl Writer, tag: &Tag) {
#[cfg(feature = "clarify")]
if let Some(clarifier) = writer.clarifier() {
clarifier.clarify_header_start_tag(tag);
}
}

#[allow(unused_variables)]
fn clarify_end_length(writer: &mut impl Writer, tag: &Tag, length: Length) {
#[cfg(feature = "clarify")]
if let Some(clarifier) = writer.clarifier() {
clarifier.clarify_header_end_length(Some(tag), length);
}
}

Expand Down
5 changes: 5 additions & 0 deletions der/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ pub use {
pem_rfc7468 as pem,
};

#[cfg(feature = "clarify")]
pub use writer::clarify::{
Clarifier, ClarifyFlavor, ClarifyOptions, ClarifySliceWriter, EncodeClarifyExt,
};

#[cfg(feature = "time")]
pub use time;

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