communicate: Signal - decrypt group state and refresh real membership
- Commit
a06b81cedc52882aeadc995a6849523eff52ae34- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-23 04:57 UTC (3 months ago)
- Parents
b4fcbab
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 db198f2fd1..89486f9e84 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@@ -16,6 +16,7 @@ import com.vayunmathur.communicate.data.signal.call.toContent import com.vayunmathur.communicate.data.signal.call.toRingRtc import com.vayunmathur.communicate.data.signal.transport.SignalAttachmentUpload import com.vayunmathur.communicate.data.signal.transport.SignalCallingApi+import com.vayunmathur.communicate.data.signal.transport.SignalGroupsApi import com.vayunmathur.communicate.data.signal.transport.SignalKeysApi import com.vayunmathur.communicate.data.signal.transport.SignalPayload import com.vayunmathur.communicate.data.signal.transport.SignalSocket@@ -2065,6 +2066,50 @@ object SignalClient { private suspend fun conversationIdFor(serviceId: String): String = contactFor(serviceId)?.phoneE164?.takeIf { it.isNotBlank() } ?: serviceId + /**+ * Refresh a group's membership and title from the server.+ *+ * This is what makes a reply reach the whole group: membership learned from inbound messages only covers+ * whoever has spoken, whereas the server knows everyone. Returns false when the group could not be fetched,+ * leaving the locally-known membership in place rather than emptying it.+ */+ suspend fun refreshGroup(conversationId: String): Boolean {+ val auth = authData ?: return false+ val dao = db?.conversationDao() ?: return false+ val existing = try { dao.getConversation(conversationId) } catch (_: Exception) { null } ?: return false+ val masterKey = existing.groupMasterKey ?: return false+ return try {+ val secretParams = org.signal.libsignal.zkgroup.groups.GroupSecretParams.deriveFromMasterKey(+ org.signal.libsignal.zkgroup.groups.GroupMasterKey(masterKey),+ )+ val credential = SignalGroupsApi+ .fetchCredentials(basicAuthHeader(), signalTls())+ // Credentials come a week at a time; the one for today is the only one the server accepts now.+ .minByOrNull { kotlin.math.abs(it.redemptionTimeSeconds - System.currentTimeMillis() / 1000) }+ ?: return false+ val authorization = SignalGroupsApi.authorizationFor(auth, secretParams, credential) ?: return false+ val group = SignalGroupsApi.fetchGroup(authorization, signalTls()) ?: return false+ val state = SignalGroupsApi.decryptGroup(secretParams, group) ?: return false+ dao.upsert(+ existing.copy(+ isGroup = true,+ name = state.title.ifBlank { existing.name },+ participants = state.memberAcis.joinToString(","),+ groupRevision = state.revision,+ ),+ )+ Log.i(+ TAG,+ "refreshed $conversationId: \"${state.title}\" revision=${state.revision} " ++ "members=${state.memberAcis.size} pending=${state.pendingCount}",+ )+ true+ } catch (t: Throwable) {+ Log.w(TAG, "could not refresh the group $conversationId", t)+ false+ }+ }+ /** * Record a group we received a message from, so we can reply to it. *@@ -2092,6 +2137,9 @@ object SignalClient { participants = members.joinToString(","), ), )+ // Now that the master key is known, ask the server for the real membership. Best-effort: the+ // message is already usable with what we learned from the envelope.+ scope.launch { refreshGroup(conversationId) } } catch (t: Throwable) { Log.w(TAG, "could not record the group behind $conversationId", t) }
communicate/src/main/java/com/vayunmathur/communicate/data/signal/transport/SignalGroupsApi.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/transport/SignalGroupsApi.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/transport/SignalGroupsApi.ktindex aab0111524..b42326acf7 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/transport/SignalGroupsApi.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/transport/SignalGroupsApi.kt@@ -12,8 +12,11 @@ import org.signal.libsignal.protocol.ServiceId import org.signal.libsignal.zkgroup.ServerPublicParams import org.signal.libsignal.zkgroup.auth.AuthCredentialWithPniResponse import org.signal.libsignal.zkgroup.auth.ClientZkAuthOperations+import org.signal.libsignal.zkgroup.groups.ClientZkGroupCipher import org.signal.libsignal.zkgroup.groups.GroupSecretParams+import org.signal.libsignal.zkgroup.groups.UuidCiphertext import org.signal.storageservice.storage.protos.groups.Group+import org.signal.storageservice.storage.protos.groups.GroupAttributeBlob import javax.net.ssl.SSLSocketFactory import kotlin.io.encoding.Base64 import kotlin.io.encoding.ExperimentalEncodingApi@@ -154,6 +157,55 @@ object SignalGroupsApi { } } + /** A group's server state, decrypted. */+ data class GroupState(+ val title: String,+ val revision: Int,+ /** Full membership as ACI strings — what a fan-out send needs. */+ val memberAcis: List<String>,+ /** Invited but not yet joined; they cannot receive group messages yet. */+ val pendingCount: Int,+ )++ /**+ * Decrypt a fetched group.+ *+ * Every identifying field on the wire is encrypted under the group's secret params: members are+ * `UuidCiphertext` and the title is a `GroupAttributeBlob` inside an encrypted blob. That is the whole point+ * of GroupsV2 — the server routes the group without knowing who is in it.+ */+ fun decryptGroup(secretParams: GroupSecretParams, group: Group): GroupState? = try {+ val cipher = ClientZkGroupCipher(secretParams)+ val members = group.membersList.mapNotNull { member ->+ try {+ // A member carrying a presentation has not been reduced to a plain userId yet; skip rather than+ // guess, since its ACI is inside the presentation.+ if (member.presentation != null && member.presentation.size() > 0) return@mapNotNull null+ val serviceId = cipher.decrypt(UuidCiphertext(member.userId.toByteArray()))+ (serviceId as? ServiceId.Aci)?.toString()+ } catch (t: Throwable) {+ Log.i(TAG, "skipping an undecryptable group member: ${t.message}")+ null+ }+ }+ val title = try {+ val blobBytes = cipher.decryptBlob(group.title.toByteArray())+ GroupAttributeBlob.parseFrom(blobBytes).title.trim()+ } catch (t: Throwable) {+ Log.i(TAG, "could not decrypt the group title: ${t.message}")+ ""+ }+ GroupState(+ title = title,+ revision = group.version,+ memberAcis = members,+ pendingCount = group.membersPendingProfileKeyCount,+ )+ } catch (t: Throwable) {+ Log.w(TAG, "could not decrypt the group", t)+ null+ }+ /** Signal's zkgroup server public params, needed to verify credentials it issues. */ private fun serverPublicParams(): ByteArray = Base64.Default.decode(SERVER_PUBLIC_PARAMS_B64)