ruby_olm 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (194) hide show
  1. checksums.yaml +7 -0
  2. data/ext/ruby_olm/ext_lib_olm/ext_account.c +274 -0
  3. data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.c +51 -0
  4. data/ext/ruby_olm/ext_lib_olm/ext_lib_olm.h +13 -0
  5. data/ext/ruby_olm/ext_lib_olm/ext_session.c +363 -0
  6. data/ext/ruby_olm/ext_lib_olm/ext_utility.c +69 -0
  7. data/ext/ruby_olm/ext_lib_olm/extconf.rb +69 -0
  8. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.cpp +695 -0
  9. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_account.h +56 -0
  10. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.cpp +654 -0
  11. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_inbound_group_session.h +51 -0
  12. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni.h +81 -0
  13. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.cpp +224 -0
  14. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_jni_helper.h +30 -0
  15. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.cpp +35 -0
  16. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_manager.h +36 -0
  17. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.cpp +563 -0
  18. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_outbound_group_session.h +49 -0
  19. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.cpp +716 -0
  20. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_pk.h +48 -0
  21. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.cpp +977 -0
  22. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_session.h +59 -0
  23. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.cpp +236 -0
  24. data/ext/ruby_olm/ext_lib_olm/olm/android/olm-sdk/src/main/jni/olm_utility.h +40 -0
  25. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decode_message.cpp +14 -0
  26. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_decrypt.cpp +65 -0
  27. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_group_decrypt.cpp +73 -0
  28. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_account.cpp +14 -0
  29. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/fuzz_unpickle_session.cpp +14 -0
  30. data/ext/ruby_olm/ext_lib_olm/olm/fuzzers/include/fuzzing.hh +82 -0
  31. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/account.hh +160 -0
  32. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.h +77 -0
  33. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/base64.hh +63 -0
  34. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/cipher.h +138 -0
  35. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/crypto.h +202 -0
  36. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/error.h +72 -0
  37. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/inbound_group_session.h +235 -0
  38. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/list.hh +119 -0
  39. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/megolm.h +95 -0
  40. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.h +41 -0
  41. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/memory.hh +90 -0
  42. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.h +93 -0
  43. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/message.hh +138 -0
  44. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.h +451 -0
  45. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/olm.hh +4 -0
  46. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/outbound_group_session.h +181 -0
  47. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.h +90 -0
  48. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle.hh +149 -0
  49. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pickle_encoding.h +76 -0
  50. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/pk.h +214 -0
  51. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/ratchet.hh +184 -0
  52. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/session.hh +156 -0
  53. data/ext/ruby_olm/ext_lib_olm/olm/include/olm/utility.hh +61 -0
  54. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.c +1073 -0
  55. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes.h +123 -0
  56. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/aes_test.c +276 -0
  57. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.c +45 -0
  58. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour.h +30 -0
  59. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/arcfour_test.c +47 -0
  60. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.c +135 -0
  61. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64.h +27 -0
  62. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/base64_test.c +54 -0
  63. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.c +269 -0
  64. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish.h +32 -0
  65. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/blowfish_test.c +68 -0
  66. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.c +269 -0
  67. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des.h +37 -0
  68. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/des_test.c +83 -0
  69. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.c +104 -0
  70. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2.h +33 -0
  71. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md2_test.c +58 -0
  72. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.c +189 -0
  73. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5.h +34 -0
  74. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/md5_test.c +60 -0
  75. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.c +35 -0
  76. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13.h +20 -0
  77. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/rot-13_test.c +44 -0
  78. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.c +149 -0
  79. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1.h +35 -0
  80. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha1_test.c +58 -0
  81. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.c +159 -0
  82. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256.h +34 -0
  83. data/ext/ruby_olm/ext_lib_olm/olm/lib/crypto-algorithms/sha256_test.c +61 -0
  84. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.c +118 -0
  85. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/contrib/Curve25519Donna.h +53 -0
  86. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna-c64.c +449 -0
  87. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/curve25519-donna.c +860 -0
  88. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/python-src/curve25519/curve25519module.c +105 -0
  89. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/speed-curve25519.c +50 -0
  90. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-curve25519.c +54 -0
  91. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-noncanon.c +39 -0
  92. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna/test-sc-curve25519.c +72 -0
  93. data/ext/ruby_olm/ext_lib_olm/olm/lib/curve25519-donna.h +18 -0
  94. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/add_scalar.c +56 -0
  95. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ed25519.h +38 -0
  96. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.c +1493 -0
  97. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fe.h +41 -0
  98. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/fixedint.h +72 -0
  99. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.c +467 -0
  100. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/ge.h +74 -0
  101. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/key_exchange.c +79 -0
  102. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/keypair.c +16 -0
  103. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/precomp_data.h +1391 -0
  104. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.c +814 -0
  105. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sc.h +12 -0
  106. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/seed.c +40 -0
  107. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.c +275 -0
  108. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sha512.h +21 -0
  109. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/sign.c +31 -0
  110. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/src/verify.c +77 -0
  111. data/ext/ruby_olm/ext_lib_olm/olm/lib/ed25519/test.c +150 -0
  112. data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stddef.h +0 -0
  113. data/ext/ruby_olm/ext_lib_olm/olm/python/dummy/stdint.h +0 -0
  114. data/ext/ruby_olm/ext_lib_olm/olm/src/account.cpp +380 -0
  115. data/ext/ruby_olm/ext_lib_olm/olm/src/base64.cpp +167 -0
  116. data/ext/ruby_olm/ext_lib_olm/olm/src/cipher.cpp +152 -0
  117. data/ext/ruby_olm/ext_lib_olm/olm/src/crypto.cpp +299 -0
  118. data/ext/ruby_olm/ext_lib_olm/olm/src/ed25519.c +22 -0
  119. data/ext/ruby_olm/ext_lib_olm/olm/src/error.c +44 -0
  120. data/ext/ruby_olm/ext_lib_olm/olm/src/inbound_group_session.c +524 -0
  121. data/ext/ruby_olm/ext_lib_olm/olm/src/megolm.c +150 -0
  122. data/ext/ruby_olm/ext_lib_olm/olm/src/memory.cpp +45 -0
  123. data/ext/ruby_olm/ext_lib_olm/olm/src/message.cpp +401 -0
  124. data/ext/ruby_olm/ext_lib_olm/olm/src/olm.cpp +738 -0
  125. data/ext/ruby_olm/ext_lib_olm/olm/src/outbound_group_session.c +363 -0
  126. data/ext/ruby_olm/ext_lib_olm/olm/src/pickle.cpp +242 -0
  127. data/ext/ruby_olm/ext_lib_olm/olm/src/pickle_encoding.c +92 -0
  128. data/ext/ruby_olm/ext_lib_olm/olm/src/pk.cpp +412 -0
  129. data/ext/ruby_olm/ext_lib_olm/olm/src/ratchet.cpp +625 -0
  130. data/ext/ruby_olm/ext_lib_olm/olm/src/session.cpp +462 -0
  131. data/ext/ruby_olm/ext_lib_olm/olm/src/utility.cpp +57 -0
  132. data/ext/ruby_olm/ext_lib_olm/olm/tests/include/unittest.hh +107 -0
  133. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_base64.cpp +70 -0
  134. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_crypto.cpp +246 -0
  135. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_group_session.cpp +329 -0
  136. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_list.cpp +92 -0
  137. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_megolm.cpp +134 -0
  138. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_message.cpp +112 -0
  139. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm.cpp +405 -0
  140. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_decrypt.cpp +90 -0
  141. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_sha256.cpp +20 -0
  142. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_signature.cpp +81 -0
  143. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_olm_using_malloc.cpp +210 -0
  144. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_pk.cpp +166 -0
  145. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_ratchet.cpp +221 -0
  146. data/ext/ruby_olm/ext_lib_olm/olm/tests/test_session.cpp +144 -0
  147. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount.h +51 -0
  148. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMAccount_Private.h +25 -0
  149. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMInboundGroupSession.h +38 -0
  150. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMKit.h +37 -0
  151. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMMessage.h +38 -0
  152. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMOutboundGroupSession.h +32 -0
  153. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkDecryption.h +71 -0
  154. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkEncryption.h +42 -0
  155. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMPkMessage.h +31 -0
  156. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSerializable.h +29 -0
  157. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession.h +44 -0
  158. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMSession_Private.h +26 -0
  159. data/ext/ruby_olm/ext_lib_olm/olm/xcode/OLMKit/OLMUtility.h +49 -0
  160. data/ext/ruby_olm/ext_lib_olm/staging/account.cpp +380 -0
  161. data/ext/ruby_olm/ext_lib_olm/staging/aes.c +1073 -0
  162. data/ext/ruby_olm/ext_lib_olm/staging/base64.cpp +167 -0
  163. data/ext/ruby_olm/ext_lib_olm/staging/cipher.cpp +152 -0
  164. data/ext/ruby_olm/ext_lib_olm/staging/crypto.cpp +299 -0
  165. data/ext/ruby_olm/ext_lib_olm/staging/curve25519-donna.c +860 -0
  166. data/ext/ruby_olm/ext_lib_olm/staging/ed25519.c +22 -0
  167. data/ext/ruby_olm/ext_lib_olm/staging/error.c +44 -0
  168. data/ext/ruby_olm/ext_lib_olm/staging/inbound_group_session.c +524 -0
  169. data/ext/ruby_olm/ext_lib_olm/staging/megolm.c +150 -0
  170. data/ext/ruby_olm/ext_lib_olm/staging/memory.cpp +45 -0
  171. data/ext/ruby_olm/ext_lib_olm/staging/message.cpp +401 -0
  172. data/ext/ruby_olm/ext_lib_olm/staging/olm.cpp +738 -0
  173. data/ext/ruby_olm/ext_lib_olm/staging/outbound_group_session.c +363 -0
  174. data/ext/ruby_olm/ext_lib_olm/staging/pickle.cpp +242 -0
  175. data/ext/ruby_olm/ext_lib_olm/staging/pickle_encoding.c +92 -0
  176. data/ext/ruby_olm/ext_lib_olm/staging/pk.cpp +412 -0
  177. data/ext/ruby_olm/ext_lib_olm/staging/ratchet.cpp +625 -0
  178. data/ext/ruby_olm/ext_lib_olm/staging/session.cpp +461 -0
  179. data/ext/ruby_olm/ext_lib_olm/staging/sha256.c +159 -0
  180. data/ext/ruby_olm/ext_lib_olm/staging/utility.cpp +57 -0
  181. data/lib/ruby_olm/account.rb +42 -0
  182. data/lib/ruby_olm/message.rb +6 -0
  183. data/lib/ruby_olm/olm_error.rb +70 -0
  184. data/lib/ruby_olm/olm_message.rb +25 -0
  185. data/lib/ruby_olm/pre_key_message.rb +6 -0
  186. data/lib/ruby_olm/session.rb +16 -0
  187. data/lib/ruby_olm/version.rb +5 -0
  188. data/lib/ruby_olm.rb +10 -0
  189. data/rakefile +18 -0
  190. data/test/examples/test_bob_no_answer.rb +62 -0
  191. data/test/examples/test_exchange.rb +60 -0
  192. data/test/spec/test_account.rb +152 -0
  193. data/test/unit/test_account_methods.rb +85 -0
  194. metadata +282 -0
@@ -0,0 +1,716 @@
1
+ /*
2
+ * Copyright 2018 New Vector Ltd
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include "olm_pk.h"
18
+
19
+ #include "olm/olm.h"
20
+
21
+ using namespace AndroidOlmSdk;
22
+
23
+ OlmPkEncryption * initializePkEncryptionMemory()
24
+ {
25
+ size_t encryptionSize = olm_pk_encryption_size();
26
+ OlmPkEncryption *encryptionPtr = (OlmPkEncryption *)malloc(encryptionSize);
27
+
28
+ if (encryptionPtr)
29
+ {
30
+ // init encryption object
31
+ encryptionPtr = olm_pk_encryption(encryptionPtr);
32
+ LOGD(
33
+ "## initializePkEncryptionMemory(): success - OLM encryption size=%lu",
34
+ static_cast<long unsigned int>(encryptionSize)
35
+ );
36
+ }
37
+ else
38
+ {
39
+ LOGE("## initializePkEncryptionMemory(): failure - OOM");
40
+ }
41
+
42
+ return encryptionPtr;
43
+ }
44
+
45
+ JNIEXPORT jlong OLM_PK_ENCRYPTION_FUNC_DEF(createNewPkEncryptionJni)(JNIEnv *env, jobject thiz)
46
+ {
47
+ const char* errorMessage = NULL;
48
+ OlmPkEncryption *encryptionPtr = initializePkEncryptionMemory();
49
+
50
+ // init encryption memory allocation
51
+ if (!encryptionPtr)
52
+ {
53
+ LOGE("## createNewPkEncryptionJni(): failure - init encryption OOM");
54
+ errorMessage = "init encryption OOM";
55
+ }
56
+ else
57
+ {
58
+ LOGD("## createNewPkEncryptionJni(): success - OLM encryption created");
59
+ LOGD(
60
+ "## createNewPkEncryptionJni(): encryptionPtr=%p (jlong)(intptr_t)encryptionPtr=%lld",
61
+ encryptionPtr, (jlong)(intptr_t)encryptionPtr
62
+ );
63
+ }
64
+
65
+ if (errorMessage)
66
+ {
67
+ // release the allocated data
68
+ if (encryptionPtr)
69
+ {
70
+ olm_clear_pk_encryption(encryptionPtr);
71
+ free(encryptionPtr);
72
+ }
73
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
74
+ }
75
+
76
+ return (jlong)(intptr_t)encryptionPtr;
77
+ }
78
+
79
+ JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(releasePkEncryptionJni)(JNIEnv *env, jobject thiz)
80
+ {
81
+ LOGD("## releasePkEncryptionJni(): IN");
82
+
83
+ OlmPkEncryption* encryptionPtr = getPkEncryptionInstanceId(env, thiz);
84
+
85
+ if (!encryptionPtr)
86
+ {
87
+ LOGE(" ## releasePkEncryptionJni(): failure - invalid Encryption ptr=NULL");
88
+ }
89
+ else
90
+ {
91
+ LOGD(" ## releasePkEncryptionJni(): encryptionPtr=%p", encryptionPtr);
92
+ olm_clear_pk_encryption(encryptionPtr);
93
+
94
+ LOGD(" ## releasePkEncryptionJni(): IN");
95
+ // even if free(NULL) does not crash, logs are performed for debug
96
+ // purpose
97
+ free(encryptionPtr);
98
+ LOGD(" ## releasePkEncryptionJni(): OUT");
99
+ }
100
+ }
101
+
102
+ JNIEXPORT void OLM_PK_ENCRYPTION_FUNC_DEF(setRecipientKeyJni)(
103
+ JNIEnv *env, jobject thiz, jbyteArray aKeyBuffer
104
+ ) {
105
+ const char *errorMessage = NULL;
106
+ jbyte *keyPtr = NULL;
107
+
108
+ OlmPkEncryption *encryptionPtr = getPkEncryptionInstanceId(env, thiz);
109
+
110
+ if (!encryptionPtr)
111
+ {
112
+ LOGE(" ## pkSetRecipientKeyJni(): failure - invalid Encryption ptr=NULL");
113
+ }
114
+ else if (!aKeyBuffer)
115
+ {
116
+ LOGE(" ## pkSetRecipientKeyJni(): failure - invalid key");
117
+ errorMessage = "invalid key";
118
+ }
119
+ else if (!(keyPtr = env->GetByteArrayElements(aKeyBuffer, 0)))
120
+ {
121
+ LOGE(" ## pkSetRecipientKeyJni(): failure - key JNI allocation OOM");
122
+ errorMessage = "key JNI allocation OOM";
123
+ }
124
+ else
125
+ {
126
+ if (olm_pk_encryption_set_recipient_key(encryptionPtr, keyPtr, (size_t)env->GetArrayLength(aKeyBuffer)) == olm_error())
127
+ {
128
+ errorMessage = olm_pk_encryption_last_error(encryptionPtr);
129
+ LOGE(
130
+ " ## pkSetRecipientKeyJni(): failure - olm_pk_encryption_set_recipient_key Msg=%s",
131
+ errorMessage
132
+ );
133
+ }
134
+ }
135
+
136
+ if (keyPtr)
137
+ {
138
+ env->ReleaseByteArrayElements(aKeyBuffer, keyPtr, JNI_ABORT);
139
+ }
140
+
141
+ if (errorMessage)
142
+ {
143
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
144
+ }
145
+ }
146
+
147
+ JNIEXPORT jbyteArray OLM_PK_ENCRYPTION_FUNC_DEF(encryptJni)(
148
+ JNIEnv *env, jobject thiz, jbyteArray aPlaintextBuffer, jobject aEncryptedMsg
149
+ ) {
150
+ jbyteArray encryptedMsgRet = 0;
151
+ const char* errorMessage = NULL;
152
+ jbyte *plaintextPtr = NULL;
153
+ jboolean plaintextIsCopied = JNI_FALSE;
154
+
155
+ OlmPkEncryption *encryptionPtr = getPkEncryptionInstanceId(env, thiz);
156
+ jclass encryptedMsgJClass = 0;
157
+ jfieldID macFieldId;
158
+ jfieldID ephemeralFieldId;
159
+
160
+ if (!encryptionPtr)
161
+ {
162
+ LOGE(" ## pkEncryptJni(): failure - invalid Encryption ptr=NULL");
163
+ }
164
+ else if (!aPlaintextBuffer)
165
+ {
166
+ LOGE(" ## pkEncryptJni(): failure - invalid clear message");
167
+ errorMessage = "invalid clear message";
168
+ }
169
+ else if (!(plaintextPtr = env->GetByteArrayElements(aPlaintextBuffer, &plaintextIsCopied)))
170
+ {
171
+ LOGE(" ## pkEncryptJni(): failure - plaintext JNI allocation OOM");
172
+ errorMessage = "plaintext JNI allocation OOM";
173
+ }
174
+ else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
175
+ {
176
+ LOGE(" ## pkEncryptJni(): failure - unable to get encrypted message class");
177
+ errorMessage = "unable to get encrypted message class";
178
+ }
179
+ else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass, "mMac", "Ljava/lang/String;")))
180
+ {
181
+ LOGE("## pkEncryptJni(): failure - unable to get MAC field");
182
+ errorMessage = "unable to get MAC field";
183
+ }
184
+ else if (!(ephemeralFieldId = env->GetFieldID(encryptedMsgJClass, "mEphemeralKey", "Ljava/lang/String;")))
185
+ {
186
+ LOGE("## pkEncryptJni(): failure - unable to get ephemeral key field");
187
+ errorMessage = "unable to get ephemeral key field";
188
+ }
189
+ else
190
+ {
191
+ size_t plaintextLength = (size_t)env->GetArrayLength(aPlaintextBuffer);
192
+ size_t ciphertextLength = olm_pk_ciphertext_length(encryptionPtr, plaintextLength);
193
+ size_t macLength = olm_pk_mac_length(encryptionPtr);
194
+ size_t ephemeralLength = olm_pk_key_length();
195
+ uint8_t *ciphertextPtr = NULL, *macPtr = NULL, *ephemeralPtr = NULL;
196
+ size_t randomLength = olm_pk_encrypt_random_length(encryptionPtr);
197
+ uint8_t *randomBuffPtr = NULL;
198
+ LOGD("## pkEncryptJni(): randomLength=%lu",static_cast<long unsigned int>(randomLength));
199
+ if (!(ciphertextPtr = (uint8_t*)malloc(ciphertextLength)))
200
+ {
201
+ LOGE("## pkEncryptJni(): failure - ciphertext JNI allocation OOM");
202
+ errorMessage = "ciphertext JNI allocation OOM";
203
+ }
204
+ else if (!(macPtr = (uint8_t*)malloc(macLength + 1)))
205
+ {
206
+ LOGE("## pkEncryptJni(): failure - MAC JNI allocation OOM");
207
+ errorMessage = "MAC JNI allocation OOM";
208
+ }
209
+ else if (!(ephemeralPtr = (uint8_t*)malloc(ephemeralLength + 1)))
210
+ {
211
+ LOGE("## pkEncryptJni(): failure: ephemeral key JNI allocation OOM");
212
+ errorMessage = "ephemeral JNI allocation OOM";
213
+ }
214
+ else if (!setRandomInBuffer(env, &randomBuffPtr, randomLength))
215
+ {
216
+ LOGE("## pkEncryptJni(): failure - random buffer init");
217
+ errorMessage = "random buffer init";
218
+ }
219
+ else
220
+ {
221
+ macPtr[macLength] = '\0';
222
+ ephemeralPtr[ephemeralLength] = '\0';
223
+
224
+ size_t returnValue = olm_pk_encrypt(
225
+ encryptionPtr,
226
+ plaintextPtr, plaintextLength,
227
+ ciphertextPtr, ciphertextLength,
228
+ macPtr, macLength,
229
+ ephemeralPtr, ephemeralLength,
230
+ randomBuffPtr, randomLength
231
+ );
232
+
233
+ if (returnValue == olm_error())
234
+ {
235
+ errorMessage = olm_pk_encryption_last_error(encryptionPtr);
236
+ LOGE("## pkEncryptJni(): failure - olm_pk_encrypt Msg=%s", errorMessage);
237
+ }
238
+ else
239
+ {
240
+ encryptedMsgRet = env->NewByteArray(ciphertextLength);
241
+ env->SetByteArrayRegion(
242
+ encryptedMsgRet, 0, ciphertextLength, (jbyte*)ciphertextPtr
243
+ );
244
+
245
+ jstring macStr = env->NewStringUTF((char*)macPtr);
246
+ env->SetObjectField(aEncryptedMsg, macFieldId, macStr);
247
+ jstring ephemeralStr = env->NewStringUTF((char*)ephemeralPtr);
248
+ env->SetObjectField(aEncryptedMsg, ephemeralFieldId, ephemeralStr);
249
+ }
250
+ }
251
+
252
+ if (randomBuffPtr)
253
+ {
254
+ memset(randomBuffPtr, 0, randomLength);
255
+ free(randomBuffPtr);
256
+ }
257
+ if (ephemeralPtr)
258
+ {
259
+ free(ephemeralPtr);
260
+ }
261
+ if (macPtr)
262
+ {
263
+ free(macPtr);
264
+ }
265
+ if (ciphertextPtr)
266
+ {
267
+ free(ciphertextPtr);
268
+ }
269
+ }
270
+
271
+ if (plaintextPtr)
272
+ {
273
+ if (plaintextIsCopied)
274
+ {
275
+ memset(plaintextPtr, 0, (size_t)env->GetArrayLength(aPlaintextBuffer));
276
+ }
277
+ env->ReleaseByteArrayElements(aPlaintextBuffer, plaintextPtr, JNI_ABORT);
278
+ }
279
+
280
+ if (errorMessage)
281
+ {
282
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
283
+ }
284
+
285
+ return encryptedMsgRet;
286
+ }
287
+
288
+ OlmPkDecryption * initializePkDecryptionMemory()
289
+ {
290
+ size_t decryptionSize = olm_pk_decryption_size();
291
+ OlmPkDecryption *decryptionPtr = (OlmPkDecryption *)malloc(decryptionSize);
292
+
293
+ if (decryptionPtr)
294
+ {
295
+ // init decryption object
296
+ decryptionPtr = olm_pk_decryption(decryptionPtr);
297
+ LOGD(
298
+ "## initializePkDecryptionMemory(): success - OLM decryption size=%lu",
299
+ static_cast<long unsigned int>(decryptionSize)
300
+ );
301
+ }
302
+ else
303
+ {
304
+ LOGE("## initializePkDecryptionMemory(): failure - OOM");
305
+ }
306
+
307
+ return decryptionPtr;
308
+ }
309
+
310
+ JNIEXPORT jlong OLM_PK_DECRYPTION_FUNC_DEF(createNewPkDecryptionJni)(JNIEnv *env, jobject thiz)
311
+ {
312
+ const char* errorMessage = NULL;
313
+ OlmPkDecryption *decryptionPtr = initializePkDecryptionMemory();
314
+
315
+ // init encryption memory allocation
316
+ if (!decryptionPtr)
317
+ {
318
+ LOGE("## createNewPkDecryptionJni(): failure - init decryption OOM");
319
+ errorMessage = "init decryption OOM";
320
+ }
321
+ else
322
+ {
323
+ LOGD("## createNewPkDecryptionJni(): success - OLM decryption created");
324
+ LOGD(
325
+ "## createNewPkDecryptionJni(): decryptionPtr=%p (jlong)(intptr_t)decryptionPtr=%lld",
326
+ decryptionPtr, (jlong)(intptr_t)decryptionPtr
327
+ );
328
+ }
329
+
330
+ if (errorMessage)
331
+ {
332
+ // release the allocated data
333
+ if (decryptionPtr)
334
+ {
335
+ olm_clear_pk_decryption(decryptionPtr);
336
+ free(decryptionPtr);
337
+ }
338
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
339
+ }
340
+
341
+ return (jlong)(intptr_t)decryptionPtr;
342
+ }
343
+
344
+ JNIEXPORT void OLM_PK_DECRYPTION_FUNC_DEF(releasePkDecryptionJni)(JNIEnv *env, jobject thiz)
345
+ {
346
+ LOGD("## releasePkDecryptionJni(): IN");
347
+
348
+ OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz);
349
+
350
+ if (!decryptionPtr)
351
+ {
352
+ LOGE(" ## releasePkDecryptionJni(): failure - invalid Decryption ptr=NULL");
353
+ }
354
+ else
355
+ {
356
+ LOGD(" ## releasePkDecryptionJni(): decryptionPtr=%p", encryptionPtr);
357
+ olm_clear_pk_decryption(decryptionPtr);
358
+
359
+ LOGD(" ## releasePkDecryptionJni(): IN");
360
+ // even if free(NULL) does not crash, logs are performed for debug
361
+ // purpose
362
+ free(decryptionPtr);
363
+ LOGD(" ## releasePkDecryptionJni(): OUT");
364
+ }
365
+ }
366
+
367
+ JNIEXPORT jint OLM_PK_DECRYPTION_FUNC_DEF(privateKeyLength)(JNIEnv *env, jobject thiz)
368
+ {
369
+ return (jint) olm_pk_private_key_length();
370
+ }
371
+
372
+ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(setPrivateKeyJni)(JNIEnv *env, jobject thiz, jbyteArray key)
373
+ {
374
+ jbyteArray publicKeyRet = 0;
375
+ jbyte *keyPtr = NULL;
376
+ jboolean keyWasCopied = JNI_FALSE;
377
+
378
+ const char* errorMessage = NULL;
379
+
380
+ OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz);
381
+
382
+ if (!decryptionPtr)
383
+ {
384
+ LOGE(" ## pkSetPrivateKeyJni(): failure - invalid Decryption ptr=NULL");
385
+ }
386
+ else if (!key)
387
+ {
388
+ LOGE(" ## pkSetPrivateKeyJni(): failure - invalid key");
389
+ errorMessage = "invalid key";
390
+ }
391
+ else if (!(keyPtr = env->GetByteArrayElements(key, &keyWasCopied)))
392
+ {
393
+ LOGE(" ## pkSetPrivateKeyJni(): failure - key JNI allocation OOM");
394
+ errorMessage = "key JNI allocation OOM";
395
+ }
396
+ else
397
+ {
398
+ size_t publicKeyLength = olm_pk_key_length();
399
+ uint8_t *publicKeyPtr = NULL;
400
+ size_t keyLength = (size_t)env->GetArrayLength(key);
401
+ if (!(publicKeyPtr = (uint8_t*)malloc(publicKeyLength)))
402
+ {
403
+ LOGE("## pkSetPrivateKeyJni(): failure - public key JNI allocation OOM");
404
+ errorMessage = "public key JNI allocation OOM";
405
+ }
406
+ else
407
+ {
408
+ size_t returnValue = olm_pk_key_from_private(
409
+ decryptionPtr,
410
+ publicKeyPtr, publicKeyLength,
411
+ keyPtr, keyLength
412
+ );
413
+ if (returnValue == olm_error())
414
+ {
415
+ errorMessage = olm_pk_decryption_last_error(decryptionPtr);
416
+ LOGE(" ## pkSetPrivateKeyJni(): failure - olm_pk_key_from_private Msg=%s", errorMessage);
417
+ }
418
+ else
419
+ {
420
+ publicKeyRet = env->NewByteArray(publicKeyLength);
421
+ env->SetByteArrayRegion(
422
+ publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr
423
+ );
424
+ }
425
+ }
426
+ }
427
+
428
+ if (keyPtr)
429
+ {
430
+ if (keyWasCopied)
431
+ {
432
+ memset(keyPtr, 0, (size_t)env->GetArrayLength(key));
433
+ }
434
+ env->ReleaseByteArrayElements(key, keyPtr, JNI_ABORT);
435
+ }
436
+
437
+ if (errorMessage)
438
+ {
439
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
440
+ }
441
+
442
+ return publicKeyRet;
443
+ }
444
+
445
+ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(generateKeyJni)(JNIEnv *env, jobject thiz)
446
+ {
447
+ size_t randomLength = olm_pk_private_key_length();
448
+ uint8_t *randomBuffPtr = NULL;
449
+
450
+ jbyteArray publicKeyRet = 0;
451
+ uint8_t *publicKeyPtr = NULL;
452
+ size_t publicKeyLength = olm_pk_key_length();
453
+ const char* errorMessage = NULL;
454
+
455
+ OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz);
456
+
457
+ if (!decryptionPtr)
458
+ {
459
+ LOGE(" ## pkGenerateKeyJni(): failure - invalid Decryption ptr=NULL");
460
+ errorMessage = "invalid Decryption ptr=NULL";
461
+ }
462
+ else if (!setRandomInBuffer(env, &randomBuffPtr, randomLength))
463
+ {
464
+ LOGE("## pkGenerateKeyJni(): failure - random buffer init");
465
+ errorMessage = "random buffer init";
466
+ }
467
+ else if (!(publicKeyPtr = static_cast<uint8_t*>(malloc(publicKeyLength))))
468
+ {
469
+ LOGE("## pkGenerateKeyJni(): failure - public key allocation OOM");
470
+ errorMessage = "public key allocation OOM";
471
+ }
472
+ else
473
+ {
474
+ if (olm_pk_key_from_private(decryptionPtr, publicKeyPtr, publicKeyLength, randomBuffPtr, randomLength) == olm_error())
475
+ {
476
+ errorMessage = olm_pk_decryption_last_error(decryptionPtr);
477
+ LOGE("## pkGenerateKeyJni(): failure - olm_pk_generate_key Msg=%s", errorMessage);
478
+ }
479
+ else
480
+ {
481
+ publicKeyRet = env->NewByteArray(publicKeyLength);
482
+ env->SetByteArrayRegion(publicKeyRet, 0, publicKeyLength, (jbyte*)publicKeyPtr);
483
+ LOGD("## pkGenerateKeyJni(): public key generated");
484
+ }
485
+ }
486
+
487
+ if (randomBuffPtr)
488
+ {
489
+ memset(randomBuffPtr, 0, randomLength);
490
+ free(randomBuffPtr);
491
+ }
492
+
493
+ if (errorMessage)
494
+ {
495
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
496
+ }
497
+
498
+ return publicKeyRet;
499
+ }
500
+
501
+ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(privateKeyJni)(JNIEnv *env, jobject thiz)
502
+ {
503
+ jbyteArray privateKeyRet = 0;
504
+
505
+ const char* errorMessage = NULL;
506
+
507
+ OlmPkDecryption* decryptionPtr = getPkDecryptionInstanceId(env, thiz);
508
+
509
+ if (!decryptionPtr)
510
+ {
511
+ LOGE(" ## pkPrivateKeyJni(): failure - invalid Decryption ptr=NULL");
512
+ }
513
+ else
514
+ {
515
+ size_t privateKeyLength = olm_pk_private_key_length();
516
+ uint8_t *privateKeyPtr = NULL;
517
+ if (!(privateKeyPtr = (uint8_t*)malloc(privateKeyLength)))
518
+ {
519
+ LOGE("## pkPrivateKeyJni(): failure - private key JNI allocation OOM");
520
+ errorMessage = "private key JNI allocation OOM";
521
+ }
522
+ else
523
+ {
524
+ size_t returnValue = olm_pk_get_private_key(
525
+ decryptionPtr,
526
+ privateKeyPtr, privateKeyLength
527
+ );
528
+ if (returnValue == olm_error())
529
+ {
530
+ errorMessage = olm_pk_decryption_last_error(decryptionPtr);
531
+ LOGE(" ## pkPrivateKeyJni(): failure - olm_pk_get_private_key Msg=%s", errorMessage);
532
+ }
533
+ else
534
+ {
535
+ privateKeyRet = env->NewByteArray(privateKeyLength);
536
+ env->SetByteArrayRegion(
537
+ privateKeyRet, 0, privateKeyLength, (jbyte*)privateKeyPtr
538
+ );
539
+ memset(privateKeyPtr, 0, privateKeyLength);
540
+ }
541
+ }
542
+ }
543
+
544
+ if (errorMessage)
545
+ {
546
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
547
+ }
548
+
549
+ return privateKeyRet;
550
+ }
551
+
552
+ JNIEXPORT jbyteArray OLM_PK_DECRYPTION_FUNC_DEF(decryptJni)(
553
+ JNIEnv *env, jobject thiz, jobject aEncryptedMsg
554
+ ) {
555
+ const char* errorMessage = NULL;
556
+ OlmPkDecryption *decryptionPtr = getPkDecryptionInstanceId(env, thiz);
557
+
558
+ jclass encryptedMsgJClass = 0;
559
+ jstring ciphertextJstring = 0;
560
+ jstring macJstring = 0;
561
+ jstring ephemeralKeyJstring = 0;
562
+ jfieldID ciphertextFieldId;
563
+ jfieldID macFieldId;
564
+ jfieldID ephemeralKeyFieldId;
565
+
566
+ const char *ciphertextPtr = NULL;
567
+ const char *macPtr = NULL;
568
+ const char *ephemeralKeyPtr = NULL;
569
+
570
+ jbyteArray decryptedMsgRet = 0;
571
+
572
+ if (!decryptionPtr)
573
+ {
574
+ LOGE(" ## pkDecryptJni(): failure - invalid Decryption ptr=NULL");
575
+ errorMessage = "invalid Decryption ptr=NULL";
576
+ }
577
+ else if (!aEncryptedMsg)
578
+ {
579
+ LOGE(" ## pkDecryptJni(): failure - invalid encrypted message");
580
+ errorMessage = "invalid encrypted message";
581
+ }
582
+ else if (!(encryptedMsgJClass = env->GetObjectClass(aEncryptedMsg)))
583
+ {
584
+ LOGE("## pkDecryptJni(): failure - unable to get encrypted message class");
585
+ errorMessage = "unable to get encrypted message class";
586
+ }
587
+ else if (!(ciphertextFieldId = env->GetFieldID(encryptedMsgJClass,"mCipherText","Ljava/lang/String;")))
588
+ {
589
+ LOGE("## pkDecryptJni(): failure - unable to get message field");
590
+ errorMessage = "unable to get message field";
591
+ }
592
+ else if (!(ciphertextJstring = (jstring)env->GetObjectField(aEncryptedMsg, ciphertextFieldId)))
593
+ {
594
+ LOGE("## pkDecryptJni(): failure - no ciphertext");
595
+ errorMessage = "no ciphertext";
596
+ }
597
+ else if (!(ciphertextPtr = env->GetStringUTFChars(ciphertextJstring, 0)))
598
+ {
599
+ LOGE("## pkDecryptJni(): failure - ciphertext JNI allocation OOM");
600
+ errorMessage = "ciphertext JNI allocation OOM";
601
+ }
602
+ else if (!(ciphertextJstring = (jstring)env->GetObjectField(aEncryptedMsg, ciphertextFieldId)))
603
+ {
604
+ LOGE("## pkDecryptJni(): failure - no ciphertext");
605
+ errorMessage = "no ciphertext";
606
+ }
607
+ else if (!(ciphertextPtr = env->GetStringUTFChars(ciphertextJstring, 0)))
608
+ {
609
+ LOGE("## decryptMessageJni(): failure - ciphertext JNI allocation OOM");
610
+ errorMessage = "ciphertext JNI allocation OOM";
611
+ }
612
+ else if (!(macFieldId = env->GetFieldID(encryptedMsgJClass,"mMac","Ljava/lang/String;")))
613
+ {
614
+ LOGE("## pkDecryptJni(): failure - unable to get MAC field");
615
+ errorMessage = "unable to get MAC field";
616
+ }
617
+ else if (!(macJstring = (jstring)env->GetObjectField(aEncryptedMsg, macFieldId)))
618
+ {
619
+ LOGE("## pkDecryptJni(): failure - no MAC");
620
+ errorMessage = "no MAC";
621
+ }
622
+ else if (!(macPtr = env->GetStringUTFChars(macJstring, 0)))
623
+ {
624
+ LOGE("## pkDecryptJni(): failure - MAC JNI allocation OOM");
625
+ errorMessage = "ciphertext JNI allocation OOM";
626
+ }
627
+ else if (!(ephemeralKeyFieldId = env->GetFieldID(encryptedMsgJClass,"mEphemeralKey","Ljava/lang/String;")))
628
+ {
629
+ LOGE("## pkDecryptJni(): failure - unable to get ephemeral key field");
630
+ errorMessage = "unable to get ephemeral key field";
631
+ }
632
+ else if (!(ephemeralKeyJstring = (jstring)env->GetObjectField(aEncryptedMsg, ephemeralKeyFieldId)))
633
+ {
634
+ LOGE("## pkDecryptJni(): failure - no ephemeral key");
635
+ errorMessage = "no ephemeral key";
636
+ }
637
+ else if (!(ephemeralKeyPtr = env->GetStringUTFChars(ephemeralKeyJstring, 0)))
638
+ {
639
+ LOGE("## pkDecryptJni(): failure - ephemeral key JNI allocation OOM");
640
+ errorMessage = "ephemeral key JNI allocation OOM";
641
+ }
642
+ else
643
+ {
644
+ size_t maxPlaintextLength = olm_pk_max_plaintext_length(
645
+ decryptionPtr,
646
+ (size_t)env->GetStringUTFLength(ciphertextJstring)
647
+ );
648
+ uint8_t *plaintextPtr = NULL;
649
+ uint8_t *tempCiphertextPtr = NULL;
650
+ size_t ciphertextLength = (size_t)env->GetStringUTFLength(ciphertextJstring);
651
+ if (!(plaintextPtr = (uint8_t*)malloc(maxPlaintextLength)))
652
+ {
653
+ LOGE("## pkDecryptJni(): failure - plaintext JNI allocation OOM");
654
+ errorMessage = "plaintext JNI allocation OOM";
655
+ }
656
+ else if (!(tempCiphertextPtr = (uint8_t*)malloc(ciphertextLength)))
657
+ {
658
+ LOGE("## pkDecryptJni(): failure - temp ciphertext JNI allocation OOM");
659
+ }
660
+ else
661
+ {
662
+ memcpy(tempCiphertextPtr, ciphertextPtr, ciphertextLength);
663
+ size_t plaintextLength = olm_pk_decrypt(
664
+ decryptionPtr,
665
+ ephemeralKeyPtr, (size_t)env->GetStringUTFLength(ephemeralKeyJstring),
666
+ macPtr, (size_t)env->GetStringUTFLength(macJstring),
667
+ tempCiphertextPtr, ciphertextLength,
668
+ plaintextPtr, maxPlaintextLength
669
+ );
670
+ if (plaintextLength == olm_error())
671
+ {
672
+ errorMessage = olm_pk_decryption_last_error(decryptionPtr);
673
+ LOGE("## pkDecryptJni(): failure - olm_pk_decrypt Msg=%s", errorMessage);
674
+ }
675
+ else
676
+ {
677
+ decryptedMsgRet = env->NewByteArray(plaintextLength);
678
+ env->SetByteArrayRegion(decryptedMsgRet, 0, plaintextLength, (jbyte*)plaintextPtr);
679
+ LOGD(
680
+ "## pkDecryptJni(): success returnedLg=%lu OK",
681
+ static_cast<long unsigned int>(plaintextLength)
682
+ );
683
+ }
684
+ }
685
+
686
+ if (tempCiphertextPtr)
687
+ {
688
+ free(tempCiphertextPtr);
689
+ }
690
+ if (plaintextPtr)
691
+ {
692
+ memset(plaintextPtr, 0, maxPlaintextLength);
693
+ free(plaintextPtr);
694
+ }
695
+ }
696
+
697
+ if (ciphertextPtr)
698
+ {
699
+ env->ReleaseStringUTFChars(ciphertextJstring, ciphertextPtr);
700
+ }
701
+ if (macPtr)
702
+ {
703
+ env->ReleaseStringUTFChars(macJstring, macPtr);
704
+ }
705
+ if (ephemeralKeyPtr)
706
+ {
707
+ env->ReleaseStringUTFChars(ephemeralKeyJstring, ephemeralKeyPtr);
708
+ }
709
+
710
+ if (errorMessage)
711
+ {
712
+ env->ThrowNew(env->FindClass("java/lang/Exception"), errorMessage);
713
+ }
714
+
715
+ return decryptedMsgRet;
716
+ }