make things work

This commit is contained in:
Ryan ZHAO 2021-01-25 16:58:04 +11:00
parent 8c22514b73
commit ef2380da76
3 changed files with 20 additions and 32 deletions

View File

@ -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)
}

View File

@ -23,11 +23,11 @@ private typealias Path = List<Snode>
/**
* 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<Path, Int>()
private val snodeFailureCount = mutableMapOf<Snode, Int>()
public var guardSnodes = setOf<Snode>()
public var paths: List<Path> // Not a set to ensure we consistently show the same path to the user
var guardSnodes = setOf<Snode>()
var paths: List<Path> // 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<Map<*, *>, Exception> {
fun sendOnionRequest(request: Request, server: String, x25519PublicKey: String, target: String = "/loki/v3/lsrpc", isJSONRequired: Boolean = true): Promise<Map<*, *>, Exception> {
val headers = request.getHeadersForOnionRequest()
val url = request.url()
val urlAsString = url.toString()

View File

@ -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 {