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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
//! DomainKeys Identified Mail version 2 (DKIM2).
//!
//! Every hop that relays a message adds a `DKIM2-Signature` header field. A
//! hop that changes the message also adds a `Message-Instance` header field
//! with hashes of the new content and a recipe that recreates the previous
//! content. Each signature binds the SMTP envelope (`MAIL FROM` and `RCPT TO`)
//! used by its hop, or names the domain of the next signer, so a verifier can
//! follow the chain of custody from the originator to the last hop.
//!
//! Implemented specifications:
//!
//! - [draft-ietf-dkim-dkim2-spec-08](https://datatracker.ietf.org/doc/draft-ietf-dkim-dkim2-spec/):
//! DKIM2 Signatures. Section references (`§`) in this module point to this draft.
//! - [draft-ietf-dkim-dkim2-dns](https://datatracker.ietf.org/doc/draft-ietf-dkim-dkim2-dns/):
//! DKIM2 DNS Records. Pure ML-DSA-44 keys are published with `k=mldsa44`,
//! which the DNS draft does not define yet.
//!
//! Sign with [`Dkim2Signer`]. Verify a message with
//! [`MessageAuthenticator::verify_dkim2`](crate::MessageAuthenticator::verify_dkim2)
//! (or check that it is Provisionally Verified with
//! [`MessageAuthenticator::verify_dkim2_provisionally`](crate::MessageAuthenticator::verify_dkim2_provisionally))
//! and an inbound delivery status notification with
//! [`MessageAuthenticator::verify_dkim2_dsn`](crate::MessageAuthenticator::verify_dkim2_dsn).
use crate::;
pub use ;
pub use Dkim2Error;
pub use ;
pub use ;
pub use ;
/// DKIM2 message signer (§12).
///
/// Built in typestate steps. [`Dkim2Signer::from_key`] takes the first
/// private key, [`domain`](Dkim2Signer::domain) sets the signing domain (`d=`)
/// and [`selector`](Dkim2Signer::selector) sets the selector of the first key,
/// which yields a `Dkim2Signer<`[`Ready`](crate::signer::Ready)`>`. Only a
/// ready signer can sign. Optional settings on a ready signer:
/// [`additional_key`](Dkim2Signer::additional_key) adds an alternative
/// signature under another selector (`|`),
/// [`hybrid_key`](Dkim2Signer::hybrid_key) adds a signature that must pass
/// together with the previous one (`,`), [`hashes`](Dkim2Signer::hashes)
/// selects the `Message-Instance` hash algorithms, [`flags`](Dkim2Signer::flags)
/// sets the `f=` tag, [`nonce`](Dkim2Signer::nonce) sets the `n=` tag and
/// [`out_of_band_arrangement`](Dkim2Signer::out_of_band_arrangement) allows
/// signing messages that are not Fully Verified DKIM2 Messages.
///
/// The signing methods ([`sign`](Dkim2Signer::sign),
/// [`sign_revised`](Dkim2Signer::sign_revised),
/// [`sign_with_recipe`](Dkim2Signer::sign_with_recipe) and
/// [`sign_with_message_instance`](Dkim2Signer::sign_with_message_instance))
/// return a [`Dkim2Signed`] holding the header fields to prepend to the
/// message. A message that already carries DKIM2 header fields is signed only
/// with the [`Dkim2Output`] of a full verification that passed (§12).
///
/// # Example
///
/// ```rust,no_run
/// use mail_auth::{
/// crypto::{Ed25519Key, RsaKey, Sha256},
/// dkim2::{Dkim2Signer, Flag, Hop},
/// };
/// use rustls_pki_types::{PrivateKeyDer, pem::PemObject};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let rsa_pem = "";
/// # let ed25519_pkcs8: &[u8] = &[];
/// # let message = "From: sender@example.com\r\n\r\nHello\r\n";
/// let rsa_key = RsaKey::<Sha256>::from_key_der(PrivateKeyDer::from_pem_slice(rsa_pem.as_bytes())?)?;
/// let signed = Dkim2Signer::from_key(rsa_key)
/// .domain("example.com")
/// .selector("rsa-sel")
/// .additional_key(Ed25519Key::from_pkcs8_der(ed25519_pkcs8)?, "ed-sel")
/// .flags([Flag::Feedback])
/// .sign(
/// message.as_bytes(),
/// Hop::real("sender@example.com", ["recipient@example.org"]),
/// None,
/// )?;
///
/// let signed_message = format!("{}{}", signed.to_header(), message);
/// # Ok(())
/// # }
/// ```
pub
/// A parsed `DKIM2-Signature` header field (§10).
///
/// Produced by [`Signature::parse`] when a message is parsed into an
/// [`AuthenticatedMessage`](crate::AuthenticatedMessage), and by the
/// [`Dkim2Signer`] signing methods in [`Dkim2Signed::signature`]. Verified
/// signatures are exposed through [`ChainLink::signature`].
/// One entry of the `s=` tag of a `DKIM2-Signature` (§10.9).
///
/// A signature carries one entry per signing key, which gives algorithmic
/// agility: each entry names its own selector and algorithm. Entries that
/// share a [`group`](Self::group) are separated by `,` and all of them must
/// pass; groups are separated by `|` and the signature passes if any group
/// does.
/// The algorithm name of a [`SignatureValue`]: an algorithm this crate
/// implements, or the name of one it does not, which never passes (§14.6).
/// How a `DKIM2-Signature` links to the next hop in the chain of custody
/// (§10.5 to §10.7, §12.2 and §12.3).
/// A value of the `f=` tag of a `DKIM2-Signature` (§10.10). Flag names are
/// case-sensitive.
/// A parsed `Message-Instance` header field (§9).
///
/// Records the hashes of the message content at one revision and, for a
/// revision made by a Reviser, the recipe that recreates the previous
/// revision. Produced by [`MessageInstance::parse`] when a message is parsed,
/// by [`MessageInstance::from_message`] and [`MessageInstance::from_recipe`],
/// and by the signers in [`Dkim2Signed::message_instance`].
/// A hash algorithm defined for DKIM2 `Message-Instance` hashes (§3.1).
/// The hash name of a [`MessageHash`]: an algorithm this crate implements, or
/// the name of one it does not, which never passes (§14.7).
/// One entry of the `h=` tag of a `Message-Instance` (§9.3 and §6).
///
/// Entries that share a [`group`](Self::group) are separated by `,` and all
/// of them must pass; groups are separated by `|` and the hashes pass if any
/// group does.