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")
47static const char* rpc_client_state_str(RPC_CLIENT_STATE state)
50 const char* str =
"RPC_CLIENT_STATE_UNKNOWN";
54 case RPC_CLIENT_STATE_INITIAL:
55 str =
"RPC_CLIENT_STATE_INITIAL";
58 case RPC_CLIENT_STATE_ESTABLISHED:
59 str =
"RPC_CLIENT_STATE_ESTABLISHED";
62 case RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK:
63 str =
"RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK";
66 case RPC_CLIENT_STATE_WAIT_UNSECURE_BIND_ACK:
67 str =
"RPC_CLIENT_STATE_WAIT_UNSECURE_BIND_ACK";
70 case RPC_CLIENT_STATE_WAIT_SECURE_ALTER_CONTEXT_RESPONSE:
71 str =
"RPC_CLIENT_STATE_WAIT_SECURE_ALTER_CONTEXT_RESPONSE";
74 case RPC_CLIENT_STATE_CONTEXT_NEGOTIATED:
75 str =
"RPC_CLIENT_STATE_CONTEXT_NEGOTIATED";
78 case RPC_CLIENT_STATE_WAIT_RESPONSE:
79 str =
"RPC_CLIENT_STATE_WAIT_RESPONSE";
82 case RPC_CLIENT_STATE_FINAL:
83 str =
"RPC_CLIENT_STATE_FINAL";
91static void rpc_pdu_reset(
RPC_PDU* pdu)
96 Stream_SetPosition(pdu->s, 0);
97 Stream_SetLength(pdu->s, 0);
100static RPC_PDU* rpc_pdu_new(
void)
108 pdu->s = Stream_New(
nullptr, 4096);
120static void rpc_pdu_free(
RPC_PDU* pdu)
125 Stream_Free(pdu->s, TRUE);
129static int rpc_client_receive_pipe_write(
RpcClient* client,
const BYTE* buffer,
size_t length)
133 if (!client || !buffer)
136 EnterCriticalSection(&(client->PipeLock));
138 if (ringbuffer_write(&(client->ReceivePipe), buffer, length))
139 status += (int)length;
141 if (ringbuffer_used(&(client->ReceivePipe)) > 0)
142 (
void)SetEvent(client->PipeEvent);
144 LeaveCriticalSection(&(client->PipeLock));
148int rpc_client_receive_pipe_read(
RpcClient* client, BYTE* buffer,
size_t length)
154 if (!client || !buffer)
157 EnterCriticalSection(&(client->PipeLock));
158 nchunks = ringbuffer_peek(&(client->ReceivePipe), chunks, length);
160 for (
int index = 0; index < nchunks; index++)
162 CopyMemory(&buffer[status], chunks[index].data, chunks[index].size);
163 status += chunks[index].size;
167 ringbuffer_commit_read_bytes(&(client->ReceivePipe), status);
169 if (ringbuffer_used(&(client->ReceivePipe)) < 1)
170 (void)ResetEvent(client->PipeEvent);
172 LeaveCriticalSection(&(client->PipeLock));
174 if (status > INT_MAX)
179static int rpc_client_transition_to_state(rdpRpc* rpc, RPC_CLIENT_STATE state)
184 WLog_DBG(TAG,
"%s", rpc_client_state_str(state));
188static int rpc_client_recv_pdu_int(rdpRpc* rpc,
RPC_PDU* pdu)
196 rdpTsg* tsg = transport_get_tsg(rpc->transport);
198 WLog_Print(rpc->log, WLOG_TRACE,
"client state %s, vc state %s",
199 rpc_client_state_str(rpc->State), rpc_vc_state_str(rpc->VirtualConnection->State));
202 rts_match_pdu_signature_ex(&RTS_PDU_PING_SIGNATURE, pdu->s,
nullptr, &found, TRUE);
203 rts_print_pdu_signature(rpc->log, WLOG_TRACE, &found);
205 return rts_recv_ping_pdu(rpc, pdu->s);
207 if (rpc->VirtualConnection->State < VIRTUAL_CONNECTION_STATE_OPENED)
209 switch (rpc->VirtualConnection->State)
211 case VIRTUAL_CONNECTION_STATE_INITIAL:
214 case VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT:
217 case VIRTUAL_CONNECTION_STATE_WAIT_A3W:
218 if (memcmp(&found, &RTS_PDU_CONN_A3_SIGNATURE,
sizeof(found)) != 0)
220 WLog_Print(rpc->log, WLOG_ERROR,
"unexpected RTS PDU: Expected CONN/A3");
221 rts_print_pdu_signature(rpc->log, WLOG_ERROR, &found);
225 if (!rts_recv_CONN_A3_pdu(rpc, pdu->s))
227 WLog_Print(rpc->log, WLOG_ERROR,
"rts_recv_CONN_A3_pdu failure");
231 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
232 VIRTUAL_CONNECTION_STATE_WAIT_C2);
236 case VIRTUAL_CONNECTION_STATE_WAIT_C2:
237 if (memcmp(&found, &RTS_PDU_CONN_C2_SIGNATURE,
sizeof(found)) != 0)
239 WLog_Print(rpc->log, WLOG_ERROR,
"unexpected RTS PDU: Expected CONN/C2");
240 rts_print_pdu_signature(rpc->log, WLOG_ERROR, &found);
244 if (!rts_recv_CONN_C2_pdu(rpc, pdu->s))
246 WLog_Print(rpc->log, WLOG_ERROR,
"rts_recv_CONN_C2_pdu failure");
250 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
251 VIRTUAL_CONNECTION_STATE_OPENED);
252 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_ESTABLISHED);
254 if (rpc_send_bind_pdu(rpc, TRUE) < 0)
256 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_send_bind_pdu failure");
260 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK);
264 case VIRTUAL_CONNECTION_STATE_OPENED:
267 case VIRTUAL_CONNECTION_STATE_FINAL:
273 else if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
275 if (rpc->State == RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK)
277 if (pdu->Type == PTYPE_BIND_ACK || pdu->Type == PTYPE_ALTER_CONTEXT_RESP)
279 if (!rpc_recv_bind_ack_pdu(rpc, pdu->s))
281 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_recv_bind_ack_pdu failure");
287 WLog_Print(rpc->log, WLOG_ERROR,
288 "RPC_CLIENT_STATE_WAIT_SECURE_BIND_ACK unexpected pdu type: 0x%08" PRIX32
294 switch (rpc_bind_state(rpc))
296 case RPC_BIND_STATE_INCOMPLETE:
297 if (rpc_send_bind_pdu(rpc, FALSE) < 0)
299 WLog_Print(rpc->log, WLOG_ERROR,
"rpc_send_bind_pdu failure");
303 case RPC_BIND_STATE_LAST_LEG:
304 if (rpc_send_rpc_auth_3_pdu(rpc) < 0)
306 WLog_Print(rpc->log, WLOG_ERROR,
307 "rpc_secure_bind: error sending rpc_auth_3 pdu!");
312 case RPC_BIND_STATE_COMPLETE:
313 rpc_client_transition_to_state(rpc, RPC_CLIENT_STATE_CONTEXT_NEGOTIATED);
315 if (!tsg_proxy_begin(tsg))
317 WLog_Print(rpc->log, WLOG_ERROR,
"tsg_proxy_begin failure");
329 WLog_Print(rpc->log, WLOG_ERROR,
"invalid rpc->State: %u", rpc->State);
332 else if (rpc->State >= RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
334 if (!tsg_recv_pdu(tsg, pdu))
343static int rpc_client_recv_pdu(rdpRpc* rpc,
RPC_PDU* pdu)
348 Stream_SealLength(pdu->s);
349 Stream_SetPosition(pdu->s, 0);
351 const size_t before = Stream_GetRemainingLength(pdu->s);
352 WLog_Print(rpc->log, WLOG_TRACE,
"RPC PDU parsing %" PRIuz
" bytes", before);
353 const int rc = rpc_client_recv_pdu_int(rpc, pdu);
356 const size_t after = Stream_GetRemainingLength(pdu->s);
360 WLog_Print(rpc->log, WLOG_WARN,
"Incompletely parsed RPC PDU (%" PRIuz
" bytes remain)",
367static int rpc_client_recv_fragment(rdpRpc* rpc,
wStream* fragment)
371 size_t StubOffset = 0;
372 size_t StubLength = 0;
377 WINPR_ASSERT(rpc->client);
378 WINPR_ASSERT(fragment);
380 pdu = rpc->client->pdu;
383 Stream_SealLength(fragment);
384 Stream_SetPosition(fragment, 0);
386 if (!rts_read_pdu_header(fragment, &header))
389 if (header.common.ptype == PTYPE_RESPONSE)
391 rpc->VirtualConnection->DefaultOutChannel->BytesReceived += header.common.frag_length;
392 rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow -=
393 header.common.frag_length;
395 if (rpc->VirtualConnection->DefaultOutChannel->ReceiverAvailableWindow <
396 (rpc->ReceiveWindow / 2))
398 if (!rts_send_flow_control_ack_pdu(rpc))
402 if (!rpc_get_stub_data_info(rpc, &header, &StubOffset, &StubLength))
404 WLog_ERR(TAG,
"expected stub");
410 if ((header.common.call_id == rpc->PipeCallId) &&
411 (header.common.pfc_flags & PFC_LAST_FRAG))
414 TerminateEventArgs e;
415 rdpContext* context = transport_get_context(rpc->transport);
416 rdpTsg* tsg = transport_get_tsg(rpc->transport);
418 WINPR_ASSERT(context);
420 if (Stream_Length(fragment) < StubOffset + 4)
422 Stream_SetPosition(fragment, StubOffset);
423 Stream_Read_UINT32(fragment, rpc->result);
425 utils_abort_connect(context->rdp);
426 tsg_set_state(tsg, TSG_STATE_TUNNEL_CLOSE_PENDING);
427 EventArgsInit(&e,
"freerdp");
429 PubSub_OnTerminate(context->rdp->pubSub, context, &e);
434 if (header.common.call_id != rpc->PipeCallId)
442 if (rpc->StubFragCount == 0)
443 rpc->StubCallId = header.common.call_id;
445 if (rpc->StubCallId != header.common.call_id)
448 "invalid call_id: actual: %" PRIu32
", expected: %" PRIu32
449 ", frag_count: %" PRIu32
"",
450 rpc->StubCallId, header.common.call_id, rpc->StubFragCount);
453 call = rpc_client_call_find_by_id(rpc->client, rpc->StubCallId);
458 if (call->OpNum != TsProxySetupReceivePipeOpnum)
462 if (!Stream_EnsureCapacity(pdu->s, response->alloc_hint))
465 if (Stream_Length(fragment) < StubOffset + StubLength)
468 Stream_SetPosition(fragment, StubOffset);
469 Stream_Write(pdu->s, Stream_ConstPointer(fragment), StubLength);
470 rpc->StubFragCount++;
472 if (response->alloc_hint == StubLength)
474 pdu->Flags = RPC_PDU_FLAG_STUB;
475 pdu->Type = PTYPE_RESPONSE;
476 pdu->CallId = rpc->StubCallId;
478 if (rpc_client_recv_pdu(rpc, pdu) < 0)
481 rpc->StubFragCount = 0;
488 if (Stream_Length(fragment) < StubOffset + StubLength)
490 Stream_SetPosition(fragment, StubOffset);
491 rpc_client_receive_pipe_write(rpc->client, Stream_ConstPointer(fragment), StubLength);
492 rpc->StubFragCount++;
494 if (response->alloc_hint == StubLength)
496 rpc->StubFragCount = 0;
503 else if (header.common.ptype == PTYPE_RTS)
505 if (rpc->State < RPC_CLIENT_STATE_CONTEXT_NEGOTIATED)
508 pdu->Type = header.common.ptype;
509 pdu->CallId = header.common.call_id;
511 const size_t len = Stream_Length(fragment);
512 if (!Stream_EnsureCapacity(pdu->s, len))
515 Stream_Write(pdu->s, Stream_Buffer(fragment), len);
517 if (rpc_client_recv_pdu(rpc, pdu) < 0)
524 if (!rts_recv_out_of_sequence_pdu(rpc, fragment, &header))
530 else if (header.common.ptype == PTYPE_BIND_ACK ||
531 header.common.ptype == PTYPE_ALTER_CONTEXT_RESP)
534 pdu->Type = header.common.ptype;
535 pdu->CallId = header.common.call_id;
537 const size_t len = Stream_Length(fragment);
538 if (!Stream_EnsureCapacity(pdu->s, len))
541 Stream_Write(pdu->s, Stream_Buffer(fragment), len);
543 if (rpc_client_recv_pdu(rpc, pdu) < 0)
549 else if (header.common.ptype == PTYPE_FAULT)
552 rpc_recv_fault_pdu(fault->status);
557 WLog_ERR(TAG,
"unexpected RPC PDU type 0x%02" PRIX8
"", header.common.ptype);
562 rc = (rc < 0) ? 1 : 0;
565 rts_free_pdu_header(&header, FALSE);
569static SSIZE_T rpc_client_default_out_channel_recv(rdpRpc* rpc)
572 HttpResponse* response =
nullptr;
575 HANDLE outChannelEvent =
nullptr;
577 inChannel = connection->DefaultInChannel;
578 outChannel = connection->DefaultOutChannel;
579 BIO_get_event(outChannel->common.tls->bio, &outChannelEvent);
581 if (outChannel->State < CLIENT_OUT_CHANNEL_STATE_OPENED)
583 if (WaitForSingleObject(outChannelEvent, 0) != WAIT_OBJECT_0)
586 response = http_response_recv(outChannel->common.tls, TRUE);
591 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_SECURITY)
594 if (!rpc_ncacn_http_recv_out_channel_response(&outChannel->common, response))
596 http_response_free(response);
597 WLog_ERR(TAG,
"rpc_ncacn_http_recv_out_channel_response failure");
603 if (!rpc_ncacn_http_send_out_channel_request(&outChannel->common, FALSE))
605 http_response_free(response);
606 WLog_ERR(TAG,
"rpc_ncacn_http_send_out_channel_request failure");
610 if (rpc_ncacn_http_is_final_request(&outChannel->common))
612 rpc_ncacn_http_auth_uninit(&outChannel->common);
613 rpc_out_channel_transition_to_state(outChannel,
614 CLIENT_OUT_CHANNEL_STATE_NEGOTIATED);
618 if (!rts_send_CONN_A1_pdu(rpc))
620 http_response_free(response);
621 WLog_ERR(TAG,
"rpc_send_CONN_A1_pdu error!");
625 rpc_out_channel_transition_to_state(outChannel, CLIENT_OUT_CHANNEL_STATE_OPENED);
627 if (inChannel->State == CLIENT_IN_CHANNEL_STATE_OPENED)
629 rpc_virtual_connection_transition_to_state(
630 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
637 http_response_free(response);
639 else if (connection->State == VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT)
642 if (WaitForSingleObject(outChannelEvent, 0) != WAIT_OBJECT_0)
645 response = http_response_recv(outChannel->common.tls, FALSE);
650 const UINT16 statusCode = http_response_get_status_code(response);
651 if (statusCode != HTTP_STATUS_OK)
653 http_response_log_error_status(WLog_Get(TAG), WLOG_ERROR, response);
655 if (statusCode == HTTP_STATUS_DENIED)
657 rdpContext* context = transport_get_context(rpc->transport);
658 freerdp_set_last_error_if_not(context, FREERDP_ERROR_CONNECT_ACCESS_DENIED);
661 http_response_free(response);
665 http_response_free(response);
666 rpc_virtual_connection_transition_to_state(rpc, rpc->VirtualConnection,
667 VIRTUAL_CONNECTION_STATE_WAIT_A3W);
672 wStream* fragment = rpc->client->ReceiveFragment;
679 while (Stream_GetPosition(fragment) < RPC_COMMON_FIELDS_LENGTH)
681 status = rpc_channel_read(&outChannel->common, fragment,
682 RPC_COMMON_FIELDS_LENGTH - Stream_GetPosition(fragment));
687 if (Stream_GetPosition(fragment) < RPC_COMMON_FIELDS_LENGTH)
691 pos = Stream_GetPosition(fragment);
692 Stream_SetPosition(fragment, 0);
695 const rts_pdu_status_t rc = rts_read_common_pdu_header(fragment, &header, TRUE);
696 if (rc == RTS_PDU_FAIL)
699 Stream_SetPosition(fragment, pos);
701 if (header.frag_length > rpc->max_recv_frag)
704 "rpc_client_recv: invalid fragment size: %" PRIu16
" (max: %" PRIu16
")",
705 header.frag_length, rpc->max_recv_frag);
706 winpr_HexDump(TAG, WLOG_ERROR, Stream_Buffer(fragment),
707 Stream_GetPosition(fragment));
711 while (Stream_GetPosition(fragment) < header.frag_length)
713 status = rpc_channel_read(&outChannel->common, fragment,
714 header.frag_length - Stream_GetPosition(fragment));
718 WLog_ERR(TAG,
"error reading fragment body");
722 if (Stream_GetPosition(fragment) < header.frag_length)
728 status = rpc_client_recv_fragment(rpc, fragment);
734 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_RECYCLED &&
735 connection->NonDefaultOutChannel)
737 rpc_channel_free(&connection->DefaultOutChannel->common);
738 connection->DefaultOutChannel = connection->NonDefaultOutChannel;
739 connection->NonDefaultOutChannel =
nullptr;
740 rpc_out_channel_transition_to_state(connection->DefaultOutChannel,
741 CLIENT_OUT_CHANNEL_STATE_OPENED);
742 rpc_virtual_connection_transition_to_state(
743 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
747 Stream_SetPosition(fragment, 0);
755static SSIZE_T rpc_client_nondefault_out_channel_recv(rdpRpc* rpc)
758 HttpResponse* response =
nullptr;
760 HANDLE nextOutChannelEvent =
nullptr;
761 nextOutChannel = rpc->VirtualConnection->NonDefaultOutChannel;
762 BIO_get_event(nextOutChannel->common.tls->bio, &nextOutChannelEvent);
764 if (WaitForSingleObject(nextOutChannelEvent, 0) != WAIT_OBJECT_0)
767 response = http_response_recv(nextOutChannel->common.tls, TRUE);
771 switch (nextOutChannel->State)
773 case CLIENT_OUT_CHANNEL_STATE_SECURITY:
774 if (rpc_ncacn_http_recv_out_channel_response(&nextOutChannel->common, response))
776 if (rpc_ncacn_http_send_out_channel_request(&nextOutChannel->common, TRUE))
778 if (rpc_ncacn_http_is_final_request(&nextOutChannel->common))
780 rpc_ncacn_http_auth_uninit(&nextOutChannel->common);
782 if (rts_send_OUT_R1_A3_pdu(rpc))
785 rpc_out_channel_transition_to_state(
786 nextOutChannel, CLIENT_OUT_CHANNEL_STATE_OPENED_A6W);
790 WLog_ERR(TAG,
"rts_send_OUT_R1/A3_pdu failure");
800 WLog_ERR(TAG,
"rpc_ncacn_http_send_out_channel_request failure");
805 WLog_ERR(TAG,
"rpc_ncacn_http_recv_out_channel_response failure");
810 case CLIENT_OUT_CHANNEL_STATE_INITIAL:
811 case CLIENT_OUT_CHANNEL_STATE_CONNECTED:
812 case CLIENT_OUT_CHANNEL_STATE_NEGOTIATED:
815 "rpc_client_nondefault_out_channel_recv: Unexpected message %08" PRIx32,
816 nextOutChannel->State);
820 http_response_free(response);
826int rpc_client_out_channel_recv(rdpRpc* rpc)
831 if (connection->DefaultOutChannel)
833 status = rpc_client_default_out_channel_recv(rpc);
839 if (connection->NonDefaultOutChannel)
841 status = rpc_client_nondefault_out_channel_recv(rpc);
850int rpc_client_in_channel_recv(rdpRpc* rpc)
853 HttpResponse* response =
nullptr;
856 HANDLE InChannelEvent =
nullptr;
858 inChannel = connection->DefaultInChannel;
859 outChannel = connection->DefaultOutChannel;
860 BIO_get_event(inChannel->common.tls->bio, &InChannelEvent);
862 if (WaitForSingleObject(InChannelEvent, 0) != WAIT_OBJECT_0)
865 if (inChannel->State < CLIENT_IN_CHANNEL_STATE_OPENED)
867 response = http_response_recv(inChannel->common.tls, TRUE);
872 if (inChannel->State == CLIENT_IN_CHANNEL_STATE_SECURITY)
874 if (!rpc_ncacn_http_recv_in_channel_response(&inChannel->common, response))
876 WLog_ERR(TAG,
"rpc_ncacn_http_recv_in_channel_response failure");
877 http_response_free(response);
883 if (!rpc_ncacn_http_send_in_channel_request(&inChannel->common))
885 WLog_ERR(TAG,
"rpc_ncacn_http_send_in_channel_request failure");
886 http_response_free(response);
890 if (rpc_ncacn_http_is_final_request(&inChannel->common))
892 rpc_ncacn_http_auth_uninit(&inChannel->common);
893 rpc_in_channel_transition_to_state(inChannel, CLIENT_IN_CHANNEL_STATE_NEGOTIATED);
897 if (!rts_send_CONN_B1_pdu(rpc))
899 WLog_ERR(TAG,
"rpc_send_CONN_B1_pdu error!");
900 http_response_free(response);
904 rpc_in_channel_transition_to_state(inChannel, CLIENT_IN_CHANNEL_STATE_OPENED);
906 if (outChannel->State == CLIENT_OUT_CHANNEL_STATE_OPENED)
908 rpc_virtual_connection_transition_to_state(
909 rpc, connection, VIRTUAL_CONNECTION_STATE_OUT_CHANNEL_WAIT);
916 http_response_free(response);
920 response = http_response_recv(inChannel->common.tls, TRUE);
926 http_response_free(response);
944 ArrayList_Lock(client->ClientCallList);
945 const size_t count = ArrayList_Count(client->ClientCallList);
947 for (
size_t index = 0; index < count; index++)
949 clientCall = (
RpcClientCall*)ArrayList_GetItem(client->ClientCallList, index);
951 if (clientCall->CallId == CallId)
955 ArrayList_Unlock(client->ClientCallList);
959RpcClientCall* rpc_client_call_new(UINT32 CallId, UINT32 OpNum)
967 clientCall->CallId = CallId;
968 clientCall->OpNum = OpNum;
969 clientCall->State = RPC_CLIENT_CALL_STATE_SEND_PDUS;
978static void rpc_array_client_call_free(
void* call)
983int rpc_in_channel_send_pdu(
RpcInChannel* inChannel,
const BYTE* buffer,
size_t length)
989 SSIZE_T status = rpc_channel_write(&inChannel->common, buffer, length);
994 Stream_StaticConstInit(&s, buffer, length);
995 const rts_pdu_status_t rc = rts_read_common_pdu_header(&s, &header, FALSE);
996 if (rc != RTS_PDU_VALID)
999 clientCall = rpc_client_call_find_by_id(inChannel->common.client, header.call_id);
1003 clientCall->State = RPC_CLIENT_CALL_STATE_DISPATCHED;
1012 if (header.ptype == PTYPE_REQUEST)
1014 const uint32_t ustatus = WINPR_ASSERTING_INT_CAST(uint32_t, status);
1015 inChannel->BytesSent += ustatus;
1016 inChannel->SenderAvailableWindow -= ustatus;
1019 if (status > INT32_MAX)
1024BOOL rpc_client_write_call(rdpRpc* rpc,
wStream* s, UINT16 opnum)
1027 BYTE* buffer =
nullptr;
1028 size_t stub_data_pad = 0;
1030 SecBuffer ciphertext = WINPR_C_ARRAY_INIT;
1032 rdpCredsspAuth* auth =
nullptr;
1045 connection = rpc->VirtualConnection;
1049 WLog_ERR(TAG,
"invalid auth context");
1056 inChannel = connection->DefaultInChannel;
1061 Stream_SealLength(s);
1064 const size_t length = Stream_Length(s);
1065 if (length > UINT32_MAX)
1069 const size_t asize = credssp_auth_trailer_size(auth);
1070 request_pdu.header = rpc_pdu_header_init(rpc);
1071 request_pdu.header.ptype = PTYPE_REQUEST;
1072 request_pdu.header.pfc_flags = PFC_FIRST_FRAG | PFC_LAST_FRAG;
1073 request_pdu.header.auth_length = (UINT16)asize;
1075 request_pdu.header.call_id = rpc->CallId++;
1076 request_pdu.alloc_hint = (UINT32)length;
1077 request_pdu.p_cont_id = 0x0000;
1078 request_pdu.opnum = opnum;
1079 clientCall = rpc_client_call_new(request_pdu.header.call_id, request_pdu.opnum);
1084 if (!ArrayList_Append(rpc->client->ClientCallList, clientCall))
1086 rpc_client_call_free(clientCall);
1091 if (request_pdu.opnum == TsProxySetupReceivePipeOpnum)
1092 rpc->PipeCallId = request_pdu.header.call_id;
1094 request_pdu.stub_data = Stream_Buffer(s);
1096 stub_data_pad = rpc_offset_align(&offset, 8);
1100 const size_t alg = rpc_offset_align(&offset, 4);
1101 WINPR_ASSERT(alg <= UINT8_MAX);
1102 request_pdu.auth_verifier.auth_pad_length = (UINT8)alg;
1104 request_pdu.auth_verifier.auth_type =
1105 rpc_auth_pkg_to_security_provider(credssp_auth_pkg_name(rpc->auth));
1106 request_pdu.auth_verifier.auth_level = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
1107 request_pdu.auth_verifier.auth_reserved = 0x00;
1108 request_pdu.auth_verifier.auth_context_id = 0x00000000;
1109 offset += (8 + request_pdu.header.auth_length);
1111 if (offset > UINT16_MAX)
1113 request_pdu.header.frag_length = (UINT16)offset;
1114 buffer = (BYTE*)calloc(1, request_pdu.header.frag_length);
1119 CopyMemory(buffer, &request_pdu, 24);
1121 rpc_offset_pad(&offset, stub_data_pad);
1122 CopyMemory(&buffer[offset], request_pdu.stub_data, length);
1126 rpc_offset_pad(&offset, request_pdu.auth_verifier.auth_pad_length);
1127 CopyMemory(&buffer[offset], &request_pdu.auth_verifier.auth_type, 8);
1130 if (offset > request_pdu.header.frag_length)
1133 plaintext.pvBuffer = buffer;
1134 plaintext.cbBuffer = (UINT32)offset;
1135 plaintext.BufferType = SECBUFFER_READONLY;
1139 if (!credssp_auth_encrypt(auth, &plaintext, &ciphertext, &size, rpc->SendSeqNum++))
1142 if (offset + size > request_pdu.header.frag_length)
1144 sspi_SecBufferFree(&ciphertext);
1148 CopyMemory(&buffer[offset], ciphertext.pvBuffer, size);
1152 sspi_SecBufferFree(&ciphertext);
1154 if (rpc_in_channel_send_pdu(inChannel, buffer, request_pdu.header.frag_length) < 0)
1160 Stream_Free(s, TRUE);
1164static BOOL rpc_client_resolve_gateway(rdpSettings* settings,
char** host, UINT16* port,
1167 struct addrinfo* result =
nullptr;
1169 if (!settings || !host || !port || !isProxy)
1177 *isProxy = proxy_prepare(settings, &peerHostname, port, &proxyUsername, &proxyPassword);
1178 result = freerdp_tcp_resolve_host(peerHostname, *port, 0);
1184 freerdp_tcp_address_to_string((
const struct sockaddr_storage*)result->ai_addr,
nullptr);
1185 freeaddrinfo(result);
1190RpcClient* rpc_client_new(rdpContext* context, UINT32 max_recv_frag)
1198 if (!rpc_client_resolve_gateway(context->settings, &client->host, &client->port,
1202 client->context = context;
1204 if (!client->context)
1207 client->pdu = rpc_pdu_new();
1212 client->ReceiveFragment = Stream_New(
nullptr, max_recv_frag);
1214 if (!client->ReceiveFragment)
1217 client->PipeEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr);
1219 if (!client->PipeEvent)
1222 if (!ringbuffer_init(&(client->ReceivePipe), 4096))
1225 if (!InitializeCriticalSectionAndSpinCount(&(client->PipeLock), 4000))
1228 client->ClientCallList = ArrayList_New(TRUE);
1230 if (!client->ClientCallList)
1233 obj = ArrayList_Object(client->ClientCallList);
1237 WINPR_PRAGMA_DIAG_PUSH
1238 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
1239 rpc_client_free(client);
1240 WINPR_PRAGMA_DIAG_POP
1251 if (client->ReceiveFragment)
1252 Stream_Free(client->ReceiveFragment, TRUE);
1254 if (client->PipeEvent)
1255 (void)CloseHandle(client->PipeEvent);
1257 ringbuffer_destroy(&(client->ReceivePipe));
1258 DeleteCriticalSection(&(client->PipeLock));
1261 rpc_pdu_free(client->pdu);
1263 if (client->ClientCallList)
1264 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