| 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 alloc::vec::Vec; |
| 6 |
|
| 7 |
use mls_rs_core::{ |
| 8 |
crypto::{CipherSuite, SignatureSecretKey}, |
| 9 |
extension::ExtensionList, |
| 10 |
identity::SigningIdentity, |
| 11 |
protocol_version::ProtocolVersion, |
| 12 |
}; |
| 13 |
|
| 14 |
use crate::{client::MlsError, Client, Group, MlsMessage}; |
| 15 |
|
| 16 |
use super::{ |
| 17 |
proposal::ReInitProposal, ClientConfig, ExportedTree, JustPreSharedKeyID, MessageProcessor, |
| 18 |
NewMemberInfo, PreSharedKeyID, PskGroupId, PskSecretInput, ResumptionPSKUsage, ResumptionPsk, |
| 19 |
}; |
| 20 |
|
| 21 |
struct ResumptionGroupParameters<'a> { |
| 22 |
group_id: &'a [u8], |
| 23 |
cipher_suite: CipherSuite, |
| 24 |
version: ProtocolVersion, |
| 25 |
extensions: &'a ExtensionList, |
| 26 |
} |
| 27 |
|
| 28 |
pub struct ReinitClient<C: ClientConfig + Clone> { |
| 29 |
client: Client<C>, |
| 30 |
reinit: ReInitProposal, |
| 31 |
psk_input: PskSecretInput, |
| 32 |
} |
| 33 |
|
| 34 |
impl<C> Group<C> |
| 35 |
where |
| 36 |
C: ClientConfig + Clone, |
| 37 |
{ |
| 38 |
/// Create a sub-group from a subset of the current group members. |
| 39 |
/// |
| 40 |
/// Membership within the resulting sub-group is indicated by providing a |
| 41 |
/// key package that produces the same |
| 42 |
/// [identity](crate::IdentityProvider::identity) value |
| 43 |
/// as an existing group member. The identity value of each key package |
| 44 |
/// is determined using the |
| 45 |
/// [`IdentityProvider`](crate::IdentityProvider) |
| 46 |
/// that is currently in use by this group instance. |
| 47 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 48 |
pub async fn branch( |
| 49 |
&self, |
| 50 |
sub_group_id: Vec<u8>, |
| 51 |
new_key_packages: Vec<MlsMessage>, |
| 52 |
) -> Result<(Group<C>, Vec<MlsMessage>), MlsError> { |
| 53 |
let new_group_params = ResumptionGroupParameters { |
| 54 |
group_id: &sub_group_id, |
| 55 |
cipher_suite: self.cipher_suite(), |
| 56 |
version: self.protocol_version(), |
| 57 |
extensions: &self.group_state().context.extensions, |
| 58 |
}; |
| 59 |
|
| 60 |
let current_leaf_node_extensions = &self.current_user_leaf_node()?.ungreased_extensions(); |
| 61 |
resumption_create_group( |
| 62 |
self.config.clone(), |
| 63 |
new_key_packages, |
| 64 |
&new_group_params, |
| 65 |
// TODO investigate if it's worth updating your own signing identity here |
| 66 |
self.current_member_signing_identity()?.clone(), |
| 67 |
self.signer.clone(), |
| 68 |
current_leaf_node_extensions, |
| 69 |
#[cfg(any(feature = "private_message", feature = "psk"))] |
| 70 |
self.resumption_psk_input(ResumptionPSKUsage::Branch)?, |
| 71 |
) |
| 72 |
.await |
| 73 |
} |
| 74 |
|
| 75 |
/// Join a subgroup that was created by [`Group::branch`]. |
| 76 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 77 |
pub async fn join_subgroup( |
| 78 |
&self, |
| 79 |
welcome: &MlsMessage, |
| 80 |
tree_data: Option<ExportedTree<'_>>, |
| 81 |
) -> Result<(Group<C>, NewMemberInfo), MlsError> { |
| 82 |
let expected_new_group_prams = ResumptionGroupParameters { |
| 83 |
group_id: &[], |
| 84 |
cipher_suite: self.cipher_suite(), |
| 85 |
version: self.protocol_version(), |
| 86 |
extensions: &self.group_state().context.extensions, |
| 87 |
}; |
| 88 |
|
| 89 |
resumption_join_group( |
| 90 |
self.config.clone(), |
| 91 |
self.signer.clone(), |
| 92 |
welcome, |
| 93 |
tree_data, |
| 94 |
expected_new_group_prams, |
| 95 |
false, |
| 96 |
self.resumption_psk_input(ResumptionPSKUsage::Branch)?, |
| 97 |
) |
| 98 |
.await |
| 99 |
} |
| 100 |
|
| 101 |
/// Generate a [`ReinitClient`] that can be used to create or join a new group |
| 102 |
/// that is based on properties defined by a [`ReInitProposal`] |
| 103 |
/// committed in a previously accepted commit. This is the only action available |
| 104 |
/// after accepting such a commit. The old group can no longer be used according to the RFC. |
| 105 |
/// |
| 106 |
/// If the [`ReInitProposal`] changes the ciphersuite, then `new_signer` |
| 107 |
/// and `new_signer_identity` must be set and match the new ciphersuite, as indicated by |
| 108 |
/// the [`CommitEffect::ReInit`](crate::group::CommitEffect::ReInit) outputted after processing the |
| 109 |
/// commit to the reinit proposal. The value of [identity](crate::IdentityProvider::identity) |
| 110 |
/// must be the same for `new_signing_identity` and the current identity in use by this |
| 111 |
/// group instance. |
| 112 |
pub fn get_reinit_client( |
| 113 |
self, |
| 114 |
new_signer: Option<SignatureSecretKey>, |
| 115 |
new_signing_identity: Option<SigningIdentity>, |
| 116 |
) -> Result<ReinitClient<C>, MlsError> { |
| 117 |
let psk_input = self.resumption_psk_input(ResumptionPSKUsage::Reinit)?; |
| 118 |
|
| 119 |
let new_signing_identity = new_signing_identity |
| 120 |
.map(Ok) |
| 121 |
.unwrap_or_else(|| self.current_member_signing_identity().cloned())?; |
| 122 |
|
| 123 |
let reinit = self |
| 124 |
.state |
| 125 |
.pending_reinit |
| 126 |
.ok_or(MlsError::PendingReInitNotFound)?; |
| 127 |
|
| 128 |
let new_signer = match new_signer { |
| 129 |
Some(signer) => signer, |
| 130 |
None => self.signer, |
| 131 |
}; |
| 132 |
|
| 133 |
let client = Client::new( |
| 134 |
self.config, |
| 135 |
Some(new_signer), |
| 136 |
Some((new_signing_identity, reinit.new_cipher_suite())), |
| 137 |
reinit.new_version(), |
| 138 |
); |
| 139 |
|
| 140 |
Ok(ReinitClient { |
| 141 |
client, |
| 142 |
reinit, |
| 143 |
psk_input, |
| 144 |
}) |
| 145 |
} |
| 146 |
|
| 147 |
fn resumption_psk_input(&self, usage: ResumptionPSKUsage) -> Result<PskSecretInput, MlsError> { |
| 148 |
let psk = self.epoch_secrets.resumption_secret.clone(); |
| 149 |
|
| 150 |
let id = JustPreSharedKeyID::Resumption(ResumptionPsk { |
| 151 |
usage, |
| 152 |
psk_group_id: PskGroupId(self.group_id().to_vec()), |
| 153 |
psk_epoch: self.current_epoch(), |
| 154 |
}); |
| 155 |
|
| 156 |
let id = PreSharedKeyID::new(id, self.cipher_suite_provider())?; |
| 157 |
Ok(PskSecretInput { id, psk }) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/// A [`Client`] that can be used to create or join a new group |
| 162 |
/// that is based on properties defined by a [`ReInitProposal`] |
| 163 |
/// committed in a previously accepted commit. |
| 164 |
impl<C: ClientConfig + Clone> ReinitClient<C> { |
| 165 |
/// Generate a key package for the new group. The key package can |
| 166 |
/// be used in [`ReinitClient::commit`]. |
| 167 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 168 |
pub async fn generate_key_package(&self) -> Result<MlsMessage, MlsError> { |
| 169 |
self.client |
| 170 |
.generate_key_package_message(Default::default(), Default::default()) |
| 171 |
.await |
| 172 |
} |
| 173 |
|
| 174 |
/// Create the new group using new key packages of all group members, possibly |
| 175 |
/// generated by [`ReinitClient::generate_key_package`]. |
| 176 |
/// |
| 177 |
/// # Warning |
| 178 |
/// |
| 179 |
/// This function will fail if the number of members in the reinitialized |
| 180 |
/// group is not the same as the prior group roster. |
| 181 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 182 |
pub async fn commit( |
| 183 |
self, |
| 184 |
new_key_packages: Vec<MlsMessage>, |
| 185 |
new_leaf_node_extensions: ExtensionList, |
| 186 |
) -> Result<(Group<C>, Vec<MlsMessage>), MlsError> { |
| 187 |
let new_group_params = ResumptionGroupParameters { |
| 188 |
group_id: self.reinit.group_id(), |
| 189 |
cipher_suite: self.reinit.new_cipher_suite(), |
| 190 |
version: self.reinit.new_version(), |
| 191 |
extensions: self.reinit.new_group_context_extensions(), |
| 192 |
}; |
| 193 |
|
| 194 |
resumption_create_group( |
| 195 |
self.client.config.clone(), |
| 196 |
new_key_packages, |
| 197 |
&new_group_params, |
| 198 |
// These private fields are created with `Some(x)` by `get_reinit_client` |
| 199 |
self.client.signing_identity.unwrap().0, |
| 200 |
self.client.signer.unwrap(), |
| 201 |
&new_leaf_node_extensions, |
| 202 |
#[cfg(any(feature = "private_message", feature = "psk"))] |
| 203 |
self.psk_input, |
| 204 |
) |
| 205 |
.await |
| 206 |
} |
| 207 |
|
| 208 |
/// Join a reinitialized group that was created by [`ReinitClient::commit`]. |
| 209 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 210 |
pub async fn join( |
| 211 |
self, |
| 212 |
welcome: &MlsMessage, |
| 213 |
tree_data: Option<ExportedTree<'_>>, |
| 214 |
) -> Result<(Group<C>, NewMemberInfo), MlsError> { |
| 215 |
let reinit = self.reinit; |
| 216 |
|
| 217 |
let expected_group_params = ResumptionGroupParameters { |
| 218 |
group_id: reinit.group_id(), |
| 219 |
cipher_suite: reinit.new_cipher_suite(), |
| 220 |
version: reinit.new_version(), |
| 221 |
extensions: reinit.new_group_context_extensions(), |
| 222 |
}; |
| 223 |
|
| 224 |
resumption_join_group( |
| 225 |
self.client.config, |
| 226 |
// This private field is created with `Some(x)` by `get_reinit_client` |
| 227 |
self.client.signer.unwrap(), |
| 228 |
welcome, |
| 229 |
tree_data, |
| 230 |
expected_group_params, |
| 231 |
true, |
| 232 |
self.psk_input, |
| 233 |
) |
| 234 |
.await |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 239 |
async fn resumption_create_group<C: ClientConfig + Clone>( |
| 240 |
config: C, |
| 241 |
new_key_packages: Vec<MlsMessage>, |
| 242 |
new_group_params: &ResumptionGroupParameters<'_>, |
| 243 |
signing_identity: SigningIdentity, |
| 244 |
signer: SignatureSecretKey, |
| 245 |
leaf_node_extensions: &ExtensionList, |
| 246 |
psk_input: PskSecretInput, |
| 247 |
) -> Result<(Group<C>, Vec<MlsMessage>), MlsError> { |
| 248 |
// Create a new group with new parameters |
| 249 |
let mut group = Group::new( |
| 250 |
config, |
| 251 |
Some(new_group_params.group_id.to_vec()), |
| 252 |
new_group_params.cipher_suite, |
| 253 |
new_group_params.version, |
| 254 |
signing_identity, |
| 255 |
new_group_params.extensions.clone(), |
| 256 |
leaf_node_extensions.clone(), |
| 257 |
signer, |
| 258 |
) |
| 259 |
.await?; |
| 260 |
|
| 261 |
// Install the resumption psk in the new group |
| 262 |
group.previous_psk = Some(psk_input); |
| 263 |
|
| 264 |
// Create a commit that adds new key packages and uses the resumption PSK |
| 265 |
let mut commit = group.commit_builder(); |
| 266 |
|
| 267 |
for kp in new_key_packages.into_iter() { |
| 268 |
commit = commit.add_member(kp)?; |
| 269 |
} |
| 270 |
|
| 271 |
let commit = commit.build().await?; |
| 272 |
group.apply_pending_commit().await?; |
| 273 |
|
| 274 |
// Uninstall the resumption psk on success (in case of failure, the new group is discarded anyway) |
| 275 |
group.previous_psk = None; |
| 276 |
|
| 277 |
Ok((group, commit.welcome_messages)) |
| 278 |
} |
| 279 |
|
| 280 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 281 |
async fn resumption_join_group<C: ClientConfig + Clone>( |
| 282 |
config: C, |
| 283 |
signer: SignatureSecretKey, |
| 284 |
welcome: &MlsMessage, |
| 285 |
tree_data: Option<ExportedTree<'_>>, |
| 286 |
expected_new_group_params: ResumptionGroupParameters<'_>, |
| 287 |
verify_group_id: bool, |
| 288 |
psk_input: PskSecretInput, |
| 289 |
) -> Result<(Group<C>, NewMemberInfo), MlsError> { |
| 290 |
let psk_input = Some(psk_input); |
| 291 |
|
| 292 |
let (group, new_member_info) = |
| 293 |
Group::<C>::from_welcome_message(welcome, tree_data, config, signer, psk_input).await?; |
| 294 |
|
| 295 |
if group.protocol_version() != expected_new_group_params.version { |
| 296 |
Err(MlsError::ProtocolVersionMismatch) |
| 297 |
} else if group.cipher_suite() != expected_new_group_params.cipher_suite { |
| 298 |
Err(MlsError::CipherSuiteMismatch) |
| 299 |
} else if verify_group_id && group.group_id() != expected_new_group_params.group_id { |
| 300 |
Err(MlsError::GroupIdMismatch) |
| 301 |
} else if &group.group_state().context.extensions != expected_new_group_params.extensions { |
| 302 |
Err(MlsError::ReInitExtensionsMismatch) |
| 303 |
} else { |
| 304 |
Ok((group, new_member_info)) |
| 305 |
} |
| 306 |
} |