Skip to content
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
167 changes: 95 additions & 72 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,34 @@ use crate::{
/// Octet strings represent contiguous sequences of octets, a.k.a. bytes.
///
/// This is a zero-copy reference type which borrows from the input data.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct OctetStringRef<'a> {
#[derive(Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct OctetStringRef {
/// Inner value
inner: &'a BytesRef,
inner: BytesRef,
}

impl<'a> OctetStringRef<'a> {
impl OctetStringRef {
/// Create a new ASN.1 `OCTET STRING` from a byte slice.
pub fn new(slice: &'a [u8]) -> Result<Self, Error> {
pub fn new<'a>(slice: &'a [u8]) -> Result<&'a Self, Error> {
BytesRef::new(slice)
.map(|inner| Self { inner })
.map(Self::from_bytes_ref)
.map_err(|_| ErrorKind::Length { tag: Self::TAG }.into())
}

/// Create an [`OctetStringRef`] from a [`BytesRef`].
///
/// Implemented as an inherent method to keep [`BytesRef`] out of the public API.
fn from_bytes_ref(bytes_ref: &BytesRef) -> &Self {
// SAFETY: `Self` is a `repr(transparent)` newtype for `BytesRef`
#[allow(unsafe_code)]
unsafe {
&*(bytes_ref.as_ptr() as *const Self)
}
}

/// Borrow the inner byte slice.
pub fn as_bytes(&self) -> &'a [u8] {
pub fn as_bytes(&self) -> &[u8] {
self.inner.as_slice()
}

Expand All @@ -40,29 +52,28 @@ impl<'a> OctetStringRef<'a> {
}

/// Parse `T` from this `OCTET STRING`'s contents.
pub fn decode_into<T: Decode<'a>>(&self) -> Result<T, T::Error> {
pub fn decode_into<'a, T: Decode<'a>>(&'a self) -> Result<T, T::Error> {
Decode::from_der(self.as_bytes())
}
}

impl_any_conversions!(OctetStringRef<'a>, 'a);
//impl_any_conversions!(OctetStringRef<'a>, 'a);

impl AsRef<[u8]> for OctetStringRef<'_> {
impl AsRef<[u8]> for OctetStringRef {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}

impl<'a> DecodeValue<'a> for OctetStringRef<'a> {
impl<'a> DecodeValue<'a> for &'a OctetStringRef {
type Error = Error;

fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> Result<Self, Error> {
let inner = <&'a BytesRef>::decode_value(reader, header)?;
Ok(Self { inner })
<&'a BytesRef>::decode_value(reader, header).map(OctetStringRef::from_bytes_ref)
}
}

impl EncodeValue for OctetStringRef<'_> {
impl EncodeValue for OctetStringRef {
fn value_len(&self) -> Result<Length, Error> {
self.inner.value_len()
}
Expand All @@ -72,59 +83,47 @@ impl EncodeValue for OctetStringRef<'_> {
}
}

impl FixedTag for OctetStringRef<'_> {
impl FixedTag for OctetStringRef {
const TAG: Tag = Tag::OctetString;
}

impl OrdIsValueOrd for OctetStringRef<'_> {}

impl<'a> From<&OctetStringRef<'a>> for OctetStringRef<'a> {
fn from(value: &OctetStringRef<'a>) -> OctetStringRef<'a> {
*value
}
impl FixedTag for &OctetStringRef {
const TAG: Tag = Tag::OctetString;
}
Comment on lines +86 to 91
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little weird, but seems to work


impl<'a> From<OctetStringRef<'a>> for AnyRef<'a> {
fn from(octet_string: OctetStringRef<'a>) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::OctetString, octet_string.inner)
impl OrdIsValueOrd for OctetStringRef {}

impl<'a> From<&'a OctetStringRef> for AnyRef<'a> {
fn from(octet_string: &'a OctetStringRef) -> AnyRef<'a> {
AnyRef::from_tag_and_value(Tag::OctetString, &octet_string.inner)
}
}

impl<'a> From<OctetStringRef<'a>> for &'a [u8] {
fn from(octet_string: OctetStringRef<'a>) -> &'a [u8] {
impl<'a> From<&'a OctetStringRef> for &'a [u8] {
fn from(octet_string: &'a OctetStringRef) -> &'a [u8] {
octet_string.as_bytes()
}
}

impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
impl<'a> TryFrom<&'a [u8]> for &'a OctetStringRef {
type Error = Error;

fn try_from(byte_slice: &'a [u8]) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

/// Hack for simplifying the custom derive use case.
impl<'a> TryFrom<&&'a [u8]> for OctetStringRef<'a> {
type Error = Error;

fn try_from(byte_slice: &&'a [u8]) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

impl<'a, const N: usize> TryFrom<&'a [u8; N]> for OctetStringRef<'a> {
impl<'a, const N: usize> TryFrom<&'a [u8; N]> for &'a OctetStringRef {
type Error = Error;

fn try_from(byte_slice: &'a [u8; N]) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for [u8; N] {
impl<'a, const N: usize> TryFrom<&'a OctetStringRef> for [u8; N] {
type Error = Error;

fn try_from(octet_string: OctetStringRef<'a>) -> Result<Self, Self::Error> {
fn try_from(octet_string: &'a OctetStringRef) -> Result<Self, Self::Error> {
octet_string
.as_bytes()
.try_into()
Expand All @@ -133,10 +132,10 @@ impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for [u8; N] {
}

#[cfg(feature = "heapless")]
impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for heapless::Vec<u8, N> {
impl<const N: usize> TryFrom<&OctetStringRef> for heapless::Vec<u8, N> {
type Error = Error;

fn try_from(octet_string: OctetStringRef<'a>) -> Result<Self, Self::Error> {
fn try_from(octet_string: &OctetStringRef) -> Result<Self, Self::Error> {
octet_string
.as_bytes()
.try_into()
Expand All @@ -145,7 +144,7 @@ impl<'a, const N: usize> TryFrom<OctetStringRef<'a>> for heapless::Vec<u8, N> {
}

#[cfg(feature = "heapless")]
impl<'a, const N: usize> TryFrom<&'a heapless::Vec<u8, N>> for OctetStringRef<'a> {
impl<'a, const N: usize> TryFrom<&'a heapless::Vec<u8, N>> for &'a OctetStringRef {
type Error = Error;

fn try_from(byte_vec: &'a heapless::Vec<u8, N>) -> Result<Self, Error> {
Expand All @@ -159,8 +158,13 @@ pub use self::allocating::OctetString;
#[cfg(feature = "alloc")]
mod allocating {
use super::*;
use crate::{BytesOwned, referenced::*};
use alloc::{borrow::Cow, boxed::Box, vec::Vec};
use crate::BytesOwned;
use alloc::{
borrow::{Borrow, Cow},
boxed::Box,
vec::Vec,
};
use std::prelude::rust_2015::ToOwned;

/// ASN.1 `OCTET STRING` type: owned form.
///
Expand Down Expand Up @@ -214,6 +218,12 @@ mod allocating {
}
}

impl Borrow<OctetStringRef> for OctetString {
fn borrow(&self) -> &OctetStringRef {
OctetStringRef::from_bytes_ref(self.inner.as_ref())
}
}

impl<'a> DecodeValue<'a> for OctetString {
type Error = Error;

Expand All @@ -237,40 +247,30 @@ mod allocating {
const TAG: Tag = Tag::OctetString;
}

impl<'a> From<&'a OctetString> for OctetStringRef<'a> {
fn from(octet_string: &'a OctetString) -> OctetStringRef<'a> {
OctetStringRef {
inner: octet_string.inner.as_ref(),
}
}
}

impl OrdIsValueOrd for OctetString {}

impl<'a> RefToOwned<'a> for OctetStringRef<'a> {
type Owned = OctetString;
fn ref_to_owned(&self) -> Self::Owned {
OctetString {
inner: self.inner.into(),
}
impl<'a> From<&'a OctetString> for &'a OctetStringRef {
fn from(octet_string: &'a OctetString) -> &'a OctetStringRef {
OctetStringRef::from_bytes_ref(octet_string.inner.as_ref())
}
}

impl OwnedToRef for OctetString {
type Borrowed<'a> = OctetStringRef<'a>;
fn owned_to_ref(&self) -> Self::Borrowed<'_> {
self.into()
impl From<&OctetStringRef> for OctetString {
fn from(octet_string_ref: &OctetStringRef) -> OctetString {
Self {
inner: octet_string_ref.inner.to_owned(),
}
}
}

impl From<OctetStringRef<'_>> for Vec<u8> {
fn from(octet_string: OctetStringRef<'_>) -> Vec<u8> {
impl From<&OctetStringRef> for Vec<u8> {
fn from(octet_string: &OctetStringRef) -> Vec<u8> {
Vec::from(octet_string.as_bytes())
}
}

/// Hack for simplifying the custom derive use case.
impl<'a> TryFrom<&'a Vec<u8>> for OctetStringRef<'a> {
impl<'a> TryFrom<&'a Vec<u8>> for &'a OctetStringRef {
type Error = Error;

fn try_from(byte_vec: &'a Vec<u8>) -> Result<Self, Error> {
Expand All @@ -284,18 +284,26 @@ mod allocating {
}
}

impl<'a> TryFrom<&'a Cow<'a, [u8]>> for OctetStringRef<'a> {
impl ToOwned for OctetStringRef {
type Owned = OctetString;

fn to_owned(&self) -> OctetString {
self.into()
}
}

impl<'a> TryFrom<&'a Cow<'a, [u8]>> for &'a OctetStringRef {
type Error = Error;

fn try_from(byte_slice: &'a Cow<'a, [u8]>) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

impl<'a> TryFrom<OctetStringRef<'a>> for Cow<'a, [u8]> {
impl<'a> TryFrom<&'a OctetStringRef> for Cow<'a, [u8]> {
type Error = Error;

fn try_from(octet_string: OctetStringRef<'a>) -> Result<Self, Self::Error> {
fn try_from(octet_string: &'a OctetStringRef) -> Result<Self, Self::Error> {
Ok(Cow::Borrowed(octet_string.as_bytes()))
}
}
Expand Down Expand Up @@ -345,8 +353,8 @@ mod bytes {
const TAG: Tag = Tag::OctetString;
}

impl From<OctetStringRef<'_>> for Bytes {
fn from(octet_string: OctetStringRef<'_>) -> Bytes {
impl From<&OctetStringRef> for Bytes {
fn from(octet_string: &OctetStringRef) -> Bytes {
Vec::from(octet_string).into()
}
}
Expand All @@ -361,7 +369,22 @@ mod bytes {
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use crate::asn1::{OctetStringRef, PrintableStringRef};
use crate::{
Decode,
asn1::{OctetStringRef, PrintableStringRef},
};
use hex_literal::hex;

#[test]
fn octet_string_decode() {
const EXAMPLE: &[u8] = &hex!(
"040c" // primitive definite length OCTET STRING
"48656c6c6f2c20776f726c64" // "Hello, world"
);

let decoded = <&OctetStringRef>::from_der(EXAMPLE).unwrap();
assert_eq!(decoded.as_bytes(), b"Hello, world");
}

#[test]
fn octet_string_decode_into() {
Expand Down
5 changes: 5 additions & 0 deletions der/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl BytesRef {
}
}

/// Get a pointer to this [`BytesRef`].
pub(crate) const fn as_ptr(&self) -> *const BytesRef {
self as *const BytesRef
}

/// Borrow the inner byte slice
pub const fn as_slice(&self) -> &[u8] {
&self.0
Expand Down
Loading