mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
WIP: clean up signal protocol
This commit is contained in:
parent
735cceafdb
commit
bbd067873a
@ -12,7 +12,7 @@ import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.messaging.fileserver.FileServerAPI
|
||||
|
||||
import org.session.libsignal.utilities.logging.Log
|
||||
import org.session.libsignal.libsignal.loki.DiffieHellman
|
||||
import org.session.libsignal.utilities.DiffieHellman
|
||||
import org.session.libsignal.service.api.crypto.ProfileCipherOutputStream
|
||||
import org.session.libsignal.service.api.messages.SignalServiceAttachment
|
||||
import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException
|
||||
|
@ -8,7 +8,6 @@ package org.session.libsignal.libsignal;
|
||||
import org.session.libsignal.libsignal.ecc.Curve;
|
||||
import org.session.libsignal.libsignal.ecc.ECKeyPair;
|
||||
import org.session.libsignal.libsignal.ecc.ECPublicKey;
|
||||
import org.session.libsignal.libsignal.loki.FallbackSessionCipher;
|
||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage;
|
||||
import org.session.libsignal.libsignal.protocol.PreKeySignalMessage;
|
||||
import org.session.libsignal.libsignal.protocol.SignalMessage;
|
||||
@ -332,7 +331,7 @@ public class SessionCipher {
|
||||
synchronized (SESSION_LOCK) {
|
||||
if (!sessionStore.containsSession(remoteAddress)) {
|
||||
// Loki - If we have no session then we must be using the FallbackSessionCipher
|
||||
return FallbackSessionCipher.getSessionVersion();
|
||||
return 3;
|
||||
}
|
||||
|
||||
SessionRecord record = sessionStore.loadSession(remoteAddress);
|
||||
|
@ -1,39 +0,0 @@
|
||||
package org.session.libsignal.libsignal.loki
|
||||
|
||||
import com.google.protobuf.ByteString
|
||||
import org.session.libsignal.utilities.logging.Log
|
||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage
|
||||
import org.session.libsignal.libsignal.protocol.SignalProtos
|
||||
|
||||
class ClosedGroupCiphertextMessage(val ivAndCiphertext: ByteArray, val senderPublicKey: ByteArray, val keyIndex: Int) : CiphertextMessage {
|
||||
private val serialized: ByteArray
|
||||
|
||||
companion object {
|
||||
|
||||
fun from(serialized: ByteArray): ClosedGroupCiphertextMessage? {
|
||||
try {
|
||||
val proto = SignalProtos.ClosedGroupCiphertextMessage.parseFrom(serialized)
|
||||
return ClosedGroupCiphertextMessage(proto.ciphertext.toByteArray(), proto.senderPublicKey.toByteArray(), proto.keyIndex)
|
||||
} catch (exception: Exception) {
|
||||
Log.d("Loki", "Couldn't parse proto due to error: $exception.")
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init {
|
||||
val builder = SignalProtos.ClosedGroupCiphertextMessage.newBuilder()
|
||||
builder.ciphertext = ByteString.copyFrom(ivAndCiphertext)
|
||||
builder.senderPublicKey = ByteString.copyFrom(senderPublicKey)
|
||||
builder.keyIndex = keyIndex
|
||||
serialized = builder.build().toByteArray()
|
||||
}
|
||||
|
||||
override fun getType(): Int {
|
||||
return CiphertextMessage.CLOSED_GROUP_CIPHERTEXT
|
||||
}
|
||||
|
||||
override fun serialize(): ByteArray {
|
||||
return serialized
|
||||
}
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package org.session.libsignal.libsignal.loki
|
||||
|
||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage
|
||||
|
||||
class FallbackMessage(private val paddedMessageBody: ByteArray) : CiphertextMessage {
|
||||
|
||||
override fun serialize(): ByteArray {
|
||||
return paddedMessageBody
|
||||
}
|
||||
|
||||
override fun getType(): Int {
|
||||
return CiphertextMessage.FALLBACK_MESSAGE_TYPE
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package org.session.libsignal.libsignal.loki
|
||||
|
||||
import org.whispersystems.curve25519.Curve25519
|
||||
import org.session.libsignal.utilities.Hex
|
||||
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
||||
|
||||
/**
|
||||
* A session cipher that uses the current user's private key along with a contact's public key to encrypt data.
|
||||
*/
|
||||
class FallbackSessionCipher(private val userPrivateKey: ByteArray, private val hexEncodedContactPublicKey: String) {
|
||||
|
||||
private val contactPublicKey by lazy {
|
||||
val hexEncodedContactPublicKey = hexEncodedContactPublicKey.removing05PrefixIfNeeded()
|
||||
Hex.fromStringCondensed(hexEncodedContactPublicKey)
|
||||
}
|
||||
|
||||
private val symmetricKey: ByteArray?
|
||||
get() {
|
||||
try {
|
||||
val curve = Curve25519.getInstance(Curve25519.BEST)
|
||||
return curve.calculateAgreement(contactPublicKey, userPrivateKey)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic val sessionVersion = 3
|
||||
}
|
||||
|
||||
fun encrypt(paddedMessageBody: ByteArray): ByteArray? {
|
||||
val symmetricKey = symmetricKey ?: return null
|
||||
try {
|
||||
return DiffieHellman.encrypt(paddedMessageBody, symmetricKey)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
fun decrypt(bytes: ByteArray): ByteArray? {
|
||||
val symmetricKey = symmetricKey ?: return null
|
||||
try {
|
||||
return DiffieHellman.decrypt(bytes, symmetricKey)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return null
|
||||
}
|
||||
}
|
||||
}
|
@ -23,7 +23,6 @@ import org.session.libsignal.libsignal.ecc.ECKeyPair;
|
||||
import org.session.libsignal.libsignal.ecc.ECPrivateKey;
|
||||
import org.session.libsignal.libsignal.ecc.ECPublicKey;
|
||||
import org.session.libsignal.libsignal.kdf.HKDFv3;
|
||||
import org.session.libsignal.libsignal.loki.FallbackSessionCipher;
|
||||
import org.session.libsignal.libsignal.protocol.CiphertextMessage;
|
||||
import org.session.libsignal.libsignal.protocol.PreKeySignalMessage;
|
||||
import org.session.libsignal.libsignal.protocol.SignalMessage;
|
||||
@ -197,14 +196,6 @@ public class SealedSessionCipher {
|
||||
switch (message.getType()) {
|
||||
case CiphertextMessage.WHISPER_TYPE: return new SessionCipher(signalProtocolStore, sender).decrypt(new SignalMessage(message.getContent()));
|
||||
case CiphertextMessage.PREKEY_TYPE: return new SessionCipher(signalProtocolStore, sender).decrypt(new PreKeySignalMessage(message.getContent()));
|
||||
case CiphertextMessage.FALLBACK_MESSAGE_TYPE: {
|
||||
try {
|
||||
byte[] privateKey = signalProtocolStore.getIdentityKeyPair().getPrivateKey().serialize();
|
||||
return new FallbackSessionCipher(privateKey, sender.getName()).decrypt(message.getContent());
|
||||
} catch (Exception e) {
|
||||
throw new InvalidMessageException("Failed to decrypt fallback message.");
|
||||
}
|
||||
}
|
||||
default: throw new InvalidMessageException("Unknown type: " + message.getType());
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import okhttp3.MultipartBody
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsignal.utilities.logging.Log
|
||||
import org.session.libsignal.libsignal.loki.DiffieHellman
|
||||
import org.session.libsignal.utilities.DiffieHellman
|
||||
import org.session.libsignal.service.api.crypto.ProfileCipherOutputStream
|
||||
import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException
|
||||
import org.session.libsignal.service.api.push.exceptions.PushNetworkException
|
||||
|
@ -1,4 +1,4 @@
|
||||
package org.session.libsignal.libsignal.loki
|
||||
package org.session.libsignal.utilities
|
||||
|
||||
import org.whispersystems.curve25519.Curve25519
|
||||
import org.session.libsignal.service.internal.util.Util
|
Loading…
Reference in New Issue
Block a user