Modern Apps git

← Modern-Apps.git

communicate: Signal - address retry receipts with the real registration id

Commit
6d30795f6566868074e9a91300927696a6298623
Author
vayun-mathur <[email protected]>
When
2026-08-22 21:41 UTC (3 months ago)
Parents
1df3beb

Diff

communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalClient.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalClient.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalClient.ktindex c5f8016b99..028744c31b 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalClient.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalClient.kt@@ -1678,12 +1678,20 @@ object SignalClient {                 env.sourceDevice,             )             val body = PlaintextContent(errorMessage).serialize()-            // Their next message builds a new session, so drop ours rather than keep a chain they abandoned.-            e.archiveSession(sender, env.sourceDevice)+            // The server rejects a send whose destinationRegistrationId does not match the recipient's, so+            // this must be their real one. A pre-key message carries it; otherwise fall back to the session.+            // Deliberately read before anything touches the session, and the session is not archived here —+            // the receipt asks *them* to archive theirs.+            val registrationId = e.senderRegistrationId(env.content)+                ?: e.remoteRegistrationId(sender, env.sourceDevice)+            if (registrationId == null) {+                Log.w(TAG, "no registration id for $sender:${env.sourceDevice}, cannot send a retry receipt")+                return+            }             val message = SignalPayload.OutgoingPushMessage(                 type = SignalServiceProtos.Envelope.Type.PLAINTEXT_CONTENT.number,                 destinationDeviceId = env.sourceDevice,-                destinationRegistrationId = 0,+                destinationRegistrationId = registrationId,                 content = body,             )             val json = SignalPayload.buildPutMessagesBody(
communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/SignalE2E.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/SignalE2E.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/SignalE2E.ktindex dae3b8ca1e..871ee2af47 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/SignalE2E.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/e2e/SignalE2E.kt@@ -342,6 +342,23 @@ class SignalE2E(         return PreKeyUpload(signedPreKey = signed, lastResortKyber = kyber, oneTimeEcPreKeys = oneTime)     } +    /**+     * The registration id to address [aci]'s device with, from our session with them. A send whose+     * `destinationRegistrationId` does not match what the server holds is rejected as a stale device.+     */+    fun remoteRegistrationId(aci: String, deviceId: Int): Int? = try {+        protocolStore.loadSession(signalAddress(aci, deviceId)).remoteRegistrationId+    } catch (_: Throwable) {+        null+    }++    /** The sender's registration id, carried on every pre-key message. */+    fun senderRegistrationId(ciphertext: ByteArray): Int? = try {+        PreKeySignalMessage(ciphertext).registrationId+    } catch (_: Throwable) {+        null+    }+     /** Whether our store holds a signed pre-key [id] whose public half is exactly [publicKey]. */     fun hasSignedPreKeyMatching(id: Int, publicKey: ByteArray?): Boolean {         if (publicKey == null) return true