mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
clean & refactor session protocol encryption
This commit is contained in:
parent
60f51af295
commit
91f9138d62
@ -55,7 +55,7 @@ import org.session.libsignal.service.loki.utilities.mentions.MentionsManager;
|
|||||||
import org.session.libsignal.utilities.logging.Log;
|
import org.session.libsignal.utilities.logging.Log;
|
||||||
import org.signal.aesgcmprovider.AesGcmProvider;
|
import org.signal.aesgcmprovider.AesGcmProvider;
|
||||||
import org.thoughtcrime.securesms.components.TypingStatusSender;
|
import org.thoughtcrime.securesms.components.TypingStatusSender;
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
import org.session.libsession.utilities.IdentityKeyUtil;
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||||
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||||||
|
@ -18,7 +18,6 @@ import org.session.libsession.utilities.Conversions
|
|||||||
import org.thoughtcrime.securesms.backup.BackupProtos.*
|
import org.thoughtcrime.securesms.backup.BackupProtos.*
|
||||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
||||||
import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream
|
import org.thoughtcrime.securesms.crypto.ClassicDecryptingPartInputStream
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
|
||||||
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream
|
import org.thoughtcrime.securesms.crypto.ModernDecryptingPartInputStream
|
||||||
import org.thoughtcrime.securesms.database.*
|
import org.thoughtcrime.securesms.database.*
|
||||||
import org.session.libsignal.utilities.logging.Log
|
import org.session.libsignal.utilities.logging.Log
|
||||||
@ -91,7 +90,7 @@ object FullBackupExporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (preference in IdentityKeyUtil.getBackupRecords(context)) {
|
for (preference in BackupUtil.getBackupRecords(context)) {
|
||||||
EventBus.getDefault().post(BackupEvent.createProgress(++count))
|
EventBus.getDefault().post(BackupEvent.createProgress(++count))
|
||||||
outputStream.writePreferenceEntry(preference)
|
outputStream.writePreferenceEntry(preference)
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import android.content.Context;
|
|||||||
|
|
||||||
import org.session.libsignal.libsignal.IdentityKeyPair;
|
import org.session.libsignal.libsignal.IdentityKeyPair;
|
||||||
import org.session.libsignal.libsignal.state.IdentityKeyStore;
|
import org.session.libsignal.libsignal.state.IdentityKeyStore;
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
import org.session.libsession.utilities.IdentityKeyUtil;
|
||||||
|
|
||||||
public class SignalProtocolStoreImpl implements IdentityKeyStore {
|
public class SignalProtocolStoreImpl implements IdentityKeyStore {
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ import org.session.libsignal.service.api.messages.SignalServiceGroup
|
|||||||
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
import org.session.libsignal.service.internal.push.SignalServiceProtos
|
||||||
import org.session.libsignal.service.loki.api.opengroups.PublicChat
|
import org.session.libsignal.service.loki.api.opengroups.PublicChat
|
||||||
import org.session.libsignal.utilities.logging.Log
|
import org.session.libsignal.utilities.logging.Log
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||||
import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase
|
import org.thoughtcrime.securesms.loki.database.LokiThreadDatabase
|
||||||
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol
|
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol
|
||||||
|
@ -34,7 +34,7 @@ import org.session.libsession.utilities.GroupUtil;
|
|||||||
import org.session.libsession.utilities.TextSecurePreferences;
|
import org.session.libsession.utilities.TextSecurePreferences;
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.contactshare.ContactModelMapper;
|
import org.thoughtcrime.securesms.contactshare.ContactModelMapper;
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
import org.session.libsession.utilities.IdentityKeyUtil;
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||||||
import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult;
|
import org.thoughtcrime.securesms.database.MessagingDatabase.InsertResult;
|
||||||
|
@ -30,10 +30,7 @@ import org.session.libsession.messaging.sending_receiving.MessageSender
|
|||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
import org.greenrobot.eventbus.Subscribe
|
import org.greenrobot.eventbus.Subscribe
|
||||||
import org.greenrobot.eventbus.ThreadMode
|
import org.greenrobot.eventbus.ThreadMode
|
||||||
import org.session.libsession.utilities.GroupUtil
|
import org.session.libsession.utilities.*
|
||||||
import org.session.libsession.utilities.ProfilePictureModifiedEvent
|
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
|
||||||
import org.session.libsession.utilities.Util
|
|
||||||
import org.session.libsignal.service.loki.utilities.mentions.MentionsManager
|
import org.session.libsignal.service.loki.utilities.mentions.MentionsManager
|
||||||
import org.session.libsignal.service.loki.utilities.toHexString
|
import org.session.libsignal.service.loki.utilities.toHexString
|
||||||
import org.session.libsignal.utilities.ThreadUtils
|
import org.session.libsignal.utilities.ThreadUtils
|
||||||
@ -43,7 +40,6 @@ import org.thoughtcrime.securesms.conversation.ConversationActivity
|
|||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.database.model.ThreadRecord
|
import org.thoughtcrime.securesms.database.model.ThreadRecord
|
||||||
import org.thoughtcrime.securesms.loki.dialogs.*
|
import org.thoughtcrime.securesms.loki.dialogs.*
|
||||||
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupsProtocolV2
|
|
||||||
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
||||||
import org.thoughtcrime.securesms.loki.utilities.*
|
import org.thoughtcrime.securesms.loki.utilities.*
|
||||||
import org.thoughtcrime.securesms.loki.views.ConversationView
|
import org.thoughtcrime.securesms.loki.views.ConversationView
|
||||||
|
@ -3,11 +3,9 @@ package org.thoughtcrime.securesms.loki.activities
|
|||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.Toast
|
|
||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
import org.thoughtcrime.securesms.ApplicationContext
|
|
||||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.loki.utilities.push
|
import org.thoughtcrime.securesms.loki.utilities.push
|
||||||
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
||||||
import org.thoughtcrime.securesms.loki.views.FakeChatView
|
import org.thoughtcrime.securesms.loki.views.FakeChatView
|
||||||
|
@ -35,7 +35,7 @@ import org.thoughtcrime.securesms.ApplicationContext
|
|||||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||||
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragment
|
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragment
|
||||||
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragmentDelegate
|
import org.thoughtcrime.securesms.loki.fragments.ScanQRCodeWrapperFragmentDelegate
|
||||||
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.push
|
import org.thoughtcrime.securesms.loki.utilities.push
|
||||||
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
||||||
|
@ -19,7 +19,7 @@ import org.session.libsignal.service.loki.crypto.MnemonicCodec
|
|||||||
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
||||||
import org.session.libsignal.utilities.Hex
|
import org.session.libsignal.utilities.Hex
|
||||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||||
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.push
|
import org.thoughtcrime.securesms.loki.utilities.push
|
||||||
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
||||||
|
@ -23,7 +23,7 @@ import org.session.libsignal.libsignal.ecc.ECKeyPair
|
|||||||
import org.session.libsignal.libsignal.util.KeyHelper
|
import org.session.libsignal.libsignal.util.KeyHelper
|
||||||
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
||||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||||
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.push
|
import org.thoughtcrime.securesms.loki.utilities.push
|
||||||
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
import org.thoughtcrime.securesms.loki.utilities.setUpActionBarSessionLogo
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -12,7 +12,7 @@ import android.widget.Toast
|
|||||||
import kotlinx.android.synthetic.main.activity_seed.*
|
import kotlinx.android.synthetic.main.activity_seed.*
|
||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
import org.thoughtcrime.securesms.BaseActionBarActivity
|
import org.thoughtcrime.securesms.BaseActionBarActivity
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
||||||
import org.thoughtcrime.securesms.loki.utilities.getColorWithID
|
import org.thoughtcrime.securesms.loki.utilities.getColorWithID
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
|
@ -7,12 +7,11 @@ import nl.komponents.kovenant.Promise
|
|||||||
import nl.komponents.kovenant.functional.bind
|
import nl.komponents.kovenant.functional.bind
|
||||||
import nl.komponents.kovenant.functional.map
|
import nl.komponents.kovenant.functional.map
|
||||||
import org.thoughtcrime.securesms.ApplicationContext
|
import org.thoughtcrime.securesms.ApplicationContext
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.session.libsession.messaging.threads.Address
|
import org.session.libsession.messaging.threads.Address
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
import org.thoughtcrime.securesms.jobs.PushDecryptJob
|
||||||
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
|
import org.thoughtcrime.securesms.jobs.RetrieveProfileAvatarJob
|
||||||
import org.thoughtcrime.securesms.loki.protocol.SessionMetaProtocol
|
|
||||||
import org.session.libsession.messaging.threads.recipients.Recipient
|
import org.session.libsession.messaging.threads.recipients.Recipient
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
import org.session.libsignal.libsignal.util.guava.Optional
|
import org.session.libsignal.libsignal.util.guava.Optional
|
||||||
|
@ -14,36 +14,12 @@ import org.session.libsignal.service.loki.api.crypto.SessionProtocol
|
|||||||
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
import org.session.libsignal.service.loki.utilities.hexEncodedPublicKey
|
||||||
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
||||||
import org.session.libsignal.service.loki.utilities.toHexString
|
import org.session.libsignal.service.loki.utilities.toHexString
|
||||||
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
|
|
||||||
class SessionProtocolImpl(private val context: Context) : SessionProtocol {
|
class SessionProtocolImpl(private val context: Context) : SessionProtocol {
|
||||||
|
|
||||||
private val sodium by lazy { LazySodiumAndroid(SodiumAndroid()) }
|
private val sodium by lazy { LazySodiumAndroid(SodiumAndroid()) }
|
||||||
|
|
||||||
override fun encrypt(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray {
|
|
||||||
val userED25519KeyPair = KeyPairUtilities.getUserED25519KeyPair(context) ?: throw SessionProtocol.Exception.NoUserED25519KeyPair
|
|
||||||
val recipientX25519PublicKey = Hex.fromStringCondensed(recipientHexEncodedX25519PublicKey.removing05PrefixIfNeeded())
|
|
||||||
|
|
||||||
val verificationData = plaintext + userED25519KeyPair.publicKey.asBytes + recipientX25519PublicKey
|
|
||||||
val signature = ByteArray(Sign.BYTES)
|
|
||||||
try {
|
|
||||||
sodium.cryptoSignDetached(signature, verificationData, verificationData.size.toLong(), userED25519KeyPair.secretKey.asBytes)
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
Log.d("Loki", "Couldn't sign message due to error: $exception.")
|
|
||||||
throw SessionProtocol.Exception.SigningFailed
|
|
||||||
}
|
|
||||||
val plaintextWithMetadata = plaintext + userED25519KeyPair.publicKey.asBytes + signature
|
|
||||||
val ciphertext = ByteArray(plaintextWithMetadata.size + Box.SEALBYTES)
|
|
||||||
try {
|
|
||||||
sodium.cryptoBoxSeal(ciphertext, plaintextWithMetadata, plaintextWithMetadata.size.toLong(), recipientX25519PublicKey)
|
|
||||||
} catch (exception: Exception) {
|
|
||||||
Log.d("Loki", "Couldn't encrypt message due to error: $exception.")
|
|
||||||
throw SessionProtocol.Exception.EncryptionFailed
|
|
||||||
}
|
|
||||||
|
|
||||||
return ciphertext
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun decrypt(ciphertext: ByteArray, x25519KeyPair: ECKeyPair): Pair<ByteArray, String> {
|
override fun decrypt(ciphertext: ByteArray, x25519KeyPair: ECKeyPair): Pair<ByteArray, String> {
|
||||||
val recipientX25519PrivateKey = x25519KeyPair.privateKey.serialize()
|
val recipientX25519PrivateKey = x25519KeyPair.privateKey.serialize()
|
||||||
val recipientX25519PublicKey = Hex.fromStringCondensed(x25519KeyPair.hexEncodedPublicKey.removing05PrefixIfNeeded())
|
val recipientX25519PublicKey = Hex.fromStringCondensed(x25519KeyPair.hexEncodedPublicKey.removing05PrefixIfNeeded())
|
||||||
|
@ -13,7 +13,7 @@ import org.session.libsignal.service.loki.api.Snode
|
|||||||
import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol
|
import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol
|
||||||
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
||||||
import org.session.libsignal.service.loki.utilities.toHexString
|
import org.session.libsignal.service.loki.utilities.toHexString
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.session.libsignal.utilities.Hex
|
import org.session.libsignal.utilities.Hex
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
import org.session.libsignal.service.loki.utilities.PublicKeyValidation
|
import org.session.libsignal.service.loki.utilities.PublicKeyValidation
|
||||||
|
@ -11,7 +11,7 @@ import kotlinx.android.synthetic.main.dialog_clear_all_data.view.*
|
|||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
import org.thoughtcrime.securesms.ApplicationContext
|
import org.thoughtcrime.securesms.ApplicationContext
|
||||||
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
import org.thoughtcrime.securesms.loki.protocol.MultiDeviceProtocol
|
||||||
import org.thoughtcrime.securesms.loki.utilities.KeyPairUtilities
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
|
|
||||||
class ClearAllDataDialog : DialogFragment() {
|
class ClearAllDataDialog : DialogFragment() {
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ import androidx.appcompat.app.AlertDialog
|
|||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
import kotlinx.android.synthetic.main.dialog_seed.view.*
|
import kotlinx.android.synthetic.main.dialog_seed.view.*
|
||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
import org.thoughtcrime.securesms.loki.utilities.MnemonicUtilities
|
||||||
import org.session.libsignal.service.loki.crypto.MnemonicCodec
|
import org.session.libsignal.service.loki.crypto.MnemonicCodec
|
||||||
import org.session.libsignal.service.loki.utilities.hexEncodedPrivateKey
|
import org.session.libsignal.service.loki.utilities.hexEncodedPrivateKey
|
||||||
|
@ -7,35 +7,73 @@ import android.net.Uri
|
|||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.DocumentsContract
|
import android.provider.DocumentsContract
|
||||||
import org.session.libsignal.utilities.logging.Log
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.annotation.WorkerThread
|
import androidx.annotation.WorkerThread
|
||||||
import androidx.documentfile.provider.DocumentFile
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import network.loki.messenger.R
|
import network.loki.messenger.R
|
||||||
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.EventBus
|
||||||
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
|
import org.session.libsignal.libsignal.util.ByteUtil
|
||||||
|
import org.session.libsignal.utilities.logging.Log
|
||||||
import org.thoughtcrime.securesms.backup.BackupEvent
|
import org.thoughtcrime.securesms.backup.BackupEvent
|
||||||
import org.thoughtcrime.securesms.backup.BackupPassphrase
|
import org.thoughtcrime.securesms.backup.BackupPassphrase
|
||||||
|
import org.thoughtcrime.securesms.backup.BackupProtos.SharedPreference
|
||||||
import org.thoughtcrime.securesms.backup.FullBackupExporter
|
import org.thoughtcrime.securesms.backup.FullBackupExporter
|
||||||
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
|
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
|
||||||
|
import org.session.libsession.utilities.IdentityKeyUtil
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.loki.database.BackupFileRecord
|
import org.thoughtcrime.securesms.loki.database.BackupFileRecord
|
||||||
import org.thoughtcrime.securesms.service.LocalBackupListener
|
import org.thoughtcrime.securesms.service.LocalBackupListener
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
|
||||||
import org.session.libsignal.libsignal.util.ByteUtil
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
import java.security.NoSuchAlgorithmException
|
import java.security.NoSuchAlgorithmException
|
||||||
import java.security.SecureRandom
|
import java.security.SecureRandom
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.jvm.Throws
|
|
||||||
|
|
||||||
object BackupUtil {
|
object BackupUtil {
|
||||||
|
private const val MASTER_SECRET_UTIL_PREFERENCES_NAME = "SecureSMS-Preferences"
|
||||||
private const val TAG = "BackupUtil"
|
private const val TAG = "BackupUtil"
|
||||||
const val BACKUP_FILE_MIME_TYPE = "application/session-backup"
|
const val BACKUP_FILE_MIME_TYPE = "application/session-backup"
|
||||||
const val BACKUP_PASSPHRASE_LENGTH = 30
|
const val BACKUP_PASSPHRASE_LENGTH = 30
|
||||||
|
|
||||||
|
fun getBackupRecords(context: Context): List<SharedPreference> {
|
||||||
|
val prefName = MASTER_SECRET_UTIL_PREFERENCES_NAME
|
||||||
|
val preferences = context.getSharedPreferences(prefName, 0)
|
||||||
|
val prefList = LinkedList<SharedPreference>()
|
||||||
|
prefList.add(SharedPreference.newBuilder()
|
||||||
|
.setFile(prefName)
|
||||||
|
.setKey(IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF)
|
||||||
|
.setValue(preferences.getString(IdentityKeyUtil.IDENTITY_PUBLIC_KEY_PREF, null))
|
||||||
|
.build())
|
||||||
|
prefList.add(SharedPreference.newBuilder()
|
||||||
|
.setFile(prefName)
|
||||||
|
.setKey(IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF)
|
||||||
|
.setValue(preferences.getString(IdentityKeyUtil.IDENTITY_PRIVATE_KEY_PREF, null))
|
||||||
|
.build())
|
||||||
|
if (preferences.contains(IdentityKeyUtil.ED25519_PUBLIC_KEY)) {
|
||||||
|
prefList.add(SharedPreference.newBuilder()
|
||||||
|
.setFile(prefName)
|
||||||
|
.setKey(IdentityKeyUtil.ED25519_PUBLIC_KEY)
|
||||||
|
.setValue(preferences.getString(IdentityKeyUtil.ED25519_PUBLIC_KEY, null))
|
||||||
|
.build())
|
||||||
|
}
|
||||||
|
if (preferences.contains(IdentityKeyUtil.ED25519_SECRET_KEY)) {
|
||||||
|
prefList.add(SharedPreference.newBuilder()
|
||||||
|
.setFile(prefName)
|
||||||
|
.setKey(IdentityKeyUtil.ED25519_SECRET_KEY)
|
||||||
|
.setValue(preferences.getString(IdentityKeyUtil.ED25519_SECRET_KEY, null))
|
||||||
|
.build())
|
||||||
|
}
|
||||||
|
prefList.add(SharedPreference.newBuilder()
|
||||||
|
.setFile(prefName)
|
||||||
|
.setKey(IdentityKeyUtil.LOKI_SEED)
|
||||||
|
.setValue(preferences.getString(IdentityKeyUtil.LOKI_SEED, null))
|
||||||
|
.build())
|
||||||
|
return prefList
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set app-wide configuration to enable the backups and schedule them.
|
* Set app-wide configuration to enable the backups and schedule them.
|
||||||
*
|
*
|
||||||
@ -91,7 +129,7 @@ object BackupUtil {
|
|||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun generateBackupPassphrase(): Array<String> {
|
fun generateBackupPassphrase(): Array<String> {
|
||||||
val random = ByteArray(BACKUP_PASSPHRASE_LENGTH).also { SecureRandom().nextBytes(it) }
|
val random = ByteArray(BACKUP_PASSPHRASE_LENGTH).also { SecureRandom().nextBytes(it) }
|
||||||
return Array(6) {i ->
|
return Array(6) { i ->
|
||||||
String.format("%05d", ByteUtil.byteArray5ToLong(random, i * 5) % 100000)
|
String.format("%05d", ByteUtil.byteArray5ToLong(random, i * 5) % 100000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,53 @@
|
|||||||
package org.session.libsession.messaging.sending_receiving
|
package org.session.libsession.messaging.sending_receiving
|
||||||
|
|
||||||
|
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
|
||||||
|
import com.goterl.lazycode.lazysodium.SodiumAndroid
|
||||||
|
import com.goterl.lazycode.lazysodium.interfaces.Box
|
||||||
|
import com.goterl.lazycode.lazysodium.interfaces.Sign
|
||||||
|
|
||||||
import org.session.libsession.messaging.MessagingConfiguration
|
import org.session.libsession.messaging.MessagingConfiguration
|
||||||
|
import org.session.libsession.messaging.sending_receiving.MessageSender.Error
|
||||||
|
import org.session.libsession.utilities.KeyPairUtilities
|
||||||
|
|
||||||
|
import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded
|
||||||
|
import org.session.libsignal.utilities.Hex
|
||||||
|
import org.session.libsignal.utilities.logging.Log
|
||||||
|
|
||||||
object MessageSenderEncryption {
|
object MessageSenderEncryption {
|
||||||
|
|
||||||
internal fun encryptWithSessionProtocol(plaintext: ByteArray, recipientPublicKey: String): ByteArray{
|
private val sodium by lazy { LazySodiumAndroid(SodiumAndroid()) }
|
||||||
return MessagingConfiguration.shared.sessionProtocol.encrypt(plaintext, recipientPublicKey)
|
|
||||||
|
/**
|
||||||
|
* Encrypts `plaintext` using the Session protocol for `hexEncodedX25519PublicKey`.
|
||||||
|
*
|
||||||
|
* @param plaintext the plaintext to encrypt. Must already be padded.
|
||||||
|
* @param recipientHexEncodedX25519PublicKey the X25519 public key to encrypt for. Could be the Session ID of a user, or the public key of a closed group.
|
||||||
|
*
|
||||||
|
* @return the encrypted message.
|
||||||
|
*/
|
||||||
|
internal fun encryptWithSessionProtocol(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray{
|
||||||
|
val context = MessagingConfiguration.shared.context
|
||||||
|
val userED25519KeyPair = KeyPairUtilities.getUserED25519KeyPair(context) ?: throw Error.NoUserED25519KeyPair
|
||||||
|
val recipientX25519PublicKey = Hex.fromStringCondensed(recipientHexEncodedX25519PublicKey.removing05PrefixIfNeeded())
|
||||||
|
|
||||||
|
val verificationData = plaintext + userED25519KeyPair.publicKey.asBytes + recipientX25519PublicKey
|
||||||
|
val signature = ByteArray(Sign.BYTES)
|
||||||
|
try {
|
||||||
|
sodium.cryptoSignDetached(signature, verificationData, verificationData.size.toLong(), userED25519KeyPair.secretKey.asBytes)
|
||||||
|
} catch (exception: Exception) {
|
||||||
|
Log.d("Loki", "Couldn't sign message due to error: $exception.")
|
||||||
|
throw Error.SigningFailed
|
||||||
|
}
|
||||||
|
val plaintextWithMetadata = plaintext + userED25519KeyPair.publicKey.asBytes + signature
|
||||||
|
val ciphertext = ByteArray(plaintextWithMetadata.size + Box.SEALBYTES)
|
||||||
|
try {
|
||||||
|
sodium.cryptoBoxSeal(ciphertext, plaintextWithMetadata, plaintextWithMetadata.size.toLong(), recipientX25519PublicKey)
|
||||||
|
} catch (exception: Exception) {
|
||||||
|
Log.d("Loki", "Couldn't encrypt message due to error: $exception.")
|
||||||
|
throw Error.EncryptionFailed
|
||||||
|
}
|
||||||
|
|
||||||
|
return ciphertext
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -15,7 +15,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package org.thoughtcrime.securesms.crypto;
|
package org.session.libsession.utilities;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
@ -23,7 +23,6 @@ import android.content.SharedPreferences.Editor;
|
|||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import org.session.libsignal.libsignal.ecc.ECPublicKey;
|
import org.session.libsignal.libsignal.ecc.ECPublicKey;
|
||||||
import org.thoughtcrime.securesms.backup.BackupProtos;
|
|
||||||
import org.session.libsignal.libsignal.IdentityKey;
|
import org.session.libsignal.libsignal.IdentityKey;
|
||||||
import org.session.libsignal.libsignal.IdentityKeyPair;
|
import org.session.libsignal.libsignal.IdentityKeyPair;
|
||||||
import org.session.libsignal.libsignal.InvalidKeyException;
|
import org.session.libsignal.libsignal.InvalidKeyException;
|
||||||
@ -34,8 +33,6 @@ import org.session.libsignal.libsignal.ecc.ECPrivateKey;
|
|||||||
import org.session.libsignal.utilities.Base64;
|
import org.session.libsignal.utilities.Base64;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Utility class for working with identity keys.
|
* Utility class for working with identity keys.
|
||||||
@ -95,45 +92,6 @@ public class IdentityKeyUtil {
|
|||||||
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize()));
|
save(context, IDENTITY_PRIVATE_KEY_PREF, Base64.encodeBytes(privateKey.serialize()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<BackupProtos.SharedPreference> getBackupRecords(@NonNull Context context) {
|
|
||||||
final String prefName = MASTER_SECRET_UTIL_PREFERENCES_NAME;
|
|
||||||
SharedPreferences preferences = context.getSharedPreferences(prefName, 0);
|
|
||||||
|
|
||||||
LinkedList<BackupProtos.SharedPreference> prefList = new LinkedList<>();
|
|
||||||
|
|
||||||
prefList.add(BackupProtos.SharedPreference.newBuilder()
|
|
||||||
.setFile(prefName)
|
|
||||||
.setKey(IDENTITY_PUBLIC_KEY_PREF)
|
|
||||||
.setValue(preferences.getString(IDENTITY_PUBLIC_KEY_PREF, null))
|
|
||||||
.build());
|
|
||||||
prefList.add(BackupProtos.SharedPreference.newBuilder()
|
|
||||||
.setFile(prefName)
|
|
||||||
.setKey(IDENTITY_PRIVATE_KEY_PREF)
|
|
||||||
.setValue(preferences.getString(IDENTITY_PRIVATE_KEY_PREF, null))
|
|
||||||
.build());
|
|
||||||
if (preferences.contains(ED25519_PUBLIC_KEY)) {
|
|
||||||
prefList.add(BackupProtos.SharedPreference.newBuilder()
|
|
||||||
.setFile(prefName)
|
|
||||||
.setKey(ED25519_PUBLIC_KEY)
|
|
||||||
.setValue(preferences.getString(ED25519_PUBLIC_KEY, null))
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
if (preferences.contains(ED25519_SECRET_KEY)) {
|
|
||||||
prefList.add(BackupProtos.SharedPreference.newBuilder()
|
|
||||||
.setFile(prefName)
|
|
||||||
.setKey(ED25519_SECRET_KEY)
|
|
||||||
.setValue(preferences.getString(ED25519_SECRET_KEY, null))
|
|
||||||
.build());
|
|
||||||
}
|
|
||||||
prefList.add(BackupProtos.SharedPreference.newBuilder()
|
|
||||||
.setFile(prefName)
|
|
||||||
.setKey(LOKI_SEED)
|
|
||||||
.setValue(preferences.getString(LOKI_SEED, null))
|
|
||||||
.build());
|
|
||||||
|
|
||||||
return prefList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String retrieve(Context context, String key) {
|
public static String retrieve(Context context, String key) {
|
||||||
SharedPreferences preferences = context.getSharedPreferences(MASTER_SECRET_UTIL_PREFERENCES_NAME, 0);
|
SharedPreferences preferences = context.getSharedPreferences(MASTER_SECRET_UTIL_PREFERENCES_NAME, 0);
|
||||||
return preferences.getString(key, null);
|
return preferences.getString(key, null);
|
@ -1,11 +1,10 @@
|
|||||||
package org.thoughtcrime.securesms.loki.utilities
|
package org.session.libsession.utilities
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
|
import com.goterl.lazycode.lazysodium.LazySodiumAndroid
|
||||||
import com.goterl.lazycode.lazysodium.SodiumAndroid
|
import com.goterl.lazycode.lazysodium.SodiumAndroid
|
||||||
import com.goterl.lazycode.lazysodium.utils.Key
|
import com.goterl.lazycode.lazysodium.utils.Key
|
||||||
import com.goterl.lazycode.lazysodium.utils.KeyPair
|
import com.goterl.lazycode.lazysodium.utils.KeyPair
|
||||||
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil
|
|
||||||
import org.session.libsignal.utilities.Base64
|
import org.session.libsignal.utilities.Base64
|
||||||
import org.session.libsignal.utilities.Hex
|
import org.session.libsignal.utilities.Hex
|
||||||
import org.session.libsignal.libsignal.ecc.DjbECPrivateKey
|
import org.session.libsignal.libsignal.ecc.DjbECPrivateKey
|
@ -17,17 +17,6 @@ interface SessionProtocol {
|
|||||||
object DecryptionFailed : Exception("Couldn't decrypt message.")
|
object DecryptionFailed : Exception("Couldn't decrypt message.")
|
||||||
object InvalidSignature : Exception("Invalid message signature.")
|
object InvalidSignature : Exception("Invalid message signature.")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Encrypts `plaintext` using the Session protocol for `hexEncodedX25519PublicKey`.
|
|
||||||
*
|
|
||||||
* @param plaintext the plaintext to encrypt. Must already be padded.
|
|
||||||
* @param recipientHexEncodedX25519PublicKey the X25519 public key to encrypt for. Could be the Session ID of a user, or the public key of a closed group.
|
|
||||||
*
|
|
||||||
* @return the encrypted message.
|
|
||||||
*/
|
|
||||||
fun encrypt(plaintext: ByteArray, recipientHexEncodedX25519PublicKey: String): ByteArray
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decrypts `ciphertext` using the Session protocol and `x25519KeyPair`.
|
* Decrypts `ciphertext` using the Session protocol and `x25519KeyPair`.
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user