2011-07-10 23:34:43 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-10 23:34:43 +04:00
|
|
|
* RDP Core
|
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
#include "rdp.h"
|
|
|
|
|
2011-07-27 03:14:11 +04:00
|
|
|
#include "info.h"
|
|
|
|
#include "redirection.h"
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <freerdp/crypto/per.h>
|
|
|
|
|
2012-02-29 00:31:08 +04:00
|
|
|
#ifdef WITH_DEBUG_RDP
|
2013-09-06 02:53:55 +04:00
|
|
|
const char* DATA_PDU_TYPE_STRINGS[80] =
|
2011-07-22 19:02:31 +04:00
|
|
|
{
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", /* 0x00 - 0x01 */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Update", /* 0x02 */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", "?", "?", "?", "?", "?", "?", /* 0x03 - 0x0A */
|
|
|
|
"?", "?", "?", "?", "?", "?", "?", "?", "?", /* 0x0B - 0x13 */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Control", /* 0x14 */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", "?", "?", "?", "?", /* 0x15 - 0x1A */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Pointer", /* 0x1B */
|
|
|
|
"Input", /* 0x1C */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", /* 0x1D - 0x1E */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Synchronize", /* 0x1F */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", /* 0x20 */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Refresh Rect", /* 0x21 */
|
|
|
|
"Play Sound", /* 0x22 */
|
|
|
|
"Suppress Output", /* 0x23 */
|
|
|
|
"Shutdown Request", /* 0x24 */
|
|
|
|
"Shutdown Denied", /* 0x25 */
|
|
|
|
"Save Session Info", /* 0x26 */
|
|
|
|
"Font List", /* 0x27 */
|
|
|
|
"Font Map", /* 0x28 */
|
|
|
|
"Set Keyboard Indicators", /* 0x29 */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", /* 0x2A */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Bitmap Cache Persistent List", /* 0x2B */
|
|
|
|
"Bitmap Cache Error", /* 0x2C */
|
|
|
|
"Set Keyboard IME Status", /* 0x2D */
|
|
|
|
"Offscreen Cache Error", /* 0x2E */
|
|
|
|
"Set Error Info", /* 0x2F */
|
|
|
|
"Draw Nine Grid Error", /* 0x30 */
|
|
|
|
"Draw GDI+ Error", /* 0x31 */
|
|
|
|
"ARC Status", /* 0x32 */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", "?", /* 0x33 - 0x35 */
|
2011-07-22 19:02:31 +04:00
|
|
|
"Status Info", /* 0x36 */
|
|
|
|
"Monitor Layout" /* 0x37 */
|
2013-09-06 02:53:55 +04:00
|
|
|
"FrameAcknowledge", "?", "?", /* 0x38 - 0x40 */
|
2012-02-29 00:31:08 +04:00
|
|
|
"?", "?", "?", "?", "?", "?" /* 0x41 - 0x46 */
|
2011-07-22 19:02:31 +04:00
|
|
|
};
|
2012-02-29 00:31:08 +04:00
|
|
|
#endif
|
2011-07-22 19:02:31 +04:00
|
|
|
|
2011-07-11 23:58:39 +04:00
|
|
|
/**
|
|
|
|
* Read RDP Security Header.\n
|
|
|
|
* @msdn{cc240579}
|
|
|
|
* @param s stream
|
|
|
|
* @param flags security flags
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_read_security_header(wStream* s, UINT16* flags)
|
2011-07-11 23:58:39 +04:00
|
|
|
{
|
|
|
|
/* Basic Security Header */
|
2013-04-30 19:26:33 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, *flags); /* flags */
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 2); /* flagsHi (unused) */
|
2013-01-12 03:43:16 +04:00
|
|
|
return TRUE;
|
2011-07-11 23:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write RDP Security Header.\n
|
|
|
|
* @msdn{cc240579}
|
|
|
|
* @param s stream
|
|
|
|
* @param flags security flags
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void rdp_write_security_header(wStream* s, UINT16 flags)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
|
|
|
/* Basic Security Header */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, flags); /* flags */
|
|
|
|
Stream_Write_UINT16(s, 0); /* flagsHi (unused) */
|
2011-07-10 23:34:43 +04:00
|
|
|
}
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_read_share_control_header(wStream* s, UINT16* length, UINT16* type, UINT16* channel_id)
|
2011-07-20 00:30:05 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 2)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
/* Share Control Header */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, *length); /* totalLength */
|
2012-02-07 01:31:41 +04:00
|
|
|
|
2014-02-11 07:12:13 +04:00
|
|
|
if (((size_t) *length - 2) > Stream_GetRemainingLength(s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-07 01:31:41 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, *type); /* pduType */
|
2011-07-20 00:30:05 +04:00
|
|
|
*type &= 0x0F; /* type is in the 4 least significant bits */
|
2011-08-21 11:52:44 +04:00
|
|
|
|
2012-02-07 01:34:16 +04:00
|
|
|
if (*length > 4)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, *channel_id); /* pduSource */
|
2013-02-05 01:39:05 +04:00
|
|
|
else
|
|
|
|
*channel_id = 0; /* Windows XP can send such short DEACTIVATE_ALL PDUs. */
|
2012-02-07 01:34:16 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-20 00:30:05 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void rdp_write_share_control_header(wStream* s, UINT16 length, UINT16 type, UINT16 channel_id)
|
2011-07-21 21:57:57 +04:00
|
|
|
{
|
2012-01-25 14:12:37 +04:00
|
|
|
length -= RDP_PACKET_HEADER_MAX_LENGTH;
|
2011-07-24 02:05:34 +04:00
|
|
|
|
2011-07-21 21:57:57 +04:00
|
|
|
/* Share Control Header */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, length); /* totalLength */
|
|
|
|
Stream_Write_UINT16(s, type | 0x10); /* pduType */
|
|
|
|
Stream_Write_UINT16(s, channel_id); /* pduSource */
|
2011-07-21 21:57:57 +04:00
|
|
|
}
|
|
|
|
|
2013-09-06 02:53:55 +04:00
|
|
|
BOOL rdp_read_share_data_header(wStream* s, UINT16* length, BYTE* type, UINT32* shareId,
|
2014-03-10 21:15:10 +04:00
|
|
|
BYTE* compressedType, UINT16* compressedLength)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 12)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2011-07-21 23:10:53 +04:00
|
|
|
/* Share Data Header */
|
2013-09-06 02:53:55 +04:00
|
|
|
Stream_Read_UINT32(s, *shareId); /* shareId (4 bytes) */
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Seek_UINT8(s); /* pad1 (1 byte) */
|
|
|
|
Stream_Seek_UINT8(s); /* streamId (1 byte) */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, *length); /* uncompressedLength (2 bytes) */
|
|
|
|
Stream_Read_UINT8(s, *type); /* pduType2, Data PDU Type (1 byte) */
|
2013-09-06 02:53:55 +04:00
|
|
|
Stream_Read_UINT8(s, *compressedType); /* compressedType (1 byte) */
|
2014-03-10 21:15:10 +04:00
|
|
|
Stream_Read_UINT16(s, *compressedLength); /* compressedLength (2 bytes) */
|
2013-09-06 02:53:55 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-21 23:10:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void rdp_write_share_data_header(wStream* s, UINT16 length, BYTE type, UINT32 share_id)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
2012-01-25 14:12:37 +04:00
|
|
|
length -= RDP_PACKET_HEADER_MAX_LENGTH;
|
2011-07-24 02:05:34 +04:00
|
|
|
length -= RDP_SHARE_CONTROL_HEADER_LENGTH;
|
|
|
|
length -= RDP_SHARE_DATA_HEADER_LENGTH;
|
|
|
|
|
2011-07-21 23:10:53 +04:00
|
|
|
/* Share Data Header */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32(s, share_id); /* shareId (4 bytes) */
|
|
|
|
Stream_Write_UINT8(s, 0); /* pad1 (1 byte) */
|
|
|
|
Stream_Write_UINT8(s, STREAM_LOW); /* streamId (1 byte) */
|
|
|
|
Stream_Write_UINT16(s, length); /* uncompressedLength (2 bytes) */
|
|
|
|
Stream_Write_UINT8(s, type); /* pduType2, Data PDU Type (1 byte) */
|
|
|
|
Stream_Write_UINT8(s, 0); /* compressedType (1 byte) */
|
|
|
|
Stream_Write_UINT16(s, 0); /* compressedLength (2 bytes) */
|
2011-07-21 23:10:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int rdp_security_stream_init(rdpRdp* rdp, wStream* s)
|
2011-09-13 10:40:27 +04:00
|
|
|
{
|
|
|
|
if (rdp->do_crypt)
|
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 12);
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-11-08 08:29:24 +04:00
|
|
|
if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 4);
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
rdp->sec_flags |= SEC_ENCRYPT;
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2012-01-25 19:06:31 +04:00
|
|
|
if (rdp->do_secure_checksum)
|
|
|
|
rdp->sec_flags |= SEC_SECURE_CHECKSUM;
|
2011-09-13 10:40:27 +04:00
|
|
|
}
|
|
|
|
else if (rdp->sec_flags != 0)
|
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 4);
|
2011-09-13 10:40:27 +04:00
|
|
|
}
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-05-15 23:54:33 +04:00
|
|
|
int rdp_init_stream(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
Stream_Seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
|
|
|
|
rdp_security_stream_init(rdp, s);
|
|
|
|
return 0;
|
|
|
|
}
|
2011-07-12 04:46:03 +04:00
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* rdp_send_stream_init(rdpRdp* rdp)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2013-03-21 23:19:33 +04:00
|
|
|
wStream* s;
|
2011-07-21 21:57:57 +04:00
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
2013-05-15 23:54:33 +04:00
|
|
|
rdp_init_stream(rdp, s);
|
2011-07-12 02:46:36 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-05-15 23:54:33 +04:00
|
|
|
int rdp_init_stream_pdu(rdpRdp* rdp, wStream* s)
|
2011-07-22 04:06:28 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
|
2012-12-12 08:34:51 +04:00
|
|
|
rdp_security_stream_init(rdp, s);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, RDP_SHARE_CONTROL_HEADER_LENGTH);
|
2013-05-15 23:54:33 +04:00
|
|
|
return 0;
|
2011-07-22 04:06:28 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 23:54:33 +04:00
|
|
|
int rdp_init_stream_data_pdu(rdpRdp* rdp, wStream* s)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
|
2012-12-12 08:34:51 +04:00
|
|
|
rdp_security_stream_init(rdp, s);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, RDP_SHARE_CONTROL_HEADER_LENGTH);
|
|
|
|
Stream_Seek(s, RDP_SHARE_DATA_HEADER_LENGTH);
|
2013-05-15 23:54:33 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wStream* rdp_data_pdu_init(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
|
|
|
rdp_init_stream_data_pdu(rdp, s);
|
2011-07-21 23:10:53 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2013-11-15 08:05:29 +04:00
|
|
|
BOOL rdp_set_error_info(rdpRdp* rdp, UINT32 errorInfo)
|
|
|
|
{
|
|
|
|
rdp->errorInfo = errorInfo;
|
|
|
|
|
|
|
|
if (rdp->errorInfo != ERRINFO_SUCCESS)
|
|
|
|
{
|
|
|
|
ErrorInfoEventArgs e;
|
|
|
|
rdpContext* context = rdp->instance->context;
|
|
|
|
|
2014-03-21 23:25:00 +04:00
|
|
|
rdp->context->LastError = MAKE_FREERDP_ERROR(ERRINFO, errorInfo);
|
2013-11-15 08:05:29 +04:00
|
|
|
rdp_print_errinfo(rdp->errorInfo);
|
|
|
|
|
|
|
|
EventArgsInit(&e, "freerdp");
|
|
|
|
e.code = rdp->errorInfo;
|
|
|
|
PubSub_OnErrorInfo(context->pubSub, context, &e);
|
|
|
|
}
|
2014-03-21 21:45:43 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rdp->context->LastError = FREERDP_ERROR_SUCCESS;
|
|
|
|
}
|
2013-11-15 08:05:29 +04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
wStream* rdp_message_channel_pdu_init(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
|
|
|
Stream_Seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
|
|
|
|
rdp_security_stream_init(rdp, s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-08-20 12:43:29 +04:00
|
|
|
/**
|
|
|
|
* Read an RDP packet header.\n
|
|
|
|
* @param rdp rdp module
|
|
|
|
* @param s stream
|
|
|
|
* @param length RDP packet length
|
|
|
|
* @param channel_id channel id
|
|
|
|
*/
|
|
|
|
|
2013-07-20 02:24:56 +04:00
|
|
|
BOOL rdp_read_header(rdpRdp* rdp, wStream* s, UINT16* length, UINT16* channelId)
|
2011-08-20 12:43:29 +04:00
|
|
|
{
|
2013-07-20 02:24:56 +04:00
|
|
|
BYTE byte;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 initiator;
|
2011-08-20 12:43:29 +04:00
|
|
|
enum DomainMCSPDU MCSPDU;
|
|
|
|
|
2012-11-08 03:23:25 +04:00
|
|
|
MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataRequest : DomainMCSPDU_SendDataIndication;
|
2012-10-02 18:28:04 +04:00
|
|
|
|
2012-05-16 17:29:35 +04:00
|
|
|
if (!mcs_read_domain_mcspdu_header(s, &MCSPDU, length))
|
2012-10-02 18:28:04 +04:00
|
|
|
{
|
|
|
|
if (MCSPDU != DomainMCSPDU_DisconnectProviderUltimatum)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-10-02 18:28:04 +04:00
|
|
|
}
|
2012-02-07 01:31:41 +04:00
|
|
|
|
2014-02-11 07:12:13 +04:00
|
|
|
if ((size_t) (*length - 8) > Stream_GetRemainingLength(s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-07 01:37:18 +04:00
|
|
|
|
|
|
|
if (MCSPDU == DomainMCSPDU_DisconnectProviderUltimatum)
|
|
|
|
{
|
2013-11-15 08:05:29 +04:00
|
|
|
int reason = 0;
|
2013-06-19 00:55:23 +04:00
|
|
|
TerminateEventArgs e;
|
2014-04-02 16:17:39 +04:00
|
|
|
rdpContext* context;
|
2012-02-07 01:37:18 +04:00
|
|
|
|
2013-11-15 08:05:29 +04:00
|
|
|
if (!mcs_recv_disconnect_provider_ultimatum(rdp->mcs, s, &reason))
|
|
|
|
return FALSE;
|
|
|
|
|
2014-04-02 16:17:39 +04:00
|
|
|
if (rdp->instance == NULL)
|
|
|
|
{
|
|
|
|
rdp->disconnect = TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
context = rdp->instance->context;
|
|
|
|
|
2013-11-15 08:05:29 +04:00
|
|
|
if (rdp->errorInfo == ERRINFO_SUCCESS)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Some servers like Windows Server 2008 R2 do not send the error info pdu
|
|
|
|
* when the user logs off like they should. Map DisconnectProviderUltimatum
|
|
|
|
* to a ERRINFO_LOGOFF_BY_USER when the errinfo code is ERRINFO_SUCCESS.
|
|
|
|
*/
|
|
|
|
|
2013-11-15 08:41:46 +04:00
|
|
|
if (reason == MCS_Reason_provider_initiated)
|
|
|
|
rdp_set_error_info(rdp, ERRINFO_RPC_INITIATED_DISCONNECT);
|
|
|
|
else if (reason == MCS_Reason_user_requested)
|
|
|
|
rdp_set_error_info(rdp, ERRINFO_LOGOFF_BY_USER);
|
|
|
|
else
|
|
|
|
rdp_set_error_info(rdp, ERRINFO_RPC_INITIATED_DISCONNECT);
|
2013-11-15 08:05:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "DisconnectProviderUltimatum: reason: %d\n", reason);
|
2012-02-07 01:37:18 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
rdp->disconnect = TRUE;
|
2012-02-07 01:37:18 +04:00
|
|
|
|
2013-06-19 00:55:23 +04:00
|
|
|
EventArgsInit(&e, "freerdp");
|
|
|
|
e.code = 0;
|
|
|
|
PubSub_OnTerminate(context->pubSub, context, &e);
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-02-07 01:37:18 +04:00
|
|
|
}
|
2011-08-20 12:43:29 +04:00
|
|
|
|
2013-04-30 19:26:33 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 5)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
2013-05-15 23:54:33 +04:00
|
|
|
|
2011-08-20 12:43:29 +04:00
|
|
|
per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID); /* initiator (UserId) */
|
2013-07-20 02:24:56 +04:00
|
|
|
per_read_integer16(s, channelId, 0); /* channelId */
|
|
|
|
Stream_Read_UINT8(s, byte); /* dataPriority + Segmentation (0x70) */
|
2011-08-25 00:54:32 +04:00
|
|
|
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!per_read_length(s, length)) /* userData (OCTET_STRING) */
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
2013-05-15 23:54:33 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (*length > Stream_GetRemainingLength(s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-20 12:43:29 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-20 12:43:29 +04:00
|
|
|
}
|
|
|
|
|
2011-07-12 09:16:59 +04:00
|
|
|
/**
|
|
|
|
* Write an RDP packet header.\n
|
|
|
|
* @param rdp rdp module
|
|
|
|
* @param s stream
|
|
|
|
* @param length RDP packet length
|
2011-08-03 08:34:55 +04:00
|
|
|
* @param channel_id channel id
|
2011-07-12 09:16:59 +04:00
|
|
|
*/
|
|
|
|
|
2013-07-20 02:24:56 +04:00
|
|
|
void rdp_write_header(rdpRdp* rdp, wStream* s, UINT16 length, UINT16 channelId)
|
2011-07-12 09:16:59 +04:00
|
|
|
{
|
2011-09-16 03:54:03 +04:00
|
|
|
int body_length;
|
2011-11-10 00:59:03 +04:00
|
|
|
enum DomainMCSPDU MCSPDU;
|
2011-08-20 12:43:29 +04:00
|
|
|
|
2012-11-08 03:23:25 +04:00
|
|
|
MCSPDU = (rdp->settings->ServerMode) ? DomainMCSPDU_SendDataIndication : DomainMCSPDU_SendDataRequest;
|
2011-08-20 12:43:29 +04:00
|
|
|
|
2012-11-08 08:29:24 +04:00
|
|
|
if ((rdp->sec_flags & SEC_ENCRYPT) && (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS))
|
2011-09-27 09:30:58 +04:00
|
|
|
{
|
2011-09-16 03:54:03 +04:00
|
|
|
int pad;
|
|
|
|
|
2012-01-25 14:12:37 +04:00
|
|
|
body_length = length - RDP_PACKET_HEADER_MAX_LENGTH - 16;
|
2011-09-16 03:54:03 +04:00
|
|
|
pad = 8 - (body_length % 8);
|
2012-12-12 08:34:51 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
if (pad != 8)
|
|
|
|
length += pad;
|
|
|
|
}
|
|
|
|
|
2011-08-20 12:43:29 +04:00
|
|
|
mcs_write_domain_mcspdu_header(s, MCSPDU, length, 0);
|
2014-02-14 02:06:33 +04:00
|
|
|
per_write_integer16(s, rdp->mcs->userId, MCS_BASE_CHANNEL_ID); /* initiator */
|
2013-07-20 02:24:56 +04:00
|
|
|
per_write_integer16(s, channelId, 0); /* channelId */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, 0x70); /* dataPriority + segmentation */
|
2012-01-25 19:25:22 +04:00
|
|
|
/*
|
2013-07-20 02:24:56 +04:00
|
|
|
* We always encode length in two bytes, even though we could use
|
2012-01-25 19:25:22 +04:00
|
|
|
* only one byte if length <= 0x7F. It is just easier that way,
|
|
|
|
* because we can leave room for fixed-length header, store all
|
|
|
|
* the data first and then store the header.
|
|
|
|
*/
|
2012-01-25 14:12:37 +04:00
|
|
|
length = (length - RDP_PACKET_HEADER_MAX_LENGTH) | 0x8000;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16_BE(s, length); /* userData (OCTET_STRING) */
|
2011-07-12 09:16:59 +04:00
|
|
|
}
|
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
static UINT32 rdp_security_stream_out(rdpRdp* rdp, wStream* s, int length, UINT32 sec_flags)
|
2011-09-13 10:40:27 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* data;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 pad = 0;
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
sec_flags |= rdp->sec_flags;
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
if (sec_flags != 0)
|
|
|
|
{
|
|
|
|
rdp_write_security_header(s, sec_flags);
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
if (sec_flags & SEC_ENCRYPT)
|
|
|
|
{
|
2012-11-08 08:29:24 +04:00
|
|
|
if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
|
2011-09-16 03:54:03 +04:00
|
|
|
{
|
2013-05-15 20:14:26 +04:00
|
|
|
data = Stream_Pointer(s) + 12;
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
length = length - (data - Stream_Buffer(s));
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16(s, 0x10); /* length */
|
|
|
|
Stream_Write_UINT8(s, 0x1); /* TSFIPS_VERSION 1*/
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
/* handle padding */
|
|
|
|
pad = 8 - (length % 8);
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
if (pad == 8)
|
|
|
|
pad = 0;
|
|
|
|
if (pad)
|
|
|
|
memset(data+length, 0, pad);
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, pad);
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
security_hmac_signature(data, length, Stream_Pointer(s), rdp);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 8);
|
2011-09-16 03:54:03 +04:00
|
|
|
security_fips_encrypt(data, length + pad, rdp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-15 20:14:26 +04:00
|
|
|
data = Stream_Pointer(s) + 8;
|
|
|
|
length = length - (data - Stream_Buffer(s));
|
|
|
|
|
2012-01-24 18:58:30 +04:00
|
|
|
if (sec_flags & SEC_SECURE_CHECKSUM)
|
2013-05-15 20:14:26 +04:00
|
|
|
security_salted_mac_signature(rdp, data, length, TRUE, Stream_Pointer(s));
|
2012-01-24 18:58:30 +04:00
|
|
|
else
|
2013-05-15 20:14:26 +04:00
|
|
|
security_mac_signature(rdp, data, length, Stream_Pointer(s));
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 8);
|
2013-05-15 20:14:26 +04:00
|
|
|
security_encrypt(Stream_Pointer(s), length, rdp);
|
2011-09-16 03:54:03 +04:00
|
|
|
}
|
2011-09-13 10:40:27 +04:00
|
|
|
}
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
rdp->sec_flags = 0;
|
|
|
|
}
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
return pad;
|
2011-09-13 10:40:27 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static UINT32 rdp_get_sec_bytes(rdpRdp* rdp)
|
2011-09-13 10:40:27 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 sec_bytes;
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2011-09-24 10:09:29 +04:00
|
|
|
if (rdp->sec_flags & SEC_ENCRYPT)
|
|
|
|
{
|
2011-09-13 10:40:27 +04:00
|
|
|
sec_bytes = 12;
|
2012-01-14 01:20:36 +04:00
|
|
|
|
2012-11-08 08:29:24 +04:00
|
|
|
if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
|
2011-09-16 03:54:03 +04:00
|
|
|
sec_bytes += 4;
|
2011-09-24 10:09:29 +04:00
|
|
|
}
|
|
|
|
else if (rdp->sec_flags != 0)
|
2012-01-14 01:20:36 +04:00
|
|
|
{
|
2011-09-13 10:40:27 +04:00
|
|
|
sec_bytes = 4;
|
2012-01-14 01:20:36 +04:00
|
|
|
}
|
2011-09-13 10:40:27 +04:00
|
|
|
else
|
2012-01-14 01:20:36 +04:00
|
|
|
{
|
2011-09-13 10:40:27 +04:00
|
|
|
sec_bytes = 0;
|
2012-01-14 01:20:36 +04:00
|
|
|
}
|
|
|
|
|
2011-09-13 10:40:27 +04:00
|
|
|
return sec_bytes;
|
|
|
|
}
|
|
|
|
|
2011-07-12 04:46:03 +04:00
|
|
|
/**
|
|
|
|
* Send an RDP packet.\n
|
|
|
|
* @param rdp RDP module
|
|
|
|
* @param s stream
|
2011-08-03 08:34:55 +04:00
|
|
|
* @param channel_id channel id
|
2011-07-12 04:46:03 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_send(rdpRdp* rdp, wStream* s, UINT16 channel_id)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
length = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2011-08-03 08:34:55 +04:00
|
|
|
rdp_write_header(rdp, s, length, channel_id);
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
length += rdp_security_stream_out(rdp, s, length, 0);
|
2012-01-25 19:27:00 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, length);
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_SealLength(s);
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
if (transport_write(rdp->transport, s) < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-22 11:03:58 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_send_pdu(rdpRdp* rdp, wStream* s, UINT16 type, UINT16 channel_id)
|
2011-07-22 04:06:28 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 sec_bytes;
|
2013-05-15 23:54:33 +04:00
|
|
|
int sec_hold;
|
2011-07-22 04:06:28 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
length = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2011-07-22 04:06:28 +04:00
|
|
|
|
2011-08-03 08:34:55 +04:00
|
|
|
rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
sec_bytes = rdp_get_sec_bytes(rdp);
|
2013-05-15 23:54:33 +04:00
|
|
|
sec_hold = Stream_GetPosition(s);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, sec_bytes);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2012-01-16 00:16:32 +04:00
|
|
|
rdp_write_share_control_header(s, length - sec_bytes, type, channel_id);
|
2011-07-22 04:06:28 +04:00
|
|
|
|
2013-05-15 23:54:33 +04:00
|
|
|
Stream_SetPosition(s, sec_hold);
|
2014-01-24 03:01:31 +04:00
|
|
|
length += rdp_security_stream_out(rdp, s, length, 0);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, length);
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_SealLength(s);
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
if (transport_write(rdp->transport, s) < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-22 11:03:58 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-22 04:06:28 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_send_data_pdu(rdpRdp* rdp, wStream* s, BYTE type, UINT16 channel_id)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 sec_bytes;
|
2013-05-15 23:54:33 +04:00
|
|
|
int sec_hold;
|
2011-07-21 23:10:53 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
length = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
2011-07-21 23:10:53 +04:00
|
|
|
|
2011-08-03 08:34:55 +04:00
|
|
|
rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2011-09-16 03:54:03 +04:00
|
|
|
sec_bytes = rdp_get_sec_bytes(rdp);
|
2013-05-15 23:54:33 +04:00
|
|
|
sec_hold = Stream_GetPosition(s);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, sec_bytes);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2012-01-16 00:16:32 +04:00
|
|
|
rdp_write_share_control_header(s, length - sec_bytes, PDU_TYPE_DATA, channel_id);
|
2012-11-08 03:23:25 +04:00
|
|
|
rdp_write_share_data_header(s, length - sec_bytes, type, rdp->settings->ShareId);
|
2011-07-21 23:10:53 +04:00
|
|
|
|
2013-05-15 23:54:33 +04:00
|
|
|
Stream_SetPosition(s, sec_hold);
|
2014-01-24 03:01:31 +04:00
|
|
|
length += rdp_security_stream_out(rdp, s, length, 0);
|
|
|
|
|
|
|
|
Stream_SetPosition(s, length);
|
|
|
|
Stream_SealLength(s);
|
|
|
|
|
|
|
|
if (transport_write(rdp->transport, s) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_send_message_channel_pdu(rdpRdp* rdp, wStream* s, UINT16 sec_flags)
|
|
|
|
{
|
|
|
|
UINT16 length;
|
|
|
|
UINT32 sec_bytes;
|
|
|
|
int sec_hold;
|
|
|
|
|
|
|
|
length = Stream_GetPosition(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
|
2014-02-14 02:06:33 +04:00
|
|
|
rdp_write_header(rdp, s, length, rdp->mcs->messageChannelId);
|
2014-01-24 03:01:31 +04:00
|
|
|
|
|
|
|
sec_bytes = rdp_get_sec_bytes(rdp);
|
|
|
|
sec_hold = Stream_GetPosition(s);
|
|
|
|
Stream_Seek(s, sec_bytes);
|
|
|
|
|
|
|
|
Stream_SetPosition(s, sec_hold);
|
|
|
|
length += rdp_security_stream_out(rdp, s, length, sec_flags);
|
2011-09-13 10:40:27 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, length);
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_SealLength(s);
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
if (transport_write(rdp->transport, s) < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-22 11:03:58 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-21 23:10:53 +04:00
|
|
|
}
|
|
|
|
|
2014-01-12 07:54:08 +04:00
|
|
|
BOOL rdp_recv_server_shutdown_denied_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_recv_server_set_keyboard_indicators_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
UINT16 unitId;
|
|
|
|
UINT16 ledFlags;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT16(s, unitId); /* unitId (2 bytes) */
|
|
|
|
Stream_Read_UINT16(s, ledFlags); /* ledFlags (2 bytes) */
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_recv_server_set_keyboard_ime_status_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
UINT16 unitId;
|
|
|
|
UINT32 imeState;
|
|
|
|
UINT32 imeConvMode;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 10)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT16(s, unitId); /* unitId (2 bytes) */
|
|
|
|
Stream_Read_UINT32(s, imeState); /* imeState (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, imeConvMode); /* imeConvMode (4 bytes) */
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_recv_set_error_info_data_pdu(rdpRdp* rdp, wStream* s)
|
2011-07-22 00:20:41 +04:00
|
|
|
{
|
2013-11-15 08:05:29 +04:00
|
|
|
UINT32 errorInfo;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
2013-02-12 05:38:19 +04:00
|
|
|
|
2013-11-15 08:05:29 +04:00
|
|
|
Stream_Read_UINT32(s, errorInfo); /* errorInfo (4 bytes) */
|
2011-07-22 00:20:41 +04:00
|
|
|
|
2013-11-15 08:05:29 +04:00
|
|
|
rdp_set_error_info(rdp, errorInfo);
|
2013-04-29 04:40:49 +04:00
|
|
|
|
2013-01-12 03:43:16 +04:00
|
|
|
return TRUE;
|
2011-07-22 00:20:41 +04:00
|
|
|
}
|
|
|
|
|
2014-01-12 07:54:08 +04:00
|
|
|
BOOL rdp_recv_server_auto_reconnect_status_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 arcStatus;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, arcStatus); /* arcStatus (4 bytes) */
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_recv_server_status_info_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
UINT32 statusCode;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, statusCode); /* statusCode (4 bytes) */
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_recv_monitor_layout_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
UINT32 index;
|
2014-01-12 07:54:08 +04:00
|
|
|
UINT32 monitorCount;
|
|
|
|
MONITOR_DEF* monitor;
|
|
|
|
MONITOR_DEF* monitorDefArray;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, monitorCount); /* monitorCount (4 bytes) */
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < (monitorCount * 20))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
monitorDefArray = (MONITOR_DEF*) malloc(sizeof(MONITOR_DEF) * monitorCount);
|
|
|
|
ZeroMemory(monitorDefArray, sizeof(MONITOR_DEF) * monitorCount);
|
|
|
|
|
|
|
|
for (index = 0; index < monitorCount; index++)
|
|
|
|
{
|
|
|
|
monitor = &(monitorDefArray[index]);
|
|
|
|
|
|
|
|
Stream_Read_UINT32(s, monitor->left); /* left (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->top); /* top (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->right); /* right (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->bottom); /* bottom (4 bytes) */
|
|
|
|
Stream_Read_UINT32(s, monitor->flags); /* flags (4 bytes) */
|
|
|
|
}
|
|
|
|
|
|
|
|
free(monitorDefArray);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rdp_write_monitor_layout_pdu(wStream* s, UINT32 monitorCount, MONITOR_DEF* monitorDefArray)
|
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
UINT32 index;
|
2014-01-12 07:54:08 +04:00
|
|
|
MONITOR_DEF* monitor;
|
|
|
|
|
|
|
|
Stream_EnsureRemainingCapacity(s, 4 + (monitorCount * 20));
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, monitorCount); /* monitorCount (4 bytes) */
|
|
|
|
|
|
|
|
for (index = 0; index < monitorCount; index++)
|
|
|
|
{
|
|
|
|
monitor = &(monitorDefArray[index]);
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, monitor->left); /* left (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, monitor->top); /* top (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, monitor->right); /* right (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, monitor->bottom); /* bottom (4 bytes) */
|
|
|
|
Stream_Write_UINT32(s, monitor->flags); /* flags (4 bytes) */
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int rdp_recv_data_pdu(rdpRdp* rdp, wStream* s)
|
2011-07-22 00:20:41 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE type;
|
2013-04-30 19:26:33 +04:00
|
|
|
wStream* cs;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2014-03-10 21:15:10 +04:00
|
|
|
UINT32 shareId;
|
|
|
|
BYTE compressedType;
|
|
|
|
UINT16 compressedLength;
|
2011-07-22 00:20:41 +04:00
|
|
|
|
2014-03-10 21:15:10 +04:00
|
|
|
if (!rdp_read_share_data_header(s, &length, &type, &shareId, &compressedType, &compressedLength))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-22 00:20:41 +04:00
|
|
|
|
2013-04-30 19:26:33 +04:00
|
|
|
cs = s;
|
2012-03-01 07:37:02 +04:00
|
|
|
|
2014-03-10 21:15:10 +04:00
|
|
|
if (compressedType & PACKET_COMPRESSED)
|
2012-03-01 07:37:02 +04:00
|
|
|
{
|
2014-03-10 21:15:10 +04:00
|
|
|
UINT32 DstSize = 0;
|
|
|
|
BYTE* pDstData = NULL;
|
|
|
|
UINT32 SrcSize = compressedLength - 18;
|
|
|
|
|
|
|
|
if (Stream_GetRemainingLength(s) < (size_t) SrcSize)
|
2013-02-02 01:43:20 +04:00
|
|
|
{
|
2014-03-10 21:15:10 +04:00
|
|
|
fprintf(stderr, "bulk_decompress: not enough bytes for compressedLength %d\n", compressedLength);
|
|
|
|
return -1;
|
2013-02-02 01:43:20 +04:00
|
|
|
}
|
2013-04-30 19:26:33 +04:00
|
|
|
|
2014-03-10 21:15:10 +04:00
|
|
|
if (bulk_decompress(rdp->bulk, Stream_Pointer(s), SrcSize, &pDstData, &DstSize, compressedType))
|
2012-03-01 07:37:02 +04:00
|
|
|
{
|
2014-03-10 21:15:10 +04:00
|
|
|
cs = StreamPool_Take(rdp->transport->ReceivePool, DstSize);
|
2013-04-30 19:26:33 +04:00
|
|
|
|
|
|
|
Stream_SetPosition(cs, 0);
|
2014-03-10 21:15:10 +04:00
|
|
|
Stream_Write(cs, pDstData, DstSize);
|
2013-04-30 19:26:33 +04:00
|
|
|
Stream_SealLength(cs);
|
|
|
|
Stream_SetPosition(cs, 0);
|
2012-03-01 07:37:02 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-10 21:15:10 +04:00
|
|
|
fprintf(stderr, "bulk_decompress() failed\n");
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2012-03-01 07:37:02 +04:00
|
|
|
}
|
2013-04-30 19:26:33 +04:00
|
|
|
|
2014-03-10 21:15:10 +04:00
|
|
|
Stream_Seek(s, SrcSize);
|
2012-03-01 07:37:02 +04:00
|
|
|
}
|
|
|
|
|
2011-09-02 21:29:17 +04:00
|
|
|
#ifdef WITH_DEBUG_RDP
|
2013-09-06 02:53:55 +04:00
|
|
|
printf("recv %s Data PDU (0x%02X), length: %d\n",
|
2014-01-24 03:01:31 +04:00
|
|
|
type < ARRAYSIZE(DATA_PDU_TYPE_STRINGS) ? DATA_PDU_TYPE_STRINGS[type] : "???", type, length);
|
2011-09-02 21:29:17 +04:00
|
|
|
#endif
|
2011-07-22 00:20:41 +04:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
2011-07-23 00:19:08 +04:00
|
|
|
case DATA_PDU_TYPE_UPDATE:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!update_recv(rdp->update, cs))
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_CONTROL:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!rdp_recv_server_control_pdu(rdp, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_POINTER:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!update_recv_pointer(rdp->update, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SYNCHRONIZE:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!rdp_recv_synchronize_pdu(rdp, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_PLAY_SOUND:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!update_recv_play_sound(rdp->update, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SHUTDOWN_DENIED:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_server_shutdown_denied_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SAVE_SESSION_INFO:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!rdp_recv_save_session_info(rdp, cs))
|
2013-02-02 01:43:20 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_FONT_MAP:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!rdp_recv_font_map_pdu(rdp, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SET_KEYBOARD_INDICATORS:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_server_set_keyboard_indicators_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SET_KEYBOARD_IME_STATUS:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_server_set_keyboard_ime_status_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
2011-07-22 00:20:41 +04:00
|
|
|
case DATA_PDU_TYPE_SET_ERROR_INFO:
|
2013-04-30 19:26:33 +04:00
|
|
|
if (!rdp_recv_set_error_info_data_pdu(rdp, cs))
|
2013-01-12 03:43:16 +04:00
|
|
|
return -1;
|
2011-07-22 00:20:41 +04:00
|
|
|
break;
|
|
|
|
|
2011-07-23 00:19:08 +04:00
|
|
|
case DATA_PDU_TYPE_ARC_STATUS:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_server_auto_reconnect_status_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_STATUS_INFO:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_server_status_info_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_MONITOR_LAYOUT:
|
2014-01-12 07:54:08 +04:00
|
|
|
if (!rdp_recv_monitor_layout_pdu(rdp, cs))
|
|
|
|
return -1;
|
2011-07-23 00:19:08 +04:00
|
|
|
break;
|
|
|
|
|
2011-07-22 00:20:41 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-02-11 17:30:09 +04:00
|
|
|
|
2013-04-30 19:26:33 +04:00
|
|
|
if (cs != s)
|
|
|
|
Stream_Release(cs);
|
2012-03-01 07:37:02 +04:00
|
|
|
|
2013-01-09 02:18:10 +04:00
|
|
|
return 0;
|
2011-07-22 00:20:41 +04:00
|
|
|
}
|
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
int rdp_recv_message_channel_pdu(rdpRdp* rdp, wStream* s)
|
|
|
|
{
|
|
|
|
UINT16 securityFlags;
|
|
|
|
|
|
|
|
if (!rdp_read_security_header(s, &securityFlags))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (securityFlags & SEC_AUTODETECT_REQ)
|
|
|
|
{
|
|
|
|
/* Server Auto-Detect Request PDU */
|
|
|
|
return rdp_recv_autodetect_packet(rdp, s);
|
|
|
|
}
|
|
|
|
|
2014-01-24 07:23:47 +04:00
|
|
|
if (securityFlags & SEC_HEARTBEAT)
|
|
|
|
{
|
|
|
|
/* Heartbeat PDU */
|
|
|
|
return rdp_recv_heartbeat_packet(rdp, s);
|
|
|
|
}
|
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
if (securityFlags & SEC_TRANSPORT_REQ)
|
|
|
|
{
|
|
|
|
/* Initiate Multitransport Request PDU */
|
|
|
|
return rdp_recv_multitransport_packet(rdp, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-11-05 00:52:29 +04:00
|
|
|
int rdp_recv_out_of_sequence_pdu(rdpRdp* rdp, wStream* s)
|
2011-09-03 05:34:51 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 type;
|
|
|
|
UINT16 length;
|
|
|
|
UINT16 channelId;
|
2011-09-03 05:34:51 +04:00
|
|
|
|
2013-02-05 01:39:05 +04:00
|
|
|
if (!rdp_read_share_control_header(s, &length, &type, &channelId))
|
2013-11-05 00:52:29 +04:00
|
|
|
return -1;
|
2011-09-03 05:34:51 +04:00
|
|
|
|
|
|
|
if (type == PDU_TYPE_DATA)
|
|
|
|
{
|
2013-11-05 00:52:29 +04:00
|
|
|
return rdp_recv_data_pdu(rdp, s);
|
2011-09-03 05:34:51 +04:00
|
|
|
}
|
|
|
|
else if (type == PDU_TYPE_SERVER_REDIRECTION)
|
|
|
|
{
|
2013-01-12 03:43:16 +04:00
|
|
|
return rdp_recv_enhanced_security_redirection_packet(rdp, s);
|
2011-09-03 05:34:51 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-05 00:52:29 +04:00
|
|
|
return -1;
|
2011-09-03 05:34:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-05 22:02:52 +04:00
|
|
|
/**
|
|
|
|
* Decrypt an RDP packet.\n
|
|
|
|
* @param rdp RDP module
|
|
|
|
* @param s stream
|
|
|
|
* @param length int
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL rdp_decrypt(rdpRdp* rdp, wStream* s, int length, UINT16 securityFlags)
|
2011-09-05 22:02:52 +04:00
|
|
|
{
|
2012-11-28 09:32:12 +04:00
|
|
|
BYTE cmac[8];
|
|
|
|
BYTE wmac[8];
|
2011-09-15 01:14:50 +04:00
|
|
|
|
2012-11-08 08:29:24 +04:00
|
|
|
if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
|
2011-09-16 03:54:03 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 len;
|
|
|
|
BYTE version, pad;
|
2012-11-28 09:32:12 +04:00
|
|
|
BYTE* sig;
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 12)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
2013-01-17 03:01:10 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16(s, len); /* 0x10 */
|
|
|
|
Stream_Read_UINT8(s, version); /* 0x1 */
|
|
|
|
Stream_Read_UINT8(s, pad);
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
sig = Stream_Pointer(s);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 8); /* signature */
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2012-01-16 00:01:16 +04:00
|
|
|
length -= 12;
|
2011-09-16 03:54:03 +04:00
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
if (!security_fips_decrypt(Stream_Pointer(s), length, rdp))
|
2011-09-16 03:54:03 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "FATAL: cannot decrypt\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE; /* TODO */
|
2011-09-16 03:54:03 +04:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:14:26 +04:00
|
|
|
if (!security_fips_check_signature(Stream_Pointer(s), length - pad, sig, rdp))
|
2011-09-16 03:54:03 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "FATAL: invalid packet signature\n");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE; /* TODO */
|
2011-09-16 03:54:03 +04:00
|
|
|
}
|
|
|
|
|
2014-04-02 16:17:39 +04:00
|
|
|
Stream_Length(s) -= pad;
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-16 03:54:03 +04:00
|
|
|
}
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 8)
|
2013-01-12 03:43:16 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read(s, wmac, sizeof(wmac));
|
2012-01-16 00:01:16 +04:00
|
|
|
length -= sizeof(wmac);
|
2013-05-15 20:14:26 +04:00
|
|
|
|
|
|
|
if (!security_decrypt(Stream_Pointer(s), length, rdp))
|
2013-03-05 11:08:03 +04:00
|
|
|
return FALSE;
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2012-01-24 18:58:30 +04:00
|
|
|
if (securityFlags & SEC_SECURE_CHECKSUM)
|
2013-05-15 20:14:26 +04:00
|
|
|
security_salted_mac_signature(rdp, Stream_Pointer(s), length, FALSE, cmac);
|
2012-01-24 18:58:30 +04:00
|
|
|
else
|
2013-05-15 20:14:26 +04:00
|
|
|
security_mac_signature(rdp, Stream_Pointer(s), length, cmac);
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2012-01-25 19:26:32 +04:00
|
|
|
if (memcmp(wmac, cmac, sizeof(wmac)) != 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "WARNING: invalid packet signature\n");
|
2012-01-24 18:58:30 +04:00
|
|
|
/*
|
|
|
|
* Because Standard RDP Security is totally broken,
|
|
|
|
* and cannot protect against MITM, don't treat signature
|
|
|
|
* verification failure as critical. This at least enables
|
|
|
|
* us to work with broken RDP clients and servers that
|
|
|
|
* generate invalid signatures.
|
|
|
|
*/
|
2012-10-09 10:31:28 +04:00
|
|
|
//return FALSE;
|
2012-01-16 00:01:16 +04:00
|
|
|
}
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-05 22:02:52 +04:00
|
|
|
}
|
|
|
|
|
2011-07-12 04:46:03 +04:00
|
|
|
/**
|
2011-08-01 08:43:53 +04:00
|
|
|
* Process an RDP packet.\n
|
2011-07-12 04:46:03 +04:00
|
|
|
* @param rdp RDP module
|
2011-08-01 08:43:53 +04:00
|
|
|
* @param s stream
|
2011-07-12 04:46:03 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int rdp_recv_tpkt_pdu(rdpRdp* rdp, wStream* s)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
|
|
|
UINT16 pduType;
|
|
|
|
UINT16 pduLength;
|
|
|
|
UINT16 pduSource;
|
2013-08-30 18:31:23 +04:00
|
|
|
UINT16 channelId = 0;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 securityFlags;
|
2013-05-15 22:42:37 +04:00
|
|
|
int nextPosition;
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2011-08-20 12:43:29 +04:00
|
|
|
if (!rdp_read_header(rdp, s, &length, &channelId))
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Incorrect RDP header.\n");
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2011-07-20 00:30:05 +04:00
|
|
|
}
|
2011-08-08 07:59:28 +04:00
|
|
|
|
2012-11-08 08:29:24 +04:00
|
|
|
if (rdp->settings->DisableEncryption)
|
2011-09-05 22:02:52 +04:00
|
|
|
{
|
2013-01-12 03:43:16 +04:00
|
|
|
if (!rdp_read_security_header(s, &securityFlags))
|
|
|
|
return -1;
|
2012-11-28 09:32:12 +04:00
|
|
|
|
|
|
|
if (securityFlags & (SEC_ENCRYPT | SEC_REDIRECTION_PKT))
|
2011-09-05 22:02:52 +04:00
|
|
|
{
|
2012-01-24 18:58:30 +04:00
|
|
|
if (!rdp_decrypt(rdp, s, length - 4, securityFlags))
|
2011-09-05 22:02:52 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rdp_decrypt failed\n");
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2011-09-05 22:02:52 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-28 09:32:12 +04:00
|
|
|
|
2012-01-24 18:57:06 +04:00
|
|
|
if (securityFlags & SEC_REDIRECTION_PKT)
|
2011-12-21 23:59:31 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* [MS-RDPBCGR] 2.2.13.2.1
|
|
|
|
* - no share control header, nor the 2 byte pad
|
|
|
|
*/
|
2013-05-15 20:14:26 +04:00
|
|
|
Stream_Rewind(s, 2);
|
2013-11-05 00:52:29 +04:00
|
|
|
|
|
|
|
return rdp_recv_enhanced_security_redirection_packet(rdp, s);
|
2011-12-21 23:59:31 +04:00
|
|
|
}
|
2011-09-05 22:02:52 +04:00
|
|
|
}
|
|
|
|
|
2014-01-24 03:01:31 +04:00
|
|
|
if (channelId == MCS_GLOBAL_CHANNEL_ID)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
while (Stream_GetRemainingLength(s) > 3)
|
2012-03-09 05:47:01 +04:00
|
|
|
{
|
2013-05-15 22:42:37 +04:00
|
|
|
nextPosition = Stream_GetPosition(s);
|
2013-01-17 03:01:10 +04:00
|
|
|
|
2013-01-12 03:43:16 +04:00
|
|
|
if (!rdp_read_share_control_header(s, &pduLength, &pduType, &pduSource))
|
|
|
|
return -1;
|
2013-01-17 03:01:10 +04:00
|
|
|
|
2013-05-15 22:42:37 +04:00
|
|
|
nextPosition += pduLength;
|
2011-11-19 11:33:49 +04:00
|
|
|
|
2012-11-08 03:23:25 +04:00
|
|
|
rdp->settings->PduSource = pduSource;
|
2011-07-20 00:30:05 +04:00
|
|
|
|
2012-03-09 05:47:01 +04:00
|
|
|
switch (pduType)
|
|
|
|
{
|
|
|
|
case PDU_TYPE_DATA:
|
2013-01-09 02:18:10 +04:00
|
|
|
if (rdp_recv_data_pdu(rdp, s) < 0)
|
2012-03-09 05:47:01 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "rdp_recv_data_pdu failed\n");
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2012-03-09 05:47:01 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PDU_TYPE_DEACTIVATE_ALL:
|
|
|
|
if (!rdp_recv_deactivate_all(rdp, s))
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2012-03-09 05:47:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PDU_TYPE_SERVER_REDIRECTION:
|
2013-11-05 00:52:29 +04:00
|
|
|
return rdp_recv_enhanced_security_redirection_packet(rdp, s);
|
2012-03-09 05:47:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "incorrect PDU type: 0x%04X\n", pduType);
|
2012-03-09 05:47:01 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-05-15 22:42:37 +04:00
|
|
|
|
|
|
|
Stream_SetPosition(s, nextPosition);
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
}
|
2014-03-15 00:56:10 +04:00
|
|
|
else if (rdp->mcs->messageChannelId && channelId == rdp->mcs->messageChannelId)
|
2014-01-24 03:01:31 +04:00
|
|
|
{
|
|
|
|
return rdp_recv_message_channel_pdu(rdp, s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!freerdp_channel_process(rdp->instance, s, channelId))
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-22 11:03:58 +04:00
|
|
|
|
2013-01-09 02:18:10 +04:00
|
|
|
return 0;
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int rdp_recv_fastpath_pdu(rdpRdp* rdp, wStream* s)
|
2011-08-09 12:40:58 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2011-09-27 09:30:58 +04:00
|
|
|
rdpFastPath* fastpath;
|
2011-08-09 19:28:02 +04:00
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
fastpath = rdp->fastpath;
|
2013-01-17 03:01:10 +04:00
|
|
|
|
2013-01-14 02:37:50 +04:00
|
|
|
if (!fastpath_read_header_rdp(fastpath, s, &length))
|
|
|
|
return -1;
|
2012-01-25 19:27:00 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if ((length == 0) || (length > Stream_GetRemainingLength(s)))
|
2011-08-13 12:37:03 +04:00
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "incorrect FastPath PDU header length %d\n", length);
|
2013-01-09 02:18:10 +04:00
|
|
|
return -1;
|
2011-08-13 12:37:03 +04:00
|
|
|
}
|
2011-08-09 19:28:02 +04:00
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
if (fastpath->encryptionFlags & FASTPATH_OUTPUT_ENCRYPTED)
|
2011-09-15 01:14:50 +04:00
|
|
|
{
|
2013-01-12 03:43:16 +04:00
|
|
|
UINT16 flags = (fastpath->encryptionFlags & FASTPATH_OUTPUT_SECURE_CHECKSUM) ? SEC_SECURE_CHECKSUM : 0;
|
2013-01-17 03:01:10 +04:00
|
|
|
|
2013-01-12 03:43:16 +04:00
|
|
|
if (!rdp_decrypt(rdp, s, length, flags))
|
|
|
|
return -1;
|
2011-09-15 01:14:50 +04:00
|
|
|
}
|
2011-08-09 12:40:58 +04:00
|
|
|
|
2011-08-23 09:05:58 +04:00
|
|
|
return fastpath_recv_updates(rdp->fastpath, s);
|
2011-08-09 12:40:58 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int rdp_recv_pdu(rdpRdp* rdp, wStream* s)
|
2011-08-09 12:40:58 +04:00
|
|
|
{
|
|
|
|
if (tpkt_verify_header(s))
|
2011-08-23 08:58:10 +04:00
|
|
|
return rdp_recv_tpkt_pdu(rdp, s);
|
2011-08-09 12:40:58 +04:00
|
|
|
else
|
2011-08-23 08:58:10 +04:00
|
|
|
return rdp_recv_fastpath_pdu(rdp, s);
|
2011-08-09 12:40:58 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int rdp_recv_callback(rdpTransport* transport, wStream* s, void* extra)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
2013-01-09 02:18:10 +04:00
|
|
|
int status = 0;
|
2011-08-01 08:43:53 +04:00
|
|
|
rdpRdp* rdp = (rdpRdp*) extra;
|
|
|
|
|
2014-01-30 07:53:32 +04:00
|
|
|
/*
|
|
|
|
* At any point in the connection sequence between when all
|
|
|
|
* MCS channels have been joined and when the RDP connection
|
|
|
|
* enters the active state, an auto-detect PDU can be received
|
|
|
|
* on the MCS message channel.
|
|
|
|
*/
|
|
|
|
if ((rdp->state > CONNECTION_STATE_MCS_CHANNEL_JOIN) &&
|
|
|
|
(rdp->state < CONNECTION_STATE_ACTIVE))
|
|
|
|
{
|
|
|
|
if (rdp_client_connect_auto_detect(rdp, s))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
switch (rdp->state)
|
|
|
|
{
|
|
|
|
case CONNECTION_STATE_NEGO:
|
|
|
|
if (!rdp_client_connect_mcs_connect_response(rdp, s))
|
2013-01-09 02:18:10 +04:00
|
|
|
status = -1;
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_ATTACH_USER:
|
|
|
|
if (!rdp_client_connect_mcs_attach_user_confirm(rdp, s))
|
2013-01-09 02:18:10 +04:00
|
|
|
status = -1;
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_CHANNEL_JOIN:
|
|
|
|
if (!rdp_client_connect_mcs_channel_join_confirm(rdp, s))
|
2013-01-09 02:18:10 +04:00
|
|
|
status = -1;
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_LICENSING:
|
2013-11-05 00:52:29 +04:00
|
|
|
status = rdp_client_connect_license(rdp, s);
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
|
2013-11-05 00:52:29 +04:00
|
|
|
status = rdp_client_connect_demand_active(rdp, s);
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
2011-12-19 00:15:48 +04:00
|
|
|
case CONNECTION_STATE_FINALIZATION:
|
2013-01-09 02:18:10 +04:00
|
|
|
status = rdp_recv_pdu(rdp, s);
|
2013-07-19 01:15:10 +04:00
|
|
|
|
2013-01-09 02:18:10 +04:00
|
|
|
if ((status >= 0) && (rdp->finalize_sc_pdus == FINALIZE_SC_COMPLETE))
|
2013-07-19 01:15:10 +04:00
|
|
|
rdp_client_transition_to_state(rdp, CONNECTION_STATE_ACTIVE);
|
2011-12-19 00:15:48 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-22 11:03:58 +04:00
|
|
|
case CONNECTION_STATE_ACTIVE:
|
2013-01-09 02:18:10 +04:00
|
|
|
status = rdp_recv_pdu(rdp, s);
|
2011-08-22 11:03:58 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Invalid state %d\n", rdp->state);
|
2013-01-09 02:18:10 +04:00
|
|
|
status = -1;
|
|
|
|
break;
|
2011-08-22 11:03:58 +04:00
|
|
|
}
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2013-01-09 02:18:10 +04:00
|
|
|
return status;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2014-02-16 01:32:38 +04:00
|
|
|
int rdp_send_channel_data(rdpRdp* rdp, UINT16 channelId, BYTE* data, int size)
|
2011-08-03 07:00:56 +04:00
|
|
|
{
|
2014-02-14 02:06:33 +04:00
|
|
|
return freerdp_channel_send(rdp, channelId, data, size);
|
2011-08-03 07:00:56 +04:00
|
|
|
}
|
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
/**
|
|
|
|
* Set non-blocking mode information.
|
|
|
|
* @param rdp RDP module
|
|
|
|
* @param blocking blocking mode
|
|
|
|
*/
|
2012-10-09 10:38:39 +04:00
|
|
|
void rdp_set_blocking_mode(rdpRdp* rdp, BOOL blocking)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
2012-12-22 00:49:02 +04:00
|
|
|
rdp->transport->ReceiveCallback = rdp_recv_callback;
|
|
|
|
rdp->transport->ReceiveExtra = rdp;
|
2011-08-01 08:43:53 +04:00
|
|
|
transport_set_blocking_mode(rdp->transport, blocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rdp_check_fds(rdpRdp* rdp)
|
|
|
|
{
|
2013-11-01 22:13:09 +04:00
|
|
|
int status;
|
2014-04-29 00:44:52 +04:00
|
|
|
|
2013-11-04 01:25:56 +04:00
|
|
|
status = transport_check_fds(rdp->transport);
|
2014-04-29 00:44:52 +04:00
|
|
|
|
|
|
|
if (status == 1)
|
|
|
|
{
|
|
|
|
status = rdp_client_redirect(rdp); /* session redirection */
|
|
|
|
}
|
|
|
|
|
2013-11-01 22:13:09 +04:00
|
|
|
return status;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
/**
|
|
|
|
* Instantiate new RDP module.
|
|
|
|
* @return new RDP module
|
|
|
|
*/
|
|
|
|
|
2013-07-20 05:52:28 +04:00
|
|
|
rdpRdp* rdp_new(rdpContext* context)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
|
|
|
rdpRdp* rdp;
|
2013-10-13 02:20:25 +04:00
|
|
|
DWORD flags;
|
2014-04-10 23:07:53 +04:00
|
|
|
BOOL newSettings = FALSE;
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
rdp = (rdpRdp*) calloc(1, sizeof(rdpRdp));
|
|
|
|
if (!rdp)
|
|
|
|
return NULL;
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
rdp->context = context;
|
|
|
|
rdp->instance = context->instance;
|
2013-10-13 02:20:25 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
flags = 0;
|
2013-10-13 02:20:25 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (context->ServerMode)
|
|
|
|
flags |= FREERDP_SETTINGS_SERVER_MODE;
|
2013-10-13 02:20:25 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (!context->settings)
|
|
|
|
{
|
|
|
|
context->settings = freerdp_settings_new(flags);
|
2013-10-13 06:58:09 +04:00
|
|
|
if (!context->settings)
|
2014-04-10 23:07:53 +04:00
|
|
|
goto out_free;
|
|
|
|
newSettings = TRUE;
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
rdp->settings = context->settings;
|
|
|
|
rdp->settings->instance = context->instance;
|
2014-04-14 22:49:03 +04:00
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
if (context->instance)
|
|
|
|
context->instance->settings = rdp->settings;
|
|
|
|
|
|
|
|
rdp->transport = transport_new(rdp->settings);
|
|
|
|
if (!rdp->transport)
|
2014-04-14 22:49:03 +04:00
|
|
|
goto out_free_settings;
|
|
|
|
|
2014-04-10 23:07:53 +04:00
|
|
|
rdp->transport->rdp = rdp;
|
|
|
|
|
|
|
|
rdp->license = license_new(rdp);
|
|
|
|
if (!rdp->license)
|
|
|
|
goto out_free_transport;
|
|
|
|
|
|
|
|
rdp->input = input_new(rdp);
|
|
|
|
if (!rdp->input)
|
|
|
|
goto out_free_license;
|
|
|
|
|
|
|
|
rdp->update = update_new(rdp);
|
|
|
|
if (!rdp->update)
|
|
|
|
goto out_free_input;
|
|
|
|
|
|
|
|
rdp->fastpath = fastpath_new(rdp);
|
|
|
|
if (!rdp->fastpath)
|
|
|
|
goto out_free_update;
|
|
|
|
|
|
|
|
rdp->nego = nego_new(rdp->transport);
|
|
|
|
if (!rdp->nego)
|
|
|
|
goto out_free_fastpath;
|
|
|
|
|
|
|
|
rdp->mcs = mcs_new(rdp->transport);
|
|
|
|
if (!rdp->mcs)
|
|
|
|
goto out_free_nego;
|
|
|
|
|
|
|
|
rdp->redirection = redirection_new();
|
|
|
|
if (!rdp->redirection)
|
|
|
|
goto out_free_mcs;
|
|
|
|
|
|
|
|
rdp->autodetect = autodetect_new();
|
|
|
|
if (!rdp->autodetect)
|
|
|
|
goto out_free_redirection;
|
|
|
|
|
|
|
|
rdp->heartbeat = heartbeat_new();
|
|
|
|
if (!rdp->heartbeat)
|
|
|
|
goto out_free_autodetect;
|
|
|
|
|
|
|
|
rdp->multitransport = multitransport_new();
|
|
|
|
if (!rdp->multitransport)
|
|
|
|
goto out_free_heartbeat;
|
|
|
|
|
|
|
|
rdp->bulk = bulk_new(context);
|
|
|
|
if (!rdp->bulk)
|
|
|
|
goto out_free_multitransport;
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
return rdp;
|
2014-04-10 23:07:53 +04:00
|
|
|
|
|
|
|
out_free_multitransport:
|
|
|
|
multitransport_free(rdp->multitransport);
|
|
|
|
out_free_heartbeat:
|
|
|
|
heartbeat_free(rdp->heartbeat);
|
|
|
|
out_free_autodetect:
|
|
|
|
autodetect_free(rdp->autodetect);
|
|
|
|
out_free_redirection:
|
|
|
|
redirection_free(rdp->redirection);
|
|
|
|
out_free_mcs:
|
|
|
|
mcs_free(rdp->mcs);
|
|
|
|
out_free_nego:
|
|
|
|
nego_free(rdp->nego);
|
|
|
|
out_free_fastpath:
|
|
|
|
fastpath_free(rdp->fastpath);
|
|
|
|
out_free_update:
|
|
|
|
update_free(rdp->update);
|
|
|
|
out_free_input:
|
|
|
|
input_free(rdp->input);
|
|
|
|
out_free_license:
|
|
|
|
license_free(rdp->license);
|
|
|
|
out_free_transport:
|
|
|
|
transport_free(rdp->transport);
|
|
|
|
out_free_settings:
|
|
|
|
if (newSettings)
|
|
|
|
freerdp_settings_free(rdp->settings);
|
|
|
|
out_free:
|
|
|
|
free(rdp);
|
|
|
|
return NULL;
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 01:25:56 +04:00
|
|
|
void rdp_reset(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
rdpSettings* settings;
|
|
|
|
|
|
|
|
settings = rdp->settings;
|
|
|
|
|
2014-03-10 19:41:10 +04:00
|
|
|
bulk_reset(rdp->bulk);
|
|
|
|
|
2013-11-04 01:25:56 +04:00
|
|
|
crypto_rc4_free(rdp->rc4_decrypt_key);
|
|
|
|
rdp->rc4_decrypt_key = NULL;
|
|
|
|
crypto_rc4_free(rdp->rc4_encrypt_key);
|
|
|
|
rdp->rc4_encrypt_key = NULL;
|
|
|
|
crypto_des3_free(rdp->fips_encrypt);
|
|
|
|
rdp->fips_encrypt = NULL;
|
|
|
|
crypto_des3_free(rdp->fips_decrypt);
|
|
|
|
rdp->fips_decrypt = NULL;
|
|
|
|
crypto_hmac_free(rdp->fips_hmac);
|
|
|
|
rdp->fips_hmac = NULL;
|
|
|
|
|
|
|
|
mcs_free(rdp->mcs);
|
|
|
|
nego_free(rdp->nego);
|
|
|
|
license_free(rdp->license);
|
|
|
|
transport_free(rdp->transport);
|
|
|
|
|
|
|
|
free(settings->ServerRandom);
|
|
|
|
settings->ServerRandom = NULL;
|
|
|
|
free(settings->ServerCertificate);
|
|
|
|
settings->ServerCertificate = NULL;
|
|
|
|
free(settings->ClientAddress);
|
|
|
|
settings->ClientAddress = NULL;
|
|
|
|
|
|
|
|
rdp->transport = transport_new(rdp->settings);
|
2013-12-21 03:26:07 +04:00
|
|
|
rdp->transport->rdp = rdp;
|
2013-11-04 01:25:56 +04:00
|
|
|
rdp->license = license_new(rdp);
|
|
|
|
rdp->nego = nego_new(rdp->transport);
|
|
|
|
rdp->mcs = mcs_new(rdp->transport);
|
|
|
|
rdp->transport->layer = TRANSPORT_LAYER_TCP;
|
|
|
|
}
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
/**
|
|
|
|
* Free RDP module.
|
2011-07-12 04:46:03 +04:00
|
|
|
* @param rdp RDP module to be freed
|
2011-07-12 02:46:36 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void rdp_free(rdpRdp* rdp)
|
|
|
|
{
|
2013-11-01 22:13:09 +04:00
|
|
|
if (rdp)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2012-02-11 00:35:22 +04:00
|
|
|
crypto_rc4_free(rdp->rc4_decrypt_key);
|
|
|
|
crypto_rc4_free(rdp->rc4_encrypt_key);
|
|
|
|
crypto_des3_free(rdp->fips_encrypt);
|
|
|
|
crypto_des3_free(rdp->fips_decrypt);
|
|
|
|
crypto_hmac_free(rdp->fips_hmac);
|
2012-11-09 04:01:52 +04:00
|
|
|
freerdp_settings_free(rdp->settings);
|
2013-11-01 22:13:09 +04:00
|
|
|
freerdp_settings_free(rdp->settingsCopy);
|
2011-07-12 04:46:03 +04:00
|
|
|
transport_free(rdp->transport);
|
|
|
|
license_free(rdp->license);
|
2011-07-28 08:38:25 +04:00
|
|
|
input_free(rdp->input);
|
2011-07-27 02:32:14 +04:00
|
|
|
update_free(rdp->update);
|
2011-08-09 19:28:02 +04:00
|
|
|
fastpath_free(rdp->fastpath);
|
2011-08-25 09:45:43 +04:00
|
|
|
nego_free(rdp->nego);
|
2011-07-12 04:46:03 +04:00
|
|
|
mcs_free(rdp->mcs);
|
2011-09-04 00:36:27 +04:00
|
|
|
redirection_free(rdp->redirection);
|
2014-01-24 03:01:31 +04:00
|
|
|
autodetect_free(rdp->autodetect);
|
2014-01-24 17:38:28 +04:00
|
|
|
heartbeat_free(rdp->heartbeat);
|
2014-01-24 03:01:31 +04:00
|
|
|
multitransport_free(rdp->multitransport);
|
2014-03-10 19:41:10 +04:00
|
|
|
bulk_free(rdp->bulk);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(rdp);
|
2011-07-12 02:46:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|