From ef2380da76fd8ce0aa763e85df6b9c637c831f41 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Mon, 25 Jan 2021 16:58:04 +1100 Subject: [PATCH] make things work --- .../loki/utilities/KeyPairUtilities.kt | 8 +++--- .../libsession/snode/OnionRequestAPI.kt | 28 ++++++++++--------- .../service/loki/crypto/LokiServiceCipher.kt | 16 +---------- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt index 37886fe21a..2000102a05 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/KeyPairUtilities.kt @@ -46,10 +46,10 @@ object KeyPairUtilities { } fun getUserED25519KeyPair(context: Context): KeyPair? { - val hexEncodedED25519PublicKey = IdentityKeyUtil.retrieve(context, IdentityKeyUtil.ED25519_PUBLIC_KEY) ?: return null - val hexEncodedED25519SecretKey = IdentityKeyUtil.retrieve(context, IdentityKeyUtil.ED25519_SECRET_KEY) ?: return null - val ed25519PublicKey = Key.fromBase64String(hexEncodedED25519PublicKey) - val ed25519SecretKey = Key.fromBase64String(hexEncodedED25519SecretKey) + val base64EncodedED25519PublicKey = IdentityKeyUtil.retrieve(context, IdentityKeyUtil.ED25519_PUBLIC_KEY) ?: return null + val base64EncodedED25519SecretKey = IdentityKeyUtil.retrieve(context, IdentityKeyUtil.ED25519_SECRET_KEY) ?: return null + val ed25519PublicKey = Key.fromBytes(Base64.decode(base64EncodedED25519PublicKey)) + val ed25519SecretKey = Key.fromBytes(Base64.decode(base64EncodedED25519SecretKey)) return KeyPair(ed25519PublicKey, ed25519SecretKey) } diff --git a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt index 4d12bde107..b2673f0311 100644 --- a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt @@ -23,11 +23,11 @@ private typealias Path = List /** * See the "Onion Requests" section of [The Session Whitepaper](https://arxiv.org/pdf/2002.04609.pdf) for more information. */ -public object OnionRequestAPI { +object OnionRequestAPI { private val pathFailureCount = mutableMapOf() private val snodeFailureCount = mutableMapOf() - public var guardSnodes = setOf() - public var paths: List // Not a set to ensure we consistently show the same path to the user + var guardSnodes = setOf() + var paths: List // Not a set to ensure we consistently show the same path to the user get() = SnodeAPI.database.getOnionRequestPaths() set(newValue) { if (newValue.isEmpty()) { @@ -41,19 +41,19 @@ public object OnionRequestAPI { /** * The number of snodes (including the guard snode) in a path. */ - private val pathSize = 3 + private const val pathSize = 3 /** * The number of times a path can fail before it's replaced. */ - private val pathFailureThreshold = 2 + private const val pathFailureThreshold = 1 /** * The number of times a snode can fail before it's replaced. */ - private val snodeFailureThreshold = 2 + private const val snodeFailureThreshold = 1 /** * The number of paths to maintain. */ - public val targetPathCount = 2 // A main path and a backup path for the case where the target snode is in the main path + const val targetPathCount = 2 // A main path and a backup path for the case where the target snode is in the main path /** * The number of guard snodes required to maintain `targetPathCount` paths. @@ -67,9 +67,9 @@ public object OnionRequestAPI { class InsufficientSnodesException : Exception("Couldn't find enough snodes to build a path.") private data class OnionBuildingResult( - internal val guardSnode: Snode, - internal val finalEncryptionResult: EncryptionResult, - internal val destinationSymmetricKey: ByteArray + val guardSnode: Snode, + val finalEncryptionResult: EncryptionResult, + val destinationSymmetricKey: ByteArray ) internal sealed class Destination { @@ -406,9 +406,11 @@ public object OnionRequestAPI { } else { handleUnspecificError() } + } else if (destination is Destination.Server && exception.statusCode == 400) { + Log.d("Loki","Destination server returned ${exception.statusCode}") } else if (message == "Loki Server error") { - // Do nothing - } else { + Log.d("Loki", "message was $message") + } else { // Only drop snode/path if not receiving above two exception cases handleUnspecificError() } } @@ -438,7 +440,7 @@ public object OnionRequestAPI { * * `publicKey` is the hex encoded public key of the user the call is associated with. This is needed for swarm cache maintenance. */ - public fun sendOnionRequest(request: Request, server: String, x25519PublicKey: String, target: String = "/loki/v3/lsrpc", isJSONRequired: Boolean = true): Promise, Exception> { + fun sendOnionRequest(request: Request, server: String, x25519PublicKey: String, target: String = "/loki/v3/lsrpc", isJSONRequired: Boolean = true): Promise, Exception> { val headers = request.getHeadersForOnionRequest() val url = request.url() val urlAsString = url.toString() diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/crypto/LokiServiceCipher.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/crypto/LokiServiceCipher.kt index d4bac87b62..43c6810bfe 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/crypto/LokiServiceCipher.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/crypto/LokiServiceCipher.kt @@ -19,21 +19,7 @@ class LokiServiceCipher(localAddress: SignalServiceAddress, private val signalPr private val userPrivateKey get() = signalProtocolStore.identityKeyPair.privateKey.serialize() override fun decrypt(envelope: SignalServiceEnvelope, ciphertext: ByteArray): Plaintext { -// return if (envelope.isFallbackMessage) decryptFallbackMessage(envelope, ciphertext) else super.decrypt(envelope, ciphertext) - - return when { - envelope.isUnidentifiedSender -> { - //AC: Messages come unencrypted (for refactoring time being). - val transportDetails = PushTransportDetails(FallbackSessionCipher.sessionVersion) - val unpaddedMessageBody = transportDetails.getStrippedPaddingMessageBody(ciphertext) - val metadata = Metadata(envelope.source, envelope.sourceDevice, envelope.timestamp, false) - return Plaintext(metadata, unpaddedMessageBody) - } - envelope.isFallbackMessage -> - decryptFallbackMessage(envelope, ciphertext) - else -> - super.decrypt(envelope, ciphertext) - } + return if (envelope.isFallbackMessage) decryptFallbackMessage(envelope, ciphertext) else super.decrypt(envelope, ciphertext) } private fun decryptFallbackMessage(envelope: SignalServiceEnvelope, ciphertext: ByteArray): Plaintext {