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