/[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 25 - (hide annotations)
Sat Jan 6 03:47:04 2001 UTC (23 years, 4 months ago) by matty
File MIME type: text/plain
File size: 6463 byte(s)
Changed indentation style (-psl).

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

  ViewVC Help
Powered by ViewVC 1.1.26