Skip to content

Commit

Permalink
spki: add AssociatedAlgorithmIdentifier::Params type (RustCrypto#966)
Browse files Browse the repository at this point in the history
Adds an associated `Params` type which is passed as
`AlgorithmIdentifier<Params>`, which makes it *much* easier to define
`AlgorithmIdentifier` constants.

Without this, `const fn` type conversions are required to
`AnyRef<'static>`. This isn't currently possible for `ObjectIdentifier`
for example.
  • Loading branch information
tarcieri authored Apr 2, 2023
1 parent 59afffb commit fd069a6
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions spki/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Traits for encoding/decoding SPKI public keys.
use crate::{AlgorithmIdentifierRef, Error, Result, SubjectPublicKeyInfoRef};
use crate::{AlgorithmIdentifier, Error, Result, SubjectPublicKeyInfoRef};

#[cfg(feature = "alloc")]
use {
crate::AlgorithmIdentifierOwned,
der::{referenced::RefToOwned, Document},
der::{Any, Document},
};

#[cfg(feature = "pem")]
Expand Down Expand Up @@ -101,8 +101,11 @@ pub trait EncodePublicKey {
///
/// This is useful for e.g. keys for digital signature algorithms.
pub trait AssociatedAlgorithmIdentifier {
/// Algorithm parameters.
type Params: der::Encode;

/// `AlgorithmIdentifier` for this structure.
const ALGORITHM_IDENTIFIER: AlgorithmIdentifierRef<'static>;
const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;
}

/// Returns `AlgorithmIdentifier` associated with the structure.
Expand All @@ -115,8 +118,15 @@ pub trait DynAssociatedAlgorithmIdentifier {
}

#[cfg(feature = "alloc")]
impl<T: AssociatedAlgorithmIdentifier> DynAssociatedAlgorithmIdentifier for T {
impl<T> DynAssociatedAlgorithmIdentifier for T
where
T: AssociatedAlgorithmIdentifier,
T::Params: Into<Any>,
{
fn algorithm_identifier(&self) -> AlgorithmIdentifierOwned {
Self::ALGORITHM_IDENTIFIER.ref_to_owned()
AlgorithmIdentifierOwned {
oid: T::ALGORITHM_IDENTIFIER.oid,
parameters: T::ALGORITHM_IDENTIFIER.parameters.map(Into::into),
}
}
}

0 comments on commit fd069a6

Please sign in to comment.