libpurple/protocols/mxit/cipher.c

Tue, 16 Aug 2011 05:38:01 +0000

author
Pieter Loubser <pieter.loubser@mxit.com>
date
Tue, 16 Aug 2011 05:38:01 +0000
branch
mxit
changeset 32180
766c92a94b04
parent 32177
b4729e4322f3
child 32182
fdf3af8def04
permissions
-rw-r--r--

merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
and '3ead8f25f2a9475b2d641717f083565ecbb56961'

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 *
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
4 * -- encryption --
28841
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
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
34 /* encryption */
28841
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 /*------------------------------------------------------------------------
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
41 * Add ISO10126 Padding to the data.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
42 *
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
43 * @param data The data to pad.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
44 */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
45 static void padding_add( GString* data )
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
46 {
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
47 unsigned int blocks = ( data->len / 16 ) + 1;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
48 unsigned int padding = ( blocks * 16 ) - data->len;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
49
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
50 g_string_set_size( data, blocks * 16 );
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
51 data->str[data->len - 1] = padding;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
52 }
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
53
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
54
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
55 /*------------------------------------------------------------------------
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
56 * Remove ISO10126 Padding from the data.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
57 *
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
58 * @param data The data from which to remove padding.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
59 */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
60 static void padding_remove( GString* data )
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
61 {
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
62 unsigned int padding;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
63
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
64 if ( data->len == 0 )
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
65 return;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
66
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
67 padding = data->str[data->len - 1];
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
68 g_string_truncate( data, data->len - padding );
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
69 }
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
70
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
71
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
72 /*------------------------------------------------------------------------
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
73 * Generate the Transport-Layer crypto key.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
74 * (Note: this function is not-thread safe)
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
75 *
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
76 * @param session The MXit Session object
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
77 * @return The transport-layer crypto key.
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
78 */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
79 static char* transport_layer_key( struct MXitSession* session )
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
80 {
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
81 static char key[16 + 1];
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
82 int passlen = strlen( session->acc->password );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
83
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
84 /* initialize with initial key */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
85 g_strlcpy( key, INITIAL_KEY, sizeof( key ) );
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
86
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
87 /* client key (8 bytes) */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
88 memcpy( key, session->clientkey, strlen( session->clientkey ) );
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
89
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
90 /* add last 8 characters of the PIN (no padding if less characters) */
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
91 if ( passlen <= 8 )
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
92 memcpy( key + 8, session->acc->password, passlen );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
93 else
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
94 memcpy( key + 8, session->acc->password + ( passlen - 8 ), 8 );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
95
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
96 return key;
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
97 }
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
98
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
99
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
100 /*------------------------------------------------------------------------
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
101 * 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
102 * encryption algorithm.
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 * @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
105 * @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
106 */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
107 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
108 {
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
109 char key[16 + 1];
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
110 char exkey[512];
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
111 GString* pass = NULL;
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
112 char encrypted[64];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
113 char* base64;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
114 int i;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
115
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
116 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
117
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
118 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
119
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
120 /* 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
121 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
122 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
123 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
124
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
125 /* build the custom data to be encrypted */
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
126 g_strlcpy( pass, SECRET_HEADER, sizeof( pass ) );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
127 strcat( pass, session->acc->password );
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
128
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
129 /* pad the secret data */
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
130 blocks = pad_secret_data( pass );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
131 size = blocks * 16;
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
132
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
133 /* now encrypt the password. we encrypt each block separately (ECB mode) */
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
134 for ( i = 0; i < size; i += 16 )
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
135 Encrypt( (unsigned char*) pass + i, (unsigned char*) exkey, (unsigned char*) encrypted + i );
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
136
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
137 /* now base64 encode the encrypted password */
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
138 base64 = purple_base64_encode( (unsigned char*) encrypted, pass->len );
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
139
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
140 g_string_free( pass, TRUE );
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
141
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
142 return base64;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
143 }
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
144
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
145
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
146 /*------------------------------------------------------------------------
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
147 * Decrypt a transport-layer encryptede message.
31897
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 * @param session The MXit session object
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
150 * @param message The encrypted message data.
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
151 * @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
152 */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
153 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
154 {
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
155 gsize raw_len;
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
156 guchar* raw_message;
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
157 char key[64];
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
158 int pwdlen = strlen( session->acc->password );
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
159 char exkey[512];
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
160 int i;
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
161 GString* decoded = NULL;
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
162
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
163 /* remove optional header: <mxitencrypted ver="5.2"/> */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
164 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
165 message += strlen( ENCRYPT_HEADER );
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 /* base64 decode the message */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
168 raw_message = purple_base64_decode( message, &raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
169
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
170 /* build the key - Client key, appended with last 8 characters of the PIN. (no padding) */
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
171 memset( key, 0x00, sizeof( key ) );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
172 memcpy( key, session->clientkey, strlen( session->clientkey ) );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
173 if ( pwdlen <= 8 )
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
174 strcat( key, session->acc->password );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
175 else
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
176 strncat( key, session->acc->password + ( pwdlen - 8 ), 8 );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
177 ExpandKey( (unsigned char*) key, (unsigned char*) exkey );
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
178
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
179 /* decode each block */
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
180 decoded = g_string_sized_new( raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
181 for ( i = 0; i < raw_len; i += 16 ) {
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
182 char block[16];
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
183
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
184 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
185 g_string_append_len( decoded, block, 16 );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
186 }
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
187
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
188 g_free( raw_message );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
189
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
190 purple_debug_info( MXIT_PLUGIN_ID, "decrypted: '%s'\n", decoded->str );
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
191
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
192 /* 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
193 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
194 g_string_free( decoded, TRUE );
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
195 return NULL; /* message could not be decoded */
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
196 }
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
197 g_string_erase( decoded, 0, strlen( SECRET_HEADER ) ); /* remove header */
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
198
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
199 /* remove ISO10126 padding */
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
200 // TODO
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
201
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
202 return g_string_free( decoded, FALSE );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
203 }

mercurial