1 | /* -*- c-basic-offset: 8 -*-
|
---|
2 | rdesktop: A Remote Desktop Protocol client.
|
---|
3 | RDP licensing negotiation
|
---|
4 | Copyright (C) Matthew Chapman <matthewc.unsw.edu.au> 1999-2008
|
---|
5 | Copyright (C) Thomas Uhle <[email protected]> 2011
|
---|
6 | Copyright (C) Henrik Andersson <[email protected]> 2014
|
---|
7 |
|
---|
8 |
|
---|
9 | This program is free software: you can redistribute it and/or modify
|
---|
10 | it under the terms of the GNU General Public License as published by
|
---|
11 | the Free Software Foundation, either version 3 of the License, or
|
---|
12 | (at your option) any later version.
|
---|
13 |
|
---|
14 | This program is distributed in the hope that it will be useful,
|
---|
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | GNU General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU General Public License
|
---|
20 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "rdesktop.h"
|
---|
24 | #include "ssl.h"
|
---|
25 |
|
---|
26 | extern char *g_username;
|
---|
27 | extern char g_hostname[16];
|
---|
28 | extern RDP_VERSION g_rdp_version;
|
---|
29 |
|
---|
30 | static uint8 g_licence_key[16];
|
---|
31 | static uint8 g_licence_sign_key[16];
|
---|
32 |
|
---|
33 | RD_BOOL g_licence_issued = False;
|
---|
34 | RD_BOOL g_licence_error_result = False;
|
---|
35 |
|
---|
36 | /* Generate a session key and RC4 keys, given client and server randoms */
|
---|
37 | static void
|
---|
38 | licence_generate_keys(uint8 * client_random, uint8 * server_random, uint8 * pre_master_secret)
|
---|
39 | {
|
---|
40 | uint8 master_secret[48];
|
---|
41 | uint8 key_block[48];
|
---|
42 |
|
---|
43 | /* Generate master secret and then key material */
|
---|
44 | sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A');
|
---|
45 | sec_hash_48(key_block, master_secret, server_random, client_random, 'A');
|
---|
46 |
|
---|
47 | /* Store first 16 bytes of session key as MAC secret */
|
---|
48 | memcpy(g_licence_sign_key, key_block, 16);
|
---|
49 |
|
---|
50 | /* Generate RC4 key from next 16 bytes */
|
---|
51 | sec_hash_16(g_licence_key, &key_block[16], client_random, server_random);
|
---|
52 | }
|
---|
53 |
|
---|
54 | static void
|
---|
55 | licence_generate_hwid(uint8 * hwid)
|
---|
56 | {
|
---|
57 | buf_out_uint32(hwid, 2);
|
---|
58 | strncpy((char *) (hwid + 4), g_hostname, LICENCE_HWID_SIZE - 4);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /* Send a lincece info packet to server */
|
---|
62 | static void
|
---|
63 | licence_info(uint8 * client_random, uint8 * rsa_data,
|
---|
64 | uint8 * licence_data, int licence_size, uint8 * hwid, uint8 * signature)
|
---|
65 | {
|
---|
66 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
67 | uint16 length =
|
---|
68 | 24 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE +
|
---|
69 | licence_size + LICENCE_HWID_SIZE + LICENCE_SIGNATURE_SIZE;
|
---|
70 | STREAM s;
|
---|
71 |
|
---|
72 | s = sec_init(sec_flags, length + 2);
|
---|
73 |
|
---|
74 | out_uint8(s, LICENCE_TAG_LICENCE_INFO);
|
---|
75 | out_uint8(s, ((g_rdp_version >= RDP_V5) ? 3 : 2)); /* version */
|
---|
76 | out_uint16_le(s, length);
|
---|
77 |
|
---|
78 | out_uint32_le(s, 1);
|
---|
79 | out_uint16(s, 0);
|
---|
80 | out_uint16_le(s, 0x0201);
|
---|
81 |
|
---|
82 | out_uint8p(s, client_random, SEC_RANDOM_SIZE);
|
---|
83 | out_uint16_le(s, 2);
|
---|
84 | out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
|
---|
85 | out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
|
---|
86 | out_uint8s(s, SEC_PADDING_SIZE);
|
---|
87 |
|
---|
88 | out_uint16_le(s, 1);
|
---|
89 | out_uint16_le(s, licence_size);
|
---|
90 | out_uint8p(s, licence_data, licence_size);
|
---|
91 |
|
---|
92 | out_uint16_le(s, 1);
|
---|
93 | out_uint16_le(s, LICENCE_HWID_SIZE);
|
---|
94 | out_uint8p(s, hwid, LICENCE_HWID_SIZE);
|
---|
95 |
|
---|
96 | out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
|
---|
97 |
|
---|
98 | s_mark_end(s);
|
---|
99 | sec_send(s, sec_flags);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* Send a new licence request packet */
|
---|
103 | static void
|
---|
104 | licence_send_new_licence_request(uint8 * client_random, uint8 * rsa_data, char *user, char *host)
|
---|
105 | {
|
---|
106 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
107 | uint16 userlen = strlen(user) + 1;
|
---|
108 | uint16 hostlen = strlen(host) + 1;
|
---|
109 | uint16 length =
|
---|
110 | 24 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE + userlen + hostlen;
|
---|
111 | STREAM s;
|
---|
112 |
|
---|
113 | s = sec_init(sec_flags, length + 2);
|
---|
114 |
|
---|
115 | out_uint8(s, LICENCE_TAG_NEW_LICENCE_REQUEST);
|
---|
116 | out_uint8(s, ((g_rdp_version >= RDP_V5) ? 3 : 2)); /* version */
|
---|
117 | out_uint16_le(s, length);
|
---|
118 |
|
---|
119 | out_uint32_le(s, 1); // KEY_EXCHANGE_ALG_RSA
|
---|
120 | out_uint16(s, 0);
|
---|
121 | out_uint16_le(s, 0xff01);
|
---|
122 |
|
---|
123 | out_uint8p(s, client_random, SEC_RANDOM_SIZE);
|
---|
124 | out_uint16_le(s, 2);
|
---|
125 | out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
|
---|
126 | out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
|
---|
127 | out_uint8s(s, SEC_PADDING_SIZE);
|
---|
128 |
|
---|
129 | /* Username LICENSE_BINARY_BLOB */
|
---|
130 | out_uint16_le(s, BB_CLIENT_USER_NAME_BLOB);
|
---|
131 | out_uint16_le(s, userlen);
|
---|
132 | out_uint8p(s, user, userlen);
|
---|
133 |
|
---|
134 | /* Machinename LICENSE_BINARY_BLOB */
|
---|
135 | out_uint16_le(s, BB_CLIENT_MACHINE_NAME_BLOB);
|
---|
136 | out_uint16_le(s, hostlen);
|
---|
137 | out_uint8p(s, host, hostlen);
|
---|
138 |
|
---|
139 | s_mark_end(s);
|
---|
140 | sec_send(s, sec_flags);
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Process a licence request packet */
|
---|
144 | static void
|
---|
145 | licence_process_request(STREAM s)
|
---|
146 | {
|
---|
147 | uint8 null_data[SEC_MODULUS_SIZE];
|
---|
148 | uint8 *server_random;
|
---|
149 | uint8 signature[LICENCE_SIGNATURE_SIZE];
|
---|
150 | uint8 hwid[LICENCE_HWID_SIZE];
|
---|
151 | uint8 *licence_data;
|
---|
152 | int licence_size;
|
---|
153 | RDSSL_RC4 crypt_key;
|
---|
154 |
|
---|
155 | /* Retrieve the server random from the incoming packet */
|
---|
156 | in_uint8p(s, server_random, SEC_RANDOM_SIZE);
|
---|
157 |
|
---|
158 | /* We currently use null client keys. This is a bit naughty but, hey,
|
---|
159 | the security of licence negotiation isn't exactly paramount. */
|
---|
160 | memset(null_data, 0, sizeof(null_data));
|
---|
161 | licence_generate_keys(null_data, server_random, null_data);
|
---|
162 |
|
---|
163 | licence_size = load_licence(&licence_data);
|
---|
164 | if (licence_size > 0)
|
---|
165 | {
|
---|
166 | /* Generate a signature for the HWID buffer */
|
---|
167 | licence_generate_hwid(hwid);
|
---|
168 | sec_sign(signature, 16, g_licence_sign_key, 16, hwid, sizeof(hwid));
|
---|
169 |
|
---|
170 | /* Now encrypt the HWID */
|
---|
171 | rdssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
172 | rdssl_rc4_crypt(&crypt_key, hwid, hwid, sizeof(hwid));
|
---|
173 |
|
---|
174 | #if WITH_DEBUG
|
---|
175 | DEBUG(("Sending licensing PDU (message type 0x%02x)\n", LICENCE_TAG_LICENCE_INFO));
|
---|
176 | #endif
|
---|
177 | licence_info(null_data, null_data, licence_data, licence_size, hwid, signature);
|
---|
178 |
|
---|
179 | xfree(licence_data);
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | #if WITH_DEBUG
|
---|
184 | DEBUG(("Sending licensing PDU (message type 0x%02x)\n", LICENCE_TAG_NEW_LICENCE_REQUEST));
|
---|
185 | #endif
|
---|
186 | licence_send_new_licence_request(null_data, null_data, g_username, g_hostname);
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* Send a platform challange response packet */
|
---|
190 | static void
|
---|
191 | licence_send_platform_challange_response(uint8 * token, uint8 * crypt_hwid, uint8 * signature)
|
---|
192 | {
|
---|
193 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
194 | uint16 length = 58;
|
---|
195 | STREAM s;
|
---|
196 |
|
---|
197 | s = sec_init(sec_flags, length + 2);
|
---|
198 |
|
---|
199 | out_uint8(s, LICENCE_TAG_PLATFORM_CHALLANGE_RESPONSE);
|
---|
200 | out_uint8(s, ((g_rdp_version >= RDP_V5) ? 3 : 2)); /* version */
|
---|
201 | out_uint16_le(s, length);
|
---|
202 |
|
---|
203 | out_uint16_le(s, 1);
|
---|
204 | out_uint16_le(s, LICENCE_TOKEN_SIZE);
|
---|
205 | out_uint8p(s, token, LICENCE_TOKEN_SIZE);
|
---|
206 |
|
---|
207 | out_uint16_le(s, 1);
|
---|
208 | out_uint16_le(s, LICENCE_HWID_SIZE);
|
---|
209 | out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE);
|
---|
210 |
|
---|
211 | out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
|
---|
212 |
|
---|
213 | s_mark_end(s);
|
---|
214 | sec_send(s, sec_flags);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* Parse an platform challange request packet */
|
---|
218 | static RD_BOOL
|
---|
219 | licence_parse_platform_challange(STREAM s, uint8 ** token, uint8 ** signature)
|
---|
220 | {
|
---|
221 | uint16 tokenlen;
|
---|
222 |
|
---|
223 | in_uint8s(s, 6); /* unknown: f8 3d 15 00 04 f6 */
|
---|
224 |
|
---|
225 | in_uint16_le(s, tokenlen);
|
---|
226 | if (tokenlen != LICENCE_TOKEN_SIZE)
|
---|
227 | {
|
---|
228 | error("token len %d\n", tokenlen);
|
---|
229 | return False;
|
---|
230 | }
|
---|
231 |
|
---|
232 | in_uint8p(s, *token, tokenlen);
|
---|
233 | in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE);
|
---|
234 |
|
---|
235 | return s_check_end(s);
|
---|
236 | }
|
---|
237 |
|
---|
238 | /* Process a platform challange packet */
|
---|
239 | static void
|
---|
240 | licence_process_platform_challange(STREAM s)
|
---|
241 | {
|
---|
242 | uint8 *in_token = NULL, *in_sig;
|
---|
243 | uint8 out_token[LICENCE_TOKEN_SIZE], decrypt_token[LICENCE_TOKEN_SIZE];
|
---|
244 | uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
|
---|
245 | uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
|
---|
246 | uint8 out_sig[LICENCE_SIGNATURE_SIZE];
|
---|
247 | RDSSL_RC4 crypt_key;
|
---|
248 |
|
---|
249 | /* Parse incoming packet and save the encrypted token */
|
---|
250 | licence_parse_platform_challange(s, &in_token, &in_sig);
|
---|
251 | memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
|
---|
252 |
|
---|
253 | /* Decrypt the token. It should read TEST in Unicode. */
|
---|
254 | rdssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
255 | rdssl_rc4_crypt(&crypt_key, in_token, decrypt_token, LICENCE_TOKEN_SIZE);
|
---|
256 |
|
---|
257 | /* Generate a signature for a buffer of token and HWID */
|
---|
258 | licence_generate_hwid(hwid);
|
---|
259 | memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
|
---|
260 | memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
|
---|
261 | sec_sign(out_sig, 16, g_licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
|
---|
262 |
|
---|
263 | /* Now encrypt the HWID */
|
---|
264 | rdssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
265 | rdssl_rc4_crypt(&crypt_key, hwid, crypt_hwid, LICENCE_HWID_SIZE);
|
---|
266 |
|
---|
267 | licence_send_platform_challange_response(out_token, crypt_hwid, out_sig);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /* Process a new licence packet */
|
---|
271 | static void
|
---|
272 | licence_process_new_license(STREAM s)
|
---|
273 | {
|
---|
274 | RDSSL_RC4 crypt_key;
|
---|
275 | uint32 length;
|
---|
276 | int i;
|
---|
277 |
|
---|
278 | in_uint8s(s, 2); // Skip license binary blob type
|
---|
279 | in_uint16_le(s, length);
|
---|
280 | if (!s_check_rem(s, length))
|
---|
281 | return;
|
---|
282 |
|
---|
283 | rdssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
284 | rdssl_rc4_crypt(&crypt_key, s->p, s->p, length);
|
---|
285 |
|
---|
286 | /* Parse NEW_LICENSE_INFO block */
|
---|
287 | in_uint8s(s, 4); // skip dwVersion
|
---|
288 |
|
---|
289 | /* Skip strings, Scope, CompanyName and ProductId to get
|
---|
290 | to the LicenseInfo which we store in license blob. */
|
---|
291 | length = 0;
|
---|
292 | for (i = 0; i < 4; i++)
|
---|
293 | {
|
---|
294 | in_uint8s(s, length);
|
---|
295 | in_uint32_le(s, length);
|
---|
296 | if (!s_check_rem(s, length))
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 | g_licence_issued = True;
|
---|
301 | save_licence(s->p, length);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /* process a licence error alert packet */
|
---|
305 | void
|
---|
306 | licence_process_error_alert(STREAM s)
|
---|
307 | {
|
---|
308 | uint32 error_code;
|
---|
309 | uint32 state_transition;
|
---|
310 | uint32 error_info;
|
---|
311 | in_uint32(s, error_code);
|
---|
312 | in_uint32(s, state_transition);
|
---|
313 | in_uint32(s, error_info);
|
---|
314 |
|
---|
315 | /* There is a special case in the error alert handling, when licensing is all good
|
---|
316 | and the server is not sending a license to client, a "Server License Error PDU -
|
---|
317 | Valid Client" packet is sent which means, every thing is ok.
|
---|
318 |
|
---|
319 | Therefor we should flag that everything is ok with license here.
|
---|
320 | */
|
---|
321 | if (error_code == 0x07)
|
---|
322 | {
|
---|
323 | g_licence_issued = True;
|
---|
324 | return;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /* handle error codes, for now, jsut report them */
|
---|
328 | switch (error_code)
|
---|
329 | {
|
---|
330 | case 0x6: // ERR_NO_LICENSE_SERVER
|
---|
331 | warning("License error alert from server: No license server\n");
|
---|
332 | break;
|
---|
333 |
|
---|
334 | case 0x8: // ERR_INVALID_CLIENT
|
---|
335 | warning("License error alert from server: Invalid client\n");
|
---|
336 | break;
|
---|
337 |
|
---|
338 | case 0x4: // ERR_INVALID_SCOPE
|
---|
339 | case 0xb: // ERR_INVALID_PRODUCTID
|
---|
340 | case 0xc: // ERR_INVALID_MESSAGE_LENGTH
|
---|
341 | default:
|
---|
342 | warning("License error alert from server: code %u, state transition %u\n",
|
---|
343 | error_code, state_transition);
|
---|
344 | break;
|
---|
345 | }
|
---|
346 |
|
---|
347 | g_licence_error_result = True;
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | /* Process a licence packet */
|
---|
352 | void
|
---|
353 | licence_process(STREAM s)
|
---|
354 | {
|
---|
355 | uint8 tag;
|
---|
356 |
|
---|
357 | in_uint8(s, tag);
|
---|
358 | in_uint8s(s, 3); /* version, length */
|
---|
359 |
|
---|
360 | #if WITH_DEBUG
|
---|
361 | DEBUG(("Received licensing PDU (message type 0x%02x)\n", tag));
|
---|
362 | #endif
|
---|
363 |
|
---|
364 | switch (tag)
|
---|
365 | {
|
---|
366 | case LICENCE_TAG_REQUEST:
|
---|
367 | licence_process_request(s);
|
---|
368 | break;
|
---|
369 |
|
---|
370 | case LICENCE_TAG_PLATFORM_CHALLANGE:
|
---|
371 | licence_process_platform_challange(s);
|
---|
372 | break;
|
---|
373 |
|
---|
374 | case LICENCE_TAG_NEW_LICENCE:
|
---|
375 | case LICENCE_TAG_UPGRADE_LICENCE:
|
---|
376 | /* we can handle new and upgrades of licences the same way. */
|
---|
377 | licence_process_new_license(s);
|
---|
378 | break;
|
---|
379 |
|
---|
380 | case LICENCE_TAG_ERROR_ALERT:
|
---|
381 | licence_process_error_alert(s);
|
---|
382 | break;
|
---|
383 |
|
---|
384 | default:
|
---|
385 | unimpl("licence tag 0x%02x\n", tag);
|
---|
386 | }
|
---|
387 | }
|
---|