From 1f9d46037cb86b26a7e91d924f41702a6ea58114 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Mon, 25 Feb 2019 06:39:41 +0140 Subject: [PATCH] Request for device list when OMEMO is ready --- src/omemo/omemo.c | 10 +++------- src/xmpp/omemo.c | 26 +++++++++++++++++++++++--- src/xmpp/omemo.h | 1 + src/xmpp/stanza.c | 24 ++++++++++++++++++++++++ src/xmpp/stanza.h | 1 + 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index 3ab044ee..e4926baa 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -83,14 +83,8 @@ omemo_generate_crypto_materials(ProfAccount *account) xmpp_ctx_t * const ctx = connection_get_ctx(); char *barejid = xmpp_jid_bare(ctx, session_get_account_name()); - GList *device_list = g_hash_table_lookup(omemo_ctx.device_list, barejid); - g_hash_table_steal(omemo_ctx.device_list, barejid); - omemo_ctx.device_id = randombytes_uniform(0x80000000); - device_list = g_list_append(device_list, GINT_TO_POINTER(omemo_ctx.device_id)); - g_hash_table_insert(omemo_ctx.device_list, strdup(barejid), device_list); - 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); @@ -102,7 +96,9 @@ omemo_generate_crypto_materials(ProfAccount *account) loaded = TRUE; - omemo_devicelist_publish(device_list); + /* Ensure we get our current device list, and it gets updated with our + * device_id */ + omemo_devicelist_request(barejid); omemo_bundle_publish(); } diff --git a/src/xmpp/omemo.c b/src/xmpp/omemo.c index ab10e975..5418b1b4 100644 --- a/src/xmpp/omemo.c +++ b/src/xmpp/omemo.c @@ -27,8 +27,18 @@ omemo_devicelist_publish(GList *device_list) } void -omemo_devicelist_fetch(void) +omemo_devicelist_request(const char * const jid) { + xmpp_ctx_t * const ctx = connection_get_ctx(); + char *id = connection_create_stanza_id("devicelist_request"); + + xmpp_stanza_t *iq = stanza_create_omemo_devicelist_request(ctx, id, jid); + iq_id_handler_add(id, _omemo_receive_devicelist, NULL, NULL); + + iq_send_stanza(iq); + + free(id); + xmpp_stanza_release(iq); } void @@ -74,12 +84,22 @@ _omemo_receive_devicelist(xmpp_stanza_t *const stanza, void *const userdata) return 1; } + xmpp_stanza_t *root = NULL; xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT); - if (!event) { + if (event) { + root = event; + } + + xmpp_stanza_t *pubsub = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB); + if (pubsub) { + root = pubsub; + } + + if (!root) { return 1; } - xmpp_stanza_t *items = xmpp_stanza_get_child_by_name(event, "items"); + xmpp_stanza_t *items = xmpp_stanza_get_child_by_name(root, "items"); if (!items) { return 1; } diff --git a/src/xmpp/omemo.h b/src/xmpp/omemo.h index 5409c6fd..b125f76a 100644 --- a/src/xmpp/omemo.h +++ b/src/xmpp/omemo.h @@ -2,4 +2,5 @@ void omemo_devicelist_subscribe(void); void omemo_devicelist_publish(GList *device_list); +void omemo_devicelist_request(const char * const jid); void omemo_bundle_publish(void); diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index e2ced2ac..d5993377 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -2092,6 +2092,30 @@ stanza_create_command_config_submit_iq(xmpp_ctx_t *ctx, const char *const room, return iq; } +xmpp_stanza_t* +stanza_create_omemo_devicelist_request(xmpp_ctx_t *ctx, const char *const id, + const char *const jid) +{ + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_GET, id); + xmpp_stanza_set_to(iq, jid); + + xmpp_stanza_t *pubsub = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(pubsub, STANZA_NAME_PUBSUB); + xmpp_stanza_set_ns(pubsub, STANZA_NS_PUBSUB); + + xmpp_stanza_t *items = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(items, "items"); + xmpp_stanza_set_attribute(items, "node", STANZA_NS_OMEMO_DEVICELIST); + + xmpp_stanza_add_child(pubsub, items); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(items); + xmpp_stanza_release(pubsub); + + return iq; +} + xmpp_stanza_t* stanza_create_omemo_devicelist_subscribe(xmpp_ctx_t *ctx, const char *const jid) { diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index 23b47de1..919379bb 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -288,6 +288,7 @@ xmpp_stanza_t* stanza_create_room_kick_iq(xmpp_ctx_t *const ctx, const char *con xmpp_stanza_t* stanza_create_command_exec_iq(xmpp_ctx_t *ctx, const char *const target, const char *const node); xmpp_stanza_t* stanza_create_command_config_submit_iq(xmpp_ctx_t *ctx, const char *const room, const char *const node, const char *const sessionid, DataForm *form); +xmpp_stanza_t* stanza_create_omemo_devicelist_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid); xmpp_stanza_t* stanza_create_omemo_devicelist_subscribe(xmpp_ctx_t *ctx, const char *const jid); xmpp_stanza_t* stanza_create_omemo_devicelist_publish(xmpp_ctx_t *ctx, GList *const ids); xmpp_stanza_t* stanza_create_omemo_bundle_publish(xmpp_ctx_t *ctx, uint32_t device_id, const unsigned char * const identity_key, size_t identity_key_length, const unsigned char * const signed_prekey, size_t signed_prekey_length, const unsigned char * const signed_prekey_signature, size_t signed_prekey_signature_length, GList *const prekeys, GList *const prekeys_id, GList *const prekeys_length);