Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Merge branch 'release/0.9.36'
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Sep 14, 2020
2 parents 30b243a + d4a08c3 commit 552dfd2
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 114 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
Changes to Matrix Android SDK in 0.9.36 (2020-09-14)
=======================================================

Improvements:
- Crypto: Only create one olm session at a time per device (vector-im/riot-android#3056).

Bugfix:
- Killed Application misses some notifications
- Fix a crash on the crypto code (race condition?)

Changes to Matrix Android SDK in 0.9.35 (2020-05-20)
=======================================================

Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=matrix.android.sdk&metric=vulnerabilities)](https://sonarcloud.io/project/issues?id=matrix.android.sdk&resolved=false&types=VULNERABILITY)
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=matrix.android.sdk&metric=bugs)](https://sonarcloud.io/project/issues?id=matrix.android.sdk&resolved=false&types=BUG)

Important Announcement
======================

This SDK is deprecated and the core team does not work anymore on it.

We strongly recommends that new projects use [the new Android Matrix SDK](https://github.com/matrix-org/matrix-android-sdk2).

We can provide best effort support for existing projects that are still using this SDK though.

matrix-android-sdk
==================
The [Matrix] SDK for Android wraps the Matrix REST API calls in asynchronous Java methods and provides basic structures for storing and handling data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package org.matrix.androidsdk.crypto.data;

import java.io.Serializable;

public class MXOlmSessionResult implements Serializable {
public class MXOlmSessionResult {
/**
* the device
*/
Expand All @@ -30,6 +28,9 @@ public class MXOlmSessionResult implements Serializable {
*/
public String mSessionId;

// True if mSessionId has been retrieved, (even if mSessionId is null)
public Boolean hasResult;

/**
* Constructor
*
Expand All @@ -39,5 +40,6 @@ public class MXOlmSessionResult implements Serializable {
public MXOlmSessionResult(MXDeviceInfo device, String sessionId) {
mDevice = device;
mSessionId = sessionId;
hasResult = sessionId != null && !sessionId.isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.matrix.androidsdk.crypto.interfaces.CryptoRoomState;
import org.matrix.androidsdk.crypto.interfaces.CryptoSession;
import org.matrix.androidsdk.crypto.interfaces.CryptoSyncResponse;
import org.matrix.androidsdk.crypto.internal.otk.OneTimeKeysResponseHandler;
import org.matrix.androidsdk.crypto.keysbackup.KeysBackup;
import org.matrix.androidsdk.crypto.model.crypto.KeysUploadResponse;
import org.matrix.androidsdk.crypto.model.crypto.RoomKeyContent;
Expand Down Expand Up @@ -138,6 +139,8 @@ public class MXCryptoImpl implements MXCrypto {

private Map<String, Map<String, String>> mLastPublishedOneTimeKeys;

private OneTimeKeysResponseHandler oneTimeKeysResponseHandler = new OneTimeKeysResponseHandler(this);

// the encryption is starting
private boolean mIsStarting;

Expand Down Expand Up @@ -300,7 +303,7 @@ public MXCryptoImpl(@NonNull CryptoSession matrixSession,
mCryptoStore.storeUserDevices(mSession.getMyUserId(), myDevices);

// Create the VerificationManager before setting the CryptoEventsListener, to avoid crash (vector-im/riot-android#3396)
mShortCodeVerificationManager = new VerificationManager(mSession);
mShortCodeVerificationManager = new VerificationManager(mSession, this);

mSession.getDataHandler().setCryptoEventsListener(mEventListener);

Expand Down Expand Up @@ -1107,6 +1110,7 @@ public void ensureOlmSessionsForDevices(final Map<String, List<MXDeviceInfo>> de
}

if (devicesWithoutSession.size() == 0) {
Log.d(LOG_TAG, "[MXCrypto] ensureOlmSessionsForDevices: Have already sessions for all");
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
Expand All @@ -1118,84 +1122,44 @@ public void run() {
return;
}

// Prepare the request for claiming one-time keys
// Devices for which we will make a /claim request
MXUsersDevicesMap<String> usersDevicesToClaim = new MXUsersDevicesMap<>();

final String oneTimeKeyAlgorithm = MXKey.KEY_SIGNED_CURVE_25519_TYPE;
Set<String> deviceIdentityKeysWithOlmSessionsInProgress = oneTimeKeysResponseHandler.getDeviceIdentityKeysWithOlmSessionsInProgress();

// Prepare the request for claiming one-time keys
for (MXDeviceInfo device : devicesWithoutSession) {
usersDevicesToClaim.setObject(oneTimeKeyAlgorithm, device.userId, device.deviceId);
String deviceIdentityKey = device.identityKey();

// Claim only if a request is not yet pending
if (!deviceIdentityKeysWithOlmSessionsInProgress.contains(deviceIdentityKey)) {
usersDevicesToClaim.setObject(MXKey.KEY_SIGNED_CURVE_25519_TYPE, device.userId, device.deviceId);
}
}

// TODO: this has a race condition - if we try to send another message
// while we are claiming a key, we will end up claiming two and setting up
// two sessions.
//
// That should eventually resolve itself, but it's poor form.
Log.d(LOG_TAG, "[MXCrypto] ensureOlmSessionsForDevices: " + usersDevicesToClaim.getMap().size()
+ " out of " + devicesWithoutSession.size() + " sessions to claim one time keys");

Log.d(LOG_TAG, "## claimOneTimeKeysForUsersDevices() : " + usersDevicesToClaim);

OneTimeKeysResponseHandler.PendingRequest pendingRequest = new OneTimeKeysResponseHandler.PendingRequest(
devicesByUser,
callback,
results);

oneTimeKeysResponseHandler.addPendingRequest(pendingRequest);

if (usersDevicesToClaim.getMap().isEmpty()) {
return;
}

mCryptoRestClient.claimOneTimeKeysForUsersDevices(usersDevicesToClaim, new ApiCallback<MXUsersDevicesMap<MXKey>>() {
@Override
public void onSuccess(final MXUsersDevicesMap<MXKey> oneTimeKeys) {
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
try {
Log.d(LOG_TAG, "## claimOneTimeKeysForUsersDevices() : keysClaimResponse.oneTimeKeys: " + oneTimeKeys);

Set<String> userIds = devicesByUser.keySet();

for (String userId : userIds) {
List<MXDeviceInfo> deviceInfos = devicesByUser.get(userId);

for (MXDeviceInfo deviceInfo : deviceInfos) {

MXKey oneTimeKey = null;

List<String> deviceIds = oneTimeKeys.getUserDeviceIds(userId);

if (null != deviceIds) {
for (String deviceId : deviceIds) {
MXOlmSessionResult olmSessionResult = results.getObject(deviceId, userId);

if (null != olmSessionResult.mSessionId) {
// We already have a result for this device
continue;
}

MXKey key = oneTimeKeys.getObject(deviceId, userId);

if (TextUtils.equals(key.type, oneTimeKeyAlgorithm)) {
oneTimeKey = key;
}

if (null == oneTimeKey) {
Log.d(LOG_TAG, "## ensureOlmSessionsForDevices() : No one-time keys " + oneTimeKeyAlgorithm
+ " for device " + userId + " : " + deviceId);
continue;
}

// Update the result for this device in results
olmSessionResult.mSessionId = verifyKeyAndStartSession(oneTimeKey, userId, deviceInfo);
}
}
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "## ensureOlmSessionsForDevices() " + e.getMessage(), e);
}

if (!hasBeenReleased()) {
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
public void run() {
callback.onSuccess(results);
}
});
}
}
oneTimeKeysResponseHandler.onOtkRetrieved(oneTimeKeys);
}
});
}
Expand All @@ -1204,32 +1168,41 @@ public void run() {
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "## ensureOlmSessionsForUsers(): claimOneTimeKeysForUsersDevices request failed" + e.getMessage(), e);

if (null != callback) {
callback.onNetworkError(e);
}
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
oneTimeKeysResponseHandler.onNetworkError(e, usersDevicesToClaim);
}
});
}

@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "## ensureOlmSessionsForUsers(): claimOneTimeKeysForUsersDevices request failed" + e.getMessage());

if (null != callback) {
callback.onMatrixError(e);
}
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
oneTimeKeysResponseHandler.onMatrixError(e, usersDevicesToClaim);
}
});
}

@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "## ensureOlmSessionsForUsers(): claimOneTimeKeysForUsersDevices request failed" + e.getMessage(), e);

if (null != callback) {
callback.onUnexpectedError(e);
}
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
oneTimeKeysResponseHandler.onUnexpectedError(e, usersDevicesToClaim);
}
});
}
});
}

private String verifyKeyAndStartSession(MXKey oneTimeKey, String userId, MXDeviceInfo deviceInfo) {
public String verifyKeyAndStartSession(MXKey oneTimeKey, String userId, MXDeviceInfo deviceInfo) {
String sessionId = null;

String deviceId = deviceInfo.deviceId;
Expand Down
Loading

0 comments on commit 552dfd2

Please sign in to comment.