Index: lib/Makefile
===================================================================
--- lib/Makefile	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/Makefile	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -13,5 +13,5 @@
 
 # [SH] Program variables
-objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
+objects = arc.o base64.o $(DES) $(EVENT_HANDLER) ftutil.o http_client.o ini.o md5.o misc.o oauth.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
 
 LFLAGS += -r
Index: lib/des.c
===================================================================
--- lib/des.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
+++ lib/des.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -0,0 +1,646 @@
+/*
+ *  FIPS-46-3 compliant 3DES implementation
+ *
+ *  Copyright (C) 2001-2003  Christophe Devine
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+/*
+ * Modified for BitlBee: Added a function compatible with the existing
+ * function in ssl_openssl.c, fairly specialised for MSN auth (since that's
+ * all this is used for at least for now).
+ *
+ * Added some consts to the tables at the top, and disabled some 64-bit
+ * and 128-bit key code that I don't need.
+ *
+ * *Many* thanks to Christophe for this compact and easy to import code.
+ */
+
+#include <string.h>
+#include <glib.h>
+#include "des.h"
+
+/* the eight DES S-boxes */
+
+static const uint32_t SB1[64] =
+{
+    0x01010400, 0x00000000, 0x00010000, 0x01010404,
+    0x01010004, 0x00010404, 0x00000004, 0x00010000,
+    0x00000400, 0x01010400, 0x01010404, 0x00000400,
+    0x01000404, 0x01010004, 0x01000000, 0x00000004,
+    0x00000404, 0x01000400, 0x01000400, 0x00010400,
+    0x00010400, 0x01010000, 0x01010000, 0x01000404,
+    0x00010004, 0x01000004, 0x01000004, 0x00010004,
+    0x00000000, 0x00000404, 0x00010404, 0x01000000,
+    0x00010000, 0x01010404, 0x00000004, 0x01010000,
+    0x01010400, 0x01000000, 0x01000000, 0x00000400,
+    0x01010004, 0x00010000, 0x00010400, 0x01000004,
+    0x00000400, 0x00000004, 0x01000404, 0x00010404,
+    0x01010404, 0x00010004, 0x01010000, 0x01000404,
+    0x01000004, 0x00000404, 0x00010404, 0x01010400,
+    0x00000404, 0x01000400, 0x01000400, 0x00000000,
+    0x00010004, 0x00010400, 0x00000000, 0x01010004
+};
+
+static const uint32_t SB2[64] =
+{
+    0x80108020, 0x80008000, 0x00008000, 0x00108020,
+    0x00100000, 0x00000020, 0x80100020, 0x80008020,
+    0x80000020, 0x80108020, 0x80108000, 0x80000000,
+    0x80008000, 0x00100000, 0x00000020, 0x80100020,
+    0x00108000, 0x00100020, 0x80008020, 0x00000000,
+    0x80000000, 0x00008000, 0x00108020, 0x80100000,
+    0x00100020, 0x80000020, 0x00000000, 0x00108000,
+    0x00008020, 0x80108000, 0x80100000, 0x00008020,
+    0x00000000, 0x00108020, 0x80100020, 0x00100000,
+    0x80008020, 0x80100000, 0x80108000, 0x00008000,
+    0x80100000, 0x80008000, 0x00000020, 0x80108020,
+    0x00108020, 0x00000020, 0x00008000, 0x80000000,
+    0x00008020, 0x80108000, 0x00100000, 0x80000020,
+    0x00100020, 0x80008020, 0x80000020, 0x00100020,
+    0x00108000, 0x00000000, 0x80008000, 0x00008020,
+    0x80000000, 0x80100020, 0x80108020, 0x00108000
+};
+
+static const uint32_t SB3[64] =
+{
+    0x00000208, 0x08020200, 0x00000000, 0x08020008,
+    0x08000200, 0x00000000, 0x00020208, 0x08000200,
+    0x00020008, 0x08000008, 0x08000008, 0x00020000,
+    0x08020208, 0x00020008, 0x08020000, 0x00000208,
+    0x08000000, 0x00000008, 0x08020200, 0x00000200,
+    0x00020200, 0x08020000, 0x08020008, 0x00020208,
+    0x08000208, 0x00020200, 0x00020000, 0x08000208,
+    0x00000008, 0x08020208, 0x00000200, 0x08000000,
+    0x08020200, 0x08000000, 0x00020008, 0x00000208,
+    0x00020000, 0x08020200, 0x08000200, 0x00000000,
+    0x00000200, 0x00020008, 0x08020208, 0x08000200,
+    0x08000008, 0x00000200, 0x00000000, 0x08020008,
+    0x08000208, 0x00020000, 0x08000000, 0x08020208,
+    0x00000008, 0x00020208, 0x00020200, 0x08000008,
+    0x08020000, 0x08000208, 0x00000208, 0x08020000,
+    0x00020208, 0x00000008, 0x08020008, 0x00020200
+};
+
+static const uint32_t SB4[64] =
+{
+    0x00802001, 0x00002081, 0x00002081, 0x00000080,
+    0x00802080, 0x00800081, 0x00800001, 0x00002001,
+    0x00000000, 0x00802000, 0x00802000, 0x00802081,
+    0x00000081, 0x00000000, 0x00800080, 0x00800001,
+    0x00000001, 0x00002000, 0x00800000, 0x00802001,
+    0x00000080, 0x00800000, 0x00002001, 0x00002080,
+    0x00800081, 0x00000001, 0x00002080, 0x00800080,
+    0x00002000, 0x00802080, 0x00802081, 0x00000081,
+    0x00800080, 0x00800001, 0x00802000, 0x00802081,
+    0x00000081, 0x00000000, 0x00000000, 0x00802000,
+    0x00002080, 0x00800080, 0x00800081, 0x00000001,
+    0x00802001, 0x00002081, 0x00002081, 0x00000080,
+    0x00802081, 0x00000081, 0x00000001, 0x00002000,
+    0x00800001, 0x00002001, 0x00802080, 0x00800081,
+    0x00002001, 0x00002080, 0x00800000, 0x00802001,
+    0x00000080, 0x00800000, 0x00002000, 0x00802080
+};
+
+static const uint32_t SB5[64] =
+{
+    0x00000100, 0x02080100, 0x02080000, 0x42000100,
+    0x00080000, 0x00000100, 0x40000000, 0x02080000,
+    0x40080100, 0x00080000, 0x02000100, 0x40080100,
+    0x42000100, 0x42080000, 0x00080100, 0x40000000,
+    0x02000000, 0x40080000, 0x40080000, 0x00000000,
+    0x40000100, 0x42080100, 0x42080100, 0x02000100,
+    0x42080000, 0x40000100, 0x00000000, 0x42000000,
+    0x02080100, 0x02000000, 0x42000000, 0x00080100,
+    0x00080000, 0x42000100, 0x00000100, 0x02000000,
+    0x40000000, 0x02080000, 0x42000100, 0x40080100,
+    0x02000100, 0x40000000, 0x42080000, 0x02080100,
+    0x40080100, 0x00000100, 0x02000000, 0x42080000,
+    0x42080100, 0x00080100, 0x42000000, 0x42080100,
+    0x02080000, 0x00000000, 0x40080000, 0x42000000,
+    0x00080100, 0x02000100, 0x40000100, 0x00080000,
+    0x00000000, 0x40080000, 0x02080100, 0x40000100
+};
+
+static const uint32_t SB6[64] =
+{
+    0x20000010, 0x20400000, 0x00004000, 0x20404010,
+    0x20400000, 0x00000010, 0x20404010, 0x00400000,
+    0x20004000, 0x00404010, 0x00400000, 0x20000010,
+    0x00400010, 0x20004000, 0x20000000, 0x00004010,
+    0x00000000, 0x00400010, 0x20004010, 0x00004000,
+    0x00404000, 0x20004010, 0x00000010, 0x20400010,
+    0x20400010, 0x00000000, 0x00404010, 0x20404000,
+    0x00004010, 0x00404000, 0x20404000, 0x20000000,
+    0x20004000, 0x00000010, 0x20400010, 0x00404000,
+    0x20404010, 0x00400000, 0x00004010, 0x20000010,
+    0x00400000, 0x20004000, 0x20000000, 0x00004010,
+    0x20000010, 0x20404010, 0x00404000, 0x20400000,
+    0x00404010, 0x20404000, 0x00000000, 0x20400010,
+    0x00000010, 0x00004000, 0x20400000, 0x00404010,
+    0x00004000, 0x00400010, 0x20004010, 0x00000000,
+    0x20404000, 0x20000000, 0x00400010, 0x20004010
+};
+
+static const uint32_t SB7[64] =
+{
+    0x00200000, 0x04200002, 0x04000802, 0x00000000,
+    0x00000800, 0x04000802, 0x00200802, 0x04200800,
+    0x04200802, 0x00200000, 0x00000000, 0x04000002,
+    0x00000002, 0x04000000, 0x04200002, 0x00000802,
+    0x04000800, 0x00200802, 0x00200002, 0x04000800,
+    0x04000002, 0x04200000, 0x04200800, 0x00200002,
+    0x04200000, 0x00000800, 0x00000802, 0x04200802,
+    0x00200800, 0x00000002, 0x04000000, 0x00200800,
+    0x04000000, 0x00200800, 0x00200000, 0x04000802,
+    0x04000802, 0x04200002, 0x04200002, 0x00000002,
+    0x00200002, 0x04000000, 0x04000800, 0x00200000,
+    0x04200800, 0x00000802, 0x00200802, 0x04200800,
+    0x00000802, 0x04000002, 0x04200802, 0x04200000,
+    0x00200800, 0x00000000, 0x00000002, 0x04200802,
+    0x00000000, 0x00200802, 0x04200000, 0x00000800,
+    0x04000002, 0x04000800, 0x00000800, 0x00200002
+};
+
+static const uint32_t SB8[64] =
+{
+    0x10001040, 0x00001000, 0x00040000, 0x10041040,
+    0x10000000, 0x10001040, 0x00000040, 0x10000000,
+    0x00040040, 0x10040000, 0x10041040, 0x00041000,
+    0x10041000, 0x00041040, 0x00001000, 0x00000040,
+    0x10040000, 0x10000040, 0x10001000, 0x00001040,
+    0x00041000, 0x00040040, 0x10040040, 0x10041000,
+    0x00001040, 0x00000000, 0x00000000, 0x10040040,
+    0x10000040, 0x10001000, 0x00041040, 0x00040000,
+    0x00041040, 0x00040000, 0x10041000, 0x00001000,
+    0x00000040, 0x10040040, 0x00001000, 0x00041040,
+    0x10001000, 0x00000040, 0x10000040, 0x10040000,
+    0x10040040, 0x10000000, 0x00040000, 0x10001040,
+    0x00000000, 0x10041040, 0x00040040, 0x10000040,
+    0x10040000, 0x10001000, 0x10001040, 0x00000000,
+    0x10041040, 0x00041000, 0x00041000, 0x00001040,
+    0x00001040, 0x00040040, 0x10000000, 0x10041000
+};
+
+/* PC1: left and right halves bit-swap */
+
+static const uint32_t LHs[16] =
+{
+    0x00000000, 0x00000001, 0x00000100, 0x00000101,
+    0x00010000, 0x00010001, 0x00010100, 0x00010101,
+    0x01000000, 0x01000001, 0x01000100, 0x01000101,
+    0x01010000, 0x01010001, 0x01010100, 0x01010101
+};
+
+static const uint32_t RHs[16] =
+{
+    0x00000000, 0x01000000, 0x00010000, 0x01010000,
+    0x00000100, 0x01000100, 0x00010100, 0x01010100,
+    0x00000001, 0x01000001, 0x00010001, 0x01010001,
+    0x00000101, 0x01000101, 0x00010101, 0x01010101,
+};
+
+/* platform-independant 32-bit integer manipulation macros */
+
+#define GET_UINT32(n,b,i)                         \
+{                                                 \
+    (n) = ( (uint32_t) (b)[(i)    ] << 24 )       \
+        | ( (uint32_t) (b)[(i) + 1] << 16 )       \
+        | ( (uint32_t) (b)[(i) + 2] <<  8 )       \
+        | ( (uint32_t) (b)[(i) + 3]       );      \
+}
+
+#define PUT_UINT32(n,b,i)                         \
+{                                                 \
+    (b)[(i)    ] = (uint8_t) ( (n) >> 24 );       \
+    (b)[(i) + 1] = (uint8_t) ( (n) >> 16 );       \
+    (b)[(i) + 2] = (uint8_t) ( (n) >>  8 );       \
+    (b)[(i) + 3] = (uint8_t) ( (n)       );       \
+}
+
+/* Initial Permutation macro */
+
+#define DES_IP(X,Y)                                             \
+{                                                               \
+    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
+    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
+    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
+    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
+    Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF;                    \
+    T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T;                   \
+    X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF;                    \
+}
+
+/* Final Permutation macro */
+
+#define DES_FP(X,Y)                                             \
+{                                                               \
+    X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF;                    \
+    T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T;                   \
+    Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF;                    \
+    T = ((Y >>  8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T <<  8);   \
+    T = ((Y >>  2) ^ X) & 0x33333333; X ^= T; Y ^= (T <<  2);   \
+    T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16);   \
+    T = ((X >>  4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T <<  4);   \
+}
+
+/* DES round macro */
+
+#define DES_ROUND(X,Y)                          \
+{                                               \
+    T = *SK++ ^ X;                              \
+    Y ^= SB8[ (T      ) & 0x3F ] ^              \
+         SB6[ (T >>  8) & 0x3F ] ^              \
+         SB4[ (T >> 16) & 0x3F ] ^              \
+         SB2[ (T >> 24) & 0x3F ];               \
+                                                \
+    T = *SK++ ^ ((X << 28) | (X >> 4));         \
+    Y ^= SB7[ (T      ) & 0x3F ] ^              \
+         SB5[ (T >>  8) & 0x3F ] ^              \
+         SB3[ (T >> 16) & 0x3F ] ^              \
+         SB1[ (T >> 24) & 0x3F ];               \
+}
+
+/* DES key schedule */
+
+int des_main_ks( uint32_t SK[32], const uint8_t key[8] )
+{
+    int i;
+    uint32_t X, Y, T;
+
+    GET_UINT32( X, key, 0 );
+    GET_UINT32( Y, key, 4 );
+
+    /* Permuted Choice 1 */
+
+    T =  ((Y >>  4) ^ X) & 0x0F0F0F0F;  X ^= T; Y ^= (T <<  4);
+    T =  ((Y      ) ^ X) & 0x10101010;  X ^= T; Y ^= (T      );
+
+    X =   (LHs[ (X      ) & 0xF] << 3) | (LHs[ (X >>  8) & 0xF ] << 2)
+        | (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ]     )
+        | (LHs[ (X >>  5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
+        | (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
+
+    Y =   (RHs[ (Y >>  1) & 0xF] << 3) | (RHs[ (Y >>  9) & 0xF ] << 2)
+        | (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ]     )
+        | (RHs[ (Y >>  4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
+        | (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
+
+    X &= 0x0FFFFFFF;
+    Y &= 0x0FFFFFFF;
+
+    /* calculate subkeys */
+
+    for( i = 0; i < 16; i++ )
+    {
+        if( i < 2 || i == 8 || i == 15 )
+        {
+            X = ((X <<  1) | (X >> 27)) & 0x0FFFFFFF;
+            Y = ((Y <<  1) | (Y >> 27)) & 0x0FFFFFFF;
+        }
+        else
+        {
+            X = ((X <<  2) | (X >> 26)) & 0x0FFFFFFF;
+            Y = ((Y <<  2) | (Y >> 26)) & 0x0FFFFFFF;
+        }
+
+        *SK++ =   ((X <<  4) & 0x24000000) | ((X << 28) & 0x10000000)
+                | ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
+                | ((X <<  6) & 0x01000000) | ((X <<  9) & 0x00200000)
+                | ((X >>  1) & 0x00100000) | ((X << 10) & 0x00040000)
+                | ((X <<  2) & 0x00020000) | ((X >> 10) & 0x00010000)
+                | ((Y >> 13) & 0x00002000) | ((Y >>  4) & 0x00001000)
+                | ((Y <<  6) & 0x00000800) | ((Y >>  1) & 0x00000400)
+                | ((Y >> 14) & 0x00000200) | ((Y      ) & 0x00000100)
+                | ((Y >>  5) & 0x00000020) | ((Y >> 10) & 0x00000010)
+                | ((Y >>  3) & 0x00000008) | ((Y >> 18) & 0x00000004)
+                | ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
+
+        *SK++ =   ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
+                | ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
+                | ((X >>  2) & 0x02000000) | ((X <<  1) & 0x01000000)
+                | ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
+                | ((X <<  3) & 0x00080000) | ((X >>  6) & 0x00040000)
+                | ((X << 15) & 0x00020000) | ((X >>  4) & 0x00010000)
+                | ((Y >>  2) & 0x00002000) | ((Y <<  8) & 0x00001000)
+                | ((Y >> 14) & 0x00000808) | ((Y >>  9) & 0x00000400)
+                | ((Y      ) & 0x00000200) | ((Y <<  7) & 0x00000100)
+                | ((Y >>  7) & 0x00000020) | ((Y >>  3) & 0x00000011)
+                | ((Y <<  2) & 0x00000004) | ((Y >> 21) & 0x00000002);
+    }
+
+    return( 0 );
+}
+
+#if TEST
+int des_set_key( des_context *ctx, uint8_t key[8] )
+{
+    int i;
+
+    /* setup encryption subkeys */
+
+    des_main_ks( ctx->esk, key );
+
+    /* setup decryption subkeys */
+
+    for( i = 0; i < 32; i += 2 )
+    {
+        ctx->dsk[i    ] = ctx->esk[30 - i];
+        ctx->dsk[i + 1] = ctx->esk[31 - i];
+    }
+
+    return( 0 );
+}
+
+/* DES 64-bit block encryption/decryption */
+
+void des_crypt( uint32_t SK[32], uint8_t input[8], uint8_t output[8] )
+{
+    uint32_t X, Y, T;
+
+    GET_UINT32( X, input, 0 );
+    GET_UINT32( Y, input, 4 );
+
+    DES_IP( X, Y );
+
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+
+    DES_FP( Y, X );
+
+    PUT_UINT32( Y, output, 0 );
+    PUT_UINT32( X, output, 4 );
+}
+
+void des_encrypt( des_context *ctx, uint8_t input[8], uint8_t output[8] )
+{
+    des_crypt( ctx->esk, input, output );
+}
+
+void des_decrypt( des_context *ctx, uint8_t input[8], uint8_t output[8] )
+{
+    des_crypt( ctx->dsk, input, output );
+}
+
+/* Triple-DES key schedule */
+
+int des3_set_2keys( des3_context *ctx, const uint8_t key1[8], const uint8_t key2[8] )
+{
+    int i;
+
+    des_main_ks( ctx->esk     , key1 );
+    des_main_ks( ctx->dsk + 32, key2 );
+
+    for( i = 0; i < 32; i += 2 )
+    {
+        ctx->dsk[i     ] = ctx->esk[30 - i];
+        ctx->dsk[i +  1] = ctx->esk[31 - i];
+
+        ctx->esk[i + 32] = ctx->dsk[62 - i];
+        ctx->esk[i + 33] = ctx->dsk[63 - i];
+
+        ctx->esk[i + 64] = ctx->esk[     i];
+        ctx->esk[i + 65] = ctx->esk[ 1 + i];
+
+        ctx->dsk[i + 64] = ctx->dsk[     i];
+        ctx->dsk[i + 65] = ctx->dsk[ 1 + i];
+    }
+
+    return( 0 );
+}
+#endif
+
+int des3_set_3keys( des3_context *ctx, const uint8_t key1[8], const uint8_t key2[8],
+                                       const uint8_t key3[8] )
+{
+    int i;
+
+    des_main_ks( ctx->esk     , key1 );
+    des_main_ks( ctx->dsk + 32, key2 );
+    des_main_ks( ctx->esk + 64, key3 );
+
+    for( i = 0; i < 32; i += 2 )
+    {
+        ctx->dsk[i     ] = ctx->esk[94 - i];
+        ctx->dsk[i +  1] = ctx->esk[95 - i];
+
+        ctx->esk[i + 32] = ctx->dsk[62 - i];
+        ctx->esk[i + 33] = ctx->dsk[63 - i];
+
+        ctx->dsk[i + 64] = ctx->esk[30 - i];
+        ctx->dsk[i + 65] = ctx->esk[31 - i];
+    }
+
+    return( 0 );
+}
+
+/* Triple-DES 64-bit block encryption/decryption */
+
+void des3_crypt( uint32_t SK[96], uint8_t input[8], uint8_t output[8] )
+{
+    uint32_t X, Y, T;
+
+    GET_UINT32( X, input, 0 );
+    GET_UINT32( Y, input, 4 );
+
+    DES_IP( X, Y );
+
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+    DES_ROUND( X, Y );  DES_ROUND( Y, X );
+
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+    DES_ROUND( Y, X );  DES_ROUND( X, Y );
+
+    DES_FP( Y, X );
+
+    PUT_UINT32( Y, output, 0 );
+    PUT_UINT32( X, output, 4 );
+}
+
+void des3_encrypt( des3_context *ctx, uint8_t input[8], uint8_t output[8] )
+{
+    des3_crypt( ctx->esk, input, output );
+}
+
+void des3_decrypt( des3_context *ctx, uint8_t input[8], uint8_t output[8] )
+{
+    des3_crypt( ctx->dsk, input, output );
+}
+
+size_t ssl_des3_encrypt( const unsigned char *key, size_t key_len, const unsigned char *input,
+                         size_t input_len, const unsigned char *iv, unsigned char **res )
+{
+	des3_context ctx3;
+	size_t off;
+	uint8_t buf[8];
+		
+	/* Keep it simple, for as long as this is just used for MSN auth anyway. */
+	if( key_len != 24 || ( input_len % 8 ) != 0 )
+		return 0;
+	
+	*res = g_malloc( input_len );
+	des3_set_3keys( &ctx3, key, key + 8, key + 16 );
+	
+	/* This loop does CBC 3DES. */
+	memcpy( buf, iv, 8 );
+	for( off = 0; off < input_len; off += 8 )
+	{
+		int i;
+		
+		for( i = 0; i < 8; i ++ )
+			buf[i] ^= input[off+i];
+		des3_encrypt( &ctx3, buf, buf );
+		memcpy( *res + off, buf, 8 );
+	}
+	
+	return input_len;
+}
+
+#ifdef TEST
+
+#include <string.h>
+#include <stdio.h>
+
+/*
+ * Triple-DES Monte Carlo Test: ECB mode
+ * source: NIST - tripledes-vectors.zip
+ */
+
+static const unsigned char DES3_keys[3][8] =
+{
+    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
+    { 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01 },
+    { 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23 }
+};
+
+static const unsigned char DES3_init[8] =
+{
+    0x4E, 0x6F, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74
+};
+
+static const unsigned char DES3_enc_test[3][8] =
+{
+    { 0x6A, 0x2A, 0x19, 0xF4, 0x1E, 0xCA, 0x85, 0x4B },
+    { 0x03, 0xE6, 0x9F, 0x5B, 0xFA, 0x58, 0xEB, 0x42 },
+    { 0xDD, 0x17, 0xE8, 0xB8, 0xB4, 0x37, 0xD2, 0x32 }
+};
+   
+static const unsigned char DES3_dec_test[3][8] =
+{
+    { 0xCD, 0xD6, 0x4F, 0x2F, 0x94, 0x27, 0xC1, 0x5D },
+    { 0x69, 0x96, 0xC8, 0xFA, 0x47, 0xA2, 0xAB, 0xEB },
+    { 0x83, 0x25, 0x39, 0x76, 0x44, 0x09, 0x1A, 0x0A }
+};
+
+int main( void )
+{
+    int m, n, i;
+    des_context ctx;
+    des3_context ctx3;
+    unsigned char buf[8];
+
+    for( m = 0; m < 2; m++ )
+    {
+        printf( "\n Triple-DES Monte Carlo Test (ECB mode) - " );
+
+        if( m == 0 ) printf( "encryption\n\n" );
+        if( m == 1 ) printf( "decryption\n\n" );
+
+        for( n = 0; n < 3; n++ )
+        {
+            printf( " Test %d, key size = %3d bits: ",
+                    n + 1, 64 + n * 64 );
+
+            fflush( stdout );
+
+            memcpy( buf, DES3_init, 8 );
+
+            switch( n )
+            {
+                case 0:
+                    des_set_key( &ctx, DES3_keys[0] );
+                    break;
+
+                case 1:
+                    des3_set_2keys( &ctx3, DES3_keys[0],
+                                           DES3_keys[1] );
+                    break;
+
+                case 2:
+                    des3_set_3keys( &ctx3, DES3_keys[0],
+                                           DES3_keys[1],
+                                           DES3_keys[2] );
+                    break;
+            }
+
+            for( i = 0; i < 10000; i++ )
+            {
+                if( n == 0 )
+                {
+                    if( m == 0 ) des_encrypt( &ctx, buf, buf );
+                    if( m == 1 ) des_decrypt( &ctx, buf, buf );
+                }
+                else
+                {
+                    if( m == 0 ) des3_encrypt( &ctx3, buf, buf );
+                    if( m == 1 ) des3_decrypt( &ctx3, buf, buf );
+                }
+            }
+
+            if( ( m == 0 && memcmp( buf, DES3_enc_test[n], 8 ) ) ||
+                ( m == 1 && memcmp( buf, DES3_dec_test[n], 8 ) ) )
+            {
+                printf( "failed!\n" );
+                return( 1 );
+            }
+
+            printf( "passed.\n" );
+        }
+    }
+
+    printf( "\n" );
+
+    return( 0 );
+}
+
+#endif
Index: lib/des.h
===================================================================
--- lib/des.h	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
+++ lib/des.h	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -0,0 +1,51 @@
+/*
+ *  FIPS-46-3 compliant 3DES implementation
+ *
+ *  Copyright (C) 2001-2003  Christophe Devine
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef _DES_H
+#define _DES_H
+
+#include <stdint.h>
+
+typedef struct
+{
+    uint32_t esk[32];     /* DES encryption subkeys */
+    uint32_t dsk[32];     /* DES decryption subkeys */
+}
+des_context;
+
+typedef struct
+{
+    uint32_t esk[96];     /* Triple-DES encryption subkeys */
+    uint32_t dsk[96];     /* Triple-DES decryption subkeys */
+}
+des3_context;
+
+int  des_set_key( des_context *ctx, uint8_t key[8] );
+void des_encrypt( des_context *ctx, uint8_t input[8], uint8_t output[8] );
+void des_decrypt( des_context *ctx, uint8_t input[8], uint8_t output[8] );
+
+int  des3_set_2keys( des3_context *ctx, const uint8_t key1[8], const uint8_t key2[8] );
+int  des3_set_3keys( des3_context *ctx, const uint8_t key1[8], const uint8_t key2[8],
+                                        const uint8_t key3[8] );
+
+void des3_encrypt( des3_context *ctx, uint8_t input[8], uint8_t output[8] );
+void des3_decrypt( des3_context *ctx, uint8_t input[8], uint8_t output[8] );
+
+#endif /* des.h */
Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/misc.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -308,9 +308,8 @@
 void http_encode( char *s )
 {
-	char *t;
+	char t[strlen(s)+1];
 	int i, j;
 	
-	t = g_strdup( s );
-	
+	strcpy( t, s );
 	for( i = j = 0; t[i]; i ++, j ++ )
 	{
@@ -330,6 +329,4 @@
 	}
 	s[j] = 0;
-	
-	g_free( t );
 }
 
Index: lib/sha1.c
===================================================================
--- lib/sha1.c	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/sha1.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -36,4 +36,5 @@
  */
 
+#include <string.h>
 #include "sha1.h"
 
@@ -374,2 +375,48 @@
 	sha1_process_block(context);
 }
+
+#define HMAC_BLOCK_SIZE 64
+
+/* BitlBee addition: */
+void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, uint8_t Message_Digest[sha1_hash_size])
+{
+	sha1_state_t sha1;
+	uint8_t hash[sha1_hash_size];
+	uint8_t key[HMAC_BLOCK_SIZE+1];
+	int i;
+	
+	if( key_len == 0 )
+		key_len = strlen( key_ );
+	if( payload_len == 0 )
+		payload_len = strlen( payload );
+	
+	/* Create K. If our current key is >64 chars we have to hash it,
+	   otherwise just pad. */
+	memset( key, 0, HMAC_BLOCK_SIZE + 1 );
+	if( key_len > HMAC_BLOCK_SIZE )
+	{
+		sha1_init( &sha1 );
+		sha1_append( &sha1, (uint8_t*) key_, key_len );
+		sha1_finish( &sha1, key );
+	}
+	else
+	{
+		memcpy( key, key_, key_len );
+	}
+	
+	/* Inner part: H(K XOR 0x36, text) */
+	sha1_init( &sha1 );
+	for( i = 0; i < HMAC_BLOCK_SIZE; i ++ )
+		key[i] ^= 0x36;
+	sha1_append( &sha1, key, HMAC_BLOCK_SIZE );
+	sha1_append( &sha1, (const uint8_t*) payload, payload_len );
+	sha1_finish( &sha1, hash );
+	
+	/* Final result: H(K XOR 0x5C, inner stuff) */
+	sha1_init( &sha1 );
+	for( i = 0; i < HMAC_BLOCK_SIZE; i ++ )
+		key[i] ^= 0x36 ^ 0x5c;
+	sha1_append( &sha1, key, HMAC_BLOCK_SIZE );
+	sha1_append( &sha1, hash, sha1_hash_size );
+	sha1_finish( &sha1, Message_Digest );
+}
Index: lib/sha1.h
===================================================================
--- lib/sha1.h	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/sha1.h	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -67,4 +67,5 @@
 G_MODULE_EXPORT int sha1_append(sha1_state_t *, const uint8_t *, unsigned int);
 G_MODULE_EXPORT int sha1_finish(sha1_state_t *, uint8_t Message_Digest[sha1_hash_size]);
+G_MODULE_EXPORT void sha1_hmac(const char *key_, size_t key_len, const char *payload, size_t payload_len, uint8_t Message_Digest[sha1_hash_size]);
 
 #endif
Index: lib/ssl_client.h
===================================================================
--- lib/ssl_client.h	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/ssl_client.h	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -81,2 +81,4 @@
    the same action as the handler that just received the SSL_AGAIN.) */
 G_MODULE_EXPORT b_input_condition ssl_getdirection( void *conn );
+
+G_MODULE_EXPORT size_t ssl_des3_encrypt(const unsigned char *key, size_t key_len, const unsigned char *input, size_t input_len, const unsigned char *iv, unsigned char **res);
Index: lib/ssl_gnutls.c
===================================================================
--- lib/ssl_gnutls.c	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/ssl_gnutls.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -194,4 +194,6 @@
 		ssl_errno = SSL_AGAIN;
 	
+	if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) write( 1, buf, st );
+	
 	return st;
 }
@@ -212,4 +214,6 @@
 	if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
 		ssl_errno = SSL_AGAIN;
+	
+	if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) write( 1, buf, st );
 	
 	return st;
Index: lib/ssl_openssl.c
===================================================================
--- lib/ssl_openssl.c	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/ssl_openssl.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -60,5 +60,6 @@
 {
 	initialized = TRUE;
-	SSLeay_add_ssl_algorithms();
+	SSL_library_init();
+	// SSLeay_add_ssl_algorithms();
 }
 
@@ -210,4 +211,6 @@
 	}
 	
+	if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) write( 1, buf, st );
+	
 	return st;
 }
@@ -224,4 +227,6 @@
 	
 	st = SSL_write( ((struct scd*)conn)->ssl, buf, len );
+	
+	if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) write( 1, buf, st );
 	
 	ssl_errno = SSL_OK;
@@ -277,2 +282,24 @@
 	return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? B_EV_IO_WRITE : B_EV_IO_READ );
 }
+
+size_t ssl_des3_encrypt(const unsigned char *key, size_t key_len, const unsigned char *input, size_t input_len, const unsigned char *iv, unsigned char **res)
+{
+	int output_length = 0;    
+	EVP_CIPHER_CTX ctx;
+	
+	*res = g_new0(unsigned char, 72);
+	
+	/* Don't set key or IV because we will modify the parameters */
+	EVP_CIPHER_CTX_init(&ctx);
+	EVP_CipherInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, NULL, NULL, 1);
+	EVP_CIPHER_CTX_set_key_length(&ctx, key_len);
+	EVP_CIPHER_CTX_set_padding(&ctx, 0);
+	/* We finished modifying parameters so now we can set key and IV */
+	EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, 1);
+	EVP_CipherUpdate(&ctx, *res, &output_length, input, input_len);
+	EVP_CipherFinal_ex(&ctx, *res, &output_length);
+	EVP_CIPHER_CTX_cleanup(&ctx);   
+	//EVP_cleanup();
+	
+	return output_length;
+}
Index: lib/xmltree.c
===================================================================
--- lib/xmltree.c	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/xmltree.c	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -141,5 +141,5 @@
 /* Feed the parser, don't execute any handler. Returns -1 on errors, 0 on
    end-of-stream and 1 otherwise. */
-int xt_feed( struct xt_parser *xt, char *text, int text_len )
+int xt_feed( struct xt_parser *xt, const char *text, int text_len )
 {
 	if( !g_markup_parse_context_parse( xt->parser, text, text_len, &xt->gerr ) )
@@ -174,18 +174,18 @@
 	if( node->flags & XT_COMPLETE && !( node->flags & XT_SEEN ) )
 	{
-		for( i = 0; xt->handlers[i].func; i ++ )
+		if( xt->handlers ) for( i = 0; xt->handlers[i].func; i ++ )
 		{
 			/* This one is fun! \o/ */
 			
-						/* If handler.name == NULL it means it should always match. */
+			    /* If handler.name == NULL it means it should always match. */
 			if( ( xt->handlers[i].name == NULL || 
-						/* If it's not, compare. There should always be a name. */
+			      /* If it's not, compare. There should always be a name. */
 			      g_strcasecmp( xt->handlers[i].name, node->name ) == 0 ) &&
-						/* If handler.parent == NULL, it's a match. */
+			    /* If handler.parent == NULL, it's a match. */
 			    ( xt->handlers[i].parent == NULL ||
-						/* If there's a parent node, see if the name matches. */
+			      /* If there's a parent node, see if the name matches. */
 			      ( node->parent ? g_strcasecmp( xt->handlers[i].parent, node->parent->name ) == 0 : 
-						/* If there's no parent, the handler should mention <root> as a parent. */
-			                       g_strcasecmp( xt->handlers[i].parent, "<root>" ) == 0 ) ) )
+			      /* If there's no parent, the handler should mention <root> as a parent. */
+			                       strcmp( xt->handlers[i].parent, "<root>" ) == 0 ) ) )
 			{
 				st = xt->handlers[i].func( node, xt->data );
@@ -260,4 +260,18 @@
 }
 
+struct xt_node *xt_from_string( const char *in )
+{
+	struct xt_parser *parser;
+	struct xt_node *ret;
+	
+	parser = xt_new( NULL, NULL );
+	xt_feed( parser, in, strlen( in ) );
+	ret = parser->root;
+	parser->root = NULL;
+	xt_free( parser );
+	
+	return ret;
+}
+
 static void xt_to_string_real( struct xt_node *node, GString *str )
 {
@@ -317,5 +331,5 @@
 	/* Indentation */
 	for( c = node; c->parent; c = c->parent )
-		printf( "\t" );
+		printf( "    " );
 	
 	/* Start the tag */
@@ -324,5 +338,9 @@
 	/* Print the attributes */
 	for( i = 0; node->attr[i].key; i ++ )
-		printf( " %s=\"%s\"", node->attr[i].key, g_markup_escape_text( node->attr[i].value, -1 ) );
+	{
+		char *v = g_markup_escape_text( node->attr[i].value, -1 );
+		printf( " %s=\"%s\"", node->attr[i].key, v );
+		g_free( v );
+	}
 	
 	/* /> in case there's really *nothing* inside this tag, otherwise
@@ -344,5 +362,9 @@
 		for( i = 0; node->text[i] && isspace( node->text[i] ); i ++ );
 		if( node->text[i] )
-			printf( "%s", g_markup_escape_text( node->text, -1 ) );
+		{
+			char *v = g_markup_escape_text( node->text, -1 );
+			printf( "%s", v );
+			g_free( v );
+		}
 	}
 	
@@ -355,5 +377,5 @@
 	if( node->children )
 		for( c = node; c->parent; c = c->parent )
-			printf( "\t" );
+			printf( "    " );
 	
 	/* Non-empty tag is now finished. */
@@ -460,4 +482,44 @@
 		
 		node = node->next;
+	}
+	
+	return node;
+}
+
+/* More advanced than the one above, understands something like
+   ../foo/bar to find a subnode bar of a node foo which is a child
+   of node's parent. Pass the node directly, not its list of children. */
+struct xt_node *xt_find_path( struct xt_node *node, const char *name )
+{
+	while( name && *name && node )
+	{
+		char *colon, *slash;
+		int n;
+		
+		if( ( slash = strchr( name, '/' ) ) )
+			n = slash - name;
+		else
+			n = strlen( name );
+		
+		if( strncmp( name, "..", n ) == 0 )
+		{
+			node = node->parent;
+		}
+		else
+		{
+			node = node->children;
+			
+			while( node )
+			{
+				if( g_strncasecmp( node->name, name, n ) == 0 ||
+				    ( ( colon = strchr( node->name, ':' ) ) &&
+				      g_strncasecmp( colon + 1, name, n ) == 0 ) )
+					break;
+				
+				node = node->next;
+			}
+		}
+		
+		name = slash ? slash + 1 : NULL;
 	}
 	
@@ -550,4 +612,24 @@
 }
 
+/* Same, but at the beginning. */
+void xt_insert_child( struct xt_node *parent, struct xt_node *child )
+{
+	struct xt_node *node, *last;
+	
+	for( node = child; node; node = node->next )
+	{
+		if( node->parent != NULL )
+		{
+			/* ERROR CONDITION: They seem to have a parent already??? */
+		}
+		
+		node->parent = parent;
+		last = node;
+	}
+	
+	last->next = parent->children;
+	parent->children = child;
+}
+
 void xt_add_attr( struct xt_node *node, const char *key, const char *value )
 {
Index: lib/xmltree.h
===================================================================
--- lib/xmltree.h	(revision f5c0d8e4dd3ce01945a6334a5d87c89a9f43b16b)
+++ lib/xmltree.h	(revision 04f0c10b5a45d0bb1a1f8888e0eb2f6db8fc1b84)
@@ -79,7 +79,8 @@
 struct xt_parser *xt_new( const struct xt_handler_entry *handlers, gpointer data );
 void xt_reset( struct xt_parser *xt );
-int xt_feed( struct xt_parser *xt, char *text, int text_len );
+int xt_feed( struct xt_parser *xt, const char *text, int text_len );
 int xt_handle( struct xt_parser *xt, struct xt_node *node, int depth );
 void xt_cleanup( struct xt_parser *xt, struct xt_node *node, int depth );
+struct xt_node *xt_from_string( const char *in );
 char *xt_to_string( struct xt_node *node );
 void xt_print( struct xt_node *node );
@@ -88,8 +89,10 @@
 void xt_free( struct xt_parser *xt );
 struct xt_node *xt_find_node( struct xt_node *node, const char *name );
+struct xt_node *xt_find_path( struct xt_node *node, const char *name );
 char *xt_find_attr( struct xt_node *node, const char *key );
 
 struct xt_node *xt_new_node( char *name, const char *text, struct xt_node *children );
 void xt_add_child( struct xt_node *parent, struct xt_node *child );
+void xt_insert_child( struct xt_node *parent, struct xt_node *child );
 void xt_add_attr( struct xt_node *node, const char *key, const char *value );
 int xt_remove_attr( struct xt_node *node, const char *key );
