From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/der/src/asn1/octet_string.rs | 194 ++++++++++++++++++++++-------------- 1 file changed, 121 insertions(+), 73 deletions(-) (limited to 'vendor/der/src/asn1/octet_string.rs') diff --git a/vendor/der/src/asn1/octet_string.rs b/vendor/der/src/asn1/octet_string.rs index 6f5b91a3d..d5eb0dd0f 100644 --- a/vendor/der/src/asn1/octet_string.rs +++ b/vendor/der/src/asn1/octet_string.rs @@ -1,13 +1,10 @@ //! ASN.1 `OCTET STRING` support. use crate::{ - asn1::AnyRef, ord::OrdIsValueOrd, ByteSlice, DecodeValue, EncodeValue, Error, ErrorKind, + asn1::AnyRef, ord::OrdIsValueOrd, BytesRef, Decode, DecodeValue, EncodeValue, ErrorKind, FixedTag, Header, Length, Reader, Result, Tag, Writer, }; -#[cfg(feature = "alloc")] -use alloc::vec::Vec; - /// ASN.1 `OCTET STRING` type: borrowed form. /// /// Octet strings represent contiguous sequences of octets, a.k.a. bytes. @@ -16,13 +13,13 @@ use alloc::vec::Vec; #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct OctetStringRef<'a> { /// Inner value - inner: ByteSlice<'a>, + inner: BytesRef<'a>, } impl<'a> OctetStringRef<'a> { /// Create a new ASN.1 `OCTET STRING` from a byte slice. pub fn new(slice: &'a [u8]) -> Result { - ByteSlice::new(slice) + BytesRef::new(slice) .map(|inner| Self { inner }) .map_err(|_| ErrorKind::Length { tag: Self::TAG }.into()) } @@ -41,8 +38,15 @@ impl<'a> OctetStringRef<'a> { pub fn is_empty(&self) -> bool { self.inner.is_empty() } + + /// Parse `T` from this `OCTET STRING`'s contents. + pub fn decode_into>(&self) -> Result { + Decode::from_der(self.as_bytes()) + } } +impl_any_conversions!(OctetStringRef<'a>, 'a); + impl AsRef<[u8]> for OctetStringRef<'_> { fn as_ref(&self) -> &[u8] { self.as_bytes() @@ -51,7 +55,7 @@ impl AsRef<[u8]> for OctetStringRef<'_> { impl<'a> DecodeValue<'a> for OctetStringRef<'a> { fn decode_value>(reader: &mut R, header: Header) -> Result { - let inner = ByteSlice::decode_value(reader, header)?; + let inner = BytesRef::decode_value(reader, header)?; Ok(Self { inner }) } } @@ -61,7 +65,7 @@ impl EncodeValue for OctetStringRef<'_> { self.inner.value_len() } - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { + fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { self.inner.encode_value(writer) } } @@ -78,14 +82,6 @@ impl<'a> From<&OctetStringRef<'a>> for OctetStringRef<'a> { } } -impl<'a> TryFrom> for OctetStringRef<'a> { - type Error = Error; - - fn try_from(any: AnyRef<'a>) -> Result> { - any.decode_into() - } -} - impl<'a> From> for AnyRef<'a> { fn from(octet_string: OctetStringRef<'a>) -> AnyRef<'a> { AnyRef::from_tag_and_value(Tag::OctetString, octet_string.inner) @@ -98,85 +94,137 @@ impl<'a> From> for &'a [u8] { } } -/// ASN.1 `OCTET STRING` type: owned form.. -/// -/// Octet strings represent contiguous sequences of octets, a.k.a. bytes. -/// -/// This type provides the same functionality as [`OctetStringRef`] but owns -/// the backing data. #[cfg(feature = "alloc")] -#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] -#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] -pub struct OctetString { - /// Bitstring represented as a slice of bytes. - inner: Vec, -} +pub use self::allocating::OctetString; #[cfg(feature = "alloc")] -impl OctetString { - /// Create a new ASN.1 `OCTET STRING`. - pub fn new(bytes: impl Into>) -> Result { - let inner = bytes.into(); +mod allocating { + use super::*; + use crate::referenced::*; + use alloc::vec::Vec; - // Ensure the bytes parse successfully as an `OctetStringRef` - OctetStringRef::new(&inner)?; + /// ASN.1 `OCTET STRING` type: owned form.. + /// + /// Octet strings represent contiguous sequences of octets, a.k.a. bytes. + /// + /// This type provides the same functionality as [`OctetStringRef`] but owns + /// the backing data. + #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] + pub struct OctetString { + /// Bitstring represented as a slice of bytes. + inner: Vec, + } - Ok(Self { inner }) + impl OctetString { + /// Create a new ASN.1 `OCTET STRING`. + pub fn new(bytes: impl Into>) -> Result { + let inner = bytes.into(); + + // Ensure the bytes parse successfully as an `OctetStringRef` + OctetStringRef::new(&inner)?; + + Ok(Self { inner }) + } + + /// Borrow the inner byte slice. + pub fn as_bytes(&self) -> &[u8] { + self.inner.as_slice() + } + + /// Take ownership of the octet string. + pub fn into_bytes(self) -> Vec { + self.inner + } + + /// Get the length of the inner byte slice. + pub fn len(&self) -> Length { + self.value_len().expect("invalid OCTET STRING length") + } + + /// Is the inner byte slice empty? + pub fn is_empty(&self) -> bool { + self.inner.is_empty() + } } - /// Borrow the inner byte slice. - pub fn as_bytes(&self) -> &[u8] { - self.inner.as_slice() + impl_any_conversions!(OctetString); + + impl AsRef<[u8]> for OctetString { + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } } - /// Get the length of the inner byte slice. - pub fn len(&self) -> Length { - self.value_len().expect("invalid OCTET STRING length") + impl<'a> DecodeValue<'a> for OctetString { + fn decode_value>(reader: &mut R, header: Header) -> Result { + Self::new(reader.read_vec(header.length)?) + } } - /// Is the inner byte slice empty? - pub fn is_empty(&self) -> bool { - self.inner.is_empty() + impl EncodeValue for OctetString { + fn value_len(&self) -> Result { + self.inner.len().try_into() + } + + fn encode_value(&self, writer: &mut impl Writer) -> Result<()> { + writer.write(&self.inner) + } } -} -#[cfg(feature = "alloc")] -impl AsRef<[u8]> for OctetString { - fn as_ref(&self) -> &[u8] { - self.as_bytes() + impl FixedTag for OctetString { + const TAG: Tag = Tag::OctetString; } -} -#[cfg(feature = "alloc")] -impl<'a> DecodeValue<'a> for OctetString { - fn decode_value>(reader: &mut R, header: Header) -> Result { - Self::new(reader.read_vec(header.length)?) + impl<'a> From<&'a OctetString> for OctetStringRef<'a> { + fn from(octet_string: &'a OctetString) -> OctetStringRef<'a> { + // Ensured to parse successfully in constructor + OctetStringRef::new(&octet_string.inner).expect("invalid OCTET STRING") + } } -} -#[cfg(feature = "alloc")] -impl EncodeValue for OctetString { - fn value_len(&self) -> Result { - self.inner.len().try_into() + impl OrdIsValueOrd for OctetString {} + + impl<'a> RefToOwned<'a> for OctetStringRef<'a> { + type Owned = OctetString; + fn ref_to_owned(&self) -> Self::Owned { + OctetString { + inner: Vec::from(self.inner.as_slice()), + } + } } - fn encode_value(&self, writer: &mut dyn Writer) -> Result<()> { - writer.write(&self.inner) + impl OwnedToRef for OctetString { + type Borrowed<'a> = OctetStringRef<'a>; + fn owned_to_ref(&self) -> Self::Borrowed<'_> { + self.into() + } } -} -#[cfg(feature = "alloc")] -impl FixedTag for OctetString { - const TAG: Tag = Tag::OctetString; -} + // Implement by hand because the derive would create invalid values. + // Use the constructor to create a valid value. + #[cfg(feature = "arbitrary")] + impl<'a> arbitrary::Arbitrary<'a> for OctetString { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + Self::new(Vec::arbitrary(u)?).map_err(|_| arbitrary::Error::IncorrectFormat) + } -#[cfg(feature = "alloc")] -impl<'a> From<&'a OctetString> for OctetStringRef<'a> { - fn from(octet_string: &'a OctetString) -> OctetStringRef<'a> { - // Ensured to parse successfully in constructor - OctetStringRef::new(&octet_string.inner).expect("invalid OCTET STRING") + fn size_hint(depth: usize) -> (usize, Option) { + arbitrary::size_hint::and(u8::size_hint(depth), Vec::::size_hint(depth)) + } } } -#[cfg(feature = "alloc")] -impl OrdIsValueOrd for OctetString {} +#[cfg(test)] +mod tests { + use crate::asn1::{OctetStringRef, PrintableStringRef}; + + #[test] + fn octet_string_decode_into() { + // PrintableString "hi" + let der = b"\x13\x02\x68\x69"; + let oct = OctetStringRef::new(der).unwrap(); + + let res = oct.decode_into::>().unwrap(); + assert_eq!(AsRef::::as_ref(&res), "hi"); + } +} -- cgit v1.2.3