libpurple/protocols/mxit/cipher.c

Fri, 15 Jun 2012 00:59:46 -0400

author
Elliott Sales de Andrade <qulogic@pidgin.im>
date
Fri, 15 Jun 2012 00:59:46 -0400
changeset 33052
df92c3d93904
parent 32438
dc8991868906
child 33558
8564519d01c0
child 34024
9155cf9d26fa
permissions
-rw-r--r--

From now on, prpls must specify images using SRC instead of ID.

This only fixes libpurple, the UI side doesn't follow this change
just yet.

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];
32438
dc8991868906 A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 32182
diff changeset
82 const char* password = purple_account_get_password( session->acc );
dc8991868906 A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 32182
diff changeset
83 int passlen = strlen( password );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
84
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
85 /* initialize with initial key */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
86 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
87
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
88 /* client key (8 bytes) */
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
89 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
90
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
91 /* 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
92 if ( passlen <= 8 )
32438
dc8991868906 A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 32182
diff changeset
93 memcpy( key + 8, password, passlen );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
94 else
32438
dc8991868906 A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 32182
diff changeset
95 memcpy( key + 8, password + ( passlen - 8 ), 8 );
32176
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
96
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
97 return key;
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
b9ede4a1435b Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents: 32174
diff changeset
101 /*------------------------------------------------------------------------
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
102 * 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
103 * encryption algorithm.
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
104 *
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
105 * @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
106 * @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
107 */
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
108 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
109 {
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
110 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
111 char exkey[512];
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
112 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
113 char encrypted[64];
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
114 char* base64;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
115 int i;
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
116
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
117 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
118
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
119 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
120
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
121 /* build the 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
122 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
123 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
124 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
125
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
126 /* build the secret data to be encrypted: SECRET_HEADER + password */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
127 pass = g_string_new( SECRET_HEADER );
32438
dc8991868906 A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 32182
diff changeset
128 g_string_append( pass, purple_account_get_password( session->acc) );
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
129 padding_add( pass ); /* add ISO10126 padding */
28841
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
130
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
131 /* now encrypt the secret. we encrypt each block separately (ECB mode) */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
132 for ( i = 0; i < pass->len; i += 16 )
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
133 Encrypt( (unsigned char*) pass->str + 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
134
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
135 /* now base64 encode the encrypted password */
32177
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
136 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
137
b4729e4322f3 Simplify the PIN encryption code.
Andrew Victor <andrew.victor@mxit.com>
parents: 32176
diff changeset
138 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
139
6b9568572bcb Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
140 return base64;
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
31897
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 /*------------------------------------------------------------------------
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
145 * Decrypt a message using transport-layer encryption.
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
146 *
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
147 * @param session The MXit session object
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
148 * @param message The encrypted message data (is base64-encoded).
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
149 * @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
150 */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
151 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
152 {
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
153 guchar* raw_message;
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
154 gsize raw_len;
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
155 char exkey[512];
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
156 GString* decoded = NULL;
32180
766c92a94b04 merge of '8654d04cb0949c2bbf69455b5f5366602d019da5'
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32177
diff changeset
157 int i;
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
158
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
159 /* remove optional header: <mxitencrypted ver="5.2"/> */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
160 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
161 message += strlen( ENCRYPT_HEADER );
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 /* base64 decode the message */
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
164 raw_message = purple_base64_decode( message, &raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
165
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
166 /* build the AES key */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
167 ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
168
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
169 /* AES decrypt each block */
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
170 decoded = g_string_sized_new( raw_len );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
171 for ( i = 0; i < raw_len; i += 16 ) {
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
172 char block[16];
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
173
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
174 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
175 g_string_append_len( decoded, block, 16 );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
176 }
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
177 g_free( raw_message );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
178
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
179 /* 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
180 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
181 g_string_free( decoded, TRUE );
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
182 return NULL; /* message could not be decrypted */
31897
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
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
185 /* remove ISO10126 padding */
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
186 padding_remove( decoded );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
187
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
188 /* remove encryption header */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
189 g_string_erase( decoded, 0, strlen( SECRET_HEADER ) );
31897
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
190
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
191 return g_string_free( decoded, FALSE );
6eaf6e8dee3f Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents: 29106
diff changeset
192 }
32182
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
193
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
194
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
195 /*------------------------------------------------------------------------
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
196 * Encrypt a message using transport-layer encryption.
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
197 *
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
198 * @param session The MXit session object
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
199 * @param message The message data.
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
200 * @return The encrypted message. Must be g_free'd when no longer needed.
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
201 */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
202 char* mxit_encrypt_message( struct MXitSession* session, char* message )
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
203 {
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
204 GString* raw_message = NULL;
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
205 char exkey[512];
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
206 GString* encoded = NULL;
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
207 gchar* base64;
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
208 int i;
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
209
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
210 purple_debug_info( MXIT_PLUGIN_ID, "encrypt message: '%s'\n", message );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
211
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
212 /* append encryption header to message data */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
213 raw_message = g_string_new( SECRET_HEADER );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
214 g_string_append( raw_message, message );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
215 padding_add( raw_message ); /* add ISO10126 padding */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
216
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
217 /* build the AES key */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
218 ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
219
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
220 /* AES encrypt each block */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
221 encoded = g_string_sized_new( raw_message->len );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
222 for ( i = 0; i < raw_message->len; i += 16 ) {
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
223 char block[16];
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
224
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
225 Encrypt( (unsigned char*) raw_message->str + i, (unsigned char*) exkey, (unsigned char*) block );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
226 g_string_append_len( encoded, block, 16 );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
227 }
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
228 g_string_free( raw_message, TRUE );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
229
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
230 /* base64 encode the encrypted message */
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
231 base64 = purple_base64_encode( (unsigned char *) encoded->str, encoded->len );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
232 g_string_free( encoded, TRUE );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
233
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
234 purple_debug_info( MXIT_PLUGIN_ID, "encrypted message: '%s'\n", base64 );
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
235
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
236 return base64;
fdf3af8def04 * fixed the head: mtn issue..
Pieter Loubser <pieter.loubser@mxit.com>
parents: 32180
diff changeset
237 }

mercurial