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 mObjectSigner: ObjectSigner,
// //
private val mOneTimeKeysManager: OneTimeKeysManager, private val mOneTimeKeysUploader: OneTimeKeysUploader,
// //
private val roomDecryptorProvider: RoomDecryptorProvider, private val roomDecryptorProvider: RoomDecryptorProvider,
// The SAS verification service. // The SAS verification service.
@ -96,7 +96,7 @@ internal class CryptoManager(
// //
private val mIncomingRoomKeyRequestManager: IncomingRoomKeyRequestManager, private val mIncomingRoomKeyRequestManager: IncomingRoomKeyRequestManager,
// //
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager, private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
// Room service // Room service
private val mRoomService: RoomService, private val mRoomService: RoomService,
// Olm Manager // Olm Manager
@ -262,10 +262,10 @@ internal class CryptoManager(
Timber.d(" - device id : " + mCredentials.deviceId) Timber.d(" - device id : " + mCredentials.deviceId)
Timber.d(" - ed25519 : " + mOlmDevice.deviceEd25519Key) Timber.d(" - ed25519 : " + mOlmDevice.deviceEd25519Key)
Timber.d(" - curve25519 : " + mOlmDevice.deviceCurve25519Key) Timber.d(" - curve25519 : " + mOlmDevice.deviceCurve25519Key)
Timber.d(" - oneTimeKeys: " + mOneTimeKeysManager.mLastPublishedOneTimeKeys) Timber.d(" - oneTimeKeys: " + mOneTimeKeysUploader.mLastPublishedOneTimeKeys)
Timber.d("") Timber.d("")
mOneTimeKeysManager.maybeUploadOneTimeKeys(object : MatrixCallback<Unit> { mOneTimeKeysUploader.maybeUploadOneTimeKeys(object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) { override fun onSuccess(data: Unit) {
// TODO // TODO
//if (null != mNetworkConnectivityReceiver) { //if (null != mNetworkConnectivityReceiver) {
@ -353,7 +353,7 @@ internal class CryptoManager(
if (null != syncResponse.deviceOneTimeKeysCount) { if (null != syncResponse.deviceOneTimeKeysCount) {
val currentCount = syncResponse.deviceOneTimeKeysCount.signedCurve25519 ?: 0 val currentCount = syncResponse.deviceOneTimeKeysCount.signedCurve25519 ?: 0
mOneTimeKeysManager.updateOneTimeKeyCount(currentCount) mOneTimeKeysUploader.updateOneTimeKeyCount(currentCount)
} }
if (isStarted()) { if (isStarted()) {
@ -362,7 +362,7 @@ internal class CryptoManager(
} }
if (!isCatchingUp && isStarted()) { if (!isCatchingUp && isStarted()) {
mOneTimeKeysManager.maybeUploadOneTimeKeys() mOneTimeKeysUploader.maybeUploadOneTimeKeys()
mIncomingRoomKeyRequestManager.processReceivedRoomKeyRequests() mIncomingRoomKeyRequestManager.processReceivedRoomKeyRequests()
} }
@ -865,8 +865,8 @@ internal class CryptoManager(
val encryptedRoomKeys: ByteArray val encryptedRoomKeys: ByteArray
try { try {
val moshi = MoshiProvider.providesMoshi() val adapter = MoshiProvider.providesMoshi()
val adapter = moshi.adapter(List::class.java) .adapter(List::class.java)
encryptedRoomKeys = MXMegolmExportEncryption encryptedRoomKeys = MXMegolmExportEncryption
.encryptMegolmKeyFile(adapter.toJson(exportedSessions), password, iterationCount) .encryptMegolmKeyFile(adapter.toJson(exportedSessions), password, iterationCount)
@ -909,9 +909,9 @@ internal class CryptoManager(
Timber.d("## importRoomKeys : decryptMegolmKeyFile done in " + (t1 - t0) + " ms") Timber.d("## importRoomKeys : decryptMegolmKeyFile done in " + (t1 - t0) + " ms")
try { try {
val moshi = MoshiProvider.providesMoshi() val list = MoshiProvider.providesMoshi()
val adapter = moshi.adapter(List::class.java) .adapter(List::class.java)
val list = adapter.fromJson(roomKeys) .fromJson(roomKeys)
importedSessions = list as List<MegolmSessionData> importedSessions = list as List<MegolmSessionData>
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "## importRoomKeys failed") Timber.e(e, "## importRoomKeys failed")
@ -949,7 +949,7 @@ internal class CryptoManager(
override fun onSuccess(data: MXUsersDevicesMap<MXDeviceInfo>) { override fun onSuccess(data: MXUsersDevicesMap<MXDeviceInfo>) {
val unknownDevices = getUnknownDevices(data) val unknownDevices = getUnknownDevices(data)
if (unknownDevices.map.size == 0) { if (unknownDevices.map.isEmpty()) {
callback.onSuccess(Unit) callback.onSuccess(Unit)
} else { } else {
// trigger an an unknown devices exception // trigger an an unknown devices exception
@ -1100,6 +1100,30 @@ internal class CryptoManager(
mIncomingRoomKeyRequestManager.removeRoomKeysRequestListener(listener) 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 * DEBUG INFO
* ========================================================================================== */ * ========================================================================================== */
@ -1108,30 +1132,4 @@ internal class CryptoManager(
return "CryptoManager of " + mCredentials.userId + " (" + mCredentials.deviceId + ")" 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) { scope(DefaultSession.SCOPE) {
MXOutgoingRoomKeyRequestManager(get(), get(), get()) OutgoingRoomKeyRequestManager(get(), get(), get())
} }
scope(DefaultSession.SCOPE) { scope(DefaultSession.SCOPE) {
@ -108,9 +108,9 @@ internal class CryptoModule {
ObjectSigner(get(), get()) ObjectSigner(get(), get())
} }
// OneTimeKeysManager // OneTimeKeysUploader
scope(DefaultSession.SCOPE) { scope(DefaultSession.SCOPE) {
OneTimeKeysManager(get(), get(), get(), get(), get()) OneTimeKeysUploader(get(), get(), get(), get(), get())
} }
// Actions // Actions

View file

@ -62,7 +62,7 @@ internal class DeviceListManager(private val mCryptoStore: IMXCryptoStore,
* @param callback the asynchronous callback * @param callback the asynchronous callback
*/ */
internal inner class DownloadKeysPromise(userIds: List<String>, internal inner class DownloadKeysPromise(userIds: List<String>,
val mCallback: MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>>?) { val callback: MatrixCallback<MXUsersDevicesMap<MXDeviceInfo>>?) {
// list of remain pending device keys // list of remain pending device keys
val mPendingUserIdsList: MutableList<String> 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) { if (null != callback) {
CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(usersDevicesInfoMap) } CryptoAsyncHelper.getUiHandler().post { callback.onSuccess(usersDevicesInfoMap) }

View file

@ -17,6 +17,7 @@
package im.vector.matrix.android.internal.crypto package im.vector.matrix.android.internal.crypto
// TODO Update comment
internal object MXCryptoAlgorithms { internal object MXCryptoAlgorithms {
/** /**

View file

@ -18,19 +18,15 @@ package im.vector.matrix.android.internal.crypto
import android.text.TextUtils import android.text.TextUtils
import android.util.Base64 import android.util.Base64
import timber.log.Timber
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.nio.charset.Charset
import java.security.SecureRandom import java.security.SecureRandom
import java.util.Arrays import java.util.*
import javax.crypto.Cipher import javax.crypto.Cipher
import javax.crypto.Mac import javax.crypto.Mac
import javax.crypto.SecretKey
import javax.crypto.spec.IvParameterSpec import javax.crypto.spec.IvParameterSpec
import javax.crypto.spec.SecretKeySpec import javax.crypto.spec.SecretKeySpec
import timber.log.Timber
import java.nio.charset.Charset
import kotlin.experimental.and import kotlin.experimental.and
import kotlin.experimental.xor import kotlin.experimental.xor
@ -363,11 +359,3 @@ object MXMegolmExportEncryption {
return key 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) { } catch (e: Exception) {
Timber.e(e, "## generateOneTimeKeys() : failed") 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.Json
import com.squareup.moshi.JsonClass 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. * 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") @Json(name = "forwarding_curve25519_key_chain")
var forwardingCurve25519KeyChain: List<String>? = null 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 im.vector.matrix.android.api.auth.data.Credentials
import java.util.* import java.util.*
internal class ObjectSigner( internal class ObjectSigner(private val mCredentials: Credentials,
private val mCredentials: Credentials, private val mOlmDevice: MXOlmDevice) {
private val mOlmDevice: MXOlmDevice) {
/** /**
* Sign Object * Sign Object

View file

@ -28,7 +28,7 @@ import org.matrix.olm.OlmAccount
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
internal class OneTimeKeysManager( internal class OneTimeKeysUploader(
private val mCredentials: Credentials, private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice, private val mOlmDevice: MXOlmDevice,
private val mObjectSigner: ObjectSigner, private val mObjectSigner: ObjectSigner,

View file

@ -31,7 +31,7 @@ import im.vector.matrix.android.internal.task.configureWith
import timber.log.Timber import timber.log.Timber
import java.util.* import java.util.*
internal class MXOutgoingRoomKeyRequestManager( internal class OutgoingRoomKeyRequestManager(
private val mCryptoStore: IMXCryptoStore, private val mCryptoStore: IMXCryptoStore,
private val mSendToDeviceTask: SendToDeviceTask, private val mSendToDeviceTask: SendToDeviceTask,
private val mTaskExecutor: TaskExecutor) { private val mTaskExecutor: TaskExecutor) {

View file

@ -26,7 +26,7 @@ import timber.log.Timber
internal class MegolmSessionDataImporter(private val mOlmDevice: MXOlmDevice, internal class MegolmSessionDataImporter(private val mOlmDevice: MXOlmDevice,
private val roomDecryptorProvider: RoomDecryptorProvider, private val roomDecryptorProvider: RoomDecryptorProvider,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager, private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mCryptoStore: IMXCryptoStore) { 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 im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import timber.log.Timber import timber.log.Timber
internal class SetDeviceVerificationAction(val mCryptoStore: IMXCryptoStore, internal class SetDeviceVerificationAction(private val mCryptoStore: IMXCryptoStore,
val mCredentials: Credentials, private val mCredentials: Credentials,
val mKeysBackup: KeysBackup) { private val mKeysBackup: KeysBackup) {
fun handle(verificationStatus: Int, deviceId: String, userId: String) { fun handle(verificationStatus: Int, deviceId: String, userId: String) {
val device = mCryptoStore.getUserDevice(deviceId, userId) val device = mCryptoStore.getUserDevice(deviceId, userId)

View file

@ -47,7 +47,7 @@ import java.util.*
internal class MXMegolmDecryption(private val mCredentials: Credentials, internal class MXMegolmDecryption(private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice, private val mOlmDevice: MXOlmDevice,
private val mDeviceListManager: DeviceListManager, private val mDeviceListManager: DeviceListManager,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager, private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mMessageEncrypter: MessageEncrypter, private val mMessageEncrypter: MessageEncrypter,
private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val mCryptoStore: IMXCryptoStore, private val mCryptoStore: IMXCryptoStore,
@ -68,12 +68,6 @@ internal class MXMegolmDecryption(private val mCredentials: Credentials,
@Throws(MXDecryptionException::class) @Throws(MXDecryptionException::class)
private fun decryptEvent(event: Event, timeline: String, requestKeysOnFail: Boolean): MXEventDecryptionResult? { 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>()!! val encryptedEventContent = event.content.toModel<EncryptedEventContent>()!!
if (TextUtils.isEmpty(encryptedEventContent.senderKey) || TextUtils.isEmpty(encryptedEventContent.sessionId) || TextUtils.isEmpty(encryptedEventContent.ciphertext)) { 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 recipients = ArrayList<Map<String, String>>()
val selfMap = HashMap<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"] = "*" selfMap["deviceId"] = "*"
recipients.add(selfMap) 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.api.auth.data.Credentials
import im.vector.matrix.android.internal.crypto.DeviceListManager import im.vector.matrix.android.internal.crypto.DeviceListManager
import im.vector.matrix.android.internal.crypto.MXOlmDevice 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.EnsureOlmSessionsForDevicesAction
import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter import im.vector.matrix.android.internal.crypto.actions.MessageEncrypter
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore 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, internal class MXMegolmDecryptionFactory(private val mCredentials: Credentials,
private val mOlmDevice: MXOlmDevice, private val mOlmDevice: MXOlmDevice,
private val mDeviceListManager: DeviceListManager, private val mDeviceListManager: DeviceListManager,
private val mOutgoingRoomKeyRequestManager: MXOutgoingRoomKeyRequestManager, private val mOutgoingRoomKeyRequestManager: OutgoingRoomKeyRequestManager,
private val mMessageEncrypter: MessageEncrypter, private val mMessageEncrypter: MessageEncrypter,
private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction, private val mEnsureOlmSessionsForDevicesAction: EnsureOlmSessionsForDevicesAction,
private val mCryptoStore: IMXCryptoStore, 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 im.vector.matrix.android.internal.crypto.model.MXUsersDevicesMap
import timber.log.Timber import timber.log.Timber
class MXOutboundSessionInfo( internal class MXOutboundSessionInfo(
// The id of the session // The id of the session
val mSessionId: String) { val mSessionId: String) {
// When the session was created // 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. * This allows additional checks. The class implements NSCoding so that the context can be stored.
*/ */
public class MXOlmInboundGroupSession implements Serializable { public class MXOlmInboundGroupSession implements Serializable {
//
private static final String LOG_TAG = "OlmInboundGroupSession";
// The associated olm inbound group session. // The associated olm inbound group session.
public OlmInboundGroupSession mSession; public OlmInboundGroupSession mSession;

View file

@ -14,30 +14,18 @@
* limitations under the License. * 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 { data class MXOlmSessionResult
/** (
* the device /**
*/ * the device
public final MXDeviceInfo mDevice; */
val mDevice: MXDeviceInfo,
/** /**
* Base64 olm session id. * Base64 olm session id.
* null if no session could be established. * null if no session could be established.
*/ */
public String mSessionId; var mSessionId: String?) : Serializable
/**
* Constructor
*
* @param device the device
* @param sessionId the olm session id
*/
public MXOlmSessionResult(MXDeviceInfo device, String sessionId) {
mDevice = device;
mSessionId = sessionId;
}
}