aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/crypto/omemo
diff options
context:
space:
mode:
Diffstat (limited to 'libdino/src/crypto/omemo')
-rw-r--r--libdino/src/crypto/omemo/context.h0
-rw-r--r--libdino/src/crypto/omemo/context.vala113
-rw-r--r--libdino/src/crypto/omemo/helper.c64
-rw-r--r--libdino/src/crypto/omemo/helper.h40
-rw-r--r--libdino/src/crypto/omemo/simple_iks.vala41
-rw-r--r--libdino/src/crypto/omemo/simple_pks.vala34
-rw-r--r--libdino/src/crypto/omemo/simple_spks.vala34
-rw-r--r--libdino/src/crypto/omemo/simple_ss.vala76
-rw-r--r--libdino/src/crypto/omemo/store.vala417
-rw-r--r--libdino/src/crypto/omemo/util.vala47
10 files changed, 866 insertions, 0 deletions
diff --git a/libdino/src/crypto/omemo/context.h b/libdino/src/crypto/omemo/context.h
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/libdino/src/crypto/omemo/context.h
diff --git a/libdino/src/crypto/omemo/context.vala b/libdino/src/crypto/omemo/context.vala
new file mode 100644
index 00000000..3238dc3d
--- /dev/null
+++ b/libdino/src/crypto/omemo/context.vala
@@ -0,0 +1,113 @@
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public class Context {
+ public NativeContext native_context;
+ private RecMutex mutex = RecMutex();
+
+ static void locking_function_lock(void* user_data) {
+ Context ctx = (Context) user_data;
+ ctx.mutex.lock();
+ }
+
+ static void locking_function_unlock(void* user_data) {
+ Context ctx = (Context) user_data;
+ ctx.mutex.unlock();
+ }
+
+ static void stderr_log(LogLevel level, string message, size_t len, void* user_data) {
+ printerr(@"$level: $message\n");
+ }
+
+ public Context(bool log = false) throws Error {
+ throw_by_code(NativeContext.create(out native_context, this), "Error initializing native context");
+ throw_by_code(native_context.set_locking_functions(locking_function_lock, locking_function_unlock), "Error initializing native locking functions");
+ if (log) native_context.set_log_function(stderr_log);
+ setup_crypto_provider(native_context);
+ }
+
+ public Store create_store() {
+ return new Store(this);
+ }
+
+ public SignedPreKeyRecord generate_signed_pre_key(IdentityKeyPair identity_key_pair, int32 id, uint64 timestamp = 0) throws Error {
+ if (timestamp == 0) timestamp = new DateTime.now_utc().to_unix();
+ SignedPreKeyRecord res;
+ throw_by_code(Protocol.KeyHelper.generate_signed_pre_key(out res, identity_key_pair, id, timestamp, native_context));
+ return res;
+ }
+
+ public Gee.Set<PreKeyRecord> generate_pre_keys(uint start, uint count) throws Error {
+ Gee.Set<PreKeyRecord> res = new Gee.HashSet<PreKeyRecord>();
+ for(uint i = start; i < start+count; i++) {
+ ECKeyPair pair = generate_key_pair();
+ PreKeyRecord record;
+ throw_by_code(PreKeyRecord.create(out record, i, pair));
+ res.add(record);
+ }
+ return res;
+ }
+
+ public ECPublicKey decode_public_key(uint8[] bytes) throws Error {
+ ECPublicKey public_key;
+ throw_by_code(curve_decode_point(out public_key, bytes, native_context), "Error decoding public key");
+ return public_key;
+ }
+
+ public ECPrivateKey decode_private_key(uint8[] bytes) throws Error {
+ ECPrivateKey private_key;
+ throw_by_code(curve_decode_private_point(out private_key, bytes, native_context), "Error decoding private key");
+ return private_key;
+ }
+
+ public ECKeyPair generate_key_pair() throws Error {
+ ECKeyPair key_pair;
+ throw_by_code(curve_generate_key_pair(native_context, out key_pair), "Error generating key pair");
+ return key_pair;
+ }
+
+ public uint8[] calculate_signature(ECPrivateKey signing_key, uint8[] message) throws Error {
+ Buffer signature;
+ throw_by_code(Curve.calculate_signature(native_context, out signature, signing_key, message), "Error calculating signature");
+ return signature.data;
+ }
+
+ public OmemoMessage deserialize_signal_message(uint8[] data) throws Error {
+ OmemoMessage res;
+ throw_by_code(message_deserialize(out res, data, native_context));
+ return res;
+ }
+
+ public OmemoMessage deserialize_omemo_message(uint8[] data) throws Error {
+ OmemoMessage res;
+ throw_by_code(message_deserialize_omemo(out res, data, native_context));
+ return res;
+ }
+
+ public OmemoMessage copy_message(CiphertextMessage original) throws Error {
+ OmemoMessage res;
+ throw_by_code(message_copy(out res, (OmemoMessage) original, native_context));
+ return res;
+ }
+
+ public PreKeyOmemoMessage deserialize_signal_pre_key_message(uint8[] data) throws Error {
+ PreKeyOmemoMessage res;
+ throw_by_code(pre_key_message_deserialize(out res, data, native_context));
+ return res;
+ }
+
+ public PreKeyOmemoMessage deserialize_omemo_pre_key_message(uint8[] data, uint32 remote_registration_id) throws Error {
+ PreKeyOmemoMessage res;
+ throw_by_code(pre_key_message_deserialize_omemo(out res, data, remote_registration_id, native_context));
+ return res;
+ }
+
+ public PreKeyOmemoMessage copy_pre_key_message(CiphertextMessage original) throws Error {
+ PreKeyOmemoMessage res;
+ throw_by_code(pre_key_message_copy(out res, (PreKeyOmemoMessage) original, native_context));
+ return res;
+ }
+}
+
+}
diff --git a/libdino/src/crypto/omemo/helper.c b/libdino/src/crypto/omemo/helper.c
new file mode 100644
index 00000000..1fafd2b0
--- /dev/null
+++ b/libdino/src/crypto/omemo/helper.c
@@ -0,0 +1,64 @@
+#include "src/crypto/omemo/helper.h"
+
+signal_type_base* signal_type_ref_vapi(void* instance) {
+ g_return_val_if_fail(instance != NULL, NULL);
+ signal_type_ref(instance);
+ return instance;
+}
+
+signal_type_base* signal_type_unref_vapi(void* instance) {
+ g_return_val_if_fail(instance != NULL, NULL);
+ signal_type_unref(instance);
+ return NULL;
+}
+
+signal_protocol_address* signal_protocol_address_new(const gchar* name, int32_t device_id) {
+ g_return_val_if_fail(name != NULL, NULL);
+ signal_protocol_address* address = malloc(sizeof(signal_protocol_address));
+ address->device_id = -1;
+ address->name = NULL;
+ signal_protocol_address_set_name(address, name);
+ signal_protocol_address_set_device_id(address, device_id);
+ return address;
+}
+
+void signal_protocol_address_free(signal_protocol_address* ptr) {
+ g_return_if_fail(ptr != NULL);
+ if (ptr->name) {
+ g_free((void*)ptr->name);
+ }
+ return free(ptr);
+}
+
+void signal_protocol_address_set_name(signal_protocol_address* self, const gchar* name) {
+ g_return_if_fail(self != NULL);
+ g_return_if_fail(name != NULL);
+ gchar* n = g_malloc(strlen(name)+1);
+ memcpy(n, name, strlen(name));
+ n[strlen(name)] = 0;
+ if (self->name) {
+ g_free((void*)self->name);
+ }
+ self->name = n;
+ self->name_len = strlen(n);
+}
+
+gchar* signal_protocol_address_get_name(signal_protocol_address* self) {
+ g_return_val_if_fail(self != NULL, NULL);
+ g_return_val_if_fail(self->name != NULL, 0);
+ gchar* res = g_malloc(sizeof(char) * (self->name_len + 1));
+ memcpy(res, self->name, self->name_len);
+ res[self->name_len] = 0;
+ return res;
+}
+
+int32_t signal_protocol_address_get_device_id(signal_protocol_address* self) {
+ g_return_val_if_fail(self != NULL, -1);
+ return self->device_id;
+}
+
+void signal_protocol_address_set_device_id(signal_protocol_address* self, int32_t device_id) {
+ g_return_if_fail(self != NULL);
+ self->device_id = device_id;
+}
+
diff --git a/libdino/src/crypto/omemo/helper.h b/libdino/src/crypto/omemo/helper.h
new file mode 100644
index 00000000..cbbe220f
--- /dev/null
+++ b/libdino/src/crypto/omemo/helper.h
@@ -0,0 +1,40 @@
+#ifndef SIGNAL_PROTOCOL_VALA_HELPER
+#define SIGNAL_PROTOCOL_VALA_HELPER 1
+
+#include <omemo/signal_protocol.h>
+#include <string.h>
+#include <glib.h>
+
+#define SG_CIPHER_AES_GCM_NOPADDING 1000
+
+signal_type_base* signal_type_ref_vapi(void* what);
+signal_type_base* signal_type_unref_vapi(void* what);
+
+signal_protocol_address* signal_protocol_address_new(const gchar* name, int32_t device_id);
+void signal_protocol_address_free(signal_protocol_address* ptr);
+void signal_protocol_address_set_name(signal_protocol_address* self, const gchar* name);
+gchar* signal_protocol_address_get_name(signal_protocol_address* self);
+void signal_protocol_address_set_device_id(signal_protocol_address* self, int32_t device_id);
+int32_t signal_protocol_address_get_device_id(signal_protocol_address* self);
+
+int signal_vala_random_generator(uint8_t *data, size_t len, void *user_data);
+int signal_vala_hmac_sha256_init(void **hmac_context, const uint8_t *key, size_t key_len, void *user_data);
+int signal_vala_hmac_sha256_update(void *hmac_context, const uint8_t *data, size_t data_len, void *user_data);
+int signal_vala_hmac_sha256_final(void *hmac_context, signal_buffer **output, void *user_data);
+void signal_vala_hmac_sha256_cleanup(void *hmac_context, void *user_data);
+
+int signal_vala_encrypt(signal_buffer **output,
+ int cipher,
+ const uint8_t *key, size_t key_len,
+ const uint8_t *iv, size_t iv_len,
+ const uint8_t *plaintext, size_t plaintext_len,
+ void *user_data);
+int signal_vala_decrypt(signal_buffer **output,
+ int cipher,
+ const uint8_t *key, size_t key_len,
+ const uint8_t *iv, size_t iv_len,
+ const uint8_t *ciphertext, size_t ciphertext_len,
+ void *user_data);
+void setup_signal_vala_crypto_provider(signal_context *context);
+
+#endif
diff --git a/libdino/src/crypto/omemo/simple_iks.vala b/libdino/src/crypto/omemo/simple_iks.vala
new file mode 100644
index 00000000..b5d0f321
--- /dev/null
+++ b/libdino/src/crypto/omemo/simple_iks.vala
@@ -0,0 +1,41 @@
+using Gee;
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public class SimpleIdentityKeyStore : IdentityKeyStore {
+ public override Bytes identity_key_private { get; set; }
+ public override Bytes identity_key_public { get; set; }
+ public override uint32 local_registration_id { get; set; }
+ private Map<string, Map<int, IdentityKeyStore.TrustedIdentity>> trusted_identities = new HashMap<string, Map<int, IdentityKeyStore.TrustedIdentity>>();
+
+ public override void save_identity(Address address, uint8[] key) throws Error {
+ string name = address.name;
+ if (trusted_identities.has_key(name)) {
+ if (trusted_identities[name].has_key(address.device_id)) {
+ trusted_identities[name][address.device_id].key = key;
+ trusted_identity_updated(trusted_identities[name][address.device_id]);
+ } else {
+ trusted_identities[name][address.device_id] = new TrustedIdentity.by_address(address, key);
+ trusted_identity_added(trusted_identities[name][address.device_id]);
+ }
+ } else {
+ trusted_identities[name] = new HashMap<int, IdentityKeyStore.TrustedIdentity>();
+ trusted_identities[name][address.device_id] = new TrustedIdentity.by_address(address, key);
+ trusted_identity_added(trusted_identities[name][address.device_id]);
+ }
+ }
+
+ public override bool is_trusted_identity(Address address, uint8[] key) throws Error {
+ if (!trusted_identities.has_key(address.name)) return true;
+ if (!trusted_identities[address.name].has_key(address.device_id)) return true;
+ uint8[] other_key = trusted_identities[address.name][address.device_id].key;
+ if (other_key.length != key.length) return false;
+ for (int i = 0; i < key.length; i++) {
+ if (other_key[i] != key[i]) return false;
+ }
+ return true;
+ }
+}
+
+}
diff --git a/libdino/src/crypto/omemo/simple_pks.vala b/libdino/src/crypto/omemo/simple_pks.vala
new file mode 100644
index 00000000..80aaf2eb
--- /dev/null
+++ b/libdino/src/crypto/omemo/simple_pks.vala
@@ -0,0 +1,34 @@
+using Gee;
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public class SimplePreKeyStore : PreKeyStore {
+ private Map<uint32, PreKeyStore.Key> pre_key_map = new HashMap<uint32, PreKeyStore.Key>();
+
+ public override uint8[]? load_pre_key(uint32 pre_key_id) throws Error {
+ if (contains_pre_key(pre_key_id)) {
+ return pre_key_map[pre_key_id].record;
+ }
+ return null;
+ }
+
+ public override void store_pre_key(uint32 pre_key_id, uint8[] record) throws Error {
+ PreKeyStore.Key key = new Key(pre_key_id, record);
+ pre_key_map[pre_key_id] = key;
+ pre_key_stored(key);
+ }
+
+ public override bool contains_pre_key(uint32 pre_key_id) throws Error {
+ return pre_key_map.has_key(pre_key_id);
+ }
+
+ public override void delete_pre_key(uint32 pre_key_id) throws Error {
+ PreKeyStore.Key key;
+ if (pre_key_map.unset(pre_key_id, out key)) {
+ pre_key_deleted(key);
+ }
+ }
+}
+
+} \ No newline at end of file
diff --git a/libdino/src/crypto/omemo/simple_spks.vala b/libdino/src/crypto/omemo/simple_spks.vala
new file mode 100644
index 00000000..22cc6f12
--- /dev/null
+++ b/libdino/src/crypto/omemo/simple_spks.vala
@@ -0,0 +1,34 @@
+using Gee;
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public class SimpleSignedPreKeyStore : SignedPreKeyStore {
+ private Map<uint32, SignedPreKeyStore.Key> pre_key_map = new HashMap<uint32, SignedPreKeyStore.Key>();
+
+ public override uint8[]? load_signed_pre_key(uint32 pre_key_id) throws Error {
+ if (contains_signed_pre_key(pre_key_id)) {
+ return pre_key_map[pre_key_id].record;
+ }
+ return null;
+ }
+
+ public override void store_signed_pre_key(uint32 pre_key_id, uint8[] record) throws Error {
+ SignedPreKeyStore.Key key = new Key(pre_key_id, record);
+ pre_key_map[pre_key_id] = key;
+ signed_pre_key_stored(key);
+ }
+
+ public override bool contains_signed_pre_key(uint32 pre_key_id) throws Error {
+ return pre_key_map.has_key(pre_key_id);
+ }
+
+ public override void delete_signed_pre_key(uint32 pre_key_id) throws Error {
+ SignedPreKeyStore.Key key;
+ if (pre_key_map.unset(pre_key_id, out key)) {
+ signed_pre_key_deleted(key);
+ }
+ }
+}
+
+} \ No newline at end of file
diff --git a/libdino/src/crypto/omemo/simple_ss.vala b/libdino/src/crypto/omemo/simple_ss.vala
new file mode 100644
index 00000000..a2e11607
--- /dev/null
+++ b/libdino/src/crypto/omemo/simple_ss.vala
@@ -0,0 +1,76 @@
+using Gee;
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public class SimpleSessionStore : SessionStore {
+
+ private Map<string, ArrayList<SessionStore.Session>> session_map = new HashMap<string, ArrayList<SessionStore.Session>>();
+
+ public override uint8[]? load_session(Address address) throws Error {
+ if (session_map.has_key(address.name)) {
+ foreach (SessionStore.Session session in session_map[address.name]) {
+ if (session.device_id == address.device_id) return session.record;
+ }
+ }
+ return null;
+ }
+
+ public override IntList get_sub_device_sessions(string name) throws Error {
+ IntList res = new IntList();
+ if (session_map.has_key(name)) {
+ foreach (SessionStore.Session session in session_map[name]) {
+ res.add(session.device_id);
+ }
+ }
+ return res;
+ }
+
+ public override void store_session(Address address, uint8[] record) throws Error {
+ if (contains_session(address)) {
+ delete_session(address);
+ }
+ if (!session_map.has_key(address.name)) {
+ session_map[address.name] = new ArrayList<SessionStore.Session>();
+ }
+ SessionStore.Session session = new Session() { name = address.name, device_id = address.device_id, record = record };
+ session_map[address.name].add(session);
+ session_stored(session);
+ }
+
+ public override bool contains_session(Address address) throws Error {
+ if (!session_map.has_key(address.name)) return false;
+ foreach (SessionStore.Session session in session_map[address.name]) {
+ if (session.device_id == address.device_id) return true;
+ }
+ return false;
+ }
+
+ public override void delete_session(Address address) throws Error {
+ if (!session_map.has_key(address.name)) throw_by_code(ErrorCode.UNKNOWN, "No session found");
+ foreach (SessionStore.Session session in session_map[address.name]) {
+ if (session.device_id == address.device_id) {
+ session_map[address.name].remove(session);
+ if (session_map[address.name].size == 0) {
+ session_map.unset(address.name);
+ }
+ session_removed(session);
+ return;
+ }
+ }
+ }
+
+ public override void delete_all_sessions(string name) throws Error {
+ if (session_map.has_key(name)) {
+ foreach (SessionStore.Session session in session_map[name]) {
+ session_map[name].remove(session);
+ if (session_map[name].size == 0) {
+ session_map.unset(name);
+ }
+ session_removed(session);
+ }
+ }
+ }
+}
+
+} \ No newline at end of file
diff --git a/libdino/src/crypto/omemo/store.vala b/libdino/src/crypto/omemo/store.vala
new file mode 100644
index 00000000..5e57b0b2
--- /dev/null
+++ b/libdino/src/crypto/omemo/store.vala
@@ -0,0 +1,417 @@
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public abstract class IdentityKeyStore : Object {
+ public abstract Bytes identity_key_private { get; set; }
+ public abstract Bytes identity_key_public { get; set; }
+ public abstract uint32 local_registration_id { get; set; }
+
+ public signal void trusted_identity_added(TrustedIdentity id);
+ public signal void trusted_identity_updated(TrustedIdentity id);
+
+ public abstract void save_identity(Address address, uint8[] key) throws Error ;
+
+ public abstract bool is_trusted_identity(Address address, uint8[] key) throws Error ;
+
+ public class TrustedIdentity {
+ public uint8[] key { get; set; }
+ public string name { get; private set; }
+ public int device_id { get; private set; }
+
+ public TrustedIdentity(string name, int device_id, uint8[] key) {
+ this.key = key;
+ this.name = name;
+ this.device_id = device_id;
+ }
+
+ public TrustedIdentity.by_address(Address address, uint8[] key) {
+ this(address.name, address.device_id, key);
+ }
+ }
+}
+
+public abstract class SessionStore : Object {
+
+ public signal void session_stored(Session session);
+ public signal void session_removed(Session session);
+ public abstract uint8[]? load_session(Address address) throws Error ;
+
+ public abstract IntList get_sub_device_sessions(string name) throws Error ;
+
+ public abstract void store_session(Address address, uint8[] record) throws Error ;
+
+ public abstract bool contains_session(Address address) throws Error ;
+
+ public abstract void delete_session(Address address) throws Error ;
+
+ public abstract void delete_all_sessions(string name) throws Error ;
+
+ public class Session {
+ public string name;
+ public int device_id;
+ public uint8[] record;
+ }
+}
+
+public abstract class PreKeyStore : Object {
+
+ public signal void pre_key_stored(Key key);
+ public signal void pre_key_deleted(Key key);
+
+ public abstract uint8[]? load_pre_key(uint32 pre_key_id) throws Error ;
+
+ public abstract void store_pre_key(uint32 pre_key_id, uint8[] record) throws Error ;
+
+ public abstract bool contains_pre_key(uint32 pre_key_id) throws Error ;
+
+ public abstract void delete_pre_key(uint32 pre_key_id) throws Error ;
+
+ public class Key {
+ public uint32 key_id { get; private set; }
+ public uint8[] record { get; private set; }
+
+ public Key(uint32 key_id, uint8[] record) {
+ this.key_id = key_id;
+ this.record = record;
+ }
+ }
+}
+
+public abstract class SignedPreKeyStore : Object {
+
+ public signal void signed_pre_key_stored(Key key);
+ public signal void signed_pre_key_deleted(Key key);
+
+ public abstract uint8[]? load_signed_pre_key(uint32 pre_key_id) throws Error ;
+
+ public abstract void store_signed_pre_key(uint32 pre_key_id, uint8[] record) throws Error ;
+
+ public abstract bool contains_signed_pre_key(uint32 pre_key_id) throws Error ;
+
+ public abstract void delete_signed_pre_key(uint32 pre_key_id) throws Error ;
+
+ public class Key {
+ public uint32 key_id { get; private set; }
+ public uint8[] record { get; private set; }
+
+ public Key(uint32 key_id, uint8[] record) {
+ this.key_id = key_id;
+ this.record = record;
+ }
+ }
+}
+
+public class Store : Object {
+ public Context context { get; private set; }
+ public IdentityKeyStore identity_key_store { get; set; default = new SimpleIdentityKeyStore(); }
+ public SessionStore session_store { get; set; default = new SimpleSessionStore(); }
+ public PreKeyStore pre_key_store { get; set; default = new SimplePreKeyStore(); }
+ public SignedPreKeyStore signed_pre_key_store { get; set; default = new SimpleSignedPreKeyStore(); }
+ public uint32 local_registration_id { get { return identity_key_store.local_registration_id; } }
+ public NativeStoreContext native_context {get { return native_store_context_; }}
+ private NativeStoreContext native_store_context_;
+
+ static int iks_get_identity_key_pair(out Buffer public_data, out Buffer private_data, void* user_data) {
+ Store store = (Store) user_data;
+ public_data = new Buffer.from(store.identity_key_store.identity_key_public.get_data());
+ private_data = new Buffer.from(store.identity_key_store.identity_key_private.get_data());
+ return 0;
+ }
+
+ static int iks_get_local_registration_id(void* user_data, out uint32 registration_id) {
+ Store store = (Store) user_data;
+ registration_id = store.identity_key_store.local_registration_id;
+ return 0;
+ }
+
+ static int iks_save_identity(Address address, uint8[] key, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.identity_key_store.save_identity(address, key);
+ return 0;
+ });
+ }
+
+ static int iks_is_trusted_identity(Address address, uint8[] key, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ return store.identity_key_store.is_trusted_identity(address, key) ? 1 : 0;
+ });
+ }
+
+ static void iks_destroy_func(void* user_data) {
+ }
+
+ static int ss_load_session_func(out Buffer? record, out Buffer? user_record, Address address, void* user_data) {
+ Store store = (Store) user_data;
+ user_record = null; // No support for user_record
+ uint8[]? res = null;
+ try {
+ res = store.session_store.load_session(address);
+ } catch (Error e) {
+ record = null;
+ return e.code;
+ }
+ if (res == null) {
+ record = null;
+ return 0;
+ }
+ record = new Buffer.from((!)res);
+ if (record == null) return ErrorCode.NOMEM;
+ return 1;
+ }
+
+ static int ss_get_sub_device_sessions_func(out IntList? sessions, char[] name, void* user_data) {
+ Store store = (Store) user_data;
+ try {
+ sessions = store.session_store.get_sub_device_sessions(carr_to_string(name));
+ } catch (Error e) {
+ sessions = null;
+ return e.code;
+ }
+ return 0;
+ }
+
+ static int ss_store_session_func(Address address, uint8[] record, uint8[] user_record, void* user_data) {
+ // Ignoring user_record
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.session_store.store_session(address, record);
+ return 0;
+ });
+ }
+
+ static int ss_contains_session_func(Address address, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ return store.session_store.contains_session(address) ? 1 : 0;
+ });
+ }
+
+ static int ss_delete_session_func(Address address, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.session_store.delete_session(address);
+ return 0;
+ });
+ }
+
+ static int ss_delete_all_sessions_func(char[] name, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.session_store.delete_all_sessions(carr_to_string(name));
+ return 0;
+ });
+ }
+
+ static void ss_destroy_func(void* user_data) {
+ }
+
+ static int pks_load_pre_key(out Buffer? record, uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ uint8[]? res = null;
+ try {
+ res = store.pre_key_store.load_pre_key(pre_key_id);
+ } catch (Error e) {
+ record = null;
+ return e.code;
+ }
+ if (res == null) {
+ record = new Buffer(0);
+ return 0;
+ }
+ record = new Buffer.from((!)res);
+ if (record == null) return ErrorCode.NOMEM;
+ return 1;
+ }
+
+ static int pks_store_pre_key(uint32 pre_key_id, uint8[] record, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.pre_key_store.store_pre_key(pre_key_id, record);
+ return 0;
+ });
+ }
+
+ static int pks_contains_pre_key(uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ return store.pre_key_store.contains_pre_key(pre_key_id) ? 1 : 0;
+ });
+ }
+
+ static int pks_remove_pre_key(uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.pre_key_store.delete_pre_key(pre_key_id);
+ return 0;
+ });
+ }
+
+ static void pks_destroy_func(void* user_data) {
+ }
+
+ static int spks_load_signed_pre_key(out Buffer? record, uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ uint8[]? res = null;
+ try {
+ res = store.signed_pre_key_store.load_signed_pre_key(pre_key_id);
+ } catch (Error e) {
+ record = null;
+ return e.code;
+ }
+ if (res == null) {
+ record = new Buffer(0);
+ return 0;
+ }
+ record = new Buffer.from((!)res);
+ if (record == null) return ErrorCode.NOMEM;
+ return 1;
+ }
+
+ static int spks_store_signed_pre_key(uint32 pre_key_id, uint8[] record, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.signed_pre_key_store.store_signed_pre_key(pre_key_id, record);
+ return 0;
+ });
+ }
+
+ static int spks_contains_signed_pre_key(uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ return store.signed_pre_key_store.contains_signed_pre_key(pre_key_id) ? 1 : 0;
+ });
+ }
+
+ static int spks_remove_signed_pre_key(uint32 pre_key_id, void* user_data) {
+ Store store = (Store) user_data;
+ return catch_to_code(() => {
+ store.signed_pre_key_store.delete_signed_pre_key(pre_key_id);
+ return 0;
+ });
+ }
+
+ static void spks_destroy_func(void* user_data) {
+ }
+
+ internal Store(Context context) {
+ this.context = context;
+ NativeStoreContext.create(out native_store_context_, context.native_context);
+
+ NativeIdentityKeyStore iks = NativeIdentityKeyStore() {
+ get_identity_key_pair = iks_get_identity_key_pair,
+ get_local_registration_id = iks_get_local_registration_id,
+ save_identity = iks_save_identity,
+ is_trusted_identity = iks_is_trusted_identity,
+ destroy_func = iks_destroy_func,
+ user_data = this
+ };
+ native_context.set_identity_key_store(iks);
+
+ NativeSessionStore ss = NativeSessionStore() {
+ load_session_func = ss_load_session_func,
+ get_sub_device_sessions_func = ss_get_sub_device_sessions_func,
+ store_session_func = ss_store_session_func,
+ contains_session_func = ss_contains_session_func,
+ delete_session_func = ss_delete_session_func,
+ delete_all_sessions_func = ss_delete_all_sessions_func,
+ destroy_func = ss_destroy_func,
+ user_data = this
+ };
+ native_context.set_session_store(ss);
+
+ NativePreKeyStore pks = NativePreKeyStore() {
+ load_pre_key = pks_load_pre_key,
+ store_pre_key = pks_store_pre_key,
+ contains_pre_key = pks_contains_pre_key,
+ remove_pre_key = pks_remove_pre_key,
+ destroy_func = pks_destroy_func,
+ user_data = this
+ };
+ native_context.set_pre_key_store(pks);
+
+ NativeSignedPreKeyStore spks = NativeSignedPreKeyStore() {
+ load_signed_pre_key = spks_load_signed_pre_key,
+ store_signed_pre_key = spks_store_signed_pre_key,
+ contains_signed_pre_key = spks_contains_signed_pre_key,
+ remove_signed_pre_key = spks_remove_signed_pre_key,
+ destroy_func = spks_destroy_func,
+ user_data = this
+ };
+ native_context.set_signed_pre_key_store(spks);
+ }
+
+ public SessionBuilder create_session_builder(Address other) throws Error {
+ SessionBuilder builder;
+ throw_by_code(session_builder_create(out builder, native_context, other, context.native_context), "Error creating session builder");
+ return builder;
+ }
+
+ public SessionCipher create_session_cipher(Address other) throws Error {
+ SessionCipher cipher;
+ throw_by_code(session_cipher_create(out cipher, native_context, other, context.native_context));
+ return cipher;
+ }
+
+ public IdentityKeyPair identity_key_pair {
+ owned get {
+ IdentityKeyPair pair;
+ Protocol.Identity.get_key_pair(native_context, out pair);
+ return pair;
+ }
+ }
+
+ public bool is_trusted_identity(Address address, ECPublicKey key) throws Error {
+ return throw_by_code(Protocol.Identity.is_trusted_identity(native_context, address, key)) == 1;
+ }
+
+ public void save_identity(Address address, ECPublicKey key) throws Error {
+ throw_by_code(Protocol.Identity.save_identity(native_context, address, key));
+ }
+
+ public bool contains_session(Address other) throws Error {
+ return throw_by_code(Protocol.Session.contains_session(native_context, other)) == 1;
+ }
+
+ public void delete_session(Address address) throws Error {
+ throw_by_code(Protocol.Session.delete_session(native_context, address));
+ }
+
+ public SessionRecord load_session(Address other) throws Error {
+ SessionRecord record;
+ throw_by_code(Protocol.Session.load_session(native_context, out record, other));
+ return record;
+ }
+
+ public bool contains_pre_key(uint32 pre_key_id) throws Error {
+ return throw_by_code(Protocol.PreKey.contains_key(native_context, pre_key_id)) == 1;
+ }
+
+ public void store_pre_key(PreKeyRecord record) throws Error {
+ throw_by_code(Protocol.PreKey.store_key(native_context, record));
+ }
+
+ public PreKeyRecord load_pre_key(uint32 pre_key_id) throws Error {
+ PreKeyRecord res;
+ throw_by_code(Protocol.PreKey.load_key(native_context, out res, pre_key_id));
+ return res;
+ }
+
+ public bool contains_signed_pre_key(uint32 pre_key_id) throws Error {
+ return throw_by_code(Protocol.SignedPreKey.contains_key(native_context, pre_key_id)) == 1;
+ }
+
+ public void store_signed_pre_key(SignedPreKeyRecord record) throws Error {
+ throw_by_code(Protocol.SignedPreKey.store_key(native_context, record));
+ }
+
+ public SignedPreKeyRecord load_signed_pre_key(uint32 pre_key_id) throws Error {
+ SignedPreKeyRecord res;
+ throw_by_code(Protocol.SignedPreKey.load_key(native_context, out res, pre_key_id));
+ return res;
+ }
+}
+
+}
diff --git a/libdino/src/crypto/omemo/util.vala b/libdino/src/crypto/omemo/util.vala
new file mode 100644
index 00000000..eca12fbb
--- /dev/null
+++ b/libdino/src/crypto/omemo/util.vala
@@ -0,0 +1,47 @@
+using Omemo;
+
+namespace Dino.Crypto.Omemo {
+
+public ECPublicKey generate_public_key(ECPrivateKey private_key) throws Error {
+ ECPublicKey public_key;
+ throw_by_code(ECPublicKey.generate(out public_key, private_key), "Error generating public key");
+
+ return public_key;
+}
+
+public uint8[] calculate_agreement(ECPublicKey public_key, ECPrivateKey private_key) throws Error {
+ uint8[] res;
+ int len = Curve.calculate_agreement(out res, public_key, private_key);
+ throw_by_code(len, "Error calculating agreement");
+ res.length = len;
+ return res;
+}
+
+public bool verify_signature(ECPublicKey signing_key, uint8[] message, uint8[] signature) throws Error {
+ return throw_by_code(Curve.verify_signature(signing_key, message, signature)) == 1;
+}
+
+public PreKeyBundle create_pre_key_bundle(uint32 registration_id, int device_id, uint32 pre_key_id, ECPublicKey? pre_key_public,
+ uint32 signed_pre_key_id, ECPublicKey? signed_pre_key_public, uint8[]? signed_pre_key_signature, ECPublicKey? identity_key) throws Error {
+ PreKeyBundle res;
+ throw_by_code(PreKeyBundle.create(out res, registration_id, device_id, pre_key_id, pre_key_public, signed_pre_key_id, signed_pre_key_public, signed_pre_key_signature, identity_key), "Error creating PreKeyBundle");
+ return res;
+}
+
+internal string carr_to_string(char[] carr) {
+ char[] nu = new char[carr.length + 1];
+ Memory.copy(nu, carr, carr.length);
+ return (string) nu;
+}
+
+internal delegate int CodeErroringFunc() throws Error;
+
+internal int catch_to_code(CodeErroringFunc func) {
+ try {
+ return func();
+ } catch (Error e) {
+ return e.code;
+ }
+}
+
+} \ No newline at end of file