image4-pki 0.1.0

An experimantal crate containing implementation of assymetric crypto primitives usable with the image4 crate.
Documentation
use der::asn1::BitString;
use p256::ecdsa::Signature as EcP256Signature;
use p384::ecdsa::Signature as EcP384Signature;
use rsa::pkcs1v15::Signature as RsaSignature;
use signature::SignatureEncoding;
use spki::SignatureBitStringEncoding;

/// An opaque signature type that may contain either RSA or ECDSA (P-256 and P-384) signatures.
#[derive(Clone, Debug)]
pub struct Signature(Box<[u8]>);

impl From<RsaSignature> for Signature {
    fn from(value: RsaSignature) -> Self {
        Self(value.to_vec().into_boxed_slice())
    }
}

impl From<EcP256Signature> for Signature {
    fn from(value: EcP256Signature) -> Self {
        Self(value.to_der().to_bytes())
    }
}

impl From<EcP384Signature> for Signature {
    fn from(value: EcP384Signature) -> Self {
        Self(value.to_der().to_bytes())
    }
}

impl TryFrom<&'_ [u8]> for Signature {
    type Error = signature::Error;

    fn try_from(value: &'_ [u8]) -> Result<Self, Self::Error> {
        Ok(Self(value.into()))
    }
}

impl TryFrom<Signature> for Box<[u8]> {
    type Error = signature::Error;

    fn try_from(value: Signature) -> Result<Self, Self::Error> {
        Ok(value.0)
    }
}

impl SignatureEncoding for Signature {
    type Repr = Box<[u8]>;
}

impl SignatureBitStringEncoding for Signature {
    fn to_bitstring(&self) -> der::Result<BitString> {
        BitString::from_bytes(&self.0)
    }
}