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
85 changes: 20 additions & 65 deletions der/src/asn1/any.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
//! ASN.1 `ANY` type.

#![cfg_attr(feature = "arbitrary", allow(clippy::integer_arithmetic))]

use crate::{
asn1::*, BytesRef, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind,
FixedTag, Header, Length, Reader, Result, SliceReader, Tag, Tagged, ValueOrd, Writer,
BytesRef, Choice, Decode, DecodeValue, DerOrd, EncodeValue, Error, ErrorKind, Header, Length,
Reader, Result, SliceReader, Tag, Tagged, ValueOrd, Writer,
};
use core::cmp::Ordering;

#[cfg(feature = "alloc")]
use {crate::BytesOwned, alloc::boxed::Box};

#[cfg(feature = "oid")]
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.
Expand Down Expand Up @@ -58,11 +56,14 @@ impl<'a> AnyRef<'a> {
}

/// Attempt to decode this [`AnyRef`] type into the inner value.
pub fn decode_into<T>(self) -> Result<T>
pub fn decode_as<T>(self) -> Result<T>
where
T: DecodeValue<'a> + FixedTag,
T: Choice<'a> + DecodeValue<'a>,
{
self.tag.assert_eq(T::TAG)?;
if !T::can_decode(self.tag) {
return Err(self.tag.unexpected_error(None));
}

let header = Header {
tag: self.tag,
length: self.value.len(),
Expand All @@ -78,48 +79,6 @@ impl<'a> AnyRef<'a> {
self == Self::NULL
}

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

/// Attempt to decode an ASN.1 `CONTEXT-SPECIFIC` field.
pub fn context_specific<T>(self) -> Result<ContextSpecific<T>>
where
T: Decode<'a>,
{
self.try_into()
}

/// Attempt to decode an ASN.1 `GeneralizedTime`.
pub fn generalized_time(self) -> Result<GeneralizedTime> {
self.try_into()
}

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

/// Attempt to decode an ASN.1 `OBJECT IDENTIFIER`.
#[cfg(feature = "oid")]
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
pub fn oid(self) -> Result<ObjectIdentifier> {
self.try_into()
}

/// Attempt to decode an ASN.1 `OPTIONAL` value.
pub fn optional<T>(self) -> Result<Option<T>>
where
T: Choice<'a> + TryFrom<Self, Error = Error>,
{
if T::can_decode(self.tag) {
T::try_from(self).map(Some)
} else {
Ok(None)
}
}

/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
/// nested reader and calling the provided argument with it.
pub fn sequence<F, T>(self, f: F) -> Result<T>
Expand All @@ -131,11 +90,6 @@ impl<'a> AnyRef<'a> {
let result = f(&mut reader)?;
reader.finish(result)
}

/// Attempt to decode an ASN.1 `UTCTime`.
pub fn utc_time(self) -> Result<UtcTime> {
self.try_into()
}
}

impl<'a> Choice<'a> for AnyRef<'a> {
Expand Down Expand Up @@ -231,19 +185,20 @@ impl Any {
}

/// Attempt to decode this [`Any`] type into the inner value.
pub fn decode_into<'a, T>(&'a self) -> Result<T>
pub fn decode_as<'a, T>(&'a self) -> Result<T>
where
T: DecodeValue<'a> + FixedTag,
T: Choice<'a> + DecodeValue<'a>,
{
self.tag.assert_eq(T::TAG)?;
let header = Header {
tag: self.tag,
length: self.value.len(),
};
AnyRef::from(self).decode_as()
}

let mut decoder = SliceReader::new(self.value.as_slice())?;
let result = T::decode_value(&mut decoder, header)?;
decoder.finish(result)
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
/// nested reader and calling the provided argument with it.
pub fn sequence<'a, F, T>(&'a self, f: F) -> Result<T>
where
F: FnOnce(&mut SliceReader<'a>) -> Result<T>,
{
AnyRef::from(self).sequence(f)
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/bit_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl<'a> TryFrom<AnyRef<'a>> for BitStringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<BitStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/generalized_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl TryFrom<AnyRef<'_>> for GeneralizedTime {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<GeneralizedTime> {
any.decode_into()
any.decode_as()
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ macro_rules! impl_int_encoding {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<Self> {
any.decode_into()
any.decode_as()
}
}
)+
Expand Down Expand Up @@ -113,7 +113,7 @@ macro_rules! impl_uint_encoding {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<Self> {
any.decode_into()
any.decode_as()
}
}
)+
Expand Down
8 changes: 4 additions & 4 deletions der/src/asn1/integer/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'a> TryFrom<AnyRef<'a>> for IntRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<IntRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down Expand Up @@ -167,7 +167,7 @@ impl<'a> TryFrom<AnyRef<'a>> for UintRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<UintRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ mod allocating {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<Int> {
any.decode_into()
any.decode_as()
}
}

Expand Down Expand Up @@ -372,7 +372,7 @@ mod allocating {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<Uint> {
any.decode_into()
any.decode_as()
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/internal_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ macro_rules! impl_string_type {
type Error = Error;

fn try_from(any: &'__der Any) -> Result<$type> {
any.decode_into()
any.decode_as()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl TryFrom<AnyRef<'_>> for Null {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<Null> {
any.decode_into()
any.decode_as()
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a> TryFrom<AnyRef<'a>> for OctetStringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<OctetStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/printable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'a> TryFrom<AnyRef<'a>> for PrintableStringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<PrintableStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down Expand Up @@ -206,7 +206,7 @@ mod allocation {
type Error = Error;

fn try_from(any: &AnyRef<'a>) -> Result<PrintableString> {
(*any).decode_into()
(*any).decode_as()
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/teletex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a> TryFrom<AnyRef<'a>> for TeletexStringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<TeletexStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}
impl<'a> From<TeletexStringRef<'a>> for AnyRef<'a> {
Expand Down Expand Up @@ -164,7 +164,7 @@ mod allocation {
type Error = Error;

fn try_from(any: &AnyRef<'a>) -> Result<TeletexString> {
(*any).decode_into()
(*any).decode_as()
}
}

Expand Down
2 changes: 1 addition & 1 deletion der/src/asn1/utc_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl TryFrom<AnyRef<'_>> for UtcTime {
type Error = Error;

fn try_from(any: AnyRef<'_>) -> Result<UtcTime> {
any.decode_into()
any.decode_as()
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/utf8_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl<'a> TryFrom<AnyRef<'a>> for Utf8StringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<Utf8StringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand All @@ -103,7 +103,7 @@ impl<'a> TryFrom<&'a Any> for Utf8StringRef<'a> {
type Error = Error;

fn try_from(any: &'a Any) -> Result<Utf8StringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down
4 changes: 2 additions & 2 deletions der/src/asn1/videotex_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<'a> TryFrom<AnyRef<'a>> for VideotexStringRef<'a> {
type Error = Error;

fn try_from(any: AnyRef<'a>) -> Result<VideotexStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand All @@ -110,7 +110,7 @@ impl<'a> TryFrom<&'a Any> for VideotexStringRef<'a> {
type Error = Error;

fn try_from(any: &'a Any) -> Result<VideotexStringRef<'a>> {
any.decode_into()
any.decode_as()
}
}

Expand Down
8 changes: 6 additions & 2 deletions pkcs1/tests/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ fn decode_oaep_param() {
.assert_algorithm_oid(db::rfc5912::ID_P_SPECIFIED)
.is_ok());
assert_eq!(
param.p_source.parameters_any().unwrap().octet_string(),
param
.p_source
.parameters_any()
.unwrap()
.decode_as::<OctetStringRef<'_>>(),
OctetStringRef::new(&[0xab, 0xcd, 0xef])
);
}
Expand Down Expand Up @@ -147,7 +151,7 @@ fn decode_oaep_param_default() {
.p_source
.parameters_any()
.unwrap()
.octet_string()
.decode_as::<OctetStringRef<'_>>()
.unwrap()
.is_empty(),);
assert_eq!(param, Default::default())
Expand Down
2 changes: 1 addition & 1 deletion pkcs5/src/pbes2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ impl<'a> TryFrom<AlgorithmIdentifierRef<'a>> for EncryptionScheme<'a> {
fn try_from(alg: AlgorithmIdentifierRef<'a>) -> der::Result<Self> {
// TODO(tarcieri): support for non-AES algorithms?
let iv = match alg.parameters {
Some(params) => params.octet_string()?.as_bytes(),
Some(params) => params.decode_as::<OctetStringRef<'a>>()?.as_bytes(),
None => return Err(Tag::OctetString.value_error()),
};

Expand Down
4 changes: 2 additions & 2 deletions pkcs7/tests/content_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn decode_signed_mdm_example() {
signer_infos: _,
})) => {
let _content = content
.decode_into::<SequenceRef>()
.decode_as::<SequenceRef>()
.expect("Content should be in the correct format: SequenceRef");
}
_ => panic!("expected ContentInfo::SignedData(Some(_))"),
Expand Down Expand Up @@ -132,7 +132,7 @@ fn decode_signed_scep_example() {
signer_infos: _,
})) => {
let _content = content
.decode_into::<OctetStringRef>()
.decode_as::<OctetStringRef>()
.expect("Content should be in the correct format: OctetStringRef");

assert_eq!(ver, CmsVersion::V1)
Expand Down
7 changes: 6 additions & 1 deletion pkcs8/tests/private_key.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! PKCS#8 private key tests

use der::asn1::ObjectIdentifier;
use hex_literal::hex;
use pkcs8::{PrivateKeyInfo, Version};

Expand Down Expand Up @@ -48,7 +49,11 @@ fn decode_ec_p256_der() {
assert_eq!(pk.algorithm.oid, "1.2.840.10045.2.1".parse().unwrap());

assert_eq!(
pk.algorithm.parameters.unwrap().oid().unwrap(),
pk.algorithm
.parameters
.unwrap()
.decode_as::<ObjectIdentifier>()
.unwrap(),
"1.2.840.10045.3.1.7".parse().unwrap()
);

Expand Down
2 changes: 1 addition & 1 deletion spki/src/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<'a> AlgorithmIdentifierRef<'a> {
None => None,
Some(p) => match p {
AnyRef::NULL => None,
_ => Some(p.oid()?),
_ => Some(p.decode_as::<ObjectIdentifier>()?),
},
},
))
Expand Down
Loading