core_crypto/mls/conversation/duplicate.rs
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
//! Due the current delivery semantics on backend side (at least once) we have to deal with this
//! in CoreCrypto so as not to return a decryption error to the client. Remove this when this is used
//! with a DS guaranteeing exactly once delivery semantics since the following degrades the performances
use super::{Error, Result};
use crate::{MlsError, prelude::MlsConversation};
use mls_crypto_provider::MlsCryptoProvider;
use openmls::prelude::{ContentType, FramedContentBodyIn, Proposal, PublicMessageIn, Sender};
impl MlsConversation {
pub(crate) fn is_duplicate_message(&self, backend: &MlsCryptoProvider, msg: &PublicMessageIn) -> Result<bool> {
let (sender, content_type) = (msg.sender(), msg.body().content_type());
match (content_type, sender) {
(ContentType::Commit, Sender::Member(_) | Sender::NewMemberCommit) => {
// we use the confirmation tag to detect duplicate since it is issued from the GroupContext
// which is supposed to be unique per epoch
if let Some(msg_ct) = msg.confirmation_tag() {
let group_ct = self
.group
.compute_confirmation_tag(backend)
.map_err(MlsError::wrap("computing confirmation tag"))?;
Ok(msg_ct == &group_ct)
} else {
// a commit MUST have a ConfirmationTag
Err(Error::MlsGroupInvalidState("a commit must have a ConfirmationTag"))
}
}
(ContentType::Proposal, Sender::Member(_) | Sender::NewMemberProposal) => {
match msg.body() {
FramedContentBodyIn::Proposal(proposal) => {
let proposal = Proposal::from(proposal.clone()); // TODO: eventually remove this clone 😮💨. Tracking issue: WPB-9622
let already_exists = self.group.pending_proposals().any(|pp| pp.proposal() == &proposal);
Ok(already_exists)
}
_ => Err(Error::MlsGroupInvalidState(
"message body was not a proposal despite ContentType::Proposal",
)),
}
}
(_, _) => Ok(false),
}
}
}
#[cfg(test)]
mod tests {
use super::super::error::Error;
use crate::mls::conversation::Conversation as _;
use crate::test_utils::*;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn decrypting_duplicate_member_commit_should_fail(case: TestCase) {
// cannot work in pure ciphertext since we'd have to decrypt the message first
if !case.is_pure_ciphertext() {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice_central, bob_central]| {
Box::pin(async move {
let id = conversation_id();
alice_central
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
alice_central.invite_all(&case, &id, [&bob_central]).await.unwrap();
// an commit to verify that we can still detect wrong epoch correctly
let unknown_commit = alice_central.create_unmerged_commit(&id).await.commit;
alice_central
.context
.conversation(&id)
.await
.unwrap()
.clear_pending_commit()
.await
.unwrap();
alice_central
.context
.conversation(&id)
.await
.unwrap()
.update_key_material()
.await
.unwrap();
let commit = alice_central.mls_transport.latest_commit().await;
// decrypt once ... ok
bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&commit.to_bytes().unwrap())
.await
.unwrap();
// decrypt twice ... not ok
let decrypt_duplicate = bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&commit.to_bytes().unwrap())
.await;
assert!(matches!(decrypt_duplicate.unwrap_err(), Error::DuplicateMessage));
// Decrypting unknown commit.
// It fails with this error since it's not the commit who has created this epoch
let decrypt_lost_commit = bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&unknown_commit.to_bytes().unwrap())
.await;
assert!(matches!(decrypt_lost_commit.unwrap_err(), Error::StaleCommit));
})
})
.await
}
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn decrypting_duplicate_external_commit_should_fail(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice_central, bob_central]| {
Box::pin(async move {
let id = conversation_id();
alice_central
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
let gi = alice_central.get_group_info(&id).await;
// an external commit to verify that we can still detect wrong epoch correctly
let (unknown_ext_commit, mut pending_conversation) = bob_central
.create_unmerged_external_commit(gi.clone(), case.custom_cfg(), case.credential_type)
.await;
let unknown_ext_commit = unknown_ext_commit.commit;
pending_conversation.clear().await.unwrap();
bob_central
.context
.join_by_external_commit(gi, case.custom_cfg(), case.credential_type)
.await
.unwrap();
let ext_commit = bob_central.mls_transport.latest_commit().await;
// decrypt once ... ok
alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&ext_commit.to_bytes().unwrap())
.await
.unwrap();
// decrypt twice ... not ok
let decryption = alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&ext_commit.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::DuplicateMessage));
// Decrypting unknown external commit.
// It fails with this error since it's not the external commit who has created this epoch
let decryption = alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&unknown_ext_commit.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::StaleCommit));
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn decrypting_duplicate_proposal_should_fail(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice_central, bob_central]| {
Box::pin(async move {
let id = conversation_id();
alice_central
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
alice_central.invite_all(&case, &id, [&bob_central]).await.unwrap();
let proposal = alice_central.context.new_update_proposal(&id).await.unwrap().proposal;
// decrypt once ... ok
bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&proposal.to_bytes().unwrap())
.await
.unwrap();
// decrypt twice ... not ok
let decryption = bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&proposal.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::DuplicateMessage));
// advance Bob's epoch to trigger failure
bob_central
.context
.conversation(&id)
.await
.unwrap()
.commit_pending_proposals()
.await
.unwrap();
// Epoch has advanced so we cannot detect duplicates anymore
let decryption = bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&proposal.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::StaleProposal));
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn decrypting_duplicate_external_proposal_should_fail(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice_central, bob_central]| {
Box::pin(async move {
let id = conversation_id();
alice_central
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
let epoch = alice_central.context.conversation(&id).await.unwrap().epoch().await;
let ext_proposal = bob_central
.context
.new_external_add_proposal(id.clone(), epoch.into(), case.ciphersuite(), case.credential_type)
.await
.unwrap();
// decrypt once ... ok
alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&ext_proposal.to_bytes().unwrap())
.await
.unwrap();
// decrypt twice ... not ok
let decryption = alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&ext_proposal.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::DuplicateMessage));
// advance alice's epoch
alice_central
.context
.conversation(&id)
.await
.unwrap()
.commit_pending_proposals()
.await
.unwrap();
// Epoch has advanced so we cannot detect duplicates anymore
let decryption = alice_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&ext_proposal.to_bytes().unwrap())
.await;
assert!(matches!(decryption.unwrap_err(), Error::StaleProposal));
})
})
.await
}
// Ensures decrypting an application message is durable (we increment the messages generation & persist the group)
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn decrypting_duplicate_application_message_should_fail(case: TestCase) {
run_test_with_client_ids(case.clone(), ["alice", "bob"], move |[alice_central, bob_central]| {
Box::pin(async move {
let id = conversation_id();
alice_central
.context
.new_conversation(&id, case.credential_type, case.cfg.clone())
.await
.unwrap();
alice_central.invite_all(&case, &id, [&bob_central]).await.unwrap();
let msg = b"Hello bob";
let encrypted = alice_central
.context
.conversation(&id)
.await
.unwrap()
.encrypt_message(msg)
.await
.unwrap();
// decrypt once .. ok
bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&encrypted)
.await
.unwrap();
// decrypt twice .. not ok
let decryption = bob_central
.context
.conversation(&id)
.await
.unwrap()
.decrypt_message(&encrypted)
.await;
assert!(matches!(decryption.unwrap_err(), Error::DuplicateMessage));
})
})
.await
}
}