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

Commit 552dfd2

Browse files
committed
Merge branch 'release/0.9.36'
2 parents 30b243a + d4a08c3 commit 552dfd2

File tree

10 files changed

+283
-114
lines changed

10 files changed

+283
-114
lines changed

CHANGES.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Changes to Matrix Android SDK in 0.9.36 (2020-09-14)
2+
=======================================================
3+
4+
Improvements:
5+
- Crypto: Only create one olm session at a time per device (vector-im/riot-android#3056).
6+
7+
Bugfix:
8+
- Killed Application misses some notifications
9+
- Fix a crash on the crypto code (race condition?)
10+
111
Changes to Matrix Android SDK in 0.9.35 (2020-05-20)
212
=======================================================
313

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
[![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)
44
[![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)
55

6+
Important Announcement
7+
======================
8+
9+
This SDK is deprecated and the core team does not work anymore on it.
10+
11+
We strongly recommends that new projects use [the new Android Matrix SDK](https://github.com/matrix-org/matrix-android-sdk2).
12+
13+
We can provide best effort support for existing projects that are still using this SDK though.
14+
615
matrix-android-sdk
716
==================
817
The [Matrix] SDK for Android wraps the Matrix REST API calls in asynchronous Java methods and provides basic structures for storing and handling data.

matrix-sdk-crypto/src/main/java/org/matrix/androidsdk/crypto/data/MXOlmSessionResult.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
package org.matrix.androidsdk.crypto.data;
1818

19-
import java.io.Serializable;
20-
21-
public class MXOlmSessionResult implements Serializable {
19+
public class MXOlmSessionResult {
2220
/**
2321
* the device
2422
*/
@@ -30,6 +28,9 @@ public class MXOlmSessionResult implements Serializable {
3028
*/
3129
public String mSessionId;
3230

31+
// True if mSessionId has been retrieved, (even if mSessionId is null)
32+
public Boolean hasResult;
33+
3334
/**
3435
* Constructor
3536
*
@@ -39,5 +40,6 @@ public class MXOlmSessionResult implements Serializable {
3940
public MXOlmSessionResult(MXDeviceInfo device, String sessionId) {
4041
mDevice = device;
4142
mSessionId = sessionId;
43+
hasResult = sessionId != null && !sessionId.isEmpty();
4244
}
4345
}

matrix-sdk-crypto/src/main/java/org/matrix/androidsdk/crypto/internal/MXCryptoImpl.java

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import org.matrix.androidsdk.crypto.interfaces.CryptoRoomState;
7474
import org.matrix.androidsdk.crypto.interfaces.CryptoSession;
7575
import org.matrix.androidsdk.crypto.interfaces.CryptoSyncResponse;
76+
import org.matrix.androidsdk.crypto.internal.otk.OneTimeKeysResponseHandler;
7677
import org.matrix.androidsdk.crypto.keysbackup.KeysBackup;
7778
import org.matrix.androidsdk.crypto.model.crypto.KeysUploadResponse;
7879
import org.matrix.androidsdk.crypto.model.crypto.RoomKeyContent;
@@ -138,6 +139,8 @@ public class MXCryptoImpl implements MXCrypto {
138139

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

142+
private OneTimeKeysResponseHandler oneTimeKeysResponseHandler = new OneTimeKeysResponseHandler(this);
143+
141144
// the encryption is starting
142145
private boolean mIsStarting;
143146

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

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

305308
mSession.getDataHandler().setCryptoEventsListener(mEventListener);
306309

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

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

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

1124-
final String oneTimeKeyAlgorithm = MXKey.KEY_SIGNED_CURVE_25519_TYPE;
1128+
Set<String> deviceIdentityKeysWithOlmSessionsInProgress = oneTimeKeysResponseHandler.getDeviceIdentityKeysWithOlmSessionsInProgress();
11251129

1130+
// Prepare the request for claiming one-time keys
11261131
for (MXDeviceInfo device : devicesWithoutSession) {
1127-
usersDevicesToClaim.setObject(oneTimeKeyAlgorithm, device.userId, device.deviceId);
1132+
String deviceIdentityKey = device.identityKey();
1133+
1134+
// Claim only if a request is not yet pending
1135+
if (!deviceIdentityKeysWithOlmSessionsInProgress.contains(deviceIdentityKey)) {
1136+
usersDevicesToClaim.setObject(MXKey.KEY_SIGNED_CURVE_25519_TYPE, device.userId, device.deviceId);
1137+
}
11281138
}
11291139

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

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

1145+
OneTimeKeysResponseHandler.PendingRequest pendingRequest = new OneTimeKeysResponseHandler.PendingRequest(
1146+
devicesByUser,
1147+
callback,
1148+
results);
1149+
1150+
oneTimeKeysResponseHandler.addPendingRequest(pendingRequest);
1151+
1152+
if (usersDevicesToClaim.getMap().isEmpty()) {
1153+
return;
1154+
}
1155+
11381156
mCryptoRestClient.claimOneTimeKeysForUsersDevices(usersDevicesToClaim, new ApiCallback<MXUsersDevicesMap<MXKey>>() {
11391157
@Override
11401158
public void onSuccess(final MXUsersDevicesMap<MXKey> oneTimeKeys) {
11411159
getEncryptingThreadHandler().post(new Runnable() {
11421160
@Override
11431161
public void run() {
1144-
try {
1145-
Log.d(LOG_TAG, "## claimOneTimeKeysForUsersDevices() : keysClaimResponse.oneTimeKeys: " + oneTimeKeys);
1146-
1147-
Set<String> userIds = devicesByUser.keySet();
1148-
1149-
for (String userId : userIds) {
1150-
List<MXDeviceInfo> deviceInfos = devicesByUser.get(userId);
1151-
1152-
for (MXDeviceInfo deviceInfo : deviceInfos) {
1153-
1154-
MXKey oneTimeKey = null;
1155-
1156-
List<String> deviceIds = oneTimeKeys.getUserDeviceIds(userId);
1157-
1158-
if (null != deviceIds) {
1159-
for (String deviceId : deviceIds) {
1160-
MXOlmSessionResult olmSessionResult = results.getObject(deviceId, userId);
1161-
1162-
if (null != olmSessionResult.mSessionId) {
1163-
// We already have a result for this device
1164-
continue;
1165-
}
1166-
1167-
MXKey key = oneTimeKeys.getObject(deviceId, userId);
1168-
1169-
if (TextUtils.equals(key.type, oneTimeKeyAlgorithm)) {
1170-
oneTimeKey = key;
1171-
}
1172-
1173-
if (null == oneTimeKey) {
1174-
Log.d(LOG_TAG, "## ensureOlmSessionsForDevices() : No one-time keys " + oneTimeKeyAlgorithm
1175-
+ " for device " + userId + " : " + deviceId);
1176-
continue;
1177-
}
1178-
1179-
// Update the result for this device in results
1180-
olmSessionResult.mSessionId = verifyKeyAndStartSession(oneTimeKey, userId, deviceInfo);
1181-
}
1182-
}
1183-
}
1184-
}
1185-
} catch (Exception e) {
1186-
Log.e(LOG_TAG, "## ensureOlmSessionsForDevices() " + e.getMessage(), e);
1187-
}
1188-
1189-
if (!hasBeenReleased()) {
1190-
if (null != callback) {
1191-
getUIHandler().post(new Runnable() {
1192-
@Override
1193-
public void run() {
1194-
callback.onSuccess(results);
1195-
}
1196-
});
1197-
}
1198-
}
1162+
oneTimeKeysResponseHandler.onOtkRetrieved(oneTimeKeys);
11991163
}
12001164
});
12011165
}
@@ -1204,32 +1168,41 @@ public void run() {
12041168
public void onNetworkError(Exception e) {
12051169
Log.e(LOG_TAG, "## ensureOlmSessionsForUsers(): claimOneTimeKeysForUsersDevices request failed" + e.getMessage(), e);
12061170

1207-
if (null != callback) {
1208-
callback.onNetworkError(e);
1209-
}
1171+
getEncryptingThreadHandler().post(new Runnable() {
1172+
@Override
1173+
public void run() {
1174+
oneTimeKeysResponseHandler.onNetworkError(e, usersDevicesToClaim);
1175+
}
1176+
});
12101177
}
12111178

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

1216-
if (null != callback) {
1217-
callback.onMatrixError(e);
1218-
}
1183+
getEncryptingThreadHandler().post(new Runnable() {
1184+
@Override
1185+
public void run() {
1186+
oneTimeKeysResponseHandler.onMatrixError(e, usersDevicesToClaim);
1187+
}
1188+
});
12191189
}
12201190

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

1225-
if (null != callback) {
1226-
callback.onUnexpectedError(e);
1227-
}
1195+
getEncryptingThreadHandler().post(new Runnable() {
1196+
@Override
1197+
public void run() {
1198+
oneTimeKeysResponseHandler.onUnexpectedError(e, usersDevicesToClaim);
1199+
}
1200+
});
12281201
}
12291202
});
12301203
}
12311204

1232-
private String verifyKeyAndStartSession(MXKey oneTimeKey, String userId, MXDeviceInfo deviceInfo) {
1205+
public String verifyKeyAndStartSession(MXKey oneTimeKey, String userId, MXDeviceInfo deviceInfo) {
12331206
String sessionId = null;
12341207

12351208
String deviceId = deviceInfo.deviceId;

0 commit comments

Comments
 (0)