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
//! MLS lifecycle observability primitives.
use crate::telemetry::CategorySampler;
use offline_protocol_mls::MlsError;
use serde::Serialize;
/// Operation context for MLS lifecycle events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MlsOperationContext {
/// Outbound send path.
Send,
/// Inbound receive path.
Receive,
/// MLS manager initialization path.
Initialize,
/// Session lookup or resolution path.
SessionLookup,
/// Welcome processing path.
Welcome,
}
/// Typed error category for MLS lifecycle events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum MlsErrorCategory {
/// Session or group state is missing.
SessionStateMissing,
/// MLS manager is not initialized.
NotInitialized,
/// Ciphertext/message shape is invalid.
InvalidCiphertext,
/// Sender identity or signature verification failed.
IdentityMismatch,
/// Crypto engine operation failed.
CryptoFailure,
/// Unknown or uncategorized failure.
Unknown,
}
/// Fine-grained decryption failure classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum DecryptionFailureKind {
/// Session or group state was not found.
SessionNotFound,
/// MLS manager is not initialized.
NotInitialized,
/// Message bytes cannot be decrypted/parsed as valid MLS ciphertext.
InvalidCiphertext,
/// Signature/identity verification failed.
IdentityMismatch,
/// Other cryptographic failure.
CryptoFailure,
/// Fallback class for unsupported variants.
Unknown,
}
impl DecryptionFailureKind {
/// Classifies an MLS error into a decryption failure kind.
pub fn from_mls_error(error: &MlsError) -> Self {
match error {
MlsError::SessionNotFound(_) | MlsError::GroupNotFound(_) => Self::SessionNotFound,
MlsError::NotInitialized => Self::NotInitialized,
MlsError::InvalidMessage(_)
| MlsError::Deserialization(_)
| MlsError::Decryption(_) => Self::InvalidCiphertext,
MlsError::VerificationFailed(_) | MlsError::InvalidPublicKey(_) => {
Self::IdentityMismatch
}
MlsError::Encryption(_)
| MlsError::OpenMls(_)
| MlsError::Signing(_)
| MlsError::Storage(_)
| MlsError::CryptoGeneration(_)
// An epoch desync only reaches here on the recovery-disabled
// fall-through (recovery-enabled desyncs never take the drop path);
// classify it as a crypto failure rather than the opaque `Unknown`.
| MlsError::SessionDesync(_) => Self::CryptoFailure,
_ => Self::Unknown,
}
}
/// Maps a decryption failure kind to its broader telemetry category.
pub fn error_category(self) -> MlsErrorCategory {
match self {
Self::SessionNotFound => MlsErrorCategory::SessionStateMissing,
Self::NotInitialized => MlsErrorCategory::NotInitialized,
Self::InvalidCiphertext => MlsErrorCategory::InvalidCiphertext,
Self::IdentityMismatch => MlsErrorCategory::IdentityMismatch,
Self::CryptoFailure => MlsErrorCategory::CryptoFailure,
Self::Unknown => MlsErrorCategory::Unknown,
}
}
}
/// Typed MLS lifecycle events for operational observability.
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum MlsLifecycleEvent {
/// MLS subsystem has been initialized and is ready for use.
Initialized {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
},
/// Encryption path was successfully used.
EncryptionUsed {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
},
/// Decryption failed with a typed failure class.
DecryptionFailed {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
failure_kind: DecryptionFailureKind,
},
/// Session lookup failed or session/group state is missing.
SessionMissing {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
},
/// A handshake with this peer has begun: no session existed, the peer's
/// key package was imported and a Welcome was built for them.
///
/// This is the *start* of the span `mls_session_ready_latency_p50_ms`
/// measures, and the only start that fires on the path a device usually
/// takes. [`Self::SessionMissing`] marks a handshake that could not
/// begin (no key package on hand, or MLS not initialized); with
/// `auto_key_exchange` on, which is the default, a peer's key package
/// normally arrives before the first send and the ordinary handshake
/// raises no miss at all. Pairing on misses alone samples the slow tail
/// and calls it the median.
SessionEstablishing {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
},
/// Session lifecycle has reached a ready/usable state.
SessionReady {
timestamp_ms: i64,
session_id: String,
group_id: Option<String>,
peer_id: Option<String>,
context: MlsOperationContext,
error_category: Option<MlsErrorCategory>,
},
}
impl MlsLifecycleEvent {
/// Returns the stable `snake.dot.case` telemetry name for this event.
///
/// Names are compatible with OpenTelemetry naming conventions and are
/// considered stable across minor versions of the SDK.
pub fn telemetry_name(&self) -> &'static str {
match self {
Self::Initialized { .. } => "mls.initialized",
Self::EncryptionUsed { .. } => "mls.encryption_used",
Self::DecryptionFailed { .. } => "mls.decryption_failed",
Self::SessionMissing { .. } => "mls.session_missing",
Self::SessionEstablishing { .. } => "mls.session_establishing",
Self::SessionReady { .. } => "mls.session_ready",
}
}
}
/// Sink abstraction for MLS lifecycle events.
pub trait MlsEventEmitter: Send + Sync {
/// Emits a structured MLS lifecycle event.
fn emit(&self, event: MlsLifecycleEvent);
}
/// Default no-op emitter used when observability is not configured.
#[derive(Debug, Default)]
pub struct NoopMlsEventEmitter;
impl MlsEventEmitter for NoopMlsEventEmitter {
fn emit(&self, _event: MlsLifecycleEvent) {}
}
/// Best-effort fixed-window limiter to reduce high-volume failure event floods.
///
/// Thin wrapper around [`CategorySampler`] that maps MLS lifecycle events to
/// rate-limit keys.
#[derive(Debug, Default)]
pub struct MlsEventRateLimiter {
sampler: CategorySampler,
}
impl MlsEventRateLimiter {
/// Returns true if the event passes rate limiting.
pub fn should_emit(&self, event: &MlsLifecycleEvent) -> bool {
let now_ms = timestamp_now_ms();
match event {
MlsLifecycleEvent::DecryptionFailed {
peer_id,
failure_kind,
..
} => {
let key = format!(
"decryption_failed:{}:{:?}",
peer_id.as_deref().unwrap_or("none"),
failure_kind
);
self.sampler.allow(&key, now_ms)
}
MlsLifecycleEvent::SessionMissing { peer_id, .. } => {
let key = format!("session_missing:{}", peer_id.as_deref().unwrap_or("none"));
self.sampler.allow(&key, now_ms)
}
_ => true,
}
}
}
/// Returns current wall-clock time in Unix milliseconds.
pub fn timestamp_now_ms() -> i64 {
chrono::Utc::now().timestamp_millis()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decryption_failure_classification_is_typed() {
assert_eq!(
DecryptionFailureKind::from_mls_error(&MlsError::SessionNotFound("p".to_string())),
DecryptionFailureKind::SessionNotFound
);
assert_eq!(
DecryptionFailureKind::from_mls_error(&MlsError::NotInitialized),
DecryptionFailureKind::NotInitialized
);
assert_eq!(
DecryptionFailureKind::from_mls_error(&MlsError::InvalidMessage("bad".to_string())),
DecryptionFailureKind::InvalidCiphertext
);
assert_eq!(
DecryptionFailureKind::from_mls_error(&MlsError::VerificationFailed("sig".to_string())),
DecryptionFailureKind::IdentityMismatch
);
}
#[test]
fn limiter_suppresses_flood() {
let limiter = MlsEventRateLimiter::default();
let base_ts = 1000_i64;
// First 10 DecryptionFailed events for the same peer+kind pass.
for i in 0..10 {
let event = MlsLifecycleEvent::DecryptionFailed {
timestamp_ms: base_ts + i,
session_id: "s".to_string(),
group_id: None,
peer_id: Some("peer-a".to_string()),
context: MlsOperationContext::Receive,
error_category: None,
failure_kind: DecryptionFailureKind::InvalidCiphertext,
};
assert!(limiter.should_emit(&event));
}
// 11th is suppressed.
let event = MlsLifecycleEvent::DecryptionFailed {
timestamp_ms: base_ts + 10,
session_id: "s".to_string(),
group_id: None,
peer_id: Some("peer-a".to_string()),
context: MlsOperationContext::Receive,
error_category: None,
failure_kind: DecryptionFailureKind::InvalidCiphertext,
};
assert!(!limiter.should_emit(&event));
}
}