diff options
Diffstat (limited to 'android/olm-sdk/src')
44 files changed, 0 insertions, 10608 deletions
diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java deleted file mode 100644 index 44405e3..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java +++ /dev/null @@ -1,493 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.content.Context; -import android.support.test.runner.AndroidJUnit4; -import android.text.TextUtils; -import android.util.Log; - -import org.json.JSONException; -import org.json.JSONObject; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Map; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmAccountTest { - private static final String LOG_TAG = "OlmAccountTest"; - private static final int GENERATION_ONE_TIME_KEYS_NUMBER = 50; - - private static OlmAccount mOlmAccount; - private static OlmManager mOlmManager; - private boolean mIsAccountCreated; - private final String FILE_NAME = "SerialTestFile"; - - @BeforeClass - public static void setUpClass(){ - // load native lib - mOlmManager = new OlmManager(); - - String olmLibVersion = mOlmManager.getOlmLibVersion(); - assertNotNull(olmLibVersion); - String olmSdkVersion = mOlmManager.getDetailedVersion(getInstrumentation().getContext()); - assertNotNull(olmLibVersion); - Log.d(LOG_TAG, "## setUpClass(): Versions - Android Olm SDK = "+olmSdkVersion+" Olm lib ="+olmLibVersion); - } - - @AfterClass - public static void tearDownClass() { - // TBD - } - - @Before - public void setUp() { - if(mIsAccountCreated) { - assertNotNull(mOlmAccount); - } - } - - @After - public void tearDown() { - // TBD - } - - /** - * Basic test: creation and release. - */ - @Test - public void test01CreateReleaseAccount() { - try { - mOlmAccount = new OlmAccount(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmAccount failed " + e.getMessage(), false); - } - assertNotNull(mOlmAccount); - - mOlmAccount.releaseAccount(); - assertTrue(0 == mOlmAccount.getOlmAccountId()); - } - - @Test - public void test02CreateAccount() { - try { - mOlmAccount = new OlmAccount(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmAccount failed " + e.getMessage(), false); - } - assertNotNull(mOlmAccount); - mIsAccountCreated = true; - } - - @Test - public void test04GetOlmAccountId() { - long olmNativeInstance = mOlmAccount.getOlmAccountId(); - Log.d(LOG_TAG,"## testGetOlmAccountId olmNativeInstance="+olmNativeInstance); - assertTrue(0!=olmNativeInstance); - } - - /** - * Test if {@link OlmAccount#identityKeys()} returns a JSON object - * that contains the following keys: {@link OlmAccount#JSON_KEY_FINGER_PRINT_KEY} - * and {@link OlmAccount#JSON_KEY_IDENTITY_KEY} - */ - @Test - public void test05IdentityKeys() { - Map<String, String> identityKeys = null; - try { - identityKeys = mOlmAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - assertNotNull(identityKeys); - Log.d(LOG_TAG,"## testIdentityKeys Keys="+identityKeys); - - // is JSON_KEY_FINGER_PRINT_KEY present? - String fingerPrintKey = TestHelper.getFingerprintKey(identityKeys); - assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); - - // is JSON_KEY_IDENTITY_KEY present? - String identityKey = TestHelper.getIdentityKey(identityKeys); - assertTrue("identity key missing",!TextUtils.isEmpty(identityKey)); - } - - //**************************************************** - //***************** ONE TIME KEYS TESTS ************** - //**************************************************** - @Test - public void test06MaxOneTimeKeys() { - long maxOneTimeKeys = mOlmAccount.maxOneTimeKeys(); - Log.d(LOG_TAG,"## testMaxOneTimeKeys(): maxOneTimeKeys="+maxOneTimeKeys); - - assertTrue(maxOneTimeKeys>0); - } - - /** - * Test one time keys generation. - */ - @Test - public void test07GenerateOneTimeKeys() { - String error = null; - - try { - mOlmAccount.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - error = e.getMessage(); - } - - assertTrue(null == error); - } - - /** - * Test the generated amount of one time keys = GENERATION_ONE_TIME_KEYS_NUMBER. - */ - @Test - public void test08OneTimeKeysJsonFormat() { - int oneTimeKeysCount = 0; - Map<String, Map<String, String>> oneTimeKeysJson = null; - - try { - oneTimeKeysJson = mOlmAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(oneTimeKeysJson); - - try { - Map<String, String> map = oneTimeKeysJson.get(OlmAccount.JSON_KEY_ONE_TIME_KEY); - assertTrue(OlmAccount.JSON_KEY_ONE_TIME_KEY +" object is missing", null!=map); - - // test the count of the generated one time keys: - oneTimeKeysCount = map.size(); - - assertTrue("Expected count="+GENERATION_ONE_TIME_KEYS_NUMBER+" found="+oneTimeKeysCount,GENERATION_ONE_TIME_KEYS_NUMBER==oneTimeKeysCount); - - } catch (Exception e) { - assertTrue("Exception MSg="+e.getMessage(), false); - } - } - - @Test - public void test10RemoveOneTimeKeysForSession() { - OlmSession olmSession = null; - try { - olmSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - long sessionId = olmSession.getOlmSessionId(); - assertTrue(0 != sessionId); - - String errorMessage = null; - - try { - mOlmAccount.removeOneTimeKeys(olmSession); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - olmSession.releaseSession(); - sessionId = olmSession.getOlmSessionId(); - assertTrue(0 == sessionId); - } - - @Test - public void test11MarkOneTimeKeysAsPublished() { - try { - mOlmAccount.markOneTimeKeysAsPublished(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - } - - @Test - public void test12SignMessage() { - String clearMsg = "String to be signed by olm"; - String signedMsg = null; - - try { - signedMsg = mOlmAccount.signMessage(clearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(signedMsg); - // additional tests are performed in test01VerifyEd25519Signing() - } - - - // ******************************************************** - // ************* SERIALIZATION TEST *********************** - // ******************************************************** - - @Test - public void test13Serialization() { - FileOutputStream fileOutput; - ObjectOutputStream objectOutput; - OlmAccount accountRef = null; - OlmAccount accountDeserial = null; - - try { - accountRef = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - try { - accountRef.generateOneTimeKeys(GENERATION_ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(),false); - } - - // get keys references - Map<String, String> identityKeysRef = null; - - try { - identityKeysRef = accountRef.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - Map<String, Map<String, String>> oneTimeKeysRef = null; - - try { - oneTimeKeysRef = accountRef.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(identityKeysRef); - assertNotNull(oneTimeKeysRef); - - try { - Context context = getInstrumentation().getContext(); - //context.getFilesDir(); - fileOutput = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE); - - // serialize account - objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(accountRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize account - FileInputStream fileInput = context.openFileInput(FILE_NAME); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - accountDeserial = (OlmAccount) objectInput.readObject(); - objectInput.close(); - assertNotNull(accountDeserial); - - // get de-serialized keys - Map<String, String> identityKeysDeserial = accountDeserial.identityKeys(); - Map<String, Map<String, String>> oneTimeKeysDeserial = accountDeserial.oneTimeKeys(); - assertNotNull(identityKeysDeserial); - assertNotNull(oneTimeKeysDeserial); - - // compare identity keys - assertTrue(identityKeysDeserial.toString().equals(identityKeysRef.toString())); - - // compare onetime keys - assertTrue(oneTimeKeysDeserial.toString().equals(oneTimeKeysRef.toString())); - - accountRef.releaseAccount(); - accountDeserial.releaseAccount(); - } - catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - catch (IOException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception IOException Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - /*catch (OlmException e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception OlmException Msg==" + e.getMessage()); - }*/ - catch (Exception e) { - Log.e(LOG_TAG, "## test13Serialization(): Exception Msg==" + e.getMessage()); - assertTrue("test13Serialization failed " + e.getMessage(), false); - } - } - - - // **************************************************** - // *************** SANITY CHECK TESTS ***************** - // **************************************************** - - @Test - public void test14GenerateOneTimeKeysError() { - // keys number = 0 => no error - - String errorMessage = null; - try { - mOlmAccount.generateOneTimeKeys(0); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(null == errorMessage); - - // keys number = negative value - errorMessage = null; - try { - mOlmAccount.generateOneTimeKeys(-50); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(null != errorMessage); - } - - @Test - public void test15RemoveOneTimeKeysForSessionError() { - OlmAccount olmAccount = null; - try { - olmAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - try { - olmAccount.removeOneTimeKeys(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - olmAccount.releaseAccount(); - } - - @Test - public void test16SignMessageError() { - OlmAccount olmAccount = null; - try { - olmAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - String signedMsg = null; - - try { - signedMsg = olmAccount.signMessage(null); - } catch (Exception e) { - } - - assertNull(signedMsg); - - olmAccount.releaseAccount(); - } - - /** - * Create multiple accounts and check that identity keys are still different. - * This test validates random series are provide enough random values. - */ - @Test - public void test17MultipleAccountCreation() { - try { - OlmAccount account1 = new OlmAccount(); - OlmAccount account2 = new OlmAccount(); - OlmAccount account3 = new OlmAccount(); - OlmAccount account4 = new OlmAccount(); - OlmAccount account5 = new OlmAccount(); - OlmAccount account6 = new OlmAccount(); - OlmAccount account7 = new OlmAccount(); - OlmAccount account8 = new OlmAccount(); - OlmAccount account9 = new OlmAccount(); - OlmAccount account10 = new OlmAccount(); - - Map<String, String> identityKeys1 = account1.identityKeys(); - Map<String, String> identityKeys2 = account2.identityKeys(); - Map<String, String> identityKeys3 = account3.identityKeys(); - Map<String, String> identityKeys4 = account4.identityKeys(); - Map<String, String> identityKeys5 = account5.identityKeys(); - Map<String, String> identityKeys6 = account6.identityKeys(); - Map<String, String> identityKeys7 = account7.identityKeys(); - Map<String, String> identityKeys8 = account8.identityKeys(); - Map<String, String> identityKeys9 = account9.identityKeys(); - Map<String, String> identityKeys10 = account10.identityKeys(); - - String identityKey1 = TestHelper.getIdentityKey(identityKeys1); - String identityKey2 = TestHelper.getIdentityKey(identityKeys2); - assertFalse(identityKey1.equals(identityKey2)); - - String identityKey3 = TestHelper.getIdentityKey(identityKeys3); - assertFalse(identityKey2.equals(identityKey3)); - - String identityKey4 = TestHelper.getIdentityKey(identityKeys4); - assertFalse(identityKey3.equals(identityKey4)); - - String identityKey5 = TestHelper.getIdentityKey(identityKeys5); - assertFalse(identityKey4.equals(identityKey5)); - - String identityKey6 = TestHelper.getIdentityKey(identityKeys6); - assertFalse(identityKey5.equals(identityKey6)); - - String identityKey7 = TestHelper.getIdentityKey(identityKeys7); - assertFalse(identityKey6.equals(identityKey7)); - - String identityKey8 = TestHelper.getIdentityKey(identityKeys8); - assertFalse(identityKey7.equals(identityKey8)); - - String identityKey9 = TestHelper.getIdentityKey(identityKeys9); - assertFalse(identityKey8.equals(identityKey9)); - - String identityKey10 = TestHelper.getIdentityKey(identityKeys10); - assertFalse(identityKey9.equals(identityKey10)); - - account1.releaseAccount(); - account2.releaseAccount(); - account3.releaseAccount(); - account4.releaseAccount(); - account5.releaseAccount(); - account6.releaseAccount(); - account7.releaseAccount(); - account8.releaseAccount(); - account9.releaseAccount(); - account10.releaseAccount(); - - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java deleted file mode 100644 index 69eb0e8..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupSessionTest.java +++ /dev/null @@ -1,627 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.content.Context; -import android.support.test.runner.AndroidJUnit4; -import android.text.TextUtils; -import android.util.Log; - -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmGroupSessionTest { - private static final String LOG_TAG = "OlmSessionTest"; - private final String FILE_NAME_SERIAL_OUT_SESSION = "SerialOutGroupSession"; - private final String FILE_NAME_SERIAL_IN_SESSION = "SerialInGroupSession"; - - private static OlmManager mOlmManager; - private static OlmOutboundGroupSession mAliceOutboundGroupSession; - private static String mAliceSessionIdentifier; - private static long mAliceMessageIndex; - private static final String CLEAR_MESSAGE1 = "Hello!"; - private static String mAliceToBobMessage; - private static OlmInboundGroupSession mBobInboundGroupSession; - private static String mAliceOutboundSessionKey; - private static String mBobSessionIdentifier; - private static String mBobDecryptedMessage; - - @BeforeClass - public static void setUpClass(){ - // load native lib - mOlmManager = new OlmManager(); - - String version = mOlmManager.getOlmLibVersion(); - assertNotNull(version); - Log.d(LOG_TAG, "## setUpClass(): lib version="+version); - } - - /** - * Basic test: - * - alice creates an outbound group session - * - bob creates an inbound group session with alice's outbound session key - * - alice encrypts a message with its session - * - bob decrypts the encrypted message with its session - * - decrypted message is identical to original alice message - */ - @Test - public void test01CreateOutboundSession() { - // alice creates OUTBOUND GROUP SESSION - try { - mAliceOutboundGroupSession = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - @Test - public void test02GetOutboundGroupSessionIdentifier() { - // test session ID - mAliceSessionIdentifier = null; - - try { - mAliceSessionIdentifier = mAliceOutboundGroupSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(mAliceSessionIdentifier); - assertTrue(mAliceSessionIdentifier.length() > 0); - } - - @Test - public void test03GetOutboundGroupSessionKey() { - // test session Key - mAliceOutboundSessionKey = null; - - try { - mAliceOutboundSessionKey = mAliceOutboundGroupSession.sessionKey(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(mAliceOutboundSessionKey); - assertTrue(mAliceOutboundSessionKey.length() > 0); - } - - @Test - public void test04GetOutboundGroupMessageIndex() { - // test message index before any encryption - mAliceMessageIndex = mAliceOutboundGroupSession.messageIndex(); - assertTrue(0 == mAliceMessageIndex); - } - - @Test - public void test05OutboundGroupEncryptMessage() { - // alice encrypts a message to bob - try { - mAliceToBobMessage = mAliceOutboundGroupSession.encryptMessage(CLEAR_MESSAGE1); - } catch (Exception e) { - assertTrue("Exception in bob encryptMessage, Exception code=" + e.getMessage(), false); - } - assertFalse(TextUtils.isEmpty(mAliceToBobMessage)); - - // test message index after encryption is incremented - mAliceMessageIndex = mAliceOutboundGroupSession.messageIndex(); - assertTrue(1 == mAliceMessageIndex); - } - - @Test - public void test06CreateInboundGroupSession() { - // bob creates INBOUND GROUP SESSION with alice outbound key - try { - mBobInboundGroupSession = new OlmInboundGroupSession(mAliceOutboundSessionKey); - } catch (OlmException e) { - assertTrue("Exception in bob OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - @Test - public void test08GetInboundGroupSessionIdentifier() { - // check both session identifiers are equals - mBobSessionIdentifier = null; - - try { - mBobSessionIdentifier = mBobInboundGroupSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertFalse(TextUtils.isEmpty(mBobSessionIdentifier)); - } - - @Test - public void test09SessionIdentifiersAreIdentical() { - // check both session identifiers are equals: alice vs bob - assertTrue(mAliceSessionIdentifier.equals(mBobSessionIdentifier)); - } - - @Test - public void test10InboundDecryptMessage() { - mBobDecryptedMessage = null; - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = mBobInboundGroupSession.decryptMessage(mAliceToBobMessage); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - // test decrypted message - mBobDecryptedMessage = result.mDecryptedMessage; - assertFalse(TextUtils.isEmpty(mBobDecryptedMessage)); - assertTrue(0 == result.mIndex); - } - - @Test - public void test11InboundDecryptedMessageIdentical() { - // test decrypted message - assertTrue(mBobDecryptedMessage.equals(CLEAR_MESSAGE1)); - } - - @Test - public void test12ReleaseOutboundSession() { - // release group sessions - mAliceOutboundGroupSession.releaseSession(); - } - - @Test - public void test13ReleaseInboundSession() { - // release group sessions - mBobInboundGroupSession.releaseSession(); - } - - @Test - public void test14CheckUnreleaseedCount() { - assertTrue(mAliceOutboundGroupSession.isReleased()); - assertTrue(mBobInboundGroupSession.isReleased()); - } - - @Test - public void test15SerializeOutboundSession() { - OlmOutboundGroupSession outboundGroupSessionRef=null; - OlmOutboundGroupSession outboundGroupSessionSerial; - - // create one OUTBOUND GROUP SESSION - try { - outboundGroupSessionRef = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(outboundGroupSessionRef); - - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_OUT_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(outboundGroupSessionRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_OUT_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - outboundGroupSessionSerial = (OlmOutboundGroupSession) objectInput.readObject(); - assertNotNull(outboundGroupSessionSerial); - objectInput.close(); - - // get sessions keys - String sessionKeyRef = outboundGroupSessionRef.sessionKey(); - String sessionKeySerial = outboundGroupSessionSerial.sessionKey(); - assertFalse(TextUtils.isEmpty(sessionKeyRef)); - assertFalse(TextUtils.isEmpty(sessionKeySerial)); - - // session keys comparison - assertTrue(sessionKeyRef.equals(sessionKeySerial)); - - // get sessions IDs - String sessionIdRef = outboundGroupSessionRef.sessionIdentifier(); - String sessionIdSerial = outboundGroupSessionSerial.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(sessionIdRef)); - assertFalse(TextUtils.isEmpty(sessionIdSerial)); - - // session IDs comparison - assertTrue(sessionIdRef.equals(sessionIdSerial)); - - outboundGroupSessionRef.releaseSession(); - outboundGroupSessionSerial.releaseSession(); - - assertTrue(outboundGroupSessionRef.isReleased()); - assertTrue(outboundGroupSessionSerial.isReleased()); - } catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (OlmException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception OlmException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (IOException e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (Exception e) { - Log.e(LOG_TAG, "## test15SerializeOutboundSession(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - @Test - public void test16SerializeInboundSession() { - OlmOutboundGroupSession aliceOutboundGroupSession=null; - OlmInboundGroupSession bobInboundGroupSessionRef=null; - OlmInboundGroupSession bobInboundGroupSessionSerial; - - // alice creates OUTBOUND GROUP SESSION - try { - aliceOutboundGroupSession = new OlmOutboundGroupSession(); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(aliceOutboundGroupSession); - - // get the session key from the outbound group session - String sessionKeyRef = null; - - try { - sessionKeyRef = aliceOutboundGroupSession.sessionKey(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(sessionKeyRef); - - // bob creates INBOUND GROUP SESSION - try { - bobInboundGroupSessionRef = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in OlmInboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - assertNotNull(bobInboundGroupSessionRef); - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_IN_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(bobInboundGroupSessionRef); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_IN_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - bobInboundGroupSessionSerial = (OlmInboundGroupSession)objectInput.readObject(); - assertNotNull(bobInboundGroupSessionSerial); - objectInput.close(); - - // get sessions IDs - String aliceSessionId = aliceOutboundGroupSession.sessionIdentifier(); - String sessionIdRef = bobInboundGroupSessionRef.sessionIdentifier(); - String sessionIdSerial = bobInboundGroupSessionSerial.sessionIdentifier(); - assertFalse(TextUtils.isEmpty(aliceSessionId)); - assertFalse(TextUtils.isEmpty(sessionIdRef)); - assertFalse(TextUtils.isEmpty(sessionIdSerial)); - - // session IDs comparison - assertTrue(aliceSessionId.equals(sessionIdSerial)); - assertTrue(sessionIdRef.equals(sessionIdSerial)); - - aliceOutboundGroupSession.releaseSession(); - bobInboundGroupSessionRef.releaseSession(); - bobInboundGroupSessionSerial.releaseSession(); - - assertTrue(aliceOutboundGroupSession.isReleased()); - assertTrue(bobInboundGroupSessionRef.isReleased()); - assertTrue(bobInboundGroupSessionSerial.isReleased()); - } catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (OlmException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception OlmException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (IOException e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } catch (Exception e) { - Log.e(LOG_TAG, "## test16SerializeInboundSession(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - /** - * Create multiple outbound group sessions and check that session Keys are different. - * This test validates random series are provide enough random values. - */ - @Test - public void test17MultipleOutboundSession() { - OlmOutboundGroupSession outboundGroupSession1; - OlmOutboundGroupSession outboundGroupSession2; - OlmOutboundGroupSession outboundGroupSession3; - OlmOutboundGroupSession outboundGroupSession4; - OlmOutboundGroupSession outboundGroupSession5; - OlmOutboundGroupSession outboundGroupSession6; - OlmOutboundGroupSession outboundGroupSession7; - OlmOutboundGroupSession outboundGroupSession8; - - try { - outboundGroupSession1 = new OlmOutboundGroupSession(); - outboundGroupSession2 = new OlmOutboundGroupSession(); - outboundGroupSession3 = new OlmOutboundGroupSession(); - outboundGroupSession4 = new OlmOutboundGroupSession(); - outboundGroupSession5 = new OlmOutboundGroupSession(); - outboundGroupSession6 = new OlmOutboundGroupSession(); - outboundGroupSession7 = new OlmOutboundGroupSession(); - outboundGroupSession8 = new OlmOutboundGroupSession(); - - // get the session key from the outbound group sessions - String sessionKey1 = outboundGroupSession1.sessionKey(); - String sessionKey2 = outboundGroupSession2.sessionKey(); - assertFalse(sessionKey1.equals(sessionKey2)); - - String sessionKey3 = outboundGroupSession3.sessionKey(); - assertFalse(sessionKey2.equals(sessionKey3)); - - String sessionKey4 = outboundGroupSession4.sessionKey(); - assertFalse(sessionKey3.equals(sessionKey4)); - - String sessionKey5 = outboundGroupSession5.sessionKey(); - assertFalse(sessionKey4.equals(sessionKey5)); - - String sessionKey6 = outboundGroupSession6.sessionKey(); - assertFalse(sessionKey5.equals(sessionKey6)); - - String sessionKey7 = outboundGroupSession7.sessionKey(); - assertFalse(sessionKey6.equals(sessionKey7)); - - String sessionKey8 = outboundGroupSession8.sessionKey(); - assertFalse(sessionKey7.equals(sessionKey8)); - - // get the session IDs from the outbound group sessions - String sessionId1 = outboundGroupSession1.sessionIdentifier(); - String sessionId2 = outboundGroupSession2.sessionIdentifier(); - assertFalse(sessionId1.equals(sessionId2)); - - String sessionId3 = outboundGroupSession3.sessionKey(); - assertFalse(sessionId2.equals(sessionId3)); - - String sessionId4 = outboundGroupSession4.sessionKey(); - assertFalse(sessionId3.equals(sessionId4)); - - String sessionId5 = outboundGroupSession5.sessionKey(); - assertFalse(sessionId4.equals(sessionId5)); - - String sessionId6 = outboundGroupSession6.sessionKey(); - assertFalse(sessionId5.equals(sessionId6)); - - String sessionId7 = outboundGroupSession7.sessionKey(); - assertFalse(sessionId6.equals(sessionId7)); - - String sessionId8 = outboundGroupSession8.sessionKey(); - assertFalse(sessionId7.equals(sessionId8)); - - outboundGroupSession1.releaseSession(); - outboundGroupSession2.releaseSession(); - outboundGroupSession3.releaseSession(); - outboundGroupSession4.releaseSession(); - outboundGroupSession5.releaseSession(); - outboundGroupSession6.releaseSession(); - outboundGroupSession7.releaseSession(); - outboundGroupSession8.releaseSession(); - - assertTrue(outboundGroupSession1.isReleased()); - assertTrue(outboundGroupSession2.isReleased()); - assertTrue(outboundGroupSession3.isReleased()); - assertTrue(outboundGroupSession4.isReleased()); - assertTrue(outboundGroupSession5.isReleased()); - assertTrue(outboundGroupSession6.isReleased()); - assertTrue(outboundGroupSession7.isReleased()); - assertTrue(outboundGroupSession8.isReleased()); - } catch (OlmException e) { - assertTrue("Exception in OlmOutboundGroupSession, Exception code=" + e.getExceptionCode(), false); - } - } - - /** - * Specific test for the following run time error: - * "JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0 in call to NewStringUTF".<br> - * When the msg to decrypt contain emojis, depending on the android platform, the NewStringUTF() behaves differently and - * can even crash. - * This issue is described in details here: https://github.com/eclipsesource/J2V8/issues/142 - */ - @Test - public void test18TestBadCharacterCrashInDecrypt() { - OlmInboundGroupSession bobInboundGroupSession=null; - - // values taken from a "real life" crash case - String sessionKeyRef = "AgAAAAycZE6AekIctJWYxd2AWLOY15YmxZODm/WkgbpWkyycp6ytSp/R+wo84jRrzBNWmv6ySLTZ9R0EDOk9VI2eZyQ6Efdwyo1mAvrWvTkZl9yALPdkOIVHywyG65f1SNiLrnsln3hgsT1vUrISGyKtsljoUgQpr3JDPEhD0ilAi63QBjhnGCW252b+7nF+43rb6O6lwm93LaVwe2341Gdp6EkhTUvetALezEqDOtKN00wVqAbq0RQAnUJIowxHbMswg+FyoR1K1oCjnVEoF23O9xlAn5g1XtuBZP3moJlR2lwsBA"; - String msgToDecryptWithEmoji = "AwgNEpABpjs+tYF+0y8bWtzAgYAC3N55p5cPJEEiGPU1kxIHSY7f2aG5Fj4wmcsXUkhDv0UePj922kgf+Q4dFsPHKq2aVA93n8DJAQ/FRfcM98B9E6sKCZ/PsCF78uBvF12Aaq9D3pUHBopdd7llUfVq29d5y6ZwX5VDoqV2utsATkKjXYV9CbfZuvvBMQ30ZLjEtyUUBJDY9K4FxEFcULytA/IkVnATTG9ERuLF/yB6ukSFR+iUWRYAmtuOuU0k9BvaqezbGqNoK5Grlkes+dYX6/0yUObumcw9/iAI"; - - // bob creates INBOUND GROUP SESSION - try { - bobInboundGroupSession = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getExceptionCode(), false); - } - - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = bobInboundGroupSession.decryptMessage(msgToDecryptWithEmoji); - } catch (Exception e) { - assertTrue("Exception in test18TestBadCharacterCrashInDecrypt, Exception code=" + e.getMessage(), false); - } - - assertNotNull(result.mDecryptedMessage); - assertTrue(13 == result.mIndex); - } - - /** - * Specific test to check an error message is returned by decryptMessage() API.<br> - * A corrupted encrypted message is passed, and a INVALID_BASE64 is - * espexted. - **/ - @Test - public void test19TestErrorMessageReturnedInDecrypt() { - OlmInboundGroupSession bobInboundGroupSession=null; - final String EXPECTED_ERROR_MESSAGE= "INVALID_BASE64"; - - String sessionKeyRef = "AgAAAAycZE6AekIctJWYxd2AWLOY15YmxZODm/WkgbpWkyycp6ytSp/R+wo84jRrzBNWmv6ySLTZ9R0EDOk9VI2eZyQ6Efdwyo1mAvrWvTkZl9yALPdkOIVHywyG65f1SNiLrnsln3hgsT1vUrISGyKtsljoUgQpr3JDPEhD0ilAi63QBjhnGCW252b+7nF+43rb6O6lwm93LaVwe2341Gdp6EkhTUvetALezEqDOtKN00wVqAbq0RQAnUJIowxHbMswg+FyoR1K1oCjnVEoF23O9xlAn5g1XtuBZP3moJlR2lwsBA"; - String corruptedEncryptedMsg = "AwgANYTHINGf87ge45ge7gr*/rg5ganything4gr41rrgr4re55tanythingmcsXUkhDv0UePj922kgf+"; - - // valid INBOUND GROUP SESSION - try { - bobInboundGroupSession = new OlmInboundGroupSession(sessionKeyRef); - } catch (OlmException e) { - assertTrue("Exception in test19TestErrorMessageReturnedInDecrypt, Exception code=" + e.getExceptionCode(), false); - } - - String exceptionMessage = null; - try { - bobInboundGroupSession.decryptMessage(corruptedEncryptedMsg); - } catch (OlmException e) { - exceptionMessage = e.getMessage(); - } - - assertTrue(0!=EXPECTED_ERROR_MESSAGE.length()); - assertTrue(EXPECTED_ERROR_MESSAGE.equals(exceptionMessage)); - } - - - /** - * Test the import/export functions.<br> - **/ - @Test - public void test20TestInboundGroupSessionImportExport() { - - String sessionKey = "AgAAAAAwMTIzNDU2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCREVGM" + - "DEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkRFRjAxMjM0NTY3ODlBQkNERUYwMTIzND" + - "U2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMw0bdg1BDq4Px/slBow06q8n/B9WBfw" + - "WYyNOB8DlUmXGGwrFmaSb9bR/eY8xgERrxmP07hFmD9uqA2p8PMHdnV5ysmgufE6oLZ5+" + - "8/mWQOW3VVTnDIlnwd8oHUYRuk8TCQ"; - - String message = "AwgAEhAcbh6UpbByoyZxufQ+h2B+8XHMjhR69G8F4+qjMaFlnIXusJZX3r8LnRORG9T3D" + - "XFdbVuvIWrLyRfm4i8QRbe8VPwGRFG57B1CtmxanuP8bHtnnYqlwPsD"; - - - OlmInboundGroupSession inboundGroupSession = null; - - try { - inboundGroupSession = new OlmInboundGroupSession(sessionKey); - } catch (Exception e) { - assertTrue("OlmInboundGroupSession failed " + e.getMessage(), false); - } - - boolean isVerified = false; - - try { - isVerified = inboundGroupSession.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertTrue(isVerified); - - OlmInboundGroupSession.DecryptMessageResult result = null; - - try { - result = inboundGroupSession.decryptMessage(message); - } catch (Exception e) { - assertTrue("decryptMessage failed " + e.getMessage(), false); - } - - assertTrue(TextUtils.equals(result.mDecryptedMessage, "Message")); - assertTrue(0 == result.mIndex); - - String export = null; - - try { - export = inboundGroupSession.export(0); - } catch (Exception e) { - assertTrue("export failed " + e.getMessage(), false); - } - assertTrue(!TextUtils.isEmpty(export)); - - long index = -1; - try { - index = inboundGroupSession.getFirstKnownIndex(); - } catch (Exception e) { - assertTrue("getFirstKnownIndex failed " + e.getMessage(), false); - } - assertTrue(index >=0); - - inboundGroupSession.releaseSession(); - inboundGroupSession = null; - - OlmInboundGroupSession inboundGroupSession2 = null; - - try { - inboundGroupSession2 = inboundGroupSession.importSession(export); - } catch (Exception e) { - assertTrue("OlmInboundGroupSession failed " + e.getMessage(), false); - } - - try { - isVerified = inboundGroupSession2.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertFalse(isVerified); - - result = null; - try { - result = inboundGroupSession2.decryptMessage(message); - } catch (Exception e) { - assertTrue("decryptMessage failed " + e.getMessage(), false); - } - - assertTrue(TextUtils.equals(result.mDecryptedMessage, "Message")); - assertTrue(0 == result.mIndex); - - try { - isVerified = inboundGroupSession2.isVerified(); - } catch (Exception e) { - assertTrue("isVerified failed " + e.getMessage(), false); - } - - assertTrue(isVerified); - inboundGroupSession2.releaseSession(); - } -} - diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java deleted file mode 100644 index d1e4a2e..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmPkTest.java +++ /dev/null @@ -1,201 +0,0 @@ -/* - * Copyright 2018,2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.support.test.runner.AndroidJUnit4; -import android.util.Log; - -import java.util.Arrays; - -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmPkTest { - private static final String LOG_TAG = "OlmPkEncryptionTest"; - - private static OlmPkEncryption mOlmPkEncryption; - private static OlmPkDecryption mOlmPkDecryption; - private static OlmPkSigning mOlmPkSigning; - - @Test - public void test01EncryptAndDecrypt() { - try { - mOlmPkEncryption = new OlmPkEncryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - try { - mOlmPkDecryption = new OlmPkDecryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkEncryption); - assertNotNull(mOlmPkDecryption); - - String key = null; - try { - key = mOlmPkDecryption.generateKey(); - } catch (OlmException e) { - assertTrue("Exception in generateKey, Exception code=" + e.getExceptionCode(), false); - } - Log.d(LOG_TAG, "Ephemeral Key: " + key); - try { - mOlmPkEncryption.setRecipientKey(key); - } catch (OlmException e) { - assertTrue("Exception in setRecipientKey, Exception code=" + e.getExceptionCode(), false); - } - - String clearMessage = "Public key test"; - OlmPkMessage message = null; - try { - message = mOlmPkEncryption.encrypt(clearMessage); - } catch (OlmException e) { - assertTrue("Exception in encrypt, Exception code=" + e.getExceptionCode(), false); - } - Log.d(LOG_TAG, "message: " + message.mCipherText + " " + message.mMac + " " + message.mEphemeralKey); - - String decryptedMessage = null; - try { - decryptedMessage = mOlmPkDecryption.decrypt(message); - } catch (OlmException e) { - assertTrue("Exception in decrypt, Exception code=" + e.getExceptionCode(), false); - } - assertTrue(clearMessage.equals(decryptedMessage)); - - mOlmPkEncryption.releaseEncryption(); - mOlmPkDecryption.releaseDecryption(); - assertTrue(mOlmPkEncryption.isReleased()); - assertTrue(mOlmPkDecryption.isReleased()); - } - - @Test - public void test02PrivateKey() { - try { - mOlmPkDecryption = new OlmPkDecryption(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkEncryption failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkDecryption); - - byte[] privateKey = { - (byte)0x77, (byte)0x07, (byte)0x6D, (byte)0x0A, - (byte)0x73, (byte)0x18, (byte)0xA5, (byte)0x7D, - (byte)0x3C, (byte)0x16, (byte)0xC1, (byte)0x72, - (byte)0x51, (byte)0xB2, (byte)0x66, (byte)0x45, - (byte)0xDF, (byte)0x4C, (byte)0x2F, (byte)0x87, - (byte)0xEB, (byte)0xC0, (byte)0x99, (byte)0x2A, - (byte)0xB1, (byte)0x77, (byte)0xFB, (byte)0xA5, - (byte)0x1D, (byte)0xB9, (byte)0x2C, (byte)0x2A - }; - - assertTrue(privateKey.length == OlmPkDecryption.privateKeyLength()); - - try { - mOlmPkDecryption.setPrivateKey(privateKey); - } catch (OlmException e) { - assertTrue("Exception in setPrivateKey, Exception code=" + e.getExceptionCode(), false); - } - - byte[] privateKeyCopy = null; - - try { - privateKeyCopy = mOlmPkDecryption.privateKey(); - } catch (OlmException e) { - assertTrue("Exception in privateKey, Exception code=" + e.getExceptionCode(), false); - } - - assertTrue(Arrays.equals(privateKey, privateKeyCopy)); - - mOlmPkDecryption.releaseDecryption(); - assertTrue(mOlmPkDecryption.isReleased()); - } - - @Test - public void test03Signing() { - try { - mOlmPkSigning = new OlmPkSigning(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("OlmPkSigning failed " + e.getMessage(), false); - } - - assertNotNull(mOlmPkSigning); - - byte[] seed = null; - try { - seed = OlmPkSigning.generateSeed(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("generateSeed failed " + e.getMessage(), false); - } - - assertTrue(seed.length == OlmPkSigning.seedLength()); - - String pubkey = null; - try { - pubkey = mOlmPkSigning.initWithSeed(seed); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("initWithSeed failed " + e.getMessage(), false); - } - - String message = "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness."; - - String signature = null; - try { - signature = mOlmPkSigning.sign(message); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("sign failed " + e.getMessage(), false); - } - - OlmUtility olmUtility = null; - try { - olmUtility = new OlmUtility(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("olmUtility failed " + e.getMessage(), false); - } - - try { - olmUtility.verifyEd25519Signature(signature, pubkey, message); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue("Signature verification failed " + e.getMessage(), false); - } - - mOlmPkSigning.releaseSigning(); - assertTrue(mOlmPkSigning.isReleased()); - - olmUtility.releaseUtility(); - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java deleted file mode 100644 index bbb50ae..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSasTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - - -import android.support.test.runner.AndroidJUnit4; -import android.util.Log; - -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmSasTest { - - private static OlmManager mOlmManager; - - //Enable the native lib - @BeforeClass - public static void setUpClass() { - // load native librandomBytesOfLength - mOlmManager = new OlmManager(); - } - - @Test - public void testSASCode() { - OlmSAS aliceSas = null; - OlmSAS bobSas = null; - - try { - aliceSas = new OlmSAS(); - bobSas = new OlmSAS(); - - String alicePKey = aliceSas.getPublicKey(); - String bobPKey = bobSas.getPublicKey(); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice pub Key is " + alicePKey); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob pub Key is " + bobPKey); - - aliceSas.setTheirPublicKey(bobPKey); - bobSas.setTheirPublicKey(alicePKey); - - int codeLength = 6; - byte[] alice_sas = aliceSas.generateShortCode("SAS", codeLength); - byte[] bob_sas = bobSas.generateShortCode("SAS", codeLength); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice SAS is " + new String(alice_sas, "UTF-8")); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob SAS is " + new String(bob_sas, "UTF-8")); - - assertEquals(codeLength, alice_sas.length); - assertEquals(codeLength, bob_sas.length); - assertArrayEquals(alice_sas, bob_sas); - - String aliceMac = aliceSas.calculateMac("Hello world!", "SAS"); - String bobMac = bobSas.calculateMac("Hello world!", "SAS"); - - assertEquals(aliceMac, bobMac); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice Mac is " + aliceMac); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob Mac is " + bobMac); - - - String aliceLongKdfMac = aliceSas.calculateMacLongKdf("Hello world!", "SAS"); - String bobLongKdfMac = bobSas.calculateMacLongKdf("Hello world!", "SAS"); - - assertEquals("Mac should be the same", aliceLongKdfMac, bobLongKdfMac); - - Log.e(OlmSasTest.class.getSimpleName(), "#### Alice lkdf Mac is " + aliceLongKdfMac); - Log.e(OlmSasTest.class.getSimpleName(), "#### Bob lkdf Mac is " + bobLongKdfMac); - - - } catch (Exception e) { - assertTrue("OlmSas init failed " + e.getMessage(), false); - e.printStackTrace(); - } finally { - if (aliceSas != null) { - aliceSas.releaseSas(); - } - if (bobSas != null) { - bobSas.releaseSas(); - } - } - } - -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java deleted file mode 100644 index e08b151..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java +++ /dev/null @@ -1,1014 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.content.Context; -import android.support.test.runner.AndroidJUnit4; -import android.text.TextUtils; -import android.util.Log; - -import org.json.JSONObject; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.util.Map; - -import static android.support.test.InstrumentationRegistry.getInstrumentation; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmSessionTest { - private static final String LOG_TAG = "OlmSessionTest"; - private final String INVALID_PRE_KEY = "invalid PRE KEY hu hu!"; - private final String FILE_NAME_SERIAL_SESSION = "SerialSession"; - private final int ONE_TIME_KEYS_NUMBER = 4; - - private static OlmManager mOlmManager; - - @BeforeClass - public static void setUpClass(){ - // load native lib - mOlmManager = new OlmManager(); - - String version = mOlmManager.getOlmLibVersion(); - assertNotNull(version); - Log.d(LOG_TAG, "## setUpClass(): lib version="+version); - } - - /** - * Basic test: - * - alice creates an account - * - bob creates an account - * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey) - * - alice encrypts a message with its session - * - bob creates an inbound session based on alice's encrypted message - * - bob decrypts the encrypted message with its session - */ - @Test - public void test01AliceToBob() { - final int ONE_TIME_KEYS_NUMBER = 5; - String bobIdentityKey = null; - String bobOneTimeKey=null; - OlmAccount bobAccount = null; - OlmAccount aliceAccount = null; - - // ALICE & BOB ACCOUNTS CREATION - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); - assertTrue(null!=bobIdentityKey); - - // get bob one time keys - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - String clearMsg = "Heloo bob , this is alice!"; - OlmMessage encryptedMsgToBob = null; - try { - encryptedMsgToBob = aliceSession.encryptMessage(clearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsgToBob); - assertNotNull(encryptedMsgToBob.mCipherText); - Log.d(LOG_TAG,"## test01AliceToBob(): encryptedMsg="+encryptedMsgToBob.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - try { - bobSession.initInboundSession(bobAccount, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - String decryptedMsg = null; - try { - decryptedMsg = bobSession.decryptMessage(encryptedMsgToBob); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(clearMsg.equals(decryptedMsg)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - // release accounts - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - // release sessions - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - - /** - * Same as test01AliceToBob but with bob who's encrypting messages - * to alice and alice decrypt them.<br> - * - alice creates an account - * - bob creates an account - * - alice creates an outbound session with bob (bobIdentityKey & bobOneTimeKey) - * - alice encrypts a message with its own session - * - bob creates an inbound session based on alice's encrypted message - * - bob decrypts the encrypted message with its own session - * - bob encrypts messages with its own session - * - alice decrypts bob's messages with its own message - * - alice encrypts a message - * - bob decrypts the encrypted message - */ - @Test - public void test02AliceToBobBackAndForth() { - String bobIdentityKey; - String bobOneTimeKey; - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - - // creates alice & bob accounts - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); - assertTrue(null!=bobIdentityKey); - - // get bob one time keys - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String helloClearMsg = "Hello I'm Alice!"; - - OlmMessage encryptedAliceToBobMsg1 = null; - - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(encryptedAliceToBobMsg1); - assertNotNull(encryptedAliceToBobMsg1.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - - assertTrue(0!=bobSession.getOlmSessionId()); - - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // DECRYPT MESSAGE FROM ALICE - String decryptedMsg01 = null; - try { - decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg01); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(helloClearMsg.equals(decryptedMsg01)); - - // BACK/FORTH MESSAGE COMPARISON - String clearMsg1 = "Hello I'm Bob!"; - String clearMsg2 = "Isn't life grand?"; - String clearMsg3 = "Let's go to the opera."; - - // bob encrypts messages - OlmMessage encryptedMsg1 = null; - try { - encryptedMsg1 = bobSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - OlmMessage encryptedMsg2 = null; - try { - encryptedMsg2 = bobSession.encryptMessage(clearMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg2); - - - OlmMessage encryptedMsg3 = null; - try { - encryptedMsg3 = bobSession.encryptMessage(clearMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg3); - - // alice decrypts bob's messages - String decryptedMsg1 = null; - try { - decryptedMsg1 = aliceSession.decryptMessage(encryptedMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg1); - String decryptedMsg2 = null; - try { - decryptedMsg2 = aliceSession.decryptMessage(encryptedMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg2); - String decryptedMsg3 = null; - try { - decryptedMsg3 = aliceSession.decryptMessage(encryptedMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(decryptedMsg3); - - // comparison tests - assertTrue(clearMsg1.equals(decryptedMsg1)); - assertTrue(clearMsg2.equals(decryptedMsg2)); - assertTrue(clearMsg3.equals(decryptedMsg3)); - - // and one more from alice to bob - clearMsg1 = "another message from Alice to Bob!!"; - encryptedMsg1 = null; - - try { - encryptedMsg1 = aliceSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - decryptedMsg1 = null; - try { - decryptedMsg1 = bobSession.decryptMessage(encryptedMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg1); - assertTrue(clearMsg1.equals(decryptedMsg1)); - - // comparison test - assertTrue(clearMsg1.equals(decryptedMsg1)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - - @Test - public void test03AliceBobSessionId() { - // creates alice & bob accounts - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // CREATE ALICE SESSION - - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE SESSION - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - e.printStackTrace(); - assertTrue(e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - String aliceSessionId = null; - try { - aliceSessionId = aliceSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(aliceSessionId); - - String bobSessionId = null; - try { - bobSessionId = bobSession.sessionIdentifier(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(bobSessionId); - - // must be the same for both ends of the conversation - assertTrue(aliceSessionId.equals(bobSessionId)); - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - } - - @Test - public void test04MatchInboundSession() { - OlmAccount aliceAccount=null, bobAccount=null; - OlmSession aliceSession = null, bobSession = null; - - // ACCOUNTS CREATION - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(), false); - } - - // CREATE ALICE SESSION - try { - aliceSession = new OlmSession(); - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg=" + e.getMessage(), false); - } - - // get bob/luke identity key - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - Map<String, String> aliceIdentityKeys = null; - - try { - aliceIdentityKeys = aliceAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); - String aliceIdentityKey = TestHelper.getIdentityKey(aliceIdentityKeys); - - // get bob/luke one time keys - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - try { - aliceAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String bobOneTimeKey1 = TestHelper.getOneTimeKey(bobOneTimeKeys, 1); - - // create alice inbound session for bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String aliceClearMsg = "hello helooo to bob!"; - OlmMessage encryptedAliceToBobMsg1 = null; - - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(aliceClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertFalse(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText)); - - // init bob session with alice PRE KEY - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // test matchesInboundSession() and matchesInboundSessionFrom() - assertTrue(bobSession.matchesInboundSession(encryptedAliceToBobMsg1.mCipherText)); - assertTrue(bobSession.matchesInboundSessionFrom(aliceIdentityKey, encryptedAliceToBobMsg1.mCipherText)); - // following requires olm native lib new version with https://github.com/matrix-org/olm-backup/commit/7e9f3bebb8390f975a76c0188ce4cb460fe6692e - //assertTrue(false==bobSession.matchesInboundSessionFrom(bobIdentityKey, encryptedAliceToBobMsg1.mCipherText)); - - // release objects - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - aliceSession.releaseSession(); - bobSession.releaseSession(); - assertTrue(aliceSession.isReleased()); - assertTrue(bobSession.isReleased()); - } - - // ******************************************************** - // ************* SERIALIZATION TEST *********************** - // ******************************************************** - /** - * Same as {@link #test02AliceToBobBackAndForth()}, but alice's session - * is serialized and de-serialized before performing the final - * comparison (encrypt vs ) - */ - @Test - public void test05SessionSerialization() { - final int ONE_TIME_KEYS_NUMBER = 1; - String bobIdentityKey; - String bobOneTimeKey; - OlmAccount aliceAccount = null; - OlmAccount bobAccount = null; - OlmSession aliceSessionDeserial = null; - - // creates alice & bob accounts - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - - // test accounts creation - assertTrue(0!=bobAccount.getOlmAccountId()); - assertTrue(0!=aliceAccount.getOlmAccountId()); - - // get bob identity key - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); - assertTrue(null!=bobIdentityKey); - - // get bob one time keys - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=aliceSession.getOlmSessionId()); - - // CREATE ALICE OUTBOUND SESSION and encrypt message to bob - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - String helloClearMsg = "Hello I'm Alice!"; - - OlmMessage encryptedAliceToBobMsg1 = null; - try { - encryptedAliceToBobMsg1 = aliceSession.encryptMessage(helloClearMsg); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedAliceToBobMsg1); - assertNotNull(encryptedAliceToBobMsg1.mCipherText); - - // CREATE BOB INBOUND SESSION and decrypt message from alice - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - assertTrue(0!=bobSession.getOlmSessionId()); - - // init bob session with alice PRE KEY - try { - bobSession.initInboundSession(bobAccount, encryptedAliceToBobMsg1.mCipherText); - } catch (Exception e) { - assertTrue("initInboundSessionWithAccount failed " + e.getMessage(), false); - } - - // DECRYPT MESSAGE FROM ALICE - String decryptedMsg01 = null; - - try { - decryptedMsg01 = bobSession.decryptMessage(encryptedAliceToBobMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(decryptedMsg01); - - // MESSAGE COMPARISON: decrypted vs encrypted - assertTrue(helloClearMsg.equals(decryptedMsg01)); - - // BACK/FORTH MESSAGE COMPARISON - String clearMsg1 = "Hello I'm Bob!"; - String clearMsg2 = "Isn't life grand?"; - String clearMsg3 = "Let's go to the opera."; - - // bob encrypts messages - OlmMessage encryptedMsg1 = null; - try { - encryptedMsg1 = bobSession.encryptMessage(clearMsg1); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg1); - - OlmMessage encryptedMsg2 = null; - try { - encryptedMsg2 = bobSession.encryptMessage(clearMsg2); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg2); - - OlmMessage encryptedMsg3 = null; - try { - encryptedMsg3 = bobSession.encryptMessage(clearMsg3); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsg3); - - // serialize alice session - Context context = getInstrumentation().getContext(); - try { - FileOutputStream fileOutput = context.openFileOutput(FILE_NAME_SERIAL_SESSION, Context.MODE_PRIVATE); - ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput); - objectOutput.writeObject(aliceSession); - objectOutput.flush(); - objectOutput.close(); - - // deserialize session - FileInputStream fileInput = context.openFileInput(FILE_NAME_SERIAL_SESSION); - ObjectInputStream objectInput = new ObjectInputStream(fileInput); - aliceSessionDeserial = (OlmSession) objectInput.readObject(); - objectInput.close(); - - // test deserialize return value - assertNotNull(aliceSessionDeserial); - - // de-serialized alice session decrypts bob's messages - String decryptedMsg1 = aliceSessionDeserial.decryptMessage(encryptedMsg1); - assertNotNull(decryptedMsg1); - String decryptedMsg2 = aliceSessionDeserial.decryptMessage(encryptedMsg2); - assertNotNull(decryptedMsg2); - String decryptedMsg3 = aliceSessionDeserial.decryptMessage(encryptedMsg3); - assertNotNull(decryptedMsg3); - - // comparison tests - assertTrue(clearMsg1.equals(decryptedMsg1)); - assertTrue(clearMsg2.equals(decryptedMsg2)); - assertTrue(clearMsg3.equals(decryptedMsg3)); - - // clean objects.. - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - bobAccount.releaseAccount(); - aliceAccount.releaseAccount(); - assertTrue(bobAccount.isReleased()); - assertTrue(aliceAccount.isReleased()); - - bobSession.releaseSession(); - aliceSession.releaseSession(); - aliceSessionDeserial.releaseSession(); - assertTrue(bobSession.isReleased()); - assertTrue(aliceSession.isReleased()); - assertTrue(aliceSessionDeserial.isReleased()); - } - catch (FileNotFoundException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception FileNotFoundException Msg=="+e.getMessage()); - assertTrue(e.getMessage(), false); - } - catch (ClassNotFoundException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception ClassNotFoundException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - catch (IOException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception IOException Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - /*catch (OlmException e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception OlmException Msg==" + e.getMessage()); - }*/ - catch (Exception e) { - Log.e(LOG_TAG, "## test03SessionSerialization(): Exception Msg==" + e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - - - // **************************************************** - // *************** SANITY CHECK TESTS ***************** - // **************************************************** - - @Test - public void test06SanityCheckErrors() { - final int ONE_TIME_KEYS_NUMBER = 5; - OlmAccount bobAccount = null; - OlmAccount aliceAccount = null; - - // ALICE & BOB ACCOUNTS CREATION - try { - aliceAccount = new OlmAccount(); - bobAccount = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(), false); - } - - // get bob identity key - Map<String, String> bobIdentityKeys = null; - - try { - bobIdentityKeys = bobAccount.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - String bobIdentityKey = TestHelper.getIdentityKey(bobIdentityKeys); - assertTrue(null != bobIdentityKey); - - // get bob one time keys - try { - bobAccount.generateOneTimeKeys(ONE_TIME_KEYS_NUMBER); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - Map<String, Map<String, String>> bobOneTimeKeys = null; - - try { - bobOneTimeKeys = bobAccount.oneTimeKeys(); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(bobOneTimeKeys); - String bobOneTimeKey = TestHelper.getOneTimeKey(bobOneTimeKeys,1); - assertNotNull(bobOneTimeKey); - - // CREATE ALICE SESSION - OlmSession aliceSession = null; - try { - aliceSession = new OlmSession(); - } catch (OlmException e) { - assertTrue("Exception Msg=" + e.getMessage(), false); - } - - // SANITY CHECK TESTS FOR: initOutboundSessionWithAccount() - String errorMessage = null; - try { - aliceSession.initOutboundSession(null, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, null, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - errorMessage = null; - try { - aliceSession.initOutboundSession(null, null, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null != errorMessage); - - // init properly - errorMessage = null; - try { - aliceSession.initOutboundSession(aliceAccount, bobIdentityKey, bobOneTimeKey); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - assertTrue(null == errorMessage); - - // SANITY CHECK TESTS FOR: encryptMessage() - OlmMessage message = null; - try { - message = aliceSession.encryptMessage(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertTrue(null==message); - - // encrypt properly - OlmMessage encryptedMsgToBob = null; - try { - encryptedMsgToBob = aliceSession.encryptMessage("A message for bob"); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - assertNotNull(encryptedMsgToBob); - - // SANITY CHECK TESTS FOR: initInboundSessionWithAccount() - OlmSession bobSession = null; - try { - bobSession = new OlmSession(); - errorMessage = null; - try { - bobSession.initInboundSession(null, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, null); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, INVALID_PRE_KEY); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(!TextUtils.isEmpty(errorMessage)); - - // init properly - errorMessage = null; - try { - bobSession.initInboundSession(bobAccount, encryptedMsgToBob.mCipherText); - } catch (Exception e) { - errorMessage = e.getMessage(); - } - - assertTrue(TextUtils.isEmpty(errorMessage)); - } catch (OlmException e) { - assertTrue("Exception Msg="+e.getMessage(), false); - } - - // SANITY CHECK TESTS FOR: decryptMessage() - String decryptedMsg = null; - try { - decryptedMsg = aliceSession.decryptMessage(null); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertTrue(null==decryptedMsg); - - // SANITY CHECK TESTS FOR: matchesInboundSession() - assertTrue(!aliceSession.matchesInboundSession(null)); - - // SANITY CHECK TESTS FOR: matchesInboundSessionFrom() - assertTrue(!aliceSession.matchesInboundSessionFrom(null,null)); - - // release objects - try { - bobAccount.removeOneTimeKeys(bobSession); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - aliceAccount.releaseAccount(); - bobAccount.releaseAccount(); - assertTrue(aliceAccount.isReleased()); - assertTrue(bobAccount.isReleased()); - - aliceSession.releaseSession(); - bobSession.releaseSession(); - assertTrue(aliceSession.isReleased()); - assertTrue(bobSession.isReleased()); - } - -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java deleted file mode 100644 index b560bff..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/OlmUtilityTest.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.support.test.runner.AndroidJUnit4; -import android.text.TextUtils; -import android.util.Log; - -import org.json.JSONObject; -import org.junit.BeforeClass; -import org.junit.FixMethodOrder; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.MethodSorters; - -import java.util.Map; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -@RunWith(AndroidJUnit4.class) -@FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class OlmUtilityTest { - private static final String LOG_TAG = "OlmAccountTest"; - private static final int GENERATION_ONE_TIME_KEYS_NUMBER = 50; - - private static OlmManager mOlmManager; - - @BeforeClass - public static void setUpClass(){ - // load native lib - mOlmManager = new OlmManager(); - - String version = mOlmManager.getOlmLibVersion(); - assertNotNull(version); - Log.d(LOG_TAG, "## setUpClass(): lib version="+version); - } - - /** - * Test the signing API - */ - @Test - public void test01VerifyEd25519Signing() { - String fingerPrintKey = null; - String errorMsg = null; - String message = "{\"algorithms\":[\"m.megolm.v1.aes-sha2\",\"m.olm.v1.curve25519-aes-sha2\"],\"device_id\":\"YMBYCWTWCG\",\"keys\":{\"curve25519:YMBYCWTWCG\":\"KZFa5YUXV2EOdhK8dcGMMHWB67stdgAP4+xwiS69mCU\",\"ed25519:YMBYCWTWCG\":\"0cEgQJJqjgtXUGp4ZXQQmh36RAxwxr8HJw2E9v1gvA0\"},\"user_id\":\"@mxBob14774891254276b253f42-f267-43ec-bad9-767142bfea30:localhost:8480\"}"; - OlmAccount account = null; - - // create account - try { - account = new OlmAccount(); - } catch (OlmException e) { - assertTrue(e.getMessage(),false); - } - assertNotNull(account); - - // sign message - String messageSignature = null; - - try { - messageSignature = account.signMessage(message); - } catch (Exception e) { - assertTrue(e.getMessage(), false); - } - - assertNotNull(messageSignature); - - // get identities key (finger print key) - Map<String, String> identityKeys = null; - - try { - identityKeys = account.identityKeys(); - } catch (Exception e) { - assertTrue("identityKeys failed " + e.getMessage(), false); - } - - assertNotNull(identityKeys); - fingerPrintKey = TestHelper.getFingerprintKey(identityKeys); - assertTrue("fingerprint key missing",!TextUtils.isEmpty(fingerPrintKey)); - - // instantiate utility object - OlmUtility utility = null; - - try { - utility = new OlmUtility(); - } catch (Exception e) { - assertTrue("failed to create OlmUtility", false); - } - - // verify signature - errorMsg = null; - try { - utility.verifyEd25519Signature(messageSignature, fingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(TextUtils.isEmpty(errorMsg)); - - // check a bad signature is detected => errorMsg = BAD_MESSAGE_MAC - String badSignature = "Bad signature Bad signature Bad signature.."; - - errorMsg = null; - try { - utility.verifyEd25519Signature(badSignature, fingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(!TextUtils.isEmpty(errorMsg)); - - // check bad fingerprint size => errorMsg = INVALID_BASE64 - String badSizeFingerPrintKey = fingerPrintKey.substring(fingerPrintKey.length()/2); - - errorMsg = null; - try { - utility.verifyEd25519Signature(messageSignature, badSizeFingerPrintKey, message); - } catch (Exception e) { - errorMsg = e.getMessage(); - } - assertTrue(!TextUtils.isEmpty(errorMsg)); - - utility.releaseUtility(); - assertTrue(utility.isReleased()); - - account.releaseAccount(); - assertTrue(account.isReleased()); - } - - @Test - public void test02sha256() { - OlmUtility utility = null; - - try { - utility = new OlmUtility(); - } catch (Exception e) { - assertTrue("OlmUtility creation failed", false); - } - String msgToHash = "The quick brown fox jumps over the lazy dog"; - - String hashResult = utility.sha256(msgToHash); - assertFalse(TextUtils.isEmpty(hashResult)); - - utility.releaseUtility(); - assertTrue(utility.isReleased()); - } -} diff --git a/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java b/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java deleted file mode 100644 index 4451f7a..0000000 --- a/android/olm-sdk/src/androidTest/java/org/matrix/olm/TestHelper.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import java.util.ArrayList; -import java.util.Map; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -/** - * Helper class providing helper methods used in the Olm Android SDK unit tests. - */ -public class TestHelper { - - /** - * Return the identity key {@link OlmAccount#JSON_KEY_IDENTITY_KEY} from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()} - * @return identity key string if operation succeed, null otherwise - */ - static public String getIdentityKey(Map<String, String> aIdentityKeysMap){ - String idKey = null; - - try { - idKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_IDENTITY_KEY); - } catch (Exception e) { - assertTrue("Exception MSg=" + e.getMessage(), false); - } - return idKey; - } - - /** - * Return the fingerprint key {@link OlmAccount#JSON_KEY_FINGER_PRINT_KEY} from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#identityKeys()} - * @return fingerprint key string if operation succeed, null otherwise - */ - static public String getFingerprintKey(Map<String, String> aIdentityKeysMap) { - String fingerprintKey = null; - - try { - fingerprintKey = aIdentityKeysMap.get(OlmAccount.JSON_KEY_FINGER_PRINT_KEY); - } catch (Exception e) { - assertTrue("Exception MSg=" + e.getMessage(), false); - } - return fingerprintKey; - } - - /** - * Return the first one time key from the JSON object. - * @param aIdentityKeysMap result of {@link OlmAccount#oneTimeKeys()} - * @param aKeyPosition the position of the key to be retrieved - * @return one time key string if operation succeed, null otherwise - */ - static public String getOneTimeKey(Map<String, Map<String, String>> aIdentityKeysMap, int aKeyPosition) { - String firstOneTimeKey = null; - - try { - Map<String, String> generatedKeys = aIdentityKeysMap.get(OlmAccount.JSON_KEY_ONE_TIME_KEY); - assertNotNull(OlmAccount.JSON_KEY_ONE_TIME_KEY + " object is missing", generatedKeys); - - firstOneTimeKey = (new ArrayList<>(generatedKeys.values())).get(aKeyPosition - 1); - } catch (Exception e) { - assertTrue("Exception Msg=" + e.getMessage(), false); - } - return firstOneTimeKey; - } -} diff --git a/android/olm-sdk/src/main/AndroidManifest.xml b/android/olm-sdk/src/main/AndroidManifest.xml deleted file mode 100644 index 902f2e3..0000000 --- a/android/olm-sdk/src/main/AndroidManifest.xml +++ /dev/null @@ -1,8 +0,0 @@ -<manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="org.matrix.olm"> - - <application - android:allowBackup="true" - android:label="@string/app_name"> - </application> -</manifest> diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java b/android/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java deleted file mode 100644 index 229963f..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/CommonSerializeUtils.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.util.Log; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -/** - * Helper class dedicated to serialization mechanism (template method pattern). - */ -abstract class CommonSerializeUtils { - private static final String LOG_TAG = "CommonSerializeUtils"; - - /** - * Kick off the serialization mechanism. - * @param aOutStream output stream for serializing - * @throws IOException exception - */ - protected void serialize(ObjectOutputStream aOutStream) throws IOException { - aOutStream.defaultWriteObject(); - - // generate serialization key - byte[] key = OlmUtility.getRandomKey(); - - // compute pickle string - StringBuffer errorMsg = new StringBuffer(); - byte[] pickledData = serialize(key, errorMsg); - - if(null == pickledData) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_SERIALIZATION, String.valueOf(errorMsg)); - } else { - aOutStream.writeObject(new String(key, "UTF-8")); - aOutStream.writeObject(new String(pickledData, "UTF-8")); - } - } - - /** - * Kick off the deserialization mechanism. - * @param aInStream input stream - * @throws Exception the exception - */ - protected void deserialize(ObjectInputStream aInStream) throws Exception { - aInStream.defaultReadObject(); - - String keyAsString = (String)aInStream.readObject(); - String pickledDataAsString = (String)aInStream.readObject(); - - byte[] key; - byte[] pickledData; - - try { - key = keyAsString.getBytes("UTF-8"); - pickledData = pickledDataAsString.getBytes("UTF-8"); - - deserialize(pickledData, key); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, e.getMessage()); - } - - Log.d(LOG_TAG,"## deserializeObject(): success"); - } - - protected abstract byte[] serialize(byte[] aKey, StringBuffer aErrorMsg); - protected abstract void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception; -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java deleted file mode 100644 index 98a3c5b..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java +++ /dev/null @@ -1,420 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.text.TextUtils; -import android.util.Log; - -import org.json.JSONObject; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; -import java.util.Arrays; -import java.util.Map; - -/** - * Account class used to create Olm sessions in conjunction with {@link OlmSession} class.<br> - * OlmAccount provides APIs to retrieve the Olm keys. - *<br><br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmAccount extends CommonSerializeUtils implements Serializable { - private static final long serialVersionUID = 3497486121598434824L; - private static final String LOG_TAG = "OlmAccount"; - - // JSON keys used in the JSON objects returned by JNI - /** As well as the identity key, each device creates a number of Curve25519 key pairs which are - also used to establish Olm sessions, but can only be used once. Once again, the private part - remains on the device. but the public part is published to the Matrix network **/ - public static final String JSON_KEY_ONE_TIME_KEY = "curve25519"; - - /** Curve25519 identity key is a public-key cryptographic system which can be used to establish a shared - secret.<br>In Matrix, each device has a long-lived Curve25519 identity key which is used to establish - Olm sessions with that device. The private key should never leave the device, but the - public part is signed with the Ed25519 fingerprint key ({@link #JSON_KEY_FINGER_PRINT_KEY}) and published to the network. **/ - public static final String JSON_KEY_IDENTITY_KEY = "curve25519"; - - /** Ed25519 finger print is a public-key cryptographic system for signing messages.<br>In Matrix, each device has - an Ed25519 key pair which serves to identify that device. The private the key should - never leave the device, but the public part is published to the Matrix network. **/ - public static final String JSON_KEY_FINGER_PRINT_KEY = "ed25519"; - - /** Account Id returned by JNI. - * This value identifies uniquely the native account instance. - */ - private transient long mNativeId; - - public OlmAccount() throws OlmException { - try { - mNativeId = createNewAccountJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage()); - } - } - - /** - * Create a new account and return it to JAVA side.<br> - * Since a C prt is returned as a jlong, special care will be taken - * to make the cast (OlmAccount* to jlong) platform independent. - * @return the initialized OlmAccount* instance or throw an exception if fails - **/ - private native long createNewAccountJni(); - - /** - * Getter on the account ID. - * @return native account ID - */ - long getOlmAccountId(){ - return mNativeId; - } - - /** - * Release native account and invalid its JAVA reference counter part.<br> - * Public API for {@link #releaseAccountJni()}. - */ - public void releaseAccount() { - if (0 != mNativeId) { - releaseAccountJni(); - } - mNativeId = 0; - } - - /** - * Destroy the corresponding OLM account native object.<br> - * This method must ALWAYS be called when this JAVA instance - * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #createNewAccountJni()}. - */ - private native void releaseAccountJni(); - - /** - * Return true the object resources have been released.<br> - * @return true the object resources have been released - */ - public boolean isReleased() { - return (0 == mNativeId); - } - - /** - * Return the identity keys (identity and fingerprint keys) in a dictionary.<br> - * Public API for {@link #identityKeysJni()}.<br> - * Ex:<tt> - * { - * "curve25519":"Vam++zZPMqDQM6ANKpO/uAl5ViJSHxV9hd+b0/fwRAg", - * "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I" - * }</tt> - * @return identity keys dictionary if operation succeeds, null otherwise - * @exception OlmException the failure reason - */ - public Map<String, String> identityKeys() throws OlmException { - JSONObject identityKeysJsonObj = null; - - byte[] identityKeysBuffer; - - try { - identityKeysBuffer = identityKeysJni(); - } catch (Exception e) { - Log.e(LOG_TAG, "## identityKeys(): Failure - " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS, e.getMessage()); - } - - if (null != identityKeysBuffer) { - try { - identityKeysJsonObj = new JSONObject(new String(identityKeysBuffer, "UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## identityKeys(): Exception - Msg=" + e.getMessage()); - } - } else { - Log.e(LOG_TAG, "## identityKeys(): Failure - identityKeysJni()=null"); - } - - return OlmUtility.toStringMap(identityKeysJsonObj); - } - - /** - * Get the public identity keys (Ed25519 fingerprint key and Curve25519 identity key).<br> - * Keys are Base64 encoded. - * These keys must be published on the server. - * @return the identity keys or throw an exception if it fails - */ - private native byte[] identityKeysJni(); - - /** - * Return the largest number of "one time keys" this account can store. - * @return the max number of "one time keys", -1 otherwise - */ - public long maxOneTimeKeys() { - return maxOneTimeKeysJni(); - } - - /** - * Return the largest number of "one time keys" this account can store. - * @return the max number of "one time keys", -1 otherwise - */ - private native long maxOneTimeKeysJni(); - - /** - * Generate a number of new one time keys.<br> If total number of keys stored - * by this account exceeds {@link #maxOneTimeKeys()}, the old keys are discarded.<br> - * The corresponding keys are retrieved by {@link #oneTimeKeys()}. - * @param aNumberOfKeys number of keys to generate - * @exception OlmException the failure reason - */ - public void generateOneTimeKeys(int aNumberOfKeys) throws OlmException { - try { - generateOneTimeKeysJni(aNumberOfKeys); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS, e.getMessage()); - } - } - - /** - * Generate a number of new one time keys.<br> If total number of keys stored - * by this account exceeds {@link #maxOneTimeKeys()}, the old keys are discarded. - * An exception is thrown if the operation fails.<br> - * @param aNumberOfKeys number of keys to generate - */ - private native void generateOneTimeKeysJni(int aNumberOfKeys); - - /** - * Return the "one time keys" in a dictionary.<br> - * The number of "one time keys", is specified by {@link #generateOneTimeKeys(int)}<br> - * Ex:<tt> - * { "curve25519": - * { - * "AAAABQ":"qefVZd8qvjOpsFzoKSAdfUnJVkIreyxWFlipCHjSQQg", - * "AAAABA":"/X8szMU+p+lsTnr56wKjaLgjTMQQkCk8EIWEAilZtQ8", - * "AAAAAw":"qxNxxFHzevFntaaPdT0fhhO7tc7pco4+xB/5VRG81hA", - * } - * }</tt><br> - * Public API for {@link #oneTimeKeysJni()}.<br> - * Note: these keys are to be published on the server. - * @return one time keys in string dictionary. - * @exception OlmException the failure reason - */ - public Map<String, Map<String, String>> oneTimeKeys() throws OlmException { - JSONObject oneTimeKeysJsonObj = null; - byte[] oneTimeKeysBuffer; - - try { - oneTimeKeysBuffer = oneTimeKeysJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS, e.getMessage()); - } - - if( null != oneTimeKeysBuffer) { - try { - oneTimeKeysJsonObj = new JSONObject(new String(oneTimeKeysBuffer, "UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## oneTimeKeys(): Exception - Msg=" + e.getMessage()); - } - } else { - Log.e(LOG_TAG, "## oneTimeKeys(): Failure - identityKeysJni()=null"); - } - - return OlmUtility.toStringMapMap(oneTimeKeysJsonObj); - } - - /** - * Get the public parts of the unpublished "one time keys" for the account.<br> - * The returned data is a JSON-formatted object with the single property - * <tt>curve25519</tt>, which is itself an object mapping key id to - * base64-encoded Curve25519 key.<br> - * @return byte array containing the one time keys or throw an exception if it fails - */ - private native byte[] oneTimeKeysJni(); - - /** - * Remove the "one time keys" that the session used from the account. - * @param aSession session instance - * @throws OlmException the failure reason - */ - public void removeOneTimeKeys(OlmSession aSession) throws OlmException { - if (null != aSession) { - try { - removeOneTimeKeysJni(aSession.getOlmSessionId()); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS, e.getMessage()); - } - } - } - - /** - * Remove the "one time keys" that the session used from the account. - * An exception is thrown if the operation fails. - * @param aNativeOlmSessionId native session instance identifier - */ - private native void removeOneTimeKeysJni(long aNativeOlmSessionId); - - /** - * Marks the current set of "one time keys" as being published. - * @exception OlmException the failure reason - */ - public void markOneTimeKeysAsPublished() throws OlmException { - try { - markOneTimeKeysAsPublishedJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED, e.getMessage()); - } - } - - /** - * Marks the current set of "one time keys" as being published. - * An exception is thrown if the operation fails. - */ - private native void markOneTimeKeysAsPublishedJni(); - - /** - * Sign a message with the ed25519 fingerprint key for this account.<br> - * The signed message is returned by the method. - * @param aMessage message to sign - * @return the signed message - * @exception OlmException the failure reason - */ - public String signMessage(String aMessage) throws OlmException { - String result = null; - - if (null != aMessage) { - byte[] utf8String = null; - try { - utf8String = aMessage.getBytes("UTF-8"); - if (null != utf8String) { - byte[] signedMessage = signMessageJni(utf8String); - - if (null != signedMessage) { - result = new String(signedMessage, "UTF-8"); - } - } - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE, e.getMessage()); - } finally { - if (null != utf8String) { - Arrays.fill(utf8String, (byte) 0); - } - } - } - - return result; - } - - /** - * Sign a message with the ed25519 fingerprint key for this account.<br> - * The signed message is returned by the method. - * @param aMessage message to sign - * @return the signed message - */ - private native byte[] signMessageJni(byte[] aMessage); - - //============================================================================================================== - // Serialization management - //============================================================================================================== - - /** - * Kick off the serialization mechanism. - * @param aOutStream output stream for serializing - * @throws IOException exception - */ - private void writeObject(ObjectOutputStream aOutStream) throws IOException { - serialize(aOutStream); - } - - /** - * Kick off the deserialization mechanism. - * @param aInStream input stream - * @throws Exception exception - */ - private void readObject(ObjectInputStream aInStream) throws Exception { - deserialize(aInStream); - } - - /** - * Return an account as a bytes buffer.<br> - * The account is serialized and encrypted with aKey. - * In case of failure, an error human readable - * description is provide in aErrorMsg. - * @param aKey encryption key - * @param aErrorMsg error message description - * @return the account as bytes buffer - */ - @Override - protected byte[] serialize(byte[] aKey, StringBuffer aErrorMsg) { - byte[] pickleRetValue = null; - - // sanity check - if(null == aErrorMsg) { - Log.e(LOG_TAG,"## serialize(): invalid parameter - aErrorMsg=null"); - } else if (null == aKey) { - aErrorMsg.append("Invalid input parameters in serializeDataWithKey()"); - } else { - aErrorMsg.setLength(0); - try { - pickleRetValue = serializeJni(aKey); - } catch (Exception e) { - Log.e(LOG_TAG, "## serialize() failed " + e.getMessage()); - aErrorMsg.append(e.getMessage()); - } - } - - return pickleRetValue; - } - - /** - * Serialize and encrypt account instance.<br> - * @param aKeyBuffer key used to encrypt the serialized account data - * @return the serialised account as bytes buffer. - **/ - private native byte[] serializeJni(byte[] aKeyBuffer); - - /** - * Loads an account from a pickled bytes buffer.<br> - * See {@link #serialize(byte[], StringBuffer)} - * @param aSerializedData bytes buffer - * @param aKey key used to encrypted - * @exception Exception the exception - */ - @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { - String errorMsg = null; - - try { - if ((null == aSerializedData) || (null == aKey)) { - Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg = "invalid input parameters"; - } else { - mNativeId = deserializeJni(aSerializedData, aKey); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg = e.getMessage(); - } - - if (!TextUtils.isEmpty(errorMsg)) { - releaseAccount(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); - } - } - - /** - * Allocate a new account and initialize it with the serialisation data.<br> - * @param aSerializedDataBuffer the account serialisation buffer - * @param aKeyBuffer the key used to encrypt the serialized account data - * @return the deserialized account - **/ - private native long deserializeJni(byte[] aSerializedDataBuffer, byte[] aKeyBuffer); -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java deleted file mode 100644 index 5b4a85a..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017-2019 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import java.io.IOException; - -/** - * Exception class to identify specific Olm SDK exceptions. - */ -public class OlmException extends IOException { - // exception codes - - public static final int EXCEPTION_CODE_INIT_ACCOUNT_CREATION = 10; - - public static final int EXCEPTION_CODE_ACCOUNT_SERIALIZATION = 100; - public static final int EXCEPTION_CODE_ACCOUNT_DESERIALIZATION = 101; - public static final int EXCEPTION_CODE_ACCOUNT_IDENTITY_KEYS = 102; - public static final int EXCEPTION_CODE_ACCOUNT_GENERATE_ONE_TIME_KEYS = 103; - public static final int EXCEPTION_CODE_ACCOUNT_ONE_TIME_KEYS = 104; - public static final int EXCEPTION_CODE_ACCOUNT_REMOVE_ONE_TIME_KEYS = 105; - public static final int EXCEPTION_CODE_ACCOUNT_MARK_ONE_KEYS_AS_PUBLISHED = 106; - public static final int EXCEPTION_CODE_ACCOUNT_SIGN_MESSAGE = 107; - - public static final int EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION = 200; - public static final int EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION = 201; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER = 202; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION = 203; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_FIRST_KNOWN_INDEX = 204; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_IS_VERIFIED = 205; - public static final int EXCEPTION_CODE_INBOUND_GROUP_SESSION_EXPORT = 206; - - public static final int EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION = 300; - public static final int EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION = 301; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER = 302; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY = 303; - public static final int EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE = 304; - - public static final int EXCEPTION_CODE_INIT_SESSION_CREATION = 400; - public static final int EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION = 401; - public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION = 402; - public static final int EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM = 403; - public static final int EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE = 404; - public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405; - public static final int EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER = 406; - - public static final int EXCEPTION_CODE_UTILITY_CREATION = 500; - public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 501; - - public static final int EXCEPTION_CODE_PK_ENCRYPTION_CREATION = 600; - public static final int EXCEPTION_CODE_PK_ENCRYPTION_SET_RECIPIENT_KEY = 601; - public static final int EXCEPTION_CODE_PK_ENCRYPTION_ENCRYPT = 602; - - public static final int EXCEPTION_CODE_PK_DECRYPTION_CREATION = 700; - public static final int EXCEPTION_CODE_PK_DECRYPTION_GENERATE_KEY = 701; - public static final int EXCEPTION_CODE_PK_DECRYPTION_DECRYPT = 702; - public static final int EXCEPTION_CODE_PK_DECRYPTION_SET_PRIVATE_KEY = 703; - public static final int EXCEPTION_CODE_PK_DECRYPTION_PRIVATE_KEY = 704; - - public static final int EXCEPTION_CODE_PK_SIGNING_CREATION = 800; - public static final int EXCEPTION_CODE_PK_SIGNING_GENERATE_SEED = 801; - public static final int EXCEPTION_CODE_PK_SIGNING_INIT_WITH_SEED = 802; - public static final int EXCEPTION_CODE_PK_SIGNING_SIGN = 803; - - public static final int EXCEPTION_CODE_SAS_CREATION = 900; - public static final int EXCEPTION_CODE_SAS_ERROR = 901; - public static final int EXCEPTION_CODE_SAS_MISSING_THEIR_PKEY = 902; - public static final int EXCEPTION_CODE_SAS_GENERATE_SHORT_CODE = 903; - - // exception human readable messages - public static final String EXCEPTION_MSG_INVALID_PARAMS_DESERIALIZATION = "invalid de-serialized parameters"; - - /** exception code to be taken from: {@link #EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION}, {@link #EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION}, - * {@link #EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION}, {@link #EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION}..**/ - private final int mCode; - - /** Human readable message description **/ - private final String mMessage; - - public OlmException(int aExceptionCode, String aExceptionMessage) { - super(); - mCode = aExceptionCode; - mMessage = aExceptionMessage; - } - - public int getExceptionCode() { - return mCode; - } - - @Override - public String getMessage() { - return mMessage; - } -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java deleted file mode 100644 index 2fc81ef..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ /dev/null @@ -1,372 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.text.TextUtils; -import android.util.Log; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -import java.util.Arrays; - -/** - * Class used to create an inbound <a href="http://matrix.org/docs/guides/e2e_implementation.html#handling-an-m-room-key-event">Megolm session</a>.<br> - * Counter part of the outbound group session {@link OlmOutboundGroupSession}, this class decrypts the messages sent by the outbound side. - * - * <br><br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmInboundGroupSession extends CommonSerializeUtils implements Serializable { - private static final long serialVersionUID = -772028491251653253L; - private static final String LOG_TAG = "OlmInboundGroupSession"; - - /** Session Id returned by JNI.<br> - * This value uniquely identifies the native inbound group session instance. - */ - private transient long mNativeId; - - /** - * Result in {@link #decryptMessage(String)} - */ - public static class DecryptMessageResult { - /** decrypt message **/ - public String mDecryptedMessage; - - /** decrypt index **/ - public long mIndex; - } - - /** - * Constructor.<br> - * Create and save a new native session instance ID and start a new inbound group session. - * The session key parameter is retrieved from an outbound group session. - * @param aSessionKey session key - * @throws OlmException constructor failure - */ - public OlmInboundGroupSession(String aSessionKey) throws OlmException { - this(aSessionKey, false); - } - - /** - * Constructor.<br> - * Create and save a new native session instance ID and start a new inbound group session. - * The session key parameter is retrieved from an outbound group session. - * @param aSessionKey session key - * @param isImported true when the session key has been retrieved from a backup - * @throws OlmException constructor failure - */ - private OlmInboundGroupSession(String aSessionKey, boolean isImported) throws OlmException { - if (TextUtils.isEmpty(aSessionKey)) { - Log.e(LOG_TAG, "## initInboundGroupSession(): invalid session key"); - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION, "invalid session key"); - } else { - byte[] sessionBuffer = null; - try { - sessionBuffer = aSessionKey.getBytes("UTF-8"); - mNativeId = createNewSessionJni(aSessionKey.getBytes("UTF-8"), isImported); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION, e.getMessage()); - } finally { - if (null != sessionBuffer) { - Arrays.fill(sessionBuffer, (byte) 0); - } - } - } - } - - /** - * Initialize a new inbound group session and return it to JAVA side.<br> - * Since a C prt is returned as a jlong, special care will be taken - * to make the cast (OlmInboundGroupSession* to jlong) platform independent. - * @param aSessionKeyBuffer session key from an outbound session - * @param isImported true when the session key has been retrieved from a backup - * @return the initialized OlmInboundGroupSession* instance or throw an exception it fails. - **/ - private native long createNewSessionJni(byte[] aSessionKeyBuffer, boolean isImported); - - /** - * Create an OlmInboundGroupSession from its exported session data. - * @param exported the exported data - * @return the created OlmException - * @throws OlmException the failure reason - */ - public static OlmInboundGroupSession importSession(String exported) throws OlmException { - return new OlmInboundGroupSession(exported, true); - } - - /** - * Release native session and invalid its JAVA reference counter part.<br> - * Public API for {@link #releaseSessionJni()}. - */ - public void releaseSession(){ - if (0 != mNativeId) { - releaseSessionJni(); - } - mNativeId = 0; - } - - /** - * Destroy the corresponding OLM inbound group session native object.<br> - * This method must ALWAYS be called when this JAVA instance - * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #createNewSessionJni(byte[], boolean)}. - */ - private native void releaseSessionJni(); - - /** - * Return true the object resources have been released.<br> - * @return true the object resources have been released - */ - public boolean isReleased() { - return (0 == mNativeId); - } - - /** - * Retrieve the base64-encoded identifier for this inbound group session. - * @return the session ID - * @throws OlmException the failure reason - */ - public String sessionIdentifier() throws OlmException { - try { - return new String(sessionIdentifierJni(), "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_IDENTIFIER, e.getMessage()); - } - } - - /** - * Get a base64-encoded identifier for this inbound group session. - * An exception is thrown if the operation fails. - * @return the base64-encoded identifier - */ - private native byte[] sessionIdentifierJni(); - - /** - * Provides the first known index. - * @return the first known index. - * @throws OlmException the failure reason - */ - public long getFirstKnownIndex() throws OlmException { - long index = 0; - - try { - index = firstKnownIndexJni(); - } catch (Exception e) { - Log.e(LOG_TAG, "## getFirstKnownIndex() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_FIRST_KNOWN_INDEX, e.getMessage()); - } - - return index; - } - - /** - * Provides the first known index. - * An exception is thrown if the operation fails. - * @return the first known index. - */ - private native long firstKnownIndexJni(); - - /** - * Tells if the session is verified. - * @return true if the session is verified - * @throws OlmException the failure reason - */ - public boolean isVerified() throws OlmException { - boolean isVerified; - - try { - isVerified = isVerifiedJni(); - } catch (Exception e) { - Log.e(LOG_TAG, "## isVerified() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_IS_VERIFIED, e.getMessage()); - } - - return isVerified; - } - - /** - * Tells if the session is verified. - * @return true if the session is verified - */ - private native boolean isVerifiedJni(); - - /** - * Export the session from a message index as String. - * @param messageIndex the message index - * @return the session as String - * @throws OlmException the failure reason - */ - public String export(long messageIndex) throws OlmException { - String result = null; - - try { - byte[] bytesBuffer = exportJni(messageIndex); - - if (null != bytesBuffer) { - result = new String(bytesBuffer, "UTF-8"); - Arrays.fill(bytesBuffer, (byte) 0); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## export() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_EXPORT, e.getMessage()); - } - - return result; - } - - /** - * Exports the session as byte array from a message index - * An exception is thrown if the operation fails. - * @param messageIndex key used to encrypt the serialized session data - * @return the session saved as bytes array - */ - private native byte[] exportJni(long messageIndex); - - /** - * Decrypt the message passed in parameter.<br> - * In case of error, null is returned and an error message description is provided in aErrorMsg. - * @param aEncryptedMsg the message to be decrypted - * @return the decrypted message information - * @exception OlmException the failure reason - */ - public DecryptMessageResult decryptMessage(String aEncryptedMsg) throws OlmException { - DecryptMessageResult result = new DecryptMessageResult(); - - try { - byte[] decryptedMessageBuffer = decryptMessageJni(aEncryptedMsg.getBytes("UTF-8"), result); - - if (null != decryptedMessageBuffer) { - result.mDecryptedMessage = new String(decryptedMessageBuffer, "UTF-8"); - Arrays.fill(decryptedMessageBuffer, (byte) 0); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## decryptMessage() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_INBOUND_GROUP_SESSION_DECRYPT_SESSION, e.getMessage()); - } - - return result; - } - - /** - * Decrypt a message. - * An exception is thrown if the operation fails. - * @param aEncryptedMsg the encrypted message - * @param aDecryptMessageResult the decryptMessage information - * @return the decrypted message - */ - private native byte[] decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult); - - //============================================================================================================== - // Serialization management - //============================================================================================================== - - /** - * Kick off the serialization mechanism. - * @param aOutStream output stream for serializing - * @throws IOException exception - */ - private void writeObject(ObjectOutputStream aOutStream) throws IOException { - serialize(aOutStream); - } - - /** - * Kick off the deserialization mechanism. - * @param aInStream input stream - * @throws Exception exception - */ - private void readObject(ObjectInputStream aInStream) throws Exception { - deserialize(aInStream); - } - - /** - * Return the current inbound group session as a bytes buffer.<br> - * The session is serialized and encrypted with aKey. - * In case of failure, an error human readable - * description is provide in aErrorMsg. - * @param aKey encryption key - * @param aErrorMsg error message description - * @return pickled bytes buffer if operation succeed, null otherwise - */ - @Override - protected byte[] serialize(byte[] aKey, StringBuffer aErrorMsg) { - byte[] pickleRetValue = null; - - // sanity check - if(null == aErrorMsg) { - Log.e(LOG_TAG,"## serialize(): invalid parameter - aErrorMsg=null"); - } else if (null == aKey) { - aErrorMsg.append("Invalid input parameters in serialize()"); - } else { - aErrorMsg.setLength(0); - try { - pickleRetValue = serializeJni(aKey); - } catch (Exception e) { - Log.e(LOG_TAG, "## serialize() failed " + e.getMessage()); - aErrorMsg.append(e.getMessage()); - } - } - - return pickleRetValue; - } - /** - * JNI counter part of {@link #serialize(byte[], StringBuffer)}. - * @param aKey encryption key - * @return the serialized session - */ - private native byte[] serializeJni(byte[] aKey); - - /** - * Loads an account from a pickled base64 string.<br> - * See {@link #serialize(byte[], StringBuffer)} - * @param aSerializedData pickled account in a bytes buffer - * @param aKey key used to encrypted - */ - @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { - String errorMsg = null; - - try { - if ((null == aSerializedData) || (null == aKey)) { - Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg = "invalid input parameters"; - } else { - mNativeId = deserializeJni(aSerializedData, aKey); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg = e.getMessage(); - } - - if (!TextUtils.isEmpty(errorMsg)) { - releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); - } - } - - /** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ - private native long deserializeJni(byte[] aSerializedData, byte[] aKey); -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java deleted file mode 100644 index cd33a5a..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmManager.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.content.Context; -import android.util.Log; - -/** - * Olm SDK entry point class.<br> An OlmManager instance must be created at first to enable native library load. - * <br><br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmManager { - private static final String LOG_TAG = "OlmManager"; - - /** - * Constructor. - */ - public OlmManager() { - } - - static { - try { - java.lang.System.loadLibrary("olm"); - } catch(UnsatisfiedLinkError e) { - Log.e(LOG_TAG,"Exception loadLibrary() - Msg="+e.getMessage()); - } - } - - /** - * Provide the android library version - * @return the library version - */ - public String getVersion() { - return BuildConfig.VERSION_NAME; - } - - /** - * Provide a detailed version. - * It contains the android and the native libraries versions. - * @param context the context - * @return the detailed version - */ - public String getDetailedVersion(Context context) { - String gitVersion = context.getResources().getString(R.string.git_olm_revision); - String date = context.getResources().getString(R.string.git_olm_revision_date); - return getVersion() + " - olm version (" + getOlmLibVersion() + ") - " + gitVersion + "-" + date; - } - - /** - * Provide the native OLM lib version. - * @return the lib version as a string - */ - public String getOlmLibVersion(){ - return getOlmLibVersionJni(); - } - public native String getOlmLibVersionJni(); -} - diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmMessage.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmMessage.java deleted file mode 100644 index 08db993..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmMessage.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -/** - * Message class used in Olm sessions to contain the encrypted data.<br> - * See {@link OlmSession#decryptMessage(OlmMessage)} and {@link OlmSession#encryptMessage(String)}. - * <br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmMessage { - /** PRE KEY message type (used to establish new Olm session) **/ - public final static int MESSAGE_TYPE_PRE_KEY = 0; - /** normal message type **/ - public final static int MESSAGE_TYPE_MESSAGE = 1; - - /** the encrypted message **/ - public String mCipherText; - - /** defined by {@link #MESSAGE_TYPE_MESSAGE} or {@link #MESSAGE_TYPE_PRE_KEY}**/ - public long mType; -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java deleted file mode 100644 index 55732fe..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - - -import android.text.TextUtils; -import android.util.Log; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -import java.util.Arrays; - -/** - * Class used to create an outbound a <a href="http://matrix.org/docs/guides/e2e_implementation.html#starting-a-megolm-session">Megolm session</a>.<br> - * To send a first message in an encrypted room, the client should start a new outbound Megolm session. - * The session ID and the session key must be shared with each device in the room within. - * - * <br><br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmOutboundGroupSession extends CommonSerializeUtils implements Serializable { - private static final long serialVersionUID = -3133097431283604416L; - private static final String LOG_TAG = "OlmOutboundGroupSession"; - - /** Session Id returned by JNI.<br> - * This value uniquely identifies the native outbound group session instance. - */ - private transient long mNativeId; - - /** - * Constructor.<br> - * Create and save a new session native instance ID and - * initialise a new outbound group session.<br> - * @throws OlmException constructor failure - */ - public OlmOutboundGroupSession() throws OlmException { - try { - mNativeId = createNewSessionJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, e.getMessage()); - } - } - - /** - * Create the corresponding OLM outbound group session in native side.<br> - * An exception is thrown if the operation fails. - * Do not forget to call {@link #releaseSession()} when JAVA side is done. - * @return native session instance identifier (see {@link #mNativeId}) - */ - private native long createNewSessionJni(); - - /** - * Release native session and invalid its JAVA reference counter part.<br> - * Public API for {@link #releaseSessionJni()}. - */ - public void releaseSession() { - if (0 != mNativeId) { - releaseSessionJni(); - } - mNativeId = 0; - } - - /** - * Destroy the corresponding OLM outbound group session native object.<br> - * This method must ALWAYS be called when this JAVA instance - * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #createNewSessionJni()}. - */ - private native void releaseSessionJni(); - - /** - * Return true the object resources have been released.<br> - * @return true the object resources have been released - */ - public boolean isReleased() { - return (0 == mNativeId); - } - - /** - * Get a base64-encoded identifier for this session. - * @return session identifier - * @throws OlmException the failure reason - */ - public String sessionIdentifier() throws OlmException { - try { - return new String(sessionIdentifierJni(), "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionIdentifier() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_IDENTIFIER, e.getMessage()); - } - } - - /** - * Return the session identifier. - * An exception is thrown if the operation fails. - * @return the session identifier - */ - private native byte[] sessionIdentifierJni(); - - /** - * Get the current message index for this session.<br> - * Each message is sent with an increasing index, this - * method returns the index for the next message. - * @return current session index - */ - public int messageIndex() { - return messageIndexJni(); - } - - /** - * Get the current message index for this session.<br> - * Each message is sent with an increasing index, this - * method returns the index for the next message. - * An exception is thrown if the operation fails. - * @return current session index - */ - private native int messageIndexJni(); - - /** - * Get the base64-encoded current ratchet key for this session.<br> - * Each message is sent with a different ratchet key. This method returns the - * ratchet key that will be used for the next message. - * @return outbound session key - * @exception OlmException the failure reason - */ - public String sessionKey() throws OlmException { - try { - byte[] sessionKeyBuffer = sessionKeyJni(); - String ret = new String(sessionKeyBuffer, "UTF-8"); - Arrays.fill(sessionKeyBuffer, (byte) 0); - return ret; - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionKey() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_SESSION_KEY, e.getMessage()); - } - } - - /** - * Return the session key. - * An exception is thrown if the operation fails. - * @return the session key - */ - private native byte[] sessionKeyJni(); - - /** - * Encrypt some plain-text message.<br> - * The message given as parameter is encrypted and returned as the return value. - * @param aClearMsg message to be encrypted - * @return the encrypted message - * @exception OlmException the encryption failure reason - */ - public String encryptMessage(String aClearMsg) throws OlmException { - String retValue = null; - - if (!TextUtils.isEmpty(aClearMsg)) { - try { - byte[] clearMsgBuffer = aClearMsg.getBytes("UTF-8"); - byte[] encryptedBuffer = encryptMessageJni(clearMsgBuffer); - Arrays.fill(clearMsgBuffer, (byte) 0); - - if (null != encryptedBuffer) { - retValue = new String(encryptedBuffer , "UTF-8"); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## encryptMessage() failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_OUTBOUND_GROUP_ENCRYPT_MESSAGE, e.getMessage()); - } - } - - return retValue; - } - - /** - * Encrypt a bytes buffer messages. - * An exception is thrown if the operation fails. - * @param aClearMsgBuffer the message to encode - * @return the encoded message - */ - private native byte[] encryptMessageJni(byte[] aClearMsgBuffer); - - //============================================================================================================== - // Serialization management - //============================================================================================================== - - /** - * Kick off the serialization mechanism. - * @param aOutStream output stream for serializing - * @throws IOException exception - */ - private void writeObject(ObjectOutputStream aOutStream) throws IOException { - serialize(aOutStream); - } - - /** - * Kick off the deserialization mechanism. - * @param aInStream input stream - * @throws Exception exception - */ - private void readObject(ObjectInputStream aInStream) throws Exception { - deserialize(aInStream); - } - - /** - * Return the current outbound group session as a base64 byte buffers.<br> - * The session is serialized and encrypted with aKey. - * In case of failure, an error human readable - * description is provide in aErrorMsg. - * @param aKey encryption key - * @param aErrorMsg error message description - * @return pickled base64 bytes buffer if operation succeed, null otherwise - */ - @Override - protected byte[] serialize(byte[] aKey, StringBuffer aErrorMsg) { - byte[] pickleRetValue = null; - - // sanity check - if(null == aErrorMsg) { - Log.e(LOG_TAG,"## serialize(): invalid parameter - aErrorMsg=null"); - } else if (null == aKey) { - aErrorMsg.append("Invalid input parameters in serialize()"); - } else { - try { - pickleRetValue = serializeJni(aKey); - } catch (Exception e) { - Log.e(LOG_TAG,"## serialize(): failed " + e.getMessage()); - aErrorMsg.append(e.getMessage()); - } - } - - return pickleRetValue; - } - - /** - * JNI counter part of {@link #serialize(byte[], StringBuffer)}. - * An exception is thrown if the operation fails. - * @param aKey encryption key - * @return the serialized session - */ - private native byte[] serializeJni(byte[] aKey); - - /** - * Loads an account from a pickled base64 string.<br> - * See {@link #serialize(byte[], StringBuffer)} - * @param aSerializedData pickled account in a base64 bytes buffer - * @param aKey key used to encrypted - * @exception Exception the exception - */ - @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { - String errorMsg = null; - - try { - if ((null == aSerializedData) || (null == aKey)) { - Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg = "invalid input parameters"; - } else { - mNativeId = deserializeJni(aSerializedData, aKey); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg = e.getMessage(); - } - - if (!TextUtils.isEmpty(errorMsg)) { - releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); - } - } - - /** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ - private native long deserializeJni(byte[] aSerializedData, byte[] aKey); - -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkDecryption.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkDecryption.java deleted file mode 100644 index 6a6a22b..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkDecryption.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2018 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.util.Log; - -import java.util.Arrays; - -public class OlmPkDecryption { - private static final String LOG_TAG = "OlmPkDecryption"; - - /** Session Id returned by JNI. - * This value uniquely identifies the native session instance. - **/ - private transient long mNativeId; - - public OlmPkDecryption() throws OlmException { - try { - mNativeId = createNewPkDecryptionJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_CREATION, e.getMessage()); - } - } - - private native long createNewPkDecryptionJni(); - - private native void releasePkDecryptionJni(); - - public void releaseDecryption() { - if (0 != mNativeId) { - releasePkDecryptionJni(); - } - mNativeId = 0; - } - - public boolean isReleased() { - return (0 == mNativeId); - } - - public static native int privateKeyLength(); - - public String setPrivateKey(byte[] privateKey) throws OlmException { - try { - byte[] key = setPrivateKeyJni(privateKey); - return new String(key, "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## setPrivateKey(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_SET_PRIVATE_KEY, e.getMessage()); - } - } - - private native byte[] setPrivateKeyJni(byte[] privateKey); - - public String generateKey() throws OlmException { - try { - byte[] key = generateKeyJni(); - return new String(key, "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## setRecipientKey(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_GENERATE_KEY, e.getMessage()); - } - } - - private native byte[] generateKeyJni(); - - public byte[] privateKey() throws OlmException { - try { - return privateKeyJni(); - } catch (Exception e) { - Log.e(LOG_TAG, "## privateKey(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_PRIVATE_KEY, e.getMessage()); - } - } - - private native byte[] privateKeyJni(); - - public String decrypt(OlmPkMessage aMessage) throws OlmException { - if (null == aMessage) { - return null; - } - - byte[] plaintextBuffer = decryptJni(aMessage); - try { - String plaintext = new String(plaintextBuffer, "UTF-8"); - return plaintext; - } catch (Exception e) { - Log.e(LOG_TAG, "## pkDecrypt(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_DECRYPTION_DECRYPT, e.getMessage()); - } finally { - Arrays.fill(plaintextBuffer, (byte) 0); - } - } - - private native byte[] decryptJni(OlmPkMessage aMessage); -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkEncryption.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkEncryption.java deleted file mode 100644 index 01666fd..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkEncryption.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2018 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.util.Log; - -import java.util.Arrays; - -public class OlmPkEncryption { - private static final String LOG_TAG = "OlmPkEncryption"; - - /** Session Id returned by JNI. - * This value uniquely identifies the native session instance. - **/ - private transient long mNativeId; - - public OlmPkEncryption() throws OlmException { - try { - mNativeId = createNewPkEncryptionJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_CREATION, e.getMessage()); - } - } - - private native long createNewPkEncryptionJni(); - - private native void releasePkEncryptionJni(); - - public void releaseEncryption() { - if (0 != mNativeId) { - releasePkEncryptionJni(); - } - mNativeId = 0; - } - - public boolean isReleased() { - return (0 == mNativeId); - } - - public void setRecipientKey(String aKey) throws OlmException { - if (null == aKey) { - return; - } - - try { - setRecipientKeyJni(aKey.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## setRecipientKey(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_SET_RECIPIENT_KEY, e.getMessage()); - } - } - - private native void setRecipientKeyJni(byte[] aKey); - - public OlmPkMessage encrypt(String aPlaintext) throws OlmException { - if (null == aPlaintext) { - return null; - } - - OlmPkMessage encryptedMsgRetValue = new OlmPkMessage(); - - byte[] plaintextBuffer = null; - try { - plaintextBuffer = aPlaintext.getBytes("UTF-8"); - byte[] ciphertextBuffer = encryptJni(plaintextBuffer, encryptedMsgRetValue); - - if (null != ciphertextBuffer) { - encryptedMsgRetValue.mCipherText = new String(ciphertextBuffer, "UTF-8"); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## pkEncrypt(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_ENCRYPTION_ENCRYPT, e.getMessage()); - } finally { - if (null != plaintextBuffer) { - Arrays.fill(plaintextBuffer, (byte) 0); - } - } - - return encryptedMsgRetValue; - } - - private native byte[] encryptJni(byte[] plaintext, OlmPkMessage aMessage); -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkMessage.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkMessage.java deleted file mode 100644 index f8a065a..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkMessage.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 2018 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -public class OlmPkMessage { - public String mCipherText; - public String mMac; - public String mEphemeralKey; -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkSigning.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkSigning.java deleted file mode 100644 index 89cb556..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmPkSigning.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.util.Log; - -import java.util.Arrays; - -public class OlmPkSigning { - private static final String LOG_TAG = "OlmPkSigning"; - - /** PK Signing Id returned by JNI. - * This value uniquely identifies the native PK signing instance. - **/ - private transient long mNativeId; - - public OlmPkSigning() throws OlmException { - try { - mNativeId = createNewPkSigningJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_PK_SIGNING_CREATION, e.getMessage()); - } - } - - private native long createNewPkSigningJni(); - - private native void releasePkSigningJni(); - - public void releaseSigning() { - if (0 != mNativeId) { - releasePkSigningJni(); - } - mNativeId = 0; - } - - public boolean isReleased() { - return (0 == mNativeId); - } - - public static native int seedLength(); - - public static byte[] generateSeed() throws OlmException { - try { - return generateSeedJni(); - } catch (Exception e) { - Log.e(LOG_TAG, "## generateSeed(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_SIGNING_GENERATE_SEED, e.getMessage()); - } - } - - public static native byte[] generateSeedJni(); - - public String initWithSeed(byte[] seed) throws OlmException { - try { - byte[] pubKey = setKeyFromSeedJni(seed); - return new String(pubKey, "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## initWithSeed(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_SIGNING_INIT_WITH_SEED, e.getMessage()); - } - } - - public native byte[] setKeyFromSeedJni(byte[] seed); - - public String sign(String aMessage) throws OlmException { - if (null == aMessage) { - return null; - } - - byte[] messageBuffer = null; - try { - messageBuffer = aMessage.getBytes("UTF-8"); - byte[] signature = pkSignJni(messageBuffer); - return new String(signature, "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## pkSign(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_PK_SIGNING_SIGN, e.getMessage()); - } finally { - if (null != messageBuffer) { - Arrays.fill(messageBuffer, (byte) 0); - } - } - } - - private native byte[] pkSignJni(byte[] message); -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmSAS.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmSAS.java deleted file mode 100644 index a0589e9..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmSAS.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.matrix.olm; - -import android.util.Log; - -import java.io.UnsupportedEncodingException; - -public class OlmSAS { - - private static final String LOG_TAG = OlmSAS.class.getName(); - /** - * Session Id returned by JNI. - * This value uniquely identifies the native SAS instance. - **/ - private transient long mNativeId; - - private String theirPublicKey = null; - - public OlmSAS() throws OlmException { - try { - mNativeId = createNewSASJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_CREATION, e.getMessage()); - } - } - - /** - * Gets the Public Key encoded in Base64 with no padding - * @return The public key - * @throws OlmException the failure reason - */ - public String getPublicKey() throws OlmException { - try { - byte[] buffer = getPubKeyJni(); - - if (null != buffer) { - return new String(buffer, "UTF-8"); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionIdentifier(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_ERROR, e.getMessage()); - } - - return null; - } - - /** - * Sets the public key of other user. - * - * @param otherPkey other user public key (base64 encoded with no padding) - * @throws OlmException the failure reason - */ - public void setTheirPublicKey(String otherPkey) throws OlmException { - try { - setTheirPubKey(otherPkey.getBytes("UTF-8")); - } catch (UnsupportedEncodingException e) { - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_ERROR, e.getMessage()); - } - this.theirPublicKey = otherPkey; - } - - - /** - * Generate bytes to use for the short authentication string. - * - * @param info info extra information to mix in when generating the bytes, as - * per the Matrix spec. - * @param byteNumber The size of the short code to generate - * @return The generated shortcode - * @throws OlmException the failure reason - */ - public byte[] generateShortCode(String info, int byteNumber) throws OlmException { - if (theirPublicKey == null || theirPublicKey.isEmpty()) { - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_MISSING_THEIR_PKEY, "call setTheirPublicKey first"); - } - try { - return generateShortCodeJni(info.getBytes("UTF-8"), byteNumber); - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionIdentifier(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_GENERATE_SHORT_CODE, e.getMessage()); - } - } - - - public String calculateMac(String message, String info) throws OlmException { - try { - byte[] bytes = calculateMacJni(message.getBytes("UTF-8"), info.getBytes("UTF-8")); - if (bytes != null) return new String(bytes, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_ERROR, e.getMessage()); - } - return null; - } - - public String calculateMacLongKdf(String message, String info) throws OlmException { - try { - byte[] bytes = calculateMacLongKdfJni(message.getBytes("UTF-8"), info.getBytes("UTF-8")); - if (bytes != null) return new String(bytes, "UTF-8"); - } catch (UnsupportedEncodingException e) { - throw new OlmException(OlmException.EXCEPTION_CODE_SAS_ERROR, e.getMessage()); - } - return null; - } - - /** - * Create an OLM session in native side.<br> - * Do not forget to call {@link #releaseSASJni()} when JAVA side is done. - * - * @return native account instance identifier or throw an exception. - */ - private native long createNewSASJni(); - - /** - * Destroy the corresponding OLM session native object.<br> - * This method must ALWAYS be called when this JAVA instance - * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #createNewSASJni()}. - */ - private native void releaseSASJni(); - - private native byte[] getPubKeyJni(); - - private native void setTheirPubKey(byte[] pubKey); - - private native byte[] generateShortCodeJni(byte[] info, int byteNumber); - - private native byte[] calculateMacJni(byte[] message, byte[] info); - - private native byte[] calculateMacLongKdfJni(byte[] message, byte[] info); - - /** - * Release native session and invalid its JAVA reference counter part.<br> - * Public API for {@link #releaseSASJni()}. - */ - public void releaseSas() { - if (0 != mNativeId) { - releaseSASJni(); - } - mNativeId = 0; - } -} diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java deleted file mode 100644 index 3c5ce49..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java +++ /dev/null @@ -1,452 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.text.TextUtils; -import android.util.Log; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.Serializable; - -import java.util.Arrays; - -/** - * Session class used to create Olm sessions in conjunction with {@link OlmAccount} class.<br> - * Olm session is used to encrypt data between devices, especially to create Olm group sessions (see {@link OlmOutboundGroupSession} and {@link OlmInboundGroupSession}).<br> - * To establish an Olm session with Bob, Alice calls {@link #initOutboundSession(OlmAccount, String, String)} with Bob's identity and onetime keys. Then Alice generates an encrypted PRE_KEY message ({@link #encryptMessage(String)}) - * used by Bob to open the Olm session in his side with {@link #initOutboundSession(OlmAccount, String, String)}. - * From this step on, messages can be exchanged by using {@link #encryptMessage(String)} and {@link #decryptMessage(OlmMessage)}. - * <br><br>Detailed implementation guide is available at <a href="http://matrix.org/docs/guides/e2e_implementation.html">Implementing End-to-End Encryption in Matrix clients</a>. - */ -public class OlmSession extends CommonSerializeUtils implements Serializable { - private static final long serialVersionUID = -8975488639186976419L; - private static final String LOG_TAG = "OlmSession"; - - /** Session Id returned by JNI. - * This value uniquely identifies the native session instance. - **/ - private transient long mNativeId; - - public OlmSession() throws OlmException { - try { - mNativeId = createNewSessionJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, e.getMessage()); - } - } - - /** - * Create an OLM session in native side.<br> - * Do not forget to call {@link #releaseSession()} when JAVA side is done. - * @return native account instance identifier or throw an exception. - */ - private native long createNewSessionJni(); - - /** - * Getter on the session ID. - * @return native session ID - */ - long getOlmSessionId(){ - return mNativeId; - } - - /** - * Destroy the corresponding OLM session native object.<br> - * This method must ALWAYS be called when this JAVA instance - * is destroyed (ie. garbage collected) to prevent memory leak in native side. - * See {@link #createNewSessionJni()}. - */ - private native void releaseSessionJni(); - - /** - * Release native session and invalid its JAVA reference counter part.<br> - * Public API for {@link #releaseSessionJni()}. - */ - public void releaseSession() { - if (0 != mNativeId) { - releaseSessionJni(); - } - mNativeId = 0; - } - - /** - * Return true the object resources have been released.<br> - * @return true the object resources have been released - */ - public boolean isReleased() { - return (0 == mNativeId); - } - - /** - * Creates a new out-bound session for sending messages to a recipient - * identified by an identity key and a one time key.<br> - * @param aAccount the account to associate with this session - * @param aTheirIdentityKey the identity key of the recipient - * @param aTheirOneTimeKey the one time key of the recipient - * @exception OlmException the failure reason - */ - public void initOutboundSession(OlmAccount aAccount, String aTheirIdentityKey, String aTheirOneTimeKey) throws OlmException { - if ((null == aAccount) || TextUtils.isEmpty(aTheirIdentityKey) || TextUtils.isEmpty(aTheirOneTimeKey)) { - Log.e(LOG_TAG, "## initOutboundSession(): invalid input parameters"); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION, "invalid input parameters"); - } else { - try { - initOutboundSessionJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aTheirOneTimeKey.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## initOutboundSession(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_OUTBOUND_SESSION, e.getMessage()); - } - } - } - - /** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message.<br> The recipient is defined as the entity - * with whom the session is established. - * An exception is thrown if the operation fails. - * @param aOlmAccountId account instance - * @param aTheirIdentityKey the identity key of the recipient - * @param aTheirOneTimeKey the one time key of the recipient - **/ - private native void initOutboundSessionJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aTheirOneTimeKey); - - /** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message ({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}).<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * @param aAccount the account to associate with this session - * @param aPreKeyMsg PRE KEY message - * @exception OlmException the failure reason - */ - public void initInboundSession(OlmAccount aAccount, String aPreKeyMsg) throws OlmException { - if ((null == aAccount) || TextUtils.isEmpty(aPreKeyMsg)){ - Log.e(LOG_TAG, "## initInboundSession(): invalid input parameters"); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION, "invalid input parameters"); - } else { - try { - initInboundSessionJni(aAccount.getOlmAccountId(), aPreKeyMsg.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## initInboundSession(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION, e.getMessage()); - } - } - } - - /** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message.<br> - * An exception is thrown if the operation fails. - * @param aOlmAccountId account instance - * @param aOneTimeKeyMsg PRE_KEY message - */ - private native void initInboundSessionJni(long aOlmAccountId, byte[] aOneTimeKeyMsg); - - /** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}) message based on the sender identity key.<br> - * Public API for {@link #initInboundSessionFromIdKeyJni(long, byte[], byte[])}. - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * This method must only be called the first time a pre-key message is received from an inbound session. - * @param aAccount the account to associate with this session - * @param aTheirIdentityKey the sender identity key - * @param aPreKeyMsg PRE KEY message - * @exception OlmException the failure reason - */ - public void initInboundSessionFrom(OlmAccount aAccount, String aTheirIdentityKey, String aPreKeyMsg) throws OlmException { - if ( (null==aAccount) || TextUtils.isEmpty(aPreKeyMsg)){ - Log.e(LOG_TAG, "## initInboundSessionFrom(): invalid input parameters"); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM, "invalid input parameters"); - } else { - try { - initInboundSessionFromIdKeyJni(aAccount.getOlmAccountId(), aTheirIdentityKey.getBytes("UTF-8"), aPreKeyMsg.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## initInboundSessionFrom(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_INIT_INBOUND_SESSION_FROM, e.getMessage()); - } - } - } - - /** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message based on the recipient identity key.<br> - * An exception is thrown if the operation fails. - * @param aOlmAccountId account instance - * @param aTheirIdentityKey the identity key of the recipient - * @param aOneTimeKeyMsg encrypted message - */ - private native void initInboundSessionFromIdKeyJni(long aOlmAccountId, byte[] aTheirIdentityKey, byte[] aOneTimeKeyMsg); - - /** - * Get the session identifier.<br> Will be the same for both ends of the - * conversation. The session identifier is returned as a String object. - * Session Id sample: "session_id":"M4fOVwD6AABrkTKl" - * Public API for {@link #getSessionIdentifierJni()}. - * @return the session ID - * @exception OlmException the failure reason - */ - public String sessionIdentifier() throws OlmException { - try { - byte[] buffer = getSessionIdentifierJni(); - - if (null != buffer) { - return new String(buffer, "UTF-8"); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## sessionIdentifier(): " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_SESSION_IDENTIFIER, e.getMessage()); - } - - return null; - } - - /** - * Get the session identifier for this session. - * An exception is thrown if the operation fails. - * @return the session identifier - */ - private native byte[] getSessionIdentifierJni(); - - /** - * Checks if the PRE_KEY({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}) message is for this in-bound session.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * Public API for {@link #matchesInboundSessionJni(byte[])}. - * @param aOneTimeKeyMsg PRE KEY message - * @return true if the one time key matches. - */ - public boolean matchesInboundSession(String aOneTimeKeyMsg) { - boolean retCode = false; - - try { - retCode = matchesInboundSessionJni(aOneTimeKeyMsg.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## matchesInboundSession(): failed " + e.getMessage()); - } - - return retCode; - } - - /** - * Checks if the PRE_KEY message is for this in-bound session.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * An exception is thrown if the operation fails. - * @param aOneTimeKeyMsg PRE KEY message - * @return true if the PRE_KEY message matches - */ - private native boolean matchesInboundSessionJni(byte[] aOneTimeKeyMsg); - - /** - * Checks if the PRE_KEY({@link OlmMessage#MESSAGE_TYPE_PRE_KEY}) message is for this in-bound session based on the sender identity key.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * Public API for {@link #matchesInboundSessionJni(byte[])}. - * @param aTheirIdentityKey the sender identity key - * @param aOneTimeKeyMsg PRE KEY message - * @return this if operation succeed, null otherwise - */ - public boolean matchesInboundSessionFrom(String aTheirIdentityKey, String aOneTimeKeyMsg) { - boolean retCode = false; - - try { - retCode = matchesInboundSessionFromIdKeyJni(aTheirIdentityKey.getBytes("UTF-8"), aOneTimeKeyMsg.getBytes("UTF-8")); - } catch (Exception e) { - Log.e(LOG_TAG, "## matchesInboundSessionFrom(): failed " + e.getMessage()); - } - - return retCode; - } - - /** - * Checks if the PRE_KEY message is for this in-bound session based on the sender identity key.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * An exception is thrown if the operation fails. - * @param aTheirIdentityKey the identity key of the sender - * @param aOneTimeKeyMsg PRE KEY message - * @return true if the PRE_KEY message matches. - */ - private native boolean matchesInboundSessionFromIdKeyJni(byte[] aTheirIdentityKey, byte[] aOneTimeKeyMsg); - - /** - * Encrypt a message using the session.<br> - * The encrypted message is returned in a OlmMessage object. - * Public API for {@link #encryptMessageJni(byte[], OlmMessage)}. - * @param aClearMsg message to encrypted - * @return the encrypted message - * @exception OlmException the failure reason - */ - public OlmMessage encryptMessage(String aClearMsg) throws OlmException { - if (null == aClearMsg) { - return null; - } - - OlmMessage encryptedMsgRetValue = new OlmMessage(); - - try { - byte[] clearMsgBuffer = aClearMsg.getBytes("UTF-8"); - byte[] encryptedMessageBuffer = encryptMessageJni(clearMsgBuffer, encryptedMsgRetValue); - Arrays.fill(clearMsgBuffer, (byte) 0); - - if (null != encryptedMessageBuffer) { - encryptedMsgRetValue.mCipherText = new String(encryptedMessageBuffer, "UTF-8"); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## encryptMessage(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_ENCRYPT_MESSAGE, e.getMessage()); - } - - return encryptedMsgRetValue; - } - - /** - * Encrypt a message using the session.<br> - * An exception is thrown if the operation fails. - * @param aClearMsg clear text message - * @param aEncryptedMsg ciphered message - * @return the encrypted message - */ - private native byte[] encryptMessageJni(byte[] aClearMsg, OlmMessage aEncryptedMsg); - - /** - * Decrypt a message using the session.<br> - * The encrypted message is given as a OlmMessage object. - * @param aEncryptedMsg message to decrypt - * @return the decrypted message - * @exception OlmException the failure reason - */ - public String decryptMessage(OlmMessage aEncryptedMsg) throws OlmException { - if (null == aEncryptedMsg) { - return null; - } - - try { - byte[] plaintextBuffer = decryptMessageJni(aEncryptedMsg); - String plaintext = new String(plaintextBuffer, "UTF-8"); - Arrays.fill(plaintextBuffer, (byte) 0); - return plaintext; - } catch (Exception e) { - Log.e(LOG_TAG, "## decryptMessage(): failed " + e.getMessage()); - throw new OlmException(OlmException.EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE, e.getMessage()); - } - } - /** - * Decrypt a message using the session.<br> - * An exception is thrown if the operation fails. - * @param aEncryptedMsg message to decrypt - * @return the decrypted message - */ - private native byte[] decryptMessageJni(OlmMessage aEncryptedMsg); - - //============================================================================================================== - // Serialization management - //============================================================================================================== - - /** - * Kick off the serialization mechanism. - * @param aOutStream output stream for serializing - * @throws IOException exception - */ - private void writeObject(ObjectOutputStream aOutStream) throws IOException { - serialize(aOutStream); - } - - /** - * Kick off the deserialization mechanism. - * @param aInStream input stream - * @throws IOException exception - * @throws ClassNotFoundException exception - */ - private void readObject(ObjectInputStream aInStream) throws Exception { - deserialize(aInStream); - } - - /** - * Return a session as a bytes buffer.<br> - * The account is serialized and encrypted with aKey. - * In case of failure, an error human readable - * description is provide in aErrorMsg. - * @param aKey encryption key - * @param aErrorMsg error message description - * @return session as a bytes buffer - */ - @Override - protected byte[] serialize(byte[] aKey, StringBuffer aErrorMsg) { - byte[] pickleRetValue = null; - - // sanity check - if(null == aErrorMsg) { - Log.e(LOG_TAG,"## serializeDataWithKey(): invalid parameter - aErrorMsg=null"); - } else if (null == aKey) { - aErrorMsg.append("Invalid input parameters in serializeDataWithKey()"); - } else { - aErrorMsg.setLength(0); - try { - pickleRetValue = serializeJni(aKey); - } catch (Exception e) { - Log.e(LOG_TAG,"## serializeDataWithKey(): failed " + e.getMessage()); - aErrorMsg.append(e.getMessage()); - } - } - - return pickleRetValue; - } - - /** - * Serialize and encrypt session instance.<br> - * An exception is thrown if the operation fails. - * @param aKeyBuffer key used to encrypt the serialized account data - * @return the serialised account as bytes buffer. - **/ - private native byte[] serializeJni(byte[] aKeyBuffer); - - /** - * Loads an account from a pickled base64 string.<br> - * See {@link #serialize(byte[], StringBuffer)} - * @param aSerializedData pickled account in a base64 string format - * @param aKey key used to encrypted - */ - @Override - protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception { - String errorMsg = null; - - try { - if ((null == aSerializedData) || (null == aKey)) { - Log.e(LOG_TAG, "## deserialize(): invalid input parameters"); - errorMsg = "invalid input parameters"; - } else { - mNativeId = deserializeJni(aSerializedData, aKey); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage()); - errorMsg = e.getMessage(); - } - - if (!TextUtils.isEmpty(errorMsg)) { - releaseSession(); - throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg); - } - } - /** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ - private native long deserializeJni(byte[] aSerializedData, byte[] aKey); -} - diff --git a/android/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java b/android/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java deleted file mode 100644 index 250cfb1..0000000 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmUtility.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.matrix.olm; - -import android.text.TextUtils; -import android.util.Log; - -import org.json.JSONObject; - -import java.security.SecureRandom; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -/** - * Olm SDK helper class. - */ -public class OlmUtility { - private static final String LOG_TAG = "OlmUtility"; - - public static final int RANDOM_KEY_SIZE = 32; - - /** Instance Id returned by JNI. - * This value uniquely identifies this utility instance. - **/ - private long mNativeId; - - public OlmUtility() throws OlmException { - initUtility(); - } - - /** - * Create a native utility instance. - * To be called before any other API call. - * @exception OlmException the exception - */ - private void initUtility() throws OlmException { - try { - mNativeId = createUtilityJni(); - } catch (Exception e) { - throw new OlmException(OlmException.EXCEPTION_CODE_UTILITY_CREATION, e.getMessage()); - } - } - - private native long createUtilityJni(); - - /** - * Release native instance.<br> - * Public API for {@link #releaseUtilityJni()}. - */ - public void releaseUtility() { - if (0 != mNativeId) { - releaseUtilityJni(); - } - mNativeId = 0; - } - private native void releaseUtilityJni(); - - /** - * Verify an ed25519 signature.<br> - * An exception is thrown if the operation fails. - * @param aSignature the base64-encoded message signature to be checked. - * @param aFingerprintKey the ed25519 key (fingerprint key) - * @param aMessage the signed message - * @exception OlmException the failure reason - */ - public void verifyEd25519Signature(String aSignature, String aFingerprintKey, String aMessage) throws OlmException { - String errorMessage; - byte[] messageBuffer = null; - - try { - if (TextUtils.isEmpty(aSignature) || TextUtils.isEmpty(aFingerprintKey) || TextUtils.isEmpty(aMessage)) { - Log.e(LOG_TAG, "## verifyEd25519Signature(): invalid input parameters"); - errorMessage = "JAVA sanity check failure - invalid input parameters"; - } else { - messageBuffer = aMessage.getBytes("UTF-8"); - errorMessage = verifyEd25519SignatureJni(aSignature.getBytes("UTF-8"), aFingerprintKey.getBytes("UTF-8"), messageBuffer); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## verifyEd25519Signature(): failed " + e.getMessage()); - errorMessage = e.getMessage(); - } finally { - if (messageBuffer != null) { - Arrays.fill(messageBuffer, (byte) 0); - } - } - - if (!TextUtils.isEmpty(errorMessage)) { - throw new OlmException(OlmException.EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE, errorMessage); - } - } - - /** - * Verify an ed25519 signature. - * Return a human readable error message in case of verification failure. - * @param aSignature the base64-encoded message signature to be checked. - * @param aFingerprintKey the ed25519 key - * @param aMessage the signed message - * @return null if validation succeed, the error message string if operation failed - */ - private native String verifyEd25519SignatureJni(byte[] aSignature, byte[] aFingerprintKey, byte[] aMessage); - - /** - * Compute the hash(SHA-256) value of the string given in parameter(aMessageToHash).<br> - * The hash value is the returned by the method. - * @param aMessageToHash message to be hashed - * @return hash value if operation succeed, null otherwise - */ - public String sha256(String aMessageToHash) { - String hashRetValue = null; - - if (null != aMessageToHash) { - byte[] messageBuffer = null; - try { - messageBuffer = aMessageToHash.getBytes("UTF-8"); - hashRetValue = new String(sha256Jni(messageBuffer), "UTF-8"); - } catch (Exception e) { - Log.e(LOG_TAG, "## sha256(): failed " + e.getMessage()); - } finally { - if (null != messageBuffer) { - Arrays.fill(messageBuffer, (byte) 0); - } - } - } - - return hashRetValue; - } - - /** - * Compute the digest (SHA 256) for the message passed in parameter.<br> - * The digest value is the function return value. - * An exception is thrown if the operation fails. - * @param aMessage the message - * @return digest of the message. - **/ - private native byte[] sha256Jni(byte[] aMessage); - - /** - * Helper method to compute a string based on random integers. - * @return bytes buffer containing randoms integer values - */ - public static byte[] getRandomKey() { - SecureRandom secureRandom = new SecureRandom(); - byte[] buffer = new byte[RANDOM_KEY_SIZE]; - secureRandom.nextBytes(buffer); - - // the key is saved as string - // so avoid the UTF8 marker bytes - for(int i = 0; i < RANDOM_KEY_SIZE; i++) { - buffer[i] = (byte)(buffer[i] & 0x7F); - } - return buffer; - } - - /** - * Return true the object resources have been released.<br> - * @return true the object resources have been released - */ - public boolean isReleased() { - return (0 == mNativeId); - } - - /** - * Build a string-string dictionary from a jsonObject.<br> - * @param jsonObject the object to parse - * @return the map - */ - public static Map<String, String> toStringMap(JSONObject jsonObject) { - if (null != jsonObject) { - HashMap<String, String> map = new HashMap<>(); - Iterator<String> keysItr = jsonObject.keys(); - while(keysItr.hasNext()) { - String key = keysItr.next(); - try { - Object value = jsonObject.get(key); - - if (value instanceof String) { - map.put(key, (String) value); - } else { - Log.e(LOG_TAG, "## toStringMap(): unexpected type " + value.getClass()); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## toStringMap(): failed " + e.getMessage()); - } - } - - return map; - } - - return null; - } - - /** - * Build a string-string dictionary of string dictionary from a jsonObject.<br> - * @param jsonObject the object to parse - * @return the map - */ - public static Map<String, Map<String, String>> toStringMapMap(JSONObject jsonObject) { - if (null != jsonObject) { - HashMap<String, Map<String, String>> map = new HashMap<>(); - - Iterator<String> keysItr = jsonObject.keys(); - while(keysItr.hasNext()) { - String key = keysItr.next(); - try { - Object value = jsonObject.get(key); - - if (value instanceof JSONObject) { - map.put(key, toStringMap((JSONObject) value)); - } else { - Log.e(LOG_TAG, "## toStringMapMap(): unexpected type " + value.getClass()); - } - } catch (Exception e) { - Log.e(LOG_TAG, "## toStringMapMap(): failed " + e.getMessage()); - } - } - - return map; - } - - return null; - } -} - diff --git a/android/olm-sdk/src/main/jni/Android.mk b/android/olm-sdk/src/main/jni/Android.mk deleted file mode 100644 index 101346b..0000000 --- a/android/olm-sdk/src/main/jni/Android.mk +++ /dev/null @@ -1,67 +0,0 @@ -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - - -LOCAL_MODULE := olm - -SRC_ROOT_DIR := ../../../../.. - -include $(LOCAL_PATH)/$(SRC_ROOT_DIR)/common.mk -OLM_VERSION := $(MAJOR).$(MINOR).$(PATCH) - -$(info LOCAL_PATH=$(LOCAL_PATH)) -$(info SRC_ROOT_DIR=$(SRC_ROOT_DIR)) -$(info OLM_VERSION=$(OLM_VERSION)) - -LOCAL_CPPFLAGS+= -std=c++11 -Wall -LOCAL_CONLYFLAGS+= -std=c99 -LOCAL_CFLAGS+= -DOLMLIB_VERSION_MAJOR=$(MAJOR) \ --DOLMLIB_VERSION_MINOR=$(MINOR) \ --DOLMLIB_VERSION_PATCH=$(PATCH) - -#LOCAL_CFLAGS+= -DNDK_DEBUG - -LOCAL_CFLAGS+=-fstack-protector-all -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Wall -LOCAL_LDFLAGS=-z relro -z now - -LOCAL_C_INCLUDES+= $(LOCAL_PATH)/$(SRC_ROOT_DIR)/include/ \ -$(LOCAL_PATH)/$(SRC_ROOT_DIR)/lib - -$(info LOCAL_C_INCLUDES=$(LOCAL_C_INCLUDES)) - -LOCAL_SRC_FILES := $(SRC_ROOT_DIR)/src/account.cpp \ -$(SRC_ROOT_DIR)/src/base64.cpp \ -$(SRC_ROOT_DIR)/src/cipher.cpp \ -$(SRC_ROOT_DIR)/src/crypto.cpp \ -$(SRC_ROOT_DIR)/src/memory.cpp \ -$(SRC_ROOT_DIR)/src/message.cpp \ -$(SRC_ROOT_DIR)/src/olm.cpp \ -$(SRC_ROOT_DIR)/src/pickle.cpp \ -$(SRC_ROOT_DIR)/src/ratchet.cpp \ -$(SRC_ROOT_DIR)/src/session.cpp \ -$(SRC_ROOT_DIR)/src/utility.cpp \ -$(SRC_ROOT_DIR)/src/pk.cpp \ -$(SRC_ROOT_DIR)/src/sas.c \ -$(SRC_ROOT_DIR)/src/ed25519.c \ -$(SRC_ROOT_DIR)/src/error.c \ -$(SRC_ROOT_DIR)/src/inbound_group_session.c \ -$(SRC_ROOT_DIR)/src/megolm.c \ -$(SRC_ROOT_DIR)/src/outbound_group_session.c \ -$(SRC_ROOT_DIR)/src/pickle_encoding.c \ -$(SRC_ROOT_DIR)/lib/crypto-algorithms/sha256.c \ -$(SRC_ROOT_DIR)/lib/crypto-algorithms/aes.c \ -$(SRC_ROOT_DIR)/lib/curve25519-donna/curve25519-donna.c \ -olm_account.cpp \ -olm_session.cpp \ -olm_jni_helper.cpp \ -olm_inbound_group_session.cpp \ -olm_outbound_group_session.cpp \ -olm_utility.cpp \ -olm_manager.cpp \ -olm_pk.cpp \ -olm_sas.cpp - -LOCAL_LDLIBS := -llog - -include $(BUILD_SHARED_LIBRARY) - diff --git a/android/olm-sdk/src/main/jni/Application.mk b/android/olm-sdk/src/main/jni/Application.mk deleted file mode 100644 index e052911..0000000 --- a/android/olm-sdk/src/main/jni/Application.mk +++ /dev/null @@ -1,3 +0,0 @@ -APP_PLATFORM := android-16 -APP_ABI := arm64-v8a armeabi-v7a x86_64 x86 -APP_STL := c++_static
\ No newline at end of file diff --git a/android/olm-sdk/src/main/jni/olm_account.cpp b/android/olm-sdk/src/main/jni/olm_account.cpp deleted file mode 100644 index 00b1460..0000000 --- a/android/olm-sdk/src/main/jni/olm_account.cpp +++ /dev/null @@ -1,695 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_account.h" - -using namespace AndroidOlmSdk; - -/** - * Init memory allocation for account creation. - * @return valid memory allocation, NULL otherwise - **/ -OlmAccount* initializeAccountMemory() -{ - size_t accountSize = olm_account_size(); - OlmAccount* accountPtr = (OlmAccount*)malloc(accountSize); - - if (accountPtr) - { - // init account object - accountPtr = olm_account(accountPtr); - LOGD("## initializeAccountMemory(): success - OLM account size=%lu",static_cast<long unsigned int>(accountSize)); - } - else - { - LOGE("## initializeAccountMemory(): failure - OOM"); - } - - return accountPtr; -} - -/** - * Create a new account and return it to JAVA side.<br> - * Since a C prt is returned as a jlong, special care will be taken - * to make the cast (OlmAccount* => jlong) platform independent. - * @return the initialized OlmAccount* instance or throw an exception if fails - **/ -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmAccount *accountPtr = initializeAccountMemory(); - - // init account memory allocation - if (!accountPtr) - { - LOGE("## initNewAccount(): failure - init account OOM"); - errorMessage = "init account OOM"; - } - else - { - // get random buffer size - size_t randomSize = olm_create_account_random_length(accountPtr); - - LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize)); - - uint8_t *randomBuffPtr = NULL; - size_t accountRetCode; - - // allocate random buffer - if ((0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) - { - LOGE("## initNewAccount(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - // create account - accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize); - - if (accountRetCode == olm_error()) - { - LOGE("## initNewAccount(): failure - account creation failed Msg=%s", olm_account_last_error(accountPtr)); - errorMessage = olm_account_last_error(accountPtr); - } - - LOGD("## initNewAccount(): success - OLM account created"); - LOGD("## initNewAccount(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomSize); - free(randomBuffPtr); - } - } - - if (errorMessage) - { - // release the allocated data - if (accountPtr) - { - olm_clear_account(accountPtr); - free(accountPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)accountPtr; -} -/** - * Release the account allocation made by initializeAccountMemory().<br> - * This method MUST be called when java counter part account instance is done. - */ -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releaseAccountJni(): IN"); - - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - - if (!accountPtr) - { - LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL"); - } - else - { - LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr); - olm_clear_account(accountPtr); - - LOGD(" ## releaseAccountJni(): IN"); - // even if free(NULL) does not crash, logs are performed for debug purpose - free(accountPtr); - LOGD(" ## releaseAccountJni(): OUT"); - } -} - -// ********************************************************************* -// ************************* IDENTITY KEYS API ************************* -// ********************************************************************* - -/** - * Get identity keys: Ed25519 fingerprint key and Curve25519 identity key.<br> - * The keys are returned in the byte array. - * @return the identity keys or throw an exception if it fails - **/ -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - jbyteArray byteArrayRetValue = NULL; - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - - if (!accountPtr) - { - LOGE("## identityKeys(): failure - invalid Account ptr=NULL"); - errorMessage = "invalid Account ptr"; - } - else - { - LOGD("## identityKeys(): accountPtr =%p", accountPtr); - - // identity keys allocation - size_t identityKeysLength = olm_account_identity_keys_length(accountPtr); - uint8_t *identityKeysBytesPtr = (uint8_t*)malloc(identityKeysLength); - - if (!identityKeysBytesPtr) - { - LOGE("## identityKeys(): failure - identity keys array OOM"); - errorMessage = "identity keys array OOM"; - } - else - { - // retrieve key pairs in identityKeysBytesPtr - size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength); - - if (keysResult == olm_error()) - { - errorMessage = (const char *)olm_account_last_error(accountPtr); - LOGE("## identityKeys(): failure - error getting identity keys Msg=%s", errorMessage); - } - else - { - // allocate the byte array to be returned to java - byteArrayRetValue = env->NewByteArray(identityKeysLength); - - if (!byteArrayRetValue) - { - LOGE("## identityKeys(): failure - return byte array OOM"); - errorMessage = "byte array OOM"; - } - else - { - env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, identityKeysLength, (const jbyte*)identityKeysBytesPtr); - LOGD("## identityKeys(): success - result=%lu", static_cast<long unsigned int>(keysResult)); - } - } - - free(identityKeysBytesPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return byteArrayRetValue; -} - -// ********************************************************************* -// ************************* ONE TIME KEYS API ************************* -// ********************************************************************* - -/** - * Get the public parts of the unpublished "one time keys" for the account.<br> - * The returned data is a JSON-formatted object with the single property - * <tt>curve25519</tt>, which is itself an object mapping key id to - * base64-encoded Curve25519 key.<br> - * @return byte array containing the one time keys or throw an exception if it fails - */ -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz) -{ - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - size_t maxKeys = -1; - - if (!accountPtr) - { - LOGE("## maxOneTimeKey(): failure - invalid Account ptr=NULL"); - } - else - { - maxKeys = olm_account_max_number_of_one_time_keys(accountPtr); - } - - LOGD("## maxOneTimeKey(): Max keys=%lu", static_cast<long unsigned int>(maxKeys)); - - return (jlong)maxKeys; -} - -/** - * Generate "one time keys". - * An exception is thrown if the operation fails. - * @param aNumberOfKeys number of keys to generate - **/ -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys) -{ - const char* errorMessage = NULL; - OlmAccount *accountPtr = getAccountInstanceId(env, thiz); - - if (!accountPtr) - { - LOGE("## generateOneTimeKeysJni(): failure - invalid Account ptr"); - errorMessage = "invalid Account ptr"; - } - else - { - // keys memory allocation - size_t randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys); - LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); - - uint8_t *randomBufferPtr = NULL; - - if ((0 != randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength)) - { - LOGE("## generateOneTimeKeysJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - LOGD("## generateOneTimeKeysJni(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys); - - // retrieve key pairs in keysBytesPtr - size_t result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength); - - if (result == olm_error()) - { - errorMessage = olm_account_last_error(accountPtr); - LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s", errorMessage); - } - else - { - LOGD("## generateOneTimeKeysJni(): success - result=%lu", static_cast<long unsigned int>(result)); - } - } - - - if (randomBufferPtr) - { - memset(randomBufferPtr, 0, randomLength); - free(randomBufferPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -/** - * Get "one time keys".<br> - * Return the public parts of the unpublished "one time keys" for the account - * @return a valid byte array if operation succeed, null otherwise - **/ -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - jbyteArray byteArrayRetValue = NULL; - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - - LOGD("## oneTimeKeysJni(): IN"); - - if (!accountPtr) - { - LOGE("## oneTimeKeysJni(): failure - invalid Account ptr"); - errorMessage = "invalid Account ptr"; - } - else - { - // keys memory allocation - size_t keysLength = olm_account_one_time_keys_length(accountPtr); - uint8_t *keysBytesPtr = (uint8_t *)malloc(keysLength*sizeof(uint8_t)); - - if (!keysBytesPtr) - { - LOGE("## oneTimeKeysJni(): failure - one time keys array OOM"); - errorMessage = "one time keys array OOM"; - } - else - { - // retrieve key pairs in keysBytesPtr - size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength); - - if (keysResult == olm_error()) { - LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr)); - errorMessage = (const char *)olm_account_last_error(accountPtr); - } - else - { - // allocate the byte array to be returned to java - byteArrayRetValue = env->NewByteArray(keysLength); - - if (!byteArrayRetValue) - { - LOGE("## oneTimeKeysJni(): failure - return byte array OOM"); - errorMessage = "return byte array OOM"; - } - else - { - env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, keysLength, (const jbyte*)keysBytesPtr); - LOGD("## oneTimeKeysJni(): success"); - } - } - - free(keysBytesPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return byteArrayRetValue; -} - -/** - * Remove the "one time keys" that the session used from the account. - * An exception is thrown if the operation fails. - * @param aNativeOlmSessionId session instance - **/ -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId) -{ - const char* errorMessage = NULL; - OlmAccount* accountPtr = NULL; - OlmSession* sessionPtr = (OlmSession*)aNativeOlmSessionId; - - if (!sessionPtr) - { - LOGE("## removeOneTimeKeysJni(): failure - invalid session ptr"); - errorMessage = "invalid session ptr"; - } - else if (!(accountPtr = getAccountInstanceId(env, thiz))) - { - LOGE("## removeOneTimeKeysJni(): failure - invalid account ptr"); - errorMessage = "invalid account ptr"; - } - else - { - size_t result = olm_remove_one_time_keys(accountPtr, sessionPtr); - - if (result == olm_error()) - { // the account doesn't have any matching "one time keys".. - LOGW("## removeOneTimeKeysJni(): failure - removing one time keys Msg=%s", olm_account_last_error(accountPtr)); - errorMessage = (const char *)olm_account_last_error(accountPtr); - } - else - { - LOGD("## removeOneTimeKeysJni(): success"); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -/** - * Mark the current set of "one time keys" as being published. - * An exception is thrown if the operation fails. - **/ -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmAccount* accountPtr = getAccountInstanceId(env, thiz); - - if (!accountPtr) - { - LOGE("## markOneTimeKeysAsPublishedJni(): failure - invalid account ptr"); - errorMessage = "invalid account ptr"; - } - else - { - size_t result = olm_account_mark_keys_as_published(accountPtr); - - if (result == olm_error()) - { - LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr)); - errorMessage = (const char *)olm_account_last_error(accountPtr); - } - else - { - LOGD("## markOneTimeKeysAsPublishedJni(): success - retCode=%lu",static_cast<long unsigned int>(result)); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -/** - * Sign a message with the ed25519 key (fingerprint) for this account.<br> - * The signed message is returned by the function. - * @param aMessage message to sign - * @return the signed message, null otherwise - **/ -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) -{ - const char* errorMessage = NULL; - OlmAccount* accountPtr = NULL; - jbyteArray signedMsgRetValueBuffer = NULL; - - if (!aMessage) - { - LOGE("## signMessageJni(): failure - invalid aMessage param"); - errorMessage = "invalid aMessage param"; - } - else if (!(accountPtr = getAccountInstanceId(env, thiz))) - { - LOGE("## signMessageJni(): failure - invalid account ptr"); - errorMessage = "invalid account ptr"; - } - else - { - int messageLength = env->GetArrayLength(aMessage); - jbyte* messageToSign = env->GetByteArrayElements(aMessage, NULL); - - // signature memory allocation - size_t signatureLength = olm_account_signature_length(accountPtr); - void* signedMsgPtr = malloc(signatureLength * sizeof(uint8_t)); - - if (!signedMsgPtr) - { - LOGE("## signMessageJni(): failure - signature allocation OOM"); - errorMessage = "signature allocation OOM"; - } - else - { - // sign message - size_t resultSign = olm_account_sign(accountPtr, - (void*)messageToSign, - (size_t)messageLength, - signedMsgPtr, - signatureLength); - - if (resultSign == olm_error()) - { - LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr)); - errorMessage = (const char *)olm_account_last_error(accountPtr); - } - else - { - LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength)); - - signedMsgRetValueBuffer = env->NewByteArray(signatureLength); - env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr); - } - - free(signedMsgPtr); - } - - // release messageToSign - if (messageToSign) - { - env->ReleaseByteArrayElements(aMessage, messageToSign, JNI_ABORT); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return signedMsgRetValueBuffer; -} - -/** - * Serialize and encrypt account instance.<br> - * @param aKeyBuffer key used to encrypt the serialized account data - * @return the serialised account as bytes buffer. - **/ -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - jbyteArray pickledDataRetValue = 0; - jbyte* keyPtr = NULL; - jboolean keyIsCopied = JNI_FALSE; - OlmAccount* accountPtr = NULL; - - LOGD("## serializeJni(): IN"); - - if (!aKeyBuffer) - { - LOGE(" ## serializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(accountPtr = getAccountInstanceId(env, thiz))) - { - LOGE(" ## serializeJni(): failure - invalid account ptr"); - errorMessage = "invalid account ptr"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyIsCopied))) - { - LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = olm_pickle_account_length(accountPtr); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - - void* pickledPtr = malloc(pickledLength * sizeof(uint8_t)); - - if (!pickledPtr) - { - LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); - errorMessage = "pickledPtr buffer OOM"; - } - else - { - size_t result = olm_pickle_account(accountPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_account_last_error(accountPtr); - LOGE(" ## serializeJni(): failure - olm_pickle_account() Msg=%s", errorMessage); - } - else - { - LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(pickledLength), static_cast<char*>(pickledPtr)); - pickledDataRetValue = env->NewByteArray(pickledLength); - env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr); - } - - free(pickledPtr); - } - } - - // free alloc - if (keyPtr) - { - if (keyIsCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return pickledDataRetValue; -} - -/** - * Allocate a new account and initialise it with the serialisation data.<br> - * @param aSerializedDataBuffer the account serialisation buffer - * @param aKeyBuffer the key used to encrypt the serialized account data - * @return the deserialised account - **/ -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - - OlmAccount* accountPtr = NULL; - - jbyte* keyPtr = NULL; - jboolean keyIsCopied = JNI_FALSE; - jbyte* pickledPtr = NULL; - - LOGD("## deserializeJni(): IN"); - - if (!aKeyBuffer) - { - LOGE(" ## deserializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!aSerializedDataBuffer) - { - LOGE(" ## deserializeJni(): failure - invalid serialized data"); - errorMessage = "invalid serialized data"; - } - else if (!(accountPtr = initializeAccountMemory())) - { - LOGE(" ## deserializeJni(): failure - account failure OOM"); - errorMessage = "account failure OOM"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyIsCopied))) - { - LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0))) - { - LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM"); - errorMessage = "pickledPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int> (pickledLength), (char const *)pickledPtr); - - size_t result = olm_unpickle_account(accountPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_account_last_error(accountPtr); - LOGE(" ## deserializeJni(): failure - olm_unpickle_account() Msg=%s", errorMessage); - } - else - { - LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result)); - } - } - - // free alloc - if (keyPtr) - { - if (keyIsCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (pickledPtr) - { - env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT); - } - - if (errorMessage) - { - if (accountPtr) - { - olm_clear_account(accountPtr); - free(accountPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)accountPtr; -} diff --git a/android/olm-sdk/src/main/jni/olm_account.h b/android/olm-sdk/src/main/jni/olm_account.h deleted file mode 100644 index eda434d..0000000 --- a/android/olm-sdk/src/main/jni/olm_account.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017 OpenMarket Ltd - * Copyright 2017 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMACCOUNT_H -#define _OLMACCOUNT_H - -#include "olm_jni.h" -#include "olm/olm.h" - -#define OLM_ACCOUNT_FUNC_DEF(func_name) FUNC_DEF(OlmAccount,func_name) -#define OLM_MANAGER_FUNC_DEF(func_name) FUNC_DEF(OlmManager,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -// account creation/destruction -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz); - -// identity keys -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz); - -// one time keys -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys); -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId); -JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz); - -// signing -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage); - -// serialization -JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer); -JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp b/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp deleted file mode 100644 index ae9ecf1..0000000 --- a/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +++ /dev/null @@ -1,654 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_inbound_group_session.h" - -using namespace AndroidOlmSdk; - -/** - * Release the session allocation made by initializeInboundGroupSessionMemory().<br> - * This method MUST be called when java counter part account instance is done. - */ -JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) -{ - OlmInboundGroupSession* sessionPtr = getInboundGroupSessionInstanceId(env,thiz); - - LOGD("## releaseSessionJni(): InBound group session IN"); - - if (!sessionPtr) - { - LOGE("## releaseSessionJni(): failure - invalid inbound group session instance"); - } - else - { - LOGD(" ## releaseSessionJni(): sessionPtr=%p", sessionPtr); -#ifdef ENABLE_JNI_LOG - size_t retCode = olm_clear_inbound_group_session(sessionPtr); - LOGD(" ## releaseSessionJni(): clear_inbound_group_session=%lu",static_cast<long unsigned int>(retCode)); -#else - olm_clear_inbound_group_session(sessionPtr); -#endif - - LOGD(" ## releaseSessionJni(): free IN"); - free(sessionPtr); - LOGD(" ## releaseSessionJni(): free OUT"); - } -} - -/** - * Initialize a new inbound group session and return it to JAVA side.<br> - * Since a C prt is returned as a jlong, special care will be taken - * to make the cast (OlmInboundGroupSession* => jlong) platform independent. - * @param aSessionKeyBuffer session key from an outbound session - * @param isImported true when the session key has been retrieved from a backup - * @return the initialized OlmInboundGroupSession* instance or throw an exception it fails. - **/ -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer, jboolean isImported) -{ - const char* errorMessage = NULL; - OlmInboundGroupSession* sessionPtr = NULL; - jbyte* sessionKeyPtr = NULL; - jboolean sessionWasCopied = JNI_FALSE; - size_t sessionSize = olm_inbound_group_session_size(); - - LOGD("## createNewSessionJni(): inbound group session IN"); - - if (!sessionSize) - { - LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0"); - errorMessage = "inbound group session size = 0"; - } - else if (!(sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize))) - { - LOGE(" ## createNewSessionJni(): failure - inbound group session OOM"); - errorMessage = "inbound group session OOM"; - } - else if (!aSessionKeyBuffer) - { - LOGE(" ## createNewSessionJni(): failure - invalid aSessionKey"); - errorMessage = "invalid aSessionKey"; - } - else if (!(sessionKeyPtr = env->GetByteArrayElements(aSessionKeyBuffer, &sessionWasCopied))) - { - LOGE(" ## createNewSessionJni(): failure - session key JNI allocation OOM"); - errorMessage = "Session key JNI allocation OOM"; - } - else - { - sessionPtr = olm_inbound_group_session(sessionPtr); - - size_t sessionKeyLength = (size_t)env->GetArrayLength(aSessionKeyBuffer); - LOGD(" ## createNewSessionJni(): sessionKeyLength=%lu", static_cast<long unsigned int>(sessionKeyLength)); - - size_t sessionResult; - - if (JNI_FALSE == isImported) - { - LOGD(" ## createNewSessionJni(): init"); - sessionResult = olm_init_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength); - } - else - { - LOGD(" ## createNewSessionJni(): import"); - sessionResult = olm_import_inbound_group_session(sessionPtr, (const uint8_t*)sessionKeyPtr, sessionKeyLength); - } - - if (sessionResult == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## createNewSessionJni(): failure - init inbound session creation Msg=%s", errorMessage); - } - else - { - LOGD(" ## createNewSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); - } - } - - if (sessionKeyPtr) - { - if (sessionWasCopied) { - memset(sessionKeyPtr, 0, (size_t)env->GetArrayLength(aSessionKeyBuffer)); - } - env->ReleaseByteArrayElements(aSessionKeyBuffer, sessionKeyPtr, JNI_ABORT); - } - - if (errorMessage) - { - // release the allocated session - if (sessionPtr) - { - olm_clear_inbound_group_session(sessionPtr); - free(sessionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sessionPtr; -} - -/** - * Get a base64-encoded identifier for this inbound group session. - * An exception is thrown if the operation fails. - * @return the base64-encoded identifier - */ -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - jbyteArray returnValue = 0; - - LOGD("## sessionIdentifierJni(): inbound group session IN"); - - if (!sessionPtr) - { - LOGE(" ## sessionIdentifierJni(): failure - invalid inbound group session instance"); - errorMessage = "invalid inbound group session instance"; - } - else - { - // get the size to alloc - size_t lengthSessionId = olm_inbound_group_session_id_length(sessionPtr); - LOGD(" ## sessionIdentifierJni(): inbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); - - uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)); - - if (!sessionIdPtr) - { - LOGE(" ## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"); - errorMessage = "inbound group session identifier allocation OOM"; - } - else - { - size_t result = olm_inbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - - if (result == olm_error()) - { - errorMessage = (const char *)olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s",(const char *)olm_inbound_group_session_last_error(sessionPtr)); - } - else - { - LOGD(" ## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), (char*)sessionIdPtr); - - returnValue = env->NewByteArray(result); - env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); - } - - free(sessionIdPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Decrypt a message. - * An exception is thrown if the operation fails. - * @param aEncryptedMsg the encrypted message - * @param aDecryptMessageResult the decryptMessage information - * @return the decrypted message - */ -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsgBuffer, jobject aDecryptionResult) -{ - jbyteArray decryptedMsgBuffer = 0; - const char* errorMessage = NULL; - - OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - jbyte *encryptedMsgPtr = NULL; - jclass indexObjJClass = 0; - jfieldID indexMsgFieldId; - - LOGD("## decryptMessageJni(): inbound group session IN"); - - if (!sessionPtr) - { - LOGE(" ## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"); - errorMessage = "invalid inbound group session ptr=NULL"; - } - else if (!aEncryptedMsgBuffer) - { - LOGE(" ## decryptMessageJni(): failure - invalid encrypted message"); - errorMessage = "invalid encrypted message"; - } - else if (!aDecryptionResult) - { - LOGE(" ## decryptMessageJni(): failure - invalid index object"); - errorMessage = "invalid index object"; - } - else if (!(encryptedMsgPtr = env->GetByteArrayElements(aEncryptedMsgBuffer, 0))) - { - LOGE(" ## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); - errorMessage = "encrypted message JNI allocation OOM"; - } - else if (!(indexObjJClass = env->GetObjectClass(aDecryptionResult))) - { - LOGE("## decryptMessageJni(): failure - unable to get index class"); - errorMessage = "unable to get index class"; - } - else if (!(indexMsgFieldId = env->GetFieldID(indexObjJClass,"mIndex","J"))) - { - LOGE("## decryptMessageJni(): failure - unable to get index type field"); - errorMessage = "unable to get index type field"; - } - else - { - // get encrypted message length - size_t encryptedMsgLength = (size_t)env->GetArrayLength(aEncryptedMsgBuffer); - uint8_t *tempEncryptedPtr = static_cast<uint8_t*>(malloc(encryptedMsgLength*sizeof(uint8_t))); - - // create a dedicated temp buffer to be used in next Olm API calls - if (!tempEncryptedPtr) - { - LOGE(" ## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"); - errorMessage = "tempEncryptedPtr allocation OOM"; - } - else - { - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - LOGD(" ## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%.*s",static_cast<long unsigned int>(encryptedMsgLength), static_cast<int>(encryptedMsgLength), encryptedMsgPtr); - - // get max plaintext length - size_t maxPlainTextLength = olm_group_decrypt_max_plaintext_length(sessionPtr, - tempEncryptedPtr, - encryptedMsgLength); - if (maxPlainTextLength == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage); - } - else - { - LOGD(" ## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength)); - - uint32_t messageIndex = 0; - - // allocate output decrypted message - uint8_t *plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t))); - - // decrypt, but before reload encrypted buffer (previous one was destroyed) - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - size_t plaintextLength = olm_group_decrypt(sessionPtr, - tempEncryptedPtr, - encryptedMsgLength, - plainTextMsgPtr, - maxPlainTextLength, - &messageIndex); - if (plaintextLength == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## decryptMessageJni(): failure - olm_group_decrypt Msg=%s", errorMessage); - } - else - { - // update index - env->SetLongField(aDecryptionResult, indexMsgFieldId, (jlong)messageIndex); - - decryptedMsgBuffer = env->NewByteArray(plaintextLength); - env->SetByteArrayRegion(decryptedMsgBuffer, 0 , plaintextLength, (jbyte*)plainTextMsgPtr); - - LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); - } - - if (plainTextMsgPtr) - { - memset(plainTextMsgPtr, 0, maxPlainTextLength*sizeof(uint8_t)); - free(plainTextMsgPtr); - } - } - - if (tempEncryptedPtr) - { - free(tempEncryptedPtr); - } - } - } - - // free alloc - if (encryptedMsgPtr) - { - env->ReleaseByteArrayElements(aEncryptedMsgBuffer, encryptedMsgPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return decryptedMsgBuffer; -} - -/** - * Provides the first known index. - * An exception is thrown if the operation fails. - * @return the first known index - */ -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(firstKnownIndexJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - long returnValue = 0; - - LOGD("## firstKnownIndexJni(): inbound group session IN"); - - if (!sessionPtr) - { - LOGE(" ## firstKnownIndexJni(): failure - invalid inbound group session instance"); - errorMessage = "invalid inbound group session instance"; - } - else - { - returnValue = olm_inbound_group_session_first_known_index(sessionPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Tells if the session is verified. - * An exception is thrown if the operation fails. - * @return true if the session is verified - */ -JNIEXPORT jboolean OLM_INBOUND_GROUP_SESSION_FUNC_DEF(isVerifiedJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - jboolean returnValue = JNI_FALSE; - - LOGD("## isVerifiedJni(): inbound group session IN"); - - if (!sessionPtr) - { - LOGE(" ## isVerifiedJni(): failure - invalid inbound group session instance"); - errorMessage = "invalid inbound group session instance"; - } - else - { - LOGE(" ## isVerifiedJni(): faaa %d", olm_inbound_group_session_is_verified(sessionPtr)); - - returnValue = (0 != olm_inbound_group_session_is_verified(sessionPtr)) ? JNI_TRUE : JNI_FALSE; - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Exports the session as byte array from a message index - * An exception is thrown if the operation fails. - * @param messageIndex key used to encrypt the serialized session data - * @return the session saved as bytes array - **/ -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(exportJni)(JNIEnv *env, jobject thiz, jlong messageIndex) { - jbyteArray exportedByteArray = 0; - const char* errorMessage = NULL; - OlmInboundGroupSession *sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - - LOGD("## exportJni(): inbound group session IN"); - - if (!sessionPtr) - { - LOGE(" ## exportJni (): failure - invalid inbound group session instance"); - errorMessage = "invalid inbound group session instance"; - } - else - { - size_t length = olm_export_inbound_group_session_length(sessionPtr); - - LOGD(" ## exportJni(): length =%lu", static_cast<long unsigned int>(length)); - - void *bufferPtr = malloc(length * sizeof(uint8_t)); - - if (!bufferPtr) - { - LOGE(" ## exportJni(): failure - pickledPtr buffer OOM"); - errorMessage = "pickledPtr buffer OOM"; - } - else - { - size_t result = olm_export_inbound_group_session(sessionPtr, - (uint8_t*)bufferPtr, - length, - (long) messageIndex); - - if (result == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## exportJni(): failure - olm_export_inbound_group_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## exportJni(): success - result=%lu buffer=%.*s", static_cast<long unsigned int>(result), static_cast<int>(length), static_cast<char*>(bufferPtr)); - - exportedByteArray = env->NewByteArray(length); - env->SetByteArrayRegion(exportedByteArray, 0 , length, (jbyte*)bufferPtr); - - // clean before leaving - memset(bufferPtr, 0, length); - } - - free(bufferPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return exportedByteArray; -} - -/** - * Serialize and encrypt session instance into a base64 string.<br> - * An exception is thrown if the operation fails. - * @param aKeyBuffer key used to encrypt the serialized session data - * @return a base64 string if operation succeed, null otherwise - **/ -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - - jbyteArray pickledDataRet = 0; - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - OlmInboundGroupSession* sessionPtr = getInboundGroupSessionInstanceId(env, thiz); - - LOGD("## inbound group session serializeJni(): IN"); - - if (!sessionPtr) - { - LOGE(" ## serializeJni(): failure - invalid session ptr"); - errorMessage = "invalid session ptr"; - } - else if (!aKeyBuffer) - { - LOGE(" ## serializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = olm_pickle_inbound_group_session_length(sessionPtr); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu", static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - - void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); - - if (!pickledPtr) - { - LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); - errorMessage = "pickledPtr buffer OOM"; - } - else - { - size_t result = olm_pickle_inbound_group_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(pickledLength), static_cast<char*>(pickledPtr)); - - pickledDataRet = env->NewByteArray(pickledLength); - env->SetByteArrayRegion(pickledDataRet, 0 , pickledLength, (jbyte*)pickledPtr); - } - - free(pickledPtr); - } - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return pickledDataRet; -} - -/** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - - OlmInboundGroupSession* sessionPtr = NULL; - size_t sessionSize = olm_inbound_group_session_size(); - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - jbyte* pickledPtr = NULL; - - LOGD("## deserializeJni(): IN"); - - if (!sessionSize) - { - LOGE(" ## deserializeJni(): failure - inbound group session size = 0"); - errorMessage = "inbound group session size = 0"; - } - else if (!(sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize))) - { - LOGE(" ## deserializeJni(): failure - session failure OOM"); - errorMessage = "session failure OOM"; - } - else if (!aKeyBuffer) - { - LOGE(" ## deserializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!aSerializedDataBuffer) - { - LOGE(" ## deserializeJni(): failure - serialized data"); - errorMessage = "serialized data"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0))) - { - LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM"); - errorMessage = "pickledPtr JNI allocation OOM"; - } - else - { - sessionPtr = olm_inbound_group_session(sessionPtr); - - size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int>(pickledLength), (char const *)pickledPtr); - - size_t result = olm_unpickle_inbound_group_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_inbound_group_session_last_error(sessionPtr); - LOGE(" ## deserializeJni(): failure - olm_unpickle_inbound_group_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result)); - } - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (pickledPtr) - { - env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT); - } - - if (errorMessage) - { - if (sessionPtr) - { - olm_clear_inbound_group_session(sessionPtr); - free(sessionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sessionPtr; -} diff --git a/android/olm-sdk/src/main/jni/olm_inbound_group_session.h b/android/olm-sdk/src/main/jni/olm_inbound_group_session.h deleted file mode 100644 index b4d21eb..0000000 --- a/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMINBOUND_GROUP_SESSION_H -#define _OLMINBOUND_GROUP_SESSION_H - -#include "olm_jni.h" -#include "olm/olm.h" -#include "olm/inbound_group_session.h" - -#define OLM_INBOUND_GROUP_SESSION_FUNC_DEF(func_name) FUNC_DEF(OlmInboundGroupSession,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -// session creation/destruction -JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aSessionKeyBuffer, jboolean isImported); - -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aEncryptedMsg, jobject aDecryptIndex); - -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(firstKnownIndexJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jboolean OLM_INBOUND_GROUP_SESSION_FUNC_DEF(isVerifiedJni)(JNIEnv *env, jobject thiz); - -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(exportJni)(JNIEnv *env, jobject thiz, jlong messageIndex); - -// serialization -JNIEXPORT jbyteArray OLM_INBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey); -JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_jni.h b/android/olm-sdk/src/main/jni/olm_jni.h deleted file mode 100644 index 110f089..0000000 --- a/android/olm-sdk/src/main/jni/olm_jni.h +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016,2018,2019 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMJNI_H -#define _OLMJNI_H - -#include <cstdlib> -#include <cstdio> -#include <string> -#include <string.h> -#include <sstream> -#include <jni.h> -#include <android/log.h> - - -#define TAG "OlmJniNative" - -/* logging macros */ -//#define ENABLE_JNI_LOG - -#ifdef NDK_DEBUG - #warning NDK_DEBUG is defined! -#endif - -#ifdef ENABLE_JNI_LOG - #warning ENABLE_JNI_LOG is defined! -#endif - -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__) - -#ifdef ENABLE_JNI_LOG - #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__) - #define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__) -#else - #define LOGD(...) - #define LOGW(...) -#endif - -#define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name - -namespace AndroidOlmSdk -{ - -} - - -#ifdef __cplusplus -extern "C" { -#endif - -// internal helper functions -bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize); - -struct OlmSession* getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmAccount* getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmInboundGroupSession* getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmOutboundGroupSession* getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmUtility* getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmPkDecryption* getPkDecryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmPkEncryption* getPkEncryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmPkSigning* getPkSigningInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); -struct OlmSAS* getOlmSasInstanceId(JNIEnv* aJniEnv, jobject aJavaObject); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_jni_helper.cpp b/android/olm-sdk/src/main/jni/olm_jni_helper.cpp deleted file mode 100644 index 47f83a8..0000000 --- a/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +++ /dev/null @@ -1,234 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016,2018,2019 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_jni_helper.h" -#include "olm/olm.h" -#include <sys/time.h> - -using namespace AndroidOlmSdk; - -/** -* Init a buffer with a given number of random values. -* @param aBuffer2Ptr the buffer to be initialized -* @param aRandomSize the number of random values to apply -* @return true if operation succeed, false otherwise -**/ -bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize) -{ - bool retCode = false; - int bufferLen = aRandomSize*sizeof(uint8_t); - - if (!aBuffer2Ptr) - { - LOGE("## setRandomInBuffer(): failure - aBuffer=NULL"); - } - else if (!aRandomSize) - { - LOGE("## setRandomInBuffer(): failure - random size=0"); - } - else if (!(*aBuffer2Ptr = (uint8_t*)malloc(bufferLen))) - { - LOGE("## setRandomInBuffer(): failure - alloc mem OOM"); - } - else - { - LOGD("## setRandomInBuffer(): randomSize=%lu",static_cast<long unsigned int>(aRandomSize)); - - // use the secureRandom class - jclass cls = env->FindClass("java/security/SecureRandom"); - - if (cls) - { - jobject newObj = 0; - jmethodID constructor = env->GetMethodID(cls, "<init>", "()V"); - jmethodID nextByteMethod = env->GetMethodID(cls, "nextBytes", "([B)V"); - - if (constructor) - { - newObj = env->NewObject(cls, constructor); - jbyteArray tempByteArray = env->NewByteArray(bufferLen); - - if (newObj && tempByteArray) - { - env->CallVoidMethod(newObj, nextByteMethod, tempByteArray); - - if (!env->ExceptionOccurred()) - { - jbyte* buffer = env->GetByteArrayElements(tempByteArray, NULL); - - if (buffer) - { - memcpy(*aBuffer2Ptr, buffer, bufferLen); - retCode = true; - - // clear tempByteArray to hide sensitive data. - memset(buffer, 0, bufferLen); - env->SetByteArrayRegion(tempByteArray, 0, bufferLen, buffer); - - // ensure that the buffer is released - env->ReleaseByteArrayElements(tempByteArray, buffer, JNI_ABORT); - } - } - } - - if (tempByteArray) - { - env->DeleteLocalRef(tempByteArray); - } - - if (newObj) - { - env->DeleteLocalRef(newObj); - } - } - } - - // debug purpose - /*for(int i = 0; i < aRandomSize; i++) - { - LOGD("## setRandomInBuffer(): randomBuffPtr[%ld]=%d",i, (*aBuffer2Ptr)[i]); - }*/ - } - - return retCode; -} - -/** -* Read the instance ID of the calling object. -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @param aCallingClass java calling class name -* @return the related instance ID -**/ -jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass) -{ - jlong instanceId = 0; - - if (aJniEnv) - { - jclass requiredClass = aJniEnv->FindClass(aCallingClass); - jclass loaderClass = 0; - - if (requiredClass && (JNI_TRUE != aJniEnv->IsInstanceOf(aJavaObject, requiredClass))) - { - LOGE("## getInstanceId() failure - invalid instance of"); - } - else if ((loaderClass = aJniEnv->GetObjectClass(aJavaObject))) - { - jfieldID instanceIdField = aJniEnv->GetFieldID(loaderClass, "mNativeId", "J"); - - if (instanceIdField) - { - instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField); - LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId); - } - else - { - LOGE("## getInstanceId() ERROR! GetFieldID=null"); - } - - aJniEnv->DeleteLocalRef(loaderClass); - } - else - { - LOGE("## getInstanceId() ERROR! GetObjectClass=null"); - } - } - else - { - LOGE("## getInstanceId() ERROR! aJniEnv=NULL"); - } - - LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId); - - return instanceId; -} - -/** -* Read the account instance ID of the calling object. -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the related OlmAccount. -**/ -struct OlmAccount* getAccountInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmAccount*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_ACCOUNT); -} - -/** -* Read the session instance ID of the calling object (aJavaObject).<br> -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the related OlmSession. -**/ -struct OlmSession* getSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmSession*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SESSION); -} - -/** -* Read the inbound group session instance ID of the calling object (aJavaObject).<br> -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the related OlmInboundGroupSession. -**/ -struct OlmInboundGroupSession* getInboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmInboundGroupSession*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_INBOUND_GROUP_SESSION); -} - -/** -* Read the outbound group session instance ID of the calling object (aJavaObject).<br> -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the related OlmOutboundGroupSession -**/ -struct OlmOutboundGroupSession* getOutboundGroupSessionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmOutboundGroupSession*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_OUTBOUND_GROUP_SESSION); -} - -/** -* Read the utility instance ID of the calling object (aJavaObject).<br> -* @param aJniEnv pointer pointing on the JNI function table -* @param aJavaObject reference to the object on which the method is invoked -* @return the related OlmUtility -**/ -struct OlmUtility* getUtilityInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmUtility*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_UTILITY); -} - -struct OlmPkDecryption* getPkDecryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmPkDecryption*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_PK_DECRYPTION); -} - -struct OlmPkEncryption* getPkEncryptionInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmPkEncryption*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_PK_ENCRYPTION); -} - -struct OlmPkSigning* getPkSigningInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmPkSigning*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_PK_SIGNING); -} - -struct OlmSAS* getOlmSasInstanceId(JNIEnv* aJniEnv, jobject aJavaObject) -{ - return (struct OlmSAS*)getInstanceId(aJniEnv, aJavaObject, CLASS_OLM_SAS); -} diff --git a/android/olm-sdk/src/main/jni/olm_jni_helper.h b/android/olm-sdk/src/main/jni/olm_jni_helper.h deleted file mode 100644 index 22552b4..0000000 --- a/android/olm-sdk/src/main/jni/olm_jni_helper.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016,2018,2019 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_jni.h" - -// constant strings -namespace AndroidOlmSdk -{ - static const char *CLASS_OLM_INBOUND_GROUP_SESSION = "org/matrix/olm/OlmInboundGroupSession"; - static const char *CLASS_OLM_OUTBOUND_GROUP_SESSION = "org/matrix/olm/OlmOutboundGroupSession"; - static const char *CLASS_OLM_SESSION = "org/matrix/olm/OlmSession"; - static const char *CLASS_OLM_ACCOUNT = "org/matrix/olm/OlmAccount"; - static const char *CLASS_OLM_UTILITY = "org/matrix/olm/OlmUtility"; - static const char *CLASS_OLM_PK_ENCRYPTION = "org/matrix/olm/OlmPkEncryption"; - static const char *CLASS_OLM_PK_DECRYPTION = "org/matrix/olm/OlmPkDecryption"; - static const char *CLASS_OLM_PK_SIGNING = "org/matrix/olm/OlmPkSigning"; - static const char *CLASS_OLM_SAS = "org/matrix/olm/OlmSAS"; -} diff --git a/android/olm-sdk/src/main/jni/olm_manager.cpp b/android/olm-sdk/src/main/jni/olm_manager.cpp deleted file mode 100644 index 8ee0df7..0000000 --- a/android/olm-sdk/src/main/jni/olm_manager.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_manager.h" - -using namespace AndroidOlmSdk; - -JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv* env, jobject thiz) -{ - uint8_t majorVer=0, minorVer=0, patchVer=0; - jstring returnValueStr=0; - char buff[150]; - - olm_get_library_version(&majorVer, &minorVer, &patchVer); - LOGD("## getOlmLibVersionJni(): Major=%d Minor=%d Patch=%d", majorVer, minorVer, patchVer); - - snprintf(buff, sizeof(buff), "%d.%d.%d", majorVer, minorVer, patchVer); - returnValueStr = env->NewStringUTF((const char*)buff); - - return returnValueStr; -}
\ No newline at end of file diff --git a/android/olm-sdk/src/main/jni/olm_manager.h b/android/olm-sdk/src/main/jni/olm_manager.h deleted file mode 100644 index d11c262..0000000 --- a/android/olm-sdk/src/main/jni/olm_manager.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMMANAGER_H -#define _OLMMANAGER_H - -#include "olm_jni.h" -#include "olm/olm.h" - -#define OLM_MANAGER_FUNC_DEF(func_name) FUNC_DEF(OlmManager,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -JNIEXPORT jstring OLM_MANAGER_FUNC_DEF(getOlmLibVersionJni)(JNIEnv *env, jobject thiz); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp b/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp deleted file mode 100644 index a22122a..0000000 --- a/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +++ /dev/null @@ -1,563 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_outbound_group_session.h" - -using namespace AndroidOlmSdk; - -/** - * Release the session allocation made by initializeOutboundGroupSessionMemory().<br> - * This method MUST be called when java counter part account instance is done. - * - */ -JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releaseSessionJni(): OutBound group session IN"); - - OlmOutboundGroupSession* sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - - if (!sessionPtr) - { - LOGE(" ## releaseSessionJni(): failure - invalid outbound group session instance"); - } - else - { - LOGD(" ## releaseSessionJni(): sessionPtr=%p",sessionPtr); - -#ifdef ENABLE_JNI_LOG - size_t retCode = olm_clear_outbound_group_session(sessionPtr); - LOGD(" ## releaseSessionJni(): clear_outbound_group_session=%lu",static_cast<long unsigned int>(retCode)); -#else - olm_clear_outbound_group_session(sessionPtr); -#endif - - LOGD(" ## releaseSessionJni(): free IN"); - free(sessionPtr); - LOGD(" ## releaseSessionJni(): free OUT"); - } -} - -/** - * Initialize a new outbound group session and return it to JAVA side.<br> - * Since a C prt is returned as a jlong, special care will be taken - * to make the cast (OlmOutboundGroupSession* => jlong) platform independent. - * @return the initialized OlmOutboundGroupSession* instance or throw an exception - **/ -JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - - OlmOutboundGroupSession* sessionPtr = NULL; - size_t sessionSize = 0; - - LOGD("## createNewSessionJni(): outbound group session IN"); - sessionSize = olm_outbound_group_session_size(); - - if (0 == sessionSize) - { - LOGE(" ## createNewSessionJni(): failure - outbound group session size = 0"); - errorMessage = "outbound group session size = 0"; - } - else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize))) - { - LOGE(" ## createNewSessionJni(): failure - outbound group session OOM"); - errorMessage = "outbound group session OOM"; - } - else - { - sessionPtr = olm_outbound_group_session(sessionPtr); - LOGD(" ## createNewSessionJni(): success - outbound group session size=%lu",static_cast<long unsigned int>(sessionSize)); - - // compute random buffer - size_t randomLength = olm_init_outbound_group_session_random_length(sessionPtr); - uint8_t *randomBuffPtr = NULL; - - LOGW(" ## createNewSessionJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength)); - - if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) - { - LOGE(" ## createNewSessionJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - if (0 == randomLength) - { - LOGW(" ## createNewSessionJni(): random buffer is not required"); - } - - size_t sessionResult = olm_init_outbound_group_session(sessionPtr, randomBuffPtr, randomLength); - - if (sessionResult == olm_error()) { - errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## createNewSessionJni(): failure - init outbound session creation Msg=%s", errorMessage); - } - else - { - LOGD(" ## createNewSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); - } - - // clear the random buffer - memset(randomBuffPtr, 0, randomLength); - free(randomBuffPtr); - } - } - - if (errorMessage) - { - if (sessionPtr) - { - olm_clear_outbound_group_session(sessionPtr); - free(sessionPtr); - } - - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sessionPtr; -} - -/** - * Return the session identifier. - * An exception is thrown if the operation fails. - * @return the session identifier - */ -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## sessionIdentifierJni(): outbound group session IN"); - - const char* errorMessage = NULL; - OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - jbyteArray returnValue = 0; - - if (!sessionPtr) - { - LOGE(" ## sessionIdentifierJni(): failure - invalid outbound group session instance"); - errorMessage = "invalid outbound group session instance"; - } - else - { - // get the size to alloc - size_t lengthSessionId = olm_outbound_group_session_id_length(sessionPtr); - LOGD(" ## sessionIdentifierJni(): outbound group session lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); - - uint8_t *sessionIdPtr = (uint8_t*)malloc(lengthSessionId*sizeof(uint8_t)); - - if (!sessionIdPtr) - { - LOGE(" ## sessionIdentifierJni(): failure - outbound identifier allocation OOM"); - errorMessage = "outbound identifier allocation OOM"; - } - else - { - size_t result = olm_outbound_group_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - - if (result == olm_error()) - { - errorMessage = reinterpret_cast<const char*>(olm_outbound_group_session_last_error(sessionPtr)); - LOGE(" ## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s", errorMessage); - } - else - { - returnValue = env->NewByteArray(result); - env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); - - LOGD(" ## sessionIdentifierJni(): success - outbound group session identifier result=%lu sessionId= %.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionIdPtr)); - } - - // free alloc - free(sessionIdPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - - -/** - * Get the current message index for this session.<br> - * Each message is sent with an increasing index, this - * method returns the index for the next message. - * An exception is thrown if the operation fails. - * @return current session index - */ -JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz) -{ - OlmOutboundGroupSession *sessionPtr = NULL; - jint indexRetValue = 0; - - LOGD("## messageIndexJni(): IN"); - - if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) - { - LOGE(" ## messageIndexJni(): failure - invalid outbound group session instance"); - } - else - { - indexRetValue = static_cast<jint>(olm_outbound_group_session_message_index(sessionPtr)); - } - - LOGD(" ## messageIndexJni(): success - index=%d",indexRetValue); - - return indexRetValue; -} - -/** - * Return the session key. - * An exception is thrown if the operation fails. - * @return the session key - */ -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## sessionKeyJni(): outbound group session IN"); - - const char* errorMessage = NULL; - OlmOutboundGroupSession *sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz); - jbyteArray returnValue = 0; - - if (!sessionPtr) - { - LOGE(" ## sessionKeyJni(): failure - invalid outbound group session instance"); - errorMessage = "invalid outbound group session instance"; - } - else - { - // get the size to alloc - size_t sessionKeyLength = olm_outbound_group_session_key_length(sessionPtr); - LOGD(" ## sessionKeyJni(): sessionKeyLength=%lu",static_cast<long unsigned int>(sessionKeyLength)); - - uint8_t *sessionKeyPtr = (uint8_t*)malloc(sessionKeyLength*sizeof(uint8_t)); - - if (!sessionKeyPtr) - { - LOGE(" ## sessionKeyJni(): failure - session key allocation OOM"); - errorMessage = "session key allocation OOM"; - } - else - { - size_t result = olm_outbound_group_session_key(sessionPtr, sessionKeyPtr, sessionKeyLength); - - if (result == olm_error()) - { - errorMessage = (const char *)olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## sessionKeyJni(): failure - session key failure Msg=%s", errorMessage); - } - else - { - LOGD(" ## sessionKeyJni(): success - outbound group session key result=%lu sessionKey=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), reinterpret_cast<char*>(sessionKeyPtr)); - - returnValue = env->NewByteArray(result); - env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionKeyPtr); - } - - // free alloc - free(sessionKeyPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Encrypt a bytes buffer messages. - * An exception is thrown if the operation fails. - * @param aClearMsgBuffer the message to encode - * @return the encoded message - */ -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer) -{ - LOGD("## encryptMessageJni(): IN"); - - const char* errorMessage = NULL; - jbyteArray encryptedMsgRet = 0; - - OlmOutboundGroupSession *sessionPtr = NULL; - jbyte* clearMsgPtr = NULL; - jboolean clearMsgIsCopied = JNI_FALSE; - - if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) - { - LOGE(" ## encryptMessageJni(): failure - invalid outbound group session ptr=NULL"); - errorMessage = "invalid outbound group session ptr=NULL"; - } - else if (!aClearMsgBuffer) - { - LOGE(" ## encryptMessageJni(): failure - invalid clear message"); - errorMessage = "invalid clear message"; - } - else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, &clearMsgIsCopied))) - { - LOGE(" ## encryptMessageJni(): failure - clear message JNI allocation OOM"); - errorMessage = "clear message JNI allocation OOM"; - } - else - { - // get clear message length - size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer); - LOGD(" ## encryptMessageJni(): clearMsgLength=%lu",static_cast<long unsigned int>(clearMsgLength)); - - // compute max encrypted length - size_t encryptedMsgLength = olm_group_encrypt_message_length(sessionPtr,clearMsgLength); - uint8_t *encryptedMsgPtr = (uint8_t*)malloc(encryptedMsgLength*sizeof(uint8_t)); - - if (!encryptedMsgPtr) - { - LOGE(" ## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); - errorMessage = "encryptedMsgPtr buffer OOM"; - } - else - { - LOGD(" ## encryptMessageJni(): estimated encryptedMsgLength=%lu",static_cast<long unsigned int>(encryptedMsgLength)); - - size_t encryptedLength = olm_group_encrypt(sessionPtr, - (uint8_t*)clearMsgPtr, - clearMsgLength, - encryptedMsgPtr, - encryptedMsgLength); - - - if (encryptedLength == olm_error()) - { - errorMessage = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## encryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s", errorMessage); - } - else - { - LOGD(" ## encryptMessageJni(): encrypted returnedLg=%lu plainTextMsgPtr=%.*s",static_cast<long unsigned int>(encryptedLength), static_cast<int>(encryptedLength), reinterpret_cast<char*>(encryptedMsgPtr)); - - encryptedMsgRet = env->NewByteArray(encryptedLength); - env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedLength, (jbyte*)encryptedMsgPtr); - } - - free(encryptedMsgPtr); - } - } - - // free alloc - if (clearMsgPtr) - { - if (clearMsgIsCopied) - { - memset(clearMsgPtr, 0, (size_t)env->GetArrayLength(aClearMsgBuffer)); - } - env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return encryptedMsgRet; -} - -/** - * Serialize and encrypt session instance into a base64 string.<br> - * An exception is thrown if the operation fails. - * @param aKey key used to encrypt the serialized session data - * @return a base64 string if operation succeed, null otherwise - **/ -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - OlmOutboundGroupSession* sessionPtr = NULL; - - LOGD("## outbound group session serializeJni(): IN"); - - if (!(sessionPtr = (OlmOutboundGroupSession*)getOutboundGroupSessionInstanceId(env,thiz))) - { - LOGE(" ## serializeJni(): failure - invalid session ptr"); - errorMessage = "invalid session ptr"; - } - else if (!aKeyBuffer) - { - LOGE(" ## serializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = olm_pickle_outbound_group_session_length(sessionPtr); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - - void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); - - if(!pickledPtr) - { - LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); - errorMessage = "pickledPtr buffer OOM"; - } - else - { - size_t result = olm_pickle_outbound_group_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## serializeJni(): failure - olm_pickle_outbound_group_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(result), static_cast<char*>(pickledPtr)); - - returnValue = env->NewByteArray(pickledLength); - env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr); - } - } - - free(pickledPtr); - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ -JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - size_t sessionSize = olm_outbound_group_session_size(); - OlmOutboundGroupSession* sessionPtr = NULL; - - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - jbyte* pickledPtr = NULL; - - LOGD("## deserializeJni(): IN"); - - if (!sessionSize) - { - LOGE(" ## deserializeJni(): failure - outbound group session size = 0"); - errorMessage = "outbound group session size = 0"; - } - else if (!(sessionPtr = (OlmOutboundGroupSession*)malloc(sessionSize))) - { - LOGE(" ## deserializeJni(): failure - session failure OOM"); - errorMessage = "session failure OOM"; - } - else if (!aKeyBuffer) - { - LOGE(" ## deserializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!aSerializedDataBuffer) - { - LOGE(" ## deserializeJni(): failure - serialized data"); - errorMessage = "invalid serialized data"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0))) - { - LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM"); - errorMessage = "pickledPtr JNI allocation OOM"; - } - else - { - sessionPtr = olm_outbound_group_session(sessionPtr); - size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int>(pickledLength), (char const *)pickledPtr); - - size_t result = olm_unpickle_outbound_group_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_outbound_group_session_last_error(sessionPtr); - LOGE(" ## deserializeJni(): failure - olm_unpickle_outbound_group_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result)); - } - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (pickledPtr) - { - env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT); - } - - if (errorMessage) - { - if (sessionPtr) - { - olm_clear_outbound_group_session(sessionPtr); - free(sessionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sessionPtr; -} - diff --git a/android/olm-sdk/src/main/jni/olm_outbound_group_session.h b/android/olm-sdk/src/main/jni/olm_outbound_group_session.h deleted file mode 100644 index 051dd2f..0000000 --- a/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMOUTBOUND_GROUP_SESSION_H -#define _OLMOUTBOUND_GROUP_SESSION_H - -#include "olm_jni.h" -#include "olm/olm.h" -#include "olm/outbound_group_session.h" - -#define OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(func_name) FUNC_DEF(OlmOutboundGroupSession,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -// session creation/destruction -JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); - -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(messageIndexJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionKeyJni)(JNIEnv *env, jobject thiz); - -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer); - -// serialization -JNIEXPORT jbyteArray OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey); -JNIEXPORT jlong OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_pk.cpp b/android/olm-sdk/src/main/jni/olm_pk.cpp deleted file mode 100644 index c528342..0000000 --- a/android/olm-sdk/src/main/jni/olm_pk.cpp +++ /dev/null @@ -1,992 +0,0 @@ -/* - * Copyright 2018,2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_pk.h" - -#include "olm/olm.h" - -using namespace AndroidOlmSdk; - -OlmPkEncryption * initializePkEncryptionMemory() -{ - size_t encryptionSize = olm_pk_encryption_size(); - OlmPkEncryption *encryptionPtr = (OlmPkEncryption *)malloc(encryptionSize); - - if (encryptionPtr) - { - // init encryption object - encryptionPtr = olm_pk_encryption(encryptionPtr); - LOGD( - "## initializePkEncryptionMemory(): success - OLM encryption size=%lu", - static_cast<long unsigned int>(encryptionSize) - ); - } - else - { - LOGE("## initializePkEncryptionMemory(): failure - OOM"); - } - - return encryptionPtr; -} - -JNIEXPORT jlong OLM_PK_ENCRYPTION_FUNC_DEF(createNewPkEncryptionJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmPkEncryption *encryptionPtr = initializePkEncryptionMemory(); - - // init encryption memory allocation - if (!encryptionPtr) - { - LOGE("## createNewPkEncryptionJni(): failure - init encryption OOM"); - errorMessage = "init encryption OOM"; - } - else - { - LOGD("## createNewPkEncryptionJni(): success - OLM encryption created"); - LOGD( - "## createNewPkEncryptionJni(): encryptionPtr=%p (jlong)(intptr_t)encryptionPtr=%lld", - encryptionPtr, (jlong)(intptr_t)encryptionPtr - ); - } - - if (errorMessage) - { - // release the allocated data - if (encryptionPtr) - { - olm_clear_pk_encryption(encryptionPtr); - free(encryptionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)encryptionPtr; -} - -JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(releasePkEncryptionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releasePkEncryptionJni(): IN"); - - OlmPkEncryption* encryptionPtr = getPkEncryptionInstanceId(env, thiz); - - if (!encryptionPtr) - { - LOGE(" ## releasePkEncryptionJni(): failure - invalid Encryption ptr=NULL"); - } - else - { - LOGD(" ## releasePkEncryptionJni(): encryptionPtr=%p", encryptionPtr); - olm_clear_pk_encryption(encryptionPtr); - - LOGD(" ## releasePkEncryptionJni(): IN"); - // even if free(NULL) does not crash, logs are performed for debug - // purpose - free(encryptionPtr); - LOGD(" ## releasePkEncryptionJni(): OUT"); - } -} - -JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)( - JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer -) { - const char *errorMessage = NULL; - jbyte *keyPtr = NULL; - - OlmPkEncryption *encryptionPtr = getPkEncryptionInstanceId(env, thiz); - - if (!encryptionPtr) - { - LOGE(" ## pkSetRecipientKeyJni(): failure - invalid Encryption ptr=NULL"); - } - else if (!aKeyBuffer) - { - LOGE(" ## pkSetRecipientKeyJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0))) - { - LOGE(" ## pkSetRecipientKeyJni(): failure - key JNI allocation OOM"); - errorMessage = "key JNI allocation OOM"; - } - else - { - if (olm_pk_encryption_set_recipient_key(encryptionPtr, keyPtr, (size_t)env->GetArrayLength(aKeyBuffer)) == olm_error()) - { - errorMessage = olm_pk_encryption_last_error(encryptionPtr); - LOGE( - " ## pkSetRecipientKeyJni(): failure - olm_pk_encryption_set_recipient_key Msg=%s", - errorMessage - ); - } - } - - if (keyPtr) - { - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)( - JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg -) { - jbyteArray encryptedMsgRet = 0; - const char* errorMessage = NULL; - jbyte *plaintextPtr = NULL; - jboolean plaintextIsCopied = JNI_FALSE; - - OlmPkEncryption *encryptionPtr = getPkEncryptionInstanceId(env, thiz); - jclass encryptedMsgJClass = 0; - jfieldID macFieldId; - jfieldID ephemeralFieldId; - - if (!encryptionPtr) - { - LOGE(" ## pkEncryptJni(): failure - invalid Encryption ptr=NULL"); - } - else if (!aPlaintextBuffer) - { - LOGE(" ## pkEncryptJni(): failure - invalid clear message"); - errorMessage = "invalid clear message"; - } - else if (!(plaintextPtr = env->GetByteArrayElements(aPlaintextBuffer, &plaintextIsCopied))) - { - LOGE(" ## pkEncryptJni(): failure - plaintext JNI allocation OOM"); - errorMessage = "plaintext JNI allocation OOM"; - } - else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) - { - LOGE(" ## pkEncryptJni(): failure - unable to get encrypted message class"); - errorMessage = "unable to get encrypted message class"; - } - else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass, "mMac", "Ljava/lang/String;"))) - { - LOGE("## pkEncryptJni(): failure - unable to get MAC field"); - errorMessage = "unable to get MAC field"; - } - else if (!(ephemeralFieldId = env->GetFieldID(encryptedMsgJClass, "mEphemeralKey", "Ljava/lang/String;"))) - { - LOGE("## pkEncryptJni(): failure - unable to get ephemeral key field"); - errorMessage = "unable to get ephemeral key field"; - } - else - { - size_t plaintextLength = (size_t)env->GetArrayLength(aPlaintextBuffer); - size_t ciphertextLength = olm_pk_ciphertext_length(encryptionPtr, plaintextLength); - size_t macLength = olm_pk_mac_length(encryptionPtr); - size_t ephemeralLength = olm_pk_key_length(); - uint8_t *ciphertextPtr = NULL, *macPtr = NULL, *ephemeralPtr = NULL; - size_t randomLength = olm_pk_encrypt_random_length(encryptionPtr); - uint8_t *randomBuffPtr = NULL; - LOGD("## pkEncryptJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength)); - if (!(ciphertextPtr = (uint8_t*)malloc(ciphertextLength))) - { - LOGE("## pkEncryptJni(): failure - ciphertext JNI allocation OOM"); - errorMessage = "ciphertext JNI allocation OOM"; - } - else if (!(macPtr = (uint8_t*)malloc(macLength + 1))) - { - LOGE("## pkEncryptJni(): failure - MAC JNI allocation OOM"); - errorMessage = "MAC JNI allocation OOM"; - } - else if (!(ephemeralPtr = (uint8_t*)malloc(ephemeralLength + 1))) - { - LOGE("## pkEncryptJni(): failure: ephemeral key JNI allocation OOM"); - errorMessage = "ephemeral JNI allocation OOM"; - } - else if (!setRandomInBuffer(env, &randomBuffPtr, randomLength)) - { - LOGE("## pkEncryptJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - macPtr[macLength] = '\0'; - ephemeralPtr[ephemeralLength] = '\0'; - - size_t returnValue = olm_pk_encrypt( - encryptionPtr, - plaintextPtr, plaintextLength, - ciphertextPtr, ciphertextLength, - macPtr, macLength, - ephemeralPtr, ephemeralLength, - randomBuffPtr, randomLength - ); - - if (returnValue == olm_error()) - { - errorMessage = olm_pk_encryption_last_error(encryptionPtr); - LOGE("## pkEncryptJni(): failure - olm_pk_encrypt Msg=%s", errorMessage); - } - else - { - encryptedMsgRet = env->NewByteArray(ciphertextLength); - env->SetByteArrayRegion( - encryptedMsgRet, 0, ciphertextLength, (jbyte*)ciphertextPtr - ); - - jstring macStr = env->NewStringUTF((char*)macPtr); - env->SetObjectField(aEncryptedMsg, macFieldId, macStr); - jstring ephemeralStr = env->NewStringUTF((char*)ephemeralPtr); - env->SetObjectField(aEncryptedMsg, ephemeralFieldId, ephemeralStr); - } - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomLength); - free(randomBuffPtr); - } - if (ephemeralPtr) - { - free(ephemeralPtr); - } - if (macPtr) - { - free(macPtr); - } - if (ciphertextPtr) - { - free(ciphertextPtr); - } - } - - if (plaintextPtr) - { - if (plaintextIsCopied) - { - memset(plaintextPtr, 0, (size_t)env->GetArrayLength(aPlaintextBuffer)); - } - env->ReleaseByteArrayElements(aPlaintextBuffer, plaintextPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return encryptedMsgRet; -} - -OlmPkDecryption * initializePkDecryptionMemory() -{ - size_t decryptionSize = olm_pk_decryption_size(); - OlmPkDecryption *decryptionPtr = (OlmPkDecryption *)malloc(decryptionSize); - - if (decryptionPtr) - { - // init decryption object - decryptionPtr = olm_pk_decryption(decryptionPtr); - LOGD( - "## initializePkDecryptionMemory(): success - OLM decryption size=%lu", - static_cast<long unsigned int>(decryptionSize) - ); - } - else - { - LOGE("## initializePkDecryptionMemory(): failure - OOM"); - } - - return decryptionPtr; -} - -JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmPkDecryption *decryptionPtr = initializePkDecryptionMemory(); - - // init encryption memory allocation - if (!decryptionPtr) - { - LOGE("## createNewPkDecryptionJni(): failure - init decryption OOM"); - errorMessage = "init decryption OOM"; - } - else - { - LOGD("## createNewPkDecryptionJni(): success - OLM decryption created"); - LOGD( - "## createNewPkDecryptionJni(): decryptionPtr=%p (jlong)(intptr_t)decryptionPtr=%lld", - decryptionPtr, (jlong)(intptr_t)decryptionPtr - ); - } - - if (errorMessage) - { - // release the allocated data - if (decryptionPtr) - { - olm_clear_pk_decryption(decryptionPtr); - free(decryptionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)decryptionPtr; -} - -JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releasePkDecryptionJni(): IN"); - - OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz); - - if (!decryptionPtr) - { - LOGE(" ## releasePkDecryptionJni(): failure - invalid Decryption ptr=NULL"); - } - else - { - LOGD(" ## releasePkDecryptionJni(): decryptionPtr=%p", encryptionPtr); - olm_clear_pk_decryption(decryptionPtr); - - LOGD(" ## releasePkDecryptionJni(): IN"); - // even if free(NULL) does not crash, logs are performed for debug - // purpose - free(decryptionPtr); - LOGD(" ## releasePkDecryptionJni(): OUT"); - } -} - -JNIEXPORT jint OLM_PK_DECRYPTION_FUNC_DEF(privateKeyLength)(JNIEnv *env, jobject thiz) -{ - return (jint) olm_pk_private_key_length(); -} - -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(setPrivateKeyJni)(JNIEnv *env, jobject thiz, jbyteArray key) -{ - jbyteArray publicKeyRet = 0; - jbyte *keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - - const char* errorMessage = NULL; - - OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz); - - if (!decryptionPtr) - { - LOGE(" ## pkSetPrivateKeyJni(): failure - invalid Decryption ptr=NULL"); - } - else if (!key) - { - LOGE(" ## pkSetPrivateKeyJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(keyPtr = env->GetByteArrayElements(key, &keyWasCopied))) - { - LOGE(" ## pkSetPrivateKeyJni(): failure - key JNI allocation OOM"); - errorMessage = "key JNI allocation OOM"; - } - else - { - size_t publicKeyLength = olm_pk_key_length(); - uint8_t *publicKeyPtr = NULL; - size_t keyLength = (size_t)env->GetArrayLength(key); - if (!(publicKeyPtr = (uint8_t*)malloc(publicKeyLength))) - { - LOGE("## pkSetPrivateKeyJni(): failure - public key JNI allocation OOM"); - errorMessage = "public key JNI allocation OOM"; - } - else - { - size_t returnValue = olm_pk_key_from_private( - decryptionPtr, - publicKeyPtr, publicKeyLength, - keyPtr, keyLength - ); - if (returnValue == olm_error()) - { - errorMessage = olm_pk_decryption_last_error(decryptionPtr); - LOGE(" ## pkSetPrivateKeyJni(): failure - olm_pk_key_from_private Msg=%s", errorMessage); - } - else - { - publicKeyRet = env->NewByteArray(publicKeyLength); - env->SetByteArrayRegion( - publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr - ); - } - } - } - - if (keyPtr) - { - if (keyWasCopied) - { - memset(keyPtr, 0, (size_t)env->GetArrayLength(key)); - } - env->ReleaseByteArrayElements(key, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return publicKeyRet; -} - -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz) -{ - size_t randomLength = olm_pk_private_key_length(); - uint8_t *randomBuffPtr = NULL; - - jbyteArray publicKeyRet = 0; - uint8_t *publicKeyPtr = NULL; - size_t publicKeyLength = olm_pk_key_length(); - const char* errorMessage = NULL; - - OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz); - - if (!decryptionPtr) - { - LOGE(" ## pkGenerateKeyJni(): failure - invalid Decryption ptr=NULL"); - errorMessage = "invalid Decryption ptr=NULL"; - } - else if (!setRandomInBuffer(env, &randomBuffPtr, randomLength)) - { - LOGE("## pkGenerateKeyJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else if (!(publicKeyPtr = static_cast<uint8_t*>(malloc(publicKeyLength)))) - { - LOGE("## pkGenerateKeyJni(): failure - public key allocation OOM"); - errorMessage = "public key allocation OOM"; - } - else - { - if (olm_pk_key_from_private(decryptionPtr, publicKeyPtr, publicKeyLength, randomBuffPtr, randomLength) == olm_error()) - { - errorMessage = olm_pk_decryption_last_error(decryptionPtr); - LOGE("## pkGenerateKeyJni(): failure - olm_pk_generate_key Msg=%s", errorMessage); - } - else - { - publicKeyRet = env->NewByteArray(publicKeyLength); - env->SetByteArrayRegion(publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr); - LOGD("## pkGenerateKeyJni(): public key generated"); - } - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomLength); - free(randomBuffPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return publicKeyRet; -} - -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(privateKeyJni)(JNIEnv *env, jobject thiz) -{ - jbyteArray privateKeyRet = 0; - - const char* errorMessage = NULL; - - OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz); - - if (!decryptionPtr) - { - LOGE(" ## pkPrivateKeyJni(): failure - invalid Decryption ptr=NULL"); - } - else - { - size_t privateKeyLength = olm_pk_private_key_length(); - uint8_t *privateKeyPtr = NULL; - if (!(privateKeyPtr = (uint8_t*)malloc(privateKeyLength))) - { - LOGE("## pkPrivateKeyJni(): failure - private key JNI allocation OOM"); - errorMessage = "private key JNI allocation OOM"; - } - else - { - size_t returnValue = olm_pk_get_private_key( - decryptionPtr, - privateKeyPtr, privateKeyLength - ); - if (returnValue == olm_error()) - { - errorMessage = olm_pk_decryption_last_error(decryptionPtr); - LOGE(" ## pkPrivateKeyJni(): failure - olm_pk_get_private_key Msg=%s", errorMessage); - } - else - { - privateKeyRet = env->NewByteArray(privateKeyLength); - env->SetByteArrayRegion( - privateKeyRet, 0, privateKeyLength, (jbyte*)privateKeyPtr - ); - memset(privateKeyPtr, 0, privateKeyLength); - } - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return privateKeyRet; -} - -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)( - JNIEnv *env, jobject thiz, jobject aEncryptedMsg -) { - const char* errorMessage = NULL; - OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz); - - jclass encryptedMsgJClass = 0; - jstring ciphertextJstring = 0; - jstring macJstring = 0; - jstring ephemeralKeyJstring = 0; - jfieldID ciphertextFieldId; - jfieldID macFieldId; - jfieldID ephemeralKeyFieldId; - - const char *ciphertextPtr = NULL; - const char *macPtr = NULL; - const char *ephemeralKeyPtr = NULL; - - jbyteArray decryptedMsgRet = 0; - - if (!decryptionPtr) - { - LOGE(" ## pkDecryptJni(): failure - invalid Decryption ptr=NULL"); - errorMessage = "invalid Decryption ptr=NULL"; - } - else if (!aEncryptedMsg) - { - LOGE(" ## pkDecryptJni(): failure - invalid encrypted message"); - errorMessage = "invalid encrypted message"; - } - else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) - { - LOGE("## pkDecryptJni(): failure - unable to get encrypted message class"); - errorMessage = "unable to get encrypted message class"; - } - else if (!(ciphertextFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) - { - LOGE("## pkDecryptJni(): failure - unable to get message field"); - errorMessage = "unable to get message field"; - } - else if (!(ciphertextJstring = (jstring)env->GetObjectField(aEncryptedMsg, ciphertextFieldId))) - { - LOGE("## pkDecryptJni(): failure - no ciphertext"); - errorMessage = "no ciphertext"; - } - else if (!(ciphertextPtr = env->GetStringUTFChars(ciphertextJstring, 0))) - { - LOGE("## pkDecryptJni(): failure - ciphertext JNI allocation OOM"); - errorMessage = "ciphertext JNI allocation OOM"; - } - else if (!(ciphertextJstring = (jstring)env->GetObjectField(aEncryptedMsg, ciphertextFieldId))) - { - LOGE("## pkDecryptJni(): failure - no ciphertext"); - errorMessage = "no ciphertext"; - } - else if (!(ciphertextPtr = env->GetStringUTFChars(ciphertextJstring, 0))) - { - LOGE("## decryptMessageJni(): failure - ciphertext JNI allocation OOM"); - errorMessage = "ciphertext JNI allocation OOM"; - } - else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass,"mMac","Ljava/lang/String;"))) - { - LOGE("## pkDecryptJni(): failure - unable to get MAC field"); - errorMessage = "unable to get MAC field"; - } - else if (!(macJstring = (jstring)env->GetObjectField(aEncryptedMsg, macFieldId))) - { - LOGE("## pkDecryptJni(): failure - no MAC"); - errorMessage = "no MAC"; - } - else if (!(macPtr = env->GetStringUTFChars(macJstring, 0))) - { - LOGE("## pkDecryptJni(): failure - MAC JNI allocation OOM"); - errorMessage = "ciphertext JNI allocation OOM"; - } - else if (!(ephemeralKeyFieldId = env->GetFieldID(encryptedMsgJClass,"mEphemeralKey","Ljava/lang/String;"))) - { - LOGE("## pkDecryptJni(): failure - unable to get ephemeral key field"); - errorMessage = "unable to get ephemeral key field"; - } - else if (!(ephemeralKeyJstring = (jstring)env->GetObjectField(aEncryptedMsg, ephemeralKeyFieldId))) - { - LOGE("## pkDecryptJni(): failure - no ephemeral key"); - errorMessage = "no ephemeral key"; - } - else if (!(ephemeralKeyPtr = env->GetStringUTFChars(ephemeralKeyJstring, 0))) - { - LOGE("## pkDecryptJni(): failure - ephemeral key JNI allocation OOM"); - errorMessage = "ephemeral key JNI allocation OOM"; - } - else - { - size_t maxPlaintextLength = olm_pk_max_plaintext_length( - decryptionPtr, - (size_t)env->GetStringUTFLength(ciphertextJstring) - ); - uint8_t *plaintextPtr = NULL; - uint8_t *tempCiphertextPtr = NULL; - size_t ciphertextLength = (size_t)env->GetStringUTFLength(ciphertextJstring); - if (!(plaintextPtr = (uint8_t*)malloc(maxPlaintextLength))) - { - LOGE("## pkDecryptJni(): failure - plaintext JNI allocation OOM"); - errorMessage = "plaintext JNI allocation OOM"; - } - else if (!(tempCiphertextPtr = (uint8_t*)malloc(ciphertextLength))) - { - LOGE("## pkDecryptJni(): failure - temp ciphertext JNI allocation OOM"); - } - else - { - memcpy(tempCiphertextPtr, ciphertextPtr, ciphertextLength); - size_t plaintextLength = olm_pk_decrypt( - decryptionPtr, - ephemeralKeyPtr, (size_t)env->GetStringUTFLength(ephemeralKeyJstring), - macPtr, (size_t)env->GetStringUTFLength(macJstring), - tempCiphertextPtr, ciphertextLength, - plaintextPtr, maxPlaintextLength - ); - if (plaintextLength == olm_error()) - { - errorMessage = olm_pk_decryption_last_error(decryptionPtr); - LOGE("## pkDecryptJni(): failure - olm_pk_decrypt Msg=%s", errorMessage); - } - else - { - decryptedMsgRet = env->NewByteArray(plaintextLength); - env->SetByteArrayRegion(decryptedMsgRet, 0, plaintextLength, (jbyte*)plaintextPtr); - LOGD( - "## pkDecryptJni(): success returnedLg=%lu OK", - static_cast<long unsigned int>(plaintextLength) - ); - } - } - - if (tempCiphertextPtr) - { - free(tempCiphertextPtr); - } - if (plaintextPtr) - { - memset(plaintextPtr, 0, maxPlaintextLength); - free(plaintextPtr); - } - } - - if (ciphertextPtr) - { - env->ReleaseStringUTFChars(ciphertextJstring, ciphertextPtr); - } - if (macPtr) - { - env->ReleaseStringUTFChars(macJstring, macPtr); - } - if (ephemeralKeyPtr) - { - env->ReleaseStringUTFChars(ephemeralKeyJstring, ephemeralKeyPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return decryptedMsgRet; -} - -OlmPkSigning * initializePkSigningMemory() -{ - size_t signingSize = olm_pk_signing_size(); - OlmPkSigning *signingPtr = (OlmPkSigning *)malloc(signingSize); - - if (signingPtr) - { - // init encryption object - signingPtr = olm_pk_signing(signingPtr); - LOGD( - "## initializePkSigningMemory(): success - OLM signing size=%lu", - static_cast<long unsigned int>(signingSize) - ); - } - else - { - LOGE("## initializePkSigningMemory(): failure - OOM"); - } - - return signingPtr; -} - -JNIEXPORT jlong OLM_PK_SIGNING_FUNC_DEF(createNewPkSigningJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - OlmPkSigning *signingPtr = initializePkSigningMemory(); - - // init signing memory allocation - if (!signingPtr) - { - LOGE("## createNewPkSigningJni(): failure - init signing OOM"); - errorMessage = "init signing OOM"; - } - else - { - LOGD("## createNewPkSigningJni(): success - OLM signing created"); - LOGD( - "## createNewPkSigningJni(): signingPtr=%p (jlong)(intptr_t)signingPtr=%lld", - signingPtr, (jlong)(intptr_t)signingPtr - ); - } - - if (errorMessage) - { - // release the allocated data - if (signingPtr) - { - olm_clear_pk_signing(signingPtr); - free(signingPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)signingPtr; -} - -JNIEXPORT void OLM_PK_SIGNING_FUNC_DEF(releasePkSigningJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releasePkSigningJni(): IN"); - - OlmPkSigning* signingPtr = getPkSigningInstanceId(env, thiz); - - if (!signingPtr) - { - LOGE(" ## releasePkSigningJni(): failure - invalid Signing ptr=NULL"); - } - else - { - LOGD(" ## releasePkSigningJni(): signingPtr=%p", signingPtr); - olm_clear_pk_signing(signingPtr); - - LOGD(" ## releasePkSigningJni(): IN"); - // even if free(NULL) does not crash, logs are performed for debug - // purpose - free(signingPtr); - LOGD(" ## releasePkSigningJni(): OUT"); - } -} - -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(generateSeedJni)(JNIEnv *env, jobject thiz) -{ - size_t randomLength = olm_pk_signing_seed_length(); - uint8_t *randomBuffPtr = NULL; - jbyteArray randomRet = 0; - const char* errorMessage = NULL; - - if (!setRandomInBuffer(env, &randomBuffPtr, randomLength)) - { - errorMessage = "random buffer init"; - LOGE("## pkSigningGenerateSeedJni(): failure - %s", errorMessage); - } - else if (!(randomRet = env->NewByteArray(randomLength))) - { - errorMessage = "randomRet JNI allocation OOM"; - LOGE(" ## pkSigningGenerateSeedJni(): falure - %s", errorMessage); - } - else - { - env->SetByteArrayRegion( - randomRet, 0, randomLength, (jbyte*)randomBuffPtr - ); - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomLength); - free(randomBuffPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return randomRet; -} - -JNIEXPORT jint OLM_PK_SIGNING_FUNC_DEF(seedLength)(JNIEnv *env, jobject thiz) -{ - return (jint) olm_pk_signing_seed_length(); -} - -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(setKeyFromSeedJni)(JNIEnv *env, jobject thiz, jbyteArray seed) -{ - const char* errorMessage = NULL; - OlmPkSigning *signingPtr = getPkSigningInstanceId(env, thiz); - - jbyteArray publicKeyRet = 0; - jbyte *seedPtr = NULL; - jboolean seedWasCopied = JNI_FALSE; - - if (!signingPtr) - { - errorMessage = "invalid Siging ptr=NULL"; - LOGE(" ## setPkSigningKeyFromSeedJni(): failure - %s", errorMessage); - } - else if (!seed) - { - errorMessage = "invalid seed"; - LOGE(" ## setPkSigningKeyFromSeedJni: failure - %s", errorMessage); - } - else if (!(seedPtr = env->GetByteArrayElements(seed, &seedWasCopied))) - { - errorMessage = "seed JNI allocation OOM"; - LOGE(" ## setPkSigningKeyFromSeedJni(): failure - %s", errorMessage); - } - else - { - size_t publicKeyLength = olm_pk_signing_public_key_length(); - uint8_t *publicKeyPtr = NULL; - size_t seedLength = (size_t)env->GetArrayLength(seed); - if (!(publicKeyPtr = (uint8_t*)malloc(publicKeyLength))) - { - errorMessage = "public key JNI allocation OOM"; - LOGE(" ## setPkSigningKeyFromSeedJni(): falure - %s", errorMessage); - } - else - { - size_t returnValue = olm_pk_signing_key_from_seed( - signingPtr, - publicKeyPtr, publicKeyLength, - seedPtr, seedLength - ); - if (returnValue == olm_error()) - { - errorMessage = olm_pk_signing_last_error(signingPtr); - LOGE(" ## setPkSigningKeyFromSeedJni: failure - olm_pk_signing_key_from_seed Msg=%s", errorMessage); - } - else - { - if (!(publicKeyRet = env->NewByteArray(publicKeyLength))) { - errorMessage = "publicKeyRet JNI allocation OOM"; - LOGE(" ## setPkSigningKeyFromSeedJni(): falure - %s", errorMessage); - } else { - env->SetByteArrayRegion( - publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr - ); - } - } - } - } - - if (seedPtr) - { - if (seedWasCopied) - { - memset(seedPtr, 0, (size_t)env->GetArrayLength(seed)); - } - env->ReleaseByteArrayElements(seed, seedPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return publicKeyRet; -} - -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(pkSignJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage) -{ - const char* errorMessage = NULL; - OlmPkSigning *signingPtr = getPkSigningInstanceId(env, thiz); - - jbyteArray signatureRet = 0; - jbyte *messagePtr = NULL; - jboolean messageWasCopied = JNI_FALSE; - - if (!signingPtr) - { - errorMessage = "invalid Siging ptr=NULL"; - LOGE(" ## setPkSignJni(): failure - %s", errorMessage); - } - else if (!aMessage) - { - errorMessage = "message seed"; - LOGE(" ## setPkSignJni: failure - %s", errorMessage); - } - else if (!(messagePtr = env->GetByteArrayElements(aMessage, &messageWasCopied))) - { - errorMessage = "message JNI allocation OOM"; - LOGE(" ## setPkSignJni(): failure - %s", errorMessage); - } - else - { - size_t signatureLength = olm_pk_signature_length(); - uint8_t *signaturePtr = NULL; - size_t messageLength = (size_t)env->GetArrayLength(aMessage); - if (!(signaturePtr = (uint8_t*)malloc(signatureLength))) - { - errorMessage = "signature JNI allocation OOM"; - LOGE(" ## setPkSignJni(): falure - %s", errorMessage); - } - else - { - size_t returnValue = olm_pk_sign( - signingPtr, - (uint8_t *)messagePtr, messageLength, - signaturePtr, signatureLength - ); - if (returnValue == olm_error()) - { - errorMessage = olm_pk_signing_last_error(signingPtr); - LOGE(" ## setPkSignJni: failure - olm_pk_sign Msg=%s", errorMessage); - } - else - { - if (!(signatureRet = env->NewByteArray(signatureLength))) { - errorMessage = "signatureRet JNI allocation OOM"; - LOGE(" ## setPkSignJni(): falure - %s", errorMessage); - } else { - env->SetByteArrayRegion( - signatureRet, 0, signatureLength, (jbyte*)signaturePtr - ); - } - } - } - } - - if (messagePtr) - { - if (messageWasCopied) - { - memset(messagePtr, 0, (size_t)env->GetArrayLength(aMessage)); - } - env->ReleaseByteArrayElements(aMessage, messagePtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return signatureRet; -} diff --git a/android/olm-sdk/src/main/jni/olm_pk.h b/android/olm-sdk/src/main/jni/olm_pk.h deleted file mode 100644 index 7a577bb..0000000 --- a/android/olm-sdk/src/main/jni/olm_pk.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2018,2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMPK_H -#define _OLMPK_H - -#include "olm_jni.h" -#include "olm/pk.h" - -#define OLM_PK_ENCRYPTION_FUNC_DEF(func_name) FUNC_DEF(OlmPkEncryption,func_name) -#define OLM_PK_DECRYPTION_FUNC_DEF(func_name) FUNC_DEF(OlmPkDecryption,func_name) -#define OLM_PK_SIGNING_FUNC_DEF(func_name) FUNC_DEF(OlmPkSigning,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -JNIEXPORT jlong OLM_PK_ENCRYPTION_FUNC_DEF(createNewPkEncryptionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(releasePkEncryptionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer); - -JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg); - -JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jint OLM_PK_DECRYPTION_FUNC_DEF(privateKeyLength)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(setPrivateKeyJni)(JNIEnv *env, jobject thiz, jbyteArray key); -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(privateKeyJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); - -JNIEXPORT jlong OLM_PK_SIGNING_FUNC_DEF(createNewPkSigningJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_PK_SIGNING_FUNC_DEF(releasePkSigningJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jint OLM_PK_SIGNING_FUNC_DEF(seedLength)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(generateSeedJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(setKeyFromSeedJni)(JNIEnv *env, jobject thiz, jbyteArray seed); -JNIEXPORT jbyteArray OLM_PK_SIGNING_FUNC_DEF(pkSignJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_sas.cpp b/android/olm-sdk/src/main/jni/olm_sas.cpp deleted file mode 100644 index 400934f..0000000 --- a/android/olm-sdk/src/main/jni/olm_sas.cpp +++ /dev/null @@ -1,390 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_sas.h" - -#include "olm/olm.h" - -using namespace AndroidOlmSdk; - -JNIEXPORT jlong OLM_SAS_FUNC_DEF(createNewSASJni)(JNIEnv *env, jobject thiz) -{ - - size_t sasSize = olm_sas_size(); - OlmSAS *sasPtr = (OlmSAS *) malloc(sasSize); - const char* errorMessage = NULL; - - if (!sasPtr) - { - LOGE("## createNewSASJni(): failure - init SAS OOM"); - env->ThrowNew(env->FindClass("java/lang/Exception"), "init sas OOM"); - } - else - { - sasPtr = olm_sas(sasPtr) - LOGD(" ## createNewSASJni(): success - sasPtr=%p (jlong)(intptr_t)accountPtr=%lld",sasPtr,(jlong)(intptr_t)sasPtr); - } - - size_t randomSize = olm_create_sas_random_length(sasPtr); - uint8_t *randomBuffPtr = NULL; - - LOGD("## createNewSASJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize)); - - if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) - { - LOGE("## createNewSASJni(): failure - random buffer init"); - errorMessage = "Failed to init private key"; - } - else - { - size_t result = olm_create_sas(sasPtr, randomBuffPtr, randomSize); - if (result == olm_error()) - { - errorMessage = (const char *)olm_sas_last_error(sasPtr); - LOGE("## createNewSASJni(): failure - error creating SAS Msg=%s", errorMessage); - } - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomSize); - free(randomBuffPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sasPtr; -} - -JNIEXPORT void OLM_SAS_FUNC_DEF(releaseSASJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releaseSASJni(): IN"); - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - if (!sasPtr) - { - LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); - } - else - { - olm_clear_sas(sasPtr); - // even if free(NULL) does not crash, logs are performed for debug purpose - free(sasPtr); - } -} - - -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(getPubKeyJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## getPubKeyJni(): IN"); - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - if (!sasPtr) - { - LOGE("## getPubKeyJni(): failure - invalid SAS ptr=NULL"); - errorMessage = "invalid SAS ptr=NULL"; - } - else - { - size_t pubKeyLength = olm_sas_pubkey_length(sasPtr); - void *pubkey = malloc(pubKeyLength*sizeof(uint8_t)); - size_t result = olm_sas_get_pubkey(sasPtr, pubkey, pubKeyLength); - if (result == olm_error()) - { - errorMessage = (const char *)olm_sas_last_error(sasPtr); - LOGE("## getPubKeyJni(): failure - error getting pub key Msg=%s", errorMessage); - } - else - { - returnValue = env->NewByteArray(pubKeyLength); - env->SetByteArrayRegion(returnValue, 0 , pubKeyLength, (jbyte*)pubkey); - } - if (pubkey) { - free(pubkey); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -JNIEXPORT void OLM_SAS_FUNC_DEF(setTheirPubKey)(JNIEnv *env, jobject thiz,jbyteArray pubKeyBuffer) { - - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - const char* errorMessage = NULL; - jbyte *pubKeyPtr = NULL; - jboolean pubKeyWasCopied = JNI_FALSE; - - if (!sasPtr) - { - LOGE("## setTheirPubKey(): failure - invalid SAS ptr=NULL"); - errorMessage = "invalid SAS ptr=NULL"; - } else if(!pubKeyBuffer) { - LOGE("## setTheirPubKey(): failure - invalid info"); - errorMessage = "invalid pubKey"; - } - else if (!(pubKeyPtr = env->GetByteArrayElements(pubKeyBuffer, &pubKeyWasCopied))) - { - LOGE(" ## setTheirPubKey(): failure - info JNI allocation OOM"); - errorMessage = "info JNI allocation OOM"; - } - else - { - size_t pubKeyLength = (size_t)env->GetArrayLength(pubKeyBuffer); - size_t result = olm_sas_set_their_key(sasPtr,pubKeyPtr,pubKeyLength); - if (result == olm_error()) - { - errorMessage = (const char *)olm_sas_last_error(sasPtr); - LOGE("## setTheirPubKey(): failure - error setting their key Msg=%s", errorMessage); - } - } - // free alloc - if (pubKeyPtr) - { - if (pubKeyWasCopied) - { - memset(pubKeyPtr, 0, (size_t)env->GetArrayLength(pubKeyBuffer)); - } - env->ReleaseByteArrayElements(pubKeyBuffer, pubKeyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - -} - -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(generateShortCodeJni)(JNIEnv *env, jobject thiz, jbyteArray infoStringBytes, jint byteNb) { - LOGD("## generateShortCodeJni(): IN"); - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - jbyte *infoPtr = NULL; - jboolean infoWasCopied = JNI_FALSE; - - if (!sasPtr) - { - LOGE("## generateShortCodeJni(): failure - invalid SAS ptr=NULL"); - errorMessage = "invalid SAS ptr=NULL"; - } else if(!infoStringBytes) { - LOGE("## generateShortCodeJni(): failure - invalid info"); - errorMessage = "invalid info"; - } - else if (!(infoPtr = env->GetByteArrayElements(infoStringBytes, &infoWasCopied))) - { - LOGE(" ## generateShortCodeJni(): failure - info JNI allocation OOM"); - errorMessage = "info JNI allocation OOM"; - } - else { - size_t shortBytesCodeLength = (size_t) byteNb; - void *shortBytesCode = malloc(shortBytesCodeLength * sizeof(uint8_t)); - size_t infoLength = (size_t)env->GetArrayLength(infoStringBytes); - olm_sas_generate_bytes(sasPtr, infoPtr, infoLength, shortBytesCode, shortBytesCodeLength); - returnValue = env->NewByteArray(shortBytesCodeLength); - env->SetByteArrayRegion(returnValue, 0 , shortBytesCodeLength, (jbyte*)shortBytesCode); - free(shortBytesCode); - } - - // free alloc - if (infoPtr) - { - if (infoWasCopied) - { - memset(infoPtr, 0, (size_t)env->GetArrayLength(infoStringBytes)); - } - env->ReleaseByteArrayElements(infoStringBytes, infoPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - - -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(calculateMacJni)(JNIEnv *env, jobject thiz,jbyteArray messageBuffer,jbyteArray infoBuffer) { - LOGD("## calculateMacJni(): IN"); - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - jbyte *messagePtr = NULL; - jboolean messageWasCopied = JNI_FALSE; - - jbyte *infoPtr = NULL; - jboolean infoWasCopied = JNI_FALSE; - - if (!sasPtr) - { - LOGE("## calculateMacJni(): failure - invalid SAS ptr=NULL"); - errorMessage = "invalid SAS ptr=NULL"; - } else if(!messageBuffer) { - LOGE("## calculateMacJni(): failure - invalid message"); - errorMessage = "invalid info"; - } - else if (!(messagePtr = env->GetByteArrayElements(messageBuffer, &messageWasCopied))) - { - LOGE(" ## calculateMacJni(): failure - message JNI allocation OOM"); - errorMessage = "message JNI allocation OOM"; - } - else if (!(infoPtr = env->GetByteArrayElements(infoBuffer, &infoWasCopied))) - { - LOGE(" ## calculateMacJni(): failure - info JNI allocation OOM"); - errorMessage = "info JNI allocation OOM"; - } else { - - size_t infoLength = (size_t)env->GetArrayLength(infoBuffer); - size_t messageLength = (size_t)env->GetArrayLength(messageBuffer); - size_t macLength = olm_sas_mac_length(sasPtr); - - void *macPtr = malloc(macLength*sizeof(uint8_t)); - - size_t result = olm_sas_calculate_mac(sasPtr,messagePtr,messageLength,infoPtr,infoLength,macPtr,macLength); - if (result == olm_error()) - { - errorMessage = (const char *)olm_sas_last_error(sasPtr); - LOGE("## calculateMacJni(): failure - error calculating SAS mac Msg=%s", errorMessage); - } - else - { - returnValue = env->NewByteArray(macLength); - env->SetByteArrayRegion(returnValue, 0 , macLength, (jbyte*)macPtr); - } - - if (macPtr) { - free(macPtr); - } - } - - // free alloc - if (infoPtr) - { - if (infoWasCopied) - { - memset(infoPtr, 0, (size_t)env->GetArrayLength(infoBuffer)); - } - env->ReleaseByteArrayElements(infoBuffer, infoPtr, JNI_ABORT); - } - if (messagePtr) - { - if (messageWasCopied) - { - memset(messagePtr, 0, (size_t)env->GetArrayLength(messageBuffer)); - } - env->ReleaseByteArrayElements(messageBuffer, messagePtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(calculateMacLongKdfJni)(JNIEnv *env, jobject thiz,jbyteArray messageBuffer,jbyteArray infoBuffer) { - LOGD("## calculateMacLongKdfJni(): IN"); - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - OlmSAS* sasPtr = getOlmSasInstanceId(env, thiz); - - jbyte *messagePtr = NULL; - jboolean messageWasCopied = JNI_FALSE; - - jbyte *infoPtr = NULL; - jboolean infoWasCopied = JNI_FALSE; - - if (!sasPtr) - { - LOGE("## calculateMacLongKdfJni(): failure - invalid SAS ptr=NULL"); - errorMessage = "invalid SAS ptr=NULL"; - } else if(!messageBuffer) { - LOGE("## calculateMacLongKdfJni(): failure - invalid message"); - errorMessage = "invalid info"; - } - else if (!(messagePtr = env->GetByteArrayElements(messageBuffer, &messageWasCopied))) - { - LOGE(" ## calculateMacLongKdfJni(): failure - message JNI allocation OOM"); - errorMessage = "message JNI allocation OOM"; - } - else if (!(infoPtr = env->GetByteArrayElements(infoBuffer, &infoWasCopied))) - { - LOGE(" ## calculateMacLongKdfJni(): failure - info JNI allocation OOM"); - errorMessage = "info JNI allocation OOM"; - } else { - - size_t infoLength = (size_t)env->GetArrayLength(infoBuffer); - size_t messageLength = (size_t)env->GetArrayLength(messageBuffer); - size_t macLength = olm_sas_mac_length(sasPtr); - - void *macPtr = malloc(macLength*sizeof(uint8_t)); - - size_t result = olm_sas_calculate_mac_long_kdf(sasPtr,messagePtr,messageLength,infoPtr,infoLength,macPtr,macLength); - if (result == olm_error()) - { - errorMessage = (const char *)olm_sas_last_error(sasPtr); - LOGE("## calculateMacLongKdfJni(): failure - error calculating SAS mac Msg=%s", errorMessage); - } - else - { - returnValue = env->NewByteArray(macLength); - env->SetByteArrayRegion(returnValue, 0 , macLength, (jbyte*)macPtr); - } - - if (macPtr) { - free(macPtr); - } - } - - // free alloc - if (infoPtr) - { - if (infoWasCopied) - { - memset(infoPtr, 0, (size_t)env->GetArrayLength(infoBuffer)); - } - env->ReleaseByteArrayElements(infoBuffer, infoPtr, JNI_ABORT); - } - if (messagePtr) - { - if (messageWasCopied) - { - memset(messagePtr, 0, (size_t)env->GetArrayLength(messageBuffer)); - } - env->ReleaseByteArrayElements(messageBuffer, messagePtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -}
\ No newline at end of file diff --git a/android/olm-sdk/src/main/jni/olm_sas.h b/android/olm-sdk/src/main/jni/olm_sas.h deleted file mode 100644 index 3340459..0000000 --- a/android/olm-sdk/src/main/jni/olm_sas.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2019 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OMLSAS_H -#define _OMLSAS_H - -#include "olm_jni.h" -#include "olm/sas.h" - -#define OLM_SAS_FUNC_DEF(func_name) FUNC_DEF(OlmSAS,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -JNIEXPORT jlong OLM_SAS_FUNC_DEF(createNewSASJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_SAS_FUNC_DEF(releaseSASJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(getPubKeyJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_SAS_FUNC_DEF(setTheirPubKey)(JNIEnv *env, jobject thiz,jbyteArray pubKey); -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(generateShortCodeJni)(JNIEnv *env, jobject thiz, jbyteArray infoStringBytes, jint byteNb); -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(calculateMacJni)(JNIEnv *env, jobject thiz, jbyteArray messageBuffer, jbyteArray infoBuffer); -JNIEXPORT jbyteArray OLM_SAS_FUNC_DEF(calculateMacLongKdfJni)(JNIEnv *env, jobject thiz, jbyteArray messageBuffer, jbyteArray infoBuffer); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_session.cpp b/android/olm-sdk/src/main/jni/olm_session.cpp deleted file mode 100644 index 15ad4fe..0000000 --- a/android/olm-sdk/src/main/jni/olm_session.cpp +++ /dev/null @@ -1,977 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_session.h" - -using namespace AndroidOlmSdk; - -/** -* Init memory allocation for a session creation.<br> -* Make sure releaseSessionJni() is called when one is done with the session instance. -* @return valid memory allocation, NULL otherwise -**/ -OlmSession* initializeSessionMemory() -{ - size_t sessionSize = olm_session_size(); - OlmSession* sessionPtr = (OlmSession*)malloc(sessionSize); - - if (sessionPtr) - { - // init session object - sessionPtr = olm_session(sessionPtr); - LOGD("## initializeSessionMemory(): success - OLM session size=%lu",static_cast<long unsigned int>(sessionSize)); - } - else - { - LOGE("## initializeSessionMemory(): failure - OOM"); - } - - return sessionPtr; -} - -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## createNewSessionJni(): IN"); - OlmSession* accountPtr = initializeSessionMemory(); - - if (!accountPtr) - { - LOGE("## initNewAccount(): failure - init session OOM"); - env->ThrowNew(env->FindClass("java/lang/Exception"), "init session OOM"); - } - else - { - LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr); - } - - return (jlong)(intptr_t)accountPtr; -} - -JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz) -{ - LOGD("## releaseSessionJni(): IN"); - OlmSession* sessionPtr = getSessionInstanceId(env, thiz); - - if (!sessionPtr) - { - LOGE("## releaseSessionJni(): failure - invalid Session ptr=NULL"); - } - else - { - olm_clear_session(sessionPtr); - - // even if free(NULL) does not crash, logs are performed for debug purpose - free(sessionPtr); - } -} - -// ********************************************************************* -// ********************** OUTBOUND SESSION ***************************** -// ********************************************************************* -/** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message.<br> The recipient is defined as the entity - * with whom the session is established. - * @param aOlmAccountId account instance - * @param aTheirIdentityKey the identity key of the recipient - * @param aTheirOneTimeKey the one time key of the recipient or an exception is thrown - **/ -JNIEXPORT void OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aTheirOneTimeKeyBuffer) -{ - OlmSession* sessionPtr = getSessionInstanceId(env, thiz); - const char* errorMessage = NULL; - OlmAccount* accountPtr = NULL; - - if (!sessionPtr) - { - LOGE("## initOutboundSessionJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) - { - LOGE("## initOutboundSessionJni(): failure - invalid Account ptr=NULL"); - errorMessage = "invalid Account ptr=NULL"; - } - else if (!aTheirIdentityKeyBuffer || !aTheirOneTimeKeyBuffer) - { - LOGE("## initOutboundSessionJni(): failure - invalid keys"); - errorMessage = "invalid keys"; - } - else - { - size_t randomSize = olm_create_outbound_session_random_length(sessionPtr); - uint8_t *randomBuffPtr = NULL; - - LOGD("## initOutboundSessionJni(): randomSize=%lu",static_cast<long unsigned int>(randomSize)); - - if ( (0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize)) - { - LOGE("## initOutboundSessionJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - jbyte* theirIdentityKeyPtr = NULL; - jbyte* theirOneTimeKeyPtr = NULL; - - // convert identity & one time keys to C strings - if (!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0))) - { - LOGE("## initOutboundSessionJni(): failure - identityKey JNI allocation OOM"); - errorMessage = "identityKey JNI allocation OOM"; - } - else if (!(theirOneTimeKeyPtr = env->GetByteArrayElements(aTheirOneTimeKeyBuffer, 0))) - { - LOGE("## initOutboundSessionJni(): failure - one time Key JNI allocation OOM"); - errorMessage = "one time Key JNI allocation OOM"; - } - else - { - size_t theirIdentityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer); - size_t theirOneTimeKeyLength = (size_t)env->GetArrayLength(aTheirOneTimeKeyBuffer); - LOGD("## initOutboundSessionJni(): identityKey=%.*s oneTimeKey=%.*s", static_cast<int>(theirIdentityKeyLength), theirIdentityKeyPtr, static_cast<int>(theirOneTimeKeyLength), theirOneTimeKeyPtr); - - size_t sessionResult = olm_create_outbound_session(sessionPtr, - accountPtr, - theirIdentityKeyPtr, - theirIdentityKeyLength, - theirOneTimeKeyPtr, - theirOneTimeKeyLength, - (void*)randomBuffPtr, - randomSize); - if (sessionResult == olm_error()) { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## initOutboundSessionJni(): failure - session creation Msg=%s", errorMessage); - } - else - { - LOGD("## initOutboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); - } - } - - if (theirIdentityKeyPtr) - { - env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT); - } - - if (theirOneTimeKeyPtr) - { - env->ReleaseByteArrayElements(aTheirOneTimeKeyBuffer, theirOneTimeKeyPtr, JNI_ABORT); - } - - if (randomBuffPtr) - { - memset(randomBuffPtr, 0, randomSize); - free(randomBuffPtr); - } - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - - -// ********************************************************************* -// *********************** INBOUND SESSION ***************************** -// ********************************************************************* -/** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message.<br> - * An exception is thrown if the operation fails. - * @param aOlmAccountId account instance - * @param aOneTimeKeyMsg PRE_KEY message - */ -JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsgBuffer) -{ - const char* errorMessage = NULL; - OlmSession *sessionPtr = getSessionInstanceId(env,thiz); - OlmAccount *accountPtr = NULL; - size_t sessionResult; - - if (!sessionPtr) - { - LOGE("## initInboundSessionJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) - { - LOGE("## initInboundSessionJni(): failure - invalid Account ptr=NULL"); - errorMessage = "invalid Account ptr=NULL"; - } - else if (!aOneTimeKeyMsgBuffer) - { - LOGE("## initInboundSessionJni(): failure - invalid message"); - errorMessage = "invalid message"; - } - else - { - jbyte* messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0); - - if (!messagePtr) - { - LOGE("## initInboundSessionJni(): failure - message JNI allocation OOM"); - errorMessage = "message JNI allocation OOM"; - } - else - { - size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer); - LOGD("## initInboundSessionJni(): messageLength=%lu message=%.*s", static_cast<long unsigned int>(messageLength), static_cast<int>(messageLength), messagePtr); - - sessionResult = olm_create_inbound_session(sessionPtr, accountPtr, (void*)messagePtr , messageLength); - - if (sessionResult == olm_error()) - { - errorMessage = olm_session_last_error(sessionPtr); - LOGE("## initInboundSessionJni(): failure - init inbound session creation Msg=%s", errorMessage); - } - else - { - LOGD("## initInboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); - } - - // free local alloc - env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -/** - * Create a new in-bound session for sending/receiving messages from an - * incoming PRE_KEY message based on the recipient identity key.<br> - * An exception is thrown if the operation fails. - * @param aOlmAccountId account instance - * @param aTheirIdentityKey the identity key of the recipient - * @param aOneTimeKeyMsg encrypted message - */ -JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aOneTimeKeyMsgBuffer) -{ - const char* errorMessage = NULL; - - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - OlmAccount *accountPtr = NULL; - jbyte *messagePtr = NULL; - jbyte *theirIdentityKeyPtr = NULL; - size_t sessionResult; - - if (!sessionPtr) - { - LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else if (!(accountPtr = (OlmAccount*)aOlmAccountId)) - { - LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid Account ptr=NULL"); - errorMessage = "invalid Account ptr=NULL"; - } - else if (!aTheirIdentityKeyBuffer) - { - LOGE("## initInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); - errorMessage = "invalid theirIdentityKey"; - } - else if (!aOneTimeKeyMsgBuffer) - { - LOGE("## initInboundSessionJni(): failure - invalid one time key message"); - errorMessage = "invalid invalid one time key message"; - } - else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0))) - { - LOGE("## initInboundSessionFromIdKeyJni(): failure - message JNI allocation OOM"); - errorMessage = "message JNI allocation OOM"; - } - else if(!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0))) - { - LOGE("## initInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); - errorMessage = "theirIdentityKey JNI allocation OOM"; - } - else - { - size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer); - size_t theirIdentityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer); - - LOGD("## initInboundSessionFromIdKeyJni(): message=%.*s messageLength=%lu", static_cast<int>(messageLength), messagePtr, static_cast<long unsigned int>(messageLength)); - - sessionResult = olm_create_inbound_session_from(sessionPtr, accountPtr, theirIdentityKeyPtr, theirIdentityKeyLength, (void*)messagePtr , messageLength); - if (sessionResult == olm_error()) - { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s", errorMessage); - } - else - { - LOGD("## initInboundSessionFromIdKeyJni(): success - result=%lu", static_cast<long unsigned int>(sessionResult)); - } - } - - // free local alloc - if (messagePtr) - { - env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT); - } - - if (theirIdentityKeyPtr) - { - env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } -} - -/** - * Checks if the PRE_KEY message is for this in-bound session.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * @param aOneTimeKeyMsg PRE KEY message - * @return true if the PRE_KEY message matches - */ -JNIEXPORT jboolean OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aOneTimeKeyMsgBuffer) -{ - jboolean retCode = JNI_FALSE; - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - jbyte *messagePtr = NULL; - - if (!sessionPtr) - { - LOGE("## matchesInboundSessionJni(): failure - invalid Session ptr=NULL"); - } - else if (!aOneTimeKeyMsgBuffer) - { - LOGE("## matchesInboundSessionJni(): failure - invalid one time key message"); - } - else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0))) - { - LOGE("## matchesInboundSessionJni(): failure - one time key JNI allocation OOM"); - } - else - { - size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer); - - size_t matchResult = olm_matches_inbound_session(sessionPtr, (void*)messagePtr , messageLength); - //if(matchResult == olm_error()) { - // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 - if (matchResult != 1) { - LOGE("## matchesInboundSessionJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); - } - else - { - retCode = JNI_TRUE; - LOGD("## matchesInboundSessionJni(): success - result=%lu", static_cast<long unsigned int>(matchResult)); - } - } - - // free local alloc - if (messagePtr) - { - env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT); - } - - return retCode; -} - -/** - * Checks if the PRE_KEY message is for this in-bound session based on the sender identity key.<br> - * This API may be used to process a "m.room.encrypted" event when type = 1 (PRE_KEY). - * @param aTheirIdentityKey the identity key of the sender - * @param aOneTimeKeyMsg PRE KEY message - * @return true if the PRE_KEY message matches. - */ -JNIEXPORT jboolean JNICALL OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKeyBuffer, jbyteArray aOneTimeKeyMsgBuffer) -{ - jboolean retCode = JNI_FALSE; - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - jbyte *messagePtr = NULL; - jbyte *theirIdentityKeyPtr = NULL; - - if (!sessionPtr) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid Session ptr=NULL"); - } - else if (!aTheirIdentityKeyBuffer) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid theirIdentityKey"); - } - else if (!(theirIdentityKeyPtr = env->GetByteArrayElements(aTheirIdentityKeyBuffer, 0))) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - theirIdentityKey JNI allocation OOM"); - } - else if (!aOneTimeKeyMsgBuffer) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - invalid one time key message"); - } - else if (!(messagePtr = env->GetByteArrayElements(aOneTimeKeyMsgBuffer, 0))) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - one time key JNI allocation OOM"); - } - else - { - size_t identityKeyLength = (size_t)env->GetArrayLength(aTheirIdentityKeyBuffer); - size_t messageLength = (size_t)env->GetArrayLength(aOneTimeKeyMsgBuffer); - size_t matchResult = olm_matches_inbound_session_from(sessionPtr, (void const *)theirIdentityKeyPtr, identityKeyLength, (void*)messagePtr , messageLength); - - //if(matchResult == olm_error()) { - // for now olm_matches_inbound_session() returns 1 when it succeeds, otherwise 1- or 0 - if (matchResult != 1) - { - LOGE("## matchesInboundSessionFromIdKeyJni(): failure - no match Msg=%s",(const char *)olm_session_last_error(sessionPtr)); - } - else - { - retCode = JNI_TRUE; - LOGD("## matchesInboundSessionFromIdKeyJni(): success - result=%lu", static_cast<long unsigned int>(matchResult)); - } - } - - // free local alloc - if (theirIdentityKeyPtr) - { - env->ReleaseByteArrayElements(aTheirIdentityKeyBuffer, theirIdentityKeyPtr, JNI_ABORT); - } - - if (messagePtr) - { - env->ReleaseByteArrayElements(aOneTimeKeyMsgBuffer, messagePtr, JNI_ABORT); - } - - return retCode; -} - -/** - * Encrypt a message using the session.<br> - * An exception is thrown if the operation fails. - * @param aClearMsg clear text message - * @param [out] aEncryptedMsg ciphered message - * @return the encrypted message - */ -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsgBuffer, jobject aEncryptedMsg) -{ - jbyteArray encryptedMsgRet = 0; - const char* errorMessage = NULL; - - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - jbyte *clearMsgPtr = NULL; - jboolean clearMsgIsCopied = JNI_FALSE; - jclass encryptedMsgJClass = 0; - jfieldID typeMsgFieldId; - - LOGD("## encryptMessageJni(): IN "); - - if (!sessionPtr) - { - LOGE("## encryptMessageJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else if (!aClearMsgBuffer) - { - LOGE("## encryptMessageJni(): failure - invalid clear message"); - errorMessage = "invalid clear message"; - } - else if (!aEncryptedMsg) - { - LOGE("## encryptMessageJni(): failure - invalid encrypted message"); - errorMessage = "invalid encrypted message"; - } - else if (!(clearMsgPtr = env->GetByteArrayElements(aClearMsgBuffer, &clearMsgIsCopied))) - { - LOGE("## encryptMessageJni(): failure - clear message JNI allocation OOM"); - errorMessage = "clear message JNI allocation OOM"; - } - else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) - { - LOGE("## encryptMessageJni(): failure - unable to get crypted message class"); - errorMessage = "unable to get crypted message class"; - } - else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) - { - LOGE("## encryptMessageJni(): failure - unable to get message type field"); - errorMessage = "unable to get message type field"; - } - else - { - // get message type - size_t messageType = olm_encrypt_message_type(sessionPtr); - uint8_t *randomBuffPtr = NULL; - - // compute random buffer - // Note: olm_encrypt_random_length() can return 0, which means - // it just does not need new random data to encrypt a new message - size_t randomLength = olm_encrypt_random_length(sessionPtr); - - LOGD("## encryptMessageJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength)); - - if ((0 != randomLength) && !setRandomInBuffer(env, &randomBuffPtr, randomLength)) - { - LOGE("## encryptMessageJni(): failure - random buffer init"); - errorMessage = "random buffer init"; - } - else - { - // alloc buffer for encrypted message - size_t clearMsgLength = (size_t)env->GetArrayLength(aClearMsgBuffer); - size_t encryptedMsgLength = olm_encrypt_message_length(sessionPtr, clearMsgLength); - - void *encryptedMsgPtr = malloc(encryptedMsgLength*sizeof(uint8_t)); - - if (!encryptedMsgPtr) - { - LOGE("## encryptMessageJni(): failure - encryptedMsgPtr buffer OOM"); - errorMessage = "encryptedMsgPtr buffer OOM"; - } - else - { - if (0 == randomLength) - { - LOGW("## encryptMessageJni(): random buffer is not required"); - } - - LOGD("## encryptMessageJni(): messageType=%lu randomLength=%lu clearMsgLength=%lu encryptedMsgLength=%lu",static_cast<long unsigned int>(messageType),static_cast<long unsigned int>(randomLength), static_cast<long unsigned int>(clearMsgLength), static_cast<long unsigned int>(encryptedMsgLength)); - // encrypt message - size_t result = olm_encrypt(sessionPtr, - (void const *)clearMsgPtr, - clearMsgLength, - randomBuffPtr, - randomLength, - encryptedMsgPtr, - encryptedMsgLength); - if (result == olm_error()) - { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## encryptMessageJni(): failure - Msg=%s", errorMessage); - } - else - { - // update message type: PRE KEY or normal - env->SetLongField(aEncryptedMsg, typeMsgFieldId, (jlong)messageType); - - encryptedMsgRet = env->NewByteArray(encryptedMsgLength); - env->SetByteArrayRegion(encryptedMsgRet, 0 , encryptedMsgLength, (jbyte*)encryptedMsgPtr); - - LOGD("## encryptMessageJni(): success - result=%lu Type=%lu encryptedMsg=%.*s", static_cast<long unsigned int>(result), static_cast<unsigned long int>(messageType), static_cast<int>(result), (const char*)encryptedMsgPtr); - } - - free(encryptedMsgPtr); - } - - memset(randomBuffPtr, 0, randomLength); - free(randomBuffPtr); - } - } - - // free alloc - if (clearMsgPtr) - { - if (clearMsgIsCopied) - { - memset(clearMsgPtr, 0, (size_t)env->GetArrayLength(aClearMsgBuffer)); - } - env->ReleaseByteArrayElements(aClearMsgBuffer, clearMsgPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return encryptedMsgRet; -} - -/** - * Decrypt a message using the session.<br> - * An exception is thrown if the operation fails. - * @param aEncryptedMsg message to decrypt - * @return decrypted message if operation succeed - */ -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg) -{ - const char* errorMessage = NULL; - - jbyteArray decryptedMsgRet = 0; - - jclass encryptedMsgJClass = 0; - jstring encryptedMsgJstring = 0; // <= obtained from encryptedMsgFieldId - // field IDs - jfieldID encryptedMsgFieldId; - jfieldID typeMsgFieldId; - // ptrs - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - const char *encryptedMsgPtr = NULL; // <= obtained from encryptedMsgJstring - uint8_t *plainTextMsgPtr = NULL; - char *tempEncryptedPtr = NULL; - - LOGD("## decryptMessageJni(): IN - OlmSession"); - - if (!sessionPtr) - { - LOGE("## decryptMessageJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else if (!aEncryptedMsg) - { - LOGE("## decryptMessageJni(): failure - invalid encrypted message"); - errorMessage = "invalid encrypted message"; - } - else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg))) - { - LOGE("## decryptMessageJni(): failure - unable to get encrypted message class"); - errorMessage = "unable to get encrypted message class"; - } - else if (!(encryptedMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;"))) - { - LOGE("## decryptMessageJni(): failure - unable to get message field"); - errorMessage = "unable to get message field"; - } - else if (!(typeMsgFieldId = env->GetFieldID(encryptedMsgJClass,"mType","J"))) - { - LOGE("## decryptMessageJni(): failure - unable to get message type field"); - errorMessage = "unable to get message type field"; - } - else if (!(encryptedMsgJstring = (jstring)env->GetObjectField(aEncryptedMsg, encryptedMsgFieldId))) - { - LOGE("## decryptMessageJni(): failure - JNI encrypted object "); - errorMessage = "JNI encrypted object"; - } - else if (!(encryptedMsgPtr = env->GetStringUTFChars(encryptedMsgJstring, 0))) - { - LOGE("## decryptMessageJni(): failure - encrypted message JNI allocation OOM"); - errorMessage = "encrypted message JNI allocation OOM"; - } - else - { - // get message type - size_t encryptedMsgType = (size_t)env->GetLongField(aEncryptedMsg, typeMsgFieldId); - // get encrypted message length - size_t encryptedMsgLength = (size_t)env->GetStringUTFLength(encryptedMsgJstring); - - // create a dedicated temp buffer to be used in next Olm API calls - tempEncryptedPtr = static_cast<char*>(malloc(encryptedMsgLength*sizeof(uint8_t))); - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - LOGD("## decryptMessageJni(): MsgType=%lu encryptedMsgLength=%lu encryptedMsg=%.*s",static_cast<long unsigned int>(encryptedMsgType),static_cast<long unsigned int>(encryptedMsgLength), static_cast<int>(encryptedMsgLength), encryptedMsgPtr); - - // get max plaintext length - size_t maxPlainTextLength = olm_decrypt_max_plaintext_length(sessionPtr, - static_cast<size_t>(encryptedMsgType), - static_cast<void*>(tempEncryptedPtr), - encryptedMsgLength); - // Note: tempEncryptedPtr is destroyed by olm_decrypt_max_plaintext_length() - - if (maxPlainTextLength == olm_error()) - { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt_max_plaintext_length Msg=%s", errorMessage); - } - else - { - LOGD("## decryptMessageJni(): maxPlaintextLength=%lu",static_cast<long unsigned int>(maxPlainTextLength)); - - // allocate output decrypted message - plainTextMsgPtr = static_cast<uint8_t*>(malloc(maxPlainTextLength*sizeof(uint8_t))); - - // decrypt, but before reload encrypted buffer (previous one was destroyed) - memcpy(tempEncryptedPtr, encryptedMsgPtr, encryptedMsgLength); - size_t plaintextLength = olm_decrypt(sessionPtr, - encryptedMsgType, - (void*)tempEncryptedPtr, - encryptedMsgLength, - plainTextMsgPtr, - maxPlainTextLength); - if (plaintextLength == olm_error()) - { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## decryptMessageJni(): failure - olm_decrypt Msg=%s", errorMessage); - } - else - { - decryptedMsgRet = env->NewByteArray(plaintextLength); - env->SetByteArrayRegion(decryptedMsgRet, 0 , plaintextLength, (jbyte*)plainTextMsgPtr); - - LOGD(" ## decryptMessageJni(): UTF-8 Conversion - decrypted returnedLg=%lu OK",static_cast<long unsigned int>(plaintextLength)); - } - - memset(plainTextMsgPtr, 0, maxPlainTextLength); - } - } - - // free alloc - if (encryptedMsgPtr) - { - env->ReleaseStringUTFChars(encryptedMsgJstring, encryptedMsgPtr); - } - - if (tempEncryptedPtr) - { - free(tempEncryptedPtr); - } - - if (plainTextMsgPtr) - { - free(plainTextMsgPtr); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return decryptedMsgRet; -} - -/** - * Get the session identifier for this session. - * An exception is thrown if the operation fails. - * @return the session identifier - */ -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz) -{ - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - - LOGD("## getSessionIdentifierJni(): IN "); - - OlmSession *sessionPtr = getSessionInstanceId(env, thiz); - - if (!sessionPtr) - { - LOGE("## getSessionIdentifierJni(): failure - invalid Session ptr=NULL"); - errorMessage = "invalid Session ptr=NULL"; - } - else - { - // get the size to alloc to contain the id - size_t lengthSessionId = olm_session_id_length(sessionPtr); - LOGD("## getSessionIdentifierJni(): lengthSessionId=%lu",static_cast<long unsigned int>(lengthSessionId)); - - void *sessionIdPtr = malloc(lengthSessionId*sizeof(uint8_t)); - - if (!sessionIdPtr) - { - LOGE("## getSessionIdentifierJni(): failure - identifier allocation OOM"); - errorMessage = "identifier allocation OOM"; - } - else - { - size_t result = olm_session_id(sessionPtr, sessionIdPtr, lengthSessionId); - - if (result == olm_error()) - { - errorMessage = (const char *)olm_session_last_error(sessionPtr); - LOGE("## getSessionIdentifierJni(): failure - get session identifier failure Msg=%s", errorMessage); - } - else - { - LOGD("## getSessionIdentifierJni(): success - result=%lu sessionId=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), (char*)sessionIdPtr); - - returnValue = env->NewByteArray(result); - env->SetByteArrayRegion(returnValue, 0 , result, (jbyte*)sessionIdPtr); - } - - free(sessionIdPtr); - } - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Serialize and encrypt session instance.<br> - * An exception is thrown if the operation fails. - * @param aKeyBuffer key used to encrypt the serialized account data - * @return the serialised account as bytes buffer. - **/ -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - jbyteArray returnValue = 0; - - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - OlmSession* sessionPtr = getSessionInstanceId(env, thiz); - - LOGD("## serializeJni(): IN"); - - if (!sessionPtr) - { - LOGE(" ## serializeJni(): failure - invalid session ptr"); - errorMessage = "invalid session ptr"; - } - else if (!aKeyBuffer) - { - LOGE(" ## serializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "ikeyPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = olm_pickle_session_length(sessionPtr); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - - void *pickledPtr = malloc(pickledLength*sizeof(uint8_t)); - - if (!pickledPtr) - { - LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM"); - errorMessage = "pickledPtr buffer OOM"; - } - else - { - size_t result = olm_pickle_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_session_last_error(sessionPtr); - LOGE(" ## serializeJni(): failure - olm_pickle_session() Msg=%s", errorMessage); - } - else - { - LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(pickledLength), static_cast<char*>(pickledPtr)); - - returnValue = env->NewByteArray(pickledLength); - env->SetByteArrayRegion(returnValue, 0 , pickledLength, (jbyte*)pickledPtr); - } - - free(pickledPtr); - } - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (errorMessage) - { - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return returnValue; -} - -/** - * Allocate a new session and initialize it with the serialisation data.<br> - * An exception is thrown if the operation fails. - * @param aSerializedData the session serialisation buffer - * @param aKey the key used to encrypt the serialized account data - * @return the deserialized session - **/ -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer) -{ - const char* errorMessage = NULL; - OlmSession* sessionPtr = initializeSessionMemory(); - jbyte* keyPtr = NULL; - jboolean keyWasCopied = JNI_FALSE; - jbyte* pickledPtr = NULL; - - LOGD("## deserializeJni(): IN"); - - if (!sessionPtr) - { - LOGE(" ## deserializeJni(): failure - session failure OOM"); - errorMessage = "session failure OOM"; - } - else if (!aKeyBuffer) - { - LOGE(" ## deserializeJni(): failure - invalid key"); - errorMessage = "invalid key"; - } - else if (!aSerializedDataBuffer) - { - LOGE(" ## deserializeJni(): failure - serialized data"); - errorMessage = "serialized data"; - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyWasCopied))) - { - LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM"); - errorMessage = "keyPtr JNI allocation OOM"; - } - else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0))) - { - LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM"); - errorMessage = "pickledPtr JNI allocation OOM"; - } - else - { - size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength)); - LOGD(" ## deserializeJni(): pickled=%.*s",static_cast<int>(pickledLength), (char const *)pickledPtr); - - size_t result = olm_unpickle_session(sessionPtr, - (void const *)keyPtr, - keyLength, - (void*)pickledPtr, - pickledLength); - if (result == olm_error()) - { - errorMessage = olm_session_last_error(sessionPtr); - LOGE(" ## deserializeJni(): failure - olm_unpickle_account() Msg=%s", errorMessage); - } - else - { - LOGD(" ## initJni(): success - result=%lu ", static_cast<long unsigned int>(result)); - } - } - - // free alloc - if (keyPtr) - { - if (keyWasCopied) { - memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer)); - } - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (pickledPtr) - { - env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT); - } - - if (errorMessage) - { - if (sessionPtr) - { - olm_clear_session(sessionPtr); - free(sessionPtr); - } - env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage); - } - - return (jlong)(intptr_t)sessionPtr; -} diff --git a/android/olm-sdk/src/main/jni/olm_session.h b/android/olm-sdk/src/main/jni/olm_session.h deleted file mode 100644 index 534e830..0000000 --- a/android/olm-sdk/src/main/jni/olm_session.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMSESSION_H -#define _OLMSESSION_H - -#include "olm_jni.h" -#include "olm/olm.h" - -#define OLM_SESSION_FUNC_DEF(func_name) FUNC_DEF(OlmSession,func_name) - -#ifdef __cplusplus -extern "C" { -#endif - -// session creation/destruction -JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz); - -// outbound session -JNIEXPORT void OLM_SESSION_FUNC_DEF(initOutboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aTheirOneTimeKey); - -// inbound sessions: establishment based on PRE KEY message -JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aOneTimeKeyMsg); -JNIEXPORT void OLM_SESSION_FUNC_DEF(initInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jlong aOlmAccountId, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg); - -// match inbound sessions: based on PRE KEY message -JNIEXPORT jboolean OLM_SESSION_FUNC_DEF(matchesInboundSessionJni)(JNIEnv *env, jobject thiz, jbyteArray aOneTimeKeyMsg); -JNIEXPORT jboolean OLM_SESSION_FUNC_DEF(matchesInboundSessionFromIdKeyJni)(JNIEnv *env, jobject thiz, jbyteArray aTheirIdentityKey, jbyteArray aOneTimeKeyMsg); - -// encrypt/decrypt -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(encryptMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aClearMsg, jobject aEncryptedMsg); -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *env, jobject thiz, jobject aEncryptedMsg); - -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(getSessionIdentifierJni)(JNIEnv *env, jobject thiz); - -// serialization -JNIEXPORT jbyteArray OLM_SESSION_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKey); -JNIEXPORT jlong OLM_SESSION_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedData, jbyteArray aKey); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/android/olm-sdk/src/main/jni/olm_utility.cpp b/android/olm-sdk/src/main/jni/olm_utility.cpp deleted file mode 100644 index da27eda..0000000 --- a/android/olm-sdk/src/main/jni/olm_utility.cpp +++ /dev/null @@ -1,236 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "olm_utility.h" - -using namespace AndroidOlmSdk; - -OlmUtility* initializeUtilityMemory() -{ - size_t utilitySize = olm_utility_size(); - OlmUtility* utilityPtr = (OlmUtility*)malloc(utilitySize); - - if (utilityPtr) - { - utilityPtr = olm_utility(utilityPtr); - LOGD("## initializeUtilityMemory(): success - OLM utility size=%lu",static_cast<long unsigned int>(utilitySize)); - } - else - { - LOGE("## initializeUtilityMemory(): failure - OOM"); - } - - return utilityPtr; -} - -JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz) -{ - OlmUtility* utilityPtr = initializeUtilityMemory(); - - LOGD("## createUtilityJni(): IN"); - - // init account memory allocation - if (!utilityPtr) - { - LOGE(" ## createUtilityJni(): failure - init OOM"); - env->ThrowNew(env->FindClass("java/lang/Exception"), "init OOM"); - } - else - { - LOGD(" ## createUtilityJni(): success"); - } - - return (jlong)(intptr_t)utilityPtr; -} - - -JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz) -{ - OlmUtility* utilityPtr = getUtilityInstanceId(env, thiz); - - LOGD("## releaseUtilityJni(): IN"); - - if (!utilityPtr) - { - LOGE("## releaseUtilityJni(): failure - utility ptr=NULL"); - } - else - { - olm_clear_utility(utilityPtr); - free(utilityPtr); - } -} - - -/** - * Verify an ed25519 signature. - * @param aSignature the base64-encoded message signature to be checked. - * @param aKey the ed25519 key (fingerprint key) - * @param aMessage the message which was signed - * @return 0 if validation succeed, an error message string if operation failed - */ -JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, jobject thiz, jbyteArray aSignatureBuffer, jbyteArray aKeyBuffer, jbyteArray aMessageBuffer) -{ - jstring errorMessageRetValue = 0; - OlmUtility* utilityPtr = getUtilityInstanceId(env, thiz); - jbyte* signaturePtr = NULL; - jbyte* keyPtr = NULL; - jbyte* messagePtr = NULL; - jboolean messageWasCopied = JNI_FALSE; - - LOGD("## verifyEd25519SignatureJni(): IN"); - - if (!utilityPtr) - { - LOGE(" ## verifyEd25519SignatureJni(): failure - invalid utility ptr=NULL"); - } - else if (!aSignatureBuffer || !aKeyBuffer || !aMessageBuffer) - { - LOGE(" ## verifyEd25519SignatureJni(): failure - invalid input parameters "); - } - else if (!(signaturePtr = env->GetByteArrayElements(aSignatureBuffer, 0))) - { - LOGE(" ## verifyEd25519SignatureJni(): failure - signature JNI allocation OOM"); - } - else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0))) - { - LOGE(" ## verifyEd25519SignatureJni(): failure - key JNI allocation OOM"); - } - else if (!(messagePtr = env->GetByteArrayElements(aMessageBuffer, &messageWasCopied))) - { - LOGE(" ## verifyEd25519SignatureJni(): failure - message JNI allocation OOM"); - } - else - { - size_t signatureLength = (size_t)env->GetArrayLength(aSignatureBuffer); - size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer); - size_t messageLength = (size_t)env->GetArrayLength(aMessageBuffer); - LOGD(" ## verifyEd25519SignatureJni(): signatureLength=%lu keyLength=%lu messageLength=%lu",static_cast<long unsigned int>(signatureLength),static_cast<long unsigned int>(keyLength),static_cast<long unsigned int>(messageLength)); - LOGD(" ## verifyEd25519SignatureJni(): key=%.*s", static_cast<int>(keyLength), keyPtr); - - size_t result = olm_ed25519_verify(utilityPtr, - (void const *)keyPtr, - keyLength, - (void const *)messagePtr, - messageLength, - (void*)signaturePtr, - signatureLength); - if (result == olm_error()) { - const char *errorMsgPtr = olm_utility_last_error(utilityPtr); - errorMessageRetValue = env->NewStringUTF(errorMsgPtr); - LOGE("## verifyEd25519SignatureJni(): failure - olm_ed25519_verify Msg=%s",errorMsgPtr); - } - else - { - LOGD("## verifyEd25519SignatureJni(): success - result=%lu", static_cast<long unsigned int>(result)); - } - } - - // free alloc - if (signaturePtr) - { - env->ReleaseByteArrayElements(aSignatureBuffer, signaturePtr, JNI_ABORT); - } - - if (keyPtr) - { - env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT); - } - - if (messagePtr) - { - if (messageWasCopied) { - memset(messagePtr, 0, (size_t)env->GetArrayLength(aMessageBuffer)); - } - env->ReleaseByteArrayElements(aMessageBuffer, messagePtr, JNI_ABORT); - } - - return errorMessageRetValue; -} - -/** - * Compute the digest (SHA 256) for the message passed in parameter.<br> - * The digest value is the function return value. - * An exception is thrown if the operation fails. - * @param aMessage the message - * @return digest of the message. - **/ -JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHashBuffer) -{ - jbyteArray sha256Ret = 0; - - OlmUtility* utilityPtr = getUtilityInstanceId(env, thiz); - jbyte* messagePtr = NULL; - jboolean messageWasCopied = JNI_FALSE; - - LOGD("## sha256Jni(): IN"); - - if (!utilityPtr) - { - LOGE(" ## sha256Jni(): failure - invalid utility ptr=NULL"); - } - else if(!aMessageToHashBuffer) - { - LOGE(" ## sha256Jni(): failure - invalid message parameters "); - } - else if(!(messagePtr = env->GetByteArrayElements(aMessageToHashBuffer, &messageWasCopied))) - { - LOGE(" ## sha256Jni(): failure - message JNI allocation OOM"); - } - else - { - // get lengths - size_t messageLength = (size_t)env->GetArrayLength(aMessageToHashBuffer); - size_t hashLength = olm_sha256_length(utilityPtr); - void* hashValuePtr = malloc((hashLength)*sizeof(uint8_t)); - - if (!hashValuePtr) - { - LOGE("## sha256Jni(): failure - hash value allocation OOM"); - } - else - { - size_t result = olm_sha256(utilityPtr, - (void const *)messagePtr, - messageLength, - (void *)hashValuePtr, - hashLength); - if (result == olm_error()) - { - LOGE("## sha256Jni(): failure - hash creation Msg=%s",(const char *)olm_utility_last_error(utilityPtr)); - } - else - { - LOGD("## sha256Jni(): success - result=%lu hashValue=%.*s",static_cast<long unsigned int>(result), static_cast<int>(result), (char*)hashValuePtr); - sha256Ret = env->NewByteArray(result); - env->SetByteArrayRegion(sha256Ret, 0 , result, (jbyte*)hashValuePtr); - } - - free(hashValuePtr); - } - } - - if (messagePtr) - { - if (messageWasCopied) { - memset(messagePtr, 0, (size_t)env->GetArrayLength(aMessageToHashBuffer)); - } - env->ReleaseByteArrayElements(aMessageToHashBuffer, messagePtr, JNI_ABORT); - } - - return sha256Ret; -} diff --git a/android/olm-sdk/src/main/jni/olm_utility.h b/android/olm-sdk/src/main/jni/olm_utility.h deleted file mode 100644 index 07b5094..0000000 --- a/android/olm-sdk/src/main/jni/olm_utility.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2016 OpenMarket Ltd - * Copyright 2016 Vector Creations Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef _OLMUTILITY_H -#define _OLMUTILITY_H - -#include "olm_jni.h" -#include "olm/olm.h" - -#define OLM_UTILITY_FUNC_DEF(func_name) FUNC_DEF(OlmUtility,func_name) - - -#ifdef __cplusplus -extern "C" { -#endif -JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz); -JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(JNIEnv *env, jobject thiz); -JNIEXPORT jstring OLM_UTILITY_FUNC_DEF(verifyEd25519SignatureJni)(JNIEnv *env, jobject thiz, jbyteArray aSignature, jbyteArray aKey, jbyteArray aMessage); -JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHash); -#ifdef __cplusplus -} -#endif - - - -#endif diff --git a/android/olm-sdk/src/main/res/values/strings.xml b/android/olm-sdk/src/main/res/values/strings.xml deleted file mode 100644 index 93bea1d..0000000 --- a/android/olm-sdk/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ -<resources> - <string name="app_name">OlmSdk</string> -</resources> |