Skip to content

Commit

Permalink
🐛 Fix preKeyBundle interface (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Feb 9, 2024
1 parent ef67c9a commit d7d816d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import org.signal.libsignal.protocol.state.PreKeyBundle

object PreKeyBundleSerializer : KSerializer<PreKeyBundle> {
object PreKeyBundleSerializer: KSerializer<PreKeyBundle> {

override val descriptor: SerialDescriptor = buildClassSerialDescriptor("PreKeyBundle") {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ class SignalRealm(private val realm: Realm): SignalDao {
override fun saveIdentities(identityKeys: List<ClipIdentityKey>) {
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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,7 +21,7 @@ class DesktopSyncClientApi(private val clipClient: ClipClient): SyncClientApi {
if (response.status.value != 200) {
return null
}
return response.body<PreKeyBundle>()
return decodePreKeyBundle(response.body<DataContent>().data)
} catch (e: Exception) {
logger.error(e) { "getPreKeyBundle error" }
}
Expand All @@ -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<ExchangePreKey>() , urlBuilder = toUrl)
val response = clipClient.post(dataContent, typeInfo<DataContent>() , urlBuilder = toUrl)
if (response.status.value != 200) {
return false
}
val getExchangePreKey = response.body<ExchangePreKey>()
val signalMessage = SignalMessage(getExchangePreKey.data)
val getDataContent = response.body<DataContent>()
val signalMessage = SignalMessage(getDataContent.data)
val decrypt = sessionCipher.decrypt(signalMessage)
return String(decrypt, Charsets.UTF_8) == "exchange"
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down

0 comments on commit d7d816d

Please sign in to comment.