about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/command/cmd_ac.c82
-rw-r--r--src/command/cmd_defs.c51
-rw-r--r--src/command/cmd_funcs.c493
-rw-r--r--src/command/cmd_funcs.h10
-rw-r--r--src/config/files.h1
-rw-r--r--src/config/preferences.c34
-rw-r--r--src/config/preferences.h3
-rw-r--r--src/event/client_events.c157
-rw-r--r--src/event/server_events.c220
-rw-r--r--src/event/server_events.h11
-rw-r--r--src/log.c98
-rw-r--r--src/log.h9
-rw-r--r--src/main.c6
-rw-r--r--src/omemo/crypto.c331
-rw-r--r--src/omemo/crypto.h148
-rw-r--r--src/omemo/omemo.c1410
-rw-r--r--src/omemo/omemo.h55
-rw-r--r--src/omemo/store.c382
-rw-r--r--src/omemo/store.h250
-rw-r--r--src/plugins/api.c2
-rw-r--r--src/profanity.c7
-rw-r--r--src/ui/chatwin.c4
-rw-r--r--src/ui/console.c24
-rw-r--r--src/ui/mucwin.c53
-rw-r--r--src/ui/titlebar.c30
-rw-r--r--src/ui/ui.h7
-rw-r--r--src/ui/win_types.h3
-rw-r--r--src/ui/window.c5
-rw-r--r--src/ui/window_list.c1
-rw-r--r--src/xmpp/connection.c23
-rw-r--r--src/xmpp/connection.h2
-rw-r--r--src/xmpp/iq.c26
-rw-r--r--src/xmpp/iq.h6
-rw-r--r--src/xmpp/message.c259
-rw-r--r--src/xmpp/message.h4
-rw-r--r--src/xmpp/omemo.c448
-rw-r--r--src/xmpp/omemo.h11
-rw-r--r--src/xmpp/roster.c4
-rw-r--r--src/xmpp/session.c21
-rw-r--r--src/xmpp/stanza.c340
-rw-r--r--src/xmpp/stanza.h22
-rw-r--r--src/xmpp/xmpp.h8
42 files changed, 4962 insertions, 99 deletions
diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c
index 58ad758a..0cc28bb3 100644
--- a/src/command/cmd_ac.c
+++ b/src/command/cmd_ac.c
@@ -57,6 +57,10 @@
 #include "pgp/gpg.h"
 #endif
 
+#ifdef HAVE_OMEMO
+#include "omemo/omemo.h"
+#endif
+
 static char* _sub_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _notify_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _theme_autocomplete(ProfWin *window, const char *const input, gboolean previous);
@@ -69,6 +73,7 @@ static char* _group_autocomplete(ProfWin *window, const char *const input, gbool
 static char* _bookmark_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _otr_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _pgp_autocomplete(ProfWin *window, const char *const input, gboolean previous);
+static char* _omemo_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _connect_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _alias_autocomplete(ProfWin *window, const char *const input, gboolean previous);
 static char* _join_autocomplete(ProfWin *window, const char *const input, gboolean previous);
@@ -157,6 +162,8 @@ static Autocomplete bookmark_property_ac;
 static Autocomplete otr_ac;
 static Autocomplete otr_log_ac;
 static Autocomplete otr_policy_ac;
+static Autocomplete omemo_ac;
+static Autocomplete omemo_log_ac;
 static Autocomplete connect_property_ac;
 static Autocomplete tls_property_ac;
 static Autocomplete alias_ac;
@@ -237,6 +244,7 @@ cmd_ac_init(void)
     autocomplete_add(prefs_ac, "presence");
     autocomplete_add(prefs_ac, "otr");
     autocomplete_add(prefs_ac, "pgp");
+    autocomplete_add(prefs_ac, "omemo");
 
     notify_ac = autocomplete_new();
     autocomplete_add(notify_ac, "chat");
@@ -574,6 +582,21 @@ cmd_ac_init(void)
     autocomplete_add(otr_policy_ac, "opportunistic");
     autocomplete_add(otr_policy_ac, "always");
 
+    omemo_ac = autocomplete_new();
+    autocomplete_add(omemo_ac, "gen");
+    autocomplete_add(omemo_ac, "log");
+    autocomplete_add(omemo_ac, "start");
+    autocomplete_add(omemo_ac, "end");
+    autocomplete_add(omemo_ac, "trust");
+    autocomplete_add(omemo_ac, "untrust");
+    autocomplete_add(omemo_ac, "fingerprint");
+    autocomplete_add(omemo_ac, "clear_device_list");
+
+    omemo_log_ac = autocomplete_new();
+    autocomplete_add(omemo_log_ac, "on");
+    autocomplete_add(omemo_log_ac, "off");
+    autocomplete_add(omemo_log_ac, "redact");
+
     connect_property_ac = autocomplete_new();
     autocomplete_add(connect_property_ac, "server");
     autocomplete_add(connect_property_ac, "port");
@@ -983,6 +1006,9 @@ cmd_ac_reset(ProfWin *window)
 #ifdef HAVE_LIBGPGME
     p_gpg_autocomplete_key_reset();
 #endif
+#ifdef HAVE_OMEMO
+    omemo_fingerprint_autocomplete_reset();
+#endif
     autocomplete_reset(help_ac);
     autocomplete_reset(help_commands_ac);
     autocomplete_reset(notify_ac);
@@ -1052,6 +1078,8 @@ cmd_ac_reset(ProfWin *window)
     autocomplete_reset(otr_ac);
     autocomplete_reset(otr_log_ac);
     autocomplete_reset(otr_policy_ac);
+    autocomplete_reset(omemo_ac);
+    autocomplete_reset(omemo_log_ac);
     autocomplete_reset(connect_property_ac);
     autocomplete_reset(tls_property_ac);
     autocomplete_reset(alias_ac);
@@ -1179,6 +1207,8 @@ cmd_ac_uninit(void)
     autocomplete_free(otr_ac);
     autocomplete_free(otr_log_ac);
     autocomplete_free(otr_policy_ac);
+    autocomplete_free(omemo_ac);
+    autocomplete_free(omemo_log_ac);
     autocomplete_free(connect_property_ac);
     autocomplete_free(tls_property_ac);
     autocomplete_free(alias_ac);
@@ -1438,6 +1468,7 @@ _cmd_ac_complete_params(ProfWin *window, const char *const input, gboolean previ
     g_hash_table_insert(ac_funcs, "/autoconnect",   _autoconnect_autocomplete);
     g_hash_table_insert(ac_funcs, "/otr",           _otr_autocomplete);
     g_hash_table_insert(ac_funcs, "/pgp",           _pgp_autocomplete);
+    g_hash_table_insert(ac_funcs, "/omemo",         _omemo_autocomplete);
     g_hash_table_insert(ac_funcs, "/connect",       _connect_autocomplete);
     g_hash_table_insert(ac_funcs, "/alias",         _alias_autocomplete);
     g_hash_table_insert(ac_funcs, "/join",          _join_autocomplete);
@@ -2118,6 +2149,57 @@ _pgp_autocomplete(ProfWin *window, const char *const input, gboolean previous)
 }
 
 static char*
+_omemo_autocomplete(ProfWin *window, const char *const input, gboolean previous)
+{
+    char *found = NULL;
+
+    jabber_conn_status_t conn_status = connection_get_status();
+
+    if (conn_status == JABBER_CONNECTED) {
+        found = autocomplete_param_with_func(input, "/omemo start", roster_contact_autocomplete, previous);
+        if (found) {
+            return found;
+        }
+    }
+
+    found = autocomplete_param_with_func(input, "/omemo fingerprint", roster_contact_autocomplete, previous);
+    if (found) {
+        return found;
+    }
+
+#ifdef HAVE_OMEMO
+    if (window->type == WIN_CHAT) {
+        found = autocomplete_param_with_func(input, "/omemo trust", omemo_fingerprint_autocomplete, previous);
+        if (found) {
+            return found;
+        }
+    } else {
+        found = autocomplete_param_with_func(input, "/omemo trust", roster_contact_autocomplete, previous);
+        if (found) {
+            return found;
+        }
+
+        found = autocomplete_param_no_with_func(input, "/omemo trust", 4, omemo_fingerprint_autocomplete, previous);
+        if (found) {
+            return found;
+        }
+    }
+#endif
+
+    found = autocomplete_param_with_ac(input, "/omemo log", omemo_log_ac, TRUE, previous);
+    if (found) {
+        return found;
+    }
+
+    found = autocomplete_param_with_ac(input, "/omemo", omemo_ac, TRUE, previous);
+    if (found) {
+        return found;
+    }
+
+    return NULL;
+}
+
+static char*
 _plugins_autocomplete(ProfWin *window, const char *const input, gboolean previous)
 {
     char *result = NULL;
diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c
index 4447020b..ee86aaba 100644
--- a/src/command/cmd_defs.c
+++ b/src/command/cmd_defs.c
@@ -2134,7 +2134,7 @@ static struct cmd_t command_defs[] =
         CMD_MAINFUNC(cmd_prefs)
         CMD_NOTAGS
         CMD_SYN(
-            "/prefs [ui|desktop|chat|log|conn|presence|otr|pgp]")
+            "/prefs [ui|desktop|chat|log|conn|presence|otr|pgp|omemo]")
         CMD_DESC(
             "Show preferences for different areas of functionality. "
             "Passing no arguments shows all preferences.")
@@ -2146,7 +2146,8 @@ static struct cmd_t command_defs[] =
             { "conn",     "Connection handling preferences." },
             { "presence", "Chat presence preferences." },
             { "otr",      "Off The Record preferences." },
-            { "pgp",      "OpenPGP preferences." })
+            { "pgp",      "OpenPGP preferences." },
+            { "omemo",    "OMEMO preferences." })
         CMD_NOEXAMPLES
     },
 
@@ -2328,7 +2329,51 @@ static struct cmd_t command_defs[] =
         CMD_EXAMPLES(
             "/cmd list",
             "/cmd exec ping")
-    }
+    },
+
+    { "/omemo",
+        parse_args, 1, 3, NULL,
+        CMD_SUBFUNCS(
+            { "gen", cmd_omemo_gen },
+            { "log", cmd_omemo_log },
+            { "start", cmd_omemo_start },
+            { "end", cmd_omemo_end },
+            { "trust", cmd_omemo_trust },
+            { "untrust", cmd_omemo_untrust },
+            { "fingerprint", cmd_omemo_fingerprint },
+            { "char", cmd_omemo_char },
+            { "clear_device_list", cmd_omemo_clear_device_list })
+        CMD_NOMAINFUNC
+        CMD_TAGS(
+            CMD_TAG_CHAT,
+            CMD_TAG_UI)
+        CMD_SYN(
+            "/omemo gen",
+            "/omemo log on|off|redact",
+            "/omemo start [<contact>]",
+            "/omemo trust [<contact>] <fingerprint>",
+            "/omemo end",
+            "/omemo fingerprint [<contact>]",
+            "/omemo char <char>",
+            "/omemo clear_device_list")
+        CMD_DESC(
+            "OMEMO commands to manage keys, and perform encryption during chat sessions.")
+        CMD_ARGS(
+            { "gen",                     "Generate OMEMO crytographic materials for current account." },
+            { "start [<contact>]",       "Start an OMEMO session with contact, or current recipient if omitted." },
+            { "end",                     "End the current OMEMO session." },
+            { "log on|off",              "Enable or disable plaintext logging of OMEMO encrypted messages." },
+            { "log redact",              "Log OMEMO encrypted messages, but replace the contents with [redacted]. This is the default." },
+            { "fingerprint [<contact>]", "Show contact fingerprints, or current recipient if omitted." },
+            { "char <char>",             "Set the character to be displayed next to OMEMO encrypted messages." },
+            { "clear_device_list",       "Clear your own device list on server side. Each client will reannounce itself when connected back."})
+        CMD_EXAMPLES(
+            "/omemo gen",
+            "/omemo start buddy@buddychat.org",
+            "/omemo trust c4f9c875-144d7a3b-0c4a05b6-ca3be51a-a037f329-0bd3ae62-07f99719-55559d2a",
+            "/omemo untrust buddy@buddychat.org c4f9c875-144d7a3b-0c4a05b6-ca3be51a-a037f329-0bd3ae62-07f99719-55559d2a",
+            "/omemo char *")<
.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.20)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.el \{\
.    de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "RANGER 1"
.TH RANGER 1 "ranger-1.5.5" "01/01/2013" "ranger manual"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
ranger \- visual file manager
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
\&\fBranger\fR [\fB\-\-help\fR] [\fB\-\-version\fR] [\fB\-\-debug\fR] [\fB\-\-clean\fR]
[\fB\-\-list\-unused\-keys\fR] [\fB\-\-fail\-unless\-cd\fR] [\fB\-\-choosedir\fR=\fItargetfile\fR]
[\fB\-\-choosefile\fR=\fItargetfile\fR] [\fB\-\-copy\-config\fR=\fIfile\fR] [\fB\-\-mode\fR=\fImode\fR]
[\fB\-\-flags\fR=\fIflags\fR] [\fIpath/filename\fR]
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
ranger is a console file manager with \s-1VI\s0 key bindings.  It provides a
minimalistic and nice curses interface with a view on the directory hierarchy.
The secondary task of ranger is to figure out which program you want to use to
open your files with.
.PP
This manual mainly contains information on the usage of ranger.  Refer to the
\&\fI\s-1README\s0\fR for install instructions and to \fIdoc/HACKING\fR for development
specific information.  For configuration, see the files in \fIranger/config\fR.
They are usually installed to \fI/usr/lib/python*/site\-packages/ranger/config\fR
and can be obtained with ranger's \-\-copy\-config option.
.PP
Inside ranger, you can press \fI1?\fR for a list of key bindings, \fI2?\fR for a list
of commands and \fI3?\fR for a list of settings.
.SH "OPTIONS"
.IX Header "OPTIONS"
.IP "\fB\-d\fR, \fB\-\-debug\fR" 14
.IX Item "-d, --debug"
Activate the debug mode: Whenever an error occurs, ranger will exit and print a
full traceback.  The default behavior is to merely print the name of the
exception in the statusbar/log and try to keep running.
.IP "\fB\-c\fR, \fB\-\-clean\fR" 14
.IX Item "-c, --clean"
Activate the clean mode:  ranger will not access or create any configuration
files nor will it leave any traces on your system.  This is useful when your
configuration is broken, when you want to avoid clutter, etc.
.IP "\fB\-\-choosefile\fR=\fItargetfile\fR" 14
.IX Item "--choosefile=targetfile"
Allows you to pick a file with ranger.  This changes the behavior so that when
you open a file, ranger will exit and write the absolute path of that file into
\&\fItargetfile\fR.
.IP "\fB\-\-choosefiles\fR=\fItargetfile\fR" 14
.IX Item "--choosefiles=targetfile"
Allows you to pick multiple files with ranger.  This changes the behavior so
that when you open a file, ranger will exit and write the absolute paths of all
selected files into \fItargetfile\fR, adding one newline after each filename.
.IP "\fB\-\-choosedir\fR=\fItargetfile\fR" 14
.IX Item "--choosedir=targetfile"
Allows you to pick a directory with ranger.  When you exit ranger, it will
write the last visited directory into \fItargetfile\fR.
.IP "\fB\-\-selectfile\fR=\fItargetfile\fR" 14
.IX Item "--selectfile=targetfile"
Open ranger with \fItargetfile\fR selected.
.IP "\fB\-\-copy\-config\fR=\fIfile\fR" 14
.IX Item "--copy-config=file"
Create copies of the default configuration files in your local configuration
directory.  Existing ones will not be overwritten.  Possible values: \fIall\fR,
\&\fIrc\fR, \fIcommands\fR, \fIscope\fR.
.IP "\fB\-\-list\-unused\-keys\fR" 14
.IX Item "--list-unused-keys"
List common keys which are not bound to any action in the \*(L"browser\*(R" context.
This list is not complete, you can bind any key that is supported by curses:
use the key code returned by \f(CW\*(C`getch()\*(C'\fR.
.IP "\fB\-\-list\-tagged\-files\fR=\fItag\fR" 14
.IX Item "--list-tagged-files=tag"
List all files which are tagged with the given tag.  Note: Tags are single
characters.  The default tag is \*(L"*\*(R"
.IP "\fB\-\-fail\-unless\-cd\fR" 14
.IX Item "--fail-unless-cd"
Return the exit code 1 if ranger is used to run a file instead of used for file
browsing. (For example, \*(L"ranger \-\-fail\-unless\-cd test.txt\*(R" returns 1.)
.IP "\fB\-m\fR \fIn\fR, \fB\-\-mode\fR=\fIn\fR" 14
.IX Item "-m n, --mode=n"
When a filename is supplied, run it in mode \fIn\fR.  This has no effect unless
the execution of this file type is explicitly handled in the configuration.
.IP "\fB\-f\fR \fIflags\fR, \fB\-\-flags\fR=\fIflags\fR" 14
.IX Item "-f flags, --flags=flags"
When a filename is supplied, run it with the given \fIflags\fR to modify
behavior.  The execution of this file type is explicitly handled in the
configuration.
.IP "\fB\-\-cmd\fR=\fIcommand\fR" 14
.IX Item "--cmd=command"
Execute the command after the configuration has been read.  Use this option
multiple times to run multiple commands.
.IP "\fB\-\-version\fR" 14
.IX Item "--version"
Print the version and exit.
.IP "\fB\-h\fR, \fB\-\-help\fR" 14
.IX Item "-h, --help"
Print a list of options and exit.
.SH "CONCEPTS"
.IX Header "CONCEPTS"
This part explains how certain parts of ranger work and how they can be used
efficiently.
.SS "\s-1TAGS\s0"
.IX Subsection "TAGS"
Tags are single characters which are displayed left of a filename.  You can use
tags however you want.  Press \*(L"t\*(R" to toggle tags and \*(L"T\*(R" to remove any tags of
the selection. The default tag is an Asterisk (\*(L"*\*(R"), but you can use any tag by
typing \fI"<tagname>\fR.
.SS "\s-1PREVIEWS\s0"
.IX Subsection "PREVIEWS"
By default, only text files are previewed, but you can enable external preview
scripts by setting the option \f(CW\*(C`use_preview_script\*(C'\fR and \f(CW\*(C`preview_files\*(C'\fR to True.
.PP
This default script is \fI~/.config/ranger/scope.sh\fR. It contains more
documentation and calls to the programs \fIlynx\fR and \fIelinks\fR for html,
\&\fIhighlight\fR for text/code, \fIimg2txt\fR for images, \fIatool\fR for archives,
\&\fIpdftotext\fR for PDFs and \fImediainfo\fR for video and audio files.
.PP
Install these programs (just the ones you need) and scope.sh will automatically
use them.
.SS "\s-1SELECTION\s0"
.IX Subsection "SELECTION"
The \fIselection\fR is defined as \*(L"All marked files \s-1IF\s0 \s-1THERE\s0 \s-1ARE\s0 \s-1ANY\s0, otherwise
the current file.\*(R"  Be aware of this when using the :delete command, which
deletes all files in the selection.
.PP
You can mark files by pressing <Space>, v, etc.  A yellow \fBMrk\fR symbol at the
bottom right indicates that there are marked files in this directory.
.SS "\s-1MACROS\s0"
.IX Subsection "MACROS"
Macros can be used in commands to abbreviate things.
.PP
.Vb 5
\& %f   the highlighted file
\& %d   the path of the current directory
\& %s   the selected files in the current directory.
\& %t   all tagged files in the current directory
\& %c   the full paths of the currently copied/cut files
.Ve
.PP
The macros \f(CW%f\fR, \f(CW%d\fR and \f(CW%s\fR also have upper case variants, \f(CW%F\fR, \f(CW%D\fR and \f(CW%S\fR,
which refer to the next tab.  To refer to specific tabs, add a number in
between.  (%7s = selection of the seventh tab.)
.PP
\&\f(CW%c\fR is the only macro which ranges out of the current directory. So you may
\&\*(L"abuse\*(R" the copying function for other purposes, like diffing two files which
are in different directories:
.PP
.Vb 2
\& Yank the file A (type yy), move to the file B, then type
\& @diff %c %f
.Ve
.PP
Macros for file paths are generally shell-escaped so they can be used in the
\&\f(CW\*(C`shell\*(C'\fR command.
.SS "\s-1BOOKMARKS\s0"
.IX Subsection "BOOKMARKS"
Type \fBm<key>\fR to bookmark the current directory. You can re-enter this
directory by typing \fB`<key>\fR. <key> can be any letter or digit.  Unlike vim,
both lowercase and uppercase bookmarks are persistent.
.PP
Each time you jump to a bookmark, the special bookmark at key ` will be set
to the last directory. So typing \*(L"``\*(R" gets you back to where you were before.
.PP
Bookmarks are selectable when tabbing in the :cd command.
.PP
Note: The bookmarks ' (Apostrophe) and ` (Backtick) are the same.
.SS "\s-1RIFLE\s0"
.IX Subsection "RIFLE"
Rifle is the file opener of ranger.  It can be used as a standalone program or
a python module.  It is located at \fIranger/ext/rifle.py\fR.  In contrast to
other, more simple file openers, rifle can automatically find installed
programs so it can be used effectively out of the box on a variety of systems.
.PP
It's configured in \fIrifle.conf\fR through a list of conditions and commands.
For each line the conditions are checked and if they are met, the respective
command is taken into consideration.  By default, simply the first matching
rule is used.  In ranger, you can list and choose rules by typing \*(L"r\*(R" or simply
by typing \*(L"<rulenumber><enter>\*(R".  If you use rifle standalone, you can list all
rules with the \*(L"\-l\*(R" option and pick a rule with \*(L"\-p <number>\*(R".
.PP
The rules, along with further documentation, are contained in
\&\fIranger/config/rifle.conf\fR.
.SS "\s-1FLAGS\s0"
.IX Subsection "FLAGS"
Flags give you a way to modify the behavior of the spawned process.  They are
used in the commands \f(CW\*(C`:open_with\*(C'\fR (key \*(L"r\*(R") and \f(CW\*(C`:shell\*(C'\fR (key \*(L"!\*(R").
.PP
.Vb 4
\& f   Fork the process.  (Run in background)
\& c   Run the current file only, instead of the selection
\& r   Run application with root privilege (requires sudo)
\& t   Run application in a new terminal window
.Ve
.PP
There are some additional flags that can currently be used only in the \f(CW\*(C`shell\*(C'\fR
command: (for example \f(CW\*(C`:shell \-w df\*(C'\fR)
.PP
.Vb 3
\& p   Redirect output to the pager
\& s   Silent mode.  Output will be discarded.
\& w   Wait for an Enter\-press when the process is done
.Ve
.PP
By default, all the flags are off unless specified otherwise in the
\&\fIrifle.conf\fR configuration file.  You can specify as many flags as you want.
An uppercase flag negates the effect: \*(L"ffcccFsf\*(R" is equivalent to \*(L"cs\*(R".
.PP
The terminal program name for the \*(L"t\*(R" flag is taken from the environment
variable \f(CW$TERMCMD\fR.  If it doesn't exist, it tries to extract it from \f(CW$TERM\fR and
uses \*(L"xterm\*(R" as a fallback if that fails.
.PP
Examples: \f(CW\*(C`:open_with c\*(C'\fR will open the file that you currently point at, even
if you have selected other files.  \f(CW\*(C`:shell \-w df\*(C'\fR will run \*(L"df\*(R" and wait for
you to press Enter before switching back to ranger.
.SS "\s-1PLUGINS\s0"
.IX Subsection "PLUGINS"
ranger's plugin system consists of python files which are located in
\&\fI~/.config/ranger/plugins/\fR and are imported in alphabetical order when
starting ranger.  A plugin changes rangers behavior by overwriting or extending
a function that ranger uses.  This allows you to change pretty much every part
of ranger, but there is no guarantee that things will continue to work in
future versions as the source code evolves.
.PP
There are some hooks that are specifically made for the use in plugins.  They
are functions that start with hook_ and can be found throughout the code.
.PP
.Vb 1
\& grep \*(Aqdef hook_\*(Aq \-r /path/to/rangers/source
.Ve
.PP
Also try:
.PP
.Vb 1
\& pydoc ranger.api
.Ve
.PP
Note that you should \s-1NOT\s0 simply overwrite a function unless you know what
you're doing.  Instead, save the existing function and call it from your new
one.  This way, multiple plugins can use the same hook.  There are several
sample plugins in the \fIexamples/\fR directory, including a hello-world plugin
that describes this procedure.
.SH "KEY BINDINGS"
.IX Header "KEY BINDINGS"
Key bindings are defined in the file \fIranger/config/rc.conf\fR.  Check this
file for a list of all key bindings.  You can copy it to your local
configuration directory with the \-\-copy\-config=rc option.
.PP
Many key bindings take an additional numeric argument.  Type \fI5j\fR to move
down 5 lines, \fI2l\fR to open a file in mode 2, \fI10<Space>\fR to mark 10 files.
.PP
This list contains the most useful bindings:
.SS "\s-1MAIN\s0 \s-1BINDINGS\s0"
.IX Subsection "MAIN BINDINGS"
.IP "h, j, k, l" 14
.IX Item "h, j, k, l"
Move left, down, up or right
.IP "^D or J, ^U or K" 14
.IX Item "^D or J, ^U or K"
Move a half page down, up
.IP "H, L" 14
.IX Item "H, L"
Move back and forward in the history
.IP "gg" 14
.IX Item "gg"
Move to the top
.IP "G" 14
.IX Item "G"
Move to the bottom
.IP "^R" 14
.IX Item "^R"
Reload everything
.IP "^L" 14
.IX Item "^L"
Redraw the screen
.IP "S" 14
.IX Item "S"
Open a shell in the current directory
.IP "?" 14
Opens this man page
.IP "yy" 14
.IX Item "yy"
Yank the selection to the \*(L"copy\*(R" buffer and mark them as to be copied
.IP "dd" 14
.IX Item "dd"
Cut the selection to the \*(L"copy\*(R" buffer and mark them as to be moved
.IP "pp" 14
.IX Item "pp"
Paste the files from the \*(L"copy\*(R" buffer here (by moving or copying, depending on
how they are marked.) By default, this will not overwrite existing files.  To
overwrite them, use \fIpo\fR.
.IP "m\fIX\fR" 14
.IX Item "mX"
Create a bookmark with the name \fIX\fR
.IP "`\fIX\fR" 14
.IX Item "`X"
Move to the bookmark with the name \fIX\fR
.IP "n, N" 14
.IX Item "n, N"
Find the next file.  By default, this gets you to the newest file in the
directory, but if you search something using the keys /, cm, ct, ..., it will
get you to the next found entry.
.IP "N" 14
.IX Item "N"
Find the previous file.
.IP "o\fIX\fR" 14
.IX Item "oX"
Change the sort method (like in mutt)
.IP "z\fIX\fR" 14
.IX Item "zX"
Change settings.  See the settings section for a list of settings and their
hotkey.
.IP "f" 14
.IX Item "f"
Quickly navigate by entering a part of the filename.
.IP "Space" 14
.IX Item "Space"
Mark a file.
.IP "v" 14
.IX Item "v"
Toggle the mark-status of all files, unmark all files.
.IP "V, uv" 14
.IX Item "V, uv"
Unmark all files
.IP "/" 14
Search for files in the current directory.
.IP ":" 14
Open the console.
.IP "Alt\-\fIN\fR" 14
.IX Item "Alt-N"
Open a tab. N has to be a number from 0 to 9. If the tab doesn't exist yet, it
will be created.
.IP "gn, ^N" 14
.IX Item "gn, ^N"
Create a new tab.
.IP "gt, gT" 14
.IX Item "gt, gT"
Go to the next or previous tab. You can also use \s-1TAB\s0 and \s-1SHIFT+TAB\s0 instead.
.IP "gc, ^W" 14
.IX Item "gc, ^W"
Close the current tab.  The last tab cannot be closed this way.
.SS "\s-1MIDNIGHT\s0 COMMANDER-LIKE \s-1BINDINGS\s0"
.IX Subsection "MIDNIGHT COMMANDER-LIKE BINDINGS"
.IP "<F1>" 14
.IX Item "<F1>"
Display Help.
.IP "<F3>" 14
.IX Item "<F3>"
Display the file.
.IP "<F4>" 14
.IX Item "<F4>"
Edit the file.
.IP "<F5>" 14
.IX Item "<F5>"
Copy the file.
.IP "<F6>" 14
.IX Item "<F6>"
Cut the file.
.IP "<F7>" 14
.IX Item "<F7>"
Open the console with \*(L":mkdir \*(R".
.IP "<F8>" 14
.IX Item "<F8>"
Prompt for deletion of the selected files.
.IP "<F10>" 14
.IX Item "<F10>"
Exit ranger.
.SS "READLINE-LIKE \s-1BINDINGS\s0 \s-1IN\s0 \s-1THE\s0 \s-1CONSOLE\s0"
.IX Subsection "READLINE-LIKE BINDINGS IN THE CONSOLE"
.IP "^B, ^F" 14
.IX Item "^B, ^F"
Move left and right (B for back, F for forward)
.IP "^P, ^N" 14
.IX Item "^P, ^N"
Move up and down (P for previous, N for Next)
.IP "^A, ^E" 14
.IX Item "^A, ^E"
Move to the start or to the end
.IP "^D" 14
.IX Item "^D"
Delete the current character.
.IP "^H" 14
.IX Item "^H"
Backspace.
.SH "MOUSE BUTTONS"
.IX Header "MOUSE BUTTONS"
.IP "Left Mouse Button" 4
.IX Item "Left Mouse Button"
Click on something and you'll move there.  To run a file, \*(L"enter\*(R" it, like a
directory, by clicking on the preview.
.IP "Right Mouse Button" 4
.IX Item "Right Mouse Button"
Enter a directory or run a file.
.IP "Scroll Wheel" 4
.IX Item "Scroll Wheel"
Scrolls up or down.  You can point at the column of the parent directory while
scrolling to switch directories.
.SH "SETTINGS"
.IX Header "SETTINGS"
This section lists all built-in settings of ranger.  The valid types for the
value are in [brackets].  The hotkey to toggle the setting is in <brokets>, if
a hotkey exists.
.PP
Settings can be changed in the file \fI~/.config/ranger/rc.conf\fR or on the
fly with the command \fB:set option value\fR.  Examples:
.PP
.Vb 2
\& set column_ratios 1,2,3
\& set show_hidden true
.Ve
.PP
The different types of settings and an example for each type:
.PP
.Vb 7
\& setting type   | example values
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& bool           | true, false
\& integer        | 1, 23, 1337
\& string         | foo, hello world
\& list           | 1,2,3,4
\& none           | none
.Ve
.PP
You can view a list of all settings and their current values by pressing \*(L"3?\*(R"
in ranger.
.IP "autosave_bookmarks [bool]" 4
.IX Item "autosave_bookmarks [bool]"
Save bookmarks (used with mX and `X) instantly?  This helps to synchronize
bookmarks between multiple ranger instances but leads to *slight* performance
loss.  When false, bookmarks are saved when ranger is exited.
.IP "autoupdate_cumulative_size [bool]" 4
.IX Item "autoupdate_cumulative_size [bool]"
You can display the \*(L"real\*(R" cumulative size of directories by using the command
:get_cumulative_size or typing \*(L"dc\*(R".  The size is expensive to calculate and
will not be updated automatically.  You can choose to update it automatically
though by turning on this option.
.IP "collapse_preview [bool] <zc>" 4
.IX Item "collapse_preview [bool] <zc>"
When no preview is visible, should the last column be squeezed to make use of
the whitespace?
.IP "colorscheme [string]" 4
.IX Item "colorscheme [string]"
Which colorscheme to use?  These colorschemes are available by default:
\&\fBdefault\fR, \fBjungle\fR, \fBsnow\fR.  Snow is a monochrome scheme, jungle replaces
blue directories with green ones for better visibility on certain terminals.
.IP "column_ratios [list]" 4
.IX Item "column_ratios [list]"
How many columns are there, and what are their relative widths?  For example, a
value of 1,1,1 would mean 3 evenly sized columns. 1,1,1,1,4 means 5 columns
with the preview column being as large as the other columns combined.
.IP "confirm_on_delete [string]" 4
.IX Item "confirm_on_delete [string]"
Ask for a confirmation when running the \*(L"delete\*(R" command?  Valid values are
\&\*(L"always\*(R" (default), \*(L"never\*(R", \*(L"multiple\*(R". With \*(L"multiple\*(R", ranger will ask only
if you delete multiple files at once.
.IP "dirname_in_tabs [bool]" 4
.IX Item "dirname_in_tabs [bool]"
Display the directory name in tabs?
.IP "display_size_in_main_column [bool]" 4
.IX Item "display_size_in_main_column [bool]"
Display the file size in the main column?
.IP "display_size_in_status_bar [bool]" 4
.IX Item "display_size_in_status_bar [bool]"
Display the file size in the status bar?
.IP "display_tags_in_all_columns [bool]" 4
.IX Item "display_tags_in_all_columns [bool]"
Display tags in all columns?
.IP "draw_borders [bool]" 4
.IX Item "draw_borders [bool]"
Draw borders around columns?
.IP "draw_progress_bar_in_statusbar [bool]" 4
.IX Item "draw_progress_bar_in_statusbar [bool]"
Draw a progress bar in the status bar which displays the average state of all
currently running tasks which support progress bars?
.IP "flushinput [bool] <zi>" 4
.IX Item "flushinput [bool] <zi>"
Flush the input after each key hit?  One advantage is that when scrolling down
with \*(L"j\*(R", ranger stops scrolling instantly when you release the key.  One
disadvantage is that when you type commands blindly, some keys might get lost.
.IP "hidden_filter [string]" 4
.IX Item "hidden_filter [string]"
A regular expression pattern for files which should be hidden.  For example,
this pattern will hide all files that start with a dot or end with a tilde.
.Sp
.Vb 1
\& set hidden_filter ^\e.|~$
.Ve
.IP "max_console_history_size [integer, none]" 4
.IX Item "max_console_history_size [integer, none]"
How many console commands should be kept in history?  \*(L"none\*(R" will disable the
limit.
.IP "max_history_size [integer, none]" 4
.IX Item "max_history_size [integer, none]"
How many directory changes should be kept in history?
.IP "mouse_enabled [bool] <zm>" 4
.IX Item "mouse_enabled [bool] <zm>"
Enable mouse input?
.IP "padding_right [bool]" 4
.IX Item "padding_right [bool]"
When collapse_preview is on and there is no preview, should there remain a
little padding on the right?  This allows you to click into that space to run
the file.
.IP "preview_directories [bool] <zP>" 4
.IX Item "preview_directories [bool] <zP>"
Preview directories in the preview column?
.IP "preview_files [bool] <zp>" 4
.IX Item "preview_files [bool] <zp>"
Preview files in the preview column?
.IP "preview_script [string, none]" 4
.IX Item "preview_script [string, none]"
Which script should handle generating previews?  If the file doesn't exist, or
use_preview_script is off, ranger will handle previews itself by just printing
the content.
.IP "save_console_history [bool]" 4
.IX Item "save_console_history [bool]"
Should the console history be saved on exit?  If disabled, the console history
is reset when you restart ranger.
.IP "scroll_offset [integer]" 4
.IX Item "scroll_offset [integer]"
Try to keep this much space between the top/bottom border when scrolling.
.IP "shorten_title [integer, bool]" 4
.IX Item "shorten_title [integer, bool]"
Trim the title of the window if it gets long?  The number defines how many
directories are displayed at once, False turns off this feature.
.IP "show_cursor [bool]" 4
.IX Item "show_cursor [bool]"
Always show the terminal cursor?
.IP "show_hidden_bookmarks [bool]" 4
.IX Item "show_hidden_bookmarks [bool]"
Show dotfiles in the bookmark preview window? (Type ')
.IP "show_hidden [bool] <zh>, <^H>" 4
.IX Item "show_hidden [bool] <zh>, <^H>"
Show hidden files?
.IP "sort_case_insensitive [bool] <zc>" 4
.IX Item "sort_case_insensitive [bool] <zc>"
Sort case-insensitively?  If true, \*(L"a\*(R" will be listed before \*(L"B\*(R" even though
its \s-1ASCII\s0 value is higher.
.IP "sort_directories_first [bool] <zd>" 4
.IX Item "sort_directories_first [bool] <zd>"
Sort directories first?
.IP "sort_reverse [bool] <or>" 4
.IX Item "sort_reverse [bool] <or>"
Reverse the order of files?
.IP "sort [string] <oa>, <ob>, <oc>, <om>, <on>, <ot>, <os>" 4
.IX Item "sort [string] <oa>, <ob>, <oc>, <om>, <on>, <ot>, <os>"
Which sorting mechanism should be used?  Choose one of \fBatime\fR, \fBbasename\fR,
\&\fBctime\fR, \fBmtime\fR, \fBnatural\fR, \fBtype\fR, \fBsize\fR
.Sp
Note: You can reverse the order by using an uppercase O in the key combination.
.IP "tilde_in_titlebar [bool]" 4
.IX Item "tilde_in_titlebar [bool]"
Abbreviate \f(CW$HOME\fR with ~ in the title bar (first line) of ranger?
.IP "unicode_ellipsis [bool]" 4
.IX Item "unicode_ellipsis [bool]"
Use a unicode \*(L"...\*(R" character instead of \*(L"~\*(R" to mark cut-off filenames?
.IP "update_title [bool]" 4
.IX Item "update_title [bool]"
Set a window title?
.IP "update_tmux_title [bool]" 4
.IX Item "update_tmux_title [bool]"
Set the title to \*(L"ranger\*(R" in the tmux program?
.IP "use_preview_script [bool] <zv>" 4
.IX Item "use_preview_script [bool] <zv>"
Use the preview script defined in the setting \fIpreview_script\fR?
.IP "xterm_alt_key [bool]" 4
.IX Item "xterm_alt_key [bool]"
Enable this if key combinations with the Alt Key don't work for you.
(Especially on xterm)
.SH "COMMANDS"
.IX Header "COMMANDS"
You can enter the commands in the console which is opened by pressing \*(L":\*(R".
.PP
You can always get a list of the currently existing commands by typing \*(L"2?\*(R" in
ranger.  For your convenience, this is a list of the \*(L"public\*(R" commands including their parameters, excluding descriptions:
.PP
.Vb 10
\& alias [newcommand] [oldcommand]
\& bulkrename
\& cd [directory]
\& chain command1[; command2[; command3...]]
\& chmod octal_number
\& cmap key command
\& console [\-pSTARTPOSITION] command
\& copycmap key newkey [newkey2...]
\& copymap key newkey [newkey2...]
\& copypmap key newkey [newkey2...]
\& copytmap key newkey [newkey2...]
\& cunmap keys...
\& delete [confirmation]
\& edit [filename]
\& eval [\-q] python_code
\& filter [string]
\& find pattern
\& grep pattern
\& load_copy_buffer
\& map key command
\& mark pattern
\& mark_tag [tags]
\& mkdir dirname
\& open_with [application] [flags] [mode]
\& pmap key command
\& punmap keys...
\& quit
\& quit!
\& relink newpath
\& rename newname
\& save_copy_buffer
\& search pattern
\& search_inc pattern
\& set option value
\& setlocal [path=<path>] option value
\& shell [\-FLAGS] command
\& terminal
\& tmap key command
\& touch filename
\& tunmap keys...
\& unmap keys...
\& unmark pattern
\& unmark_tag [tags]
.Ve
.PP
There are additional commands which are directly translated to python
functions, one for every method in the ranger.core.actions.Actions class.
They are not documented here, since they are mostly for key bindings, not to be
typed in by a user.  Read the source if you are interested in them.
.PP
These are the public commands including their descriptions:
.IP "alias [\fInewcommand\fR] [\fIoldcommand\fR]" 2
.IX Item "alias [newcommand] [oldcommand]"
Copies the oldcommand as newcommand.
.IP "bulkrename" 2
.IX Item "bulkrename"
This command opens a list of selected files in an external editor.  After you
edit and save the file, it will generate a shell script which does bulk
renaming according to the changes you did in the file.
.Sp
This shell script is opened in an editor for you to review.  After you close
it, it will be executed.
.IP "cd [\fIdirectory\fR]" 2
.IX Item "cd [directory]"
The cd command changes the directory.  The command \f(CW\*(C`:cd \-\*(C'\fR is equivalent to
typing ``.
.IP "chain \fIcommand1\fR[; \fIcommand2\fR[; \fIcommand3\fR...]]" 2
.IX Item "chain command1[; command2[; command3...]]"
Combines multiple commands into one, separated by semicolons.
.IP "chmod \fIoctal_number\fR" 2
.IX Item "chmod octal_number"
Sets the permissions of the selection to the octal number.
.Sp
The octal number is between 000 and 777. The digits specify the permissions for
the user, the group and others.  A 1 permits execution, a 2 permits writing, a
4 permits reading.  Add those numbers to combine them. So a 7 permits
everything.
.Sp
Key bindings in the form of [\-+]<who><what> and =<octal> also exist.  For
example, \fB+ar\fR allows reading for everyone, \-ow forbids others to write and
=777 allows everything.
.Sp
See also: man 1 chmod
.IP "cmap \fIkey\fR \fIcommand\fR" 2
.IX Item "cmap key command"
Binds keys for the console. Works like the \f(CW\*(C`map\*(C'\fR command.
.IP "console [\-p\fIN\fR] \fIcommand\fR" 2
.IX Item "console [-pN] command"
Opens the console with the command already typed in.  The cursor is placed at
\&\fIN\fR.
.IP "copycmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2
.IX Item "copycmap key newkey [newkey2 ...]"
See \f(CW\*(C`copymap\*(C'\fR
.IP "copymap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2
.IX Item "copymap key newkey [newkey2 ...]"
Copies the keybinding \fIkey\fR to \fInewkey\fR in the \*(L"browser\*(R" context.  This is a
deep copy, so if you change the new binding (or parts of it) later, the old one
is not modified.
.Sp
To copy key bindings of the console, taskview, or pager use \*(L"copycmap\*(R",
\&\*(L"copytmap\*(R" or \*(L"copypmap\*(R".
.IP "copypmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2
.IX Item "copypmap key newkey [newkey2 ...]"
See \f(CW\*(C`copymap\*(C'\fR
.IP "copytmap \fIkey\fR \fInewkey\fR [\fInewkey2\fR ...]" 2
.IX Item "copytmap key newkey [newkey2 ...]"
See \f(CW\*(C`copymap\*(C'\fR
.IP "cunmap [\fIkeys...\fR]" 2
.IX Item "cunmap [keys...]"
Removes key mappings of the console. Works like the \f(CW\*(C`unmap\*(C'\fR command.
.IP "delete [\fIconfirmation\fR]" 2
.IX Item "delete [confirmation]"
Destroy all files in the selection with a roundhouse kick.  ranger will ask for
a confirmation if you attempt to delete multiple (marked) files or non-empty
directories.
.Sp
When asking for confirmation, this command will only proceed if the last given
word starts with a `y'.
.IP "edit [\fIfilename\fR]" 2
.IX Item "edit [filename]"
Edit the current file or the file in the argument.
.IP "eval [\fI\-q\fR] \fIpython_code\fR" 2
.IX Item "eval [-q] python_code"
Evaluates the python code.  `fm' is a reference to the \s-1FM\s0 instance.  To display
text, use the function `p'.  The result is displayed on the screen unless you
use the \*(L"\-q\*(R" option.
.Sp
Examples:
 :eval fm
 :eval len(fm.env.directories)
 :eval p(\*(L"Hello World!\*(R")
.IP "filter [\fIstring\fR]" 2
.IX Item "filter [string]"
Displays only the files which contain the \fIstring\fR in their basename.  Running
this command without any parameter will reset the fitler.
.IP "find \fIpattern\fR" 2
.IX Item "find pattern"
Search files in the current directory that match the given (case-insensitive)
regular expression pattern as you type.  Once there is an unambiguous result,
it will be run immediately. (Or entered, if it's a directory.)
.IP "grep \fIpattern\fR" 2
.IX Item "grep pattern"
Looks for a string in all marked files or directories.
.IP "load_copy_buffer" 2
.IX Item "load_copy_buffer"
Load the copy buffer from \fI~/.config/ranger/copy_buffer\fR.  This can be used to
pass the list of copied files to another ranger instance.
.IP "map \fIkey\fR \fIcommand\fR" 2
.IX Item "map key command"
Assign the key combination to the given command.  Whenever you type the
key/keys, the command will be executed.  Additionally, if you use a quantifier
when typing the key, like 5j, it will be passed to the command as the attribute
\&\*(L"self.quantifier\*(R".
.Sp
The keys you bind with this command are accessible in the file browser only,
not in the console, task view or pager.  To bind keys there, use the commands
\&\*(L"cmap\*(R", \*(L"tmap\*(R" or \*(L"pmap\*(R".
.IP "mark \fIpattern\fR" 2
.IX Item "mark pattern"
Mark all files matching the regular expression pattern.
.IP "mark_tag [\fItags\fR]" 2
.IX Item "mark_tag [tags]"
Mark all tags that are tagged with either of the given tags.  When leaving out
the tag argument, all tagged files are marked.
.IP "mkdir \fIdirname\fR" 2
.IX Item "mkdir dirname"
Creates a directory with the name \fIdirname\fR.
.IP "open_with [\fIapplication\fR] [\fIflags\fR] [\fImode\fR]" 2
.IX Item "open_with [application] [flags] [mode]"
Open the selected files with the given application, unless it is omitted, in
which case the default application is used.  \fIflags\fR are characters out of
\&\*(L"sdpcwSDPCW\*(R" and \fImode\fR is any positive integer. Their meanings are discussed
in their own sections.
.IP "pmap \fIkey\fR \fIcommand\fR" 2
.IX Item "pmap key command"
Binds keys for the pager. Works like the \f(CW\*(C`map\*(C'\fR command.
.IP "punmap [\fIkeys ...\fR]" 2
.IX Item "punmap [keys ...]"
Removes key mappings of the pager. Works like the \f(CW\*(C`unmap\*(C'\fR command.
.IP "quit" 2
.IX Item "quit"
Like quit!, but closes only this tab if multiple tabs are open.
.IP "quit!" 2
.IX Item "quit!"
Quit ranger.  The current directory will be bookmarked as ' so you can re-enter
it by typing `` or '' the next time you start ranger.
.IP "relink \fInewpath\fR" 2
.IX Item "relink newpath"
Change the link destination of the current symlink file to <newpath>. First
<tab> will load the original link.
.IP "rename \fInewname\fR" 2
.IX Item "rename newname"
Rename the current file.  If a file with that name already exists, the renaming
will fail.  Also try the key binding A for appending something to a file name.
.IP "save_copy_buffer" 2
.IX Item "save_copy_buffer"
Save the copy buffer from \fI~/.config/ranger/copy_buffer\fR.  This can be used to
pass the list of copied files to another ranger instance.
.IP "search \fIpattern\fR" 2
.IX Item "search pattern"
Search files in the current directory that match the given (case insensitive)
regular expression pattern.
.IP "search_inc \fIpattern\fR" 2
.IX Item "search_inc pattern"
Search files in the current directory that match the given (case insensitive)
regular expression pattern.  This command gets you to matching files as you
type.
.IP "set \fIoption\fR \fIvalue\fR" 2
.IX Item "set option value"
Assigns a new value to an option.  Valid options are listed in the settings
section.  Use tab completion to get the current value of an option, though this
doesn't work for functions and regular expressions. Valid values are:
.Sp
.Vb 7
\& setting type   | example values
\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
\& bool           | true, false
\& integer        | 1, 23, 1337
\& string         | foo, hello world
\& list           | 1,2,3,4
\& none           | none
.Ve
.IP "setlocal [path=\fIpath\fR] \fIoption\fR \fIvalue\fR" 2
.IX Item "setlocal [path=path] option value"
Assigns a new value to an option, but locally for the directory given by
\&\fIpath\fR. This means, that this option only takes effect when visiting that
directory. If no path is given, uses the current directory.
.IP "shell [\-\fIflags\fR] \fIcommand\fR" 2
.IX Item "shell [-flags] command"
Run a shell command.  \fIflags\fR are discussed in their own section.
.IP "terminal" 2
.IX Item "terminal"
Spawns the \fIx\-terminal-emulator\fR starting in the current directory.
.IP "tmap \fIkey\fR \fIcommand\fR" 2
.IX Item "tmap key command"
Binds keys for the taskview. Works like the \f(CW\*(C`map\*(C'\fR command.
.IP "touch \fIfilename\fR" 2
.IX Item "touch filename"
Creates an empty file with the name \fIfilename\fR, unless it already exists.
.IP "tunmap [\fIkeys ...\fR]" 2
.IX Item "tunmap [keys ...]"
Removes key mappings of the taskview. Works like the \f(CW\*(C`unmap\*(C'\fR command.
.IP "unmap [\fIkeys\fR ...]" 2
.IX Item "unmap [keys ...]"
Removes the given key mappings in the \*(L"browser\*(R" context.  To unmap key bindings
in the console, taskview, or pager use \*(L"cunmap\*(R", \*(L"tunmap\*(R" or \*(L"punmap\*(R".
.IP "unmark \fIpattern\fR" 2
.IX Item "unmark pattern"
Unmark all files matching a regular expression pattern.
.IP "unmark_tag [\fItags\fR]" 2
.IX Item "unmark_tag [tags]"
Unmark all tags that are tagged with either of the given tags.  When leaving
out the tag argument, all tagged files are unmarked.
.SH "FILES"
.IX Header "FILES"
ranger reads several configuration files which are located in
\&\fI\f(CI$HOME\fI/.config/ranger\fR or \fI\f(CI$XDG_CONFIG_HOME\fI/ranger\fR if \f(CW$XDG_CONFIG_HOME\fR is
defined.  You can use the \-\-copy\-config option to obtain the default
configuration files.  Each of the files contains further documentation.
.PP
You don't need to copy the whole file though, most configuration files are
overlaid on top of the defaults (\fIcommands.py\fR, \fIrc.conf\fR) or can be
sub-classed (\fIcolorschemes\fR).
.PP
When starting ranger with the \fB\-\-clean\fR option, it will not access or create
any of these files.
.SS "\s-1CONFIGURATION\s0"
.IX Subsection "CONFIGURATION"
.IP "rc.conf" 10
.IX Item "rc.conf"
Contains a list of commands which are executed on startup.  Mostly key bindings
and settings are defined here.
.IP "commands.py" 10
.IX Item "commands.py"
A python module that defines commands which can be used in ranger's console by
typing \*(L":\*(R" or in the rc.conf file.
.IP "rifle.conf" 10
.IX Item "rifle.conf"
This is the configuration file for the built-in file launcher called \*(L"rifle\*(R".
.IP "scope.sh" 10
.IX Item "scope.sh"
This is a script that handles file previews.  When the options
\&\fIuse_preview_script\fR and \fIpreview_files\fR are set, the program specified in
the option \fIpreview_script\fR is run and its output and/or exit code determines
rangers reaction.
.IP "colorschemes/" 10
.IX Item "colorschemes/"
Colorschemes can be placed here.
.IP "plugins/" 10
.IX Item "plugins/"
Plugins can be placed here.
.SS "\s-1STORAGE\s0"
.IX Subsection "STORAGE"
.IP "bookmarks" 10
.IX Item "bookmarks"
This file contains a list of bookmarks.  The syntax is /^(.):(.*)$/. The first
character is the bookmark key and the rest after the colon is the path to the
file.  In ranger, bookmarks can be set by typing m<key>, accessed by typing
\&'<key> and deleted by typing um<key>.
.IP "copy_buffer" 10
.IX Item "copy_buffer"
When running the command :save_copy_buffer, the paths of all currently copied
files are saved in this file.  You can later run :load_copy_buffer to copy the
same files again, pass them to another ranger instance or process them in a
script.
.IP "history" 10
.IX Item "history"
Contains a list of commands that have been previously typed in.
.IP "tagged" 10
.IX Item "tagged"
Contains a list of tagged files. The syntax is /^(.:)?(.*)$/ where the first
letter is the optional name of the tag and the rest after the optional colon is
the path to the file.  In ranger, tags can be set by pressing t and removed
with T.  To assign a named tag, type "<tagname>.
.SH "ENVIRONMENT"
.IX Header "ENVIRONMENT"
These environment variables have an effect on ranger:
.IP "\s-1RANGER_LEVEL\s0" 8
.IX Item "RANGER_LEVEL"
ranger sets this environment variable to \*(L"1\*(R" or increments it if it already
exists.  External programs can determine whether they were spawned from ranger
by checking for this variable.
.IP "\s-1EDITOR\s0" 8
.IX Item "EDITOR"
Defines the editor to be used for the \*(L"E\*(R" key.  Defaults to the first installed
program out of \*(L"vim\*(R", \*(L"emacs\*(R" and \*(L"nano\*(R".
.IP "\s-1SHELL\s0" 8
.IX Item "SHELL"
Defines the shell that ranger is going to use with the :shell command and
the \*(L"S\*(R" key.  Defaults to \*(L"bash\*(R".
.IP "\s-1TERMCMD\s0" 8
.IX Item "TERMCMD"
Defines the terminal emulator command that ranger is going to use with the
:terminal command and the \*(L"t\*(R" run flag.  Defaults to \*(L"x\-terminal-emulator\*(R" or
\&\*(L"xterm\*(R"
.IP "\s-1XDG_CONFIG_HOME\s0" 8
.IX Item "XDG_CONFIG_HOME"
Specifies the directory for configuration files. Defaults to \fI\f(CI$HOME\fI/.config\fR.
.IP "\s-1PYTHONOPTIMIZE\s0" 8
.IX Item "PYTHONOPTIMIZE"
This variable determines the optimize level of python.
.Sp
Using PYTHONOPTIMIZE=1 (like python \-O) will make python discard assertion
statements.  You will gain efficiency at the cost of losing some debug info.
.Sp
Using PYTHONOPTIMIZE=2 (like python \-OO) will additionally discard any
docstrings.  Using this will disable the <F1> key on commands.
.SH "EXAMPLES"
.IX Header "EXAMPLES"
There are various examples on how to extend ranger with plugins or combine
ranger with other programs.  These can be found in the \fIexamples/\fR directory
that is provided along with the source code.
.SH "LICENSE"
.IX Header "LICENSE"
\&\s-1GNU\s0 General Public License 3 or (at your option) any later version.
.SH "LINKS"
.IX Header "LINKS"
.IP "Download: http://ranger.nongnu.org/ranger\-stable.tar.gz <http://ranger.nongnu.org/ranger-stable.tar.gz>" 4
.IX Item "Download: http://ranger.nongnu.org/ranger-stable.tar.gz <http://ranger.nongnu.org/ranger-stable.tar.gz>"
.PD 0
.IP "The project page: <http://ranger.nongnu.org/>" 4
.IX Item "The project page: <http://ranger.nongnu.org/>"
.IP "The mailing list: <http://savannah.nongnu.org/mail/?group=ranger>" 4
.IX Item "The mailing list: <http://savannah.nongnu.org/mail/?group=ranger>"
.PD
.PP
ranger is maintained with the git version control system.  To fetch a fresh
copy, run:
.PP
.Vb 1
\& git clone git://git.savannah.nongnu.org/ranger.git
.Ve
.SH "BUGS"
.IX Header "BUGS"
Report bugs here: <http://savannah.nongnu.org/bugs/?group=ranger>
.PP
Please include as much relevant information as possible.  For the most
diagnostic output, run ranger like this: \f(CW\*(C`PYTHONOPTIMIZE= ranger \-\-debug\*(C'\fR
al_protocol_types.h> + +#define AES128_GCM_KEY_LENGTH 16 +#define AES128_GCM_IV_LENGTH 16 +#define AES128_GCM_TAG_LENGTH 16 + +int omemo_crypto_init(void); +/** +* Callback for a secure random number generator. +* This function shall fill the provided buffer with random bytes. +* +* @param data pointer to the output buffer +* @param len size of the output buffer +* @return 0 on success, negative on failure +*/ +int omemo_random_func(uint8_t *data, size_t len, void *user_data); + +/** +* Callback for an HMAC-SHA256 implementation. +* This function shall initialize an HMAC context with the provided key. +* +* @param hmac_context private HMAC context pointer +* @param key pointer to the key +* @param key_len length of the key +* @return 0 on success, negative on failure +*/ +int omemo_hmac_sha256_init_func(void **hmac_context, const uint8_t *key, size_t key_len, void *user_data); + +/** +* Callback for an HMAC-SHA256 implementation. +* This function shall update the HMAC context with the provided data +* +* @param hmac_context private HMAC context pointer +* @param data pointer to the data +* @param data_len length of the data +* @return 0 on success, negative on failure +*/ +int omemo_hmac_sha256_update_func(void *hmac_context, const uint8_t *data, size_t data_len, void *user_data); + +/** +* Callback for an HMAC-SHA256 implementation. +* This function shall finalize an HMAC calculation and populate the output +* buffer with the result. +* +* @param hmac_context private HMAC context pointer +* @param output buffer to be allocated and populated with the result +* @return 0 on success, negative on failure +*/ +int omemo_hmac_sha256_final_func(void *hmac_context, signal_buffer **output, void *user_data); + +/** +* Callback for an HMAC-SHA256 implementation. +* This function shall free the private context allocated in +* hmac_sha256_init_func. +* +* @param hmac_context private HMAC context pointer +*/ +void omemo_hmac_sha256_cleanup_func(void *hmac_context, void *user_data); + +/** +* Callback for a SHA512 message digest implementation. +* This function shall initialize a digest context. +* +* @param digest_context private digest context pointer +* @return 0 on success, negative on failure +*/ +int omemo_sha512_digest_init_func(void **digest_context, void *user_data); + +/** +* Callback for a SHA512 message digest implementation. +* This function shall update the digest context with the provided data. +* +* @param digest_context private digest context pointer +* @param data pointer to the data +* @param data_len length of the data +* @return 0 on success, negative on failure +*/ +int omemo_sha512_digest_update_func(void *digest_context, const uint8_t *data, size_t data_len, void *user_data); + +/** +* Callback for a SHA512 message digest implementation. +* This function shall finalize the digest calculation, populate the +* output buffer with the result, and prepare the context for reuse. +* +* @param digest_context private digest context pointer +* @param output buffer to be allocated and populated with the result +* @return 0 on success, negative on failure +*/ +int omemo_sha512_digest_final_func(void *digest_context, signal_buffer **output, void *user_data); + +/** +* Callback for a SHA512 message digest implementation. +* This function shall free the private context allocated in +* sha512_digest_init_func. +* +* @param digest_context private digest context pointer +*/ +void omemo_sha512_digest_cleanup_func(void *digest_context, void *user_data); + +/** +* Callback for an AES encryption implementation. +* +* @param output buffer to be allocated and populated with the ciphertext +* @param cipher specific cipher variant to use, either SG_CIPHER_AES_CTR_NOPADDING or SG_CIPHER_AES_CBC_PKCS5 +* @param key the encryption key +* @param key_len length of the encryption key +* @param iv the initialization vector +* @param iv_len length of the initialization vector +* @param plaintext the plaintext to encrypt +* @param plaintext_len length of the plaintext +* @return 0 on success, negative on failure +*/ +int omemo_encrypt_func(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); + +/** +* Callback for an AES decryption implementation. +* +* @param output buffer to be allocated and populated with the plaintext +* @param cipher specific cipher variant to use, either SG_CIPHER_AES_CTR_NOPADDING or SG_CIPHER_AES_CBC_PKCS5 +* @param key the encryption key +* @param key_len length of the encryption key +* @param iv the initialization vector +* @param iv_len length of the initialization vector +* @param ciphertext the ciphertext to decrypt +* @param ciphertext_len length of the ciphertext +* @return 0 on success, negative on failure +*/ +int omemo_decrypt_func(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); + +int aes128gcm_encrypt(unsigned char *ciphertext, size_t *ciphertext_len, + unsigned char *tag, size_t *tag_len, + const unsigned char *const plaintext, size_t plaintext_len, + const unsigned char *const iv, const unsigned char *const key); + +int aes128gcm_decrypt(unsigned char *plaintext, + size_t *plaintext_len, const unsigned char *const ciphertext, + size_t ciphertext_len, const unsigned char *const iv, + const unsigned char *const key, const unsigned char *const tag); diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c new file mode 100644 index 00000000..7b3855dd --- /dev/null +++ b/src/omemo/omemo.c @@ -0,0 +1,1410 @@ +#include <sys/time.h> +#include <sys/stat.h> + +#include <assert.h> +#include <errno.h> +#include <glib.h> +#include <pthread.h> +#include <signal/key_helper.h> +#include <signal/protocol.h> +#include <signal/signal_protocol.h> +#include <signal/session_builder.h> +#include <signal/session_cipher.h> +#include <gcrypt.h> + +#include "config/account.h" +#include "config/files.h" +#include "log.h" +#include "omemo/crypto.h" +#include "omemo/omemo.h" +#include "omemo/store.h" +#include "ui/ui.h" +#include "ui/window_list.h" +#include "xmpp/connection.h" +#include "xmpp/muc.h" +#include "xmpp/omemo.h" +#include "xmpp/roster_list.h" +#include "xmpp/xmpp.h" + +static gboolean loaded; + +static void _generate_pre_keys(int count); +static void _generate_signed_pre_key(void); +static void _load_identity(void); +static void _load_trust(void); +static void _load_sessions(void); +static void _lock(void *user_data); +static void _unlock(void *user_data); +static void _omemo_log(int level, const char *message, size_t len, void *user_data); +static gboolean _handle_own_device_list(const char *const jid, GList *device_list); +static gboolean _handle_device_list_start_session(const char *const jid, GList *device_list); +static char * _omemo_fingerprint(ec_public_key *identity, gboolean formatted); +static unsigned char *_omemo_fingerprint_decode(const char *const fingerprint, size_t *len); +static void _cache_device_identity(const char *const jid, uint32_t device_id, ec_public_key *identity); +static void _g_hash_table_free(GHashTable *hash_table); + +typedef gboolean (*OmemoDeviceListHandler)(const char *const jid, GList *device_list); + +struct omemo_context_t { + pthread_mutexattr_t attr; + pthread_mutex_t lock; + signal_context *signal; + uint32_t device_id; + GHashTable *device_list; + GHashTable *device_list_handler; + ratchet_identity_key_pair *identity_key_pair; + uint32_t registration_id; + uint32_t signed_pre_key_id; + signal_protocol_store_context *store; + GHashTable *session_store; + GHashTable *pre_key_store; + GHashTable *signed_pre_key_store; + identity_key_store_t identity_key_store; + GHashTable *device_ids; + GString *identity_filename; + GKeyFile *identity_keyfile; + GString *trust_filename; + GKeyFile *trust_keyfile; + GString *sessions_filename; + GKeyFile *sessions_keyfile; + GHashTable *known_devices; + Autocomplete fingerprint_ac; +}; + +static omemo_context omemo_ctx; + +void +omemo_init(void) +{ + log_info("OMEMO: initialising"); + if (omemo_crypto_init() != 0) { + cons_show("Error initializing OMEMO crypto"); + } + + pthread_mutexattr_init(&omemo_ctx.attr); + pthread_mutexattr_settype(&omemo_ctx.attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&omemo_ctx.lock, &omemo_ctx.attr); + + omemo_ctx.fingerprint_ac = autocomplete_new(); +} + +void +omemo_on_connect(ProfAccount *account) +{ + GError *error = NULL; + + if (signal_context_create(&omemo_ctx.signal, &omemo_ctx) != 0) { + cons_show("Error initializing OMEMO context"); + return; + } + + if (signal_context_set_log_function(omemo_ctx.signal, _omemo_log) != 0) { + cons_show("Error initializing OMEMO log"); + } + + signal_crypto_provider crypto_provider = { + .random_func = omemo_random_func, + .hmac_sha256_init_func = omemo_hmac_sha256_init_func, + .hmac_sha256_update_func = omemo_hmac_sha256_update_func, + .hmac_sha256_final_func = omemo_hmac_sha256_final_func, + .hmac_sha256_cleanup_func = omemo_hmac_sha256_cleanup_func, + .sha512_digest_init_func = omemo_sha512_digest_init_func, + .sha512_digest_update_func = omemo_sha512_digest_update_func, + .sha512_digest_final_func = omemo_sha512_digest_final_func, + .sha512_digest_cleanup_func = omemo_sha512_digest_cleanup_func, + .encrypt_func = omemo_encrypt_func, + .decrypt_func = omemo_decrypt_func, + .user_data = NULL + }; + + if (signal_context_set_crypto_provider(omemo_ctx.signal, &crypto_provider) != 0) { + cons_show("Error initializing OMEMO crypto"); + return; + } + + signal_context_set_locking_functions(omemo_ctx.signal, _lock, _unlock); + + signal_protocol_store_context_create(&omemo_ctx.store, omemo_ctx.signal); + + omemo_ctx.session_store = session_store_new(); + signal_protocol_session_store session_store = { + .load_session_func = load_session, + .get_sub_device_sessions_func = get_sub_device_sessions, + .store_session_func = store_session, + .contains_session_func = contains_session, + .delete_session_func = delete_session, + .delete_all_sessions_func = delete_all_sessions, + .destroy_func = NULL, + .user_data = omemo_ctx.session_store + }; + signal_protocol_store_context_set_session_store(omemo_ctx.store, &session_store); + + omemo_ctx.pre_key_store = pre_key_store_new(); + signal_protocol_pre_key_store pre_key_store = { + .load_pre_key = load_pre_key, + .store_pre_key = store_pre_key, + .contains_pre_key = contains_pre_key, + .remove_pre_key = remove_pre_key, + .destroy_func = NULL, + .user_data = omemo_ctx.pre_key_store + }; + signal_protocol_store_context_set_pre_key_store(omemo_ctx.store, &pre_key_store); + + omemo_ctx.signed_pre_key_store = signed_pre_key_store_new(); + signal_protocol_signed_pre_key_store signed_pre_key_store = { + .load_signed_pre_key = load_signed_pre_key, + .store_signed_pre_key = store_signed_pre_key, + .contains_signed_pre_key = contains_signed_pre_key, + .remove_signed_pre_key = remove_signed_pre_key, + .destroy_func = NULL, + .user_data = omemo_ctx.signed_pre_key_store + }; + signal_protocol_store_context_set_signed_pre_key_store(omemo_ctx.store, &signed_pre_key_store); + + identity_key_store_new(&omemo_ctx.identity_key_store); + signal_protocol_identity_key_store identity_key_store = { + .get_identity_key_pair = get_identity_key_pair, + .get_local_registration_id = get_local_registration_id, + .save_identity = save_identity, + .is_trusted_identity = is_trusted_identity, + .destroy_func = NULL, + .user_data = &omemo_ctx.identity_key_store + }; + signal_protocol_store_context_set_identity_key_store(omemo_ctx.store, &identity_key_store); + + + loaded = FALSE; + omemo_ctx.device_list = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_list_free); + omemo_ctx.device_list_handler = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + omemo_ctx.known_devices = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_g_hash_table_free); + + omemo_ctx.fingerprint_ac = autocomplete_new(); + + char *omemodir = files_get_data_path(DIR_OMEMO); + GString *basedir = g_string_new(omemodir); + free(omemodir); + gchar *account_dir = str_replace(account->jid, "@", "_at_"); + g_string_append(basedir, "/"); + g_string_append(basedir, account_dir); + g_string_append(basedir, "/"); + free(account_dir); + + omemo_ctx.identity_filename = g_string_new(basedir->str); + g_string_append(omemo_ctx.identity_filename, "identity.txt"); + omemo_ctx.trust_filename = g_string_new(basedir->str); + g_string_append(omemo_ctx.trust_filename, "trust.txt"); + omemo_ctx.sessions_filename = g_string_new(basedir->str); + g_string_append(omemo_ctx.sessions_filename, "sessions.txt"); + + + errno = 0; + int res = g_mkdir_with_parents(basedir->str, S_IRWXU); + if (res == -1) { + char *errmsg = strerror(errno); + if (errmsg) { + log_error("OMEMO: error creating directory: %s, %s", basedir->str, errmsg); + } else { + log_error("OMEMO: creating directory: %s", basedir->str); + } + } + + g_string_free(basedir, TRUE); + + omemo_devicelist_subscribe(); + + omemo_ctx.identity_keyfile = g_key_file_new(); + omemo_ctx.trust_keyfile = g_key_file_new(); + omemo_ctx.sessions_keyfile = g_key_file_new(); + + if (g_key_file_load_from_file(omemo_ctx.identity_keyfile, omemo_ctx.identity_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) { + _load_identity(); + } else if (error->code != G_FILE_ERROR_NOENT) { + log_warning("OMEMO: error loading identity from: %s, %s", omemo_ctx.identity_filename->str, error->message); + return; + } + + error = NULL; + if (g_key_file_load_from_file(omemo_ctx.trust_keyfile, omemo_ctx.trust_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) { + _load_trust(); + } else if (error->code != G_FILE_ERROR_NOENT) { + log_warning("OMEMO: error loading trust from: %s, %s", omemo_ctx.sessions_filename->str, error->message); + } + + error = NULL; + if (g_key_file_load_from_file(omemo_ctx.sessions_keyfile, omemo_ctx.sessions_filename->str, G_KEY_FILE_KEEP_COMMENTS, &error)) { + _load_sessions(); + } else if (error->code != G_FILE_ERROR_NOENT) { + log_warning("OMEMO: error loading sessions from: %s, %s", omemo_ctx.sessions_filename->str, error->message); + } +} + +void +omemo_on_disconnect(void) +{ + signal_protocol_signed_pre_key_remove_key(omemo_ctx.store, omemo_ctx.signed_pre_key_id); + _g_hash_table_free(omemo_ctx.signed_pre_key_store); + + GHashTableIter iter; + gpointer id; + + g_hash_table_iter_init(&iter, omemo_ctx.pre_key_store); + while (g_hash_table_iter_next(&iter, &id, NULL)) { + signal_protocol_pre_key_remove_key(omemo_ctx.store, GPOINTER_TO_INT(id)); + } + + _g_hash_table_free(omemo_ctx.pre_key_store); + + g_string_free(omemo_ctx.identity_filename, TRUE); + g_key_file_free(omemo_ctx.identity_keyfile); + g_string_free(omemo_ctx.trust_filename, TRUE); + g_key_file_free(omemo_ctx.trust_keyfile); + g_string_free(omemo_ctx.sessions_filename, TRUE); + g_key_file_free(omemo_ctx.sessions_keyfile); +} + +void +omemo_generate_crypto_materials(ProfAccount *account) +{ + if (loaded) { + return; + } + + log_info("Generate long term OMEMO cryptography metarials"); + + /* Device ID */ + gcry_randomize(&omemo_ctx.device_id, 4, GCRY_VERY_STRONG_RANDOM); + omemo_ctx.device_id &= 0x7fffffff; + g_key_file_set_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, omemo_ctx.device_id); + log_info("OMEMO: device id: %d", omemo_ctx.device_id); + + /* Identity key */ + signal_protocol_key_helper_generate_identity_key_pair(&omemo_ctx.identity_key_pair, omemo_ctx.signal); + + ec_public_key_serialize(&omemo_ctx.identity_key_store.public, ratchet_identity_key_pair_get_public(omemo_ctx.identity_key_pair)); + char *identity_key_public = g_base64_encode(signal_buffer_data(omemo_ctx.identity_key_store.public), signal_buffer_len(omemo_ctx.identity_key_store.public)); + g_key_file_set_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, identity_key_public); + g_free(identity_key_public); + + ec_private_key_serialize(&omemo_ctx.identity_key_store.private, ratchet_identity_key_pair_get_private(omemo_ctx.identity_key_pair)); + char *identity_key_private = g_base64_encode(signal_buffer_data(omemo_ctx.identity_key_store.private), signal_buffer_len(omemo_ctx.identity_key_store.private)); + g_key_file_set_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, identity_key_private); + g_free(identity_key_private); + + /* Registration ID */ + signal_protocol_key_helper_generate_registration_id(&omemo_ctx.registration_id, 0, omemo_ctx.signal); + g_key_file_set_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, omemo_ctx.registration_id); + + /* Pre keys */ + _generate_pre_keys(100); + + /* Signed pre key */ + _generate_signed_pre_key(); + + omemo_identity_keyfile_save(); + + loaded = TRUE; + + omemo_publish_crypto_materials(); + omemo_start_sessions(); +} + +void +omemo_publish_crypto_materials(void) +{ + if (loaded != TRUE) { + log_error("OMEMO: cannot publish crypto materials before they are generated"); + return; + } + + Jid *jid = jid_create(connection_get_fulljid()); + + /* Ensure we get our current device list, and it gets updated with our + * device_id */ + g_hash_table_insert(omemo_ctx.device_list_handler, strdup(jid->barejid), _handle_own_device_list); + omemo_devicelist_request(jid->barejid); + + omemo_bundle_publish(true); + + jid_destroy(jid); +} + +void +omemo_start_sessions(void) +{ + GSList *contacts = roster_get_contacts(ROSTER_ORD_NAME); + if (contacts) { + GSList *curr = contacts; + for (curr = contacts; curr != NULL; curr = g_slist_next(curr)){ + PContact contact = curr->data; + const char *jid = p_contact_barejid(contact); + omemo_start_session(jid); + } + } +} + +void +omemo_start_session(const char *const barejid) +{ + log_info("OMEMO: start session with %s", barejid); + GList *device_list = g_hash_table_lookup(omemo_ctx.device_list, barejid); + if (!device_list) { + log_info("OMEMO: missing device list for %s", barejid); + omemo_devicelist_request(barejid); + g_hash_table_insert(omemo_ctx.device_list_handler, strdup(barejid), _handle_device_list_start_session); + return; + } + + GList *device_id; + for (device_id = device_list; device_id != NULL; device_id = device_id->next) { + omemo_bundle_request(barejid, GPOINTER_TO_INT(device_id->data), omemo_start_device_session_handle_bundle, free, strdup(barejid)); + } +} + +void +omemo_start_muc_sessions(const char *const roomjid) +{ + GList *roster = muc_roster(roomjid); + GList *iter; + for (iter = roster; iter != NULL; iter = iter->next) { + Occupant *occupant = (Occupant *)iter->data; + Jid *jid = jid_create(occupant->jid); + omemo_start_session(jid->barejid); + jid_destroy(jid); + } + g_list_free(roster); +} + +gboolean +omemo_loaded(void) +{ + return loaded; +} + +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_data(buffer), *length); + signal_buffer_free(buffer); +} + +void +omemo_signed_prekey(unsigned char **output, size_t *length) +{ + session_signed_pre_key *signed_pre_key; + signal_buffer *buffer = NULL; + + if (signal_protocol_signed_pre_key_load_key(omemo_ctx.store, &signed_pre_key, omemo_ctx.signed_pre_key_id) != SG_SUCCESS) { + *output = NULL; + *length = 0; + return; + } + + ec_public_key_serialize(&buffer, ec_key_pair_get_public(session_signed_pre_key_get_key_pair(signed_pre_key))); + SIGNAL_UNREF(signed_pre_key); + *length = signal_buffer_len(buffer); + *output = malloc(*length); + memcpy(*output, signal_buffer_data(buffer), *length); + signal_buffer_free(buffer); +} + +void +omemo_signed_prekey_signature(unsigned char **output, size_t *length) +{ + session_signed_pre_key *signed_pre_key; + + if (signal_protocol_signed_pre_key_load_key(omemo_ctx.store, &signed_pre_key, omemo_ctx.signed_pre_key_id) != SG_SUCCESS) { + *output = NULL; + *length = 0; + return; + } + + *length = session_signed_pre_key_get_signature_len(signed_pre_key); + *output = malloc(*length); + memcpy(*output, session_signed_pre_key_get_signature(signed_pre_key), *length); + SIGNAL_UNREF(signed_pre_key); +} + +void +omemo_prekeys(GList **prekeys, GList **ids, GList **lengths) +{ + GHashTableIter iter; + gpointer id; + + g_hash_table_iter_init(&iter, omemo_ctx.pre_key_store); + while (g_hash_table_iter_next(&iter, &id, NULL)) { + session_pre_key *pre_key; + int ret; + ret = signal_protocol_pre_key_load_key(omemo_ctx.store, &pre_key, GPOINTER_TO_INT(id)); + if (ret != SG_SUCCESS) { + continue; + } + + signal_buffer *public_key; + ec_public_key_serialize(&public_key, ec_key_pair_get_public(session_pre_key_get_key_pair(pre_key))); + SIGNAL_UNREF(pre_key); + size_t length = signal_buffer_len(public_key); + unsigned char *prekey_value = malloc(length); + memcpy(prekey_value, signal_buffer_data(public_key), length); + signal_buffer_free(public_key); + + *prekeys = g_list_append(*prekeys, prekey_value); + *ids = g_list_append(*ids, GINT_TO_POINTER(id)); + *lengths = g_list_append(*lengths, GINT_TO_POINTER(length)); + } +} + +void +omemo_set_device_list(const char *const from, GList * device_list) +{ + Jid *jid; + if (from) { + jid = jid_create(from); + } else { + jid = jid_create(connection_get_fulljid()); + } + + g_hash_table_insert(omemo_ctx.device_list, strdup(jid->barejid), device_list); + + OmemoDeviceListHandler handler = g_hash_table_lookup(omemo_ctx.device_list_handler, jid->barejid); + if (handler) { + gboolean keep = handler(jid->barejid, device_list); + if (!keep) { + g_hash_table_remove(omemo_ctx.device_list_handler, jid->barejid); + } + } + + jid_destroy(jid); +} + +GKeyFile * +omemo_identity_keyfile(void) +{ + return omemo_ctx.identity_keyfile; +} + +void +omemo_identity_keyfile_save(void) +{ + GError *error = NULL; + + if (!g_key_file_save_to_file(omemo_ctx.identity_keyfile, omemo_ctx.identity_filename->str, &error)) { + log_error("OMEMO: error saving identity to: %s, %s", omemo_ctx.identity_filename->str, error->message); + } +} + +GKeyFile * +omemo_trust_keyfile(void) +{ + return omemo_ctx.trust_keyfile; +} + +void +omemo_trust_keyfile_save(void) +{ + GError *error = NULL; + + if (!g_key_file_save_to_file(omemo_ctx.trust_keyfile, omemo_ctx.trust_filename->str, &error)) { + log_error("OMEMO: error saving trust to: %s, %s", omemo_ctx.trust_filename->str, error->message); + } +} + +GKeyFile * +omemo_sessions_keyfile(void) +{ + return omemo_ctx.sessions_keyfile; +} + +void +omemo_sessions_keyfile_save(void) +{ + GError *error = NULL; + + if (!g_key_file_save_to_file(omemo_ctx.sessions_keyfile, omemo_ctx.sessions_filename->str, &error)) { + log_error("OMEMO: error saving sessions to: %s, %s", omemo_ctx.sessions_filename->str, error->message); + } +} + +void +omemo_start_device_session(const char *const jid, uint32_t device_id, + GList *prekeys, uint32_t signed_prekey_id, + const unsigned char *const signed_prekey_raw, size_t signed_prekey_len, + const unsigned char *const signature, size_t signature_len, + const unsigned char *const identity_key_raw, size_t identity_key_len) +{ + signal_protocol_address address = { + .name = jid, + .name_len = strlen(jid), + .device_id = device_id, + }; + + ec_public_key *identity_key; + curve_decode_point(&identity_key, identity_key_raw, identity_key_len, omemo_ctx.signal); + _cache_device_identity(jid, device_id, identity_key); + + gboolean trusted = is_trusted_identity(&address, (uint8_t *)identity_key_raw, identity_key_len, &omemo_ctx.identity_key_store); + + if (!trusted) { + goto out; + } + + if (!contains_session(&address, omemo_ctx.session_store)) { + int res; + session_pre_key_bundle *bundle; + signal_protocol_address *address; + + address = malloc(sizeof(signal_protocol_address)); + address->name = strdup(jid); + address->name_len = strlen(jid); + address->device_id = device_id; + + session_builder *builder; + res = session_builder_create(&builder, omemo_ctx.store, address, omemo_ctx.signal); + if (res != 0) { + log_error("OMEMO: cannot create session builder for %s device %d", jid, device_id); + goto out; + } + + int prekey_index; + gcry_randomize(&prekey_index, sizeof(int), GCRY_STRONG_RANDOM); + prekey_index %= g_list_length(prekeys); + omemo_key_t *prekey = g_list_nth_data(prekeys, prekey_index); + + ec_public_key *prekey_public; + curve_decode_point(&prekey_public, prekey->data, prekey->length, omemo_ctx.signal); + ec_public_key *signed_prekey; + curve_decode_point(&signed_prekey, signed_prekey_raw, signed_prekey_len, omemo_ctx.signal); + + res = session_pre_key_bundle_create(&bundle, 0, device_id, prekey->id, prekey_public, signed_prekey_id, signed_prekey, signature, signature_len, identity_key); + if (res != 0) { + log_error("OMEMO: cannot create pre key bundle for %s device %d", jid, device_id); + goto out; + } + + res = session_builder_process_pre_key_bundle(builder, bundle); + if (res != 0) { + log_error("OMEMO: cannot process pre key bundle for %s device %d", jid, device_id); + goto out; + } + + log_info("OMEMO: create session with %s device %d", jid, device_id); + } + +out: + SIGNAL_UNREF(identity_key); +} + +char * +omemo_on_message_send(ProfWin *win, const char *const message, gboolean request_receipt, gboolean muc) +{ + char *id = NULL; + int res; + Jid *jid = jid_create(connection_get_fulljid()); + GList *keys = NULL; + + unsigned char *key; + unsigned char *iv; + unsigned char *ciphertext; + unsigned char *tag; + unsigned char *key_tag; + size_t ciphertext_len, tag_len; + + ciphertext_len = strlen(message); + ciphertext = malloc(ciphertext_len); + tag_len = AES128_GCM_TAG_LENGTH; + tag = gcry_malloc_secure(tag_len); + key_tag = gcry_malloc_secure(AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH); + + key = gcry_random_bytes_secure(AES128_GCM_KEY_LENGTH, GCRY_VERY_STRONG_RANDOM); + iv = gcry_random_bytes_secure(AES128_GCM_IV_LENGTH, GCRY_VERY_STRONG_RANDOM); + + res = aes128gcm_encrypt(ciphertext, &ciphertext_len, tag, &tag_len, (const unsigned char * const)message, strlen(message), iv, key); + if (res != 0) { + log_error("OMEMO: cannot encrypt message"); + goto out; + } + + memcpy(key_tag, key, AES128_GCM_KEY_LENGTH); + memcpy(key_tag + AES128_GCM_KEY_LENGTH, tag, AES128_GCM_TAG_LENGTH); + + GList *recipients = NULL; + if (muc) { + ProfMucWin *mucwin = (ProfMucWin *)win; + assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); + GList *roster = muc_roster(mucwin->roomjid); + GList *iter; + for (iter = roster; iter != NULL; iter = iter->next) { + Occupant *occupant = (Occupant *)iter->data; + Jid *jid = jid_create(occupant->jid); + if (!jid->barejid) { + log_warning("OMEMO: missing barejid for MUC %s occupant %s", mucwin->roomjid, occupant->nick); + } else { + recipients = g_list_append(recipients, strdup(jid->barejid)); + } + jid_destroy(jid); + } + g_list_free(roster); + } else { + ProfChatWin *chatwin = (ProfChatWin *)win; + assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); + recipients = g_list_append(recipients, strdup(chatwin->barejid)); + } + + GList *device_ids_iter; + + GList *recipients_iter; + for (recipients_iter = recipients; recipients_iter != NULL; recipients_iter = recipients_iter->next) { + GList *recipient_device_id = NULL; + recipient_device_id = g_hash_table_lookup(omemo_ctx.device_list, recipients_iter->data); + if (!recipient_device_id) { + log_warning("OMEMO: cannot find device ids for %s", recipients_iter->data); + continue; + } + + for (device_ids_iter = recipient_device_id; device_ids_iter != NULL; device_ids_iter = device_ids_iter->next) { + int res; + ciphertext_message *ciphertext; + session_cipher *cipher; + signal_protocol_address address = { + .name = recipients_iter->data, + .name_len = strlen(recipients_iter->data), + .device_id = GPOINTER_TO_INT(device_ids_iter->data) + }; + + res = session_cipher_create(&cipher, omemo_ctx.store, &address, omemo_ctx.signal); + if (res != 0) { + log_error("OMEMO: cannot create cipher for %s device id %d", address.name, address.device_id); + continue; + } + + res = session_cipher_encrypt(cipher, key_tag, AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH, &ciphertext); + session_cipher_free(cipher); + if (res != 0) { + log_error("OMEMO: cannot encrypt key for %s device id %d", address.name, address.device_id); + continue; + } + signal_buffer *buffer = ciphertext_message_get_serialized(ciphertext); + omemo_key_t *key = malloc(sizeof(omemo_key_t)); + key->length = signal_buffer_len(buffer); + key->data = malloc(key->length); + memcpy(key->data, signal_buffer_data(buffer), key->length); + key->device_id = GPOINTER_TO_INT(device_ids_iter->data); + key->prekey = ciphertext_message_get_type(ciphertext) == CIPHERTEXT_PREKEY_TYPE; + keys = g_list_append(keys, key); + SIGNAL_UNREF(ciphertext); + } + } + + g_list_free_full(recipients, free); + + if (!muc) { + GList *sender_device_id = g_hash_table_lookup(omemo_ctx.device_list, jid->barejid); + for (device_ids_iter = sender_device_id; device_ids_iter != NULL; device_ids_iter = device_ids_iter->next) { + int res; + ciphertext_message *ciphertext; + session_cipher *cipher; + signal_protocol_address address = { + .name = jid->barejid, + .name_len = strlen(jid->barejid), + .device_id = GPOINTER_TO_INT(device_ids_iter->data) + }; + + res = session_cipher_create(&cipher, omemo_ctx.store, &address, omemo_ctx.signal); + if (res != 0) { + log_error("OMEMO: cannot create cipher for %s device id %d", address.name, address.device_id); + continue; + } + + res = session_cipher_encrypt(cipher, key_tag, AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH, &ciphertext); + session_cipher_free(cipher); + if (res != 0) { + log_error("OMEMO: cannot encrypt key for %s device id %d", address.name, address.device_id); + continue; + } + signal_buffer *buffer = ciphertext_message_get_serialized(ciphertext); + omemo_key_t *key = malloc(sizeof(omemo_key_t)); + key->length = signal_buffer_len(buffer); + key->data = malloc(key->length); + memcpy(key->data, signal_buffer_data(buffer), key->length); + key->device_id = GPOINTER_TO_INT(device_ids_iter->data); + key->prekey = ciphertext_message_get_type(ciphertext) == CIPHERTEXT_PREKEY_TYPE; + keys = g_list_append(keys, key); + SIGNAL_UNREF(ciphertext); + } + } + + if (muc) { + ProfMucWin *mucwin = (ProfMucWin *)win; + assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); + id = message_send_chat_omemo(mucwin->roomjid, omemo_ctx.device_id, keys, iv, AES128_GCM_IV_LENGTH, ciphertext, ciphertext_len, request_receipt, TRUE); + } else { + ProfChatWin *chatwin = (ProfChatWin *)win; + assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); + id = message_send_chat_omemo(chatwin->barejid, omemo_ctx.device_id, keys, iv, AES128_GCM_IV_LENGTH, ciphertext, ciphertext_len, request_receipt, FALSE); + } + +out: + jid_destroy(jid); + g_list_free_full(keys, (GDestroyNotify)omemo_key_free); + free(ciphertext); + gcry_free(key); + gcry_free(iv); + gcry_free(tag); + gcry_free(key_tag); + + return id; +} + +char * +omemo_on_message_recv(const char *const from_jid, uint32_t sid, + const unsigned char *const iv, size_t iv_len, GList *keys, + const unsigned char *const payload, size_t payload_len, gboolean muc) +{ + unsigned char *plaintext = NULL; + Jid *sender = NULL; + Jid *from = jid_create(from_jid); + if (!from) { + log_error("Invalid jid %s", from_jid); + goto out; + } + + int res; + GList *key_iter; + omemo_key_t *key = NULL; + for (key_iter = keys; key_iter != NULL; key_iter = key_iter->next) { + if (((omemo_key_t *)key_iter->data)->device_id == omemo_ctx.device_id) { + key = key_iter->data; + break; + } + } + + if (!key) { + log_warning("OMEMO: Received a message with no corresponding key"); + goto out; + } + + if (muc) { + GList *roster = muc_roster(from->barejid); + GList *iter; + for (iter = roster; iter != NULL; iter = iter->next) { + Occupant *occupant = (Occupant *)iter->data; + if (g_strcmp0(occupant->nick, from->resourcepart) == 0) { + sender = jid_create(occupant->jid); + break; + } + } + g_list_free(roster); + if (!sender) { + log_warning("OMEMO: cannot find MUC message sender fulljid"); + goto out; + } + } else { + sender = jid_create(from->barejid); + } + + session_cipher *cipher; + signal_buffer *plaintext_key; + signal_protocol_address address = { + .name = sender->barejid, + .name_len = strlen(sender->barejid), + .device_id = sid + }; + + res = session_cipher_create(&cipher, omemo_ctx.store, &address, omemo_ctx.signal); + if (res != 0) { + log_error("OMEMO: cannot create session cipher"); + goto out; + } + + if (key->prekey) { + log_debug("OMEMO: decrypting message with prekey"); + pre_key_signal_message *message; + + pre_key_signal_message_deserialize(&message, key->data, key->length, omemo_ctx.signal); + + res = session_cipher_decrypt_pre_key_signal_message(cipher, message, NULL, &plaintext_key); + /* Replace used pre_key in bundle */ + uint32_t pre_key_id = pre_key_signal_message_get_pre_key_id(message); + ec_key_pair *ec_pair; + session_pre_key *new_pre_key; + curve_generate_key_pair(omemo_ctx.signal, &ec_pair); + session_pre_key_create(&new_pre_key, pre_key_id, ec_pair); + signal_protocol_pre_key_store_key(omemo_ctx.store, new_pre_key); + SIGNAL_UNREF(new_pre_key); + SIGNAL_UNREF(message); + SIGNAL_UNREF(ec_pair); + omemo_bundle_publish(true); + + if (res == 0) { + /* Start a new session */ + omemo_bundle_request(sender->barejid, sid, omemo_start_device_session_handle_bundle, free, strdup(sender->barejid)); + } + } else { + log_debug("OMEMO: decrypting message with existing session"); + signal_message *message; + signal_message_deserialize(&message, key->data, key->length, omemo_ctx.signal); + res = session_cipher_decrypt_signal_message(cipher, message, NULL, &plaintext_key); + SIGNAL_UNREF(message); + } + + session_cipher_free(cipher); + if (res != 0) { + log_error("OMEMO: cannot decrypt message key"); + goto out; + } + + if (signal_buffer_len(plaintext_key) != AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH) { + log_error("OMEMO: invalid key length"); + signal_buffer_free(plaintext_key); + goto out; + } + + size_t plaintext_len = payload_len; + plaintext = malloc(plaintext_len + 1); + res = aes128gcm_decrypt(plaintext, &plaintext_len, payload, payload_len, iv, + signal_buffer_data(plaintext_key), + signal_buffer_data(plaintext_key) + AES128_GCM_KEY_LENGTH); + signal_buffer_free(plaintext_key); + if (res != 0) { + log_error("OMEMO: cannot decrypt message: %s", gcry_strerror(res)); + free(plaintext); + plaintext = NULL; + goto out; + } + + plaintext[plaintext_len] = '\0'; + +out: + jid_destroy(from); + jid_destroy(sender); + return (char *)plaintext; +} + +char * +omemo_format_fingerprint(const char *const fingerprint) +{ + char *output = malloc(strlen(fingerprint) + strlen(fingerprint) / 8); + + int i, j; + for (i = 0, j = 0; i < strlen(fingerprint); i++) { + if (i > 0 && i % 8 == 0) { + output[j++] = '-'; + } + output[j++] = fingerprint[i]; + } + + output[j] = '\0'; + + return output; +} + +char * +omemo_own_fingerprint(gboolean formatted) +{ + ec_public_key *identity = ratchet_identity_key_pair_get_public(omemo_ctx.identity_key_pair); + return _omemo_fingerprint(identity, formatted); +} + +GList * +omemo_known_device_identities(const char *const jid) +{ + GHashTable *known_identities = g_hash_table_lookup(omemo_ctx.known_devices, jid); + if (!known_identities) { + return NULL; + } + + 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) +{ + GHashTable *known_identities = g_hash_table_lookup(omemo_ctx.known_devices, jid); + if (!known_identities) { + return FALSE; + } + + void *device_id = g_hash_table_lookup(known_identities, fingerprint); + if (!device_id) { + return FALSE; + } + + signal_protocol_address address = { + .name = jid, + .name_len = strlen(jid), + .device_id = GPOINTER_TO_INT(device_id), + }; + + size_t fingerprint_len; + unsigned char *fingerprint_raw = _omemo_fingerprint_decode(fingerprint, &fingerprint_len); + unsigned char djb_type[] = {'\x05'}; + signal_buffer *buffer = signal_buffer_create(djb_type, 1); + buffer = signal_buffer_append(buffer, fingerprint_raw, fingerprint_len); + + gboolean trusted = is_trusted_identity(&address, signal_buffer_data(buffer), signal_buffer_len(buffer), &omemo_ctx.identity_key_store); + + free(fingerprint_raw); + signal_buffer_free(buffer); + + return trusted; +} + +static char * +_omemo_fingerprint(ec_public_key *identity, gboolean formatted) +{ + int i; + signal_buffer *identity_public_key; + + ec_public_key_serialize(&identity_public_key, identity); + size_t identity_public_key_len = signal_buffer_len(identity_public_key); + unsigned char *identity_public_key_data = signal_buffer_data(identity_public_key); + + /* Skip first byte corresponding to signal DJB_TYPE */ + identity_public_key_len--; + identity_public_key_data = &identity_public_key_data[1]; + + char *fingerprint = malloc(identity_public_key_len * 2 + 1); + + for (i = 0; i < identity_public_key_len; i++) { + fingerprint[i * 2] = (identity_public_key_data[i] & 0xf0) >> 4; + fingerprint[i * 2] += '0'; + if (fingerprint[i * 2] > '9') { + fingerprint[i * 2] += 0x27; + } + + fingerprint[(i * 2) + 1] = identity_public_key_data[i] & 0x0f; + fingerprint[(i * 2) + 1] += '0'; + if (fingerprint[(i * 2) + 1] > '9') { + fingerprint[(i * 2) + 1] += 0x27; + } + } + + fingerprint[i * 2] = '\0'; + signal_buffer_free(identity_public_key); + + if (!formatted) { + return fingerprint; + } else { + char *formatted_fingerprint = omemo_format_fingerprint(fingerprint); + free(fingerprint); + return formatted_fingerprint; + } +} + +static unsigned char * +_omemo_fingerprint_decode(const char *const fingerprint, size_t *len) +{ + unsigned char *output = malloc(strlen(fingerprint) / 2 + 1); + + int i; + int j; + for (i = 0, j = 0; i < strlen(fingerprint);) { + if (!g_ascii_isxdigit(fingerprint[i])) { + i++; + continue; + } + + output[j] = g_ascii_xdigit_value(fingerprint[i++]) << 4; + output[j] |= g_ascii_xdigit_value(fingerprint[i++]); + j++; + } + + *len = j; + + return output; +} + +void +omemo_trust(const char *const jid, const char *const fingerprint_formatted) +{ + size_t len; + + GHashTable *known_identities = g_hash_table_lookup(omemo_ctx.known_devices, jid); + if (!known_identities) { + log_warning("OMEMO: cannot trust unknown device: %s", fingerprint_formatted); + cons_show("Cannot trust unknown device: %s", fingerprint_formatted); + return; + } + + /* Unformat fingerprint */ + char *fingerprint = malloc(strlen(fingerprint_formatted)); + int i; + int j; + for (i = 0, j = 0; fingerprint_formatted[i] != '\0'; i++) { + if (!g_ascii_isxdigit(fingerprint_formatted[i])) { + continue; + } + fingerprint[j++] = fingerprint_formatted[i]; + } + + fingerprint[j] = '\0'; + + uint32_t device_id = GPOINTER_TO_INT(g_hash_table_lookup(known_identities, fingerprint)); + free(fingerprint); + + if (!device_id) { + log_warning("OMEMO: cannot trust unknown device: %s", fingerprint_formatted); + cons_show("Cannot trust unknown device: %s", fingerprint_formatted); + return; + } + + /* TODO should not hardcode DJB_TYPE here + * should instead store identity key in known_identities along with + * device_id */ + signal_protocol_address address = { + .name = jid, + .name_len = strlen(jid), + .device_id = device_id, + }; + unsigned char *fingerprint_raw = _omemo_fingerprint_decode(fingerprint_formatted, &len); + unsigned char djb_type[] = {'\x05'}; + signal_buffer *buffer = signal_buffer_create(djb_type, 1); + buffer = signal_buffer_append(buffer, fingerprint_raw, len); + save_identity(&address, signal_buffer_data(buffer), signal_buffer_len(buffer), &omemo_ctx.identity_key_store); + free(fingerprint_raw); + signal_buffer_free(buffer); + + omemo_bundle_request(jid, device_id, omemo_start_device_session_handle_bundle, free, strdup(jid)); +} + +void +omemo_untrust(const char *const jid, const char *const fingerprint_formatted) +{ + size_t len; + unsigned char *fingerprint = _omemo_fingerprint_decode(fingerprint_formatted, &len); + + GHashTableIter iter; + gpointer key, value; + + GHashTable *trusted = g_hash_table_lookup(omemo_ctx.identity_key_store.trusted, jid); + if (!trusted) { + return; + } + + g_hash_table_iter_init(&iter, trusted); + while (g_hash_table_iter_next(&iter, &key, &value)) { + signal_buffer *buffer = value; + unsigned char *original = signal_buffer_data(buffer); + /* Skip DJB_TYPE byte */ + original++; + if ((signal_buffer_len(buffer) - 1) == len && memcmp(original, fingerprint, len) == 0) { + g_hash_table_remove(trusted, key); + } + } + free(fingerprint); +} + +static void +_lock(void *user_data) +{ + omemo_context *ctx = (omemo_context *)user_data; + pthread_mutex_lock(&ctx->lock); +} + +static void +_unlock(void *user_data) +{ + omemo_context *ctx = (omemo_context *)user_data; + pthread_mutex_unlock(&ctx->lock); +} + +static void +_omemo_log(int level, const char *message, size_t len, void *user_data) +{ + switch (level) { + case SG_LOG_ERROR: + log_error("OMEMO: %s", message); + break; + case SG_LOG_WARNING: + log_warning("OMEMO: %s", message); + break; + case SG_LOG_NOTICE: + case SG_LOG_INFO: + log_info("OMEMO: %s", message); + break; + case SG_LOG_DEBUG: + log_debug("OMEMO: %s", message); + break; + } +} + +static gboolean +_handle_own_device_list(const char *const jid, GList *device_list) +{ + if (!g_list_find(device_list, GINT_TO_POINTER(omemo_ctx.device_id))) { + device_list = g_list_copy(device_list); + device_list = g_list_append(device_list, GINT_TO_POINTER(omemo_ctx.device_id)); + g_hash_table_insert(omemo_ctx.device_list, strdup(jid), device_list); + omemo_devicelist_publish(device_list); + } + + GList *device_id; + for (device_id = device_list; device_id != NULL; device_id = device_id->next) { + omemo_bundle_request(jid, GPOINTER_TO_INT(device_id->data), omemo_start_device_session_handle_bundle, free, strdup(jid)); + } + + return TRUE; +} + +static gboolean +_handle_device_list_start_session(const char *const jid, GList *device_list) +{ + omemo_start_session(jid); + + return FALSE; +} + +void +omemo_key_free(omemo_key_t *key) +{ + if (key == NULL) { + return; + } + + free(key->data); + free(key); +} + +char* +omemo_fingerprint_autocomplete(const char *const search_str, gboolean previous) +{ + return autocomplete_complete(omemo_ctx.fingerprint_ac, search_str, FALSE, previous); +} + +void +omemo_fingerprint_autocomplete_reset(void) +{ + autocomplete_reset(omemo_ctx.fingerprint_ac); +} + +static void +_load_identity(void) +{ + log_info("Loading OMEMO identity"); + + /* Device ID */ + omemo_ctx.device_id = g_key_file_get_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_DEVICE_ID, NULL); + log_info("OMEMO: device id: %d", omemo_ctx.device_id); + + /* Registration ID */ + omemo_ctx.registration_id = g_key_file_get_uint64(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_REGISTRATION_ID, NULL); + + /* Identity key */ + char *identity_key_public_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC, NULL); + size_t identity_key_public_len; + unsigned char *identity_key_public = g_base64_decode(identity_key_public_b64, &identity_key_public_len); + g_free(identity_key_public_b64); + omemo_ctx.identity_key_store.public = signal_buffer_create(identity_key_public, identity_key_public_len); + + char *identity_key_private_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_IDENTITY, OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE, NULL); + size_t identity_key_private_len; + unsigned char *identity_key_private = g_base64_decode(identity_key_private_b64, &identity_key_private_len); + g_free(identity_key_private_b64); + omemo_ctx.identity_key_store.private = signal_buffer_create(identity_key_private, identity_key_private_len); + + ec_public_key *public_key; + curve_decode_point(&public_key, identity_key_public, identity_key_public_len, omemo_ctx.signal); + ec_private_key *private_key; + curve_decode_private_point(&private_key, identity_key_private, identity_key_private_len, omemo_ctx.signal); + ratchet_identity_key_pair_create(&omemo_ctx.identity_key_pair, public_key, private_key); + + g_free(identity_key_public); + g_free(identity_key_private); + + char **keys = NULL; + int i; + /* Pre keys */ + i = 0; + keys = g_key_file_get_keys(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_PREKEYS, NULL, NULL); + if (keys) { + for (i = 0; keys[i] != NULL; i++) { + char *pre_key_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_PREKEYS, keys[i], NULL); + size_t pre_key_len; + unsigned char *pre_key = g_base64_decode(pre_key_b64, &pre_key_len); + g_free(pre_key_b64); + signal_buffer *buffer = signal_buffer_create(pre_key, pre_key_len); + g_free(pre_key); + g_hash_table_insert(omemo_ctx.pre_key_store, GINT_TO_POINTER(strtoul(keys[i], NULL, 10)), buffer); + } + + g_strfreev(keys); + } + + /* Ensure we have at least 100 pre keys */ + if (i < 100) { + _generate_pre_keys(100 - i); + } + + /* Signed pre keys */ + i = 0; + keys = g_key_file_get_keys(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, NULL, NULL); + if (keys) { + for (i = 0; keys[i] != NULL; i++) { + char *signed_pre_key_b64 = g_key_file_get_string(omemo_ctx.identity_keyfile, OMEMO_STORE_GROUP_SIGNED_PREKEYS, keys[i], NULL); + size_t signed_pre_key_len; + unsigned char *signed_pre_key = g_base64_decode(signed_pre_key_b64, &signed_pre_key_len); + g_free(signed_pre_key_b64); + signal_buffer *buffer = signal_buffer_create(signed_pre_key, signed_pre_key_len); + g_free(signed_pre_key); + g_hash_table_insert(omemo_ctx.signed_pre_key_store, GINT_TO_POINTER(strtoul(keys[i], NULL, 10)), buffer); + omemo_ctx.signed_pre_key_id = strtoul(keys[i], NULL, 10); + } + g_strfreev(keys); + } + + if (i == 0) { + _generate_signed_pre_key(); + } + + loaded = TRUE; + + omemo_identity_keyfile_save(); + omemo_start_sessions(); +} + +static void +_load_trust(void) +{ + char **keys = NULL; + char **groups = g_key_file_get_groups(omemo_ctx.trust_keyfile, NULL); + 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; + for (j = 0; keys[j] != NULL; j++) { + char *key_b64 = g_key_file_get_string(omemo_ctx.trust_keyfile, groups[i], keys[j], NULL); + size_t key_len; + unsigned char *key = g_base64_decode(key_b64, &key_len); + g_free(key_b64); + signal_buffer *buffer = signal_buffer_create(key, key_len); + g_free(key); + uint32_t device_id = strtoul(keys[j], NULL, 10); + g_hash_table_insert(trusted, GINT_TO_POINTER(device_id), buffer); + } + g_strfreev(keys); + } + g_strfreev(groups); + } +} + +static void +_load_sessions(void) +{ + int i; + char **groups = g_key_file_get_groups(omemo_ctx.sessions_keyfile, NULL); + if (groups) { + for (i = 0; groups[i] != NULL; i++) { + int j; + GHashTable *device_store = NULL; + + 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, strdup(groups[i]), device_store); + } + + char **keys = g_key_file_get_keys(omemo_ctx.sessions_keyfile, groups[i], NULL, NULL); + for (j = 0; keys[j] != NULL; j++) { + uint32_t id = strtoul(keys[j], NULL, 10); + char *record_b64 = g_key_file_get_string(omemo_ctx.sessions_keyfile, groups[i], keys[j], NULL); + size_t record_len; + unsigned char *record = g_base64_decode(record_b64, &record_len); + g_free(record_b64); + signal_buffer *buffer = signal_buffer_create(record, record_len); + g_free(record); + g_hash_table_insert(device_store, GINT_TO_POINTER(id), buffer); + } + g_strfreev(keys); + } + g_strfreev(groups); + } +} + +static void +_cache_device_identity(const char *const jid, uint32_t device_id, ec_public_key *identity) +{ + GHashTable *known_identities = g_hash_table_lookup(omemo_ctx.known_devices, jid); + if (!known_identities) { + known_identities = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); + g_hash_table_insert(omemo_ctx.known_devices, strdup(jid), known_identities); + } + + char *fingerprint = _omemo_fingerprint(identity, FALSE); + log_info("OMEMO: cache identity for %s:%d: %s", jid, device_id, fingerprint); + g_hash_table_insert(known_identities, strdup(fingerprint), GINT_TO_POINTER(device_id)); + + char *formatted_fingerprint = omemo_format_fingerprint(fingerprint); + autocomplete_add(omemo_ctx.fingerprint_ac, formatted_fingerprint); + free(formatted_fingerprint); + free(fingerprint); +} + +static void +_g_hash_table_free(GHashTable *hash_table) +{ + g_hash_table_remove_all(hash_table); + g_hash_table_unref(hash_table); +} + +static void +_generate_pre_keys(int count) +{ + unsigned int start; + gcry_randomize(&start, sizeof(unsigned int), GCRY_VERY_STRONG_RANDOM); + signal_protocol_key_helper_pre_key_list_node *pre_keys_head; + signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, start, count, omemo_ctx.signal); + + signal_protocol_key_helper_pre_key_list_node *p; + for (p = 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_protocol_pre_key_store_key(omemo_ctx.store, prekey); + } + signal_protocol_key_helper_key_list_free(pre_keys_head); +} + +static void +_generate_signed_pre_key(void) +{ + session_signed_pre_key *signed_pre_key; + struct timeval tv; + gettimeofday(&tv, NULL); + unsigned long long timestamp = (unsigned long long)(tv.tv_sec) * 1000 + (unsigned long long)(tv.tv_usec) / 1000; + + omemo_ctx.signed_pre_key_id = 1; + signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, omemo_ctx.identity_key_pair, omemo_ctx.signed_pre_key_id, timestamp, omemo_ctx.signal); + signal_protocol_signed_pre_key_store_key(omemo_ctx.store, signed_pre_key); + SIGNAL_UNREF(signed_pre_key); +} diff --git a/src/omemo/omemo.h b/src/omemo/omemo.h new file mode 100644 index 00000000..166a5292 --- /dev/null +++ b/src/omemo/omemo.h @@ -0,0 +1,55 @@ +#include <glib.h> + +#include "ui/ui.h" +#include "config/account.h" + +#define OMEMO_ERR_UNSUPPORTED_CRYPTO -10000 +#define OMEMO_ERR_GCRYPT -20000 + +typedef struct omemo_context_t omemo_context; + +typedef struct omemo_key { + unsigned char *data; + size_t length; + gboolean prekey; + uint32_t device_id; + uint32_t id; +} omemo_key_t; + +void omemo_init(void); +void omemo_on_connect(ProfAccount *account); +void omemo_on_disconnect(void); +void omemo_generate_crypto_materials(ProfAccount *account); +void omemo_key_free(omemo_key_t *key); +void omemo_publish_crypto_materials(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 **prekeys, GList **ids, GList **lengths); +void omemo_set_device_list(const char *const jid, GList * device_list); +GKeyFile *omemo_identity_keyfile(void); +void omemo_identity_keyfile_save(void); +GKeyFile *omemo_trust_keyfile(void); +void omemo_trust_keyfile_save(void); +GKeyFile *omemo_sessions_keyfile(void); +void omemo_sessions_keyfile_save(void); +char *omemo_format_fingerprint(const char *const fingerprint); +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); + +void omemo_start_sessions(void); +void omemo_start_session(const char *const barejid); +void omemo_start_muc_sessions(const char *const roomjid); +void omemo_start_device_session(const char *const jid, uint32_t device_id, GList *prekeys, uint32_t signed_prekey_id, const unsigned char *const signed_prekey, size_t signed_prekey_len, const unsigned char *const signature, size_t signature_len, const unsigned char *const identity_key, size_t identity_key_len); + +gboolean omemo_loaded(void); +char * omemo_on_message_send(ProfWin *win, const char *const message, gboolean request_receipt, gboolean muc); +char * omemo_on_message_recv(const char *const from, uint32_t sid, const unsigned char *const iv, size_t iv_len, GList *keys, const unsigned char *const payload, size_t payload_len, gboolean muc); diff --git a/src/omemo/store.c b/src/omemo/store.c new file mode 100644 index 00000000..76b7449c --- /dev/null +++ b/src/omemo/store.c @@ -0,0 +1,382 @@ +#include <glib.h> +#include <signal/signal_protocol.h> + +#include "config.h" +#include "omemo/omemo.h" +#include "omemo/store.h" + +GHashTable * +session_store_new(void) +{ + return g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); +} + +GHashTable * +pre_key_store_new(void) +{ + return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); +} + +GHashTable * +signed_pre_key_store_new(void) +{ + return g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); +} + +void +identity_key_store_new(identity_key_store_t *identity_key_store) +{ + identity_key_store->trusted = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)signal_buffer_free); + identity_key_store->private = NULL; + identity_key_store->public = NULL; +} + +#ifdef HAVE_LIBSIGNAL_LT_2_3_2 +int +load_session(signal_buffer **record, const signal_protocol_address *address, + void *user_data) +#else +int +load_session(signal_buffer **record, signal_buffer **user_record, + const signal_protocol_address *address, void *user_data) +#endif +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + + device_store = g_hash_table_lookup(session_store, address->name); + if (!device_store) { + *record = NULL; + return 0; + } + + signal_buffer *original = g_hash_table_lookup(device_store, GINT_TO_POINTER(address->device_id)); + if (!original) { + *record = NULL; + return 0; + } + *record = signal_buffer_copy(original); + return 1; +} + +int +get_sub_device_sessions(signal_int_list **sessions, const char *name, + size_t name_len, void *user_data) +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + GHashTableIter iter; + gpointer key, value; + + device_store = g_hash_table_lookup(session_store, name); + if (!device_store) { + return SG_SUCCESS; + } + + *sessions = signal_int_list_alloc(); + g_hash_table_iter_init(&iter, device_store); + while (g_hash_table_iter_next(&iter, &key, &value)) { + signal_int_list_push_back(*sessions, GPOINTER_TO_INT(key)); + } + + + return SG_SUCCESS; +} + +#ifdef HAVE_LIBSIGNAL_LT_2_3_2 +int +store_session(const signal_protocol_address *address, uint8_t *record, + size_t record_len, void *user_data) +#else +int +store_session(const signal_protocol_address *address, + uint8_t *record, size_t record_len, + uint8_t *user_record, size_t user_record_len, + void *user_data) +#endif +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + + device_store = g_hash_table_lookup(session_store, (void *)address->name); + 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(session_store, strdup(address->name), device_store); + } + + signal_buffer *buffer = signal_buffer_create(record, record_len); + g_hash_table_insert(device_store, GINT_TO_POINTER(address->device_id), buffer); + + + char *record_b64 = g_base64_encode(record, record_len); + char *device_id = g_strdup_printf("%d", address->device_id); + g_key_file_set_string(omemo_sessions_keyfile(), address->name, device_id, record_b64); + free(device_id); + g_free(record_b64); + + omemo_sessions_keyfile_save(); + + return SG_SUCCESS; +} + +int +contains_session(const signal_protocol_address *address, void *user_data) +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + + device_store = g_hash_table_lookup(session_store, address->name); + if (!device_store) { + return 0; + } + + if (!g_hash_table_lookup(device_store, GINT_TO_POINTER(address->device_id))) { + return 0; + } + + return 1; +} + +int +delete_session(const signal_protocol_address *address, void *user_data) +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + + device_store = g_hash_table_lookup(session_store, address->name); + if (!device_store) { + return SG_SUCCESS; + } + + return g_hash_table_remove(device_store, GINT_TO_POINTER(address->device_id)); +} + +int +delete_all_sessions(const char *name, size_t name_len, void *user_data) +{ + GHashTable *session_store = (GHashTable *)user_data; + GHashTable *device_store = NULL; + + device_store = g_hash_table_lookup(session_store, name); + if (!device_store) { + return SG_SUCCESS; + } + + guint len = g_hash_table_size(device_store); + g_hash_table_remove_all(device_store); + return len; +} + +int +load_pre_key(signal_buffer **record, uint32_t pre_key_id, void *user_data) +{ + signal_buffer *original; + GHashTable *pre_key_store = (GHashTable *)user_data; + + original = g_hash_table_lookup(pre_key_store, GINT_TO_POINTER(pre_key_id)); + if (original == NULL) { + return SG_ERR_INVALID_KEY_ID; + } + + *record = signal_buffer_copy(original); + return SG_SUCCESS; +} + +int +store_pre_key(uint32_t pre_key_id, uint8_t *record, size_t record_len, + void *user_data) +{ + GHashTable *pre_key_store = (GHashTable *)user_data; + + signal_buffer *buffer = signal_buffer_create(record, record_len); + g_hash_table_insert(pre_key_store, GINT_TO_POINTER(pre_key_id), buffer); + + /* Long term storage */ + char *pre_key_id_str = g_strdup_printf("%d", pre_key_id); + char *record_b64 = g_base64_encode(record, record_len); + g_key_file_set_string(omemo_identity_keyfile(), OMEMO_STORE_GROUP_PREKEYS, pre_key_id_str, record_b64); + g_free(pre_key_id_str); + g_free(record_b64); + + omemo_identity_keyfile_save(); + + return SG_SUCCESS; +} + +int +contains_pre_key(uint32_t pre_key_id, void *user_data) +{ + GHashTable *pre_key_store = (GHashTable *)user_data; + + return g_hash_table_lookup(pre_key_store, GINT_TO_POINTER(pre_key_id)) != NULL; +} + +int +remove_pre_key(uint32_t pre_key_id, void *user_data) +{ + GHashTable *pre_key_store = (GHashTable *)user_data; + + int ret = g_hash_table_remove(pre_key_store, GINT_TO_POINTER(pre_key_id)); + + /* Long term storage */ + char *pre_key_id_str = g_strdup_printf("%d", pre_key_id); + g_key_file_remove_key(omemo_identity_keyfile(), OMEMO_STORE_GROUP_PREKEYS, pre_key_id_str, NULL); + g_free(pre_key_id_str); + + omemo_identity_keyfile_save(); + + if (ret > 0) { + return SG_SUCCESS; + } else { + return SG_ERR_INVALID_KEY_ID; + } +} + +int +load_signed_pre_key(signal_buffer **record, uint32_t signed_pre_key_id, + void *user_data) +{ + signal_buffer *original; + GHashTable *signed_pre_key_store = (GHashTable *)user_data; + + original = g_hash_table_lookup(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id)); + if (!original) { + return SG_ERR_INVALID_KEY_ID; + } + + *record = signal_buffer_copy(original); + return SG_SUCCESS; +} + +int +store_signed_pre_key(uint32_t signed_pre_key_id, uint8_t *record, + size_t record_len, void *user_data) +{ + GHashTable *signed_pre_key_store = (GHashTable *)user_data; + + signal_buffer *buffer = signal_buffer_create(record, record_len); + g_hash_table_insert(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id), buffer); + + /* Long term storage */ + char *signed_pre_key_id_str = g_strdup_printf("%d", signed_pre_key_id); + char *record_b64 = g_base64_encode(record, record_len); + g_key_file_set_string(omemo_identity_keyfile(), OMEMO_STORE_GROUP_SIGNED_PREKEYS, signed_pre_key_id_str, record_b64); + g_free(signed_pre_key_id_str); + g_free(record_b64); + + omemo_identity_keyfile_save(); + + return SG_SUCCESS; +} + +int +contains_signed_pre_key(uint32_t signed_pre_key_id, void *user_data) +{ + GHashTable *signed_pre_key_store = (GHashTable *)user_data; + + return g_hash_table_lookup(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id)) != NULL; +} + +int +remove_signed_pre_key(uint32_t signed_pre_key_id, void *user_data) +{ + GHashTable *signed_pre_key_store = (GHashTable *)user_data; + + int ret = g_hash_table_remove(signed_pre_key_store, GINT_TO_POINTER(signed_pre_key_id)); + + /* Long term storage */ + char *signed_pre_key_id_str = g_strdup_printf("%d", signed_pre_key_id); + g_key_file_remove_key(omemo_identity_keyfile(), OMEMO_STORE_GROUP_PREKEYS, signed_pre_key_id_str, NULL); + g_free(signed_pre_key_id_str); + + omemo_identity_keyfile_save(); + + return ret; +} + +int +get_identity_key_pair(signal_buffer **public_data, signal_buffer **private_data, + void *user_data) +{ + identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data; + + *public_data = signal_buffer_copy(identity_key_store->public); + *private_data = signal_buffer_copy(identity_key_store->private); + + return SG_SUCCESS; +} + +int +get_local_registration_id(void *user_data, uint32_t *registration_id) +{ + identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data; + + *registration_id = identity_key_store->registration_id; + + return SG_SUCCESS; +} + +int +save_identity(const signal_protocol_address *address, uint8_t *key_data, + size_t key_len, void *user_data) +{ + identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data; + + signal_buffer *buffer = signal_buffer_create(key_data, key_len); + + GHashTable *trusted = g_hash_table_lookup(identity_key_store->trusted, strdup(address->name)); + if (!trusted) { + trusted = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify)signal_buffer_free); + g_hash_table_insert(identity_key_store->trusted, strdup(address->name), trusted); + } + g_hash_table_insert(trusted, GINT_TO_POINTER(address->device_id), buffer); + + /* Long term storage */ + char *key_b64 = g_base64_encode(key_data, key_len); + char *device_id = g_strdup_printf("%d", address->device_id); + g_key_file_set_string(omemo_trust_keyfile(), address->name, strdup(device_id), key_b64); + g_free(device_id); + g_free(key_b64); + + omemo_trust_keyfile_save(); + + return SG_SUCCESS; +} + +int +is_trusted_identity(const signal_protocol_address *address, uint8_t *key_data, + size_t key_len, void *user_data) +{ + int ret; + identity_key_store_t *identity_key_store = (identity_key_store_t *)user_data; + + GHashTable *trusted = g_hash_table_lookup(identity_key_store->trusted, address->name); + if (!trusted) { + return 0; + } + + signal_buffer *buffer = signal_buffer_create(key_data, key_len); + signal_buffer *original = g_hash_table_lookup(trusted, GINT_TO_POINTER(address->device_id)); + + ret = original != NULL && signal_buffer_compare(buffer, original) == 0; + + signal_buffer_free(buffer); + + return ret; +} + +int +store_sender_key(const signal_protocol_sender_key_name *sender_key_name, + uint8_t *record, size_t record_len, uint8_t *user_record, + size_t user_record_len, void *user_data) +{ + return SG_SUCCESS; +} + +int +load_sender_key(signal_buffer **record, signal_buffer **user_record, + const signal_protocol_sender_key_name *sender_key_name, + void *user_data) +{ + return SG_SUCCESS; +} diff --git a/src/omemo/store.h b/src/omemo/store.h new file mode 100644 index 00000000..d4096c90 --- /dev/null +++ b/src/omemo/store.h @@ -0,0 +1,250 @@ +#include <signal/signal_protocol.h> + +#include "config.h" + +#define OMEMO_STORE_GROUP_IDENTITY "identity" +#define OMEMO_STORE_GROUP_PREKEYS "prekeys" +#define OMEMO_STORE_GROUP_SIGNED_PREKEYS "signed_prekeys" +#define OMEMO_STORE_KEY_DEVICE_ID "device_id" +#define OMEMO_STORE_KEY_REGISTRATION_ID "registration_id" +#define OMEMO_STORE_KEY_IDENTITY_KEY_PUBLIC "identity_key_public" +#define OMEMO_STORE_KEY_IDENTITY_KEY_PRIVATE "identity_key_private" + +typedef struct { + signal_buffer *public; + signal_buffer *private; + uint32_t registration_id; + GHashTable *trusted; +} identity_key_store_t; + +GHashTable * session_store_new(void); +GHashTable * pre_key_store_new(void); +GHashTable * signed_pre_key_store_new(void); +void identity_key_store_new(identity_key_store_t *identity_key_store); + +/** + * Returns a copy of the serialized session record corresponding to the + * provided recipient ID + device ID tuple. + * + * @param record pointer to a freshly allocated buffer containing the + * serialized session record. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @param address the address of the remote client + * @return 1 if the session was loaded, 0 if the session was not found, negative on failure + */ +#ifdef HAVE_LIBSIGNAL_LT_2_3_2 +int load_session(signal_buffer **record, const signal_protocol_address *address, void *user_data); +#else +int load_session(signal_buffer **record, signal_buffer **user_record, const signal_protocol_address *address, void *user_data); +#endif + +/** + * Returns all known devices with active sessions for a recipient + * + * @param pointer to an array that will be allocated and populated with the result + * @param name the name of the remote client + * @param name_len the length of the name + * @return size of the sessions array, or negative on failure + */ +int get_sub_device_sessions(signal_int_list **sessions, const char *name, size_t name_len, void *user_data); + +/** + * Commit to storage the session record for a given + * recipient ID + device ID tuple. + * + * @param address the address of the remote client + * @param record pointer to a buffer containing the serialized session + * record for the remote client + * @param record_len length of the serialized session record + * @return 0 on success, negative on failure + */ +#ifdef HAVE_LIBSIGNAL_LT_2_3_2 +int store_session(const signal_protocol_address *address, uint8_t *record, size_t record_len, void *user_data); +#else +int store_session(const signal_protocol_address *address, uint8_t *record, size_t record_len, uint8_t *user_record, size_t user_record_len, void *user_data); +#endif + +/** + * Determine whether there is a committed session record for a + * recipient ID + device ID tuple. + * + * @param address the address of the remote client + * @return 1 if a session record exists, 0 otherwise. + */ +int contains_session(const signal_protocol_address *address, void *user_data); + +/** + * Remove a session record for a recipient ID + device ID tuple. + * + * @param address the address of the remote client + * @return 1 if a session was deleted, 0 if a session was not deleted, negative on error + */ +int delete_session(const signal_protocol_address *address, void *user_data); + +/** + * Remove the session records corresponding to all devices of a recipient ID. + * + * @param name the name of the remote client + * @param name_len the length of the name + * @return the number of deleted sessions on success, negative on failure + */ +int delete_all_sessions(const char *name, size_t name_len, void *user_data); + +/** + * Load a local serialized PreKey record. + * + * @param record pointer to a newly allocated buffer containing the record, + * if found. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @param pre_key_id the ID of the local serialized PreKey record + * @retval SG_SUCCESS if the key was found + * @retval SG_ERR_INVALID_KEY_ID if the key could not be found + */ +int load_pre_key(signal_buffer **record, uint32_t pre_key_id, void *user_data); + +/** + * Store a local serialized PreKey record. + * + * @param pre_key_id the ID of the PreKey record to store. + * @param record pointer to a buffer containing the serialized record + * @param record_len length of the serialized record + * @return 0 on success, negative on failure + */ +int store_pre_key(uint32_t pre_key_id, uint8_t *record, size_t record_len, void *user_data); + +/** + * Determine whether there is a committed PreKey record matching the + * provided ID. + * + * @param pre_key_id A PreKey record ID. + * @return 1 if the store has a record for the PreKey ID, 0 otherwise + */ +int contains_pre_key(uint32_t pre_key_id, void *user_data); + +/** + * Delete a PreKey record from local storage. + * + * @param pre_key_id The ID of the PreKey record to remove. + * @return 0 on success, negative on failure + */ +int remove_pre_key(uint32_t pre_key_id, void *user_data); + +/** + * Load a local serialized signed PreKey record. + * + * @param record pointer to a newly allocated buffer containing the record, + * if found. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @param signed_pre_key_id the ID of the local signed PreKey record + * @retval SG_SUCCESS if the key was found + * @retval SG_ERR_INVALID_KEY_ID if the key could not be found + */ +int load_signed_pre_key(signal_buffer **record, uint32_t signed_pre_key_id, void *user_data); + +/** + * Store a local serialized signed PreKey record. + * + * @param signed_pre_key_id the ID of the signed PreKey record to store + * @param record pointer to a buffer containing the serialized record + * @param record_len length of the serialized record + * @return 0 on success, negative on failure + */ +int store_signed_pre_key(uint32_t signed_pre_key_id, uint8_t *record, size_t record_len, void *user_data); + +/** + * Determine whether there is a committed signed PreKey record matching + * the provided ID. + * + * @param signed_pre_key_id A signed PreKey record ID. + * @return 1 if the store has a record for the signed PreKey ID, 0 otherwise + */ +int contains_signed_pre_key(uint32_t signed_pre_key_id, void *user_data); + +/** + * Delete a SignedPreKeyRecord from local storage. + * + * @param signed_pre_key_id The ID of the signed PreKey record to remove. + * @return 0 on success, negative on failure + */ +int remove_signed_pre_key(uint32_t signed_pre_key_id, void *user_data); + +/** + * Get the local client's identity key pair. + * + * @param public_data pointer to a newly allocated buffer containing the + * public key, if found. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @param private_data pointer to a newly allocated buffer containing the + * private key, if found. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @return 0 on success, negative on failure + */ +int get_identity_key_pair(signal_buffer **public_data, signal_buffer **private_data, void *user_data); + +/** + * Return the local client's registration ID. + * + * Clients should maintain a registration ID, a random number + * between 1 and 16380 that's generated once at install time. + * + * @param registration_id pointer to be set to the local client's + * registration ID, if it was successfully retrieved. + * @return 0 on success, negative on failure + */ +int get_local_registration_id(void *user_data, uint32_t *registration_id); + +/** + * Save a remote client's identity key + * <p> + * Store a remote client's identity key as trusted. + * The value of key_data may be null. In this case remove the key data + * from the identity store, but retain any metadata that may be kept + * alongside it. + * + * @param address the address of the remote client + * @param key_data Pointer to the remote client's identity key, may be null + * @param key_len Length of the remote client's identity key + * @return 0 on success, negative on failure + */ +int save_identity(const signal_protocol_address *address, uint8_t *key_data, size_t key_len, void *user_data); + +/** + * Verify a remote client's identity key. + * + * Determine whether a remote client's identity is trusted. Convention is + * that the TextSecure protocol is 'trust on first use.' This means that + * an identity key is considered 'trusted' if there is no entry for the recipient + * in the local store, or if it matches the saved key for a recipient in the local + * store. Only if it mismatches an entry in the local store is it considered + * 'untrusted.' + * + * @param address the address of the remote client + * @param identityKey The identity key to verify. + * @param key_data Pointer to the identity key to verify + * @param key_len Length of the identity key to verify + * @return 1 if trusted, 0 if untrusted, negative on failure + */ +int is_trusted_identity(const signal_protocol_address *address, uint8_t *key_data, size_t key_len, void *user_data); + +/** + * Store a serialized sender key record for a given + * (groupId + senderId + deviceId) tuple. + * + * @param sender_key_name the (groupId + senderId + deviceId) tuple + * @param record pointer to a buffer containing the serialized record + * @param record_len length of the serialized record + * @return 0 on success, negative on failure + */ +int store_sender_key(const signal_protocol_sender_key_name *sender_key_name, uint8_t *record, size_t record_len, uint8_t *user_record, size_t user_record_len, void *user_data); + +/** + * Returns a copy of the sender key record corresponding to the + * (groupId + senderId + deviceId) tuple. + * + * @param record pointer to a newly allocated buffer containing the record, + * if found. Unset if no record was found. + * The Signal Protocol library is responsible for freeing this buffer. + * @param sender_key_name the (groupId + senderId + deviceId) tuple + * @return 1 if the record was loaded, 0 if the record was not found, negative on failure + */ +int load_sender_key(signal_buffer **record, signal_buffer **user_record, const signal_protocol_sender_key_name *sender_key_name, void *user_data); diff --git a/src/plugins/api.c b/src/plugins/api.c index 4d8434e1..fc47f193 100644 --- a/src/plugins/api.c +++ b/src/plugins/api.c @@ -473,7 +473,7 @@ api_settings_int_set(const char *const group, const char *const key, int value) void api_incoming_message(const char *const barejid, const char *const resource, const char *const message) { - sv_ev_incoming_message((char*)barejid, (char*)resource, (char*)message, NULL, NULL); + sv_ev_incoming_message((char*)barejid, (char*)resource, (char*)message, NULL, NULL, FALSE); // TODO handle all states sv_ev_activity((char*)barejid, (char*)resource, FALSE); diff --git a/src/profanity.c b/src/profanity.c index 1d4a2c35..f21f02c0 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -80,6 +80,10 @@ #include "pgp/gpg.h" #endif +#ifdef HAVE_OMEMO +#include "omemo/omemo.h" +#endif + static void _init(char *log_level); static void _shutdown(void); static void _connect_default(const char * const account); @@ -197,6 +201,9 @@ _init(char *log_level) #ifdef HAVE_LIBGPGME p_gpg_init(); #endif +#ifdef HAVE_OMEMO + omemo_init(); +#endif atexit(_shutdown); plugins_init(); #ifdef HAVE_GTK diff --git a/src/ui/chatwin.c b/src/ui/chatwin.c index 98431a60..5064b194 100644 --- a/src/ui/chatwin.c +++ b/src/ui/chatwin.c @@ -305,6 +305,8 @@ chatwin_outgoing_msg(ProfChatWin *chatwin, const char *const message, char *id, enc_char = prefs_get_otr_char(); } else if (enc_mode == PROF_MSG_PGP) { enc_char = prefs_get_pgp_char(); + } else if (enc_mode == PROF_MSG_OMEMO) { + enc_char = prefs_get_omemo_char(); } if (request_receipt && id) { @@ -322,6 +324,8 @@ chatwin_outgoing_carbon(ProfChatWin *chatwin, const char *const message, prof_en char enc_char = '-'; if (enc_mode == PROF_MSG_PGP) { enc_char = prefs_get_pgp_char(); + } else if (enc_mode == PROF_MSG_OMEMO) { + enc_char = prefs_get_omemo_char(); } ProfWin *window = (ProfWin*)chatwin; diff --git a/src/ui/console.c b/src/ui/console.c index e5c12158..260658c8 100644 --- a/src/ui/console.c +++ b/src/ui/console.c @@ -1999,6 +1999,28 @@ cons_show_pgp_prefs(void) } void +cons_show_omemo_prefs(void) +{ + cons_show("OMEMO preferences:"); + cons_show(""); + + char *log_value = prefs_get_string(PREF_OMEMO_LOG); + if (strcmp(log_value, "on") == 0) { + cons_show("OMEMO logging (/omemo log) : ON"); + } else if (strcmp(log_value, "off") == 0) { + cons_show("OMEMO logging (/omemo log) : OFF"); + } else { + cons_show("OMEMO logging (/omemo log) : Redacted"); + } + prefs_free_string(log_value); + + char ch = prefs_get_omemo_char(); + cons_show("OMEMO char (/omemo char) : %c", ch); + + cons_alert(); +} + +void cons_show_themes(GSList *themes) { cons_show(""); @@ -2072,6 +2094,8 @@ cons_prefs(void) cons_show(""); cons_show_pgp_prefs(); cons_show(""); + cons_show_omemo_prefs(); + cons_show(""); cons_alert(); } diff --git a/src/ui/mucwin.c b/src/ui/mucwin.c index 0f9f4f2b..0122950a 100644 --- a/src/ui/mucwin.c +++ b/src/ui/mucwin.c @@ -478,29 +478,60 @@ _mucwin_print_triggers(ProfWin *window, const char *const message, GList *trigge } void -mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, GSList *mentions, GList *triggers) +mucwin_outgoing_msg(ProfMucWin *mucwin, const char *const message, const char *const id, prof_enc_t enc_mode) { assert(mucwin != NULL); + g_hash_table_insert(mucwin->sent_messages, strdup(id), NULL); + ProfWin *window = (ProfWin*)mucwin; char *mynick = muc_nick(mucwin->roomjid); char ch = '-'; if (mucwin->message_char) { ch = mucwin->message_char[0]; + } else if (enc_mode == PROF_MSG_OTR) { + ch = prefs_get_otr_char(); + } else if (enc_mode == PROF_MSG_PGP) { + ch = prefs_get_pgp_char(); + } else if (enc_mode == PROF_MSG_OMEMO) { + ch = prefs_get_omemo_char(); } - if (g_strcmp0(nick, mynick) != 0) { - if (g_slist_length(mentions) > 0) { - _mucwin_print_mention(window, message, nick, mynick, mentions, &ch); - } else if (triggers) { - win_print_them(window, THEME_ROOMTRIGGER, ch, nick); - _mucwin_print_triggers(window, message, triggers); - } else { - win_println_them_message(window, ch, nick, "%s", message); - } + win_println_me_message(window, ch, mynick, "%s", message); +} + +void +mucwin_incoming_msg(ProfMucWin *mucwin, const char *const nick, const char *const message, const char *const id, GSList *mentions, GList *triggers, prof_enc_t enc_mode) +{ + assert(mucwin != NULL); + + if (g_hash_table_remove(mucwin->sent_messages, id)) { + /* Ignore reflection messages */ + return; + } + + ProfWin *window = (ProfWin*)mucwin; + char *mynick = muc_nick(mucwin->roomjid); + + char ch = '-'; + if (mucwin->message_char) { + ch = mucwin->message_char[0]; + } else if (enc_mode == PROF_MSG_OTR) { + ch = prefs_get_otr_char(); + } else if (enc_mode == PROF_MSG_PGP) { + ch = prefs_get_pgp_char(); + } else if (enc_mode == PROF_MSG_OMEMO) { + ch = prefs_get_omemo_char(); + } + + if (g_slist_length(mentions) > 0) { + _mucwin_print_mention(window, message, nick, mynick, mentions, &ch); + } else if (triggers) { + win_print_them(window, THEME_ROOMTRIGGER, ch, nick); + _mucwin_print_triggers(window, message, triggers); } else { - win_println_me_message(window, ch, mynick, "%s", message); + win_println_them_message(window, ch, nick, "%s", message); } } diff --git a/src/ui/titlebar.c b/src/ui/titlebar.c index f519fdd2..e1758d81 100644 --- a/src/ui/titlebar.c +++ b/src/ui/titlebar.c @@ -321,6 +321,21 @@ _show_muc_privacy(ProfMucWin *mucwin) int bracket_attrs = theme_attrs(THEME_TITLE_BRACKET); int encrypted_attrs = theme_attrs(THEME_TITLE_ENCRYPTED); + if (mucwin->is_omemo) { + wprintw(win, " "); + wattron(win, bracket_attrs); + wprintw(win, "["); + wattroff(win, bracket_attrs); + wattron(win, encrypted_attrs); + wprintw(win, "OMEMO"); + wattroff(win, encrypted_attrs); + wattron(win, bracket_attrs); + wprintw(win, "]"); + wattroff(win, bracket_attrs); + + return; + } + if (mucwin->enctext) { wprintw(win, " "); wattron(win, bracket_attrs); @@ -421,6 +436,21 @@ _show_privacy(ProfChatWin *chatwin) return; } + if (chatwin->is_omemo) { + wprintw(win, " "); + wattron(win, bracket_attrs); + wprintw(win, "["); + wattroff(win, bracket_attrs); + wattron(win, encrypted_attrs); + wprintw(win, "OMEMO"); + wattroff(win, encrypted_attrs); + wattron(win, bracket_attrs); + wprintw(win, "]"); + wattroff(win, bracket_attrs); + + return; + } + if (prefs_get_boolean(PREF_ENC_WARN)) { wprintw(win, " "); wattron(win, bracket_attrs); diff --git a/src/ui/ui.h b/src/ui/ui.h index ad5a1216..b94fe475 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -56,7 +56,8 @@ typedef enum { PROF_MSG_PLAIN, PROF_MSG_OTR, - PROF_MSG_PGP + PROF_MSG_PGP, + PROF_MSG_OMEMO } prof_enc_t; // core UI @@ -161,7 +162,8 @@ void mucwin_occupant_role_and_affiliation_change(ProfMucWin *mucwin, const char const char *const role, const char *const affiliation, const char *const actor, const char *const reason); void mucwin_roster(ProfMucWin *mucwin, GList *occupants, const char *const presence); void mucwin_history(ProfMucWin *mucwin, const char *const nick, GDateTime *timestamp, const char *const message); -void mucwin_message(ProfMucWin *mucwin, const char *const nick, const char *const message, GSList *mentions, GList *triggers); +void mucwin_outgoing_msg(ProfMucWin *mucwin, const char *const message, const char *const id, prof_enc_t enc_mode); +void mucwin_incoming_msg(ProfMucWin *mucwin, const char *const nick, const char *const message, const char *const id, GSList *mentions, GList *triggers, prof_enc_t enc_mode); void mucwin_subject(ProfMucWin *mucwin, const char *const nick, const char *const subject); void mucwin_requires_config(ProfMucWin *mucwin); void mucwin_info(ProfMucWin *mucwin); @@ -250,6 +252,7 @@ void cons_show_presence_prefs(void); void cons_show_connection_prefs(void); void cons_show_otr_prefs(void); void cons_show_pgp_prefs(void); +void cons_show_omemo_prefs(void); void cons_show_account(ProfAccount *account); void cons_debug(const char *const msg, ...); void cons_show_error(const char *const cmd, ...); diff --git a/src/ui/win_types.h b/src/ui/win_types.h index 92618a36..e1e64bf9 100644 --- a/src/ui/win_types.h +++ b/src/ui/win_types.h @@ -152,6 +152,7 @@ typedef struct prof_chat_win_t { gboolean otr_is_trusted; gboolean pgp_send; gboolean pgp_recv; + gboolean is_omemo; char *resource_override; gboolean history_shown; unsigned long memcheck; @@ -167,9 +168,11 @@ typedef struct prof_muc_win_t { gboolean unread_mentions; gboolean unread_triggers; gboolean showjid; + gboolean is_omemo; unsigned long memcheck; char *enctext; char *message_char; + GHashTable *sent_messages; } ProfMucWin; typedef struct prof_conf_win_t ProfConfWin; diff --git a/src/ui/window.c b/src/ui/window.c index cc2c2062..12b6c15b 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -143,6 +143,7 @@ win_create_chat(const char *const barejid) new_win->otr_is_trusted = FALSE; new_win->pgp_recv = FALSE; new_win->pgp_send = FALSE; + new_win->is_omemo = FALSE; new_win->history_shown = FALSE; new_win->unread = 0; new_win->state = chat_state_new(); @@ -196,6 +197,8 @@ win_create_muc(const char *const roomjid) } new_win->enctext = NULL; new_win->message_char = NULL; + new_win->is_omemo = FALSE; + new_win->sent_messages = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); new_win->memcheck = PROFMUCWIN_MEMCHECK; @@ -1057,6 +1060,8 @@ win_print_incoming(ProfWin *window, GDateTime *timestamp, enc_char = prefs_get_otr_char(); } else if (enc_mode == PROF_MSG_PGP) { enc_char = prefs_get_pgp_char(); + } else if (enc_mode == PROF_MSG_OMEMO) { + enc_char = prefs_get_omemo_char(); } _win_printf(window, enc_char, 0, timestamp, NO_ME, THEME_TEXT_THEM, from, "%s", message); break; diff --git a/src/ui/window_list.c b/src/ui/window_list.c index 5ce68d63..43230b57 100644 --- a/src/ui/window_list.c +++ b/src/ui/window_list.c @@ -561,6 +561,7 @@ wins_close_by_num(int i) ProfMucWin *mucwin = (ProfMucWin*)window; autocomplete_remove(wins_ac, mucwin->roomjid); autocomplete_remove(wins_close_ac, mucwin->roomjid); + g_hash_table_remove_all(mucwin->sent_messages); break; } case WIN_PRIVATE: diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 2adda46e..afcd8199 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -63,6 +63,7 @@ typedef struct prof_conn_t { char *domain; GHashTable *available_resources; GHashTable *features_by_jid; + GHashTable *requested_features; } ProfConnection; static ProfConnection conn; @@ -89,6 +90,7 @@ connection_init(void) conn.domain = NULL; conn.features_by_jid = NULL; conn.available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy); + conn.requested_features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); } void @@ -231,6 +233,10 @@ connection_clear_data(void) if (conn.available_resources) { g_hash_table_remove_all(conn.available_resources); } + + if (conn.requested_features) { + g_hash_table_remove_all(conn.requested_features); + } } #ifdef HAVE_LIBMESODE @@ -314,11 +320,20 @@ connection_jid_for_feature(const char *const feature) } void +connection_request_features(void) +{ + /* We don't record it as a requested feature to avoid triggering th + * sv_ev_connection_features_received too soon */ + iq_disco_info_request_onconnect(conn.domain); +} + +void connection_set_disco_items(GSList *items) { GSList *curr = items; while (curr) { DiscoItem *item = curr->data; + g_hash_table_insert(conn.requested_features, strdup(item->jid), NULL); g_hash_table_insert(conn.features_by_jid, strdup(item->jid), g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL)); @@ -357,6 +372,14 @@ connection_get_fulljid(void) } } +void +connection_features_received(const char *const jid) +{ + if (g_hash_table_remove(conn.requested_features, jid) && g_hash_table_size(conn.requested_features) == 0) { + sv_ev_connection_features_received(); + } +} + GHashTable* connection_get_features(const char *const jid) { diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index 170bc2bf..044cf368 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -53,6 +53,8 @@ void connection_set_disco_items(GSList *items); xmpp_conn_t* connection_get_conn(void); xmpp_ctx_t* connection_get_ctx(void); char *connection_get_domain(void); +void connection_request_features(void); +void connection_features_received(const char *const jid); GHashTable* connection_get_features(const char *const jid); void connection_clear_data(void); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index a77ef59b..d6e4c153 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -77,11 +77,11 @@ typedef struct p_room_info_data_t { gboolean display; } ProfRoomInfoData; -typedef struct p_id_handle_t { - ProfIdCallback func; - ProfIdFreeCallback free_func; +typedef struct p_iq_handle_t { + ProfIqCallback func; + ProfIqFreeCallback free_func; void *userdata; -} ProfIdHandler; +} ProfIqHandler; typedef struct privilege_set_t { char *item; @@ -205,7 +205,7 @@ _iq_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const us const char *id = xmpp_stanza_get_id(stanza); if (id) { - ProfIdHandler *handler = g_hash_table_lookup(id_handlers, id); + ProfIqHandler *handler = g_hash_table_lookup(id_handlers, id); if (handler) { int keep = handler->func(stanza, handler->userdata); if (!keep) { @@ -234,7 +234,7 @@ iq_handlers_init(void) GList *keys = g_hash_table_get_keys(id_handlers); GList *curr = keys; while (curr) { - ProfIdHandler *handler = g_hash_table_lookup(id_handlers, curr->data); + ProfIqHandler *handler = g_hash_table_lookup(id_handlers, curr->data); if (handler->free_func && handler->userdata) { handler->free_func(handler->userdata); } @@ -248,9 +248,9 @@ iq_handlers_init(void) } void -iq_id_handler_add(const char *const id, ProfIdCallback func, ProfIdFreeCallback free_func, void *userdata) +iq_id_handler_add(const char *const id, ProfIqCallback func, ProfIqFreeCallback free_func, void *userdata) { - ProfIdHandler *handler = malloc(sizeof(ProfIdHandler)); + ProfIqHandler *handler = malloc(sizeof(ProfIqHandler)); handler->func = func; handler->free_func = free_func; handler->userdata = userdata; @@ -438,7 +438,7 @@ iq_room_info_request(const char *const room, gboolean display_result) cb_data->room = strdup(room); cb_data->display = display_result; - iq_id_handler_add(id, _room_info_response_id_handler, (ProfIdFreeCallback)_iq_free_room_data, cb_data); + iq_id_handler_add(id, _room_info_response_id_handler, (ProfIqFreeCallback)_iq_free_room_data, cb_data); free(id); @@ -651,7 +651,7 @@ iq_room_affiliation_set(const char *const room, const char *const jid, char *aff affiliation_set->item = strdup(jid); affiliation_set->privilege = strdup(affiliation); - iq_id_handler_add(id, _room_affiliation_set_result_id_handler, (ProfIdFreeCallback)_iq_free_affiliation_set, affiliation_set); + iq_id_handler_add(id, _room_affiliation_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, affiliation_set); iq_send_stanza(iq); xmpp_stanza_release(iq); @@ -670,7 +670,7 @@ iq_room_role_set(const char *const room, const char *const nick, char *role, role_set->item = strdup(nick); role_set->privilege = strdup(role); - iq_id_handler_add(id, _room_role_set_result_id_handler, (ProfIdFreeCallback)_iq_free_affiliation_set, role_set); + iq_id_handler_add(id, _room_role_set_result_id_handler, (ProfIqFreeCallback)_iq_free_affiliation_set, role_set); iq_send_stanza(iq); xmpp_stanza_release(iq); @@ -697,7 +697,7 @@ iq_send_ping(const char *const target) const char *id = xmpp_stanza_get_id(iq); GDateTime *now = g_date_time_new_now_local(); - iq_id_handler_add(id, _manual_pong_id_handler, (ProfIdFreeCallback)g_date_time_unref, now); + iq_id_handler_add(id, _manual_pong_id_handler, (ProfIqFreeCallback)g_date_time_unref, now); iq_send_stanza(iq); xmpp_stanza_release(iq); @@ -2291,6 +2291,8 @@ _disco_info_response_id_handler_onconnect(xmpp_stanza_t *const stanza, void *con } } + connection_features_received(from); + return 0; } diff --git a/src/xmpp/iq.h b/src/xmpp/iq.h index 025d5e9f..bc273db4 100644 --- a/src/xmpp/iq.h +++ b/src/xmpp/iq.h @@ -35,12 +35,12 @@ #ifndef XMPP_IQ_H #define XMPP_IQ_H -typedef int(*ProfIdCallback)(xmpp_stanza_t *const stanza, void *const userdata); -typedef void(*ProfIdFreeCallback)(void *userdata); +typedef int(*ProfIqCallback)(xmpp_stanza_t *const stanza, void *const userdata); +typedef void(*ProfIqFreeCallback)(void *userdata); void iq_handlers_init(void); void iq_send_stanza(xmpp_stanza_t *const stanza); -void iq_id_handler_add(const char *const id, ProfIdCallback func, ProfIdFreeCallback free_func, void *userdata); +void iq_id_handler_add(const char *const id, ProfIqCallback func, ProfIqFreeCallback free_func, void *userdata); void iq_disco_info_request_onconnect(gchar *jid); void iq_disco_items_request_onconnect(gchar *jid); void iq_send_caps_request(const char *const to, const char *const id, const char *const node, const char *const ver); diff --git a/src/xmpp/message.c b/src/xmpp/message.c index adea5c10..47cf35d7 100644 --- a/src/xmpp/message.c +++ b/src/xmpp/message.c @@ -52,6 +52,7 @@ #include "pgp/gpg.h" #include "plugins/plugins.h" #include "ui/ui.h" +#include "ui/window_list.h" #include "xmpp/chat_session.h" #include "xmpp/muc.h" #include "xmpp/session.h" @@ -62,6 +63,17 @@ #include "xmpp/connection.h" #include "xmpp/xmpp.h" +#ifdef HAVE_OMEMO +#include "xmpp/omemo.h" +#include "omemo/omemo.h" +#endif + +typedef struct p_message_handle_t { + ProfMessageCallback func; + ProfMessageFreeCallback free_func; + void *userdata; +} ProfMessageHandler; + static int _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata); static void _handle_error(xmpp_stanza_t *const stanza); @@ -74,6 +86,8 @@ static void _handle_chat(xmpp_stanza_t *const stanza); static void _send_message_stanza(xmpp_stanza_t *const stanza); +static GHashTable *pubsub_event_handlers; + static int _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata) { @@ -118,6 +132,23 @@ _message_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *con _handle_receipt_received(stanza); } + xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_EVENT); + if (event) { + xmpp_stanza_t *child = xmpp_stanza_get_children(event); + if (child) { + const char *node = xmpp_stanza_get_attribute(child, STANZA_ATTR_NODE); + if (node) { + ProfMessageHandler *handler = g_hash_table_lookup(pubsub_event_handlers, node); + if (handler) { + int keep = handler->func(stanza, handler->userdata); + if (!keep) { + g_hash_table_remove(pubsub_event_handlers, node); + } + } + } + } + } + _handle_chat(stanza); return 1; @@ -129,6 +160,33 @@ message_handlers_init(void) xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_handler_add(conn, _message_handler, NULL, STANZA_NAME_MESSAGE, NULL, ctx); + + if (pubsub_event_handlers) { + GList *keys = g_hash_table_get_keys(pubsub_event_handlers); + GList *curr = keys; + while (curr) { + ProfMessageHandler *handler = g_hash_table_lookup(pubsub_event_handlers, curr->data); + if (handler->free_func && handler->userdata) { + handler->free_func(handler->userdata); + } + curr = g_list_next(curr); + } + g_list_free(keys); + g_hash_table_destroy(pubsub_event_handlers); + } + + pubsub_event_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, free, free); +} + +void +message_pubsub_event_handler_add(const char *const node, ProfMessageCallback func, ProfMessageFreeCallback free_func, void *userdata) +{ + ProfMessageHandler *handler = malloc(sizeof(ProfMessageHandler)); + handler->func = func; + handler->free_func = free_func; + handler->userdata = userdata; + + g_hash_table_insert(pubsub_event_handlers, strdup(node), handler); } char* @@ -254,6 +312,118 @@ message_send_chat_otr(const char *const barejid, const char *const msg, gboolean return id; } +#ifdef HAVE_OMEMO +char* +message_send_chat_omemo(const char *const jid, uint32_t sid, GList *keys, + const unsigned char *const iv, size_t iv_len, + const unsigned char *const ciphertext, size_t ciphertext_len, + gboolean request_receipt, gboolean muc) +{ + char *state = chat_session_get_state(jid); + xmpp_ctx_t * const ctx = connection_get_ctx(); + char *id; + xmpp_stanza_t *message; + if (muc) { + id = connection_create_stanza_id("muc"); + message = xmpp_message_new(ctx, STANZA_TYPE_GROUPCHAT, jid, id); + stanza_attach_origin_id(ctx, message, id); + } else { + id = connection_create_stanza_id("msg"); + message = xmpp_message_new(ctx, STANZA_TYPE_CHAT, jid, id); + } + + xmpp_stanza_t *encrypted = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(encrypted, "encrypted"); + xmpp_stanza_set_ns(encrypted, STANZA_NS_OMEMO); + + xmpp_stanza_t *header = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(header, "header"); + char *sid_text = g_strdup_printf("%d", sid); + xmpp_stanza_set_attribute(header, "sid", sid_text); + g_free(sid_text); + + GList *key_iter; + for (key_iter = keys; key_iter != NULL; key_iter = key_iter->next) { + omemo_key_t *key = (omemo_key_t *)key_iter->data; + + xmpp_stanza_t *key_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(key_stanza, "key"); + char *rid = g_strdup_printf("%d", key->device_id); + xmpp_stanza_set_attribute(key_stanza, "rid", rid); + g_free(rid); + if (key->prekey) { + xmpp_stanza_set_attribute(key_stanza, "prekey", "true"); + } + + gchar *key_raw = g_base64_encode(key->data, key->length); + xmpp_stanza_t *key_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(key_text, key_raw); + g_free(key_raw); + + xmpp_stanza_add_child(key_stanza, key_text); + xmpp_stanza_add_child(header, key_stanza); + xmpp_stanza_release(key_text); + xmpp_stanza_release(key_stanza); + } + + xmpp_stanza_t *iv_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(iv_stanza, "iv"); + + gchar *iv_raw = g_base64_encode(iv, iv_len); + xmpp_stanza_t *iv_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(iv_text, iv_raw); + g_free(iv_raw); + + xmpp_stanza_add_child(iv_stanza, iv_text); + xmpp_stanza_add_child(header, iv_stanza); + xmpp_stanza_release(iv_text); + xmpp_stanza_release(iv_stanza); + + xmpp_stanza_add_child(encrypted, header); + xmpp_stanza_release(header); + + xmpp_stanza_t *payload = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(payload, "payload"); + + gchar *ciphertext_raw = g_base64_encode(ciphertext, ciphertext_len); + xmpp_stanza_t *payload_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(payload_text, ciphertext_raw); + g_free(ciphertext_raw); + + xmpp_stanza_add_child(payload, payload_text); + xmpp_stanza_add_child(encrypted, payload); + xmpp_stanza_release(payload_text); + xmpp_stanza_release(payload); + + xmpp_stanza_add_child(message, encrypted); + xmpp_stanza_release(encrypted); + + xmpp_stanza_t *body = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(body, "body"); + xmpp_stanza_t *body_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(body_text, "You received a message encrypted with OMEMO but your client doesn't support OMEMO."); + xmpp_stanza_add_child(body, body_text); + xmpp_stanza_release(body_text); + xmpp_stanza_add_child(message, body); + xmpp_stanza_release(body); + + if (state) { + stanza_attach_state(ctx, message, state); + } + + stanza_attach_hints_store(ctx, message); + + if (request_receipt) { + stanza_attach_receipt_request(ctx, message); + } + + _send_message_stanza(message); + xmpp_stanza_release(message); + + return id; +} +#endif + void message_send_private(const char *const fulljid, const char *const msg, const char *const oob_url) { @@ -273,23 +443,24 @@ message_send_private(const char *const fulljid, const char *const msg, const cha xmpp_stanza_release(message); } -void +char* message_send_groupchat(const char *const roomjid, const char *const msg, const char *const oob_url) { xmpp_ctx_t * const ctx = connection_get_ctx(); char *id = connection_create_stanza_id("muc"); xmpp_stanza_t *message = xmpp_message_new(ctx, STANZA_TYPE_GROUPCHAT, roomjid, id); + stanza_attach_origin_id(ctx, message, id); xmpp_message_set_body(message, msg); - free(id); - if (oob_url) { stanza_attach_x_oob_url(ctx, message, oob_url); } _send_message_stanza(message); xmpp_stanza_release(message); + + return id; } void @@ -518,6 +689,14 @@ _handle_groupchat(xmpp_stanza_t *const stanza) { xmpp_ctx_t *ctx = connection_get_ctx(); char *message = NULL; + + const char *id = xmpp_stanza_get_id(stanza); + + xmpp_stanza_t *origin = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_STABLE_ID); + if (origin && g_strcmp0(xmpp_stanza_get_name(origin), STANZA_NAME_ORIGIN_ID) == 0) { + id = xmpp_stanza_get_attribute(origin, STANZA_ATTR_ID); + } + const char *room_jid = xmpp_stanza_get_from(stanza); Jid *jid = jid_create(room_jid); @@ -560,19 +739,28 @@ _handle_groupchat(xmpp_stanza_t *const stanza) return; } - message = xmpp_message_get_body(stanza); + // check omemo encryption + gboolean omemo = FALSE; +#ifdef HAVE_OMEMO + message = omemo_receive_message(stanza); + omemo = message != NULL; +#endif + if (!message) { - jid_destroy(jid); - return; + message = xmpp_message_get_body(stanza); + if (!message) { + jid_destroy(jid); + return; + } } // determine if the notifications happened whilst offline GDateTime *timestamp = stanza_get_delay(stanza); if (timestamp) { - sv_ev_room_history(jid->barejid, jid->resourcepart, timestamp, message); + sv_ev_room_history(jid->barejid, jid->resourcepart, timestamp, message, omemo); g_date_time_unref(timestamp); } else { - sv_ev_room_message(jid->barejid, jid->resourcepart, message); + sv_ev_room_message(jid->barejid, jid->resourcepart, message, id, omemo); } xmpp_free(ctx, message); @@ -675,6 +863,7 @@ _private_chat_handler(xmpp_stanza_t *const stanza, const char *const fulljid) static gboolean _handle_carbons(xmpp_stanza_t *const stanza) { + char *message_txt = NULL; xmpp_stanza_t *carbons = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CARBONS); if (!carbons) { return FALSE; @@ -708,10 +897,19 @@ _handle_carbons(xmpp_stanza_t *const stanza) return TRUE; } - char *message_txt = xmpp_message_get_body(message); + // check omemo encryption + gboolean omemo = FALSE; +#ifdef HAVE_OMEMO + message_txt = omemo_receive_message(message); + omemo = message_txt != NULL; +#endif + if (!message_txt) { - log_warning("Carbon received with no message."); - return TRUE; + message_txt = xmpp_message_get_body(message); + if (!message_txt) { + log_warning("Carbon received with no message."); + return TRUE; + } } Jid *my_jid = jid_create(connection_get_fulljid()); @@ -739,11 +937,11 @@ _handle_carbons(xmpp_stanza_t *const stanza) // if we are the recipient, treat as standard incoming message if (g_strcmp0(my_jid->barejid, jid_to->barejid) == 0) { - sv_ev_incoming_carbon(jid_from->barejid, jid_from->resourcepart, message_txt, enc_message); + sv_ev_incoming_carbon(jid_from->barejid, jid_from->resourcepart, message_txt, enc_message, omemo); // else treat as a sent message } else { - sv_ev_outgoing_carbon(jid_to->barejid, message_txt, enc_message); + sv_ev_outgoing_carbon(jid_to->barejid, message_txt, enc_message, omemo); } xmpp_ctx_t *ctx = connection_get_ctx(); @@ -760,6 +958,7 @@ _handle_carbons(xmpp_stanza_t *const stanza) static void _handle_chat(xmpp_stanza_t *const stanza) { + char *message = NULL; // ignore if type not chat or absent const char *type = xmpp_stanza_get_type(stanza); if (!(g_strcmp0(type, "chat") == 0 || type == NULL)) { @@ -772,6 +971,13 @@ _handle_chat(xmpp_stanza_t *const stanza) return; } + // check omemo encryption + gboolean omemo = FALSE; +#ifdef HAVE_OMEMO + message = omemo_receive_message(stanza); + omemo = message != NULL; +#endif + // ignore handled namespaces xmpp_stanza_t *conf = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CONFERENCE); xmpp_stanza_t *captcha = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_CAPTCHA); @@ -801,19 +1007,24 @@ _handle_chat(xmpp_stanza_t *const stanza) // standard chat message, use jid without resource xmpp_ctx_t *ctx = connection_get_ctx(); GDateTime *timestamp = stanza_get_delay(stanza); - if (body) { - char *message = xmpp_stanza_get_text(body); - if (message) { - char *enc_message = NULL; - xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_ENCRYPTED); - if (x) { - enc_message = xmpp_stanza_get_text(x); - } - sv_ev_incoming_message(jid->barejid, jid->resourcepart, message, enc_message, timestamp); - xmpp_free(ctx, enc_message); + if (!message && body) { + message = xmpp_stanza_get_text(body); + } - _receipt_request_handler(stanza); + if (message) { + char *enc_message = NULL; + xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_ENCRYPTED); + if (x) { + enc_message = xmpp_stanza_get_text(x); + } + sv_ev_incoming_message(jid->barejid, jid->resourcepart, message, enc_message, timestamp, omemo); + xmpp_free(ctx, enc_message); + _receipt_request_handler(stanza); + + if (omemo) { + free(message); + } else { xmpp_free(ctx, message); } } diff --git a/src/xmpp/message.h b/src/xmpp/message.h index dee9be2d..0c81ca39 100644 --- a/src/xmpp/message.h +++ b/src/xmpp/message.h @@ -35,6 +35,10 @@ #ifndef XMPP_MESSAGE_H #define XMPP_MESSAGE_H +typedef int(*ProfMessageCallback)(xmpp_stanza_t *const stanza, void *const userdata); +typedef void(*ProfMessageFreeCallback)(void *userdata); + void message_handlers_init(void); +void message_pubsub_event_handler_add(const char *const node, ProfMessageCallback func, ProfMessageFreeCallback free_func, void *userdata); #endif diff --git a/src/xmpp/omemo.c b/src/xmpp/omemo.c new file mode 100644 index 00000000..4b77ef23 --- /dev/null +++ b/src/xmpp/omemo.c @@ -0,0 +1,448 @@ +#include <glib.h> + +#include "log.h" +#include "xmpp/connection.h" +#include "xmpp/form.h" +#include "xmpp/iq.h" +#include "xmpp/message.h" +#include "xmpp/stanza.h" + +#include "omemo/omemo.h" + +static int _omemo_receive_devicelist(xmpp_stanza_t *const stanza, void *const userdata); +static int _omemo_bundle_publish_result(xmpp_stanza_t *const stanza, void *const userdata); +static int _omemo_bundle_publish_configure(xmpp_stanza_t *const stanza, void *const userdata); +static int _omemo_bundle_publish_configure_result(xmpp_stanza_t *const stanza, void *const userdata); + +void +omemo_devicelist_subscribe(void) +{ + message_pubsub_event_handler_add(STANZA_NS_OMEMO_DEVICELIST, _omemo_receive_devicelist, NULL, NULL); + + caps_add_feature(XMPP_FEATURE_OMEMO_DEVICELIST_NOTIFY); +} + +void +omemo_devicelist_publish(GList *device_list) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_omemo_devicelist_publish(ctx, device_list); + + if (connection_supports(XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS)) { + stanza_attach_publish_options(ctx, iq, "pubsub#access_model", "open"); + } + + iq_send_stanza(iq); + xmpp_stanza_release(iq); +} + +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 +omemo_bundle_publish(gboolean first) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + unsigned char *identity_key = NULL; + size_t identity_key_length; + unsigned char *signed_prekey = NULL; + size_t signed_prekey_length; + unsigned char *signed_prekey_signature = NULL; + size_t signed_prekey_signature_length; + GList *prekeys = NULL, *ids = NULL, *lengths = NULL; + + omemo_identity_key(&identity_key, &identity_key_length); + omemo_signed_prekey(&signed_prekey, &signed_prekey_length); + omemo_signed_prekey_signature(&signed_prekey_signature, &signed_prekey_signature_length); + omemo_prekeys(&prekeys, &ids, &lengths); + + char *id = connection_create_stanza_id("omemo_bundle_publish"); + xmpp_stanza_t *iq = stanza_create_omemo_bundle_publish(ctx, id, + omemo_device_id(), identity_key, identity_key_length, signed_prekey, + signed_prekey_length, signed_prekey_signature, + signed_prekey_signature_length, prekeys, ids, lengths); + + g_list_free_full(prekeys, free); + g_list_free(lengths); + g_list_free(ids); + + if (connection_supports(XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS)) { + stanza_attach_publish_options(ctx, iq, "pubsub#access_model", "open"); + } + + iq_id_handler_add(id, _omemo_bundle_publish_result, NULL, GINT_TO_POINTER(first)); + + iq_send_stanza(iq); + + xmpp_stanza_release(iq); + free(identity_key); + free(signed_prekey); + free(signed_prekey_signature); + free(id); +} + +void +omemo_bundle_request(const char * const jid, uint32_t device_id, ProfIqCallback func, ProfIqFreeCallback free_func, void *userdata) +{ + xmpp_ctx_t * const ctx = connection_get_ctx(); + char *id = connection_create_stanza_id("bundle_request"); + + xmpp_stanza_t *iq = stanza_create_omemo_bundle_request(ctx, id, jid, device_id); + iq_id_handler_add(id, func, free_func, userdata); + + iq_send_stanza(iq); + + free(id); + xmpp_stanza_release(iq); +} + +int +omemo_start_device_session_handle_bundle(xmpp_stanza_t *const stanza, void *const userdata) +{ + char *from = NULL; + const char *from_attr = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + if (!from_attr) { + Jid *jid = jid_create(connection_get_fulljid()); + from = strdup(jid->barejid); + jid_destroy(jid); + } else { + from = strdup(from_attr); + } + + if (g_strcmp0(from, userdata) != 0) { + return 1; + } + + xmpp_stanza_t *pubsub = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB); + if (!pubsub) { + return 1; + } + + xmpp_stanza_t *items = xmpp_stanza_get_child_by_name(pubsub, "items"); + if (!items) { + return 1; + } + const char *node = xmpp_stanza_get_attribute(items, "node"); + char *device_id_str = strstr(node, ":"); + if (!device_id_str) { + return 1; + } + + uint32_t device_id = strtoul(++device_id_str, NULL, 10); + + xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(items, "item"); + if (!item) { + return 1; + } + + xmpp_stanza_t *bundle = xmpp_stanza_get_child_by_ns(item, STANZA_NS_OMEMO); + if (!bundle) { + return 1; + } + + xmpp_stanza_t *prekeys = xmpp_stanza_get_child_by_name(bundle, "prekeys"); + if (!prekeys) { + return 1; + } + + GList *prekeys_list = NULL; + xmpp_stanza_t *prekey; + for (prekey = xmpp_stanza_get_children(prekeys); prekey != NULL; prekey = xmpp_stanza_get_next(prekey)) { + omemo_key_t *key = malloc(sizeof(omemo_key_t)); + + const char *prekey_id_text = xmpp_stanza_get_attribute(prekey, "preKeyId"); + if (!prekey_id_text) { + return 1; + } + key->id = strtoul(prekey_id_text, NULL, 10); + xmpp_stanza_t *prekey_text = xmpp_stanza_get_children(prekey); + if (!prekey_text) { + return 1; + } + char *prekey_b64 = xmpp_stanza_get_text(prekey_text); + key->data = g_base64_decode(prekey_b64, &key->length); + free(prekey_b64); + key->prekey = TRUE; + key->device_id = device_id; + + prekeys_list = g_list_append(prekeys_list, key); + } + + xmpp_stanza_t *signed_prekey = xmpp_stanza_get_child_by_name(bundle, "signedPreKeyPublic"); + if (!signed_prekey) { + return 1; + } + const char *signed_prekey_id_text = xmpp_stanza_get_attribute(signed_prekey, "signedPreKeyId"); + if (!signed_prekey_id_text) { + return 1; + } + uint32_t signed_prekey_id = strtoul(signed_prekey_id_text, NULL, 10); + xmpp_stanza_t *signed_prekey_text = xmpp_stanza_get_children(signed_prekey); + if (!signed_prekey_text) { + return 1; + } + size_t signed_prekey_len; + char *signed_prekey_b64 = xmpp_stanza_get_text(signed_prekey_text); + unsigned char *signed_prekey_raw = g_base64_decode(signed_prekey_b64, &signed_prekey_len); + free(signed_prekey_b64); + + xmpp_stanza_t *signed_prekey_signature = xmpp_stanza_get_child_by_name(bundle, "signedPreKeySignature"); + if (!signed_prekey_signature) { + return 1; + } + xmpp_stanza_t *signed_prekey_signature_text = xmpp_stanza_get_children(signed_prekey_signature); + if (!signed_prekey_signature_text) { + return 1; + } + size_t signed_prekey_signature_len; + char *signed_prekey_signature_b64 = xmpp_stanza_get_text(signed_prekey_signature_text); + unsigned char *signed_prekey_signature_raw = g_base64_decode(signed_prekey_signature_b64, &signed_prekey_signature_len); + free(signed_prekey_signature_b64); + + xmpp_stanza_t *identity_key = xmpp_stanza_get_child_by_name(bundle, "identityKey"); + if (!identity_key) { + return 1; + } + xmpp_stanza_t *identity_key_text = xmpp_stanza_get_children(identity_key); + if (!identity_key_text) { + return 1; + } + size_t identity_key_len; + char *identity_key_b64 = xmpp_stanza_get_text(identity_key_text); + unsigned char *identity_key_raw = g_base64_decode(identity_key_b64, &identity_key_len); + free(identity_key_b64); + + omemo_start_device_session(from, device_id, prekeys_list, signed_prekey_id, + signed_prekey_raw, signed_prekey_len, signed_prekey_signature_raw, + signed_prekey_signature_len, identity_key_raw, identity_key_len); + + free(from); + g_list_free_full(prekeys_list, (GDestroyNotify)omemo_key_free); + g_free(signed_prekey_raw); + g_free(identity_key_raw); + g_free(signed_prekey_signature_raw); + return 1; +} + +char * +omemo_receive_message(xmpp_stanza_t *const stanza) +{ + const char *type = xmpp_stanza_get_type(stanza); + + xmpp_stanza_t *encrypted = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_OMEMO); + if (!encrypted) { + return NULL; + } + + xmpp_stanza_t *header = xmpp_stanza_get_child_by_name(encrypted, "header"); + if (!header) { + return NULL; + } + + const char *sid_text = xmpp_stanza_get_attribute(header, "sid"); + if (!sid_text) { + return NULL; + } + uint32_t sid = strtoul(sid_text, NULL, 10); + + xmpp_stanza_t *iv = xmpp_stanza_get_child_by_name(header, "iv"); + if (!iv) { + return NULL; + } + char *iv_text = xmpp_stanza_get_text(iv); + if (!iv_text) { + return NULL; + } + size_t iv_len; + unsigned char *iv_raw = g_base64_decode(iv_text, &iv_len); + + xmpp_stanza_t *payload = xmpp_stanza_get_child_by_name(encrypted, "payload"); + if (!payload) { + return NULL; + } + char *payload_text = xmpp_stanza_get_text(payload); + if (!payload_text) { + return NULL; + } + size_t payload_len; + unsigned char *payload_raw = g_base64_decode(payload_text, &payload_len); + + GList *keys = NULL; + xmpp_stanza_t *key_stanza; + for (key_stanza = xmpp_stanza_get_children(header); key_stanza != NULL; key_stanza = xmpp_stanza_get_next(key_stanza)) { + if (g_strcmp0(xmpp_stanza_get_name(key_stanza), "key") != 0) { + continue; + } + + omemo_key_t *key = malloc(sizeof(omemo_key_t)); + char *key_text = xmpp_stanza_get_text(key_stanza); + if (!key_text) { + goto skip; + } + + + const char *rid_text = xmpp_stanza_get_attribute(key_stanza, "rid"); + key->device_id = strtoul(rid_text, NULL, 10); + if (!key->device_id) { + goto skip; + } + key->data = g_base64_decode(key_text, &key->length); + free(key_text); + key->prekey = g_strcmp0(xmpp_stanza_get_attribute(key_stanza, "prekey"), "true") == 0; + keys = g_list_append(keys, key); + continue; + +skip: + free(key); + } + + const char *from = xmpp_stanza_get_from(stanza); + + char *plaintext = omemo_on_message_recv(from, sid, iv_raw, iv_len, + keys, payload_raw, payload_len, + g_strcmp0(type, STANZA_TYPE_GROUPCHAT) == 0); + + g_list_free_full(keys, (GDestroyNotify)omemo_key_free); + g_free(iv_raw); + g_free(payload_raw); + g_free(iv_text); + g_free(payload_text); + + return plaintext; +} + +static int +_omemo_receive_devicelist(xmpp_stanza_t *const stanza, void *const userdata) +{ + GList *device_list = NULL; + const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + + xmpp_stanza_t *root = NULL; + xmpp_stanza_t *event = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_PUBSUB_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(root, "items"); + if (!items) { + return 1; + } + + xmpp_stanza_t *item = xmpp_stanza_get_child_by_name(items, "item"); + if (item) { + xmpp_stanza_t *list = xmpp_stanza_get_child_by_ns(item, STANZA_NS_OMEMO); + if (!list) { + return 1; + } + + xmpp_stanza_t *device; + for (device = xmpp_stanza_get_children(list); device != NULL; device = xmpp_stanza_get_next(device)) { + const char *id = xmpp_stanza_get_id(device); + device_list = g_list_append(device_list, GINT_TO_POINTER(strtoul(id, NULL, 10))); + } + } + omemo_set_device_list(from, device_list); + + return 1; +} + +static int +_omemo_bundle_publish_result(xmpp_stanza_t *const stanza, void *const userdata) +{ + const char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) != 0) { + return 0; + } + + if (!GPOINTER_TO_INT(userdata)) { + log_error("OMEMO: definitely cannot publish bundle with an open access model"); + return 0; + } + + log_info("OMEMO: cannot publish bundle with open access model, trying to configure node"); + xmpp_ctx_t * const ctx = connection_get_ctx(); + Jid *jid = jid_create(connection_get_fulljid()); + char *id = connection_create_stanza_id("omemo_bundle_node_configure_request"); + char *node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id()); + xmpp_stanza_t *iq = stanza_create_pubsub_configure_request(ctx, id, jid->barejid, node); + g_free(node); + + iq_id_handler_add(id, _omemo_bundle_publish_configure, NULL, userdata); + + iq_send_stanza(iq); + + xmpp_stanza_release(iq); + free(id); + jid_destroy(jid); + return 0; +} + +static int +_omemo_bundle_publish_configure(xmpp_stanza_t *const stanza, void *const userdata) +{ + /* TODO handle error */ + xmpp_stanza_t *pubsub = xmpp_stanza_get_child_by_name(stanza, "pubsub"); + xmpp_stanza_t *configure = xmpp_stanza_get_child_by_name(pubsub, STANZA_NAME_CONFIGURE); + xmpp_stanza_t *x = xmpp_stanza_get_child_by_name(configure, "x"); + + DataForm* form = form_create(x); + char *tag = g_hash_table_lookup(form->var_to_tag, "pubsub#access_model"); + if (!tag) { + log_info("OMEMO: cannot configure bundle to an open access model"); + return 0; + } + form_set_value(form, tag, "open"); + + xmpp_ctx_t * const ctx = connection_get_ctx(); + Jid *jid = jid_create(connection_get_fulljid()); + char *id = connection_create_stanza_id("omemo_bundle_node_configure_submit"); + char *node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, omemo_device_id()); + xmpp_stanza_t *iq = stanza_create_pubsub_configure_submit(ctx, id, jid->barejid, node, form); + g_free(node); + + iq_id_handler_add(id, _omemo_bundle_publish_configure_result, NULL, userdata); + + iq_send_stanza(iq); + + xmpp_stanza_release(iq); + free(id); + jid_destroy(jid); + return 0; +} + +static int +_omemo_bundle_publish_configure_result(xmpp_stanza_t *const stanza, void *const userdata) +{ + const char *type = xmpp_stanza_get_type(stanza); + + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + log_error("OMEMO: cannot configure bundle to an open access model"); + return 0; + } + + omemo_bundle_publish(TRUE); + + return 0; +} diff --git a/src/xmpp/omemo.h b/src/xmpp/omemo.h new file mode 100644 index 00000000..f1fff7b7 --- /dev/null +++ b/src/xmpp/omemo.h @@ -0,0 +1,11 @@ +#include <glib.h> + +#include "xmpp/iq.h" + +void omemo_devicelist_subscribe(void); +void omemo_devicelist_publish(GList *device_list); +void omemo_devicelist_request(const char * const jid); +void omemo_bundle_publish(gboolean first); +void omemo_bundle_request(const char * const jid, uint32_t device_id, ProfIqCallback func, ProfIqFreeCallback free_func, void *userdata); +int omemo_start_device_session_handle_bundle(xmpp_stanza_t *const stanza, void *const userdata); +char * omemo_receive_message(xmpp_stanza_t *const stanza); diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index 9be154e7..fe15515f 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -137,7 +137,7 @@ roster_send_add_to_group(const char *const group, PContact contact) } xmpp_ctx_t * const ctx = connection_get_ctx(); - iq_id_handler_add(unique_id, _group_add_id_handler, (ProfIdFreeCallback)_free_group_data, data); + iq_id_handler_add(unique_id, _group_add_id_handler, (ProfIqFreeCallback)_free_group_data, data); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), p_contact_name(contact), new_groups); iq_send_stanza(iq); @@ -180,7 +180,7 @@ roster_send_remove_from_group(const char *const group, PContact contact) data->name = strdup(p_contact_barejid(contact)); } - iq_id_handler_add(unique_id, _group_remove_id_handler, (ProfIdFreeCallback)_free_group_data, data); + iq_id_handler_add(unique_id, _group_remove_id_handler, (ProfIqFreeCallback)_free_group_data, data); xmpp_stanza_t *iq = stanza_create_roster_set(ctx, unique_id, p_contact_barejid(contact), p_contact_name(contact), new_groups); iq_send_stanza(iq); diff --git a/src/xmpp/session.c b/src/xmpp/session.c index de7fb7ac..675f23af 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -60,6 +60,11 @@ #include "xmpp/chat_session.h" #include "xmpp/jid.h" +#ifdef HAVE_OMEMO +#include "omemo/omemo.h" +#include "xmpp/omemo.h" +#endif + // for auto reconnect static struct { char *name; @@ -286,6 +291,12 @@ session_get_account_name(void) void session_login_success(gboolean secured) { + chat_sessions_init(); + + message_handlers_init(); + presence_handlers_init(); + iq_handlers_init(); + // logged in with account if (saved_account.name) { log_debug("Connection handler: logged in with account name: %s", saved_account.name); @@ -297,26 +308,20 @@ session_login_success(gboolean secured) accounts_add(saved_details.name, saved_details.altdomain, saved_details.port, saved_details.tls_policy); accounts_set_jid(saved_details.name, saved_details.jid); - sv_ev_login_account_success(saved_details.name, secured); saved_account.name = strdup(saved_details.name); saved_account.passwd = strdup(saved_details.passwd); _session_free_saved_details(); + sv_ev_login_account_success(saved_account.name, secured); } - chat_sessions_init(); - - message_handlers_init(); - presence_handlers_init(); - iq_handlers_init(); - roster_request(); bookmark_request(); blocking_request(); // items discovery + connection_request_features(); char *domain = connection_get_domain(); - iq_disco_info_request_onconnect(domain); iq_disco_items_request_onconnect(domain); if (prefs_get_boolean(PREF_CARBONS)){ diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 534ee06b..615de44f 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -396,6 +396,18 @@ stanza_attach_hints_no_store(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza) } xmpp_stanza_t* +stanza_attach_hints_store(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza) +{ + xmpp_stanza_t *store = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(store, "store"); + xmpp_stanza_set_ns(store, STANZA_NS_HINTS); + xmpp_stanza_add_child(stanza, store); + xmpp_stanza_release(store); + + return stanza; +} + +xmpp_stanza_t* stanza_attach_receipt_request(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza) { xmpp_stanza_t *receipet_request = xmpp_stanza_new(ctx); @@ -1821,6 +1833,45 @@ stanza_get_error_message(xmpp_stanza_t *stanza) } void +stanza_attach_publish_options(xmpp_ctx_t *const ctx, xmpp_stanza_t *const iq, const char *const option, const char *const value) +{ + xmpp_stanza_t *publish_options = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(publish_options, STANZA_NAME_PUBLISH_OPTIONS); + + xmpp_stanza_t *x = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(x, STANZA_NAME_X); + xmpp_stanza_set_ns(x, STANZA_NS_DATA); + xmpp_stanza_set_type(x, "submit"); + xmpp_stanza_add_child(publish_options, x); + + xmpp_stanza_t *form_type = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(form_type, STANZA_NAME_FIELD); + xmpp_stanza_set_attribute(form_type, STANZA_ATTR_VAR, "FORM_TYPE"); + xmpp_stanza_set_type(form_type, "hidden"); + xmpp_stanza_t *form_type_value = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(form_type_value, STANZA_NAME_VALUE); + xmpp_stanza_t *form_type_value_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(form_type_value_text, XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS); + xmpp_stanza_add_child(form_type_value, form_type_value_text); + xmpp_stanza_add_child(form_type, form_type_value); + xmpp_stanza_add_child(x, form_type); + + xmpp_stanza_t *access_model = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(access_model, STANZA_NAME_FIELD); + xmpp_stanza_set_attribute(access_model, STANZA_ATTR_VAR, option); + xmpp_stanza_t *access_model_value = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(access_model_value, STANZA_NAME_VALUE); + xmpp_stanza_t *access_model_value_text = xmpp_stanza_new(ctx); + xmpp_stanza_set_text(access_model_value_text, value); + xmpp_stanza_add_child(access_model_value, access_model_value_text); + xmpp_stanza_add_child(access_model, access_model_value); + xmpp_stanza_add_child(x, access_model); + + xmpp_stanza_t *pubsub = xmpp_stanza_get_child_by_ns(iq, STANZA_NS_PUBSUB); + xmpp_stanza_add_child(pubsub, publish_options); +} + +void stanza_attach_priority(xmpp_ctx_t *const ctx, xmpp_stanza_t *const presence, const int pri) { if (pri == 0) { @@ -2092,6 +2143,295 @@ 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, STANZA_ATTR_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) +{ + char *id = connection_create_stanza_id("omemo_devicelist_subscribe"); + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id); + free(id); + + 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 *subscribe = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(subscribe, STANZA_NAME_SUBSCRIBE); + xmpp_stanza_set_attribute(subscribe, STANZA_ATTR_NODE, STANZA_NS_OMEMO_DEVICELIST); + xmpp_stanza_set_attribute(subscribe, "jid", jid); + + xmpp_stanza_add_child(pubsub, subscribe); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(subscribe); + xmpp_stanza_release(pubsub); + + return iq; +} + +xmpp_stanza_t* +stanza_create_omemo_devicelist_publish(xmpp_ctx_t *ctx, GList *const ids) +{ + char *id = connection_create_stanza_id("omemo_devicelist_publish"); + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id); + free(id); + + 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 *publish = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH); + xmpp_stanza_set_attribute(publish, STANZA_ATTR_NODE, STANZA_NS_OMEMO_DEVICELIST); + + xmpp_stanza_t *item = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(item, STANZA_NAME_ITEM); + xmpp_stanza_set_attribute(item, "id", "current"); + + xmpp_stanza_t *list = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(list, "list"); + xmpp_stanza_set_ns(list, "eu.siacs.conversations.axolotl"); + + GList *i; + for (i = ids; i != NULL; i = i->next) { + xmpp_stanza_t *device = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(device, "device"); + char *id = g_strdup_printf("%d", GPOINTER_TO_INT(i->data)); + xmpp_stanza_set_attribute(device, "id", id); + g_free(id); + + xmpp_stanza_add_child(list, device); + xmpp_stanza_release(device); + } + + xmpp_stanza_add_child(item, list); + xmpp_stanza_add_child(publish, item); + xmpp_stanza_add_child(pubsub, publish); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(list); + xmpp_stanza_release(item); + xmpp_stanza_release(publish); + xmpp_stanza_release(pubsub); + + return iq; +} + +xmpp_stanza_t* +stanza_create_omemo_bundle_publish(xmpp_ctx_t *ctx, const char *const id, + 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) +{ + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id); + + 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 *publish = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(publish, STANZA_NAME_PUBLISH); + char *node = g_strdup_printf("%s:%d", "eu.siacs.conversations.axolotl.bundles", device_id); + xmpp_stanza_set_attribute(publish, STANZA_ATTR_NODE, node); + g_free(node); + + xmpp_stanza_t *item = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(item, STANZA_NAME_ITEM); + xmpp_stanza_set_attribute(item, "id", "current"); + + xmpp_stanza_t *bundle = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(bundle, "bundle"); + xmpp_stanza_set_ns(bundle, "eu.siacs.conversations.axolotl"); + + xmpp_stanza_t *signed_prekey_public_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(signed_prekey_public_stanza , "signedPreKeyPublic"); + xmpp_stanza_set_attribute(signed_prekey_public_stanza, "signedPreKeyId", "1"); + + xmpp_stanza_t *signed_prekey_public_stanza_text= xmpp_stanza_new(ctx); + char *signed_prekey_b64 = g_base64_encode(signed_prekey, signed_prekey_length); + xmpp_stanza_set_text(signed_prekey_public_stanza_text, signed_prekey_b64); + g_free(signed_prekey_b64); + xmpp_stanza_add_child(signed_prekey_public_stanza, signed_prekey_public_stanza_text); + xmpp_stanza_release(signed_prekey_public_stanza_text); + + xmpp_stanza_t *signed_prekey_signature_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(signed_prekey_signature_stanza , "signedPreKeySignature"); + + xmpp_stanza_t *signed_prekey_signature_stanza_text= xmpp_stanza_new(ctx); + char *signed_prekey_signature_b64 = g_base64_encode(signed_prekey_signature, signed_prekey_signature_length); + xmpp_stanza_set_text(signed_prekey_signature_stanza_text, signed_prekey_signature_b64); + g_free(signed_prekey_signature_b64); + xmpp_stanza_add_child(signed_prekey_signature_stanza, signed_prekey_signature_stanza_text); + xmpp_stanza_release(signed_prekey_signature_stanza_text); + + xmpp_stanza_t *identity_key_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(identity_key_stanza , "identityKey"); + + xmpp_stanza_t *identity_key_stanza_text= xmpp_stanza_new(ctx); + char *identity_key_b64 = g_base64_encode(identity_key, identity_key_length); + xmpp_stanza_set_text(identity_key_stanza_text, identity_key_b64); + g_free(identity_key_b64); + xmpp_stanza_add_child(identity_key_stanza, identity_key_stanza_text); + xmpp_stanza_release(identity_key_stanza_text); + + xmpp_stanza_t *prekeys_stanza = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(prekeys_stanza, "prekeys"); + + GList *p, *i, *l; + for (p = prekeys, i = prekeys_id, l = prekeys_length; p != NULL; p = p->next, i = i->next, l = l->next) { + xmpp_stanza_t *prekey = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(prekey, "preKeyPublic"); + char *id = g_strdup_printf("%d", GPOINTER_TO_INT(i->data)); + xmpp_stanza_set_attribute(prekey, "preKeyId", id); + g_free(id); + + xmpp_stanza_t *prekey_text = xmpp_stanza_new(ctx); + char *prekey_b64 = g_base64_encode(p->data, GPOINTER_TO_INT(l->data)); + xmpp_stanza_set_text(prekey_text, prekey_b64); + g_free(prekey_b64); + + xmpp_stanza_add_child(prekey, prekey_text); + xmpp_stanza_add_child(prekeys_stanza, prekey); + xmpp_stanza_release(prekey_text); + xmpp_stanza_release(prekey); + } + + xmpp_stanza_add_child(bundle, signed_prekey_public_stanza); + xmpp_stanza_add_child(bundle, signed_prekey_signature_stanza); + xmpp_stanza_add_child(bundle, identity_key_stanza); + xmpp_stanza_add_child(bundle, prekeys_stanza); + xmpp_stanza_add_child(item, bundle); + xmpp_stanza_add_child(publish, item); + xmpp_stanza_add_child(pubsub, publish); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(signed_prekey_public_stanza); + xmpp_stanza_release(signed_prekey_signature_stanza); + xmpp_stanza_release(identity_key_stanza); + xmpp_stanza_release(prekeys_stanza); + xmpp_stanza_release(bundle); + xmpp_stanza_release(item); + xmpp_stanza_release(publish); + xmpp_stanza_release(pubsub); + + return iq; +} + +xmpp_stanza_t* +stanza_create_omemo_bundle_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid, uint32_t device_id) +{ + 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"); + char *node = g_strdup_printf("%s:%d", STANZA_NS_OMEMO_BUNDLES, device_id); + xmpp_stanza_set_attribute(items, STANZA_ATTR_NODE, node); + g_free(node); + + 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_pubsub_configure_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid, const char *const node) +{ + 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_OWNER); + + xmpp_stanza_t *configure = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(configure, STANZA_NAME_CONFIGURE); + xmpp_stanza_set_attribute(configure, STANZA_ATTR_NODE, node); + + xmpp_stanza_add_child(pubsub, configure); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(configure); + xmpp_stanza_release(pubsub); + + return iq; +} + +xmpp_stanza_t* +stanza_create_pubsub_configure_submit(xmpp_ctx_t *ctx, const char *const id, const char *const jid, const char *const node, DataForm *form) +{ + xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, 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_OWNER); + + xmpp_stanza_t *configure = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(configure, STANZA_NAME_CONFIGURE); + xmpp_stanza_set_attribute(configure, STANZA_ATTR_NODE, node); + + xmpp_stanza_t *x = form_create_submission(form); + + xmpp_stanza_add_child(configure, x); + xmpp_stanza_add_child(pubsub, configure); + xmpp_stanza_add_child(iq, pubsub); + + xmpp_stanza_release(x); + xmpp_stanza_release(configure); + xmpp_stanza_release(pubsub); + + return iq; +} + +xmpp_stanza_t* +stanza_attach_origin_id(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza, const char *const id) +{ + xmpp_stanza_t *origin_id = xmpp_stanza_new(ctx); + xmpp_stanza_set_name(origin_id, STANZA_NAME_ORIGIN_ID); + xmpp_stanza_set_ns(origin_id, STANZA_NS_STABLE_ID); + xmpp_stanza_set_attribute(origin_id, STANZA_ATTR_ID, id); + + xmpp_stanza_add_child(stanza, origin_id); + + xmpp_stanza_release(origin_id); + + return stanza; +} + static void _stanza_add_unique_id(xmpp_stanza_t *stanza, char *prefix) { diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index d3c3c9dc..e5e17ba4 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -82,6 +82,7 @@ #define STANZA_NAME_PUBSUB "pubsub" #define STANZA_NAME_PUBLISH "publish" #define STANZA_NAME_PUBLISH_OPTIONS "publish-options" +#define STANZA_NAME_SUBSCRIBE "subscribe" #define STANZA_NAME_FIELD "field" #define STANZA_NAME_STORAGE "storage" #define STANZA_NAME_NICK "nick" @@ -100,6 +101,8 @@ #define STANZA_NAME_GET "get" #define STANZA_NAME_URL "url" #define STANZA_NAME_COMMAND "command" +#define STANZA_NAME_CONFIGURE "configure" +#define STANZA_NAME_ORIGIN_ID "origin-id" // error conditions #define STANZA_NAME_BAD_REQUEST "bad-request" @@ -179,6 +182,8 @@ #define STANZA_NS_CONFERENCE "jabber:x:conference" #define STANZA_NS_CAPTCHA "urn:xmpp:captcha" #define STANZA_NS_PUBSUB "http://jabber.org/protocol/pubsub" +#define STANZA_NS_PUBSUB_OWNER "http://jabber.org/protocol/pubsub#owner" +#define STANZA_NS_PUBSUB_EVENT "http://jabber.org/protocol/pubsub#event" #define STANZA_NS_CARBONS "urn:xmpp:carbons:2" #define STANZA_NS_HINTS "urn:xmpp:hints" #define STANZA_NS_FORWARD "urn:xmpp:forward:0" @@ -189,6 +194,10 @@ #define STANZA_NS_X_OOB "jabber:x:oob" #define STANZA_NS_BLOCKING "urn:xmpp:blocking" #define STANZA_NS_COMMAND "http://jabber.org/protocol/commands" +#define STANZA_NS_OMEMO "eu.siacs.conversations.axolotl" +#define STANZA_NS_OMEMO_DEVICELIST "eu.siacs.conversations.axolotl.devicelist" +#define STANZA_NS_OMEMO_BUNDLES "eu.siacs.conversations.axolotl.bundles" +#define STANZA_NS_STABLE_ID "urn:xmpp:sid:0" #define STANZA_DATAFORM_SOFTWARE "urn:xmpp:dataforms:softwareinfo" @@ -228,8 +237,10 @@ xmpp_stanza_t* stanza_attach_state(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza, const xmpp_stanza_t* stanza_attach_carbons_private(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza); xmpp_stanza_t* stanza_attach_hints_no_copy(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza); xmpp_stanza_t* stanza_attach_hints_no_store(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza); +xmpp_stanza_t* stanza_attach_hints_store(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza); xmpp_stanza_t* stanza_attach_receipt_request(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza); xmpp_stanza_t* stanza_attach_x_oob_url(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza, const char *const url); +xmpp_stanza_t* stanza_attach_origin_id(xmpp_ctx_t *ctx, xmpp_stanza_t *stanza, const char *const id); xmpp_stanza_t* stanza_create_room_join_presence(xmpp_ctx_t *const ctx, const char *const full_room_jid, const char *const passwd); @@ -284,6 +295,17 @@ 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); +void stanza_attach_publish_options(xmpp_ctx_t *const ctx, xmpp_stanza_t *const publish, const char *const option, const char *const value); + +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, const char *const id, 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); +xmpp_stanza_t* stanza_create_omemo_bundle_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid, uint32_t device_id); + +xmpp_stanza_t* stanza_create_pubsub_configure_request(xmpp_ctx_t *ctx, const char *const id, const char *const jid, const char *const node); +xmpp_stanza_t* stanza_create_pubsub_configure_submit(xmpp_ctx_t *ctx, const char *const id, const char *const jid, const char *const node, DataForm *form); + int stanza_get_idle_time(xmpp_stanza_t *const stanza); void stanza_attach_priority(xmpp_ctx_t *const ctx, xmpp_stanza_t *const presence, const int pri); diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index c9403090..d5330599 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -35,6 +35,8 @@ #ifndef XMPP_XMPP_H #define XMPP_XMPP_H +#include <stdint.h> + #include "config.h" #ifdef HAVE_LIBMESODE @@ -61,6 +63,9 @@ #define XMPP_FEATURE_LASTACTIVITY "jabber:iq:last" #define XMPP_FEATURE_MUC "http://jabber.org/protocol/muc" #define XMPP_FEATURE_COMMANDS "http://jabber.org/protocol/commands" +#define XMPP_FEATURE_OMEMO_DEVICELIST_NOTIFY "eu.siacs.conversations.axolotl.devicelist+notify" +#define XMPP_FEATURE_PUBSUB "http://jabber.org/protocol/pubsub" +#define XMPP_FEATURE_PUBSUB_PUBLISH_OPTIONS "http://jabber.org/protocol/pubsub#publish-options" typedef enum { JABBER_CONNECTING, @@ -139,8 +144,9 @@ char* message_send_chat(const char *const barejid, const char *const msg, const gboolean request_receipt); char* message_send_chat_otr(const char *const barejid, const char *const msg, gboolean request_receipt); char* message_send_chat_pgp(const char *const barejid, const char *const msg, gboolean request_receipt); +char* message_send_chat_omemo(const char *const jid, uint32_t sid, GList *keys, const unsigned char *const iv, size_t iv_len, const unsigned char *const ciphertext, size_t ciphertext_len, gboolean request_receipt, gboolean muc); void message_send_private(const char *const fulljid, const char *const msg, const char *const oob_url); -void message_send_groupchat(const char *const roomjid, const char *const msg, const char *const oob_url); +char* message_send_groupchat(const char *const roomjid, const char *const msg, const char *const oob_url); void message_send_groupchat_subject(const char *const roomjid, const char *const subject); void message_send_inactive(const char *const jid); void message_send_composing(const char *const jid);