| 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::Vec}; |
| 6 |
use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize}; |
| 7 |
use mls_rs_core::{ |
| 8 |
error::IntoAnyError, |
| 9 |
group::GroupContext, |
| 10 |
identity::{IdentityProvider, MemberValidationContext}, |
| 11 |
}; |
| 12 |
|
| 13 |
use super::{ |
| 14 |
leaf_node::LeafNode, |
| 15 |
leaf_node_validator::{LeafNodeValidator, ValidationContext}, |
| 16 |
node::LeafIndex, |
| 17 |
}; |
| 18 |
use crate::{ |
| 19 |
client::MlsError, |
| 20 |
crypto::{CipherSuiteProvider, HpkeCiphertext, HpkePublicKey}, |
| 21 |
}; |
| 22 |
use crate::{group::message_processor::ProvisionalState, time::MlsTime}; |
| 23 |
|
| 24 |
#[derive(Clone, Debug, PartialEq, Eq, MlsSize, MlsEncode, MlsDecode)] |
| 25 |
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
| 26 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 27 |
pub struct UpdatePathNode { |
| 28 |
pub public_key: HpkePublicKey, |
| 29 |
pub encrypted_path_secret: Vec<HpkeCiphertext>, |
| 30 |
} |
| 31 |
|
| 32 |
#[derive(Clone, Debug, PartialEq, MlsSize, MlsEncode, MlsDecode)] |
| 33 |
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
| 34 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 35 |
pub struct UpdatePath { |
| 36 |
pub leaf_node: LeafNode, |
| 37 |
pub nodes: Vec<UpdatePathNode>, |
| 38 |
} |
| 39 |
|
| 40 |
#[derive(Clone, Debug, PartialEq)] |
| 41 |
pub struct ValidatedUpdatePath { |
| 42 |
pub leaf_node: LeafNode, |
| 43 |
pub nodes: Vec<Option<UpdatePathNode>>, |
| 44 |
} |
| 45 |
|
| 46 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 47 |
pub(crate) async fn validate_update_path<C: IdentityProvider, CSP: CipherSuiteProvider>( |
| 48 |
identity_provider: &C, |
| 49 |
cipher_suite_provider: &CSP, |
| 50 |
path: UpdatePath, |
| 51 |
state: &ProvisionalState, |
| 52 |
sender: LeafIndex, |
| 53 |
commit_time: Option<MlsTime>, |
| 54 |
current_context: &GroupContext, |
| 55 |
) -> Result<ValidatedUpdatePath, MlsError> { |
| 56 |
let member_validation_context = MemberValidationContext::ForCommit { |
| 57 |
current_context, |
| 58 |
new_extensions: &state.group_context.extensions, |
| 59 |
}; |
| 60 |
|
| 61 |
let leaf_validator = LeafNodeValidator::new( |
| 62 |
cipher_suite_provider, |
| 63 |
identity_provider, |
| 64 |
member_validation_context, |
| 65 |
); |
| 66 |
|
| 67 |
leaf_validator |
| 68 |
.check_if_valid( |
| 69 |
&path.leaf_node, |
| 70 |
ValidationContext::Commit((&state.group_context.group_id, *sender, commit_time)), |
| 71 |
) |
| 72 |
.await?; |
| 73 |
|
| 74 |
let check_identity_eq = state.applied_proposals.external_initializations.is_empty(); |
| 75 |
|
| 76 |
if check_identity_eq { |
| 77 |
let existing_leaf = state.public_tree.nodes.borrow_as_leaf(sender)?; |
| 78 |
let original_leaf_node = existing_leaf.clone(); |
| 79 |
|
| 80 |
identity_provider |
| 81 |
.valid_successor( |
| 82 |
&original_leaf_node.signing_identity, |
| 83 |
&path.leaf_node.signing_identity, |
| 84 |
&state.group_context.extensions, |
| 85 |
) |
| 86 |
.await |
| 87 |
.map_err(|e| MlsError::IdentityProviderError(e.into_any_error()))? |
| 88 |
.then_some(()) |
| 89 |
.ok_or(MlsError::InvalidSuccessor)?; |
| 90 |
|
| 91 |
(existing_leaf.public_key != path.leaf_node.public_key) |
| 92 |
.then_some(()) |
| 93 |
.ok_or(MlsError::SameHpkeKey(*sender))?; |
| 94 |
} |
| 95 |
|
| 96 |
// Unfilter the update path |
| 97 |
let filtered = state.public_tree.nodes.filtered(sender)?; |
| 98 |
let mut unfiltered_nodes = vec![]; |
| 99 |
let mut i = 0; |
| 100 |
|
| 101 |
for n in path.nodes { |
| 102 |
while *filtered.get(i).ok_or(MlsError::WrongPathLen)? { |
| 103 |
unfiltered_nodes.push(None); |
| 104 |
i += 1; |
| 105 |
} |
| 106 |
|
| 107 |
unfiltered_nodes.push(Some(n)); |
| 108 |
i += 1; |
| 109 |
} |
| 110 |
|
| 111 |
Ok(ValidatedUpdatePath { |
| 112 |
leaf_node: path.leaf_node, |
| 113 |
nodes: unfiltered_nodes, |
| 114 |
}) |
| 115 |
} |
| 116 |
|
| 117 |
#[cfg(test)] |
| 118 |
mod tests { |
| 119 |
use alloc::vec; |
| 120 |
use assert_matches::assert_matches; |
| 121 |
|
| 122 |
use crate::client::test_utils::TEST_CIPHER_SUITE; |
| 123 |
use crate::crypto::test_utils::test_cipher_suite_provider; |
| 124 |
use crate::crypto::HpkeCiphertext; |
| 125 |
use crate::group::message_processor::ProvisionalState; |
| 126 |
use crate::group::test_utils::{get_test_group_context, random_bytes, TEST_GROUP}; |
| 127 |
use crate::identity::basic::BasicIdentityProvider; |
| 128 |
use crate::tree_kem::leaf_node::test_utils::default_properties; |
| 129 |
use crate::tree_kem::leaf_node::test_utils::get_basic_test_node_sig_key; |
| 130 |
use crate::tree_kem::leaf_node::LeafNodeSource; |
| 131 |
use crate::tree_kem::node::LeafIndex; |
| 132 |
use crate::tree_kem::parent_hash::ParentHash; |
| 133 |
use crate::tree_kem::test_utils::{get_test_leaf_nodes, get_test_tree}; |
| 134 |
use crate::tree_kem::validate_update_path; |
| 135 |
|
| 136 |
use super::{UpdatePath, UpdatePathNode}; |
| 137 |
use crate::{cipher_suite::CipherSuite, tree_kem::MlsError}; |
| 138 |
|
| 139 |
use alloc::vec::Vec; |
| 140 |
|
| 141 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 142 |
async fn test_update_path(cipher_suite: CipherSuite, cred: &str) -> UpdatePath { |
| 143 |
let (mut leaf_node, _, signer) = get_basic_test_node_sig_key(cipher_suite, cred).await; |
| 144 |
|
| 145 |
leaf_node.leaf_node_source = LeafNodeSource::Commit(ParentHash::from(hex!("beef"))); |
| 146 |
|
| 147 |
leaf_node |
| 148 |
.commit( |
| 149 |
&test_cipher_suite_provider(cipher_suite), |
| 150 |
TEST_GROUP, |
| 151 |
0, |
| 152 |
Some(default_properties()), |
| 153 |
None, |
| 154 |
&signer, |
| 155 |
) |
| 156 |
.await |
| 157 |
.unwrap(); |
| 158 |
|
| 159 |
let node = UpdatePathNode { |
| 160 |
public_key: random_bytes(32).into(), |
| 161 |
encrypted_path_secret: vec![HpkeCiphertext { |
| 162 |
kem_output: random_bytes(32), |
| 163 |
ciphertext: random_bytes(32), |
| 164 |
}], |
| 165 |
}; |
| 166 |
|
| 167 |
UpdatePath { |
| 168 |
leaf_node, |
| 169 |
nodes: vec![node.clone(), node], |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 174 |
async fn test_provisional_state(cipher_suite: CipherSuite) -> ProvisionalState { |
| 175 |
let mut tree = get_test_tree(cipher_suite).await.public; |
| 176 |
let leaf_nodes = get_test_leaf_nodes(cipher_suite).await; |
| 177 |
|
| 178 |
tree.add_leaves( |
| 179 |
leaf_nodes, |
| 180 |
&BasicIdentityProvider, |
| 181 |
&test_cipher_suite_provider(cipher_suite), |
| 182 |
) |
| 183 |
.await |
| 184 |
.unwrap(); |
| 185 |
|
| 186 |
ProvisionalState { |
| 187 |
public_tree: tree, |
| 188 |
applied_proposals: Default::default(), |
| 189 |
group_context: get_test_group_context(1, cipher_suite).await, |
| 190 |
indexes_of_added_kpkgs: vec![], |
| 191 |
external_init_index: None, |
| 192 |
unused_proposals: vec![], |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 197 |
async fn test_valid_leaf_node() { |
| 198 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 199 |
let update_path = test_update_path(TEST_CIPHER_SUITE, "creator").await; |
| 200 |
let state = test_provisional_state(TEST_CIPHER_SUITE).await; |
| 201 |
|
| 202 |
let validated = validate_update_path( |
| 203 |
&BasicIdentityProvider, |
| 204 |
&cipher_suite_provider, |
| 205 |
update_path.clone(), |
| 206 |
&state, |
| 207 |
LeafIndex(0), |
| 208 |
None, |
| 209 |
&state.group_context, |
| 210 |
) |
| 211 |
.await |
| 212 |
.unwrap(); |
| 213 |
|
| 214 |
let expected = update_path.nodes.into_iter().map(Some).collect::<Vec<_>>(); |
| 215 |
|
| 216 |
assert_eq!(validated.nodes, expected); |
| 217 |
assert_eq!(validated.leaf_node, update_path.leaf_node); |
| 218 |
} |
| 219 |
|
| 220 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 221 |
async fn test_invalid_key_package() { |
| 222 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 223 |
let mut update_path = test_update_path(TEST_CIPHER_SUITE, "creator").await; |
| 224 |
update_path.leaf_node.signature = random_bytes(32); |
| 225 |
let state = test_provisional_state(TEST_CIPHER_SUITE).await; |
| 226 |
|
| 227 |
let validated = validate_update_path( |
| 228 |
&BasicIdentityProvider, |
| 229 |
&cipher_suite_provider, |
| 230 |
update_path, |
| 231 |
&state, |
| 232 |
LeafIndex(0), |
| 233 |
None, |
| 234 |
&state.group_context, |
| 235 |
) |
| 236 |
.await; |
| 237 |
|
| 238 |
assert_matches!(validated, Err(MlsError::InvalidSignature)); |
| 239 |
} |
| 240 |
|
| 241 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 242 |
async fn validating_path_fails_with_different_identity() { |
| 243 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 244 |
let cipher_suite = TEST_CIPHER_SUITE; |
| 245 |
let update_path = test_update_path(cipher_suite, "foobar").await; |
| 246 |
let state = test_provisional_state(TEST_CIPHER_SUITE).await; |
| 247 |
|
| 248 |
let validated = validate_update_path( |
| 249 |
&BasicIdentityProvider, |
| 250 |
&cipher_suite_provider, |
| 251 |
update_path, |
| 252 |
&state, |
| 253 |
LeafIndex(0), |
| 254 |
None, |
| 255 |
&state.group_context, |
| 256 |
) |
| 257 |
.await; |
| 258 |
|
| 259 |
assert_matches!(validated, Err(MlsError::InvalidSuccessor)); |
| 260 |
} |
| 261 |
|
| 262 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 263 |
async fn validating_path_fails_with_same_hpke_key() { |
| 264 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 265 |
let update_path = test_update_path(TEST_CIPHER_SUITE, "creator").await; |
| 266 |
let mut state = test_provisional_state(TEST_CIPHER_SUITE).await; |
| 267 |
|
| 268 |
state |
| 269 |
.public_tree |
| 270 |
.nodes |
| 271 |
.borrow_as_leaf_mut(LeafIndex(0)) |
| 272 |
.unwrap() |
| 273 |
.public_key = update_path.leaf_node.public_key.clone(); |
| 274 |
|
| 275 |
let validated = validate_update_path( |
| 276 |
&BasicIdentityProvider, |
| 277 |
&cipher_suite_provider, |
| 278 |
update_path, |
| 279 |
&state, |
| 280 |
LeafIndex(0), |
| 281 |
None, |
| 282 |
&state.group_context, |
| 283 |
) |
| 284 |
.await; |
| 285 |
|
| 286 |
assert_matches!(validated, Err(MlsError::SameHpkeKey(_))); |
| 287 |
} |
| 288 |
} |