Skip to main content

rusted_mail/models/
crypto.rs

1//! Message cryptography, key material, trust policy, headers, and attachments.
2
3use super::{Deserialize, Serialize, fmt};
4
5#[derive(Clone, Deserialize)]
6#[serde(rename_all = "camelCase")]
7pub struct OutboundCrypto {
8    pub protocol: CryptoProtocol,
9    pub mode: OutboundCryptoMode,
10    pub signer: Option<CryptoSigner>,
11    #[serde(default)]
12    pub recipients: Vec<CryptoRecipient>,
13    #[serde(default)]
14    pub detached_signature: bool,
15}
16
17impl fmt::Debug for OutboundCrypto {
18    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
19        formatter
20            .debug_struct("OutboundCrypto")
21            .field("protocol", &self.protocol)
22            .field("mode", &self.mode)
23            .field("signer", &self.signer)
24            .field("recipients", &self.recipients)
25            .field("detached_signature", &self.detached_signature)
26            .finish()
27    }
28}
29
30#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
31#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
32pub enum CryptoProtocol {
33    Smime,
34    Openpgp,
35}
36
37#[derive(Debug, Clone, Copy, Deserialize, PartialEq, Eq)]
38#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
39pub enum OutboundCryptoMode {
40    Sign,
41    Encrypt,
42    SignAndEncrypt,
43}
44
45#[allow(dead_code)]
46#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
47#[serde(rename_all = "camelCase")]
48pub struct CryptoKeyMaterial {
49    pub format: CryptoKeyFormat,
50    pub value: String,
51    pub passphrase: Option<String>,
52}
53
54impl fmt::Debug for CryptoKeyMaterial {
55    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
56        formatter
57            .debug_struct("CryptoKeyMaterial")
58            .field("format", &self.format)
59            .field("value", &"[REDACTED]")
60            .field(
61                "passphrase",
62                &self.passphrase.as_ref().map(|_| "[REDACTED]"),
63            )
64            .finish()
65    }
66}
67
68#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
69#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
70pub enum CryptoKeyFormat {
71    Pem,
72    DerBase64,
73    Armored,
74}
75
76#[derive(Clone, Deserialize)]
77#[serde(rename_all = "camelCase")]
78pub struct CryptoRecipient {
79    pub material: CryptoKeyMaterial,
80}
81
82impl fmt::Debug for CryptoRecipient {
83    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
84        formatter
85            .debug_struct("CryptoRecipient")
86            .field("material", &self.material)
87            .finish()
88    }
89}
90
91#[derive(Clone, Deserialize)]
92#[serde(rename_all = "camelCase")]
93pub struct CryptoSigner {
94    pub public_material: Option<CryptoKeyMaterial>,
95    pub private_material: CryptoKeyMaterial,
96}
97
98impl fmt::Debug for CryptoSigner {
99    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
100        formatter
101            .debug_struct("CryptoSigner")
102            .field("public_material", &self.public_material)
103            .field("private_material", &self.private_material)
104            .finish()
105    }
106}
107
108#[derive(Debug, Clone, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct CryptoVerificationOptions {
111    #[serde(default)]
112    pub materials: Vec<CryptoKeyMaterial>,
113    #[serde(default)]
114    pub trusted_signer_ids: Vec<String>,
115    #[serde(default)]
116    pub rejected_signer_ids: Vec<String>,
117    #[serde(default)]
118    pub require_signed: bool,
119    #[serde(default)]
120    pub require_valid_signature: bool,
121    #[serde(default)]
122    pub require_trusted_signer: bool,
123}
124
125impl CryptoVerificationOptions {
126    #[must_use]
127    pub fn requires_crypto_policy(&self) -> bool {
128        self.require_signed
129            || self.require_valid_signature
130            || self.require_trusted_signer
131            || !self.rejected_signer_ids.is_empty()
132    }
133}
134
135#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
136#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
137pub enum CryptoTrustScope {
138    Account,
139    Contact,
140    Domain,
141}
142
143#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
144#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
145pub enum CryptoTrustRecordKind {
146    Signer,
147    SmimeTrustAnchor,
148    SmimeIntermediate,
149}
150
151#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
152#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
153pub enum CryptoTrustDecision {
154    Pending,
155    Trusted,
156    Rejected,
157    Revoked,
158}
159
160#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
161#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
162pub enum CryptoTrustSource {
163    Manual,
164    Tofu,
165    Autocrypt,
166    Wkd,
167    OsCa,
168    EnterpriseCa,
169    Ocsp,
170    Crl,
171}
172
173#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
174#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
175#[derive(Default)]
176pub enum CryptoTrustDiscoveryMode {
177    #[default]
178    Disabled,
179    PendingOnly,
180}
181
182#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
183#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
184pub enum CryptoRevocationMode {
185    Disabled,
186    SoftFail,
187    HardFail,
188}
189
190#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
191#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
192pub enum CryptoRevocationStatus {
193    NotChecked,
194    Good,
195    Revoked,
196    Unknown,
197    NetworkError,
198    Unsupported,
199}
200
201fn default_crypto_trust_source() -> CryptoTrustSource {
202    CryptoTrustSource::Manual
203}
204
205fn default_crypto_revocation_status() -> CryptoRevocationStatus {
206    CryptoRevocationStatus::NotChecked
207}
208
209#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
210#[serde(rename_all = "camelCase")]
211pub struct CryptoTrustRecord {
212    pub id: String,
213    pub account_id: String,
214    pub scope: CryptoTrustScope,
215    pub scope_value: String,
216    pub protocol: Option<CryptoProtocol>,
217    pub kind: CryptoTrustRecordKind,
218    pub material: Option<CryptoKeyMaterial>,
219    #[serde(default)]
220    pub signer_ids: Vec<String>,
221    #[serde(default)]
222    pub issuer_serial_ids: Vec<String>,
223    pub decision: CryptoTrustDecision,
224    #[serde(default = "default_crypto_trust_source")]
225    pub source: CryptoTrustSource,
226    #[serde(default)]
227    pub source_uri: Option<String>,
228    #[serde(default)]
229    pub source_header: Option<String>,
230    #[serde(default)]
231    pub confidence: Option<u8>,
232    #[serde(default = "default_crypto_revocation_status")]
233    pub revocation_status: CryptoRevocationStatus,
234    #[serde(default)]
235    pub revocation_checked_at_ms: Option<u64>,
236    #[serde(default)]
237    pub revocation_next_check_at_ms: Option<u64>,
238    #[serde(default)]
239    pub revocation_error: Option<String>,
240    #[serde(default)]
241    pub valid_from_ms: Option<u64>,
242    #[serde(default)]
243    pub valid_until_ms: Option<u64>,
244    #[serde(default)]
245    pub supersedes_record_id: Option<String>,
246    #[serde(default)]
247    pub superseded_by_record_id: Option<String>,
248    #[serde(default)]
249    pub created_at_ms: u64,
250    #[serde(default)]
251    pub updated_at_ms: u64,
252}
253
254#[derive(Debug, Clone, Deserialize)]
255#[serde(rename_all = "camelCase")]
256pub struct CryptoTrustRecordUpsertRequest {
257    pub record: CryptoTrustRecord,
258}
259
260#[derive(Debug, Clone, Deserialize)]
261#[serde(rename_all = "camelCase")]
262pub struct CryptoTrustRecordListRequest {
263    pub account_id: String,
264    #[serde(default)]
265    pub scope: Option<CryptoTrustScope>,
266    #[serde(default)]
267    pub scope_value: Option<String>,
268    #[serde(default)]
269    pub protocol: Option<CryptoProtocol>,
270    #[serde(default)]
271    pub include_expired: bool,
272}
273
274fn default_crypto_trust_discovery_mode() -> CryptoTrustDiscoveryMode {
275    CryptoTrustDiscoveryMode::PendingOnly
276}
277
278fn default_crypto_revocation_mode() -> CryptoRevocationMode {
279    CryptoRevocationMode::SoftFail
280}
281
282fn default_trust_timeout_ms() -> u64 {
283    3000
284}
285
286fn default_trust_cache_ttl_ms() -> u64 {
287    86_400_000
288}
289
290#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
291#[serde(rename_all = "camelCase")]
292pub struct CryptoTrustPolicy {
293    pub account_id: String,
294    #[serde(default = "default_crypto_trust_discovery_mode")]
295    pub tofu: CryptoTrustDiscoveryMode,
296    #[serde(default = "default_crypto_trust_discovery_mode")]
297    pub autocrypt: CryptoTrustDiscoveryMode,
298    #[serde(default)]
299    pub wkd: CryptoTrustDiscoveryMode,
300    #[serde(default)]
301    pub os_ca_import: CryptoTrustDiscoveryMode,
302    #[serde(default = "default_crypto_revocation_mode")]
303    pub revocation: CryptoRevocationMode,
304    #[serde(default = "default_trust_timeout_ms")]
305    pub network_timeout_ms: u64,
306    #[serde(default = "default_trust_cache_ttl_ms")]
307    pub cache_ttl_ms: u64,
308    #[serde(default)]
309    pub created_at_ms: u64,
310    #[serde(default)]
311    pub updated_at_ms: u64,
312}
313
314#[derive(Debug, Clone, Deserialize)]
315#[serde(rename_all = "camelCase")]
316pub struct CryptoTrustPolicyUpsertRequest {
317    pub policy: CryptoTrustPolicy,
318}
319
320#[derive(Debug, Clone, Deserialize)]
321#[serde(rename_all = "camelCase")]
322pub struct CryptoTrustAnchorImportRequest {
323    pub account_id: String,
324    #[serde(default)]
325    pub scope: Option<CryptoTrustScope>,
326    #[serde(default)]
327    pub scope_value: Option<String>,
328    #[serde(default = "default_crypto_trust_source")]
329    pub source: CryptoTrustSource,
330    #[serde(default)]
331    pub materials: Vec<CryptoKeyMaterial>,
332}
333
334#[derive(Debug, Clone, Deserialize)]
335#[serde(rename_all = "camelCase")]
336pub struct CryptoDecryptionOptions {
337    #[serde(default)]
338    pub materials: Vec<CryptoKeyMaterial>,
339}
340
341#[allow(dead_code)]
342#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
343#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
344pub enum CryptoSignatureStatus {
345    NotSigned,
346    Valid,
347    Invalid,
348    UnknownKey,
349    Unsupported,
350}
351
352#[allow(dead_code)]
353#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
354#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
355pub enum CryptoTrustStatus {
356    NotSigned,
357    Trusted,
358    Untrusted,
359    Rejected,
360    Unknown,
361    Unverified,
362}
363
364#[allow(dead_code)]
365#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
366#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
367pub enum CryptoPolicyStatus {
368    NotConfigured,
369    Satisfied,
370    Failed,
371}
372
373#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
374#[serde(rename_all = "camelCase")]
375pub struct MessageCryptoStatus {
376    pub protocol: Option<CryptoProtocol>,
377    pub signed: bool,
378    pub encrypted: bool,
379    pub signature_status: CryptoSignatureStatus,
380    pub trust_status: CryptoTrustStatus,
381    pub policy_status: CryptoPolicyStatus,
382    pub signer_ids: Vec<String>,
383    pub errors: Vec<String>,
384}
385
386impl Default for MessageCryptoStatus {
387    fn default() -> Self {
388        Self {
389            protocol: None,
390            signed: false,
391            encrypted: false,
392            signature_status: CryptoSignatureStatus::NotSigned,
393            trust_status: CryptoTrustStatus::NotSigned,
394            policy_status: CryptoPolicyStatus::NotConfigured,
395            signer_ids: Vec::new(),
396            errors: Vec::new(),
397        }
398    }
399}
400
401#[derive(Debug, Clone, Deserialize)]
402#[serde(rename_all = "camelCase")]
403pub struct HeaderEntry {
404    pub name: String,
405    pub value: String,
406}
407
408#[derive(Debug, Clone, Copy, Deserialize)]
409#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
410pub enum MessagePriority {
411    High,
412    Normal,
413    Low,
414}
415
416#[derive(Clone, Deserialize)]
417#[serde(rename_all = "camelCase")]
418pub struct Attachment {
419    pub filename: String,
420    pub content_type: String,
421    pub content_id: Option<String>,
422    #[serde(default)]
423    pub inline: bool,
424    pub base64: Option<String>,
425    pub path: Option<String>,
426}
427
428impl fmt::Debug for Attachment {
429    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
430        formatter
431            .debug_struct("Attachment")
432            .field("filename", &self.filename)
433            .field("content_type", &self.content_type)
434            .field("content_id", &self.content_id)
435            .field("inline", &self.inline)
436            .field("base64", &self.base64.as_ref().map(|_| "[REDACTED]"))
437            .field("path", &self.path.as_ref().map(|_| "[REDACTED]"))
438            .finish()
439    }
440}