1 /* -*- c-basic-offset: 8 -*-
2 rdesktop: A Remote Desktop Protocol client.
3 RDP licensing negotiation
4 Copyright (C) Matthew Chapman 1999-2005
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 ssl_rc4_info_create(void);
26 ssl_rc4_info_delete(void * rc4_info
);
28 ssl_rc4_set_key(void * rc4_info
, char * key
, int len
);
30 ssl_rc4_crypt(void * rc4_info
, char * in_data
, char * out_data
, int len
);
32 ssl_mod_exp(char* out
, int out_len
, char* in
, int in_len
,
33 char* mod
, int mod_len
, char* exp
, int exp_len
);
35 extern char g_username
[];
36 extern char g_hostname
[];
38 static uint8 g_licence_key
[16];
39 static uint8 g_licence_sign_key
[16];
41 BOOL g_licence_issued
= False
;
43 /* Generate a session key and RC4 keys, given client and server randoms */
45 licence_generate_keys(uint8
* client_random
, uint8
* server_random
, uint8
* pre_master_secret
)
47 uint8 master_secret
[48];
50 /* Generate master secret and then key material */
51 sec_hash_48(master_secret
, pre_master_secret
, client_random
, server_random
, 'A');
52 sec_hash_48(key_block
, master_secret
, server_random
, client_random
, 'A');
54 /* Store first 16 bytes of session key as MAC secret */
55 memcpy(g_licence_sign_key
, key_block
, 16);
57 /* Generate RC4 key from next 16 bytes */
58 sec_hash_16(g_licence_key
, &key_block
[16], client_random
, server_random
);
62 licence_generate_hwid(uint8
* hwid
)
64 buf_out_uint32(hwid
, 2);
65 strncpy((char *) (hwid
+ 4), g_hostname
, LICENCE_HWID_SIZE
- 4);
68 /* Present an existing licence to the server */
70 licence_present(uint8
* client_random
, uint8
* rsa_data
,
71 uint8
* licence_data
, int licence_size
, uint8
* hwid
, uint8
* signature
)
73 uint32 sec_flags
= SEC_LICENCE_NEG
;
75 16 + SEC_RANDOM_SIZE
+ SEC_MODULUS_SIZE
+ SEC_PADDING_SIZE
+
76 licence_size
+ LICENCE_HWID_SIZE
+ LICENCE_SIGNATURE_SIZE
;
79 s
= sec_init(sec_flags
, length
+ 4);
81 out_uint8(s
, LICENCE_TAG_PRESENT
);
82 out_uint8(s
, 2); /* version */
83 out_uint16_le(s
, length
);
87 out_uint16_le(s
, 0x0201);
89 out_uint8p(s
, client_random
, SEC_RANDOM_SIZE
);
91 out_uint16_le(s
, (SEC_MODULUS_SIZE
+ SEC_PADDING_SIZE
));
92 out_uint8p(s
, rsa_data
, SEC_MODULUS_SIZE
);
93 out_uint8s(s
, SEC_PADDING_SIZE
);
96 out_uint16_le(s
, licence_size
);
97 out_uint8p(s
, licence_data
, licence_size
);
100 out_uint16_le(s
, LICENCE_HWID_SIZE
);
101 out_uint8p(s
, hwid
, LICENCE_HWID_SIZE
);
103 out_uint8p(s
, signature
, LICENCE_SIGNATURE_SIZE
);
106 sec_send(s
, sec_flags
);
109 /* Send a licence request packet */
111 licence_send_request(uint8
* client_random
, uint8
* rsa_data
, char *user
, char *host
)
113 uint32 sec_flags
= SEC_LICENCE_NEG
;
114 uint16 userlen
= strlen(user
) + 1;
115 uint16 hostlen
= strlen(host
) + 1;
116 uint16 length
= 128 + userlen
+ hostlen
;
119 s
= sec_init(sec_flags
, length
+ 2);
121 out_uint8(s
, LICENCE_TAG_REQUEST
);
122 out_uint8(s
, 2); /* version */
123 out_uint16_le(s
, length
);
127 out_uint16_le(s
, 0xff01);
129 out_uint8p(s
, client_random
, SEC_RANDOM_SIZE
);
131 out_uint16_le(s
, (SEC_MODULUS_SIZE
+ SEC_PADDING_SIZE
));
132 out_uint8p(s
, rsa_data
, SEC_MODULUS_SIZE
);
133 out_uint8s(s
, SEC_PADDING_SIZE
);
135 out_uint16_le(s
, LICENCE_TAG_USER
);
136 out_uint16_le(s
, userlen
);
137 out_uint8p(s
, user
, userlen
);
139 out_uint16_le(s
, LICENCE_TAG_HOST
);
140 out_uint16_le(s
, hostlen
);
141 out_uint8p(s
, host
, hostlen
);
144 sec_send(s
, sec_flags
);
147 /* Process a licence demand packet */
149 licence_process_demand(STREAM s
)
151 uint8 null_data
[SEC_MODULUS_SIZE
];
152 uint8
*server_random
;
153 uint8 signature
[LICENCE_SIGNATURE_SIZE
];
154 uint8 hwid
[LICENCE_HWID_SIZE
];
159 /* Retrieve the server random from the incoming packet */
160 in_uint8p(s
, server_random
, SEC_RANDOM_SIZE
);
162 /* We currently use null client keys. This is a bit naughty but, hey,
163 the security of licence negotiation isn't exactly paramount. */
164 memset(null_data
, 0, sizeof(null_data
));
165 licence_generate_keys(null_data
, server_random
, null_data
);
167 licence_size
= load_licence(&licence_data
);
168 if (licence_size
> 0)
170 /* Generate a signature for the HWID buffer */
171 licence_generate_hwid(hwid
);
172 sec_sign(signature
, 16, g_licence_sign_key
, 16, hwid
, sizeof(hwid
));
174 /* Now encrypt the HWID */
175 crypt_key
= ssl_rc4_info_create();
176 ssl_rc4_set_key(crypt_key
, (char *)g_licence_key
, 16);
177 ssl_rc4_crypt(crypt_key
, (char *)hwid
, (char *)hwid
, sizeof(hwid
));
178 ssl_rc4_info_delete(crypt_key
);
180 licence_present(null_data
, null_data
, licence_data
, licence_size
, hwid
, signature
);
185 licence_send_request(null_data
, null_data
, g_username
, g_hostname
);
188 /* Send an authentication response packet */
190 licence_send_authresp(uint8
* token
, uint8
* crypt_hwid
, uint8
* signature
)
192 uint32 sec_flags
= SEC_LICENCE_NEG
;
196 s
= sec_init(sec_flags
, length
+ 2);
198 out_uint8(s
, LICENCE_TAG_AUTHRESP
);
199 out_uint8(s
, 2); /* version */
200 out_uint16_le(s
, length
);
203 out_uint16_le(s
, LICENCE_TOKEN_SIZE
);
204 out_uint8p(s
, token
, LICENCE_TOKEN_SIZE
);
207 out_uint16_le(s
, LICENCE_HWID_SIZE
);
208 out_uint8p(s
, crypt_hwid
, LICENCE_HWID_SIZE
);
210 out_uint8p(s
, signature
, LICENCE_SIGNATURE_SIZE
);
213 sec_send(s
, sec_flags
);
216 /* Parse an authentication request packet */
218 licence_parse_authreq(STREAM s
, uint8
** token
, uint8
** signature
)
222 in_uint8s(s
, 6); /* unknown: f8 3d 15 00 04 f6 */
224 in_uint16_le(s
, tokenlen
);
225 if (tokenlen
!= LICENCE_TOKEN_SIZE
)
227 error("token len %d\n", tokenlen
);
231 in_uint8p(s
, *token
, tokenlen
);
232 in_uint8p(s
, *signature
, LICENCE_SIGNATURE_SIZE
);
234 return s_check_end(s
);
237 /* Process an authentication request packet */
239 licence_process_authreq(STREAM s
)
241 uint8
*in_token
= NULL
, *in_sig
;
242 uint8 out_token
[LICENCE_TOKEN_SIZE
], decrypt_token
[LICENCE_TOKEN_SIZE
];
243 uint8 hwid
[LICENCE_HWID_SIZE
], crypt_hwid
[LICENCE_HWID_SIZE
];
244 uint8 sealed_buffer
[LICENCE_TOKEN_SIZE
+ LICENCE_HWID_SIZE
];
245 uint8 out_sig
[LICENCE_SIGNATURE_SIZE
];
248 /* Parse incoming packet and save the encrypted token */
249 licence_parse_authreq(s
, &in_token
, &in_sig
);
250 memcpy(out_token
, in_token
, LICENCE_TOKEN_SIZE
);
252 /* Decrypt the token. It should read TEST in Unicode. */
253 crypt_key
= ssl_rc4_info_create();
254 ssl_rc4_set_key(crypt_key
, (char *)g_licence_key
, 16);
255 ssl_rc4_crypt(crypt_key
, (char *)in_token
, (char *)decrypt_token
, LICENCE_TOKEN_SIZE
);
256 ssl_rc4_info_delete(crypt_key
);
258 /* Generate a signature for a buffer of token and HWID */
259 licence_generate_hwid(hwid
);
260 memcpy(sealed_buffer
, decrypt_token
, LICENCE_TOKEN_SIZE
);
261 memcpy(sealed_buffer
+ LICENCE_TOKEN_SIZE
, hwid
, LICENCE_HWID_SIZE
);
262 sec_sign(out_sig
, 16, g_licence_sign_key
, 16, sealed_buffer
, sizeof(sealed_buffer
));
264 /* Now encrypt the HWID */
265 crypt_key
= ssl_rc4_info_create();
266 ssl_rc4_set_key(crypt_key
, (char *)g_licence_key
, 16);
267 ssl_rc4_crypt(crypt_key
, (char *)hwid
, (char *)crypt_hwid
, LICENCE_HWID_SIZE
);
268 ssl_rc4_info_delete(crypt_key
);
270 licence_send_authresp(out_token
, crypt_hwid
, out_sig
);
273 /* Process an licence issue packet */
275 licence_process_issue(STREAM s
)
282 in_uint8s(s
, 2); /* 3d 45 - unknown */
283 in_uint16_le(s
, length
);
284 if (!s_check_rem(s
, length
))
287 crypt_key
= ssl_rc4_info_create();
288 ssl_rc4_set_key(crypt_key
, (char *)g_licence_key
, 16);
289 ssl_rc4_crypt(crypt_key
, (char *)s
->p
, (char *)s
->p
, length
);
290 ssl_rc4_info_delete(crypt_key
);
296 g_licence_issued
= True
;
298 in_uint8s(s
, 2); /* pad */
300 /* advance to fourth string */
302 for (i
= 0; i
< 4; i
++)
304 in_uint8s(s
, length
);
305 in_uint32_le(s
, length
);
306 if (!s_check_rem(s
, length
))
310 g_licence_issued
= True
;
311 save_licence(s
->p
, length
);
314 /* Process a licence packet */
316 licence_process(STREAM s
)
321 in_uint8s(s
, 3); /* version, length */
325 case LICENCE_TAG_DEMAND
:
326 licence_process_demand(s
);
329 case LICENCE_TAG_AUTHREQ
:
330 licence_process_authreq(s
);
333 case LICENCE_TAG_ISSUE
:
334 licence_process_issue(s
);
337 case LICENCE_TAG_REISSUE
:
338 case LICENCE_TAG_RESULT
:
342 unimpl("licence tag 0x%x\n", tag
);