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
ebfcd03c
Commit
ebfcd03c
authored
Oct 14, 2016
by
pedroGitt
Browse files
- Add in/outbound group unit test OK
parent
10280995
Changes
9
Hide whitespace changes
Inline
Side-by-side
java/android/OlmLibSdk/olm-sdk/src/androidTest/java/org/matrix/olm/OlmGroupTest.java
View file @
ebfcd03c
package
org.matrix.olm
;
import
android.support.test.runner.AndroidJUnit4
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
org.junit.BeforeClass
;
...
...
@@ -10,6 +11,7 @@ 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
.
assertTrue
;
...
...
@@ -19,6 +21,15 @@ public class OlmGroupTest {
private
static
final
String
LOG_TAG
=
"OlmSessionTest"
;
private
static
OlmManager
mOlmManager
;
private
static
OlmOutboundGroupSession
mAliceOutboundSession
;
private
static
String
mAliceSessionIdentifier
;
private
static
long
mAliceMessageIndex
;
public
static
final
String
CLEAR_MESSAGE1
=
"Hello!"
;
private
static
String
mAliceToBobMessage
;
private
static
OlmInboundGroupSession
mBobInboundSession
;
private
static
String
mAliceOutboundSessionKey
;
private
static
String
mBobSessionIdentifier
;
private
static
String
mBobDecryptedMessage
;
@BeforeClass
public
static
void
setUpClass
(){
...
...
@@ -30,66 +41,112 @@ public class OlmGroupTest {
Log
.
d
(
LOG_TAG
,
"## setUpClass(): lib version="
+
version
);
}
@Test
public
void
test00AliceToBob
()
{
// TBD
}
/**
* Basic test:
* - alice creates an account
* - bob creates an account
* - 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
test01AliceToBob
()
{
// creates alice outbound session
OlmOutboundGroupSession
aliceOutboundSession
=
new
OlmOutboundGroupSession
();
@Test
public
void
test01CreateOutboundSession
()
{
// alice creates OUTBOUND GROUP SESSION
try
{
mAliceOutboundSession
=
new
OlmOutboundGroupSession
();
}
catch
(
OlmException
e
)
{
assertTrue
(
"Exception in OlmOutboundGroupSession, Exception code="
+
e
.
getExceptionCode
(),
false
);
}
}
// test accounts creation
String
aliceSessionIdentifier
=
aliceOutboundSession
.
sessionIdentifier
();
assertNotNull
(
aliceSessionIdentifier
);
assertTrue
(
aliceSessionIdentifier
.
length
()>
0
);
@Test
public
void
test02GetOutboundGroupSessionIdentifier
()
{
// test session ID
mAliceSessionIdentifier
=
mAliceOutboundSession
.
sessionIdentifier
();
assertNotNull
(
mAliceSessionIdentifier
);
assertTrue
(
mAliceSessionIdentifier
.
length
()
>
0
);
}
String
aliceOutboundSessionKey
=
aliceOutboundSession
.
sessionKey
();
assertNotNull
(
aliceOutboundSessionKey
);
assertTrue
(
aliceOutboundSessionKey
.
length
()>
0
);
@Test
public
void
test03GetOutboundGroupSessionKey
()
{
// test session Key
mAliceOutboundSessionKey
=
mAliceOutboundSession
.
sessionKey
();
assertNotNull
(
mAliceOutboundSessionKey
);
assertTrue
(
mAliceOutboundSessionKey
.
length
()
>
0
);
}
long
messageIndex
=
aliceOutboundSession
.
messageIndex
();
assertTrue
(
0
==
messageIndex
);
@Test
public
void
test04GetOutboundGroupMessageIndex
()
{
// test message index before any encryption
mAliceMessageIndex
=
mAliceOutboundSession
.
messageIndex
();
assertTrue
(
0
==
mAliceMessageIndex
);
}
String
clearMessage
=
"Hello!"
;
String
encryptedMessage
=
aliceOutboundSession
.
encryptMessage
(
clearMessage
);
assertNotNull
(
encryptedMessage
);
@Test
public
void
test05OutboundGroupEncryptMessage
()
{
// alice encrypts a message to bob
mAliceToBobMessage
=
mAliceOutboundSession
.
encryptMessage
(
CLEAR_MESSAGE1
);
assertFalse
(
TextUtils
.
isEmpty
(
mAliceToBobMessage
));
// test message index after encryption is incremented
mAliceMessageIndex
=
mAliceOutboundSession
.
messageIndex
();
assertTrue
(
1
==
mAliceMessageIndex
);
}
@Test
public
void
test06CreateInboundGroupSession
()
{
// bob creates INBOUND GROUP SESSION with alice outbound key
try
{
mBobInboundSession
=
new
OlmInboundGroupSession
(
mAliceOutboundSessionKey
);
}
catch
(
OlmException
e
)
{
assertTrue
(
"Exception in bob OlmInboundGroupSession, Exception code="
+
e
.
getExceptionCode
(),
false
);
}
}
messageIndex
=
aliceOutboundSession
.
messageIndex
();
assertTrue
(
1
==
messageIndex
);
@Test
public
void
test07OutboundGroupSessionIdentifiers
()
{
// check session identifiers are equals
mAliceSessionIdentifier
=
mAliceOutboundSession
.
sessionIdentifier
();
assertFalse
(
TextUtils
.
isEmpty
(
mAliceSessionIdentifier
));
}
assertTrue
(
encryptedMessage
.
length
()>=
0
);
@Test
public
void
test08InboundGroupSessionIdentifiers
()
{
// check session identifiers are equals
mBobSessionIdentifier
=
mBobInboundSession
.
sessionIdentifier
();
assertFalse
(
TextUtils
.
isEmpty
(
mBobSessionIdentifier
));
assertTrue
(
mAliceSessionIdentifier
.
equals
(
mBobSessionIdentifier
));
}
OlmInboundGroupSession
bobInboundSession
=
new
OlmInboundGroupSession
();
bobInboundSession
.
initInboundGroupSessionWithSessionKey
(
aliceOutboundSessionKey
);
@Test
public
void
test09SessionIdentifiersIdentical
()
{
// check session identifiers are equals
aliceSessionIdentifier
=
aliceOutboundSession
.
sessionIdentifier
();
String
bobSessionIdentifier
=
aliceOutboundSession
.
sessionIdentifier
();
assertTrue
(
aliceSessionIdentifier
.
equals
(
bobSessionIdentifier
));
assertTrue
(
mAliceSessionIdentifier
.
equals
(
mBobSessionIdentifier
));
}
String
decryptedMessage
=
bobInboundSession
.
decryptMessage
(
encryptedMessage
);
assertTrue
(
decryptedMessage
.
equals
(
bobSessionIdentifier
));
@Test
public
void
test10InboundDecryptMessage
()
{
// test decrypted message
mBobDecryptedMessage
=
mBobInboundSession
.
decryptMessage
(
mAliceToBobMessage
);
assertFalse
(
TextUtils
.
isEmpty
(
mBobDecryptedMessage
));
assertTrue
(
mBobDecryptedMessage
.
equals
(
CLEAR_MESSAGE1
));
}
//@Test
public
void
test02InboundGroupSession
()
{
// creates alice outbound session
OlmInboundGroupSession
aliceInboundSession
=
new
OlmInboundGroupSession
();
@Test
public
void
test11InboundDecryptedMessageIdentical
()
{
// test decrypted message
assertTrue
(
mBobDecryptedMessage
.
equals
(
CLEAR_MESSAGE1
));
}
// test session identifier
String
sessionIdentifier
=
aliceInboundSession
.
sessionIdentifier
();
assertNotNull
(
sessionIdentifier
);
assertTrue
(
sessionIdentifier
.
length
()>
0
);
@Test
public
void
test12ReleaseOutboundSession
()
{
// release group sessions
mAliceOutboundSession
.
releaseSession
(
);
}
@Test
public
void
test13ReleaseInboundSession
()
{
// release group sessions
mBobInboundSession
.
releaseSession
();
}
}
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/Olm
Utils
Exception.java
→
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmException.java
View file @
ebfcd03c
...
...
@@ -16,7 +16,7 @@
package
org.matrix.olm
;
public
class
Olm
Utils
Exception
extends
Exception
{
public
class
OlmException
extends
Exception
{
// exception codes
public
static
final
int
EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE
=
0
;
public
static
final
int
EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION
=
1
;
...
...
@@ -24,7 +24,7 @@ public class OlmUtilsException extends Exception {
private
final
int
mCode
;
public
Olm
Utils
Exception
(
int
aExceptionCode
)
{
public
OlmException
(
int
aExceptionCode
)
{
super
();
mCode
=
aExceptionCode
;
}
...
...
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmInboundGroupSession.java
View file @
ebfcd03c
...
...
@@ -48,16 +48,16 @@ public class OlmInboundGroupSession implements Serializable {
* The session key parameter is retrieved from a outbound group session
* See {@link #initNewSession()} and {@link #initInboundGroupSessionWithSessionKey(String)}
* @param aSessionKey session key
* @throws Olm
Utils
Exception
* @throws OlmException
*/
public
OlmInboundGroupSession
(
String
aSessionKey
)
throws
Olm
Utils
Exception
{
public
OlmInboundGroupSession
(
String
aSessionKey
)
throws
OlmException
{
if
(
initNewSession
())
{
if
(
0
!=
initInboundGroupSessionWithSessionKey
(
aSessionKey
))
{
releaseSession
();
// prevent memory leak before throwing
throw
new
Olm
Utils
Exception
(
Olm
Utils
Exception
.
EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION
);
throw
new
OlmException
(
OlmException
.
EXCEPTION_CODE_INIT_INBOUND_GROUP_SESSION
);
}
}
else
{
throw
new
Olm
Utils
Exception
(
Olm
Utils
Exception
.
EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE
);
throw
new
OlmException
(
OlmException
.
EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE
);
}
}
...
...
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmOutboundGroupSession.java
View file @
ebfcd03c
...
...
@@ -18,7 +18,6 @@ package org.matrix.olm;
import
android.text.TextUtils
;
import
android.util.Log
;
public
class
OlmOutboundGroupSession
{
private
static
final
String
LOG_TAG
=
"OlmOutboundGroupSession"
;
...
...
@@ -41,16 +40,16 @@ public class OlmOutboundGroupSession {
* Create and save a new session native instance ID and
* initialise a new outbound group session.<br>
* See {@link #initNewSession()} and {@link #initOutboundGroupSession()}
* @throws Olm
Utils
Exception
* @throws OlmException
*/
public
OlmOutboundGroupSession
()
throws
Olm
Utils
Exception
{
public
OlmOutboundGroupSession
()
throws
OlmException
{
if
(
initNewSession
())
{
if
(
0
!=
initOutboundGroupSession
())
{
releaseSession
();
// prevent memory leak before throwing
throw
new
Olm
Utils
Exception
(
Olm
Utils
Exception
.
EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION
);
throw
new
OlmException
(
OlmException
.
EXCEPTION_CODE_INIT_OUTBOUND_GROUP_SESSION
);
}
}
else
{
throw
new
Olm
Utils
Exception
(
Olm
Utils
Exception
.
EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE
);
throw
new
OlmException
(
OlmException
.
EXCEPTION_CODE_INIT_NEW_SESSION_FAILURE
);
}
}
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_inbound_group_session.cpp
View file @
ebfcd03c
...
...
@@ -26,7 +26,7 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
{
OlmInboundGroupSession
*
sessionPtr
=
NULL
;
LOGD
(
"## releaseSessionJni():
sessionPtr=%p"
,
session
Ptr
);
LOGD
(
"## releaseSessionJni():
intbound group
session
"
);
if
(
NULL
==
(
sessionPtr
=
(
OlmInboundGroupSession
*
)
getInboundGroupSessionInstanceId
(
env
,
thiz
)))
{
...
...
@@ -34,12 +34,14 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
}
else
{
LOGD
(
" ## releaseSessionJni(): sessionPtr=%p"
,
sessionPtr
);
size_t
retCode
=
olm_clear_inbound_group_session
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): clear_inbound_group_session=%lu"
,
retCode
);
LOGD
(
"
## releaseSessionJni(): clear_inbound_group_session=%lu"
,
retCode
);
LOGD
(
"## releaseSessionJni(): IN"
);
LOGD
(
"
## releaseSessionJni():
free
IN"
);
free
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): OUT"
);
LOGD
(
"
## releaseSessionJni():
free
OUT"
);
}
}
...
...
@@ -52,20 +54,23 @@ JNIEXPORT void OLM_INBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *env
JNIEXPORT
jlong
OLM_INBOUND_GROUP_SESSION_FUNC_DEF
(
initNewSessionJni
)(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmInboundGroupSession
*
sessionPtr
=
NULL
;
size_t
sessionSize
=
olm_inbound_group_session_size
();
size_t
sessionSize
=
0
;
LOGD
(
"## initNewSessionJni(): inbound group session IN"
);
sessionSize
=
olm_inbound_group_session_size
();
if
(
0
==
sessionSize
)
{
LOGE
(
"## initNewSessionJni(): failure - inbound group session size = 0"
);
LOGE
(
"
## initNewSessionJni(): failure - inbound group session size = 0"
);
}
else
if
(
NULL
!=
(
sessionPtr
=
(
OlmInboundGroupSession
*
)
malloc
(
sessionSize
)))
{
sessionPtr
=
olm_inbound_group_session
(
sessionPtr
);
LOGD
(
"## initNewSessionJni(): success - inbound group session size=%lu"
,
sessionSize
);
LOGD
(
"
## initNewSessionJni(): success - inbound group session size=%lu"
,
sessionSize
);
}
else
{
LOGE
(
"## initNewSessionJni(): failure - inbound group session OOM"
);
LOGE
(
"
## initNewSessionJni(): failure - inbound group session OOM"
);
}
return
(
jlong
)(
intptr_t
)
sessionPtr
;
...
...
@@ -83,32 +88,34 @@ JNIEXPORT jint OLM_INBOUND_GROUP_SESSION_FUNC_DEF(initInboundGroupSessionWithSes
const
uint8_t
*
sessionKeyPtr
=
NULL
;
size_t
sessionResult
;
LOGD
(
"## initInboundGroupSessionWithSessionKeyJni(): inbound group session IN"
);
if
(
NULL
==
(
sessionPtr
=
(
OlmInboundGroupSession
*
)
getInboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"
);
LOGE
(
"
## initInboundGroupSessionWithSessionKeyJni(): failure - invalid inbound group session instance"
);
}
else
if
(
0
==
aSessionKey
)
{
LOGE
(
"## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"
);
LOGE
(
"
## initInboundGroupSessionWithSessionKeyJni(): failure - invalid aSessionKey"
);
}
else
if
(
NULL
==
(
sessionKeyPtr
=
(
const
uint8_t
*
)
env
->
GetStringUTFChars
(
aSessionKey
,
0
)))
{
LOGE
(
"## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"
);
LOGE
(
"
## initInboundSessionFromIdKeyJni(): failure - session key JNI allocation OOM"
);
}
else
{
size_t
sessionKeyLength
=
(
size_t
)
env
->
GetStringUTFLength
(
aSessionKey
);
LOGD
(
"## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu"
,
sessionKeyLength
);
LOGD
(
"
## initInboundSessionFromIdKeyJni(): sessionKeyLength=%lu"
,
sessionKeyLength
);
sessionResult
=
olm_init_inbound_group_session
(
sessionPtr
,
sessionKeyPtr
,
sessionKeyLength
);
if
(
sessionResult
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_inbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## initInboundSessionFromIdKeyJni(): failure - init inbound session creation Msg=%s"
,
errorMsgPtr
);
}
else
{
retCode
=
ERROR_CODE_OK
;
LOGD
(
"## initInboundSessionFromIdKeyJni(): success - result=%lu"
,
sessionResult
);
LOGD
(
"
## initInboundSessionFromIdKeyJni(): success - result=%lu"
,
sessionResult
);
}
}
...
...
@@ -135,17 +142,17 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
if
(
NULL
==
(
sessionPtr
=
(
OlmInboundGroupSession
*
)
getInboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"## sessionIdentifierJni(): failure - invalid inbound group session instance"
);
LOGE
(
"
## sessionIdentifierJni(): failure - 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"
,
lengthSessionId
);
LOGD
(
"
## sessionIdentifierJni(): inbound group session lengthSessionId=%lu"
,
lengthSessionId
);
if
(
NULL
==
(
sessionIdPtr
=
(
uint8_t
*
)
malloc
(
lengthSessionId
*
sizeof
(
uint8_t
))))
{
LOGE
(
"## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"
);
LOGE
(
"
## sessionIdentifierJni(): failure - inbound group session identifier allocation OOM"
);
}
else
{
...
...
@@ -153,13 +160,13 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIEn
if
(
result
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_inbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## sessionIdentifierJni(): failure - get inbound group session identifier failure Msg=%s"
,
errorMsgPtr
);
}
else
{
// update length
sessionIdPtr
[
result
]
=
static_cast
<
char
>
(
'\0'
);
LOGD
(
"## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s"
,
result
,
(
char
*
)
sessionIdPtr
);
LOGD
(
"
## sessionIdentifierJni(): success - inbound group session result=%lu sessionId=%s"
,
result
,
(
char
*
)
sessionIdPtr
);
returnValueStr
=
env
->
NewStringUTF
((
const
char
*
)
sessionIdPtr
);
}
free
(
sessionIdPtr
);
...
...
@@ -178,19 +185,19 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
uint8_t
*
plainTextMsgPtr
=
NULL
;
uint8_t
*
tempEncryptedPtr
=
NULL
;
LOGD
(
"## decryptMessageJni(): IN"
);
LOGD
(
"## decryptMessageJni():
inbound group session
IN"
);
if
(
NULL
==
(
sessionPtr
=
(
OlmInboundGroupSession
*
)
getInboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"##
decryptMessageJni(): failure - invalid inbound group session ptr=NULL"
);
LOGE
(
"
## decryptMessageJni(): failure - invalid inbound group session ptr=NULL"
);
}
else
if
(
0
==
aEncryptedMsg
)
{
LOGE
(
"##
decryptMessageJni(): failure - invalid encrypted message"
);
LOGE
(
"
## decryptMessageJni(): failure - invalid encrypted message"
);
}
else
if
(
0
==
(
encryptedMsgPtr
=
env
->
GetStringUTFChars
(
aEncryptedMsg
,
0
)))
{
LOGE
(
"##
decryptMessageJni(): failure - encrypted message JNI allocation OOM"
);
LOGE
(
"
## decryptMessageJni(): failure - encrypted message JNI allocation OOM"
);
}
else
{
...
...
@@ -200,12 +207,12 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
// create a dedicated temp buffer to be used in next Olm API calls
if
(
NULL
==
(
tempEncryptedPtr
=
static_cast
<
uint8_t
*>
(
malloc
(
encryptedMsgLength
*
sizeof
(
uint8_t
)))))
{
LOGE
(
"##
decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"
);
LOGE
(
"
## decryptMessageJni(): failure - tempEncryptedPtr allocation OOM"
);
}
else
{
memcpy
(
tempEncryptedPtr
,
encryptedMsgPtr
,
encryptedMsgLength
);
LOGD
(
"##
decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s"
,
encryptedMsgLength
,
encryptedMsgPtr
);
LOGD
(
"
## decryptMessageJni(): encryptedMsgLength=%lu encryptedMsg=%s"
,
encryptedMsgLength
,
encryptedMsgPtr
);
// get max plaintext length
size_t
maxPlainTextLength
=
olm_group_decrypt_max_plaintext_length
(
sessionPtr
,
...
...
@@ -214,11 +221,11 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
if
(
maxPlainTextLength
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_inbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"##
decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## decryptMessageJni(): failure - olm_group_decrypt_max_plaintext_length Msg=%s"
,
errorMsgPtr
);
}
else
{
LOGD
(
"##
decryptMessageJni(): maxPlaintextLength=%lu"
,
maxPlainTextLength
);
LOGD
(
"
## decryptMessageJni(): maxPlaintextLength=%lu"
,
maxPlainTextLength
);
// allocate output decrypted message
plainTextMsgPtr
=
static_cast
<
uint8_t
*>
(
malloc
(
maxPlainTextLength
*
sizeof
(
uint8_t
)));
...
...
@@ -233,14 +240,14 @@ JNIEXPORT jstring OLM_INBOUND_GROUP_SESSION_FUNC_DEF(decryptMessageJni)(JNIEnv *
if
(
plaintextLength
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_inbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"##
decryptMessageJni(): failure - olm_group_decrypt Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## decryptMessageJni(): failure - olm_group_decrypt Msg=%s"
,
errorMsgPtr
);
}
else
{
// update decrypted buffer size
plainTextMsgPtr
[
plaintextLength
]
=
static_cast
<
char
>
(
'\0'
);
LOGD
(
"##
decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s"
,
plaintextLength
,
(
char
*
)
plainTextMsgPtr
);
LOGD
(
"
## decryptMessageJni(): decrypted returnedLg=%lu plainTextMsgPtr=%s"
,
plaintextLength
,
(
char
*
)
plainTextMsgPtr
);
decryptedMsgRetValue
=
env
->
NewStringUTF
((
const
char
*
)
plainTextMsgPtr
);
}
}
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_outbound_group_session.cpp
View file @
ebfcd03c
...
...
@@ -26,20 +26,22 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en
{
OlmOutboundGroupSession
*
sessionPtr
=
NULL
;
LOGD
(
"## releaseSessionJni():
sessionPtr=%p"
,
session
Ptr
);
LOGD
(
"## releaseSessionJni():
outbound group
session
"
);
if
(
NULL
==
(
sessionPtr
=
(
OlmOutboundGroupSession
*
)
getOutboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"## releaseSessionJni(): failure - invalid outbound group session instance"
);
LOGE
(
"
## releaseSessionJni(): failure - invalid outbound group session instance"
);
}
else
{
LOGD
(
" ## releaseSessionJni(): sessionPtr=%p"
,
sessionPtr
);
size_t
retCode
=
olm_clear_outbound_group_session
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): clear_outbound_group_session=%lu"
,
retCode
);
LOGD
(
"
## releaseSessionJni(): clear_outbound_group_session=%lu"
,
retCode
);
LOGD
(
"## releaseSessionJni(): IN"
);
LOGD
(
"
## releaseSessionJni():
free
IN"
);
free
(
sessionPtr
);
LOGD
(
"## releaseSessionJni(): OUT"
);
LOGD
(
"
## releaseSessionJni():
free
OUT"
);
}
}
...
...
@@ -52,20 +54,23 @@ JNIEXPORT void OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(releaseSessionJni)(JNIEnv *en
JNIEXPORT
jlong
OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF
(
initNewSessionJni
)(
JNIEnv
*
env
,
jobject
thiz
)
{
OlmOutboundGroupSession
*
sessionPtr
=
NULL
;
size_t
sessionSize
=
olm_outbound_group_session_size
();
size_t
sessionSize
=
0
;
LOGD
(
"## initNewSessionJni(): outbound group session IN"
);
sessionSize
=
olm_outbound_group_session_size
();
if
(
0
==
sessionSize
)
{
LOGE
(
"## initNewSessionJni(): failure - outbound group session size = 0"
);
LOGE
(
"
## initNewSessionJni(): failure - outbound group session size = 0"
);
}
else
if
(
NULL
!=
(
sessionPtr
=
(
OlmOutboundGroupSession
*
)
malloc
(
sessionSize
)))
{
sessionPtr
=
olm_outbound_group_session
(
sessionPtr
);
LOGD
(
"## initNewSessionJni(): success - outbound group session size=%lu"
,
sessionSize
);
LOGD
(
"
## initNewSessionJni(): success - outbound group session size=%lu"
,
sessionSize
);
}
else
{
LOGE
(
"## initNewSessionJni(): failure - outbound group session OOM"
);
LOGE
(
"
## initNewSessionJni(): failure - outbound group session OOM"
);
}
return
(
jlong
)(
intptr_t
)
sessionPtr
;
...
...
@@ -85,33 +90,33 @@ JNIEXPORT jint OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(initOutboundGroupSessionJni)(
if
(
NULL
==
(
sessionPtr
=
(
OlmOutboundGroupSession
*
)
getOutboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"
);
LOGE
(
"
## initOutboundGroupSessionJni(): failure - invalid outbound group session instance"
);
}
else
{
// compute random buffer
size_t
randomLength
=
olm_init_outbound_group_session_random_length
(
sessionPtr
);
LOGW
(
"## initOutboundGroupSessionJni(): randomLength=%lu"
,
randomLength
);
LOGW
(
"
## initOutboundGroupSessionJni(): randomLength=%lu"
,
randomLength
);
if
((
0
!=
randomLength
)
&&
!
setRandomInBuffer
(
&
randomBuffPtr
,
randomLength
))
{
LOGE
(
"## initOutboundGroupSessionJni(): failure - random buffer init"
);
LOGE
(
"
## initOutboundGroupSessionJni(): failure - random buffer init"
);
}
else
{
if
(
0
==
randomLength
)
{
LOGW
(
"## initOutboundGroupSessionJni(): random buffer is not required"
);
LOGW
(
"
## initOutboundGroupSessionJni(): random buffer is not required"
);
}
size_t
sessionResult
=
olm_init_outbound_group_session
(
sessionPtr
,
randomBuffPtr
,
randomLength
);
if
(
sessionResult
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_outbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## initOutboundGroupSessionJni(): failure - init outbound session creation Msg=%s"
,
errorMsgPtr
);
}
else
{
retCode
=
ERROR_CODE_OK
;
LOGD
(
"## initOutboundGroupSessionJni(): success - result=%lu"
,
sessionResult
);
LOGD
(
"
## initOutboundGroupSessionJni(): success - result=%lu"
,
sessionResult
);
}
}
}
...
...
@@ -137,17 +142,17 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE
if
(
NULL
==
(
sessionPtr
=
(
OlmOutboundGroupSession
*
)
getOutboundGroupSessionInstanceId
(
env
,
thiz
)))
{
LOGE
(
"## sessionIdentifierJni(): failure - invalid outbound group session instance"
);
LOGE
(
"
## sessionIdentifierJni(): failure - 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"
,
lengthSessionId
);
LOGD
(
"
## sessionIdentifierJni(): outbound group session lengthSessionId=%lu"
,
lengthSessionId
);
if
(
NULL
==
(
sessionIdPtr
=
(
uint8_t
*
)
malloc
(
lengthSessionId
*
sizeof
(
uint8_t
))))
{
LOGE
(
"## sessionIdentifierJni(): failure - outbound identifier allocation OOM"
);
LOGE
(
"
## sessionIdentifierJni(): failure - outbound identifier allocation OOM"
);
}
else
{
...
...
@@ -155,13 +160,13 @@ JNIEXPORT jstring OLM_OUTBOUND_GROUP_SESSION_FUNC_DEF(sessionIdentifierJni)(JNIE
if
(
result
==
olm_error
())
{
const
char
*
errorMsgPtr
=
olm_outbound_group_session_last_error
(
sessionPtr
);
LOGE
(
"## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s"
,
errorMsgPtr
);
LOGE
(
"
## sessionIdentifierJni(): failure - outbound group session identifier failure Msg=%s"
,
errorMsgPtr
);
}
else
{
// update length
sessionIdPtr
[
result
]
=
static_cast
<