From 0bc660400b9a62ae2982b1ec6c374de13ec54477 Mon Sep 17 00:00:00 2001 From: Paul Fariello Date: Tue, 9 Apr 2019 08:22:35 +0320 Subject: Automatically starts OMEMO if one identity is trusted --- src/command/cmd_funcs.c | 50 ++++++++++++++++++++++++++++++++++++++ src/omemo/omemo.c | 29 +++++++++++++++++----- src/omemo/omemo.h | 1 + tests/unittests/omemo/stub_omemo.c | 6 +++++ 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index e1f62ace..b004c4e1 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -2150,17 +2150,67 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args) } ui_focus_win((ProfWin*)chatwin); +#ifdef HAVE_OMEMO +#ifndef HAVE_LIBOTR + if (omemo_is_trusted_jid(barejid)) { + omemo_start_session(barejid); + chatwin->is_omemo = TRUE; + } + + if (msg) { + cl_ev_send_msg(chatwin, msg, NULL); + } + + return TRUE; +#endif +#endif + +#ifdef HAVE_OMEMO +#ifdef HAVE_LIBOTR + if (omemo_is_trusted_jid(barejid) && otr_is_secure(barejid)) { + win_println(window, THEME_DEFAULT, '!', "Chat could be either OMEMO or OTR encrypted. Use '/omemo start %s' or '/otr start %s' to start a session.", usr, usr); + return TRUE; + } else if (omemo_is_trusted_jid(barejid)) { + omemo_start_session(barejid); + chatwin->is_omemo = TRUE; + } + if (msg) { cl_ev_send_msg(chatwin, msg, NULL); } else { + if (otr_is_secure(barejid)) { + chatwin_otr_secured(chatwin, otr_is_trusted(barejid)); + } + } + + return TRUE; +#endif +#endif + +#ifndef HAVE_OMEMO #ifdef HAVE_LIBOTR + if (msg) { + cl_ev_send_msg(chatwin, msg, NULL); + } else { if (otr_is_secure(barejid)) { chatwin_otr_secured(chatwin, otr_is_trusted(barejid)); } + } + + return TRUE; #endif +#endif + +#ifndef HAVE_OMEMO +#ifndef HAVE_LIBOTR + if (msg) { + cl_ev_send_msg(chatwin, msg, NULL); } return TRUE; +#endif +#endif + } } diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index 377a2637..1d641e4c 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -909,6 +909,21 @@ omemo_known_device_identities(const char *const jid) return g_hash_table_get_keys(known_identities); } +gboolean +omemo_is_trusted_jid(const char *const jid) +{ + GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, jid); + if (!trusted) { + return FALSE; + } + + if (g_hash_table_size(trusted) > 0) { + return TRUE; + } + + return FALSE; +} + gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint) { @@ -1262,6 +1277,13 @@ _load_trust(void) if (groups) { int i; for (i = 0; groups[i] != NULL; i++) { + GHashTable *trusted; + + trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, groups[i]); + if (!trusted) { + trusted = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); + g_hash_table_insert(omemo_ctx.identity_key_store.trusted, strdup(groups[i]), trusted); + } keys = g_key_file_get_keys(omemo_ctx.trust_keyfile, groups[i], NULL, NULL); int j; @@ -1273,11 +1295,6 @@ _load_trust(void) signal_buffer *buffer = signal_buffer_create(key, key_len); g_free(key); uint32_t device_id = strtoul(keys[j], NULL, 10); - GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, groups[i]); - if (!trusted) { - trusted = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); - g_hash_table_insert(omemo_ctx.identity_key_store.trusted, strdup(groups[i]), trusted); - } g_hash_table_insert(trusted, GINT_TO_POINTER(device_id), buffer); } g_strfreev(keys); @@ -1299,7 +1316,7 @@ _load_sessions(void) device_store = g_hash_table_lookup(omemo_ctx.session_store, groups[i]); if (!device_store) { device_store = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); - g_hash_table_insert(omemo_ctx.session_store, groups[i], device_store); + g_hash_table_insert(omemo_ctx.session_store, strdup(groups[i]), device_store); } char **keys = g_key_file_get_keys(omemo_ctx.sessions_keyfile, groups[i], NULL, NULL); diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h index e9186dfa..dcae9266 100644 --- a/src/omemo/omemo.h +++ b/src/omemo/omemo.h @@ -40,6 +40,7 @@ char *omemo_own_fingerprint(gboolean formatted); void omemo_trust(const char *const jid, const char *const fingerprint); void omemo_untrust(const char *const jid, const char *const fingerprint); GList *omemo_known_device_identities(const char *const jid); +gboolean omemo_is_trusted_jid(const char *const jid); gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint); char *omemo_fingerprint_autocomplete(const char *const search_str, gboolean previous); void omemo_fingerprint_autocomplete_reset(void); diff --git a/tests/unittests/omemo/stub_omemo.c b/tests/unittests/omemo/stub_omemo.c index 84398378..5b1d02df 100644 --- a/tests/unittests/omemo/stub_omemo.c +++ b/tests/unittests/omemo/stub_omemo.c @@ -21,6 +21,12 @@ omemo_format_fingerprint(const char *const fingerprint) void omemo_generate_crypto_materials(ProfAccount *account) {} +gboolean +omemo_is_trusted_jid(const char *const jid) +{ + return TRUE; +} + gboolean omemo_is_trusted_identity(const char *const jid, const char *const fingerprint) { -- cgit 1.4.1-2-gfad0