From d7d816daf739deb52bc6736b5f0f0832925ea7d7 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Fri, 9 Feb 2024 20:49:50 +0800 Subject: [PATCH] :bug: Fix preKeyBundle interface (#311) --- .../dto/sync/{ExchangePreKey.kt => DataContent.kt} | 4 ++-- .../clipevery/serializer/PreKeyBundleSerializer.kt | 2 +- .../kotlin/com/clipevery/utils/PreKeyBundleUtils.kt | 2 +- .../kotlin/com/clipevery/dao/signal/SignalRealm.kt | 10 +++++----- .../clipevery/net/clientapi/DesktopSyncClientApi.kt | 13 +++++++------ .../kotlin/com/clipevery/routing/SyncRouting.kt | 13 ++++++------- .../kotlin/com/clipevery/signal/IdentityKeyStore.kt | 6 +++++- 7 files changed, 27 insertions(+), 23 deletions(-) rename composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/{ExchangePreKey.kt => DataContent.kt} (89%) diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/ExchangePreKey.kt b/composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/DataContent.kt similarity index 89% rename from composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/ExchangePreKey.kt rename to composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/DataContent.kt index e4f550f8d..a808cbe72 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/ExchangePreKey.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/DataContent.kt @@ -4,14 +4,14 @@ import com.clipevery.serializer.Base64MimeByteArraySerializer import kotlinx.serialization.Serializable @Serializable -data class ExchangePreKey( +data class DataContent( @Serializable(with = Base64MimeByteArraySerializer::class) val data: ByteArray ) { override fun equals(other: Any?): Boolean { if (this === other) return true if (javaClass != other?.javaClass) return false - other as ExchangePreKey + other as DataContent return data.contentEquals(other.data) } diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/serializer/PreKeyBundleSerializer.kt b/composeApp/src/commonMain/kotlin/com/clipevery/serializer/PreKeyBundleSerializer.kt index e74cdb4d0..bf114cf98 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/serializer/PreKeyBundleSerializer.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/serializer/PreKeyBundleSerializer.kt @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import org.signal.libsignal.protocol.state.PreKeyBundle -object PreKeyBundleSerializer : KSerializer { +object PreKeyBundleSerializer: KSerializer { override val descriptor: SerialDescriptor = buildClassSerialDescriptor("PreKeyBundle") { diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/utils/PreKeyBundleUtils.kt b/composeApp/src/commonMain/kotlin/com/clipevery/utils/PreKeyBundleUtils.kt index 3170382bd..37a4b79cb 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/utils/PreKeyBundleUtils.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/utils/PreKeyBundleUtils.kt @@ -30,7 +30,7 @@ fun decodePreKeyBundle(encoded: ByteArray): PreKeyBundle { val signedPreKeySignatureSize = dataStream.readInt() val signedPreKeySignatureBytes = ByteArray(signedPreKeySignatureSize) - dataStream.read(signedPreKeyPublicBytes) + dataStream.read(signedPreKeySignatureBytes) val identityKeySize = dataStream.readInt() val identityKeyBytes = ByteArray(identityKeySize) diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/signal/SignalRealm.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/signal/SignalRealm.kt index 5c4bd9404..b668f993f 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/dao/signal/SignalRealm.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/dao/signal/SignalRealm.kt @@ -57,11 +57,11 @@ class SignalRealm(private val realm: Realm): SignalDao { override fun saveIdentities(identityKeys: List) { realm.writeBlocking { identityKeys.forEach { identityKey -> - query(ClipIdentityKey::class, "appInstanceId == $0", identityKey.appInstanceId) - .first() - .find()?.let { clipIdentityKey -> - copyToRealm(clipIdentityKey, updatePolicy = UpdatePolicy.ALL) - } + val newClipIdentityKey = ClipIdentityKey().apply { + this.appInstanceId = identityKey.appInstanceId + this.serialized = identityKey.serialized + } + copyToRealm(newClipIdentityKey, updatePolicy = UpdatePolicy.ALL) } } } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/net/clientapi/DesktopSyncClientApi.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/net/clientapi/DesktopSyncClientApi.kt index 60f2481cb..a92ed3fb4 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/net/clientapi/DesktopSyncClientApi.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/net/clientapi/DesktopSyncClientApi.kt @@ -1,7 +1,8 @@ package com.clipevery.net.clientapi -import com.clipevery.dto.sync.ExchangePreKey +import com.clipevery.dto.sync.DataContent import com.clipevery.net.ClipClient +import com.clipevery.utils.decodePreKeyBundle import io.github.oshai.kotlinlogging.KotlinLogging import io.ktor.client.call.body import io.ktor.http.URLBuilder @@ -20,7 +21,7 @@ class DesktopSyncClientApi(private val clipClient: ClipClient): SyncClientApi { if (response.status.value != 200) { return null } - return response.body() + return decodePreKeyBundle(response.body().data) } catch (e: Exception) { logger.error(e) { "getPreKeyBundle error" } } @@ -32,14 +33,14 @@ class DesktopSyncClientApi(private val clipClient: ClipClient): SyncClientApi { try { val ciphertextMessage = sessionCipher.encrypt("exchange".toByteArray(Charsets.UTF_8)) - val exchangePreKey = ExchangePreKey(data = ciphertextMessage.serialize()) + val dataContent = DataContent(data = ciphertextMessage.serialize()) - val response = clipClient.post(exchangePreKey, typeInfo() , urlBuilder = toUrl) + val response = clipClient.post(dataContent, typeInfo() , urlBuilder = toUrl) if (response.status.value != 200) { return false } - val getExchangePreKey = response.body() - val signalMessage = SignalMessage(getExchangePreKey.data) + val getDataContent = response.body() + val signalMessage = SignalMessage(getDataContent.data) val decrypt = sessionCipher.decrypt(signalMessage) return String(decrypt, Charsets.UTF_8) == "exchange" } catch (e: Exception) { diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/routing/SyncRouting.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/routing/SyncRouting.kt index ebcac6f4f..ba6080368 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/routing/SyncRouting.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/routing/SyncRouting.kt @@ -5,7 +5,7 @@ import com.clipevery.app.AppUI import com.clipevery.dao.signal.ClipIdentityKey import com.clipevery.dao.signal.SignalDao import com.clipevery.dao.sync.SyncRuntimeInfoDao -import com.clipevery.dto.sync.ExchangePreKey +import com.clipevery.dto.sync.DataContent import com.clipevery.dto.sync.RequestTrust import com.clipevery.dto.sync.RequestTrustSyncInfo import com.clipevery.dto.sync.SyncInfo @@ -84,7 +84,6 @@ fun Routing.syncRouting() { val signedPreKey = signalDao.generatesSignedPreKeyPair(identityKeyPair.privateKey) val signedPreKeyId = signedPreKey.id val signedPreKeyRecord = SignedPreKeyRecord(signedPreKey.serialized) - signedPreKeyRecord.keyPair.publicKey val signedPreKeySignature = signedPreKeyRecord.signature val preKeyBundle = PreKeyBundle( @@ -95,18 +94,18 @@ fun Routing.syncRouting() { signedPreKeyId, signedPreKeyRecord.keyPair.publicKey, signedPreKeySignature, - signalProtocolStore.identityKeyPair.publicKey + identityKeyPair.publicKey ) val bytes = encodePreKeyBundle(preKeyBundle) - successResponse(call, bytes) + successResponse(call, DataContent(bytes)) } } post("sync/exchangePreKey") { getAppInstanceId(call).let { appInstanceId -> - val exchangePreKey = call.receive(ExchangePreKey::class) - val bytes = exchangePreKey.data + val dataContent = call.receive(DataContent::class) + val bytes = dataContent.data val signalProtocolAddress = SignalProtocolAddress(appInstanceId, 1) val identityKey = signalProtocolStore.getIdentity(signalProtocolAddress) val sessionCipher = SessionCipher(signalProtocolStore, signalProtocolAddress) @@ -137,7 +136,7 @@ fun Routing.syncRouting() { if (Objects.equals("exchange", String(decrypt!!, Charsets.UTF_8))) { val ciphertextMessage = sessionCipher.encrypt("exchange".toByteArray(Charsets.UTF_8)) - successResponse(call, ExchangePreKey(ciphertextMessage.serialize())) + successResponse(call, DataContent(ciphertextMessage.serialize())) } else { failResponse(call, StandardErrorCode.SIGNAL_EXCHANGE_FAIL.toErrorCode()) } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/signal/IdentityKeyStore.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/signal/IdentityKeyStore.kt index 54cc238f5..ad59165e1 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/signal/IdentityKeyStore.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/signal/IdentityKeyStore.kt @@ -45,7 +45,11 @@ class DesktopIdentityKeyStore(private val signalDao: SignalDao, direction: IdentityKeyStore.Direction ): Boolean { val identity: IdentityKey? = getIdentity(address) - return identity?.let { it == identityKey } ?: false + return identity?.let { + val eq = (it == identityKey) + print("isTrustedIdentity: $eq") + return eq + } ?: false } override fun getIdentity(address: SignalProtocolAddress): IdentityKey? {