This commit is contained in:
Ryan ZHAO 2021-02-17 16:42:53 +11:00
parent 64352707d0
commit c69a8bc15f
2 changed files with 0 additions and 59 deletions

View File

@ -108,12 +108,8 @@ import org.session.libsignal.service.loki.api.opengroups.PublicChatAPI;
import org.session.libsignal.service.loki.api.shelved.p2p.LokiP2PAPI; import org.session.libsignal.service.loki.api.shelved.p2p.LokiP2PAPI;
import org.session.libsignal.service.loki.api.shelved.p2p.LokiP2PAPIDelegate; import org.session.libsignal.service.loki.api.shelved.p2p.LokiP2PAPIDelegate;
import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol; import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol;
import org.session.libsignal.service.loki.protocol.closedgroups.SharedSenderKeysImplementation;
import org.session.libsignal.service.loki.protocol.closedgroups.SharedSenderKeysImplementationDelegate;
import org.session.libsignal.service.loki.protocol.mentions.MentionsManager; import org.session.libsignal.service.loki.protocol.mentions.MentionsManager;
import org.session.libsignal.service.loki.protocol.meta.SessionMetaProtocol; import org.session.libsignal.service.loki.protocol.meta.SessionMetaProtocol;
import org.session.libsignal.service.loki.protocol.sessionmanagement.SessionManagementProtocol;
import org.session.libsignal.service.loki.protocol.sessionmanagement.SessionManagementProtocolDelegate;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;

View File

@ -1,66 +1,11 @@
package org.session.libsession.messaging.sending_receiving package org.session.libsession.messaging.sending_receiving
import org.session.libsession.messaging.MessagingConfiguration import org.session.libsession.messaging.MessagingConfiguration
import org.session.libsession.messaging.sending_receiving.MessageReceiver.Error
import org.session.libsession.utilities.AESGCM
import org.session.libsession.utilities.GroupUtil
import org.session.libsignal.libsignal.ecc.ECKeyPair import org.session.libsignal.libsignal.ecc.ECKeyPair
import org.whispersystems.curve25519.Curve25519
import org.session.libsignal.libsignal.loki.ClosedGroupCiphertextMessage
import org.session.libsignal.service.api.messages.SignalServiceEnvelope
import org.session.libsignal.service.internal.push.SignalServiceProtos
import org.session.libsignal.service.loki.protocol.closedgroups.SharedSenderKeysImplementation
import org.session.libsignal.service.loki.utilities.toHexString
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
object MessageReceiverDecryption { object MessageReceiverDecryption {
/*internal fun decryptWithSignalProtocol(envelope: SignalServiceProtos.Envelope): Pair<ByteArray, String> {
val storage = MessagingConfiguration.shared.signalStorage
val sskDatabase = MessagingConfiguration.shared.sskDatabase
val sessionResetImp = MessagingConfiguration.shared.sessionResetImp
val certificateValidator = MessagingConfiguration.shared.certificateValidator
val data = envelope.content
if (data.count() == 0) { throw Error.NoData }
val userPublicKey = MessagingConfiguration.shared.storage.getUserPublicKey() ?: throw Error.NoUserPublicKey
val localAddress = SignalServiceAddress(userPublicKey)
val cipher = SignalServiceCipher(localAddress, storage, sskDatabase, sessionResetImp, certificateValidator)
val result = cipher.decrypt(SignalServiceEnvelope(envelope))
return Pair(ByteArray(1), result.sender) // TODO: Return real plaintext
}*/
internal fun decryptWithSessionProtocol(ciphertext: ByteArray, x25519KeyPair: ECKeyPair): Pair<ByteArray, String> { internal fun decryptWithSessionProtocol(ciphertext: ByteArray, x25519KeyPair: ECKeyPair): Pair<ByteArray, String> {
return MessagingConfiguration.shared.sessionProtocol.decrypt(ciphertext, x25519KeyPair) return MessagingConfiguration.shared.sessionProtocol.decrypt(ciphertext, x25519KeyPair)
} }
/*internal fun decryptWithSharedSenderKeys(envelope: SignalServiceProtos.Envelope): Pair<ByteArray, String> {
// 1. ) Check preconditions
val groupPublicKey = envelope.source
if (!GroupUtil.isClosedGroup(groupPublicKey)) { throw Error.InvalidGroupPublicKey }
val data = envelope.content
if (data.count() == 0) { throw Error.NoData }
val groupPrivateKey = MessagingConfiguration.shared.sskDatabase.getClosedGroupPrivateKey(groupPublicKey) ?: throw Error.NoGroupPrivateKey
// 2. ) Parse the wrapper
val wrapper = SignalServiceProtos.ClosedGroupCiphertextMessageWrapper.parseFrom(data)
val ivAndCiphertext = wrapper.ciphertext.toByteArray()
val ephemeralPublicKey = wrapper.ephemeralPublicKey.toByteArray()
// 3. ) Decrypt the data inside
val ephemeralSharedSecret = Curve25519.getInstance(Curve25519.BEST).calculateAgreement(ephemeralPublicKey, groupPrivateKey.toByteArray())
val mac = Mac.getInstance("HmacSHA256")
mac.init(SecretKeySpec("LOKI".toByteArray(), "HmacSHA256"))
val symmetricKey = mac.doFinal(ephemeralSharedSecret)
val closedGroupCiphertextMessageAsData = AESGCM.decrypt(ivAndCiphertext, symmetricKey)
// 4. ) Parse the closed group ciphertext message
val closedGroupCiphertextMessage = ClosedGroupCiphertextMessage.from(closedGroupCiphertextMessageAsData) ?: throw Error.ParsingFailed
val senderPublicKey = closedGroupCiphertextMessage.senderPublicKey.toHexString()
if (senderPublicKey == MessagingConfiguration.shared.storage.getUserPublicKey()) { throw Error.SelfSend }
// 5. ) Use the info inside the closed group ciphertext message to decrypt the actual message content
val plaintext = SharedSenderKeysImplementation.shared.decrypt(closedGroupCiphertextMessage.ivAndCiphertext, groupPublicKey, senderPublicKey, closedGroupCiphertextMessage.keyIndex)
// 6. ) Return
return Pair(plaintext, senderPublicKey)
}*/
} }