20#include <freerdp/config.h>
22#include <freerdp/log.h>
25#include <winpr/wtypes.h>
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28#include <winpr/print.h>
29#include <winpr/synch.h>
30#include <winpr/thread.h>
31#include <winpr/stream.h>
34#include "ncacn_http.h"
38#include "rpc_client.h"
39#include "rts_signature.h"
45#define TAG FREERDP_TAG("core.gateway.rpc")
48static const char* rpc_client_state_str(RPC_CLIENT_STATE state)
51 const char* str =
"RPC_CLIENT_STATE_UNKNOWN";
55 case RPC_CLIENT_STATE_INITIAL:
56 str =
"RPC_CLIENT_STATE_INITIAL";
59 case RPC_CLIENT_STATE_ESTABLISHED:
60 str =
"RPC_CLIENT_STATE_ESTABLISHED";
63 case RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK:
64 str =
"RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK";
67 case RPC_CLIENT_STATE_WAIT_UNSECURE_BIND_ACK:
68 str =
"RPC_CLIENT_STATE_WAIT_UNSECURE_BIND_ACK";
71 case RPC_CLIENT_STATE_WAIT_SECURE_ALTER_CONTEXT_RESPONSE:
72 str =
"RPC_CLIENT_STATE_WAIT_SECURE_ALTER_CONTEXT_RESPONSE";
75 case RPC_CLIENT_STATE_CONTEXT_NEGOTIATED:
76 str =
"RPC_CLIENT_STATE_CONTEXT_NEGOTIATED";
79 case RPC_CLIENT_STATE_WAIT_RESPONSE:
80 str =
"RPC_CLIENT_STATE_WAIT_RESPONSE";
83 case RPC_CLIENT_STATE_FINAL:
84 str =
"RPC_CLIENT_STATE_FINAL";
93static BOOL rpc_pdu_reset(
RPC_PDU* pdu)
99 Stream_ResetPosition(pdu->s);
100 return Stream_SetLength(pdu->s, 0);
103static void rpc_pdu_free(
RPC_PDU* pdu)
108 Stream_Free(pdu->s, TRUE);
112WINPR_ATTR_MALLOC(rpc_pdu_free, 1)
113static
RPC_PDU* rpc_pdu_new(
void)
120 pdu->s = Stream_New(
nullptr, 4096);
125 if (!rpc_pdu_reset(pdu))
136static int rpc_client_receive_pipe_write(
RpcClient* client,
const BYTE* buffer,
size_t length)
140 if (!client || !buffer)
143 EnterCriticalSection(&(client->PipeLock));
145 if (ringbuffer_write(&(client->ReceivePipe), buffer, length))
146 status += (int)length;
148 if (ringbuffer_used(&(client->ReceivePipe)) > 0)
149 (
void)SetEvent(client->PipeEvent);
151 LeaveCriticalSection(&(client->PipeLock));
155int rpc_client_receive_pipe_read(
RpcClient* client, BYTE* buffer,
size_t length)
161 if (!client || !buffer)
164 EnterCriticalSection(&(client->PipeLock));
165 nchunks = ringbuffer_peek(&(client->ReceivePipe), chunks, length);
167 for (
int index = 0; index < nchunks; index++)
169 CopyMemory(&buffer[status], chunks[index].data, chunks[index].size);
170 status += chunks[index].size;
174 ringbuffer_commit_read_bytes(&(client->ReceivePipe), status);
176 if (ringbuffer_used(&(client->ReceivePipe)) < 1)
177 (void)ResetEvent(client->PipeEvent);
179 LeaveCriticalSection(&(client->PipeLock));
181 if (status > INT_MAX)
186static int rpc_client_transition_to_state(rdpRpc* rpc, RPC_CLIENT_STATE state)
191 WLog_DBG(TAG,
"%s", rpc_client_state_str(state));
196static int rpc_client_recv_pdu_int(rdpRpc* rpc,
RPC_PDU* pdu)
204 rdpTsg* tsg = transport_get_tsg(rpc->transport);
206 WLog_Print(rpc->log, WLOG_TRACE,
"client state %s, vc state %s",
207 rpc_client_state_str(rpc->State), rpc_vc_state_str(rpc->VirtualConnection->State));
210 rts_match_pdu_signature_ex(&RTS_PDU_PING_SIGNATURE, pdu->s,
nullptr, &found, TRUE);
211 rts_print_pdu_signature(rpc->log, WLOG_TRACE, &found);
213 return rts_recv_ping_pdu(rpc, pdu->s);
215 if (rpc->VirtualConnection->State < VIRTUAL_CONNECTION_STATE_OPENED)
217 switch (rpc->VirtualConnection->State)
219 case VIRTUAL_CONNECTION_STATE_INITIAL:
222 case VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT:
225 case VIRTUAL_CONNECTION_STATE_WAIT_A3W:
226 if (memcmp(&found, &RTS_PDU_CONN_A3_SIGNATURE,
sizeof(found)) != 0)
228 WLog_Print(rpc->log, WLOG_ERROR,
"unexpected RTS PDU: Expected CONN/A3");
229 rts_print_pdu_signature(rpc->log, WLOG_ERROR, &found);
233 if (!rts_recv_CONN_A3_pdu(rpc, pdu->s))
235 WLog_Print(rpc->log, WLOG_ERROR,
"rts_recv_CONN_A3_pdu failure");
239 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
240 VIRTUAL_CONNECTION_STATE_WAIT_C2);
244 case VIRTUAL_CONNECTION_STATE_WAIT_C2:
245 if (memcmp(&found, &RTS_PDU_CONN_C2_SIGNATURE,
sizeof(found)) != 0)
247 WLog_Print(rpc->log, WLOG_ERROR,
"unexpected RTS PDU: Expected CONN/C2");
248 rts_print_pdu_signature(rpc->log, WLOG_ERROR, &found);
252 if (!rts_recv_CONN_C2_pdu(rpc, pdu->s))
254 WLog_Print(rpc->log, WLOG_ERROR,
"rts_recv_CONN_C2_pdu failure");
258 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
259 VIRTUAL_CONNECTION_STATE_OPENED);
260 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_ESTABLISHED);
262 if (rpc_send_bind_pdu(rpc, TRUE) < 0)
264 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_send_bind_pdu failure");
268 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK);
272 case VIRTUAL_CONNECTION_STATE_OPENED:
275 case VIRTUAL_CONNECTION_STATE_FINAL:
281 else if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
283 if (rpc->State == RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK)
285 if (pdu->Type == PTYPE_BIND_ACK || pdu->Type == PTYPE_ALTER_CONTEXT_RESP)
287 if (!rpc_recv_bind_ack_pdu(rpc, pdu->s))
289 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_recv_bind_ack_pdu failure");
295 WLog_Print(rpc->log, WLOG_ERROR,
296 "RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK unexpected pdu type: 0x%08" PRIX32
302 switch (rpc_bind_state(rpc))
304 case RPC_BIND_STATE_INCOMPLETE:
305 if (rpc_send_bind_pdu(rpc, FALSE) < 0)
307 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_send_bind_pdu failure");
311 case RPC_BIND_STATE_LAST_LEG:
312 if (rpc_send_rpc_auth_3_pdu(rpc) < 0)
314 WLog_Print(rpc->log, WLOG_ERROR,
315 "rpc_secure_bind: error sending rpc_auth_3 pdu!");
320 case RPC_BIND_STATE_COMPLETE:
321 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_CONTEXT_NEGOTIATED);
323 if (!tsg_proxy_begin(tsg))
325 WLog_Print(rpc->log, WLOG_ERROR,
"tsg_proxy_begin failure");
337 WLog_Print(rpc->log, WLOG_ERROR,
"invalid rpc->State: %u", rpc->State);
340 else if (rpc->State >= RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
342 if (!tsg_recv_pdu(tsg, pdu))
352static int rpc_client_recv_pdu(rdpRpc* rpc,
RPC_PDU* pdu)
357 Stream_SealLength(pdu->s);
358 Stream_ResetPosition(pdu->s);
360 const size_t before = Stream_GetRemainingLength(pdu->s);
361 WLog_Print(rpc->log, WLOG_TRACE,
"RPC PDU parsing %" PRIuz
" bytes", before);
362 const int rc = rpc_client_recv_pdu_int(rpc, pdu);
365 const size_t after = Stream_GetRemainingLength(pdu->s);
369 WLog_Print(rpc->log, WLOG_WARN,
"Incompletely parsed RPC PDU (%" PRIuz
" bytes remain)",
377static int rpc_client_recv_fragment(rdpRpc* rpc,
wStream* fragment)
380 size_t StubOffset = 0;
381 size_t StubLength = 0;
386 WINPR_ASSERT(rpc->client);
387 WINPR_ASSERT(fragment);
389 RPC_PDU* pdu = rpc->client->pdu;
392 Stream_SealLength(fragment);
393 Stream_ResetPosition(fragment);
395 if (!rts_read_pdu_header(fragment, &header))
398 if (header.common.ptype == PTYPE_RESPONSE)
400 rpc->VirtualConnection->DefaultOutChannel->BytesReceived += header.common.frag_length;
401 rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow -=
402 header.common.frag_length;
404 if (rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow <
405 (rpc->ReceiveWindow / 2))
407 if (!rts_send_flow_control_ack_pdu(rpc))
411 if (!rpc_get_stub_data_info(rpc, &header, &StubOffset, &StubLength))
413 WLog_ERR(TAG,
"expected stub");
419 if ((header.common.call_id == rpc->PipeCallId) &&
420 (header.common.pfc_flags & PFC_LAST_FRAG))
423 TerminateEventArgs e;
424 rdpContext* context = transport_get_context(rpc->transport);
425 rdpTsg* tsg = transport_get_tsg(rpc->transport);
427 WINPR_ASSERT(context);
429 if (Stream_Length(fragment) < StubOffset + 4)
431 if (!Stream_SetPosition(fragment, StubOffset))
433 Stream_Read_UINT32(fragment, rpc->result);
435 utils_abort_connect(context->rdp);
436 tsg_set_state(tsg, TSG_STATE_TUNNEL_CLOSE_PENDING);
437 EventArgsInit(&e,
"freerdp");
439 rc = PubSub_OnTerminate(context->rdp->pubSub, context, &e) >= 0 ? 0 : -1;
443 if (header.common.call_id != rpc->PipeCallId)
451 if (rpc->StubFragCount == 0)
452 rpc->StubCallId = header.common.call_id;
454 if (rpc->StubCallId != header.common.call_id)
457 "invalid call_id: actual: %" PRIu32
", expected: %" PRIu32
458 ", frag_count: %" PRIu32
"",
459 rpc->StubCallId, header.common.call_id, rpc->StubFragCount);
462 call = rpc_client_call_find_by_id(rpc->client, rpc->StubCallId);
467 if (call->OpNum != TsProxySetupReceivePipeOpnum)
471 if (!Stream_EnsureRemainingCapacity(pdu->s, StubLength))
474 if (Stream_Length(fragment) < StubOffset + StubLength)
477 if (!Stream_SetPosition(fragment, StubOffset))
480 Stream_Write(pdu->s, Stream_ConstPointer(fragment), StubLength);
481 rpc->StubFragCount++;
483 if (response->alloc_hint == StubLength)
485 pdu->Flags = RPC_PDU_FLAG_STUB;
486 pdu->Type = PTYPE_RESPONSE;
487 pdu->CallId = rpc->StubCallId;
489 if (rpc_client_recv_pdu(rpc, pdu) < 0)
491 if (!rpc_pdu_reset(pdu))
493 rpc->StubFragCount = 0;
500 if (Stream_Length(fragment) < StubOffset + StubLength)
502 if (!Stream_SetPosition(fragment, StubOffset))
504 if (rpc_client_receive_pipe_write(rpc->client, Stream_ConstPointer(fragment),
507 rpc->StubFragCount++;
509 if (response->alloc_hint == StubLength)
511 rpc->StubFragCount = 0;
518 else if (header.common.ptype == PTYPE_RTS)
520 if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
523 pdu->Type = header.common.ptype;
524 pdu->CallId = header.common.call_id;
526 const size_t len = Stream_Length(fragment);
527 if (!Stream_EnsureRemainingCapacity(pdu->s, len))
530 Stream_Write(pdu->s, Stream_Buffer(fragment), len);
532 if (rpc_client_recv_pdu(rpc, pdu) < 0)
535 if (!rpc_pdu_reset(pdu))
540 if (!rts_recv_out_of_sequence_pdu(rpc, fragment, &header))
546 else if (header.common.ptype == PTYPE_BIND_ACK ||
547 header.common.ptype == PTYPE_ALTER_CONTEXT_RESP)
550 pdu->Type = header.common.ptype;
551 pdu->CallId = header.common.call_id;
553 const size_t len = Stream_Length(fragment);
554 if (!Stream_EnsureRemainingCapacity(pdu->s, len))
557 Stream_Write(pdu->s, Stream_Buffer(fragment), len);
559 if (rpc_client_recv_pdu(rpc, pdu) < 0)
562 if (!rpc_pdu_reset(pdu))
566 else if (header.common.ptype == PTYPE_FAULT)
569 rpc_recv_fault_pdu(fault->status);
574 WLog_ERR(TAG,
"unexpected RPC PDU type 0x%02" PRIX8
"", header.common.ptype);
579 rc = (rc < 0) ? 1 : 0;
582 rts_free_pdu_header(&header, FALSE);
587static SSIZE_T rpc_client_default_out_channel_recv(rdpRpc* rpc)
590 HttpResponse* response =
nullptr;
593 HANDLE outChannelEvent =
nullptr;
595 inChannel = connection->DefaultInChannel;
596 outChannel = connection->DefaultOutChannel;
597 BIO_get_event(outChannel->common.tls->bio, &outChannelEvent);
599 if (outChannel->State < CLIENT_OUT_CHANNEL_STATE_OPENED)
601 if (WaitForSingleObject(outChannelEvent, 0) != WAIT_OBJECT_0)
604 response = http_response_recv(outChannel->common.tls, TRUE);
609 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_SECURITY)
612 if (!rpc_ncacn_http_recv_out_channel_response(&outChannel->common, response))
614 http_response_free(response);
615 WLog_ERR(TAG,
"rpc_ncacn_http_recv_out_channel_response failure");
621 if (!rpc_ncacn_http_send_out_channel_request(&outChannel->common, FALSE))
623 http_response_free(response);
624 WLog_ERR(TAG,
"rpc_ncacn_http_send_out_channel_request failure");
628 if (rpc_ncacn_http_is_final_request(&outChannel->common))
630 rpc_ncacn_http_auth_uninit(&outChannel->common);
631 rpc_out_channel_transition_to_state(outChannel,
632 CLIENT_OUT_CHANNEL_STATE_NEGOTIATED);
636 if (!rts_send_CONN_A1_pdu(rpc))
638 http_response_free(response);
639 WLog_ERR(TAG,
"rpc_send_CONN_A1_pdu error!");
643 rpc_out_channel_transition_to_state(outChannel, CLIENT_OUT_CHANNEL_STATE_OPENED);
645 if (inChannel->State == CLIENT_IN_CHANNEL_STATE_OPENED)
647 rpc_virtual_connection_transition_to_state(
648 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
655 http_response_free(response);
657 else if (connection->State == VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT)
660 if (WaitForSingleObject(outChannelEvent, 0) != WAIT_OBJECT_0)
663 response = http_response_recv(outChannel->common.tls, FALSE);
668 const UINT16 statusCode = http_response_get_status_code(response);
669 if (statusCode != HTTP_STATUS_OK)
671 http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
673 if (statusCode == HTTP_STATUS_DENIED)
675 rdpContext* context = transport_get_context(rpc->transport);
676 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_ACCESS_DENIED);
679 http_response_free(response);
683 http_response_free(response);
684 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
685 VIRTUAL_CONNECTION_STATE_WAIT_A3W);
690 wStream* fragment = rpc->client->ReceiveFragment;
697 while (Stream_GetPosition(fragment) < RPC_COMMON_FIELDS_LENGTH)
699 status = rpc_channel_read(&outChannel->common, fragment,
700 RPC_COMMON_FIELDS_LENGTH - Stream_GetPosition(fragment));
705 if (Stream_GetPosition(fragment) < RPC_COMMON_FIELDS_LENGTH)
709 pos = Stream_GetPosition(fragment);
710 Stream_ResetPosition(fragment);
713 const rts_pdu_status_t rc = rts_read_common_pdu_header(fragment, &header, TRUE);
714 if (rc == RTS_PDU_FAIL)
717 if (!Stream_SetPosition(fragment, pos))
720 if (header.frag_length >= rpc->max_recv_frag)
723 "rpc_client_recv: invalid fragment size: %" PRIu16
" (max: %" PRIu16
")",
724 header.frag_length, rpc->max_recv_frag);
725 winpr_HexDump(TAG, WLOG_ERROR, Stream_Buffer(fragment),
726 Stream_GetPosition(fragment));
730 while (Stream_GetPosition(fragment) < header.frag_length)
732 status = rpc_channel_read(&outChannel->common, fragment,
733 header.frag_length - Stream_GetPosition(fragment));
737 WLog_ERR(TAG,
"error reading fragment body");
741 if (Stream_GetPosition(fragment) < header.frag_length)
747 status = rpc_client_recv_fragment(rpc, fragment);
753 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_RECYCLED &&
754 connection->NonDefaultOutChannel)
756 rpc_channel_free(&connection->DefaultOutChannel->common);
757 connection->DefaultOutChannel = connection->NonDefaultOutChannel;
758 connection->NonDefaultOutChannel =
nullptr;
759 rpc_out_channel_transition_to_state(connection->DefaultOutChannel,
760 CLIENT_OUT_CHANNEL_STATE_OPENED);
761 rpc_virtual_connection_transition_to_state(
762 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
766 Stream_ResetPosition(fragment);
775static SSIZE_T rpc_client_nondefault_out_channel_recv(rdpRpc* rpc)
778 HttpResponse* response =
nullptr;
780 HANDLE nextOutChannelEvent =
nullptr;
781 nextOutChannel = rpc->VirtualConnection->NonDefaultOutChannel;
782 BIO_get_event(nextOutChannel->common.tls->bio, &nextOutChannelEvent);
784 if (WaitForSingleObject(nextOutChannelEvent, 0) != WAIT_OBJECT_0)
787 response = http_response_recv(nextOutChannel->common.tls, TRUE);
791 switch (nextOutChannel->State)
793 case CLIENT_OUT_CHANNEL_STATE_SECURITY:
794 if (rpc_ncacn_http_recv_out_channel_response(&nextOutChannel->common, response))
796 if (rpc_ncacn_http_send_out_channel_request(&nextOutChannel->common, TRUE))
798 if (rpc_ncacn_http_is_final_request(&nextOutChannel->common))
800 rpc_ncacn_http_auth_uninit(&nextOutChannel->common);
802 if (rts_send_OUT_R1_A3_pdu(rpc))
805 rpc_out_channel_transition_to_state(
806 nextOutChannel, CLIENT_OUT_CHANNEL_STATE_OPENED_A6W);
810 WLog_ERR(TAG,
"rts_send_OUT_R1/A3_pdu failure");
820 WLog_ERR(TAG,
"rpc_ncacn_http_send_out_channel_request failure");
825 WLog_ERR(TAG,
"rpc_ncacn_http_recv_out_channel_response failure");
830 case CLIENT_OUT_CHANNEL_STATE_INITIAL:
831 case CLIENT_OUT_CHANNEL_STATE_CONNECTED:
832 case CLIENT_OUT_CHANNEL_STATE_NEGOTIATED:
835 "rpc_client_nondefault_out_channel_recv: Unexpected message %08" PRIx32,
836 nextOutChannel->State);
840 http_response_free(response);
846int rpc_client_out_channel_recv(rdpRpc* rpc)
851 if (connection->DefaultOutChannel)
853 status = rpc_client_default_out_channel_recv(rpc);
859 if (connection->NonDefaultOutChannel)
861 status = rpc_client_nondefault_out_channel_recv(rpc);
870int rpc_client_in_channel_recv(rdpRpc* rpc)
873 HttpResponse* response =
nullptr;
876 HANDLE InChannelEvent =
nullptr;
878 inChannel = connection->DefaultInChannel;
879 outChannel = connection->DefaultOutChannel;
880 BIO_get_event(inChannel->common.tls->bio, &InChannelEvent);
882 if (WaitForSingleObject(InChannelEvent, 0) != WAIT_OBJECT_0)
885 if (inChannel->State < CLIENT_IN_CHANNEL_STATE_OPENED)
887 response = http_response_recv(inChannel->common.tls, TRUE);
892 if (inChannel->State == CLIENT_IN_CHANNEL_STATE_SECURITY)
894 if (!rpc_ncacn_http_recv_in_channel_response(&inChannel->common, response))
896 WLog_ERR(TAG,
"rpc_ncacn_http_recv_in_channel_response failure");
897 http_response_free(response);
903 if (!rpc_ncacn_http_send_in_channel_request(&inChannel->common))
905 WLog_ERR(TAG,
"rpc_ncacn_http_send_in_channel_request failure");
906 http_response_free(response);
910 if (rpc_ncacn_http_is_final_request(&inChannel->common))
912 rpc_ncacn_http_auth_uninit(&inChannel->common);
913 rpc_in_channel_transition_to_state(inChannel, CLIENT_IN_CHANNEL_STATE_NEGOTIATED);
917 if (!rts_send_CONN_B1_pdu(rpc))
919 WLog_ERR(TAG,
"rpc_send_CONN_B1_pdu error!");
920 http_response_free(response);
924 rpc_in_channel_transition_to_state(inChannel, CLIENT_IN_CHANNEL_STATE_OPENED);
926 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_OPENED)
928 rpc_virtual_connection_transition_to_state(
929 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
936 http_response_free(response);
940 response = http_response_recv(inChannel->common.tls, TRUE);
946 http_response_free(response);
964 ArrayList_Lock(client->ClientCallList);
965 const size_t count = ArrayList_Count(client->ClientCallList);
967 for (
size_t index = 0; index < count; index++)
969 clientCall = (
RpcClientCall*)ArrayList_GetItem(client->ClientCallList, index);
971 if (clientCall->CallId == CallId)
975 ArrayList_Unlock(client->ClientCallList);
979RpcClientCall* rpc_client_call_new(UINT32 CallId, UINT32 OpNum)
987 clientCall->CallId = CallId;
988 clientCall->OpNum = OpNum;
989 clientCall->State = RPC_CLIENT_CALL_STATE_SEND_PDUS;
998static void rpc_array_client_call_free(
void* call)
1003int rpc_in_channel_send_pdu(
RpcInChannel* inChannel,
const BYTE* buffer,
size_t length)
1009 SSIZE_T status = rpc_channel_write(&inChannel->common, buffer, length);
1014 Stream_StaticConstInit(&s, buffer, length);
1015 const rts_pdu_status_t rc = rts_read_common_pdu_header(&s, &header, FALSE);
1016 if (rc != RTS_PDU_VALID)
1019 clientCall = rpc_client_call_find_by_id(inChannel->common.client, header.call_id);
1023 clientCall->State = RPC_CLIENT_CALL_STATE_DISPATCHED;
1032 if (header.ptype == PTYPE_REQUEST)
1034 const uint32_t ustatus = WINPR_ASSERTING_INT_CAST(uint32_t, status);
1035 inChannel->BytesSent += ustatus;
1036 inChannel->SenderAvailableWindow -= ustatus;
1039 if (status > INT32_MAX)
1044BOOL rpc_client_write_call(rdpRpc* rpc,
wStream* s, UINT16 opnum)
1047 BYTE* buffer =
nullptr;
1048 size_t stub_data_pad = 0;
1050 SecBuffer ciphertext = WINPR_C_ARRAY_INIT;
1052 rdpCredsspAuth* auth =
nullptr;
1065 connection = rpc->VirtualConnection;
1069 WLog_ERR(TAG,
"invalid auth context");
1076 inChannel = connection->DefaultInChannel;
1081 Stream_SealLength(s);
1084 const size_t length = Stream_Length(s);
1085 if (length > UINT32_MAX)
1089 const size_t asize = credssp_auth_trailer_size(auth);
1090 request_pdu.header = rpc_pdu_header_init(rpc);
1091 request_pdu.header.ptype = PTYPE_REQUEST;
1092 request_pdu.header.pfc_flags = PFC_FIRST_FRAG | PFC_LAST_FRAG;
1093 request_pdu.header.auth_length = (UINT16)asize;
1095 request_pdu.header.call_id = rpc->CallId++;
1096 request_pdu.alloc_hint = (UINT32)length;
1097 request_pdu.p_cont_id = 0x0000;
1098 request_pdu.opnum = opnum;
1099 clientCall = rpc_client_call_new(request_pdu.header.call_id, request_pdu.opnum);
1104 if (!ArrayList_Append(rpc->client->ClientCallList, clientCall))
1106 rpc_client_call_free(clientCall);
1111 if (request_pdu.opnum == TsProxySetupReceivePipeOpnum)
1112 rpc->PipeCallId = request_pdu.header.call_id;
1114 request_pdu.stub_data = Stream_Buffer(s);
1116 stub_data_pad = rpc_offset_align(&offset, 8);
1120 const size_t alg = rpc_offset_align(&offset, 4);
1121 WINPR_ASSERT(alg <= UINT8_MAX);
1122 request_pdu.auth_verifier.auth_pad_length = (UINT8)alg;
1124 request_pdu.auth_verifier.auth_type =
1125 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
1126 request_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
1127 request_pdu.auth_verifier.auth_reserved = 0x00;
1128 request_pdu.auth_verifier.auth_context_id = 0x00000000;
1129 offset += (8 + request_pdu.header.auth_length);
1131 if (offset > UINT16_MAX)
1133 request_pdu.header.frag_length = (UINT16)offset;
1134 buffer = (BYTE*)calloc(1, request_pdu.header.frag_length);
1139 CopyMemory(buffer, &request_pdu, 24);
1141 rpc_offset_pad(&offset, stub_data_pad);
1142 CopyMemory(&buffer[offset], request_pdu.stub_data, length);
1146 rpc_offset_pad(&offset, request_pdu.auth_verifier.auth_pad_length);
1147 CopyMemory(&buffer[offset], &request_pdu.auth_verifier.auth_type, 8);
1150 if (offset > request_pdu.header.frag_length)
1153 plaintext.pvBuffer = buffer;
1154 plaintext.cbBuffer = (UINT32)offset;
1155 plaintext.BufferType = SECBUFFER_READONLY;
1159 if (!credssp_auth_encrypt(auth, &plaintext, &ciphertext, &size, rpc->SendSeqNum++))
1162 if (offset + size > request_pdu.header.frag_length)
1164 sspi_SecBufferFree(&ciphertext);
1168 CopyMemory(&buffer[offset], ciphertext.pvBuffer, size);
1172 sspi_SecBufferFree(&ciphertext);
1174 if (rpc_in_channel_send_pdu(inChannel, buffer, request_pdu.header.frag_length) < 0)
1180 Stream_Free(s, TRUE);
1185static BOOL rpc_client_resolve_gateway(rdpSettings* settings,
char** host, UINT16* port,
1188 struct addrinfo* result =
nullptr;
1190 if (!settings || !host || !port || !isProxy)
1198 *isProxy = proxy_prepare(settings, &peerHostname, port, &proxyUsername, &proxyPassword);
1199 result = freerdp_tcp_resolve_host(peerHostname, *port, 0);
1204 *host = freerdp_tcp_address_to_string(
1205 WINPR_PACKED_ALIGN_CAST(
const struct sockaddr_storage*, result->ai_addr),
nullptr);
1206 freeaddrinfo(result);
1211RpcClient* rpc_client_new(rdpContext* context, UINT32 max_recv_frag)
1219 if (!rpc_client_resolve_gateway(context->settings, &client->host, &client->port,
1223 client->context = context;
1225 if (!client->context)
1228 client->pdu = rpc_pdu_new();
1233 client->ReceiveFragment = Stream_New(
nullptr, max_recv_frag);
1235 if (!client->ReceiveFragment)
1238 client->PipeEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1240 if (!client->PipeEvent)
1243 if (!ringbuffer_init(&(client->ReceivePipe), 4096))
1246 if (!InitializeCriticalSectionAndSpinCount(&(client->PipeLock), 4000))
1249 client->ClientCallList = ArrayList_New(TRUE);
1251 if (!client->ClientCallList)
1254 obj = ArrayList_Object(client->ClientCallList);
1258 WINPR_PRAGMA_DIAG_PUSH
1259 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1260 rpc_client_free(client);
1261 WINPR_PRAGMA_DIAG_POP
1272 if (client->ReceiveFragment)
1273 Stream_Free(client->ReceiveFragment, TRUE);
1275 if (client->PipeEvent)
1276 (void)CloseHandle(client->PipeEvent);
1278 ringbuffer_destroy(&(client->ReceivePipe));
1279 DeleteCriticalSection(&(client->PipeLock));
1282 rpc_pdu_free(client->pdu);
1284 if (client->ClientCallList)
1285 ArrayList_Free(client->ClientCallList);
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
a piece of data in the ring buffer, exactly like a glibc iovec
This struct contains function pointer to initialize/free objects.
OBJECT_FREE_FN fnObjectFree