use of org.matrix.androidsdk.crypto.data.MXDeviceInfo in project matrix-android-sdk by matrix-org.
the class CryptoTest method test07_testAliceAndBobInACryptedRoom.
@Test
public void test07_testAliceAndBobInACryptedRoom() throws Exception {
Log.e(LOG_TAG, "test07_testAliceAndBobInACryptedRoom");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(true);
final String messageFromAlice = "Hello I'm Alice!";
Room roomFromBobPOV = mBobSession.getDataHandler().getRoom(mRoomId);
Room roomFromAlicePOV = mAliceSession.getDataHandler().getRoom(mRoomId);
assertTrue(roomFromBobPOV.isEncrypted());
assertTrue(roomFromAlicePOV.isEncrypted());
final CountDownLatch lock1 = new CountDownLatch(1);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
lock1.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock1.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
results.put("sendEventError", e);
lock1.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock1.countDown();
}
});
lock1.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("sendEventError"));
MXCryptoError error = (MXCryptoError) results.get("sendEventError");
assertTrue(TextUtils.equals(error.errcode, MXCryptoError.UNKNOWN_DEVICES_CODE));
MXUsersDevicesMap<MXDeviceInfo> unknownDevices = (MXUsersDevicesMap<MXDeviceInfo>) error.mExceptionData;
List<String> deviceInfos = unknownDevices.getUserDeviceIds(mBobSession.getMyUserId());
assertTrue(1 == deviceInfos.size());
assertTrue(TextUtils.equals(deviceInfos.get(0), mBobSession.getCrypto().getMyDevice().deviceId));
final CountDownLatch lock2 = new CountDownLatch(1);
mAliceSession.getCrypto().setDevicesKnown(Arrays.asList(mBobSession.getCrypto().getMyDevice()), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDevicesKnown", "setDevicesKnown");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDevicesKnown"));
final CountDownLatch lock3 = new CountDownLatch(3);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onLiveEvent(Event event, RoomState roomState) {
try {
if (TextUtils.equals(event.getType(), Event.EVENT_TYPE_MESSAGE)) {
if (checkEncryptedEvent(event, mRoomId, messageFromAlice, mAliceSession)) {
results.put("onLiveEvent", "onLiveEvent");
lock3.countDown();
}
}
} catch (Exception e) {
}
}
};
mBobSession.getDataHandler().addListener(new MXEventListener() {
@Override
public void onToDeviceEvent(Event event) {
results.put("onToDeviceEvent", event);
lock3.countDown();
}
});
roomFromBobPOV.addEventListener(eventListener);
roomFromAlicePOV.sendEvent(buildTextEvent(messageFromAlice, mAliceSession), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
lock3.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3.countDown();
}
});
lock3.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onToDeviceEvent"));
assertTrue(results.containsKey("onLiveEvent"));
assertTrue(mBobSession.getCrypto().getDeviceTrackingStatus(mBobSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mBobSession.getCrypto().getDeviceTrackingStatus(mAliceSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mAliceSession.getCrypto().getDeviceTrackingStatus(mBobSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
assertTrue(mAliceSession.getCrypto().getDeviceTrackingStatus(mAliceSession.getMyUserId()) == MXDeviceList.TRACKING_STATUS_UP_TO_DATE);
mBobSession.clear(context);
}
use of org.matrix.androidsdk.crypto.data.MXDeviceInfo in project matrix-android-sdk by matrix-org.
the class CryptoTest method test03_testKeysUploadAndDownload.
@Test
public void test03_testKeysUploadAndDownload() throws Exception {
Log.e(LOG_TAG, "test03_testKeysUploadAndDownload");
Context context = InstrumentationRegistry.getContext();
final HashMap<String, Object> results = new HashMap<>();
createAliceAccount();
mAliceSession.getCredentials().deviceId = "AliceDevice";
final CountDownLatch lock0 = new CountDownLatch(1);
mAliceSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto", "enableCrypto");
lock0.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock0.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock0.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock0.countDown();
}
});
lock0.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("enableCrypto"));
createBobAccount();
final CountDownLatch lock2 = new CountDownLatch(1);
mBobSession.getCredentials().deviceId = "BobDevice";
mBobSession.enableCrypto(true, new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("enableCrypto2", "enableCrypto2");
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("enableCrypto2"));
final CountDownLatch lock3 = new CountDownLatch(1);
mBobSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId(), mAliceSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys", info);
lock3.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3.countDown();
}
});
lock3.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys"));
MXUsersDevicesMap<MXDeviceInfo> usersDevicesInfoMap = (MXUsersDevicesMap<MXDeviceInfo>) results.get("downloadKeys");
assertTrue(2 == usersDevicesInfoMap.getUserIds().size());
assertTrue(1 == usersDevicesInfoMap.getUserDeviceIds(mAliceSession.getMyUserId()).size());
MXDeviceInfo aliceDeviceFromBobPOV = usersDevicesInfoMap.getObject("AliceDevice", mAliceSession.getMyUserId());
assertTrue(null != aliceDeviceFromBobPOV);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
// Continue testing other methods
assertTrue(null != mBobSession.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM));
assertTrue(aliceDeviceFromBobPOV.isUnknown());
final CountDownLatch lock3a = new CountDownLatch(1);
mBobSession.getCrypto().setDevicesKnown(Arrays.asList(aliceDeviceFromBobPOV), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDevicesKnown", info);
lock3a.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3a.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3a.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3a.countDown();
}
});
lock3a.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDevicesKnown"));
assertTrue(aliceDeviceFromBobPOV.isUnverified());
final CountDownLatch lock3b = new CountDownLatch(1);
mBobSession.getCrypto().setDeviceVerification(MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, aliceDeviceFromBobPOV.deviceId, mAliceSession.getMyUserId(), new ApiCallback<Void>() {
@Override
public void onSuccess(Void info) {
results.put("setDeviceVerification1", info);
lock3b.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock3b.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock3b.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock3b.countDown();
}
});
lock3b.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("setDeviceVerification1"));
assertTrue(aliceDeviceFromBobPOV.isBlocked());
Credentials bobCredentials = mBobSession.getCredentials();
Uri uri = Uri.parse(CryptoTestHelper.TESTS_HOME_SERVER_URL);
HomeServerConnectionConfig hs = new HomeServerConnectionConfig(uri);
hs.setCredentials(bobCredentials);
IMXStore store = new MXFileStore(hs, context);
MXSession bobSession2 = new MXSession(hs, new MXDataHandler(store, bobCredentials), context);
final CountDownLatch lock4 = new CountDownLatch(1);
MXStoreListener listener = new MXStoreListener() {
@Override
public void postProcess(String accountId) {
}
@Override
public void onStoreReady(String accountId) {
results.put("onStoreReady", "onStoreReady");
lock4.countDown();
}
@Override
public void onStoreCorrupted(String accountId, String description) {
lock4.countDown();
}
@Override
public void onStoreOOM(String accountId, String description) {
lock4.countDown();
}
};
bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
bobSession2.getDataHandler().getStore().open();
lock4.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onStoreReady"));
final CountDownLatch lock4b = new CountDownLatch(2);
MXEventListener eventListener = new MXEventListener() {
@Override
public void onInitialSyncComplete(String toToken) {
results.put("onInitialSyncComplete", "onInitialSyncComplete");
lock4b.countDown();
}
@Override
public void onCryptoSyncComplete() {
results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
lock4b.countDown();
}
};
bobSession2.getDataHandler().addListener(eventListener);
bobSession2.startEventStream(null);
lock4b.await(2000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("onInitialSyncComplete"));
assertTrue(results.containsKey("onCryptoSyncComplete"));
MXDeviceInfo aliceDeviceFromBobPOV2 = bobSession2.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM);
assertTrue(null != aliceDeviceFromBobPOV2);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV2.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
assertTrue(aliceDeviceFromBobPOV2.mVerified + " instead of " + MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED, aliceDeviceFromBobPOV2.mVerified == MXDeviceInfo.DEVICE_VERIFICATION_BLOCKED);
// Download again alice device
final CountDownLatch lock5 = new CountDownLatch(1);
bobSession2.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mAliceSession.getMyUserId()), true, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys2", info);
lock5.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock5.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock5.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock5.countDown();
}
});
lock5.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys2"));
MXDeviceInfo aliceDeviceFromBobPOV3 = bobSession2.getCrypto().deviceWithIdentityKey(mAliceSession.getCrypto().getOlmDevice().getDeviceCurve25519Key(), mAliceSession.getMyUserId(), MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM);
assertTrue(null != aliceDeviceFromBobPOV3);
assertTrue(TextUtils.equals(aliceDeviceFromBobPOV3.fingerprint(), mAliceSession.getCrypto().getOlmDevice().getDeviceEd25519Key()));
assertTrue(aliceDeviceFromBobPOV3.isBlocked());
mAliceSession.clear(context);
mBobSession.clear(context);
bobSession2.clear(context);
}
use of org.matrix.androidsdk.crypto.data.MXDeviceInfo in project matrix-android-sdk by matrix-org.
the class CryptoTest method test22_testDownloadKeysForUserWithNoDevice.
@Test
public void test22_testDownloadKeysForUserWithNoDevice() throws Exception {
Log.e(LOG_TAG, "test22_testDownloadKeysForUserWithNoDevice");
final HashMap<String, Object> results = new HashMap<>();
doE2ETestWithAliceAndBobInARoom(false);
mAliceSession.getCrypto().setWarnOnUnknownDevices(false);
final CountDownLatch lock1 = new CountDownLatch(1);
mAliceSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys", info);
lock1.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock1.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock1.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock1.countDown();
}
});
lock1.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys"));
MXUsersDevicesMap<MXDeviceInfo> usersDevicesInfoMap = (MXUsersDevicesMap<MXDeviceInfo>) results.get("downloadKeys");
// MXCrypto.downloadKeys should return @[] for Bob to distinguish him from an unknown user
List<String> bobDevices = usersDevicesInfoMap.getUserDeviceIds(mBobSession.getMyUserId());
assertTrue(null != bobDevices);
assertTrue(0 == bobDevices.size());
// try again
// it should not failed
final CountDownLatch lock2 = new CountDownLatch(1);
mAliceSession.getCrypto().getDeviceList().downloadKeys(Arrays.asList(mBobSession.getMyUserId()), false, new ApiCallback<MXUsersDevicesMap<MXDeviceInfo>>() {
@Override
public void onSuccess(MXUsersDevicesMap<MXDeviceInfo> info) {
results.put("downloadKeys2", info);
lock2.countDown();
}
@Override
public void onNetworkError(Exception e) {
lock2.countDown();
}
@Override
public void onMatrixError(MatrixError e) {
lock2.countDown();
}
@Override
public void onUnexpectedError(Exception e) {
lock2.countDown();
}
});
lock2.await(1000, TimeUnit.MILLISECONDS);
assertTrue(results.containsKey("downloadKeys2"));
}
use of org.matrix.androidsdk.crypto.data.MXDeviceInfo in project matrix-android-sdk by matrix-org.
the class MXCrypto method setDeviceVerification.
/**
* Update the blocked/verified state of the given device.
*
* @param verificationStatus the new verification status
* @param deviceId the unique identifier for the device.
* @param userId the owner of the device
* @param callback the asynchronous callback
*/
public void setDeviceVerification(final int verificationStatus, final String deviceId, final String userId, final ApiCallback<Void> callback) {
if (hasBeenReleased()) {
return;
}
final ArrayList<String> userRoomIds = new ArrayList<>();
Collection<Room> rooms = mSession.getDataHandler().getStore().getRooms();
for (Room room : rooms) {
if (room.isEncrypted()) {
RoomMember roomMember = room.getMember(userId);
// test if the user joins the room
if ((null != roomMember) && TextUtils.equals(roomMember.membership, RoomMember.MEMBERSHIP_JOIN)) {
userRoomIds.add(room.getRoomId());
}
}
}
getEncryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
MXDeviceInfo device = mCryptoStore.getUserDevice(deviceId, userId);
// Sanity check
if (null == device) {
Log.e(LOG_TAG, "## setDeviceVerification() : Unknown device " + userId + ":" + deviceId);
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
public void run() {
callback.onSuccess(null);
}
});
}
return;
}
if (device.mVerified != verificationStatus) {
device.mVerified = verificationStatus;
mCryptoStore.storeUserDevice(userId, device);
}
if (null != callback) {
getUIHandler().post(new Runnable() {
@Override
public void run() {
callback.onSuccess(null);
}
});
}
}
});
}
use of org.matrix.androidsdk.crypto.data.MXDeviceInfo in project matrix-android-sdk by matrix-org.
the class MXCrypto method deviceWithIdentityKey.
/**
* Find a device by curve25519 identity key
*
* @param userId the owner of the device.
* @param algorithm the encryption algorithm.
* @param senderKey the curve25519 key to match.
* @return the device info.
*/
public MXDeviceInfo deviceWithIdentityKey(final String senderKey, final String userId, final String algorithm) {
if (!hasBeenReleased()) {
if (!TextUtils.equals(algorithm, MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_MEGOLM) && !TextUtils.equals(algorithm, MXCryptoAlgorithms.MXCRYPTO_ALGORITHM_OLM)) {
// We only deal in olm keys
return null;
}
if (!TextUtils.isEmpty(userId)) {
final ArrayList<MXDeviceInfo> result = new ArrayList<>();
final CountDownLatch lock = new CountDownLatch(1);
getDecryptingThreadHandler().post(new Runnable() {
@Override
public void run() {
List<MXDeviceInfo> devices = getUserDevices(userId);
if (null != devices) {
for (MXDeviceInfo device : devices) {
Set<String> keys = device.keys.keySet();
for (String keyId : keys) {
if (keyId.startsWith("curve25519:")) {
if (TextUtils.equals(senderKey, device.keys.get(keyId))) {
result.add(device);
}
}
}
}
}
lock.countDown();
}
});
try {
lock.await();
} catch (Exception e) {
Log.e(LOG_TAG, "## deviceWithIdentityKey() : failed " + e.getMessage());
}
return (result.size() > 0) ? result.get(0) : null;
}
}
// Doesn't match a known device
return null;
}
Aggregations