changeset 31888:56092767c298

Encrypt a message using transport-encryption.
author andrew.victor@mxit.com
date Sun, 14 Aug 2011 20:45:48 +0000
parents 1d47a7ee0b61
children 79e1479747cf
files libpurple/protocols/mxit/cipher.c libpurple/protocols/mxit/cipher.h
diffstat 2 files changed, 76 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/mxit/cipher.c	Sat Aug 13 22:11:50 2011 +0000
+++ b/libpurple/protocols/mxit/cipher.c	Sun Aug 14 20:45:48 2011 +0000
@@ -109,7 +109,7 @@
 
 
 /*------------------------------------------------------------------------
- * Decrypt a transport-layer encryptede message.
+ * Decrypt a transport-layer encrypted message.
  *
  *  @param session	The MXit session object
  *	@param message	The encrypted message data.
@@ -151,8 +151,6 @@
 	}
 	g_free( raw_message );
 
-	purple_debug_info( MXIT_PLUGIN_ID, "decrypted: '%s'\n", decoded->str );
-
 	/* check that the decrypted message starts with header: <mxit/> */
 	if ( strncmp( decoded->str, SECRET_HEADER, strlen( SECRET_HEADER ) != 0 ) ) {
 		g_string_free( decoded, TRUE );
@@ -171,3 +169,77 @@
 
 	return g_string_free( decoded, FALSE );
 }
+
+
+/*------------------------------------------------------------------------
+ * Encrypt a message using transport-layer encryption.
+ *
+ *  @param session	The MXit session object
+ *	@param message	The message data.
+ *  @return			The encrypted message.  Must be g_free'd when no longer needed.
+ */
+char* mxit_encrypt_message( struct MXitSession* session, char* message )
+{
+	char		key[64];
+	int			pwdlen		= strlen( session->acc->password );
+	char		exkey[512];
+	int			i;
+	GString*	decoded		= NULL;
+	GString*	encoded		= NULL;
+	gchar*		base64;
+
+	purple_debug_info( MXIT_PLUGIN_ID, "encrypt message: '%s'\n", message );
+
+	/* build the key - Client key, appended with last 8 characters of the PIN. (no padding) */
+	memset( key, 0x00, sizeof( key ) );
+	memcpy( key, session->clientkey, strlen( session->clientkey ) );
+	if ( pwdlen <= 8 )
+		strcat( key, session->acc->password );
+	else
+		strncat( key, session->acc->password + ( pwdlen - 8 ), 8 );
+	ExpandKey( (unsigned char*) key, (unsigned char*) exkey );
+
+	/* append encryption header */
+	decoded = g_string_sized_new( strlen( SECRET_HEADER ) + strlen( message ) );
+	g_string_append( decoded, SECRET_HEADER );
+	g_string_append( decoded, message );
+
+	/* add ISO10126 padding */
+	{
+		int blocks = ( decoded->len / 16 ) + 1;
+		int padding = ( blocks * 16 ) - decoded->len;
+
+		g_string_set_size( decoded, blocks * 16 );
+		decoded->str[decoded->len - 1] = padding;
+	}
+
+	/* encrypt each block */
+	encoded = g_string_sized_new( decoded->len );
+	for ( i = 0; i < decoded->len; i += 16 ) {
+		char	block[16];
+
+		Encrypt( (unsigned char*) decoded->str + i, (unsigned char*) exkey, (unsigned char*) block );
+		g_string_append_len( encoded, block, 16 );
+	}
+
+	/* now base64 encode the encrypted message */
+	base64 = purple_base64_encode( (unsigned char *) encoded->str, encoded->len );
+
+#if 0
+	/* and add optional header */
+	{
+		GString* tmp = g_string_sized_new( strlen( base64 ) + strlen( ENCRYPT_HEADER ) );
+		g_string_append( tmp, ENCRYPT_HEADER );
+		g_string_append( tmp, base64 );
+		g_free(base64);
+		base64 = g_string_free( tmp, FALSE );
+	}
+#endif
+
+	g_string_free( decoded, TRUE );
+	g_string_free( encoded, TRUE );
+
+	purple_debug_info( MXIT_PLUGIN_ID, "encrypted message: '%s'\n", base64 );
+
+	return base64;
+}
--- a/libpurple/protocols/mxit/cipher.h	Sat Aug 13 22:11:50 2011 +0000
+++ b/libpurple/protocols/mxit/cipher.h	Sun Aug 14 20:45:48 2011 +0000
@@ -33,6 +33,6 @@
 char* mxit_encrypt_password( struct MXitSession* session );
 
 char* mxit_decrypt_message( struct MXitSession* session, char* message );
-
+char* mxit_encrypt_message( struct MXitSession* session, char* message );
 
 #endif		/* _MXIT_CIPHER_H_ */