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
c3eb050b
Commit
c3eb050b
authored
Dec 21, 2016
by
ylecollen
Browse files
signMessage : the utf8 conversion is done on Java side.
parent
e17eb690
Changes
3
Hide whitespace changes
Inline
Side-by-side
java/android/OlmLibSdk/olm-sdk/src/main/java/org/matrix/olm/OlmAccount.java
View file @
c3eb050b
...
...
@@ -383,9 +383,25 @@ public class OlmAccount extends CommonSerializeUtils implements Serializable {
* @return the signed message if operation succeed, null otherwise
*/
public
String
signMessage
(
String
aMessage
)
{
return
signMessageJni
(
aMessage
);
if
(
null
==
aMessage
)
{
return
null
;
}
byte
[]
utf8String
=
null
;
try
{
utf8String
=
aMessage
.
getBytes
(
"UTF-8"
);
}
catch
(
Exception
e
)
{
Log
.
d
(
LOG_TAG
,
"## signMessage(): failed ="
+
e
.
getMessage
());
}
if
(
null
==
utf8String
)
{
return
null
;
}
return
signMessageJni
(
utf8String
);
}
private
native
String
signMessageJni
(
String
aMessage
);
private
native
String
signMessageJni
(
byte
[]
aMessage
);
/**
* Return the number of unreleased OlmAccount instances.<br>
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.cpp
View file @
c3eb050b
...
...
@@ -393,7 +393,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env,
* @param aMessage message to sign
* @return the signed message, null otherwise
**/
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
signMessageJni
)(
JNIEnv
*
env
,
jobject
thiz
,
j
string
aMessage
)
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
signMessageJni
)(
JNIEnv
*
env
,
jobject
thiz
,
j
byteArray
aMessage
)
{
OlmAccount
*
accountPtr
=
NULL
;
size_t
signatureLength
;
...
...
@@ -411,48 +411,41 @@ JNIEXPORT jstring OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz
}
else
{
// convert message from JAVA to C string
const
char
*
messageToSign
=
env
->
GetStringUTFChars
(
aMessage
,
0
);
if
(
NULL
==
messageToSign
)
int
messageLength
=
env
->
GetArrayLength
(
aMessage
);
unsigned
char
*
messageToSign
=
new
unsigned
char
[
messageLength
];
env
->
GetByteArrayRegion
(
aMessage
,
0
,
messageLength
,
reinterpret_cast
<
jbyte
*>
(
messageToSign
));
// signature memory allocation
signatureLength
=
olm_account_signature_length
(
accountPtr
);
if
(
NULL
==
(
signedMsgPtr
=
(
void
*
)
malloc
((
signatureLength
+
1
)
*
sizeof
(
uint8_t
))))
{
LOGE
(
"## signMessageJni(): failure -
message JNI
allocation OOM"
);
LOGE
(
"## signMessageJni(): failure -
signature
allocation OOM"
);
}
else
{
int
messageLength
=
env
->
GetStringUTFLength
(
aMessage
);
// signature memory allocation
signatureLength
=
olm_account_signature_length
(
accountPtr
);
if
(
NULL
==
(
signedMsgPtr
=
(
void
*
)
malloc
((
signatureLength
+
1
)
*
sizeof
(
uint8_t
))))
{
// sign message
resultSign
=
olm_account_sign
(
accountPtr
,
(
void
*
)
messageToSign
,
(
size_t
)
messageLength
,
signedMsgPtr
,
signatureLength
);
if
(
resultSign
==
olm_error
())
{
LOGE
(
"## signMessageJni(): failure -
signature allocation OOM"
);
LOGE
(
"## signMessageJni(): failure -
error signing message Msg=%s"
,(
const
char
*
)
olm_account_last_error
(
accountPtr
)
);
}
else
{
// sign message
resultSign
=
olm_account_sign
(
accountPtr
,
(
void
*
)
messageToSign
,
(
size_t
)
messageLength
,
signedMsgPtr
,
signatureLength
);
if
(
resultSign
==
olm_error
())
{
LOGE
(
"## signMessageJni(): failure - error signing message Msg=%s"
,(
const
char
*
)
olm_account_last_error
(
accountPtr
));
}
else
{
// info: signatureLength is always equal to resultSign
(
static_cast
<
char
*>
(
signedMsgPtr
))[
signatureLength
]
=
static_cast
<
char
>
(
'\0'
);
// convert to jstring
signedMsgRetValue
=
env
->
NewStringUTF
((
const
char
*
)
signedMsgPtr
);
// UTF8
LOGD
(
"## signMessageJni(): success - retCode=%lu signatureLength=%lu"
,
static_cast
<
long
unsigned
int
>
(
resultSign
),
static_cast
<
long
unsigned
int
>
(
signatureLength
));
}
free
(
signedMsgPtr
);
{
// info: signatureLength is always equal to resultSign
(
static_cast
<
char
*>
(
signedMsgPtr
))[
signatureLength
]
=
static_cast
<
char
>
(
'\0'
);
// convert to jstring
signedMsgRetValue
=
env
->
NewStringUTF
((
const
char
*
)
signedMsgPtr
);
// UTF8
LOGD
(
"## signMessageJni(): success - retCode=%lu signatureLength=%lu"
,
static_cast
<
long
unsigned
int
>
(
resultSign
),
static_cast
<
long
unsigned
int
>
(
signatureLength
));
}
// release messageToSign
env
->
ReleaseStringUTFChars
(
aMessage
,
messageToSign
);
free
(
signedMsgPtr
);
}
// release messageToSign
free
(
messageToSign
);
}
return
signedMsgRetValue
;
...
...
java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_account.h
View file @
c3eb050b
...
...
@@ -43,7 +43,7 @@ JNIEXPORT jint OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysForSessionJni)(JNIEnv *env,
JNIEXPORT
jint
OLM_ACCOUNT_FUNC_DEF
(
markOneTimeKeysAsPublishedJni
)(
JNIEnv
*
env
,
jobject
thiz
);
// signing
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
signMessageJni
)(
JNIEnv
*
env
,
jobject
thiz
,
j
string
aMessage
);
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
signMessageJni
)(
JNIEnv
*
env
,
jobject
thiz
,
j
byteArray
aMessage
);
// serialization
JNIEXPORT
jstring
OLM_ACCOUNT_FUNC_DEF
(
serializeDataWithKeyJni
)(
JNIEnv
*
env
,
jobject
thiz
,
jstring
aKey
,
jobject
aErrorMsg
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment