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
20 changes: 10 additions & 10 deletions der/derive/src/asn1_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ impl Asn1Type {
/// Get a `der::Decoder` object for a particular ASN.1 type
pub fn decoder(self) -> TokenStream {
match self {
Asn1Type::BitString => quote!(::der::asn1::BitString::decode(reader)?),
Asn1Type::Ia5String => quote!(::der::asn1::Ia5String::decode(reader)?),
Asn1Type::BitString => quote!(::der::asn1::BitStringRef::decode(reader)?),
Asn1Type::Ia5String => quote!(::der::asn1::Ia5StringRef::decode(reader)?),
Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime::decode(reader)?),
Asn1Type::OctetString => quote!(::der::asn1::OctetString::decode(reader)?),
Asn1Type::PrintableString => quote!(::der::asn1::PrintableString::decode(reader)?),
Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef::decode(reader)?),
Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef::decode(reader)?),
Asn1Type::UtcTime => quote!(::der::asn1::UtcTime::decode(reader)?),
Asn1Type::Utf8String => quote!(::der::asn1::Utf8String::decode(reader)?),
Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef::decode(reader)?),
}
}

Expand All @@ -74,13 +74,13 @@ impl Asn1Type {
/// Get a `der::Encoder` object for a particular ASN.1 type
pub fn type_path(self) -> TokenStream {
match self {
Asn1Type::BitString => quote!(::der::asn1::BitString),
Asn1Type::Ia5String => quote!(::der::asn1::Ia5String),
Asn1Type::BitString => quote!(::der::asn1::BitStringRef),
Asn1Type::Ia5String => quote!(::der::asn1::Ia5StringRef),
Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime),
Asn1Type::OctetString => quote!(::der::asn1::OctetString),
Asn1Type::PrintableString => quote!(::der::asn1::PrintableString),
Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef),
Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef),
Asn1Type::UtcTime => quote!(::der::asn1::UtcTime),
Asn1Type::Utf8String => quote!(::der::asn1::Utf8String),
Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions der/derive/src/choice/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ mod tests {
variant.to_decode_tokens().to_string(),
quote! {
::der::Tag::Utf8String => Ok(Self::ExampleVariant(
::der::asn1::Utf8String::decode(reader)?
::der::asn1::Utf8StringRef::decode(reader)?
.try_into()?
)),
}
Expand All @@ -224,7 +224,7 @@ mod tests {
assert_eq!(
variant.to_encode_value_tokens().to_string(),
quote! {
Self::ExampleVariant(variant) => ::der::asn1::Utf8String::new(variant)?.encode_value(encoder),
Self::ExampleVariant(variant) => ::der::asn1::Utf8StringRef::new(variant)?.encode_value(encoder),
}
.to_string()
);
Expand Down
16 changes: 8 additions & 8 deletions der/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,26 @@ mod utc_time;
mod utf8_string;

pub use self::{
any::Any,
bit_string::{BitString, BitStringIter},
any::AnyRef,
bit_string::{BitStringIter, BitStringRef},
choice::Choice,
context_specific::{ContextSpecific, ContextSpecificRef},
generalized_time::GeneralizedTime,
ia5_string::Ia5String,
integer::bigint::UIntBytes,
ia5_string::Ia5StringRef,
integer::bigint::UIntRef,
null::Null,
octet_string::OctetString,
printable_string::PrintableString,
octet_string::OctetStringRef,
printable_string::PrintableStringRef,
sequence::{Sequence, SequenceRef},
sequence_of::{SequenceOf, SequenceOfIter},
set_of::{SetOf, SetOfIter},
utc_time::UtcTime,
utf8_string::Utf8String,
utf8_string::Utf8StringRef,
};

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub use self::{bit_string::BitStringOwned, set_of::SetOfVec};
pub use self::{bit_string::BitString, set_of::SetOfVec};

#[cfg(feature = "oid")]
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
Expand Down
50 changes: 26 additions & 24 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,48 @@ use crate::asn1::ObjectIdentifier;

/// ASN.1 `ANY`: represents any explicitly tagged ASN.1 value.
///
/// This is a zero-copy reference type which borrows from the input data.
///
/// Technically `ANY` hasn't been a recommended part of ASN.1 since the X.209
/// revision from 1988. It was deprecated and replaced by Information Object
/// Classes in X.680 in 1994, and X.690 no longer refers to it whatsoever.
///
/// Nevertheless, this crate defines an [`Any`] type as it remains a familiar
/// Nevertheless, this crate defines an `ANY` type as it remains a familiar
/// and useful concept which is still extensively used in things like
/// PKI-related RFCs.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct Any<'a> {
pub struct AnyRef<'a> {
/// Tag representing the type of the encoded value.
tag: Tag,

/// Inner value encoded as bytes.
value: ByteSlice<'a>,
}

impl<'a> Any<'a> {
/// [`Any`] representation of the ASN.1 `NULL` type.
impl<'a> AnyRef<'a> {
/// [`AnyRef`] representation of the ASN.1 `NULL` type.
pub const NULL: Self = Self {
tag: Tag::Null,
value: ByteSlice::EMPTY,
};

/// Create a new [`Any`] from the provided [`Tag`] and byte slice.
/// Create a new [`AnyRef`] from the provided [`Tag`] and byte slice.
pub fn new(tag: Tag, bytes: &'a [u8]) -> Result<Self> {
let value = ByteSlice::new(bytes).map_err(|_| ErrorKind::Length { tag })?;
Ok(Self { tag, value })
}

/// Infallible creation of an [`Any`] from a [`ByteSlice`].
/// Infallible creation of an [`AnyRef`] from a [`ByteSlice`].
pub(crate) fn from_tag_and_value(tag: Tag, value: ByteSlice<'a>) -> Self {
Self { tag, value }
}

/// Get the raw value for this [`Any`] type as a byte slice.
/// Get the raw value for this [`AnyRef`] type as a byte slice.
pub fn value(self) -> &'a [u8] {
self.value.as_slice()
}

/// Attempt to decode this [`Any`] type into the inner value.
/// Attempt to decode this [`AnyRef`] type into the inner value.
pub fn decode_into<T>(self) -> Result<T>
where
T: DecodeValue<'a> + FixedTag,
Expand All @@ -72,7 +74,7 @@ impl<'a> Any<'a> {
}

/// Attempt to decode an ASN.1 `BIT STRING`.
pub fn bit_string(self) -> Result<BitString<'a>> {
pub fn bit_string(self) -> Result<BitStringRef<'a>> {
self.try_into()
}

Expand All @@ -90,12 +92,12 @@ impl<'a> Any<'a> {
}

/// Attempt to decode an ASN.1 `IA5String`.
pub fn ia5_string(self) -> Result<Ia5String<'a>> {
pub fn ia5_string(self) -> Result<Ia5StringRef<'a>> {
self.try_into()
}

/// Attempt to decode an ASN.1 `OCTET STRING`.
pub fn octet_string(self) -> Result<OctetString<'a>> {
pub fn octet_string(self) -> Result<OctetStringRef<'a>> {
self.try_into()
}

Expand All @@ -119,7 +121,7 @@ impl<'a> Any<'a> {
}

/// Attempt to decode an ASN.1 `PrintableString`.
pub fn printable_string(self) -> Result<PrintableString<'a>> {
pub fn printable_string(self) -> Result<PrintableStringRef<'a>> {
self.try_into()
}

Expand All @@ -141,19 +143,19 @@ impl<'a> Any<'a> {
}

/// Attempt to decode an ASN.1 `UTF8String`.
pub fn utf8_string(self) -> Result<Utf8String<'a>> {
pub fn utf8_string(self) -> Result<Utf8StringRef<'a>> {
self.try_into()
}
}

impl<'a> Choice<'a> for Any<'a> {
impl<'a> Choice<'a> for AnyRef<'a> {
fn can_decode(_: Tag) -> bool {
true
}
}

impl<'a> Decode<'a> for Any<'a> {
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<Any<'a>> {
impl<'a> Decode<'a> for AnyRef<'a> {
fn decode<R: Reader<'a>>(reader: &mut R) -> Result<AnyRef<'a>> {
let header = Header::decode(reader)?;

Ok(Self {
Expand All @@ -163,7 +165,7 @@ impl<'a> Decode<'a> for Any<'a> {
}
}

impl EncodeValue for Any<'_> {
impl EncodeValue for AnyRef<'_> {
fn value_len(&self) -> Result<Length> {
Ok(self.value.len())
}
Expand All @@ -173,28 +175,28 @@ impl EncodeValue for Any<'_> {
}
}

impl Tagged for Any<'_> {
impl Tagged for AnyRef<'_> {
fn tag(&self) -> Tag {
self.tag
}
}

impl ValueOrd for Any<'_> {
impl ValueOrd for AnyRef<'_> {
fn value_cmp(&self, other: &Self) -> Result<Ordering> {
self.value.der_cmp(&other.value)
}
}

impl<'a> From<Any<'a>> for ByteSlice<'a> {
fn from(any: Any<'a>) -> ByteSlice<'a> {
impl<'a> From<AnyRef<'a>> for ByteSlice<'a> {
fn from(any: AnyRef<'a>) -> ByteSlice<'a> {
any.value
}
}

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

fn try_from(bytes: &'a [u8]) -> Result<Any<'a>> {
Any::from_der(bytes)
fn try_from(bytes: &'a [u8]) -> Result<AnyRef<'a>> {
AnyRef::from_der(bytes)
}
}
Loading