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 |
|
---|
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 3 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
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
22 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
23 | * the General Public License version 2 (GPLv2) at this time for any software where
|
---|
24 | * a choice of GPL license versions is made available with the language indicating
|
---|
25 | * that GPLv2 or any later version may be used, or where a choice of which version
|
---|
26 | * of the GPL is applied is otherwise unspecified.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include "rdesktop.h"
|
---|
30 | #include "ssl.h"
|
---|
31 |
|
---|
32 | extern char *g_username;
|
---|
33 | extern char g_hostname[16];
|
---|
34 |
|
---|
35 | static uint8 g_licence_key[16];
|
---|
36 | static uint8 g_licence_sign_key[16];
|
---|
37 |
|
---|
38 | RD_BOOL g_licence_issued = False;
|
---|
39 |
|
---|
40 | /* Generate a session key and RC4 keys, given client and server randoms */
|
---|
41 | static void
|
---|
42 | licence_generate_keys(uint8 * client_random, uint8 * server_random, uint8 * pre_master_secret)
|
---|
43 | {
|
---|
44 | uint8 master_secret[48];
|
---|
45 | uint8 key_block[48];
|
---|
46 |
|
---|
47 | /* Generate master secret and then key material */
|
---|
48 | sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A');
|
---|
49 | sec_hash_48(key_block, master_secret, server_random, client_random, 'A');
|
---|
50 |
|
---|
51 | /* Store first 16 bytes of session key as MAC secret */
|
---|
52 | memcpy(g_licence_sign_key, key_block, 16);
|
---|
53 |
|
---|
54 | /* Generate RC4 key from next 16 bytes */
|
---|
55 | sec_hash_16(g_licence_key, &key_block[16], client_random, server_random);
|
---|
56 | }
|
---|
57 |
|
---|
58 | static void
|
---|
59 | licence_generate_hwid(uint8 * hwid)
|
---|
60 | {
|
---|
61 | buf_out_uint32(hwid, 2);
|
---|
62 | strncpy((char *) (hwid + 4), g_hostname, LICENCE_HWID_SIZE - 4);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Present an existing licence to the server */
|
---|
66 | static void
|
---|
67 | licence_present(uint8 * client_random, uint8 * rsa_data,
|
---|
68 | uint8 * licence_data, int licence_size, uint8 * hwid, uint8 * signature)
|
---|
69 | {
|
---|
70 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
71 | uint16 length =
|
---|
72 | 16 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE +
|
---|
73 | licence_size + LICENCE_HWID_SIZE + LICENCE_SIGNATURE_SIZE;
|
---|
74 | STREAM s;
|
---|
75 |
|
---|
76 | s = sec_init(sec_flags, length + 4);
|
---|
77 |
|
---|
78 | out_uint8(s, LICENCE_TAG_PRESENT);
|
---|
79 | out_uint8(s, 2); /* version */
|
---|
80 | out_uint16_le(s, length);
|
---|
81 |
|
---|
82 | out_uint32_le(s, 1);
|
---|
83 | out_uint16(s, 0);
|
---|
84 | out_uint16_le(s, 0x0201);
|
---|
85 |
|
---|
86 | out_uint8p(s, client_random, SEC_RANDOM_SIZE);
|
---|
87 | out_uint16(s, 0);
|
---|
88 | out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
|
---|
89 | out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
|
---|
90 | out_uint8s(s, SEC_PADDING_SIZE);
|
---|
91 |
|
---|
92 | out_uint16_le(s, 1);
|
---|
93 | out_uint16_le(s, licence_size);
|
---|
94 | out_uint8p(s, licence_data, licence_size);
|
---|
95 |
|
---|
96 | out_uint16_le(s, 1);
|
---|
97 | out_uint16_le(s, LICENCE_HWID_SIZE);
|
---|
98 | out_uint8p(s, hwid, LICENCE_HWID_SIZE);
|
---|
99 |
|
---|
100 | out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
|
---|
101 |
|
---|
102 | s_mark_end(s);
|
---|
103 | sec_send(s, sec_flags);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /* Send a licence request packet */
|
---|
107 | static void
|
---|
108 | licence_send_request(uint8 * client_random, uint8 * rsa_data, char *user, char *host)
|
---|
109 | {
|
---|
110 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
111 | uint16 userlen = strlen(user) + 1;
|
---|
112 | uint16 hostlen = strlen(host) + 1;
|
---|
113 | uint16 length = 128 + userlen + hostlen;
|
---|
114 | STREAM s;
|
---|
115 |
|
---|
116 | s = sec_init(sec_flags, length + 2);
|
---|
117 |
|
---|
118 | out_uint8(s, LICENCE_TAG_REQUEST);
|
---|
119 | out_uint8(s, 2); /* version */
|
---|
120 | out_uint16_le(s, length);
|
---|
121 |
|
---|
122 | out_uint32_le(s, 1);
|
---|
123 | out_uint16(s, 0);
|
---|
124 | out_uint16_le(s, 0xff01);
|
---|
125 |
|
---|
126 | out_uint8p(s, client_random, SEC_RANDOM_SIZE);
|
---|
127 | out_uint16(s, 0);
|
---|
128 | out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
|
---|
129 | out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
|
---|
130 | out_uint8s(s, SEC_PADDING_SIZE);
|
---|
131 |
|
---|
132 | out_uint16_le(s, LICENCE_TAG_USER);
|
---|
133 | out_uint16_le(s, userlen);
|
---|
134 | out_uint8p(s, user, userlen);
|
---|
135 |
|
---|
136 | out_uint16_le(s, LICENCE_TAG_HOST);
|
---|
137 | out_uint16_le(s, hostlen);
|
---|
138 | out_uint8p(s, host, hostlen);
|
---|
139 |
|
---|
140 | s_mark_end(s);
|
---|
141 | sec_send(s, sec_flags);
|
---|
142 | }
|
---|
143 |
|
---|
144 | /* Process a licence demand packet */
|
---|
145 | static void
|
---|
146 | licence_process_demand(STREAM s)
|
---|
147 | {
|
---|
148 | uint8 null_data[SEC_MODULUS_SIZE];
|
---|
149 | uint8 *server_random;
|
---|
150 | uint8 signature[LICENCE_SIGNATURE_SIZE];
|
---|
151 | uint8 hwid[LICENCE_HWID_SIZE];
|
---|
152 | uint8 *licence_data;
|
---|
153 | int licence_size;
|
---|
154 | SSL_RC4 crypt_key;
|
---|
155 |
|
---|
156 | /* Retrieve the server random from the incoming packet */
|
---|
157 | in_uint8p(s, server_random, SEC_RANDOM_SIZE);
|
---|
158 |
|
---|
159 | /* We currently use null client keys. This is a bit naughty but, hey,
|
---|
160 | the security of licence negotiation isn't exactly paramount. */
|
---|
161 | memset(null_data, 0, sizeof(null_data));
|
---|
162 | licence_generate_keys(null_data, server_random, null_data);
|
---|
163 |
|
---|
164 | licence_size = load_licence(&licence_data);
|
---|
165 | if (licence_size > 0)
|
---|
166 | {
|
---|
167 | /* Generate a signature for the HWID buffer */
|
---|
168 | licence_generate_hwid(hwid);
|
---|
169 | sec_sign(signature, 16, g_licence_sign_key, 16, hwid, sizeof(hwid));
|
---|
170 |
|
---|
171 | /* Now encrypt the HWID */
|
---|
172 | ssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
173 | ssl_rc4_crypt(&crypt_key, hwid, hwid, sizeof(hwid));
|
---|
174 |
|
---|
175 | licence_present(null_data, null_data, licence_data, licence_size, hwid, signature);
|
---|
176 | xfree(licence_data);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | licence_send_request(null_data, null_data, g_username, g_hostname);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /* Send an authentication response packet */
|
---|
184 | static void
|
---|
185 | licence_send_authresp(uint8 * token, uint8 * crypt_hwid, uint8 * signature)
|
---|
186 | {
|
---|
187 | uint32 sec_flags = SEC_LICENCE_NEG;
|
---|
188 | uint16 length = 58;
|
---|
189 | STREAM s;
|
---|
190 |
|
---|
191 | s = sec_init(sec_flags, length + 2);
|
---|
192 |
|
---|
193 | out_uint8(s, LICENCE_TAG_AUTHRESP);
|
---|
194 | out_uint8(s, 2); /* version */
|
---|
195 | out_uint16_le(s, length);
|
---|
196 |
|
---|
197 | out_uint16_le(s, 1);
|
---|
198 | out_uint16_le(s, LICENCE_TOKEN_SIZE);
|
---|
199 | out_uint8p(s, token, LICENCE_TOKEN_SIZE);
|
---|
200 |
|
---|
201 | out_uint16_le(s, 1);
|
---|
202 | out_uint16_le(s, LICENCE_HWID_SIZE);
|
---|
203 | out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE);
|
---|
204 |
|
---|
205 | out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
|
---|
206 |
|
---|
207 | s_mark_end(s);
|
---|
208 | sec_send(s, sec_flags);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* Parse an authentication request packet */
|
---|
212 | static RD_BOOL
|
---|
213 | licence_parse_authreq(STREAM s, uint8 ** token, uint8 ** signature)
|
---|
214 | {
|
---|
215 | uint16 tokenlen;
|
---|
216 |
|
---|
217 | in_uint8s(s, 6); /* unknown: f8 3d 15 00 04 f6 */
|
---|
218 |
|
---|
219 | in_uint16_le(s, tokenlen);
|
---|
220 | if (tokenlen != LICENCE_TOKEN_SIZE)
|
---|
221 | {
|
---|
222 | error("token len %d\n", tokenlen);
|
---|
223 | return False;
|
---|
224 | }
|
---|
225 |
|
---|
226 | in_uint8p(s, *token, tokenlen);
|
---|
227 | in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE);
|
---|
228 |
|
---|
229 | return s_check_end(s);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* Process an authentication request packet */
|
---|
233 | static void
|
---|
234 | licence_process_authreq(STREAM s)
|
---|
235 | {
|
---|
236 | uint8 *in_token = NULL, *in_sig;
|
---|
237 | uint8 out_token[LICENCE_TOKEN_SIZE], decrypt_token[LICENCE_TOKEN_SIZE];
|
---|
238 | uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
|
---|
239 | uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
|
---|
240 | uint8 out_sig[LICENCE_SIGNATURE_SIZE];
|
---|
241 | SSL_RC4 crypt_key;
|
---|
242 |
|
---|
243 | /* Parse incoming packet and save the encrypted token */
|
---|
244 | licence_parse_authreq(s, &in_token, &in_sig);
|
---|
245 | memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
|
---|
246 |
|
---|
247 | /* Decrypt the token. It should read TEST in Unicode. */
|
---|
248 | ssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
249 | ssl_rc4_crypt(&crypt_key, in_token, decrypt_token, LICENCE_TOKEN_SIZE);
|
---|
250 |
|
---|
251 | /* Generate a signature for a buffer of token and HWID */
|
---|
252 | licence_generate_hwid(hwid);
|
---|
253 | memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
|
---|
254 | memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
|
---|
255 | sec_sign(out_sig, 16, g_licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
|
---|
256 |
|
---|
257 | /* Now encrypt the HWID */
|
---|
258 | ssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
259 | ssl_rc4_crypt(&crypt_key, hwid, crypt_hwid, LICENCE_HWID_SIZE);
|
---|
260 |
|
---|
261 | licence_send_authresp(out_token, crypt_hwid, out_sig);
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* Process an licence issue packet */
|
---|
265 | static void
|
---|
266 | licence_process_issue(STREAM s)
|
---|
267 | {
|
---|
268 | SSL_RC4 crypt_key;
|
---|
269 | uint32 length;
|
---|
270 | uint16 check;
|
---|
271 | int i;
|
---|
272 |
|
---|
273 | in_uint8s(s, 2); /* 3d 45 - unknown */
|
---|
274 | in_uint16_le(s, length);
|
---|
275 | if (!s_check_rem(s, length))
|
---|
276 | return;
|
---|
277 |
|
---|
278 | ssl_rc4_set_key(&crypt_key, g_licence_key, 16);
|
---|
279 | ssl_rc4_crypt(&crypt_key, s->p, s->p, length);
|
---|
280 |
|
---|
281 | in_uint16(s, check);
|
---|
282 | if (check != 0)
|
---|
283 | return;
|
---|
284 |
|
---|
285 | g_licence_issued = True;
|
---|
286 |
|
---|
287 | in_uint8s(s, 2); /* pad */
|
---|
288 |
|
---|
289 | /* advance to fourth string */
|
---|
290 | length = 0;
|
---|
291 | for (i = 0; i < 4; i++)
|
---|
292 | {
|
---|
293 | in_uint8s(s, length);
|
---|
294 | in_uint32_le(s, length);
|
---|
295 | if (!s_check_rem(s, length))
|
---|
296 | return;
|
---|
297 | }
|
---|
298 |
|
---|
299 | g_licence_issued = True;
|
---|
300 | save_licence(s->p, length);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Process a licence packet */
|
---|
304 | void
|
---|
305 | licence_process(STREAM s)
|
---|
306 | {
|
---|
307 | uint8 tag;
|
---|
308 |
|
---|
309 | in_uint8(s, tag);
|
---|
310 | in_uint8s(s, 3); /* version, length */
|
---|
311 |
|
---|
312 | switch (tag)
|
---|
313 | {
|
---|
314 | case LICENCE_TAG_DEMAND:
|
---|
315 | licence_process_demand(s);
|
---|
316 | break;
|
---|
317 |
|
---|
318 | case LICENCE_TAG_AUTHREQ:
|
---|
319 | licence_process_authreq(s);
|
---|
320 | break;
|
---|
321 |
|
---|
322 | case LICENCE_TAG_ISSUE:
|
---|
323 | licence_process_issue(s);
|
---|
324 | break;
|
---|
325 |
|
---|
326 | case LICENCE_TAG_REISSUE:
|
---|
327 | case LICENCE_TAG_RESULT:
|
---|
328 | break;
|
---|
329 |
|
---|
330 | default:
|
---|
331 | unimpl("licence tag 0x%x\n", tag);
|
---|
332 | }
|
---|
333 | }
|
---|