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