kumo_dkim/
lib.rs

1// Implementation of DKIM: https://datatracker.ietf.org/doc/html/rfc6376
2
3use crate::errors::Status;
4use crate::hash::HeaderList;
5use crate::header::TaggedHeader;
6use dns_resolver::{HickoryResolver, Resolver};
7use ed25519_dalek::pkcs8::DecodePrivateKey;
8use ed25519_dalek::SigningKey;
9use mailparsing::AuthenticationResult;
10use openssl::md::Md;
11use openssl::pkey::PKey;
12use openssl::pkey_ctx::PkeyCtx;
13use openssl::rsa::{Padding, Rsa};
14use std::collections::BTreeMap;
15
16pub mod arc;
17pub mod canonicalization;
18mod errors;
19mod hash;
20mod header;
21mod parsed_email;
22mod parser;
23mod public_key;
24#[cfg(test)]
25mod roundtrip_test;
26mod sign;
27
28pub use errors::DKIMError;
29use header::{DKIMHeader, DKIM_SIGNATURE_HEADER_NAME};
30pub use parsed_email::ParsedEmail;
31pub use parser::{tag_list as parse_tag_list, Tag};
32pub use sign::{Signer, SignerBuilder};
33
34const DNS_NAMESPACE: &str = "_domainkey";
35
36#[derive(Debug)]
37pub(crate) enum DkimPublicKey {
38    Rsa(PKey<openssl::pkey::Public>),
39    Ed25519(ed25519_dalek::VerifyingKey),
40}
41
42#[allow(clippy::large_enum_variant)]
43#[derive(Debug)]
44pub enum DkimPrivateKey {
45    Ed25519(SigningKey),
46    OpenSSLRsa(Rsa<openssl::pkey::Private>),
47}
48
49impl DkimPrivateKey {
50    /// Parse RSA key data into a DkimPrivateKey
51    pub fn rsa_key(data: &[u8]) -> Result<Self, DKIMError> {
52        let mut errors = vec![];
53
54        match Rsa::private_key_from_pem(data) {
55            Ok(key) => return Ok(Self::OpenSSLRsa(key)),
56            Err(err) => errors.push(format!("openssl private_key_from_pem: {err:#}")),
57        };
58        match Rsa::private_key_from_der(data) {
59            Ok(key) => return Ok(Self::OpenSSLRsa(key)),
60            Err(err) => errors.push(format!("openssl private_key_from_der: {err:#}")),
61        };
62
63        Err(DKIMError::PrivateKeyLoadError(errors.join(". ")))
64    }
65
66    /// Load RSA key data from a file and parse it into a DkimPrivateKey
67    pub fn rsa_key_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self, DKIMError> {
68        let path = path.as_ref();
69        let data = std::fs::read(path).map_err(|err| {
70            DKIMError::PrivateKeyLoadError(format!(
71                "rsa_key_file: failed to read file {path:?}: {err:#}"
72            ))
73        })?;
74        Self::rsa_key(&data)
75    }
76
77    /// Parse PKCS8 encoded ed25519 key data into a DkimPrivateKey.
78    /// Both DER and PEM are supported
79    pub fn ed25519_key(data: &[u8]) -> Result<Self, DKIMError> {
80        let mut errors = vec![];
81
82        match SigningKey::from_pkcs8_der(data) {
83            Ok(key) => return Ok(Self::Ed25519(key)),
84            Err(err) => errors.push(format!("Ed25519 SigningKey::from_pkcs8_der: {err:#}")),
85        }
86
87        match std::str::from_utf8(data) {
88            Ok(s) => match SigningKey::from_pkcs8_pem(s) {
89                Ok(key) => return Ok(Self::Ed25519(key)),
90                Err(err) => errors.push(format!("Ed25519 SigningKey::from_pkcs8_pem: {err:#}")),
91            },
92            Err(err) => errors.push(format!("ed25519_key: data is not UTF-8: {err:#}")),
93        }
94
95        Err(DKIMError::PrivateKeyLoadError(errors.join(". ")))
96    }
97
98    /// Parse either an RSA or ed25519 key into a DkimPrivateKey.
99    /// Both DER and PEM are supported.
100    pub fn key(data: &[u8]) -> Result<Self, DKIMError> {
101        let mut errors = vec![];
102
103        match Rsa::private_key_from_pem(data) {
104            Ok(key) => return Ok(Self::OpenSSLRsa(key)),
105            Err(err) => errors.push(format!("openssl private_key_from_pem: {err:#}")),
106        }
107        match Rsa::private_key_from_der(data) {
108            Ok(key) => return Ok(Self::OpenSSLRsa(key)),
109            Err(err) => errors.push(format!("openssl private_key_from_der: {err:#}")),
110        }
111        match SigningKey::from_pkcs8_der(data) {
112            Ok(key) => return Ok(Self::Ed25519(key)),
113            Err(err) => errors.push(format!("Ed25519 SigningKey::from_pkcs8_der: {err:#}")),
114        }
115        match std::str::from_utf8(data) {
116            Ok(s) => match SigningKey::from_pkcs8_pem(s) {
117                Ok(key) => return Ok(Self::Ed25519(key)),
118                Err(err) => errors.push(format!("Ed25519 SigningKey::from_pkcs8_pem: {err:#}")),
119            },
120            Err(err) => errors.push(format!("ed25519_key: data is not UTF-8: {err:#}")),
121        }
122
123        Err(DKIMError::PrivateKeyLoadError(errors.join(". ")))
124    }
125}
126
127// https://datatracker.ietf.org/doc/html/rfc6376#section-6.1.3 Step 4
128fn verify_signature(
129    hash_algo: hash::HashAlgo,
130    header_hash: &[u8],
131    signature: &[u8],
132    public_key: DkimPublicKey,
133) -> Result<bool, DKIMError> {
134    Ok(match public_key {
135        DkimPublicKey::Rsa(public_key) => {
136            let md = match hash_algo {
137                hash::HashAlgo::RsaSha1 => Md::sha1(),
138                hash::HashAlgo::RsaSha256 => Md::sha256(),
139                hash @ hash::HashAlgo::Ed25519Sha256 => {
140                    // Algo is not compatible with RSA.
141                    // This case can happen when we're looking a DKIM-Header
142                    // with a=ed25519-sha256, but where domain publishes both
143                    // an rsa and an ed25519 public key in dns
144                    return Err(DKIMError::UnsupportedHashAlgorithm(format!("{hash:?}")));
145                }
146            };
147
148            let mut ctx = PkeyCtx::new(&public_key).map_err(|err| {
149                DKIMError::SignatureSyntaxError(format!("Error loading RSA public key: {err}"))
150            })?;
151
152            ctx.verify_init().map_err(|err| {
153                DKIMError::UnknownInternalError(format!("ctx.verify_init failed: {err}"))
154            })?;
155            ctx.set_rsa_padding(Padding::PKCS1).map_err(|err| {
156                DKIMError::UnknownInternalError(format!("ctx.set_rsa_padding failed: {err}"))
157            })?;
158            ctx.set_signature_md(md).map_err(|err| {
159                DKIMError::UnknownInternalError(format!("ctx.set_signature_md failed: {err}"))
160            })?;
161            ctx.verify(header_hash, signature).unwrap_or_default()
162        }
163        DkimPublicKey::Ed25519(public_key) => {
164            let mut sig_bytes = [0u8; ed25519_dalek::Signature::BYTE_SIZE];
165            if signature.len() != sig_bytes.len() {
166                return Err(DKIMError::SignatureSyntaxError(format!(
167                    "ed25519 signatures should be {} bytes in length, have: {}",
168                    ed25519_dalek::Signature::BYTE_SIZE,
169                    signature.len()
170                )));
171            }
172            sig_bytes.copy_from_slice(signature);
173
174            public_key
175                .verify_strict(
176                    header_hash,
177                    &ed25519_dalek::Signature::from_bytes(&sig_bytes),
178                )
179                .is_ok()
180        }
181    })
182}
183
184async fn verify_email_header<'a>(
185    resolver: &dyn Resolver,
186    signature_header_name: &str,
187    dkim_header: &'a TaggedHeader,
188    email: &'a ParsedEmail<'a>,
189) -> Result<(), DKIMError> {
190    let public_keys = public_key::retrieve_public_keys(
191        resolver,
192        dkim_header.get_required_tag("d"),
193        dkim_header.get_required_tag("s"),
194    )
195    .await?;
196
197    let (header_canonicalization_type, body_canonicalization_type) =
198        parser::parse_canonicalization(dkim_header.get_tag("c"))?;
199    let hash_algo = parser::parse_hash_algo(dkim_header.get_required_tag("a"))?;
200
201    let header_list: Vec<String> = dkim_header
202        .get_required_tag("h")
203        .split(':')
204        .map(|s| s.trim().to_ascii_lowercase())
205        .collect();
206    let header_list = HeaderList::new(header_list).compute_concrete_header_list(email);
207
208    let computed_headers_hash = hash::compute_headers_hash(
209        header_canonicalization_type,
210        &header_list,
211        hash_algo,
212        dkim_header,
213        signature_header_name,
214    )?;
215
216    if let Some(header_body_hash) = dkim_header.get_tag("bh") {
217        let computed_body_hash = hash::compute_body_hash(
218            body_canonicalization_type,
219            dkim_header.parse_tag("l")?,
220            hash_algo,
221            email,
222        )?;
223        tracing::debug!("body_hash {:?}", computed_body_hash);
224        if header_body_hash != computed_body_hash {
225            return Err(DKIMError::BodyHashDidNotVerify);
226        }
227    }
228
229    let signature = data_encoding::BASE64
230        .decode(dkim_header.get_required_tag("b").as_bytes())
231        .map_err(|err| {
232            DKIMError::SignatureSyntaxError(format!("failed to decode signature: {}", err))
233        })?;
234
235    let mut errors = vec![];
236    for public_key in public_keys {
237        match verify_signature(hash_algo, &computed_headers_hash, &signature, public_key) {
238            Ok(true) => return Ok(()),
239            Ok(false) => {}
240            Err(err) => {
241                errors.push(err);
242            }
243        }
244    }
245
246    if let Some(err) = errors.pop() {
247        // Something definitely failed
248        return Err(err);
249    }
250
251    // There were no errors and all keys returned false from verify_signature().
252    // That means that the signature is not verified.
253    Err(DKIMError::SignatureDidNotVerify)
254}
255
256/// Run the DKIM verification on the email providing an existing resolver
257pub async fn verify_email_with_resolver<'a>(
258    email: &'a ParsedEmail<'a>,
259    resolver: &dyn Resolver,
260) -> Result<Vec<AuthenticationResult>, DKIMError> {
261    fn populate_props(tagged_header: &TaggedHeader) -> BTreeMap<String, bstr::BString> {
262        let mut props = BTreeMap::new();
263
264        if let Some(signing_domain) = tagged_header.get_tag("d") {
265            props.insert("header.d".into(), signing_domain.into());
266            props.insert("header.i".into(), format!("@{signing_domain}").into());
267        }
268        if let Some(a_tag) = tagged_header.get_tag("a") {
269            props.insert("header.a".into(), a_tag.into());
270        }
271        if let Some(s_tag) = tagged_header.get_tag("s") {
272            props.insert("header.s".into(), s_tag.into());
273        }
274
275        if let Some(c_tag) = tagged_header.get_tag("c") {
276            props.insert("header.c".into(), c_tag.into());
277        }
278        if let Some(t_tag) = tagged_header.get_tag("t") {
279            props.insert("header.t".into(), t_tag.into());
280        }
281        if let Some(x_tag) = tagged_header.get_tag("x") {
282            props.insert("header.x".into(), x_tag.into());
283        }
284
285        props
286    }
287
288    let mut results = vec![];
289
290    let mut dkim_headers = vec![];
291
292    for h in email.get_headers().iter_named(DKIM_SIGNATURE_HEADER_NAME) {
293        if results.len() > 10 {
294            // Limit DoS impact if a malicious message is filled
295            // with signatures
296            break;
297        }
298
299        let raw_header = h.get_raw_value_string()?;
300        match DKIMHeader::parse(&raw_header) {
301            Ok(v) => {
302                dkim_headers.push(v);
303            }
304            Err(err) => {
305                let props = TaggedHeader::parse(&raw_header)
306                    .map(|tagged| populate_props(&tagged))
307                    .unwrap_or_default();
308                results.push(AuthenticationResult {
309                    method: "dkim".into(),
310                    method_version: None,
311                    result: "permerror".into(),
312                    reason: Some(format!("{err}").into()),
313                    props,
314                });
315            }
316        }
317    }
318
319    /// <https://datatracker.ietf.org/doc/html/rfc6008>
320    /// The value associated with this item in the header field MUST be
321    /// at least the first eight characters of the digital signature
322    /// (the "b=" tag from a DKIM-Signature) for which a result is being
323    /// relayed, and MUST be long enough to be unique among the results being
324    /// reported.
325    fn compute_header_b(b_tag: &str, headers: &[DKIMHeader]) -> String {
326        let mut len = 8;
327
328        'bigger: while len < b_tag.len() {
329            for h in headers {
330                let candidate = h.get_required_tag("b");
331                if candidate == b_tag {
332                    continue;
333                }
334                if b_tag[0..len] == candidate[0..len] {
335                    len += 2;
336                    continue 'bigger;
337                }
338            }
339            return b_tag[0..len].to_string();
340        }
341        b_tag.to_string()
342    }
343
344    for dkim_header in &dkim_headers {
345        let mut props = populate_props(dkim_header);
346
347        let b_tag = compute_header_b(dkim_header.get_required_tag("b"), &dkim_headers);
348        props.insert("header.b".into(), b_tag.into());
349
350        let mut reason = None;
351        let result =
352            match verify_email_header(resolver, DKIM_SIGNATURE_HEADER_NAME, dkim_header, email)
353                .await
354            {
355                Ok(()) => "pass",
356                Err(err) => {
357                    reason.replace(format!("{err}"));
358                    match err.status() {
359                        Status::Tempfail => "temperror",
360                        Status::Permfail => "permerror",
361                    }
362                }
363            };
364
365        results.push(AuthenticationResult {
366            method: "dkim".into(),
367            method_version: None,
368            result: result.into(),
369            reason: reason.map(Into::into),
370            props,
371        });
372    }
373
374    Ok(results)
375}
376
377/// Run the DKIM verification on the email
378pub async fn verify_email<'a>(
379    email: &'a ParsedEmail<'a>,
380) -> Result<Vec<AuthenticationResult>, DKIMError> {
381    let resolver = HickoryResolver::new().map_err(|err| {
382        DKIMError::UnknownInternalError(format!("failed to create DNS resolver: {}", err))
383    })?;
384
385    verify_email_with_resolver(email, &resolver).await
386}
387
388#[cfg(test)]
389mod tests {
390    use super::*;
391    use bstr::ByteSlice;
392    use dns_resolver::TestResolver;
393
394    const NEW_ENGLAND_DKIM: (&str, &str) = (
395        "newengland._domainkey.example.com",
396        "v=DKIM1; p=MIGJAoGBALVI635dLK4cJJAH3Lx6upo3X/Lm1tQz3mezcWTA3BUBnyIsdnRf57aD5BtNmhPrYYDlWlzw3UgnKisIxktkk5+iMQMlFtAS10JB8L3YadXNJY+JBcbeSi5TgJe4WFzNgW95FWDAuSTRXSWZfA/8xjflbTLDx0euFZOM7C4T0GwLAgMBAAE=",
397    );
398
399    #[test]
400    fn test_validate_header() {
401        let header = r#"v=1; a=rsa-sha256; d=example.net; s=brisbane;
402c=relaxed/simple; q=dns/txt; i=foo@eng.example.net;
403t=1117574938; x=9118006938; l=200;
404h=from:to:subject:date:keywords:keywords;
405z=From:foo@eng.example.net|To:joe@example.com|
406Subject:demo=20run|Date:July=205,=202005=203:44:08=20PM=20-0700;
407bh=MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=;
408b=dzdVyOfAKCdLXdJOc9G2q8LoXSlEniSbav+yuU4zGeeruD00lszZ
409      VoG4ZHRNiYzR
410        "#;
411        DKIMHeader::parse(header).unwrap();
412    }
413
414    #[test]
415    fn test_validate_header_missing_tag() {
416        let header = "v=1; a=rsa-sha256; bh=a; b=b";
417        assert_eq!(
418            DKIMHeader::parse(header).unwrap_err(),
419            DKIMError::SignatureMissingRequiredTag("d")
420        );
421    }
422
423    #[test]
424    fn test_validate_header_domain_mismatch() {
425        let header = r#"v=1; a=rsa-sha256; d=example.net; s=brisbane; i=foo@hein.com; h=headers; bh=hash; b=hash
426        "#;
427        assert_eq!(
428            DKIMHeader::parse(header).unwrap_err(),
429            DKIMError::DomainMismatch
430        );
431
432        let header = r#"v=1; a=rsa-sha256; d=example.net; s=brisbane.net; i=foo@fexample.net; h=headers; bh=hash; b=hash
433        "#;
434        assert_eq!(
435            DKIMHeader::parse(header).unwrap_err(),
436            DKIMError::DomainMismatch
437        );
438    }
439
440    #[test]
441    fn test_validate_header_incompatible_version() {
442        let header = r#"v=3; a=rsa-sha256; d=example.net; s=brisbane; i=foo@example.net; h=headers; bh=hash; b=hash
443        "#;
444        assert_eq!(
445            DKIMHeader::parse(header).unwrap_err(),
446            DKIMError::IncompatibleVersion
447        );
448    }
449
450    #[test]
451    fn test_validate_header_missing_from_in_headers_signature() {
452        let header = r#"v=1; a=rsa-sha256; d=example.net; s=brisbane; i=foo@example.net; h=Subject:A:B; bh=hash; b=hash
453        "#;
454        assert_eq!(
455            DKIMHeader::parse(header).unwrap_err(),
456            DKIMError::FromFieldNotSigned
457        );
458    }
459
460    #[test]
461    fn test_validate_header_expired_in_drift() {
462        let mut now = chrono::Utc::now().naive_utc();
463        now -= chrono::Duration::try_seconds(1).expect("1 second to be valid");
464
465        let header = format!("v=1; a=rsa-sha256; d=example.net; s=brisbane; i=foo@example.net; h=From:B; bh=hash; b=hash; x={}", now.and_utc().timestamp());
466
467        assert!(DKIMHeader::parse(&header).is_ok());
468    }
469
470    #[test]
471    fn test_validate_header_expired() {
472        let mut now = chrono::Utc::now().naive_utc();
473        now -= chrono::Duration::try_hours(3).expect("3 hours to be legit");
474
475        let header = format!("v=1; a=rsa-sha256; d=example.net; s=brisbane; i=foo@example.net; h=From:B; bh=hash; b=hash; x={}", now.and_utc().timestamp());
476
477        assert_eq!(
478            DKIMHeader::parse(&header).unwrap_err(),
479            DKIMError::SignatureExpired
480        );
481    }
482
483    #[tokio::test]
484    async fn test_expired_signature_populates_props() {
485        let x_value = "1";
486
487        let raw_email = format!(
488            "DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=example.net; s=brisbane; h=From; bh=hash; b=abcdefgh12345678; t=1; x={}\r\nFrom: user@example.net\r\n\r\nhello",
489            x_value,
490        );
491
492        let email = ParsedEmail::parse(raw_email).unwrap();
493        let resolver = TestResolver::default();
494
495        let res = verify_email_with_resolver(&email, &resolver).await.unwrap();
496        assert_eq!(res.len(), 1);
497
498        let result = &res[0];
499        assert_eq!(result.result, "permerror");
500        assert_eq!(
501            result.props.get("header.d").unwrap().to_str().unwrap(),
502            "example.net"
503        );
504        assert_eq!(
505            result.props.get("header.a").unwrap().to_str().unwrap(),
506            "rsa-sha256"
507        );
508        assert_eq!(
509            result.props.get("header.s").unwrap().to_str().unwrap(),
510            "brisbane"
511        );
512        assert_eq!(
513            result.props.get("header.c").unwrap().to_str().unwrap(),
514            "simple/simple"
515        );
516        assert_eq!(result.props.get("header.t").unwrap().to_str().unwrap(), "1");
517        assert_eq!(
518            result.props.get("header.x").unwrap().to_str().unwrap(),
519            x_value
520        );
521        assert!(result.props.get("header.b").is_none());
522    }
523
524    #[tokio::test]
525    async fn test_validate_email_header_ed25519() {
526        let raw_email = r#"DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed;
527 d=football.example.com; i=@football.example.com;
528 q=dns/txt; s=brisbane; t=1528637909; h=from : to :
529 subject : date : message-id : from : subject : date;
530 bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
531 b=/gCrinpcQOoIfuHNQIbq4pgh9kyIK3AQUdt9OdqQehSwhEIug4D11Bus
532 Fa3bT3FY5OsU7ZbnKELq+eXdp1Q1Dw==
533DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
534 d=football.example.com; i=@football.example.com;
535 q=dns/txt; s=test; t=1528637909; h=from : to : subject :
536 date : message-id : from : subject : date;
537 bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
538 b=F45dVWDfMbQDGHJFlXUNB2HKfbCeLRyhDXgFpEL8GwpsRe0IeIixNTe3
539 DhCVlUrSjV4BwcVcOF6+FF3Zo9Rpo1tFOeS9mPYQTnGdaSGsgeefOsk2Jz
540 dA+L10TeYt9BgDfQNZtKdN1WO//KgIqXP7OdEFE4LjFYNcUxZQ4FADY+8=
541From: Joe SixPack <joe@football.example.com>
542To: Suzie Q <suzie@shopping.example.net>
543Subject: Is dinner ready?
544Date: Fri, 11 Jul 2003 21:00:37 -0700 (PDT)
545Message-ID: <20030712040037.46341.5F8J@football.example.com>
546
547Hi.
548
549We lost the game.  Are you hungry yet?
550
551Joe."#
552            .replace('\n', "\r\n");
553
554        let email = ParsedEmail::parse(raw_email).unwrap();
555        let raw_header_dkim = email
556            .get_headers()
557            .iter_named(DKIM_SIGNATURE_HEADER_NAME)
558            .next()
559            .unwrap()
560            .get_raw_value_string()
561            .unwrap();
562
563        const DKIM_BRISBANE: &str = r#"
564$ORIGIN brisbane._domainkey.football.example.com
565@     300 TXT "v=DKIM1; p=MIGJAoGBALVI635dLK4cJJAH3Lx6upo3X/Lm1tQz3mezcWTA3BUBnyIsdnRf57aD5BtNmhPrYYDlWlzw3UgnKisIxktkk5+iMQMlFtAS10JB8L3YadXNJY+JBcbeSi5TgJe4WFzNgW95FWDAuSTRXSWZfA/8xjflbTLDx0euFZOM7C4T0GwLAgMBAAE="
566          TXT "v=DKIM1; k=ed25519; p=11qYAYKxCrfVS/7TyWQHOg7hcvPapiMlrwIaaPcHURo="
567"#;
568
569        let resolver = TestResolver::default().with_zone(DKIM_BRISBANE).unwrap();
570
571        verify_email_header(
572            &resolver,
573            DKIM_SIGNATURE_HEADER_NAME,
574            &DKIMHeader::parse(raw_header_dkim).unwrap(),
575            &email,
576        )
577        .await
578        .unwrap();
579    }
580
581    #[tokio::test]
582    async fn test_validate_email_header_rsa() {
583        // unfortunately the original RFC spec had a typo, and the mail content differs
584        // between algorithms
585        // https://www.rfc-editor.org/errata_search.php?rfc=6376&rec_status=0
586        let raw_email =
587            r#"DKIM-Signature: a=rsa-sha256; bh=2jUSOH9NhtVGCQWNr9BrIAPreKQjO6Sn7XIkfJVOzv8=;
588 c=simple/simple; d=example.com;
589 h=Received:From:To:Subject:Date:Message-ID; i=joe@football.example.com;
590 s=newengland; t=1615825284; v=1;
591 b=Xh4Ujb2wv5x54gXtulCiy4C0e+plRm6pZ4owF+kICpYzs/8WkTVIDBrzhJP0DAYCpnL62T0G
592 k+0OH8pi/yqETVjKtKk+peMnNvKkut0GeWZMTze0bfq3/JUK3Ln3jTzzpXxrgVnvBxeY9EZIL4g
593 s4wwFRRKz/1bksZGSjD8uuSU=
594Received: from client1.football.example.com  [192.0.2.1]
595      by submitserver.example.com with SUBMISSION;
596      Fri, 11 Jul 2003 21:01:54 -0700 (PDT)
597From: Joe SixPack <joe@football.example.com>
598To: Suzie Q <suzie@shopping.example.net>
599Subject: Is dinner ready?
600Date: Fri, 11 Jul 2003 21:00:37 -0700 (PDT)
601Message-ID: <20030712040037.46341.5F8J@football.example.com>
602
603Hi.
604
605We lost the game. Are you hungry yet?
606
607Joe.
608"#
609            .replace('\n', "\r\n");
610        let email = ParsedEmail::parse(raw_email).unwrap();
611        let raw_header_rsa = email
612            .get_headers()
613            .iter_named(DKIM_SIGNATURE_HEADER_NAME)
614            .next()
615            .unwrap()
616            .get_raw_value_string()
617            .unwrap();
618
619        let resolver =
620            TestResolver::default().with_txt(NEW_ENGLAND_DKIM.0, NEW_ENGLAND_DKIM.1.to_owned());
621
622        verify_email_header(
623            &resolver,
624            DKIM_SIGNATURE_HEADER_NAME,
625            &DKIMHeader::parse(raw_header_rsa).unwrap(),
626            &email,
627        )
628        .await
629        .unwrap();
630    }
631}