/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 320 - (hide annotations)
Mon Feb 10 13:05:40 2003 UTC (21 years, 3 months ago) by astrand
File MIME type: text/plain
File size: 11513 byte(s)
load_licence/save_licence static.

1 matty 10 /*
2     rdesktop: A Remote Desktop Protocol client.
3     RDP licensing negotiation
4 matthewc 207 Copyright (C) Matthew Chapman 1999-2002
5 matty 10
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 jsorg71 60
11 matty 10 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 astrand 317 #include <sys/types.h>
22     #include <sys/stat.h>
23     #include <unistd.h>
24     #include <fcntl.h>
25     #include <errno.h>
26 matty 10 #include "rdesktop.h"
27 astrand 293
28     #ifdef WITH_OPENSSL
29     #include <openssl/rc4.h>
30     #else
31 matty 10 #include "crypto/rc4.h"
32 astrand 293 #endif
33 matty 10
34     extern char username[16];
35     extern char hostname[16];
36    
37     static uint8 licence_key[16];
38     static uint8 licence_sign_key[16];
39    
40 matty 28 BOOL licence_issued = False;
41    
42 astrand 317
43 astrand 320 static int
44 astrand 317 load_licence(unsigned char **data)
45     {
46     char *path;
47     char *home;
48     struct stat st;
49     int fd;
50    
51     home = getenv("HOME");
52     if (home == NULL)
53     return -1;
54    
55     path = xmalloc(strlen(home) + strlen(hostname) + 20);
56     sprintf(path, "%s/.rdesktop/licence.%s", home, hostname);
57    
58     fd = open(path, O_RDONLY);
59     if (fd == -1)
60     return -1;
61    
62     if (fstat(fd, &st))
63     return -1;
64    
65     *data = xmalloc(st.st_size);
66     return read(fd, *data, st.st_size);
67     }
68    
69 astrand 320 static void
70 astrand 317 save_licence(unsigned char *data, int length)
71     {
72     char *fpath; /* file path for licence */
73     char *fname, *fnamewrk; /* file name for licence .inkl path. */
74     char *home;
75     uint32 y;
76     struct flock fnfl;
77     int fnfd, fnwrkfd, i, wlen;
78     struct stream s, *s_ptr;
79     uint32 len;
80    
81     /* Construct a stream, so that we can use macros to extract the
82     * licence.
83     */
84     s_ptr = &s;
85     s_ptr->p = data;
86     /* Skip first two bytes */
87     in_uint16(s_ptr, len);
88    
89     /* Skip three strings */
90     for (i = 0; i < 3; i++)
91     {
92     in_uint32(s_ptr, len);
93     s_ptr->p += len;
94     /* Make sure that we won't be past the end of data after
95     * reading the next length value
96     */
97     if ((s_ptr->p) + 4 > data + length)
98     {
99     printf("Error in parsing licence key.\n");
100 astrand 318 printf("Strings %d end value %x > supplied length (%x)\n", i,
101     (unsigned int) s_ptr->p, (unsigned int) data + length);
102 astrand 317 return;
103     }
104     }
105     in_uint32(s_ptr, len);
106     if (s_ptr->p + len > data + length)
107     {
108     printf("Error in parsing licence key.\n");
109 astrand 318 printf("End of licence %x > supplied length (%x)\n",
110     (unsigned int) s_ptr->p + len, (unsigned int) data + length);
111 astrand 317 return;
112     }
113    
114     home = getenv("HOME");
115     if (home == NULL)
116     return;
117    
118     /* set and create the directory -- if it doesn't exist. */
119     fpath = xmalloc(strlen(home) + 11);
120     STRNCPY(fpath, home, strlen(home) + 1);
121    
122     sprintf(fpath, "%s/.rdesktop", fpath);
123     if (mkdir(fpath, 0700) == -1 && errno != EEXIST)
124     {
125     perror("mkdir");
126     exit(1);
127     }
128    
129     /* set the real licence filename, and put a write lock on it. */
130     fname = xmalloc(strlen(fpath) + strlen(hostname) + 10);
131     sprintf(fname, "%s/licence.%s", fpath, hostname);
132     fnfd = open(fname, O_RDONLY);
133     if (fnfd != -1)
134     {
135     fnfl.l_type = F_WRLCK;
136     fnfl.l_whence = SEEK_SET;
137     fnfl.l_start = 0;
138     fnfl.l_len = 1;
139     fcntl(fnfd, F_SETLK, &fnfl);
140     }
141    
142     /* create a temporary licence file */
143     fnamewrk = xmalloc(strlen(fname) + 12);
144     for (y = 0;; y++)
145     {
146 astrand 318 sprintf(fnamewrk, "%s.%lu", fname, (long unsigned int) y);
147 astrand 317 fnwrkfd = open(fnamewrk, O_WRONLY | O_CREAT | O_EXCL, 0600);
148     if (fnwrkfd == -1)
149     {
150     if (errno == EINTR || errno == EEXIST)
151     continue;
152     perror("create");
153     exit(1);
154     }
155     break;
156     }
157     /* write to the licence file */
158     for (y = 0; y < len;)
159     {
160     do
161     {
162     wlen = write(fnwrkfd, s_ptr->p + y, len - y);
163     }
164     while (wlen == -1 && errno == EINTR);
165     if (wlen < 1)
166     {
167     perror("write");
168     unlink(fnamewrk);
169     exit(1);
170     }
171     y += wlen;
172     }
173    
174     /* close the file and rename it to fname */
175     if (close(fnwrkfd) == -1)
176     {
177     perror("close");
178     unlink(fnamewrk);
179     exit(1);
180     }
181     if (rename(fnamewrk, fname) == -1)
182     {
183     perror("rename");
184     unlink(fnamewrk);
185     exit(1);
186     }
187     /* close the file lock on fname */
188     if (fnfd != -1)
189     {
190     fnfl.l_type = F_UNLCK;
191     fnfl.l_whence = SEEK_SET;
192     fnfl.l_start = 0;
193     fnfl.l_len = 1;
194     fcntl(fnfd, F_SETLK, &fnfl);
195     close(fnfd);
196     }
197    
198     }
199    
200 matty 10 /* Generate a session key and RC4 keys, given client and server randoms */
201 matthewc 39 static void
202 astrand 82 licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)
203 matty 10 {
204     uint8 session_key[48];
205     uint8 temp_hash[48];
206    
207     /* Generate session key - two rounds of sec_hash_48 */
208 matty 24 sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65);
209     sec_hash_48(session_key, temp_hash, server_key, client_key, 65);
210 matty 10
211     /* Store first 16 bytes of session key, for generating signatures */
212 matty 24 memcpy(licence_sign_key, session_key, 16);
213 matty 10
214     /* Generate RC4 key */
215     sec_hash_16(licence_key, &session_key[16], client_key, server_key);
216     }
217    
218 matthewc 39 static void
219 astrand 64 licence_generate_hwid(uint8 * hwid)
220 matthewc 39 {
221     buf_out_uint32(hwid, 2);
222 astrand 77 strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4);
223 matthewc 39 }
224    
225     /* Present an existing licence to the server */
226     static void
227 astrand 64 licence_present(uint8 * client_random, uint8 * rsa_data,
228 astrand 82 uint8 * licence_data, int licence_size, uint8 * hwid, uint8 * signature)
229 matthewc 39 {
230     uint32 sec_flags = SEC_LICENCE_NEG;
231 astrand 64 uint16 length =
232     16 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE +
233     licence_size + LICENCE_HWID_SIZE + LICENCE_SIGNATURE_SIZE;
234 matthewc 39 STREAM s;
235    
236     s = sec_init(sec_flags, length + 4);
237    
238     out_uint16_le(s, LICENCE_TAG_PRESENT);
239     out_uint16_le(s, length);
240    
241     out_uint32_le(s, 1);
242     out_uint16(s, 0);
243     out_uint16_le(s, 0x0201);
244    
245     out_uint8p(s, client_random, SEC_RANDOM_SIZE);
246     out_uint16(s, 0);
247     out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
248     out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
249     out_uint8s(s, SEC_PADDING_SIZE);
250    
251     out_uint16_le(s, 1);
252     out_uint16_le(s, licence_size);
253     out_uint8p(s, licence_data, licence_size);
254    
255     out_uint16_le(s, 1);
256     out_uint16_le(s, LICENCE_HWID_SIZE);
257     out_uint8p(s, hwid, LICENCE_HWID_SIZE);
258    
259     out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
260    
261     s_mark_end(s);
262     sec_send(s, sec_flags);
263     }
264    
265 matty 10 /* Send a licence request packet */
266 matty 25 static void
267 astrand 82 licence_send_request(uint8 * client_random, uint8 * rsa_data, char *user, char *host)
268 matty 10 {
269     uint32 sec_flags = SEC_LICENCE_NEG;
270     uint16 userlen = strlen(user) + 1;
271     uint16 hostlen = strlen(host) + 1;
272 jsorg71 60 uint16 length = 128 + userlen + hostlen;
273 matty 10 STREAM s;
274    
275     s = sec_init(sec_flags, length + 2);
276    
277     out_uint16_le(s, LICENCE_TAG_REQUEST);
278     out_uint16_le(s, length);
279    
280     out_uint32_le(s, 1);
281 matthewc 39 out_uint16(s, 0);
282     out_uint16_le(s, 0xff01);
283 matty 10
284     out_uint8p(s, client_random, SEC_RANDOM_SIZE);
285     out_uint16(s, 0);
286     out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
287     out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
288     out_uint8s(s, SEC_PADDING_SIZE);
289    
290     out_uint16(s, LICENCE_TAG_USER);
291     out_uint16(s, userlen);
292     out_uint8p(s, user, userlen);
293    
294     out_uint16(s, LICENCE_TAG_HOST);
295     out_uint16(s, hostlen);
296     out_uint8p(s, host, hostlen);
297    
298     s_mark_end(s);
299     sec_send(s, sec_flags);
300     }
301    
302     /* Process a licence demand packet */
303 matty 25 static void
304     licence_process_demand(STREAM s)
305 matty 10 {
306     uint8 null_data[SEC_MODULUS_SIZE];
307 matthewc 190 uint8 *server_random;
308     uint8 signature[LICENCE_SIGNATURE_SIZE];
309 matthewc 39 uint8 hwid[LICENCE_HWID_SIZE];
310     uint8 *licence_data;
311     int licence_size;
312     RC4_KEY crypt_key;
313 matty 10
314     /* Retrieve the server random from the incoming packet */
315     in_uint8p(s, server_random, SEC_RANDOM_SIZE);
316    
317     /* We currently use null client keys. This is a bit naughty but, hey,
318     the security of licence negotiation isn't exactly paramount. */
319     memset(null_data, 0, sizeof(null_data));
320     licence_generate_keys(null_data, server_random, null_data);
321    
322 matthewc 39 licence_size = load_licence(&licence_data);
323 matthewc 159 if (licence_size != -1)
324 matthewc 39 {
325 matthewc 159 /* Generate a signature for the HWID buffer */
326     licence_generate_hwid(hwid);
327     sec_sign(signature, 16, licence_sign_key, 16, hwid, sizeof(hwid));
328    
329     /* Now encrypt the HWID */
330     RC4_set_key(&crypt_key, 16, licence_key);
331     RC4(&crypt_key, sizeof(hwid), hwid, hwid);
332    
333     licence_present(null_data, null_data, licence_data, licence_size, hwid, signature);
334     xfree(licence_data);
335 matthewc 39 return;
336     }
337    
338 matthewc 159 licence_send_request(null_data, null_data, username, hostname);
339 matty 10 }
340    
341     /* Send an authentication response packet */
342 matty 25 static void
343 astrand 64 licence_send_authresp(uint8 * token, uint8 * crypt_hwid, uint8 * signature)
344 matty 10 {
345     uint32 sec_flags = SEC_LICENCE_NEG;
346     uint16 length = 58;
347     STREAM s;
348    
349     s = sec_init(sec_flags, length + 2);
350    
351     out_uint16_le(s, LICENCE_TAG_AUTHRESP);
352     out_uint16_le(s, length);
353    
354     out_uint16_le(s, 1);
355     out_uint16_le(s, LICENCE_TOKEN_SIZE);
356     out_uint8p(s, token, LICENCE_TOKEN_SIZE);
357    
358     out_uint16_le(s, 1);
359     out_uint16_le(s, LICENCE_HWID_SIZE);
360     out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE);
361    
362     out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
363    
364     s_mark_end(s);
365     sec_send(s, sec_flags);
366     }
367    
368     /* Parse an authentication request packet */
369 matty 25 static BOOL
370 astrand 64 licence_parse_authreq(STREAM s, uint8 ** token, uint8 ** signature)
371 matty 10 {
372     uint16 tokenlen;
373    
374 matty 24 in_uint8s(s, 6); /* unknown: f8 3d 15 00 04 f6 */
375 matty 10
376     in_uint16_le(s, tokenlen);
377     if (tokenlen != LICENCE_TOKEN_SIZE)
378     {
379 matty 30 error("token len %d\n", tokenlen);
380 matty 10 return False;
381     }
382    
383     in_uint8p(s, *token, tokenlen);
384     in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE);
385    
386     return s_check_end(s);
387     }
388    
389     /* Process an authentication request packet */
390 matty 25 static void
391     licence_process_authreq(STREAM s)
392 matty 10 {
393     uint8 *in_token, *in_sig;
394 astrand 82 uint8 out_token[LICENCE_TOKEN_SIZE], decrypt_token[LICENCE_TOKEN_SIZE];
395 matty 10 uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
396     uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
397     uint8 out_sig[LICENCE_SIGNATURE_SIZE];
398     RC4_KEY crypt_key;
399    
400     /* Parse incoming packet and save the encrypted token */
401     licence_parse_authreq(s, &in_token, &in_sig);
402     memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
403    
404     /* Decrypt the token. It should read TEST in Unicode. */
405     RC4_set_key(&crypt_key, 16, licence_key);
406     RC4(&crypt_key, LICENCE_TOKEN_SIZE, in_token, decrypt_token);
407    
408     /* Generate a signature for a buffer of token and HWID */
409 matthewc 39 licence_generate_hwid(hwid);
410 matty 10 memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
411     memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
412 astrand 82 sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
413 matty 10
414     /* Now encrypt the HWID */
415     RC4_set_key(&crypt_key, 16, licence_key);
416     RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid);
417    
418     licence_send_authresp(out_token, crypt_hwid, out_sig);
419     }
420    
421     /* Process an licence issue packet */
422 matty 25 static void
423     licence_process_issue(STREAM s)
424 matty 10 {
425     RC4_KEY crypt_key;
426     uint32 length;
427     uint16 check;
428    
429 matty 24 in_uint8s(s, 2); /* 3d 45 - unknown */
430 matty 10 in_uint16_le(s, length);
431     if (!s_check_rem(s, length))
432     return;
433    
434     RC4_set_key(&crypt_key, 16, licence_key);
435     RC4(&crypt_key, length, s->p, s->p);
436    
437     in_uint16(s, check);
438     if (check != 0)
439     return;
440    
441 matty 28 licence_issued = True;
442 astrand 64 save_licence(s->p, length - 2);
443 matty 10 }
444    
445     /* Process a licence packet */
446 matty 25 void
447     licence_process(STREAM s)
448 matty 10 {
449     uint16 tag;
450    
451     in_uint16_le(s, tag);
452 matty 24 in_uint8s(s, 2); /* length */
453 matty 10
454     switch (tag)
455     {
456     case LICENCE_TAG_DEMAND:
457     licence_process_demand(s);
458     break;
459    
460     case LICENCE_TAG_AUTHREQ:
461     licence_process_authreq(s);
462     break;
463    
464     case LICENCE_TAG_ISSUE:
465     licence_process_issue(s);
466     break;
467    
468 matthewc 39 case LICENCE_TAG_REISSUE:
469     break;
470    
471 matty 10 case LICENCE_TAG_RESULT:
472     break;
473    
474     default:
475 matty 30 unimpl("licence tag 0x%x\n", tag);
476 matty 10 }
477     }

  ViewVC Help
Powered by ViewVC 1.1.26