1 |
/* |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
RDP licensing negotiation |
4 |
Copyright (C) Matthew Chapman 1999-2000 |
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 |
17 |
along with this program; if not, write to the Free Software |
18 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include "rdesktop.h" |
22 |
#include "crypto/rc4.h" |
23 |
|
24 |
extern char username[16]; |
25 |
extern char hostname[16]; |
26 |
extern BOOL licence; |
27 |
|
28 |
static uint8 licence_key[16]; |
29 |
static uint8 licence_sign_key[16]; |
30 |
|
31 |
BOOL licence_issued = False; |
32 |
|
33 |
/* Generate a session key and RC4 keys, given client and server randoms */ |
34 |
void |
35 |
licence_generate_keys(uint8 *client_key, uint8 *server_key, uint8 *client_rsa) |
36 |
{ |
37 |
uint8 session_key[48]; |
38 |
uint8 temp_hash[48]; |
39 |
|
40 |
/* Generate session key - two rounds of sec_hash_48 */ |
41 |
sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65); |
42 |
sec_hash_48(session_key, temp_hash, server_key, client_key, 65); |
43 |
|
44 |
/* Store first 16 bytes of session key, for generating signatures */ |
45 |
memcpy(licence_sign_key, session_key, 16); |
46 |
|
47 |
/* Generate RC4 key */ |
48 |
sec_hash_16(licence_key, &session_key[16], client_key, server_key); |
49 |
} |
50 |
|
51 |
/* Send a licence request packet */ |
52 |
static void |
53 |
licence_send_request(uint8 *client_random, uint8 *rsa_data, |
54 |
char *user, char *host) |
55 |
{ |
56 |
uint32 sec_flags = SEC_LICENCE_NEG; |
57 |
uint16 userlen = strlen(user) + 1; |
58 |
uint16 hostlen = strlen(host) + 1; |
59 |
uint16 length = 120 + userlen + hostlen; |
60 |
STREAM s; |
61 |
|
62 |
s = sec_init(sec_flags, length + 2); |
63 |
|
64 |
out_uint16_le(s, LICENCE_TAG_REQUEST); |
65 |
out_uint16_le(s, length); |
66 |
|
67 |
out_uint32_le(s, 1); |
68 |
out_uint32_le(s, 0xff010000); |
69 |
|
70 |
out_uint8p(s, client_random, SEC_RANDOM_SIZE); |
71 |
out_uint16(s, 0); |
72 |
out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE)); |
73 |
out_uint8p(s, rsa_data, SEC_MODULUS_SIZE); |
74 |
out_uint8s(s, SEC_PADDING_SIZE); |
75 |
|
76 |
out_uint16(s, LICENCE_TAG_USER); |
77 |
out_uint16(s, userlen); |
78 |
out_uint8p(s, user, userlen); |
79 |
|
80 |
out_uint16(s, LICENCE_TAG_HOST); |
81 |
out_uint16(s, hostlen); |
82 |
out_uint8p(s, host, hostlen); |
83 |
|
84 |
s_mark_end(s); |
85 |
sec_send(s, sec_flags); |
86 |
} |
87 |
|
88 |
/* Process a licence demand packet */ |
89 |
static void |
90 |
licence_process_demand(STREAM s) |
91 |
{ |
92 |
uint8 null_data[SEC_MODULUS_SIZE]; |
93 |
uint8 *server_random; |
94 |
|
95 |
/* Retrieve the server random from the incoming packet */ |
96 |
in_uint8p(s, server_random, SEC_RANDOM_SIZE); |
97 |
|
98 |
/* We currently use null client keys. This is a bit naughty but, hey, |
99 |
the security of licence negotiation isn't exactly paramount. */ |
100 |
memset(null_data, 0, sizeof(null_data)); |
101 |
licence_generate_keys(null_data, server_random, null_data); |
102 |
|
103 |
/* Send a certificate request back to the server */ |
104 |
licence_send_request(null_data, null_data, username, hostname); |
105 |
} |
106 |
|
107 |
/* Send an authentication response packet */ |
108 |
static void |
109 |
licence_send_authresp(uint8 *token, uint8 *crypt_hwid, uint8 *signature) |
110 |
{ |
111 |
uint32 sec_flags = SEC_LICENCE_NEG; |
112 |
uint16 length = 58; |
113 |
STREAM s; |
114 |
|
115 |
s = sec_init(sec_flags, length + 2); |
116 |
|
117 |
out_uint16_le(s, LICENCE_TAG_AUTHRESP); |
118 |
out_uint16_le(s, length); |
119 |
|
120 |
out_uint16_le(s, 1); |
121 |
out_uint16_le(s, LICENCE_TOKEN_SIZE); |
122 |
out_uint8p(s, token, LICENCE_TOKEN_SIZE); |
123 |
|
124 |
out_uint16_le(s, 1); |
125 |
out_uint16_le(s, LICENCE_HWID_SIZE); |
126 |
out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE); |
127 |
|
128 |
out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE); |
129 |
|
130 |
s_mark_end(s); |
131 |
sec_send(s, sec_flags); |
132 |
} |
133 |
|
134 |
/* Parse an authentication request packet */ |
135 |
static BOOL |
136 |
licence_parse_authreq(STREAM s, uint8 **token, uint8 **signature) |
137 |
{ |
138 |
uint16 tokenlen; |
139 |
|
140 |
in_uint8s(s, 6); /* unknown: f8 3d 15 00 04 f6 */ |
141 |
|
142 |
in_uint16_le(s, tokenlen); |
143 |
if (tokenlen != LICENCE_TOKEN_SIZE) |
144 |
{ |
145 |
ERROR("token len %d\n", tokenlen); |
146 |
return False; |
147 |
} |
148 |
|
149 |
in_uint8p(s, *token, tokenlen); |
150 |
in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE); |
151 |
|
152 |
return s_check_end(s); |
153 |
} |
154 |
|
155 |
/* Process an authentication request packet */ |
156 |
static void |
157 |
licence_process_authreq(STREAM s) |
158 |
{ |
159 |
uint8 *in_token, *in_sig; |
160 |
uint8 out_token[LICENCE_TOKEN_SIZE], |
161 |
decrypt_token[LICENCE_TOKEN_SIZE]; |
162 |
uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE]; |
163 |
uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE]; |
164 |
uint8 out_sig[LICENCE_SIGNATURE_SIZE]; |
165 |
RC4_KEY crypt_key; |
166 |
|
167 |
/* Parse incoming packet and save the encrypted token */ |
168 |
licence_parse_authreq(s, &in_token, &in_sig); |
169 |
memcpy(out_token, in_token, LICENCE_TOKEN_SIZE); |
170 |
|
171 |
/* Decrypt the token. It should read TEST in Unicode. */ |
172 |
RC4_set_key(&crypt_key, 16, licence_key); |
173 |
RC4(&crypt_key, LICENCE_TOKEN_SIZE, in_token, decrypt_token); |
174 |
|
175 |
/* Construct HWID */ |
176 |
buf_out_uint32(hwid, 2); |
177 |
strncpy(hwid + 4, hostname, LICENCE_HWID_SIZE - 4); |
178 |
|
179 |
/* Generate a signature for a buffer of token and HWID */ |
180 |
memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE); |
181 |
memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE); |
182 |
sec_sign(out_sig, licence_sign_key, 16, |
183 |
sealed_buffer, sizeof(sealed_buffer)); |
184 |
|
185 |
/* Deliberately break signature if licencing disabled */ |
186 |
if (!licence) |
187 |
memset(out_sig, 0, sizeof(out_sig)); |
188 |
|
189 |
/* Now encrypt the HWID */ |
190 |
RC4_set_key(&crypt_key, 16, licence_key); |
191 |
RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid); |
192 |
|
193 |
licence_send_authresp(out_token, crypt_hwid, out_sig); |
194 |
} |
195 |
|
196 |
/* Process an licence issue packet */ |
197 |
static void |
198 |
licence_process_issue(STREAM s) |
199 |
{ |
200 |
RC4_KEY crypt_key; |
201 |
uint32 length; |
202 |
uint16 check; |
203 |
|
204 |
in_uint8s(s, 2); /* 3d 45 - unknown */ |
205 |
in_uint16_le(s, length); |
206 |
if (!s_check_rem(s, length)) |
207 |
return; |
208 |
|
209 |
RC4_set_key(&crypt_key, 16, licence_key); |
210 |
RC4(&crypt_key, length, s->p, s->p); |
211 |
|
212 |
in_uint16(s, check); |
213 |
if (check != 0) |
214 |
return; |
215 |
|
216 |
licence_issued = True; |
217 |
|
218 |
/* We should save the licence here */ |
219 |
STATUS("Server issued licence.\n"); |
220 |
} |
221 |
|
222 |
/* Process a licence packet */ |
223 |
void |
224 |
licence_process(STREAM s) |
225 |
{ |
226 |
uint16 tag; |
227 |
|
228 |
in_uint16_le(s, tag); |
229 |
in_uint8s(s, 2); /* length */ |
230 |
|
231 |
switch (tag) |
232 |
{ |
233 |
case LICENCE_TAG_DEMAND: |
234 |
licence_process_demand(s); |
235 |
break; |
236 |
|
237 |
case LICENCE_TAG_AUTHREQ: |
238 |
licence_process_authreq(s); |
239 |
break; |
240 |
|
241 |
case LICENCE_TAG_ISSUE: |
242 |
licence_process_issue(s); |
243 |
break; |
244 |
|
245 |
case LICENCE_TAG_RESULT: |
246 |
break; |
247 |
|
248 |
default: |
249 |
NOTIMP("licence tag 0x%x\n", tag); |
250 |
} |
251 |
} |