ruby_olm 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/ruby_olm/ext_lib_olm/ext_account.c +274 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.c +51 -0
- data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.h +13 -0
- data/ext/ruby_olm/ext_lib_olm/ext_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/ext_utility.c +69 -0
- data/ext/ruby_olm/ext_lib_olm/extconf.rb +69 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.cpp +695 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.h +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +654 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni.h +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +224 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.cpp +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.h +36 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +563 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.cpp +716 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.h +48 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.cpp +977 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.h +59 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.cpp +236 -0
- data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.h +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decode_message.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decrypt.cpp +65 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_group_decrypt.cpp +73 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_account.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_session.cpp +14 -0
- data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/include/fuzzing.hh +82 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/account.hh +160 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.h +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.hh +63 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/cipher.h +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/crypto.h +202 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/error.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/inbound_group_session.h +235 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/list.hh +119 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/megolm.h +95 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.hh +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.h +93 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.hh +138 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.h +451 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.hh +4 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/outbound_group_session.h +181 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.h +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.hh +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle_encoding.h +76 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pk.h +214 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/ratchet.hh +184 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/session.hh +156 -0
- data/ext/ruby_olm/ext_lib_olm/olm/include/olm/utility.hh +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.h +123 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes_test.c +276 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.c +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.h +30 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour_test.c +47 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.c +135 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.h +27 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64_test.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish_test.c +68 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.c +269 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des_test.c +83 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.c +104 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.h +33 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.c +189 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5_test.c +60 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.c +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.h +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13_test.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.c +149 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.h +35 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1_test.c +58 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.h +34 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256_test.c +61 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.c +118 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.h +53 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna-c64.c +449 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/python-src/curve25519/curve25519module.c +105 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/speed-curve25519.c +50 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-curve25519.c +54 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-noncanon.c +39 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-sc-curve25519.c +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna.h +18 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/add_scalar.c +56 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ed25519.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.c +1493 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.h +41 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fixedint.h +72 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.c +467 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.h +74 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/key_exchange.c +79 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/keypair.c +16 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/precomp_data.h +1391 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.c +814 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.h +12 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/seed.c +40 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.c +275 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.h +21 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sign.c +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/verify.c +77 -0
- data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/test.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stddef.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stdint.h +0 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/session.cpp +462 -0
- data/ext/ruby_olm/ext_lib_olm/olm/src/utility.cpp +57 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/include/unittest.hh +107 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_base64.cpp +70 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_crypto.cpp +246 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_group_session.cpp +329 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_list.cpp +92 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_megolm.cpp +134 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_message.cpp +112 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm.cpp +405 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_decrypt.cpp +90 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_sha256.cpp +20 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_signature.cpp +81 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_using_malloc.cpp +210 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_pk.cpp +166 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_ratchet.cpp +221 -0
- data/ext/ruby_olm/ext_lib_olm/olm/tests/test_session.cpp +144 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount.h +51 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount_Private.h +25 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMInboundGroupSession.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMKit.h +37 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMMessage.h +38 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMOutboundGroupSession.h +32 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkDecryption.h +71 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkEncryption.h +42 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkMessage.h +31 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSerializable.h +29 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession.h +44 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession_Private.h +26 -0
- data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMUtility.h +49 -0
- data/ext/ruby_olm/ext_lib_olm/staging/account.cpp +380 -0
- data/ext/ruby_olm/ext_lib_olm/staging/aes.c +1073 -0
- data/ext/ruby_olm/ext_lib_olm/staging/base64.cpp +167 -0
- data/ext/ruby_olm/ext_lib_olm/staging/cipher.cpp +152 -0
- data/ext/ruby_olm/ext_lib_olm/staging/crypto.cpp +299 -0
- data/ext/ruby_olm/ext_lib_olm/staging/curve25519-donna.c +860 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ed25519.c +22 -0
- data/ext/ruby_olm/ext_lib_olm/staging/error.c +44 -0
- data/ext/ruby_olm/ext_lib_olm/staging/inbound_group_session.c +524 -0
- data/ext/ruby_olm/ext_lib_olm/staging/megolm.c +150 -0
- data/ext/ruby_olm/ext_lib_olm/staging/memory.cpp +45 -0
- data/ext/ruby_olm/ext_lib_olm/staging/message.cpp +401 -0
- data/ext/ruby_olm/ext_lib_olm/staging/olm.cpp +738 -0
- data/ext/ruby_olm/ext_lib_olm/staging/outbound_group_session.c +363 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle.cpp +242 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pickle_encoding.c +92 -0
- data/ext/ruby_olm/ext_lib_olm/staging/pk.cpp +412 -0
- data/ext/ruby_olm/ext_lib_olm/staging/ratchet.cpp +625 -0
- data/ext/ruby_olm/ext_lib_olm/staging/session.cpp +461 -0
- data/ext/ruby_olm/ext_lib_olm/staging/sha256.c +159 -0
- data/ext/ruby_olm/ext_lib_olm/staging/utility.cpp +57 -0
- data/lib/ruby_olm/account.rb +42 -0
- data/lib/ruby_olm/message.rb +6 -0
- data/lib/ruby_olm/olm_error.rb +70 -0
- data/lib/ruby_olm/olm_message.rb +25 -0
- data/lib/ruby_olm/pre_key_message.rb +6 -0
- data/lib/ruby_olm/session.rb +16 -0
- data/lib/ruby_olm/version.rb +5 -0
- data/lib/ruby_olm.rb +10 -0
- data/rakefile +18 -0
- data/test/examples/test_bob_no_answer.rb +62 -0
- data/test/examples/test_exchange.rb +60 -0
- data/test/spec/test_account.rb +152 -0
- data/test/unit/test_account_methods.rb +85 -0
- metadata +282 -0
@@ -0,0 +1,695 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright 2016 OpenMarket Ltd
|
3
|
+
* Copyright 2016 Vector Creations Ltd
|
4
|
+
*
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
* you may not use this file except in compliance with the License.
|
7
|
+
* You may obtain a copy of the License at
|
8
|
+
*
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
*
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
* See the License for the specific language governing permissions and
|
15
|
+
* limitations under the License.
|
16
|
+
*/
|
17
|
+
|
18
|
+
#include "olm_account.h"
|
19
|
+
|
20
|
+
using namespace AndroidOlmSdk;
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Init memory allocation for account creation.
|
24
|
+
* @return valid memory allocation, NULL otherwise
|
25
|
+
**/
|
26
|
+
OlmAccount* initializeAccountMemory()
|
27
|
+
{
|
28
|
+
size_t accountSize = olm_account_size();
|
29
|
+
OlmAccount* accountPtr = (OlmAccount*)malloc(accountSize);
|
30
|
+
|
31
|
+
if (accountPtr)
|
32
|
+
{
|
33
|
+
// init account object
|
34
|
+
accountPtr = olm_account(accountPtr);
|
35
|
+
LOGD("## initializeAccountMemory(): success - OLM account size=%lu",static_cast<long unsigned int>(accountSize));
|
36
|
+
}
|
37
|
+
else
|
38
|
+
{
|
39
|
+
LOGE("## initializeAccountMemory(): failure - OOM");
|
40
|
+
}
|
41
|
+
|
42
|
+
return accountPtr;
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Create a new account and return it to JAVA side.<br>
|
47
|
+
* Since a C prt is returned as a jlong, special care will be taken
|
48
|
+
* to make the cast (OlmAccount* => jlong) platform independent.
|
49
|
+
* @return the initialized OlmAccount* instance or throw an exception if fails
|
50
|
+
**/
|
51
|
+
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(createNewAccountJni)(JNIEnv *env, jobject thiz)
|
52
|
+
{
|
53
|
+
const char* errorMessage = NULL;
|
54
|
+
OlmAccount *accountPtr = initializeAccountMemory();
|
55
|
+
|
56
|
+
// init account memory allocation
|
57
|
+
if (!accountPtr)
|
58
|
+
{
|
59
|
+
LOGE("## initNewAccount(): failure - init account OOM");
|
60
|
+
errorMessage = "init account OOM";
|
61
|
+
}
|
62
|
+
else
|
63
|
+
{
|
64
|
+
// get random buffer size
|
65
|
+
size_t randomSize = olm_create_account_random_length(accountPtr);
|
66
|
+
|
67
|
+
LOGD("## initNewAccount(): randomSize=%lu", static_cast<long unsigned int>(randomSize));
|
68
|
+
|
69
|
+
uint8_t *randomBuffPtr = NULL;
|
70
|
+
size_t accountRetCode;
|
71
|
+
|
72
|
+
// allocate random buffer
|
73
|
+
if ((0 != randomSize) && !setRandomInBuffer(env, &randomBuffPtr, randomSize))
|
74
|
+
{
|
75
|
+
LOGE("## initNewAccount(): failure - random buffer init");
|
76
|
+
errorMessage = "random buffer init";
|
77
|
+
}
|
78
|
+
else
|
79
|
+
{
|
80
|
+
// create account
|
81
|
+
accountRetCode = olm_create_account(accountPtr, (void*)randomBuffPtr, randomSize);
|
82
|
+
|
83
|
+
if (accountRetCode == olm_error())
|
84
|
+
{
|
85
|
+
LOGE("## initNewAccount(): failure - account creation failed Msg=%s", olm_account_last_error(accountPtr));
|
86
|
+
errorMessage = olm_account_last_error(accountPtr);
|
87
|
+
}
|
88
|
+
|
89
|
+
LOGD("## initNewAccount(): success - OLM account created");
|
90
|
+
LOGD("## initNewAccount(): success - accountPtr=%p (jlong)(intptr_t)accountPtr=%lld",accountPtr,(jlong)(intptr_t)accountPtr);
|
91
|
+
}
|
92
|
+
|
93
|
+
if (randomBuffPtr)
|
94
|
+
{
|
95
|
+
memset(randomBuffPtr, 0, randomSize);
|
96
|
+
free(randomBuffPtr);
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
if (errorMessage)
|
101
|
+
{
|
102
|
+
// release the allocated data
|
103
|
+
if (accountPtr)
|
104
|
+
{
|
105
|
+
olm_clear_account(accountPtr);
|
106
|
+
free(accountPtr);
|
107
|
+
}
|
108
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
109
|
+
}
|
110
|
+
|
111
|
+
return (jlong)(intptr_t)accountPtr;
|
112
|
+
}
|
113
|
+
/**
|
114
|
+
* Release the account allocation made by initializeAccountMemory().<br>
|
115
|
+
* This method MUST be called when java counter part account instance is done.
|
116
|
+
*/
|
117
|
+
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(releaseAccountJni)(JNIEnv *env, jobject thiz)
|
118
|
+
{
|
119
|
+
LOGD("## releaseAccountJni(): IN");
|
120
|
+
|
121
|
+
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
|
122
|
+
|
123
|
+
if (!accountPtr)
|
124
|
+
{
|
125
|
+
LOGE(" ## releaseAccountJni(): failure - invalid Account ptr=NULL");
|
126
|
+
}
|
127
|
+
else
|
128
|
+
{
|
129
|
+
LOGD(" ## releaseAccountJni(): accountPtr=%p",accountPtr);
|
130
|
+
olm_clear_account(accountPtr);
|
131
|
+
|
132
|
+
LOGD(" ## releaseAccountJni(): IN");
|
133
|
+
// even if free(NULL) does not crash, logs are performed for debug purpose
|
134
|
+
free(accountPtr);
|
135
|
+
LOGD(" ## releaseAccountJni(): OUT");
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
// *********************************************************************
|
140
|
+
// ************************* IDENTITY KEYS API *************************
|
141
|
+
// *********************************************************************
|
142
|
+
|
143
|
+
/**
|
144
|
+
* Get identity keys: Ed25519 fingerprint key and Curve25519 identity key.<br>
|
145
|
+
* The keys are returned in the byte array.
|
146
|
+
* @return the identity keys or throw an exception if it fails
|
147
|
+
**/
|
148
|
+
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(identityKeysJni)(JNIEnv *env, jobject thiz)
|
149
|
+
{
|
150
|
+
const char* errorMessage = NULL;
|
151
|
+
jbyteArray byteArrayRetValue = NULL;
|
152
|
+
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
|
153
|
+
|
154
|
+
if (!accountPtr)
|
155
|
+
{
|
156
|
+
LOGE("## identityKeys(): failure - invalid Account ptr=NULL");
|
157
|
+
errorMessage = "invalid Account ptr";
|
158
|
+
}
|
159
|
+
else
|
160
|
+
{
|
161
|
+
LOGD("## identityKeys(): accountPtr =%p", accountPtr);
|
162
|
+
|
163
|
+
// identity keys allocation
|
164
|
+
size_t identityKeysLength = olm_account_identity_keys_length(accountPtr);
|
165
|
+
uint8_t *identityKeysBytesPtr = (uint8_t*)malloc(identityKeysLength);
|
166
|
+
|
167
|
+
if (!identityKeysBytesPtr)
|
168
|
+
{
|
169
|
+
LOGE("## identityKeys(): failure - identity keys array OOM");
|
170
|
+
errorMessage = "identity keys array OOM";
|
171
|
+
}
|
172
|
+
else
|
173
|
+
{
|
174
|
+
// retrieve key pairs in identityKeysBytesPtr
|
175
|
+
size_t keysResult = olm_account_identity_keys(accountPtr, identityKeysBytesPtr, identityKeysLength);
|
176
|
+
|
177
|
+
if (keysResult == olm_error())
|
178
|
+
{
|
179
|
+
errorMessage = (const char *)olm_account_last_error(accountPtr);
|
180
|
+
LOGE("## identityKeys(): failure - error getting identity keys Msg=%s", errorMessage);
|
181
|
+
}
|
182
|
+
else
|
183
|
+
{
|
184
|
+
// allocate the byte array to be returned to java
|
185
|
+
byteArrayRetValue = env->NewByteArray(identityKeysLength);
|
186
|
+
|
187
|
+
if (!byteArrayRetValue)
|
188
|
+
{
|
189
|
+
LOGE("## identityKeys(): failure - return byte array OOM");
|
190
|
+
errorMessage = "byte array OOM";
|
191
|
+
}
|
192
|
+
else
|
193
|
+
{
|
194
|
+
env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, identityKeysLength, (const jbyte*)identityKeysBytesPtr);
|
195
|
+
LOGD("## identityKeys(): success - result=%lu", static_cast<long unsigned int>(keysResult));
|
196
|
+
}
|
197
|
+
}
|
198
|
+
|
199
|
+
free(identityKeysBytesPtr);
|
200
|
+
}
|
201
|
+
}
|
202
|
+
|
203
|
+
if (errorMessage)
|
204
|
+
{
|
205
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
206
|
+
}
|
207
|
+
|
208
|
+
return byteArrayRetValue;
|
209
|
+
}
|
210
|
+
|
211
|
+
// *********************************************************************
|
212
|
+
// ************************* ONE TIME KEYS API *************************
|
213
|
+
// *********************************************************************
|
214
|
+
|
215
|
+
/**
|
216
|
+
* Get the public parts of the unpublished "one time keys" for the account.<br>
|
217
|
+
* The returned data is a JSON-formatted object with the single property
|
218
|
+
* <tt>curve25519</tt>, which is itself an object mapping key id to
|
219
|
+
* base64-encoded Curve25519 key.<br>
|
220
|
+
* @return byte array containing the one time keys or throw an exception if it fails
|
221
|
+
*/
|
222
|
+
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(maxOneTimeKeysJni)(JNIEnv *env, jobject thiz)
|
223
|
+
{
|
224
|
+
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
|
225
|
+
size_t maxKeys = -1;
|
226
|
+
|
227
|
+
if (!accountPtr)
|
228
|
+
{
|
229
|
+
LOGE("## maxOneTimeKey(): failure - invalid Account ptr=NULL");
|
230
|
+
}
|
231
|
+
else
|
232
|
+
{
|
233
|
+
maxKeys = olm_account_max_number_of_one_time_keys(accountPtr);
|
234
|
+
}
|
235
|
+
|
236
|
+
LOGD("## maxOneTimeKey(): Max keys=%lu", static_cast<long unsigned int>(maxKeys));
|
237
|
+
|
238
|
+
return (jlong)maxKeys;
|
239
|
+
}
|
240
|
+
|
241
|
+
/**
|
242
|
+
* Generate "one time keys".
|
243
|
+
* An exception is thrown if the operation fails.
|
244
|
+
* @param aNumberOfKeys number of keys to generate
|
245
|
+
**/
|
246
|
+
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(generateOneTimeKeysJni)(JNIEnv *env, jobject thiz, jint aNumberOfKeys)
|
247
|
+
{
|
248
|
+
const char* errorMessage = NULL;
|
249
|
+
OlmAccount *accountPtr = getAccountInstanceId(env, thiz);
|
250
|
+
|
251
|
+
if (!accountPtr)
|
252
|
+
{
|
253
|
+
LOGE("## generateOneTimeKeysJni(): failure - invalid Account ptr");
|
254
|
+
errorMessage = "invalid Account ptr";
|
255
|
+
}
|
256
|
+
else
|
257
|
+
{
|
258
|
+
// keys memory allocation
|
259
|
+
size_t randomLength = olm_account_generate_one_time_keys_random_length(accountPtr, (size_t)aNumberOfKeys);
|
260
|
+
LOGD("## generateOneTimeKeysJni(): randomLength=%lu", static_cast<long unsigned int>(randomLength));
|
261
|
+
|
262
|
+
uint8_t *randomBufferPtr = NULL;
|
263
|
+
|
264
|
+
if ((0 != randomLength) && !setRandomInBuffer(env, &randomBufferPtr, randomLength))
|
265
|
+
{
|
266
|
+
LOGE("## generateOneTimeKeysJni(): failure - random buffer init");
|
267
|
+
errorMessage = "random buffer init";
|
268
|
+
}
|
269
|
+
else
|
270
|
+
{
|
271
|
+
LOGD("## generateOneTimeKeysJni(): accountPtr =%p aNumberOfKeys=%d",accountPtr, aNumberOfKeys);
|
272
|
+
|
273
|
+
// retrieve key pairs in keysBytesPtr
|
274
|
+
size_t result = olm_account_generate_one_time_keys(accountPtr, (size_t)aNumberOfKeys, (void*)randomBufferPtr, randomLength);
|
275
|
+
|
276
|
+
if (result == olm_error())
|
277
|
+
{
|
278
|
+
errorMessage = olm_account_last_error(accountPtr);
|
279
|
+
LOGE("## generateOneTimeKeysJni(): failure - error generating one time keys Msg=%s", errorMessage);
|
280
|
+
}
|
281
|
+
else
|
282
|
+
{
|
283
|
+
LOGD("## generateOneTimeKeysJni(): success - result=%lu", static_cast<long unsigned int>(result));
|
284
|
+
}
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
if (randomBufferPtr)
|
289
|
+
{
|
290
|
+
memset(randomBufferPtr, 0, randomLength);
|
291
|
+
free(randomBufferPtr);
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
if (errorMessage)
|
296
|
+
{
|
297
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
298
|
+
}
|
299
|
+
}
|
300
|
+
|
301
|
+
/**
|
302
|
+
* Get "one time keys".<br>
|
303
|
+
* Return the public parts of the unpublished "one time keys" for the account
|
304
|
+
* @return a valid byte array if operation succeed, null otherwise
|
305
|
+
**/
|
306
|
+
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(oneTimeKeysJni)(JNIEnv *env, jobject thiz)
|
307
|
+
{
|
308
|
+
const char* errorMessage = NULL;
|
309
|
+
jbyteArray byteArrayRetValue = NULL;
|
310
|
+
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
|
311
|
+
|
312
|
+
LOGD("## oneTimeKeysJni(): IN");
|
313
|
+
|
314
|
+
if (!accountPtr)
|
315
|
+
{
|
316
|
+
LOGE("## oneTimeKeysJni(): failure - invalid Account ptr");
|
317
|
+
errorMessage = "invalid Account ptr";
|
318
|
+
}
|
319
|
+
else
|
320
|
+
{
|
321
|
+
// keys memory allocation
|
322
|
+
size_t keysLength = olm_account_one_time_keys_length(accountPtr);
|
323
|
+
uint8_t *keysBytesPtr = (uint8_t *)malloc(keysLength*sizeof(uint8_t));
|
324
|
+
|
325
|
+
if (!keysBytesPtr)
|
326
|
+
{
|
327
|
+
LOGE("## oneTimeKeysJni(): failure - one time keys array OOM");
|
328
|
+
errorMessage = "one time keys array OOM";
|
329
|
+
}
|
330
|
+
else
|
331
|
+
{
|
332
|
+
// retrieve key pairs in keysBytesPtr
|
333
|
+
size_t keysResult = olm_account_one_time_keys(accountPtr, keysBytesPtr, keysLength);
|
334
|
+
|
335
|
+
if (keysResult == olm_error()) {
|
336
|
+
LOGE("## oneTimeKeysJni(): failure - error getting one time keys Msg=%s",(const char *)olm_account_last_error(accountPtr));
|
337
|
+
errorMessage = (const char *)olm_account_last_error(accountPtr);
|
338
|
+
}
|
339
|
+
else
|
340
|
+
{
|
341
|
+
// allocate the byte array to be returned to java
|
342
|
+
byteArrayRetValue = env->NewByteArray(keysLength);
|
343
|
+
|
344
|
+
if (!byteArrayRetValue)
|
345
|
+
{
|
346
|
+
LOGE("## oneTimeKeysJni(): failure - return byte array OOM");
|
347
|
+
errorMessage = "return byte array OOM";
|
348
|
+
}
|
349
|
+
else
|
350
|
+
{
|
351
|
+
env->SetByteArrayRegion(byteArrayRetValue, 0/*offset*/, keysLength, (const jbyte*)keysBytesPtr);
|
352
|
+
LOGD("## oneTimeKeysJni(): success");
|
353
|
+
}
|
354
|
+
}
|
355
|
+
|
356
|
+
free(keysBytesPtr);
|
357
|
+
}
|
358
|
+
}
|
359
|
+
|
360
|
+
if (errorMessage)
|
361
|
+
{
|
362
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
363
|
+
}
|
364
|
+
|
365
|
+
return byteArrayRetValue;
|
366
|
+
}
|
367
|
+
|
368
|
+
/**
|
369
|
+
* Remove the "one time keys" that the session used from the account.
|
370
|
+
* An exception is thrown if the operation fails.
|
371
|
+
* @param aNativeOlmSessionId session instance
|
372
|
+
**/
|
373
|
+
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(removeOneTimeKeysJni)(JNIEnv *env, jobject thiz, jlong aNativeOlmSessionId)
|
374
|
+
{
|
375
|
+
const char* errorMessage = NULL;
|
376
|
+
OlmAccount* accountPtr = NULL;
|
377
|
+
OlmSession* sessionPtr = (OlmSession*)aNativeOlmSessionId;
|
378
|
+
|
379
|
+
if (!sessionPtr)
|
380
|
+
{
|
381
|
+
LOGE("## removeOneTimeKeysJni(): failure - invalid session ptr");
|
382
|
+
errorMessage = "invalid session ptr";
|
383
|
+
}
|
384
|
+
else if (!(accountPtr = getAccountInstanceId(env, thiz)))
|
385
|
+
{
|
386
|
+
LOGE("## removeOneTimeKeysJni(): failure - invalid account ptr");
|
387
|
+
errorMessage = "invalid account ptr";
|
388
|
+
}
|
389
|
+
else
|
390
|
+
{
|
391
|
+
size_t result = olm_remove_one_time_keys(accountPtr, sessionPtr);
|
392
|
+
|
393
|
+
if (result == olm_error())
|
394
|
+
{ // the account doesn't have any matching "one time keys"..
|
395
|
+
LOGW("## removeOneTimeKeysJni(): failure - removing one time keys Msg=%s", olm_account_last_error(accountPtr));
|
396
|
+
errorMessage = (const char *)olm_account_last_error(accountPtr);
|
397
|
+
}
|
398
|
+
else
|
399
|
+
{
|
400
|
+
LOGD("## removeOneTimeKeysJni(): success");
|
401
|
+
}
|
402
|
+
}
|
403
|
+
|
404
|
+
if (errorMessage)
|
405
|
+
{
|
406
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
407
|
+
}
|
408
|
+
}
|
409
|
+
|
410
|
+
/**
|
411
|
+
* Mark the current set of "one time keys" as being published.
|
412
|
+
* An exception is thrown if the operation fails.
|
413
|
+
**/
|
414
|
+
JNIEXPORT void OLM_ACCOUNT_FUNC_DEF(markOneTimeKeysAsPublishedJni)(JNIEnv *env, jobject thiz)
|
415
|
+
{
|
416
|
+
const char* errorMessage = NULL;
|
417
|
+
OlmAccount* accountPtr = getAccountInstanceId(env, thiz);
|
418
|
+
|
419
|
+
if (!accountPtr)
|
420
|
+
{
|
421
|
+
LOGE("## markOneTimeKeysAsPublishedJni(): failure - invalid account ptr");
|
422
|
+
errorMessage = "invalid account ptr";
|
423
|
+
}
|
424
|
+
else
|
425
|
+
{
|
426
|
+
size_t result = olm_account_mark_keys_as_published(accountPtr);
|
427
|
+
|
428
|
+
if (result == olm_error())
|
429
|
+
{
|
430
|
+
LOGW("## markOneTimeKeysAsPublishedJni(): failure - Msg=%s",(const char *)olm_account_last_error(accountPtr));
|
431
|
+
errorMessage = (const char *)olm_account_last_error(accountPtr);
|
432
|
+
}
|
433
|
+
else
|
434
|
+
{
|
435
|
+
LOGD("## markOneTimeKeysAsPublishedJni(): success - retCode=%lu",static_cast<long unsigned int>(result));
|
436
|
+
}
|
437
|
+
}
|
438
|
+
|
439
|
+
if (errorMessage)
|
440
|
+
{
|
441
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
442
|
+
}
|
443
|
+
}
|
444
|
+
|
445
|
+
/**
|
446
|
+
* Sign a message with the ed25519 key (fingerprint) for this account.<br>
|
447
|
+
* The signed message is returned by the function.
|
448
|
+
* @param aMessage message to sign
|
449
|
+
* @return the signed message, null otherwise
|
450
|
+
**/
|
451
|
+
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(signMessageJni)(JNIEnv *env, jobject thiz, jbyteArray aMessage)
|
452
|
+
{
|
453
|
+
const char* errorMessage = NULL;
|
454
|
+
OlmAccount* accountPtr = NULL;
|
455
|
+
jbyteArray signedMsgRetValueBuffer = NULL;
|
456
|
+
|
457
|
+
if (!aMessage)
|
458
|
+
{
|
459
|
+
LOGE("## signMessageJni(): failure - invalid aMessage param");
|
460
|
+
errorMessage = "invalid aMessage param";
|
461
|
+
}
|
462
|
+
else if (!(accountPtr = getAccountInstanceId(env, thiz)))
|
463
|
+
{
|
464
|
+
LOGE("## signMessageJni(): failure - invalid account ptr");
|
465
|
+
errorMessage = "invalid account ptr";
|
466
|
+
}
|
467
|
+
else
|
468
|
+
{
|
469
|
+
int messageLength = env->GetArrayLength(aMessage);
|
470
|
+
jbyte* messageToSign = env->GetByteArrayElements(aMessage, NULL);
|
471
|
+
|
472
|
+
// signature memory allocation
|
473
|
+
size_t signatureLength = olm_account_signature_length(accountPtr);
|
474
|
+
void* signedMsgPtr = malloc(signatureLength * sizeof(uint8_t));
|
475
|
+
|
476
|
+
if (!signedMsgPtr)
|
477
|
+
{
|
478
|
+
LOGE("## signMessageJni(): failure - signature allocation OOM");
|
479
|
+
errorMessage = "signature allocation OOM";
|
480
|
+
}
|
481
|
+
else
|
482
|
+
{
|
483
|
+
// sign message
|
484
|
+
size_t resultSign = olm_account_sign(accountPtr,
|
485
|
+
(void*)messageToSign,
|
486
|
+
(size_t)messageLength,
|
487
|
+
signedMsgPtr,
|
488
|
+
signatureLength);
|
489
|
+
|
490
|
+
if (resultSign == olm_error())
|
491
|
+
{
|
492
|
+
LOGE("## signMessageJni(): failure - error signing message Msg=%s",(const char *)olm_account_last_error(accountPtr));
|
493
|
+
errorMessage = (const char *)olm_account_last_error(accountPtr);
|
494
|
+
}
|
495
|
+
else
|
496
|
+
{
|
497
|
+
LOGD("## signMessageJni(): success - retCode=%lu signatureLength=%lu", static_cast<long unsigned int>(resultSign), static_cast<long unsigned int>(signatureLength));
|
498
|
+
|
499
|
+
signedMsgRetValueBuffer = env->NewByteArray(signatureLength);
|
500
|
+
env->SetByteArrayRegion(signedMsgRetValueBuffer, 0 , signatureLength, (jbyte*)signedMsgPtr);
|
501
|
+
}
|
502
|
+
|
503
|
+
free(signedMsgPtr);
|
504
|
+
}
|
505
|
+
|
506
|
+
// release messageToSign
|
507
|
+
if (messageToSign)
|
508
|
+
{
|
509
|
+
env->ReleaseByteArrayElements(aMessage, messageToSign, JNI_ABORT);
|
510
|
+
}
|
511
|
+
}
|
512
|
+
|
513
|
+
if (errorMessage)
|
514
|
+
{
|
515
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
516
|
+
}
|
517
|
+
|
518
|
+
return signedMsgRetValueBuffer;
|
519
|
+
}
|
520
|
+
|
521
|
+
/**
|
522
|
+
* Serialize and encrypt account instance.<br>
|
523
|
+
* @param aKeyBuffer key used to encrypt the serialized account data
|
524
|
+
* @return the serialised account as bytes buffer.
|
525
|
+
**/
|
526
|
+
JNIEXPORT jbyteArray OLM_ACCOUNT_FUNC_DEF(serializeJni)(JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer)
|
527
|
+
{
|
528
|
+
const char* errorMessage = NULL;
|
529
|
+
jbyteArray pickledDataRetValue = 0;
|
530
|
+
jbyte* keyPtr = NULL;
|
531
|
+
jboolean keyIsCopied = JNI_FALSE;
|
532
|
+
OlmAccount* accountPtr = NULL;
|
533
|
+
|
534
|
+
LOGD("## serializeJni(): IN");
|
535
|
+
|
536
|
+
if (!aKeyBuffer)
|
537
|
+
{
|
538
|
+
LOGE(" ## serializeJni(): failure - invalid key");
|
539
|
+
errorMessage = "invalid key";
|
540
|
+
}
|
541
|
+
else if (!(accountPtr = getAccountInstanceId(env, thiz)))
|
542
|
+
{
|
543
|
+
LOGE(" ## serializeJni(): failure - invalid account ptr");
|
544
|
+
errorMessage = "invalid account ptr";
|
545
|
+
}
|
546
|
+
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyIsCopied)))
|
547
|
+
{
|
548
|
+
LOGE(" ## serializeJni(): failure - keyPtr JNI allocation OOM");
|
549
|
+
errorMessage = "keyPtr JNI allocation OOM";
|
550
|
+
}
|
551
|
+
else
|
552
|
+
{
|
553
|
+
size_t pickledLength = olm_pickle_account_length(accountPtr);
|
554
|
+
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
|
555
|
+
LOGD(" ## serializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
|
556
|
+
|
557
|
+
void* pickledPtr = malloc(pickledLength * sizeof(uint8_t));
|
558
|
+
|
559
|
+
if (!pickledPtr)
|
560
|
+
{
|
561
|
+
LOGE(" ## serializeJni(): failure - pickledPtr buffer OOM");
|
562
|
+
errorMessage = "pickledPtr buffer OOM";
|
563
|
+
}
|
564
|
+
else
|
565
|
+
{
|
566
|
+
size_t result = olm_pickle_account(accountPtr,
|
567
|
+
(void const *)keyPtr,
|
568
|
+
keyLength,
|
569
|
+
(void*)pickledPtr,
|
570
|
+
pickledLength);
|
571
|
+
if (result == olm_error())
|
572
|
+
{
|
573
|
+
errorMessage = olm_account_last_error(accountPtr);
|
574
|
+
LOGE(" ## serializeJni(): failure - olm_pickle_account() Msg=%s", errorMessage);
|
575
|
+
}
|
576
|
+
else
|
577
|
+
{
|
578
|
+
LOGD(" ## serializeJni(): success - result=%lu pickled=%.*s", static_cast<long unsigned int>(result), static_cast<int>(pickledLength), static_cast<char*>(pickledPtr));
|
579
|
+
pickledDataRetValue = env->NewByteArray(pickledLength);
|
580
|
+
env->SetByteArrayRegion(pickledDataRetValue, 0 , pickledLength, (jbyte*)pickledPtr);
|
581
|
+
}
|
582
|
+
|
583
|
+
free(pickledPtr);
|
584
|
+
}
|
585
|
+
}
|
586
|
+
|
587
|
+
// free alloc
|
588
|
+
if (keyPtr)
|
589
|
+
{
|
590
|
+
if (keyIsCopied) {
|
591
|
+
memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer));
|
592
|
+
}
|
593
|
+
env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
|
594
|
+
}
|
595
|
+
|
596
|
+
if (errorMessage)
|
597
|
+
{
|
598
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
599
|
+
}
|
600
|
+
|
601
|
+
return pickledDataRetValue;
|
602
|
+
}
|
603
|
+
|
604
|
+
/**
|
605
|
+
* Allocate a new account and initialise it with the serialisation data.<br>
|
606
|
+
* @param aSerializedDataBuffer the account serialisation buffer
|
607
|
+
* @param aKeyBuffer the key used to encrypt the serialized account data
|
608
|
+
* @return the deserialised account
|
609
|
+
**/
|
610
|
+
JNIEXPORT jlong OLM_ACCOUNT_FUNC_DEF(deserializeJni)(JNIEnv *env, jobject thiz, jbyteArray aSerializedDataBuffer, jbyteArray aKeyBuffer)
|
611
|
+
{
|
612
|
+
const char* errorMessage = NULL;
|
613
|
+
|
614
|
+
OlmAccount* accountPtr = NULL;
|
615
|
+
|
616
|
+
jbyte* keyPtr = NULL;
|
617
|
+
jboolean keyIsCopied = JNI_FALSE;
|
618
|
+
jbyte* pickledPtr = NULL;
|
619
|
+
|
620
|
+
LOGD("## deserializeJni(): IN");
|
621
|
+
|
622
|
+
if (!aKeyBuffer)
|
623
|
+
{
|
624
|
+
LOGE(" ## deserializeJni(): failure - invalid key");
|
625
|
+
errorMessage = "invalid key";
|
626
|
+
}
|
627
|
+
else if (!aSerializedDataBuffer)
|
628
|
+
{
|
629
|
+
LOGE(" ## deserializeJni(): failure - invalid serialized data");
|
630
|
+
errorMessage = "invalid serialized data";
|
631
|
+
}
|
632
|
+
else if (!(accountPtr = initializeAccountMemory()))
|
633
|
+
{
|
634
|
+
LOGE(" ## deserializeJni(): failure - account failure OOM");
|
635
|
+
errorMessage = "account failure OOM";
|
636
|
+
}
|
637
|
+
else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, &keyIsCopied)))
|
638
|
+
{
|
639
|
+
LOGE(" ## deserializeJni(): failure - keyPtr JNI allocation OOM");
|
640
|
+
errorMessage = "keyPtr JNI allocation OOM";
|
641
|
+
}
|
642
|
+
else if (!(pickledPtr = env->GetByteArrayElements(aSerializedDataBuffer, 0)))
|
643
|
+
{
|
644
|
+
LOGE(" ## deserializeJni(): failure - pickledPtr JNI allocation OOM");
|
645
|
+
errorMessage = "pickledPtr JNI allocation OOM";
|
646
|
+
}
|
647
|
+
else
|
648
|
+
{
|
649
|
+
size_t pickledLength = (size_t)env->GetArrayLength(aSerializedDataBuffer);
|
650
|
+
size_t keyLength = (size_t)env->GetArrayLength(aKeyBuffer);
|
651
|
+
LOGD(" ## deserializeJni(): pickledLength=%lu keyLength=%lu",static_cast<long unsigned int>(pickledLength), static_cast<long unsigned int>(keyLength));
|
652
|
+
LOGD(" ## deserializeJni(): pickled=%.*s", static_cast<int> (pickledLength), (char const *)pickledPtr);
|
653
|
+
|
654
|
+
size_t result = olm_unpickle_account(accountPtr,
|
655
|
+
(void const *)keyPtr,
|
656
|
+
keyLength,
|
657
|
+
(void*)pickledPtr,
|
658
|
+
pickledLength);
|
659
|
+
if (result == olm_error())
|
660
|
+
{
|
661
|
+
errorMessage = olm_account_last_error(accountPtr);
|
662
|
+
LOGE(" ## deserializeJni(): failure - olm_unpickle_account() Msg=%s", errorMessage);
|
663
|
+
}
|
664
|
+
else
|
665
|
+
{
|
666
|
+
LOGD(" ## deserializeJni(): success - result=%lu ", static_cast<long unsigned int>(result));
|
667
|
+
}
|
668
|
+
}
|
669
|
+
|
670
|
+
// free alloc
|
671
|
+
if (keyPtr)
|
672
|
+
{
|
673
|
+
if (keyIsCopied) {
|
674
|
+
memset(keyPtr, 0, (size_t)env->GetArrayLength(aKeyBuffer));
|
675
|
+
}
|
676
|
+
env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
|
677
|
+
}
|
678
|
+
|
679
|
+
if (pickledPtr)
|
680
|
+
{
|
681
|
+
env->ReleaseByteArrayElements(aSerializedDataBuffer, pickledPtr, JNI_ABORT);
|
682
|
+
}
|
683
|
+
|
684
|
+
if (errorMessage)
|
685
|
+
{
|
686
|
+
if (accountPtr)
|
687
|
+
{
|
688
|
+
olm_clear_account(accountPtr);
|
689
|
+
free(accountPtr);
|
690
|
+
}
|
691
|
+
env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
|
692
|
+
}
|
693
|
+
|
694
|
+
return (jlong)(intptr_t)accountPtr;
|
695
|
+
}
|