Skip to content

Commit

Permalink
Add der feature for encoding and decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudy committed Dec 4, 2023
1 parent 40e1614 commit b1fc535
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ categories = [
base64ct = { version = "1.6", features = ["std"] }
bytes = { version = "1.5" }
chrono = { version = "0.4", features = ["serde"] }
der = { version = "0.7.8", optional = true }
digest = { version = "0.10" }
ecdsa = { version = "0.16", features = ["signing", "der"], optional = true }
hmac = { version = "0.12", optional = true }
Expand Down Expand Up @@ -56,6 +57,7 @@ p256 = ["dep:p256", "ecdsa"]
p384 = ["dep:p384", "ecdsa"]
p521 = ["dep:p521", "ecdsa"]
spki = ["dep:spki"]
der = ["dep:der"]

[[example]]
name = "acme-new-account"
Expand Down
37 changes: 37 additions & 0 deletions src/algorithms/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,40 @@ impl spki::SignatureBitStringEncoding for SignatureBytes {
spki::der::asn1::BitString::from_bytes(self.0.as_ref())
}
}

#[cfg(feature = "der")]
impl<'c> der::Decode<'c> for SignatureBytes {
fn decode<R: der::Reader<'c>>(reader: &mut R) -> der::Result<Self> {
use der::Encode;

let header = reader.peek_header()?;
header.tag.assert_eq(der::Tag::Sequence)?;

let len = (header.encoded_len()? + header.length)?;
let mut buf = Vec::with_capacity(usize::try_from(len)?);
let slice = buf
.get_mut(..usize::try_from(len)?)
.ok_or_else(|| reader.error(der::Tag::Sequence.length_error().kind()))?;

reader.read_into(slice)?;
Ok(Self::from(buf))
}
}

#[cfg(feature = "der")]
impl der::Tagged for SignatureBytes {
fn tag(&self) -> der::Tag {
der::Tag::Sequence
}
}

#[cfg(feature = "der")]
impl der::Encode for SignatureBytes {
fn encoded_len(&self) -> der::Result<der::Length> {
der::Length::try_from(self.0.len())
}

fn encode(&self, writer: &mut impl der::Writer) -> der::Result<()> {
writer.write(self.0.as_ref())
}
}

0 comments on commit b1fc535

Please sign in to comment.