use crate::{AlgorithmIdentifier, Error, Result, SubjectPublicKeyInfoRef};
use der::{EncodeValue, Tagged};
#[cfg(feature = "alloc")]
use {
crate::AlgorithmIdentifierOwned,
der::{asn1::BitString, Any, Document},
};
#[cfg(feature = "pem")]
use {
alloc::string::String,
der::pem::{LineEnding, PemLabel},
};
#[cfg(feature = "std")]
use std::path::Path;
#[cfg(doc)]
use crate::SubjectPublicKeyInfo;
pub trait DecodePublicKey: Sized {
fn from_public_key_der(bytes: &[u8]) -> Result<Self>;
#[cfg(feature = "pem")]
fn from_public_key_pem(s: &str) -> Result<Self> {
let (label, doc) = Document::from_pem(s)?;
SubjectPublicKeyInfoRef::validate_pem_label(label)?;
Self::from_public_key_der(doc.as_bytes())
}
#[cfg(feature = "std")]
fn read_public_key_der_file(path: impl AsRef<Path>) -> Result<Self> {
let doc = Document::read_der_file(path)?;
Self::from_public_key_der(doc.as_bytes())
}
#[cfg(all(feature = "pem", feature = "std"))]
fn read_public_key_pem_file(path: impl AsRef<Path>) -> Result<Self> {
let (label, doc) = Document::read_pem_file(path)?;
SubjectPublicKeyInfoRef::validate_pem_label(&label)?;
Self::from_public_key_der(doc.as_bytes())
}
}
impl<T> DecodePublicKey for T
where
T: for<'a> TryFrom<SubjectPublicKeyInfoRef<'a>, Error = Error>,
{
fn from_public_key_der(bytes: &[u8]) -> Result<Self> {
Self::try_from(SubjectPublicKeyInfoRef::try_from(bytes)?)
}
}
#[cfg(feature = "alloc")]
pub trait EncodePublicKey {
fn to_public_key_der(&self) -> Result<Document>;
#[cfg(feature = "pem")]
fn to_public_key_pem(&self, line_ending: LineEnding) -> Result<String> {
let doc = self.to_public_key_der()?;
Ok(doc.to_pem(SubjectPublicKeyInfoRef::PEM_LABEL, line_ending)?)
}
#[cfg(feature = "std")]
fn write_public_key_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
Ok(self.to_public_key_der()?.write_der_file(path)?)
}
#[cfg(all(feature = "pem", feature = "std"))]
fn write_public_key_pem_file(
&self,
path: impl AsRef<Path>,
line_ending: LineEnding,
) -> Result<()> {
let doc = self.to_public_key_der()?;
Ok(doc.write_pem_file(path, SubjectPublicKeyInfoRef::PEM_LABEL, line_ending)?)
}
}
pub trait AssociatedAlgorithmIdentifier {
type Params: Tagged + EncodeValue;
const ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;
}
#[cfg(feature = "alloc")]
pub trait DynAssociatedAlgorithmIdentifier {
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
}
#[cfg(feature = "alloc")]
impl<T> DynAssociatedAlgorithmIdentifier for T
where
T: AssociatedAlgorithmIdentifier,
{
fn algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned> {
Ok(AlgorithmIdentifierOwned {
oid: T::ALGORITHM_IDENTIFIER.oid,
parameters: T::ALGORITHM_IDENTIFIER
.parameters
.as_ref()
.map(Any::encode_from)
.transpose()?,
})
}
}
pub trait SignatureAlgorithmIdentifier {
type Params: Tagged + EncodeValue;
const SIGNATURE_ALGORITHM_IDENTIFIER: AlgorithmIdentifier<Self::Params>;
}
#[cfg(feature = "alloc")]
pub trait DynSignatureAlgorithmIdentifier {
fn signature_algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned>;
}
#[cfg(feature = "alloc")]
impl<T> DynSignatureAlgorithmIdentifier for T
where
T: SignatureAlgorithmIdentifier,
{
fn signature_algorithm_identifier(&self) -> Result<AlgorithmIdentifierOwned> {
Ok(AlgorithmIdentifierOwned {
oid: T::SIGNATURE_ALGORITHM_IDENTIFIER.oid,
parameters: T::SIGNATURE_ALGORITHM_IDENTIFIER
.parameters
.as_ref()
.map(Any::encode_from)
.transpose()?,
})
}
}
#[cfg(feature = "alloc")]
pub trait SignatureBitStringEncoding {
fn to_bitstring(&self) -> der::Result<BitString>;
}