image4_pki/
sig.rs

1use der::asn1::BitString;
2use p256::ecdsa::Signature as EcP256Signature;
3use p384::ecdsa::Signature as EcP384Signature;
4use rsa::pkcs1v15::Signature as RsaSignature;
5use signature::SignatureEncoding;
6use spki::SignatureBitStringEncoding;
7
8/// An opaque signature type that may contain either RSA or ECDSA (P-256 and P-384) signatures.
9#[derive(Clone, Debug)]
10pub struct Signature(Box<[u8]>);
11
12impl From<RsaSignature> for Signature {
13    fn from(value: RsaSignature) -> Self {
14        Self(value.to_vec().into_boxed_slice())
15    }
16}
17
18impl From<EcP256Signature> for Signature {
19    fn from(value: EcP256Signature) -> Self {
20        Self(value.to_der().to_bytes())
21    }
22}
23
24impl From<EcP384Signature> for Signature {
25    fn from(value: EcP384Signature) -> Self {
26        Self(value.to_der().to_bytes())
27    }
28}
29
30impl TryFrom<&'_ [u8]> for Signature {
31    type Error = signature::Error;
32
33    fn try_from(value: &'_ [u8]) -> Result<Self, Self::Error> {
34        Ok(Self(value.into()))
35    }
36}
37
38impl TryFrom<Signature> for Box<[u8]> {
39    type Error = signature::Error;
40
41    fn try_from(value: Signature) -> Result<Self, Self::Error> {
42        Ok(value.0)
43    }
44}
45
46impl SignatureEncoding for Signature {
47    type Repr = Box<[u8]>;
48}
49
50impl SignatureBitStringEncoding for Signature {
51    fn to_bitstring(&self) -> der::Result<BitString> {
52        BitString::from_bytes(&self.0)
53    }
54}