Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
matrix-org
Olm
Commits
f2ca1ce3
Commit
f2ca1ce3
authored
Oct 11, 2016
by
pedroGitt
Browse files
- Add OlmSession unit test
- Simplify JNI function signatures definition (Account & Session)
parent
67f79394
Changes
10
Hide whitespace changes
Inline
Side-by-side
java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmAccountTest.java
View file @
f2ca1ce3
...
...
@@ -30,9 +30,6 @@ public class OlmAccountTest {
private
static
OlmManager
mOlmManager
;
private
boolean
mIsAccountCreated
;
public
static
final
String
TEST_STRING
=
"This is a string"
;
public
static
final
long
TEST_LONG
=
12345678L
;
@BeforeClass
public
static
void
setUpClass
(){
// load native lib
...
...
@@ -61,26 +58,35 @@ public class OlmAccountTest {
}
@Test
public
void
test1CreateAccount
()
{
public
void
test01CreateReleaseAccount
()
{
mOlmAccount
=
new
OlmAccount
();
assertNotNull
(
mOlmAccount
);
mOlmAccount
.
releaseAccount
();
assertTrue
(
0
==
mOlmAccount
.
getOlmAccountId
());
}
@Test
public
void
test02CreateAccount
()
{
mOlmAccount
=
new
OlmAccount
();
assertNotNull
(
mOlmAccount
);
}
@Test
public
void
test
2
InitNewAccount
()
{
public
void
test
03
InitNewAccount
()
{
assertTrue
(
mOlmAccount
.
initNewAccount
());
mIsAccountCreated
=
true
;
}
@Test
public
void
test
3
GetOlmAccountId
()
{
public
void
test
04
GetOlmAccountId
()
{
long
olmNativeInstance
=
mOlmAccount
.
getOlmAccountId
();
Log
.
d
(
LOG_TAG
,
"## testGetOlmAccountId olmNativeInstance="
+
olmNativeInstance
);
assertTrue
(
0
!=
olmNativeInstance
);
}
@Test
public
void
test
4
IdentityKeys
()
{
public
void
test
05
IdentityKeys
()
{
JSONObject
identityKeysJson
=
mOlmAccount
.
identityKeys
();
assertNotNull
(
identityKeysJson
);
Log
.
d
(
LOG_TAG
,
"## testIdentityKeys Keys="
+
identityKeysJson
);
...
...
@@ -108,7 +114,7 @@ public class OlmAccountTest {
//** ************** One time keys TESTS **************
//****************************************************
@Test
public
void
test
5
MaxOneTimeKeys
()
{
public
void
test
06
MaxOneTimeKeys
()
{
long
maxOneTimeKeys
=
mOlmAccount
.
maxOneTimeKeys
();
Log
.
d
(
LOG_TAG
,
"## testMaxOneTimeKeys(): maxOneTimeKeys="
+
maxOneTimeKeys
);
...
...
@@ -116,13 +122,13 @@ public class OlmAccountTest {
}
@Test
public
void
test
6
GenerateOneTimeKeys
()
{
public
void
test
07
GenerateOneTimeKeys
()
{
int
retValue
=
mOlmAccount
.
generateOneTimeKeys
(
GENERATION_ONE_TIME_KEYS_NUMBER
);
assertTrue
(
0
==
retValue
);
}
@Test
public
void
test
7
OneTimeKeysJsonFormat
()
{
public
void
test
08
OneTimeKeysJsonFormat
()
{
int
oneTimeKeysCount
=
0
;
JSONObject
generatedKeysJsonObj
;
JSONObject
oneTimeKeysJson
=
mOlmAccount
.
oneTimeKeys
();
...
...
@@ -145,27 +151,31 @@ public class OlmAccountTest {
}
}
// TODO testRemoveOneTimeKeysForSession when session is available
/*@Test
public void testRemoveOneTimeKeysForSession() {
Log.d(LOG_TAG,"## testRemoveOneTimeKeysForSession");
OLMSession olmSession = new OLMSession();
JSONArray keysJsonArray = mOlmAccount.removeOneTimeKeysForSession(olmSession);
assertNotNull(keysJsonArray);
// TODO add extra test to test the JSON content format..
}*/
@Test
public
void
test10RemoveOneTimeKeysForSession
()
{
OlmSession
olmSession
=
new
OlmSession
();
olmSession
.
initNewSession
();
long
sessionId
=
olmSession
.
getOlmSessionId
();
assertTrue
(
0
!=
sessionId
);
int
sessionRetCode
=
mOlmAccount
.
removeOneTimeKeysForSession
(
sessionId
);
// no one time key has been use in the session, so removeOneTimeKeysForSession() returns an error
assertTrue
(
0
!=
sessionRetCode
);
olmSession
.
releaseSession
();
sessionId
=
olmSession
.
getOlmSessionId
();
assertTrue
(
"sessionRetCode="
+
sessionRetCode
,
0
==
sessionId
);
}
@Test
public
void
test
8
MarkOneTimeKeysAsPublished
()
{
public
void
test
11
MarkOneTimeKeysAsPublished
()
{
int
retCode
=
mOlmAccount
.
markOneTimeKeysAsPublished
();
// if OK => retCode=0
assertTrue
(
0
==
retCode
);
}
@Test
public
void
test
9
SignMessage
()
{
public
void
test
12
SignMessage
()
{
String
clearMsg
=
"String to be signed by olm"
;
String
signedMsg
=
mOlmAccount
.
signMessage
(
clearMsg
);
assertNotNull
(
signedMsg
);
...
...
@@ -193,10 +203,8 @@ public class OlmAccountTest {
int
generateRetCode
=
account
.
generateOneTimeKeys
(
50
);
Log
.
d
(
LOG_TAG
,
"## testJni(): generateRetCode="
+
generateRetCode
);
JSONObject
onteTimeKeysKeysJson
=
account
.
oneTimeKeys
();
Log
.
d
(
LOG_TAG
,
"## testJni(): onteTimeKeysKeysJson="
+
onteTimeKeysKeysJson
.
toString
());
// TODO removeOneTimeKeysForSession(session);
JSONObject
oneTimeKeysKeysJson
=
account
.
oneTimeKeys
();
Log
.
d
(
LOG_TAG
,
"## testJni(): oneTimeKeysKeysJson="
+
oneTimeKeysKeysJson
.
toString
());
int
asPublishedRetCode
=
account
.
markOneTimeKeysAsPublished
();
Log
.
d
(
LOG_TAG
,
"## testJni(): asPublishedRetCode="
+
asPublishedRetCode
);
...
...
java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmSessionTest.java
0 → 100644
View file @
f2ca1ce3
package
org.matrix.olm
;
import
android.support.test.runner.AndroidJUnit4
;
import
android.util.Log
;
import
org.json.JSONException
;
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.Iterator
;
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
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
public
void
test01AliceToBob
()
{
String
bobIdentityKey
=
null
;
String
bobOneTimeKey
=
null
;
// creates alice & bob accounts
OlmAccount
aliceAccount
=
new
OlmAccount
();
aliceAccount
.
initNewAccount
();
OlmAccount
bobAccount
=
new
OlmAccount
();
bobAccount
.
initNewAccount
();
// test accounts creation
assertTrue
(
0
!=
bobAccount
.
getOlmAccountId
());
assertTrue
(
0
!=
aliceAccount
.
getOlmAccountId
());
// get bob identity key
JSONObject
bobIdentityKeysJson
=
bobAccount
.
identityKeys
();
assertNotNull
(
bobIdentityKeysJson
);
try
{
bobIdentityKey
=
bobIdentityKeysJson
.
getString
(
OlmAccount
.
JSON_KEY_IDENTITY_KEY
);
assertTrue
(
null
!=
bobIdentityKey
);
}
catch
(
JSONException
e
)
{
assertTrue
(
"Exception MSg="
+
e
.
getMessage
(),
false
);
}
// get bob one time keys
assertTrue
(
0
==
bobAccount
.
generateOneTimeKeys
(
5
));
JSONObject
bobOneTimeKeysJsonObj
=
bobAccount
.
oneTimeKeys
();
assertNotNull
(
bobOneTimeKeysJsonObj
);
try
{
JSONObject
generatedKeys
=
bobOneTimeKeysJsonObj
.
getJSONObject
(
OlmAccount
.
JSON_KEY_ONE_TIME_KEY
);
assertNotNull
(
OlmAccount
.
JSON_KEY_ONE_TIME_KEY
+
" object is missing"
,
generatedKeys
);
// test the count of the generated one time keys:
Iterator
<
String
>
generatedKeysIt
=
generatedKeys
.
keys
();
if
(
generatedKeysIt
.
hasNext
())
{
bobOneTimeKey
=
generatedKeys
.
getString
(
generatedKeysIt
.
next
());
}
assertNotNull
(
bobOneTimeKey
);
}
catch
(
JSONException
e
)
{
assertTrue
(
"Exception MSg="
+
e
.
getMessage
(),
false
);
}
// CREATE ALICE SESSION
OlmSession
aliceSession
=
new
OlmSession
();
aliceSession
.
initNewSession
();
assertTrue
(
0
!=
aliceSession
.
getOlmSessionId
());
// CREATE ALICE OUTBOUND SESSION and encrypt message to bob
assertNotNull
(
aliceSession
.
initOutboundSessionWithAccount
(
aliceAccount
,
bobIdentityKey
,
bobOneTimeKey
));
String
clearMsg
=
"Heloo bob , this is alice!"
;
OlmMessage
encryptedMsgToBob
=
aliceSession
.
encryptMessage
(
clearMsg
);
assertNotNull
(
encryptedMsgToBob
);
Log
.
d
(
LOG_TAG
,
"## test01AliceToBob(): encryptedMsg="
+
encryptedMsgToBob
.
mCipherText
);
// CREATE BOB INBOUND SESSION and decrypt message from alice
OlmSession
bobSession
=
new
OlmSession
();
bobSession
.
initNewSession
();
assertTrue
(
0
!=
bobSession
.
getOlmSessionId
());
assertNotNull
(
bobSession
.
initInboundSessionWithAccount
(
bobAccount
,
encryptedMsgToBob
.
mCipherText
));
String
decryptedMsg
=
bobSession
.
decryptMessage
(
encryptedMsgToBob
);
assertNotNull
(
decryptedMsg
);
// MESSAGE COMPARISON: decrypted vs encrypted
assertTrue
(
clearMsg
.
equals
(
decryptedMsg
));
// clean objects..
assertTrue
(
0
==
bobAccount
.
removeOneTimeKeysForSession
(
bobSession
.
getOlmSessionId
()));
bobAccount
.
releaseAccount
();
aliceAccount
.
releaseAccount
();
bobSession
.
releaseSession
();
aliceSession
.
releaseSession
();
}
}
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java
View file @
f2ca1ce3
...
...
@@ -29,16 +29,12 @@ public class OlmAccount {
public
static
final
String
JSON_KEY_IDENTITY_KEY
=
"curve25519"
;
public
static
final
String
JSON_KEY_FINGER_PRINT_KEY
=
"ed25519"
;
/** instance unique identifier, used in JNI to match the corresponding native class **/
private
int
mJavaInstanceId
;
/** account raw pointer value (OlmAccount*) returned by JNI.
* this value identifies uniquely the native account instance.
*/
private
long
mNativeOlmAccountId
;
public
OlmAccount
()
{
mJavaInstanceId
=
hashCode
();
//initNewAccount();
}
...
...
@@ -101,8 +97,13 @@ public class OlmAccount {
/**
* Return the identity keys (identity & fingerprint keys) in a JSON array.<br>
* Public API for {@link #identityKeysJni()}.
* @return identity keys in JSON array format if operation succeed, null otherwise
* Public API for {@link #identityKeysJni()}.<br>
* Ex:<tt>
* {
* "curve25519":"Vam++zZPMqDQM6ANKpO/uAl5ViJSHxV9hd+b0/fwRAg",
* "ed25519":"+v8SOlOASFTMrX3MCKBM4iVnYoZ+JIjpNt1fi8Z9O2I"
* }</tt>
* @return identity keys in JSON array if operation succeed, null otherwise
*/
public
JSONObject
identityKeys
()
{
JSONObject
identityKeysJsonObj
=
null
;
...
...
@@ -150,6 +151,14 @@ public class OlmAccount {
/**
* Return the "one time keys" in a JSON array.<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 JSON array format if operation succeed, null otherwise
...
...
@@ -192,9 +201,4 @@ public class OlmAccount {
* @return the signed message if operation succeed, null otherwise
*/
public
native
String
signMessage
(
String
aMessage
);
@Override
public
String
toString
()
{
return
super
.
toString
();
}
}
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmSession.java
View file @
f2ca1ce3
...
...
@@ -19,11 +19,9 @@ package org.matrix.olm;
import
android.text.TextUtils
;
import
android.util.Log
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
public
class
OlmSession
{
private
static
final
String
LOG_TAG
=
"OlmSession"
;
/** session raw pointer value (OlmSession*) returned by JNI.
* this value uniquely identifies the native session instance.
**/
...
...
@@ -44,6 +42,14 @@ public class OlmSession {
return
mNativeOlmSessionId
;
}
/**
* Getter on the session ID.
* @return native session ID
*/
public
OlmAccount
getOlmAccountId
(){
return
mOlmAccount
;
}
/**
* Destroy the corresponding OLM session native object.<br>
* This method must ALWAYS be called when this JAVA instance
...
...
@@ -105,8 +111,9 @@ public class OlmSession {
// set the account of this session
mOlmAccount
=
aAccount
;
int
retCode
=
initOutboundSessionJni
(
mOlmAccount
.
getOlmAccountId
(),
aTheirIdentityKey
,
aTheirOneTimeKey
);
retObj
=
this
;
if
(
0
==
initOutboundSessionJni
(
mOlmAccount
.
getOlmAccountId
(),
aTheirIdentityKey
,
aTheirOneTimeKey
))
{
retObj
=
this
;
}
}
return
retObj
;
...
...
@@ -121,7 +128,7 @@ public class OlmSession {
* Public API for {@link #initInboundSessionJni(long, String)}.
* 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 aOneTimeKeyMsg PRE KEY message
TODO TBC
* @param aOneTimeKeyMsg PRE KEY message
* @return this if operation succeed, null otherwise
*/
public
OlmSession
initInboundSessionWithAccount
(
OlmAccount
aAccount
,
String
aOneTimeKeyMsg
)
{
...
...
@@ -227,6 +234,7 @@ public class OlmSession {
/**
* Encrypt a message using the session.<br>
* The encrypted message is returned in a OlmMessage object.
* Public API for {@link #encryptMessageJni(String, OlmMessage)}.
* @param aClearMsg message to encrypted
* @return the encrypted message if operation succeed, null otherwise
...
...
@@ -243,11 +251,12 @@ public class OlmSession {
private
native
int
encryptMessageJni
(
String
aClearMsg
,
OlmMessage
aEncryptedMsg
);
@Override
public
String
toString
()
{
return
super
.
toString
();
}
/**
* 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 if operation succeed, null otherwise
*/
public
native
String
decryptMessage
(
OlmMessage
aEncryptedMsg
);
}
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp
View file @
f2ca1ce3
...
...
@@ -44,7 +44,7 @@ OlmAccount* initializeAccountMemory()
* This method MUST be called when java counter part account instance is done.
*
*/
JNIEXPORT
void
JNICALL
Java_org_matrix_olm_OlmAccount_
releaseAccountJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
void
OLM_ACCOUNT_FUNC_DEF
(
releaseAccountJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmAccount
*
accountPtr
=
NULL
;
...
...
@@ -55,8 +55,11 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmAccount_releaseAccountJni(JNIEnv *
LOGE
(
"## releaseAccountJni(): failure - invalid Account ptr=NULL"
);
}
else
{
// even if free(NULL) does not crash, a test is performed for debug purpose
{
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"
);
}
...
...
@@ -68,7 +71,7 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmAccount_releaseAccountJni(JNIEnv *
* to make the cast (OlmAccount* => jlong) platform independant.
* @return the initialized OlmAccount* instance if init succeed, NULL otherwise
**/
JNIEXPORT
jlong
JNICALL
Java_org_matrix_olm_OlmAccount_
initNewAccountJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jlong
OLM_ACCOUNT_FUNC_DEF
(
initNewAccountJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmAccount
*
accountPtr
=
NULL
;
uint8_t
*
randomBuffPtr
=
NULL
;
...
...
@@ -120,7 +123,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_initNewAccountJni(JNIEnv
* The keys are returned in the byte array.
* @return a valid byte array if operation succeed, null otherwise
**/
JNIEXPORT
jbyteArray
JNICALL
Java_org_matrix_olm_OlmAccount_
identityKeysJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jbyteArray
OLM_ACCOUNT_FUNC_DEF
(
identityKeysJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmAccount
*
accountPtr
=
NULL
;
size_t
identityKeysLength
;
...
...
@@ -175,7 +178,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_identityKeysJni(JNIE
* Get the maximum number of "one time keys" the account can store.
*
**/
JNIEXPORT
jlong
JNICALL
Java_org_matrix_olm_OlmAccount_
maxOneTimeKeys
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jlong
OLM_ACCOUNT_FUNC_DEF
(
maxOneTimeKeys
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmAccount
*
accountPtr
=
NULL
;
size_t
maxKeys
=
-
1
;
...
...
@@ -198,7 +201,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmAccount_maxOneTimeKeys(JNIEnv *en
* @param aNumberOfKeys number of keys to generate
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
**/
JNIEXPORT
jint
JNICALL
Java_org_matrix_olm_OlmAccount_
generateOneTimeKeys
(
JNIEnv
*
env
,
jobject
thiz
,
jint
aNumberOfKeys
)
JNIEXPORT
jint
OLM_ACCOUNT_FUNC_DEF
(
generateOneTimeKeys
)
(
JNIEnv
*
env
,
jobject
thiz
,
jint
aNumberOfKeys
)
{
OlmAccount
*
accountPtr
=
NULL
;
uint8_t
*
randomBufferPtr
=
NULL
;
...
...
@@ -206,7 +209,6 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv
size_t
randomLength
;
size_t
result
;
LOGD
(
"## generateOneTimeKeys(): accountPtr =%p aNumberOfKeys=%d"
,
accountPtr
,
aNumberOfKeys
);
if
(
NULL
==
(
accountPtr
=
(
OlmAccount
*
)
getAccountInstanceId
(
env
,
thiz
)))
{
...
...
@@ -222,7 +224,10 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv
LOGE
(
"## generateOneTimeKeys(): failure - random buffer init"
);
}
else
{
// retrieve key pairs in keysBytesPtr
{
LOGD
(
"## generateOneTimeKeys(): accountPtr =%p aNumberOfKeys=%d"
,
accountPtr
,
aNumberOfKeys
);
// retrieve key pairs in keysBytesPtr
result
=
olm_account_generate_one_time_keys
(
accountPtr
,
aNumberOfKeys
,
(
void
*
)
randomBufferPtr
,
randomLength
);
if
(
result
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_account_last_error
(
accountPtr
);
...
...
@@ -249,7 +254,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_generateOneTimeKeys(JNIEnv
* 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
JNICALL
Java_org_matrix_olm_OlmAccount_
oneTimeKeysJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jbyteArray
OLM_ACCOUNT_FUNC_DEF
(
oneTimeKeysJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmAccount
*
accountPtr
=
NULL
;
size_t
keysLength
;
...
...
@@ -303,7 +308,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_matrix_olm_OlmAccount_oneTimeKeysJni(JNIEn
* @param aNativeOlmSessionId session instance
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS if no matching keys, ERROR_CODE_KO otherwise
**/
JNIEXPORT
jint
JNICALL
Java_org_matrix_olm_OlmAccount_
removeOneTimeKeysForSession
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aNativeOlmSessionId
)
JNIEXPORT
jint
OLM_ACCOUNT_FUNC_DEF
(
removeOneTimeKeysForSession
)
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aNativeOlmSessionId
)
{
jint
retCode
=
ERROR_CODE_KO
;
OlmAccount
*
accountPtr
=
NULL
;
...
...
@@ -342,7 +347,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_removeOneTimeKeysForSessio
* Mark the current set of "one time keys" as being published.
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
**/
JNIEXPORT
jint
JNICALL
Java_org_matrix_olm_OlmAccount_
markOneTimeKeysAsPublished
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jint
OLM_ACCOUNT_FUNC_DEF
(
markOneTimeKeysAsPublished
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
jint
retCode
=
ERROR_CODE_OK
;
OlmAccount
*
accountPtr
=
NULL
;
...
...
@@ -377,7 +382,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmAccount_markOneTimeKeysAsPublished
* @param aMessage message to sign
* @return the signed message, null otherwise
**/
JNIEXPORT
jstring
JNICALL
Java_org_matrix_olm_OlmAccount_
signMessage
(
JNIEnv
*
env
,
jobject
thiz
,
jstring
aMessage
)
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
signMessage
)
(
JNIEnv
*
env
,
jobject
thiz
,
jstring
aMessage
)
{
OlmAccount
*
accountPtr
=
NULL
;
size_t
signatureLength
;
...
...
@@ -439,7 +444,7 @@ JNIEXPORT jstring JNICALL Java_org_matrix_olm_OlmAccount_signMessage(JNIEnv *env
}
JNIEXPORT
jstring
JNICALL
Java_org_matrix_olm_OlmManager_
getOlmLibVersion
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jstring
OLM_MANAGER_FUNC_DEF
(
getOlmLibVersion
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
uint8_t
majorVer
=
0
,
minorVer
=
0
,
patchVer
=
0
;
jstring
returnValueStr
=
0
;
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h
View file @
f2ca1ce3
...
...
@@ -3,6 +3,9 @@
#include "olm_jni.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
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni.h
View file @
f2ca1ce3
...
...
@@ -28,6 +28,8 @@
#define LOGE(...)
#endif
#define FUNC_DEF(class_name,func_name) JNICALL Java_org_matrix_olm_##class_name##_##func_name
// Error codes definition
static
const
int
ERROR_CODE_OK
=
0
;
static
const
int
ERROR_CODE_NO_MATCHING_ONE_TIME_KEYS
=
ERROR_CODE_OK
+
1
;
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_session.cpp
View file @
f2ca1ce3
...
...
@@ -41,7 +41,7 @@ OlmSession* initializeSessionMemory()
return
sessionPtr
;
}
JNIEXPORT
void
JNICALL
Java_org_matrix_olm_OlmSession_
releaseSessionJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
void
OLM_SESSION_FUNC_DEF
(
releaseSessionJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmSession
*
sessionPtr
=
NULL
;
...
...
@@ -50,8 +50,11 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmSession_releaseSessionJni(JNIEnv *
LOGE
(
"## releaseSessionJni(): failure - invalid Session ptr=NULL"
);
}
else
{
// even if free(NULL) does not crash, a test is performed for debug purpose
{
olm_clear_session
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): IN"
);
// even if free(NULL) does not crash, logs are performed for debug purpose
free
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): OUT"
);
}
...
...
@@ -63,7 +66,7 @@ JNIEXPORT void JNICALL Java_org_matrix_olm_OlmSession_releaseSessionJni(JNIEnv *
* to make the cast (OlmSession* => jlong) platform independent.
* @return the initialized OlmSession* instance if init succeed, NULL otherwise
**/
JNIEXPORT
jlong
JNICALL
Java_org_matrix_olm_OlmSession_
initNewSessionJni
(
JNIEnv
*
env
,
jobject
thiz
)
JNIEXPORT
jlong
OLM_SESSION_FUNC_DEF
(
initNewSessionJni
)
(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmSession
*
sessionPtr
=
NULL
;
...
...
@@ -92,7 +95,7 @@ JNIEXPORT jlong JNICALL Java_org_matrix_olm_OlmSession_initNewSessionJni(JNIEnv
* @param aTheirOneTimeKey the one time key of the recipient
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
**/
JNIEXPORT
jint
JNICALL
Java_org_matrix_olm_OlmSession_
initOutboundSessionJni
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aOlmAccountId
,
jstring
aTheirIdentityKey
,
jstring
aTheirOneTimeKey
)
JNIEXPORT
jint
OLM_SESSION_FUNC_DEF
(
initOutboundSessionJni
)
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aOlmAccountId
,
jstring
aTheirIdentityKey
,
jstring
aTheirOneTimeKey
)
{
jint
retCode
=
ERROR_CODE_KO
;
OlmSession
*
sessionPtr
=
NULL
;
...
...
@@ -117,7 +120,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initOutboundSessionJni(JNI
else
{
// allocate random buffer
size_t
randomSize
=
olm_create_outbound_session_random_length
(
sessionPtr
);
if
(
false
==
setRandomInBuffer
(
&
randomBuffPtr
,
randomSize
))
if
((
0
!=
randomSize
)
&&
(
false
==
setRandomInBuffer
(
&
randomBuffPtr
,
randomSize
))
)
{
LOGE
(
"## initOutboundSessionJni(): failure - random buffer init"
);
}
...
...
@@ -188,7 +191,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initOutboundSessionJni(JNI
* @param aOneTimeKeyMsg PRE_KEY message
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
*/
JNIEXPORT
jint
JNICALL
Java_org_matrix_olm_OlmSession_
initInboundSessionJni
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aOlmAccountId
,
jstring
aOneTimeKeyMsg
)
JNIEXPORT
jint
OLM_SESSION_FUNC_DEF
(
initInboundSessionJni
)
(
JNIEnv
*
env
,
jobject
thiz
,
jlong
aOlmAccountId
,
jstring
aOneTimeKeyMsg
)
{
jint
retCode
=
ERROR_CODE_KO
;
OlmSession
*
sessionPtr
=
NULL
;
...
...
@@ -206,7 +209,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE
}
else
if
(
0
==
aOneTimeKeyMsg
)
{
LOGE
(
"## init
Out
boundSessionJni(): failure - invalid message"
);
LOGE
(
"## init
In
boundSessionJni(): failure - invalid message"
);
}
else
{
// convert message to C strings
...
...
@@ -217,7 +220,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE
else
{
int
messageLength
=
env
->
GetStringUTFLength
(
aOneTimeKeyMsg
);
LOGD
(
"## initInboundSessionJni(): message=%
s
message
Length
=%
d
"
,
messagePtr
,
messageLength
);
LOGD
(
"## initInboundSessionJni(): message
Length
=%
d
message=%
s
"
,
messageLength
,
messagePtr
);
sessionResult
=
olm_create_inbound_session
(
sessionPtr
,
accountPtr
,
(
void
*
)
messagePtr
,
messageLength
);
if
(
sessionResult
==
olm_error
())
{
...
...
@@ -245,7 +248,7 @@ JNIEXPORT jint JNICALL Java_org_matrix_olm_OlmSession_initInboundSessionJni(JNIE
* @param aOneTimeKeyMsg encrypted message
* @return ERROR_CODE_OK if operation succeed, ERROR_CODE_KO otherwise
*/
JNIEXPORT
j