summary refs log tree commit diff stats
path: root/plugins/fishlim/plugin_hexchat.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/fishlim/plugin_hexchat.c')
-rw-r--r--plugins/fishlim/plugin_hexchat.c192
1 files changed, 176 insertions, 16 deletions
diff --git a/plugins/fishlim/plugin_hexchat.c b/plugins/fishlim/plugin_hexchat.c
index 0afecd82..a163fd10 100644
--- a/plugins/fishlim/plugin_hexchat.c
+++ b/plugins/fishlim/plugin_hexchat.c
@@ -32,6 +32,7 @@
 #define HEXCHAT_MAX_WORDS 32
 
 #include "fish.h"
+#include "dh1080.h"
 #include "keystore.h"
 #include "irc.h"
 
@@ -41,8 +42,11 @@ static const char plugin_version[] = "0.0.17";
 
 static const char usage_setkey[] = "Usage: SETKEY [<nick or #channel>] <password>, sets the key for a channel or nick";
 static const char usage_delkey[] = "Usage: DELKEY <nick or #channel>, deletes the key for a channel or nick";
+static const char usage_keyx[] = "Usage: KEYX [<nick>], performs DH1080 key-exchange with <nick>";
+
 
 static hexchat_plugin *ph;
+static GHashTable *pending_exchanges;
 
 
 /**
@@ -58,6 +62,38 @@ gchar *get_config_filename(void) {
     return filename_fs;
 }
 
+static inline gboolean irc_is_query (const char *name) {
+    const char *chantypes = hexchat_list_str (ph, NULL, "chantypes");
+
+    return strchr (chantypes, name[0]) == NULL;
+}
+
+static hexchat_context *find_context_on_network (const char *name) {
+    hexchat_list *channels;
+    hexchat_context *ret = NULL;
+    int id;
+
+    if (hexchat_get_prefs(ph, "id", NULL, &id) != 2)
+        return NULL;
+
+    channels = hexchat_list_get(ph, "channels");
+    if (!channels)
+        return NULL;
+
+    while (hexchat_list_next(ph, channels)) {
+        int chan_id = hexchat_list_int(ph, channels, "id");
+        const char *chan_name = hexchat_list_str(ph, channels, "channel");
+
+        if (chan_id == id && chan_name && hexchat_nickcmp (ph, chan_name, name) == 0) {
+            ret = (hexchat_context*)hexchat_list_str(ph, channels, "context");
+            break;
+        }
+    };
+
+    hexchat_list_free(ph, channels);
+    return ret;
+}
+
 int irc_nick_cmp(const char *a, const char *b) {
 	return hexchat_nickcmp (ph, a, b);
 }
@@ -163,11 +199,11 @@ static int handle_incoming(char *word[], char *word_eol[], hexchat_event_attrs *
                 /* Prefix with colon, which gets stripped out otherwise */
                 g_string_append_c (message, ':');
             }
-            
+
             if (prefix_char) {
                 g_string_append_c (message, prefix_char);
             }
-            
+
         } else {
             /* Add unencrypted data (for example, a prefix from a bouncer or bot) */
             peice = word[uw];
@@ -176,7 +212,7 @@ static int handle_incoming(char *word[], char *word_eol[], hexchat_event_attrs *
         g_string_append (message, peice);
     }
     g_free(decrypted);
-    
+
     /* Simulate unencrypted message */
     /* hexchat_printf(ph, "simulating: %s\n", message->str); */
     hexchat_command(ph, message->str);
@@ -184,26 +220,103 @@ static int handle_incoming(char *word[], char *word_eol[], hexchat_event_attrs *
     g_string_free (message, TRUE);
     g_free(sender_nick);
     return HEXCHAT_EAT_HEXCHAT;
-  
+
   decrypt_error:
     g_free(decrypted);
     g_free(sender_nick);
     return HEXCHAT_EAT_NONE;
 }
 
+static int handle_keyx_notice(char *word[], char *word_eol[], void *userdata) {
+    const char *dh_message = word[4];
+    const char *dh_pubkey = word[5];
+    hexchat_context *query_ctx;
+    const char *prefix;
+    gboolean cbc;
+    char *sender, *secret_key, *priv_key = NULL;
+
+    if (!*dh_message || !*dh_pubkey || strlen(dh_pubkey) != 181)
+        return HEXCHAT_EAT_NONE;
+
+    if (!irc_parse_message((const char**)word, &prefix, NULL, NULL) || !prefix)
+        return HEXCHAT_EAT_NONE;
+
+    sender = irc_prefix_get_nick(prefix);
+    query_ctx = find_context_on_network(sender);
+    if (query_ctx)
+        hexchat_set_context(ph, query_ctx);
+
+    dh_message++; /* : prefix */
+    if (*dh_message == '+' || *dh_message == '-')
+        dh_message++; /* identify-msg */
+
+    cbc = g_strcmp0 (word[6], "CBC") == 0;
+
+    if (!strcmp(dh_message, "DH1080_INIT")) {
+        char *pub_key;
+
+        if (cbc) {
+            hexchat_print(ph, "Recieved key exchange for CBC mode which is not supported.");
+            goto cleanup;
+        }
+
+        hexchat_printf(ph, "Received public key from %s, sending mine...", sender);
+        if (dh1080_generate_key(&priv_key, &pub_key)) {
+            hexchat_commandf(ph, "quote NOTICE %s :DH1080_FINISH %s", sender, pub_key);
+            g_free(pub_key);
+        } else {
+            hexchat_print(ph, "Failed to generate keys");
+            goto cleanup;
+        }
+    } else if (!strcmp (dh_message, "DH1080_FINISH")) {
+        char *sender_lower = g_ascii_strdown(sender, -1);
+        /* FIXME: Properly respect irc casing */
+        priv_key = g_hash_table_lookup(pending_exchanges, sender_lower);
+        g_hash_table_steal(pending_exchanges, sender_lower);
+        g_free(sender_lower);
+
+        if (cbc) {
+            hexchat_print(ph, "Recieved key exchange for CBC mode which is not supported.");
+            goto cleanup;
+        }
+
+        if (!priv_key) {
+            hexchat_printf(ph, "Recieved a key exchange response for unknown user: %s", sender);
+            goto cleanup;
+        }
+    } else {
+        /* Regular notice */
+        g_free(sender);
+        return HEXCHAT_EAT_NONE;
+    }
+
+    if (dh1080_compute_key(priv_key, dh_pubkey, &secret_key)) {
+        keystore_store_key(sender, secret_key);
+        hexchat_printf(ph, "Stored new key for %s", sender);
+        g_free(secret_key);
+    } else {
+        hexchat_print(ph, "Failed to create secret key!");
+    }
+
+cleanup:
+    g_free(sender);
+    g_free(priv_key);
+    return HEXCHAT_EAT_ALL;
+}
+
 /**
  * Command handler for /setkey
  */
 static int handle_setkey(char *word[], char *word_eol[], void *userdata) {
     const char *nick;
     const char *key;
-    
+
     /* Check syntax */
     if (*word[2] == '\0') {
         hexchat_printf(ph, "%s\n", usage_setkey);
         return HEXCHAT_EAT_HEXCHAT;
     }
-    
+
     if (*word[3] == '\0') {
         /* /setkey password */
         nick = hexchat_get_info(ph, "channel");
@@ -213,14 +326,14 @@ static int handle_setkey(char *word[], char *word_eol[], void *userdata) {
         nick = word[2];
         key = word_eol[3];
     }
-    
+
     /* Set password */
     if (keystore_store_key(nick, key)) {
         hexchat_printf(ph, "Stored key for %s\n", nick);
     } else {
         hexchat_printf(ph, "\00305Failed to store key in addon_fishlim.conf\n");
     }
-    
+
     return HEXCHAT_EAT_HEXCHAT;
 }
 
@@ -229,25 +342,62 @@ static int handle_setkey(char *word[], char *word_eol[], void *userdata) {
  */
 static int handle_delkey(char *word[], char *word_eol[], void *userdata) {
     const char *nick;
-    
+
     /* Check syntax */
     if (*word[2] == '\0' || *word[3] != '\0') {
         hexchat_printf(ph, "%s\n", usage_delkey);
         return HEXCHAT_EAT_HEXCHAT;
     }
-    
+
     nick = g_strstrip (word_eol[2]);
-    
+
     /* Delete the given nick from the key store */
     if (keystore_delete_nick(nick)) {
         hexchat_printf(ph, "Deleted key for %s\n", nick);
     } else {
         hexchat_printf(ph, "\00305Failed to delete key in addon_fishlim.conf!\n");
     }
-    
+
     return HEXCHAT_EAT_HEXCHAT;
 }
 
+static int handle_keyx(char *word[], char *word_eol[], void *userdata) {
+    const char *target = word[2];
+    hexchat_context *query_ctx = NULL;
+    char *pub_key, *priv_key;
+    int ctx_type;
+
+    if (*target)
+        query_ctx = find_context_on_network(target);
+    else {
+        target = hexchat_get_info(ph, "channel");
+        query_ctx = hexchat_get_context (ph);
+    }
+
+    if (query_ctx) {
+        hexchat_set_context(ph, query_ctx);
+        ctx_type = hexchat_list_int(ph, NULL, "type");
+    }
+
+    if ((query_ctx && ctx_type != 3) || (!query_ctx && !irc_is_query(target))) {
+        hexchat_print(ph, "You can only exchange keys with individuals");
+        return HEXCHAT_EAT_ALL;
+    }
+
+    if (dh1080_generate_key(&priv_key, &pub_key)) {
+        g_hash_table_replace (pending_exchanges, g_ascii_strdown(target, -1), priv_key);
+
+        hexchat_commandf(ph, "quote NOTICE %s :DH1080_INIT %s", target, pub_key);
+        hexchat_printf(ph, "Sent public key to %s, waiting for reply...", target);
+
+        g_free(pub_key);
+    } else {
+        hexchat_print(ph, "Failed to generate keys");
+    }
+
+    return HEXCHAT_EAT_ALL;
+}
+
 /**
  * Returns the plugin name version information.
  */
@@ -267,30 +417,40 @@ int hexchat_plugin_init(hexchat_plugin *plugin_handle,
                       const char **version,
                       char *arg) {
     ph = plugin_handle;
-    
+
     /* Send our info to HexChat */
     *name = plugin_name;
     *desc = plugin_desc;
     *version = plugin_version;
-    
+
     /* Register commands */
     hexchat_hook_command(ph, "SETKEY", HEXCHAT_PRI_NORM, handle_setkey, usage_setkey, NULL);
     hexchat_hook_command(ph, "DELKEY", HEXCHAT_PRI_NORM, handle_delkey, usage_delkey, NULL);
-    
+    hexchat_hook_command(ph, "KEYX", HEXCHAT_PRI_NORM, handle_keyx, usage_keyx, NULL);
+
     /* Add handlers */
     hexchat_hook_command(ph, "", HEXCHAT_PRI_NORM, handle_outgoing, NULL, NULL);
+    hexchat_hook_server(ph, "NOTICE", HEXCHAT_PRI_HIGHEST, handle_keyx_notice, NULL);
     hexchat_hook_server_attrs(ph, "NOTICE", HEXCHAT_PRI_NORM, handle_incoming, NULL);
     hexchat_hook_server_attrs(ph, "PRIVMSG", HEXCHAT_PRI_NORM, handle_incoming, NULL);
     /* hexchat_hook_server(ph, "RAW LINE", HEXCHAT_PRI_NORM, handle_debug, NULL); */
     hexchat_hook_server_attrs(ph, "TOPIC", HEXCHAT_PRI_NORM, handle_incoming, NULL);
     hexchat_hook_server_attrs(ph, "332", HEXCHAT_PRI_NORM, handle_incoming, NULL);
-    
+
+    if (!dh1080_init())
+        return 0;
+
+    pending_exchanges = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+
     hexchat_printf(ph, "%s plugin loaded\n", plugin_name);
     /* Return success */
     return 1;
 }
 
 int hexchat_plugin_deinit(void) {
+    g_clear_pointer(&pending_exchanges, g_hash_table_destroy);
+    dh1080_deinit();
+
     hexchat_printf(ph, "%s plugin unloaded\n", plugin_name);
     return 1;
 }