libpurple/protocols/mxit/cipher.c

Fri, 12 Aug 2011 21:41:34 +0000

author
Andrew Victor <andrew.victor@mxit.com>
date
Fri, 12 Aug 2011 21:41:34 +0000
branch
mxit
changeset 32165
3ed7292a69f3
parent 31897
6eaf6e8dee3f
parent 31958
e1b3f14fba94
child 32171
92cb32a21383
permissions
-rw-r--r--

propagate from branch 'im.pidgin.pidgin' (head a727618dfaa697c504839afb78b7ecb859eb2d27)
to branch 'im.pidgin.pidgin.mxit' (head d0676f987e39c0467d8bef1b829860612872fad5)

28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
1 /*
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
2 * MXit Protocol libPurple Plugin
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
3 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
4 * -- user password encryption --
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
5 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
6 * Pieter Loubser <libpurple@mxit.com>
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
7 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
8 * (C) Copyright 2009 MXit Lifestyle (Pty) Ltd.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
9 * <http://www.mxitlifestyle.com>
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
10 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
11 * This program is free software; you can redistribute it and/or modify
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
12 * it under the terms of the GNU General Public License as published by
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
13 * the Free Software Foundation; either version 2 of the License, or
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
14 * (at your option) any later version.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
15 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
16 * This program is distributed in the hope that it will be useful,
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
19 * GNU General Public License for more details.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
20 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
21 * You should have received a copy of the GNU General Public License
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
22 * along with this program; if not, write to the Free Software
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
24 */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
25
29106
51c7b2177e42 Fix all the remaining files for which internal.h doesn't end up being the first include.
Paul Aurich <darkrain42@pidgin.im>
parents: 28841
diff changeset
26 #include "internal.h"
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
27 #include "purple.h"
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
28
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
29 #include "mxit.h"
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
30 #include "cipher.h"
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
31 #include "aes.h"
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
32
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
33
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
34 /* password encryption */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
35 #define INITIAL_KEY "6170383452343567"
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
36 #define SECRET_HEADER "<mxit/>"
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
37 #define ENCRYPT_HEADER "<mxitencrypted ver=\"5.2\"/>"
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
38
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
39
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
40 /*------------------------------------------------------------------------
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
41 * Pad the secret data using ISO10126 Padding.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
42 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
43 * @param secret The data to pad (caller must ensure buffer has enough space for padding)
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
44 * @return The total number of 128-bit blocks used
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
45 */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
46 static int pad_secret_data( char* secret )
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
47 {
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
48 int blocks = 0;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
49 int passlen;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
50 int padding;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
51
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
52 passlen = strlen( secret );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
53 blocks = ( passlen / 16 ) + 1;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
54 padding = ( blocks * 16 ) - passlen;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
55 secret[passlen] = 0x50;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
56 secret[(blocks * 16) - 1] = padding;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
57
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
58 return blocks;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
59 }
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
60
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
61
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
62 /*------------------------------------------------------------------------
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
63 * Encrypt the user's cleartext password using the AES 128-bit (ECB)
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
64 * encryption algorithm.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
65 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
66 * @param session The MXit session object
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
67 * @return The encrypted & encoded password. Must be g_free'd when no longer needed.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
68 */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
69 char* mxit_encrypt_password( struct MXitSession* session )
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
70 {
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
71 char key[64];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
72 char exkey[512];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
73 char pass[64];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
74 char encrypted[64];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
75 char* base64;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
76 int blocks;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
77 int size;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
78 int i;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
79
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
80 purple_debug_info( MXIT_PLUGIN_ID, "mxit_encrypt_password\n" );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
81
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
82 memset( encrypted, 0x00, sizeof( encrypted ) );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
83 memset( exkey, 0x00, sizeof( exkey ) );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
84 memset( pass, 0x58, sizeof( pass ) );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
85 pass[sizeof( pass ) - 1] = '\0';
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
86
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
87 /* build the custom AES encryption key */
31958
e1b3f14fba94 Changed a strcpy in the mxit prpl to a bounds-checked g_strlcpy.
Ethan Blanton <elb@pidgin.im>
parents: 29106
diff changeset
88 g_strlcpy( key, INITIAL_KEY, sizeof( key ) );
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
89 memcpy( key, session->clientkey, strlen( session->clientkey ) );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
90 ExpandKey( (unsigned char*) key, (unsigned char*) exkey );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
91
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
92 /* build the custom data to be encrypted */
31958
e1b3f14fba94 Changed a strcpy in the mxit prpl to a bounds-checked g_strlcpy.
Ethan Blanton <elb@pidgin.im>
parents: 29106
diff changeset
93 g_strlcpy( pass, SECRET_HEADER, sizeof( pass ) );
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
94 strcat( pass, session->acc->password );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
95
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
96 /* pad the secret data */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
97 blocks = pad_secret_data( pass );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
98 size = blocks * 16;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
99
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
100 /* now encrypt the password. we encrypt each block separately (ECB mode) */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
101 for ( i = 0; i < size; i += 16 )
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
102 Encrypt( (unsigned char*) pass + i, (unsigned char*) exkey, (unsigned char*) encrypted + i );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
103
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
104 /* now base64 encode the encrypted password */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
105 base64 = purple_base64_encode( (unsigned char*) encrypted, size );
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
106
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
107 return base64;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
108 }
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
109
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
110
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
111 /*------------------------------------------------------------------------
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
112 * Decrypt a transport-layer encryptede message.
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
113 *
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
114 * @param session The MXit session object
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
115 * @param message The encrypted message data.
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
116 * @return The decrypted message. Must be g_free'd when no longer needed.
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
117 */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
118 char* mxit_decrypt_message( struct MXitSession* session, char* message )
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
119 {
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
120 gsize raw_len;
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
121 guchar* raw_message;
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
122 char key[64];
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
123 int pwdlen = strlen( session->acc->password );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
124 char exkey[512];
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
125 int i;
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
126 GString* decoded = NULL;
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
127
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
128 /* remove optional header: <mxitencrypted ver="5.2"/> */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
129 if ( strncmp( message, ENCRYPT_HEADER, strlen( ENCRYPT_HEADER ) ) == 0 )
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
130 message += strlen( ENCRYPT_HEADER );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
131
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
132 /* base64 decode the message */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
133 raw_message = purple_base64_decode( message, &raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
134
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
135 /* build the key - Client key, appended with last 8 characters of the PIN. (no padding) */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
136 memset( key, 0x00, sizeof( key ) );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
137 memcpy( key, session->clientkey, strlen( session->clientkey ) );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
138 if ( pwdlen <= 8 )
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
139 strcat( key, session->acc->password );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
140 else
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
141 strncat( key, session->acc->password + ( pwdlen - 8 ), 8 );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
142 ExpandKey( (unsigned char*) key, (unsigned char*) exkey );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
143
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
144 /* decode each block */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
145 decoded = g_string_sized_new( raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
146 for ( i = 0; i < raw_len; i += 16 ) {
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
147 char block[16];
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
148
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
149 Decrypt( (unsigned char*) raw_message + i, (unsigned char*) exkey, (unsigned char*) block );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
150 g_string_append_len( decoded, block, 16 );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
151 }
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
152
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
153 g_free( raw_message );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
154
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
155 purple_debug_info( MXIT_PLUGIN_ID, "decrypted: '%s'\n", decoded->str );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
156
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
157 /* check that the decrypted message starts with header: <mxit/> */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
158 if ( strncmp( decoded->str, SECRET_HEADER, strlen( SECRET_HEADER ) != 0 ) ) {
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
159 g_string_free( decoded, TRUE );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
160 return NULL; /* message could not be decoded */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
161 }
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
162 g_string_erase( decoded, 0, strlen( SECRET_HEADER ) ); /* remove header */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
163
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
164 /* remove ISO10126 padding */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
165 // TODO
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
166
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
167 return g_string_free( decoded, FALSE );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
168 }

mercurial