]> git.sesse.net Git - rdpsrv/blob - licence.c
Fix encryption, client connects!!
[rdpsrv] / licence.c
1 /*
2    rdesktop: A Remote Desktop Protocol client.
3    RDP licensing negotiation
4    Copyright (C) Matthew Chapman 1999-2002
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
23 #include <openssl/rc4.h>
24
25 extern char g_username[16];
26 extern char hostname[16];
27
28 static uint8 g_licence_key[16];
29 static uint8 g_licence_sign_key[16];
30
31 BOOL g_licence_issued = False;
32
33 /* Generate a session key and RC4 keys, given client and server randoms */
34 void
35 licence_generate_keys(uint8 * client_key, uint8 * server_key, uint8 * client_rsa)
36 {
37         uint8 session_key[48];
38         uint8 temp_hash[48];
39
40         /* Generate session key - two rounds of sec_hash_48 */
41         sec_hash_48(temp_hash, client_rsa, client_key, server_key, 65);
42         sec_hash_48(session_key, temp_hash, server_key, client_key, 65);
43
44         /* Store first 16 bytes of session key, for generating signatures */
45         memcpy(g_licence_sign_key, session_key, 16);
46
47         /* Generate RC4 key */
48         sec_hash_16(g_licence_key, &session_key[16], client_key, server_key);
49
50         {
51                 int i;
52
53                 printf("g_license_key:\n");
54                 for (i = 0; i < 16; ++i)
55                         printf(" 0x%02x", g_licence_key[i]);
56                 printf("\n");
57
58                 printf("g_license_sign_key:\n");
59                 for (i = 0; i < 16; ++i)
60                         printf(" 0x%02x", g_licence_sign_key[i]);
61                 printf("\n");
62         }
63 }
64
65 // "TEST" in UTF-16
66 unsigned char license_token[] = {
67         0x54, 0x00, 0x45, 0x00, 0x53, 0x00, 0x54, 0x00, 0x00, 0x00
68 };
69
70 void
71 send_authreq()
72 {
73         RC4_KEY crypt_key;
74         uint8 in_token[LICENCE_TOKEN_SIZE], encrypt_token[LICENCE_TOKEN_SIZE];
75         uint8 signature[LICENCE_SIGNATURE_SIZE];
76
77         STREAM s = sec_init(38, SEC_LICENCE_NEG);
78         
79         out_uint8(s, LICENCE_TAG_AUTHREQ);
80         out_uint8(s, 3);  // version
81         out_uint16_le(s, 38); // length
82
83         // unknown
84         out_uint16_le(s, 0xffff);
85         out_uint16_le(s, 0xffff);
86         out_uint16_le(s, 0x0000);
87
88         out_uint16_le(s, LICENCE_TOKEN_SIZE);
89         memcpy(in_token, license_token, LICENCE_TOKEN_SIZE);
90
91         // encrypt the token
92         RC4_set_key(&crypt_key, 16, g_licence_key);
93         RC4(&crypt_key, LICENCE_TOKEN_SIZE, in_token, encrypt_token);
94
95         out_uint8p(s, encrypt_token, LICENCE_TOKEN_SIZE);
96
97         // what are we supposed to sign here, really? guess at the
98         // token, nothing else has been sent...
99         sec_sign(signature, 16, g_licence_sign_key, 16, encrypt_token, LICENCE_TOKEN_SIZE);
100         out_uint8p(s, signature, LICENCE_SIGNATURE_SIZE);
101
102         s_mark_end(s);
103         sec_send(s, SEC_LICENCE_NEG);
104 }
105