This commit is contained in:
Benoit Marty 2019-05-20 16:55:45 +02:00
parent 532a028e41
commit f5bd215f36
17 changed files with 72 additions and 114 deletions

View File

@ -88,7 +88,7 @@ internal class CryptoManager(
//
private val mObjectSigner: ObjectSigner,
//
private val mOneTimeKeysManager: OneTimeKeysManager,
private val mOneTimeKeysUploader: OneTimeKeysUploader,
//
private val roomDecryptorProvider: RoomDecryptorProvider,
// The SAS verification service.
@ -96,7 +96,7 @@ internal class CryptoManager(
//
private val mIncomingRoomKeyRequestManager: IncomingRoomKeyRequestManager,
//
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager,
private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
// Room service
private val mRoomService: RoomService,
// Olm Manager
@ -262,10 +262,10 @@ internal class CryptoManager(
Timber.d(" - device id : " + mCredentials.deviceId)
Timber.d(" - ed25519 : " + mOlmDevice.deviceEd25519Key)
Timber.d(" - curve25519 : " + mOlmDevice.deviceCurve25519Key)
Timber.d(" - oneTimeKeys: " + mOneTimeKeysManager.mLastPublishedOneTimeKeys)
Timber.d(" - oneTimeKeys: " + mOneTimeKeysUploader.mLastPublishedOneTimeKeys)
Timber.d("")

mOneTimeKeysManager.maybeUploadOneTimeKeys(object : MatrixCallback<Unit> {
mOneTimeKeysUploader.maybeUploadOneTimeKeys(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
// TODO
//if (null != mNetworkConnectivityReceiver) {
@ -353,7 +353,7 @@ internal class CryptoManager(

if (null != syncResponse.deviceOneTimeKeysCount) {
val currentCount = syncResponse.deviceOneTimeKeysCount.signedCurve25519 ?: 0
mOneTimeKeysManager.updateOneTimeKeyCount(currentCount)
mOneTimeKeysUploader.updateOneTimeKeyCount(currentCount)
}

if (isStarted()) {
@ -362,7 +362,7 @@ internal class CryptoManager(
}

if (!isCatchingUp && isStarted()) {
mOneTimeKeysManager.maybeUploadOneTimeKeys()
mOneTimeKeysUploader.maybeUploadOneTimeKeys()

mIncomingRoomKeyRequestManager.processReceivedRoomKeyRequests()
}
@ -865,8 +865,8 @@ internal class CryptoManager(
val encryptedRoomKeys: ByteArray

try {
val moshi = MoshiProvider.providesMoshi()
val adapter = moshi.adapter(List::class.java)
val adapter = MoshiProvider.providesMoshi()
.adapter(List::class.java)

encryptedRoomKeys = MXMegolmExportEncryption
.encryptMegolmKeyFile(adapter.toJson(exportedSessions), password, iterationCount)
@ -909,9 +909,9 @@ internal class CryptoManager(
Timber.d("## importRoomKeys : decryptMegolmKeyFile done in " + (t1 - t0) + " ms")

try {
val moshi = MoshiProvider.providesMoshi()
val adapter = moshi.adapter(List::class.java)
val list = adapter.fromJson(roomKeys)
val list = MoshiProvider.providesMoshi()
.adapter(List::class.java)
.fromJson(roomKeys)
importedSessions = list as List<MegolmSessionData>
} catch (e: Exception) {
Timber.e(e, "## importRoomKeys failed")
@ -949,7 +949,7 @@ internal class CryptoManager(
override fun onSuccess(data: MXUsersDevicesMap<MXDeviceInfo>) {
val unknownDevices = getUnknownDevices(data)

if (unknownDevices.map.size == 0) {
if (unknownDevices.map.isEmpty()) {
callback.onSuccess(Unit)
} else {
// trigger an an unknown devices exception
@ -1100,6 +1100,30 @@ internal class CryptoManager(
mIncomingRoomKeyRequestManager.removeRoomKeysRequestListener(listener)
}

/**
* Provides the list of unknown devices
*
* @param devicesInRoom the devices map
* @return the unknown devices map
*/
private fun getUnknownDevices(devicesInRoom: MXUsersDevicesMap<MXDeviceInfo>): MXUsersDevicesMap<MXDeviceInfo> {
val unknownDevices = MXUsersDevicesMap<MXDeviceInfo>()

val userIds = devicesInRoom.userIds
for (userId in userIds) {
val deviceIds = devicesInRoom.getUserDeviceIds(userId)
for (deviceId in deviceIds!!) {
val deviceInfo = devicesInRoom.getObject(deviceId, userId)

if (deviceInfo!!.isUnknown) {
unknownDevices.setObject(deviceInfo, userId, deviceId)
}
}
}

return unknownDevices
}

/* ==========================================================================================
* DEBUG INFO
* ========================================================================================== */
@ -1108,30 +1132,4 @@ internal class CryptoManager(
return "CryptoManager of " + mCredentials.userId + " (" + mCredentials.deviceId + ")"

}

companion object {
/**
* Provides the list of unknown devices
*
* @param devicesInRoom the devices map
* @return the unknown devices map
*/
fun getUnknownDevices(devicesInRoom: MXUsersDevicesMap<MXDeviceInfo>): MXUsersDevicesMap<MXDeviceInfo> {
val unknownDevices = MXUsersDevicesMap<MXDeviceInfo>()

val userIds = devicesInRoom.userIds
for (userId in userIds) {
val deviceIds = devicesInRoom.getUserDeviceIds(userId)
for (deviceId in deviceIds!!) {
val deviceInfo = devicesInRoom.getObject(deviceId, userId)

if (deviceInfo!!.isUnknown) {
unknownDevices.setObject(deviceInfo, userId, deviceId)
}
}
}

return unknownDevices
}
}
}

View File

@ -85,7 +85,7 @@ internal class CryptoModule {

//
scope(DefaultSession.SCOPE) {
MXOutgoingRoomKeyRequestManager(get(), get(), get())
OutgoingRoomKeyRequestManager(get(), get(), get())
}

scope(DefaultSession.SCOPE) {
@ -108,9 +108,9 @@ internal class CryptoModule {
ObjectSigner(get(), get())
}

// OneTimeKeysManager
// OneTimeKeysUploader
scope(DefaultSession.SCOPE) {
OneTimeKeysManager(get(), get(), get(), get(), get())
OneTimeKeysUploader(get(), get(), get(), get(), get())
}

// Actions

View File

@ -62,7 +62,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
* @param callback the asynchronous callback
*/
internal inner class DownloadKeysPromise(userIds: List<String>,
val mCallback: MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>>?) {
val callback: MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>>?) {
// list of remain pending device keys
val mPendingUserIdsList: MutableList<String>

@ -327,7 +327,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
}
}

val callback = promise.mCallback
val callback = promise.callback

if (null != callback) {
CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(usersDevicesInfoMap) }

View File

@ -17,6 +17,7 @@

package im.vector.matrix.android.internal.crypto

// TODO Update comment
internal object MXCryptoAlgorithms {

/**

View File

@ -18,19 +18,15 @@ package im.vector.matrix.android.internal.crypto

import android.text.TextUtils
import android.util.Base64

import timber.log.Timber
import java.io.ByteArrayOutputStream
import java.nio.charset.Charset
import java.security.SecureRandom
import java.util.Arrays

import java.util.*
import javax.crypto.Cipher
import javax.crypto.Mac
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec

import timber.log.Timber
import java.nio.charset.Charset
import kotlin.experimental.and
import kotlin.experimental.xor

@ -363,11 +359,3 @@ object MXMegolmExportEncryption {
return key
}
}
/**
* Encrypt a string into the megolm export format.
*
* @param data the data to encrypt.
* @param password the password
* @return the encrypted data
* @throws Exception the failure reason
*/

View File

@ -187,7 +187,6 @@ internal class MXOlmDevice(
} catch (e: Exception) {
Timber.e(e, "## generateOneTimeKeys() : failed")
}

}

/**

View File

@ -18,7 +18,6 @@ package im.vector.matrix.android.internal.crypto

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import im.vector.matrix.android.internal.di.MoshiProvider

/**
* The type of object we use for importing and exporting megolm session data.
@ -71,10 +70,4 @@ data class MegolmSessionData(
*/
@Json(name = "forwarding_curve25519_key_chain")
var forwardingCurve25519KeyChain: List<String>? = null
) {

fun toJsonString(): String {
return MoshiProvider.providesMoshi().adapter(MegolmSessionData::class.java).toJson(this)
}
}

)

View File

@ -19,9 +19,8 @@ package im.vector.matrix.android.internal.crypto
import im.vector.matrix.android.api.auth.data.Credentials
import java.util.*

internal class ObjectSigner(
private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice) {
internal class ObjectSigner(private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice) {

/**
* Sign Object

View File

@ -28,7 +28,7 @@ import org.matrix.olm.OlmAccount
import timber.log.Timber
import java.util.*

internal class OneTimeKeysManager(
internal class OneTimeKeysUploader(
private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice,
private val mObjectSigner: ObjectSigner,

View File

@ -31,7 +31,7 @@ import im.vector.matrix.android.internal.task.configureWith
import timber.log.Timber
import java.util.*

internal class MXOutgoingRoomKeyRequestManager(
internal class OutgoingRoomKeyRequestManager(
private val mCryptoStore: IMXCryptoStore,
private val mSendToDeviceTask: SendToDeviceTask,
private val mTaskExecutor: TaskExecutor) {

View File

@ -26,7 +26,7 @@ import timber.log.Timber

internal class MegolmSessionDataImporter(private val mOlmDevice: MXOlmDevice,
private val roomDecryptorProvider: RoomDecryptorProvider,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager,
private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mCryptoStore: IMXCryptoStore) {

/**

View File

@ -21,9 +21,9 @@ import im.vector.matrix.android.internal.crypto.keysbackup.KeysBackup
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import timber.log.Timber

internal class SetDeviceVerificationAction(val mCryptoStore: IMXCryptoStore,
val mCredentials: Credentials,
val mKeysBackup: KeysBackup) {
internal class SetDeviceVerificationAction(private val mCryptoStore: IMXCryptoStore,
private val mCredentials: Credentials,
private val mKeysBackup: KeysBackup) {

fun handle(verificationStatus: Int, deviceId: String, userId: String) {
val device = mCryptoStore.getUserDevice(deviceId, userId)

View File

@ -47,7 +47,7 @@ import java.util.*
internal class MXMegolmDecryption(private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice,
private val mDeviceListManager: DeviceListManager,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager,
private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mMessageEncrypter: MessageEncrypter,
private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val mCryptoStore: IMXCryptoStore,
@ -68,12 +68,6 @@ internal class MXMegolmDecryption(private val mCredentials: Credentials,

@Throws(MXDecryptionException::class)
private fun decryptEvent(event: Event, timeline: String, requestKeysOnFail: Boolean): MXEventDecryptionResult? {
// sanity check // TODO Remove check
if (null == event) {
Timber.e("## decryptEvent() : null event")
return null
}

val encryptedEventContent = event.content.toModel<EncryptedEventContent>()!!

if (TextUtils.isEmpty(encryptedEventContent.senderKey) || TextUtils.isEmpty(encryptedEventContent.sessionId) || TextUtils.isEmpty(encryptedEventContent.ciphertext)) {
@ -147,7 +141,7 @@ internal class MXMegolmDecryption(private val mCredentials: Credentials,
val recipients = ArrayList<Map<String, String>>()

val selfMap = HashMap<String, String>()
selfMap["userId"] = mCredentials.userId // TODO Replace this hard coded keys (see MXOutgoingRoomKeyRequestManager)
selfMap["userId"] = mCredentials.userId // TODO Replace this hard coded keys (see OutgoingRoomKeyRequestManager)
selfMap["deviceId"] = "*"
recipients.add(selfMap)


View File

@ -19,7 +19,7 @@ package im.vector.matrix.android.internal.crypto.algorithms.megolm
import im.vector.matrix.android.api.auth.data.Credentials
import im.vector.matrix.android.internal.crypto.DeviceListManager
import im.vector.matrix.android.internal.crypto.MXOlmDevice
import im.vector.matrix.android.internal.crypto.MXOutgoingRoomKeyRequestManager
import im.vector.matrix.android.internal.crypto.OutgoingRoomKeyRequestManager
import im.vector.matrix.android.internal.crypto.actions.EnsureOlmSessionsForDevicesAction
import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
@ -29,7 +29,7 @@ import im.vector.matrix.android.internal.task.TaskExecutor
internal class MXMegolmDecryptionFactory(private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice,
private val mDeviceListManager: DeviceListManager,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager,
private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mMessageEncrypter: MessageEncrypter,
private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val mCryptoStore: IMXCryptoStore,

View File

@ -21,7 +21,7 @@ import im.vector.matrix.android.internal.crypto.model.MXDeviceInfo
import im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap
import timber.log.Timber

class MXOutboundSessionInfo(
internal class MXOutboundSessionInfo(
// The id of the session
val mSessionId: String) {
// When the session was created

View File

@ -29,8 +29,6 @@ import timber.log.Timber;
* This allows additional checks. The class implements NSCoding so that the context can be stored.
*/
public class MXOlmInboundGroupSession implements Serializable {
//
private static final String LOG_TAG = "OlmInboundGroupSession";

// The associated olm inbound group session.
public OlmInboundGroupSession mSession;

View File

@ -14,30 +14,18 @@
* limitations under the License.
*/

package im.vector.matrix.android.internal.crypto.model;
package im.vector.matrix.android.internal.crypto.model

import java.io.Serializable;
import java.io.Serializable

public class MXOlmSessionResult implements Serializable {
/**
* the device
*/
public final MXDeviceInfo mDevice;

/**
* Base64 olm session id.
* null if no session could be established.
*/
public String mSessionId;

/**
* Constructor
*
* @param device the device
* @param sessionId the olm session id
*/
public MXOlmSessionResult(MXDeviceInfo device, String sessionId) {
mDevice = device;
mSessionId = sessionId;
}
}
data class MXOlmSessionResult
(
/**
* the device
*/
val mDevice: MXDeviceInfo,
/**
* Base64 olm session id.
* null if no session could be established.
*/
var mSessionId: String?) : Serializable