Modern Apps git

← Modern-Apps.git

communicate: Signal - call zkgroup directly instead of via reflection

Commit
cfb63df13daca472733d2d6ee3c71f07f9d37f1d
Author
vayun-mathur <[email protected]>
When
2026-08-22 04:16 UTC (3 months ago)
Parents
b7b4c65

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 6713a9554a..7ed273a892 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@@ -470,7 +470,7 @@ object SignalClient {      suspend fun createGroup(subject: String, contacts: List<String>): String? {         // GroupsV2 via SignalGroups: GroupMasterKey 32B -> GroupSecretParams, GroupAttributeBlob, PUT /v2/groups/-        val (masterKey, secretParams) = SignalGroups.generateMasterKeyAndSecretParams()+        val (masterKey, _) = SignalGroups.generateMasterKeyAndSecretParams()         val groupId = "group:${SignalGroups.groupIdFromMasterKey(masterKey)}"         val requestBody = SignalGroups.buildCreateGroupRequest(masterKey, subject, contacts, revision = 0)         val auth = authData ?: return null@@ -479,11 +479,7 @@ object SignalClient {         _events.emit(SignalEvent.ConversationUpdate(conversationId = groupId, peerName = subject, peerPhone = null, avatarUrl = null, lastPreview = null, lastTimestamp = System.currentTimeMillis(), unreadCount = 0, isGroup = true, participantCount = contacts.size))         try {             db?.conversationDao()?.upsert(SignalConversation(chatId = groupId, isGroup = true, name = subject, participants = contacts.joinToString(",")))-            if (secretParams != null) {-                Log.i(TAG, "createGroup $groupId with zkgroup GroupSecretParams (live PUT /v2/groups/ ${if (ok) "ok" else "offline"} )")-            } else {-                Log.w(TAG, "createGroup $groupId stub zkgroup (live-only), PUT /v2/groups/ ${if (ok) "ok" else "offline"}")-            }+            Log.i(TAG, "createGroup $groupId (PUT /v2/groups/ ${if (ok) "ok" else "failed"})")         } catch (_: Exception) {}         // Live-only: cache group-send-token per revision via SignalGroups.fetchGroupSendEndorsements (zkgroup GroupSendDerivedKeyPair + GroupSendFullToken.verify)         return groupId
communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalGroups.kt
diff --git a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalGroups.kt b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalGroups.ktindex 95d661eece..7d6957c1c3 100644--- a/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalGroups.kt+++ b/communicate/src/main/java/com/vayunmathur/communicate/data/signal/SignalGroups.kt@@ -5,6 +5,9 @@ import android.util.Log import com.google.protobuf.ByteString import com.vayunmathur.communicate.data.signal.transport.SignalPayload import com.vayunmathur.library.network.NetworkClient+import org.signal.libsignal.zkgroup.groups.ClientZkGroupCipher+import org.signal.libsignal.zkgroup.groups.GroupMasterKey+import org.signal.libsignal.zkgroup.groups.GroupSecretParams import org.whispersystems.signalservice.internal.push.SignalServiceProtos import java.security.SecureRandom import javax.net.ssl.SSLSocketFactory@@ -20,12 +23,10 @@ import javax.net.ssl.SSLSocketFactory  *   Sho("Signal_ZKGroup_20200424_GroupMasterKey...") derive (group_id 32B, blob_key AesKey, UidEncKeyPair, ProfileKeyEncKeyPair)  * - GroupSendDerivedKeyPair, GroupSendEndorsementsResponse, GroupSendEndorsement -> GroupSendFullToken.verify()  *- * zkgroup API gap (live-only): libsignal-android 0.86.5 ships GroupMasterKey/GroupSecretParams/ClientZkGroupCipher- * via the Rust JNI (libsignal_jni.so) but the Java wrappers for GroupsV2 operations (GroupsV2Operations,- * GroupsV2Api, GroupChange.Actions builder) are in Signal-Android, not in libsignal-android. Until those are- * vendored or the `libsignal-net` GroupsV2 client is added, this helper implements the wire-correct request- * structure and encrypts GroupAttributeBlob via ClientZkGroupCipher where available, falling back to a- * documented stub that keeps the build compilable and the PUT /v2/groups/ shape correct for live validation.+ * Still missing (live-only): the GroupsV2 *operations* wrappers (GroupsV2Operations, GroupsV2Api,+ * GroupChange.Actions builder) live in Signal-Android rather than libsignal, so this helper builds the+ * request structure by hand. The zkgroup primitives themselves come from libsignal-client, which+ * arrives transitively with libsignal-android.  */ object SignalGroups { @@ -38,39 +39,14 @@ object SignalGroups {         val masterKey: ByteArray,         val revision: Int,         val groupId: String,-        val groupSecretParamsBytes: ByteArray?,+        val groupSecretParamsBytes: ByteArray,     ) -    /**-     * Generate a fresh 32-byte GroupMasterKey and derive GroupSecretParams.-     * Wire-correct: GroupMasterKey -> GroupSecretParams.derive_from_master_key.-     *-     * Uses reflection to avoid hard compile dependency on zkgroup classes that live in-     * signal-ref libsignal/java/shared but are not published in libsignal-android 0.86.5's-     * classes.jar (aar ships only Rust TLS verifier). Falls back to raw random with a-     * one-line live-only comment if the class is not on the classpath, keeping compilable.-     */-    fun generateMasterKeyAndSecretParams(): Pair<ByteArray, ByteArray?> {+    /** Generate a fresh 32-byte GroupMasterKey and derive its GroupSecretParams. */+    fun generateMasterKeyAndSecretParams(): Pair<ByteArray, ByteArray> {         val masterKey = ByteArray(32).also { SecureRandom().nextBytes(it) }-        val secretParams: ByteArray? = try {-            val gmkClass = Class.forName("org.signal.libsignal.zkgroup.groups.GroupMasterKey")-            val gspClass = Class.forName("org.signal.libsignal.zkgroup.groups.GroupSecretParams")-            val gmk = gmkClass.getConstructor(ByteArray::class.java).newInstance(masterKey)-            val derived = gspClass.getMethod("deriveFromMasterKey", gmkClass).invoke(null, gmk)-            val getContents = derived.javaClass.getMethod("getInternalContentsForJNI")-            getContents.invoke(derived) as ByteArray-        } catch (e: ClassNotFoundException) {-            // Live-only gap: zkgroup GroupsV2 Java wrappers not in libsignal-android 0.86.5 classes.jar;-            // masterKey is still 32B random and PUT /v2/groups/ shape is wire-correct. On live,-            // replace with GroupMasterKey(32B).deriveFromMasterKey -> GroupSecretParams per-            // rust/zkgroup/src/api/groups/group_params.rs:20. Keep compilable.-            Log.w(TAG, "zkgroup not on classpath, using stub GroupSecretParams (live-only): ${e.message}")-            null-        } catch (e: Exception) {-            Log.w(TAG, "GroupSecretParams derive failed (live-only), stub: ${e.message}")-            null-        }-        return masterKey to secretParams+        val secretParams = GroupSecretParams.deriveFromMasterKey(GroupMasterKey(masterKey))+        return masterKey to secretParams.serialize()     }      fun groupIdFromMasterKey(masterKey: ByteArray): String =@@ -84,27 +60,10 @@ object SignalGroups {         return b.build()     } -    /**-     * Encrypt title/description/avatar/timer into GroupAttributeBlob via ClientZkGroupCipher.encryptBlob-     * when available; otherwise return plaintext blob bytes with a live-only comment.-     */-    fun encryptGroupBlob(secretParamsBytes: ByteArray?, plaintext: ByteArray): ByteArray {-        if (secretParamsBytes == null) {-            // Live-only: ClientZkGroupCipher.encryptBlob requires GroupSecretParams on classpath.-            // Keep wire-correct by returning padded plaintext structure; live will encrypt blob.-            return plaintext-        }-        return try {-            val gspClass = Class.forName("org.signal.libsignal.zkgroup.groups.GroupSecretParams")-            val cipherClass = Class.forName("org.signal.libsignal.zkgroup.groups.ClientZkGroupCipher")-            val gsp = gspClass.getConstructor(ByteArray::class.java).newInstance(secretParamsBytes)-            val cipher = cipherClass.getConstructor(gspClass).newInstance(gsp)-            val encryptBlob = cipherClass.getMethod("encryptBlob", ByteArray::class.java)-            encryptBlob.invoke(cipher, plaintext) as ByteArray-        } catch (e: Exception) {-            Log.w(TAG, "encryptBlob failed (live-only), returning plaintext: ${e.message}")-            plaintext-        }+    /** Encrypt a GroupAttributeBlob (title/description/avatar/timer) under the group's secret params. */+    fun encryptGroupBlob(secretParamsBytes: ByteArray, plaintext: ByteArray): ByteArray {+        val cipher = ClientZkGroupCipher(GroupSecretParams(secretParamsBytes))+        return cipher.encryptBlob(plaintext)     }      /**