use the same way to name the creation method i.e. createNewXX. Avoid the initWithXX.
This commit is contained in:
parent
13d3f4a1c7
commit
7bf7a7e415
14 changed files with 131 additions and 195 deletions
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.matrix.olm;
|
package org.matrix.olm;
|
||||||
|
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
@ -61,7 +62,7 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
|
||||||
private transient long mNativeId;
|
private transient long mNativeId;
|
||||||
|
|
||||||
public OlmAccount() throws OlmException {
|
public OlmAccount() throws OlmException {
|
||||||
initNewAccount();
|
createNewAccount();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -85,43 +86,23 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
|
||||||
* Destroy the corresponding OLM account native object.<br>
|
* Destroy the corresponding OLM account native object.<br>
|
||||||
* This method must ALWAYS be called when this JAVA instance
|
* This method must ALWAYS be called when this JAVA instance
|
||||||
* is destroyed (ie. garbage collected) to prevent memory leak in native side.
|
* is destroyed (ie. garbage collected) to prevent memory leak in native side.
|
||||||
* See {@link #initNewAccountJni()}.
|
* See {@link #createNewAccountJni()}.
|
||||||
*/
|
*/
|
||||||
private native void releaseAccountJni();
|
private native void releaseAccountJni();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create and initialize a native account instance.<br>
|
* Create and initialize a native account instance.<br>
|
||||||
* Wrapper for {@link #initNewAccountJni()}.
|
|
||||||
* To be called before any other API call.
|
* To be called before any other API call.
|
||||||
* @exception OlmException the failure reason
|
* @exception OlmException the failure reason
|
||||||
*/
|
*/
|
||||||
private void initNewAccount() throws OlmException {
|
private void createNewAccount() throws OlmException {
|
||||||
try {
|
try {
|
||||||
mNativeId = initNewAccountJni();
|
mNativeId = createNewAccountJni();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage());
|
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION, e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create and initialize an OLM account in native side.<br>
|
|
||||||
* Do not forget to call {@link #releaseAccount()} when JAVA side is done.
|
|
||||||
* @return native account instance identifier (see {@link #mNativeId})
|
|
||||||
*/
|
|
||||||
private native long initNewAccountJni();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a native account instance without any initialization.<br>
|
|
||||||
* Since the account is left uninitialized, this
|
|
||||||
* method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).<br>
|
|
||||||
* Public wrapper for {@link #createNewAccountJni()}.
|
|
||||||
* @return true if init succeed, false otherwise.
|
|
||||||
*/
|
|
||||||
private boolean createNewAccount() {
|
|
||||||
mNativeId = initNewAccountJni();
|
|
||||||
return (0 != mNativeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an OLM account in native side.<br>
|
* Create an OLM account in native side.<br>
|
||||||
* Do not forget to call {@link #releaseAccount()} when JAVA side is done.
|
* Do not forget to call {@link #releaseAccount()} when JAVA side is done.
|
||||||
|
@ -408,10 +389,9 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
|
||||||
/**
|
/**
|
||||||
* Kick off the deserialization mechanism.
|
* Kick off the deserialization mechanism.
|
||||||
* @param aInStream input stream
|
* @param aInStream input stream
|
||||||
* @throws IOException exception
|
* @throws Exception exception
|
||||||
* @throws ClassNotFoundException exception
|
|
||||||
*/
|
*/
|
||||||
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream aInStream) throws Exception {
|
||||||
deserialize(aInStream);
|
deserialize(aInStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -453,31 +433,28 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
|
||||||
* See {@link #serialize(byte[], StringBuffer)}
|
* See {@link #serialize(byte[], StringBuffer)}
|
||||||
* @param aSerializedData bytes buffer
|
* @param aSerializedData bytes buffer
|
||||||
* @param aKey key used to encrypted
|
* @param aKey key used to encrypted
|
||||||
|
* @exception Exception the exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
|
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
|
||||||
if (!createNewAccount()) {
|
createNewAccount();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
|
String errorMsg;
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer errorMsg = new StringBuffer();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jniError;
|
|
||||||
if ((null == aSerializedData) || (null == aKey)) {
|
if ((null == aSerializedData) || (null == aKey)) {
|
||||||
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
||||||
errorMsg.append("invalid input parameters");
|
errorMsg = "invalid input parameters";
|
||||||
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
|
} else {
|
||||||
errorMsg.append(jniError);
|
errorMsg = deserializeJni(aSerializedData, aKey);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
||||||
errorMsg.append(e.getMessage());
|
errorMsg = e.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorMsg.length() > 0) {
|
if (!TextUtils.isEmpty(errorMsg)) {
|
||||||
releaseAccount();
|
releaseAccount();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
|
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,6 +55,7 @@ public class OlmException extends IOException {
|
||||||
public static final int EXCEPTION_CODE_SESSION_DECRYPT_MESSAGE = 405;
|
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_SESSION_SESSION_IDENTIFIER = 406;
|
||||||
|
|
||||||
|
public static final int EXCEPTION_CODE_UTILITY_CREATION = 501;
|
||||||
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500;
|
public static final int EXCEPTION_CODE_UTILITY_VERIFY_SIGNATURE = 500;
|
||||||
|
|
||||||
// exception human readable messages
|
// exception human readable messages
|
||||||
|
|
|
@ -61,11 +61,8 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
|
||||||
* @throws OlmException constructor failure
|
* @throws OlmException constructor failure
|
||||||
*/
|
*/
|
||||||
public OlmInboundGroupSession(String aSessionKey) throws OlmException {
|
public OlmInboundGroupSession(String aSessionKey) throws OlmException {
|
||||||
if(createNewSession()) {
|
createNewSession();
|
||||||
initInboundGroupSession(aSessionKey);
|
initInboundGroupSession(aSessionKey);
|
||||||
} else {
|
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_INBOUND_GROUP_SESSION);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,11 +85,14 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
|
||||||
/**
|
/**
|
||||||
* Create and save the session native instance ID.<br>
|
* Create and save the session native instance ID.<br>
|
||||||
* To be called before any other API call.
|
* To be called before any other API call.
|
||||||
* @return true if init succeed, false otherwise.
|
* @exception OlmException the failure reason
|
||||||
*/
|
*/
|
||||||
private boolean createNewSession() {
|
private void createNewSession() throws OlmException {
|
||||||
|
try {
|
||||||
mNativeId = createNewSessionJni();
|
mNativeId = createNewSessionJni();
|
||||||
return (0 != mNativeId);
|
} catch (Exception e) {
|
||||||
|
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_INBOUND_GROUP_SESSION, e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -190,10 +190,9 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
|
||||||
/**
|
/**
|
||||||
* Kick off the deserialization mechanism.
|
* Kick off the deserialization mechanism.
|
||||||
* @param aInStream input stream
|
* @param aInStream input stream
|
||||||
* @throws IOException exception
|
* @throws Exception exception
|
||||||
* @throws ClassNotFoundException exception
|
|
||||||
*/
|
*/
|
||||||
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream aInStream) throws Exception {
|
||||||
deserialize(aInStream);
|
deserialize(aInStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,29 +241,26 @@ public class OlmInboundGroupSession extends CommonSerializeUtils implements Seri
|
||||||
* @param aKey key used to encrypted
|
* @param aKey key used to encrypted
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
|
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
|
||||||
if (!createNewSession()) {
|
createNewSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer errorMsg = new StringBuffer();
|
String errorMsg;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jniError;
|
|
||||||
if ((null == aSerializedData) || (null == aKey)) {
|
if ((null == aSerializedData) || (null == aKey)) {
|
||||||
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
||||||
errorMsg.append("invalid input parameters");
|
errorMsg = "invalid input parameters";
|
||||||
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
|
} else {
|
||||||
errorMsg.append(jniError);
|
errorMsg = deserializeJni(aSerializedData, aKey);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
||||||
errorMsg.append(e.getMessage());
|
errorMsg = e.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorMsg.length() > 0) {
|
if (!TextUtils.isEmpty(errorMsg)) {
|
||||||
releaseSession();
|
releaseSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
|
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,8 +42,6 @@ public class OlmManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSdkOlmVersion() {
|
public String getSdkOlmVersion() {
|
||||||
//Date currentDate = Calendar.getInstance().getTime();
|
|
||||||
//String retVal = new SimpleDateFormat("yyyyMMdd_HH:mm:ss").format(currentDate);
|
|
||||||
return SDK_OLM_VERSION;
|
return SDK_OLM_VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,11 +50,8 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
|
||||||
* @throws OlmException constructor failure
|
* @throws OlmException constructor failure
|
||||||
*/
|
*/
|
||||||
public OlmOutboundGroupSession() throws OlmException {
|
public OlmOutboundGroupSession() throws OlmException {
|
||||||
if(createNewSession()) {
|
createNewSession();
|
||||||
initOutboundGroupSession();
|
initOutboundGroupSession();
|
||||||
} else {
|
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, OlmException.EXCEPTION_MSG_NEW_OUTBOUND_GROUP_SESSION);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,11 +75,14 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
|
||||||
* Create and save the session native instance ID.
|
* Create and save the session native instance ID.
|
||||||
* Wrapper for {@link #createNewSessionJni()}.<br>
|
* Wrapper for {@link #createNewSessionJni()}.<br>
|
||||||
* To be called before any other API call.
|
* To be called before any other API call.
|
||||||
* @return true if init succeed, false otherwise.
|
* @exception OlmException the exception.
|
||||||
*/
|
*/
|
||||||
private boolean createNewSession() {
|
private void createNewSession() throws OlmException {
|
||||||
|
try {
|
||||||
mNativeId = createNewSessionJni();
|
mNativeId = createNewSessionJni();
|
||||||
return (0 != mNativeId);
|
} catch (Exception e) {
|
||||||
|
throw new OlmException(OlmException.EXCEPTION_CODE_CREATE_OUTBOUND_GROUP_SESSION, e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -203,10 +203,9 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
|
||||||
/**
|
/**
|
||||||
* Kick off the deserialization mechanism.
|
* Kick off the deserialization mechanism.
|
||||||
* @param aInStream input stream
|
* @param aInStream input stream
|
||||||
* @throws IOException exception
|
* @throws Exception exception
|
||||||
* @throws ClassNotFoundException exception
|
|
||||||
*/
|
*/
|
||||||
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream aInStream) throws Exception {
|
||||||
deserialize(aInStream);
|
deserialize(aInStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,31 +246,29 @@ public class OlmOutboundGroupSession extends CommonSerializeUtils implements Ser
|
||||||
* See {@link #serialize(byte[], StringBuffer)}
|
* See {@link #serialize(byte[], StringBuffer)}
|
||||||
* @param aSerializedData pickled account in a base64 bytes buffer
|
* @param aSerializedData pickled account in a base64 bytes buffer
|
||||||
* @param aKey key used to encrypted
|
* @param aKey key used to encrypted
|
||||||
|
* @exception Exception the exception
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
|
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
|
||||||
if (!createNewSession()) {
|
createNewSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer errorMsg = new StringBuffer();
|
String errorMsg = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String jniError;
|
|
||||||
if ((null == aSerializedData) || (null == aKey)) {
|
if ((null == aSerializedData) || (null == aKey)) {
|
||||||
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
Log.e(LOG_TAG, "## deserialize(): invalid input parameters");
|
||||||
errorMsg.append("invalid input parameters");
|
errorMsg = "invalid input parameters";
|
||||||
} else if (null != (jniError = deserializeJni(aSerializedData, aKey))) {
|
} else {
|
||||||
errorMsg.append(jniError);
|
errorMsg = deserializeJni(aSerializedData, aKey);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
Log.e(LOG_TAG, "## deserialize() failed " + e.getMessage());
|
||||||
errorMsg.append(e.getMessage());
|
errorMsg = e.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorMsg.length() > 0) {
|
if (!TextUtils.isEmpty(errorMsg)) {
|
||||||
releaseSession();
|
releaseSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, String.valueOf(errorMsg));
|
throw new OlmException(OlmException.EXCEPTION_CODE_ACCOUNT_DESERIALIZATION, errorMsg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
||||||
private transient long mNativeId;
|
private transient long mNativeId;
|
||||||
|
|
||||||
public OlmSession() throws OlmException {
|
public OlmSession() throws OlmException {
|
||||||
if(!initNewSession()) {
|
createNewSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, OlmException.EXCEPTION_MSG_INIT_SESSION_CREATION);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -60,7 +58,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
||||||
* Destroy the corresponding OLM session native object.<br>
|
* Destroy the corresponding OLM session native object.<br>
|
||||||
* This method must ALWAYS be called when this JAVA instance
|
* This method must ALWAYS be called when this JAVA instance
|
||||||
* is destroyed (ie. garbage collected) to prevent memory leak in native side.
|
* is destroyed (ie. garbage collected) to prevent memory leak in native side.
|
||||||
* See {@link #initNewSessionJni()}.
|
* See {@link #createNewSessionJni()}.
|
||||||
*/
|
*/
|
||||||
private native void releaseSessionJni();
|
private native void releaseSessionJni();
|
||||||
|
|
||||||
|
@ -73,34 +71,19 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
||||||
mNativeId = 0;
|
mNativeId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Create and save the session native instance ID.
|
|
||||||
* Wrapper for {@link #initNewSessionJni()}.<br>
|
|
||||||
* To be called before any other API call.
|
|
||||||
* @return true if init succeed, false otherwise.
|
|
||||||
*/
|
|
||||||
private boolean initNewSession() {
|
|
||||||
mNativeId = initNewSessionJni();
|
|
||||||
return (0 != mNativeId);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create the corresponding OLM session in native side.<br>
|
|
||||||
* Do not forget to call {@link #releaseSession()} when JAVA side is done.
|
|
||||||
* @return native session instance identifier (see {@link #mNativeId})
|
|
||||||
*/
|
|
||||||
private native long initNewSessionJni();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a native account instance without any initialization.<br>
|
* Create a native account instance without any initialization.<br>
|
||||||
* Since the account is left uninitialized, this
|
* Since the account is left uninitialized, this
|
||||||
* method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).<br>
|
* method is intended to be used in the serialization mechanism (see {@link #readObject(ObjectInputStream)}).<br>
|
||||||
* Public wrapper for {@link #createNewSessionJni()}.
|
* Public wrapper for {@link #createNewSessionJni()}.
|
||||||
* @return true if init succeed, false otherwise.
|
* @exception OlmException the exception
|
||||||
*/
|
*/
|
||||||
private boolean createNewSession() {
|
private void createNewSession() throws OlmException {
|
||||||
mNativeId = initNewSessionJni();
|
try {
|
||||||
return (0 != mNativeId);
|
mNativeId = createNewSessionJni();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_SESSION_CREATION, e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -331,7 +314,7 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
||||||
* @throws IOException exception
|
* @throws IOException exception
|
||||||
* @throws ClassNotFoundException exception
|
* @throws ClassNotFoundException exception
|
||||||
*/
|
*/
|
||||||
private void readObject(ObjectInputStream aInStream) throws IOException, ClassNotFoundException {
|
private void readObject(ObjectInputStream aInStream) throws Exception {
|
||||||
deserialize(aInStream);
|
deserialize(aInStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,10 +357,8 @@ public class OlmSession extends CommonSerializeUtils implements Serializable {
|
||||||
* @param aKey key used to encrypted
|
* @param aKey key used to encrypted
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws IOException {
|
protected void deserialize(byte[] aSerializedData, byte[] aKey) throws Exception {
|
||||||
if (!createNewSession()) {
|
createNewSession();
|
||||||
throw new OlmException(OlmException.EXCEPTION_CODE_INIT_ACCOUNT_CREATION,OlmException.EXCEPTION_MSG_INIT_ACCOUNT_CREATION);
|
|
||||||
}
|
|
||||||
|
|
||||||
StringBuffer errorMsg = new StringBuffer();
|
StringBuffer errorMsg = new StringBuffer();
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,6 @@ public class OlmUtility {
|
||||||
private static final String LOG_TAG = "OlmUtility";
|
private static final String LOG_TAG = "OlmUtility";
|
||||||
|
|
||||||
public static final int RANDOM_KEY_SIZE = 32;
|
public static final int RANDOM_KEY_SIZE = 32;
|
||||||
public static final int RANDOM_RANGE = 256;
|
|
||||||
|
|
||||||
/** Instance Id returned by JNI.
|
/** Instance Id returned by JNI.
|
||||||
* This value uniquely identifies this utility instance.
|
* This value uniquely identifies this utility instance.
|
||||||
|
@ -46,11 +45,11 @@ public class OlmUtility {
|
||||||
* @return true if init succeed, false otherwise.
|
* @return true if init succeed, false otherwise.
|
||||||
*/
|
*/
|
||||||
private boolean initUtility() {
|
private boolean initUtility() {
|
||||||
mNativeId = initUtilityJni();
|
mNativeId = createUtilityJni();
|
||||||
return (0 != mNativeId);
|
return (0 != mNativeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private native long initUtilityJni();
|
private native long createUtilityJni();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Release native instance.<br>
|
* Release native instance.<br>
|
||||||
|
@ -136,6 +135,12 @@ public class OlmUtility {
|
||||||
SecureRandom secureRandom = new SecureRandom();
|
SecureRandom secureRandom = new SecureRandom();
|
||||||
byte[] buffer = new byte[RANDOM_KEY_SIZE];
|
byte[] buffer = new byte[RANDOM_KEY_SIZE];
|
||||||
secureRandom.nextBytes(buffer);
|
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 buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,49 +42,13 @@ OlmAccount* initializeAccountMemory()
|
||||||
return accountPtr;
|
return accountPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
|
|
||||||
{
|
|
||||||
LOGD("## createNewAccountJni(): IN");
|
|
||||||
OlmAccount* accountPtr = initializeAccountMemory();
|
|
||||||
|
|
||||||
LOGD(" ## createNewAccountJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
|
|
||||||
return (jlong)(intptr_t)accountPtr;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Release the account allocation made by initializeAccountMemory().<br>
|
* Create a new account and return it to JAVA side.<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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a new account and return it to JAVA side.<br>
|
|
||||||
* Since a C prt is returned as a jlong, special care will be taken
|
* Since a C prt is returned as a jlong, special care will be taken
|
||||||
* to make the cast (OlmAccount* => jlong) platform independent.
|
* to make the cast (OlmAccount* => jlong) platform independent.
|
||||||
* @return the initialized OlmAccount* instance
|
* @return the initialized OlmAccount* instance
|
||||||
**/
|
**/
|
||||||
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz)
|
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
|
||||||
{
|
{
|
||||||
const char* errorMessage = NULL;
|
const char* errorMessage = NULL;
|
||||||
OlmAccount *accountPtr = initializeAccountMemory();
|
OlmAccount *accountPtr = initializeAccountMemory();
|
||||||
|
@ -139,6 +103,32 @@ JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thi
|
||||||
|
|
||||||
return (jlong)(intptr_t)accountPtr;
|
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 *************************
|
// ************************* IDENTITY KEYS API *************************
|
||||||
|
|
|
@ -30,7 +30,6 @@ extern "C" {
|
||||||
|
|
||||||
// account creation/destruction
|
// account creation/destruction
|
||||||
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz);
|
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz);
|
||||||
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(initNewAccountJni)(JNIEnv *env, jobject thiz);
|
|
||||||
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz);
|
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz);
|
||||||
|
|
||||||
// identity keys
|
// identity keys
|
||||||
|
|
|
@ -58,6 +58,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
|
||||||
**/
|
**/
|
||||||
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
|
JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz)
|
||||||
{
|
{
|
||||||
|
const char* errorMessage = NULL;
|
||||||
OlmInboundGroupSession* sessionPtr = NULL;
|
OlmInboundGroupSession* sessionPtr = NULL;
|
||||||
size_t sessionSize = 0;
|
size_t sessionSize = 0;
|
||||||
|
|
||||||
|
@ -67,6 +68,7 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
|
||||||
if (!sessionSize)
|
if (!sessionSize)
|
||||||
{
|
{
|
||||||
LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
|
LOGE(" ## createNewSessionJni(): failure - inbound group session size = 0");
|
||||||
|
errorMessage = "inbound group session size = 0";
|
||||||
}
|
}
|
||||||
else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
|
else if ((sessionPtr = (OlmInboundGroupSession*)malloc(sessionSize)))
|
||||||
{
|
{
|
||||||
|
@ -76,6 +78,12 @@ JNIEXPORT jlong OLM_INBOUND_GROUP_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
|
LOGE(" ## createNewSessionJni(): failure - inbound group session OOM");
|
||||||
|
errorMessage = "inbound group session OOM";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errorMessage)
|
||||||
|
{
|
||||||
|
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (jlong)(intptr_t)sessionPtr;
|
return (jlong)(intptr_t)sessionPtr;
|
||||||
|
|
|
@ -48,7 +48,16 @@ JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject t
|
||||||
LOGD("## createNewSessionJni(): IN");
|
LOGD("## createNewSessionJni(): IN");
|
||||||
OlmSession* accountPtr = initializeSessionMemory();
|
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);
|
LOGD(" ## createNewSessionJni(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
|
||||||
|
}
|
||||||
|
|
||||||
return (jlong)(intptr_t)accountPtr;
|
return (jlong)(intptr_t)accountPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,31 +79,6 @@ JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a new 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 (OlmSession* => jlong) platform independent.
|
|
||||||
* @return the initialized OlmSession* instance if init succeed, NULL otherwise
|
|
||||||
**/
|
|
||||||
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz)
|
|
||||||
{
|
|
||||||
LOGD("## initNewSessionJni(): OlmSession IN");
|
|
||||||
|
|
||||||
OlmSession* sessionPtr = initializeSessionMemory();
|
|
||||||
|
|
||||||
// init account memory allocation
|
|
||||||
if (!sessionPtr)
|
|
||||||
{
|
|
||||||
LOGE(" ## initNewSessionJni(): failure - init session OOM");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOGD(" ## initNewSessionJni(): success - OLM session created");
|
|
||||||
}
|
|
||||||
|
|
||||||
return (jlong)(intptr_t)sessionPtr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// *********************************************************************
|
// *********************************************************************
|
||||||
// ********************** OUTBOUND SESSION *****************************
|
// ********************** OUTBOUND SESSION *****************************
|
||||||
// *********************************************************************
|
// *********************************************************************
|
||||||
|
|
|
@ -29,7 +29,6 @@ extern "C" {
|
||||||
|
|
||||||
// session creation/destruction
|
// session creation/destruction
|
||||||
JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
|
JNIEXPORT void OLM_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env, jobject thiz);
|
||||||
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(initNewSessionJni)(JNIEnv *env, jobject thiz);
|
|
||||||
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
|
JNIEXPORT jlong OLM_SESSION_FUNC_DEF(createNewSessionJni)(JNIEnv *env, jobject thiz);
|
||||||
|
|
||||||
// outbound session
|
// outbound session
|
||||||
|
|
|
@ -37,20 +37,21 @@ OlmUtility* initializeUtilityMemory()
|
||||||
return utilityPtr;
|
return utilityPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz)
|
JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz)
|
||||||
{
|
{
|
||||||
OlmUtility* utilityPtr = initializeUtilityMemory();
|
OlmUtility* utilityPtr = initializeUtilityMemory();
|
||||||
|
|
||||||
LOGD("## initUtilityJni(): IN");
|
LOGD("## createUtilityJni(): IN");
|
||||||
|
|
||||||
// init account memory allocation
|
// init account memory allocation
|
||||||
if (!utilityPtr)
|
if (!utilityPtr)
|
||||||
{
|
{
|
||||||
LOGE(" ## initUtilityJni(): failure - init OOM");
|
LOGE(" ## createUtilityJni(): failure - init OOM");
|
||||||
|
env->ThrowNew(env->FindClass("java/lang/Exception"), "init OOM");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOGD(" ## initUtilityJni(): success");
|
LOGD(" ## createUtilityJni(): success");
|
||||||
}
|
}
|
||||||
|
|
||||||
return (jlong)(intptr_t)utilityPtr;
|
return (jlong)(intptr_t)utilityPtr;
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(initUtilityJni)(JNIEnv *env, jobject thiz);
|
JNIEXPORT jlong OLM_UTILITY_FUNC_DEF(createUtilityJni)(JNIEnv *env, jobject thiz);
|
||||||
JNIEXPORT void OLM_UTILITY_FUNC_DEF(releaseUtilityJni)(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 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);
|
JNIEXPORT jbyteArray OLM_UTILITY_FUNC_DEF(sha256Jni)(JNIEnv *env, jobject thiz, jbyteArray aMessageToHash);
|
||||||
|
|
Loading…
Reference in a new issue