2 * FreeRDP: A Remote Desktop Protocol Implementation
5 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6 * Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
39 #include "autodetect.h"
40 #include "heartbeat.h"
41 #include "multitransport.h"
43 #include "transport.h"
44 #include "connection.h"
45 #include "redirection.h"
46 #include "capabilities.h"
49 #include <freerdp/freerdp.h>
50 #include <freerdp/settings.h>
51 #include <freerdp/log.h>
53 #include <winpr/stream.h>
55 /* Security Header Flags */
56 #define SEC_EXCHANGE_PKT 0x0001
57 #define SEC_TRANSPORT_REQ 0x0002
58 #define SEC_TRANSPORT_RSP 0x0004
59 #define SEC_ENCRYPT 0x0008
60 #define SEC_RESET_SEQNO 0x0010
61 #define SEC_IGNORE_SEQNO 0x0020
62 #define SEC_INFO_PKT 0x0040
63 #define SEC_LICENSE_PKT 0x0080
64 #define SEC_LICENSE_ENCRYPT_CS 0x0200
65 #define SEC_LICENSE_ENCRYPT_SC 0x0200
66 #define SEC_REDIRECTION_PKT 0x0400
67 #define SEC_SECURE_CHECKSUM 0x0800
68 #define SEC_AUTODETECT_REQ 0x1000
69 #define SEC_AUTODETECT_RSP 0x2000
70 #define SEC_HEARTBEAT 0x4000
71 #define SEC_FLAGSHI_VALID 0x8000
73 #define SEC_PKT_CS_MASK (SEC_EXCHANGE_PKT | SEC_INFO_PKT)
74 #define SEC_PKT_SC_MASK (SEC_LICENSE_PKT | SEC_REDIRECTION_PKT)
75 #define SEC_PKT_MASK (SEC_PKT_CS_MASK | SEC_PKT_SC_MASK)
77 #define RDP_SECURITY_HEADER_LENGTH 4
78 #define RDP_SHARE_CONTROL_HEADER_LENGTH 6
79 #define RDP_SHARE_DATA_HEADER_LENGTH 12
80 #define RDP_PACKET_HEADER_MAX_LENGTH (TPDU_DATA_LENGTH + MCS_SEND_DATA_HEADER_MAX_LENGTH)
82 #define PDU_TYPE_DEMAND_ACTIVE 0x1
83 #define PDU_TYPE_CONFIRM_ACTIVE 0x3
84 #define PDU_TYPE_DEACTIVATE_ALL 0x6
85 #define PDU_TYPE_DATA 0x7
86 #define PDU_TYPE_SERVER_REDIRECTION 0xA
88 #define PDU_TYPE_FLOW_TEST 0x41
89 #define PDU_TYPE_FLOW_RESPONSE 0x42
90 #define PDU_TYPE_FLOW_STOP 0x43
92 #define FINALIZE_SC_SYNCHRONIZE_PDU 0x01
93 #define FINALIZE_SC_CONTROL_COOPERATE_PDU 0x02
94 #define FINALIZE_SC_CONTROL_GRANTED_PDU 0x04
95 #define FINALIZE_SC_FONT_MAP_PDU 0x08
96 #define FINALIZE_SC_COMPLETE 0x0F
99 #define DATA_PDU_TYPE_UPDATE 0x02
100 #define DATA_PDU_TYPE_CONTROL 0x14
101 #define DATA_PDU_TYPE_POINTER 0x1B
102 #define DATA_PDU_TYPE_INPUT 0x1C
103 #define DATA_PDU_TYPE_SYNCHRONIZE 0x1F
104 #define DATA_PDU_TYPE_REFRESH_RECT 0x21
105 #define DATA_PDU_TYPE_PLAY_SOUND 0x22
106 #define DATA_PDU_TYPE_SUPPRESS_OUTPUT 0x23
107 #define DATA_PDU_TYPE_SHUTDOWN_REQUEST 0x24
108 #define DATA_PDU_TYPE_SHUTDOWN_DENIED 0x25
109 #define DATA_PDU_TYPE_SAVE_SESSION_INFO 0x26
110 #define DATA_PDU_TYPE_FONT_LIST 0x27
111 #define DATA_PDU_TYPE_FONT_MAP 0x28
112 #define DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS 0x29
113 #define DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST 0x2B
114 #define DATA_PDU_TYPE_BITMAP_CACHE_ERROR 0x2C
115 #define DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS 0x2D
116 #define DATA_PDU_TYPE_OFFSCREEN_CACHE_ERROR 0x2E
117 #define DATA_PDU_TYPE_SET_ERROR_INFO 0x2F
118 #define DATA_PDU_TYPE_DRAW_NINEGRID_ERROR 0x30
119 #define DATA_PDU_TYPE_DRAW_GDIPLUS_ERROR 0x31
120 #define DATA_PDU_TYPE_ARC_STATUS 0x32
121 #define DATA_PDU_TYPE_STATUS_INFO 0x36
122 #define DATA_PDU_TYPE_MONITOR_LAYOUT 0x37
123 #define DATA_PDU_TYPE_FRAME_ACKNOWLEDGE 0x38
125 /* Stream Identifiers */
126 #define STREAM_UNDEFINED 0x00
127 #define STREAM_LOW 0x01
128 #define STREAM_MED 0x02
129 #define STREAM_HI 0x04
142 rdpFastPath* fastpath;
144 rdpRedirection* redirection;
145 rdpSettings* settings;
146 rdpTransport* transport;
147 rdpAutoDetect* autodetect;
148 rdpHeartbeat* heartbeat;
149 rdpMultitransport* multitransport;
150 struct crypto_rc4_struct* rc4_decrypt_key;
151 int decrypt_use_count;
152 int decrypt_checksum_use_count;
153 struct crypto_rc4_struct* rc4_encrypt_key;
154 int encrypt_use_count;
155 int encrypt_checksum_use_count;
156 struct crypto_des3_struct* fips_encrypt;
157 struct crypto_des3_struct* fips_decrypt;
158 struct crypto_hmac_struct* fips_hmac;
161 BOOL do_crypt_license;
162 BOOL do_secure_checksum;
164 BYTE decrypt_key[16];
165 BYTE encrypt_key[16];
166 BYTE decrypt_update_key[16];
167 BYTE encrypt_update_key[16];
169 BYTE fips_sign_key[20];
170 BYTE fips_encrypt_key[24];
171 BYTE fips_decrypt_key[24];
173 UINT32 finalize_sc_pdus;
175 BOOL deactivation_reactivation;
176 BOOL AwaitCapabilities;
177 rdpSettings* settingsCopy;
180 BOOL rdp_read_security_header(wStream* s, UINT16* flags);
181 void rdp_write_security_header(wStream* s, UINT16 flags);
183 BOOL rdp_read_share_control_header(wStream* s, UINT16* length, UINT16* type, UINT16* channel_id);
184 void rdp_write_share_control_header(wStream* s, UINT16 length, UINT16 type, UINT16 channel_id);
186 BOOL rdp_read_share_data_header(wStream* s, UINT16* length, BYTE* type, UINT32* share_id,
187 BYTE *compressed_type, UINT16 *compressed_len);
189 void rdp_write_share_data_header(wStream* s, UINT16 length, BYTE type, UINT32 share_id);
191 int rdp_init_stream(rdpRdp* rdp, wStream* s);
192 wStream* rdp_send_stream_init(rdpRdp* rdp);
194 BOOL rdp_read_header(rdpRdp* rdp, wStream* s, UINT16* length, UINT16* channel_id);
195 void rdp_write_header(rdpRdp* rdp, wStream* s, UINT16 length, UINT16 channel_id);
197 int rdp_init_stream_pdu(rdpRdp* rdp, wStream* s);
198 BOOL rdp_send_pdu(rdpRdp* rdp, wStream* s, UINT16 type, UINT16 channel_id);
200 wStream* rdp_data_pdu_init(rdpRdp* rdp);
201 int rdp_init_stream_data_pdu(rdpRdp* rdp, wStream* s);
202 BOOL rdp_send_data_pdu(rdpRdp* rdp, wStream* s, BYTE type, UINT16 channel_id);
203 int rdp_recv_data_pdu(rdpRdp* rdp, wStream* s);
205 BOOL rdp_send(rdpRdp* rdp, wStream* s, UINT16 channelId);
207 int rdp_send_channel_data(rdpRdp* rdp, UINT16 channelId, BYTE* data, int size);
209 wStream* rdp_message_channel_pdu_init(rdpRdp* rdp);
210 BOOL rdp_send_message_channel_pdu(rdpRdp* rdp, wStream* s, UINT16 sec_flags);
211 int rdp_recv_message_channel_pdu(rdpRdp* rdp, wStream* s, UINT16 securityFlags);
213 int rdp_recv_out_of_sequence_pdu(rdpRdp* rdp, wStream* s);
215 void rdp_read_flow_control_pdu(wStream* s, UINT16* type);
217 BOOL rdp_write_monitor_layout_pdu(wStream* s, UINT32 monitorCount, const rdpMonitor* monitorDefArray);
219 int rdp_recv_callback(rdpTransport* transport, wStream* s, void* extra);
221 int rdp_check_fds(rdpRdp* rdp);
223 rdpRdp* rdp_new(rdpContext* context);
224 void rdp_reset(rdpRdp* rdp);
225 void rdp_free(rdpRdp* rdp);
227 #define RDP_TAG FREERDP_TAG("core.rdp")
228 #ifdef WITH_DEBUG_RDP
229 #define DEBUG_RDP(fmt, ...) WLog_DBG(RDP_TAG, fmt, ## __VA_ARGS__)
231 #define DEBUG_RDP(fmt, ...) do { } while (0)
234 BOOL rdp_decrypt(rdpRdp* rdp, wStream* s, int length, UINT16 securityFlags);
236 BOOL rdp_set_error_info(rdpRdp* rdp, UINT32 errorInfo);
237 BOOL rdp_send_error_info(rdpRdp* rdp);