From 3c02c1547c09f5934c2d5620790545701d2034eb Mon Sep 17 00:00:00 2001 From: Yannick LE COLLEN Date: Wed, 18 Jan 2017 15:33:14 +0100 Subject: Android: Add wrappers for export/import of inbound group sessions --- .../src/main/java/org/matrix/olm/OlmException.java | 3 + .../org/matrix/olm/OlmInboundGroupSession.java | 111 ++++++++++++++++++++- 2 files changed, 110 insertions(+), 4 deletions(-) (limited to 'android/olm-sdk/src/main/java') 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 index b0b1a6a..33ac49e 100644 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java +++ b/android/olm-sdk/src/main/java/org/matrix/olm/OlmException.java @@ -40,6 +40,9 @@ public class OlmException extends IOException { 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; 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 index 571bddb..f5173c3 100644 --- a/android/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java +++ b/android/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java @@ -59,12 +59,24 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * @throws OlmException constructor failure */ public OlmInboundGroupSession(String aSessionKey) throws OlmException { + this(aSessionKey, false); + } + + /** + * Constructor.
+ * 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 { try { - mNativeId = createNewSessionJni(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()); } @@ -76,9 +88,20 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * 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); + 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.
@@ -95,7 +118,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * Destroy the corresponding OLM inbound group session native object.
* 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[])}. + * See {@link #createNewSessionJni(byte[], boolean)}. */ private native void releaseSessionJni(); @@ -128,6 +151,86 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri */ 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"); + } + } 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.
* In case of error, null is returned and an error message description is provided in aErrorMsg. @@ -156,7 +259,7 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri * Decrypt a message. * An exception is thrown if the operation fails. * @param aEncryptedMsg the encrypted message - * @param aDecryptMessageResult the decryptMessage informaton + * @param aDecryptMessageResult the decryptMessage information * @return the decrypted message */ private native byte[] decryptMessageJni(byte[] aEncryptedMsg, DecryptMessageResult aDecryptMessageResult); -- cgit v1.2.3