Fixed (?) and enabled RDP5 encryption.
[rdpsrv] / rdp5.c
1 /* -*- c-basic-offset: 8 -*-
2    rdesktop: A Remote Desktop Protocol client.
3    Protocol services - Multipoint Communications Service
4    Copyright (C) Matthew Chapman 1999-2002
5    Copyright (C) Erik Forsberg 2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "rdesktop.h"
23
24 extern uint8 *g_next_packet;
25 extern int listen_on_vnc;
26
27 extern uint8 sec_sign_key[16];
28 extern int rc4_key_len;
29
30 /* Initialise secure transport packet */
31 STREAM
32 rdp5_init(int maxlen, BOOL encryption)
33 {
34         int hdrlen;
35         STREAM s;
36
37         hdrlen = encryption ? 11 : 3;
38         s = tcp_init(maxlen + hdrlen);
39         s_push_layer(s, sec_hdr, hdrlen);
40
41         return s;
42 }
43
44 void
45 rdp5_send(STREAM s, BOOL encryption)
46 {
47         int datalen;
48
49         s_pop_layer(s, sec_hdr);
50
51         datalen = s->end - s->p;
52
53         out_uint8(s, encryption ? 0x80 : 0);    // protocol
54         out_uint8(s, 0x80 | (datalen >> 8));
55         out_uint8(s, datalen & 0xff);
56
57         if (encryption) {
58                 datalen -= 11;
59
60                 sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen);
61                 sec_encrypt(s->p + 8, datalen);
62         }
63
64         tcp_send(s);
65 }
66
67 void
68 rdp5_process(STREAM s, BOOL encryption)
69 {
70         uint16 length, count, x, y;
71         uint8 type;
72         uint8 *next;
73
74         if (encryption)
75         {
76                 in_uint8s(s, 8);        /* signature */
77                 sec_decrypt(s->p, s->end - s->p);
78         }
79
80 #if 1
81         printf("RDP5 data:\n");
82         hexdump(s->p, s->end - s->p);
83 #endif
84
85         while (s->p < s->end)
86         {
87                 in_uint8(s, type);
88
89                 switch (type)
90                 {
91                         case 32: { // mouse
92                                 listen_on_vnc = 1;
93                                 uint16 device_flags, x, y;
94                                 static int mouse1_down = 0, mouse2_down = 0;
95
96                                 in_uint16_le(s, device_flags);
97                                 in_uint16_le(s, x);
98                                 in_uint16_le(s, y);
99                                 
100                                 printf("- Type: Mouse\n");
101                                 printf("- Device flags: %x\n", device_flags);
102                                 printf("- Position: (%u,%u)\n", x, y);
103
104                                 if (device_flags & MOUSE_FLAG_BUTTON1)
105                                         mouse1_down = (device_flags & MOUSE_FLAG_DOWN) ? 0x01 : 0;
106                                 if (device_flags & MOUSE_FLAG_BUTTON2)
107                                         mouse2_down = (device_flags & MOUSE_FLAG_DOWN) ? 0x02 : 0;
108
109                                 printf("button mask = %x\n", mouse1_down | mouse2_down);
110
111                         /*      buf[0] = 5; // message type
112                                 buf[1] = mouse1_down | mouse2_down; // button mask
113                                 buf[2] = param1 >> 8;
114                                 buf[3] = param1 & 0xff;
115                                 buf[4] = param2 >> 8;
116                                 buf[5] = param2 & 0xff;
117                                 write(vnc_sock, buf, 6); */
118
119                                 break;
120                         }
121                         default:
122                                 printf("Unimplemented RDP5 opcode %d (len=%u)\n", type, length);
123                                 return;
124                 }
125         }
126 }