e0404bda3dae

bind relayed revocations to the issuer's known signing keys; repair stale adapter fixtures in the epoch and adoption suites (2 cross-client KAT tests still await the canonical alignment)

time
2026-09-10 17:29 UTC · 2 months ago
machine
koh2
agent
deepseek/deepseek-v4-flash
session
cross-client remediation
parent
ed8f5b545f44
7 files VERSION+1 −1app/src/main/java/com/peachpasswords/sync/DeviceManifestSupport.kt+24 −7app/src/main/java/com/peachpasswords/sync/VaultSyncAdapter.kt+11 −1app/src/test/java/com/peachpasswords/sync/DeviceManifestSupportTest.kt+60 −10app/src/test/java/com/peachpasswords/sync/RelayAdoptionReceiverTest.kt+1 −0app/src/test/java/com/peachpasswords/sync/VaultSyncAdapterTest.kt+2 −1app/src/test/java/com/peachpasswords/sync/VaultSyncManifestEpochRaceTest.kt+7 −1
VERSION+1 −1
@@ -1 +1 @@1-2.17.221+2.17.23

view file

app/src/main/java/com/peachpasswords/sync/DeviceManifestSupport.kt+24 −7
@@ -158,9 +158,27 @@158158     )159159 }160160 161+/**162+ * A relayed revocation counts only when the key it names is one the receiver163+ * already trusts for `revokedByDeviceId` — that device's current signing key or164+ * an entry in its immutable known-key history. An unresolvable issuer fails165+ * closed: the manifest is rejected rather than the record being skipped, so a166+ * dropped history entry can never silently un-revoke a device.167+ */168+private fun isAuthorisedRevocation(169+    record: DeviceRevocation,170+    resolveIssuerSigningKeys: (String) -> Set<String>,171+): Boolean {172+    val statedKey = record.issuerSigningKeyHex.lowercase()173+    val trusted = resolveIssuerSigningKeys(record.revokedByDeviceId).map { it.lowercase() }174+    if (statedKey !in trusted) return false175+    return verifyDeviceRevocation(record, hexToPubkey(statedKey))176+}177+161178 fun verifyDeviceManifest(162179     manifest: SignedDeviceManifest,163180     issuerSigningPublicKey: ByteArray,181+    resolveIssuerSigningKeys: (String) -> Set<String>,164182     nowMs: Long = System.currentTimeMillis(),165183     freshnessMs: Long = DEVICE_MANIFEST_FRESHNESS_MS,166184 ): DeviceManifestBody? {@@ -185,13 +203,12 @@185203     val canonical = canonicalDeviceManifestBody(body).toByteArray(Charsets.UTF_8)186204     if (!verifyEd25519(issuerSigningPublicKey, canonical, signature)) return null187205     // Forwarded tombstones retain the original issuer's immutable signature.188-    // The enclosing manifest signature authenticates the forwarder; the189-    // embedded issuer key authenticates the revocation itself.190-    if (!body.revoked.all { record ->191-            val recordKey = runCatching { hexToPubkey(record.issuerSigningKeyHex) }.getOrNull()192-                ?: return@all false193-            verifyDeviceRevocation(record, recordKey)194-        }) return null206+    // The enclosing manifest signature authenticates the forwarder only, so the207+    // revocation's own issuer key MUST be one the receiver already knows for208+    // `revokedByDeviceId` — never the key carried inside the record. Trusting209+    // the record's own claim would let any admitted peer that can issue a210+    // manifest eject any third device.211+    if (!body.revoked.all { record -> isAuthorisedRevocation(record, resolveIssuerSigningKeys) }) return null195212     // Return entries with the display name capped so every consumer (registry196213     // upserts, pending-vouch queue, key-rotation alerts) stores the bounded197214     // name. The signature above was verified against the raw body.

view file

app/src/main/java/com/peachpasswords/sync/VaultSyncAdapter.kt+11 −1
@@ -163,9 +163,19 @@163163         val knownIssuerKeys = registry.issuerSigningKeys(issuer.deviceId) + currentSigningKey164164         // Historical keys and the persisted current key remain valid; mutable165165         // device metadata never rewrites the issuer's key history.166+        //167+        // A revocation may name any device as its issuer — including one whose168+        // active row is gone — so resolve that issuer's key from the registry169+        // and its immutable history rather than from the record itself.170+        val resolveIssuerSigningKeys: (String) -> Set<String> = { deviceId ->171+            val history = registry.issuerSigningKeys(deviceId)172+            val current = registry.get(deviceId)?.signingPublicKeyHex?.lowercase()173+            if (current != null && current.length == 64) history + current else history174+        }166175         val verified = knownIssuerKeys.firstNotNullOfOrNull { candidate ->167176             runCatching {168-                verifyDeviceManifest(manifest, hexToPubkey(candidate))?.let { candidate to it }177+                verifyDeviceManifest(manifest, hexToPubkey(candidate), resolveIssuerSigningKeys)178+                    ?.let { candidate to it }169179             }.getOrNull()170180         } ?: return171181         val (signingKey, body) = verified

view file

app/src/test/java/com/peachpasswords/sync/DeviceManifestSupportTest.kt+60 −10
@@ -13,6 +13,13 @@1313     private val master = ByteArray(32) { it.toByte() }1414     private val signing = deriveSyncSigningKeypair(master, "issuer")1515 16+    /**17+     * Known signing keys per issuer id. Test revocations are issued by the18+     * fixture key, so any issuer id resolves to it; a record naming a different19+     * key must not be authorised.20+     */21+    private val knownIssuerKeys: (String) -> Set<String> = { setOf(pubkeyToHex(signing.publicKeyRaw)) }22+1623     private fun validEntry(deviceId: String = "peer", pairedAt: Long = 1L) = ManifestDeviceEntry(1724         deviceId = deviceId,1825         deviceName = "Peer",@@ -34,25 +41,25 @@3441         // abs(Long.MIN_VALUE) is still negative, so the legacy abs()-based gate3542         // accepted this. The new two-sided bound must reject it.3643         val manifest = manifestWith(issuedAt = Long.MIN_VALUE)37-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_700_000_000_000L))44+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_700_000_000_000L))3845     }3946 4047     @Test fun rejects_issued_at_too_old() {4148         val manifest = manifestWith(issuedAt = 1L)42-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_700_000_000_000L))49+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_700_000_000_000L))4350     }4451 4552     @Test fun rejects_issued_at_too_far_in_future() {4653         val now = 1_700_000_000_000L4754         // 5 min + 1 ms past the default freshness window.4855         val manifest = manifestWith(issuedAt = now + 5 * 60_000L + 1L)49-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = now))56+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = now))5057     }5158 5259     @Test fun accepts_issued_at_within_window() {5360         val now = 1_700_000_000_000L5461         val manifest = manifestWith(issuedAt = now)55-        assertNotNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = now))62+        assertNotNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = now))5663     }5764 5865     @Test fun rejects_oversized_device_id_string() {@@ -60,7 +67,7 @@6067             issuedAt = 1_000L,6168             devices = listOf(validEntry(deviceId = "x".repeat(10_000))),6269         )63-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_000L))70+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))6471     }6572 6673     @Test fun caps_oversized_device_name_at_verify() {@@ -82,6 +89,7 @@8289         val verified = verifyDeviceManifest(8390             SignedDeviceManifest(body, pubkeyToHex(signature)),8491             signing.publicKeyRaw,92+            knownIssuerKeys,8593             nowMs = now,8694         )8795         assertNotNull(verified)@@ -101,7 +109,7 @@101109             issuedAt = 1_000L,102110             devices = listOf(validEntry(pairedAt = -1L)),103111         )104-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_000L))112+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))105113     }106114 107115     @Test fun rejects_too_many_devices() {@@ -111,13 +119,13 @@111119             signingPrivateKey = signing.privateKey,112120             nowMs = 1_000L,113121         )114-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_000L))122+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))115123     }116124 117125     @Test fun rejects_oversized_issuer_device_id() {118126         val manifest = manifestWith(issuedAt = 1_000L)119127         val oversized = manifest.copy(body = manifest.body.copy(issuerDeviceId = "y".repeat(2_000)))120-        assertNull(verifyDeviceManifest(oversized, signing.publicKeyRaw, nowMs = 1_000L))128+        assertNull(verifyDeviceManifest(oversized, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))121129     }122130 123131     @Test fun rejects_negative_revoked_at() {@@ -135,7 +143,7 @@135143                 ),136144             ),137145         )138-        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, nowMs = 1_000L))146+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))139147     }140148     @Test fun revocation_signature_binds_immutable_fields() {141149         val record = signDeviceRevocation(@@ -163,6 +171,48 @@163171             revoked = listOf(original),164172         )165173         assertEquals(original, forwarded.body.revoked.single())166-        assertNotNull(verifyDeviceManifest(forwarded, forwarder.publicKeyRaw, nowMs = 1_000L))174+        assertNotNull(verifyDeviceManifest(forwarded, forwarder.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L))175+    }176+177+    @Test fun rejects_a_revocation_signed_by_an_untrusted_issuer_key() {178+        // The record is internally consistent — it carries its own key and a179+        // signature that verifies under it — which is exactly why trusting that180+        // key let any admitted peer eject any device. Only a key the receiver181+        // already knows for `revokedByDeviceId` may authorise a revocation.182+        val attacker = deriveSyncSigningKeypair(ByteArray(32) { (it + 9).toByte() }, "attacker")183+        val forged = signDeviceRevocation(184+            DeviceRevocation("victim", "aa".repeat(32), 42L, "attacker"),185+            attacker.privateKey,186+        )187+        assertEquals(true, verifyDeviceRevocation(forged, attacker.publicKeyRaw))188+189+        val manifest = buildSignedDeviceManifest(190+            issuerDeviceId = "issuer",191+            devices = emptyList(),192+            signingPrivateKey = signing.privateKey,193+            nowMs = 1_000L,194+            revoked = listOf(forged),195+        )196+        assertNull(197+            verifyDeviceManifest(manifest, signing.publicKeyRaw, knownIssuerKeys, nowMs = 1_000L),198+        )199+    }200+201+    @Test fun rejects_a_revocation_when_the_issuer_key_cannot_be_resolved() {202+        val record = signDeviceRevocation(203+            DeviceRevocation("peer", "aa".repeat(32), 42L, "issuer"),204+            signing.privateKey,205+        )206+        val manifest = buildSignedDeviceManifest(207+            issuerDeviceId = "issuer",208+            devices = emptyList(),209+            signingPrivateKey = signing.privateKey,210+            nowMs = 1_000L,211+            revoked = listOf(record),212+        )213+        // An empty history must fail closed: the manifest is rejected rather214+        // than the record being quietly skipped, so a pruned history entry can215+        // never silently un-revoke a device.216+        assertNull(verifyDeviceManifest(manifest, signing.publicKeyRaw, { emptySet() }, nowMs = 1_000L))167217     }168218 }

view file

app/src/test/java/com/peachpasswords/sync/RelayAdoptionReceiverTest.kt+1 −0
@@ -470,6 +470,7 @@470470                         ourDeviceId = "receiver-id",471471                         pairedDevices = receiverRegistryStore,472472                         pendingVouched = receiverPendingStore,473+                        localCredentials = randomLocalIdentityCredentials("receiver-id"),473474                     )474475                     adapter.applyIncomingDeviceManifest(manifest, authenticatedRemoteStaticPublicKey)475476                 }

view file

app/src/test/java/com/peachpasswords/sync/VaultSyncAdapterTest.kt+2 −1
@@ -176,12 +176,13 @@176176         val verified = verifyDeviceManifest(177177             manifest,178178             signing.publicKeyRaw,179+            { setOf(pubkeyToHex(signing.publicKeyRaw)) },179180             nowMs = 1_000L,180181         )181182 182183         assertEquals("extension-issuer", verified?.issuerDeviceId)183184         assertEquals("android-peer", verified?.devices?.single()?.deviceId)184-        assertTrue(verifyDeviceManifest(manifest.copy(signatureHex = "00".repeat(64)), signing.publicKeyRaw, nowMs = 1_000L) == null)185+        assertTrue(verifyDeviceManifest(manifest.copy(signatureHex = "00".repeat(64)), signing.publicKeyRaw, { setOf(pubkeyToHex(signing.publicKeyRaw)) }, nowMs = 1_000L) == null)185186     }186187 187188     @Test fun `vouched peer revocation evicts the device and permanently blocks re-vouching`() = runBlocking {

view file

app/src/test/java/com/peachpasswords/sync/VaultSyncManifestEpochRaceTest.kt+7 −1
@@ -95,7 +95,13 @@9595                 ),9696             ),9797         )98-        val adapter = VaultSyncAdapter(repo, "android-self", registry, Pending())98+        val adapter = VaultSyncAdapter(99+            repo,100+            "android-self",101+            registry,102+            Pending(),103+            localCredentials = randomLocalIdentityCredentials("android-self"),104+        )99105         val manifest = buildSignedDeviceManifest(100106             issuerDeviceId = "issuer",101107             devices = listOf(

view file