diff --git a/der/src/asn1.rs b/der/src/asn1.rs index 7bfe55af4..fce92c17b 100644 --- a/der/src/asn1.rs +++ b/der/src/asn1.rs @@ -5,6 +5,7 @@ mod internal_macros; mod any; +mod application; mod bit_string; #[cfg(feature = "alloc")] mod bmp_string; @@ -37,7 +38,7 @@ pub use self::{ bit_string::{BitStringIter, BitStringRef}, choice::Choice, //context_specific::{ContextSpecific, ContextSpecificRef}, - context_specific::ContextSpecific, + context_specific::{ContextSpecificExplicit, ContextSpecificImplicit}, custom_class::{AnyCustomClassExplicit, AnyCustomClassImplicit}, generalized_time::GeneralizedTime, ia5_string::Ia5StringRef, @@ -46,7 +47,7 @@ pub use self::{ octet_string::OctetStringRef, printable_string::PrintableStringRef, //private::{Private, PrivateRef}, - private::Private, + private::{PrivateExplicit, PrivateImplicit}, sequence::{Sequence, SequenceRef}, sequence_of::{SequenceOf, SequenceOfIter}, set_of::{SetOf, SetOfIter}, diff --git a/der/src/asn1/application.rs b/der/src/asn1/application.rs new file mode 100644 index 000000000..0ded9b993 --- /dev/null +++ b/der/src/asn1/application.rs @@ -0,0 +1,11 @@ +//! Application field. + +use crate::tag::CLASS_APPLICATION; + +use super::custom_class::{CustomClassExplicit, CustomClassImplicit}; + +/// Application class, EXPLICIT +pub type ApplicationExplicit = CustomClassExplicit; + +/// Application class, IMPLICIT +pub type ApplicationImplicit = CustomClassImplicit; diff --git a/der/src/asn1/context_specific.rs b/der/src/asn1/context_specific.rs index 8a714d04a..07312af0d 100644 --- a/der/src/asn1/context_specific.rs +++ b/der/src/asn1/context_specific.rs @@ -8,11 +8,12 @@ use crate::{ use core::cmp::Ordering; use super::custom_class::{ - self, AnyCustomClassExplicit, AnyCustomClassImplicit, CustomClass, CustomClassImplicit, + self, AnyCustomClassExplicit, AnyCustomClassImplicit, CustomClassExplicit, CustomClassImplicit, }; /// Context-specific class, EXPLICIT -pub type ContextSpecific = CustomClass; +pub type ContextSpecificExplicit = + CustomClassExplicit; /// Context-specific class, IMPLICIT pub type ContextSpecificImplicit = @@ -291,7 +292,7 @@ pub fn decode_explicit<'a, R: Reader<'a>, T: Decode<'a>>( #[cfg(test)] #[allow(clippy::unwrap_used)] mod tests { - use super::ContextSpecific; + use super::ContextSpecificExplicit; use crate::{ asn1::{context_specific::ContextSpecificImplicit, BitStringRef}, Decode, Encode, SliceReader, TagMode, TagNumber, @@ -304,7 +305,8 @@ mod tests { #[test] fn round_trip() { - let field = ContextSpecific::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap(); + let field = + ContextSpecificExplicit::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap(); assert_eq!( field.value, BitStringRef::from_bytes(&EXAMPLE_BYTES[5..]).unwrap() @@ -320,20 +322,20 @@ mod tests { // Empty message let mut reader = SliceReader::new(&[]).unwrap(); assert_eq!( - ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(), + ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(), None ); // Message containing a non-context-specific type let mut reader = SliceReader::new(&hex!("020100")).unwrap(); assert_eq!( - ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(), + ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(), None ); // Message containing an EXPLICIT context-specific field let mut reader = SliceReader::new(&hex!("A003020100")).unwrap(); - let field = ContextSpecific::<0, u8>::decode_optional(&mut reader) + let field = ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader) .unwrap() .unwrap(); @@ -365,7 +367,7 @@ mod tests { #[test] fn context_specific_skipping_unknown_field() { let mut reader = SliceReader::new(&hex!("A003020100A103020101")).unwrap(); - let field = ContextSpecific::<1, u8>::decode_optional(&mut reader) + let field = ContextSpecificExplicit::<1, u8>::decode_optional(&mut reader) .unwrap() .unwrap(); assert_eq!(field.value, 1); @@ -375,7 +377,7 @@ mod tests { fn context_specific_returns_none_on_greater_tag_number() { let mut reader = SliceReader::new(&hex!("A103020101")).unwrap(); assert_eq!( - ContextSpecific::<0, u8>::decode_optional(&mut reader).unwrap(), + ContextSpecificExplicit::<0, u8>::decode_optional(&mut reader).unwrap(), None ); } diff --git a/der/src/asn1/custom_class.rs b/der/src/asn1/custom_class.rs index 973856077..5c1e5b680 100644 --- a/der/src/asn1/custom_class.rs +++ b/der/src/asn1/custom_class.rs @@ -13,7 +13,7 @@ use crate::{ /// This type decodes/encodes a field which is specific to a particular context /// and is identified by a [`TagNumber`]. #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] -pub struct CustomClass { +pub struct CustomClassExplicit { /// Value of the field. pub value: T, } @@ -28,7 +28,7 @@ pub struct CustomClassImplicit { pub value: T, } -impl CustomClass { +impl CustomClassExplicit { /// Attempt to decode an `EXPLICIT` ASN.1 custom-tagged field with the /// provided [`TagNumber`]. /// @@ -186,7 +186,7 @@ where } } -impl<'a, T, const TAG: u16, const CLASS: u8> Choice<'a> for CustomClass +impl<'a, T, const TAG: u16, const CLASS: u8> Choice<'a> for CustomClassExplicit where T: Decode<'a> + Tagged, { @@ -206,7 +206,7 @@ where } } -impl<'a, T, const TAG: u16, const CLASS: u8> Decode<'a> for CustomClass +impl<'a, T, const TAG: u16, const CLASS: u8> Decode<'a> for CustomClassExplicit where T: Decode<'a>, { @@ -243,7 +243,7 @@ where } } -impl EncodeValue for CustomClass +impl EncodeValue for CustomClassExplicit where T: EncodeValue + Tagged, { @@ -287,7 +287,7 @@ pub const fn expected_tag_constructed(class: Class, number: TagNumber, construct } } -impl Tagged for CustomClass { +impl Tagged for CustomClassExplicit { fn tag(&self) -> Tag { expected_tag_constructed(Class::from(CLASS), TagNumber(TAG), true) } @@ -303,7 +303,8 @@ where } } -impl<'a, T, const TAG: u16, const CLASS: u8> TryFrom> for CustomClass +impl<'a, T, const TAG: u16, const CLASS: u8> TryFrom> + for CustomClassExplicit where T: Decode<'a>, { @@ -353,7 +354,7 @@ where } } -impl ValueOrd for CustomClass +impl ValueOrd for CustomClassExplicit where T: DerOrd, { diff --git a/der/src/asn1/private.rs b/der/src/asn1/private.rs index c6e7f1b59..e709ffa79 100644 --- a/der/src/asn1/private.rs +++ b/der/src/asn1/private.rs @@ -8,16 +8,10 @@ use crate::{ use crate::{Class, FixedTag}; use core::cmp::Ordering; -use super::custom_class::{CustomClass, CustomClassImplicit}; - -/// Application class, EXPLICIT -pub type Application = CustomClass; +use super::custom_class::{CustomClassExplicit, CustomClassImplicit}; /// Private class, EXPLICIT -pub type Private = CustomClass; - -/// Application class, IMPLICIT -pub type ApplicationImplicit = CustomClassImplicit; +pub type PrivateExplicit = CustomClassExplicit; /// Private class, IMPLICIT pub type PrivateImplicit = CustomClassImplicit; @@ -75,7 +69,7 @@ pub type PrivateImplicit = CustomClassImplicit>::from_der(EXAMPLE_BYTES).unwrap(); + let field = PrivateExplicit::<1, BitStringRef<'_>>::from_der(EXAMPLE_BYTES).unwrap(); assert_eq!( field.value, BitStringRef::from_bytes(&EXAMPLE_BYTES[5..]).unwrap() @@ -117,7 +111,7 @@ mod tests { // Message containing an EXPLICIT private field let mut reader = SliceReader::new(&hex!("C003020100")).unwrap(); - let field = Private::<0, u8>::decode_optional(&mut reader) + let field = PrivateExplicit::<0, u8>::decode_optional(&mut reader) .unwrap() .unwrap(); @@ -141,7 +135,7 @@ mod tests { #[test] fn private_not_skipping_unknown_field() { let mut reader = SliceReader::new(&hex!("E003020100E103020101")).unwrap(); - let field = Private::<1, u8>::decode_optional(&mut reader).unwrap(); + let field = PrivateExplicit::<1, u8>::decode_optional(&mut reader).unwrap(); assert_eq!(field, None); } @@ -149,7 +143,7 @@ mod tests { fn private_returns_none_on_unequal_tag_number() { let mut reader = SliceReader::new(&hex!("C103020101")).unwrap(); assert_eq!( - Private::<0, u8>::decode_optional(&mut reader).unwrap(), + PrivateExplicit::<0, u8>::decode_optional(&mut reader).unwrap(), None ); } diff --git a/der/src/reader.rs b/der/src/reader.rs index b67779213..91e0f5aa9 100644 --- a/der/src/reader.rs +++ b/der/src/reader.rs @@ -5,7 +5,7 @@ pub(crate) mod pem; pub(crate) mod slice; use crate::{ - asn1::{AnyCustomClassExplicit, AnyCustomClassImplicit, ContextSpecific}, + asn1::{AnyCustomClassExplicit, AnyCustomClassImplicit, ContextSpecificExplicit}, Class, Decode, DecodeValue, Encode, EncodingRules, Error, ErrorKind, FixedTag, Header, Length, Tag, TagMode, TagNumber, };