annotate libpurple/protocols/mxit/cipher.c @ 32672:3828a61c44da

A boring and large patch so I can merge heads.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 23 Dec 2011 08:21:58 +0000
parents 2754bbe70ac1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
1 /*
69aa4660401a 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
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
3 *
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
4 * -- encryption --
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
5 *
69aa4660401a 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>
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
7 *
69aa4660401a 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.
69aa4660401a 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>
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
10 *
69aa4660401a 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
69aa4660401a 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
69aa4660401a 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
69aa4660401a 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.
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
15 *
69aa4660401a 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,
69aa4660401a 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
69aa4660401a 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
69aa4660401a 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.
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
20 *
69aa4660401a 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
69aa4660401a 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
69aa4660401a 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
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
24 */
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
25
28770
259bbfb423d4 Fix all the remaining files for which internal.h doesn't end up being the first include.
Paul Aurich <paul@darkrain42.org>
parents: 28526
diff changeset
26 #include "internal.h"
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
27 #include "purple.h"
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
28
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
29 #include "mxit.h"
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
30 #include "cipher.h"
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
31 #include "aes.h"
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
32
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
33
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
34 /* encryption */
28526
69aa4660401a 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"
69aa4660401a 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/>"
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
37 #define ENCRYPT_HEADER "<mxitencrypted ver=\"5.2\"/>"
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
38
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
39
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
40 /*------------------------------------------------------------------------
31889
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
41 * Add ISO10126 Padding to the data.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
42 *
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
43 * @param data The data to pad.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
44 */
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
45 static void padding_add( GString* data )
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
46 {
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
47 unsigned int blocks = ( data->len / 16 ) + 1;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
48 unsigned int padding = ( blocks * 16 ) - data->len;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
49
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
50 g_string_set_size( data, blocks * 16 );
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
51 data->str[data->len - 1] = padding;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
52 }
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
53
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
54
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
55 /*------------------------------------------------------------------------
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
56 * Remove ISO10126 Padding from the data.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
57 *
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
58 * @param data The data from which to remove padding.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
59 */
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
60 static void padding_remove( GString* data )
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
61 {
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
62 unsigned int padding;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
63
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
64 if ( data->len == 0 )
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
65 return;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
66
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
67 padding = data->str[data->len - 1];
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
68 g_string_truncate( data, data->len - padding );
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
69 }
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
70
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
71
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
72 /*------------------------------------------------------------------------
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
73 * Generate the Transport-Layer crypto key.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
74 * (Note: this function is not-thread safe)
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
75 *
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
76 * @param session The MXit Session object
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
77 * @return The transport-layer crypto key.
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
78 */
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
79 static char* transport_layer_key( struct MXitSession* session )
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
80 {
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
81 static char key[16 + 1];
32672
3828a61c44da A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 31894
diff changeset
82 const char* password = purple_account_get_password( session->acc );
3828a61c44da A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 31894
diff changeset
83 int passlen = strlen( password );
31889
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
84
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
85 /* initialize with initial key */
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
86 g_strlcpy( key, INITIAL_KEY, sizeof( key ) );
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
87
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
88 /* client key (8 bytes) */
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
89 memcpy( key, session->clientkey, strlen( session->clientkey ) );
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
90
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
91 /* add last 8 characters of the PIN (no padding if less characters) */
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
92 if ( passlen <= 8 )
32672
3828a61c44da A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 31894
diff changeset
93 memcpy( key + 8, password, passlen );
31889
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
94 else
32672
3828a61c44da A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 31894
diff changeset
95 memcpy( key + 8, password + ( passlen - 8 ), 8 );
31889
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
96
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
97 return key;
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
98 }
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
99
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
100
79e1479747cf Refactor the transport-layer encryption and decryption.
andrew.victor@mxit.com
parents: 31888
diff changeset
101 /*------------------------------------------------------------------------
28526
69aa4660401a 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)
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
103 * encryption algorithm.
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
104 *
69aa4660401a 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
69aa4660401a 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.
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
107 */
69aa4660401a 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 )
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
109 {
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
110 char key[16 + 1];
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
111 char exkey[512];
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
112 GString* pass = NULL;
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
113 char encrypted[64];
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
114 char* base64;
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
115 int i;
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
116
69aa4660401a 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" );
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
118
69aa4660401a 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 ) );
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
120
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
121 /* build the AES encryption key */
31774
4ad2a0668687 Changed a strcpy in the mxit prpl to a bounds-checked g_strlcpy.
Ethan Blanton <elb@pidgin.im>
parents: 28770
diff changeset
122 g_strlcpy( key, INITIAL_KEY, sizeof( key ) );
28526
69aa4660401a 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 ) );
69aa4660401a 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 );
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
125
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
126 /* build the secret data to be encrypted: SECRET_HEADER + password */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
127 pass = g_string_new( SECRET_HEADER );
32672
3828a61c44da A boring and large patch so I can merge heads.
Elliott Sales de Andrade <qulogic@pidgin.im>
parents: 31894
diff changeset
128 g_string_append( pass, purple_account_get_password( session->acc) );
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
129 padding_add( pass ); /* add ISO10126 padding */
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
130
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
131 /* now encrypt the secret. we encrypt each block separately (ECB mode) */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
132 for ( i = 0; i < pass->len; i += 16 )
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
133 Encrypt( (unsigned char*) pass->str + i, (unsigned char*) exkey, (unsigned char*) encrypted + i );
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
134
69aa4660401a 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 */
31890
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
136 base64 = purple_base64_encode( (unsigned char*) encrypted, pass->len );
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
137
a86d2f291923 Simplify the PIN encryption code.
andrew.victor@mxit.com
parents: 31889
diff changeset
138 g_string_free( pass, TRUE );
28526
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
139
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
140 return base64;
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
141 }
69aa4660401a Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff changeset
142
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
143
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
144 /*------------------------------------------------------------------------
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
145 * Decrypt a message using transport-layer encryption.
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
146 *
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
147 * @param session The MXit session object
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
148 * @param message The encrypted message data (is base64-encoded).
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
149 * @return The decrypted message. Must be g_free'd when no longer needed.
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
150 */
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
151 char* mxit_decrypt_message( struct MXitSession* session, char* message )
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
152 {
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
153 guchar* raw_message;
31892
a330d1187fa7 merge of 'abe467cd836e25b339d81461c056f6289d88b2f0'
pieter.loubser@mxit.com
parents: 31890
diff changeset
154 gsize raw_len;
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
155 char exkey[512];
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
156 GString* decoded = NULL;
31892
a330d1187fa7 merge of 'abe467cd836e25b339d81461c056f6289d88b2f0'
pieter.loubser@mxit.com
parents: 31890
diff changeset
157 int i;
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
158
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
159 /* remove optional header: <mxitencrypted ver="5.2"/> */
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
160 if ( strncmp( message, ENCRYPT_HEADER, strlen( ENCRYPT_HEADER ) ) == 0 )
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
161 message += strlen( ENCRYPT_HEADER );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
162
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
163 /* base64 decode the message */
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
164 raw_message = purple_base64_decode( message, &raw_len );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
165
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
166 /* build the AES key */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
167 ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
168
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
169 /* AES decrypt each block */
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
170 decoded = g_string_sized_new( raw_len );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
171 for ( i = 0; i < raw_len; i += 16 ) {
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
172 char block[16];
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
173
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
174 Decrypt( (unsigned char*) raw_message + i, (unsigned char*) exkey, (unsigned char*) block );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
175 g_string_append_len( decoded, block, 16 );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
176 }
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
177 g_free( raw_message );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
178
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
179 /* check that the decrypted message starts with header: <mxit/> */
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
180 if ( strncmp( decoded->str, SECRET_HEADER, strlen( SECRET_HEADER ) != 0 ) ) {
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
181 g_string_free( decoded, TRUE );
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
182 return NULL; /* message could not be decrypted */
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
183 }
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
184
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
185 /* remove ISO10126 padding */
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
186 padding_remove( decoded );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
187
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
188 /* remove encryption header */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
189 g_string_erase( decoded, 0, strlen( SECRET_HEADER ) );
31876
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
190
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
191 return g_string_free( decoded, FALSE );
3951afa56940 Start implementing MXit secure messaging.
andrew.victor@mxit.com
parents: 28770
diff changeset
192 }
31894
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
193
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
194
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
195 /*------------------------------------------------------------------------
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
196 * Encrypt a message using transport-layer encryption.
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
197 *
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
198 * @param session The MXit session object
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
199 * @param message The message data.
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
200 * @return The encrypted message. Must be g_free'd when no longer needed.
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
201 */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
202 char* mxit_encrypt_message( struct MXitSession* session, char* message )
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
203 {
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
204 GString* raw_message = NULL;
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
205 char exkey[512];
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
206 GString* encoded = NULL;
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
207 gchar* base64;
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
208 int i;
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
209
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
210 purple_debug_info( MXIT_PLUGIN_ID, "encrypt message: '%s'\n", message );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
211
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
212 /* append encryption header to message data */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
213 raw_message = g_string_new( SECRET_HEADER );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
214 g_string_append( raw_message, message );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
215 padding_add( raw_message ); /* add ISO10126 padding */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
216
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
217 /* build the AES key */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
218 ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
219
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
220 /* AES encrypt each block */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
221 encoded = g_string_sized_new( raw_message->len );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
222 for ( i = 0; i < raw_message->len; i += 16 ) {
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
223 char block[16];
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
224
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
225 Encrypt( (unsigned char*) raw_message->str + i, (unsigned char*) exkey, (unsigned char*) block );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
226 g_string_append_len( encoded, block, 16 );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
227 }
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
228 g_string_free( raw_message, TRUE );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
229
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
230 /* base64 encode the encrypted message */
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
231 base64 = purple_base64_encode( (unsigned char *) encoded->str, encoded->len );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
232 g_string_free( encoded, TRUE );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
233
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
234 purple_debug_info( MXIT_PLUGIN_ID, "encrypted message: '%s'\n", base64 );
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
235
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
236 return base64;
2754bbe70ac1 * fixed the head: mtn issue..
pieter.loubser@mxit.com
parents: 31892
diff changeset
237 }