summaryrefslogtreecommitdiffstats
path: root/vendor/spki/src/algorithm.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/spki/src/algorithm.rs')
-rw-r--r--vendor/spki/src/algorithm.rs131
1 files changed, 95 insertions, 36 deletions
diff --git a/vendor/spki/src/algorithm.rs b/vendor/spki/src/algorithm.rs
index 2a8b6c7f9..b0e08b5b3 100644
--- a/vendor/spki/src/algorithm.rs
+++ b/vendor/spki/src/algorithm.rs
@@ -2,8 +2,14 @@
use crate::{Error, Result};
use core::cmp::Ordering;
-use der::asn1::{AnyRef, ObjectIdentifier};
-use der::{Decode, DecodeValue, DerOrd, Encode, Header, Reader, Sequence, ValueOrd};
+use der::{
+ asn1::{AnyRef, Choice, ObjectIdentifier},
+ Decode, DecodeValue, DerOrd, Encode, EncodeValue, Header, Length, Reader, Sequence, ValueOrd,
+ Writer,
+};
+
+#[cfg(feature = "alloc")]
+use der::asn1::Any;
/// X.509 `AlgorithmIdentifier` as defined in [RFC 5280 Section 4.1.1.2].
///
@@ -14,17 +20,79 @@ use der::{Decode, DecodeValue, DerOrd, Encode, Header, Reader, Sequence, ValueOr
/// ```
///
/// [RFC 5280 Section 4.1.1.2]: https://tools.ietf.org/html/rfc5280#section-4.1.1.2
+#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
-pub struct AlgorithmIdentifier<'a> {
+pub struct AlgorithmIdentifier<Params> {
/// Algorithm OID, i.e. the `algorithm` field in the `AlgorithmIdentifier`
/// ASN.1 schema.
pub oid: ObjectIdentifier,
/// Algorithm `parameters`.
- pub parameters: Option<AnyRef<'a>>,
+ pub parameters: Option<Params>,
+}
+
+impl<'a, Params> DecodeValue<'a> for AlgorithmIdentifier<Params>
+where
+ Params: Choice<'a>,
+{
+ fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
+ reader.read_nested(header.length, |reader| {
+ Ok(Self {
+ oid: reader.decode()?,
+ parameters: reader.decode()?,
+ })
+ })
+ }
+}
+
+impl<Params> EncodeValue for AlgorithmIdentifier<Params>
+where
+ Params: Encode,
+{
+ fn value_len(&self) -> der::Result<Length> {
+ self.oid.encoded_len()? + self.parameters.encoded_len()?
+ }
+
+ fn encode_value(&self, writer: &mut impl Writer) -> der::Result<()> {
+ self.oid.encode(writer)?;
+ self.parameters.encode(writer)?;
+ Ok(())
+ }
+}
+
+impl<'a, Params> Sequence<'a> for AlgorithmIdentifier<Params> where Params: Choice<'a> + Encode {}
+
+impl<'a, Params> TryFrom<&'a [u8]> for AlgorithmIdentifier<Params>
+where
+ Params: Choice<'a> + Encode,
+{
+ type Error = Error;
+
+ fn try_from(bytes: &'a [u8]) -> Result<Self> {
+ Ok(Self::from_der(bytes)?)
+ }
+}
+
+impl<Params> ValueOrd for AlgorithmIdentifier<Params>
+where
+ Params: DerOrd,
+{
+ fn value_cmp(&self, other: &Self) -> der::Result<Ordering> {
+ match self.oid.der_cmp(&other.oid)? {
+ Ordering::Equal => self.parameters.der_cmp(&other.parameters),
+ other => Ok(other),
+ }
+ }
}
-impl<'a> AlgorithmIdentifier<'a> {
+/// `AlgorithmIdentifier` reference which has `AnyRef` parameters.
+pub type AlgorithmIdentifierRef<'a> = AlgorithmIdentifier<AnyRef<'a>>;
+
+/// `AlgorithmIdentifier` reference which has `Any` parameters.
+#[cfg(feature = "alloc")]
+pub type AlgorithmIdentifierOwned = AlgorithmIdentifier<Any>;
+
+impl<Params> AlgorithmIdentifier<Params> {
/// Assert the `algorithm` OID is an expected value.
pub fn assert_algorithm_oid(&self, expected_oid: ObjectIdentifier) -> Result<ObjectIdentifier> {
if self.oid == expected_oid {
@@ -33,7 +101,9 @@ impl<'a> AlgorithmIdentifier<'a> {
Err(Error::OidUnknown { oid: expected_oid })
}
}
+}
+impl<'a> AlgorithmIdentifierRef<'a> {
/// Assert `parameters` is an OID and has the expected value.
pub fn assert_parameters_oid(
&self,
@@ -87,46 +157,35 @@ impl<'a> AlgorithmIdentifier<'a> {
None => None,
Some(p) => match p {
AnyRef::NULL => None,
- _ => Some(p.oid()?),
+ _ => Some(p.decode_as::<ObjectIdentifier>()?),
},
},
))
}
}
-impl<'a> DecodeValue<'a> for AlgorithmIdentifier<'a> {
- fn decode_value<R: Reader<'a>>(reader: &mut R, header: Header) -> der::Result<Self> {
- reader.read_nested(header.length, |reader| {
- Ok(Self {
- oid: reader.decode()?,
- parameters: reader.decode()?,
- })
- })
- }
-}
-
-impl<'a> Sequence<'a> for AlgorithmIdentifier<'a> {
- fn fields<F, T>(&self, f: F) -> der::Result<T>
- where
- F: FnOnce(&[&dyn Encode]) -> der::Result<T>,
- {
- f(&[&self.oid, &self.parameters])
- }
-}
+#[cfg(feature = "alloc")]
+mod allocating {
+ use super::*;
+ use der::referenced::*;
-impl<'a> TryFrom<&'a [u8]> for AlgorithmIdentifier<'a> {
- type Error = Error;
-
- fn try_from(bytes: &'a [u8]) -> Result<Self> {
- Ok(Self::from_der(bytes)?)
+ impl<'a> RefToOwned<'a> for AlgorithmIdentifierRef<'a> {
+ type Owned = AlgorithmIdentifierOwned;
+ fn ref_to_owned(&self) -> Self::Owned {
+ AlgorithmIdentifier {
+ oid: self.oid,
+ parameters: self.parameters.ref_to_owned(),
+ }
+ }
}
-}
-impl ValueOrd for AlgorithmIdentifier<'_> {
- fn value_cmp(&self, other: &Self) -> der::Result<Ordering> {
- match self.oid.der_cmp(&other.oid)? {
- Ordering::Equal => self.parameters.der_cmp(&other.parameters),
- other => Ok(other),
+ impl OwnedToRef for AlgorithmIdentifierOwned {
+ type Borrowed<'a> = AlgorithmIdentifierRef<'a>;
+ fn owned_to_ref(&self) -> Self::Borrowed<'_> {
+ AlgorithmIdentifier {
+ oid: self.oid,
+ parameters: self.parameters.owned_to_ref(),
+ }
}
}
}