use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test15_testReplayAttack.
@Test
public void test15_testReplayAttack() throws Exception {
Log.e(LOG_TAG, "test15_testReplayAttack");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
bobSession.getCrypto().setWarnOnUnknownDevices(false);
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
String messageFromAlice = "Hello I'm Alice!";
final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(2);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), bobSession.getMyUserId())) {
results.put("bobEcho", event);
event.setClearData(null);
bobSession.getDataHandler().decryptEvent(event, roomFromBobPOV.getTimeline().getTimelineId());
results.put("decrypted", event);
lock1.countDown();
}
}
};
roomFromBobPOV.addEventListener(bobEventListener);
bobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
lock1.countDown();
}
});
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onToDeviceEvent"));
Assert.assertTrue(results.containsKey("bobEcho"));
Assert.assertTrue(results.containsKey("decrypted"));
Event decryptedEvent = (Event) results.get("decrypted");
Assert.assertNull(decryptedEvent.getClearEvent());
Assert.assertEquals(MXCryptoError.DUPLICATED_MESSAGE_INDEX_ERROR_CODE, decryptedEvent.getCryptoError().errcode);
// Decrypting it with no replay attack mitigation must still work
bobSession.getDataHandler().decryptEvent(decryptedEvent, null);
mCryptoTestHelper.checkEncryptedEvent(decryptedEvent, aliceRoomId, messageFromAlice, aliceSession);
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test08_testAliceAndBobInAEncryptedRoom2.
@Test
public void test08_testAliceAndBobInAEncryptedRoom2() throws Exception {
Log.e(LOG_TAG, "test08_testAliceAndBobInAEncryptedRoom2");
Context context = InstrumentationRegistry.getContext();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
bobSession.getCrypto().setWarnOnUnknownDevices(false);
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
final int[] nbReceivedMessagesFromAlice = { 0 };
final int[] nbReceivedMessagesFromBob = { 0 };
final List<CountDownLatch> list = new ArrayList<>();
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), bobSession.getMyUserId())) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, mCryptoTestHelper.getMessagesFromAlice().get(nbReceivedMessagesFromAlice[0]), aliceSession);
nbReceivedMessagesFromAlice[0]++;
list.get(list.size() - 1).countDown();
}
}
};
MXEventListener aliceEventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE) && !TextUtils.equals(event.getSender(), aliceSession.getMyUserId())) {
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, mCryptoTestHelper.getMessagesFromBob().get(nbReceivedMessagesFromBob[0]), bobSession);
nbReceivedMessagesFromBob[0]++;
list.get(list.size() - 1).countDown();
}
}
};
ApiCallback<Void> callback = new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignored
}
};
roomFromBobPOV.addEventListener(bobEventListener);
roomFromAlicePOV.addEventListener(aliceEventListener);
list.add(new CountDownLatch(2));
final Map<String, Object> results = new HashMap<>();
bobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
list.get(0).countDown();
}
});
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(mCryptoTestHelper.getMessagesFromAlice().get(nbReceivedMessagesFromAlice[0]), aliceSession, aliceRoomId), callback);
mTestHelper.await(list.get(list.size() - 1));
Assert.assertTrue(results.containsKey("onToDeviceEvent"));
Assert.assertEquals(1, nbReceivedMessagesFromAlice[0]);
list.add(new CountDownLatch(1));
roomFromBobPOV.sendEvent(mCryptoTestHelper.buildTextEvent(mCryptoTestHelper.getMessagesFromBob().get(nbReceivedMessagesFromBob[0]), bobSession, aliceRoomId), callback);
mTestHelper.await(list.get(list.size() - 1));
Assert.assertEquals(1, nbReceivedMessagesFromBob[0]);
list.add(new CountDownLatch(1));
roomFromBobPOV.sendEvent(mCryptoTestHelper.buildTextEvent(mCryptoTestHelper.getMessagesFromBob().get(nbReceivedMessagesFromBob[0]), bobSession, aliceRoomId), callback);
mTestHelper.await(list.get(list.size() - 1));
Assert.assertEquals(2, nbReceivedMessagesFromBob[0]);
list.add(new CountDownLatch(1));
roomFromBobPOV.sendEvent(mCryptoTestHelper.buildTextEvent(mCryptoTestHelper.getMessagesFromBob().get(nbReceivedMessagesFromBob[0]), bobSession, aliceRoomId), callback);
mTestHelper.await(list.get(list.size() - 1));
Assert.assertEquals(3, nbReceivedMessagesFromBob[0]);
list.add(new CountDownLatch(1));
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(mCryptoTestHelper.getMessagesFromAlice().get(nbReceivedMessagesFromAlice[0]), aliceSession, aliceRoomId), callback);
mTestHelper.await(list.get(list.size() - 1));
Assert.assertEquals(2, nbReceivedMessagesFromAlice[0]);
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test20_testAliceAndBlockedBob.
@Test
public void test20_testAliceAndBlockedBob() throws Exception {
Log.e(LOG_TAG, "test20_testAliceAndBlockedBob");
Context context = InstrumentationRegistry.getContext();
final Map<String, String> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
bobSession.getCrypto().setWarnOnUnknownDevices(false);
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(1);
final List<Event> receivedEvents = new ArrayList<>();
EventTimeline.Listener eventTimelineListener = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents.add(event);
lock1.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener);
String aliceMessage1 = "Hello I'm Alice!";
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(aliceMessage1, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock1);
Assert.assertEquals(1, receivedEvents.size());
Event event = receivedEvents.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, aliceMessage1, aliceSession);
// block the bob's device
CountDownLatch lock1b = new CountDownLatch(1);
aliceSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, bobSession.getCredentials().deviceId, bobSession.getMyUserId(), new TestApiCallback<Void>(lock1b) {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification20", "setDeviceVerification20");
super.onSuccess(info);
}
});
mTestHelper.await(lock1b);
Assert.assertTrue(results.containsKey("setDeviceVerification20"));
// /
final CountDownLatch lock2 = new CountDownLatch(1);
final List<Event> receivedEvents2 = new ArrayList<>();
EventTimeline.Listener eventTimelineListener2 = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE_ENCRYPTED)) {
receivedEvents2.add(event);
lock2.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener2);
String aliceMessage2 = "Hello I'm still Alice!";
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(aliceMessage2, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock2);
Assert.assertEquals(1, receivedEvents2.size());
event = receivedEvents2.get(0);
Assert.assertNull(event.getClearEvent());
Assert.assertNotNull(event.getCryptoError());
Assert.assertEquals(MXCryptoError.UNKNOWN_INBOUND_SESSION_ID_ERROR_CODE, event.getCryptoError().errcode);
// unblock the bob's device
CountDownLatch lock2b = new CountDownLatch(1);
aliceSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_UNVERIFIED, bobSession.getCredentials().deviceId, bobSession.getMyUserId(), new TestApiCallback<Void>(lock2b) {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification40", "setDeviceVerification40");
super.onSuccess(info);
}
});
mTestHelper.await(lock2b);
Assert.assertTrue(results.containsKey("setDeviceVerification40"));
// /
final CountDownLatch lock3 = new CountDownLatch(1);
final List<Event> receivedEvents3 = new ArrayList<>();
EventTimeline.Listener eventTimelineListener3 = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents3.add(event);
lock3.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener3);
String aliceMessage3 = "Hello I'm still Alice and you can read this!";
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(aliceMessage3, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock3);
Assert.assertEquals(1, receivedEvents3.size());
event = receivedEvents3.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, aliceMessage3, aliceSession);
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test06_testAliceInAEncryptedRoom.
@Test
public void test06_testAliceInAEncryptedRoom() throws Exception {
Log.e(LOG_TAG, "test06_testAliceInAEncryptedRoom");
Context context = InstrumentationRegistry.getContext();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceInARoom();
MXSession aliceSession = cryptoTestData.getFirstSession();
String aliceRoomId = cryptoTestData.getRoomId();
final String message = "Hello myself!";
Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
CountDownLatch lock1 = new CountDownLatch(1);
// the IOS client echoes the message
// the android client does not
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(message, aliceSession, aliceRoomId), new TestApiCallback<Void>(lock1));
mTestHelper.await(lock1);
cryptoTestData.clear(context);
}
use of org.matrix.androidsdk.MXSession in project matrix-android-sdk by matrix-org.
the class CryptoTest method test16_testRoomKeyReshare.
@Test
public void test16_testRoomKeyReshare() throws Exception {
Log.e(LOG_TAG, "test16_testRoomKeyReshare");
Context context = InstrumentationRegistry.getContext();
final Map<String, Object> results = new HashMap<>();
CryptoTestData cryptoTestData = mCryptoTestHelper.doE2ETestWithAliceAndBobInARoom(true);
final MXSession aliceSession = cryptoTestData.getFirstSession();
final String aliceRoomId = cryptoTestData.getRoomId();
final MXSession bobSession = cryptoTestData.getSecondSession();
bobSession.getCrypto().setWarnOnUnknownDevices(false);
aliceSession.getCrypto().setWarnOnUnknownDevices(false);
String messageFromAlice = "Hello I'm Alice!";
final Room roomFromBobPOV = bobSession.getDataHandler().getRoom(aliceRoomId);
final Room roomFromAlicePOV = aliceSession.getDataHandler().getRoom(aliceRoomId);
Assert.assertTrue(roomFromBobPOV.isEncrypted());
Assert.assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(2);
MXEventListener bobEventListener = new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
if (!results.containsKey("onToDeviceEvent")) {
results.put("onToDeviceEvent", event);
lock1.countDown();
}
}
};
bobSession.getDataHandler().addListener(bobEventListener);
final List<Event> receivedEvents = new ArrayList<>();
EventTimeline.Listener eventTimelineListener = new EventTimeline.Listener() {
public void onEvent(Event event, EventTimeline.Direction direction, RoomState roomState) {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
receivedEvents.add(event);
lock1.countDown();
}
}
};
roomFromBobPOV.getTimeline().addEventTimelineListener(eventTimelineListener);
roomFromAlicePOV.sendEvent(mCryptoTestHelper.buildTextEvent(messageFromAlice, aliceSession, aliceRoomId), new SimpleApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
// Ignore
}
});
mTestHelper.await(lock1);
Assert.assertTrue(results.containsKey("onToDeviceEvent"));
Assert.assertEquals(1, receivedEvents.size());
Event event = receivedEvents.get(0);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, messageFromAlice, aliceSession);
// Reinject a modified version of the received room_key event from Alice.
// From Bob pov, that mimics Alice resharing her keys but with an advanced outbound group session.
Event toDeviceEvent = (Event) results.get("onToDeviceEvent");
String sessionId = toDeviceEvent.getContentAsJsonObject().get("session_id").getAsString();
String newSessionKey = aliceSession.getCrypto().getOlmDevice().getSessionKey(sessionId);
JsonObject content = toDeviceEvent.getClearEvent().getContentAsJsonObject();
content.add("session_key", new JsonPrimitive(newSessionKey));
bobSession.getDataHandler().onToDeviceEvent(toDeviceEvent);
// We still must be able to decrypt the event
// ie, the implementation must have ignored the new room key with the advanced outbound group
// session key
event.setClearData(null);
bobSession.getDataHandler().decryptEvent(event, null);
mCryptoTestHelper.checkEncryptedEvent(event, aliceRoomId, messageFromAlice, aliceSession);
cryptoTestData.clear(context);
}
Aggregations