Mon, 15 Aug 2011 19:24:16 +0000
Refactor the transport-layer encryption and decryption.
Also fix key initialization when PIN is shorter than 8 characters.
|
28841
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
1 | /* |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
2 | * MXit Protocol libPurple Plugin |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
3 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
4 | * -- user password encryption -- |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
5 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
6 | * Pieter Loubser <libpurple@mxit.com> |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
7 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
8 | * (C) Copyright 2009 MXit Lifestyle (Pty) Ltd. |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
9 | * <http://www.mxitlifestyle.com> |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
10 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
11 | * This program is free software; you can redistribute it and/or modify |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
12 | * it under the terms of the GNU General Public License as published by |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
13 | * the Free Software Foundation; either version 2 of the License, or |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
14 | * (at your option) any later version. |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
15 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
16 | * This program is distributed in the hope that it will be useful, |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
19 | * GNU General Public License for more details. |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
20 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
21 | * You should have received a copy of the GNU General Public License |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
22 | * along with this program; if not, write to the Free Software |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
24 | */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
25 | |
|
29106
51c7b2177e42
Fix all the remaining files for which internal.h doesn't end up being the first include.
Paul Aurich <darkrain42@pidgin.im>
parents:
28841
diff
changeset
|
26 | #include "internal.h" |
|
28841
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
27 | #include "purple.h" |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
28 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
29 | #include "mxit.h" |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
30 | #include "cipher.h" |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
31 | #include "aes.h" |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
32 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
33 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
34 | /* password encryption */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
35 | #define INITIAL_KEY "6170383452343567" |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
36 | #define SECRET_HEADER "<mxit/>" |
|
31897
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
37 | #define ENCRYPT_HEADER "<mxitencrypted ver=\"5.2\"/>" |
|
28841
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
38 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
39 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
40 | /*------------------------------------------------------------------------ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
41 | * Pad the secret data using ISO10126 Padding. |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
42 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
43 | * @param secret The data to pad (caller must ensure buffer has enough space for padding) |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
44 | * @return The total number of 128-bit blocks used |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
45 | */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
46 | static int pad_secret_data( char* secret ) |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
47 | { |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
48 | int blocks = 0; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
49 | int passlen; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
50 | int padding; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
51 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
52 | passlen = strlen( secret ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
53 | blocks = ( passlen / 16 ) + 1; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
54 | padding = ( blocks * 16 ) - passlen; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
55 | secret[passlen] = 0x50; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
56 | secret[(blocks * 16) - 1] = padding; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
57 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
58 | return blocks; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
59 | } |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
60 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
61 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
62 | /*------------------------------------------------------------------------ |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
63 | * Add ISO10126 Padding to the data. |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
64 | * |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
65 | * @param data The data to pad. |
|
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 | static void padding_add( GString* data ) |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
68 | { |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
69 | 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
|
70 | 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
|
71 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
72 | 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
|
73 | data->str[data->len - 1] = padding; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
74 | } |
|
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 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
77 | /*------------------------------------------------------------------------ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
78 | * Remove ISO10126 Padding from the data. |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
79 | * |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
80 | * @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
|
81 | */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
82 | static void padding_remove( GString* data ) |
|
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 | unsigned int padding; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
85 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
86 | if ( data->len == 0 ) |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
87 | return; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
88 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
89 | padding = data->str[data->len - 1]; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
90 | 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
|
91 | } |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
92 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
93 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
94 | /*------------------------------------------------------------------------ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
95 | * Generate the Transport-Layer crypto key. |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
96 | * (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
|
97 | * |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
98 | * @param session The MXit Session object |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
99 | * @return The transport-layer crypto key. |
|
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 | 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
|
102 | { |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
103 | static char key[16 + 1]; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
104 | int pwdlen = strlen( session->acc->password ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
105 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
106 | /* initialize with initial key */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
107 | 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
|
108 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
109 | /* client key (8 bytes) */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
110 | 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
|
111 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
112 | /* add last 8 characters of the PIN (no padding if less characters) */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
113 | if ( pwdlen <= 8 ) |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
114 | memcpy( key + 8, session->acc->password, pwdlen ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
115 | else |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
116 | memcpy( key + 8, session->acc->password + ( pwdlen - 8 ), 8 ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
117 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
118 | return key; |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
119 | } |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
120 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
121 | |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
122 | /*------------------------------------------------------------------------ |
|
28841
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
123 | * 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
|
124 | * encryption algorithm. |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
125 | * |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
126 | * @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
|
127 | * @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
|
128 | */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
129 | 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
|
130 | { |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
131 | char key[64]; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
132 | char exkey[512]; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
133 | char pass[64]; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
134 | char encrypted[64]; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
135 | char* base64; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
136 | int blocks; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
137 | int size; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
138 | int i; |
|
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 | 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
|
141 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
142 | 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
|
143 | memset( exkey, 0x00, sizeof( exkey ) ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
144 | memset( pass, 0x58, sizeof( pass ) ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
145 | pass[sizeof( pass ) - 1] = '\0'; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
146 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
147 | /* 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
|
148 | 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
|
149 | 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
|
150 | 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
|
151 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
152 | /* build the custom data to be encrypted */ |
|
31958
e1b3f14fba94
Changed a strcpy in the mxit prpl to a bounds-checked g_strlcpy.
Ethan Blanton <elb@pidgin.im>
parents:
29106
diff
changeset
|
153 | g_strlcpy( pass, SECRET_HEADER, sizeof( pass ) ); |
|
28841
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
154 | strcat( pass, session->acc->password ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
155 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
156 | /* pad the secret data */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
157 | blocks = pad_secret_data( pass ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
158 | size = blocks * 16; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
159 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
160 | /* now encrypt the password. we encrypt each block separately (ECB mode) */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
161 | for ( i = 0; i < size; i += 16 ) |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
162 | Encrypt( (unsigned char*) pass + i, (unsigned char*) exkey, (unsigned char*) encrypted + i ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
163 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
164 | /* now base64 encode the encrypted password */ |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
165 | base64 = purple_base64_encode( (unsigned char*) encrypted, size ); |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
166 | |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
167 | return base64; |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
168 | } |
|
6b9568572bcb
Initial addition of the MXit protocol plugin, provided by the MXit folks
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
diff
changeset
|
169 | |
|
31897
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
170 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
171 | /*------------------------------------------------------------------------ |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
172 | * Decrypt a message using transport-layer encryption. |
|
31897
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 | * @param session The MXit session object |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
175 | * @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
|
176 | * @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
|
177 | */ |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
178 | 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
|
179 | { |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
180 | gsize raw_len; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
181 | guchar* raw_message; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
182 | char exkey[512]; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
183 | int i; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
184 | GString* decoded = NULL; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
185 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
186 | /* remove optional header: <mxitencrypted ver="5.2"/> */ |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
187 | 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
|
188 | message += strlen( ENCRYPT_HEADER ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
189 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
190 | /* base64 decode the message */ |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
191 | raw_message = purple_base64_decode( message, &raw_len ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
192 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
193 | /* build the AES key */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
194 | 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
|
195 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
196 | /* AES decrypt each block */ |
|
31897
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
197 | decoded = g_string_sized_new( raw_len ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
198 | for ( i = 0; i < raw_len; i += 16 ) { |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
199 | char block[16]; |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
200 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
201 | 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
|
202 | g_string_append_len( decoded, block, 16 ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
203 | } |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
204 | g_free( raw_message ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
205 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
206 | /* 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
|
207 | 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
|
208 | g_string_free( decoded, TRUE ); |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
209 | return NULL; /* message could not be decrypted */ |
|
31897
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
210 | } |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
211 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
212 | /* remove ISO10126 padding */ |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
213 | padding_remove( decoded ); |
|
32171
92cb32a21383
Remove ISO10126 padding when decrypting an incoming transport-encrypted message.
Andrew Victor <andrew.victor@mxit.com>
parents:
32165
diff
changeset
|
214 | |
|
92cb32a21383
Remove ISO10126 padding when decrypting an incoming transport-encrypted message.
Andrew Victor <andrew.victor@mxit.com>
parents:
32165
diff
changeset
|
215 | /* remove encryption header */ |
|
92cb32a21383
Remove ISO10126 padding when decrypting an incoming transport-encrypted message.
Andrew Victor <andrew.victor@mxit.com>
parents:
32165
diff
changeset
|
216 | 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
|
217 | |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
218 | return g_string_free( decoded, FALSE ); |
|
6eaf6e8dee3f
Start implementing MXit secure messaging.
Andrew Victor <andrew.victor@mxit.com>
parents:
29106
diff
changeset
|
219 | } |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
220 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
221 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
222 | /*------------------------------------------------------------------------ |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
223 | * Encrypt a message using transport-layer encryption. |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
224 | * |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
225 | * @param session The MXit session object |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
226 | * @param message The message data. |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
227 | * @return The encrypted message. Must be g_free'd when no longer needed. |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
228 | */ |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
229 | char* mxit_encrypt_message( struct MXitSession* session, char* message ) |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
230 | { |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
231 | char exkey[512]; |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
232 | int i; |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
233 | GString* raw_message = NULL; |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
234 | GString* encoded = NULL; |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
235 | gchar* base64; |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
236 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
237 | purple_debug_info( MXIT_PLUGIN_ID, "encrypt message: '%s'\n", message ); |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
238 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
239 | /* build the AES key */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
240 | ExpandKey( (unsigned char*) transport_layer_key( session ), (unsigned char*) exkey ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
241 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
242 | /* append encryption header to message data */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
243 | raw_message = g_string_sized_new( strlen( SECRET_HEADER ) + strlen( message ) ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
244 | g_string_append( raw_message, SECRET_HEADER ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
245 | g_string_append( raw_message, message ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
246 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
247 | /* add ISO10126 padding */ |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
248 | padding_add( raw_message ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
249 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
250 | /* encrypt each block */ |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
251 | encoded = g_string_sized_new( raw_message->len ); |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
252 | for ( i = 0; i < raw_message->len; i += 16 ) { |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
253 | char block[16]; |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
254 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
255 | Encrypt( (unsigned char*) raw_message->str + i, (unsigned char*) exkey, (unsigned char*) block ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
256 | g_string_append_len( encoded, block, 16 ); |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
257 | } |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
258 | g_string_free( raw_message, TRUE ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
259 | |
|
32176
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
260 | /* base64 encode the encrypted message */ |
|
b9ede4a1435b
Refactor the transport-layer encryption and decryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32174
diff
changeset
|
261 | base64 = purple_base64_encode( (unsigned char *) encoded->str, encoded->len ); |
|
32174
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
262 | g_string_free( encoded, TRUE ); |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
263 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
264 | purple_debug_info( MXIT_PLUGIN_ID, "encrypted message: '%s'\n", base64 ); |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
265 | |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
266 | return base64; |
|
42c550421b10
Encrypt a message using transport-encryption.
Andrew Victor <andrew.victor@mxit.com>
parents:
32171
diff
changeset
|
267 | } |