office: cryptographic read-revocation via owner-signed key rotation (Remove rotates + re-baselines under a new key)
- Commit
8803630e9e1dd193d91a98d9218609a8ff641f58- Author
- Vayun Mathur <[email protected]>
- When
- 2026-07-06 19:07 UTC (5 months ago)
- Parents
ad86e0b
Diff
office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.ktindex 8156133631..87e13644f3 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeSync.kt@@ -141,6 +141,18 @@ object OfficeSync { suspend fun verify(publicBundle: ByteArray, data: ByteArray, signature: ByteArray): Boolean = Pqc.verify(publicBundle, data, signature) + /** Seals [data] to a recipient's public bundle (hybrid PQC) — e.g. a rotated document key. */+ suspend fun seal(bundle: ByteArray, data: ByteArray): ByteArray = Pqc.encryptTo(bundle, data)++ /** Unseals data that was sealed to this device. */+ suspend fun unseal(data: ByteArray): ByteArray = identity.decrypt(data)++ /** Appends opaque (already-encoded) blobs to a channel (no extra encryption). */+ suspend fun appendRaw(channel: String, items: List<String>): Int? = append(channel, items)++ /** Pulls opaque blobs from a channel (no decryption). */+ suspend fun pullRaw(channel: String, since: Int): List<String> = pull(channel, since)?.actions ?: emptyList()+ // --- Live sync + presence over WebSocket (receive live; send presence) --- private const val WS_URL = "wss://findfamily.cc/office/ws"
office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt
diff --git a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.ktindex dcfcefbac5..38aebfc48c 100644--- a/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt+++ b/office/src/main/java/com/vayunmathur/office/util/OfficeViewModel.kt@@ -85,6 +85,16 @@ data class SignedMember(val member: OfficeMember, val sig: String) @Serializable data class SignedTitle(val title: String, val sig: String) +/**+ * An owner-signed key epoch for cryptographic read-revocation. On revoke the owner mints a new+ * content key, seals it to each remaining member (id → PQC-sealed key), and future ops use it. The+ * removed member never receives the new key, so they can't read new content. Op decryption is+ * unchanged (undecryptable ops are simply skipped), so a member who misses a key degrades to stale+ * sync rather than being locked out.+ */+@Serializable+data class KeyEpoch(val epoch: Int, val wraps: Map<String, String>, val sig: String)+ /** An author-signed CRDT op batch: author id, signature over [ops], and the ops JSON. */ @Serializable data class SignedOp(val author: String, val sig: String, val ops: String)@@ -1999,6 +2009,8 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) private var currentCharMode: Boolean = false private var currentRole: String = OfficeRoles.OWNER private var currentOwnerKey: ByteArray? = null+ private var currentEpoch: Int = 0+ private var currentBaseKey: ByteArray? = null // epoch-0 (invite) key, used to read the key-epoch channel context private val currentMembers = java.util.concurrent.ConcurrentHashMap<String, String>() // id -> role private val memberKeyCache = java.util.concurrent.ConcurrentHashMap<String, ByteArray>() // id -> pubkey PEM private val syncJson = Json { ignoreUnknownKeys = true }@@ -2312,6 +2324,8 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) currentRole = OfficeRoles.OWNER currentOwnerKey = OfficeSync.publicBundle currentMembers[OfficeSync.deviceId] = OfficeRoles.OWNER+ currentEpoch = 0+ currentBaseKey = key // Reflect the chosen name in the open editor so its title matches the online doc. if (doc != null) withContext(Dispatchers.Main) { (_state.value as? ViewState.Loaded)?.document?.let { _state.value = ViewState.Loaded(withTitle(it, title)) }@@ -2366,10 +2380,14 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) runCatching { OfficeSync.init(getApplication()) val ds = DataStoreUtils.getInstance(getApplication())- val key = Base64.decode(meta.keyB64)+ val inviteKey = Base64.decode(meta.keyB64) currentRole = meta.role currentOwnerKey = meta.ownerKeyB64.takeIf { it.isNotBlank() }?.let { Base64.decode(it) }+ currentBaseKey = inviteKey currentMembers.clear()+ // Resolve the current content key (honors key rotations / read-revocation).+ val (epoch, key) = fetchCurrentKey(meta.docId, inviteKey, currentOwnerKey)+ currentEpoch = epoch val crdt = loadCrdt(ds, meta.docId) ?: DocumentCrdt(OfficeSync.deviceId) // Refresh the (owner-signed) roster so op role-checks are correct before applying. runCatching { fetchMembers(meta.docId, key, currentOwnerKey) }@@ -2501,11 +2519,67 @@ class OfficeViewModel(application: Application) : AndroidViewModel(application) runCatching { OfficeSync.init(getApplication()) recordMembers(docId, key, listOf(OfficeMember(memberId, "", role)))+ // Revoking read access requires rotating the content key so the removed member can no+ // longer decrypt new content.+ if (role == OfficeRoles.REVOKED) rotateKey(docId) } onResult(true) } } + private fun epochSigningBytes(docId: String, epoch: Int, wraps: Map<String, String>): ByteArray =+ (listOf(docId, epoch.toString()) + wraps.entries.sortedBy { it.key }.map { "${it.key}=${it.value}" }).joinToString("|").encodeToByteArray()++ /**+ * Resolves the current content key: the highest owner-signed [KeyEpoch] this device can unseal,+ * falling back to the epoch-0 (invite) key. Returns (epoch, key).+ */+ private suspend fun fetchCurrentKey(docId: String, inviteKey: ByteArray, ownerKey: ByteArray?): Pair<Int, ByteArray> {+ if (ownerKey == null) return 0 to inviteKey+ var bestEpoch = 0; var bestKey = inviteKey+ for (blob in runCatching { OfficeSync.pullRaw("keys:$docId", 0) }.getOrDefault(emptyList())) {+ val ke = runCatching { syncJson.decodeFromString<KeyEpoch>(Base64.decode(blob).decodeToString()) }.getOrNull() ?: continue+ if (!OfficeSync.verify(ownerKey, epochSigningBytes(docId, ke.epoch, ke.wraps), Base64.decode(ke.sig))) continue+ val myWrap = ke.wraps[OfficeSync.deviceId] ?: continue+ val k = runCatching { OfficeSync.unseal(Base64.decode(myWrap)) }.getOrNull() ?: continue+ if (ke.epoch >= bestEpoch) { bestEpoch = ke.epoch; bestKey = k }+ }+ return bestEpoch to bestKey+ }++ /** Owner mints a new content key sealed to the remaining members and re-baselines the doc under it. */+ private suspend fun rotateKey(docId: String) {+ if (currentRole != OfficeRoles.OWNER) return+ val oldKey = currentDocKey ?: return+ val members = runCatching { fetchMembers(docId, oldKey, currentOwnerKey) }.getOrDefault(emptyList())+ .filter { it.role != OfficeRoles.REVOKED }+ val newEpoch = currentEpoch + 1+ val newKey = OfficeSync.newDocumentKey()+ val wraps = HashMap<String, String>()+ for (m in members) {+ val bundle = memberKey(m.id) ?: continue+ wraps[m.id] = Base64.encode(OfficeSync.seal(bundle, newKey))+ }+ // Always include ourselves so the owner keeps access even if not yet in the roster fetch.+ wraps[OfficeSync.deviceId] = Base64.encode(OfficeSync.seal(OfficeSync.publicBundle, newKey))+ val sig = Base64.encode(OfficeSync.sign(epochSigningBytes(docId, newEpoch, wraps)))+ val record = Base64.encode(syncJson.encodeToString(KeyEpoch(newEpoch, wraps, sig)).encodeToByteArray())+ OfficeSync.appendRaw("keys:$docId", listOf(record))+ // Switch to the new key and re-baseline: push the full current CRDT under the new key so+ // remaining members rebuild from it (their old-key ops still apply; new ops use the new key).+ currentEpoch = newEpoch+ currentDocKey = newKey+ val crdt = currentCrdt+ if (crdt != null) {+ val snapshot = crdt.toState().elements+ val opsJson = syncJson.encodeToString(snapshot)+ val opSig = Base64.encode(OfficeSync.sign(opsJson.encodeToByteArray()))+ OfficeSync.appendDocActions(docId, newKey, listOf(syncJson.encodeToString(SignedOp(OfficeSync.deviceId, opSig, opsJson))))+ }+ // Restart the live loop on the new key.+ startLive(docId, newKey)+ }+ private fun titleSigningBytes(docId: String, title: String): ByteArray = "$docId|title|$title".encodeToByteArray() /** Reads the latest owner-signed document title, if the owner ever renamed it. */
office/src/test/java/com/vayunmathur/office/util/OfficeSharingE2ETest.kt
diff --git a/office/src/test/java/com/vayunmathur/office/util/OfficeSharingE2ETest.kt b/office/src/test/java/com/vayunmathur/office/util/OfficeSharingE2ETest.ktindex f8c8bd460c..14c3797bd3 100644--- a/office/src/test/java/com/vayunmathur/office/util/OfficeSharingE2ETest.kt+++ b/office/src/test/java/com/vayunmathur/office/util/OfficeSharingE2ETest.kt@@ -10,6 +10,7 @@ import com.vayunmathur.library.ui.odf.OdfParagraph import com.vayunmathur.library.ui.odf.OdfSpan import kotlinx.coroutines.runBlocking import kotlinx.serialization.json.Json+import org.junit.Assert.assertArrayEquals import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue@@ -127,6 +128,28 @@ class OfficeSharingE2ETest { ) } + @Test+ fun revoked_member_cannot_read_after_key_rotation() = runBlocking {+ val owner = PqcIdentity.loadOrCreate(MemStore(), "o")+ val editor = PqcIdentity.loadOrCreate(MemStore(), "e")+ val revoked = PqcIdentity.loadOrCreate(MemStore(), "r")+ // Owner rotates: new content key sealed only to the remaining members (owner + editor).+ val newKey = E2ee.newContentKey()+ val wraps = mapOf(+ "owner" to Base64.encode(Pqc.encryptTo(owner.publicBundle, newKey)),+ "editor" to Base64.encode(Pqc.encryptTo(editor.publicBundle, newKey)),+ )+ // The remaining editor recovers the new key; the revoked member has no wrap for it.+ assertArrayEquals(newKey, editor.decrypt(Base64.decode(wraps.getValue("editor"))))+ assertTrue(wraps["revoked"] == null)+ // New content encrypted under the new key is readable by the editor but not by the revoked+ // member (who only ever held the old key).+ val ct = E2ee.aesEncrypt(newKey, "post-revoke secret".encodeToByteArray())+ assertEquals("post-revoke secret", E2ee.aesDecrypt(newKey, ct).decodeToString())+ val oldKey = E2ee.newContentKey()+ assertTrue(runCatching { E2ee.aesDecrypt(oldKey, ct) }.isFailure) // wrong key can't decrypt+ }+ @Test fun invite_maps_to_metadata_with_role_and_owner_key() { val inv = OfficeSync.Invite("d", "k", "Title", charMode = true, role = OfficeRoles.VIEWER, ownerKey = "OWNERKEY")