Blame libfreerdp/core/connection.c

Packit 1fb8d4
/*
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Connection Sequence
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include "info.h"
Packit 1fb8d4
#include "input.h"
Packit 1fb8d4
#include "rdp.h"
Packit 1fb8d4
Packit 1fb8d4
#include "connection.h"
Packit 1fb8d4
#include "transport.h"
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/crypto.h>
Packit 1fb8d4
#include <winpr/ssl.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/log.h>
Packit 1fb8d4
#include <freerdp/error.h>
Packit 1fb8d4
#include <freerdp/listener.h>
Packit 1fb8d4
#include <freerdp/cache/pointer.h>
Packit 1fb8d4
Packit 1fb8d4
#define TAG FREERDP_TAG("core.connection")
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 *                                      Connection Sequence
Packit 1fb8d4
 *     client                                                                    server
Packit 1fb8d4
 *        |                                                                         |
Packit 1fb8d4
 *        |-----------------------X.224 Connection Request PDU--------------------->|
Packit 1fb8d4
 *        |<----------------------X.224 Connection Confirm PDU----------------------|
Packit 1fb8d4
 *        |-------MCS Connect-Initial PDU with GCC Conference Create Request------->|
Packit 1fb8d4
 *        |<-----MCS Connect-Response PDU with GCC Conference Create Response-------|
Packit 1fb8d4
 *        |------------------------MCS Erect Domain Request PDU-------------------->|
Packit 1fb8d4
 *        |------------------------MCS Attach User Request PDU--------------------->|
Packit 1fb8d4
 *        |<-----------------------MCS Attach User Confirm PDU----------------------|
Packit 1fb8d4
 *        |------------------------MCS Channel Join Request PDU-------------------->|
Packit 1fb8d4
 *        |<-----------------------MCS Channel Join Confirm PDU---------------------|
Packit 1fb8d4
 *        |----------------------------Security Exchange PDU----------------------->|
Packit 1fb8d4
 *        |-------------------------------Client Info PDU-------------------------->|
Packit 1fb8d4
 *        |<---------------------License Error PDU - Valid Client-------------------|
Packit 1fb8d4
 *        |<-----------------------------Demand Active PDU--------------------------|
Packit 1fb8d4
 *        |------------------------------Confirm Active PDU------------------------>|
Packit 1fb8d4
 *        |-------------------------------Synchronize PDU-------------------------->|
Packit 1fb8d4
 *        |---------------------------Control PDU - Cooperate---------------------->|
Packit 1fb8d4
 *        |------------------------Control PDU - Request Control------------------->|
Packit 1fb8d4
 *        |--------------------------Persistent Key List PDU(s)-------------------->|
Packit 1fb8d4
 *        |--------------------------------Font List PDU--------------------------->|
Packit 1fb8d4
 *        |<------------------------------Synchronize PDU---------------------------|
Packit 1fb8d4
 *        |<--------------------------Control PDU - Cooperate-----------------------|
Packit 1fb8d4
 *        |<-----------------------Control PDU - Granted Control--------------------|
Packit 1fb8d4
 *        |<-------------------------------Font Map PDU-----------------------------|
Packit 1fb8d4
 *
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 *
Packit 1fb8d4
 * Connection Sequence
Packit 1fb8d4
 *
Packit 1fb8d4
 * 1.	Connection Initiation: The client initiates the connection by sending the server a
Packit 1fb8d4
 * 	Class 0 X.224 Connection Request PDU (section 2.2.1.1). The server responds with a
Packit 1fb8d4
 * 	Class 0 X.224 Connection Confirm PDU (section 2.2.1.2). From this point, all subsequent
Packit 1fb8d4
 * 	data sent between client and server is wrapped in an X.224 Data Protocol Data Unit (PDU).
Packit 1fb8d4
 *
Packit 1fb8d4
 * 2.	Basic Settings Exchange: Basic settings are exchanged between the client and server by
Packit Service 5a9772
 * 	using the MCS Connect Initial PDU (section 2.2.1.3) and MCS Connect Response PDU
Packit Service 5a9772
 *(section 2.2.1.4). The Connect Initial PDU contains a Generic Conference Control (GCC) Conference
Packit Service 5a9772
 *Create Request, while the Connect Response PDU contains a GCC Conference Create Response. These
Packit Service 5a9772
 *two GCC packets contain concatenated blocks of settings data (such as core data, security data,
Packit Service 5a9772
 *and network data) which are read by client and server.
Packit 1fb8d4
 *
Packit 1fb8d4
 * 3.	Channel Connection: The client sends an MCS Erect Domain Request PDU (section 2.2.1.5),
Packit 1fb8d4
 * 	followed by an MCS Attach User Request PDU (section 2.2.1.6) to attach the primary user identity
Packit 1fb8d4
 * 	to the MCS domain. The server responds with an MCS Attach User Confirm PDU (section 2.2.1.7)
Packit 1fb8d4
 * 	containing the User Channel ID. The client then proceeds to join the user channel, the
Packit 1fb8d4
 * 	input/output (I/O) channel, and all of the static virtual channels (the I/O and static virtual
Packit Service 5a9772
 * 	channel IDs are obtained from the data embedded in the GCC packets) by using multiple MCS
Packit Service 5a9772
 *Channel Join Request PDUs (section 2.2.1.8). The server confirms each channel with an MCS Channel
Packit Service 5a9772
 *Join Confirm PDU (section 2.2.1.9). (The client only sends a Channel Join Request after it has
Packit Service 5a9772
 *received the Channel Join Confirm for the previously sent request.)
Packit 1fb8d4
 *
Packit Service 5a9772
 * 	From this point, all subsequent data sent from the client to the server is wrapped in an MCS
Packit Service 5a9772
 *Send Data Request PDU, while data sent from the server to the client is wrapped in an MCS Send
Packit Service 5a9772
 *Data Indication PDU. This is in addition to the data being wrapped by an X.224 Data PDU.
Packit 1fb8d4
 *
Packit Service 5a9772
 * 4.	RDP Security Commencement: If Standard RDP Security mechanisms (section 5.3) are being
Packit Service 5a9772
 *employed and encryption is in force (this is determined by examining the data embedded in the GCC
Packit Service 5a9772
 *Conference Create Response packet) then the client sends a Security Exchange PDU
Packit Service 5a9772
 *(section 2.2.1.10) containing an encrypted 32-byte random number to the server. This random number
Packit Service 5a9772
 *is encrypted with the public key of the server as described in section 5.3.4.1 (the server's
Packit Service 5a9772
 *public key, as well as a 32-byte server-generated random number, are both obtained from the data
Packit Service 5a9772
 *embedded in the GCC Conference Create Response packet). The client and server then utilize the two
Packit Service 5a9772
 *32-byte random numbers to generate session keys which are used to encrypt and validate the
Packit Service 5a9772
 *integrity of subsequent RDP traffic.
Packit 1fb8d4
 *
Packit Service 5a9772
 * 	From this point, all subsequent RDP traffic can be encrypted and a security header is included
Packit Service 5a9772
 *with the data if encryption is in force. (The Client Info PDU (section 2.2.1.11) and licensing
Packit Service 5a9772
 *PDUs ([MS-RDPELE] section 2.2.2) are an exception in that they always have a security header). The
Packit Service 5a9772
 *Security Header follows the X.224 and MCS Headers and indicates whether the attached data is
Packit Service 5a9772
 *encrypted. Even if encryption is in force, server-to-client traffic may not always be encrypted,
Packit Service 5a9772
 *while client-to-server traffic must always be encrypted (encryption of licensing PDUs is optional,
Packit Service 5a9772
 *however).
Packit 1fb8d4
 *
Packit Service 5a9772
 * 5.	Secure Settings Exchange: Secure client data (such as the username, password, and
Packit Service 5a9772
 *auto-reconnect cookie) is sent to the server by using the Client Info PDU (section 2.2.1.11).
Packit 1fb8d4
 *
Packit Service 5a9772
 * 6.	Optional Connect-Time Auto-Detection: During the optional connect-time auto-detect phase the
Packit Service 5a9772
 *goal is to determine characteristics of the network, such as the round-trip latency time and the
Packit Service 5a9772
 *bandwidth of the link between the server and client. This is accomplished by exchanging a
Packit Service 5a9772
 *collection of PDUs (specified in section 2.2.1.4) over a predetermined period of time with enough
Packit Service 5a9772
 *data to ensure that the results are statistically relevant.
Packit 1fb8d4
 *
Packit Service 5a9772
 * 7.	Licensing: The goal of the licensing exchange is to transfer a license from the server to
Packit Service 5a9772
 *the client. The client stores this license and on subsequent connections sends the license to the
Packit Service 5a9772
 *server for validation. However, in some situations the client may not be issued a license to
Packit Service 5a9772
 *store. In effect, the packets exchanged during this phase of the protocol depend on the licensing
Packit Service 5a9772
 *mechanisms employed by the server. Within the context of this document, it is assumed that the
Packit Service 5a9772
 *client will not be issued a license to store. For details regarding more advanced licensing
Packit Service 5a9772
 *scenarios that take place during the Licensing Phase, see [MS-RDPELE] section 1.3.
Packit 1fb8d4
 *
Packit Service 5a9772
 * 8.	Optional Multitransport Bootstrapping: After the connection has been secured and the
Packit Service 5a9772
 *Licensing Phase has run to completion, the server can choose to initiate multitransport
Packit Service 5a9772
 *connections ([MS-RDPEMT] section 1.3). The Initiate Multitransport Request PDU (section 2.2.15.1)
Packit Service 5a9772
 *is sent by the server to the client and results in the out-of-band creation of a multitransport
Packit Service 5a9772
 *connection using messages from the RDP-UDP, TLS, DTLS, and multitransport protocols ([MS-RDPEMT]
Packit Service 5a9772
 *section 1.3.1).
Packit 1fb8d4
 *
Packit Service 5a9772
 * 9.	Capabilities Exchange: The server sends the set of capabilities it supports to the client in
Packit Service 5a9772
 *a Demand Active PDU (section 2.2.1.13.1). The client responds with its capabilities by sending a
Packit Service 5a9772
 *Confirm Active PDU (section 2.2.1.13.2).
Packit 1fb8d4
 *
Packit Service 5a9772
 * 10.	Connection Finalization: The client and server exchange PDUs to finalize the connection
Packit Service 5a9772
 *details. The client-to-server PDUs sent during this phase have no dependencies on any of the
Packit Service 5a9772
 *server-to-client PDUs; they may be sent as a single batch, provided that sequencing is maintained.
Packit 1fb8d4
 *
Packit Service 5a9772
 * 	- The Client Synchronize PDU (section 2.2.1.14) is sent after transmitting the Confirm Active
Packit Service 5a9772
 *PDU.
Packit Service 5a9772
 * 	- The Client Control (Cooperate) PDU (section 2.2.1.15) is sent after transmitting the Client
Packit Service 5a9772
 *Synchronize PDU.
Packit Service 5a9772
 * 	- The Client Control (Request Control) PDU (section 2.2.1.16) is sent after transmitting the
Packit Service 5a9772
 *Client Control (Cooperate) PDU.
Packit Service 5a9772
 * 	- The optional Persistent Key List PDUs (section 2.2.1.17) are sent after transmitting the
Packit Service 5a9772
 *Client Control (Request Control) PDU.
Packit Service 5a9772
 * 	- The Font List PDU (section 2.2.1.18) is sent after transmitting the Persistent Key List PDUs
Packit Service 5a9772
 *or, if the Persistent Key List PDUs were not sent, it is sent after transmitting the Client
Packit Service 5a9772
 *Control (Request Control) PDU (section 2.2.1.16).
Packit 1fb8d4
 *
Packit Service 5a9772
 *	The server-to-client PDUs sent during the Connection Finalization Phase have dependencies on the
Packit Service 5a9772
 *client-to-server PDUs.
Packit 1fb8d4
 *
Packit Service 5a9772
 *	- The optional Monitor Layout PDU (section 2.2.12.1) has no dependency on any client-to-server
Packit Service 5a9772
 *PDUs and is sent after the Demand Active PDU.
Packit 1fb8d4
 *	- The Server Synchronize PDU (section 2.2.1.19) is sent in response to the Confirm Active PDU.
Packit Service 5a9772
 *	- The Server Control (Cooperate) PDU (section 2.2.1.20) is sent after transmitting the Server
Packit Service 5a9772
 *Synchronize PDU.
Packit Service 5a9772
 *	- The Server Control (Granted Control) PDU (section 2.2.1.21) is sent in response to the Client
Packit Service 5a9772
 *Control (Request Control) PDU.
Packit 1fb8d4
 *	- The Font Map PDU (section 2.2.1.22) is sent in response to the Font List PDU.
Packit 1fb8d4
 *
Packit Service 5a9772
 *	Once the client has sent the Confirm Active PDU, it can start sending mouse and keyboard input
Packit Service 5a9772
 *to the server, and upon receipt of the Font List PDU the server can start sending graphics output
Packit Service 5a9772
 *to the client.
Packit 1fb8d4
 *
Packit Service 5a9772
 *	Besides input and graphics data, other data that can be exchanged between client and server
Packit Service 5a9772
 *after the connection has been finalized includes connection management information and virtual
Packit Service 5a9772
 *channel messages (exchanged between client-side plug-ins and server-side applications).
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
static int rdp_client_connect_finalize(rdpRdp* rdp);
Packit Service 5a9772
static BOOL rdp_send_server_control_granted_pdu(rdpRdp* rdp);
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_reset_codecs(rdpContext* context)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpSettings* settings;
Packit 1fb8d4
Packit 1fb8d4
	if (!context || !context->settings)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	settings = context->settings;
Packit 1fb8d4
	context->codecs = codecs_new(context);
Packit 1fb8d4
Packit 1fb8d4
	if (!context->codecs)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	if (!freerdp_client_codecs_prepare(context->codecs, FREERDP_CODEC_ALL, settings->DesktopWidth,
Packit Service 5a9772
	                                   settings->DesktopHeight))
Packit Service 5a9772
		return FALSE;
Packit Service 5a9772
Packit Service 5a9772
/* Runtime H264 detection. (only available if dynamic backend loading is defined)
Packit Service 5a9772
 * If no backend is available disable it before the channel is loaded.
Packit Service 5a9772
 */
Packit Service 5a9772
#if defined(WITH_GFX_H264) && defined(WITH_OPENH264_LOADING)
Packit Service 5a9772
	if (!context->codecs->h264)
Packit Service 5a9772
	{
Packit Service 5a9772
		settings->GfxH264 = FALSE;
Packit Service 5a9772
		settings->GfxAVC444 = FALSE;
Packit Service 5a9772
		settings->GfxAVC444v2 = FALSE;
Packit Service 5a9772
	}
Packit Service 5a9772
#endif
Packit Service 5a9772
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Establish RDP Connection based on the settings given in the 'rdp' parameter.
Packit 1fb8d4
 * @msdn{cc240452}
Packit 1fb8d4
 * @param rdp RDP module
Packit 1fb8d4
 * @return true if the connection succeeded. FALSE otherwise.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_connect(rdpRdp* rdp)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT32 SelectedProtocol;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	rdpSettings* settings = rdp->settings;
Packit Service 5a9772
	/* make sure SSL is initialize for earlier enough for crypto, by taking advantage of winpr SSL
Packit Service 5a9772
	 * FIPS flag for openssl initialization */
Packit 1fb8d4
	DWORD flags = WINPR_SSL_INIT_DEFAULT;
Packit Service 5a9772
	UINT32 timeout;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_client_reset_codecs(rdp->context))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (settings->FIPSMode)
Packit 1fb8d4
		flags |= WINPR_SSL_INIT_ENABLE_FIPS;
Packit 1fb8d4
Packit 1fb8d4
	winpr_InitializeSSL(flags);
Packit 1fb8d4
Packit 1fb8d4
	/* FIPS Mode forces the following and overrides the following(by happening later */
Packit 1fb8d4
	/* in the command line processing): */
Packit Service 5a9772
	/* 1. Disables NLA Security since NLA in freerdp uses NTLM(no Kerberos support yet) which uses
Packit Service 5a9772
	 * algorithms */
Packit 1fb8d4
	/*      not allowed in FIPS for sensitive data. So, we disallow NLA when FIPS is required. */
Packit 1fb8d4
	/* 2. Forces the only supported RDP encryption method to be FIPS. */
Packit 1fb8d4
	if (settings->FIPSMode || winpr_FIPSMode())
Packit 1fb8d4
	{
Packit 1fb8d4
		settings->NlaSecurity = FALSE;
Packit 1fb8d4
		settings->EncryptionMethods = ENCRYPTION_METHOD_FIPS;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	nego_init(rdp->nego);
Packit 1fb8d4
	nego_set_target(rdp->nego, settings->ServerHostname, settings->ServerPort);
Packit 1fb8d4
Packit 1fb8d4
	if (settings->GatewayEnabled)
Packit 1fb8d4
	{
Packit 1fb8d4
		char* user = NULL;
Packit 1fb8d4
		char* domain = NULL;
Packit 1fb8d4
		char* cookie = NULL;
Packit 1fb8d4
		int user_length = 0;
Packit 1fb8d4
		int domain_length = 0;
Packit 1fb8d4
		int cookie_length = 0;
Packit 1fb8d4
Packit 1fb8d4
		if (settings->Username)
Packit 1fb8d4
		{
Packit 1fb8d4
			user = settings->Username;
Packit 1fb8d4
			user_length = strlen(settings->Username);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (settings->Domain)
Packit 1fb8d4
			domain = settings->Domain;
Packit 1fb8d4
		else
Packit 1fb8d4
			domain = settings->ComputerName;
Packit 1fb8d4
Packit 1fb8d4
		domain_length = strlen(domain);
Packit 1fb8d4
		cookie_length = domain_length + 1 + user_length;
Packit Service 5a9772
		cookie = (char*)malloc(cookie_length + 1);
Packit 1fb8d4
Packit 1fb8d4
		if (!cookie)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		CopyMemory(cookie, domain, domain_length);
Packit 1fb8d4
		CharUpperBuffA(cookie, domain_length);
Packit 1fb8d4
		cookie[domain_length] = '\\';
Packit 1fb8d4
Packit 1fb8d4
		if (settings->Username)
Packit 1fb8d4
			CopyMemory(&cookie[domain_length + 1], user, user_length);
Packit 1fb8d4
Packit 1fb8d4
		cookie[cookie_length] = '\0';
Packit 1fb8d4
		status = nego_set_cookie(rdp->nego, cookie);
Packit 1fb8d4
		free(cookie);
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		status = nego_set_cookie(rdp->nego, settings->Username);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!status)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	nego_set_send_preconnection_pdu(rdp->nego, settings->SendPreconnectionPdu);
Packit 1fb8d4
	nego_set_preconnection_id(rdp->nego, settings->PreconnectionId);
Packit 1fb8d4
	nego_set_preconnection_blob(rdp->nego, settings->PreconnectionBlob);
Packit 1fb8d4
	nego_set_negotiation_enabled(rdp->nego, settings->NegotiateSecurityLayer);
Packit 1fb8d4
	nego_set_restricted_admin_mode_required(rdp->nego, settings->RestrictedAdminModeRequired);
Packit 1fb8d4
	nego_set_gateway_enabled(rdp->nego, settings->GatewayEnabled);
Packit 1fb8d4
	nego_set_gateway_bypass_local(rdp->nego, settings->GatewayBypassLocal);
Packit 1fb8d4
	nego_enable_rdp(rdp->nego, settings->RdpSecurity);
Packit 1fb8d4
	nego_enable_tls(rdp->nego, settings->TlsSecurity);
Packit 1fb8d4
	nego_enable_nla(rdp->nego, settings->NlaSecurity);
Packit 1fb8d4
	nego_enable_ext(rdp->nego, settings->ExtSecurity);
Packit 1fb8d4
Packit 1fb8d4
	if (settings->MstscCookieMode)
Packit 1fb8d4
		settings->CookieMaxLength = MSTSC_COOKIE_MAX_LENGTH;
Packit 1fb8d4
Packit 1fb8d4
	nego_set_cookie_max_length(rdp->nego, settings->CookieMaxLength);
Packit 1fb8d4
Packit Service 5a9772
	if (settings->LoadBalanceInfo && (settings->LoadBalanceInfoLength > 0))
Packit 1fb8d4
	{
Packit Service 5a9772
		if (!nego_set_routing_token(rdp->nego, settings->LoadBalanceInfo,
Packit Service 5a9772
		                            settings->LoadBalanceInfoLength))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp_client_transition_to_state(rdp, CONNECTION_STATE_NEGO);
Packit 1fb8d4
Packit 1fb8d4
	if (!nego_connect(rdp->nego))
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!freerdp_get_last_error(rdp->context))
Packit 1fb8d4
		{
Packit Service 5a9772
			freerdp_set_last_error_log(rdp->context, FREERDP_ERROR_SECURITY_NEGO_CONNECT_FAILED);
Packit 1fb8d4
			WLog_ERR(TAG, "Error: protocol security negotiation or connection failure");
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	SelectedProtocol = nego_get_selected_protocol(rdp->nego);
Packit Service 5a9772
Packit Service 5a9772
	if ((SelectedProtocol & PROTOCOL_SSL) || (SelectedProtocol == PROTOCOL_RDP))
Packit 1fb8d4
	{
Packit Service 5a9772
		if ((settings->Username != NULL) &&
Packit Service 5a9772
		    ((settings->Password != NULL) ||
Packit Service 5a9772
		     (settings->RedirectionPassword != NULL && settings->RedirectionPasswordLength > 0)))
Packit 1fb8d4
			settings->AutoLogonEnabled = TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* everything beyond this point is event-driven and non blocking */
Packit 1fb8d4
	rdp->transport->ReceiveCallback = rdp_recv_callback;
Packit 1fb8d4
	rdp->transport->ReceiveExtra = rdp;
Packit 1fb8d4
	transport_set_blocking_mode(rdp->transport, FALSE);
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->state != CONNECTION_STATE_NLA)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!mcs_client_begin(rdp->mcs))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	for (timeout = 0; timeout < settings->TcpAckTimeout; timeout += 100)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (rdp_check_fds(rdp) < 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			freerdp_set_last_error_if_not(rdp->context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
		}
Packit Service 5a9772
Packit Service 5a9772
		if (rdp->state == CONNECTION_STATE_ACTIVE)
Packit Service 5a9772
			return TRUE;
Packit Service 5a9772
Packit Service 5a9772
		Sleep(100);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	WLog_ERR(TAG, "Timeout waiting for activation");
Packit Service 5a9772
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_disconnect(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpContext* context;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp || !rdp->settings || !rdp->context)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	context = rdp->context;
Packit 1fb8d4
Packit 1fb8d4
	if (!nego_disconnect(rdp->nego))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_reset(rdp);
Packit 1fb8d4
	rdp_client_transition_to_state(rdp, CONNECTION_STATE_INITIAL);
Packit 1fb8d4
Packit 1fb8d4
	if (freerdp_channels_disconnect(context->channels, context->instance) != CHANNEL_RC_OK)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	codecs_free(context->codecs);
Packit Service 5a9772
	context->codecs = NULL;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_disconnect_and_clear(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	rdpContext* context;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_client_disconnect(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	context = rdp->context;
Packit 1fb8d4
	context->LastError = FREERDP_ERROR_SUCCESS;
Packit 1fb8d4
	clearChannelError(context);
Packit 1fb8d4
	ResetEvent(context->abortEvent);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_reconnect_channels(rdpRdp* rdp, BOOL redirect)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL status = FALSE;
Packit 1fb8d4
	rdpContext* context;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp || !rdp->context || !rdp->context->channels)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	context = rdp->context;
Packit 1fb8d4
Packit 1fb8d4
	if (context->instance->ConnectionCallbackState == CLIENT_STATE_INITIAL)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (context->instance->ConnectionCallbackState == CLIENT_STATE_PRECONNECT_PASSED)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (redirect)
Packit 1fb8d4
			return TRUE;
Packit 1fb8d4
Packit 1fb8d4
		pointer_cache_register_callbacks(context->update);
Packit 1fb8d4
Packit 1fb8d4
		if (!IFCALLRESULT(FALSE, context->instance->PostConnect, context->instance))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		context->instance->ConnectionCallbackState = CLIENT_STATE_POSTCONNECT_PASSED;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (context->instance->ConnectionCallbackState == CLIENT_STATE_POSTCONNECT_PASSED)
Packit Service 5a9772
		status =
Packit Service 5a9772
		    (freerdp_channels_post_connect(context->channels, context->instance) == CHANNEL_RC_OK);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_redirect_resolvable(const char* host)
Packit 1fb8d4
{
Packit 1fb8d4
	struct addrinfo* result = freerdp_tcp_resolve_host(host, -1, 0);
Packit 1fb8d4
Packit 1fb8d4
	if (!result)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	freeaddrinfo(result);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_redirect_try_fqdn(rdpSettings* settings)
Packit 1fb8d4
{
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_TARGET_FQDN)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (settings->GatewayEnabled ||
Packit Service 5a9772
		    rdp_client_redirect_resolvable(settings->RedirectionTargetFQDN))
Packit 1fb8d4
		{
Packit 1fb8d4
			free(settings->ServerHostname);
Packit 1fb8d4
			settings->ServerHostname = _strdup(settings->RedirectionTargetFQDN);
Packit 1fb8d4
Packit 1fb8d4
			if (!settings->ServerHostname)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			return TRUE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_redirect_try_ip(rdpSettings* settings)
Packit 1fb8d4
{
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_TARGET_NET_ADDRESS)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(settings->ServerHostname);
Packit 1fb8d4
		settings->ServerHostname = _strdup(settings->TargetNetAddress);
Packit 1fb8d4
Packit 1fb8d4
		if (!settings->ServerHostname)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_redirect_try_netbios(rdpSettings* settings)
Packit 1fb8d4
{
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_TARGET_NETBIOS_NAME)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (settings->GatewayEnabled ||
Packit Service 5a9772
		    rdp_client_redirect_resolvable(settings->RedirectionTargetNetBiosName))
Packit 1fb8d4
		{
Packit 1fb8d4
			free(settings->ServerHostname);
Packit 1fb8d4
			settings->ServerHostname = _strdup(settings->RedirectionTargetNetBiosName);
Packit 1fb8d4
Packit 1fb8d4
			if (!settings->ServerHostname)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			return TRUE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_redirect(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	rdpSettings* settings;
Packit 1fb8d4
Packit Service 5a9772
	if (!rdp || !rdp->settings)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	settings = rdp->settings;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_client_disconnect_and_clear(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp_redirection_apply_settings(rdp) != 0)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_LOAD_BALANCE_INFO)
Packit 1fb8d4
	{
Packit Service 5a9772
		if (settings->LoadBalanceInfo && (settings->LoadBalanceInfoLength > 0))
Packit Service 5a9772
		{
Packit Service 5a9772
			if (!nego_set_routing_token(rdp->nego, settings->LoadBalanceInfo,
Packit Service 5a9772
			                            settings->LoadBalanceInfoLength))
Packit Service 5a9772
				return FALSE;
Packit Service 5a9772
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		BOOL haveRedirectAddress = FALSE;
Packit 1fb8d4
		UINT32 redirectionMask = settings->RedirectionPreferType;
Packit 1fb8d4
Packit 1fb8d4
		do
Packit 1fb8d4
		{
Packit 1fb8d4
			const BOOL tryFQDN = (redirectionMask & 0x01) == 0;
Packit 1fb8d4
			const BOOL tryNetAddress = (redirectionMask & 0x02) == 0;
Packit 1fb8d4
			const BOOL tryNetbios = (redirectionMask & 0x04) == 0;
Packit 1fb8d4
Packit 1fb8d4
			if (tryFQDN && !haveRedirectAddress)
Packit 1fb8d4
				haveRedirectAddress = rdp_client_redirect_try_fqdn(settings);
Packit 1fb8d4
Packit 1fb8d4
			if (tryNetAddress && !haveRedirectAddress)
Packit 1fb8d4
				haveRedirectAddress = rdp_client_redirect_try_ip(settings);
Packit 1fb8d4
Packit 1fb8d4
			if (tryNetbios && !haveRedirectAddress)
Packit 1fb8d4
				haveRedirectAddress = rdp_client_redirect_try_netbios(settings);
Packit 1fb8d4
Packit 1fb8d4
			redirectionMask >>= 3;
Packit Service 5a9772
		} while (!haveRedirectAddress && (redirectionMask != 0));
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_USERNAME)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(settings->Username);
Packit 1fb8d4
		settings->Username = _strdup(settings->RedirectionUsername);
Packit 1fb8d4
Packit 1fb8d4
		if (!settings->Username)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (settings->RedirectionFlags & LB_DOMAIN)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(settings->Domain);
Packit 1fb8d4
		settings->Domain = _strdup(settings->RedirectionDomain);
Packit 1fb8d4
Packit 1fb8d4
		if (!settings->Domain)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = rdp_client_connect(rdp);
Packit 1fb8d4
Packit 1fb8d4
	if (status)
Packit 1fb8d4
		status = rdp_client_reconnect_channels(rdp, TRUE);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_reconnect(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp || !rdp->context || !rdp->context->channels)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_client_disconnect_and_clear(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	status = rdp_client_connect(rdp);
Packit 1fb8d4
Packit 1fb8d4
	if (status)
Packit 1fb8d4
		status = rdp_client_reconnect_channels(rdp, FALSE);
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static const BYTE fips_ivec[8] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdp_client_establish_keys(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* mod;
Packit 1fb8d4
	BYTE* exp;
Packit 1fb8d4
	wStream* s;
Packit 1fb8d4
	UINT32 length;
Packit 1fb8d4
	UINT32 key_len;
Packit 1fb8d4
	int status = 0;
Packit 1fb8d4
	BOOL ret = FALSE;
Packit 1fb8d4
	rdpSettings* settings;
Packit 1fb8d4
	BYTE* crypt_client_random = NULL;
Packit 1fb8d4
	settings = rdp->settings;
Packit 1fb8d4
Packit 1fb8d4
	if (!settings->UseRdpSecurityLayer)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* no RDP encryption */
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* encrypt client random */
Packit 1fb8d4
	free(settings->ClientRandom);
Packit 1fb8d4
	settings->ClientRandomLength = CLIENT_RANDOM_LENGTH;
Packit 1fb8d4
	settings->ClientRandom = malloc(settings->ClientRandomLength);
Packit 1fb8d4
Packit 1fb8d4
	if (!settings->ClientRandom)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	winpr_RAND(settings->ClientRandom, settings->ClientRandomLength);
Packit 1fb8d4
	key_len = settings->RdpServerCertificate->cert_info.ModulusLength;
Packit 1fb8d4
	mod = settings->RdpServerCertificate->cert_info.Modulus;
Packit 1fb8d4
	exp = settings->RdpServerCertificate->cert_info.exponent;
Packit 1fb8d4
	/*
Packit 1fb8d4
	 * client random must be (bitlen / 8) + 8 - see [MS-RDPBCGR] 5.3.4.1
Packit 1fb8d4
	 * for details
Packit 1fb8d4
	 */
Packit 1fb8d4
	crypt_client_random = calloc(key_len + 8, 1);
Packit 1fb8d4
Packit 1fb8d4
	if (!crypt_client_random)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	crypto_rsa_public_encrypt(settings->ClientRandom, settings->ClientRandomLength, key_len, mod,
Packit Service 5a9772
	                          exp, crypt_client_random);
Packit 1fb8d4
	/* send crypt client random to server */
Packit 1fb8d4
	length = RDP_PACKET_HEADER_MAX_LENGTH + RDP_SECURITY_HEADER_LENGTH + 4 + key_len + 8;
Packit 1fb8d4
	s = Stream_New(NULL, length);
Packit 1fb8d4
Packit 1fb8d4
	if (!s)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Stream_New failed!");
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp_write_header(rdp, s, length, MCS_GLOBAL_CHANNEL_ID);
Packit 1fb8d4
	rdp_write_security_header(s, SEC_EXCHANGE_PKT | SEC_LICENSE_ENCRYPT_SC);
Packit 1fb8d4
	length = key_len + 8;
Packit 1fb8d4
	Stream_Write_UINT32(s, length);
Packit 1fb8d4
	Stream_Write(s, crypt_client_random, length);
Packit 1fb8d4
	Stream_SealLength(s);
Packit 1fb8d4
	status = transport_write(rdp->mcs->transport, s);
Packit 1fb8d4
	Stream_Free(s, TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		goto end;
Packit 1fb8d4
Packit 1fb8d4
	rdp->do_crypt_license = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	/* now calculate encrypt / decrypt and update keys */
Packit 1fb8d4
	if (!security_establish_keys(settings->ClientRandom, rdp))
Packit 1fb8d4
		goto end;
Packit 1fb8d4
Packit 1fb8d4
	rdp->do_crypt = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	if (settings->SaltedChecksum)
Packit 1fb8d4
		rdp->do_secure_checksum = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	if (settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
Packit 1fb8d4
	{
Packit Service 5a9772
		rdp->fips_encrypt = winpr_Cipher_New(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT,
Packit Service 5a9772
		                                     rdp->fips_encrypt_key, fips_ivec);
Packit 1fb8d4
Packit 1fb8d4
		if (!rdp->fips_encrypt)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unable to allocate des3 encrypt key");
Packit 1fb8d4
			goto end;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		rdp->fips_decrypt = winpr_Cipher_New(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT,
Packit Service 5a9772
		                                     rdp->fips_decrypt_key, fips_ivec);
Packit 1fb8d4
Packit 1fb8d4
		if (!rdp->fips_decrypt)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unable to allocate des3 decrypt key");
Packit 1fb8d4
			goto end;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		ret = TRUE;
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp->rc4_decrypt_key = winpr_RC4_New(rdp->decrypt_key, rdp->rc4_key_len);
Packit 1fb8d4
	rdp->rc4_encrypt_key = winpr_RC4_New(rdp->encrypt_key, rdp->rc4_key_len);
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp->rc4_decrypt_key || !rdp->rc4_encrypt_key)
Packit 1fb8d4
		goto end;
Packit 1fb8d4
Packit 1fb8d4
	ret = TRUE;
Packit 1fb8d4
end:
Packit 1fb8d4
	free(crypt_client_random);
Packit 1fb8d4
Packit 1fb8d4
	if (!ret)
Packit 1fb8d4
	{
Packit 1fb8d4
		winpr_Cipher_Free(rdp->fips_decrypt);
Packit 1fb8d4
		winpr_Cipher_Free(rdp->fips_encrypt);
Packit 1fb8d4
		winpr_RC4_Free(rdp->rc4_decrypt_key);
Packit 1fb8d4
		winpr_RC4_Free(rdp->rc4_encrypt_key);
Packit 1fb8d4
		rdp->fips_decrypt = NULL;
Packit 1fb8d4
		rdp->fips_encrypt = NULL;
Packit 1fb8d4
		rdp->rc4_decrypt_key = NULL;
Packit 1fb8d4
		rdp->rc4_encrypt_key = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_establish_keys(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* client_random = NULL;
Packit 1fb8d4
	BYTE* crypt_client_random = NULL;
Packit 1fb8d4
	UINT32 rand_len, key_len;
Packit 1fb8d4
	UINT16 channel_id, length, sec_flags;
Packit 1fb8d4
	BYTE* mod;
Packit 1fb8d4
	BYTE* priv_exp;
Packit 1fb8d4
	BOOL ret = FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp->settings->UseRdpSecurityLayer)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* No RDP Security. */
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_read_header(rdp, s, &length, &channel_id))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid RDP header");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_read_security_header(s, &sec_flags, NULL))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid security header");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if ((sec_flags & SEC_EXCHANGE_PKT) == 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "missing SEC_EXCHANGE_PKT in security header");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp->do_crypt_license = (sec_flags & SEC_LICENSE_ENCRYPT_SC) != 0 ? TRUE : FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < 4)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read_UINT32(s, rand_len);
Packit 1fb8d4
Packit 1fb8d4
	/* rand_len already includes 8 bytes of padding */
Packit 1fb8d4
	if (Stream_GetRemainingLength(s) < rand_len)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	key_len = rdp->settings->RdpServerRsaKey->ModulusLength;
Packit 1fb8d4
	client_random = malloc(key_len);
Packit 1fb8d4
Packit 1fb8d4
	if (!client_random)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (rand_len != key_len + 8)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "invalid encrypted client random length");
Packit 1fb8d4
		free(client_random);
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	crypt_client_random = calloc(1, rand_len);
Packit 1fb8d4
Packit 1fb8d4
	if (!crypt_client_random)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(client_random);
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	Stream_Read(s, crypt_client_random, rand_len);
Packit 1fb8d4
	mod = rdp->settings->RdpServerRsaKey->Modulus;
Packit 1fb8d4
	priv_exp = rdp->settings->RdpServerRsaKey->PrivateExponent;
Packit 1fb8d4
Packit 1fb8d4
	if (crypto_rsa_private_decrypt(crypt_client_random, rand_len - 8, key_len, mod, priv_exp,
Packit 1fb8d4
	                               client_random) <= 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		free(client_random);
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp->settings->ClientRandom = client_random;
Packit 1fb8d4
	rdp->settings->ClientRandomLength = 32;
Packit 1fb8d4
Packit 1fb8d4
	/* now calculate encrypt / decrypt and update keys */
Packit 1fb8d4
	if (!security_establish_keys(client_random, rdp))
Packit 1fb8d4
		goto end;
Packit 1fb8d4
Packit 1fb8d4
	rdp->do_crypt = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
Packit 1fb8d4
	{
Packit Service 5a9772
		rdp->fips_encrypt = winpr_Cipher_New(WINPR_CIPHER_DES_EDE3_CBC, WINPR_ENCRYPT,
Packit Service 5a9772
		                                     rdp->fips_encrypt_key, fips_ivec);
Packit 1fb8d4
Packit 1fb8d4
		if (!rdp->fips_encrypt)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unable to allocate des3 encrypt key");
Packit 1fb8d4
			goto end;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit Service 5a9772
		rdp->fips_decrypt = winpr_Cipher_New(WINPR_CIPHER_DES_EDE3_CBC, WINPR_DECRYPT,
Packit Service 5a9772
		                                     rdp->fips_decrypt_key, fips_ivec);
Packit 1fb8d4
Packit 1fb8d4
		if (!rdp->fips_decrypt)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "unable to allocate des3 decrypt key");
Packit 1fb8d4
			goto end;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		ret = TRUE;
Packit 1fb8d4
		goto end;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp->rc4_decrypt_key = winpr_RC4_New(rdp->decrypt_key, rdp->rc4_key_len);
Packit 1fb8d4
	rdp->rc4_encrypt_key = winpr_RC4_New(rdp->encrypt_key, rdp->rc4_key_len);
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp->rc4_decrypt_key || !rdp->rc4_encrypt_key)
Packit 1fb8d4
		goto end;
Packit 1fb8d4
Packit Service 5a9772
	ret = tpkt_ensure_stream_consumed(s, length);
Packit 1fb8d4
end:
Packit 1fb8d4
	free(crypt_client_random);
Packit 1fb8d4
Packit 1fb8d4
	if (!ret)
Packit 1fb8d4
	{
Packit 1fb8d4
		winpr_Cipher_Free(rdp->fips_encrypt);
Packit 1fb8d4
		winpr_Cipher_Free(rdp->fips_decrypt);
Packit 1fb8d4
		winpr_RC4_Free(rdp->rc4_encrypt_key);
Packit 1fb8d4
		winpr_RC4_Free(rdp->rc4_decrypt_key);
Packit 1fb8d4
		rdp->fips_encrypt = NULL;
Packit 1fb8d4
		rdp->fips_decrypt = NULL;
Packit 1fb8d4
		rdp->rc4_encrypt_key = NULL;
Packit 1fb8d4
		rdp->rc4_decrypt_key = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return ret;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_connect_mcs_channel_join_confirm(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
	UINT16 channelId;
Packit 1fb8d4
	BOOL allJoined = TRUE;
Packit 1fb8d4
	rdpMcs* mcs = rdp->mcs;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_recv_channel_join_confirm(mcs, s, &channelId))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs->userChannelJoined)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (channelId != mcs->userId)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		mcs->userChannelJoined = TRUE;
Packit 1fb8d4
Packit 1fb8d4
		if (!mcs_send_channel_join_request(mcs, MCS_GLOBAL_CHANNEL_ID))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
	else if (!mcs->globalChannelJoined)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (channelId != MCS_GLOBAL_CHANNEL_ID)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		mcs->globalChannelJoined = TRUE;
Packit 1fb8d4
Packit 1fb8d4
		if (mcs->messageChannelId != 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (!mcs_send_channel_join_request(mcs, mcs->messageChannelId))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			allJoined = FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			if (mcs->channelCount > 0)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (!mcs_send_channel_join_request(mcs, mcs->channels[0].ChannelId))
Packit 1fb8d4
					return FALSE;
Packit 1fb8d4
Packit 1fb8d4
				allJoined = FALSE;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else if ((mcs->messageChannelId != 0) && !mcs->messageChannelJoined)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (channelId != mcs->messageChannelId)
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		mcs->messageChannelJoined = TRUE;
Packit 1fb8d4
Packit 1fb8d4
		if (mcs->channelCount > 0)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (!mcs_send_channel_join_request(mcs, mcs->channels[0].ChannelId))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			allJoined = FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		for (i = 0; i < mcs->channelCount; i++)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (mcs->channels[i].joined)
Packit 1fb8d4
				continue;
Packit 1fb8d4
Packit 1fb8d4
			if (mcs->channels[i].ChannelId != channelId)
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			mcs->channels[i].joined = TRUE;
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		if (i + 1 < mcs->channelCount)
Packit 1fb8d4
		{
Packit 1fb8d4
			if (!mcs_send_channel_join_request(mcs, mcs->channels[i + 1].ChannelId))
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
Packit 1fb8d4
			allJoined = FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (mcs->userChannelJoined && mcs->globalChannelJoined && allJoined)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!rdp_client_establish_keys(rdp))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		if (!rdp_send_client_info(rdp))
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
Packit 1fb8d4
		rdp_client_transition_to_state(rdp, CONNECTION_STATE_LICENSING);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_client_connect_auto_detect(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* mark;
Packit 1fb8d4
	UINT16 length;
Packit 1fb8d4
	UINT16 channelId;
Packit 1fb8d4
Packit 1fb8d4
	/* If the MCS message channel has been joined... */
Packit 1fb8d4
	if (rdp->mcs->messageChannelId != 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		/* Process any MCS message channel PDUs. */
Packit 1fb8d4
		Stream_GetPointer(s, mark);
Packit 1fb8d4
Packit 1fb8d4
		if (rdp_read_header(rdp, s, &length, &channelId))
Packit 1fb8d4
		{
Packit 1fb8d4
			if (channelId == rdp->mcs->messageChannelId)
Packit 1fb8d4
			{
Packit 1fb8d4
				UINT16 securityFlags = 0;
Packit 1fb8d4
Packit 1fb8d4
				if (!rdp_read_security_header(s, &securityFlags, &length))
Packit 1fb8d4
					return FALSE;
Packit 1fb8d4
Packit 1fb8d4
				if (securityFlags & SEC_ENCRYPT)
Packit 1fb8d4
				{
Packit Service 5a9772
					if (!rdp_decrypt(rdp, s, &length, securityFlags))
Packit 1fb8d4
					{
Packit 1fb8d4
						WLog_ERR(TAG, "rdp_decrypt failed");
Packit 1fb8d4
						return FALSE;
Packit 1fb8d4
					}
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if (rdp_recv_message_channel_pdu(rdp, s, securityFlags) == 0)
Packit Service 5a9772
					return tpkt_ensure_stream_consumed(s, length);
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		Stream_SetPointer(s, mark);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int rdp_client_connect_license(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	status = license_recv(rdp->license, s);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		return status;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->license->state == LICENSE_STATE_ABORTED)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "license connection sequence aborted.");
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->license->state == LICENSE_STATE_COMPLETED)
Packit 1fb8d4
	{
Packit 1fb8d4
		rdp_client_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int rdp_client_connect_demand_active(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	BYTE* mark;
Packit 1fb8d4
	UINT16 width;
Packit 1fb8d4
	UINT16 height;
Packit Service 5a9772
	UINT16 length;
Packit 1fb8d4
	width = rdp->settings->DesktopWidth;
Packit 1fb8d4
	height = rdp->settings->DesktopHeight;
Packit 1fb8d4
	Stream_GetPointer(s, mark);
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_recv_demand_active(rdp, s))
Packit 1fb8d4
	{
Packit Service 5a9772
		int rc;
Packit 1fb8d4
		UINT16 channelId;
Packit 1fb8d4
		Stream_SetPointer(s, mark);
Packit Service 5a9772
		if (!rdp_recv_get_active_header(rdp, s, &channelId, &length))
Packit Service 5a9772
			return -1;
Packit 1fb8d4
		/* Was Stream_Seek(s, RDP_PACKET_HEADER_MAX_LENGTH);
Packit 1fb8d4
		 * but the headers aren't always that length,
Packit 1fb8d4
		 * so that could result in a bad offset.
Packit 1fb8d4
		 */
Packit Service 5a9772
		rc = rdp_recv_out_of_sequence_pdu(rdp, s);
Packit Service 5a9772
		if (rc < 0)
Packit Service 5a9772
			return rc;
Packit Service 5a9772
		if (!tpkt_ensure_stream_consumed(s, length))
Packit Service 5a9772
			return -1;
Packit Service 5a9772
		return rc;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (freerdp_shall_disconnect(rdp->instance))
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_confirm_active(rdp))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (!input_register_client_callbacks(rdp->input))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "error registering client callbacks");
Packit 1fb8d4
		return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * The server may request a different desktop size during Deactivation-Reactivation sequence.
Packit 1fb8d4
	 * In this case, the UI should be informed and do actual window resizing at this point.
Packit 1fb8d4
	 */
Packit 1fb8d4
	if (width != rdp->settings->DesktopWidth || height != rdp->settings->DesktopHeight)
Packit 1fb8d4
	{
Packit 1fb8d4
		BOOL status = TRUE;
Packit 1fb8d4
		IFCALLRET(rdp->update->DesktopResize, status, rdp->update->context);
Packit 1fb8d4
Packit 1fb8d4
		if (!status)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "client desktop resize callback failed");
Packit 1fb8d4
			return -1;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	rdp_client_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION);
Packit 1fb8d4
	return rdp_client_connect_finalize(rdp);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int rdp_client_connect_finalize(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * [MS-RDPBCGR] 1.3.1.1 - 8.
Packit Service 5a9772
	 * The client-to-server PDUs sent during this phase have no dependencies on any of the
Packit Service 5a9772
	 * server-to- client PDUs; they may be sent as a single batch, provided that sequencing is
Packit Service 5a9772
	 * maintained.
Packit 1fb8d4
	 */
Packit 1fb8d4
	if (!rdp_send_client_synchronize_pdu(rdp))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_client_control_pdu(rdp, CTRLACTION_COOPERATE))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_client_control_pdu(rdp, CTRLACTION_REQUEST_CONTROL))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * [MS-RDPBCGR] 2.2.1.17
Packit 1fb8d4
	 * Client persistent key list must be sent if a bitmap is
Packit 1fb8d4
	 * stored in persistent bitmap cache or the server has advertised support for bitmap
Packit 1fb8d4
	 * host cache and a deactivation reactivation sequence is *not* in progress.
Packit 1fb8d4
	 */
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp->deactivation_reactivation && rdp->settings->BitmapCachePersistEnabled)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!rdp_send_client_persistent_key_list_pdu(rdp))
Packit 1fb8d4
			return -1;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_client_font_list_pdu(rdp, FONTLIST_FIRST | FONTLIST_LAST))
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int rdp_client_transition_to_state(rdpRdp* rdp, int state)
Packit 1fb8d4
{
Packit 1fb8d4
	int status = 0;
Packit 1fb8d4
Packit 1fb8d4
	switch (state)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CONNECTION_STATE_INITIAL:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_INITIAL;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_NEGO:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_NEGO;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_NLA:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_NLA;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_CONNECT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_CONNECT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_ERECT_DOMAIN:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_ERECT_DOMAIN;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_ATTACH_USER:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_ATTACH_USER;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_CHANNEL_JOIN:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_CHANNEL_JOIN;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_LICENSING:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_LICENSING;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_CAPABILITIES_EXCHANGE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_FINALIZATION:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_FINALIZATION;
Packit 1fb8d4
			update_reset_state(rdp->update);
Packit 1fb8d4
			rdp->finalize_sc_pdus = 0;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_ACTIVE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_ACTIVE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			status = -1;
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_accept_nego(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit Service 5a9772
	UINT32 SelectedProtocol = 0;
Packit Service 5a9772
	UINT32 RequestedProtocols;
Packit 1fb8d4
	BOOL status;
Packit 1fb8d4
	rdpSettings* settings = rdp->settings;
Packit 1fb8d4
	rdpNego* nego = rdp->nego;
Packit 1fb8d4
	transport_set_blocking_mode(rdp->transport, TRUE);
Packit 1fb8d4
Packit 1fb8d4
	if (!nego_read_request(nego, s))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	RequestedProtocols = nego_get_requested_protocols(nego);
Packit 1fb8d4
	WLog_INFO(TAG, "Client Security: NLA:%d TLS:%d RDP:%d",
Packit Service 5a9772
	          (RequestedProtocols & PROTOCOL_HYBRID) ? 1 : 0,
Packit Service 5a9772
	          (RequestedProtocols & PROTOCOL_SSL) ? 1 : 0,
Packit Service 5a9772
	          (RequestedProtocols == PROTOCOL_RDP) ? 1 : 0);
Packit Service 5a9772
	WLog_INFO(TAG, "Server Security: NLA:%" PRId32 " TLS:%" PRId32 " RDP:%" PRId32 "",
Packit 1fb8d4
	          settings->NlaSecurity, settings->TlsSecurity, settings->RdpSecurity);
Packit 1fb8d4
Packit Service 5a9772
	if ((settings->NlaSecurity) && (RequestedProtocols & PROTOCOL_HYBRID))
Packit 1fb8d4
	{
Packit Service 5a9772
		SelectedProtocol = PROTOCOL_HYBRID;
Packit 1fb8d4
	}
Packit Service 5a9772
	else if ((settings->TlsSecurity) && (RequestedProtocols & PROTOCOL_SSL))
Packit 1fb8d4
	{
Packit Service 5a9772
		SelectedProtocol = PROTOCOL_SSL;
Packit 1fb8d4
	}
Packit Service 5a9772
	else if ((settings->RdpSecurity) && (RequestedProtocols == PROTOCOL_RDP))
Packit 1fb8d4
	{
Packit Service 5a9772
		SelectedProtocol = PROTOCOL_RDP;
Packit 1fb8d4
	}
Packit 1fb8d4
	else
Packit 1fb8d4
	{
Packit 1fb8d4
		/*
Packit 1fb8d4
		 * when here client and server aren't compatible, we select the right
Packit 1fb8d4
		 * error message to return to the client in the nego failure packet
Packit 1fb8d4
		 */
Packit Service 5a9772
		SelectedProtocol = PROTOCOL_FAILED_NEGO;
Packit 1fb8d4
Packit 1fb8d4
		if (settings->RdpSecurity)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "server supports only Standard RDP Security");
Packit Service 5a9772
			SelectedProtocol |= SSL_NOT_ALLOWED_BY_SERVER;
Packit 1fb8d4
		}
Packit 1fb8d4
		else
Packit 1fb8d4
		{
Packit 1fb8d4
			if (settings->NlaSecurity && !settings->TlsSecurity)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_WARN(TAG, "server supports only NLA Security");
Packit Service 5a9772
				SelectedProtocol |= HYBRID_REQUIRED_BY_SERVER;
Packit 1fb8d4
			}
Packit 1fb8d4
			else
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_WARN(TAG, "server supports only a SSL based Security (TLS or NLA)");
Packit Service 5a9772
				SelectedProtocol |= SSL_REQUIRED_BY_SERVER;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		WLog_ERR(TAG, "Protocol security negotiation failure");
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!(SelectedProtocol & PROTOCOL_FAILED_NEGO))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_INFO(TAG, "Negotiated Security: NLA:%d TLS:%d RDP:%d",
Packit Service 5a9772
		          (SelectedProtocol & PROTOCOL_HYBRID) ? 1 : 0,
Packit Service 5a9772
		          (SelectedProtocol & PROTOCOL_SSL) ? 1 : 0,
Packit Service 5a9772
		          (SelectedProtocol == PROTOCOL_RDP) ? 1 : 0);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if (!nego_set_selected_protocol(nego, SelectedProtocol))
Packit Service 5a9772
		return FALSE;
Packit Service 5a9772
Packit 1fb8d4
	if (!nego_send_negotiation_response(nego))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	SelectedProtocol = nego_get_selected_protocol(nego);
Packit 1fb8d4
	status = FALSE;
Packit 1fb8d4
Packit Service 5a9772
	if (SelectedProtocol & PROTOCOL_HYBRID)
Packit 1fb8d4
		status = transport_accept_nla(rdp->transport);
Packit Service 5a9772
	else if (SelectedProtocol & PROTOCOL_SSL)
Packit 1fb8d4
		status = transport_accept_tls(rdp->transport);
Packit Service 5a9772
	else if (SelectedProtocol == PROTOCOL_RDP) /* 0 */
Packit 1fb8d4
		status = transport_accept_rdp(rdp->transport);
Packit 1fb8d4
Packit 1fb8d4
	if (!status)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	transport_set_blocking_mode(rdp->transport, FALSE);
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_NEGO);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_accept_mcs_connect_initial(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
	rdpMcs* mcs = rdp->mcs;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_recv_connect_initial(mcs, s))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	WLog_INFO(TAG, "Accepted client: %s", rdp->settings->ClientHostname);
Packit 1fb8d4
	WLog_INFO(TAG, "Accepted channels:");
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < mcs->channelCount; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_INFO(TAG, " %s", mcs->channels[i].Name);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_send_connect_response(mcs))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_CONNECT);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_accept_mcs_erect_domain_request(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!mcs_recv_erect_domain_request(rdp->mcs, s))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ERECT_DOMAIN);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_accept_mcs_attach_user_request(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	if (!mcs_recv_attach_user_request(rdp->mcs, s))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_send_attach_user_confirm(rdp->mcs))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_MCS_ATTACH_USER);
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_accept_mcs_channel_join_request(rdpRdp* rdp, wStream* s)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT32 i;
Packit 1fb8d4
	UINT16 channelId;
Packit 1fb8d4
	BOOL allJoined = TRUE;
Packit 1fb8d4
	rdpMcs* mcs = rdp->mcs;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_recv_channel_join_request(mcs, s, &channelId))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!mcs_send_channel_join_confirm(mcs, channelId))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (channelId == mcs->userId)
Packit 1fb8d4
		mcs->userChannelJoined = TRUE;
Packit 1fb8d4
	else if (channelId == MCS_GLOBAL_CHANNEL_ID)
Packit 1fb8d4
		mcs->globalChannelJoined = TRUE;
Packit 1fb8d4
	else if (channelId == mcs->messageChannelId)
Packit 1fb8d4
		mcs->messageChannelJoined = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	for (i = 0; i < mcs->channelCount; i++)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (mcs->channels[i].ChannelId == channelId)
Packit 1fb8d4
			mcs->channels[i].joined = TRUE;
Packit 1fb8d4
Packit 1fb8d4
		if (!mcs->channels[i].joined)
Packit 1fb8d4
			allJoined = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if ((mcs->userChannelJoined) && (mcs->globalChannelJoined) &&
Packit Service 5a9772
	    (mcs->messageChannelId == 0 || mcs->messageChannelJoined) && allJoined)
Packit 1fb8d4
	{
Packit 1fb8d4
		rdp_server_transition_to_state(rdp, CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit Service 5a9772
BOOL rdp_server_accept_confirm_active(rdpRdp* rdp, wStream* s, UINT16 pduLength)
Packit 1fb8d4
{
Packit 1fb8d4
	freerdp_peer* peer = rdp->context->peer;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->state != CONNECTION_STATE_CAPABILITIES_EXCHANGE)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit Service 5a9772
	if (!rdp_recv_confirm_active(rdp, s, pduLength))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (peer->ClientCapabilities && !peer->ClientCapabilities(peer))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->settings->SaltedChecksum)
Packit 1fb8d4
		rdp->do_secure_checksum = TRUE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION);
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_server_synchronize_pdu(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_server_control_cooperate_pdu(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
BOOL rdp_server_reactivate(rdpRdp* rdp)
Packit 1fb8d4
{
Packit 1fb8d4
	freerdp_peer* client = NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->context && rdp->context->peer)
Packit 1fb8d4
		client = rdp->context->peer;
Packit 1fb8d4
Packit 1fb8d4
	if (client)
Packit 1fb8d4
		client->activated = FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_deactivate_all(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp_server_transition_to_state(rdp, CONNECTION_STATE_CAPABILITIES_EXCHANGE);
Packit 1fb8d4
Packit 1fb8d4
	if (!rdp_send_demand_active(rdp))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	rdp->AwaitCapabilities = TRUE;
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
int rdp_server_transition_to_state(rdpRdp* rdp, int state)
Packit 1fb8d4
{
Packit 1fb8d4
	int status = 0;
Packit 1fb8d4
	freerdp_peer* client = NULL;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->state >= CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT)
Packit 1fb8d4
		client = rdp->context->peer;
Packit 1fb8d4
Packit 1fb8d4
	if (rdp->state < CONNECTION_STATE_ACTIVE)
Packit 1fb8d4
	{
Packit 1fb8d4
		if (client)
Packit 1fb8d4
			client->activated = FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	switch (state)
Packit 1fb8d4
	{
Packit 1fb8d4
		case CONNECTION_STATE_INITIAL:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_INITIAL;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_NEGO:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_NEGO;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_CONNECT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_CONNECT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_ERECT_DOMAIN:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_ERECT_DOMAIN;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_ATTACH_USER:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_ATTACH_USER;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MCS_CHANNEL_JOIN:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MCS_CHANNEL_JOIN;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_LICENSING:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_LICENSING;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_CAPABILITIES_EXCHANGE;
Packit 1fb8d4
			rdp->AwaitCapabilities = FALSE;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_FINALIZATION:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_FINALIZATION;
Packit 1fb8d4
			rdp->finalize_sc_pdus = 0;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case CONNECTION_STATE_ACTIVE:
Packit 1fb8d4
			rdp->state = CONNECTION_STATE_ACTIVE;
Packit 1fb8d4
			update_reset_state(rdp->update);
Packit 1fb8d4
Packit 1fb8d4
			if (client)
Packit 1fb8d4
			{
Packit 1fb8d4
				if (!client->connected)
Packit 1fb8d4
				{
Packit 1fb8d4
					/**
Packit 1fb8d4
					 * PostConnect should only be called once and should not
Packit 1fb8d4
					 * be called after a reactivation sequence.
Packit 1fb8d4
					 */
Packit 1fb8d4
					IFCALLRET(client->PostConnect, client->connected, client);
Packit 1fb8d4
Packit 1fb8d4
					if (!client->connected)
Packit 1fb8d4
						return -1;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				if (rdp->state >= CONNECTION_STATE_ACTIVE)
Packit 1fb8d4
				{
Packit 1fb8d4
					IFCALLRET(client->Activate, client->activated, client);
Packit 1fb8d4
Packit 1fb8d4
					if (!client->activated)
Packit 1fb8d4
						return -1;
Packit 1fb8d4
				}
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			status = -1;
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return status;
Packit 1fb8d4
}
Packit Service 5a9772
Packit Service 5a9772
const char* rdp_client_connection_state_string(int state)
Packit Service 5a9772
{
Packit Service 5a9772
	switch (state)
Packit Service 5a9772
	{
Packit Service 5a9772
		case CLIENT_STATE_INITIAL:
Packit Service 5a9772
			return "CLIENT_STATE_INITIAL";
Packit Service 5a9772
		case CLIENT_STATE_PRECONNECT_PASSED:
Packit Service 5a9772
			return "CLIENT_STATE_PRECONNECT_PASSED";
Packit Service 5a9772
		case CLIENT_STATE_POSTCONNECT_PASSED:
Packit Service 5a9772
			return "CLIENT_STATE_POSTCONNECT_PASSED";
Packit Service 5a9772
		default:
Packit Service 5a9772
			return "UNKNOWN";
Packit Service 5a9772
	}
Packit Service 5a9772
}
Packit Service 5a9772
Packit Service 5a9772
const char* rdp_server_connection_state_string(int state)
Packit Service 5a9772
{
Packit Service 5a9772
	switch (state)
Packit Service 5a9772
	{
Packit Service 5a9772
		case CONNECTION_STATE_INITIAL:
Packit Service 5a9772
			return "CONNECTION_STATE_INITIAL";
Packit Service 5a9772
		case CONNECTION_STATE_NEGO:
Packit Service 5a9772
			return "CONNECTION_STATE_NEGO";
Packit Service 5a9772
		case CONNECTION_STATE_NLA:
Packit Service 5a9772
			return "CONNECTION_STATE_NLA";
Packit Service 5a9772
		case CONNECTION_STATE_MCS_CONNECT:
Packit Service 5a9772
			return "CONNECTION_STATE_MCS_CONNECT";
Packit Service 5a9772
		case CONNECTION_STATE_MCS_ERECT_DOMAIN:
Packit Service 5a9772
			return "CONNECTION_STATE_MCS_ERECT_DOMAIN";
Packit Service 5a9772
		case CONNECTION_STATE_MCS_ATTACH_USER:
Packit Service 5a9772
			return "CONNECTION_STATE_MCS_ATTACH_USER";
Packit Service 5a9772
		case CONNECTION_STATE_MCS_CHANNEL_JOIN:
Packit Service 5a9772
			return "CONNECTION_STATE_MCS_CHANNEL_JOIN";
Packit Service 5a9772
		case CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT:
Packit Service 5a9772
			return "CONNECTION_STATE_RDP_SECURITY_COMMENCEMENT";
Packit Service 5a9772
		case CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE:
Packit Service 5a9772
			return "CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE";
Packit Service 5a9772
		case CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT:
Packit Service 5a9772
			return "CONNECTION_STATE_CONNECT_TIME_AUTO_DETECT";
Packit Service 5a9772
		case CONNECTION_STATE_LICENSING:
Packit Service 5a9772
			return "CONNECTION_STATE_LICENSING";
Packit Service 5a9772
		case CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING:
Packit Service 5a9772
			return "CONNECTION_STATE_MULTITRANSPORT_BOOTSTRAPPING";
Packit Service 5a9772
		case CONNECTION_STATE_CAPABILITIES_EXCHANGE:
Packit Service 5a9772
			return "CONNECTION_STATE_CAPABILITIES_EXCHANGE";
Packit Service 5a9772
		case CONNECTION_STATE_FINALIZATION:
Packit Service 5a9772
			return "CONNECTION_STATE_FINALIZATION";
Packit Service 5a9772
		case CONNECTION_STATE_ACTIVE:
Packit Service 5a9772
			return "CONNECTION_STATE_ACTIVE";
Packit Service 5a9772
		default:
Packit Service 5a9772
			return "UNKNOWN";
Packit Service 5a9772
	}
Packit Service 5a9772
}