From 3699ed9a6695aa6529bada55c6c000fe4caee82d Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 16 Jul 2023 18:06:31 -0600 Subject: [PATCH] der: `bytes` feature Adds (optional) support for using `bytes::Bytes` as an `OctetString`. --- Cargo.lock | 7 +++++++ der/Cargo.toml | 4 +++- der/src/asn1/octet_string.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e387ce98b..b5988fed6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -156,6 +156,12 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" +[[package]] +name = "bytes" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" + [[package]] name = "cast" version = "0.3.0" @@ -420,6 +426,7 @@ name = "der" version = "0.7.7" dependencies = [ "arbitrary", + "bytes", "const-oid 0.9.3", "der_derive", "flagset", diff --git a/der/Cargo.toml b/der/Cargo.toml index 38f5ac364..712cb0b5b 100644 --- a/der/Cargo.toml +++ b/der/Cargo.toml @@ -17,7 +17,8 @@ rust-version = "1.65" [dependencies] arbitrary = { version = "1.3", features = ["derive"], optional = true } -const-oid = { version = "0.9.2", optional = true } # TODO: path = "../const-oid" +bytes = { version = "1", optional = true, default-features = false } +const-oid = { version = "0.9.2", optional = true } der_derive = { version = "0.7.1", optional = true } flagset = { version = "0.4.3", optional = true } pem-rfc7468 = { version = "0.7", optional = true, features = ["alloc"] } @@ -33,6 +34,7 @@ alloc = ["zeroize?/alloc"] std = ["alloc"] arbitrary = ["dep:arbitrary", "const-oid?/arbitrary", "std"] +bytes = ["dep:bytes", "alloc"] derive = ["dep:der_derive"] oid = ["dep:const-oid"] pem = ["dep:pem-rfc7468", "alloc", "zeroize"] diff --git a/der/src/asn1/octet_string.rs b/der/src/asn1/octet_string.rs index d5eb0dd0f..53d8ecb6a 100644 --- a/der/src/asn1/octet_string.rs +++ b/der/src/asn1/octet_string.rs @@ -112,7 +112,7 @@ mod allocating { #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct OctetString { /// Bitstring represented as a slice of bytes. - inner: Vec, + pub(super) inner: Vec, } impl OctetString { @@ -214,6 +214,33 @@ mod allocating { } } +#[cfg(feature = "bytes")] +mod bytes { + use super::OctetString; + use crate::{DecodeValue, EncodeValue, FixedTag, Header, Length, Reader, Result, Tag, Writer}; + use bytes::Bytes; + + impl<'a> DecodeValue<'a> for Bytes { + fn decode_value>(reader: &mut R, header: Header) -> Result { + OctetString::decode_value(reader, header).map(|octet_string| octet_string.inner.into()) + } + } + + impl EncodeValue for Bytes { + fn value_len(&self) -> Result { + self.len().try_into() + } + + fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { + writer.write(self.as_ref()) + } + } + + impl FixedTag for Bytes { + const TAG: Tag = Tag::OctetString; + } +} + #[cfg(test)] mod tests { use crate::asn1::{OctetStringRef, PrintableStringRef};