Skip to content
Merged
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
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>,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debatably we could get rid of this bound by using Any::encode_msg, however that's fallible so we'd have to add an expect and panic on encoding failure.

{
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),
}
}
}