communicate: Signal - pass raw 32-byte identity keys for call key derivation
- Commit
2833c8f80e5caf899d38162f1f7cba9a884993e0- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-23 00:48 UTC (3 months ago)
- Parents
249beaf
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 4d6cca9059..e444ce9bf4 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@@ -1246,7 +1246,9 @@ object SignalClient { val e = e2e ?: return null // RingRTC binds the SRTP key derivation to both identity keys, so a call cannot be // set up before a session with this peer exists.- val remote = e.storedIdentityKey(aci) ?: return null+ // Raw 32-byte keys, matching official's WebRtcUtil.getPublicKeyBytes; the serialized+ // 33-byte form derives different SRTP keys than the peer.+ val remote = e.callIdentityKey(aci) ?: return null val local = e.ownIdentityPublicKey // Logged because a size or encoding mismatch here yields SRTP keys that differ from the // peer's, which looks like a connected call that never progresses.
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 e84177d158..6bef4bfef5 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@@ -51,19 +51,30 @@ class SignalE2E( private val onIdentityChanged: (String, ByteArray) -> Unit = { _, _ -> }, ) { /**- * Our own identity public key, taken from the protocol store rather than from preferences.+ * Our own identity public key in the form RingRTC expects for call key derivation: the **raw 32-byte**+ * key, not the 33-byte serialized form with its `0x05` type prefix. *- * These must be byte-identical to what the peer has for us: RingRTC derives the SRTP keys for a call- * from both identity keys, so a different encoding — raw 32 bytes versus the 33-byte serialized form —- * produces keys that do not match, ICE connects, and every RTP packet is then undecryptable. The store- * is the authoritative copy because it is what messaging already uses successfully.+ * Official passes both identity keys through `ECPublicKey.getPublicKeyBytes()`+ * (`WebRtcUtil.getPublicKeyBytes`) before handing them to `receivedOffer`/`receivedAnswer`. Passing the+ * serialized form instead derives SRTP keys that differ from the peer's, so ICE connects and then every+ * RTP packet — including RingRTC's `Accepted` message — is undecryptable, which looks like a call that+ * connects but never progresses past ringing. */ val ownIdentityPublicKey: ByteArray get() = try {- protocolStore.identityKeyPair.publicKey.serialize()+ // IdentityKeyPair -> IdentityKey -> ECPublicKey -> raw bytes, mirroring official's+ // getAciIdentityKey().getPublicKey().serialize() passed through getPublicKeyBytes().+ protocolStore.identityKeyPair.publicKey.publicKey.publicKeyBytes } catch (_: Throwable) {- b64(auth.identityPublicKey)+ rawPublicKeyBytes(b64(auth.identityPublicKey)) }++ /** Strip the type prefix from a serialized EC public key, as RingRTC's key derivation requires. */+ private fun rawPublicKeyBytes(serialized: ByteArray): ByteArray = try {+ ECPublicKey(serialized).publicKeyBytes+ } catch (_: Throwable) {+ serialized+ } private val ownIdentityPrivate: ByteArray = b64(auth.identityPrivateKey) private val ownAci: String = auth.aci.ifEmpty { auth.phoneNumber } private val ownDeviceId: Int = auth.deviceId@@ -138,6 +149,13 @@ class SignalE2E( } /** The identity key currently recorded for [aci], or null if we have never seen one. */+ /**+ * The peer's identity key for call key derivation — raw 32 bytes, matching [ownIdentityPublicKey].+ * The stored identity is the 33-byte serialized form, so the type prefix has to come off.+ */+ fun callIdentityKey(aci: String): ByteArray? =+ storedIdentityKey(aci)?.let { rawPublicKeyBytes(it) }+ fun storedIdentityKey(aci: String): ByteArray? = try { protocolStore.getIdentity(signalAddress(aci))?.serialize() } catch (_: Exception) { null }