2011-07-10 23:34:43 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Client
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rdp.h"
|
|
|
|
|
2011-07-11 23:58:39 +04:00
|
|
|
/**
|
|
|
|
* Read RDP Security Header.\n
|
|
|
|
* @msdn{cc240579}
|
|
|
|
* @param s stream
|
|
|
|
* @param flags security flags
|
|
|
|
*/
|
|
|
|
|
|
|
|
void rdp_read_security_header(STREAM* s, uint16* flags)
|
|
|
|
{
|
|
|
|
/* Basic Security Header */
|
|
|
|
stream_read_uint16(s, *flags); /* flags */
|
|
|
|
stream_seek(s, 2); /* flagsHi (unused) */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write RDP Security Header.\n
|
|
|
|
* @msdn{cc240579}
|
|
|
|
* @param s stream
|
|
|
|
* @param flags security flags
|
|
|
|
*/
|
|
|
|
|
2011-07-10 23:34:43 +04:00
|
|
|
void rdp_write_security_header(STREAM* s, uint16 flags)
|
|
|
|
{
|
|
|
|
/* Basic Security Header */
|
|
|
|
stream_write_uint16(s, flags); /* flags */
|
|
|
|
stream_write_uint16(s, 0); /* flagsHi (unused) */
|
|
|
|
}
|
2011-07-12 02:46:36 +04:00
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
void rdp_read_share_control_header(STREAM* s, uint16* length, uint16* type, uint16* channel_id)
|
|
|
|
{
|
|
|
|
/* Share Control Header */
|
|
|
|
stream_read_uint16(s, *length); /* totalLength */
|
|
|
|
stream_read_uint16(s, *type); /* pduType */
|
|
|
|
stream_read_uint16(s, *channel_id); /* pduSource */
|
|
|
|
*type &= 0x0F; /* type is in the 4 least significant bits */
|
|
|
|
}
|
|
|
|
|
2011-07-21 21:57:57 +04:00
|
|
|
void rdp_write_share_control_header(STREAM* s, uint16 length, uint16 type, uint16 channel_id)
|
|
|
|
{
|
|
|
|
/* Share Control Header */
|
|
|
|
stream_write_uint16(s, length); /* totalLength */
|
|
|
|
stream_write_uint16(s, type); /* pduType */
|
|
|
|
stream_write_uint16(s, channel_id); /* pduSource */
|
|
|
|
}
|
|
|
|
|
2011-07-22 00:20:41 +04:00
|
|
|
void rdp_read_share_data_header(STREAM* s, uint16* length, uint8* type, uint32* share_id)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
|
|
|
/* Share Data Header */
|
2011-07-22 00:20:41 +04:00
|
|
|
stream_read_uint32(s, *share_id); /* shareId (4 bytes) */
|
2011-07-21 23:10:53 +04:00
|
|
|
stream_seek_uint8(s); /* pad1 (1 byte) */
|
|
|
|
stream_seek_uint8(s); /* streamId (1 byte) */
|
2011-07-22 00:20:41 +04:00
|
|
|
stream_read_uint16(s, *length); /* uncompressedLength (2 bytes) */
|
|
|
|
stream_read_uint8(s, *type); /* pduType2, Data PDU Type (1 byte) */
|
|
|
|
stream_seek_uint8(s); /* compressedType (1 byte) */
|
|
|
|
stream_seek_uint16(s); /* compressedLength (2 bytes) */
|
2011-07-21 23:10:53 +04:00
|
|
|
}
|
|
|
|
|
2011-07-22 00:20:41 +04:00
|
|
|
void rdp_write_share_data_header(STREAM* s, uint16 length, uint8 type, uint32 share_id)
|
2011-07-21 23:10:53 +04:00
|
|
|
{
|
|
|
|
/* Share Data Header */
|
2011-07-22 00:20:41 +04:00
|
|
|
stream_write_uint32(s, share_id); /* shareId (4 bytes) */
|
2011-07-21 23:10:53 +04:00
|
|
|
stream_write_uint8(s, 0); /* pad1 (1 byte) */
|
2011-07-22 00:20:41 +04:00
|
|
|
stream_write_uint8(s, STREAM_LOW); /* streamId (1 byte) */
|
2011-07-21 23:10:53 +04:00
|
|
|
stream_write_uint16(s, length); /* uncompressedLength (2 bytes) */
|
|
|
|
stream_write_uint8(s, type); /* pduType2, Data PDU Type (1 byte) */
|
|
|
|
stream_write_uint16(s, length); /* compressedLength (2 byte2) */
|
|
|
|
}
|
|
|
|
|
2011-07-12 04:46:03 +04:00
|
|
|
/**
|
|
|
|
* Initialize an RDP packet stream.\n
|
|
|
|
* @param rdp rdp module
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
STREAM* rdp_send_stream_init(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
2011-07-21 21:57:57 +04:00
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
2011-07-12 02:46:36 +04:00
|
|
|
stream_seek(s, RDP_PACKET_HEADER_LENGTH);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-07-22 04:06:28 +04:00
|
|
|
STREAM* rdp_pdu_init(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
|
|
|
stream_seek(s, RDP_PACKET_HEADER_LENGTH);
|
|
|
|
stream_seek(s, RDP_SHARE_CONTROL_HEADER_LENGTH);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-07-21 23:10:53 +04:00
|
|
|
STREAM* rdp_data_pdu_init(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
s = transport_send_stream_init(rdp->transport, 2048);
|
|
|
|
stream_seek(s, RDP_PACKET_HEADER_LENGTH);
|
|
|
|
stream_seek(s, RDP_SHARE_DATA_HEADER_LENGTH);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
void rdp_write_header(rdpRdp* rdp, STREAM* s, int length)
|
|
|
|
{
|
|
|
|
mcs_write_domain_mcspdu_header(s, DomainMCSPDU_SendDataRequest, length);
|
|
|
|
per_write_integer16(s, rdp->mcs->user_id, MCS_BASE_CHANNEL_ID); /* initiator */
|
|
|
|
per_write_integer16(s, MCS_GLOBAL_CHANNEL_ID, 0); /* channelId */
|
|
|
|
stream_write_uint8(s, 0x70); /* dataPriority + segmentation */
|
|
|
|
per_write_length(s, length - RDP_PACKET_HEADER_LENGTH); /* userData (OCTET_STRING) */
|
|
|
|
}
|
|
|
|
|
2011-07-12 04:46:03 +04:00
|
|
|
/**
|
|
|
|
* Send an RDP packet.\n
|
|
|
|
* @param rdp RDP module
|
|
|
|
* @param s stream
|
|
|
|
*/
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
void rdp_send(rdpRdp* rdp, STREAM* s)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = stream_get_length(s);
|
|
|
|
stream_set_pos(s, 0);
|
|
|
|
|
2011-07-12 09:16:59 +04:00
|
|
|
rdp_write_header(rdp, s, length);
|
2011-07-12 02:46:36 +04:00
|
|
|
|
|
|
|
stream_set_pos(s, length);
|
|
|
|
transport_write(rdp->transport, s);
|
|
|
|
}
|
|
|
|
|
2011-07-22 04:06:28 +04:00
|
|
|
void rdp_send_pdu(rdpRdp* rdp, STREAM* s, uint16 type, uint16 channel_id)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = stream_get_length(s);
|
|
|
|
stream_set_pos(s, 0);
|
|
|
|
|
|
|
|
rdp_write_header(rdp, s, length);
|
|
|
|
rdp_write_share_control_header(s, length, PDU_TYPE_DATA, channel_id);
|
|
|
|
|
|
|
|
stream_set_pos(s, length);
|
|
|
|
transport_write(rdp->transport, s);
|
|
|
|
}
|
|
|
|
|
2011-07-21 23:10:53 +04:00
|
|
|
void rdp_send_data_pdu(rdpRdp* rdp, STREAM* s, uint16 type, uint16 channel_id)
|
|
|
|
{
|
|
|
|
int length;
|
|
|
|
|
|
|
|
length = stream_get_length(s);
|
|
|
|
stream_set_pos(s, 0);
|
|
|
|
|
|
|
|
rdp_write_header(rdp, s, length);
|
2011-07-22 00:20:41 +04:00
|
|
|
rdp_write_share_control_header(s, length, PDU_TYPE_DATA, channel_id);
|
|
|
|
rdp_write_share_data_header(s, length, type, rdp->settings->share_id);
|
2011-07-21 23:10:53 +04:00
|
|
|
|
|
|
|
stream_set_pos(s, length);
|
|
|
|
transport_write(rdp->transport, s);
|
|
|
|
}
|
|
|
|
|
2011-07-22 00:20:41 +04:00
|
|
|
void rdp_read_set_error_info_data_pdu(STREAM* s)
|
|
|
|
{
|
|
|
|
uint32 errorInfo;
|
|
|
|
|
|
|
|
stream_read_uint32(s, errorInfo); /* errorInfo (4 bytes) */
|
|
|
|
|
|
|
|
printf("Error Info: 0x%08X\n", errorInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rdp_read_data_pdu(rdpRdp* rdp, STREAM* s)
|
|
|
|
{
|
|
|
|
uint8 type;
|
|
|
|
uint16 length;
|
|
|
|
uint32 share_id;
|
|
|
|
|
|
|
|
rdp_read_share_data_header(s, &length, &type, &share_id);
|
|
|
|
|
|
|
|
printf("data pdu type:%d length:%d\n", type, length);
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case DATA_PDU_TYPE_SET_ERROR_INFO:
|
|
|
|
rdp_read_set_error_info_data_pdu(s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-12 04:46:03 +04:00
|
|
|
/**
|
|
|
|
* Receive an RDP packet.\n
|
|
|
|
* @param rdp RDP module
|
|
|
|
*/
|
|
|
|
|
2011-07-12 02:46:36 +04:00
|
|
|
void rdp_recv(rdpRdp* rdp)
|
|
|
|
{
|
|
|
|
STREAM* s;
|
|
|
|
int length;
|
2011-07-20 00:30:05 +04:00
|
|
|
uint16 pduType;
|
|
|
|
uint16 pduLength;
|
2011-07-12 02:46:36 +04:00
|
|
|
uint16 initiator;
|
|
|
|
uint16 channelId;
|
|
|
|
uint16 sec_flags;
|
|
|
|
enum DomainMCSPDU MCSPDU;
|
|
|
|
|
|
|
|
s = transport_recv_stream_init(rdp->transport, 4096);
|
|
|
|
transport_read(rdp->transport, s);
|
|
|
|
|
|
|
|
MCSPDU = DomainMCSPDU_SendDataIndication;
|
|
|
|
mcs_read_domain_mcspdu_header(s, &MCSPDU, &length);
|
|
|
|
|
|
|
|
per_read_integer16(s, &initiator, MCS_BASE_CHANNEL_ID); /* initiator (UserId) */
|
|
|
|
per_read_integer16(s, &channelId, 0); /* channelId */
|
|
|
|
stream_seek(s, 1); /* dataPriority + Segmentation (0x70) */
|
|
|
|
per_read_length(s, &pduLength); /* userData (OCTET_STRING) */
|
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
if (rdp->connected != True)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2011-07-20 00:30:05 +04:00
|
|
|
rdp_read_security_header(s, &sec_flags);
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
if (sec_flags & SEC_PKT_MASK)
|
|
|
|
{
|
|
|
|
switch (sec_flags & SEC_PKT_MASK)
|
|
|
|
{
|
|
|
|
case SEC_LICENSE_PKT:
|
|
|
|
license_recv(rdp->license, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SEC_REDIRECTION_PKT:
|
|
|
|
rdp_read_redirection_packet(rdp, s);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("incorrect security flags: 0x%04X\n", sec_flags);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2011-07-21 23:10:53 +04:00
|
|
|
rdp_read_share_control_header(s, &pduLength, &pduType, &rdp->settings->pdu_source);
|
2011-07-20 00:30:05 +04:00
|
|
|
|
|
|
|
switch (pduType)
|
2011-07-12 02:46:36 +04:00
|
|
|
{
|
2011-07-22 00:20:41 +04:00
|
|
|
case PDU_TYPE_DATA:
|
|
|
|
rdp_read_data_pdu(rdp, s);
|
|
|
|
break;
|
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
case PDU_TYPE_DEMAND_ACTIVE:
|
|
|
|
rdp_read_demand_active(s, rdp->settings);
|
2011-07-21 21:57:57 +04:00
|
|
|
rdp_send_confirm_active(rdp);
|
2011-07-20 00:30:05 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PDU_TYPE_DEACTIVATE_ALL:
|
|
|
|
rdp_read_deactivate_all(s, rdp->settings);
|
2011-07-12 02:46:36 +04:00
|
|
|
break;
|
|
|
|
|
2011-07-20 00:30:05 +04:00
|
|
|
case PDU_TYPE_SERVER_REDIRECTION:
|
|
|
|
rdp_read_enhanced_security_redirection_packet(rdp, s);
|
2011-07-12 02:46:36 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-07-20 00:30:05 +04:00
|
|
|
printf("incorrect PDU type: 0x%04X\n", pduType);
|
2011-07-12 02:46:36 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate new RDP module.
|
|
|
|
* @return new RDP module
|
|
|
|
*/
|
|
|
|
|
|
|
|
rdpRdp* rdp_new()
|
|
|
|
{
|
|
|
|
rdpRdp* rdp;
|
|
|
|
|
|
|
|
rdp = (rdpRdp*) xzalloc(sizeof(rdpRdp));
|
|
|
|
|
|
|
|
if (rdp != NULL)
|
|
|
|
{
|
2011-07-20 00:30:05 +04:00
|
|
|
rdp->connected = False;
|
2011-07-12 02:46:36 +04:00
|
|
|
rdp->settings = settings_new();
|
2011-07-14 09:23:44 +04:00
|
|
|
rdp->registry = registry_new(rdp->settings);
|
2011-07-12 02:46:36 +04:00
|
|
|
rdp->transport = transport_new(rdp->settings);
|
2011-07-12 09:16:59 +04:00
|
|
|
rdp->license = license_new(rdp);
|
2011-07-12 02:46:36 +04:00
|
|
|
rdp->nego = nego_new(rdp->transport);
|
|
|
|
rdp->mcs = mcs_new(rdp->transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rdp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
if (rdp != NULL)
|
|
|
|
{
|
|
|
|
settings_free(rdp->settings);
|
2011-07-12 04:46:03 +04:00
|
|
|
transport_free(rdp->transport);
|
|
|
|
license_free(rdp->license);
|
|
|
|
mcs_free(rdp->mcs);
|
2011-07-12 02:46:36 +04:00
|
|
|
xfree(rdp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|