[INTL]
[reactos.git] / reactos / base / applications / mstsc / licence.c
1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 RDP licensing negotiation
4 Copyright (C) Matthew Chapman 1999-2005
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "precomp.h"
22
23 void *
24 ssl_rc4_info_create(void);
25 void
26 ssl_rc4_info_delete(void * rc4_info);
27 void
28 ssl_rc4_set_key(void * rc4_info, char * key, int len);
29 void
30 ssl_rc4_crypt(void * rc4_info, char * in_data, char * out_data, int len);
31 int
32 ssl_mod_exp(char* out, int out_len, char* in, int in_len,
33 char* mod, int mod_len, char* exp, int exp_len);
34
35 extern char g_username[];
36 extern char g_hostname[];
37
38 static uint8 g_licence_key[16];
39 static uint8 g_licence_sign_key[16];
40
41 BOOL g_licence_issued = False;
42
43 /* Generate a session key and RC4 keys, given client and server randoms */
44 static void
45 licence_generate_keys(uint8 * client_random, uint8 * server_random, uint8 * pre_master_secret)
46 {
47 uint8 master_secret[48];
48 uint8 key_block[48];
49
50 /* Generate master secret and then key material */
51 sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A');
52 sec_hash_48(key_block, master_secret, server_random, client_random, 'A');
53
54 /* Store first 16 bytes of session key as MAC secret */
55 memcpy(g_licence_sign_key, key_block, 16);
56
57 /* Generate RC4 key from next 16 bytes */
58 sec_hash_16(g_licence_key, &key_block[16], client_random, server_random);
59 }
60
61 static void
62 licence_generate_hwid(uint8 * hwid)
63 {
64 buf_out_uint32(hwid, 2);
65 strncpy((char *) (hwid + 4), g_hostname, LICENCE_HWID_SIZE - 4);
66 }
67
68 /* Present an existing licence to the server */
69 static void
70 licence_present(uint8 * client_random, uint8 * rsa_data,
71 uint8 * licence_data, int licence_size, uint8 * hwid, uint8 * signature)
72 {
73 uint32 sec_flags = SEC_LICENCE_NEG;
74 uint16 length =
75 16 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE +
76 licence_size + LICENCE_HWID_SIZE + LICENCE_SIGNATURE_SIZE;
77 STREAM s;
78
79 s = sec_init(sec_flags, length + 4);
80
81 out_uint8(s, LICENCE_TAG_PRESENT);
82 out_uint8(s, 2); /* version */
83 out_uint16_le(s, length);
84
85 out_uint32_le(s, 1);
86 out_uint16(s, 0);
87 out_uint16_le(s, 0x0201);
88
89 out_uint8p(s, client_random, SEC_RANDOM_SIZE);
90 out_uint16(s, 0);
91 out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
92 out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
93 out_uint8s(s, SEC_PADDING_SIZE);
94
95 out_uint16_le(s, 1);
96 out_uint16_le(s, licence_size);
97 out_uint8p(s, licence_data, licence_size);
98
99 out_uint16_le(s, 1);
100 out_uint16_le(s, LICENCE_HWID_SIZE);
101 out_uint8p(s, hwid, LICENCE_HWID_SIZE);
102
103 out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
104
105 s_mark_end(s);
106 sec_send(s, sec_flags);
107 }
108
109 /* Send a licence request packet */
110 static void
111 licence_send_request(uint8 * client_random, uint8 * rsa_data, char *user, char *host)
112 {
113 uint32 sec_flags = SEC_LICENCE_NEG;
114 uint16 userlen = strlen(user) + 1;
115 uint16 hostlen = strlen(host) + 1;
116 uint16 length = 128 + userlen + hostlen;
117 STREAM s;
118
119 s = sec_init(sec_flags, length + 2);
120
121 out_uint8(s, LICENCE_TAG_REQUEST);
122 out_uint8(s, 2); /* version */
123 out_uint16_le(s, length);
124
125 out_uint32_le(s, 1);
126 out_uint16(s, 0);
127 out_uint16_le(s, 0xff01);
128
129 out_uint8p(s, client_random, SEC_RANDOM_SIZE);
130 out_uint16(s, 0);
131 out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
132 out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
133 out_uint8s(s, SEC_PADDING_SIZE);
134
135 out_uint16_le(s, LICENCE_TAG_USER);
136 out_uint16_le(s, userlen);
137 out_uint8p(s, user, userlen);
138
139 out_uint16_le(s, LICENCE_TAG_HOST);
140 out_uint16_le(s, hostlen);
141 out_uint8p(s, host, hostlen);
142
143 s_mark_end(s);
144 sec_send(s, sec_flags);
145 }
146
147 /* Process a licence demand packet */
148 static void
149 licence_process_demand(STREAM s)
150 {
151 uint8 null_data[SEC_MODULUS_SIZE];
152 uint8 *server_random;
153 uint8 signature[LICENCE_SIGNATURE_SIZE];
154 uint8 hwid[LICENCE_HWID_SIZE];
155 uint8 *licence_data;
156 int licence_size;
157 void * crypt_key;
158
159 /* Retrieve the server random from the incoming packet */
160 in_uint8p(s, server_random, SEC_RANDOM_SIZE);
161
162 /* We currently use null client keys. This is a bit naughty but, hey,
163 the security of licence negotiation isn't exactly paramount. */
164 memset(null_data, 0, sizeof(null_data));
165 licence_generate_keys(null_data, server_random, null_data);
166
167 licence_size = load_licence(&licence_data);
168 if (licence_size > 0)
169 {
170 /* Generate a signature for the HWID buffer */
171 licence_generate_hwid(hwid);
172 sec_sign(signature, 16, g_licence_sign_key, 16, hwid, sizeof(hwid));
173
174 /* Now encrypt the HWID */
175 crypt_key = ssl_rc4_info_create();
176 ssl_rc4_set_key(crypt_key, (char *)g_licence_key, 16);
177 ssl_rc4_crypt(crypt_key, (char *)hwid, (char *)hwid, sizeof(hwid));
178 ssl_rc4_info_delete(crypt_key);
179
180 licence_present(null_data, null_data, licence_data, licence_size, hwid, signature);
181 xfree(licence_data);
182 return;
183 }
184
185 licence_send_request(null_data, null_data, g_username, g_hostname);
186 }
187
188 /* Send an authentication response packet */
189 static void
190 licence_send_authresp(uint8 * token, uint8 * crypt_hwid, uint8 * signature)
191 {
192 uint32 sec_flags = SEC_LICENCE_NEG;
193 uint16 length = 58;
194 STREAM s;
195
196 s = sec_init(sec_flags, length + 2);
197
198 out_uint8(s, LICENCE_TAG_AUTHRESP);
199 out_uint8(s, 2); /* version */
200 out_uint16_le(s, length);
201
202 out_uint16_le(s, 1);
203 out_uint16_le(s, LICENCE_TOKEN_SIZE);
204 out_uint8p(s, token, LICENCE_TOKEN_SIZE);
205
206 out_uint16_le(s, 1);
207 out_uint16_le(s, LICENCE_HWID_SIZE);
208 out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE);
209
210 out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
211
212 s_mark_end(s);
213 sec_send(s, sec_flags);
214 }
215
216 /* Parse an authentication request packet */
217 static BOOL
218 licence_parse_authreq(STREAM s, uint8 ** token, uint8 ** signature)
219 {
220 uint16 tokenlen;
221
222 in_uint8s(s, 6); /* unknown: f8 3d 15 00 04 f6 */
223
224 in_uint16_le(s, tokenlen);
225 if (tokenlen != LICENCE_TOKEN_SIZE)
226 {
227 error("token len %d\n", tokenlen);
228 return False;
229 }
230
231 in_uint8p(s, *token, tokenlen);
232 in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE);
233
234 return s_check_end(s);
235 }
236
237 /* Process an authentication request packet */
238 static void
239 licence_process_authreq(STREAM s)
240 {
241 uint8 *in_token = NULL, *in_sig;
242 uint8 out_token[LICENCE_TOKEN_SIZE], decrypt_token[LICENCE_TOKEN_SIZE];
243 uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
244 uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
245 uint8 out_sig[LICENCE_SIGNATURE_SIZE];
246 void * crypt_key;
247
248 /* Parse incoming packet and save the encrypted token */
249 licence_parse_authreq(s, &in_token, &in_sig);
250 memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
251
252 /* Decrypt the token. It should read TEST in Unicode. */
253 crypt_key = ssl_rc4_info_create();
254 ssl_rc4_set_key(crypt_key, (char *)g_licence_key, 16);
255 ssl_rc4_crypt(crypt_key, (char *)in_token, (char *)decrypt_token, LICENCE_TOKEN_SIZE);
256 ssl_rc4_info_delete(crypt_key);
257
258 /* Generate a signature for a buffer of token and HWID */
259 licence_generate_hwid(hwid);
260 memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
261 memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
262 sec_sign(out_sig, 16, g_licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
263
264 /* Now encrypt the HWID */
265 crypt_key = ssl_rc4_info_create();
266 ssl_rc4_set_key(crypt_key, (char *)g_licence_key, 16);
267 ssl_rc4_crypt(crypt_key, (char *)hwid, (char *)crypt_hwid, LICENCE_HWID_SIZE);
268 ssl_rc4_info_delete(crypt_key);
269
270 licence_send_authresp(out_token, crypt_hwid, out_sig);
271 }
272
273 /* Process an licence issue packet */
274 static void
275 licence_process_issue(STREAM s)
276 {
277 void * crypt_key;
278 uint32 length;
279 uint16 check;
280 int i;
281
282 in_uint8s(s, 2); /* 3d 45 - unknown */
283 in_uint16_le(s, length);
284 if (!s_check_rem(s, length))
285 return;
286
287 crypt_key = ssl_rc4_info_create();
288 ssl_rc4_set_key(crypt_key, (char *)g_licence_key, 16);
289 ssl_rc4_crypt(crypt_key, (char *)s->p, (char *)s->p, length);
290 ssl_rc4_info_delete(crypt_key);
291
292 in_uint16(s, check);
293 if (check != 0)
294 return;
295
296 g_licence_issued = True;
297
298 in_uint8s(s, 2); /* pad */
299
300 /* advance to fourth string */
301 length = 0;
302 for (i = 0; i < 4; i++)
303 {
304 in_uint8s(s, length);
305 in_uint32_le(s, length);
306 if (!s_check_rem(s, length))
307 return;
308 }
309
310 g_licence_issued = True;
311 save_licence(s->p, length);
312 }
313
314 /* Process a licence packet */
315 void
316 licence_process(STREAM s)
317 {
318 uint8 tag;
319
320 in_uint8(s, tag);
321 in_uint8s(s, 3); /* version, length */
322
323 switch (tag)
324 {
325 case LICENCE_TAG_DEMAND:
326 licence_process_demand(s);
327 break;
328
329 case LICENCE_TAG_AUTHREQ:
330 licence_process_authreq(s);
331 break;
332
333 case LICENCE_TAG_ISSUE:
334 licence_process_issue(s);
335 break;
336
337 case LICENCE_TAG_REISSUE:
338 case LICENCE_TAG_RESULT:
339 break;
340
341 default:
342 unimpl("licence tag 0x%x\n", tag);
343 }
344 }