Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

licence.cpp

Go to the documentation of this file.
00001 /*
00002    rdesktop: A Remote Desktop Protocol client.
00003    RDP licensing negotiation
00004    Copyright (C) Matthew Chapman 1999-2002
00005    
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015    
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019 */
00020 
00021 #include "rdesktop.h"
00022 
00023 #ifdef WITH_OPENSSL
00024 #include <openssl/rc4.h>
00025 #else
00026 #include "crypto/rc4.h"
00027 #endif
00028 
00029 extern char username[16];
00030 extern char hostname[16];
00031 
00032 static uint8 licence_key[16];
00033 static uint8 licence_sign_key[16];
00034 
00035 BOOL licence_issued = False;
00036 
00037 /* Generate a session key and RC4 keys, given client and server randoms */
00038 static void
00039 licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)
00040 {
00041         uint8 session_key[48];
00042         uint8 temp_hash[48];
00043 
00044         /* Generate session key - two rounds of sec_hash_48 */
00045         sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65);
00046         sec_hash_48(session_key, temp_hash, server_key, client_key, 65);
00047 
00048         /* Store first 16 bytes of session key, for generating signatures */
00049         memcpy(licence_sign_key, session_key, 16);
00050 
00051         /* Generate RC4 key */
00052         sec_hash_16(licence_key, &session_key[16], client_key, server_key);
00053 }
00054 
00055 static void
00056 licence_generate_hwid(uint8 * hwid)
00057 {
00058         buf_out_uint32(hwid, 2);
00059         strncpy((char *) (hwid + 4), hostname, LICENCE_HWID_SIZE - 4);
00060 }
00061 
00062 /* Present an existing licence to the server */
00063 static void
00064 licence_present(uint8 * client_random, uint8 * rsa_data,
00065                 uint8 * licence_data, int licence_size, uint8 * hwid, uint8 * signature)
00066 {
00067         uint32 sec_flags = SEC_LICENCE_NEG;
00068         uint16 length =
00069                 16 + SEC_RANDOM_SIZE + SEC_MODULUS_SIZE + SEC_PADDING_SIZE +
00070                 licence_size + LICENCE_HWID_SIZE + LICENCE_SIGNATURE_SIZE;
00071         STREAM s;
00072 
00073         s = sec_init(sec_flags, length + 4);
00074 
00075         out_uint16_le(s, LICENCE_TAG_PRESENT);
00076         out_uint16_le(s, length);
00077 
00078         out_uint32_le(s, 1);
00079         out_uint16(s, 0);
00080         out_uint16_le(s, 0x0201);
00081 
00082         out_uint8p(s, client_random, SEC_RANDOM_SIZE);
00083         out_uint16(s, 0);
00084         out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
00085         out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
00086         out_uint8s(s, SEC_PADDING_SIZE);
00087 
00088         out_uint16_le(s, 1);
00089         out_uint16_le(s, licence_size);
00090         out_uint8p(s, licence_data, licence_size);
00091 
00092         out_uint16_le(s, 1);
00093         out_uint16_le(s, LICENCE_HWID_SIZE);
00094         out_uint8p(s, hwid, LICENCE_HWID_SIZE);
00095 
00096         out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
00097 
00098         s_mark_end(s);
00099         sec_send(s, sec_flags);
00100 }
00101 
00102 /* Send a licence request packet */
00103 static void
00104 licence_send_request(uint8 * client_random, uint8 * rsa_data, char *user, char *host)
00105 {
00106         uint32 sec_flags = SEC_LICENCE_NEG;
00107         uint16 userlen = strlen(user) + 1;
00108         uint16 hostlen = strlen(host) + 1;
00109         uint16 length = 128 + userlen + hostlen;
00110         STREAM s;
00111 
00112         s = sec_init(sec_flags, length + 2);
00113 
00114         out_uint16_le(s, LICENCE_TAG_REQUEST);
00115         out_uint16_le(s, length);
00116 
00117         out_uint32_le(s, 1);
00118         out_uint16(s, 0);
00119         out_uint16_le(s, 0xff01);
00120 
00121         out_uint8p(s, client_random, SEC_RANDOM_SIZE);
00122         out_uint16(s, 0);
00123         out_uint16_le(s, (SEC_MODULUS_SIZE + SEC_PADDING_SIZE));
00124         out_uint8p(s, rsa_data, SEC_MODULUS_SIZE);
00125         out_uint8s(s, SEC_PADDING_SIZE);
00126 
00127         out_uint16(s, LICENCE_TAG_USER);
00128         out_uint16(s, userlen);
00129         out_uint8p(s, user, userlen);
00130 
00131         out_uint16(s, LICENCE_TAG_HOST);
00132         out_uint16(s, hostlen);
00133         out_uint8p(s, host, hostlen);
00134 
00135         s_mark_end(s);
00136         sec_send(s, sec_flags);
00137 }
00138 
00139 /* Process a licence demand packet */
00140 static void
00141 licence_process_demand(STREAM s)
00142 {
00143         uint8 null_data[SEC_MODULUS_SIZE];
00144         uint8 *server_random;
00145         uint8 signature[LICENCE_SIGNATURE_SIZE];
00146         uint8 hwid[LICENCE_HWID_SIZE];
00147         uint8 *licence_data;
00148         int licence_size;
00149         RC4_KEY crypt_key;
00150 
00151         /* Retrieve the server random from the incoming packet */
00152         in_uint8p(s, server_random, SEC_RANDOM_SIZE);
00153 
00154         /* We currently use null client keys. This is a bit naughty but, hey,
00155            the security of licence negotiation isn't exactly paramount. */
00156         memset(null_data, 0, sizeof(null_data));
00157         licence_generate_keys(null_data, server_random, null_data);
00158 
00159         licence_size = load_licence(&licence_data);
00160         if (licence_size != -1)
00161         {
00162                 /* Generate a signature for the HWID buffer */
00163                 licence_generate_hwid(hwid);
00164                 sec_sign(signature, 16, licence_sign_key, 16, hwid, sizeof(hwid));
00165 
00166                 /* Now encrypt the HWID */
00167                 RC4_set_key(&crypt_key, 16, licence_key);
00168                 RC4(&crypt_key, sizeof(hwid), hwid, hwid);
00169 
00170                 licence_present(null_data, null_data, licence_data, licence_size, hwid, signature);
00171                 xfree(licence_data);
00172                 return;
00173         }
00174 
00175         licence_send_request(null_data, null_data, username, hostname);
00176 }
00177 
00178 /* Send an authentication response packet */
00179 static void
00180 licence_send_authresp(uint8 * token, uint8 * crypt_hwid, uint8 * signature)
00181 {
00182         uint32 sec_flags = SEC_LICENCE_NEG;
00183         uint16 length = 58;
00184         STREAM s;
00185 
00186         s = sec_init(sec_flags, length + 2);
00187 
00188         out_uint16_le(s, LICENCE_TAG_AUTHRESP);
00189         out_uint16_le(s, length);
00190 
00191         out_uint16_le(s, 1);
00192         out_uint16_le(s, LICENCE_TOKEN_SIZE);
00193         out_uint8p(s, token, LICENCE_TOKEN_SIZE);
00194 
00195         out_uint16_le(s, 1);
00196         out_uint16_le(s, LICENCE_HWID_SIZE);
00197         out_uint8p(s, crypt_hwid, LICENCE_HWID_SIZE);
00198 
00199         out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
00200 
00201         s_mark_end(s);
00202         sec_send(s, sec_flags);
00203 }
00204 
00205 /* Parse an authentication request packet */
00206 static BOOL
00207 licence_parse_authreq(STREAM s, uint8 ** token, uint8 ** signature)
00208 {
00209         uint16 tokenlen;
00210 
00211         in_uint8s(s, 6);        /* unknown: f8 3d 15 00 04 f6 */
00212 
00213         in_uint16_le(s, tokenlen);
00214         if (tokenlen != LICENCE_TOKEN_SIZE)
00215         {
00216                 error("token len %d\n", tokenlen);
00217                 return False;
00218         }
00219 
00220         in_uint8p(s, *token, tokenlen);
00221         in_uint8p(s, *signature, LICENCE_SIGNATURE_SIZE);
00222 
00223         return s_check_end(s);
00224 }
00225 
00226 /* Process an authentication request packet */
00227 static void
00228 licence_process_authreq(STREAM s)
00229 {
00230         uint8 *in_token, *in_sig;
00231         uint8 out_token[LICENCE_TOKEN_SIZE], decrypt_token[LICENCE_TOKEN_SIZE];
00232         uint8 hwid[LICENCE_HWID_SIZE], crypt_hwid[LICENCE_HWID_SIZE];
00233         uint8 sealed_buffer[LICENCE_TOKEN_SIZE + LICENCE_HWID_SIZE];
00234         uint8 out_sig[LICENCE_SIGNATURE_SIZE];
00235         RC4_KEY crypt_key;
00236 
00237         /* Parse incoming packet and save the encrypted token */
00238         licence_parse_authreq(s, &in_token, &in_sig);
00239         memcpy(out_token, in_token, LICENCE_TOKEN_SIZE);
00240 
00241         /* Decrypt the token. It should read TEST in Unicode. */
00242         RC4_set_key(&crypt_key, 16, licence_key);
00243         RC4(&crypt_key, LICENCE_TOKEN_SIZE, in_token, decrypt_token);
00244 
00245         /* Generate a signature for a buffer of token and HWID */
00246         licence_generate_hwid(hwid);
00247         memcpy(sealed_buffer, decrypt_token, LICENCE_TOKEN_SIZE);
00248         memcpy(sealed_buffer + LICENCE_TOKEN_SIZE, hwid, LICENCE_HWID_SIZE);
00249         sec_sign(out_sig, 16, licence_sign_key, 16, sealed_buffer, sizeof(sealed_buffer));
00250 
00251         /* Now encrypt the HWID */
00252         RC4_set_key(&crypt_key, 16, licence_key);
00253         RC4(&crypt_key, LICENCE_HWID_SIZE, hwid, crypt_hwid);
00254 
00255         licence_send_authresp(out_token, crypt_hwid, out_sig);
00256 }
00257 
00258 /* Process an licence issue packet */
00259 static void
00260 licence_process_issue(STREAM s)
00261 {
00262         RC4_KEY crypt_key;
00263         uint32 length;
00264         uint16 check;
00265 
00266         in_uint8s(s, 2);        /* 3d 45 - unknown */
00267         in_uint16_le(s, length);
00268         if (!s_check_rem(s, length))
00269                 return;
00270 
00271         RC4_set_key(&crypt_key, 16, licence_key);
00272         RC4(&crypt_key, length, s->p, s->p);
00273 
00274         in_uint16(s, check);
00275         if (check != 0)
00276                 return;
00277 
00278         licence_issued = True;
00279         save_licence(s->p, length - 2);
00280 }
00281 
00282 /* Process a licence packet */
00283 void
00284 licence_process(STREAM s)
00285 {
00286         uint16 tag;
00287 
00288         in_uint16_le(s, tag);
00289         in_uint8s(s, 2);        /* length */
00290 
00291         switch (tag)
00292         {
00293                 case LICENCE_TAG_DEMAND:
00294                         licence_process_demand(s);
00295                         break;
00296 
00297                 case LICENCE_TAG_AUTHREQ:
00298                         licence_process_authreq(s);
00299                         break;
00300 
00301                 case LICENCE_TAG_ISSUE:
00302                         licence_process_issue(s);
00303                         break;
00304 
00305                 case LICENCE_TAG_REISSUE:
00306                         break;
00307 
00308                 case LICENCE_TAG_RESULT:
00309                         break;
00310 
00311                 default:
00312                         unimpl("licence tag 0x%x\n", tag);
00313         }
00314 }

Generated on Sat Nov 5 16:17:41 2005 for OPIE by  doxygen 1.4.2