WIP: clean up signal protocol

This commit is contained in:
Ryan ZHAO 2021-02-19 11:35:06 +11:00
parent 735cceafdb
commit bbd067873a
8 changed files with 4 additions and 118 deletions

View File

@ -12,7 +12,7 @@ import org.session.libsession.snode.SnodeAPI
import org.session.libsession.messaging.fileserver.FileServerAPI import org.session.libsession.messaging.fileserver.FileServerAPI
import org.session.libsignal.utilities.logging.Log 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.crypto.ProfileCipherOutputStream
import org.session.libsignal.service.api.messages.SignalServiceAttachment import org.session.libsignal.service.api.messages.SignalServiceAttachment
import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException

View File

@ -8,7 +8,6 @@ package org.session.libsignal.libsignal;
import org.session.libsignal.libsignal.ecc.Curve; import org.session.libsignal.libsignal.ecc.Curve;
import org.session.libsignal.libsignal.ecc.ECKeyPair; import org.session.libsignal.libsignal.ecc.ECKeyPair;
import org.session.libsignal.libsignal.ecc.ECPublicKey; 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.CiphertextMessage;
import org.session.libsignal.libsignal.protocol.PreKeySignalMessage; import org.session.libsignal.libsignal.protocol.PreKeySignalMessage;
import org.session.libsignal.libsignal.protocol.SignalMessage; import org.session.libsignal.libsignal.protocol.SignalMessage;
@ -332,7 +331,7 @@ public class SessionCipher {
synchronized (SESSION_LOCK) { synchronized (SESSION_LOCK) {
if (!sessionStore.containsSession(remoteAddress)) { if (!sessionStore.containsSession(remoteAddress)) {
// Loki - If we have no session then we must be using the FallbackSessionCipher // Loki - If we have no session then we must be using the FallbackSessionCipher
return FallbackSessionCipher.getSessionVersion(); return 3;
} }
SessionRecord record = sessionStore.loadSession(remoteAddress); SessionRecord record = sessionStore.loadSession(remoteAddress);

View File

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

View File

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

View File

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

View File

@ -23,7 +23,6 @@ import org.session.libsignal.libsignal.ecc.ECKeyPair;
import org.session.libsignal.libsignal.ecc.ECPrivateKey; import org.session.libsignal.libsignal.ecc.ECPrivateKey;
import org.session.libsignal.libsignal.ecc.ECPublicKey; import org.session.libsignal.libsignal.ecc.ECPublicKey;
import org.session.libsignal.libsignal.kdf.HKDFv3; 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.CiphertextMessage;
import org.session.libsignal.libsignal.protocol.PreKeySignalMessage; import org.session.libsignal.libsignal.protocol.PreKeySignalMessage;
import org.session.libsignal.libsignal.protocol.SignalMessage; import org.session.libsignal.libsignal.protocol.SignalMessage;
@ -197,14 +196,6 @@ public class SealedSessionCipher {
switch (message.getType()) { switch (message.getType()) {
case CiphertextMessage.WHISPER_TYPE: return new SessionCipher(signalProtocolStore, sender).decrypt(new SignalMessage(message.getContent())); 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.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()); default: throw new InvalidMessageException("Unknown type: " + message.getType());
} }
} }

View File

@ -9,7 +9,7 @@ import okhttp3.MultipartBody
import okhttp3.Request import okhttp3.Request
import okhttp3.RequestBody import okhttp3.RequestBody
import org.session.libsignal.utilities.logging.Log 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.crypto.ProfileCipherOutputStream
import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException import org.session.libsignal.service.api.push.exceptions.NonSuccessfulResponseCodeException
import org.session.libsignal.service.api.push.exceptions.PushNetworkException import org.session.libsignal.service.api.push.exceptions.PushNetworkException

View File

@ -1,4 +1,4 @@
package org.session.libsignal.libsignal.loki package org.session.libsignal.utilities
import org.whispersystems.curve25519.Curve25519 import org.whispersystems.curve25519.Curve25519
import org.session.libsignal.service.internal.util.Util import org.session.libsignal.service.internal.util.Util