core_crypto/mls/conversation/decrypt.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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
//! MLS defines 3 kind of messages: Proposal, Commits and Application messages. Since they can (should)
//! be all encrypted we need to first decrypt them before deciding what to do with them.
//!
//! This table summarizes when a MLS group can decrypt any message:
//!
//! | can decrypt ? | 0 pend. Commit | 1 pend. Commit |
//! |-------------------|----------------|----------------|
//! | 0 pend. Proposal | ✅ | ✅ |
//! | 1+ pend. Proposal | ✅ | ✅ |
use log::{debug, info};
use openmls::prelude::StageCommitError;
use openmls::{
framing::errors::{MessageDecryptionError, SecretTreeError},
group::StagedCommit,
prelude::{
ContentType, CredentialType, MlsMessageIn, MlsMessageInBody, ProcessMessageError, ProcessedMessage,
ProcessedMessageContent, Proposal, ProtocolMessage, ValidationError,
},
};
use openmls_traits::OpenMlsCryptoProvider;
use std::fmt::Debug;
use tls_codec::Deserialize;
use core_crypto_keystore::entities::MlsPendingMessage;
use mls_crypto_provider::MlsCryptoProvider;
use crate::context::CentralContext;
use crate::obfuscate::Obfuscated;
use crate::{
e2e_identity::{conversation_state::compute_state, init_certificates::NewCrlDistributionPoint},
group_store::GroupStoreValue,
mls::{
client::Client,
conversation::renew::Renew,
credential::{
crl::{
extract_crl_uris_from_proposals, extract_crl_uris_from_update_path, get_new_crl_distribution_points,
},
ext::CredentialExt,
},
ClientId, ConversationId, MlsConversation,
},
prelude::{E2eiConversationState, MlsProposalBundle, WireIdentity},
CryptoError, CryptoResult, MlsError,
};
/// Represents the potential items a consumer might require after passing us an encrypted message we
/// have decrypted for him
#[derive(Debug)]
pub struct MlsConversationDecryptMessage {
/// Decrypted text message
pub app_msg: Option<Vec<u8>>,
/// Only when decrypted message is a commit, CoreCrypto will renew local proposal which could not make it in the commit.
/// This will contain either:
/// * local pending proposal not in the accepted commit
/// * If there is a pending commit, its proposals which are not in the accepted commit
pub proposals: Vec<MlsProposalBundle>,
/// Is the conversation still active after receiving this commit aka has the user been removed from the group
pub is_active: bool,
/// Delay time in seconds to feed caller timer for committing
pub delay: Option<u64>,
/// [ClientId] of the sender of the message being decrypted. Only present for application messages.
pub sender_client_id: Option<ClientId>,
/// Is the epoch changed after decrypting this message
pub has_epoch_changed: bool,
/// Identity claims present in the sender credential
/// Present for all messages
pub identity: WireIdentity,
/// Only set when the decrypted message is a commit.
/// Contains buffered messages for next epoch which were received before the commit creating the epoch
/// because the DS did not fan them out in order.
pub buffered_messages: Option<Vec<MlsBufferedConversationDecryptMessage>>,
/// New CRL distribution points that appeared by the introduction of a new credential
pub crl_new_distribution_points: NewCrlDistributionPoint,
}
/// Type safe recursion of [MlsConversationDecryptMessage]
#[derive(Debug)]
pub struct MlsBufferedConversationDecryptMessage {
/// see [MlsConversationDecryptMessage]
pub app_msg: Option<Vec<u8>>,
/// see [MlsConversationDecryptMessage]
pub proposals: Vec<MlsProposalBundle>,
/// see [MlsConversationDecryptMessage]
pub is_active: bool,
/// see [MlsConversationDecryptMessage]
pub delay: Option<u64>,
/// see [MlsConversationDecryptMessage]
pub sender_client_id: Option<ClientId>,
/// see [MlsConversationDecryptMessage]
pub has_epoch_changed: bool,
/// see [MlsConversationDecryptMessage]
pub identity: WireIdentity,
/// see [MlsConversationDecryptMessage]
pub crl_new_distribution_points: NewCrlDistributionPoint,
}
impl From<MlsConversationDecryptMessage> for MlsBufferedConversationDecryptMessage {
fn from(from: MlsConversationDecryptMessage) -> Self {
Self {
app_msg: from.app_msg,
proposals: from.proposals,
is_active: from.is_active,
delay: from.delay,
sender_client_id: from.sender_client_id,
has_epoch_changed: from.has_epoch_changed,
identity: from.identity,
crl_new_distribution_points: from.crl_new_distribution_points,
}
}
}
/// Abstraction over a MLS group capable of decrypting a MLS message
impl MlsConversation {
/// see [CentralContext::decrypt_message]
#[allow(clippy::too_many_arguments)]
#[cfg_attr(test, crate::durable)]
// FIXME: this might be causing stack overflow. Retry when this is solved: https://github.com/tokio-rs/tracing/issues/1147. Tracking issue: WPB-9654
// #[cfg_attr(not(test), tracing::instrument(err, skip_all))]
pub async fn decrypt_message(
&mut self,
message: MlsMessageIn,
parent_conv: Option<&GroupStoreValue<MlsConversation>>,
client: &Client,
backend: &MlsCryptoProvider,
restore_pending: bool,
) -> CryptoResult<MlsConversationDecryptMessage> {
let message = match self.parse_message(backend, message.clone()).await {
// Handles the case where we receive our own commits.
Err(CryptoError::MlsError(MlsError::MlsMessageError(ProcessMessageError::InvalidCommit(
StageCommitError::OwnCommit,
)))) => {
let ct = self.extract_confirmation_tag_from_own_commit(&message)?;
return self.handle_own_commit(backend, ct).await;
}
Ok(processed_message) => Ok(processed_message),
Err(e) => Err(e),
}?;
let credential = message.credential();
let epoch = message.epoch();
let identity = credential.extract_identity(
self.ciphersuite(),
backend.authentication_service().borrow().await.as_ref(),
)?;
let sender_client_id: ClientId = credential.credential.identity().into();
let decrypted = match message.into_content() {
ProcessedMessageContent::ApplicationMessage(app_msg) => {
debug!(
group_id = Obfuscated::from(&self.id),
epoch = epoch.as_u64(),
sender_client_id = Obfuscated::from(&sender_client_id);
"Application message"
);
MlsConversationDecryptMessage {
app_msg: Some(app_msg.into_bytes()),
proposals: vec![],
is_active: true,
delay: None,
sender_client_id: Some(sender_client_id),
has_epoch_changed: false,
identity,
buffered_messages: None,
crl_new_distribution_points: None.into(),
}
}
ProcessedMessageContent::ProposalMessage(proposal) => {
let crl_dps = extract_crl_uris_from_proposals(&[proposal.proposal().clone()])?;
let crl_new_distribution_points = get_new_crl_distribution_points(backend, crl_dps).await?;
info!(
group_id = Obfuscated::from(&self.id),
sender = Obfuscated::from(proposal.sender()),
proposals = Obfuscated::from(&proposal.proposal);
"Received proposal"
);
self.group.store_pending_proposal(*proposal);
MlsConversationDecryptMessage {
app_msg: None,
proposals: vec![],
is_active: true,
delay: self.compute_next_commit_delay(),
sender_client_id: None,
has_epoch_changed: false,
identity,
buffered_messages: None,
crl_new_distribution_points,
}
}
ProcessedMessageContent::StagedCommitMessage(staged_commit) => {
self.validate_commit(&staged_commit, backend).await?;
#[allow(clippy::needless_collect)] // false positive
let pending_proposals = self.self_pending_proposals().cloned().collect::<Vec<_>>();
let proposal_refs: Vec<Proposal> = pending_proposals
.iter()
.map(|p| p.proposal().clone())
.chain(
staged_commit
.add_proposals()
.map(|p| Proposal::Add(p.add_proposal().clone())),
)
.chain(
staged_commit
.update_proposals()
.map(|p| Proposal::Update(p.update_proposal().clone())),
)
.collect();
// - This requires a change in OpenMLS to get access to it
let mut crl_dps = extract_crl_uris_from_proposals(&proposal_refs)?;
crl_dps.extend(extract_crl_uris_from_update_path(&staged_commit)?);
let crl_new_distribution_points = get_new_crl_distribution_points(backend, crl_dps).await?;
// getting the pending has to be done before `merge_staged_commit` otherwise it's wiped out
let pending_commit = self.group.pending_commit().cloned();
self.group
.merge_staged_commit(backend, *staged_commit.clone())
.await
.map_err(MlsError::from)?;
let (proposals_to_renew, needs_update) = Renew::renew(
&self.group.own_leaf_index(),
pending_proposals.into_iter(),
pending_commit.as_ref(),
staged_commit.as_ref(),
);
let proposals = self
.renew_proposals_for_current_epoch(client, backend, proposals_to_renew.into_iter(), needs_update)
.await?;
let buffered_messages = if restore_pending {
if let Some(pm) = self
.restore_pending_messages(client, backend, parent_conv, false)
.await?
{
backend.key_store().remove::<MlsPendingMessage, _>(self.id()).await?;
Some(pm)
} else {
None
}
} else {
None
};
info!(
group_id = Obfuscated::from(&self.id),
epoch = staged_commit.staged_context().epoch().as_u64(),
proposals:? = staged_commit.queued_proposals().map(Obfuscated::from).collect::<Vec<_>>();
"Epoch advanced"
);
MlsConversationDecryptMessage {
app_msg: None,
proposals,
is_active: self.group.is_active(),
delay: self.compute_next_commit_delay(),
sender_client_id: None,
has_epoch_changed: true,
identity,
buffered_messages,
crl_new_distribution_points,
}
}
ProcessedMessageContent::ExternalJoinProposalMessage(proposal) => {
info!(
group_id = Obfuscated::from(&self.id),
sender = Obfuscated::from(proposal.sender());
"Received external join proposal"
);
let crl_dps = extract_crl_uris_from_proposals(&[proposal.proposal().clone()])?;
let crl_new_distribution_points = get_new_crl_distribution_points(backend, crl_dps).await?;
self.group.store_pending_proposal(*proposal);
MlsConversationDecryptMessage {
app_msg: None,
proposals: vec![],
is_active: true,
delay: self.compute_next_commit_delay(),
sender_client_id: None,
has_epoch_changed: false,
identity,
buffered_messages: None,
crl_new_distribution_points,
}
}
};
self.persist_group_when_changed(&backend.keystore(), false).await?;
Ok(decrypted)
}
async fn parse_message(
&mut self,
backend: &MlsCryptoProvider,
msg_in: MlsMessageIn,
) -> CryptoResult<ProcessedMessage> {
let mut is_duplicate = false;
let (protocol_message, content_type) = match msg_in.extract() {
MlsMessageInBody::PublicMessage(m) => {
is_duplicate = self.is_duplicate_message(backend, &m)?;
let ct = m.content_type();
(ProtocolMessage::PublicMessage(m), ct)
}
MlsMessageInBody::PrivateMessage(m) => {
let ct = m.content_type();
(ProtocolMessage::PrivateMessage(m), ct)
}
_ => {
return Err(CryptoError::MlsError(
ProcessMessageError::IncompatibleWireFormat.into(),
))
}
};
let msg_epoch = protocol_message.epoch().as_u64();
let group_epoch = self.group.epoch().as_u64();
let processed_msg = self
.group
.process_message(backend, protocol_message)
.await
.map_err(|e| match e {
ProcessMessageError::ValidationError(ValidationError::UnableToDecrypt(
MessageDecryptionError::GenerationOutOfBound,
)) => CryptoError::DuplicateMessage,
ProcessMessageError::ValidationError(ValidationError::WrongEpoch) => {
if is_duplicate {
CryptoError::DuplicateMessage
} else if msg_epoch == group_epoch + 1 {
// limit to next epoch otherwise if we were buffering a commit for epoch + 2
// we would fail when trying to decrypt it in [MlsCentral::commit_accepted]
CryptoError::BufferedFutureMessage
} else if msg_epoch < group_epoch {
match content_type {
ContentType::Application => CryptoError::StaleMessage,
ContentType::Commit => CryptoError::StaleCommit,
ContentType::Proposal => CryptoError::StaleProposal,
}
} else {
CryptoError::WrongEpoch
}
}
ProcessMessageError::ValidationError(ValidationError::UnableToDecrypt(
MessageDecryptionError::AeadError,
)) => CryptoError::DecryptionError,
ProcessMessageError::ValidationError(ValidationError::UnableToDecrypt(
MessageDecryptionError::SecretTreeError(SecretTreeError::TooDistantInThePast),
)) => CryptoError::MessageEpochTooOld,
_ => CryptoError::from(MlsError::from(e)),
})?;
if is_duplicate {
return Err(CryptoError::DuplicateMessage);
}
Ok(processed_msg)
}
async fn validate_commit(&self, commit: &StagedCommit, backend: &MlsCryptoProvider) -> CryptoResult<()> {
if backend.authentication_service().is_env_setup().await {
let credentials: Vec<_> = commit
.add_proposals()
.filter_map(|add_proposal| {
let credential = add_proposal.add_proposal().key_package().leaf_node().credential();
matches!(credential.credential_type(), CredentialType::X509).then(|| credential.clone())
})
.collect();
let state = compute_state(
self.ciphersuite(),
credentials.iter(),
crate::prelude::MlsCredentialType::X509,
backend.authentication_service().borrow().await.as_ref(),
)
.await;
if state != E2eiConversationState::Verified {
// FIXME: Uncomment when PKI env can be seeded - the computation is still done to assess performance and impact of the validations. Tracking issue: WPB-9665
// return Err(CryptoError::InvalidCertificateChain);
}
}
Ok(())
}
}
impl CentralContext {
/// Deserializes a TLS-serialized message, then deciphers it
///
/// # Arguments
/// * `conversation` - the group/conversation id
/// * `message` - the encrypted message as a byte array
///
/// # Return type
/// This method will return a tuple containing an optional message and an optional delay time
/// for the callers to wait for committing. A message will be `None` in case the provided payload in
/// case of a system message, such as Proposals and Commits. Otherwise it will return the message as a
/// byte array. The delay will be `Some` when the message has a proposal
///
/// # Errors
/// If the conversation can't be found, an error will be returned. Other errors are originating
/// from OpenMls and the KeyStore
pub async fn decrypt_message(
&self,
id: &ConversationId,
message: impl AsRef<[u8]>,
) -> CryptoResult<MlsConversationDecryptMessage> {
let msg = MlsMessageIn::tls_deserialize(&mut message.as_ref()).map_err(MlsError::from)?;
let Ok(conversation) = self.get_conversation(id).await else {
return self.handle_when_group_is_pending(id, message).await;
};
let parent_conversation = self.get_parent_conversation(&conversation).await?;
let client = &self.mls_client().await?;
let decrypt_message = conversation
.write()
.await
.decrypt_message(
msg,
parent_conversation.as_ref(),
client,
&self.mls_provider().await?,
true,
)
.await;
let decrypt_message = match decrypt_message {
Err(CryptoError::BufferedFutureMessage) => self.handle_future_message(id, message).await?,
_ => decrypt_message?,
};
if !decrypt_message.is_active {
self.wipe_conversation(id).await?;
}
Ok(decrypt_message)
}
}
#[cfg(test)]
mod tests {
use wasm_bindgen_test::*;
use crate::{mls::conversation::config::MAX_PAST_EPOCHS, prelude::MlsCommitBundle, test_utils::*, CryptoError};
use super::*;
wasm_bindgen_test_configure!(run_in_browser);
mod is_active {
use super::*;
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_regular_commit_should_leave_conversation_active(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 MlsCommitBundle { commit, .. } = bob_central.context.update_keying_material(&id).await.unwrap();
let MlsConversationDecryptMessage { is_active, .. } = alice_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
assert!(is_active)
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_removing_self_should_set_conversation_inactive(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 MlsCommitBundle { commit, .. } = bob_central
.context
.remove_members_from_conversation(&id, &[alice_central.get_client_id().await])
.await
.unwrap();
let MlsConversationDecryptMessage { is_active, .. } = alice_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
assert!(!is_active)
})
})
.await
}
}
mod commit {
use super::*;
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_succeed(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 epoch_before = alice_central.context.conversation_epoch(&id).await.unwrap();
let MlsCommitBundle { commit, .. } =
alice_central.context.update_keying_material(&id).await.unwrap();
alice_central.context.commit_accepted(&id).await.unwrap();
let decrypted = bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
let epoch_after = bob_central.context.conversation_epoch(&id).await.unwrap();
assert_eq!(epoch_after, epoch_before + 1);
assert!(decrypted.has_epoch_changed);
assert!(decrypted.delay.is_none());
assert!(decrypted.app_msg.is_none());
alice_central.verify_sender_identity(&case, &decrypted).await;
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_clear_pending_commit(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie", "debbie"],
move |[alice_central, bob_central, charlie_central, debbie_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();
// Alice creates a commit which will be superseded by Bob's one
let charlie = charlie_central.rand_key_package(&case).await;
let debbie = debbie_central.rand_key_package(&case).await;
alice_central
.context
.add_members_to_conversation(&id, vec![charlie.clone()])
.await
.unwrap();
assert!(alice_central.pending_commit(&id).await.is_some());
let add_debbie_commit = bob_central
.context
.add_members_to_conversation(&id, vec![debbie.clone()])
.await
.unwrap()
.commit;
let decrypted = alice_central
.context
.decrypt_message(&id, add_debbie_commit.to_bytes().unwrap())
.await
.unwrap();
// Now Debbie should be in members and not Charlie
let members = alice_central.get_conversation_unchecked(&id).await.members();
let dc = debbie.unverified_credential();
let debbie_id = dc.credential.identity();
assert!(members.get(debbie_id).is_some());
let cc = charlie.unverified_credential();
let charlie_id = cc.credential.identity();
assert!(members.get(charlie_id).is_none());
// Previous commit to add Charlie has been discarded but its proposals will be renewed
assert!(alice_central.pending_commit(&id).await.is_none());
assert!(decrypted.has_epoch_changed)
})
},
)
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_renew_proposals_in_pending_commit(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie"],
move |[mut alice_central, bob_central, charlie_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();
// Alice will create a commit to add Charlie
// Bob will create a commit which will be accepted first by DS so Alice will decrypt it
// Then Alice will renew the proposal in her pending commit
let charlie = charlie_central.rand_key_package(&case).await;
let bob_commit = bob_central.context.update_keying_material(&id).await.unwrap().commit;
bob_central.context.commit_accepted(&id).await.unwrap();
// Alice propose to add Charlie
alice_central
.context
.add_members_to_conversation(&id, vec![charlie.clone()])
.await
.unwrap();
assert!(alice_central.pending_commit(&id).await.is_some());
// But first she receives Bob commit
let MlsConversationDecryptMessage { proposals, delay, .. } = alice_central
.context
.decrypt_message(&id, bob_commit.to_bytes().unwrap())
.await
.unwrap();
// So Charlie has not been added to the group
let cc = charlie.unverified_credential();
let charlie_id = cc.credential.identity();
assert!(alice_central
.get_conversation_unchecked(&id)
.await
.members()
.get(charlie_id)
.is_none());
// Make sure we are suggesting a commit delay
assert!(delay.is_some());
// But its proposal to add Charlie has been renewed and is also in store
assert!(!proposals.is_empty());
assert_eq!(alice_central.pending_proposals(&id).await.len(), 1);
assert!(alice_central.pending_commit(&id).await.is_none());
// Let's commit this proposal to see if it works
for p in proposals {
// But first, proposals have to be fan out to Bob
bob_central
.context
.decrypt_message(&id, p.proposal.to_bytes().unwrap())
.await
.unwrap();
}
let MlsCommitBundle { commit, welcome, .. } = alice_central
.context
.commit_pending_proposals(&id)
.await
.unwrap()
.unwrap();
alice_central.context.commit_accepted(&id).await.unwrap();
// Charlie is now in the group
assert!(alice_central
.get_conversation_unchecked(&id)
.await
.members()
.get(charlie_id)
.is_some());
let decrypted = bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
// Bob also has Charlie in the group
let cc = charlie.unverified_credential();
let charlie_id = cc.credential.identity();
assert!(bob_central
.get_conversation_unchecked(&id)
.await
.members()
.get(charlie_id)
.is_some());
assert!(decrypted.has_epoch_changed);
// Charlie can join with the Welcome from renewed Add proposal
let id = charlie_central
.context
.process_welcome_message(welcome.unwrap().into(), case.custom_cfg())
.await
.unwrap()
.id;
assert!(charlie_central.try_talk_to(&id, &alice_central).await.is_ok());
})
},
)
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_not_renew_proposals_in_valid_commit(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie"],
move |[mut alice_central, bob_central, charlie_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();
// Bob will create a proposal to add Charlie
// Alice will decrypt this proposal
// Then Bob will create a commit to update
// Alice will decrypt the commit but musn't renew the proposal to add Charlie
let charlie_kp = charlie_central.get_one_key_package(&case).await;
let add_charlie_proposal = bob_central.context.new_add_proposal(&id, charlie_kp).await.unwrap();
alice_central
.context
.decrypt_message(&id, add_charlie_proposal.proposal.to_bytes().unwrap())
.await
.unwrap();
let MlsCommitBundle { commit, .. } =
bob_central.context.update_keying_material(&id).await.unwrap();
let MlsConversationDecryptMessage {
proposals,
delay,
has_epoch_changed,
..
} = alice_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
assert!(proposals.is_empty());
assert!(delay.is_none());
assert!(alice_central.pending_proposals(&id).await.is_empty());
assert!(has_epoch_changed)
})
},
)
.await
}
// orphan proposal = not backed by the pending commit
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_renew_orphan_pending_proposals(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie"],
move |[mut alice_central, mut bob_central, charlie_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();
// Alice will create a proposal to add Charlie
// Bob will create a commit which Alice will decrypt
// Then Alice will renew her proposal
let bob_commit = bob_central.context.update_keying_material(&id).await.unwrap().commit;
bob_central.context.commit_accepted(&id).await.unwrap();
let commit_epoch = bob_commit.epoch().unwrap();
// Alice propose to add Charlie
let charlie_kp = charlie_central.get_one_key_package(&case).await;
alice_central.context.new_add_proposal(&id, charlie_kp).await.unwrap();
assert_eq!(alice_central.pending_proposals(&id).await.len(), 1);
// But first she receives Bob commit
let MlsConversationDecryptMessage { proposals, delay, .. } = alice_central
.context
.decrypt_message(&id, bob_commit.to_bytes().unwrap())
.await
.unwrap();
// So Charlie has not been added to the group
assert!(alice_central
.get_conversation_unchecked(&id)
.await
.members()
.get(&b"charlie".to_vec())
.is_none());
// Make sure we are suggesting a commit delay
assert!(delay.is_some());
// But its proposal to add Charlie has been renewed and is also in store
assert!(!proposals.is_empty());
assert_eq!(alice_central.pending_proposals(&id).await.len(), 1);
let renewed_proposal = proposals.first().unwrap();
assert_eq!(
commit_epoch.as_u64() + 1,
renewed_proposal.proposal.epoch().unwrap().as_u64()
);
// Let's use this proposal to see if it works
bob_central
.context
.decrypt_message(&id, renewed_proposal.proposal.to_bytes().unwrap())
.await
.unwrap();
assert_eq!(bob_central.pending_proposals(&id).await.len(), 1);
let MlsCommitBundle { commit, .. } = bob_central
.context
.commit_pending_proposals(&id)
.await
.unwrap()
.unwrap();
let decrypted = alice_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
// Charlie is now in the group
assert!(alice_central
.get_conversation_unchecked(&id)
.await
.members()
.get::<Vec<u8>>(&charlie_central.get_client_id().await.to_vec())
.is_some());
// Bob also has Charlie in the group
bob_central.context.commit_accepted(&id).await.unwrap();
assert!(bob_central
.get_conversation_unchecked(&id)
.await
.members()
.get::<Vec<u8>>(&charlie_central.get_client_id().await.to_vec())
.is_some());
assert!(decrypted.has_epoch_changed);
})
},
)
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
pub async fn decrypting_a_commit_should_discard_pending_external_proposals(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie"],
move |[mut alice_central, bob_central, charlie_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();
// DS will create an external proposal to add Charlie
// But meanwhile Bob, before receiving the external proposal,
// will create a commit and send it to Alice.
// Alice will not renew the external proposal
let ext_proposal = charlie_central
.context
.new_external_add_proposal(
id.clone(),
alice_central.get_conversation_unchecked(&id).await.group.epoch(),
case.ciphersuite(),
case.credential_type,
)
.await
.unwrap();
assert!(alice_central.pending_proposals(&id).await.is_empty());
alice_central
.context
.decrypt_message(&id, ext_proposal.to_bytes().unwrap())
.await
.unwrap();
assert_eq!(alice_central.pending_proposals(&id).await.len(), 1);
let MlsCommitBundle { commit, .. } =
bob_central.context.update_keying_material(&id).await.unwrap();
let alice_renewed_proposals = alice_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap()
.proposals;
assert!(alice_renewed_proposals.is_empty());
assert!(alice_central.pending_proposals(&id).await.is_empty());
})
},
)
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn should_not_return_sender_client_id(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 commit = alice_central.context.update_keying_material(&id).await.unwrap().commit;
let sender_client_id = bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap()
.sender_client_id;
assert!(sender_client_id.is_none());
})
})
.await
}
}
mod external_proposal {
use super::*;
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn can_decrypt_external_proposal(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "alice2"],
move |[alice_central, bob_central, alice2_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 epoch = alice_central.get_conversation_unchecked(&id).await.group.epoch();
let ext_proposal = alice2_central
.context
.new_external_add_proposal(id.clone(), epoch, case.ciphersuite(), case.credential_type)
.await
.unwrap();
let decrypted = alice_central
.context
.decrypt_message(&id, &ext_proposal.to_bytes().unwrap())
.await
.unwrap();
assert!(decrypted.app_msg.is_none());
assert!(decrypted.delay.is_some());
let decrypted = bob_central
.context
.decrypt_message(&id, &ext_proposal.to_bytes().unwrap())
.await
.unwrap();
assert!(decrypted.app_msg.is_none());
assert!(decrypted.delay.is_some());
assert!(!decrypted.has_epoch_changed)
})
},
)
.await
}
}
mod proposal {
use super::*;
// Ensures decrypting an proposal is durable
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn can_decrypt_proposal(case: TestCase) {
run_test_with_client_ids(
case.clone(),
["alice", "bob", "charlie"],
move |[alice_central, bob_central, charlie_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 charlie_kp = charlie_central.get_one_key_package(&case).await;
let proposal = alice_central
.context
.new_add_proposal(&id, charlie_kp)
.await
.unwrap()
.proposal;
let decrypted = bob_central
.context
.decrypt_message(&id, proposal.to_bytes().unwrap())
.await
.unwrap();
assert_eq!(bob_central.get_conversation_unchecked(&id).await.members().len(), 2);
// if 'decrypt_message' is not durable the commit won't contain the add proposal
bob_central
.context
.commit_pending_proposals(&id)
.await
.unwrap()
.unwrap();
bob_central.context.commit_accepted(&id).await.unwrap();
assert_eq!(bob_central.get_conversation_unchecked(&id).await.members().len(), 3);
assert!(!decrypted.has_epoch_changed);
alice_central.verify_sender_identity(&case, &decrypted).await;
})
},
)
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn should_not_return_sender_client_id(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;
let sender_client_id = bob_central
.context
.decrypt_message(&id, proposal.to_bytes().unwrap())
.await
.unwrap()
.sender_client_id;
assert!(sender_client_id.is_none());
})
})
.await
}
}
mod app_message {
use super::*;
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn can_decrypt_app_message(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.encrypt_message(&id, msg).await.unwrap();
assert_ne!(&msg[..], &encrypted[..]);
let decrypted = bob_central.context.decrypt_message(&id, encrypted).await.unwrap();
let dec_msg = decrypted.app_msg.as_ref().unwrap().as_slice();
assert_eq!(dec_msg, &msg[..]);
assert!(!decrypted.has_epoch_changed);
alice_central.verify_sender_identity(&case, &decrypted).await;
let msg = b"Hello alice";
let encrypted = bob_central.context.encrypt_message(&id, msg).await.unwrap();
assert_ne!(&msg[..], &encrypted[..]);
let decrypted = alice_central.context.decrypt_message(&id, encrypted).await.unwrap();
let dec_msg = decrypted.app_msg.as_ref().unwrap().as_slice();
assert_eq!(dec_msg, &msg[..]);
assert!(!decrypted.has_epoch_changed);
bob_central.verify_sender_identity(&case, &decrypted).await;
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn cannot_decrypt_app_message_after_rejoining(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();
// encrypt a message in epoch 1
let msg = b"Hello bob";
let encrypted = alice_central.context.encrypt_message(&id, msg).await.unwrap();
// Now Bob will rejoin the group and try to decrypt Alice's message
// in epoch 2 which should fail
let gi = alice_central.get_group_info(&id).await;
bob_central
.context
.join_by_external_commit(gi, case.custom_cfg(), case.credential_type)
.await
.unwrap();
bob_central
.context
.merge_pending_group_from_external_commit(&id)
.await
.unwrap();
// fails because of Forward Secrecy
let decrypt = bob_central.context.decrypt_message(&id, &encrypted).await;
assert!(matches!(decrypt.unwrap_err(), CryptoError::DecryptionError));
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn cannot_decrypt_app_message_from_future_epoch(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();
// only Alice will change epoch without notifying Bob
let commit = alice_central.context.update_keying_material(&id).await.unwrap().commit;
alice_central.context.commit_accepted(&id).await.unwrap();
// Now in epoch 2 Alice will encrypt a message
let msg = b"Hello bob";
let encrypted = alice_central.context.encrypt_message(&id, msg).await.unwrap();
// which Bob cannot decrypt because of Post CompromiseSecurity
let decrypt = bob_central.context.decrypt_message(&id, &encrypted).await;
assert!(matches!(decrypt.unwrap_err(), CryptoError::BufferedFutureMessage));
let decrypted_commit = bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
let buffered_msg = decrypted_commit.buffered_messages.unwrap();
let decrypted_msg = buffered_msg.first().unwrap().app_msg.clone().unwrap();
assert_eq!(&decrypted_msg, msg);
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn can_decrypt_app_message_in_any_order(mut case: TestCase) {
// otherwise the test would fail because we decrypt messages in reverse order which is
// kinda dropping them
case.cfg.custom.maximum_forward_distance = 0;
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 out_of_order_tolerance = case.custom_cfg().out_of_order_tolerance;
let nb_messages = out_of_order_tolerance * 2;
let mut messages = vec![];
// stack up encrypted messages..
for i in 0..nb_messages {
let msg = format!("Hello {i}");
let encrypted = alice_central.context.encrypt_message(&id, &msg).await.unwrap();
messages.push((msg, encrypted));
}
// ..then unstack them to see out_of_order_tolerance come into play
messages.reverse();
for (i, (original, encrypted)) in messages.iter().enumerate() {
let decrypt = bob_central.context.decrypt_message(&id, encrypted).await;
if i > out_of_order_tolerance as usize {
let decrypted = decrypt.unwrap().app_msg.unwrap();
assert_eq!(decrypted, original.as_bytes());
} else {
assert!(matches!(decrypt.unwrap_err(), CryptoError::DuplicateMessage))
}
}
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn returns_sender_client_id(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.encrypt_message(&id, msg).await.unwrap();
assert_ne!(&msg[..], &encrypted[..]);
let sender_client_id = bob_central
.context
.decrypt_message(&id, encrypted)
.await
.unwrap()
.sender_client_id
.unwrap();
assert_eq!(sender_client_id, alice_central.get_client_id().await);
})
})
.await
}
}
mod epoch_sync {
use super::*;
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn should_throw_specialized_error_when_epoch_too_old(mut case: TestCase) {
case.cfg.custom.out_of_order_tolerance = 0;
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();
// Alice encrypts a message to Bob
let bob_message1 = alice_central.context.encrypt_message(&id, b"Hello Bob").await.unwrap();
let bob_message2 = alice_central
.context
.encrypt_message(&id, b"Hello again Bob")
.await
.unwrap();
// Move group's epoch forward by self updating
for _ in 0..MAX_PAST_EPOCHS {
let commit = alice_central.context.update_keying_material(&id).await.unwrap().commit;
alice_central.context.commit_accepted(&id).await.unwrap();
bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
}
// Decrypt should work
let decrypt = bob_central.context.decrypt_message(&id, &bob_message1).await.unwrap();
assert_eq!(decrypt.app_msg.unwrap(), b"Hello Bob");
// Moving the epochs once more should cause an error
let commit = alice_central.context.update_keying_material(&id).await.unwrap().commit;
alice_central.context.commit_accepted(&id).await.unwrap();
bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
let decrypt = bob_central.context.decrypt_message(&id, &bob_message2).await;
assert!(matches!(decrypt.unwrap_err(), CryptoError::MessageEpochTooOld));
})
})
.await
}
#[apply(all_cred_cipher)]
#[wasm_bindgen_test]
async fn should_throw_specialized_error_when_epoch_desynchronized(mut case: TestCase) {
case.cfg.custom.out_of_order_tolerance = 0;
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();
// Alice generates a bunch of soon to be outdated messages
let old_proposal = alice_central
.context
.new_update_proposal(&id)
.await
.unwrap()
.proposal
.to_bytes()
.unwrap();
alice_central
.get_conversation_unchecked(&id)
.await
.group
.clear_pending_proposals();
let old_commit = alice_central
.context
.update_keying_material(&id)
.await
.unwrap()
.commit
.to_bytes()
.unwrap();
alice_central.context.clear_pending_commit(&id).await.unwrap();
// Now let's jump to next epoch
let commit = alice_central.context.update_keying_material(&id).await.unwrap().commit;
alice_central.context.commit_accepted(&id).await.unwrap();
bob_central
.context
.decrypt_message(&id, commit.to_bytes().unwrap())
.await
.unwrap();
// trying to consume outdated messages should fail with a dedicated error
let decrypt_err = bob_central
.context
.decrypt_message(&id, &old_proposal)
.await
.unwrap_err();
assert!(matches!(decrypt_err, CryptoError::StaleProposal));
let decrypt_err = bob_central.context.decrypt_message(&id, &old_commit).await.unwrap_err();
assert!(matches!(decrypt_err, CryptoError::StaleCommit));
})
})
.await
}
}
}