/[rdesktop]/sourceforge.net/trunk/rdesktop/licence.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/licence.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations)
Tue Aug 15 10:23:24 2000 UTC (23 years, 8 months ago) by matty
File MIME type: text/plain
File size: 6474 byte(s)
Major commit of work from laptop - done in various free moments.
Implemented encryption layer and some basic licensing negotiation.
Reorganised code somewhat. While this is not quite as clean, it is
a lot faster - our parser speed was becoming a bottle-neck.

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], decrypt_token[LICENCE_TOKEN_SIZE];
155 uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
156 uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
157 uint8 out_sig[LICENCE_SIGNATURE_SIZE];
158 RC4_KEY crypt_key;
159
160 /* Parse incoming packet and save the encrypted token */
161 licence_parse_authreq(s, &in_token, &in_sig);
162 memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
163
164 /* Decrypt the token. It should read TEST in Unicode. */
165 RC4_set_key(&crypt_key, 16, licence_key);
166 RC4(&crypt_key, LICENCE_TOKEN_SIZE, in_token, decrypt_token);
167
168 /* Construct HWID */
169 buf_out_uint32(hwid, 2);
170 strncpy(hwid + 4, hostname, LICENCE_HWID_SIZE - 4);
171
172 /* Generate a signature for a buffer of token and HWID */
173 memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
174 memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
175 sec_sign(out_sig, licence_sign_key, 16,
176 sealed_buffer, sizeof(sealed_buffer));
177
178 /* Deliberately break signature if licencing disabled */
179 if (!licence)
180 memset(out_sig, 0, sizeof(out_sig));
181
182 /* Now encrypt the HWID */
183 RC4_set_key(&crypt_key, 16, licence_key);
184 RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid);
185
186 licence_send_authresp(out_token, crypt_hwid, out_sig);
187 }
188
189 /* Process an licence issue packet */
190 static void licence_process_issue(STREAM s)
191 {
192 RC4_KEY crypt_key;
193 uint32 length;
194 uint16 check;
195
196 in_uint8s(s, 2); /* 3d 45 - unknown */
197 in_uint16_le(s, length);
198 if (!s_check_rem(s, length))
199 return;
200
201 RC4_set_key(&crypt_key, 16, licence_key);
202 RC4(&crypt_key, length, s->p, s->p);
203
204 in_uint16(s, check);
205 if (check != 0)
206 return;
207
208 /* We should save the licence here */
209 STATUS("Server issued licence.\n");
210 }
211
212 /* Process a licence packet */
213 void licence_process(STREAM s)
214 {
215 uint16 tag;
216
217 in_uint16_le(s, tag);
218 in_uint8s(s, 2); /* length */
219
220 switch (tag)
221 {
222 case LICENCE_TAG_DEMAND:
223 licence_process_demand(s);
224 break;
225
226 case LICENCE_TAG_AUTHREQ:
227 licence_process_authreq(s);
228 break;
229
230 case LICENCE_TAG_ISSUE:
231 licence_process_issue(s);
232 break;
233
234 case LICENCE_TAG_RESULT:
235 break;
236
237 default:
238 NOTIMP("licence tag 0x%x\n", tag);
239 }
240 }

  ViewVC Help
Powered by ViewVC 1.1.26