about summary refs log tree commit diff stats
path: root/src/omemo
diff options
context:
space:
mode:
Diffstat (limited to 'src/omemo')
-rw-r--r--src/omemo/omemo.c134
-rw-r--r--src/omemo/omemo.h10
2 files changed, 125 insertions, 19 deletions
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 1b2998b0..080baf1b 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -1,26 +1,41 @@
+#include <sys/time.h>
+
+#include <glib.h>
 #include <pthread.h>
+#include <signal/key_helper.h>
 #include <signal/signal_protocol.h>
+#include <sodium.h>
 
 #include "config/account.h"
-#include "ui/ui.h"
-#include "omemo/omemo.h"
+#include "log.h"
 #include "omemo/crypto.h"
+#include "omemo/omemo.h"
+#include "ui/ui.h"
+#include "xmpp/omemo.h"
+
+static gboolean loaded;
 
 static void lock(void *user_data);
 static void unlock(void *user_data);
-static void omemo_load_crypto_materials(ProfAccount *account);
 
 struct omemo_context_t {
     pthread_mutexattr_t attr;
     pthread_mutex_t lock;
+    signal_context *signal;
+    uint32_t device_id;
+    GList *device_list;
+    ratchet_identity_key_pair *identity_key_pair;
+    uint32_t registration_id;
+    signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
+    session_signed_pre_key *signed_pre_key;
 };
 
+static omemo_context omemo_ctx;
+
 void
 omemo_init(void)
 {
     log_info("Initialising OMEMO");
-    signal_context *signal_ctx;
-    omemo_context *ctx = malloc(sizeof(omemo_context));
     signal_crypto_provider crypto_provider = {
         .random_func = omemo_random_func,
         .hmac_sha256_init_func = omemo_hmac_sha256_init_func,
@@ -40,35 +55,116 @@ omemo_init(void)
         cons_show("Error initializing OMEMO crypto");
     }
 
-    pthread_mutexattr_init(&ctx->attr);
-    pthread_mutexattr_settype(&ctx->attr, PTHREAD_MUTEX_RECURSIVE);
-    pthread_mutex_init(&ctx->lock, &ctx->attr);
+    pthread_mutexattr_init(&omemo_ctx.attr);
+    pthread_mutexattr_settype(&omemo_ctx.attr, PTHREAD_MUTEX_RECURSIVE);
+    pthread_mutex_init(&omemo_ctx.lock, &omemo_ctx.attr);
 
-    if (signal_context_create(&signal_ctx, ctx) != 0) {
+    if (signal_context_create(&omemo_ctx.signal, &omemo_ctx) != 0) {
         cons_show("Error initializing OMEMO context");
         return;
     }
 
-    if (signal_context_set_crypto_provider(signal_ctx, &crypto_provider) != 0) {
+    if (signal_context_set_crypto_provider(omemo_ctx.signal, &crypto_provider) != 0) {
         cons_show("Error initializing OMEMO crypto");
         return;
     }
 
-    signal_context_set_locking_functions(signal_ctx, lock, unlock);
+    signal_context_set_locking_functions(omemo_ctx.signal, lock, unlock);
+
+    loaded = FALSE;
+    omemo_ctx.device_list = NULL;
 }
 
 void
 omemo_generate_crypto_materials(ProfAccount *account)
 {
-    ratchet_identity_key_pair *identity_key_pair;
-    uint32_t registration_id;
-    signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
-    session_signed_pre_key *signed_pre_key;
+    omemo_ctx.device_id = randombytes_uniform(0x80000000);
+    omemo_ctx.device_list = g_list_append(omemo_ctx.device_list, GINT_TO_POINTER(omemo_ctx.device_id));
+    signal_protocol_key_helper_generate_identity_key_pair(&omemo_ctx.identity_key_pair, omemo_ctx.signal);
+    signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal);
+    signal_protocol_key_helper_generate_pre_keys(&omemo_ctx.pre_keys_head, randombytes_random(), 100, omemo_ctx.signal);
+
+    struct timeval tv;
+    gettimeofday(&tv, NULL);
+    unsigned long long timestamp = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000;
+    signal_protocol_key_helper_generate_signed_pre_key(&omemo_ctx.signed_pre_key, omemo_ctx.identity_key_pair, 5, timestamp, omemo_ctx.signal);
+
+    loaded = TRUE;
+
+    omemo_devicelist_publish();
+    omemo_bundle_publish();
+}
+
+void
+omemo_start_session(ProfAccount *account, char *barejid)
+{
+
+}
 
-    signal_protocol_key_helper_generate_identity_key_pair(&identity_key_pair, global_context);
-    signal_protocol_key_helper_generate_registration_id(&registration_id, 0, global_context);
-    signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, start_id, 100, global_context);
-    signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, identity_key_pair, 5, timestamp, global_context);
+gboolean
+omemo_loaded(void)
+{
+    return loaded;
+}
+
+GList * const
+omemo_device_list(void)
+{
+    return omemo_ctx.device_list;
+}
+
+uint32_t
+omemo_device_id(void)
+{
+    return omemo_ctx.device_id;
+}
+
+void
+omemo_identity_key(unsigned char **output, size_t *length)
+{
+    signal_buffer *buffer = NULL;
+    ec_public_key_serialize(&buffer, ratchet_identity_key_pair_get_public(omemo_ctx.identity_key_pair));
+    *length = signal_buffer_len(buffer);
+    *output = malloc(*length);
+    memcpy(*output, signal_buffer_const_data(buffer), *length);
+    signal_buffer_free(buffer);
+}
+
+void
+omemo_signed_prekey(unsigned char **output, size_t *length)
+{
+    signal_buffer *buffer = NULL;
+    ec_public_key_serialize(&buffer, ec_key_pair_get_public(session_signed_pre_key_get_key_pair(omemo_ctx.signed_pre_key)));
+    *length = signal_buffer_len(buffer);
+    *output = malloc(*length);
+    memcpy(*output, signal_buffer_const_data(buffer), *length);
+    signal_buffer_free(buffer);
+}
+
+void
+omemo_signed_prekey_signature(unsigned char **output, size_t *length)
+{
+    *length = session_signed_pre_key_get_signature_len(omemo_ctx.signed_pre_key);
+    *output = malloc(*length);
+    memcpy(*output, session_signed_pre_key_get_signature(omemo_ctx.signed_pre_key), *length);
+}
+
+void
+omemo_prekeys(GList ** const prekeys, GList ** const ids, GList ** const lengths)
+{
+    signal_protocol_key_helper_pre_key_list_node *p;
+    for (p = omemo_ctx.pre_keys_head; p != NULL; p = signal_protocol_key_helper_key_list_next(p)) {
+        session_pre_key *prekey = signal_protocol_key_helper_key_list_element(p);
+        signal_buffer *buffer = NULL;
+        ec_public_key_serialize(&buffer, ec_key_pair_get_public(session_pre_key_get_key_pair(prekey)));
+        size_t length = signal_buffer_len(buffer);
+        unsigned char *prekey_value = malloc(length);
+        memcpy(prekey_value, signal_buffer_const_data(buffer), length);
+        signal_buffer_free(buffer);
+        *prekeys = g_list_append(*prekeys, prekey_value);
+        *ids = g_list_append(*ids, GINT_TO_POINTER(session_pre_key_get_id(prekey)));
+        *lengths = g_list_append(*lengths, GINT_TO_POINTER(length));
+    }
 }
 
 static void
diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h
index 825529b1..20fd5d5d 100644
--- a/src/omemo/omemo.h
+++ b/src/omemo/omemo.h
@@ -4,3 +4,13 @@ typedef struct omemo_context_t omemo_context;
 
 void omemo_init(void);
 void omemo_generate_crypto_materials(ProfAccount *account);
+
+GList * const omemo_device_list(void);
+uint32_t omemo_device_id(void);
+void omemo_identity_key(unsigned char **output, size_t *length);
+void omemo_signed_prekey(unsigned char **output, size_t *length);
+void omemo_signed_prekey_signature(unsigned char **output, size_t *length);
+void omemo_prekeys(GList ** const prekeys, GList ** const ids, GList ** const lengths);
+
+void omemo_start_session(ProfAccount *account, char *barejid);
+gboolean omemo_loaded(void);