diff --git a/.github/workflows/base16ct.yml b/.github/workflows/base16ct.yml index 56405dfbe..22d58a6f1 100644 --- a/.github/workflows/base16ct.yml +++ b/.github/workflows/base16ct.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -49,7 +49,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/base32ct.yml b/.github/workflows/base32ct.yml index 36bbdf111..0d5fe70ce 100644 --- a/.github/workflows/base32ct.yml +++ b/.github/workflows/base32ct.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -49,7 +49,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/const-oid.yml b/.github/workflows/const-oid.yml index 3fa3f5fea..64676a646 100644 --- a/.github/workflows/const-oid.yml +++ b/.github/workflows/const-oid.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -49,7 +49,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/der.yml b/.github/workflows/der.yml index 1b9dd849e..f99fe2cad 100644 --- a/.github/workflows/der.yml +++ b/.github/workflows/der.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -52,7 +52,7 @@ jobs: # 32-bit Linux - target: i686-unknown-linux-gnu platform: ubuntu-latest - rust: 1.60.0 # MSRV + rust: 1.65.0 # MSRV deps: sudo apt update && sudo apt install gcc-multilib - target: i686-unknown-linux-gnu platform: ubuntu-latest @@ -62,7 +62,7 @@ jobs: # 64-bit Linux - target: x86_64-unknown-linux-gnu platform: ubuntu-latest - rust: 1.60.0 # MSRV + rust: 1.65.0 # MSRV - target: x86_64-unknown-linux-gnu platform: ubuntu-latest rust: stable @@ -71,7 +71,7 @@ jobs: # 64-bit Windows #- target: x86_64-pc-windows-msvc # platform: windows-latest - # rust: 1.60.0 # MSRV + # rust: 1.65.0 # MSRV #- target: x86_64-pc-windows-msvc # platform: windows-latest # rust: stable @@ -93,7 +93,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/pem-rfc7468.yml b/.github/workflows/pem-rfc7468.yml index b2d9853d5..8a13269aa 100644 --- a/.github/workflows/pem-rfc7468.yml +++ b/.github/workflows/pem-rfc7468.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -50,7 +50,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/pkcs1.yml b/.github/workflows/pkcs1.yml index 313cd503e..11bf4d36a 100644 --- a/.github/workflows/pkcs1.yml +++ b/.github/workflows/pkcs1.yml @@ -27,7 +27,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -53,7 +53,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/pkcs5.yml b/.github/workflows/pkcs5.yml index 287906144..65afa20ed 100644 --- a/.github/workflows/pkcs5.yml +++ b/.github/workflows/pkcs5.yml @@ -26,7 +26,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -52,7 +52,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/pkcs7.yml b/.github/workflows/pkcs7.yml index c17c3c2aa..a572bacd9 100644 --- a/.github/workflows/pkcs7.yml +++ b/.github/workflows/pkcs7.yml @@ -25,7 +25,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -51,7 +51,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/pkcs8.yml b/.github/workflows/pkcs8.yml index fb44bba60..bcddf24e8 100644 --- a/.github/workflows/pkcs8.yml +++ b/.github/workflows/pkcs8.yml @@ -28,7 +28,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -54,7 +54,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/sec1.yml b/.github/workflows/sec1.yml index 2ff267ad6..3b7ab8b88 100644 --- a/.github/workflows/sec1.yml +++ b/.github/workflows/sec1.yml @@ -27,7 +27,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -53,7 +53,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/serdect.yml b/.github/workflows/serdect.yml index ab94bc04f..0fab6292a 100644 --- a/.github/workflows/serdect.yml +++ b/.github/workflows/serdect.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -50,7 +50,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/spki.yml b/.github/workflows/spki.yml index 42febe6ba..bf0bc4419 100644 --- a/.github/workflows/spki.yml +++ b/.github/workflows/spki.yml @@ -26,7 +26,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -52,7 +52,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/tai64.yml b/.github/workflows/tai64.yml index db3f223e2..5b4d2bb46 100644 --- a/.github/workflows/tai64.yml +++ b/.github/workflows/tai64.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -49,7 +49,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/.github/workflows/tls_codec.yml b/.github/workflows/tls_codec.yml index 02b94a76a..060f5fd70 100644 --- a/.github/workflows/tls_codec.yml +++ b/.github/workflows/tls_codec.yml @@ -24,7 +24,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - wasm32-unknown-unknown @@ -52,7 +52,7 @@ jobs: include: # 32-bit Linux - target: i686-unknown-linux-gnu - rust: 1.60.0 # MSRV + rust: 1.65.0 # MSRV deps: sudo apt update && sudo apt install gcc-multilib - target: i686-unknown-linux-gnu rust: stable @@ -60,7 +60,7 @@ jobs: # 64-bit Linux - target: x86_64-unknown-linux-gnu - rust: 1.60.0 # MSRV + rust: 1.65.0 # MSRV - target: x86_64-unknown-linux-gnu rust: stable steps: diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml index 2c5a7f29b..53c7a4933 100644 --- a/.github/workflows/workspace.yml +++ b/.github/workflows/workspace.yml @@ -21,7 +21,7 @@ jobs: - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 with: - toolchain: 1.60.0 + toolchain: 1.65.0 components: clippy override: true profile: minimal diff --git a/.github/workflows/x509-cert.yml b/.github/workflows/x509-cert.yml index cb693bce3..70cacf336 100644 --- a/.github/workflows/x509-cert.yml +++ b/.github/workflows/x509-cert.yml @@ -26,7 +26,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable target: - thumbv7em-none-eabi @@ -52,7 +52,7 @@ jobs: strategy: matrix: rust: - - 1.60.0 # MSRV + - 1.65.0 # MSRV - stable steps: - uses: actions/checkout@v3 diff --git a/der/src/asn1/any.rs b/der/src/asn1/any.rs index 3c086c558..94157a56b 100644 --- a/der/src/asn1/any.rs +++ b/der/src/asn1/any.rs @@ -294,3 +294,26 @@ where } } } + +#[cfg(feature = "alloc")] +mod allocating { + use super::*; + use crate::referenced::*; + + impl<'a> RefToOwned<'a> for AnyRef<'a> { + type Owned = Any; + fn to_owned(&self) -> Self::Owned { + Any { + tag: self.tag(), + value: Bytes::from(self.value), + } + } + } + + impl OwnedToRef for Any { + type Borrowed<'a> = AnyRef<'a>; + fn to_ref(&self) -> Self::Borrowed<'_> { + self.into() + } + } +} diff --git a/der/src/asn1/bit_string.rs b/der/src/asn1/bit_string.rs index eed14e456..800f5f9d6 100644 --- a/der/src/asn1/bit_string.rs +++ b/der/src/asn1/bit_string.rs @@ -180,7 +180,7 @@ impl<'a> TryFrom<&&'a [u8]> for BitStringRef<'a> { type Error = Error; fn try_from(bytes: &&'a [u8]) -> Result> { - BitStringRef::from_bytes(*bytes) + BitStringRef::from_bytes(bytes) } } @@ -337,6 +337,31 @@ impl ValueOrd for BitString { } } +#[cfg(feature = "alloc")] +mod allocating { + use super::*; + use crate::referenced::*; + use alloc::vec::Vec; + + impl<'a> RefToOwned<'a> for BitStringRef<'a> { + type Owned = BitString; + fn to_owned(&self) -> Self::Owned { + BitString { + unused_bits: self.unused_bits, + bit_length: self.bit_length, + inner: Vec::from(self.inner.as_slice()), + } + } + } + + impl OwnedToRef for BitString { + type Borrowed<'a> = BitStringRef<'a>; + fn to_ref(&self) -> Self::Borrowed<'_> { + self.into() + } + } +} + /// Iterator over the bits of a [`BitString`]. pub struct BitStringIter<'a> { /// [`BitString`] being iterated over. diff --git a/der/src/asn1/integer.rs b/der/src/asn1/integer.rs index 20e2f018f..762eb7cdb 100644 --- a/der/src/asn1/integer.rs +++ b/der/src/asn1/integer.rs @@ -126,7 +126,7 @@ impl_uint_encoding!(u8, u16, u32, u64, u128); #[inline] fn is_highest_bit_set(bytes: &[u8]) -> bool { bytes - .get(0) + .first() .map(|byte| byte & 0b10000000 != 0) .unwrap_or(false) } diff --git a/der/src/asn1/integer/uint.rs b/der/src/asn1/integer/uint.rs index e45a72f2a..73ead53eb 100644 --- a/der/src/asn1/integer/uint.rs +++ b/der/src/asn1/integer/uint.rs @@ -75,7 +75,7 @@ pub(crate) fn strip_leading_zeroes(mut bytes: &[u8]) -> &[u8] { /// Does the given integer need a leading zero? fn needs_leading_zero(bytes: &[u8]) -> bool { - matches!(bytes.get(0), Some(byte) if *byte >= 0x80) + matches!(bytes.first(), Some(byte) if *byte >= 0x80) } #[cfg(test)] diff --git a/der/src/asn1/real.rs b/der/src/asn1/real.rs index f872d2d0b..fd5297e67 100644 --- a/der/src/asn1/real.rs +++ b/der/src/asn1/real.rs @@ -23,7 +23,7 @@ impl<'a> DecodeValue<'a> for f64 { Ok(0.0) } else if is_nth_bit_one::<7>(bytes) { // Binary encoding from section 8.5.7 applies - let sign: u64 = if is_nth_bit_one::<6>(bytes) { 1 } else { 0 }; + let sign: u64 = u64::from(is_nth_bit_one::<6>(bytes)); // Section 8.5.7.2: Check the base -- the DER specs say that only base 2 should be supported in DER let base = mnth_bits_to_u8::<5, 4>(bytes); @@ -204,7 +204,7 @@ impl FixedTag for f64 { pub(crate) fn is_nth_bit_one(bytes: &[u8]) -> bool { if N < 8 { bytes - .get(0) + .first() .map(|byte| byte & (1 << N) != 0) .unwrap_or(false) } else { diff --git a/der/src/lib.rs b/der/src/lib.rs index 4657e132c..51c394fa8 100644 --- a/der/src/lib.rs +++ b/der/src/lib.rs @@ -340,6 +340,7 @@ extern crate alloc; extern crate std; pub mod asn1; +pub mod referenced; pub(crate) mod arrayvec; mod byte_slice; diff --git a/der/src/reader/slice.rs b/der/src/reader/slice.rs index 6bab091f3..fe7560807 100644 --- a/der/src/reader/slice.rs +++ b/der/src/reader/slice.rs @@ -64,7 +64,7 @@ impl<'a> Reader<'a> for SliceReader<'a> { fn peek_byte(&self) -> Option { self.remaining() .ok() - .and_then(|bytes| bytes.get(0).cloned()) + .and_then(|bytes| bytes.first().cloned()) } fn peek_header(&self) -> Result
{ diff --git a/der/src/referenced.rs b/der/src/referenced.rs new file mode 100644 index 000000000..dc9a5f3af --- /dev/null +++ b/der/src/referenced.rs @@ -0,0 +1,47 @@ +//! A module for working with referenced data. + +/// A trait for borrowing data from an owned struct +pub trait OwnedToRef { + /// The resulting type referencing back to Self + type Borrowed<'a> + where + Self: 'a; + + /// Creates a new object referencing back to the self for storage + fn to_ref(&self) -> Self::Borrowed<'_>; +} + +/// A trait for cloning a referenced structure and getting owned objects +/// +/// This is the pendant to [`OwnedToRef`] +pub trait RefToOwned<'a> { + /// The resulting type after obtaining ownership. + type Owned: OwnedToRef = Self> + where + Self: 'a; + + /// Creates a new object taking ownership of the data + fn to_owned(&self) -> Self::Owned; +} + +impl OwnedToRef for Option +where + T: OwnedToRef, +{ + type Borrowed<'a> = Option> where T: 'a; + + fn to_ref(&self) -> Self::Borrowed<'_> { + self.as_ref().map(|o| o.to_ref()) + } +} + +impl<'a, T> RefToOwned<'a> for Option +where + T: RefToOwned<'a> + 'a, + T::Owned: OwnedToRef, +{ + type Owned = Option; + fn to_owned(&self) -> Self::Owned { + self.as_ref().map(|o| o.to_owned()) + } +} diff --git a/pkcs8/src/version.rs b/pkcs8/src/version.rs index 339368392..069f825c9 100644 --- a/pkcs8/src/version.rs +++ b/pkcs8/src/version.rs @@ -6,7 +6,7 @@ use der::{Decode, Encode, FixedTag, Reader, Tag, Writer}; /// Version identifier for PKCS#8 documents. /// /// (RFC 5958 designates `0` and `1` as the only valid versions for PKCS#8 documents) -#[derive(Clone, Debug, Copy, PartialEq)] +#[derive(Clone, Debug, Copy, PartialEq, Eq)] pub enum Version { /// Denotes PKCS#8 v1: no public key field. V1 = 0, diff --git a/spki/src/algorithm.rs b/spki/src/algorithm.rs index 5107ca58c..92a42746f 100644 --- a/spki/src/algorithm.rs +++ b/spki/src/algorithm.rs @@ -7,6 +7,9 @@ use der::{ Decode, DecodeValue, DerOrd, Encode, Header, Reader, Sequence, ValueOrd, }; +#[cfg(feature = "alloc")] +use der::asn1::Any; + /// X.509 `AlgorithmIdentifier` as defined in [RFC 5280 Section 4.1.1.2]. /// /// ```text @@ -78,6 +81,10 @@ where /// `AlgorithmIdentifier` reference which has `AnyRef` parameters. pub type AlgorithmIdentifierRef<'a> = AlgorithmIdentifier>; +/// `AlgorithmIdentifier` reference which has `Any` parameters. +#[cfg(feature = "alloc")] +pub type AlgorithmIdentifierOwned = AlgorithmIdentifier; + impl AlgorithmIdentifier { /// Assert the `algorithm` OID is an expected value. pub fn assert_algorithm_oid(&self, expected_oid: ObjectIdentifier) -> Result { @@ -149,3 +156,29 @@ impl<'a> AlgorithmIdentifierRef<'a> { )) } } + +#[cfg(feature = "alloc")] +mod allocating { + use super::*; + use der::referenced::*; + + impl<'a> RefToOwned<'a> for AlgorithmIdentifierRef<'a> { + type Owned = AlgorithmIdentifierOwned; + fn to_owned(&self) -> Self::Owned { + AlgorithmIdentifier { + oid: self.oid, + parameters: self.parameters.to_owned(), + } + } + } + + impl OwnedToRef for AlgorithmIdentifierOwned { + type Borrowed<'a> = AlgorithmIdentifierRef<'a>; + fn to_ref(&self) -> Self::Borrowed<'_> { + AlgorithmIdentifier { + oid: self.oid, + parameters: self.parameters.to_ref(), + } + } + } +} diff --git a/spki/src/lib.rs b/spki/src/lib.rs index 34f5b347e..ad883254c 100644 --- a/spki/src/lib.rs +++ b/spki/src/lib.rs @@ -50,7 +50,10 @@ pub use der::{self, asn1::ObjectIdentifier}; #[cfg(feature = "alloc")] pub use { - crate::{spki::SubjectPublicKeyInfoOwned, traits::EncodePublicKey}, + crate::{ + algorithm::AlgorithmIdentifierOwned, spki::SubjectPublicKeyInfoOwned, + traits::EncodePublicKey, + }, der::Document, }; diff --git a/spki/src/spki.rs b/spki/src/spki.rs index ee7494d4a..3ad8aa9cd 100644 --- a/spki/src/spki.rs +++ b/spki/src/spki.rs @@ -173,3 +173,29 @@ where impl PemLabel for SubjectPublicKeyInfo { const PEM_LABEL: &'static str = "PUBLIC KEY"; } + +#[cfg(feature = "alloc")] +mod allocating { + use super::*; + use der::referenced::*; + + impl<'a> RefToOwned<'a> for SubjectPublicKeyInfoRef<'a> { + type Owned = SubjectPublicKeyInfoOwned; + fn to_owned(&self) -> Self::Owned { + SubjectPublicKeyInfo { + algorithm: self.algorithm.to_owned(), + subject_public_key: self.subject_public_key.to_owned(), + } + } + } + + impl OwnedToRef for SubjectPublicKeyInfoOwned { + type Borrowed<'a> = SubjectPublicKeyInfoRef<'a>; + fn to_ref(&self) -> Self::Borrowed<'_> { + SubjectPublicKeyInfo { + algorithm: self.algorithm.to_ref(), + subject_public_key: self.subject_public_key.to_ref(), + } + } + } +} diff --git a/x509-cert/src/certificate.rs b/x509-cert/src/certificate.rs index 7cb19b9c1..0c2faf982 100644 --- a/x509-cert/src/certificate.rs +++ b/x509-cert/src/certificate.rs @@ -36,7 +36,7 @@ pub enum Version { impl ValueOrd for Version { fn value_cmp(&self, other: &Self) -> der::Result { - (&(*self as u8)).value_cmp(&(*other as u8)) + (*self as u8).value_cmp(&(*other as u8)) } }