1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#[cfg(feature = "sha1")]
use sha1::{digest::Output, Digest};

use crate::{dkim::Canonicalization, Result};

use super::headers::{Writable, Writer};

#[cfg(feature = "rust-crypto")]
mod rust_crypto;
#[cfg(feature = "rust-crypto")]
pub use rust_crypto::{Ed25519Key, RsaKey};
#[cfg(feature = "rust-crypto")]
pub(crate) use rust_crypto::{Ed25519PublicKey, RsaPublicKey};

#[cfg(all(feature = "ring", not(feature = "rust-crypto")))]
mod ring_impls;
#[cfg(all(feature = "ring", not(feature = "rust-crypto")))]
pub use ring_impls::{Ed25519Key, RsaKey};
#[cfg(all(feature = "ring", not(feature = "rust-crypto")))]
pub(crate) use ring_impls::{Ed25519PublicKey, RsaPublicKey};

pub trait SigningKey {
    type Hasher: HashImpl;

    fn sign(&self, input: impl Writable) -> Result<Vec<u8>>;

    fn hash(&self, data: impl Writable) -> HashOutput {
        let mut hasher = <Self::Hasher as HashImpl>::hasher();
        data.write(&mut hasher);
        hasher.complete()
    }

    fn algorithm(&self) -> Algorithm;
}

pub trait VerifyingKey {
    fn verify<'a>(
        &self,
        headers: &mut dyn Iterator<Item = (&'a [u8], &'a [u8])>,
        signature: &[u8],
        canonicalication: Canonicalization,
        algorithm: Algorithm,
    ) -> Result<()>;
}

pub(crate) enum VerifyingKeyType {
    Rsa,
    Ed25519,
}

impl VerifyingKeyType {
    pub(crate) fn verifying_key(
        &self,
        bytes: &[u8],
    ) -> Result<Box<dyn VerifyingKey + Send + Sync>> {
        match self {
            #[cfg(feature = "rust-crypto")]
            Self::Rsa => RsaPublicKey::verifying_key_from_bytes(bytes),
            #[cfg(feature = "rust-crypto")]
            Self::Ed25519 => Ed25519PublicKey::verifying_key_from_bytes(bytes),
            #[cfg(all(feature = "ring", not(feature = "rust-crypto")))]
            Self::Rsa => RsaPublicKey::verifying_key_from_bytes(bytes),
            #[cfg(all(feature = "ring", not(feature = "rust-crypto")))]
            Self::Ed25519 => Ed25519PublicKey::verifying_key_from_bytes(bytes),
        }
    }
}

pub trait HashContext: Writer + Sized {
    fn complete(self) -> HashOutput;
}

pub trait HashImpl {
    type Context: HashContext;

    fn hasher() -> Self::Context;
}

#[derive(Clone, Copy)]
pub struct Sha1;

#[derive(Clone, Copy)]
pub struct Sha256;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u64)]
pub enum HashAlgorithm {
    Sha1 = R_HASH_SHA1,
    Sha256 = R_HASH_SHA256,
}

impl HashAlgorithm {
    pub fn hash(&self, data: impl Writable) -> HashOutput {
        match self {
            #[cfg(feature = "sha1")]
            Self::Sha1 => {
                let mut hasher = sha1::Sha1::new();
                data.write(&mut hasher);
                HashOutput::RustCryptoSha1(hasher.finalize())
            }
            #[cfg(feature = "sha2")]
            Self::Sha256 => {
                let mut hasher = sha2::Sha256::new();
                data.write(&mut hasher);
                HashOutput::RustCryptoSha256(hasher.finalize())
            }
            #[cfg(all(feature = "ring", not(feature = "sha1")))]
            Self::Sha1 => {
                let mut hasher =
                    ring::digest::Context::new(&ring::digest::SHA1_FOR_LEGACY_USE_ONLY);
                data.write(&mut hasher);
                HashOutput::Ring(hasher.finish())
            }
            #[cfg(all(feature = "ring", not(feature = "sha2")))]
            Self::Sha256 => {
                let mut hasher = ring::digest::Context::new(&ring::digest::SHA256);
                data.write(&mut hasher);
                HashOutput::Ring(hasher.finish())
            }
        }
    }
}

#[non_exhaustive]
pub enum HashOutput {
    #[cfg(feature = "ring")]
    Ring(ring::digest::Digest),
    #[cfg(feature = "sha1")]
    RustCryptoSha1(Output<sha1::Sha1>),
    #[cfg(feature = "sha2")]
    RustCryptoSha256(Output<sha2::Sha256>),
}

impl AsRef<[u8]> for HashOutput {
    fn as_ref(&self) -> &[u8] {
        match self {
            #[cfg(feature = "ring")]
            Self::Ring(output) => output.as_ref(),
            #[cfg(feature = "sha1")]
            Self::RustCryptoSha1(output) => output.as_ref(),
            #[cfg(feature = "sha2")]
            Self::RustCryptoSha256(output) => output.as_ref(),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Algorithm {
    RsaSha1,
    #[default]
    RsaSha256,
    Ed25519Sha256,
}

pub(crate) const R_HASH_SHA1: u64 = 0x01;
pub(crate) const R_HASH_SHA256: u64 = 0x02;