capsule_core/crypto/membership.rs
1//! The album roster attestation — the one membership fact a **key-free server** can verify.
2//!
3//! # What the server cannot see, and what this gives it instead
4//!
5//! Membership of a shared album is decided inside the MLS group, and every control message
6//! that adds or removes a member is AEAD-protected under a group key the server never holds.
7//! `crypto::authority` can classify a commit chain as behind, ahead or forked from a server's
8//! view of it, but it cannot tell the server *who is in the group*. So the server has no
9//! roster — and without one it cannot answer "may this account read this album's blobs", which
10//! is why the blob route and the album write routes have been owner-only.
11//!
12//! The [`SignedAlbumRoster`] is the album owner's **statement** of the roster, signed by one of
13//! the owner account's devices. The server verifies it against the owner's published
14//! [`DeviceDirectory`] — the same trust anchor, and the same check
15//! [`SignedUpgradeIntent::verify`](crate::crypto::upgrade::SignedUpgradeIntent::verify) runs
16//! for the upgrade ceremony — and then holds it as a *transport control*: who may fetch which
17//! bytes. It is **not** a confidentiality control. A former member who kept the AMK for an
18//! epoch can still decrypt what they already downloaded; what the roster does is stop the
19//! server handing them anything further, exactly as design/federation.md says an unshare cuts
20//! read access to the historical photos at the transport level.
21//!
22//! # A full document, versioned, and the owner is implicit
23//!
24//! The roster is the **whole** member list every time, with a strictly monotonic
25//! [`AlbumRoster::roster_version`] — the same shape as the device directory's
26//! `directory_version` (invariant 23). One monotonic field gives idempotency, replay-safety and
27//! ordering at once, and it needs no per-grant ids and no separate revocation artefact:
28//! removal is *absence* at a higher version. The owner account is never listed, because the
29//! owner's access is the album record's `owner_id` fact and a roster that could omit the owner
30//! would be a roster that could lock the owner out.
31//!
32//! # Why this lives here and not in the server crate
33//!
34//! A client signs it. The DSK that signs a roster is on a device, so the type must be
35//! constructible and signable without the server crate — which is the rule `crypto::upgrade`
36//! records for the upgrade intent, for the same reason: a structure defined at both ends is one
37//! added field away from a signature that stops verifying.
38
39use serde::{Deserialize, Serialize};
40use uuid::Uuid;
41
42use crate::crypto::keys::{AmkVersion, DeviceDirectory, HybridSignature, HybridSigningKey};
43
44/// What went wrong encoding or verifying an album roster.
45#[derive(Debug, thiserror::Error, PartialEq, Eq)]
46pub enum MembershipError {
47 /// The roster could not be canonically encoded for signing.
48 #[error("the album roster could not be encoded: {0}")]
49 Encode(String),
50 /// The signature did not verify, or the attesting device is not a live device of the album
51 /// owner's account.
52 #[error("the album roster's attester signature did not verify: {0}")]
53 Attester(&'static str),
54}
55
56/// What a member may do with the album's contents, as far as the server is concerned.
57///
58/// Two values only. The finer MLS-side distinctions (admin, for one) never reach the server,
59/// which needs exactly this: whether to serve bytes, and whether to accept them.
60#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
61#[serde(rename_all = "snake_case")]
62pub enum MemberRole {
63 /// May read the album's blobs and its sync feed.
64 Reader,
65 /// May read, and may add or change assets under the album owner's namespace.
66 Writer,
67}
68
69/// One member of an album, as the roster names them.
70#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
71pub struct RosterMember {
72 /// The member's account.
73 pub user_id: Uuid,
74 /// What the member may do.
75 pub role: MemberRole,
76}
77
78/// The signed-over content of a roster attestation. Every field is covered by the attesting
79/// device's DSK hybrid signature in the enclosing [`SignedAlbumRoster`].
80#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
81pub struct AlbumRoster {
82 /// The album this roster is for.
83 pub album_id: Uuid,
84 /// Strictly monotonic per album. A server refuses a version at or below the one it holds
85 /// unless the bytes are identical (a replay), so a roster can neither be rolled back nor
86 /// silently replaced.
87 ///
88 /// A server also bounds it **above**, by a small step over the version it holds: the field
89 /// is an ordering, not a count, and a version nothing could ever exceed would freeze the
90 /// album's membership permanently. A client that is refused for it re-signs the same
91 /// document one above the version the refusal names.
92 pub roster_version: u64,
93 /// The AMK epoch the group is at after the commit this roster reflects. Non-decreasing
94 /// across versions; the server records the epoch at which a member was granted and the one
95 /// at which they vanished.
96 pub amk_epoch: AmkVersion,
97 /// The album owner's account. The server anchors on this account's published device
98 /// directory, and refuses a roster whose owner is not the album's.
99 pub attested_by_user: Uuid,
100 /// The owner-account device whose DSK signed this roster. Must be present and **not
101 /// revoked** in the owner's directory.
102 pub attested_by_device: Uuid,
103 /// RFC 3339 time the client produced the roster. Audit-only: the server orders by
104 /// `roster_version`, never by this.
105 pub attested_at: String,
106 /// Everyone other than the owner who may read the album, and what they may do. Absence at a
107 /// higher version *is* removal.
108 pub members: Vec<RosterMember>,
109}
110
111impl AlbumRoster {
112 /// The canonical-CBOR signing bytes the attesting device's DSK covers.
113 ///
114 /// # Errors
115 ///
116 /// Returns [`MembershipError::Encode`] if the roster cannot be canonically encoded.
117 pub fn signing_bytes(&self) -> Result<Vec<u8>, MembershipError> {
118 crate::cbor::to_canonical_vec(self).map_err(|e| MembershipError::Encode(e.to_string()))
119 }
120}
121
122/// An [`AlbumRoster`] plus the attesting device's DSK **hybrid** signature over it.
123#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
124pub struct SignedAlbumRoster {
125 /// The attested roster.
126 pub roster: AlbumRoster,
127 /// The attesting DSK's hybrid signature over [`AlbumRoster::signing_bytes`].
128 pub attester_sig: HybridSignature,
129}
130
131impl SignedAlbumRoster {
132 /// Sign `roster` with the attesting device's DSK.
133 ///
134 /// The caller is responsible for `roster.attested_by_device` naming the device `dsk`
135 /// belongs to; [`verify`](Self::verify) is what checks it, on the other end.
136 ///
137 /// # Errors
138 ///
139 /// Returns [`MembershipError::Encode`] if the roster cannot be canonically encoded.
140 pub fn sign(roster: AlbumRoster, dsk: &HybridSigningKey) -> Result<Self, MembershipError> {
141 let attester_sig = dsk.sign(&roster.signing_bytes()?);
142 Ok(Self {
143 roster,
144 attester_sig,
145 })
146 }
147
148 /// Verify the attester's DSK hybrid signature (Ed25519 **and** ML-DSA) against the album
149 /// owner's published device directory.
150 ///
151 /// Stricter than the upgrade intent's check in one respect: a device the directory has
152 /// **revoked** may not attest a roster, whatever it signed before. The entry is retained so
153 /// that older manifests stay verifiable; it is not a licence to keep issuing new documents.
154 ///
155 /// # Errors
156 ///
157 /// Returns [`MembershipError::Attester`] when the directory names a different account, does
158 /// not hold the attesting device, holds it revoked, or the signature does not verify under
159 /// its DSK.
160 pub fn verify(&self, directory: &DeviceDirectory) -> Result<(), MembershipError> {
161 if directory.core.user_id != self.roster.attested_by_user {
162 return Err(MembershipError::Attester(
163 "the roster's attested_by_user is not this directory's account",
164 ));
165 }
166 let entry =
167 directory
168 .device(&self.roster.attested_by_device)
169 .ok_or(MembershipError::Attester(
170 "the attesting device is not in the directory",
171 ))?;
172 if entry.revoked_at.is_some() {
173 return Err(MembershipError::Attester("the attesting device is revoked"));
174 }
175 if !entry
176 .dsk_public
177 .verify(&self.roster.signing_bytes()?, &self.attester_sig)
178 {
179 return Err(MembershipError::Attester(
180 "the attester's DSK signature does not verify",
181 ));
182 }
183 Ok(())
184 }
185}
186
187#[cfg(test)]
188mod tests;