Search in sources :

Example 1 with MXCrypto

use of org.matrix.androidsdk.crypto.MXCrypto in project matrix-android-sdk by matrix-org.

the class MXSession method checkCrypto.

/**
 * Check if the crypto engine is properly initialized.
 * Launch it it is was not yet done.
 */
public void checkCrypto() {
    MXFileCryptoStore fileCryptoStore = new MXFileCryptoStore();
    fileCryptoStore.initWithCredentials(mAppContent, mCredentials);
    if ((fileCryptoStore.hasData() || mEnableCryptoWhenStartingMXSession) && (null == mCrypto)) {
        boolean isStoreLoaded = false;
        try {
            // open the store
            fileCryptoStore.open();
            isStoreLoaded = true;
        } catch (UnsatisfiedLinkError e) {
            Log.e(LOG_TAG, "## checkCrypto() failed " + e.getMessage());
        }
        if (!isStoreLoaded) {
            // load again the olm manager
            // reported by rageshake, it seems that the olm lib is unloaded.
            mOlmManager = new OlmManager();
            try {
                // open the store
                fileCryptoStore.open();
                isStoreLoaded = true;
            } catch (UnsatisfiedLinkError e) {
                Log.e(LOG_TAG, "## checkCrypto() failed 2 " + e.getMessage());
            }
        }
        if (!isStoreLoaded) {
            Log.e(LOG_TAG, "## checkCrypto() : cannot enable the crypto because of olm lib");
            return;
        }
        mCrypto = new MXCrypto(MXSession.this, fileCryptoStore);
        mDataHandler.setCrypto(mCrypto);
        // the room summaries are not stored with decrypted content
        decryptRoomSummaries();
        Log.d(LOG_TAG, "## checkCrypto() : the crypto engine is ready");
    } else if (mDataHandler.getCrypto() != mCrypto) {
        Log.e(LOG_TAG, "## checkCrypto() : the data handler crypto was not initialized");
        mDataHandler.setCrypto(mCrypto);
    }
}
Also used : MXFileCryptoStore(org.matrix.androidsdk.data.cryptostore.MXFileCryptoStore) OlmManager(org.matrix.olm.OlmManager) MXCrypto(org.matrix.androidsdk.crypto.MXCrypto)

Example 2 with MXCrypto

use of org.matrix.androidsdk.crypto.MXCrypto in project matrix-android-sdk by matrix-org.

the class MXSession method enableCrypto.

/**
 * Enable / disable the crypto.
 *
 * @param cryptoEnabled true to enable the crypto
 * @param callback      the asynchronous callback called when the action has been done
 */
public void enableCrypto(boolean cryptoEnabled, final ApiCallback<Void> callback) {
    if (cryptoEnabled != isCryptoEnabled()) {
        if (cryptoEnabled) {
            Log.d(LOG_TAG, "Crypto is enabled");
            MXFileCryptoStore fileCryptoStore = new MXFileCryptoStore();
            fileCryptoStore.initWithCredentials(mAppContent, mCredentials);
            fileCryptoStore.open();
            mCrypto = new MXCrypto(this, fileCryptoStore);
            mCrypto.start(true, new ApiCallback<Void>() {

                @Override
                public void onSuccess(Void info) {
                    decryptRoomSummaries();
                    if (null != callback) {
                        callback.onSuccess(null);
                    }
                }

                @Override
                public void onNetworkError(Exception e) {
                    if (null != callback) {
                        callback.onNetworkError(e);
                    }
                }

                @Override
                public void onMatrixError(MatrixError e) {
                    if (null != callback) {
                        callback.onMatrixError(e);
                    }
                }

                @Override
                public void onUnexpectedError(Exception e) {
                    if (null != callback) {
                        callback.onUnexpectedError(e);
                    }
                }
            });
        } else if (null != mCrypto) {
            Log.d(LOG_TAG, "Crypto is disabled");
            IMXCryptoStore store = mCrypto.mCryptoStore;
            mCrypto.close();
            store.deleteStore();
            mCrypto = null;
            mDataHandler.setCrypto(null);
            decryptRoomSummaries();
            if (null != callback) {
                callback.onSuccess(null);
            }
        }
        mDataHandler.setCrypto(mCrypto);
    } else {
        if (null != callback) {
            callback.onSuccess(null);
        }
    }
}
Also used : IMXCryptoStore(org.matrix.androidsdk.data.cryptostore.IMXCryptoStore) MXFileCryptoStore(org.matrix.androidsdk.data.cryptostore.MXFileCryptoStore) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) MXCrypto(org.matrix.androidsdk.crypto.MXCrypto)

Example 3 with MXCrypto

use of org.matrix.androidsdk.crypto.MXCrypto in project matrix-android-sdk by matrix-org.

the class CryptoTest method test02_testCryptoPersistenceInStore.

@Test
public void test02_testCryptoPersistenceInStore() throws Exception {
    Log.e(LOG_TAG, "test02_testCryptoPersistenceInStore");
    Context context = InstrumentationRegistry.getContext();
    final HashMap<String, Object> results = new HashMap<>();
    createBobAccount();
    mBobSession.getCredentials().deviceId = "BobDevice";
    assertTrue(null == mBobSession.getCrypto());
    final CountDownLatch lock0 = new CountDownLatch(1);
    mBobSession.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"));
    assertTrue(null != mBobSession.getCrypto());
    SystemClock.sleep(1000);
    final String deviceCurve25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceCurve25519Key();
    final String deviceEd25519Key = mBobSession.getCrypto().getOlmDevice().getDeviceEd25519Key();
    final List<MXDeviceInfo> myUserDevices = mBobSession.getCrypto().getUserDevices(mBobSession.getMyUserId());
    assertTrue(null != myUserDevices);
    assertTrue(1 == myUserDevices.size());
    final 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 lock1 = new CountDownLatch(1);
    MXStoreListener listener = new MXStoreListener() {

        @Override
        public void postProcess(String accountId) {
        }

        @Override
        public void onStoreReady(String accountId) {
            results.put("onStoreReady", "onStoreReady");
            lock1.countDown();
        }

        @Override
        public void onStoreCorrupted(String accountId, String description) {
            lock1.countDown();
        }

        @Override
        public void onStoreOOM(String accountId, String description) {
            lock1.countDown();
        }
    };
    bobSession2.getDataHandler().getStore().addMXStoreListener(listener);
    bobSession2.getDataHandler().getStore().open();
    lock1.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(results.containsKey("onStoreReady"));
    assertTrue(bobSession2.isCryptoEnabled());
    final CountDownLatch lock2 = new CountDownLatch(2);
    MXEventListener eventsListener = new MXEventListener() {

        @Override
        public void onInitialSyncComplete(String toToken) {
            results.put("onInitialSyncComplete", "onInitialSyncComplete");
            lock2.countDown();
        }

        @Override
        public void onCryptoSyncComplete() {
            results.put("onCryptoSyncComplete", "onCryptoSyncComplete");
            lock2.countDown();
        }
    };
    bobSession2.getDataHandler().addListener(eventsListener);
    bobSession2.startEventStream(null);
    lock2.await(1000, TimeUnit.MILLISECONDS);
    assertTrue(results.containsKey("onInitialSyncComplete"));
    assertTrue(results.containsKey("onCryptoSyncComplete"));
    MXCrypto crypto = bobSession2.getCrypto();
    assertNotNull(crypto);
    assertTrue(TextUtils.equals(deviceCurve25519Key, crypto.getOlmDevice().getDeviceCurve25519Key()));
    assertTrue(TextUtils.equals(deviceEd25519Key, crypto.getOlmDevice().getDeviceEd25519Key()));
    List<MXDeviceInfo> myUserDevices2 = bobSession2.getCrypto().getUserDevices(bobSession2.getMyUserId());
    assertTrue(1 == myUserDevices2.size());
    assertTrue(TextUtils.equals(myUserDevices2.get(0).deviceId, myUserDevices.get(0).deviceId));
    mBobSession.clear(context);
    bobSession2.clear(context);
}
Also used : Context(android.content.Context) MXStoreListener(org.matrix.androidsdk.data.store.MXStoreListener) HashMap(java.util.HashMap) IMXStore(org.matrix.androidsdk.data.store.IMXStore) MXFileStore(org.matrix.androidsdk.data.store.MXFileStore) MXDeviceInfo(org.matrix.androidsdk.crypto.data.MXDeviceInfo) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MXEventListener(org.matrix.androidsdk.listeners.MXEventListener) JsonObject(com.google.gson.JsonObject) MatrixError(org.matrix.androidsdk.rest.model.MatrixError) Credentials(org.matrix.androidsdk.rest.model.login.Credentials) MXCrypto(org.matrix.androidsdk.crypto.MXCrypto) Test(org.junit.Test)

Aggregations

MXCrypto (org.matrix.androidsdk.crypto.MXCrypto)3 MXFileCryptoStore (org.matrix.androidsdk.data.cryptostore.MXFileCryptoStore)2 MatrixError (org.matrix.androidsdk.rest.model.MatrixError)2 Context (android.content.Context)1 Uri (android.net.Uri)1 JsonObject (com.google.gson.JsonObject)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 MXDeviceInfo (org.matrix.androidsdk.crypto.data.MXDeviceInfo)1 IMXCryptoStore (org.matrix.androidsdk.data.cryptostore.IMXCryptoStore)1 IMXStore (org.matrix.androidsdk.data.store.IMXStore)1 MXFileStore (org.matrix.androidsdk.data.store.MXFileStore)1 MXStoreListener (org.matrix.androidsdk.data.store.MXStoreListener)1 MXEventListener (org.matrix.androidsdk.listeners.MXEventListener)1 Credentials (org.matrix.androidsdk.rest.model.login.Credentials)1 OlmManager (org.matrix.olm.OlmManager)1