From fd069a6ff7a9f5abfd3d797b51f50ca0a5307e44 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 2 Apr 2023 14:34:14 -0600 Subject: [PATCH] spki: add `AssociatedAlgorithmIdentifier::Params` type (#966) Adds an associated `Params` type which is passed as `AlgorithmIdentifier`, 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. --- spki/src/traits.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/spki/src/traits.rs b/spki/src/traits.rs index 6838a07d4..73e8c1b88 100644 --- a/spki/src/traits.rs +++ b/spki/src/traits.rs @@ -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")] @@ -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; } /// Returns `AlgorithmIdentifier` associated with the structure. @@ -115,8 +118,15 @@ pub trait DynAssociatedAlgorithmIdentifier { } #[cfg(feature = "alloc")] -impl DynAssociatedAlgorithmIdentifier for T { +impl DynAssociatedAlgorithmIdentifier for T +where + T: AssociatedAlgorithmIdentifier, + T::Params: Into, +{ 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), + } } }