Skip to content
Merged
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
8 changes: 4 additions & 4 deletions cms/src/attr.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Attribute-related types
//!
use alloc::{boxed::Box, vec};
use core::borrow::Borrow;
use der::{
DecodeValue, EncodeValue, FixedTag, Length, Tag,
asn1::{OctetString, OctetStringRef},
referenced::OwnedToRef,
};

use x509_cert::time::Time;

use crate::signed_data::SignerInfo;
Expand Down Expand Up @@ -50,8 +50,8 @@ impl MessageDigest {

/// Return an [`OctetStringRef`] pointing to the underlying data
#[inline]
pub fn as_octet_string_ref<'a>(&'a self) -> OctetStringRef<'a> {
self.0.owned_to_ref()
pub fn as_octet_string_ref(&self) -> &OctetStringRef {
self.0.borrow()
}
}

Expand Down
2 changes: 1 addition & 1 deletion cms/src/timestamped_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct TimeStampedData<'a> {
#[asn1(optional = "true")]
pub meta_data: Option<MetaData>,
#[asn1(optional = "true")]
pub content: Option<OctetStringRef<'a>>,
pub content: Option<&'a OctetStringRef>,
pub temporal_evidence: Evidence,
}

Expand Down
2 changes: 1 addition & 1 deletion cms/tests/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ fn test_create_password_recipient_info() {
.to_der()
.unwrap();
let iv = Iv::<cbc::Decryptor<Aes128>>::try_from(
OctetStringRef::from_der(algorithm_params_der.as_slice())
<&OctetStringRef>::from_der(algorithm_params_der.as_slice())
.unwrap()
.as_bytes(),
)
Expand Down
2 changes: 1 addition & 1 deletion cms/tests/tests_from_pkcs7_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn cms_decode_signed_der() {
sd.encap_content_info
.econtent
.unwrap()
.decode_as::<OctetStringRef>()
.decode_as::<&OctetStringRef>()
.unwrap()
.as_bytes()
.len(),
Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub use self::{
ia5_string::Ia5StringRef,
integer::{int::IntRef, uint::UintRef},
null::Null,
octet_string::OctetStringRef,
octet_string::{OctetStringRef, OctetStringRef2},
printable_string::PrintableStringRef,
private::{Private, PrivateRef},
sequence::{Sequence, SequenceRef},
Expand Down
164 changes: 100 additions & 64 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,43 @@ use crate::{
Tag, Writer, asn1::AnyRef, ord::OrdIsValueOrd,
};

// TODO(tarcieri): custom derive hack until the logic is updated to support `&'a` reference types
#[doc(hidden)]
pub type OctetStringRef2<'a> = &'a OctetStringRef;
Comment on lines +8 to +10
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 one of the hacks. I didn't implement special case support for &'a MyRef types in der_derive yet, so this hack lets it use the old syntax.


/// ASN.1 `OCTET STRING` type: borrowed form.
///
/// 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(slice: &[u8]) -> Result<&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 +56,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!(&'a OctetStringRef, '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,31 +87,28 @@ 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;
}

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> {
Expand All @@ -105,26 +117,26 @@ impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
}

/// Hack for simplifying the custom derive use case.
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)
}
}

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 +145,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 +157,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 +171,12 @@ 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, ToOwned},
boxed::Box,
vec::Vec,
};

/// ASN.1 `OCTET STRING` type: owned form.
///
Expand Down Expand Up @@ -214,6 +230,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 +259,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 +296,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 +365,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 +381,23 @@ 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() {
// PrintableString "hi"
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
Loading