| 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 |
/// The example shows how how to create an MLS extension implementing an access control policy |
| 6 |
/// based on the concept of users, similar to |
| 7 |
/// https://bifurcation.github.io/ietf-mimi-protocol/draft-ralston-mimi-protocol.html. |
| 8 |
/// |
| 9 |
/// A user, e.g. "[email protected]", owns zero or more MLS members, e.g. Bob's tablet and PC. |
| 10 |
/// Users do not have MLS cryptographic state, while MLS members do. At any point in time, |
| 11 |
/// the MLS group has a fixed set of users and for each user, zero or more MLS members they |
| 12 |
/// own. Each user also has a role, e.g. a regular user or moderator (which may possibly change |
| 13 |
/// over time). |
| 14 |
/// |
| 15 |
/// The goal is to implement the following rule: |
| 16 |
/// 1. Each MLS member belongs to a user in the group. |
| 17 |
/// |
| 18 |
/// To this end, we implement the following: |
| 19 |
/// * A GroupContext extension containing the current list of users. MLS guarantees agreement |
| 20 |
/// on the list. |
| 21 |
/// * An AddUser proposal that modifies the user list. |
| 22 |
/// * An MLS credential type for MLS members with the owning user's public key and signature. |
| 23 |
/// When MLS members join using MLS Add proposals, the signature is verified. |
| 24 |
/// * Proposal validation rules that enforce 1. above. |
| 25 |
/// |
| 26 |
use assert_matches::assert_matches; |
| 27 |
use mls_rs::{ |
| 28 |
client_builder::{MlsConfig, PaddingMode}, |
| 29 |
error::MlsError, |
| 30 |
group::{ |
| 31 |
proposal::{MlsCustomProposal, Proposal}, |
| 32 |
GroupContext, Roster, Sender, |
| 33 |
}, |
| 34 |
mls_rules::{ |
| 35 |
CommitDirection, CommitOptions, CommitSource, EncryptionOptions, ProposalBundle, |
| 36 |
ProposalSource, |
| 37 |
}, |
| 38 |
CipherSuite, CipherSuiteProvider, Client, CryptoProvider, ExtensionList, IdentityProvider, |
| 39 |
MlsRules, |
| 40 |
}; |
| 41 |
use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize}; |
| 42 |
use mls_rs_core::{ |
| 43 |
crypto::{SignaturePublicKey, SignatureSecretKey}, |
| 44 |
error::IntoAnyError, |
| 45 |
extension::{ExtensionError, ExtensionType, MlsCodecExtension}, |
| 46 |
group::ProposalType, |
| 47 |
identity::{ |
| 48 |
Credential, CredentialType, CustomCredential, MemberValidationContext, MlsCredential, |
| 49 |
SigningIdentity, |
| 50 |
}, |
| 51 |
time::MlsTime, |
| 52 |
}; |
| 53 |
|
| 54 |
use std::fmt::Display; |
| 55 |
|
| 56 |
const CIPHER_SUITE: CipherSuite = CipherSuite::CURVE25519_AES128; |
| 57 |
|
| 58 |
const ROSTER_EXTENSION_V1: ExtensionType = ExtensionType::new(65000); |
| 59 |
const ADD_USER_PROPOSAL_V1: ProposalType = ProposalType::new(65001); |
| 60 |
const CREDENTIAL_V1: CredentialType = CredentialType::new(65002); |
| 61 |
|
| 62 |
fn crypto() -> impl CryptoProvider + Clone { |
| 63 |
mls_rs_crypto_openssl::OpensslCryptoProvider::new() |
| 64 |
} |
| 65 |
|
| 66 |
fn cipher_suite() -> impl CipherSuiteProvider { |
| 67 |
crypto().cipher_suite_provider(CIPHER_SUITE).unwrap() |
| 68 |
} |
| 69 |
|
| 70 |
#[derive(MlsSize, MlsDecode, MlsEncode)] |
| 71 |
#[repr(u8)] |
| 72 |
enum UserRole { |
| 73 |
Regular = 1u8, |
| 74 |
Moderator = 2u8, |
| 75 |
} |
| 76 |
|
| 77 |
#[derive(MlsSize, MlsDecode, MlsEncode)] |
| 78 |
struct UserCredential { |
| 79 |
name: String, |
| 80 |
role: UserRole, |
| 81 |
public_key: SignaturePublicKey, |
| 82 |
} |
| 83 |
|
| 84 |
#[derive(MlsSize, MlsDecode, MlsEncode)] |
| 85 |
struct MemberCredential { |
| 86 |
name: String, |
| 87 |
user_public_key: SignaturePublicKey, // Identifies the user |
| 88 |
signature: Vec<u8>, |
| 89 |
} |
| 90 |
|
| 91 |
#[derive(MlsSize, MlsEncode)] |
| 92 |
struct MemberCredentialTBS<'a> { |
| 93 |
name: &'a str, |
| 94 |
user_public_key: &'a SignaturePublicKey, |
| 95 |
public_key: &'a SignaturePublicKey, |
| 96 |
} |
| 97 |
|
| 98 |
/// The roster will be stored in the custom RosterExtension, an extension in the MLS GroupContext |
| 99 |
#[derive(MlsSize, MlsDecode, MlsEncode)] |
| 100 |
struct RosterExtension { |
| 101 |
roster: Vec<UserCredential>, |
| 102 |
} |
| 103 |
|
| 104 |
impl MlsCodecExtension for RosterExtension { |
| 105 |
fn extension_type() -> ExtensionType { |
| 106 |
ROSTER_EXTENSION_V1 |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/// The custom AddUser proposal will be used to update the RosterExtension |
| 111 |
#[derive(MlsSize, MlsDecode, MlsEncode)] |
| 112 |
struct AddUserProposal { |
| 113 |
new_user: UserCredential, |
| 114 |
} |
| 115 |
|
| 116 |
impl MlsCustomProposal for AddUserProposal { |
| 117 |
fn proposal_type() -> ProposalType { |
| 118 |
ADD_USER_PROPOSAL_V1 |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/// MlsRules tell MLS how to handle our custom proposal |
| 123 |
#[derive(Debug, Clone, Copy)] |
| 124 |
struct CustomMlsRules; |
| 125 |
|
| 126 |
impl MlsRules for CustomMlsRules { |
| 127 |
type Error = CustomError; |
| 128 |
|
| 129 |
fn filter_proposals( |
| 130 |
&self, |
| 131 |
_: CommitDirection, |
| 132 |
_: CommitSource, |
| 133 |
_: &Roster, |
| 134 |
context: &GroupContext, |
| 135 |
mut proposals: ProposalBundle, |
| 136 |
) -> Result<ProposalBundle, Self::Error> { |
| 137 |
// Find our extension |
| 138 |
let mut roster: RosterExtension = context |
| 139 |
.extensions |
| 140 |
.get_as() |
| 141 |
.ok() |
| 142 |
.flatten() |
| 143 |
.ok_or(CustomError)?; |
| 144 |
|
| 145 |
// Find AddUser proposals |
| 146 |
let add_user_proposals = proposals |
| 147 |
.custom_proposals() |
| 148 |
.iter() |
| 149 |
.filter(|p| p.proposal.proposal_type() == ADD_USER_PROPOSAL_V1); |
| 150 |
|
| 151 |
for add_user_info in add_user_proposals { |
| 152 |
let add_user = AddUserProposal::from_custom_proposal(&add_user_info.proposal)?; |
| 153 |
|
| 154 |
// Eventually we should check for duplicates |
| 155 |
roster.roster.push(add_user.new_user); |
| 156 |
} |
| 157 |
|
| 158 |
// Issue GroupContextExtensions proposal to modify our roster (eventually we don't have to do this if there were no AddUser proposals) |
| 159 |
let mut new_extensions = context.extensions.clone(); |
| 160 |
new_extensions.set_from(roster)?; |
| 161 |
let gce_proposal = Proposal::GroupContextExtensions(new_extensions); |
| 162 |
proposals.add(gce_proposal, Sender::Member(0), ProposalSource::Local); |
| 163 |
|
| 164 |
Ok(proposals) |
| 165 |
} |
| 166 |
|
| 167 |
fn commit_options( |
| 168 |
&self, |
| 169 |
_: &Roster, |
| 170 |
_: &GroupContext, |
| 171 |
_: &ProposalBundle, |
| 172 |
) -> Result<CommitOptions, Self::Error> { |
| 173 |
Ok(CommitOptions::new()) |
| 174 |
} |
| 175 |
|
| 176 |
fn encryption_options( |
| 177 |
&self, |
| 178 |
_: &Roster, |
| 179 |
_: &GroupContext, |
| 180 |
) -> Result<EncryptionOptions, Self::Error> { |
| 181 |
Ok(EncryptionOptions::new(false, PaddingMode::None)) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// The IdentityProvider will tell MLS how to validate members' identities. We will use custom identity |
| 186 |
// type to store our User structs. |
| 187 |
impl MlsCredential for MemberCredential { |
| 188 |
type Error = CustomError; |
| 189 |
|
| 190 |
fn credential_type() -> CredentialType { |
| 191 |
CREDENTIAL_V1 |
| 192 |
} |
| 193 |
|
| 194 |
fn into_credential(self) -> Result<Credential, Self::Error> { |
| 195 |
Ok(Credential::Custom(CustomCredential::new( |
| 196 |
Self::credential_type(), |
| 197 |
self.mls_encode_to_vec()?, |
| 198 |
))) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
#[derive(Debug, Clone, Copy)] |
| 203 |
struct CustomIdentityProvider; |
| 204 |
|
| 205 |
impl IdentityProvider for CustomIdentityProvider { |
| 206 |
type Error = CustomError; |
| 207 |
|
| 208 |
fn validate_member( |
| 209 |
&self, |
| 210 |
signing_identity: &SigningIdentity, |
| 211 |
_: Option<MlsTime>, |
| 212 |
context: MemberValidationContext<'_>, |
| 213 |
) -> Result<(), Self::Error> { |
| 214 |
let Some(extensions) = context.new_extensions() else { |
| 215 |
return Ok(()); |
| 216 |
}; |
| 217 |
|
| 218 |
let roster = extensions |
| 219 |
.get_as::<RosterExtension>() |
| 220 |
.ok() |
| 221 |
.flatten() |
| 222 |
.ok_or(CustomError)?; |
| 223 |
|
| 224 |
// Retrieve the MemberCredential from the MLS credential |
| 225 |
let Credential::Custom(custom) = &signing_identity.credential else { |
| 226 |
return Err(CustomError); |
| 227 |
}; |
| 228 |
|
| 229 |
if custom.credential_type != CREDENTIAL_V1 { |
| 230 |
return Err(CustomError); |
| 231 |
} |
| 232 |
|
| 233 |
let member = MemberCredential::mls_decode(&mut &*custom.data)?; |
| 234 |
|
| 235 |
// Validate the MemberCredential |
| 236 |
|
| 237 |
let tbs = MemberCredentialTBS { |
| 238 |
name: &member.name, |
| 239 |
user_public_key: &member.user_public_key, |
| 240 |
public_key: &signing_identity.signature_key, |
| 241 |
} |
| 242 |
.mls_encode_to_vec()?; |
| 243 |
|
| 244 |
cipher_suite() |
| 245 |
.verify(&member.user_public_key, &member.signature, &tbs) |
| 246 |
.map_err(|_| CustomError)?; |
| 247 |
|
| 248 |
let user_in_roster = roster |
| 249 |
.roster |
| 250 |
.iter() |
| 251 |
.any(|u| u.public_key == member.user_public_key); |
| 252 |
|
| 253 |
if !user_in_roster { |
| 254 |
return Err(CustomError); |
| 255 |
} |
| 256 |
|
| 257 |
Ok(()) |
| 258 |
} |
| 259 |
|
| 260 |
fn identity( |
| 261 |
&self, |
| 262 |
signing_identity: &SigningIdentity, |
| 263 |
_: &ExtensionList, |
| 264 |
) -> Result<Vec<u8>, Self::Error> { |
| 265 |
Ok(signing_identity.mls_encode_to_vec()?) |
| 266 |
} |
| 267 |
|
| 268 |
fn supported_types(&self) -> Vec<CredentialType> { |
| 269 |
vec![CREDENTIAL_V1] |
| 270 |
} |
| 271 |
|
| 272 |
fn valid_successor( |
| 273 |
&self, |
| 274 |
_: &SigningIdentity, |
| 275 |
_: &SigningIdentity, |
| 276 |
_: &ExtensionList, |
| 277 |
) -> Result<bool, Self::Error> { |
| 278 |
Ok(true) |
| 279 |
} |
| 280 |
|
| 281 |
fn validate_external_sender( |
| 282 |
&self, |
| 283 |
_: &SigningIdentity, |
| 284 |
_: Option<MlsTime>, |
| 285 |
_: Option<&ExtensionList>, |
| 286 |
) -> Result<(), Self::Error> { |
| 287 |
Ok(()) |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
// Convenience structs to create users and members |
| 292 |
|
| 293 |
struct User { |
| 294 |
credential: UserCredential, |
| 295 |
signer: SignatureSecretKey, |
| 296 |
} |
| 297 |
|
| 298 |
impl User { |
| 299 |
fn new(name: &str, role: UserRole) -> Result<Self, CustomError> { |
| 300 |
let (signer, public_key) = cipher_suite() |
| 301 |
.signature_key_generate() |
| 302 |
.map_err(|_| CustomError)?; |
| 303 |
|
| 304 |
let credential = UserCredential { |
| 305 |
name: name.into(), |
| 306 |
role, |
| 307 |
public_key, |
| 308 |
}; |
| 309 |
|
| 310 |
Ok(Self { credential, signer }) |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
struct Member { |
| 315 |
credential: MemberCredential, |
| 316 |
public_key: SignaturePublicKey, |
| 317 |
signer: SignatureSecretKey, |
| 318 |
} |
| 319 |
|
| 320 |
impl Member { |
| 321 |
fn new(name: &str, user: &User) -> Result<Self, CustomError> { |
| 322 |
let (signer, public_key) = cipher_suite() |
| 323 |
.signature_key_generate() |
| 324 |
.map_err(|_| CustomError)?; |
| 325 |
|
| 326 |
let tbs = MemberCredentialTBS { |
| 327 |
name, |
| 328 |
user_public_key: &user.credential.public_key, |
| 329 |
public_key: &public_key, |
| 330 |
} |
| 331 |
.mls_encode_to_vec()?; |
| 332 |
|
| 333 |
let signature = cipher_suite() |
| 334 |
.sign(&user.signer, &tbs) |
| 335 |
.map_err(|_| CustomError)?; |
| 336 |
|
| 337 |
let credential = MemberCredential { |
| 338 |
name: name.into(), |
| 339 |
user_public_key: user.credential.public_key.clone(), |
| 340 |
signature, |
| 341 |
}; |
| 342 |
|
| 343 |
Ok(Self { |
| 344 |
credential, |
| 345 |
signer, |
| 346 |
public_key, |
| 347 |
}) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// Set up Client to use our custom providers |
| 352 |
fn make_client(member: Member) -> Result<Client<impl MlsConfig>, CustomError> { |
| 353 |
let mls_credential = member.credential.into_credential()?; |
| 354 |
let signing_identity = SigningIdentity::new(mls_credential, member.public_key); |
| 355 |
|
| 356 |
Ok(Client::builder() |
| 357 |
.identity_provider(CustomIdentityProvider) |
| 358 |
.mls_rules(CustomMlsRules) |
| 359 |
.custom_proposal_type(ADD_USER_PROPOSAL_V1) |
| 360 |
.extension_type(ROSTER_EXTENSION_V1) |
| 361 |
.crypto_provider(crypto()) |
| 362 |
.signing_identity(signing_identity, member.signer, CIPHER_SUITE) |
| 363 |
.build()) |
| 364 |
} |
| 365 |
|
| 366 |
fn main() -> Result<(), CustomError> { |
| 367 |
let alice = User::new("alice", UserRole::Moderator)?; |
| 368 |
let bob = User::new("bob", UserRole::Regular)?; |
| 369 |
|
| 370 |
let alice_tablet = Member::new("alice tablet", &alice)?; |
| 371 |
let alice_pc = Member::new("alice pc", &alice)?; |
| 372 |
let bob_tablet = Member::new("bob tablet", &bob)?; |
| 373 |
|
| 374 |
// Alice creates the group with our RosterExtension containing her user |
| 375 |
let mut context_extensions = ExtensionList::new(); |
| 376 |
let roster = vec![alice.credential]; |
| 377 |
context_extensions.set_from(RosterExtension { roster })?; |
| 378 |
|
| 379 |
let mut alice_tablet_group = |
| 380 |
make_client(alice_tablet)?.create_group(context_extensions, Default::default())?; |
| 381 |
|
| 382 |
// Alice can add her other device |
| 383 |
let alice_pc_client = make_client(alice_pc)?; |
| 384 |
let key_package = |
| 385 |
alice_pc_client.generate_key_package_message(Default::default(), Default::default())?; |
| 386 |
|
| 387 |
let welcome = alice_tablet_group |
| 388 |
.commit_builder() |
| 389 |
.add_member(key_package)? |
| 390 |
.build()? |
| 391 |
.welcome_messages |
| 392 |
.remove(0); |
| 393 |
|
| 394 |
alice_tablet_group.apply_pending_commit()?; |
| 395 |
let (mut alice_pc_group, _) = alice_pc_client.join_group(None, &welcome)?; |
| 396 |
|
| 397 |
// Alice cannot add bob's devices yet |
| 398 |
let bob_tablet_client = make_client(bob_tablet)?; |
| 399 |
let key_package = |
| 400 |
bob_tablet_client.generate_key_package_message(Default::default(), Default::default())?; |
| 401 |
|
| 402 |
let res = alice_tablet_group |
| 403 |
.commit_builder() |
| 404 |
.add_member(key_package.clone())? |
| 405 |
.build(); |
| 406 |
|
| 407 |
assert_matches!(res, Err(MlsError::IdentityProviderError(_))); |
| 408 |
|
| 409 |
// Alice can add bob's user and device |
| 410 |
let add_bob = AddUserProposal { |
| 411 |
new_user: bob.credential, |
| 412 |
}; |
| 413 |
|
| 414 |
let commit = alice_tablet_group |
| 415 |
.commit_builder() |
| 416 |
.custom_proposal(add_bob.to_custom_proposal()?) |
| 417 |
.add_member(key_package)? |
| 418 |
.build()?; |
| 419 |
|
| 420 |
bob_tablet_client.join_group(None, &commit.welcome_messages[0])?; |
| 421 |
alice_tablet_group.apply_pending_commit()?; |
| 422 |
alice_pc_group.process_incoming_message(commit.commit_message)?; |
| 423 |
|
| 424 |
Ok(()) |
| 425 |
} |
| 426 |
|
| 427 |
#[derive(Debug, thiserror::Error)] |
| 428 |
struct CustomError; |
| 429 |
|
| 430 |
impl IntoAnyError for CustomError { |
| 431 |
fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> { |
| 432 |
Ok(Box::new(self)) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
impl Display for CustomError { |
| 437 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 438 |
f.write_str("Custom Error") |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
impl From<MlsError> for CustomError { |
| 443 |
fn from(_: MlsError) -> Self { |
| 444 |
Self |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
impl From<mls_rs_codec::Error> for CustomError { |
| 449 |
fn from(_: mls_rs_codec::Error) -> Self { |
| 450 |
Self |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
impl From<ExtensionError> for CustomError { |
| 455 |
fn from(_: ExtensionError) -> Self { |
| 456 |
Self |
| 457 |
} |
| 458 |
} |