2011-07-03 05:50:11 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-03 05:50:11 +04:00
|
|
|
* Network Transport Layer
|
|
|
|
*
|
|
|
|
* Copyright 2011 Vic Lee
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2023-10-16 13:59:19 +03:00
|
|
|
#include "settings.h"
|
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-15 05:46:51 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-11-27 05:15:48 +04:00
|
|
|
#include <winpr/synch.h>
|
2012-12-14 09:25:48 +04:00
|
|
|
#include <winpr/print.h>
|
2014-02-05 20:54:42 +04:00
|
|
|
#include <winpr/stream.h>
|
2014-12-12 17:08:39 +03:00
|
|
|
#include <winpr/winsock.h>
|
2017-01-09 18:43:28 +03:00
|
|
|
#include <winpr/crypto.h>
|
2012-11-15 05:46:51 +04:00
|
|
|
|
2014-08-19 20:26:39 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-12-14 09:25:48 +04:00
|
|
|
#include <freerdp/error.h>
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <freerdp/utils/ringbuffer.h>
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <openssl/bio.h>
|
2011-07-03 12:37:36 +04:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
2011-07-03 11:53:55 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2011-08-16 01:05:48 +04:00
|
|
|
#ifndef _WIN32
|
2011-08-16 23:35:29 +04:00
|
|
|
#include <netdb.h>
|
2011-08-16 01:05:48 +04:00
|
|
|
#include <sys/socket.h>
|
2014-07-02 17:15:46 +04:00
|
|
|
#endif /* _WIN32 */
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2023-01-10 16:47:16 +03:00
|
|
|
#ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
|
2014-05-21 19:32:14 +04:00
|
|
|
#include <valgrind/memcheck.h>
|
2011-08-16 01:05:48 +04:00
|
|
|
#endif
|
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#include "tpkt.h"
|
2011-08-01 20:19:39 +04:00
|
|
|
#include "fastpath.h"
|
2011-07-03 06:59:07 +04:00
|
|
|
#include "transport.h"
|
2013-12-21 03:26:07 +04:00
|
|
|
#include "rdp.h"
|
2017-08-18 20:17:17 +03:00
|
|
|
#include "proxy.h"
|
2021-07-29 13:33:22 +03:00
|
|
|
#include "utils.h"
|
2022-11-11 14:26:28 +03:00
|
|
|
#include "state.h"
|
2023-09-22 23:13:54 +03:00
|
|
|
#include "childsession.h"
|
2011-07-03 05:50:11 +04:00
|
|
|
|
2023-06-17 22:59:49 +03:00
|
|
|
#include "gateway/rdg.h"
|
|
|
|
#include "gateway/wst.h"
|
2023-06-26 20:24:58 +03:00
|
|
|
#include "gateway/arm.h"
|
2023-06-17 22:59:49 +03:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("core.transport")
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2011-07-03 14:30:43 +04:00
|
|
|
#define BUFFER_SIZE 16384
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
struct rdp_transport
|
|
|
|
{
|
|
|
|
TRANSPORT_LAYER layer;
|
|
|
|
BIO* frontBio;
|
|
|
|
rdpRdg* rdg;
|
|
|
|
rdpTsg* tsg;
|
2023-06-17 22:59:49 +03:00
|
|
|
rdpWst* wst;
|
2021-09-06 12:01:36 +03:00
|
|
|
rdpTls* tls;
|
|
|
|
rdpContext* context;
|
|
|
|
rdpNla* nla;
|
|
|
|
void* ReceiveExtra;
|
|
|
|
wStream* ReceiveBuffer;
|
|
|
|
TransportRecv ReceiveCallback;
|
|
|
|
wStreamPool* ReceivePool;
|
|
|
|
HANDLE connectedEvent;
|
|
|
|
BOOL NlaMode;
|
2023-03-07 13:58:28 +03:00
|
|
|
BOOL RdstlsMode;
|
2023-03-08 04:06:47 +03:00
|
|
|
BOOL AadMode;
|
2021-09-06 12:01:36 +03:00
|
|
|
BOOL blocking;
|
|
|
|
BOOL GatewayEnabled;
|
|
|
|
CRITICAL_SECTION ReadLock;
|
|
|
|
CRITICAL_SECTION WriteLock;
|
|
|
|
ULONG written;
|
|
|
|
HANDLE rereadEvent;
|
|
|
|
BOOL haveMoreBytesToRead;
|
|
|
|
wLog* log;
|
|
|
|
rdpTransportIo io;
|
2022-11-15 17:04:13 +03:00
|
|
|
HANDLE ioEvent;
|
2022-11-16 12:55:58 +03:00
|
|
|
BOOL useIoEvent;
|
2023-06-23 01:49:27 +03:00
|
|
|
BOOL earlyUserAuth;
|
2021-09-06 12:01:36 +03:00
|
|
|
};
|
|
|
|
|
2024-09-09 12:42:14 +03:00
|
|
|
static void transport_ssl_cb(const SSL* ssl, int where, int ret)
|
2015-08-05 00:52:44 +03:00
|
|
|
{
|
2017-02-27 17:41:27 +03:00
|
|
|
if (where & SSL_CB_ALERT)
|
2015-08-05 00:52:44 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
rdpTransport* transport = (rdpTransport*)SSL_get_app_data(ssl);
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
2017-02-22 11:42:56 +03:00
|
|
|
|
2017-02-22 11:29:52 +03:00
|
|
|
switch (ret)
|
2016-06-20 12:49:54 +03:00
|
|
|
{
|
2017-02-27 17:41:27 +03:00
|
|
|
case (SSL3_AL_FATAL << 8) | SSL_AD_ACCESS_DENIED:
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
2021-09-06 12:01:36 +03:00
|
|
|
if (!freerdp_get_last_error(transport_get_context(transport)))
|
2017-02-22 11:29:52 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "ACCESS DENIED");
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_log(transport_get_context(transport),
|
2020-01-08 19:39:25 +03:00
|
|
|
FREERDP_ERROR_AUTHENTICATION_FAILED);
|
2017-02-22 11:29:52 +03:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
}
|
|
|
|
break;
|
2017-02-22 11:29:52 +03:00
|
|
|
|
2017-02-27 17:41:27 +03:00
|
|
|
case (SSL3_AL_FATAL << 8) | SSL_AD_INTERNAL_ERROR:
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
|
|
|
if (transport->NlaMode)
|
2017-02-22 11:29:52 +03:00
|
|
|
{
|
2021-09-06 12:01:36 +03:00
|
|
|
if (!freerdp_get_last_error(transport_get_context(transport)))
|
2019-11-06 17:24:51 +03:00
|
|
|
{
|
2023-09-21 10:24:41 +03:00
|
|
|
UINT32 kret = 0;
|
|
|
|
if (transport->nla)
|
|
|
|
kret = nla_get_error(transport->nla);
|
2021-04-16 16:47:53 +03:00
|
|
|
if (kret == 0)
|
|
|
|
kret = FREERDP_ERROR_CONNECT_PASSWORD_CERTAINLY_EXPIRED;
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_log(transport_get_context(transport), kret);
|
2019-11-06 17:24:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
break;
|
2017-02-22 11:29:52 +03:00
|
|
|
|
2017-02-27 17:41:27 +03:00
|
|
|
case (SSL3_AL_WARNING << 8) | SSL3_AD_CLOSE_NOTIFY:
|
|
|
|
break;
|
|
|
|
|
2017-02-22 11:29:52 +03:00
|
|
|
default:
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Print(transport->log, WLOG_WARN,
|
|
|
|
"Unhandled SSL error (where=%d, ret=%d [%s, %s])", where, ret,
|
2017-02-22 11:42:56 +03:00
|
|
|
SSL_alert_type_string_long(ret), SSL_alert_desc_string_long(ret));
|
2017-02-22 11:29:52 +03:00
|
|
|
break;
|
2019-11-06 17:24:51 +03:00
|
|
|
}
|
2016-06-20 12:49:54 +03:00
|
|
|
}
|
2015-08-05 00:52:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
wStream* transport_send_stream_init(rdpTransport* transport, size_t size)
|
2011-07-10 23:34:43 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
wStream* s = NULL;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!(s = StreamPool_Take(transport->ReceivePool, size)))
|
|
|
|
return NULL;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2015-03-30 18:15:45 +03:00
|
|
|
if (!Stream_EnsureCapacity(s, size))
|
2015-03-26 19:09:47 +03:00
|
|
|
{
|
|
|
|
Stream_Release(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2011-07-10 23:34:43 +04:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-02-14 18:14:13 +03:00
|
|
|
BOOL transport_attach(rdpTransport* transport, int sockfd)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
return IFCALLRESULT(FALSE, transport->io.TransportAttach, transport, sockfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_attach(rdpTransport* transport, int sockfd)
|
2011-08-18 19:15:28 +04:00
|
|
|
{
|
2018-10-25 14:08:00 +03:00
|
|
|
BIO* socketBio = NULL;
|
2024-01-23 18:49:54 +03:00
|
|
|
BIO* bufferedBio = NULL;
|
|
|
|
const rdpSettings* settings = NULL;
|
2022-05-06 01:05:16 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "Running peer without socket (sockfd=%d)", sockfd);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2022-05-06 01:05:16 +03:00
|
|
|
settings = context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
if (sockfd >= 0)
|
|
|
|
{
|
|
|
|
if (!freerdp_tcp_set_keep_alive_mode(settings, sockfd))
|
|
|
|
goto fail;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
socketBio = BIO_new(BIO_s_simple_socket());
|
2015-02-13 16:41:47 +03:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
if (!socketBio)
|
|
|
|
goto fail;
|
|
|
|
}
|
2015-02-14 18:14:13 +03:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
bufferedBio = BIO_new(BIO_s_buffered_socket());
|
2015-02-14 18:14:13 +03:00
|
|
|
if (!bufferedBio)
|
2018-10-25 14:08:00 +03:00
|
|
|
goto fail;
|
2015-02-14 18:14:13 +03:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
if (socketBio)
|
2024-02-23 12:42:18 +03:00
|
|
|
{
|
2022-10-10 12:55:17 +03:00
|
|
|
bufferedBio = BIO_push(bufferedBio, socketBio);
|
2024-02-23 12:42:18 +03:00
|
|
|
if (!bufferedBio)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* Attach the socket only when this function can no longer fail.
|
|
|
|
* This ensures solid ownership:
|
|
|
|
* - if this function fails, the caller is responsible to clean up
|
|
|
|
* - if this function is successful, the caller MUST NOT close the socket any more.
|
|
|
|
*/
|
|
|
|
BIO_set_fd(socketBio, sockfd, BIO_CLOSE);
|
|
|
|
}
|
2015-02-15 18:06:17 +03:00
|
|
|
transport->frontBio = bufferedBio;
|
2015-02-14 18:14:13 +03:00
|
|
|
return TRUE;
|
2018-10-25 14:08:00 +03:00
|
|
|
fail:
|
|
|
|
|
|
|
|
if (socketBio)
|
|
|
|
BIO_free_all(socketBio);
|
|
|
|
else
|
2022-07-14 12:46:25 +03:00
|
|
|
closesocket(sockfd);
|
2018-10-25 14:08:00 +03:00
|
|
|
|
|
|
|
return FALSE;
|
2011-08-18 19:15:28 +04:00
|
|
|
}
|
|
|
|
|
2021-07-29 13:33:22 +03:00
|
|
|
BOOL transport_connect_rdp(rdpTransport* transport)
|
2021-01-22 15:12:25 +03:00
|
|
|
{
|
2021-07-29 13:33:22 +03:00
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
2021-01-22 15:12:25 +03:00
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
switch (utils_authenticate(transport_get_context(transport)->instance, AUTH_RDP, FALSE))
|
2021-01-22 15:12:25 +03:00
|
|
|
{
|
2021-07-29 13:33:22 +03:00
|
|
|
case AUTH_SKIP:
|
|
|
|
case AUTH_SUCCESS:
|
|
|
|
case AUTH_NO_CREDENTIALS:
|
2021-01-22 15:12:25 +03:00
|
|
|
return TRUE;
|
2023-02-13 14:37:17 +03:00
|
|
|
case AUTH_CANCELLED:
|
|
|
|
freerdp_set_last_error_if_not(transport_get_context(transport),
|
|
|
|
FREERDP_ERROR_CONNECT_CANCELLED);
|
|
|
|
return FALSE;
|
2021-07-29 13:33:22 +03:00
|
|
|
default:
|
2021-01-22 15:12:25 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_connect_tls(rdpTransport* transport)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
const rdpSettings* settings = NULL;
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2021-01-22 15:12:25 +03:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2021-01-22 15:12:25 +03:00
|
|
|
/* Only prompt for password if we use TLS (NLA also calls this function) */
|
2022-03-23 15:18:35 +03:00
|
|
|
if (settings->SelectedProtocol == PROTOCOL_SSL)
|
2021-01-22 15:12:25 +03:00
|
|
|
{
|
2023-08-15 15:06:35 +03:00
|
|
|
switch (utils_authenticate(context->instance, AUTH_TLS, FALSE))
|
2021-07-29 13:33:22 +03:00
|
|
|
{
|
|
|
|
case AUTH_SKIP:
|
|
|
|
case AUTH_SUCCESS:
|
|
|
|
case AUTH_NO_CREDENTIALS:
|
|
|
|
break;
|
2023-02-13 14:37:17 +03:00
|
|
|
case AUTH_CANCELLED:
|
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
|
|
|
return FALSE;
|
2021-07-29 13:33:22 +03:00
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
2021-01-22 15:12:25 +03:00
|
|
|
}
|
|
|
|
|
2020-09-02 14:37:04 +03:00
|
|
|
return IFCALLRESULT(FALSE, transport->io.TLSConnect, transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_connect_tls(rdpTransport* transport)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int tlsStatus = 0;
|
2015-02-13 00:22:25 +03:00
|
|
|
rdpTls* tls = NULL;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpContext* context = NULL;
|
|
|
|
rdpSettings* settings = NULL;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
context = transport_get_context(transport);
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = context->settings;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(settings);
|
2014-03-25 23:19:52 +04:00
|
|
|
|
2024-06-27 09:40:05 +03:00
|
|
|
if (!(tls = freerdp_tls_new(context)))
|
2015-05-05 20:45:34 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
transport->tls = tls;
|
|
|
|
|
2015-02-11 22:27:29 +03:00
|
|
|
if (transport->GatewayEnabled)
|
2013-10-24 22:13:41 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_TSG_TLS;
|
2014-05-21 19:32:14 +04:00
|
|
|
else
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
2015-02-11 22:27:29 +03:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
tls->hostname = settings->ServerHostname;
|
2023-06-12 18:00:04 +03:00
|
|
|
tls->serverName = settings->UserSpecifiedServerName;
|
2015-02-13 00:22:25 +03:00
|
|
|
tls->port = settings->ServerPort;
|
2012-11-15 08:21:00 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tls->port == 0)
|
|
|
|
tls->port = 3389;
|
2013-12-07 07:15:45 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
tls->isGatewayTransport = FALSE;
|
2023-01-14 05:25:26 +03:00
|
|
|
tlsStatus = freerdp_tls_connect(tls, transport->frontBio);
|
2014-04-02 00:23:27 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tlsStatus < 1)
|
2012-11-15 08:21:00 +04:00
|
|
|
{
|
2015-02-13 00:22:25 +03:00
|
|
|
if (tlsStatus < 0)
|
2014-04-02 00:23:27 +04:00
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_TLS_CONNECT_FAILED);
|
2014-04-02 00:23:27 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_CANCELLED);
|
2014-04-02 00:23:27 +04:00
|
|
|
}
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->frontBio = tls->bio;
|
2024-09-09 12:42:14 +03:00
|
|
|
BIO_callback_ctrl(tls->bio, BIO_CTRL_SET_CALLBACK, (BIO_info_cb*)transport_ssl_cb);
|
2015-08-05 00:52:44 +03:00
|
|
|
SSL_set_app_data(tls->ssl, transport);
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->frontBio)
|
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "unable to prepend a filtering TLS bio");
|
2012-11-15 08:21:00 +04:00
|
|
|
return FALSE;
|
2012-04-10 23:15:36 +04:00
|
|
|
}
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-04 03:27:02 +04:00
|
|
|
}
|
|
|
|
|
2023-06-23 01:49:27 +03:00
|
|
|
BOOL transport_connect_nla(rdpTransport* transport, BOOL earlyUserAuth)
|
2011-07-04 03:27:02 +04:00
|
|
|
{
|
2020-09-02 14:37:04 +03:00
|
|
|
rdpContext* context = NULL;
|
|
|
|
rdpSettings* settings = NULL;
|
|
|
|
rdpRdp* rdp = NULL;
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
context = transport_get_context(transport);
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(context);
|
2022-03-25 12:47:05 +03:00
|
|
|
|
|
|
|
settings = context->settings;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(settings);
|
2022-03-25 12:47:05 +03:00
|
|
|
|
|
|
|
rdp = context->rdp;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(rdp);
|
|
|
|
|
2012-11-15 05:46:51 +04:00
|
|
|
if (!transport_connect_tls(transport))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-04 03:27:02 +04:00
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (!settings->Authentication)
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-18 21:07:52 +04:00
|
|
|
|
2020-05-12 10:09:13 +03:00
|
|
|
nla_free(rdp->nla);
|
2022-03-25 12:47:05 +03:00
|
|
|
rdp->nla = nla_new(context, transport);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (!rdp->nla)
|
|
|
|
return FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2023-06-23 01:49:27 +03:00
|
|
|
nla_set_early_user_auth(rdp->nla, earlyUserAuth);
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
transport_set_nla_mode(transport, TRUE);
|
2011-07-07 21:37:48 +04:00
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (settings->AuthenticationServiceClass)
|
|
|
|
{
|
2022-09-06 06:16:21 +03:00
|
|
|
if (!nla_set_service_principal(rdp->nla, settings->AuthenticationServiceClass,
|
2022-10-14 20:39:12 +03:00
|
|
|
freerdp_settings_get_server_name(settings)))
|
2015-02-16 00:04:59 +03:00
|
|
|
return FALSE;
|
2014-02-12 09:43:02 +04:00
|
|
|
}
|
|
|
|
|
2015-02-16 00:04:59 +03:00
|
|
|
if (nla_client_begin(rdp->nla) < 0)
|
2011-07-07 21:37:48 +04:00
|
|
|
{
|
2018-05-04 10:10:31 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "NLA begin failed");
|
|
|
|
|
2020-01-09 12:34:27 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
|
2014-03-21 02:19:54 +04:00
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-07 21:37:48 +04:00
|
|
|
}
|
|
|
|
|
2022-11-15 16:19:54 +03:00
|
|
|
return rdp_client_transition_to_state(rdp, CONNECTION_STATE_NLA);
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
|
|
|
|
2023-03-07 13:58:28 +03:00
|
|
|
BOOL transport_connect_rdstls(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
rdpRdstls* rdstls = NULL;
|
|
|
|
rdpContext* context = NULL;
|
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
|
|
|
context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
if (!transport_connect_tls(transport))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rdstls = rdstls_new(context, transport);
|
2023-03-08 16:09:12 +03:00
|
|
|
if (!rdstls)
|
|
|
|
goto fail;
|
|
|
|
|
2023-03-07 13:58:28 +03:00
|
|
|
transport_set_rdstls_mode(transport, TRUE);
|
|
|
|
|
|
|
|
if (rdstls_authenticate(rdstls) < 0)
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "RDSTLS authentication failed");
|
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
transport_set_rdstls_mode(transport, FALSE);
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
rdstls_free(rdstls);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
BOOL transport_connect_aad(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
rdpContext* context = NULL;
|
|
|
|
rdpSettings* settings = NULL;
|
|
|
|
rdpRdp* rdp = NULL;
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
settings = context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
|
|
|
|
rdp = context->rdp;
|
|
|
|
WINPR_ASSERT(rdp);
|
|
|
|
|
|
|
|
if (!transport_connect_tls(transport))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!settings->Authentication)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (!rdp->aad)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
transport_set_aad_mode(transport, TRUE);
|
|
|
|
|
|
|
|
if (aad_client_begin(rdp->aad) < 0)
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "AAD begin failed");
|
|
|
|
|
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_AUTHENTICATION_FAILED);
|
|
|
|
|
|
|
|
transport_set_aad_mode(transport, FALSE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rdp_client_transition_to_state(rdp, CONNECTION_STATE_AAD);
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:41:21 +03:00
|
|
|
BOOL transport_connect(rdpTransport* transport, const char* hostname, UINT16 port, DWORD timeout)
|
2012-03-26 20:20:38 +04:00
|
|
|
{
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL status = FALSE;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpSettings* settings = NULL;
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2024-01-23 18:49:54 +03:00
|
|
|
BOOL rpcFallback = 0;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
WINPR_ASSERT(context);
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(hostname);
|
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = context->settings;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(settings);
|
2022-03-23 15:18:35 +03:00
|
|
|
|
|
|
|
rpcFallback = !settings->GatewayHttpTransport;
|
2013-03-27 21:03:41 +04:00
|
|
|
|
2014-03-24 22:44:18 +04:00
|
|
|
if (transport->GatewayEnabled)
|
2012-03-26 20:20:38 +04:00
|
|
|
{
|
2023-06-17 22:59:49 +03:00
|
|
|
if (settings->GatewayUrl)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(!transport->wst);
|
|
|
|
transport->wst = wst_new(context);
|
|
|
|
|
|
|
|
if (!transport->wst)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
status = wst_connect(transport->wst, timeout);
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
|
|
|
transport->frontBio = wst_get_front_bio_and_take_ownership(transport->wst);
|
|
|
|
WINPR_ASSERT(transport->frontBio);
|
|
|
|
BIO_set_nonblock(transport->frontBio, 0);
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wst_free(transport->wst);
|
|
|
|
transport->wst = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-03-19 18:44:47 +03:00
|
|
|
if (!status && settings->GatewayHttpTransport)
|
2015-03-17 21:54:16 +03:00
|
|
|
{
|
2023-06-17 22:59:49 +03:00
|
|
|
WINPR_ASSERT(!transport->rdg);
|
2018-10-24 16:42:44 +03:00
|
|
|
transport->rdg = rdg_new(context);
|
2015-03-18 23:13:32 +03:00
|
|
|
|
2015-03-19 18:44:47 +03:00
|
|
|
if (!transport->rdg)
|
2015-03-18 23:13:32 +03:00
|
|
|
return FALSE;
|
2015-03-19 18:44:47 +03:00
|
|
|
|
2018-04-18 03:05:20 +03:00
|
|
|
status = rdg_connect(transport->rdg, timeout, &rpcFallback);
|
2015-03-19 18:44:47 +03:00
|
|
|
|
|
|
|
if (status)
|
|
|
|
{
|
2018-11-13 14:22:09 +03:00
|
|
|
transport->frontBio = rdg_get_front_bio_and_take_ownership(transport->rdg);
|
2022-04-29 11:03:09 +03:00
|
|
|
WINPR_ASSERT(transport->frontBio);
|
2015-03-19 18:44:47 +03:00
|
|
|
BIO_set_nonblock(transport->frontBio, 0);
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
status = TRUE;
|
2015-03-18 23:13:32 +03:00
|
|
|
}
|
2015-03-19 18:44:47 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
rdg_free(transport->rdg);
|
|
|
|
transport->rdg = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2018-04-18 03:05:20 +03:00
|
|
|
if (!status && settings->GatewayRpcTransport && rpcFallback)
|
2015-03-19 18:44:47 +03:00
|
|
|
{
|
2023-06-17 22:59:49 +03:00
|
|
|
WINPR_ASSERT(!transport->tsg);
|
2015-03-18 23:13:32 +03:00
|
|
|
transport->tsg = tsg_new(transport);
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-18 23:13:32 +03:00
|
|
|
if (!transport->tsg)
|
|
|
|
return FALSE;
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2019-09-23 12:26:55 +03:00
|
|
|
/* Reset error condition from RDG */
|
2020-01-08 19:39:25 +03:00
|
|
|
freerdp_set_last_error_log(context, FREERDP_ERROR_SUCCESS);
|
2015-03-19 18:44:47 +03:00
|
|
|
status = tsg_connect(transport->tsg, hostname, port, timeout);
|
2015-03-17 21:54:16 +03:00
|
|
|
|
2015-03-19 18:44:47 +03:00
|
|
|
if (status)
|
|
|
|
{
|
2018-09-28 09:43:43 +03:00
|
|
|
transport->frontBio = tsg_get_bio(transport->tsg);
|
2015-03-19 18:44:47 +03:00
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
status = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tsg_free(transport->tsg);
|
|
|
|
transport->tsg = NULL;
|
|
|
|
}
|
2015-03-18 23:13:32 +03:00
|
|
|
}
|
2012-03-26 20:20:38 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT16 peerPort = 0;
|
|
|
|
const char* proxyHostname = NULL;
|
|
|
|
const char* proxyUsername = NULL;
|
|
|
|
const char* proxyPassword = NULL;
|
2019-11-06 17:24:51 +03:00
|
|
|
BOOL isProxyConnection =
|
|
|
|
proxy_prepare(settings, &proxyHostname, &peerPort, &proxyUsername, &proxyPassword);
|
2017-08-18 20:17:17 +03:00
|
|
|
|
2024-09-05 15:59:21 +03:00
|
|
|
rdpTransportLayer* layer = NULL;
|
2017-08-18 20:17:17 +03:00
|
|
|
if (isProxyConnection)
|
2024-09-05 15:59:21 +03:00
|
|
|
layer = transport_connect_layer(transport, proxyHostname, peerPort, timeout);
|
2017-08-18 20:17:17 +03:00
|
|
|
else
|
2024-09-05 15:59:21 +03:00
|
|
|
layer = transport_connect_layer(transport, hostname, port, timeout);
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2024-09-05 15:59:21 +03:00
|
|
|
if (!layer)
|
2015-02-13 22:26:02 +03:00
|
|
|
return FALSE;
|
2015-02-11 19:57:02 +03:00
|
|
|
|
2024-09-05 15:59:21 +03:00
|
|
|
if (!transport_attach_layer(transport, layer))
|
|
|
|
{
|
|
|
|
transport_layer_free(layer);
|
2015-04-28 18:00:41 +03:00
|
|
|
return FALSE;
|
2024-09-05 15:59:21 +03:00
|
|
|
}
|
2015-02-13 22:26:02 +03:00
|
|
|
|
2017-08-18 20:17:17 +03:00
|
|
|
if (isProxyConnection)
|
2018-04-29 22:25:28 +03:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!proxy_connect(settings, transport->frontBio, proxyUsername, proxyPassword,
|
|
|
|
hostname, port))
|
2018-04-29 22:25:28 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-08-18 20:17:17 +03:00
|
|
|
|
2015-02-13 22:26:02 +03:00
|
|
|
status = TRUE;
|
2012-03-26 20:20:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-09-22 23:13:54 +03:00
|
|
|
BOOL transport_connect_childsession(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
|
|
|
transport->frontBio = createChildSessionBio();
|
|
|
|
if (!transport->frontBio)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TSG;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_rdp(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2020-09-02 14:37:04 +03:00
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
/* RDP encryption */
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_tls(rdpTransport* transport)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
return IFCALLRESULT(FALSE, transport->io.TLSAccept, transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_accept_tls(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpSettings* settings = NULL;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
WINPR_ASSERT(context);
|
2021-09-03 09:00:24 +03:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
settings = context->settings;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(settings);
|
2015-02-11 22:27:29 +03:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
if (!transport->tls)
|
2024-06-27 09:40:05 +03:00
|
|
|
transport->tls = freerdp_tls_new(context);
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
transport->layer = TRANSPORT_LAYER_TLS;
|
|
|
|
|
2023-01-14 05:25:26 +03:00
|
|
|
if (!freerdp_tls_accept(transport->tls, transport->frontBio, settings))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->frontBio = transport->tls->bio;
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_accept_nla(rdpTransport* transport)
|
2011-08-19 09:35:29 +04:00
|
|
|
{
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpSettings* settings = NULL;
|
2022-03-23 15:18:35 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
settings = context->settings;
|
|
|
|
WINPR_ASSERT(settings);
|
|
|
|
|
2020-09-02 14:37:04 +03:00
|
|
|
if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
|
|
|
/* Network Level Authentication */
|
|
|
|
|
2013-11-08 02:37:58 +04:00
|
|
|
if (!settings->Authentication)
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (!transport->nla)
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
2022-03-25 12:47:05 +03:00
|
|
|
transport->nla = nla_new(context, transport);
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, TRUE);
|
|
|
|
}
|
2012-02-14 07:27:59 +04:00
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
if (nla_authenticate(transport->nla) < 0)
|
2012-02-14 07:27:59 +04:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "client authentication failure");
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2015-02-15 19:10:14 +03:00
|
|
|
nla_free(transport->nla);
|
|
|
|
transport->nla = NULL;
|
2023-01-14 05:25:26 +03:00
|
|
|
freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
|
|
|
|
TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
|
|
|
|
freerdp_tls_send_alert(transport->tls);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-02-14 07:27:59 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 19:10:14 +03:00
|
|
|
/* don't free nla module yet, we need to copy the credentials from it first */
|
2014-03-27 22:24:15 +04:00
|
|
|
transport_set_nla_mode(transport, FALSE);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-19 09:35:29 +04:00
|
|
|
}
|
|
|
|
|
2023-03-07 13:58:28 +03:00
|
|
|
BOOL transport_accept_rdstls(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
BOOL rc = FALSE;
|
|
|
|
rdpRdstls* rdstls = NULL;
|
|
|
|
rdpContext* context = NULL;
|
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
|
|
|
context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
if (!IFCALLRESULT(FALSE, transport->io.TLSAccept, transport))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
rdstls = rdstls_new(context, transport);
|
2023-03-08 20:01:28 +03:00
|
|
|
if (!rdstls)
|
|
|
|
goto fail;
|
|
|
|
|
2023-03-07 13:58:28 +03:00
|
|
|
transport_set_rdstls_mode(transport, TRUE);
|
|
|
|
|
|
|
|
if (rdstls_authenticate(rdstls) < 0)
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "client authentication failure");
|
|
|
|
freerdp_tls_set_alert_code(transport->tls, TLS_ALERT_LEVEL_FATAL,
|
|
|
|
TLS_ALERT_DESCRIPTION_ACCESS_DENIED);
|
|
|
|
freerdp_tls_send_alert(transport->tls);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
transport_set_rdstls_mode(transport, FALSE);
|
|
|
|
rc = TRUE;
|
|
|
|
fail:
|
|
|
|
rdstls_free(rdstls);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2017-02-22 11:42:56 +03:00
|
|
|
#define WLog_ERR_BIO(transport, biofunc, bio) \
|
2023-07-27 10:02:03 +03:00
|
|
|
transport_bio_error_log(transport, biofunc, bio, __FILE__, __func__, __LINE__)
|
2016-01-20 14:22:32 +03:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static void transport_bio_error_log(rdpTransport* transport, LPCSTR biofunc, BIO* bio, LPCSTR file,
|
|
|
|
LPCSTR func, DWORD line)
|
2016-01-20 14:22:32 +03:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
unsigned long sslerr = 0;
|
|
|
|
int saveerrno = 0;
|
|
|
|
DWORD level = 0;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2016-01-20 14:22:32 +03:00
|
|
|
saveerrno = errno;
|
2016-10-07 15:04:40 +03:00
|
|
|
level = WLOG_ERROR;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2017-02-22 11:42:56 +03:00
|
|
|
if (level < WLog_GetLogLevel(transport->log))
|
2016-01-20 14:22:32 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (ERR_peek_error() == 0)
|
|
|
|
{
|
2024-02-05 15:01:08 +03:00
|
|
|
char ebuffer[256] = { 0 };
|
2016-10-07 15:04:40 +03:00
|
|
|
const char* fmt = "%s returned a system error %d: %s";
|
2023-05-22 22:07:51 +03:00
|
|
|
if (saveerrno == 0)
|
|
|
|
fmt = "%s retries exceeded";
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
|
2024-02-05 15:01:08 +03:00
|
|
|
saveerrno, winpr_strerror(saveerrno, ebuffer, sizeof(ebuffer)));
|
2016-01-20 14:22:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:46:25 +03:00
|
|
|
while ((sslerr = ERR_get_error()))
|
|
|
|
{
|
|
|
|
char buf[120] = { 0 };
|
|
|
|
const char* fmt = "%s returned an error: %s";
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2022-07-14 12:46:25 +03:00
|
|
|
ERR_error_string_n(sslerr, buf, 120);
|
|
|
|
WLog_PrintMessage(transport->log, WLOG_MESSAGE_TEXT, level, line, file, func, fmt, biofunc,
|
|
|
|
buf);
|
|
|
|
}
|
2016-01-20 14:22:32 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 13:30:14 +03:00
|
|
|
static SSIZE_T transport_read_layer(rdpTransport* transport, BYTE* data, size_t bytes)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2019-02-07 16:22:28 +03:00
|
|
|
SSIZE_T read = 0;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpRdp* rdp = NULL;
|
|
|
|
rdpContext* context = NULL;
|
2021-09-03 09:00:24 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
rdp = context->rdp;
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(rdp);
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
if (!transport->frontBio || (bytes > SSIZE_MAX))
|
2014-05-30 21:08:00 +04:00
|
|
|
{
|
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
|
2014-05-30 21:08:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-05-30 23:06:07 +04:00
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
while (read < (SSIZE_T)bytes)
|
2012-12-09 03:27:08 +04:00
|
|
|
{
|
2019-02-07 16:22:28 +03:00
|
|
|
const SSIZE_T tr = (SSIZE_T)bytes - read;
|
|
|
|
int r = (int)((tr > INT_MAX) ? INT_MAX : tr);
|
2022-07-01 06:30:21 +03:00
|
|
|
ERR_clear_error();
|
2019-10-16 19:49:24 +03:00
|
|
|
int status = BIO_read(transport->frontBio, data + read, r);
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (freerdp_shall_disconnect_context(context))
|
2021-11-26 14:30:43 +03:00
|
|
|
return -1;
|
|
|
|
|
2014-06-02 05:37:20 +04:00
|
|
|
if (status <= 0)
|
2012-12-09 03:27:08 +04:00
|
|
|
{
|
2014-05-30 19:34:04 +04:00
|
|
|
if (!transport->frontBio || !BIO_should_retry(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
|
|
|
/* something unexpected happened, let's close */
|
2016-01-20 14:22:32 +03:00
|
|
|
if (!transport->frontBio)
|
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "BIO_read: transport->frontBio null");
|
2016-01-20 14:22:32 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_ERR_BIO(transport, "BIO_read", transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* non blocking will survive a partial read */
|
|
|
|
if (!transport->blocking)
|
|
|
|
return read;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
/* blocking means that we can't continue until we have read the number of requested
|
|
|
|
* bytes */
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_wait_read(transport->frontBio, 100) < 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_ERR_BIO(transport, "BIO_wait_read", transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
continue;
|
2012-12-09 03:27:08 +04:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2023-01-10 16:47:16 +03:00
|
|
|
#ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
|
2014-05-21 19:32:14 +04:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(data + read, bytes - read);
|
|
|
|
#endif
|
|
|
|
read += status;
|
2019-10-28 14:46:18 +03:00
|
|
|
rdp->inBytes += status;
|
2012-12-09 03:27:08 +04:00
|
|
|
}
|
2012-12-13 08:52:23 +04:00
|
|
|
|
2012-12-09 03:27:08 +04:00
|
|
|
return read;
|
2012-12-08 07:18:50 +04:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
/**
|
|
|
|
* @brief Tries to read toRead bytes from the specified transport
|
|
|
|
*
|
|
|
|
* Try to read toRead bytes from the transport to the stream.
|
2019-11-06 17:24:51 +03:00
|
|
|
* In case it was not possible to read toRead bytes 0 is returned. The stream is always advanced by
|
|
|
|
* the number of bytes read.
|
2014-07-22 21:14:43 +04:00
|
|
|
*
|
2014-07-28 15:12:01 +04:00
|
|
|
* The function assumes that the stream has enough capacity to hold the data.
|
2014-07-22 21:14:43 +04:00
|
|
|
*
|
|
|
|
* @param[in] transport rdpTransport
|
|
|
|
* @param[in] s wStream
|
|
|
|
* @param[in] toRead number of bytes to read
|
2014-07-28 15:12:01 +04:00
|
|
|
* @return < 0 on error; 0 if not enough data is available (non blocking mode); 1 toRead bytes read
|
2014-07-22 21:14:43 +04:00
|
|
|
*/
|
2019-11-06 17:24:51 +03:00
|
|
|
static SSIZE_T transport_read_layer_bytes(rdpTransport* transport, wStream* s, size_t toRead)
|
2014-07-22 21:14:43 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
SSIZE_T status = 0;
|
2020-09-02 14:37:04 +03:00
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
if (toRead > SSIZE_MAX)
|
|
|
|
return 0;
|
|
|
|
|
2020-09-02 14:37:04 +03:00
|
|
|
status = IFCALLRESULT(-1, transport->io.ReadBytes, transport, Stream_Pointer(s), toRead);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
if (status <= 0)
|
|
|
|
return status;
|
|
|
|
|
2019-02-07 16:22:28 +03:00
|
|
|
Stream_Seek(s, (size_t)status);
|
|
|
|
return status == (SSIZE_T)toRead ? 1 : 0;
|
2014-07-22 21:14:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-07-28 15:12:01 +04:00
|
|
|
* @brief Try to read a complete PDU (NLA, fast-path or tpkt) from the underlying transport.
|
2014-07-22 21:14:43 +04:00
|
|
|
*
|
|
|
|
* If possible a complete PDU is read, in case of non blocking transport this might not succeed.
|
|
|
|
* Except in case of an error the passed stream will point to the last byte read (correct
|
|
|
|
* position). When the pdu read is completed the stream is sealed and the pointer set to 0
|
|
|
|
*
|
|
|
|
* @param[in] transport rdpTransport
|
|
|
|
* @param[in] s wStream
|
2014-07-28 15:12:01 +04:00
|
|
|
* @return < 0 on error; 0 if not enough data is available (non blocking mode); > 0 number of
|
2014-07-22 21:14:43 +04:00
|
|
|
* bytes of the *complete* pdu read
|
|
|
|
*/
|
2014-07-22 13:19:38 +04:00
|
|
|
int transport_read_pdu(rdpTransport* transport, wStream* s)
|
2012-12-08 07:18:50 +04:00
|
|
|
{
|
2020-09-02 14:37:04 +03:00
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
return IFCALLRESULT(-1, transport->io.ReadPdu, transport, s);
|
|
|
|
}
|
|
|
|
|
2023-03-08 23:45:20 +03:00
|
|
|
static SSIZE_T parse_nla_mode_pdu(rdpTransport* transport, wStream* stream)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
SSIZE_T pduLength = 0;
|
|
|
|
wStream sbuffer = { 0 };
|
2023-03-09 00:30:38 +03:00
|
|
|
wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
|
2023-03-08 23:45:20 +03:00
|
|
|
/*
|
|
|
|
* In case NlaMode is set TSRequest package(s) are expected
|
|
|
|
* 0x30 = DER encoded data with these bits set:
|
|
|
|
* bit 6 P/C constructed
|
|
|
|
* bit 5 tag number - sequence
|
|
|
|
*/
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 typeEncoding = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
2020-09-02 14:37:04 +03:00
|
|
|
return 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
Stream_Read_UINT8(s, typeEncoding);
|
|
|
|
if (typeEncoding == 0x30)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
/* TSRequest (NLA) */
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 lengthEncoding = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
|
|
|
return 0;
|
|
|
|
Stream_Read_UINT8(s, lengthEncoding);
|
|
|
|
if (lengthEncoding & 0x80)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
if ((lengthEncoding & ~(0x80)) == 1)
|
2012-12-21 23:13:40 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 length = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
|
|
|
return 0;
|
|
|
|
Stream_Read_UINT8(s, length);
|
|
|
|
pduLength = length;
|
|
|
|
pduLength += 3;
|
|
|
|
}
|
|
|
|
else if ((lengthEncoding & ~(0x80)) == 2)
|
|
|
|
{
|
|
|
|
/* check for header bytes already was readed in previous calls */
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT16 length = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 2)
|
|
|
|
return 0;
|
|
|
|
Stream_Read_UINT16_BE(s, length);
|
|
|
|
pduLength = length;
|
|
|
|
pduLength += 4;
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "Error reading TSRequest!");
|
|
|
|
return -1;
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
|
|
|
}
|
2023-03-08 23:45:20 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pduLength = lengthEncoding;
|
|
|
|
pduLength += 2;
|
|
|
|
}
|
2012-12-21 23:13:40 +04:00
|
|
|
}
|
2023-03-08 23:45:20 +03:00
|
|
|
|
|
|
|
return pduLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SSIZE_T parse_default_mode_pdu(rdpTransport* transport, wStream* stream)
|
|
|
|
{
|
|
|
|
SSIZE_T pduLength = 0;
|
|
|
|
wStream sbuffer = { 0 };
|
2023-03-09 00:30:38 +03:00
|
|
|
wStream* s = Stream_StaticConstInit(&sbuffer, Stream_Buffer(stream), Stream_Length(stream));
|
2023-03-08 23:45:20 +03:00
|
|
|
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 version = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
|
|
|
return 0;
|
|
|
|
Stream_Read_UINT8(s, version);
|
|
|
|
if (version == 0x03)
|
2023-03-08 19:56:41 +03:00
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
/* TPKT header */
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT16 length = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 3)
|
|
|
|
return 0;
|
|
|
|
Stream_Seek(s, 1);
|
|
|
|
Stream_Read_UINT16_BE(s, length);
|
|
|
|
pduLength = length;
|
|
|
|
|
|
|
|
/* min and max values according to ITU-T Rec. T.123 (01/2007) section 8 */
|
|
|
|
if ((pduLength < 7) || (pduLength > 0xFFFF))
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "tpkt - invalid pduLength: %" PRIdz, pduLength);
|
|
|
|
return -1;
|
|
|
|
}
|
2023-03-08 19:56:41 +03:00
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
else
|
|
|
|
{
|
2023-03-08 23:45:20 +03:00
|
|
|
/* Fast-Path Header */
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 length1 = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
|
|
|
return 0;
|
|
|
|
Stream_Read_UINT8(s, length1);
|
|
|
|
if (length1 & 0x80)
|
2014-03-27 22:24:15 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT8 length2 = 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
2020-09-02 14:37:04 +03:00
|
|
|
return 0;
|
2023-03-08 23:45:20 +03:00
|
|
|
Stream_Read_UINT8(s, length2);
|
|
|
|
pduLength = ((length1 & 0x7F) << 8) | length2;
|
2014-03-27 22:24:15 +04:00
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
else
|
2023-03-08 23:45:20 +03:00
|
|
|
pduLength = length1;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2023-03-08 23:45:20 +03:00
|
|
|
/*
|
|
|
|
* fast-path has 7 bits for length so the maximum size, including headers is 0x8000
|
|
|
|
* The theoretical minimum fast-path PDU consists only of two header bytes plus one
|
|
|
|
* byte for data (e.g. fast-path input synchronize pdu)
|
|
|
|
*/
|
|
|
|
if (pduLength < 3 || pduLength > 0x8000)
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "fast path - invalid pduLength: %" PRIdz,
|
|
|
|
pduLength);
|
|
|
|
return -1;
|
2014-03-27 22:24:15 +04:00
|
|
|
}
|
2012-12-08 07:18:50 +04:00
|
|
|
}
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2023-03-08 23:45:20 +03:00
|
|
|
return pduLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSIZE_T transport_parse_pdu(rdpTransport* transport, wStream* s, BOOL* incomplete)
|
|
|
|
{
|
|
|
|
size_t pduLength = 0;
|
|
|
|
|
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (incomplete)
|
|
|
|
*incomplete = TRUE;
|
|
|
|
|
2023-03-09 00:30:38 +03:00
|
|
|
Stream_SealLength(s);
|
2023-03-08 23:45:20 +03:00
|
|
|
if (transport->NlaMode)
|
|
|
|
pduLength = parse_nla_mode_pdu(transport, s);
|
|
|
|
else if (transport->RdstlsMode)
|
|
|
|
pduLength = rdstls_parse_pdu(transport->log, s);
|
|
|
|
else
|
|
|
|
pduLength = parse_default_mode_pdu(transport, s);
|
|
|
|
|
|
|
|
if (pduLength == 0)
|
|
|
|
return pduLength;
|
|
|
|
|
2023-03-09 00:30:38 +03:00
|
|
|
const size_t len = Stream_Length(s);
|
2023-03-08 23:45:20 +03:00
|
|
|
if (len > pduLength)
|
2020-09-02 14:37:04 +03:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (incomplete)
|
2023-03-08 23:45:20 +03:00
|
|
|
*incomplete = len < pduLength;
|
2020-09-02 14:37:04 +03:00
|
|
|
|
|
|
|
return pduLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_default_read_pdu(rdpTransport* transport, wStream* s)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BOOL incomplete = 0;
|
|
|
|
SSIZE_T status = 0;
|
|
|
|
size_t pduLength = 0;
|
|
|
|
size_t position = 0;
|
2020-09-02 14:37:04 +03:00
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
/* RDS AAD Auth PDUs have no length indicator. We need to determine the end of the PDU by
|
|
|
|
* reading in one byte at a time until we encounter the terminating null byte */
|
|
|
|
if (transport->AadMode)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
2023-03-08 17:09:14 +03:00
|
|
|
BYTE c = '\0';
|
2023-03-08 17:00:13 +03:00
|
|
|
do
|
2023-03-08 04:06:47 +03:00
|
|
|
{
|
2023-03-08 17:00:13 +03:00
|
|
|
const int rc = transport_read_layer(transport, &c, 1);
|
2023-03-08 04:06:47 +03:00
|
|
|
if (rc != 1)
|
|
|
|
return rc;
|
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 1))
|
|
|
|
return -1;
|
2023-03-08 17:00:13 +03:00
|
|
|
Stream_Write_UINT8(s, c);
|
|
|
|
} while (c != '\0');
|
2020-09-02 14:37:04 +03:00
|
|
|
}
|
2023-06-23 01:49:27 +03:00
|
|
|
else if (transport->earlyUserAuth)
|
|
|
|
{
|
|
|
|
if (!Stream_EnsureCapacity(s, 4))
|
|
|
|
return -1;
|
|
|
|
const int rc = transport_read_layer_bytes(transport, s, 4);
|
|
|
|
if (rc != 1)
|
|
|
|
return rc;
|
|
|
|
}
|
2023-03-08 04:06:47 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Read in pdu length */
|
|
|
|
status = transport_parse_pdu(transport, s, &incomplete);
|
|
|
|
while ((status == 0) && incomplete)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int rc = 0;
|
2023-03-08 04:06:47 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(s, 1))
|
|
|
|
return -1;
|
|
|
|
rc = transport_read_layer_bytes(transport, s, 1);
|
|
|
|
if (rc != 1)
|
|
|
|
return rc;
|
|
|
|
status = transport_parse_pdu(transport, s, &incomplete);
|
|
|
|
}
|
2020-09-02 14:37:04 +03:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
if (status < 0)
|
|
|
|
return -1;
|
2020-09-02 14:37:04 +03:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
pduLength = (size_t)status;
|
2020-09-02 14:37:04 +03:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
/* Read in rest of the PDU */
|
|
|
|
if (!Stream_EnsureCapacity(s, pduLength))
|
|
|
|
return -1;
|
2020-09-02 14:37:04 +03:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
position = Stream_GetPosition(s);
|
|
|
|
if (position > pduLength)
|
|
|
|
return -1;
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
status = transport_read_layer_bytes(transport, s, pduLength - Stream_GetPosition(s));
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
if (status != 1)
|
|
|
|
return status;
|
2012-12-08 07:43:53 +04:00
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
if (Stream_GetPosition(s) >= pduLength)
|
|
|
|
WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), pduLength,
|
|
|
|
WLOG_PACKET_INBOUND);
|
|
|
|
}
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2014-07-22 21:14:43 +04:00
|
|
|
Stream_SealLength(s);
|
|
|
|
Stream_SetPosition(s, 0);
|
|
|
|
return Stream_Length(s);
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int transport_write(rdpTransport* transport, wStream* s)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
|
|
|
if (!transport)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return IFCALLRESULT(-1, transport->io.WritePdu, transport, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_default_write(rdpTransport* transport, wStream* s)
|
2011-07-03 14:30:43 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
2013-05-15 20:14:26 +04:00
|
|
|
int status = -1;
|
2015-04-14 13:49:01 +03:00
|
|
|
int writtenlength = 0;
|
2024-01-23 18:49:54 +03:00
|
|
|
rdpRdp* rdp = NULL;
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(context);
|
2015-02-15 18:06:17 +03:00
|
|
|
|
2018-10-02 11:28:28 +03:00
|
|
|
if (!s)
|
2016-08-22 12:17:11 +03:00
|
|
|
return -1;
|
|
|
|
|
2022-06-24 17:42:40 +03:00
|
|
|
Stream_AddRef(s);
|
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
rdp = context->rdp;
|
2020-09-18 09:47:56 +03:00
|
|
|
if (!rdp)
|
2018-10-02 11:28:28 +03:00
|
|
|
goto fail;
|
|
|
|
|
2021-09-17 10:07:40 +03:00
|
|
|
EnterCriticalSection(&(transport->WriteLock));
|
2016-08-22 12:17:11 +03:00
|
|
|
if (!transport->frontBio)
|
2021-09-17 10:07:40 +03:00
|
|
|
goto out_cleanup;
|
2016-08-22 12:17:11 +03:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
length = Stream_GetPosition(s);
|
2015-04-14 13:49:01 +03:00
|
|
|
writtenlength = length;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_SetPosition(s, 0);
|
2011-08-03 05:34:23 +04:00
|
|
|
|
2013-10-09 07:43:57 +04:00
|
|
|
if (length > 0)
|
|
|
|
{
|
2019-10-28 14:46:18 +03:00
|
|
|
rdp->outBytes += length;
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_Packet(transport->log, WLOG_TRACE, Stream_Buffer(s), length, WLOG_PACKET_OUTBOUND);
|
2013-10-09 07:43:57 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
while (length > 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
2022-07-01 06:30:21 +03:00
|
|
|
ERR_clear_error();
|
2023-06-07 16:38:21 +03:00
|
|
|
status = BIO_write(transport->frontBio, Stream_ConstPointer(s), length);
|
2011-08-01 11:04:07 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (status <= 0)
|
2011-08-01 11:04:07 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
/* the buffered BIO that is at the end of the chain always says OK for writing,
|
|
|
|
* so a retry means that for any reason we need to read. The most probable
|
|
|
|
* is a SSL or TSG BIO in the chain.
|
|
|
|
*/
|
|
|
|
if (!BIO_should_retry(transport->frontBio))
|
2016-01-20 14:22:32 +03:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_ERR_BIO(transport, "BIO_should_retry", transport->frontBio);
|
2015-04-06 19:24:08 +03:00
|
|
|
goto out_cleanup;
|
2016-01-20 14:22:32 +03:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
|
|
|
/* non-blocking can live with blocked IOs */
|
2011-08-01 18:21:06 +04:00
|
|
|
if (!transport->blocking)
|
2016-01-20 14:22:32 +03:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_ERR_BIO(transport, "BIO_write", transport->frontBio);
|
2015-04-06 19:24:08 +03:00
|
|
|
goto out_cleanup;
|
2016-01-20 14:22:32 +03:00
|
|
|
}
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_wait_write(transport->frontBio, 100) < 0)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_ERR_BIO(transport, "BIO_wait_write", transport->frontBio);
|
2015-04-06 19:24:08 +03:00
|
|
|
status = -1;
|
|
|
|
goto out_cleanup;
|
2011-08-20 16:30:18 +04:00
|
|
|
}
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-21 12:23:50 +04:00
|
|
|
|
2022-03-23 15:18:35 +03:00
|
|
|
WINPR_ASSERT(context->settings);
|
|
|
|
if (transport->blocking || context->settings->WaitForOutputBufferFlush)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
while (BIO_write_blocked(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_wait_write(transport->frontBio, 100) < 0)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "error when selecting for write");
|
2015-04-06 19:24:08 +03:00
|
|
|
status = -1;
|
|
|
|
goto out_cleanup;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_flush(transport->frontBio) < 1)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "error when flushing outputBuffer");
|
2015-04-06 19:24:08 +03:00
|
|
|
status = -1;
|
|
|
|
goto out_cleanup;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
}
|
2011-08-01 11:04:07 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:42:30 +04:00
|
|
|
length -= status;
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, status);
|
2011-08-01 11:04:07 +04:00
|
|
|
}
|
2011-07-03 14:30:43 +04:00
|
|
|
|
2016-09-20 09:58:04 +03:00
|
|
|
transport->written += writtenlength;
|
2015-04-06 19:24:08 +03:00
|
|
|
out_cleanup:
|
2015-04-14 13:49:01 +03:00
|
|
|
|
2011-08-25 09:30:17 +04:00
|
|
|
if (status < 0)
|
|
|
|
{
|
|
|
|
/* A write error indicates that the peer has dropped the connection */
|
|
|
|
transport->layer = TRANSPORT_LAYER_CLOSED;
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
|
2011-08-25 09:30:17 +04:00
|
|
|
}
|
|
|
|
|
2013-11-08 00:14:59 +04:00
|
|
|
LeaveCriticalSection(&(transport->WriteLock));
|
2018-10-02 11:28:28 +03:00
|
|
|
fail:
|
|
|
|
Stream_Release(s);
|
2011-07-10 23:34:43 +04:00
|
|
|
return status;
|
2011-07-04 06:11:14 +04:00
|
|
|
}
|
|
|
|
|
2024-01-02 19:00:14 +03:00
|
|
|
BOOL transport_get_public_key(rdpTransport* transport, const BYTE** data, DWORD* length)
|
|
|
|
{
|
|
|
|
return IFCALLRESULT(FALSE, transport->io.GetPublicKey, transport, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_get_public_key(rdpTransport* transport, const BYTE** data,
|
|
|
|
DWORD* length)
|
|
|
|
{
|
|
|
|
rdpTls* tls = transport_get_tls(transport);
|
|
|
|
if (!tls)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*data = tls->PublicKey;
|
|
|
|
*length = tls->PublicKeyLength;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD transport_get_event_handles(rdpTransport* transport, HANDLE* events, DWORD count)
|
2011-08-20 16:30:18 +04:00
|
|
|
{
|
2022-11-15 17:04:13 +03:00
|
|
|
DWORD nCount = 0; /* always the reread Event */
|
2012-11-27 05:15:48 +04:00
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(events);
|
|
|
|
WINPR_ASSERT(count > 0);
|
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
if (events)
|
|
|
|
{
|
|
|
|
if (count < 1)
|
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "provided handles array is too small");
|
2016-05-03 18:24:07 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-15 17:04:13 +03:00
|
|
|
events[nCount++] = transport->rereadEvent;
|
|
|
|
|
2022-11-16 12:55:58 +03:00
|
|
|
if (transport->useIoEvent)
|
2022-11-15 17:04:13 +03:00
|
|
|
{
|
|
|
|
if (count < 2)
|
|
|
|
return 0;
|
|
|
|
events[nCount++] = transport->ioEvent;
|
|
|
|
}
|
2016-05-03 18:24:07 +03:00
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
if (!transport->GatewayEnabled)
|
2012-11-27 05:15:48 +04:00
|
|
|
{
|
2016-05-03 18:24:07 +03:00
|
|
|
if (events)
|
2015-04-21 13:24:50 +03:00
|
|
|
{
|
2021-09-03 09:00:24 +03:00
|
|
|
if (nCount >= count)
|
2016-05-03 18:24:07 +03:00
|
|
|
{
|
2017-02-22 11:42:56 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR,
|
2023-01-23 14:03:18 +03:00
|
|
|
"provided handles array is too small (count=%" PRIu32 " nCount=%" PRIu32
|
|
|
|
")",
|
|
|
|
count, nCount);
|
2015-07-09 13:20:22 +03:00
|
|
|
return 0;
|
2016-05-03 18:24:07 +03:00
|
|
|
}
|
2016-09-20 09:58:04 +03:00
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
if (transport->frontBio)
|
2016-05-03 18:24:07 +03:00
|
|
|
{
|
2022-11-15 17:04:13 +03:00
|
|
|
if (BIO_get_event(transport->frontBio, &events[nCount]) != 1)
|
2021-09-03 09:00:24 +03:00
|
|
|
{
|
2023-01-23 14:03:18 +03:00
|
|
|
WLog_Print(transport->log, WLOG_ERROR, "error getting the frontBio handle");
|
2021-09-03 09:00:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nCount++;
|
2016-05-03 18:24:07 +03:00
|
|
|
}
|
2015-04-21 13:24:50 +03:00
|
|
|
}
|
2012-11-27 05:15:48 +04:00
|
|
|
}
|
2015-02-15 18:06:17 +03:00
|
|
|
else
|
2013-03-28 04:06:10 +04:00
|
|
|
{
|
2015-03-17 21:54:16 +03:00
|
|
|
if (transport->rdg)
|
|
|
|
{
|
2023-06-05 13:16:57 +03:00
|
|
|
const DWORD tmp =
|
|
|
|
rdg_get_event_handles(transport->rdg, &events[nCount], count - nCount);
|
2015-04-21 13:24:50 +03:00
|
|
|
|
|
|
|
if (tmp == 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
nCount += tmp;
|
2015-03-17 21:54:16 +03:00
|
|
|
}
|
|
|
|
else if (transport->tsg)
|
|
|
|
{
|
2023-06-05 13:16:57 +03:00
|
|
|
const DWORD tmp =
|
|
|
|
tsg_get_event_handles(transport->tsg, &events[nCount], count - nCount);
|
2015-04-21 13:24:50 +03:00
|
|
|
|
|
|
|
if (tmp == 0)
|
|
|
|
return 0;
|
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
nCount += tmp;
|
2015-03-17 21:54:16 +03:00
|
|
|
}
|
2023-06-17 22:59:49 +03:00
|
|
|
else if (transport->wst)
|
|
|
|
{
|
|
|
|
const DWORD tmp =
|
|
|
|
wst_get_event_handles(transport->wst, &events[nCount], count - nCount);
|
|
|
|
|
|
|
|
if (tmp == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nCount += tmp;
|
|
|
|
}
|
2013-03-28 04:06:10 +04:00
|
|
|
}
|
2015-02-15 18:06:17 +03:00
|
|
|
|
|
|
|
return nCount;
|
2011-08-20 16:30:18 +04:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:04:58 +03:00
|
|
|
#if defined(WITH_FREERDP_DEPRECATED)
|
2015-02-15 18:06:17 +03:00
|
|
|
void transport_get_fds(rdpTransport* transport, void** rfds, int* rcount)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
DWORD nCount = 0;
|
2021-08-25 12:14:35 +03:00
|
|
|
HANDLE events[MAXIMUM_WAIT_OBJECTS] = { 0 };
|
2021-09-03 09:00:24 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(rfds);
|
|
|
|
WINPR_ASSERT(rcount);
|
|
|
|
|
2021-08-25 12:14:35 +03:00
|
|
|
nCount = transport_get_event_handles(transport, events, ARRAYSIZE(events));
|
2016-05-03 18:24:07 +03:00
|
|
|
*rcount = nCount + 1;
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2024-01-30 12:25:38 +03:00
|
|
|
for (DWORD index = 0; index < nCount; index++)
|
2013-05-01 01:16:38 +04:00
|
|
|
{
|
2015-04-21 13:24:50 +03:00
|
|
|
rfds[index] = GetEventWaitObject(events[index]);
|
2013-05-01 01:16:38 +04:00
|
|
|
}
|
2016-05-03 18:24:07 +03:00
|
|
|
|
2018-03-06 15:58:40 +03:00
|
|
|
rfds[nCount] = GetEventWaitObject(transport->rereadEvent);
|
2013-05-01 01:16:38 +04:00
|
|
|
}
|
2021-08-24 16:04:58 +03:00
|
|
|
#endif
|
2013-05-01 01:16:38 +04:00
|
|
|
|
2015-02-17 18:54:39 +03:00
|
|
|
BOOL transport_is_write_blocked(rdpTransport* transport)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
2021-09-17 10:07:40 +03:00
|
|
|
WINPR_ASSERT(transport->frontBio);
|
2015-02-15 18:06:17 +03:00
|
|
|
return BIO_write_blocked(transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-17 18:54:39 +03:00
|
|
|
int transport_drain_output_buffer(rdpTransport* transport)
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-11 19:57:02 +03:00
|
|
|
BOOL status = FALSE;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
2021-09-17 10:07:40 +03:00
|
|
|
WINPR_ASSERT(transport->frontBio);
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_write_blocked(transport->frontBio))
|
2014-05-21 19:32:14 +04:00
|
|
|
{
|
2015-02-15 18:06:17 +03:00
|
|
|
if (BIO_flush(transport->frontBio) < 1)
|
2014-05-21 19:32:14 +04:00
|
|
|
return -1;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
status |= BIO_write_blocked(transport->frontBio);
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2015-02-11 19:57:02 +03:00
|
|
|
return status;
|
2014-05-21 19:32:14 +04:00
|
|
|
}
|
|
|
|
|
2013-11-04 01:25:56 +04:00
|
|
|
int transport_check_fds(rdpTransport* transport)
|
2011-07-03 05:50:11 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
int status = 0;
|
|
|
|
state_run_t recv_status = STATE_RUN_FAILED;
|
|
|
|
wStream* received = NULL;
|
2022-03-23 15:18:35 +03:00
|
|
|
rdpContext* context = transport_get_context(transport);
|
2011-08-01 08:43:53 +04:00
|
|
|
|
2021-09-06 12:01:36 +03:00
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
2019-12-27 13:58:27 +03:00
|
|
|
if (transport->layer == TRANSPORT_LAYER_CLOSED)
|
|
|
|
{
|
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "transport_check_fds: transport layer closed");
|
2021-09-06 12:01:36 +03:00
|
|
|
freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_TRANSPORT_FAILED);
|
2019-12-27 13:58:27 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:59:28 +03:00
|
|
|
/**
|
|
|
|
* Note: transport_read_pdu tries to read one PDU from
|
|
|
|
* the transport layer.
|
|
|
|
* The ReceiveBuffer might have a position > 0 in case of a non blocking
|
|
|
|
* transport. If transport_read_pdu returns 0 the pdu couldn't be read at
|
|
|
|
* this point.
|
|
|
|
* Note that transport->ReceiveBuffer is replaced after each iteration
|
|
|
|
* of this loop with a fresh stream instance from a pool.
|
|
|
|
*/
|
|
|
|
if ((status = transport_read_pdu(transport, transport->ReceiveBuffer)) <= 0)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
2022-11-30 16:59:28 +03:00
|
|
|
if (status < 0)
|
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "transport_check_fds: transport_read_pdu() - %i",
|
|
|
|
status);
|
|
|
|
if (transport->haveMoreBytesToRead)
|
2017-11-06 16:02:22 +03:00
|
|
|
{
|
2022-11-30 16:59:28 +03:00
|
|
|
transport->haveMoreBytesToRead = FALSE;
|
|
|
|
ResetEvent(transport->rereadEvent);
|
2017-09-19 11:18:41 +03:00
|
|
|
}
|
2022-11-30 16:59:28 +03:00
|
|
|
return status;
|
|
|
|
}
|
2017-09-19 11:18:41 +03:00
|
|
|
|
2022-11-30 16:59:28 +03:00
|
|
|
received = transport->ReceiveBuffer;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2022-11-30 16:59:28 +03:00
|
|
|
if (!(transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0)))
|
|
|
|
return -1;
|
2016-05-03 18:24:07 +03:00
|
|
|
|
2022-11-30 16:59:28 +03:00
|
|
|
/**
|
|
|
|
* status:
|
|
|
|
* -1: error
|
|
|
|
* 0: success
|
|
|
|
* 1: redirection
|
|
|
|
*/
|
|
|
|
WINPR_ASSERT(transport->ReceiveCallback);
|
|
|
|
recv_status = transport->ReceiveCallback(transport, received, transport->ReceiveExtra);
|
|
|
|
Stream_Release(received);
|
|
|
|
|
|
|
|
if (state_run_failed(recv_status))
|
|
|
|
{
|
|
|
|
char buffer[64] = { 0 };
|
|
|
|
WLog_Print(transport->log, WLOG_ERROR,
|
|
|
|
"transport_check_fds: transport->ReceiveCallback() - %s",
|
|
|
|
state_run_result_string(recv_status, buffer, ARRAYSIZE(buffer)));
|
|
|
|
return -1;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2022-11-30 16:59:28 +03:00
|
|
|
/* Run this again to be sure we consumed all input data.
|
|
|
|
* This will be repeated until a (not fully) received packet is in buffer
|
|
|
|
*/
|
|
|
|
if (!transport->haveMoreBytesToRead)
|
2016-05-03 18:24:07 +03:00
|
|
|
{
|
|
|
|
transport->haveMoreBytesToRead = TRUE;
|
2022-11-30 16:59:28 +03:00
|
|
|
SetEvent(transport->rereadEvent);
|
2016-05-03 18:24:07 +03:00
|
|
|
}
|
2022-11-30 16:59:28 +03:00
|
|
|
return recv_status;
|
2011-07-03 05:50:11 +04:00
|
|
|
}
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL transport_set_blocking_mode(rdpTransport* transport, BOOL blocking)
|
2011-08-01 08:43:53 +04:00
|
|
|
{
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2024-01-22 14:14:57 +03:00
|
|
|
return IFCALLRESULT(FALSE, transport->io.SetBlockingMode, transport, blocking);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_set_blocking_mode(rdpTransport* transport, BOOL blocking)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
2011-08-01 08:43:53 +04:00
|
|
|
transport->blocking = blocking;
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2022-10-10 12:55:17 +03:00
|
|
|
if (transport->frontBio)
|
|
|
|
{
|
|
|
|
if (!BIO_set_nonblock(transport->frontBio, blocking ? FALSE : TRUE))
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-11-28 03:34:00 +04:00
|
|
|
|
2015-02-13 17:27:54 +03:00
|
|
|
return TRUE;
|
2011-08-01 08:43:53 +04:00
|
|
|
}
|
|
|
|
|
2024-09-05 15:59:21 +03:00
|
|
|
rdpTransportLayer* transport_connect_layer(rdpTransport* transport, const char* hostname, int port,
|
|
|
|
DWORD timeout)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
|
|
|
|
return IFCALLRESULT(NULL, transport->io.ConnectLayer, transport, hostname, port, timeout);
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:42:14 +03:00
|
|
|
static rdpTransportLayer* transport_default_connect_layer(rdpTransport* transport,
|
|
|
|
const char* hostname, int port,
|
|
|
|
DWORD timeout)
|
2024-09-05 15:59:21 +03:00
|
|
|
{
|
|
|
|
rdpContext* context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
|
|
|
|
return freerdp_tcp_connect_layer(context, hostname, port, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_attach_layer(rdpTransport* transport, rdpTransportLayer* layer)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(layer);
|
|
|
|
|
|
|
|
return IFCALLRESULT(FALSE, transport->io.AttachLayer, transport, layer);
|
|
|
|
}
|
|
|
|
|
2024-09-09 12:42:14 +03:00
|
|
|
static BOOL transport_default_attach_layer(rdpTransport* transport, rdpTransportLayer* layer)
|
2024-09-05 15:59:21 +03:00
|
|
|
{
|
|
|
|
BIO* layerBio = BIO_new(BIO_s_transport_layer());
|
|
|
|
if (!layerBio)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
|
|
|
|
if (!bufferedBio)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
bufferedBio = BIO_push(bufferedBio, layerBio);
|
|
|
|
if (!bufferedBio)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
/* BIO takes over the layer reference at this point. */
|
|
|
|
BIO_set_data(layerBio, layer);
|
|
|
|
|
|
|
|
transport->frontBio = bufferedBio;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (layerBio)
|
|
|
|
BIO_free_all(layerBio);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-03-25 23:19:52 +04:00
|
|
|
void transport_set_gateway_enabled(rdpTransport* transport, BOOL GatewayEnabled)
|
2014-03-24 22:44:18 +04:00
|
|
|
{
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
2014-03-24 22:44:18 +04:00
|
|
|
transport->GatewayEnabled = GatewayEnabled;
|
|
|
|
}
|
|
|
|
|
2014-03-27 22:24:15 +04:00
|
|
|
void transport_set_nla_mode(rdpTransport* transport, BOOL NlaMode)
|
|
|
|
{
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(transport);
|
2014-03-27 22:24:15 +04:00
|
|
|
transport->NlaMode = NlaMode;
|
|
|
|
}
|
|
|
|
|
2023-03-07 13:58:28 +03:00
|
|
|
void transport_set_rdstls_mode(rdpTransport* transport, BOOL RdstlsMode)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->RdstlsMode = RdstlsMode;
|
|
|
|
}
|
|
|
|
|
2023-03-08 04:06:47 +03:00
|
|
|
void transport_set_aad_mode(rdpTransport* transport, BOOL AadMode)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->AadMode = AadMode;
|
|
|
|
}
|
|
|
|
|
2015-02-13 00:22:25 +03:00
|
|
|
BOOL transport_disconnect(rdpTransport* transport)
|
2020-09-02 14:37:04 +03:00
|
|
|
{
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
return IFCALLRESULT(FALSE, transport->io.TransportDisconnect, transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL transport_default_disconnect(rdpTransport* transport)
|
2015-02-13 00:22:25 +03:00
|
|
|
{
|
|
|
|
BOOL status = TRUE;
|
|
|
|
|
|
|
|
if (!transport)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-02-18 23:36:57 +03:00
|
|
|
if (transport->tls)
|
2015-02-13 00:22:25 +03:00
|
|
|
{
|
2023-01-14 05:25:26 +03:00
|
|
|
freerdp_tls_free(transport->tls);
|
2015-02-18 23:36:57 +03:00
|
|
|
transport->tls = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-18 23:36:57 +03:00
|
|
|
if (transport->frontBio)
|
2018-10-25 14:08:00 +03:00
|
|
|
BIO_free_all(transport->frontBio);
|
2015-02-18 23:36:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (transport->tsg)
|
|
|
|
{
|
|
|
|
tsg_free(transport->tsg);
|
|
|
|
transport->tsg = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:09:17 +03:00
|
|
|
if (transport->rdg)
|
|
|
|
{
|
|
|
|
rdg_free(transport->rdg);
|
|
|
|
transport->rdg = NULL;
|
|
|
|
}
|
|
|
|
|
2023-07-04 18:36:48 +03:00
|
|
|
if (transport->wst)
|
|
|
|
{
|
|
|
|
wst_free(transport->wst);
|
|
|
|
transport->wst = NULL;
|
|
|
|
}
|
|
|
|
|
2015-02-15 18:06:17 +03:00
|
|
|
transport->frontBio = NULL;
|
2015-02-13 00:22:25 +03:00
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
2023-06-23 01:49:27 +03:00
|
|
|
transport->earlyUserAuth = FALSE;
|
2015-02-13 00:22:25 +03:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2014-12-15 18:23:06 +03:00
|
|
|
rdpTransport* transport_new(rdpContext* context)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2020-09-02 14:37:04 +03:00
|
|
|
rdpTransport* transport = (rdpTransport*)calloc(1, sizeof(rdpTransport));
|
2011-07-03 23:34:15 +04:00
|
|
|
|
2021-09-03 09:00:24 +03:00
|
|
|
WINPR_ASSERT(context);
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport)
|
|
|
|
return NULL;
|
2013-03-28 04:06:10 +04:00
|
|
|
|
2017-02-22 11:42:56 +03:00
|
|
|
transport->log = WLog_Get(TAG);
|
|
|
|
|
|
|
|
if (!transport->log)
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
// transport->io.DataHandler = transport_data_handler;
|
|
|
|
transport->io.TCPConnect = freerdp_tcp_default_connect;
|
|
|
|
transport->io.TLSConnect = transport_default_connect_tls;
|
|
|
|
transport->io.TLSAccept = transport_default_accept_tls;
|
|
|
|
transport->io.TransportAttach = transport_default_attach;
|
|
|
|
transport->io.TransportDisconnect = transport_default_disconnect;
|
|
|
|
transport->io.ReadPdu = transport_default_read_pdu;
|
|
|
|
transport->io.WritePdu = transport_default_write;
|
|
|
|
transport->io.ReadBytes = transport_read_layer;
|
2024-01-02 19:00:14 +03:00
|
|
|
transport->io.GetPublicKey = transport_default_get_public_key;
|
2024-01-22 14:14:57 +03:00
|
|
|
transport->io.SetBlockingMode = transport_default_set_blocking_mode;
|
2024-09-05 15:59:21 +03:00
|
|
|
transport->io.ConnectLayer = transport_default_connect_layer;
|
|
|
|
transport->io.AttachLayer = transport_default_attach_layer;
|
2017-02-22 11:42:56 +03:00
|
|
|
|
2014-12-15 18:23:06 +03:00
|
|
|
transport->context = context;
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->ReceivePool = StreamPool_New(TRUE, BUFFER_SIZE);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->ReceivePool)
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2013-01-26 02:52:37 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
/* receive buffer for non-blocking read. */
|
|
|
|
transport->ReceiveBuffer = StreamPool_Take(transport->ReceivePool, 0);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport->ReceiveBuffer)
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2011-07-10 23:34:43 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
transport->connectedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!transport->connectedEvent || transport->connectedEvent == INVALID_HANDLE_VALUE)
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2011-09-04 00:36:27 +04:00
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
transport->rereadEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
2016-10-13 23:02:25 +03:00
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
if (!transport->rereadEvent || transport->rereadEvent == INVALID_HANDLE_VALUE)
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2016-05-03 18:24:07 +03:00
|
|
|
|
2022-11-15 17:04:13 +03:00
|
|
|
transport->ioEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
|
|
|
|
|
if (!transport->ioEvent || transport->ioEvent == INVALID_HANDLE_VALUE)
|
|
|
|
goto fail;
|
|
|
|
|
2016-05-03 18:24:07 +03:00
|
|
|
transport->haveMoreBytesToRead = FALSE;
|
2015-01-13 18:35:06 +03:00
|
|
|
transport->blocking = TRUE;
|
|
|
|
transport->GatewayEnabled = FALSE;
|
|
|
|
transport->layer = TRANSPORT_LAYER_TCP;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(transport->ReadLock), 4000))
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2014-08-19 20:26:39 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!InitializeCriticalSectionAndSpinCount(&(transport->WriteLock), 4000))
|
2020-09-02 14:37:04 +03:00
|
|
|
goto fail;
|
2011-07-03 23:34:15 +04:00
|
|
|
|
|
|
|
return transport;
|
2020-09-02 14:37:04 +03:00
|
|
|
fail:
|
2024-02-04 13:11:29 +03:00
|
|
|
WINPR_PRAGMA_DIAG_PUSH
|
|
|
|
WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
|
2020-09-02 14:37:04 +03:00
|
|
|
transport_free(transport);
|
2024-02-04 13:11:29 +03:00
|
|
|
WINPR_PRAGMA_DIAG_POP
|
2014-05-21 19:32:14 +04:00
|
|
|
return NULL;
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 21:37:48 +04:00
|
|
|
void transport_free(rdpTransport* transport)
|
2011-07-03 23:34:15 +04:00
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!transport)
|
|
|
|
return;
|
2013-11-12 18:04:26 +04:00
|
|
|
|
2015-01-12 15:32:59 +03:00
|
|
|
transport_disconnect(transport);
|
2013-04-13 01:05:42 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (transport->ReceiveBuffer)
|
|
|
|
Stream_Release(transport->ReceiveBuffer);
|
2013-02-15 05:39:56 +04:00
|
|
|
|
2020-05-12 10:09:13 +03:00
|
|
|
nla_free(transport->nla);
|
2014-05-21 19:32:14 +04:00
|
|
|
StreamPool_Free(transport->ReceivePool);
|
|
|
|
CloseHandle(transport->connectedEvent);
|
2016-10-09 23:36:04 +03:00
|
|
|
CloseHandle(transport->rereadEvent);
|
2022-11-15 17:04:13 +03:00
|
|
|
CloseHandle(transport->ioEvent);
|
2014-05-21 19:32:14 +04:00
|
|
|
DeleteCriticalSection(&(transport->ReadLock));
|
|
|
|
DeleteCriticalSection(&(transport->WriteLock));
|
|
|
|
free(transport);
|
2011-07-03 23:34:15 +04:00
|
|
|
}
|
2020-09-02 14:37:04 +03:00
|
|
|
|
|
|
|
BOOL transport_set_io_callbacks(rdpTransport* transport, const rdpTransportIo* io_callbacks)
|
|
|
|
{
|
|
|
|
if (!transport || !io_callbacks)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
transport->io = *io_callbacks;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rdpTransportIo* transport_get_io_callbacks(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
if (!transport)
|
|
|
|
return NULL;
|
|
|
|
return &transport->io;
|
|
|
|
}
|
2021-09-03 09:00:24 +03:00
|
|
|
|
|
|
|
rdpContext* transport_get_context(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->context;
|
|
|
|
}
|
2021-09-03 14:02:28 +03:00
|
|
|
|
|
|
|
rdpTransport* freerdp_get_transport(rdpContext* context)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(context->rdp);
|
|
|
|
return context->rdp->transport;
|
|
|
|
}
|
2021-09-06 12:01:36 +03:00
|
|
|
|
|
|
|
BOOL transport_set_nla(rdpTransport* transport, rdpNla* nla)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
nla_free(transport->nla);
|
|
|
|
transport->nla = nla;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpNla* transport_get_nla(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->nla;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_tls(rdpTransport* transport, rdpTls* tls)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
2023-01-14 05:25:26 +03:00
|
|
|
freerdp_tls_free(transport->tls);
|
2021-09-06 12:01:36 +03:00
|
|
|
transport->tls = tls;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpTls* transport_get_tls(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->tls;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_tsg(rdpTransport* transport, rdpTsg* tsg)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
tsg_free(transport->tsg);
|
|
|
|
transport->tsg = tsg;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpTsg* transport_get_tsg(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->tsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
wStream* transport_take_from_pool(rdpTransport* transport, size_t size)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return StreamPool_Take(transport->ReceivePool, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG transport_get_bytes_sent(rdpTransport* transport, BOOL resetCount)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
ULONG rc = 0;
|
2021-09-06 12:01:36 +03:00
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
rc = transport->written;
|
|
|
|
if (resetCount)
|
|
|
|
transport->written = 0;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRANSPORT_LAYER transport_get_layer(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_layer(rdpTransport* transport, TRANSPORT_LAYER layer)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->layer = layer;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_connected_event(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return SetEvent(transport->connectedEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_recv_callbacks(rdpTransport* transport, TransportRecv recv, void* extra)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->ReceiveCallback = recv;
|
|
|
|
transport->ReceiveExtra = extra;
|
2021-09-13 09:11:40 +03:00
|
|
|
return TRUE;
|
2021-09-06 12:01:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_get_blocking(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->blocking;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_set_blocking(rdpTransport* transport, BOOL blocking)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->blocking = blocking;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL transport_have_more_bytes_to_read(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
return transport->haveMoreBytesToRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
int transport_tcp_connect(rdpTransport* transport, const char* hostname, int port, DWORD timeout)
|
|
|
|
{
|
|
|
|
rdpContext* context = transport_get_context(transport);
|
|
|
|
WINPR_ASSERT(context);
|
|
|
|
WINPR_ASSERT(context->settings);
|
|
|
|
return IFCALLRESULT(-1, transport->io.TCPConnect, context, context->settings, hostname, port,
|
|
|
|
timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE transport_get_front_bio(rdpTransport* transport)
|
|
|
|
{
|
|
|
|
HANDLE hEvent = NULL;
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
WINPR_ASSERT(transport->frontBio);
|
|
|
|
|
|
|
|
BIO_get_event(transport->frontBio, &hEvent);
|
|
|
|
return hEvent;
|
|
|
|
}
|
2022-11-15 17:04:13 +03:00
|
|
|
|
|
|
|
BOOL transport_io_callback_set_event(rdpTransport* transport, BOOL set)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
2022-11-16 12:55:58 +03:00
|
|
|
transport->useIoEvent = TRUE;
|
2022-11-15 17:04:13 +03:00
|
|
|
if (!set)
|
|
|
|
return ResetEvent(transport->ioEvent);
|
|
|
|
return SetEvent(transport->ioEvent);
|
|
|
|
}
|
2023-06-23 01:49:27 +03:00
|
|
|
|
|
|
|
void transport_set_early_user_auth_mode(rdpTransport* transport, BOOL EUAMode)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(transport);
|
|
|
|
transport->earlyUserAuth = EUAMode;
|
|
|
|
WLog_Print(transport->log, WLOG_DEBUG, "Early User Auth Mode: %s", EUAMode ? "on" : "off");
|
|
|
|
}
|
2024-09-05 15:59:21 +03:00
|
|
|
|
|
|
|
rdpTransportLayer* transport_layer_new(rdpTransport* transport, size_t contextSize)
|
|
|
|
{
|
|
|
|
rdpTransportLayer* layer = (rdpTransportLayer*)calloc(1, sizeof(rdpTransportLayer));
|
|
|
|
if (!layer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (contextSize)
|
|
|
|
{
|
|
|
|
layer->userContext = calloc(1, contextSize);
|
|
|
|
if (!layer->userContext)
|
|
|
|
{
|
|
|
|
free(layer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void transport_layer_free(rdpTransportLayer* layer)
|
|
|
|
{
|
|
|
|
if (!layer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
IFCALL(layer->Close, layer->userContext);
|
|
|
|
free(layer->userContext);
|
|
|
|
free(layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_write(BIO* bio, const char* buf, int size)
|
|
|
|
{
|
|
|
|
if (!buf || !size)
|
|
|
|
return 0;
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
|
|
|
|
rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
|
|
|
|
if (!layer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
|
|
|
|
int status = IFCALLRESULT(-1, layer->Write, layer->userContext, buf, size);
|
|
|
|
|
|
|
|
if (status >= 0 && status < size)
|
|
|
|
BIO_set_flags(bio, (BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY));
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_read(BIO* bio, char* buf, int size)
|
|
|
|
{
|
|
|
|
if (!buf || !size)
|
|
|
|
return 0;
|
|
|
|
if (size < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
|
|
|
|
rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
|
|
|
|
if (!layer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
|
|
|
|
int status = IFCALLRESULT(-1, layer->Read, layer->userContext, buf, size);
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
BIO_set_flags(bio, (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY));
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_puts(BIO* bio, const char* str)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_gets(BIO* bio, char* str, int size)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static long transport_layer_bio_ctrl(BIO* bio, int cmd, long arg1, void* arg2)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
|
|
|
|
rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
|
|
|
|
if (!layer)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
int status = -1;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case BIO_C_GET_EVENT:
|
|
|
|
*((HANDLE*)arg2) = IFCALLRESULT(NULL, layer->GetEvent, layer->userContext);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_SET_NONBLOCK:
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_C_WAIT_READ:
|
|
|
|
{
|
|
|
|
int timeout = (int)arg1;
|
|
|
|
BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, FALSE, timeout);
|
|
|
|
/* Convert timeout to error return */
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BIO_C_WAIT_WRITE:
|
|
|
|
{
|
|
|
|
int timeout = (int)arg1;
|
|
|
|
BOOL r = IFCALLRESULT(FALSE, layer->Wait, layer->userContext, TRUE, timeout);
|
|
|
|
/* Convert timeout to error return */
|
|
|
|
if (!r)
|
|
|
|
{
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case BIO_CTRL_GET_CLOSE:
|
|
|
|
status = BIO_get_shutdown(bio);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_SET_CLOSE:
|
|
|
|
BIO_set_shutdown(bio, (int)arg1);
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BIO_CTRL_FLUSH:
|
|
|
|
case BIO_CTRL_DUP:
|
|
|
|
status = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_new(BIO* bio)
|
|
|
|
{
|
|
|
|
WINPR_ASSERT(bio);
|
|
|
|
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
|
|
|
|
BIO_set_init(bio, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int transport_layer_bio_free(BIO* bio)
|
|
|
|
{
|
|
|
|
if (!bio)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rdpTransportLayer* layer = (rdpTransportLayer*)BIO_get_data(bio);
|
|
|
|
if (layer)
|
|
|
|
transport_layer_free(layer);
|
|
|
|
|
|
|
|
BIO_set_data(bio, NULL);
|
|
|
|
BIO_set_init(bio, 0);
|
|
|
|
BIO_set_flags(bio, 0);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
BIO_METHOD* BIO_s_transport_layer(void)
|
|
|
|
{
|
|
|
|
static BIO_METHOD* bio_methods = NULL;
|
|
|
|
|
|
|
|
if (bio_methods == NULL)
|
|
|
|
{
|
|
|
|
if (!(bio_methods = BIO_meth_new(BIO_TYPE_SIMPLE, "TransportLayer")))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
BIO_meth_set_write(bio_methods, transport_layer_bio_write);
|
|
|
|
BIO_meth_set_read(bio_methods, transport_layer_bio_read);
|
|
|
|
BIO_meth_set_puts(bio_methods, transport_layer_bio_puts);
|
|
|
|
BIO_meth_set_gets(bio_methods, transport_layer_bio_gets);
|
|
|
|
BIO_meth_set_ctrl(bio_methods, transport_layer_bio_ctrl);
|
|
|
|
BIO_meth_set_create(bio_methods, transport_layer_bio_new);
|
|
|
|
BIO_meth_set_destroy(bio_methods, transport_layer_bio_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bio_methods;
|
|
|
|
}
|