| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize}; |
| 6 |
use mls_rs_core::{ |
| 7 |
crypto::SignatureSecretKey, error::IntoAnyError, extension::ExtensionList, group::Member, |
| 8 |
identity::IdentityProvider, |
| 9 |
}; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
cipher_suite::CipherSuite, |
| 13 |
client::MlsError, |
| 14 |
external_client::ExternalClientConfig, |
| 15 |
group::{ |
| 16 |
cipher_suite_provider, |
| 17 |
confirmation_tag::ConfirmationTag, |
| 18 |
framing::PublicMessage, |
| 19 |
member_from_leaf_node, |
| 20 |
message_processor::{ |
| 21 |
ApplicationMessageDescription, CommitMessageDescription, EventOrContent, |
| 22 |
MessageProcessor, ProposalMessageDescription, ProvisionalState, |
| 23 |
}, |
| 24 |
proposal::RemoveProposal, |
| 25 |
proposal_filter::ProposalInfo, |
| 26 |
snapshot::RawGroupState, |
| 27 |
state::GroupState, |
| 28 |
transcript_hash::InterimTranscriptHash, |
| 29 |
validate_tree_and_info_joiner, ContentType, ExportedTree, GroupContext, GroupInfo, Roster, |
| 30 |
Welcome, |
| 31 |
}, |
| 32 |
identity::SigningIdentity, |
| 33 |
protocol_version::ProtocolVersion, |
| 34 |
psk::AlwaysFoundPskStorage, |
| 35 |
tree_kem::{node::LeafIndex, path_secret::PathSecret, TreeKemPrivate}, |
| 36 |
CryptoProvider, KeyPackage, MlsMessage, |
| 37 |
}; |
| 38 |
|
| 39 |
#[cfg(feature = "by_ref_proposal")] |
| 40 |
use crate::{ |
| 41 |
group::{ |
| 42 |
framing::{Content, MlsMessagePayload}, |
| 43 |
message_processor::CachedProposal, |
| 44 |
message_signature::AuthenticatedContent, |
| 45 |
proposal::Proposal, |
| 46 |
proposal_ref::ProposalRef, |
| 47 |
Sender, |
| 48 |
}, |
| 49 |
WireFormat, |
| 50 |
}; |
| 51 |
|
| 52 |
#[cfg(all(feature = "by_ref_proposal", feature = "custom_proposal"))] |
| 53 |
use crate::group::proposal::CustomProposal; |
| 54 |
|
| 55 |
#[cfg(feature = "by_ref_proposal")] |
| 56 |
use mls_rs_core::{crypto::CipherSuiteProvider, psk::ExternalPskId}; |
| 57 |
|
| 58 |
#[cfg(feature = "by_ref_proposal")] |
| 59 |
use crate::{ |
| 60 |
extension::ExternalSendersExt, |
| 61 |
group::proposal::{AddProposal, ReInitProposal}, |
| 62 |
}; |
| 63 |
|
| 64 |
#[cfg(all(feature = "by_ref_proposal", feature = "psk"))] |
| 65 |
use crate::{ |
| 66 |
group::proposal::PreSharedKeyProposal, |
| 67 |
psk::{ |
| 68 |
JustPreSharedKeyID, PreSharedKeyID, PskGroupId, PskNonce, ResumptionPSKUsage, ResumptionPsk, |
| 69 |
}, |
| 70 |
}; |
| 71 |
|
| 72 |
#[cfg(feature = "private_message")] |
| 73 |
use crate::group::framing::PrivateMessage; |
| 74 |
|
| 75 |
use alloc::boxed::Box; |
| 76 |
|
| 77 |
/// The result of processing an [ExternalGroup](ExternalGroup) message using |
| 78 |
/// [process_incoming_message](ExternalGroup::process_incoming_message) |
| 79 |
#[derive(Clone, Debug)] |
| 80 |
#[allow(clippy::large_enum_variant)] |
| 81 |
pub enum ExternalReceivedMessage { |
| 82 |
/// State update as the result of a successful commit. |
| 83 |
Commit(CommitMessageDescription), |
| 84 |
/// Received proposal and its unique identifier. |
| 85 |
Proposal(ProposalMessageDescription), |
| 86 |
/// Encrypted message that can not be processed. |
| 87 |
Ciphertext(ContentType), |
| 88 |
/// Validated GroupInfo object |
| 89 |
GroupInfo(GroupInfo), |
| 90 |
/// Validated welcome message |
| 91 |
Welcome, |
| 92 |
/// Validated key package |
| 93 |
KeyPackage(KeyPackage), |
| 94 |
} |
| 95 |
|
| 96 |
/// A handle to an observed group that can track plaintext control messages |
| 97 |
/// and the resulting group state. |
| 98 |
#[derive(Clone)] |
| 99 |
pub struct ExternalGroup<C> |
| 100 |
where |
| 101 |
C: ExternalClientConfig, |
| 102 |
{ |
| 103 |
pub(crate) config: C, |
| 104 |
pub(crate) cipher_suite_provider: <C::CryptoProvider as CryptoProvider>::CipherSuiteProvider, |
| 105 |
pub(crate) state: GroupState, |
| 106 |
pub(crate) signing_data: Option<(SignatureSecretKey, SigningIdentity)>, |
| 107 |
} |
| 108 |
|
| 109 |
impl<C: ExternalClientConfig + Clone> ExternalGroup<C> { |
| 110 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 111 |
pub(crate) async fn join( |
| 112 |
config: C, |
| 113 |
signing_data: Option<(SignatureSecretKey, SigningIdentity)>, |
| 114 |
group_info: MlsMessage, |
| 115 |
tree_data: Option<ExportedTree<'_>>, |
| 116 |
) -> Result<Self, MlsError> { |
| 117 |
let protocol_version = group_info.version; |
| 118 |
|
| 119 |
if !config.version_supported(protocol_version) { |
| 120 |
return Err(MlsError::UnsupportedProtocolVersion(protocol_version)); |
| 121 |
} |
| 122 |
|
| 123 |
let group_info = group_info |
| 124 |
.into_group_info() |
| 125 |
.ok_or(MlsError::UnexpectedMessageType)?; |
| 126 |
|
| 127 |
let cipher_suite_provider = cipher_suite_provider( |
| 128 |
config.crypto_provider(), |
| 129 |
group_info.group_context.cipher_suite, |
| 130 |
)?; |
| 131 |
|
| 132 |
let public_tree = validate_tree_and_info_joiner( |
| 133 |
protocol_version, |
| 134 |
&group_info, |
| 135 |
tree_data, |
| 136 |
&config.identity_provider(), |
| 137 |
&cipher_suite_provider, |
| 138 |
) |
| 139 |
.await?; |
| 140 |
|
| 141 |
let interim_transcript_hash = InterimTranscriptHash::create( |
| 142 |
&cipher_suite_provider, |
| 143 |
&group_info.group_context.confirmed_transcript_hash, |
| 144 |
&group_info.confirmation_tag, |
| 145 |
) |
| 146 |
.await?; |
| 147 |
|
| 148 |
Ok(Self { |
| 149 |
config, |
| 150 |
signing_data, |
| 151 |
state: GroupState::new( |
| 152 |
group_info.group_context, |
| 153 |
public_tree, |
| 154 |
interim_transcript_hash, |
| 155 |
group_info.confirmation_tag, |
| 156 |
), |
| 157 |
cipher_suite_provider, |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
/// Process a message that was sent to the group. |
| 162 |
/// |
| 163 |
/// * Proposals will be stored in the group state and processed by the |
| 164 |
/// same rules as a standard group. |
| 165 |
/// |
| 166 |
/// * Commits will result in the same outcome as a standard group. |
| 167 |
/// However, the integrity of the resulting group state can only be partially |
| 168 |
/// verified, since the external group does have access to the group |
| 169 |
/// secrets required to do a complete check. |
| 170 |
/// |
| 171 |
/// * Application messages are always encrypted so they result in a no-op |
| 172 |
/// that returns [ExternalReceivedMessage::Ciphertext] |
| 173 |
/// |
| 174 |
/// # Warning |
| 175 |
/// |
| 176 |
/// Processing an encrypted commit or proposal message has the same result |
| 177 |
/// as processing an encrypted application message. Proper tracking of |
| 178 |
/// the group state requires that all proposal and commit messages are |
| 179 |
/// readable. |
| 180 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 181 |
pub async fn process_incoming_message( |
| 182 |
&mut self, |
| 183 |
message: MlsMessage, |
| 184 |
) -> Result<ExternalReceivedMessage, MlsError> { |
| 185 |
MessageProcessor::process_incoming_message( |
| 186 |
self, |
| 187 |
message, |
| 188 |
#[cfg(feature = "by_ref_proposal")] |
| 189 |
self.config.cache_proposals(), |
| 190 |
) |
| 191 |
.await |
| 192 |
} |
| 193 |
|
| 194 |
/// Replay a proposal message into the group skipping all validation steps. |
| 195 |
#[cfg(feature = "by_ref_proposal")] |
| 196 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 197 |
pub async fn insert_proposal_from_message( |
| 198 |
&mut self, |
| 199 |
message: MlsMessage, |
| 200 |
) -> Result<(), MlsError> { |
| 201 |
let ptxt = match message.payload { |
| 202 |
MlsMessagePayload::Plain(p) => Ok(p), |
| 203 |
_ => Err(MlsError::UnexpectedMessageType), |
| 204 |
}?; |
| 205 |
|
| 206 |
let auth_content: AuthenticatedContent = ptxt.into(); |
| 207 |
|
| 208 |
let proposal_ref = |
| 209 |
ProposalRef::from_content(&self.cipher_suite_provider, &auth_content).await?; |
| 210 |
|
| 211 |
let sender = auth_content.content.sender; |
| 212 |
|
| 213 |
let proposal = match auth_content.content.content { |
| 214 |
Content::Proposal(p) => Ok(*p), |
| 215 |
_ => Err(MlsError::UnexpectedMessageType), |
| 216 |
}?; |
| 217 |
|
| 218 |
self.group_state_mut() |
| 219 |
.proposals |
| 220 |
.insert(proposal_ref, proposal, sender); |
| 221 |
|
| 222 |
Ok(()) |
| 223 |
} |
| 224 |
|
| 225 |
/// Force insert a proposal directly into the internal state of the group |
| 226 |
/// with no validation. |
| 227 |
#[cfg(feature = "by_ref_proposal")] |
| 228 |
pub fn insert_proposal(&mut self, proposal: CachedProposal) { |
| 229 |
self.group_state_mut().proposals.insert( |
| 230 |
proposal.proposal_ref, |
| 231 |
proposal.proposal, |
| 232 |
proposal.sender, |
| 233 |
) |
| 234 |
} |
| 235 |
|
| 236 |
/// Create an external proposal to request that a group add a new member |
| 237 |
/// |
| 238 |
/// # Warning |
| 239 |
/// |
| 240 |
/// In order for the proposal generated by this function to be successfully |
| 241 |
/// committed, the group needs to have `signing_identity` as an entry |
| 242 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 243 |
/// as part of its group context extensions. |
| 244 |
#[cfg(feature = "by_ref_proposal")] |
| 245 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 246 |
pub async fn propose_add( |
| 247 |
&mut self, |
| 248 |
key_package: MlsMessage, |
| 249 |
authenticated_data: Vec<u8>, |
| 250 |
) -> Result<MlsMessage, MlsError> { |
| 251 |
let key_package = key_package |
| 252 |
.into_key_package() |
| 253 |
.ok_or(MlsError::UnexpectedMessageType)?; |
| 254 |
|
| 255 |
self.propose( |
| 256 |
Proposal::Add(alloc::boxed::Box::new(AddProposal { key_package })), |
| 257 |
authenticated_data, |
| 258 |
) |
| 259 |
.await |
| 260 |
} |
| 261 |
|
| 262 |
/// Create an external proposal to request that a group remove an existing member |
| 263 |
/// |
| 264 |
/// # Warning |
| 265 |
/// |
| 266 |
/// In order for the proposal generated by this function to be successfully |
| 267 |
/// committed, the group needs to have `signing_identity` as an entry |
| 268 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 269 |
/// as part of its group context extensions. |
| 270 |
#[cfg(feature = "by_ref_proposal")] |
| 271 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 272 |
pub async fn propose_remove( |
| 273 |
&mut self, |
| 274 |
index: u32, |
| 275 |
authenticated_data: Vec<u8>, |
| 276 |
) -> Result<MlsMessage, MlsError> { |
| 277 |
let to_remove = LeafIndex(index); |
| 278 |
|
| 279 |
// Verify that this leaf is actually in the tree |
| 280 |
self.group_state().public_tree.get_leaf_node(to_remove)?; |
| 281 |
|
| 282 |
self.propose( |
| 283 |
Proposal::Remove(RemoveProposal { to_remove }), |
| 284 |
authenticated_data, |
| 285 |
) |
| 286 |
.await |
| 287 |
} |
| 288 |
|
| 289 |
/// Create an external proposal to request that a group inserts an external |
| 290 |
/// pre shared key into its state. |
| 291 |
/// |
| 292 |
/// # Warning |
| 293 |
/// |
| 294 |
/// In order for the proposal generated by this function to be successfully |
| 295 |
/// committed, the group needs to have `signing_identity` as an entry |
| 296 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 297 |
/// as part of its group context extensions. |
| 298 |
#[cfg(all(feature = "by_ref_proposal", feature = "psk"))] |
| 299 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 300 |
pub async fn propose_external_psk( |
| 301 |
&mut self, |
| 302 |
psk: ExternalPskId, |
| 303 |
authenticated_data: Vec<u8>, |
| 304 |
) -> Result<MlsMessage, MlsError> { |
| 305 |
let proposal = self.psk_proposal(JustPreSharedKeyID::External(psk))?; |
| 306 |
self.propose(proposal, authenticated_data).await |
| 307 |
} |
| 308 |
|
| 309 |
/// Create an external proposal to request that a group adds a pre shared key |
| 310 |
/// from a previous epoch to the current group state. |
| 311 |
/// |
| 312 |
/// # Warning |
| 313 |
/// |
| 314 |
/// In order for the proposal generated by this function to be successfully |
| 315 |
/// committed, the group needs to have `signing_identity` as an entry |
| 316 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 317 |
/// as part of its group context extensions. |
| 318 |
#[cfg(all(feature = "by_ref_proposal", feature = "psk"))] |
| 319 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 320 |
pub async fn propose_resumption_psk( |
| 321 |
&mut self, |
| 322 |
psk_epoch: u64, |
| 323 |
authenticated_data: Vec<u8>, |
| 324 |
) -> Result<MlsMessage, MlsError> { |
| 325 |
let key_id = ResumptionPsk { |
| 326 |
psk_epoch, |
| 327 |
usage: ResumptionPSKUsage::Application, |
| 328 |
psk_group_id: PskGroupId(self.group_context().group_id().to_vec()), |
| 329 |
}; |
| 330 |
|
| 331 |
let proposal = self.psk_proposal(JustPreSharedKeyID::Resumption(key_id))?; |
| 332 |
self.propose(proposal, authenticated_data).await |
| 333 |
} |
| 334 |
|
| 335 |
#[cfg(all(feature = "by_ref_proposal", feature = "psk"))] |
| 336 |
fn psk_proposal(&self, key_id: JustPreSharedKeyID) -> Result<Proposal, MlsError> { |
| 337 |
Ok(Proposal::Psk(PreSharedKeyProposal { |
| 338 |
psk: PreSharedKeyID { |
| 339 |
key_id, |
| 340 |
psk_nonce: PskNonce::random(&self.cipher_suite_provider) |
| 341 |
.map_err(|e| MlsError::CryptoProviderError(e.into_any_error()))?, |
| 342 |
}, |
| 343 |
})) |
| 344 |
} |
| 345 |
|
| 346 |
/// Create an external proposal to request that a group sets extensions stored in the group |
| 347 |
/// state. |
| 348 |
/// |
| 349 |
/// # Warning |
| 350 |
/// |
| 351 |
/// In order for the proposal generated by this function to be successfully |
| 352 |
/// committed, the group needs to have `signing_identity` as an entry |
| 353 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 354 |
/// as part of its group context extensions. |
| 355 |
#[cfg(feature = "by_ref_proposal")] |
| 356 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 357 |
pub async fn propose_group_context_extensions( |
| 358 |
&mut self, |
| 359 |
extensions: ExtensionList, |
| 360 |
authenticated_data: Vec<u8>, |
| 361 |
) -> Result<MlsMessage, MlsError> { |
| 362 |
let proposal = Proposal::GroupContextExtensions(extensions); |
| 363 |
self.propose(proposal, authenticated_data).await |
| 364 |
} |
| 365 |
|
| 366 |
/// Create an external proposal to request that a group is reinitialized. |
| 367 |
/// |
| 368 |
/// # Warning |
| 369 |
/// |
| 370 |
/// In order for the proposal generated by this function to be successfully |
| 371 |
/// committed, the group needs to have `signing_identity` as an entry |
| 372 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 373 |
/// as part of its group context extensions. |
| 374 |
#[cfg(feature = "by_ref_proposal")] |
| 375 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 376 |
pub async fn propose_reinit( |
| 377 |
&mut self, |
| 378 |
group_id: Option<Vec<u8>>, |
| 379 |
version: ProtocolVersion, |
| 380 |
cipher_suite: CipherSuite, |
| 381 |
extensions: ExtensionList, |
| 382 |
authenticated_data: Vec<u8>, |
| 383 |
) -> Result<MlsMessage, MlsError> { |
| 384 |
let group_id = group_id.map(Ok).unwrap_or_else(|| { |
| 385 |
self.cipher_suite_provider |
| 386 |
.random_bytes_vec(self.cipher_suite_provider.kdf_extract_size()) |
| 387 |
.map_err(|e| MlsError::CryptoProviderError(e.into_any_error())) |
| 388 |
})?; |
| 389 |
|
| 390 |
let proposal = Proposal::ReInit(ReInitProposal { |
| 391 |
group_id, |
| 392 |
version, |
| 393 |
cipher_suite, |
| 394 |
extensions, |
| 395 |
}); |
| 396 |
|
| 397 |
self.propose(proposal, authenticated_data).await |
| 398 |
} |
| 399 |
|
| 400 |
/// Create a custom proposal message. |
| 401 |
/// |
| 402 |
/// # Warning |
| 403 |
/// |
| 404 |
/// In order for the proposal generated by this function to be successfully |
| 405 |
/// committed, the group needs to have `signing_identity` as an entry |
| 406 |
/// within an [ExternalSendersExt](crate::extension::built_in::ExternalSendersExt) |
| 407 |
/// as part of its group context extensions. |
| 408 |
#[cfg(all(feature = "by_ref_proposal", feature = "custom_proposal"))] |
| 409 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 410 |
pub async fn propose_custom( |
| 411 |
&mut self, |
| 412 |
proposal: CustomProposal, |
| 413 |
authenticated_data: Vec<u8>, |
| 414 |
) -> Result<MlsMessage, MlsError> { |
| 415 |
self.propose(Proposal::Custom(proposal), authenticated_data) |
| 416 |
.await |
| 417 |
} |
| 418 |
|
| 419 |
/// Issue an external proposal. |
| 420 |
/// |
| 421 |
/// This function is useful for reissuing external proposals that |
| 422 |
/// are returned in [crate::group::NewEpoch::unused_proposals] |
| 423 |
/// after a commit is processed. |
| 424 |
#[cfg(feature = "by_ref_proposal")] |
| 425 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 426 |
pub async fn propose( |
| 427 |
&mut self, |
| 428 |
proposal: Proposal, |
| 429 |
authenticated_data: Vec<u8>, |
| 430 |
) -> Result<MlsMessage, MlsError> { |
| 431 |
let (signer, signing_identity) = |
| 432 |
self.signing_data.as_ref().ok_or(MlsError::SignerNotFound)?; |
| 433 |
|
| 434 |
let external_senders_ext = self |
| 435 |
.state |
| 436 |
.context |
| 437 |
.extensions |
| 438 |
.get_as::<ExternalSendersExt>()? |
| 439 |
.ok_or(MlsError::ExternalProposalsDisabled)?; |
| 440 |
|
| 441 |
let sender_index = external_senders_ext |
| 442 |
.allowed_senders |
| 443 |
.iter() |
| 444 |
.position(|allowed_signer| signing_identity == allowed_signer) |
| 445 |
.ok_or(MlsError::InvalidExternalSigningIdentity)?; |
| 446 |
|
| 447 |
let sender = Sender::External(sender_index as u32); |
| 448 |
|
| 449 |
let auth_content = AuthenticatedContent::new_signed( |
| 450 |
&self.cipher_suite_provider, |
| 451 |
&self.state.context, |
| 452 |
sender, |
| 453 |
Content::Proposal(Box::new(proposal.clone())), |
| 454 |
signer, |
| 455 |
WireFormat::PublicMessage, |
| 456 |
authenticated_data, |
| 457 |
) |
| 458 |
.await?; |
| 459 |
|
| 460 |
let proposal_ref = |
| 461 |
ProposalRef::from_content(&self.cipher_suite_provider, &auth_content).await?; |
| 462 |
|
| 463 |
let plaintext = PublicMessage { |
| 464 |
content: auth_content.content, |
| 465 |
auth: auth_content.auth, |
| 466 |
membership_tag: None, |
| 467 |
}; |
| 468 |
|
| 469 |
let message = MlsMessage::new( |
| 470 |
self.group_context().version(), |
| 471 |
MlsMessagePayload::Plain(plaintext), |
| 472 |
); |
| 473 |
|
| 474 |
self.state.proposals.insert(proposal_ref, proposal, sender); |
| 475 |
|
| 476 |
Ok(message) |
| 477 |
} |
| 478 |
|
| 479 |
/// Delete all sent and received proposals cached for commit. |
| 480 |
#[cfg(feature = "by_ref_proposal")] |
| 481 |
pub fn clear_proposal_cache(&mut self) { |
| 482 |
self.state.proposals.clear() |
| 483 |
} |
| 484 |
|
| 485 |
#[inline(always)] |
| 486 |
pub(crate) fn group_state(&self) -> &GroupState { |
| 487 |
&self.state |
| 488 |
} |
| 489 |
|
| 490 |
/// Get the current group context summarizing various information about the group. |
| 491 |
#[inline(always)] |
| 492 |
pub fn group_context(&self) -> &GroupContext { |
| 493 |
&self.group_state().context |
| 494 |
} |
| 495 |
|
| 496 |
/// Export the current ratchet tree used within the group. |
| 497 |
pub fn export_tree(&self) -> Result<Vec<u8>, MlsError> { |
| 498 |
self.group_state() |
| 499 |
.public_tree |
| 500 |
.nodes |
| 501 |
.mls_encode_to_vec() |
| 502 |
.map_err(Into::into) |
| 503 |
} |
| 504 |
|
| 505 |
/// Get the current roster of the group. |
| 506 |
#[inline(always)] |
| 507 |
pub fn roster(&self) -> Roster { |
| 508 |
self.group_state().public_tree.roster() |
| 509 |
} |
| 510 |
|
| 511 |
/// Get the |
| 512 |
/// [transcript hash](https://messaginglayersecurity.rocks/mls-protocol/draft-ietf-mls-protocol.html#name-transcript-hashes) |
| 513 |
/// for the current epoch that the group is in. |
| 514 |
#[inline(always)] |
| 515 |
pub fn transcript_hash(&self) -> &Vec<u8> { |
| 516 |
&self.group_state().context.confirmed_transcript_hash |
| 517 |
} |
| 518 |
|
| 519 |
/// Get the |
| 520 |
/// [tree hash](https://www.rfc-editor.org/rfc/rfc9420.html#name-tree-hashes) |
| 521 |
/// for the current epoch that the group is in. |
| 522 |
#[inline(always)] |
| 523 |
pub fn tree_hash(&self) -> &[u8] { |
| 524 |
&self.group_state().context.tree_hash |
| 525 |
} |
| 526 |
|
| 527 |
/// Find a member based on their identity. |
| 528 |
/// |
| 529 |
/// Identities are matched based on the |
| 530 |
/// [IdentityProvider](crate::IdentityProvider) |
| 531 |
/// that this group was configured with. |
| 532 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 533 |
pub async fn get_member_with_identity( |
| 534 |
&self, |
| 535 |
identity_id: &SigningIdentity, |
| 536 |
) -> Result<Member, MlsError> { |
| 537 |
let identity = self |
| 538 |
.identity_provider() |
| 539 |
.identity(identity_id, self.group_context().extensions()) |
| 540 |
.await |
| 541 |
.map_err(|error| MlsError::IdentityProviderError(error.into_any_error()))?; |
| 542 |
|
| 543 |
let tree = &self.group_state().public_tree; |
| 544 |
|
| 545 |
#[cfg(feature = "tree_index")] |
| 546 |
let index = tree.get_leaf_node_with_identity(&identity); |
| 547 |
|
| 548 |
#[cfg(not(feature = "tree_index"))] |
| 549 |
let index = tree |
| 550 |
.get_leaf_node_with_identity( |
| 551 |
&identity, |
| 552 |
&self.identity_provider(), |
| 553 |
self.group_context().extensions(), |
| 554 |
) |
| 555 |
.await?; |
| 556 |
|
| 557 |
let index = index.ok_or(MlsError::MemberNotFound)?; |
| 558 |
let node = self.group_state().public_tree.get_leaf_node(index)?; |
| 559 |
|
| 560 |
Ok(member_from_leaf_node(node, index)) |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 565 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 566 |
#[cfg_attr( |
| 567 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 568 |
maybe_async::must_be_async |
| 569 |
)] |
| 570 |
impl<C> MessageProcessor for ExternalGroup<C> |
| 571 |
where |
| 572 |
C: ExternalClientConfig + Clone, |
| 573 |
{ |
| 574 |
type MlsRules = C::MlsRules; |
| 575 |
type IdentityProvider = C::IdentityProvider; |
| 576 |
type PreSharedKeyStorage = AlwaysFoundPskStorage; |
| 577 |
type OutputType = ExternalReceivedMessage; |
| 578 |
type CipherSuiteProvider = <C::CryptoProvider as CryptoProvider>::CipherSuiteProvider; |
| 579 |
|
| 580 |
fn mls_rules(&self) -> Self::MlsRules { |
| 581 |
self.config.mls_rules() |
| 582 |
} |
| 583 |
|
| 584 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 585 |
async fn verify_plaintext_authentication( |
| 586 |
&self, |
| 587 |
message: PublicMessage, |
| 588 |
) -> Result<EventOrContent<Self::OutputType>, MlsError> { |
| 589 |
let auth_content = crate::group::message_verifier::verify_plaintext_authentication( |
| 590 |
&self.cipher_suite_provider, |
| 591 |
message, |
| 592 |
None, |
| 593 |
&self.state, |
| 594 |
) |
| 595 |
.await?; |
| 596 |
|
| 597 |
Ok(EventOrContent::Content(auth_content)) |
| 598 |
} |
| 599 |
|
| 600 |
#[cfg(feature = "private_message")] |
| 601 |
async fn process_ciphertext( |
| 602 |
&mut self, |
| 603 |
cipher_text: &PrivateMessage, |
| 604 |
) -> Result<EventOrContent<Self::OutputType>, MlsError> { |
| 605 |
Ok(EventOrContent::Event(ExternalReceivedMessage::Ciphertext( |
| 606 |
cipher_text.content_type, |
| 607 |
))) |
| 608 |
} |
| 609 |
|
| 610 |
async fn update_key_schedule( |
| 611 |
&mut self, |
| 612 |
_secrets: Option<(TreeKemPrivate, PathSecret)>, |
| 613 |
interim_transcript_hash: InterimTranscriptHash, |
| 614 |
confirmation_tag: &ConfirmationTag, |
| 615 |
provisional_public_state: ProvisionalState, |
| 616 |
) -> Result<(), MlsError> { |
| 617 |
self.state.context = provisional_public_state.group_context; |
| 618 |
#[cfg(feature = "by_ref_proposal")] |
| 619 |
self.state.proposals.clear(); |
| 620 |
self.state.interim_transcript_hash = interim_transcript_hash; |
| 621 |
self.state.public_tree = provisional_public_state.public_tree; |
| 622 |
self.state.confirmation_tag = confirmation_tag.clone(); |
| 623 |
|
| 624 |
Ok(()) |
| 625 |
} |
| 626 |
|
| 627 |
fn identity_provider(&self) -> Self::IdentityProvider { |
| 628 |
self.config.identity_provider() |
| 629 |
} |
| 630 |
|
| 631 |
fn psk_storage(&self) -> Self::PreSharedKeyStorage { |
| 632 |
AlwaysFoundPskStorage |
| 633 |
} |
| 634 |
|
| 635 |
fn group_state(&self) -> &GroupState { |
| 636 |
&self.state |
| 637 |
} |
| 638 |
|
| 639 |
fn group_state_mut(&mut self) -> &mut GroupState { |
| 640 |
&mut self.state |
| 641 |
} |
| 642 |
|
| 643 |
fn removal_proposal( |
| 644 |
&self, |
| 645 |
_provisional_state: &ProvisionalState, |
| 646 |
) -> Option<ProposalInfo<RemoveProposal>> { |
| 647 |
None |
| 648 |
} |
| 649 |
|
| 650 |
#[cfg(feature = "private_message")] |
| 651 |
fn min_epoch_available(&self) -> Option<u64> { |
| 652 |
self.config |
| 653 |
.max_epoch_jitter() |
| 654 |
.map(|j| self.state.context.epoch - j) |
| 655 |
} |
| 656 |
|
| 657 |
fn cipher_suite_provider(&self) -> &Self::CipherSuiteProvider { |
| 658 |
&self.cipher_suite_provider |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
/// Serializable snapshot of an [ExternalGroup](ExternalGroup) state. |
| 663 |
#[derive(Debug, MlsEncode, MlsSize, MlsDecode, PartialEq, Clone)] |
| 664 |
pub struct ExternalSnapshot { |
| 665 |
version: u16, |
| 666 |
pub(crate) state: RawGroupState, |
| 667 |
signing_data: Option<(SignatureSecretKey, SigningIdentity)>, |
| 668 |
} |
| 669 |
|
| 670 |
impl ExternalSnapshot { |
| 671 |
/// Serialize the snapshot |
| 672 |
pub fn to_bytes(&self) -> Result<Vec<u8>, MlsError> { |
| 673 |
Ok(self.mls_encode_to_vec()?) |
| 674 |
} |
| 675 |
|
| 676 |
/// Deserialize the snapshot |
| 677 |
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MlsError> { |
| 678 |
Ok(Self::mls_decode(&mut &*bytes)?) |
| 679 |
} |
| 680 |
|
| 681 |
/// Group context encoded in the snapshot |
| 682 |
pub fn context(&self) -> &GroupContext { |
| 683 |
&self.state.context |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
impl<C> ExternalGroup<C> |
| 688 |
where |
| 689 |
C: ExternalClientConfig + Clone, |
| 690 |
{ |
| 691 |
/// Create a snapshot of this group's current internal state. |
| 692 |
pub fn snapshot(&self) -> ExternalSnapshot { |
| 693 |
ExternalSnapshot { |
| 694 |
state: RawGroupState::export(self.group_state()), |
| 695 |
version: 1, |
| 696 |
signing_data: self.signing_data.clone(), |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
/// Create a snapshot of this group's current internal state. |
| 701 |
/// The tree is not included in the state and can be stored |
| 702 |
/// separately by calling [`Group::export_tree`]. |
| 703 |
pub fn snapshot_without_ratchet_tree(&mut self) -> ExternalSnapshot { |
| 704 |
let tree = std::mem::take(&mut self.state.public_tree.nodes); |
| 705 |
|
| 706 |
let snapshot = ExternalSnapshot { |
| 707 |
state: RawGroupState::export(&self.state), |
| 708 |
version: 1, |
| 709 |
signing_data: self.signing_data.clone(), |
| 710 |
}; |
| 711 |
|
| 712 |
self.state.public_tree.nodes = tree; |
| 713 |
|
| 714 |
snapshot |
| 715 |
} |
| 716 |
|
| 717 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 718 |
pub(crate) async fn from_snapshot( |
| 719 |
config: C, |
| 720 |
snapshot: ExternalSnapshot, |
| 721 |
) -> Result<Self, MlsError> { |
| 722 |
#[cfg(feature = "tree_index")] |
| 723 |
let identity_provider = config.identity_provider(); |
| 724 |
|
| 725 |
let cipher_suite_provider = cipher_suite_provider( |
| 726 |
config.crypto_provider(), |
| 727 |
snapshot.state.context.cipher_suite, |
| 728 |
)?; |
| 729 |
|
| 730 |
Ok(ExternalGroup { |
| 731 |
config, |
| 732 |
signing_data: snapshot.signing_data, |
| 733 |
state: snapshot |
| 734 |
.state |
| 735 |
.import( |
| 736 |
#[cfg(feature = "tree_index")] |
| 737 |
&identity_provider, |
| 738 |
) |
| 739 |
.await?, |
| 740 |
cipher_suite_provider, |
| 741 |
}) |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
impl From<CommitMessageDescription> for ExternalReceivedMessage { |
| 746 |
fn from(value: CommitMessageDescription) -> Self { |
| 747 |
ExternalReceivedMessage::Commit(value) |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
impl TryFrom<ApplicationMessageDescription> for ExternalReceivedMessage { |
| 752 |
type Error = MlsError; |
| 753 |
|
| 754 |
fn try_from(_: ApplicationMessageDescription) -> Result<Self, Self::Error> { |
| 755 |
Err(MlsError::UnencryptedApplicationMessage) |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
impl From<ProposalMessageDescription> for ExternalReceivedMessage { |
| 760 |
fn from(value: ProposalMessageDescription) -> Self { |
| 761 |
ExternalReceivedMessage::Proposal(value) |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
impl From<GroupInfo> for ExternalReceivedMessage { |
| 766 |
fn from(value: GroupInfo) -> Self { |
| 767 |
ExternalReceivedMessage::GroupInfo(value) |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
impl From<Welcome> for ExternalReceivedMessage { |
| 772 |
fn from(_: Welcome) -> Self { |
| 773 |
ExternalReceivedMessage::Welcome |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
impl From<KeyPackage> for ExternalReceivedMessage { |
| 778 |
fn from(value: KeyPackage) -> Self { |
| 779 |
ExternalReceivedMessage::KeyPackage(value) |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
#[cfg(test)] |
| 784 |
pub(crate) mod test_utils { |
| 785 |
use crate::{ |
| 786 |
external_client::tests_utils::{TestExternalClientBuilder, TestExternalClientConfig}, |
| 787 |
group::test_utils::TestGroup, |
| 788 |
}; |
| 789 |
|
| 790 |
use super::ExternalGroup; |
| 791 |
|
| 792 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 793 |
pub(crate) async fn make_external_group( |
| 794 |
group: &TestGroup, |
| 795 |
) -> ExternalGroup<TestExternalClientConfig> { |
| 796 |
make_external_group_with_config( |
| 797 |
group, |
| 798 |
TestExternalClientBuilder::new_for_test().build_config(), |
| 799 |
) |
| 800 |
.await |
| 801 |
} |
| 802 |
|
| 803 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 804 |
pub(crate) async fn make_external_group_with_config( |
| 805 |
group: &TestGroup, |
| 806 |
config: TestExternalClientConfig, |
| 807 |
) -> ExternalGroup<TestExternalClientConfig> { |
| 808 |
ExternalGroup::join( |
| 809 |
config, |
| 810 |
None, |
| 811 |
group |
| 812 |
.group_info_message_allowing_ext_commit(true) |
| 813 |
.await |
| 814 |
.unwrap(), |
| 815 |
None, |
| 816 |
) |
| 817 |
.await |
| 818 |
.unwrap() |
| 819 |
} |
| 820 |
} |
| 821 |
|
| 822 |
#[cfg(test)] |
| 823 |
mod tests { |
| 824 |
use super::test_utils::make_external_group; |
| 825 |
use crate::{ |
| 826 |
cipher_suite::CipherSuite, |
| 827 |
client::{ |
| 828 |
test_utils::{TEST_CIPHER_SUITE, TEST_PROTOCOL_VERSION}, |
| 829 |
MlsError, |
| 830 |
}, |
| 831 |
crypto::{test_utils::TestCryptoProvider, SignatureSecretKey}, |
| 832 |
extension::ExternalSendersExt, |
| 833 |
external_client::{ |
| 834 |
group::test_utils::make_external_group_with_config, |
| 835 |
tests_utils::{TestExternalClientBuilder, TestExternalClientConfig}, |
| 836 |
ExternalClient, ExternalGroup, ExternalReceivedMessage, ExternalSnapshot, |
| 837 |
}, |
| 838 |
group::{ |
| 839 |
framing::{Content, MlsMessagePayload}, |
| 840 |
message_processor::CommitEffect, |
| 841 |
proposal::{AddProposal, Proposal, ProposalOrRef}, |
| 842 |
proposal_ref::ProposalRef, |
| 843 |
test_utils::{test_group, TestGroup}, |
| 844 |
CommitMessageDescription, ExportedTree, ProposalMessageDescription, |
| 845 |
}, |
| 846 |
identity::{test_utils::get_test_signing_identity, SigningIdentity}, |
| 847 |
key_package::test_utils::{test_key_package, test_key_package_message}, |
| 848 |
protocol_version::ProtocolVersion, |
| 849 |
ExtensionList, MlsMessage, |
| 850 |
}; |
| 851 |
use assert_matches::assert_matches; |
| 852 |
use mls_rs_codec::{MlsDecode, MlsEncode}; |
| 853 |
|
| 854 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 855 |
async fn test_group_with_one_commit(v: ProtocolVersion, cs: CipherSuite) -> TestGroup { |
| 856 |
let mut group = test_group(v, cs).await; |
| 857 |
group.commit(Vec::new()).await.unwrap(); |
| 858 |
group.process_pending_commit().await.unwrap(); |
| 859 |
group |
| 860 |
} |
| 861 |
|
| 862 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 863 |
async fn test_group_two_members( |
| 864 |
v: ProtocolVersion, |
| 865 |
cs: CipherSuite, |
| 866 |
#[cfg(feature = "by_ref_proposal")] ext_identity: Option<SigningIdentity>, |
| 867 |
) -> TestGroup { |
| 868 |
let mut group = test_group_with_one_commit(v, cs).await; |
| 869 |
|
| 870 |
let bob_key_package = test_key_package_message(v, cs, "bob").await; |
| 871 |
|
| 872 |
let mut commit_builder = group.commit_builder().add_member(bob_key_package).unwrap(); |
| 873 |
|
| 874 |
#[cfg(feature = "by_ref_proposal")] |
| 875 |
if let Some(ext_signer) = ext_identity { |
| 876 |
let mut ext_list = ExtensionList::new(); |
| 877 |
|
| 878 |
ext_list |
| 879 |
.set_from(ExternalSendersExt { |
| 880 |
allowed_senders: vec![ext_signer], |
| 881 |
}) |
| 882 |
.unwrap(); |
| 883 |
|
| 884 |
commit_builder = commit_builder.set_group_context_ext(ext_list).unwrap(); |
| 885 |
} |
| 886 |
|
| 887 |
commit_builder.build().await.unwrap(); |
| 888 |
|
| 889 |
group.process_pending_commit().await.unwrap(); |
| 890 |
group |
| 891 |
} |
| 892 |
|
| 893 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 894 |
async fn external_group_can_be_created() { |
| 895 |
for (v, cs) in ProtocolVersion::all().flat_map(|v| { |
| 896 |
TestCryptoProvider::all_supported_cipher_suites() |
| 897 |
.into_iter() |
| 898 |
.map(move |cs| (v, cs)) |
| 899 |
}) { |
| 900 |
make_external_group(&test_group_with_one_commit(v, cs).await).await; |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 905 |
async fn external_group_can_process_commit() { |
| 906 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 907 |
let mut server = make_external_group(&alice).await; |
| 908 |
let commit_output = alice.commit(Vec::new()).await.unwrap(); |
| 909 |
alice.apply_pending_commit().await.unwrap(); |
| 910 |
|
| 911 |
server |
| 912 |
.process_incoming_message(commit_output.commit_message) |
| 913 |
.await |
| 914 |
.unwrap(); |
| 915 |
|
| 916 |
assert_eq!(alice.state, server.state); |
| 917 |
} |
| 918 |
|
| 919 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 920 |
async fn external_group_can_process_proposals_by_reference() { |
| 921 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 922 |
let mut server = make_external_group(&alice).await; |
| 923 |
|
| 924 |
let bob_key_package = |
| 925 |
test_key_package(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE, "bob").await; |
| 926 |
|
| 927 |
let add_proposal = Proposal::Add(Box::new(AddProposal { |
| 928 |
key_package: bob_key_package, |
| 929 |
})); |
| 930 |
|
| 931 |
let packet = alice.propose(add_proposal.clone()).await; |
| 932 |
|
| 933 |
let proposal_process = server.process_incoming_message(packet).await.unwrap(); |
| 934 |
|
| 935 |
assert_matches!( |
| 936 |
proposal_process, |
| 937 |
ExternalReceivedMessage::Proposal(ProposalMessageDescription { ref proposal, ..}) if proposal == &add_proposal |
| 938 |
); |
| 939 |
|
| 940 |
let commit_output = alice.commit(vec![]).await.unwrap(); |
| 941 |
alice.apply_pending_commit().await.unwrap(); |
| 942 |
|
| 943 |
let new_epoch = match server |
| 944 |
.process_incoming_message(commit_output.commit_message) |
| 945 |
.await |
| 946 |
.unwrap() |
| 947 |
{ |
| 948 |
ExternalReceivedMessage::Commit(CommitMessageDescription { |
| 949 |
effect: CommitEffect::NewEpoch(new_epoch), |
| 950 |
.. |
| 951 |
}) => new_epoch, |
| 952 |
_ => panic!("Expected processed commit"), |
| 953 |
}; |
| 954 |
|
| 955 |
assert_eq!(new_epoch.applied_proposals.len(), 1); |
| 956 |
|
| 957 |
assert!(new_epoch |
| 958 |
.applied_proposals |
| 959 |
.into_iter() |
| 960 |
.any(|p| p.proposal == add_proposal)); |
| 961 |
|
| 962 |
assert_eq!(alice.state, server.state); |
| 963 |
} |
| 964 |
|
| 965 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 966 |
async fn external_group_can_process_commit_adding_member() { |
| 967 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 968 |
let mut server = make_external_group(&alice).await; |
| 969 |
let (_, commit) = alice.join("bob").await; |
| 970 |
|
| 971 |
let new_epoch = match server.process_incoming_message(commit).await.unwrap() { |
| 972 |
ExternalReceivedMessage::Commit(CommitMessageDescription { |
| 973 |
effect: CommitEffect::NewEpoch(new_epoch), |
| 974 |
.. |
| 975 |
}) => new_epoch, |
| 976 |
_ => panic!("Expected processed commit"), |
| 977 |
}; |
| 978 |
|
| 979 |
assert_eq!(new_epoch.applied_proposals.len(), 1); |
| 980 |
|
| 981 |
assert_eq!( |
| 982 |
new_epoch |
| 983 |
.applied_proposals |
| 984 |
.into_iter() |
| 985 |
.filter(|p| matches!(p.proposal, Proposal::Add(_))) |
| 986 |
.count(), |
| 987 |
1 |
| 988 |
); |
| 989 |
|
| 990 |
assert_eq!(server.state.public_tree.get_leaf_nodes().len(), 2); |
| 991 |
|
| 992 |
assert_eq!(alice.state, server.state); |
| 993 |
} |
| 994 |
|
| 995 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 996 |
async fn external_group_rejects_commit_not_for_current_epoch() { |
| 997 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 998 |
let mut server = make_external_group(&alice).await; |
| 999 |
|
| 1000 |
let mut commit_output = alice.commit(vec![]).await.unwrap(); |
| 1001 |
|
| 1002 |
match commit_output.commit_message.payload { |
| 1003 |
MlsMessagePayload::Plain(ref mut plain) => plain.content.epoch = 0, |
| 1004 |
_ => panic!("Unexpected non-plaintext data"), |
| 1005 |
}; |
| 1006 |
|
| 1007 |
let res = server |
| 1008 |
.process_incoming_message(commit_output.commit_message) |
| 1009 |
.await; |
| 1010 |
|
| 1011 |
assert_matches!(res, Err(MlsError::InvalidEpoch)); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1015 |
async fn external_group_can_reject_message_with_invalid_signature() { |
| 1016 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1017 |
|
| 1018 |
let mut server = make_external_group_with_config( |
| 1019 |
&alice, |
| 1020 |
TestExternalClientBuilder::new_for_test().build_config(), |
| 1021 |
) |
| 1022 |
.await; |
| 1023 |
|
| 1024 |
let mut commit_output = alice.commit(Vec::new()).await.unwrap(); |
| 1025 |
|
| 1026 |
match commit_output.commit_message.payload { |
| 1027 |
MlsMessagePayload::Plain(ref mut plain) => plain.auth.signature = Vec::new().into(), |
| 1028 |
_ => panic!("Unexpected non-plaintext data"), |
| 1029 |
}; |
| 1030 |
|
| 1031 |
let res = server |
| 1032 |
.process_incoming_message(commit_output.commit_message) |
| 1033 |
.await; |
| 1034 |
|
| 1035 |
assert_matches!(res, Err(MlsError::InvalidSignature)); |
| 1036 |
} |
| 1037 |
|
| 1038 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1039 |
async fn external_group_rejects_unencrypted_application_message() { |
| 1040 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1041 |
let mut server = make_external_group(&alice).await; |
| 1042 |
|
| 1043 |
let plaintext = alice |
| 1044 |
.make_plaintext(Content::Application(b"hello".to_vec().into())) |
| 1045 |
.await; |
| 1046 |
|
| 1047 |
let res = server.process_incoming_message(plaintext).await; |
| 1048 |
|
| 1049 |
assert_matches!(res, Err(MlsError::UnencryptedApplicationMessage)); |
| 1050 |
} |
| 1051 |
|
| 1052 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1053 |
async fn external_group_will_reject_unsupported_cipher_suites() { |
| 1054 |
let alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1055 |
|
| 1056 |
let config = |
| 1057 |
TestExternalClientBuilder::new_for_test_disabling_cipher_suite(TEST_CIPHER_SUITE) |
| 1058 |
.build_config(); |
| 1059 |
|
| 1060 |
let res = ExternalGroup::join( |
| 1061 |
config, |
| 1062 |
None, |
| 1063 |
alice |
| 1064 |
.group_info_message_allowing_ext_commit(true) |
| 1065 |
.await |
| 1066 |
.unwrap(), |
| 1067 |
None, |
| 1068 |
) |
| 1069 |
.await |
| 1070 |
.map(|_| ()); |
| 1071 |
|
| 1072 |
assert_matches!( |
| 1073 |
res, |
| 1074 |
Err(MlsError::UnsupportedCipherSuite(TEST_CIPHER_SUITE)) |
| 1075 |
); |
| 1076 |
} |
| 1077 |
|
| 1078 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1079 |
async fn external_group_will_reject_unsupported_protocol_versions() { |
| 1080 |
let alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1081 |
|
| 1082 |
let config = TestExternalClientBuilder::new_for_test().build_config(); |
| 1083 |
|
| 1084 |
let mut group_info = alice |
| 1085 |
.group_info_message_allowing_ext_commit(true) |
| 1086 |
.await |
| 1087 |
.unwrap(); |
| 1088 |
|
| 1089 |
group_info.version = ProtocolVersion::from(64); |
| 1090 |
|
| 1091 |
let res = ExternalGroup::join(config, None, group_info, None) |
| 1092 |
.await |
| 1093 |
.map(|_| ()); |
| 1094 |
|
| 1095 |
assert_matches!( |
| 1096 |
res, |
| 1097 |
Err(MlsError::UnsupportedProtocolVersion(v)) if v == |
| 1098 |
ProtocolVersion::from(64) |
| 1099 |
); |
| 1100 |
} |
| 1101 |
|
| 1102 |
#[cfg(feature = "by_ref_proposal")] |
| 1103 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 1104 |
async fn setup_extern_proposal_test( |
| 1105 |
extern_proposals_allowed: bool, |
| 1106 |
) -> (SigningIdentity, SignatureSecretKey, TestGroup) { |
| 1107 |
let (server_identity, server_key) = |
| 1108 |
get_test_signing_identity(TEST_CIPHER_SUITE, b"server").await; |
| 1109 |
|
| 1110 |
let alice = test_group_two_members( |
| 1111 |
TEST_PROTOCOL_VERSION, |
| 1112 |
TEST_CIPHER_SUITE, |
| 1113 |
extern_proposals_allowed.then(|| server_identity.clone()), |
| 1114 |
) |
| 1115 |
.await; |
| 1116 |
|
| 1117 |
(server_identity, server_key, alice) |
| 1118 |
} |
| 1119 |
|
| 1120 |
#[cfg(feature = "by_ref_proposal")] |
| 1121 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 1122 |
async fn test_external_proposal( |
| 1123 |
server: &mut ExternalGroup<TestExternalClientConfig>, |
| 1124 |
alice: &mut TestGroup, |
| 1125 |
external_proposal: MlsMessage, |
| 1126 |
) { |
| 1127 |
let auth_content = external_proposal.clone().into_plaintext().unwrap().into(); |
| 1128 |
|
| 1129 |
let proposal_ref = ProposalRef::from_content(&server.cipher_suite_provider, &auth_content) |
| 1130 |
.await |
| 1131 |
.unwrap(); |
| 1132 |
|
| 1133 |
// Alice receives the proposal |
| 1134 |
alice.process_message(external_proposal).await.unwrap(); |
| 1135 |
|
| 1136 |
// Alice commits the proposal |
| 1137 |
let commit_output = alice.commit(vec![]).await.unwrap(); |
| 1138 |
|
| 1139 |
let commit = match commit_output |
| 1140 |
.commit_message |
| 1141 |
.clone() |
| 1142 |
.into_plaintext() |
| 1143 |
.unwrap() |
| 1144 |
.content |
| 1145 |
.content |
| 1146 |
{ |
| 1147 |
Content::Commit(commit) => commit, |
| 1148 |
_ => panic!("not a commit"), |
| 1149 |
}; |
| 1150 |
|
| 1151 |
// The proposal should be in the resulting commit |
| 1152 |
assert!(commit |
| 1153 |
.proposals |
| 1154 |
.contains(&ProposalOrRef::Reference(proposal_ref))); |
| 1155 |
|
| 1156 |
alice.process_pending_commit().await.unwrap(); |
| 1157 |
|
| 1158 |
server |
| 1159 |
.process_incoming_message(commit_output.commit_message) |
| 1160 |
.await |
| 1161 |
.unwrap(); |
| 1162 |
|
| 1163 |
assert_eq!(alice.state, server.state); |
| 1164 |
} |
| 1165 |
|
| 1166 |
#[cfg(feature = "by_ref_proposal")] |
| 1167 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1168 |
async fn external_group_can_propose_add() { |
| 1169 |
let (server_identity, server_key, mut alice) = setup_extern_proposal_test(true).await; |
| 1170 |
|
| 1171 |
let mut server = make_external_group(&alice).await; |
| 1172 |
|
| 1173 |
server.signing_data = Some((server_key, server_identity)); |
| 1174 |
|
| 1175 |
let charlie_key_package = |
| 1176 |
test_key_package_message(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE, "charlie").await; |
| 1177 |
|
| 1178 |
let external_proposal = server |
| 1179 |
.propose_add(charlie_key_package, vec![]) |
| 1180 |
.await |
| 1181 |
.unwrap(); |
| 1182 |
|
| 1183 |
test_external_proposal(&mut server, &mut alice, external_proposal).await |
| 1184 |
} |
| 1185 |
|
| 1186 |
#[cfg(feature = "by_ref_proposal")] |
| 1187 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1188 |
async fn external_group_can_propose_remove() { |
| 1189 |
let (server_identity, server_key, mut alice) = setup_extern_proposal_test(true).await; |
| 1190 |
|
| 1191 |
let mut server = make_external_group(&alice).await; |
| 1192 |
|
| 1193 |
server.signing_data = Some((server_key, server_identity)); |
| 1194 |
|
| 1195 |
let external_proposal = server.propose_remove(1, vec![]).await.unwrap(); |
| 1196 |
|
| 1197 |
test_external_proposal(&mut server, &mut alice, external_proposal).await |
| 1198 |
} |
| 1199 |
|
| 1200 |
#[cfg(feature = "by_ref_proposal")] |
| 1201 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1202 |
async fn external_group_external_proposal_not_allowed() { |
| 1203 |
let (signing_id, secret_key, alice) = setup_extern_proposal_test(false).await; |
| 1204 |
let mut server = make_external_group(&alice).await; |
| 1205 |
|
| 1206 |
server.signing_data = Some((secret_key, signing_id)); |
| 1207 |
|
| 1208 |
let charlie_key_package = |
| 1209 |
test_key_package_message(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE, "charlie").await; |
| 1210 |
|
| 1211 |
let res = server.propose_add(charlie_key_package, vec![]).await; |
| 1212 |
|
| 1213 |
assert_matches!(res, Err(MlsError::ExternalProposalsDisabled)); |
| 1214 |
} |
| 1215 |
|
| 1216 |
#[cfg(feature = "by_ref_proposal")] |
| 1217 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1218 |
async fn external_group_external_signing_identity_invalid() { |
| 1219 |
let (server_identity, server_key) = |
| 1220 |
get_test_signing_identity(TEST_CIPHER_SUITE, b"server").await; |
| 1221 |
|
| 1222 |
let alice = test_group_two_members( |
| 1223 |
TEST_PROTOCOL_VERSION, |
| 1224 |
TEST_CIPHER_SUITE, |
| 1225 |
Some( |
| 1226 |
get_test_signing_identity(TEST_CIPHER_SUITE, b"not server") |
| 1227 |
.await |
| 1228 |
.0, |
| 1229 |
), |
| 1230 |
) |
| 1231 |
.await; |
| 1232 |
|
| 1233 |
let mut server = make_external_group(&alice).await; |
| 1234 |
|
| 1235 |
server.signing_data = Some((server_key, server_identity)); |
| 1236 |
|
| 1237 |
let res = server.propose_remove(1, vec![]).await; |
| 1238 |
|
| 1239 |
assert_matches!(res, Err(MlsError::InvalidExternalSigningIdentity)); |
| 1240 |
} |
| 1241 |
|
| 1242 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1243 |
async fn external_group_errors_on_old_epoch() { |
| 1244 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1245 |
|
| 1246 |
let mut server = make_external_group_with_config( |
| 1247 |
&alice, |
| 1248 |
TestExternalClientBuilder::new_for_test() |
| 1249 |
.max_epoch_jitter(0) |
| 1250 |
.build_config(), |
| 1251 |
) |
| 1252 |
.await; |
| 1253 |
|
| 1254 |
let old_application_msg = alice |
| 1255 |
.encrypt_application_message(&[], vec![]) |
| 1256 |
.await |
| 1257 |
.unwrap(); |
| 1258 |
|
| 1259 |
let commit_output = alice.commit(vec![]).await.unwrap(); |
| 1260 |
|
| 1261 |
server |
| 1262 |
.process_incoming_message(commit_output.commit_message) |
| 1263 |
.await |
| 1264 |
.unwrap(); |
| 1265 |
|
| 1266 |
let res = server.process_incoming_message(old_application_msg).await; |
| 1267 |
|
| 1268 |
assert_matches!(res, Err(MlsError::InvalidEpoch)); |
| 1269 |
} |
| 1270 |
|
| 1271 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1272 |
async fn proposals_can_be_cached_externally() { |
| 1273 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1274 |
|
| 1275 |
let mut server = make_external_group_with_config( |
| 1276 |
&alice, |
| 1277 |
TestExternalClientBuilder::new_for_test() |
| 1278 |
.cache_proposals(false) |
| 1279 |
.build_config(), |
| 1280 |
) |
| 1281 |
.await; |
| 1282 |
|
| 1283 |
let proposal = alice.propose_update(vec![]).await.unwrap(); |
| 1284 |
|
| 1285 |
let commit_output = alice.commit(vec![]).await.unwrap(); |
| 1286 |
|
| 1287 |
server |
| 1288 |
.process_incoming_message(proposal.clone()) |
| 1289 |
.await |
| 1290 |
.unwrap(); |
| 1291 |
|
| 1292 |
server.insert_proposal_from_message(proposal).await.unwrap(); |
| 1293 |
|
| 1294 |
server |
| 1295 |
.process_incoming_message(commit_output.commit_message) |
| 1296 |
.await |
| 1297 |
.unwrap(); |
| 1298 |
} |
| 1299 |
|
| 1300 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1301 |
async fn external_group_can_observe_since_creation() { |
| 1302 |
let mut alice = test_group(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1303 |
|
| 1304 |
let info = alice |
| 1305 |
.group_info_message_allowing_ext_commit(true) |
| 1306 |
.await |
| 1307 |
.unwrap(); |
| 1308 |
|
| 1309 |
let config = TestExternalClientBuilder::new_for_test().build_config(); |
| 1310 |
let mut server = ExternalGroup::join(config, None, info, None).await.unwrap(); |
| 1311 |
|
| 1312 |
for _ in 0..2 { |
| 1313 |
let commit = alice.commit(vec![]).await.unwrap().commit_message; |
| 1314 |
alice.process_pending_commit().await.unwrap(); |
| 1315 |
server.process_incoming_message(commit).await.unwrap(); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1320 |
async fn external_group_can_be_serialized_to_tls_encoding() { |
| 1321 |
let server = |
| 1322 |
make_external_group(&test_group(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await).await; |
| 1323 |
|
| 1324 |
let snapshot = server.snapshot().mls_encode_to_vec().unwrap(); |
| 1325 |
let snapshot_restored = ExternalSnapshot::mls_decode(&mut snapshot.as_slice()).unwrap(); |
| 1326 |
|
| 1327 |
let server_restored = |
| 1328 |
ExternalGroup::from_snapshot(server.config.clone(), snapshot_restored) |
| 1329 |
.await |
| 1330 |
.unwrap(); |
| 1331 |
|
| 1332 |
assert_eq!(server.group_state(), server_restored.group_state()); |
| 1333 |
} |
| 1334 |
|
| 1335 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1336 |
async fn external_group_can_validate_info() { |
| 1337 |
let alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1338 |
let mut server = make_external_group(&alice).await; |
| 1339 |
|
| 1340 |
let info = alice |
| 1341 |
.group_info_message_allowing_ext_commit(false) |
| 1342 |
.await |
| 1343 |
.unwrap(); |
| 1344 |
|
| 1345 |
let update = server.process_incoming_message(info.clone()).await.unwrap(); |
| 1346 |
let info = info.into_group_info().unwrap(); |
| 1347 |
|
| 1348 |
assert_matches!(update, ExternalReceivedMessage::GroupInfo(update_info) if update_info == info); |
| 1349 |
} |
| 1350 |
|
| 1351 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1352 |
async fn external_group_can_validate_key_package() { |
| 1353 |
let alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1354 |
let mut server = make_external_group(&alice).await; |
| 1355 |
|
| 1356 |
let kp = test_key_package_message(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE, "john").await; |
| 1357 |
|
| 1358 |
let update = server.process_incoming_message(kp.clone()).await.unwrap(); |
| 1359 |
let kp = kp.into_key_package().unwrap(); |
| 1360 |
|
| 1361 |
assert_matches!(update, ExternalReceivedMessage::KeyPackage(update_kp) if update_kp == kp); |
| 1362 |
} |
| 1363 |
|
| 1364 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1365 |
async fn external_group_can_validate_welcome() { |
| 1366 |
let mut alice = test_group_with_one_commit(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await; |
| 1367 |
let mut server = make_external_group(&alice).await; |
| 1368 |
|
| 1369 |
let [welcome] = alice |
| 1370 |
.commit_builder() |
| 1371 |
.add_member( |
| 1372 |
test_key_package_message(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE, "john").await, |
| 1373 |
) |
| 1374 |
.unwrap() |
| 1375 |
.build() |
| 1376 |
.await |
| 1377 |
.unwrap() |
| 1378 |
.welcome_messages |
| 1379 |
.try_into() |
| 1380 |
.unwrap(); |
| 1381 |
|
| 1382 |
let update = server.process_incoming_message(welcome).await.unwrap(); |
| 1383 |
|
| 1384 |
assert_matches!(update, ExternalReceivedMessage::Welcome); |
| 1385 |
} |
| 1386 |
|
| 1387 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 1388 |
async fn external_group_can_be_stored_without_tree() { |
| 1389 |
let mut server = |
| 1390 |
make_external_group(&test_group(TEST_PROTOCOL_VERSION, TEST_CIPHER_SUITE).await).await; |
| 1391 |
|
| 1392 |
let snapshot_with_tree = server.snapshot().mls_encode_to_vec().unwrap(); |
| 1393 |
|
| 1394 |
let snapshot_without_tree = server |
| 1395 |
.snapshot_without_ratchet_tree() |
| 1396 |
.mls_encode_to_vec() |
| 1397 |
.unwrap(); |
| 1398 |
|
| 1399 |
let tree = server.state.public_tree.nodes.mls_encode_to_vec().unwrap(); |
| 1400 |
let empty_tree = Vec::<u8>::new().mls_encode_to_vec().unwrap(); |
| 1401 |
|
| 1402 |
assert_eq!( |
| 1403 |
snapshot_with_tree.len() - snapshot_without_tree.len(), |
| 1404 |
tree.len() - empty_tree.len() |
| 1405 |
); |
| 1406 |
|
| 1407 |
let exported_tree = server.export_tree().unwrap(); |
| 1408 |
|
| 1409 |
let restored = ExternalClient::new(server.config.clone(), None) |
| 1410 |
.load_group_with_ratchet_tree( |
| 1411 |
ExternalSnapshot::from_bytes(&snapshot_without_tree).unwrap(), |
| 1412 |
ExportedTree::from_bytes(&exported_tree).unwrap(), |
| 1413 |
) |
| 1414 |
.await |
| 1415 |
.unwrap(); |
| 1416 |
|
| 1417 |
assert_eq!(restored.group_state(), server.group_state()); |
| 1418 |
} |
| 1419 |
} |