| 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_core::{ |
| 6 |
error::IntoAnyError, |
| 7 |
identity::{IdentityProvider, SigningIdentity}, |
| 8 |
key_package::KeyPackageStorage, |
| 9 |
}; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
cipher_suite::CipherSuite, |
| 13 |
client::MlsError, |
| 14 |
extension::RatchetTreeExt, |
| 15 |
key_package::KeyPackageGeneration, |
| 16 |
protocol_version::ProtocolVersion, |
| 17 |
signer::Signable, |
| 18 |
tree_kem::{node::LeafIndex, tree_validator::TreeValidator, TreeKemPublic}, |
| 19 |
CipherSuiteProvider, CryptoProvider, |
| 20 |
}; |
| 21 |
|
| 22 |
#[cfg(feature = "by_ref_proposal")] |
| 23 |
use crate::extension::ExternalSendersExt; |
| 24 |
|
| 25 |
use super::{ |
| 26 |
framing::Sender, message_signature::AuthenticatedContent, |
| 27 |
transcript_hash::InterimTranscriptHash, ConfirmedTranscriptHash, EncryptedGroupSecrets, |
| 28 |
ExportedTree, GroupInfo, GroupState, |
| 29 |
}; |
| 30 |
|
| 31 |
use super::message_processor::ProvisionalState; |
| 32 |
|
| 33 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 34 |
pub(crate) async fn validate_group_info_common<C: CipherSuiteProvider>( |
| 35 |
msg_version: ProtocolVersion, |
| 36 |
group_info: &GroupInfo, |
| 37 |
signer: &SigningIdentity, |
| 38 |
cs: &C, |
| 39 |
) -> Result<(), MlsError> { |
| 40 |
if msg_version != group_info.group_context.protocol_version { |
| 41 |
return Err(MlsError::ProtocolVersionMismatch); |
| 42 |
} |
| 43 |
|
| 44 |
if group_info.group_context.cipher_suite != cs.cipher_suite() { |
| 45 |
return Err(MlsError::CipherSuiteMismatch); |
| 46 |
} |
| 47 |
|
| 48 |
group_info.verify(cs, &signer.signature_key, &()).await?; |
| 49 |
|
| 50 |
Ok(()) |
| 51 |
} |
| 52 |
|
| 53 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 54 |
pub(crate) async fn validate_group_info_member<C: CipherSuiteProvider>( |
| 55 |
self_state: &GroupState, |
| 56 |
msg_version: ProtocolVersion, |
| 57 |
group_info: &GroupInfo, |
| 58 |
cs: &C, |
| 59 |
) -> Result<(), MlsError> { |
| 60 |
let signer = &self_state.public_tree.get_leaf_node(group_info.signer)?; |
| 61 |
validate_group_info_common(msg_version, group_info, &signer.signing_identity, cs).await?; |
| 62 |
|
| 63 |
let self_tree = ExportedTree::new_borrowed(&self_state.public_tree.nodes); |
| 64 |
|
| 65 |
if let Some(tree) = group_info.extensions.get_as::<RatchetTreeExt>()? { |
| 66 |
(tree.tree_data == self_tree) |
| 67 |
.then_some(()) |
| 68 |
.ok_or(MlsError::InvalidGroupInfo)?; |
| 69 |
} |
| 70 |
|
| 71 |
(group_info.group_context == self_state.context |
| 72 |
&& group_info.confirmation_tag == self_state.confirmation_tag) |
| 73 |
.then_some(()) |
| 74 |
.ok_or(MlsError::InvalidGroupInfo)?; |
| 75 |
|
| 76 |
Ok(()) |
| 77 |
} |
| 78 |
|
| 79 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 80 |
pub(crate) async fn validate_tree_and_info_joiner<C: CipherSuiteProvider, I: IdentityProvider>( |
| 81 |
msg_version: ProtocolVersion, |
| 82 |
group_info: &GroupInfo, |
| 83 |
tree: Option<ExportedTree<'_>>, |
| 84 |
id_provider: &I, |
| 85 |
cs: &C, |
| 86 |
) -> Result<TreeKemPublic, MlsError> { |
| 87 |
let public_tree = validate_tree_joiner(group_info, tree, id_provider, cs).await?; |
| 88 |
|
| 89 |
let signer = &public_tree |
| 90 |
.get_leaf_node(group_info.signer)? |
| 91 |
.signing_identity; |
| 92 |
|
| 93 |
validate_group_info_joiner(msg_version, group_info, signer, id_provider, cs).await?; |
| 94 |
|
| 95 |
Ok(public_tree) |
| 96 |
} |
| 97 |
|
| 98 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 99 |
pub(crate) async fn validate_tree_joiner<C: CipherSuiteProvider, I: IdentityProvider>( |
| 100 |
group_info: &GroupInfo, |
| 101 |
tree: Option<ExportedTree<'_>>, |
| 102 |
id_provider: &I, |
| 103 |
cs: &C, |
| 104 |
) -> Result<TreeKemPublic, MlsError> { |
| 105 |
let tree = match group_info.extensions.get_as::<RatchetTreeExt>()? { |
| 106 |
Some(ext) => ext.tree_data, |
| 107 |
None => tree.ok_or(MlsError::RatchetTreeNotFound)?, |
| 108 |
}; |
| 109 |
|
| 110 |
let context = &group_info.group_context; |
| 111 |
|
| 112 |
let mut tree = |
| 113 |
TreeKemPublic::import_node_data(tree.into(), id_provider, &context.extensions).await?; |
| 114 |
|
| 115 |
// Verify the integrity of the ratchet tree |
| 116 |
TreeValidator::new(cs, context, id_provider) |
| 117 |
.validate(&mut tree) |
| 118 |
.await?; |
| 119 |
|
| 120 |
Ok(tree) |
| 121 |
} |
| 122 |
|
| 123 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 124 |
pub(crate) async fn validate_group_info_joiner<C: CipherSuiteProvider, I: IdentityProvider>( |
| 125 |
msg_version: ProtocolVersion, |
| 126 |
group_info: &GroupInfo, |
| 127 |
signer: &SigningIdentity, |
| 128 |
#[cfg(feature = "by_ref_proposal")] id_provider: &I, |
| 129 |
#[cfg(not(feature = "by_ref_proposal"))] _id_provider: &I, |
| 130 |
cs: &C, |
| 131 |
) -> Result<(), MlsError> { |
| 132 |
#[cfg(feature = "by_ref_proposal")] |
| 133 |
let context = &group_info.group_context; |
| 134 |
|
| 135 |
#[cfg(feature = "by_ref_proposal")] |
| 136 |
if let Some(ext_senders) = context.extensions.get_as::<ExternalSendersExt>()? { |
| 137 |
// TODO do joiners verify group against current time?? |
| 138 |
ext_senders |
| 139 |
.verify_all(id_provider, None, &context.extensions) |
| 140 |
.await |
| 141 |
.map_err(|e| MlsError::IdentityProviderError(e.into_any_error()))?; |
| 142 |
} |
| 143 |
|
| 144 |
validate_group_info_common(msg_version, group_info, signer, cs).await?; |
| 145 |
|
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
pub(crate) fn commit_sender( |
| 150 |
sender: &Sender, |
| 151 |
provisional_state: &ProvisionalState, |
| 152 |
) -> Result<LeafIndex, MlsError> { |
| 153 |
match sender { |
| 154 |
Sender::Member(index) => Ok(LeafIndex(*index)), |
| 155 |
#[cfg(feature = "by_ref_proposal")] |
| 156 |
Sender::External(_) => Err(MlsError::ExternalSenderCannotCommit), |
| 157 |
#[cfg(feature = "by_ref_proposal")] |
| 158 |
Sender::NewMemberProposal => Err(MlsError::ExpectedAddProposalForNewMemberProposal), |
| 159 |
Sender::NewMemberCommit => provisional_state |
| 160 |
.external_init_index |
| 161 |
.ok_or(MlsError::ExternalCommitMissingExternalInit), |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 166 |
pub(super) async fn transcript_hashes<P: CipherSuiteProvider>( |
| 167 |
cipher_suite_provider: &P, |
| 168 |
prev_interim_transcript_hash: &InterimTranscriptHash, |
| 169 |
content: &AuthenticatedContent, |
| 170 |
) -> Result<(InterimTranscriptHash, ConfirmedTranscriptHash), MlsError> { |
| 171 |
let confirmed_transcript_hash = super::transcript_hash::create( |
| 172 |
cipher_suite_provider, |
| 173 |
prev_interim_transcript_hash, |
| 174 |
content, |
| 175 |
) |
| 176 |
.await?; |
| 177 |
|
| 178 |
let confirmation_tag = content |
| 179 |
.auth |
| 180 |
.confirmation_tag |
| 181 |
.as_ref() |
| 182 |
.ok_or(MlsError::InvalidConfirmationTag)?; |
| 183 |
|
| 184 |
let interim_transcript_hash = InterimTranscriptHash::create( |
| 185 |
cipher_suite_provider, |
| 186 |
&confirmed_transcript_hash, |
| 187 |
confirmation_tag, |
| 188 |
) |
| 189 |
.await?; |
| 190 |
|
| 191 |
Ok((interim_transcript_hash, confirmed_transcript_hash)) |
| 192 |
} |
| 193 |
|
| 194 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 195 |
pub(crate) async fn find_key_package_generation<'a, K: KeyPackageStorage>( |
| 196 |
key_package_repo: &K, |
| 197 |
secrets: &'a [EncryptedGroupSecrets], |
| 198 |
) -> Result<(&'a EncryptedGroupSecrets, KeyPackageGeneration), MlsError> { |
| 199 |
for secret in secrets { |
| 200 |
if let Some(val) = key_package_repo |
| 201 |
.get(&secret.new_member) |
| 202 |
.await |
| 203 |
.map_err(|e| MlsError::KeyPackageRepoError(e.into_any_error())) |
| 204 |
.and_then(|maybe_data| { |
| 205 |
if let Some(data) = maybe_data { |
| 206 |
KeyPackageGeneration::from_storage(secret.new_member.to_vec(), data) |
| 207 |
.map(|kpg| Some((secret, kpg))) |
| 208 |
} else { |
| 209 |
Ok::<_, MlsError>(None) |
| 210 |
} |
| 211 |
})? |
| 212 |
{ |
| 213 |
return Ok(val); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
Err(MlsError::WelcomeKeyPackageNotFound) |
| 218 |
} |
| 219 |
|
| 220 |
pub(crate) fn cipher_suite_provider<P>( |
| 221 |
crypto: P, |
| 222 |
cipher_suite: CipherSuite, |
| 223 |
) -> Result<P::CipherSuiteProvider, MlsError> |
| 224 |
where |
| 225 |
P: CryptoProvider, |
| 226 |
{ |
| 227 |
crypto |
| 228 |
.cipher_suite_provider(cipher_suite) |
| 229 |
.ok_or(MlsError::UnsupportedCipherSuite(cipher_suite)) |
| 230 |
} |