-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implementing signal protocol for desktop device
- Loading branch information
1 parent
ef2656b
commit 52005ac
Showing
30 changed files
with
668 additions
and
175 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
composeApp/src/commonMain/kotlin/com/clipevery/dao/signal/SignalDao.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.clipevery.dao.signal | ||
|
||
import org.signal.libsignal.protocol.ecc.ECPrivateKey | ||
|
||
interface SignalDao { | ||
fun generatePreKeyPair(): ClipPreKey | ||
fun generatesSignedPreKeyPair(privateKey: ECPrivateKey): ClipSignedPreKey | ||
fun saveIdentities(identityKeys: List<ClipIdentityKey>) | ||
fun saveIdentity(appInstanceId: String, serialized: ByteArray): Boolean | ||
fun identity(appInstanceId: String): ByteArray? | ||
fun loadPreKey(id: Int): ByteArray? | ||
fun storePreKey(id: Int, serialized: ByteArray) | ||
fun removePreKey(id: Int) | ||
fun loadSession(appInstanceId: String): ByteArray? | ||
fun loadExistingSessions(): List<ByteArray> | ||
fun storeSession(appInstanceId: String, sessionRecord: ByteArray) | ||
fun containSession(appInstanceId: String): Boolean | ||
fun deleteSession(appInstanceId: String) | ||
fun deleteAllSession() | ||
fun loadSignedPreKey(id: Int): ByteArray? | ||
fun loadSignedPreKeys(): List<ByteArray> | ||
fun storeSignedPreKey(id: Int, serialized: ByteArray) | ||
fun containsSignedPreKey(id: Int): Boolean | ||
fun removeSignedPreKey(id: Int) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/RequestSyncInfo.kt
This file was deleted.
Oops, something went wrong.
29 changes: 7 additions & 22 deletions
29
composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/RequestTrust.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,12 @@ | ||
package com.clipevery.dto.sync | ||
|
||
import com.clipevery.serializer.IdentityKeySerializer | ||
import kotlinx.serialization.Serializable | ||
import org.signal.libsignal.protocol.IdentityKey | ||
import java.io.ByteArrayInputStream | ||
import java.io.ByteArrayOutputStream | ||
import java.io.DataInputStream | ||
import java.io.DataOutputStream | ||
|
||
data class RequestTrust(val identityKey: IdentityKey, val token: Int) | ||
@Serializable | ||
data class RequestTrust( | ||
@Serializable(with = IdentityKeySerializer::class) val identityKey: IdentityKey, | ||
val token: Int | ||
) | ||
|
||
fun decodeRequestTrust(encoded: ByteArray): RequestTrust { | ||
val byteStream = ByteArrayInputStream(encoded) | ||
val dataStream = DataInputStream(byteStream) | ||
val token = dataStream.readInt() | ||
val serialize = dataStream.readNBytes(encoded.size - 4) | ||
val identityKey = IdentityKey(serialize) | ||
return RequestTrust(identityKey, token) | ||
} | ||
|
||
fun encodeRequestTrust(requestTrust: RequestTrust): ByteArray { | ||
val byteStream = ByteArrayOutputStream() | ||
val dataStream = DataOutputStream(byteStream) | ||
dataStream.writeInt(requestTrust.token) | ||
val identityKeyBytes = requestTrust.identityKey.serialize() | ||
dataStream.write(identityKeyBytes) | ||
return byteStream.toByteArray() | ||
} |
12 changes: 12 additions & 0 deletions
12
composeApp/src/commonMain/kotlin/com/clipevery/dto/sync/RequestTrustSyncInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.clipevery.dto.sync | ||
|
||
import com.clipevery.serializer.IdentityKeySerializer | ||
import kotlinx.serialization.Serializable | ||
import org.signal.libsignal.protocol.IdentityKey | ||
|
||
@Serializable | ||
data class RequestTrustSyncInfo( | ||
@Serializable(with = IdentityKeySerializer::class) val identityKey: IdentityKey, | ||
val syncInfo: SyncInfo | ||
) | ||
|
35 changes: 35 additions & 0 deletions
35
composeApp/src/commonMain/kotlin/com/clipevery/net/ClientHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.clipevery.net | ||
|
||
import com.clipevery.dao.sync.HostInfo | ||
import com.clipevery.net.clientapi.SyncClientApi | ||
import org.signal.libsignal.protocol.SessionBuilder | ||
import org.signal.libsignal.protocol.SessionCipher | ||
|
||
interface ClientHandler { | ||
|
||
fun getId(): String | ||
|
||
fun getSyncClientApi(): SyncClientApi | ||
|
||
fun createSessionBuilder(): SessionBuilder | ||
|
||
fun getSessionCipher(): SessionCipher | ||
|
||
fun getHostInfo(): HostInfo? | ||
|
||
fun port(): Int | ||
|
||
fun isConnected(): Boolean | ||
|
||
suspend fun updateSyncStateWithHostInfo(syncState: Int, hostInfo: HostInfo, port: Int) | ||
|
||
suspend fun updateSyncState(syncState: Int) | ||
|
||
suspend fun checkHandler(checkAction: CheckAction): Boolean | ||
} | ||
|
||
|
||
enum class CheckAction { | ||
CheckAll, | ||
CheckNonConnected | ||
} |
10 changes: 10 additions & 0 deletions
10
composeApp/src/commonMain/kotlin/com/clipevery/net/ClientHandlerManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.clipevery.net | ||
|
||
interface ClientHandlerManager { | ||
fun start() | ||
fun addHandler(id: String) | ||
fun removeHandler(id: String) | ||
fun stop() | ||
suspend fun checkConnects(checkAction: CheckAction) | ||
suspend fun checkConnect(id: String, checkAction: CheckAction): Boolean | ||
} |
11 changes: 11 additions & 0 deletions
11
composeApp/src/commonMain/kotlin/com/clipevery/net/ClipClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
package com.clipevery.net | ||
|
||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.http.URLBuilder | ||
|
||
interface ClipClient { | ||
|
||
suspend fun post( | ||
urlBuilder: URLBuilder.(URLBuilder) -> Unit, | ||
message: ByteArray, | ||
): HttpResponse | ||
|
||
suspend fun get( | ||
urlBuilder: URLBuilder.(URLBuilder) -> Unit | ||
): HttpResponse | ||
} |
10 changes: 10 additions & 0 deletions
10
composeApp/src/commonMain/kotlin/com/clipevery/net/ConnectState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.clipevery.net | ||
|
||
import com.clipevery.dao.sync.SyncRuntimeInfo | ||
|
||
interface ConnectState { | ||
|
||
suspend fun autoResolve(syncRuntimeInfo: SyncRuntimeInfo) | ||
|
||
suspend fun next(): Boolean | ||
} |
5 changes: 5 additions & 0 deletions
5
composeApp/src/commonMain/kotlin/com/clipevery/net/DeviceRefresher.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.clipevery.net | ||
|
||
interface DeviceRefresher { | ||
suspend fun refresh(checkAction: CheckAction) | ||
} |
15 changes: 15 additions & 0 deletions
15
composeApp/src/commonMain/kotlin/com/clipevery/net/clientapi/SyncClientApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.clipevery.net.clientapi | ||
|
||
import io.ktor.http.URLBuilder | ||
import org.signal.libsignal.protocol.SessionCipher | ||
import org.signal.libsignal.protocol.state.PreKeyBundle | ||
|
||
interface SyncClientApi { | ||
|
||
suspend fun getPreKeyBundle(toUrl: URLBuilder.(URLBuilder) -> Unit): PreKeyBundle? | ||
|
||
suspend fun exchangePreKey( | ||
sessionCipher: SessionCipher, | ||
toUrl: URLBuilder.(URLBuilder) -> Unit | ||
): Boolean | ||
} |
23 changes: 23 additions & 0 deletions
23
composeApp/src/commonMain/kotlin/com/clipevery/serializer/IdentityKeySerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.clipevery.serializer | ||
|
||
import com.clipevery.utils.base64Decode | ||
import com.clipevery.utils.base64Encode | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import org.signal.libsignal.protocol.IdentityKey | ||
|
||
object IdentityKeySerializer: KSerializer<IdentityKey> { | ||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("IdentityKey") {} | ||
|
||
override fun deserialize(decoder: Decoder): IdentityKey { | ||
val byteArray = base64Decode(decoder.decodeString()) | ||
return IdentityKey(byteArray) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: IdentityKey) { | ||
encoder.encodeString(base64Encode(value.serialize())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.