| 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 super::leaf_node::{LeafNode, LeafNodeSigningContext, LeafNodeSource}; |
| 6 |
use crate::client::MlsError; |
| 7 |
use crate::CipherSuiteProvider; |
| 8 |
use crate::{signer::Signable, time::MlsTime}; |
| 9 |
use mls_rs_core::identity::MemberValidationContext; |
| 10 |
use mls_rs_core::{error::IntoAnyError, identity::IdentityProvider}; |
| 11 |
|
| 12 |
use crate::extension::RequiredCapabilitiesExt; |
| 13 |
|
| 14 |
#[cfg(feature = "by_ref_proposal")] |
| 15 |
use crate::extension::ExternalSendersExt; |
| 16 |
|
| 17 |
pub enum ValidationContext<'a> { |
| 18 |
Add(Option<MlsTime>), |
| 19 |
Update((&'a [u8], u32, Option<MlsTime>)), |
| 20 |
Commit((&'a [u8], u32, Option<MlsTime>)), |
| 21 |
} |
| 22 |
|
| 23 |
impl ValidationContext<'_> { |
| 24 |
fn signing_context(&self) -> LeafNodeSigningContext { |
| 25 |
match *self { |
| 26 |
ValidationContext::Add(_) => Default::default(), |
| 27 |
ValidationContext::Update((group_id, leaf_index, _)) => (group_id, leaf_index).into(), |
| 28 |
ValidationContext::Commit((group_id, leaf_index, _)) => (group_id, leaf_index).into(), |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
fn generation_time(&self) -> Option<MlsTime> { |
| 33 |
match *self { |
| 34 |
ValidationContext::Add(t) => t, |
| 35 |
ValidationContext::Update((_, _, t)) => t, |
| 36 |
ValidationContext::Commit((_, _, t)) => t, |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
#[derive(Clone, Debug)] |
| 42 |
pub struct LeafNodeValidator<'a, C, CP> |
| 43 |
where |
| 44 |
C: IdentityProvider, |
| 45 |
CP: CipherSuiteProvider, |
| 46 |
{ |
| 47 |
cipher_suite_provider: &'a CP, |
| 48 |
identity_provider: &'a C, |
| 49 |
context: MemberValidationContext<'a>, |
| 50 |
} |
| 51 |
|
| 52 |
impl<'a, C: IdentityProvider, CP: CipherSuiteProvider> LeafNodeValidator<'a, C, CP> { |
| 53 |
pub fn new( |
| 54 |
cipher_suite_provider: &'a CP, |
| 55 |
identity_provider: &'a C, |
| 56 |
context: MemberValidationContext<'a>, |
| 57 |
) -> Self { |
| 58 |
Self { |
| 59 |
cipher_suite_provider, |
| 60 |
identity_provider, |
| 61 |
context, |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
fn check_context( |
| 66 |
&self, |
| 67 |
leaf_node: &LeafNode, |
| 68 |
context: &ValidationContext, |
| 69 |
) -> Result<(), MlsError> { |
| 70 |
// Context specific checks |
| 71 |
match context { |
| 72 |
ValidationContext::Add(time) => { |
| 73 |
// If the context is add, and we specified a time to check for lifetime, verify it |
| 74 |
if let LeafNodeSource::KeyPackage(lifetime) = &leaf_node.leaf_node_source { |
| 75 |
if let Some(current_time) = time { |
| 76 |
if !lifetime.within_lifetime(*current_time) { |
| 77 |
return Err(MlsError::InvalidLifetime); |
| 78 |
} |
| 79 |
} |
| 80 |
} else { |
| 81 |
// If the leaf_node_source is anything other than Add it is invalid |
| 82 |
return Err(MlsError::InvalidLeafNodeSource); |
| 83 |
} |
| 84 |
} |
| 85 |
ValidationContext::Update(_) => { |
| 86 |
// If the leaf_node_source is anything other than Update it is invalid |
| 87 |
if !matches!(leaf_node.leaf_node_source, LeafNodeSource::Update) { |
| 88 |
return Err(MlsError::InvalidLeafNodeSource); |
| 89 |
} |
| 90 |
} |
| 91 |
ValidationContext::Commit(_) => { |
| 92 |
// If the leaf_node_source is anything other than Commit it is invalid |
| 93 |
if !matches!(leaf_node.leaf_node_source, LeafNodeSource::Commit(_)) { |
| 94 |
return Err(MlsError::InvalidLeafNodeSource); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
Ok(()) |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 103 |
pub async fn revalidate( |
| 104 |
&self, |
| 105 |
leaf_node: &LeafNode, |
| 106 |
group_id: &[u8], |
| 107 |
leaf_index: u32, |
| 108 |
) -> Result<(), MlsError> { |
| 109 |
let context = match leaf_node.leaf_node_source { |
| 110 |
LeafNodeSource::KeyPackage(_) => ValidationContext::Add(None), |
| 111 |
LeafNodeSource::Update => ValidationContext::Update((group_id, leaf_index, None)), |
| 112 |
LeafNodeSource::Commit(_) => ValidationContext::Commit((group_id, leaf_index, None)), |
| 113 |
}; |
| 114 |
|
| 115 |
self.check_if_valid(leaf_node, context).await |
| 116 |
} |
| 117 |
|
| 118 |
pub fn validate_required_capabilities(&self, leaf_node: &LeafNode) -> Result<(), MlsError> { |
| 119 |
let Some(required_capabilities) = self |
| 120 |
.context |
| 121 |
.new_extensions() |
| 122 |
.and_then(|ext| ext.get_as::<RequiredCapabilitiesExt>().transpose()) |
| 123 |
.transpose()? |
| 124 |
else { |
| 125 |
return Ok(()); |
| 126 |
}; |
| 127 |
|
| 128 |
for extension in &required_capabilities.extensions { |
| 129 |
if !leaf_node.capabilities.extensions.contains(extension) { |
| 130 |
return Err(MlsError::RequiredExtensionNotFound(*extension)); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
for proposal in &required_capabilities.proposals { |
| 135 |
if !leaf_node.capabilities.proposals.contains(proposal) { |
| 136 |
return Err(MlsError::RequiredProposalNotFound(*proposal)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
for credential in &required_capabilities.credentials { |
| 141 |
if !leaf_node.capabilities.credentials.contains(credential) { |
| 142 |
return Err(MlsError::RequiredCredentialNotFound(*credential)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
Ok(()) |
| 147 |
} |
| 148 |
|
| 149 |
#[cfg(feature = "by_ref_proposal")] |
| 150 |
pub fn validate_external_senders_ext_credentials( |
| 151 |
&self, |
| 152 |
leaf_node: &LeafNode, |
| 153 |
) -> Result<(), MlsError> { |
| 154 |
let Some(ext) = self |
| 155 |
.context |
| 156 |
.new_extensions() |
| 157 |
.and_then(|ext| ext.get_as::<ExternalSendersExt>().transpose()) |
| 158 |
.transpose()? |
| 159 |
else { |
| 160 |
return Ok(()); |
| 161 |
}; |
| 162 |
|
| 163 |
ext.allowed_senders.iter().try_for_each(|sender| { |
| 164 |
let cred_type = sender.credential.credential_type(); |
| 165 |
leaf_node |
| 166 |
.capabilities |
| 167 |
.credentials |
| 168 |
.contains(&cred_type) |
| 169 |
.then_some(()) |
| 170 |
.ok_or(MlsError::RequiredCredentialNotFound(cred_type)) |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 175 |
pub(crate) async fn check_if_valid( |
| 176 |
&self, |
| 177 |
leaf_node: &LeafNode, |
| 178 |
context: ValidationContext<'_>, |
| 179 |
) -> Result<(), MlsError> { |
| 180 |
// Check that we are validating within the proper context |
| 181 |
self.check_context(leaf_node, &context)?; |
| 182 |
|
| 183 |
// Verify the credential |
| 184 |
self.identity_provider |
| 185 |
.validate_member( |
| 186 |
&leaf_node.signing_identity, |
| 187 |
context.generation_time(), |
| 188 |
self.context, |
| 189 |
) |
| 190 |
.await |
| 191 |
.map_err(|e| MlsError::IdentityProviderError(e.into_any_error()))?; |
| 192 |
|
| 193 |
// Verify that the credential signed the leaf node |
| 194 |
leaf_node |
| 195 |
.verify( |
| 196 |
self.cipher_suite_provider, |
| 197 |
&leaf_node.signing_identity.signature_key, |
| 198 |
&context.signing_context(), |
| 199 |
) |
| 200 |
.await?; |
| 201 |
|
| 202 |
// If required capabilities are specified, verify the leaf node meets the requirements |
| 203 |
self.validate_required_capabilities(leaf_node)?; |
| 204 |
|
| 205 |
// If there are extensions, make sure they are referenced in the capabilities field |
| 206 |
for one_ext in &*leaf_node.extensions { |
| 207 |
if !leaf_node |
| 208 |
.capabilities |
| 209 |
.extensions |
| 210 |
.contains(&one_ext.extension_type) |
| 211 |
{ |
| 212 |
return Err(MlsError::ExtensionNotInCapabilities(one_ext.extension_type)); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// Verify that group extensions are supported by the leaf |
| 217 |
if let Some(extensions) = self.context.new_extensions() { |
| 218 |
extensions |
| 219 |
.iter() |
| 220 |
.map(|ext| ext.extension_type) |
| 221 |
.find(|ext_type| { |
| 222 |
!ext_type.is_default() && !leaf_node.capabilities.extensions.contains(ext_type) |
| 223 |
}) |
| 224 |
.map(MlsError::UnsupportedGroupExtension) |
| 225 |
.map_or(Ok(()), Err)?; |
| 226 |
} |
| 227 |
|
| 228 |
#[cfg(feature = "by_ref_proposal")] |
| 229 |
self.validate_external_senders_ext_credentials(leaf_node)?; |
| 230 |
|
| 231 |
Ok(()) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
#[cfg(test)] |
| 236 |
mod tests { |
| 237 |
use crate::client::test_utils::TEST_PROTOCOL_VERSION; |
| 238 |
use crate::crypto::test_utils::try_test_cipher_suite_provider; |
| 239 |
use crate::extension::MlsExtension; |
| 240 |
use alloc::vec; |
| 241 |
use assert_matches::assert_matches; |
| 242 |
#[cfg(feature = "std")] |
| 243 |
use core::time::Duration; |
| 244 |
use mls_rs_core::crypto::CipherSuite; |
| 245 |
use mls_rs_core::group::GroupContext; |
| 246 |
use mls_rs_core::group::ProposalType; |
| 247 |
|
| 248 |
use super::*; |
| 249 |
|
| 250 |
use crate::client::test_utils::TEST_CIPHER_SUITE; |
| 251 |
use crate::crypto::test_utils::test_cipher_suite_provider; |
| 252 |
use crate::crypto::test_utils::TestCryptoProvider; |
| 253 |
use crate::crypto::SignatureSecretKey; |
| 254 |
use crate::extension::test_utils::TestExtension; |
| 255 |
use crate::group::test_utils::random_bytes; |
| 256 |
use crate::identity::basic::BasicCredential; |
| 257 |
use crate::identity::basic::BasicIdentityProvider; |
| 258 |
use crate::identity::test_utils::get_test_signing_identity; |
| 259 |
use crate::tree_kem::leaf_node::test_utils::*; |
| 260 |
use crate::tree_kem::leaf_node_validator::test_utils::FailureIdentityProvider; |
| 261 |
use crate::tree_kem::Capabilities; |
| 262 |
use crate::ExtensionList; |
| 263 |
|
| 264 |
impl<'a, P: IdentityProvider, C: CipherSuiteProvider> LeafNodeValidator<'a, P, C> { |
| 265 |
pub fn new_for_test(cipher_suite_provider: &'a C, identity_provider: &'a P) -> Self { |
| 266 |
Self { |
| 267 |
cipher_suite_provider, |
| 268 |
identity_provider, |
| 269 |
context: MemberValidationContext::None, |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 275 |
async fn get_test_add_node() -> (LeafNode, SignatureSecretKey) { |
| 276 |
let (signing_identity, secret) = get_test_signing_identity(TEST_CIPHER_SUITE, b"foo").await; |
| 277 |
|
| 278 |
let (leaf_node, _) = |
| 279 |
get_test_node(TEST_CIPHER_SUITE, signing_identity, &secret, None, None).await; |
| 280 |
|
| 281 |
(leaf_node, secret) |
| 282 |
} |
| 283 |
|
| 284 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 285 |
async fn test_basic_add_validation() { |
| 286 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 287 |
|
| 288 |
let (leaf_node, _) = get_test_add_node().await; |
| 289 |
|
| 290 |
let test_validator = |
| 291 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 292 |
|
| 293 |
let res = test_validator |
| 294 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 295 |
.await; |
| 296 |
|
| 297 |
assert_matches!(res, Ok(_)); |
| 298 |
} |
| 299 |
|
| 300 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 301 |
async fn test_failed_validation() { |
| 302 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 303 |
let (leaf_node, _) = get_test_add_node().await; |
| 304 |
|
| 305 |
let fail_test_validator = |
| 306 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &FailureIdentityProvider); |
| 307 |
|
| 308 |
let res = fail_test_validator |
| 309 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 310 |
.await; |
| 311 |
|
| 312 |
assert_matches!(res, Err(MlsError::IdentityProviderError(_))); |
| 313 |
} |
| 314 |
|
| 315 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 316 |
async fn test_basic_update_validation() { |
| 317 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 318 |
let group_id = b"group_id"; |
| 319 |
|
| 320 |
let (mut leaf_node, secret) = get_test_add_node().await; |
| 321 |
|
| 322 |
leaf_node |
| 323 |
.update( |
| 324 |
&cipher_suite_provider, |
| 325 |
group_id, |
| 326 |
0, |
| 327 |
// TODO remove identity from input |
| 328 |
Some(default_properties()), |
| 329 |
None, |
| 330 |
&secret, |
| 331 |
) |
| 332 |
.await |
| 333 |
.unwrap(); |
| 334 |
|
| 335 |
let test_validator = |
| 336 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 337 |
|
| 338 |
let res = test_validator |
| 339 |
.check_if_valid(&leaf_node, ValidationContext::Update((group_id, 0, None))) |
| 340 |
.await; |
| 341 |
|
| 342 |
assert_matches!(res, Ok(_)); |
| 343 |
} |
| 344 |
|
| 345 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 346 |
async fn test_basic_commit_validation() { |
| 347 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 348 |
let group_id = b"group_id"; |
| 349 |
|
| 350 |
let (mut leaf_node, secret) = get_test_add_node().await; |
| 351 |
|
| 352 |
leaf_node.leaf_node_source = LeafNodeSource::Commit(hex!("f00d").into()); |
| 353 |
|
| 354 |
leaf_node |
| 355 |
.commit( |
| 356 |
&cipher_suite_provider, |
| 357 |
group_id, |
| 358 |
0, |
| 359 |
Some(default_properties()), |
| 360 |
None, |
| 361 |
&secret, |
| 362 |
) |
| 363 |
.await |
| 364 |
.unwrap(); |
| 365 |
|
| 366 |
let test_validator = |
| 367 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 368 |
|
| 369 |
let res = test_validator |
| 370 |
.check_if_valid(&leaf_node, ValidationContext::Commit((group_id, 0, None))) |
| 371 |
.await; |
| 372 |
|
| 373 |
assert_matches!(res, Ok(_)); |
| 374 |
} |
| 375 |
|
| 376 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 377 |
async fn test_incorrect_context() { |
| 378 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 379 |
|
| 380 |
let test_validator = |
| 381 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 382 |
|
| 383 |
let (mut leaf_node, secret) = get_test_add_node().await; |
| 384 |
|
| 385 |
let res = test_validator |
| 386 |
.check_if_valid(&leaf_node, ValidationContext::Update((b"foo", 0, None))) |
| 387 |
.await; |
| 388 |
|
| 389 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 390 |
|
| 391 |
let res = test_validator |
| 392 |
.check_if_valid(&leaf_node, ValidationContext::Commit((b"foo", 0, None))) |
| 393 |
.await; |
| 394 |
|
| 395 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 396 |
|
| 397 |
leaf_node |
| 398 |
.update( |
| 399 |
&cipher_suite_provider, |
| 400 |
b"foo", |
| 401 |
0, |
| 402 |
Some(default_properties()), |
| 403 |
None, |
| 404 |
&secret, |
| 405 |
) |
| 406 |
.await |
| 407 |
.unwrap(); |
| 408 |
|
| 409 |
let res = test_validator |
| 410 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 411 |
.await; |
| 412 |
|
| 413 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 414 |
|
| 415 |
let res = test_validator |
| 416 |
.check_if_valid(&leaf_node, ValidationContext::Commit((b"foo", 0, None))) |
| 417 |
.await; |
| 418 |
|
| 419 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 420 |
|
| 421 |
leaf_node.leaf_node_source = LeafNodeSource::Commit(hex!("f00d").into()); |
| 422 |
|
| 423 |
leaf_node |
| 424 |
.commit( |
| 425 |
&cipher_suite_provider, |
| 426 |
b"foo", |
| 427 |
0, |
| 428 |
Some(default_properties()), |
| 429 |
None, |
| 430 |
&secret, |
| 431 |
) |
| 432 |
.await |
| 433 |
.unwrap(); |
| 434 |
|
| 435 |
let res = test_validator |
| 436 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 437 |
.await; |
| 438 |
|
| 439 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 440 |
|
| 441 |
let res = test_validator |
| 442 |
.check_if_valid(&leaf_node, ValidationContext::Update((b"foo", 0, None))) |
| 443 |
.await; |
| 444 |
|
| 445 |
assert_matches!(res, Err(MlsError::InvalidLeafNodeSource)); |
| 446 |
} |
| 447 |
|
| 448 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 449 |
async fn test_bad_signature() { |
| 450 |
for cipher_suite in TestCryptoProvider::all_supported_cipher_suites() { |
| 451 |
let cipher_suite_provider = test_cipher_suite_provider(cipher_suite); |
| 452 |
|
| 453 |
let (signing_identity, secret) = get_test_signing_identity(cipher_suite, b"foo").await; |
| 454 |
|
| 455 |
let (mut leaf_node, _) = |
| 456 |
get_test_node(cipher_suite, signing_identity, &secret, None, None).await; |
| 457 |
|
| 458 |
leaf_node.signature = random_bytes(leaf_node.signature.len()); |
| 459 |
|
| 460 |
let test_validator = |
| 461 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 462 |
|
| 463 |
let res = test_validator |
| 464 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 465 |
.await; |
| 466 |
|
| 467 |
assert_matches!(res, Err(MlsError::InvalidSignature)); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 472 |
async fn test_capabilities_mismatch() { |
| 473 |
let (signing_identity, secret) = get_test_signing_identity(TEST_CIPHER_SUITE, b"foo").await; |
| 474 |
|
| 475 |
let mut extensions = ExtensionList::new(); |
| 476 |
|
| 477 |
extensions.set_from(TestExtension::from(0)).unwrap(); |
| 478 |
|
| 479 |
let capabilities = Capabilities { |
| 480 |
credentials: vec![BasicCredential::credential_type()], |
| 481 |
..Default::default() |
| 482 |
}; |
| 483 |
|
| 484 |
let (leaf_node, _) = get_test_node( |
| 485 |
TEST_CIPHER_SUITE, |
| 486 |
signing_identity, |
| 487 |
&secret, |
| 488 |
Some(capabilities), |
| 489 |
Some(extensions), |
| 490 |
) |
| 491 |
.await; |
| 492 |
|
| 493 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 494 |
|
| 495 |
let test_validator = |
| 496 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 497 |
|
| 498 |
let res = test_validator |
| 499 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 500 |
.await; |
| 501 |
|
| 502 |
assert_matches!(res, |
| 503 |
Err(MlsError::ExtensionNotInCapabilities(ext)) if ext == 42.into()); |
| 504 |
} |
| 505 |
|
| 506 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 507 |
async fn test_cipher_suite_mismatch() { |
| 508 |
for another_cipher_suite in CipherSuite::all().filter(|cs| cs != &TEST_CIPHER_SUITE) { |
| 509 |
if let Some(cs) = try_test_cipher_suite_provider(*another_cipher_suite) { |
| 510 |
let (leaf_node, _) = get_test_add_node().await; |
| 511 |
|
| 512 |
let test_validator = LeafNodeValidator::new_for_test(&cs, &BasicIdentityProvider); |
| 513 |
|
| 514 |
let res = test_validator |
| 515 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 516 |
.await; |
| 517 |
|
| 518 |
assert_matches!(res, Err(MlsError::InvalidSignature)); |
| 519 |
} |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 524 |
async fn test_required_extension() { |
| 525 |
let required_capabilities = RequiredCapabilitiesExt { |
| 526 |
extensions: vec![43.into()], |
| 527 |
..Default::default() |
| 528 |
}; |
| 529 |
|
| 530 |
let (leaf_node, _) = get_test_add_node().await; |
| 531 |
|
| 532 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 533 |
|
| 534 |
let group_context_extensions = |
| 535 |
core::iter::once(required_capabilities.into_extension().unwrap()).collect(); |
| 536 |
|
| 537 |
let context = MemberValidationContext::ForCommit { |
| 538 |
current_context: &fake_group_context(), |
| 539 |
new_extensions: &group_context_extensions, |
| 540 |
}; |
| 541 |
|
| 542 |
let test_validator = |
| 543 |
LeafNodeValidator::new(&cipher_suite_provider, &BasicIdentityProvider, context); |
| 544 |
|
| 545 |
let res = test_validator |
| 546 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 547 |
.await; |
| 548 |
|
| 549 |
assert_matches!( |
| 550 |
res, |
| 551 |
Err(MlsError::RequiredExtensionNotFound(v)) if v == 43.into() |
| 552 |
); |
| 553 |
} |
| 554 |
|
| 555 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 556 |
async fn test_required_proposal() { |
| 557 |
let required_capabilities = RequiredCapabilitiesExt { |
| 558 |
proposals: vec![42.into()], |
| 559 |
..Default::default() |
| 560 |
}; |
| 561 |
|
| 562 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 563 |
|
| 564 |
let (leaf_node, _) = get_test_add_node().await; |
| 565 |
|
| 566 |
let group_context_extensions = |
| 567 |
core::iter::once(required_capabilities.into_extension().unwrap()).collect(); |
| 568 |
|
| 569 |
let context = MemberValidationContext::ForCommit { |
| 570 |
current_context: &fake_group_context(), |
| 571 |
new_extensions: &group_context_extensions, |
| 572 |
}; |
| 573 |
|
| 574 |
let test_validator = |
| 575 |
LeafNodeValidator::new(&cipher_suite_provider, &BasicIdentityProvider, context); |
| 576 |
|
| 577 |
let res = test_validator |
| 578 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 579 |
.await; |
| 580 |
|
| 581 |
assert_matches!( |
| 582 |
res, |
| 583 |
Err(MlsError::RequiredProposalNotFound(p)) if p == ProposalType::new(42) |
| 584 |
); |
| 585 |
} |
| 586 |
|
| 587 |
fn fake_group_context() -> GroupContext { |
| 588 |
GroupContext { |
| 589 |
protocol_version: TEST_PROTOCOL_VERSION, |
| 590 |
cipher_suite: TEST_CIPHER_SUITE, |
| 591 |
group_id: b"unused".into(), |
| 592 |
epoch: 0, |
| 593 |
tree_hash: b"unused".into(), |
| 594 |
confirmed_transcript_hash: b"unused".to_vec().into(), |
| 595 |
extensions: Default::default(), |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 600 |
async fn test_required_credential() { |
| 601 |
let required_capabilities = RequiredCapabilitiesExt { |
| 602 |
credentials: vec![0.into()], |
| 603 |
..Default::default() |
| 604 |
}; |
| 605 |
|
| 606 |
let (leaf_node, _) = get_test_add_node().await; |
| 607 |
|
| 608 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 609 |
|
| 610 |
let group_context_extensions = |
| 611 |
core::iter::once(required_capabilities.into_extension().unwrap()).collect(); |
| 612 |
|
| 613 |
let context = MemberValidationContext::ForCommit { |
| 614 |
current_context: &fake_group_context(), |
| 615 |
new_extensions: &group_context_extensions, |
| 616 |
}; |
| 617 |
|
| 618 |
let test_validator = |
| 619 |
LeafNodeValidator::new(&cipher_suite_provider, &BasicIdentityProvider, context); |
| 620 |
|
| 621 |
let res = test_validator |
| 622 |
.check_if_valid(&leaf_node, ValidationContext::Add(None)) |
| 623 |
.await; |
| 624 |
|
| 625 |
assert_matches!(res, |
| 626 |
Err(MlsError::RequiredCredentialNotFound(ext)) if ext == 0.into() |
| 627 |
); |
| 628 |
} |
| 629 |
|
| 630 |
#[cfg(feature = "std")] |
| 631 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 632 |
async fn test_add_lifetime() { |
| 633 |
let (leaf_node, _) = get_test_add_node().await; |
| 634 |
|
| 635 |
let cipher_suite_provider = test_cipher_suite_provider(TEST_CIPHER_SUITE); |
| 636 |
|
| 637 |
let test_validator = |
| 638 |
LeafNodeValidator::new_for_test(&cipher_suite_provider, &BasicIdentityProvider); |
| 639 |
|
| 640 |
let good_lifetime = MlsTime::now(); |
| 641 |
|
| 642 |
let over_one_year = good_lifetime.seconds_since_epoch() + (86400 * 366); |
| 643 |
|
| 644 |
let bad_lifetime = MlsTime::from_duration_since_epoch(Duration::from_secs(over_one_year)); |
| 645 |
|
| 646 |
let res = test_validator |
| 647 |
.check_if_valid(&leaf_node, ValidationContext::Add(Some(good_lifetime))) |
| 648 |
.await; |
| 649 |
|
| 650 |
assert_matches!(res, Ok(())); |
| 651 |
|
| 652 |
let res = test_validator |
| 653 |
.check_if_valid(&leaf_node, ValidationContext::Add(Some(bad_lifetime))) |
| 654 |
.await; |
| 655 |
|
| 656 |
assert_matches!(res, Err(MlsError::InvalidLifetime)); |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
#[cfg(test)] |
| 661 |
pub(crate) mod test_utils { |
| 662 |
#[cfg(feature = "std")] |
| 663 |
use alloc::boxed::Box; |
| 664 |
use alloc::vec; |
| 665 |
use alloc::vec::Vec; |
| 666 |
use mls_rs_codec::MlsEncode; |
| 667 |
use mls_rs_core::{ |
| 668 |
error::IntoAnyError, |
| 669 |
extension::ExtensionList, |
| 670 |
identity::{BasicCredential, IdentityProvider, MemberValidationContext}, |
| 671 |
}; |
| 672 |
|
| 673 |
use crate::{identity::SigningIdentity, time::MlsTime}; |
| 674 |
|
| 675 |
#[derive(Clone, Debug, Default)] |
| 676 |
pub struct FailureIdentityProvider; |
| 677 |
|
| 678 |
#[cfg(feature = "by_ref_proposal")] |
| 679 |
impl FailureIdentityProvider { |
| 680 |
pub fn new() -> Self { |
| 681 |
Self |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
#[derive(Debug)] |
| 686 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 687 |
#[cfg_attr(feature = "std", error("test error"))] |
| 688 |
pub struct TestFailureError; |
| 689 |
|
| 690 |
impl IntoAnyError for TestFailureError { |
| 691 |
#[cfg(feature = "std")] |
| 692 |
fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> { |
| 693 |
Ok(self.into()) |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 698 |
#[cfg_attr(mls_build_async, maybe_async::must_be_async)] |
| 699 |
impl IdentityProvider for FailureIdentityProvider { |
| 700 |
type Error = TestFailureError; |
| 701 |
|
| 702 |
async fn validate_member( |
| 703 |
&self, |
| 704 |
_signing_identity: &SigningIdentity, |
| 705 |
_timestamp: Option<MlsTime>, |
| 706 |
_context: MemberValidationContext<'_>, |
| 707 |
) -> Result<(), Self::Error> { |
| 708 |
Err(TestFailureError) |
| 709 |
} |
| 710 |
|
| 711 |
async fn validate_external_sender( |
| 712 |
&self, |
| 713 |
_signing_identity: &SigningIdentity, |
| 714 |
_timestamp: Option<MlsTime>, |
| 715 |
_extensions: Option<&ExtensionList>, |
| 716 |
) -> Result<(), Self::Error> { |
| 717 |
Err(TestFailureError) |
| 718 |
} |
| 719 |
|
| 720 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 721 |
async fn identity( |
| 722 |
&self, |
| 723 |
signing_id: &SigningIdentity, |
| 724 |
_extensions: &ExtensionList, |
| 725 |
) -> Result<Vec<u8>, Self::Error> { |
| 726 |
Ok(signing_id.credential.mls_encode_to_vec().unwrap()) |
| 727 |
} |
| 728 |
|
| 729 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 730 |
async fn valid_successor( |
| 731 |
&self, |
| 732 |
_predecessor: &SigningIdentity, |
| 733 |
_successor: &SigningIdentity, |
| 734 |
_extensions: &ExtensionList, |
| 735 |
) -> Result<bool, Self::Error> { |
| 736 |
Err(TestFailureError) |
| 737 |
} |
| 738 |
|
| 739 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 740 |
fn supported_types(&self) -> Vec<crate::identity::CredentialType> { |
| 741 |
vec![BasicCredential::credential_type()] |
| 742 |
} |
| 743 |
} |
| 744 |
} |