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
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 17da5166e2138afc190196d6edf41f5e9e7c9f3b00f7924112fa6490c87f955e
4
+ data.tar.gz: 0c5b08a346d82fcfa3f6388b6ba208a7a42d83fe9949b3ae4ced863aceaa28bc
5
+ SHA512:
6
+ metadata.gz: 679c54ffda79d6b8639fa3e0a18e25b2c514ccc26879c0981539d794f0bcafec7e54b8477e9621452e47d16eec27ed0b151843923e60278988f6556468c34e76
7
+ data.tar.gz: 3866781b6aae4957b6cd5fd7d3c30c35a7af5dafb55b94fbc359d6122581adc65310c6f6f7de776edcaaf7272fba3f77b984a4e775888d32d2f962bbdaed6123
@@ -0,0 +1,274 @@
1
+ #include "olm/olm.h"
2
+ #include "ext_lib_olm.h"
3
+
4
+ static VALUE last_error(VALUE self)
5
+ {
6
+ OlmAccount *this;
7
+ Data_Get_Struct(self, OlmAccount, this);
8
+
9
+ return rb_funcall(rb_eval_string("RubyOlm::OlmError"), rb_intern("from_string"), 1, rb_str_new2(olm_account_last_error(this)));
10
+ }
11
+
12
+ static VALUE initialize(int argc, VALUE *argv, VALUE self)
13
+ {
14
+ VALUE pickle, password, opts;
15
+ size_t size;
16
+ OlmAccount *this;
17
+ Data_Get_Struct(self, OlmAccount, this);
18
+
19
+ (void)rb_scan_args(argc, argv, "0:", &opts);
20
+
21
+ opts = (opts == Qnil) ? rb_hash_new(): opts;
22
+
23
+ pickle = rb_hash_aref(opts, ID2SYM(rb_intern("pickle")));
24
+ password = rb_hash_aref(opts, ID2SYM(rb_intern("password")));
25
+
26
+ if(pickle != Qnil){
27
+
28
+ if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){
29
+
30
+ rb_raise(rb_eTypeError, "pickle must be kind of String");
31
+ }
32
+ }
33
+
34
+ if(password != Qnil){
35
+
36
+ if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){
37
+
38
+ rb_raise(rb_eTypeError, "password must be kind of String");
39
+ }
40
+ }
41
+ else{
42
+
43
+ password = rb_str_new2("");
44
+ }
45
+
46
+ if(pickle != Qnil){
47
+
48
+ if(olm_unpickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){
49
+
50
+ raise_olm_error(olm_account_last_error(this));
51
+ }
52
+ }
53
+ else{
54
+
55
+ size = olm_create_account_random_length(this);
56
+
57
+ if(olm_create_account(this, RSTRING_PTR(get_random(size)), size) == olm_error()){
58
+
59
+ raise_olm_error(olm_account_last_error(this));
60
+ }
61
+ }
62
+
63
+ return self;
64
+ }
65
+
66
+ static VALUE identity_keys(VALUE self)
67
+ {
68
+ OlmAccount *this;
69
+ size_t size;
70
+ Data_Get_Struct(self, OlmAccount, this);
71
+
72
+ size = olm_account_identity_keys_length(this);
73
+ uint8_t buf[size];
74
+
75
+ if(olm_account_identity_keys(this, buf, size) != size){
76
+
77
+ raise_olm_error(olm_account_last_error(this));
78
+ }
79
+
80
+ return rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new((char *)buf, size));
81
+ }
82
+
83
+ static VALUE generate_one_time_keys(VALUE self, VALUE number)
84
+ {
85
+ size_t size;
86
+ OlmAccount *this;
87
+ Data_Get_Struct(self, OlmAccount, this);
88
+
89
+ size = olm_account_generate_one_time_keys_random_length(this, NUM2SIZET(number));
90
+
91
+ if(olm_account_generate_one_time_keys(this, NUM2SIZET(number), RSTRING_PTR(get_random(size)), size) == olm_error()){
92
+
93
+ raise_olm_error(olm_account_last_error(this));
94
+ }
95
+
96
+ return self;
97
+ }
98
+
99
+ static VALUE one_time_keys(VALUE self)
100
+ {
101
+ VALUE retval;
102
+ size_t size;
103
+ void *ptr;
104
+ OlmAccount *this;
105
+ Data_Get_Struct(self, OlmAccount, this);
106
+
107
+ size = olm_account_one_time_keys_length(this);
108
+
109
+ if((ptr = malloc(size)) == NULL){
110
+
111
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
112
+ }
113
+
114
+ if(olm_account_one_time_keys(this, ptr, size) != size){
115
+
116
+ free(ptr);
117
+ raise_olm_error(olm_account_last_error(this));
118
+ }
119
+
120
+ retval = rb_funcall(rb_eval_string("JSON"), rb_intern("parse"), 1, rb_str_new(ptr, size));
121
+
122
+ free(ptr);
123
+
124
+ return retval;
125
+ }
126
+
127
+ static VALUE sign(VALUE self, VALUE message)
128
+ {
129
+ VALUE retval;
130
+ size_t size;
131
+ void *ptr;
132
+ OlmAccount *this;
133
+ Data_Get_Struct(self, OlmAccount, this);
134
+
135
+ size = olm_account_signature_length(this);
136
+
137
+ if((ptr = malloc(size)) == NULL){
138
+
139
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
140
+ }
141
+
142
+ if(olm_account_sign(this, RSTRING_PTR(message), RSTRING_LEN(message), ptr, size) == olm_error()){
143
+
144
+ free(ptr);
145
+ raise_olm_error(olm_account_last_error(this));
146
+ }
147
+
148
+ retval = rb_str_new(ptr, size);
149
+
150
+ free(ptr);
151
+
152
+ return retval;
153
+ }
154
+
155
+ static VALUE mark_keys_as_published(VALUE self)
156
+ {
157
+ OlmAccount *this;
158
+ Data_Get_Struct(self, OlmAccount, this);
159
+
160
+ (void)olm_account_mark_keys_as_published(this);
161
+
162
+ return self;
163
+ }
164
+
165
+ static VALUE max_number_of_one_time_keys(VALUE self)
166
+ {
167
+ OlmAccount *this;
168
+ Data_Get_Struct(self, OlmAccount, this);
169
+
170
+ return SIZET2NUM(olm_account_max_number_of_one_time_keys(this));
171
+ }
172
+
173
+ static VALUE remove_one_time_keys(VALUE self, VALUE session)
174
+ {
175
+ OlmAccount *this;
176
+ Data_Get_Struct(self, OlmAccount, this);
177
+
178
+ OlmSession *s;
179
+ Data_Get_Struct(session, OlmSession, s);
180
+
181
+ if(olm_remove_one_time_keys(this, s) == olm_error()){
182
+
183
+ raise_olm_error(olm_account_last_error(this));
184
+ }
185
+
186
+ return self;
187
+ }
188
+
189
+ static VALUE outbound_session(VALUE self, VALUE identity_key, VALUE pre_key)
190
+ {
191
+ return rb_funcall(rb_eval_string("RubyOlm::OutboundSession"), rb_intern("new"), 3, self, identity_key, pre_key);
192
+ }
193
+
194
+ static VALUE inbound_session(int argc, VALUE *argv, VALUE self)
195
+ {
196
+ VALUE args[] = {self, Qnil, Qnil};
197
+
198
+ (void)rb_scan_args(argc, argv, "11", &args[1], &args[2]);
199
+
200
+ return rb_class_new_instance(sizeof(args)/sizeof(*args), args, rb_eval_string("RubyOlm::InboundSession"));
201
+ }
202
+
203
+ static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
204
+ {
205
+ VALUE password, retval;
206
+ OlmAccount *this;
207
+ void *ptr;
208
+ size_t size;
209
+ Data_Get_Struct(self, OlmAccount, this);
210
+
211
+ (void)rb_scan_args(argc, argv, "01", &password);
212
+
213
+ password = (password == Qnil) ? rb_str_new2("") : password;
214
+
215
+ size = olm_pickle_account_length(this);
216
+
217
+ if((ptr = malloc(size)) == NULL){
218
+
219
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
220
+ }
221
+
222
+ if(olm_pickle_account(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){
223
+
224
+ free(ptr);
225
+ raise_olm_error(olm_account_last_error(this));
226
+ }
227
+
228
+ retval = rb_str_new(ptr, size);
229
+
230
+ free(ptr);
231
+
232
+ return retval;
233
+ }
234
+
235
+ static void _free(void *ptr)
236
+ {
237
+ olm_clear_account(ptr);
238
+ free(ptr);
239
+ }
240
+
241
+ static VALUE _alloc(VALUE klass)
242
+ {
243
+ OlmAccount *this;
244
+ VALUE self;
245
+
246
+ self = Data_Wrap_Struct(klass, 0, _free, calloc(1, olm_account_size()));
247
+
248
+ Data_Get_Struct(self, OlmAccount, this);
249
+
250
+ (void)olm_account((void *)this);
251
+
252
+ return self;
253
+ }
254
+
255
+ void account_init(void)
256
+ {
257
+ VALUE cRubyOLM = rb_define_module("RubyOlm");
258
+ VALUE cAccount = rb_define_class_under(cRubyOLM, "Account", rb_cObject);
259
+
260
+ rb_define_alloc_func(cAccount, _alloc);
261
+
262
+ rb_define_method(cAccount, "initialize", initialize, -1);
263
+ rb_define_method(cAccount, "identity_keys", identity_keys, 0);
264
+ rb_define_method(cAccount, "one_time_keys", one_time_keys, 0);
265
+ rb_define_method(cAccount, "generate_one_time_keys", generate_one_time_keys, 1);
266
+ rb_define_method(cAccount, "last_error", last_error, 0);
267
+ rb_define_method(cAccount, "sign", sign, 1);
268
+ rb_define_method(cAccount, "mark_keys_as_published", mark_keys_as_published, 0);
269
+ rb_define_method(cAccount, "max_number_of_one_time_keys", max_number_of_one_time_keys, 0);
270
+ rb_define_method(cAccount, "remove_one_time_keys", remove_one_time_keys, 1);
271
+ rb_define_method(cAccount, "outbound_session", outbound_session, 2);
272
+ rb_define_method(cAccount, "inbound_session", inbound_session, -1);
273
+ rb_define_method(cAccount, "to_pickle", to_pickle, -1);
274
+ }
@@ -0,0 +1,51 @@
1
+ #include <ruby.h>
2
+ #include <assert.h>
3
+ #include <stdbool.h>
4
+
5
+ #include "olm/olm.h"
6
+
7
+ void account_init(void);
8
+ void session_init(void);
9
+ void utility_init(void);
10
+
11
+ static VALUE get_olm_version(VALUE self)
12
+ {
13
+ char buffer[20U];
14
+ uint8_t major = 0U;
15
+ uint8_t minor = 0U;
16
+ uint8_t patch = 0U;
17
+
18
+ olm_get_library_version(&major, &minor, &patch);
19
+
20
+ snprintf(buffer, sizeof(buffer), "%u.%u.%u", major, minor, patch);
21
+
22
+ return rb_str_new2(buffer);
23
+ }
24
+
25
+ void Init_ext_lib_olm(void)
26
+ {
27
+ rb_require("openssl");
28
+ rb_require("json");
29
+ rb_require("ruby_olm/olm_error");
30
+
31
+ rb_define_singleton_method(rb_eval_string("RubyOlm"), "olm_version", get_olm_version, 0);
32
+
33
+ account_init();
34
+ session_init();
35
+ utility_init();
36
+ }
37
+
38
+ void raise_olm_error(const char *error)
39
+ {
40
+ rb_funcall(rb_eval_string("RubyOlm::OlmError"), rb_intern("raise_from_string"), 1, rb_str_new2(error));
41
+ }
42
+
43
+ VALUE get_random(size_t size)
44
+ {
45
+ return rb_funcall(rb_eval_string("OpenSSL::Random"), rb_intern("random_bytes"), 1, SIZET2NUM(size));
46
+ }
47
+
48
+ VALUE dup_string(VALUE str)
49
+ {
50
+ return rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
51
+ }
@@ -0,0 +1,13 @@
1
+ #ifndef EXT_LIB_OLM_H
2
+ #define EXT_LIB_OLM_H
3
+
4
+ #include <ruby.h>
5
+
6
+ /* convert error string to exception and raise it */
7
+ void raise_olm_error(const char *error);
8
+ VALUE get_random(size_t size);
9
+
10
+ /* necessary to avoid copy-on-write weirdness */
11
+ VALUE dup_string(VALUE str);
12
+
13
+ #endif
@@ -0,0 +1,363 @@
1
+ #include "olm/olm.h"
2
+ #include "ext_lib_olm.h"
3
+
4
+ static VALUE last_error(VALUE self)
5
+ {
6
+ OlmSession *this;
7
+ Data_Get_Struct(self, OlmSession, this);
8
+
9
+ return rb_str_new2(olm_session_last_error(this));
10
+ }
11
+
12
+ static VALUE initialize(int argc, VALUE *argv, VALUE self)
13
+ {
14
+ VALUE pickle, password;
15
+ OlmSession *this;
16
+ Data_Get_Struct(self, OlmSession, this);
17
+
18
+ (void)rb_scan_args(argc, argv, "11", &pickle, &password);
19
+
20
+ if(rb_obj_is_kind_of(pickle, rb_cString) != Qtrue){
21
+
22
+ rb_raise(rb_eTypeError, "pickle must be kind of String");
23
+ }
24
+
25
+ if(password != Qnil){
26
+
27
+ if(rb_obj_is_kind_of(password, rb_cString) != Qtrue){
28
+
29
+ rb_raise(rb_eTypeError, "password must be kind of String");
30
+ }
31
+ }
32
+ else{
33
+
34
+ password = rb_str_new2("");
35
+ }
36
+
37
+ if(olm_unpickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), RSTRING_PTR(dup_string(pickle)), RSTRING_LEN(pickle)) == olm_error()){
38
+
39
+ raise_olm_error(olm_session_last_error(this));
40
+ }
41
+
42
+ return self;
43
+ }
44
+
45
+ static VALUE initialize_outbound(VALUE self, VALUE account, VALUE identity, VALUE one_time_key)
46
+ {
47
+ size_t size;
48
+ OlmSession *this;
49
+ OlmAccount *a;
50
+
51
+ Data_Get_Struct(self, OlmSession, this);
52
+ Data_Get_Struct(account, OlmAccount, a);
53
+
54
+ size = olm_create_outbound_session_random_length(this);
55
+
56
+ if(rb_obj_is_instance_of(account, rb_eval_string("RubyOlm::Account")) != Qtrue){
57
+
58
+ rb_raise(rb_eTypeError, "account must be an instance of RubyOlm::Account");
59
+ }
60
+ if(rb_obj_is_kind_of(identity, rb_eval_string("String")) != Qtrue){
61
+
62
+ rb_raise(rb_eTypeError, "identity must be kind of String");
63
+ }
64
+ if(rb_obj_is_kind_of(one_time_key, rb_eval_string("String")) != Qtrue){
65
+
66
+ rb_raise(rb_eTypeError, "one_time_key must be kind of String");
67
+ }
68
+
69
+ if(olm_create_outbound_session(this, a,
70
+ RSTRING_PTR(identity), RSTRING_LEN(identity),
71
+ RSTRING_PTR(one_time_key), RSTRING_LEN(one_time_key),
72
+ RSTRING_PTR(get_random(size)), size
73
+ ) == olm_error()){
74
+ raise_olm_error(olm_session_last_error(this));
75
+ }
76
+
77
+ return self;
78
+ }
79
+
80
+ static VALUE initialize_inbound(int argc, VALUE *argv, VALUE self)
81
+ {
82
+ VALUE account, one_time_message, identity;
83
+
84
+ identity = Qnil;
85
+
86
+ (void)rb_scan_args(argc, argv, "21", &account, &one_time_message, &identity);
87
+
88
+ OlmSession *this;
89
+ Data_Get_Struct(self, OlmSession, this);
90
+
91
+ OlmAccount *a;
92
+ Data_Get_Struct(account, OlmAccount, a);
93
+
94
+ if(rb_obj_is_kind_of(one_time_message, rb_eval_string("RubyOlm::PreKeyMessage")) != Qtrue){
95
+
96
+ rb_raise(rb_eTypeError, "one_time_message must be kind of PreKeyMessage");
97
+ }
98
+
99
+ one_time_message = rb_funcall(one_time_message, rb_intern("to_s"), 0);
100
+
101
+ if(identity == Qnil){
102
+
103
+ if(olm_create_inbound_session(this, a,
104
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
105
+ ) == olm_error()){
106
+ raise_olm_error(olm_session_last_error(this));
107
+ }
108
+ }
109
+ else{
110
+
111
+ if(olm_create_inbound_session_from(this, a,
112
+ RSTRING_PTR(identity), RSTRING_LEN(identity),
113
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
114
+ ) == olm_error()){
115
+ raise_olm_error(olm_session_last_error(this));
116
+ }
117
+ }
118
+
119
+ return self;
120
+ }
121
+
122
+ static VALUE has_received_message(VALUE self)
123
+ {
124
+ OlmSession *this;
125
+ Data_Get_Struct(self, OlmSession, this);
126
+
127
+ return (olm_session_has_received_message(this) == 0) ? Qfalse : Qtrue;
128
+ }
129
+
130
+ static VALUE get_session_id(VALUE self)
131
+ {
132
+ OlmSession *this;
133
+ Data_Get_Struct(self, OlmSession, this);
134
+
135
+ size_t size = olm_session_id_length(this);
136
+ uint8_t buf[size];
137
+
138
+ if(olm_session_id(this, buf, size) != size){
139
+
140
+ raise_olm_error(olm_session_last_error(this));
141
+ }
142
+
143
+ return rb_str_new((char *)buf, size);
144
+ }
145
+
146
+ static VALUE will_receive(int argc, VALUE *argv, VALUE self)
147
+ {
148
+ VALUE one_time_message, identity;
149
+ size_t result;
150
+ OlmSession *this;
151
+ Data_Get_Struct(self, OlmSession, this);
152
+
153
+ identity = Qnil;
154
+
155
+ (void)rb_scan_args(argc, argv, "11", &one_time_message, &identity);
156
+
157
+ if(rb_obj_is_kind_of(one_time_message, rb_eval_string("RubyOlm::PreKeyMessage")) != Qtrue){
158
+
159
+ rb_raise(rb_eTypeError, "one_time_message must be kind of PreKeyMessage");
160
+ }
161
+
162
+ one_time_message = rb_funcall(one_time_message, rb_intern("to_s"), 0);
163
+
164
+ if(identity == Qnil){
165
+
166
+ result = olm_matches_inbound_session(this,
167
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
168
+ );
169
+ }
170
+ else{
171
+
172
+ result = olm_matches_inbound_session_from(this,
173
+ RSTRING_PTR(identity), RSTRING_LEN(identity),
174
+ RSTRING_PTR(dup_string(one_time_message)), RSTRING_LEN(one_time_message)
175
+ );
176
+ }
177
+
178
+ if(result == olm_error()){
179
+
180
+ raise_olm_error(olm_session_last_error(this));
181
+ }
182
+
183
+ return (result == 1) ? Qtrue : Qfalse;
184
+ }
185
+
186
+ static VALUE message_type(VALUE self)
187
+ {
188
+ OlmSession *this;
189
+ VALUE retval;
190
+ Data_Get_Struct(self, OlmSession, this);
191
+
192
+ if(olm_encrypt_message_type(this) == OLM_MESSAGE_TYPE_PRE_KEY){
193
+
194
+ retval = rb_eval_string("RubyOlm::PreKeyMessage");
195
+ }
196
+ else if(olm_encrypt_message_type(this) == OLM_MESSAGE_TYPE_MESSAGE){
197
+
198
+ retval = rb_eval_string("RubyOlm::Message");
199
+ }
200
+ else{
201
+
202
+ rb_bug("olm_encrypt_message_type()");
203
+ }
204
+
205
+ return retval;
206
+ }
207
+
208
+ static VALUE encrypt(VALUE self, VALUE plain)
209
+ {
210
+ size_t cipher_size, random_size;
211
+ void *ptr;
212
+ OlmSession *this;
213
+ VALUE retval, type;
214
+ Data_Get_Struct(self, OlmSession, this);
215
+
216
+ cipher_size = olm_encrypt_message_length(this, RSTRING_LEN(plain));
217
+ random_size = olm_encrypt_random_length(this);
218
+
219
+ if((ptr = malloc(cipher_size)) == NULL){
220
+
221
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
222
+ }
223
+
224
+ type = message_type(self);
225
+
226
+ if(olm_encrypt(this, RSTRING_PTR(plain), RSTRING_LEN(plain),
227
+ RSTRING_PTR(get_random(random_size)), random_size,
228
+ ptr, cipher_size
229
+ ) == olm_error()){
230
+ free(ptr);
231
+ raise_olm_error(olm_session_last_error(this));
232
+ }
233
+
234
+ retval = rb_funcall(type, rb_intern("new"), 1, rb_str_new(ptr, cipher_size));
235
+
236
+ free(ptr);
237
+
238
+ return retval;
239
+ }
240
+
241
+ static VALUE decrypt(VALUE self, VALUE cipher)
242
+ {
243
+ size_t plain_size, plain_max, type;
244
+ void *ptr;
245
+ OlmSession *this;
246
+ VALUE retval, data;
247
+ Data_Get_Struct(self, OlmSession, this);
248
+
249
+ if(rb_obj_is_kind_of(cipher, rb_eval_string("RubyOlm::Message")) == Qtrue){
250
+
251
+ type = OLM_MESSAGE_TYPE_MESSAGE;
252
+ }
253
+ else if(rb_obj_is_kind_of(cipher, rb_eval_string("RubyOlm::PreKeyMessage")) == Qtrue){
254
+
255
+ type = OLM_MESSAGE_TYPE_PRE_KEY;
256
+ }
257
+ else{
258
+
259
+ rb_raise(rb_eTypeError, "cipher must be kind of Message or PreKeyMessage");
260
+ }
261
+
262
+ data = rb_funcall(cipher, rb_intern("to_s"), 0);
263
+
264
+ if((plain_max = olm_decrypt_max_plaintext_length(this, type, RSTRING_PTR(dup_string(data)), RSTRING_LEN(data))) == olm_error()){
265
+
266
+ raise_olm_error(olm_session_last_error(this));
267
+ }
268
+
269
+ /* size of output will be less than size of input */
270
+ if((ptr = malloc(plain_max)) == NULL){
271
+
272
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
273
+ }
274
+
275
+ if((plain_size = olm_decrypt(this,
276
+ type,
277
+ RSTRING_PTR(dup_string(data)), RSTRING_LEN(data),
278
+ ptr, plain_max)) == olm_error())
279
+ {
280
+ free(ptr);
281
+ raise_olm_error(olm_session_last_error(this));
282
+ }
283
+
284
+ retval = rb_str_new(ptr, plain_size);
285
+
286
+ free(ptr);
287
+
288
+ return retval;
289
+ }
290
+
291
+ static VALUE to_pickle(int argc, VALUE *argv, VALUE self)
292
+ {
293
+ VALUE password, retval;
294
+ OlmSession *this;
295
+ void *ptr;
296
+ size_t size;
297
+ Data_Get_Struct(self, OlmSession, this);
298
+
299
+ (void)rb_scan_args(argc, argv, "01", &password);
300
+
301
+ password = (password == Qnil) ? rb_str_new2("") : password;
302
+
303
+ size = olm_pickle_session_length(this);
304
+
305
+ if((ptr = malloc(size)) == NULL){
306
+
307
+ rb_raise(rb_eNoMemError, "%s()", __FUNCTION__);
308
+ }
309
+
310
+ if(olm_pickle_session(this, RSTRING_PTR(password), RSTRING_LEN(password), ptr, size) != size){
311
+
312
+ free(ptr);
313
+ raise_olm_error(olm_session_last_error(this));
314
+ }
315
+
316
+ retval = rb_str_new(ptr, size);
317
+
318
+ free(ptr);
319
+
320
+ return retval;
321
+ }
322
+
323
+ static void _free(void *ptr)
324
+ {
325
+ olm_clear_session(ptr);
326
+ free(ptr);
327
+ }
328
+
329
+ static VALUE _alloc(VALUE klass)
330
+ {
331
+ OlmSession *this;
332
+ VALUE self;
333
+
334
+ self = Data_Wrap_Struct(klass, 0, _free, calloc(1, olm_session_size()));
335
+
336
+ Data_Get_Struct(self, OlmSession, this);
337
+
338
+ (void)olm_session((void *)this);
339
+
340
+ return self;
341
+ }
342
+
343
+ void session_init(void)
344
+ {
345
+ VALUE cRubyOLM = rb_define_module("RubyOlm");
346
+ VALUE cSession = rb_define_class_under(cRubyOLM, "Session", rb_cObject);
347
+ VALUE cSessionOut = rb_define_class_under(cRubyOLM, "OutboundSession", cSession);
348
+ VALUE cSessionIn = rb_define_class_under(cRubyOLM, "InboundSession", cSession);
349
+
350
+ rb_define_alloc_func(cSession, _alloc);
351
+
352
+ rb_define_method(cSessionOut, "initialize", initialize_outbound, 3);
353
+ rb_define_method(cSessionIn, "initialize", initialize_inbound, -1);
354
+
355
+ rb_define_method(cSession, "initialize", initialize, -1);
356
+ rb_define_method(cSession, "id", get_session_id, 0);
357
+ rb_define_method(cSession, "last_error", last_error, 0);
358
+ rb_define_method(cSession, "has_received_message", has_received_message, 0);
359
+ rb_define_method(cSession, "encrypt", encrypt, 1);
360
+ rb_define_method(cSession, "decrypt", decrypt, 1);
361
+ rb_define_method(cSession, "to_pickle", to_pickle, -1);
362
+ rb_define_method(cSession, "will_receive?", will_receive, -1);
363
+ }