Переглянути джерело

* Add cmd_keyx to initiate DH1080 key exchange

Bryan Drewery 15 роки тому
батько
коміт
aa55435a97
5 змінених файлів з 67 додано та 0 видалено
  1. 3 0
      doc/UPDATES
  2. 3 0
      doc/help.txt
  3. 5 0
      src/chanprog.c
  4. 29 0
      src/mod/server.mod/cmdsserv.c
  5. 27 0
      src/mod/server.mod/servmsg.c

+ 3 - 0
doc/UPDATES

@@ -1,3 +1,6 @@
+* FiSH message support added.
+* FiSH support for DH1080 key-exchange. 'keyx' command added to start from bot, and responds to key-exchanges.
+
 1.3.2 - http://wraith.botpack.net/milestone/1.3.2
   * Add an extra 2 second delay before releasing nick to aide in syncing and time to type /nick
   * When a nick is released, start regaining as soon as it is witnessed to have been taken again

+ 3 - 0
doc/help.txt

@@ -1191,6 +1191,9 @@ See also: console, invite
    Jumping servers ALWAYS makes the bot lose ops! be careful!
  
 See also: botjump, servers, botserver
+:leaf:keyx
+### $bkeyx$b <nick>
+    Initiaite DH1080 key-exchange with nick for FiSH protocol.
 :leaf:kick
 ###  $bkick$b [channel|*] <nickname> [reason]
    Will kick a user off your current console channel (or specified

+ 5 - 0
src/chanprog.c

@@ -968,6 +968,11 @@ void privmsg(bd::String target, bd::String msg, int idx) {
   struct chanset_t* chan = NULL;
   if (have_cprivmsg && !strchr(CHANMETA, target[0]))
     chan = find_common_opped_chan(target);
+
+  // Encrypt with FiSH?
+  if (!strchr(CHANMETA, target[0]) && FishKeys.contains(target) && FishKeys[target]->sharedKey.length()) {
+    msg = "+OK " + egg_bf_encrypt(msg, FishKeys[target]->sharedKey);
+  }
   if (chan)
     dprintf(idx, "CPRIVMSG %s %s :%s\n", target.c_str(), chan->name, msg.c_str());
   else

+ 29 - 0
src/mod/server.mod/cmdsserv.c

@@ -105,6 +105,34 @@ static void cmd_jump(int idx, char *par)
   cycle_time = 0;
 }
 
+static void cmd_keyx(int idx, char *par) {
+  putlog(LOG_CMDS, "*", "#%s# keyx %s", dcc[idx].nick, par);
+
+  if (!par[0]) {
+    dprintf(idx, "Usage: keyx <nick>\n");
+    return;
+  }
+
+  if (!server_online) {
+    dprintf(idx, "Error: Not online.\n");
+    return;
+  }
+
+  char *nick = newsplit(&par);
+  bd::String myPublicKeyB64, myPrivateKey, sharedKey;
+
+  DH1080_gen(myPrivateKey, myPublicKeyB64);
+
+  putlog(LOG_MSGS, "*", "[FiSH] Initiating DH1080 key-exchange with %s - sending my public key", nick);
+  notice(nick, "DH1080_INIT " + myPublicKeyB64, DP_HELP);
+  fish_data_t* fishData = new fish_data_t;
+  fishData->myPublicKeyB64 = myPublicKeyB64;
+  fishData->myPrivateKey = myPrivateKey;
+  fishData->timestamp = now;
+  FishKeys[nick] = fishData;
+  return;
+}
+
 static void cmd_clearqueue(int idx, char *par)
 {
   int msgs;
@@ -167,6 +195,7 @@ static cmd_t C_dcc_serv[] =
   {"clearqueue",	"m",	(Function) cmd_clearqueue,	NULL, LEAF|AUTH},
   {"dump",		"a",	(Function) cmd_dump,		NULL, LEAF},
   {"jump",		"m",	(Function) cmd_jump,		NULL, LEAF},
+  {"keyx",		"o",	(Function) cmd_keyx,		NULL, LEAF},
   {"servers",		"m",	(Function) cmd_servers,		NULL, LEAF},
   {"umode",		"m",	(Function) cmd_umode,		NULL, LEAF},
   {NULL,		NULL,	NULL,				NULL, 0}

+ 27 - 0
src/mod/server.mod/servmsg.c

@@ -789,6 +789,26 @@ void handle_DH1080_init(const char* nick, const char* uhost, const char* from, s
   return;
 }
 
+void handle_DH1080_finish(const char* nick, const char* uhost, const char* from, struct userrec* u, const bd::String theirPublicKeyB64) {
+  if (!FishKeys.contains(nick)) {
+    putlog(LOG_MSGS, "*", "[FiSH] Unexpected DH1080_FINISH from (%s!%s) - ignoring", nick, uhost);
+    return;
+  }
+
+  fish_data_t* fishData = FishKeys[nick];
+  bd::String sharedKey;
+
+  if (!DH1080_comp(fishData->myPrivateKey, theirPublicKeyB64, sharedKey)) {
+    sdprintf("Error computing DH1080 for %s: %s", nick, sharedKey.c_str());
+    return;
+  }
+
+  putlog(LOG_MSGS, "*", "[FiSH] Key successfully set for (%s!%s)", nick, uhost);
+  fishData->sharedKey = sharedKey;
+  sdprintf("Set key for %s: %s", nick, sharedKey.c_str());
+  return;
+}
+
 /* Got a private notice.
  */
 static int gotnotice(char *from, char *msg)
@@ -875,6 +895,13 @@ static int gotnotice(char *from, char *msg)
             theirPublicKeyB64.resize(180, 0);
           }
           handle_DH1080_init(nick, uhost, from, u, theirPublicKeyB64);
+        } else if (!strncmp(msg, "DH1080_FINISH ", 14)) {
+          bd::String theirPublicKeyB64(msg + 14);
+          // Some FiSH implementations improperly encode their NULL terminator (A) on the end, just trim it off.
+          if (theirPublicKeyB64(-1, 1) == 'A' && theirPublicKeyB64.length() == 181) {
+            theirPublicKeyB64.resize(180, 0);
+          }
+          handle_DH1080_finish(nick, uhost, from, u, theirPublicKeyB64);
         } else {
           putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
         }