2011-08-18 12:06:32 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-18 12:06:32 +04:00
|
|
|
* RDP Server Peer
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
2015-01-13 19:09:36 +03:00
|
|
|
* Copyright 2014 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2011-08-18 12:06:32 +04:00
|
|
|
*
|
|
|
|
* 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>
|
2014-11-12 22:06:34 +03:00
|
|
|
#include <winpr/winsock.h>
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2013-07-19 01:15:10 +04:00
|
|
|
#include "info.h"
|
2012-02-17 09:58:30 +04:00
|
|
|
#include "certificate.h"
|
2012-11-22 04:22:41 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2017-05-31 12:31:36 +03:00
|
|
|
#include "rdp.h"
|
2011-08-18 12:06:32 +04:00
|
|
|
#include "peer.h"
|
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core.peer")
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
static HANDLE freerdp_peer_virtual_channel_open(freerdp_peer* client, const char* name,
|
|
|
|
UINT32 flags)
|
2014-10-11 00:11:42 +04:00
|
|
|
{
|
|
|
|
int length;
|
|
|
|
UINT32 index;
|
|
|
|
BOOL joined = FALSE;
|
|
|
|
rdpMcsChannel* mcsChannel = NULL;
|
|
|
|
rdpPeerChannel* peerChannel = NULL;
|
|
|
|
rdpMcs* mcs = client->context->rdp->mcs;
|
|
|
|
|
|
|
|
if (flags & WTS_CHANNEL_OPTION_DYNAMIC)
|
|
|
|
return NULL; /* not yet supported */
|
|
|
|
|
|
|
|
length = strlen(name);
|
|
|
|
|
|
|
|
if (length > 8)
|
|
|
|
return NULL; /* SVC maximum name length is 8 */
|
|
|
|
|
|
|
|
for (index = 0; index < mcs->channelCount; index++)
|
|
|
|
{
|
|
|
|
mcsChannel = &(mcs->channels[index]);
|
|
|
|
|
|
|
|
if (!mcsChannel->joined)
|
|
|
|
continue;
|
|
|
|
|
2015-02-23 17:24:43 +03:00
|
|
|
if (_strnicmp(name, mcsChannel->Name, length) == 0)
|
2014-10-11 00:11:42 +04:00
|
|
|
{
|
|
|
|
joined = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!joined)
|
|
|
|
return NULL; /* channel is not joined */
|
|
|
|
|
|
|
|
peerChannel = (rdpPeerChannel*) mcsChannel->handle;
|
|
|
|
|
|
|
|
if (peerChannel)
|
|
|
|
{
|
|
|
|
/* channel is already open */
|
|
|
|
return (HANDLE) peerChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
peerChannel = (rdpPeerChannel*) calloc(1, sizeof(rdpPeerChannel));
|
|
|
|
|
|
|
|
if (peerChannel)
|
|
|
|
{
|
|
|
|
peerChannel->index = index;
|
|
|
|
peerChannel->client = client;
|
|
|
|
peerChannel->channelFlags = flags;
|
|
|
|
peerChannel->channelId = mcsChannel->ChannelId;
|
|
|
|
peerChannel->mcsChannel = mcsChannel;
|
|
|
|
mcsChannel->handle = (void*) peerChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (HANDLE) peerChannel;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL freerdp_peer_virtual_channel_close(freerdp_peer* client, HANDLE hChannel)
|
|
|
|
{
|
|
|
|
rdpMcsChannel* mcsChannel = NULL;
|
|
|
|
rdpPeerChannel* peerChannel = NULL;
|
|
|
|
|
|
|
|
if (!hChannel)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
peerChannel = (rdpPeerChannel*) hChannel;
|
|
|
|
mcsChannel = peerChannel->mcsChannel;
|
|
|
|
mcsChannel->handle = NULL;
|
|
|
|
free(peerChannel);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
int freerdp_peer_virtual_channel_read(freerdp_peer* client, HANDLE hChannel, BYTE* buffer,
|
|
|
|
UINT32 length)
|
2014-10-11 00:11:42 +04:00
|
|
|
{
|
2014-10-11 01:19:38 +04:00
|
|
|
return 0; /* this needs to be implemented by the server application */
|
2014-10-11 00:11:42 +04:00
|
|
|
}
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
static int freerdp_peer_virtual_channel_write(freerdp_peer* client, HANDLE hChannel, BYTE* buffer,
|
|
|
|
UINT32 length)
|
2014-10-11 00:11:42 +04:00
|
|
|
{
|
|
|
|
wStream* s;
|
|
|
|
UINT32 flags;
|
|
|
|
UINT32 chunkSize;
|
|
|
|
UINT32 maxChunkSize;
|
|
|
|
UINT32 totalLength;
|
2014-11-08 02:06:14 +03:00
|
|
|
rdpPeerChannel* peerChannel;
|
|
|
|
rdpMcsChannel* mcsChannel;
|
2014-10-11 00:11:42 +04:00
|
|
|
rdpRdp* rdp = client->context->rdp;
|
|
|
|
|
|
|
|
if (!hChannel)
|
|
|
|
return -1;
|
|
|
|
|
2014-11-08 02:06:14 +03:00
|
|
|
peerChannel = (rdpPeerChannel*) hChannel;
|
|
|
|
mcsChannel = peerChannel->mcsChannel;
|
|
|
|
|
2014-10-11 00:11:42 +04:00
|
|
|
if (peerChannel->channelFlags & WTS_CHANNEL_OPTION_DYNAMIC)
|
|
|
|
return -1; /* not yet supported */
|
|
|
|
|
|
|
|
maxChunkSize = rdp->settings->VirtualChannelChunkSize;
|
|
|
|
totalLength = length;
|
|
|
|
flags = CHANNEL_FLAG_FIRST;
|
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
s = rdp_send_stream_init(rdp);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-03-26 19:09:47 +03:00
|
|
|
if (!s)
|
|
|
|
return -1;
|
2014-10-11 00:11:42 +04:00
|
|
|
|
|
|
|
if (length > maxChunkSize)
|
|
|
|
{
|
|
|
|
chunkSize = rdp->settings->VirtualChannelChunkSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
chunkSize = length;
|
|
|
|
flags |= CHANNEL_FLAG_LAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mcsChannel->options & CHANNEL_OPTION_SHOW_PROTOCOL)
|
|
|
|
flags |= CHANNEL_FLAG_SHOW_PROTOCOL;
|
|
|
|
|
|
|
|
Stream_Write_UINT32(s, totalLength);
|
|
|
|
Stream_Write_UINT32(s, flags);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, chunkSize))
|
2015-03-26 19:09:47 +03:00
|
|
|
{
|
|
|
|
Stream_Release(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2014-10-11 00:11:42 +04:00
|
|
|
Stream_Write(s, buffer, chunkSize);
|
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!rdp_send(rdp, s, peerChannel->channelId))
|
2015-03-26 19:09:47 +03:00
|
|
|
{
|
|
|
|
Stream_Release(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-10-11 00:11:42 +04:00
|
|
|
|
|
|
|
buffer += chunkSize;
|
|
|
|
length -= chunkSize;
|
|
|
|
flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-11 01:19:38 +04:00
|
|
|
void* freerdp_peer_virtual_channel_get_data(freerdp_peer* client, HANDLE hChannel)
|
|
|
|
{
|
|
|
|
rdpPeerChannel* peerChannel = (rdpPeerChannel*) hChannel;
|
|
|
|
|
|
|
|
if (!hChannel)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return peerChannel->extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
int freerdp_peer_virtual_channel_set_data(freerdp_peer* client, HANDLE hChannel, void* data)
|
|
|
|
{
|
|
|
|
rdpPeerChannel* peerChannel = (rdpPeerChannel*) hChannel;
|
|
|
|
|
|
|
|
if (!hChannel)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
peerChannel->extra = data;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL freerdp_peer_initialize(freerdp_peer* client)
|
2011-08-18 12:06:32 +04:00
|
|
|
{
|
2014-07-18 05:15:22 +04:00
|
|
|
rdpRdp* rdp = client->context->rdp;
|
|
|
|
rdpSettings* settings = rdp->settings;
|
2014-03-26 02:13:08 +04:00
|
|
|
settings->ServerMode = TRUE;
|
|
|
|
settings->FrameAcknowledge = 0;
|
|
|
|
settings->LocalConnection = client->local;
|
|
|
|
rdp->state = CONNECTION_STATE_INITIAL;
|
|
|
|
|
2014-07-18 05:15:22 +04:00
|
|
|
if (settings->RdpKeyFile)
|
2012-03-18 21:13:34 +04:00
|
|
|
{
|
2014-03-26 02:13:08 +04:00
|
|
|
settings->RdpServerRsaKey = key_new(settings->RdpKeyFile);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2014-03-26 02:13:08 +04:00
|
|
|
if (!settings->RdpServerRsaKey)
|
|
|
|
{
|
2015-05-27 16:13:43 +03:00
|
|
|
WLog_ERR(TAG, "invalid RDP key file %s", settings->RdpKeyFile);
|
2014-03-26 02:13:08 +04:00
|
|
|
return FALSE;
|
2014-04-03 13:43:51 +04:00
|
|
|
}
|
2012-01-25 19:45:21 +04:00
|
|
|
}
|
2016-01-21 02:17:59 +03:00
|
|
|
else if (settings->RdpKeyContent)
|
|
|
|
{
|
|
|
|
settings->RdpServerRsaKey = key_new_from_content(settings->RdpKeyContent, NULL);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2016-01-21 02:17:59 +03:00
|
|
|
if (!settings->RdpServerRsaKey)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid RDP key content");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL freerdp_peer_get_fds(freerdp_peer* client, void** rfds, int* rcount)
|
2011-08-18 12:06:32 +04:00
|
|
|
{
|
2015-02-14 00:51:08 +03:00
|
|
|
rdpTransport* transport = client->context->rdp->transport;
|
2016-10-27 18:43:09 +03:00
|
|
|
transport_get_fds(transport, rfds, rcount);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|
|
|
|
|
2016-10-28 19:28:10 +03:00
|
|
|
static HANDLE freerdp_peer_get_event_handle(freerdp_peer* client)
|
|
|
|
{
|
|
|
|
HANDLE hEvent = NULL;
|
|
|
|
rdpTransport* transport = client->context->rdp->transport;
|
|
|
|
BIO_get_event(transport->frontBio, &hEvent);
|
|
|
|
return hEvent;
|
|
|
|
}
|
|
|
|
|
2016-10-27 18:43:09 +03:00
|
|
|
static DWORD freerdp_peer_get_event_handles(freerdp_peer* client, HANDLE* events, DWORD count)
|
2013-07-21 06:34:05 +04:00
|
|
|
{
|
2016-10-29 08:18:17 +03:00
|
|
|
return transport_get_event_handles(client->context->rdp->transport, events, count);
|
2013-07-21 06:34:05 +04:00
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
static BOOL freerdp_peer_check_fds(freerdp_peer* peer)
|
2011-08-18 12:06:32 +04:00
|
|
|
{
|
2011-08-18 19:15:28 +04:00
|
|
|
int status;
|
2012-03-18 21:13:34 +04:00
|
|
|
rdpRdp* rdp;
|
2014-05-21 19:32:14 +04:00
|
|
|
rdp = peer->context->rdp;
|
2011-08-18 19:15:28 +04:00
|
|
|
status = rdp_check_fds(rdp);
|
2012-03-18 21:13:34 +04:00
|
|
|
|
2011-08-18 19:15:28 +04:00
|
|
|
if (status < 0)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static BOOL peer_recv_data_pdu(freerdp_peer* client, wStream* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE type;
|
|
|
|
UINT16 length;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 share_id;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE compressed_type;
|
|
|
|
UINT16 compressed_len;
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2011-09-08 09:17:58 +04:00
|
|
|
if (!rdp_read_share_data_header(s, &length, &type, &share_id, &compressed_type, &compressed_len))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2013-09-06 02:53:55 +04:00
|
|
|
#ifdef WITH_DEBUG_RDP
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_DBG(TAG, "recv %s Data PDU (0x%02"PRIX8"), length: %"PRIu16"",
|
2016-10-13 23:02:25 +03:00
|
|
|
type < ARRAYSIZE(DATA_PDU_TYPE_STRINGS) ? DATA_PDU_TYPE_STRINGS[type] : "???", type, length);
|
2013-09-06 02:53:55 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case DATA_PDU_TYPE_SYNCHRONIZE:
|
2012-01-09 01:02:59 +04:00
|
|
|
if (!rdp_recv_client_synchronize_pdu(client->context->rdp, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_CONTROL:
|
2011-10-18 11:10:12 +04:00
|
|
|
if (!rdp_server_accept_client_control_pdu(client->context->rdp, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-21 19:20:37 +04:00
|
|
|
break;
|
|
|
|
|
2012-02-07 16:38:27 +04:00
|
|
|
case DATA_PDU_TYPE_INPUT:
|
|
|
|
if (!input_recv(client->context->rdp->input, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2012-02-07 16:38:27 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-21 19:20:37 +04:00
|
|
|
case DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST:
|
|
|
|
/* TODO: notify server bitmap cache data */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_FONT_LIST:
|
2011-10-18 11:10:12 +04:00
|
|
|
if (!rdp_server_accept_client_font_list_pdu(client->context->rdp, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-03-18 21:13:34 +04:00
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-26 14:14:34 +04:00
|
|
|
case DATA_PDU_TYPE_SHUTDOWN_REQUEST:
|
2011-10-18 11:10:12 +04:00
|
|
|
mcs_send_disconnect_provider_ultimatum(client->context->rdp->mcs);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-26 14:14:34 +04:00
|
|
|
|
2012-05-11 12:35:11 +04:00
|
|
|
case DATA_PDU_TYPE_FRAME_ACKNOWLEDGE:
|
2013-07-20 02:24:56 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 4)
|
2013-01-15 02:40:34 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32(s, client->ack_frame_id);
|
2013-06-02 00:57:34 +04:00
|
|
|
IFCALL(client->update->SurfaceFrameAcknowledge, client->update->context, client->ack_frame_id);
|
2012-05-11 12:35:11 +04:00
|
|
|
break;
|
|
|
|
|
2012-05-26 17:34:09 +04:00
|
|
|
case DATA_PDU_TYPE_REFRESH_RECT:
|
|
|
|
if (!update_read_refresh_rect(client->update, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2012-05-26 17:34:09 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DATA_PDU_TYPE_SUPPRESS_OUTPUT:
|
|
|
|
if (!update_read_suppress_output(client->update, s))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2012-05-26 17:34:09 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
default:
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "Data PDU type %"PRIu8"", type);
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int peer_recv_tpkt_pdu(freerdp_peer* client, wStream* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2012-03-18 21:13:34 +04:00
|
|
|
rdpRdp* rdp;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
|
|
|
UINT16 pduType;
|
|
|
|
UINT16 pduLength;
|
|
|
|
UINT16 pduSource;
|
|
|
|
UINT16 channelId;
|
2016-12-02 12:46:43 +03:00
|
|
|
UINT16 securityFlags = 0;
|
2012-01-25 19:30:54 +04:00
|
|
|
rdp = client->context->rdp;
|
|
|
|
|
|
|
|
if (!rdp_read_header(rdp, s, &length, &channelId))
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2015-03-19 02:41:29 +03:00
|
|
|
WLog_ERR(TAG, "Incorrect RDP header.");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2015-09-05 15:57:30 +03:00
|
|
|
if (freerdp_shall_disconnect(rdp->instance))
|
2014-08-20 13:54:05 +04:00
|
|
|
return 0;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
Standard RDP Security Layer Levels/Method Overhaul
[MS-RDPBCGR] Section 5.3 describes the encryption level and method values for
standard RDP security.
Looking at the current usage of these values in the FreeRDP code gives me
reason to believe that there is a certain lack of understanding of how these
values should be handled.
The encryption level is only configured on the server side in the "Encryption
Level" setting found in the Remote Desktop Session Host Configuration RDP-Tcp
properties dialog and this value is never transferred from the client to the
server over the wire.
The possible options are "None", "Low", "Client Compatible", "High" and
"FIPS Compliant". The client receices this value in the Server Security Data
block (TS_UD_SC_SEC1), probably only for informational purposes and maybe to
give the client the possibility to verify if the server's decision for the
encryption method confirms to the server's encryption level.
The possible encryption methods are "NONE", "40BIT", "56BIT", "128BIT" and
"FIPS" and the RDP client advertises the ones it supports to the server in the
Client Security Data block (TS_UD_CS_SEC).
The server's configured encryption level value restricts the possible final
encryption method.
Something that I was not able to find in the documentation is the priority
level of the individual encryption methods based on which the server makes its
final method decision if there are several options.
My analysis with Windows Servers reveiled that the order is 128, 56, 40, FIPS.
The server only chooses FIPS if the level is "FIPS Comliant" or if it is the
only method advertised by the client.
Bottom line:
* FreeRDP's client side does not need to set settings->EncryptionLevel
(which was done quite frequently).
* FreeRDP's server side does not have to set the supported encryption methods
list in settings->EncryptionMethods
Changes in this commit:
Removed unnecessary/confusing changes of EncryptionLevel/Methods settings
Refactor settings->DisableEncryption
* This value actually means "Advanced RDP Encryption (NLA/TLS) is NOT used"
* The old name caused lots of confusion among developers
* Renamed it to "UseRdpSecurityLayer" (the compare logic stays untouched)
Any client's setting of settings->EncryptionMethods were annihilated
* All clients "want" to set all supported methods
* Some clients forgot 56bit because 56bit was not supported at the time the
code was written
* settings->EncryptionMethods was overwritten anyways in nego_connect()
* Removed all client side settings of settings->EncryptionMethods
The default is "None" (0)
* Changed nego_connect() to advertise all supported methods if
settings->EncryptionMethods is 0 (None)
* Added a commandline option /encryption-methods:comma separated list of the
values "40", "56", "128", "FIPS". E.g. /encryption-methods:56,128
* Print warning if server chooses non-advertised method
Verify received level and method in client's gcc_read_server_security_data
* Only accept valid/known encryption methods
* Verify encryption level/method combinations according to MS-RDPBCGR 5.3.2
Server implementations can now set settings->EncryptionLevel
* The default for settings->EncryptionLevel is 0 (None)
* nego_send_negotiation_response() changes it to ClientCompatible in that case
* default to ClientCompatible if the server implementation set an invalid level
Fix server's gcc_write_server_security_data
* Verify server encryption level value set by server implementations
* Choose rdp encryption method based on level and supported client methods
* Moved FIPS to the lowest priority (only used if other methods are possible)
Updated sample server
* Support RDP Security (RdpKeyFile was not set)
* Added commented sample code for setting the security level
2014-12-12 04:17:12 +03:00
|
|
|
if (rdp->settings->UseRdpSecurityLayer)
|
2012-01-25 19:30:54 +04:00
|
|
|
{
|
2017-05-29 11:50:22 +03:00
|
|
|
if (!rdp_read_security_header(s, &securityFlags, &length))
|
2013-01-15 02:40:34 +04:00
|
|
|
return -1;
|
2012-03-18 21:13:34 +04:00
|
|
|
|
2012-01-25 19:30:54 +04:00
|
|
|
if (securityFlags & SEC_ENCRYPT)
|
|
|
|
{
|
2017-05-29 11:50:22 +03:00
|
|
|
if (!rdp_decrypt(rdp, s, length, securityFlags))
|
2012-01-25 19:30:54 +04:00
|
|
|
{
|
2015-03-19 02:41:29 +03:00
|
|
|
WLog_ERR(TAG, "rdp_decrypt failed");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2012-01-25 19:30:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-28 11:06:04 +03:00
|
|
|
if (channelId == MCS_GLOBAL_CHANNEL_ID)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2011-11-19 11:33:49 +04:00
|
|
|
if (!rdp_read_share_control_header(s, &pduLength, &pduType, &pduSource))
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2012-11-08 03:23:25 +04:00
|
|
|
client->settings->PduSource = pduSource;
|
2011-11-19 11:33:49 +04:00
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
switch (pduType)
|
|
|
|
{
|
|
|
|
case PDU_TYPE_DATA:
|
2011-10-18 11:10:12 +04:00
|
|
|
if (!peer_recv_data_pdu(client, s))
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case PDU_TYPE_CONFIRM_ACTIVE:
|
|
|
|
if (!rdp_server_accept_confirm_active(rdp, s))
|
|
|
|
return -1;
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
break;
|
|
|
|
|
2014-11-05 22:32:46 +03:00
|
|
|
case PDU_TYPE_FLOW_RESPONSE:
|
|
|
|
case PDU_TYPE_FLOW_STOP:
|
|
|
|
case PDU_TYPE_FLOW_TEST:
|
|
|
|
break;
|
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
default:
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "Client sent pduType %"PRIu16"", pduType);
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-28 11:06:04 +03:00
|
|
|
else if (rdp->mcs->messageChannelId && channelId == rdp->mcs->messageChannelId)
|
|
|
|
{
|
2015-06-19 15:47:00 +03:00
|
|
|
if (!rdp->settings->UseRdpSecurityLayer)
|
2017-05-29 11:50:22 +03:00
|
|
|
if (!rdp_read_security_header(s, &securityFlags, NULL))
|
2015-06-19 15:47:00 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return rdp_recv_message_channel_pdu(rdp, s, securityFlags);
|
2014-10-28 11:06:04 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!freerdp_channel_peer_process(client, s, channelId))
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-21 18:52:37 +04:00
|
|
|
|
2013-01-07 02:24:08 +04:00
|
|
|
return 0;
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int peer_recv_fastpath_pdu(freerdp_peer* client, wStream* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
2011-09-27 09:30:58 +04:00
|
|
|
rdpRdp* rdp;
|
2012-10-09 11:01:37 +04:00
|
|
|
UINT16 length;
|
2011-09-27 09:30:58 +04:00
|
|
|
rdpFastPath* fastpath;
|
2011-10-18 11:10:12 +04:00
|
|
|
rdp = client->context->rdp;
|
2011-09-27 09:30:58 +04:00
|
|
|
fastpath = rdp->fastpath;
|
2013-01-15 01:10:05 +04:00
|
|
|
fastpath_read_header_rdp(fastpath, s, &length);
|
2011-08-23 11:51:51 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if ((length == 0) || (length > Stream_GetRemainingLength(s)))
|
2011-08-23 11:51:51 +04:00
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
WLog_ERR(TAG, "incorrect FastPath PDU header length %"PRIu16"", length);
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2011-08-23 11:51:51 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
if (fastpath->encryptionFlags & FASTPATH_OUTPUT_ENCRYPTED)
|
2011-09-15 01:14:50 +04:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
if (!rdp_decrypt(rdp, s, length,
|
|
|
|
(fastpath->encryptionFlags & FASTPATH_OUTPUT_SECURE_CHECKSUM) ? SEC_SECURE_CHECKSUM : 0))
|
2013-01-12 17:49:01 +04:00
|
|
|
return -1;
|
2011-09-15 01:14:50 +04:00
|
|
|
}
|
|
|
|
|
2011-09-27 09:30:58 +04:00
|
|
|
return fastpath_recv_inputs(fastpath, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int peer_recv_pdu(freerdp_peer* client, wStream* s)
|
2011-08-21 18:52:37 +04:00
|
|
|
{
|
|
|
|
if (tpkt_verify_header(s))
|
2011-10-18 11:10:12 +04:00
|
|
|
return peer_recv_tpkt_pdu(client, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
else
|
2011-10-18 11:10:12 +04:00
|
|
|
return peer_recv_fastpath_pdu(client, s);
|
2011-08-21 18:52:37 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
static int peer_recv_callback(rdpTransport* transport, wStream* s, void* extra)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
2011-10-18 11:10:12 +04:00
|
|
|
freerdp_peer* client = (freerdp_peer*) extra;
|
2012-09-16 23:30:11 +04:00
|
|
|
rdpRdp* rdp = client->context->rdp;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
2012-09-16 23:30:11 +04:00
|
|
|
switch (rdp->state)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
|
|
|
case CONNECTION_STATE_INITIAL:
|
2012-09-16 23:30:11 +04:00
|
|
|
if (!rdp_server_accept_nego(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2015-06-23 12:08:44 +03:00
|
|
|
WLog_ERR(TAG, "peer_recv_callback: CONNECTION_STATE_INITIAL - rdp_server_accept_nego() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2012-09-16 23:30:11 +04:00
|
|
|
|
2016-07-22 00:53:20 +03:00
|
|
|
client->settings->NlaSecurity = (rdp->nego->SelectedProtocol & PROTOCOL_NLA) ? TRUE : FALSE;
|
|
|
|
client->settings->TlsSecurity = (rdp->nego->SelectedProtocol & PROTOCOL_TLS) ? TRUE : FALSE;
|
2018-04-09 18:13:22 +03:00
|
|
|
client->settings->RdpSecurity = (rdp->nego->SelectedProtocol == PROTOCOL_RDP) ? TRUE : FALSE;
|
2016-07-22 00:53:20 +03:00
|
|
|
|
2015-02-03 01:16:32 +03:00
|
|
|
if (rdp->nego->SelectedProtocol & PROTOCOL_NLA)
|
2012-09-16 23:30:11 +04:00
|
|
|
{
|
2015-06-15 16:03:13 +03:00
|
|
|
sspi_CopyAuthIdentity(&client->identity, rdp->nego->transport->nla->identity);
|
2012-10-09 10:31:28 +04:00
|
|
|
IFCALLRET(client->Logon, client->authenticated, client, &client->identity, TRUE);
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_free(rdp->nego->transport->nla);
|
|
|
|
rdp->nego->transport->nla = NULL;
|
2012-09-16 23:30:11 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
IFCALLRET(client->Logon, client->authenticated, client, &client->identity, FALSE);
|
2012-09-16 23:30:11 +04:00
|
|
|
}
|
|
|
|
|
2011-08-19 13:39:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_NEGO:
|
2012-09-16 23:30:11 +04:00
|
|
|
if (!rdp_server_accept_mcs_connect_initial(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_NEGO - rdp_server_accept_mcs_connect_initial() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-19 22:03:48 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_CONNECT:
|
2012-09-16 23:30:11 +04:00
|
|
|
if (!rdp_server_accept_mcs_erect_domain_request(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_MCS_CONNECT - rdp_server_accept_mcs_erect_domain_request() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-19 22:03:48 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_ERECT_DOMAIN:
|
2012-09-16 23:30:11 +04:00
|
|
|
if (!rdp_server_accept_mcs_attach_user_request(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_MCS_ERECT_DOMAIN - rdp_server_accept_mcs_attach_user_request() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-20 10:05:03 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_MCS_ATTACH_USER:
|
2012-09-16 23:30:11 +04:00
|
|
|
if (!rdp_server_accept_mcs_channel_join_request(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_MCS_ATTACH_USER - rdp_server_accept_mcs_channel_join_request() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-20 14:22:14 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
|
Standard RDP Security Layer Levels/Method Overhaul
[MS-RDPBCGR] Section 5.3 describes the encryption level and method values for
standard RDP security.
Looking at the current usage of these values in the FreeRDP code gives me
reason to believe that there is a certain lack of understanding of how these
values should be handled.
The encryption level is only configured on the server side in the "Encryption
Level" setting found in the Remote Desktop Session Host Configuration RDP-Tcp
properties dialog and this value is never transferred from the client to the
server over the wire.
The possible options are "None", "Low", "Client Compatible", "High" and
"FIPS Compliant". The client receices this value in the Server Security Data
block (TS_UD_SC_SEC1), probably only for informational purposes and maybe to
give the client the possibility to verify if the server's decision for the
encryption method confirms to the server's encryption level.
The possible encryption methods are "NONE", "40BIT", "56BIT", "128BIT" and
"FIPS" and the RDP client advertises the ones it supports to the server in the
Client Security Data block (TS_UD_CS_SEC).
The server's configured encryption level value restricts the possible final
encryption method.
Something that I was not able to find in the documentation is the priority
level of the individual encryption methods based on which the server makes its
final method decision if there are several options.
My analysis with Windows Servers reveiled that the order is 128, 56, 40, FIPS.
The server only chooses FIPS if the level is "FIPS Comliant" or if it is the
only method advertised by the client.
Bottom line:
* FreeRDP's client side does not need to set settings->EncryptionLevel
(which was done quite frequently).
* FreeRDP's server side does not have to set the supported encryption methods
list in settings->EncryptionMethods
Changes in this commit:
Removed unnecessary/confusing changes of EncryptionLevel/Methods settings
Refactor settings->DisableEncryption
* This value actually means "Advanced RDP Encryption (NLA/TLS) is NOT used"
* The old name caused lots of confusion among developers
* Renamed it to "UseRdpSecurityLayer" (the compare logic stays untouched)
Any client's setting of settings->EncryptionMethods were annihilated
* All clients "want" to set all supported methods
* Some clients forgot 56bit because 56bit was not supported at the time the
code was written
* settings->EncryptionMethods was overwritten anyways in nego_connect()
* Removed all client side settings of settings->EncryptionMethods
The default is "None" (0)
* Changed nego_connect() to advertise all supported methods if
settings->EncryptionMethods is 0 (None)
* Added a commandline option /encryption-methods:comma separated list of the
values "40", "56", "128", "FIPS". E.g. /encryption-methods:56,128
* Print warning if server chooses non-advertised method
Verify received level and method in client's gcc_read_server_security_data
* Only accept valid/known encryption methods
* Verify encryption level/method combinations according to MS-RDPBCGR 5.3.2
Server implementations can now set settings->EncryptionLevel
* The default for settings->EncryptionLevel is 0 (None)
* nego_send_negotiation_response() changes it to ClientCompatible in that case
* default to ClientCompatible if the server implementation set an invalid level
Fix server's gcc_write_server_security_data
* Verify server encryption level value set by server implementations
* Choose rdp encryption method based on level and supported client methods
* Moved FIPS to the lowest priority (only used if other methods are possible)
Updated sample server
* Support RDP Security (RdpKeyFile was not set)
* Added commented sample code for setting the security level
2014-12-12 04:17:12 +03:00
|
|
|
if (rdp->settings->UseRdpSecurityLayer)
|
2012-09-16 23:30:11 +04:00
|
|
|
{
|
2013-07-19 01:15:10 +04:00
|
|
|
if (!rdp_server_establish_keys(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT - rdp_server_establish_keys() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2012-01-25 20:08:10 +04:00
|
|
|
}
|
|
|
|
|
2013-07-19 01:15:10 +04:00
|
|
|
rdp_server_transition_to_state(rdp, CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2014-03-26 02:13:08 +04:00
|
|
|
if (Stream_GetRemainingLength(s) > 0)
|
|
|
|
return peer_recv_callback(transport, s, extra);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
break;
|
2012-03-18 21:13:34 +04:00
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
|
2013-07-19 01:15:10 +04:00
|
|
|
if (!rdp_recv_client_info(rdp, s))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE - rdp_recv_client_info() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2013-07-18 01:46:58 +04:00
|
|
|
|
2013-07-19 01:15:10 +04:00
|
|
|
rdp_server_transition_to_state(rdp, CONNECTION_STATE_LICENSING);
|
2013-07-18 23:18:59 +04:00
|
|
|
return peer_recv_callback(transport, NULL, extra);
|
2011-08-21 11:52:44 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_LICENSING:
|
|
|
|
if (!license_send_valid_client_error_packet(rdp->license))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_LICENSING - license_send_valid_client_error_packet() fail");
|
2017-01-19 19:57:44 +03:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2013-07-18 23:18:59 +04:00
|
|
|
|
2013-07-19 01:15:10 +04:00
|
|
|
rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE);
|
2013-07-18 23:18:59 +04:00
|
|
|
return peer_recv_callback(transport, NULL, extra);
|
2013-07-18 01:46:58 +04:00
|
|
|
break;
|
|
|
|
|
2013-07-18 23:18:59 +04:00
|
|
|
case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
|
2013-07-19 01:15:10 +04:00
|
|
|
if (!rdp->AwaitCapabilities)
|
2013-07-18 23:18:59 +04:00
|
|
|
{
|
2017-01-19 19:57:44 +03:00
|
|
|
if (client->Capabilities && !client->Capabilities(client))
|
|
|
|
return -1;
|
2013-07-18 23:18:59 +04:00
|
|
|
|
|
|
|
if (!rdp_send_demand_active(rdp))
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2016-10-13 23:02:25 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"peer_recv_callback: CONNECTION_STATE_CAPABILITIES_EXCHANGE - rdp_send_demand_active() fail");
|
2013-07-18 23:18:59 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2013-07-19 01:15:10 +04:00
|
|
|
|
|
|
|
rdp->AwaitCapabilities = TRUE;
|
|
|
|
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
if (peer_recv_pdu(client, s) < 0)
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2015-06-23 12:08:44 +03:00
|
|
|
WLog_ERR(TAG, "peer_recv_callback: CONNECTION_STATE_CAPABILITIES_EXCHANGE - peer_recv_pdu() fail");
|
2013-07-19 01:15:10 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2013-07-19 01:15:10 +04:00
|
|
|
}
|
2013-07-18 23:18:59 +04:00
|
|
|
}
|
|
|
|
else
|
2011-12-18 21:13:09 +04:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* During reactivation sequence the client might sent some input or channel data
|
|
|
|
* before receiving the Deactivate All PDU. We need to process them as usual.
|
|
|
|
*/
|
2013-07-20 02:24:56 +04:00
|
|
|
if (peer_recv_pdu(client, s) < 0)
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2015-06-23 12:08:44 +03:00
|
|
|
WLog_ERR(TAG, "peer_recv_callback: CONNECTION_STATE_CAPABILITIES_EXCHANGE - peer_recv_pdu() fail");
|
2013-07-20 02:24:56 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2011-12-18 21:13:09 +04:00
|
|
|
}
|
2013-07-18 23:18:59 +04:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_FINALIZATION:
|
|
|
|
if (peer_recv_pdu(client, s) < 0)
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2015-06-23 12:08:44 +03:00
|
|
|
WLog_ERR(TAG, "peer_recv_callback: CONNECTION_STATE_FINALIZATION - peer_recv_pdu() fail");
|
2013-07-18 23:18:59 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-21 18:52:37 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONNECTION_STATE_ACTIVE:
|
2013-01-15 01:10:05 +04:00
|
|
|
if (peer_recv_pdu(client, s) < 0)
|
2015-05-02 06:26:08 +03:00
|
|
|
{
|
2015-06-23 12:08:44 +03:00
|
|
|
WLog_ERR(TAG, "peer_recv_callback: CONNECTION_STATE_ACTIVE - peer_recv_pdu() fail");
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2015-03-17 05:10:58 +03:00
|
|
|
}
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2011-08-19 05:54:43 +04:00
|
|
|
break;
|
2011-08-18 19:15:28 +04:00
|
|
|
|
|
|
|
default:
|
2015-03-19 02:41:29 +03:00
|
|
|
WLog_ERR(TAG, "Invalid state %d", rdp->state);
|
2013-01-07 02:24:08 +04:00
|
|
|
return -1;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2013-01-07 02:24:08 +04:00
|
|
|
return 0;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL freerdp_peer_close(freerdp_peer* client)
|
2012-04-13 11:58:28 +04:00
|
|
|
{
|
2015-02-13 20:25:50 +03:00
|
|
|
/** if negotiation has failed, we're not MCS connected. So don't
|
|
|
|
* send anything else, or some mstsc will consider that as an error
|
|
|
|
*/
|
|
|
|
if (client->context->rdp->nego->SelectedProtocol & PROTOCOL_FAILED_NEGO)
|
|
|
|
return TRUE;
|
|
|
|
|
2012-04-13 11:58:28 +04:00
|
|
|
/**
|
|
|
|
* [MS-RDPBCGR] 1.3.1.4.2 User-Initiated Disconnection Sequence on Server
|
|
|
|
* The server first sends the client a Deactivate All PDU followed by an
|
|
|
|
* optional MCS Disconnect Provider Ultimatum PDU.
|
|
|
|
*/
|
|
|
|
if (!rdp_send_deactivate_all(client->context->rdp))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2013-07-18 23:18:59 +04:00
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
if (freerdp_get_param_bool(client->settings, FreeRDP_SupportErrorInfoPdu))
|
|
|
|
{
|
2015-01-13 19:09:36 +03:00
|
|
|
rdp_send_error_info(client->context->rdp);
|
|
|
|
}
|
|
|
|
|
2012-04-13 11:58:28 +04:00
|
|
|
return mcs_send_disconnect_provider_ultimatum(client->context->rdp->mcs);
|
|
|
|
}
|
|
|
|
|
2011-08-18 12:06:32 +04:00
|
|
|
static void freerdp_peer_disconnect(freerdp_peer* client)
|
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
rdpTransport* transport = client->context->rdp->transport;
|
|
|
|
transport_disconnect(transport);
|
2011-10-18 11:10:12 +04:00
|
|
|
}
|
|
|
|
|
2018-07-18 16:16:50 +03:00
|
|
|
static int freerdp_peer_send_channel_data(freerdp_peer* client, UINT16 channelId, const BYTE* data,
|
2016-10-13 23:02:25 +03:00
|
|
|
int size)
|
2011-12-10 12:41:29 +04:00
|
|
|
{
|
|
|
|
return rdp_send_channel_data(client->context->rdp, channelId, data, size);
|
|
|
|
}
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
static BOOL freerdp_peer_is_write_blocked(freerdp_peer* peer)
|
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
rdpTransport* transport = peer->context->rdp->transport;
|
2015-02-17 18:54:39 +03:00
|
|
|
return transport_is_write_blocked(transport);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int freerdp_peer_drain_output_buffer(freerdp_peer* peer)
|
|
|
|
{
|
2014-10-11 00:11:42 +04:00
|
|
|
rdpTransport* transport = peer->context->rdp->transport;
|
2015-02-17 18:54:39 +03:00
|
|
|
return transport_drain_output_buffer(transport);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2016-10-13 23:02:25 +03:00
|
|
|
static BOOL freerdp_peer_has_more_to_read(freerdp_peer* peer)
|
|
|
|
{
|
2016-05-03 18:24:07 +03:00
|
|
|
return peer->context->rdp->transport->haveMoreBytesToRead;
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
BOOL freerdp_peer_context_new(freerdp_peer* client)
|
2011-10-18 11:10:12 +04:00
|
|
|
{
|
|
|
|
rdpRdp* rdp;
|
2015-02-11 19:57:02 +03:00
|
|
|
rdpContext* context;
|
2015-04-28 18:00:41 +03:00
|
|
|
BOOL ret = TRUE;
|
2011-10-18 11:10:12 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!client)
|
|
|
|
return FALSE;
|
2013-07-20 05:52:28 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(context = (rdpContext*) calloc(1, client->ContextSize)))
|
|
|
|
goto fail_context;
|
|
|
|
|
|
|
|
client->context = context;
|
2015-05-21 17:54:19 +03:00
|
|
|
context->peer = client;
|
2015-02-11 19:57:02 +03:00
|
|
|
context->ServerMode = TRUE;
|
2016-07-22 00:53:20 +03:00
|
|
|
context->settings = client->settings;
|
2014-05-26 20:30:58 +04:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(context->metrics = metrics_new(context)))
|
|
|
|
goto fail_metrics;
|
2015-02-11 19:57:02 +03:00
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!(rdp = rdp_new(context)))
|
|
|
|
goto fail_rdp;
|
2015-03-23 19:25:23 +03:00
|
|
|
|
2011-10-18 11:10:12 +04:00
|
|
|
client->input = rdp->input;
|
|
|
|
client->update = rdp->update;
|
|
|
|
client->settings = rdp->settings;
|
2014-10-28 18:29:40 +03:00
|
|
|
client->autodetect = rdp->autodetect;
|
2015-02-11 19:57:02 +03:00
|
|
|
context->rdp = rdp;
|
|
|
|
context->input = client->input;
|
|
|
|
context->update = client->update;
|
|
|
|
context->settings = client->settings;
|
|
|
|
context->autodetect = client->autodetect;
|
|
|
|
client->update->context = context;
|
|
|
|
client->input->context = context;
|
|
|
|
client->autodetect->context = context;
|
2011-10-18 11:10:12 +04:00
|
|
|
update_register_server_callbacks(client->update);
|
2014-10-28 18:29:40 +03:00
|
|
|
autodetect_register_server_callbacks(client->autodetect);
|
2011-10-18 11:10:12 +04:00
|
|
|
|
2016-08-27 16:51:29 +03:00
|
|
|
if (!(context->errorDescription = calloc(1, 500)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
goto fail_error_description;
|
|
|
|
}
|
|
|
|
|
2015-04-28 18:00:41 +03:00
|
|
|
if (!transport_attach(rdp->transport, client->sockfd))
|
|
|
|
goto fail_transport_attach;
|
2011-08-23 12:14:32 +04:00
|
|
|
|
2012-12-22 00:49:02 +04:00
|
|
|
rdp->transport->ReceiveCallback = peer_recv_callback;
|
|
|
|
rdp->transport->ReceiveExtra = client;
|
2012-10-09 10:31:28 +04:00
|
|
|
transport_set_blocking_mode(rdp->transport, FALSE);
|
2014-05-21 19:32:14 +04:00
|
|
|
client->IsWriteBlocked = freerdp_peer_is_write_blocked;
|
|
|
|
client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
|
2016-05-03 18:24:07 +03:00
|
|
|
client->HasMoreToRead = freerdp_peer_has_more_to_read;
|
2015-04-28 18:00:41 +03:00
|
|
|
IFCALLRET(client->ContextNew, ret, client, client->context);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
WLog_ERR(TAG, "ContextNew callback failed");
|
|
|
|
fail_transport_attach:
|
2016-08-27 16:51:29 +03:00
|
|
|
free(context->errorDescription);
|
|
|
|
fail_error_description:
|
2015-04-28 18:00:41 +03:00
|
|
|
rdp_free(client->context->rdp);
|
|
|
|
fail_rdp:
|
|
|
|
metrics_free(context->metrics);
|
|
|
|
fail_metrics:
|
|
|
|
free(client->context);
|
|
|
|
fail_context:
|
|
|
|
client->context = NULL;
|
|
|
|
WLog_ERR(TAG, "Failed to create new peer context");
|
|
|
|
return FALSE;
|
2011-10-18 11:10:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_peer_context_free(freerdp_peer* client)
|
|
|
|
{
|
|
|
|
IFCALL(client->ContextFree, client, client->context);
|
2014-06-03 16:54:56 +04:00
|
|
|
|
2016-08-27 16:51:29 +03:00
|
|
|
if (client->context)
|
|
|
|
{
|
|
|
|
free(client->context->errorDescription);
|
|
|
|
client->context->errorDescription = NULL;
|
|
|
|
rdp_free(client->context->rdp);
|
|
|
|
client->context->rdp = NULL;
|
|
|
|
metrics_free(client->context->metrics);
|
|
|
|
client->context->metrics = NULL;
|
|
|
|
free(client->context);
|
|
|
|
client->context = NULL;
|
|
|
|
}
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
freerdp_peer* freerdp_peer_new(int sockfd)
|
|
|
|
{
|
2014-11-12 22:06:34 +03:00
|
|
|
UINT32 option_value;
|
|
|
|
socklen_t option_len;
|
2011-08-18 12:06:32 +04:00
|
|
|
freerdp_peer* client;
|
2014-05-26 20:30:58 +04:00
|
|
|
client = (freerdp_peer*) calloc(1, sizeof(freerdp_peer));
|
2011-08-18 12:06:32 +04:00
|
|
|
|
2014-11-12 22:06:34 +03:00
|
|
|
if (!client)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
option_value = TRUE;
|
|
|
|
option_len = sizeof(option_value);
|
|
|
|
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (void*) &option_value, option_len);
|
2012-03-18 21:13:34 +04:00
|
|
|
|
2013-07-21 06:34:05 +04:00
|
|
|
if (client)
|
2011-10-30 09:43:04 +04:00
|
|
|
{
|
|
|
|
client->sockfd = sockfd;
|
2013-06-14 05:34:46 +04:00
|
|
|
client->ContextSize = sizeof(rdpContext);
|
2011-10-30 09:43:04 +04:00
|
|
|
client->Initialize = freerdp_peer_initialize;
|
|
|
|
client->GetFileDescriptor = freerdp_peer_get_fds;
|
2016-10-28 19:28:10 +03:00
|
|
|
client->GetEventHandle = freerdp_peer_get_event_handle;
|
2016-10-27 18:43:09 +03:00
|
|
|
client->GetEventHandles = freerdp_peer_get_event_handles;
|
2011-10-30 09:43:04 +04:00
|
|
|
client->CheckFileDescriptor = freerdp_peer_check_fds;
|
2012-04-13 11:58:28 +04:00
|
|
|
client->Close = freerdp_peer_close;
|
2011-10-30 09:43:04 +04:00
|
|
|
client->Disconnect = freerdp_peer_disconnect;
|
2011-12-10 12:41:29 +04:00
|
|
|
client->SendChannelData = freerdp_peer_send_channel_data;
|
2014-05-21 19:32:14 +04:00
|
|
|
client->IsWriteBlocked = freerdp_peer_is_write_blocked;
|
|
|
|
client->DrainOutputBuffer = freerdp_peer_drain_output_buffer;
|
2016-05-03 18:24:07 +03:00
|
|
|
client->HasMoreToRead = freerdp_peer_has_more_to_read;
|
2014-10-11 00:11:42 +04:00
|
|
|
client->VirtualChannelOpen = freerdp_peer_virtual_channel_open;
|
|
|
|
client->VirtualChannelClose = freerdp_peer_virtual_channel_close;
|
|
|
|
client->VirtualChannelWrite = freerdp_peer_virtual_channel_write;
|
2014-10-11 01:19:38 +04:00
|
|
|
client->VirtualChannelRead = NULL; /* must be defined by server application */
|
|
|
|
client->VirtualChannelGetData = freerdp_peer_virtual_channel_get_data;
|
|
|
|
client->VirtualChannelSetData = freerdp_peer_virtual_channel_set_data;
|
2011-10-30 09:43:04 +04:00
|
|
|
}
|
2011-08-18 12:06:32 +04:00
|
|
|
|
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_peer_free(freerdp_peer* client)
|
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
free(client);
|
2011-08-18 12:06:32 +04:00
|
|
|
}
|