23#include <freerdp/config.h>
32#include <winpr/assert.h>
33#include <winpr/stream.h>
35#include <freerdp/api.h>
36#include <freerdp/log.h>
37#include <freerdp/crypto/per.h>
45#include "../cache/pointer.h"
46#include "../cache/palette.h"
47#include "../cache/bitmap.h"
49#define TAG FREERDP_TAG("core.fastpath")
51enum FASTPATH_INPUT_ENCRYPTION_FLAGS
53 FASTPATH_INPUT_SECURE_CHECKSUM = 0x1,
54 FASTPATH_INPUT_ENCRYPTED = 0x2
57enum FASTPATH_OUTPUT_ENCRYPTION_FLAGS
59 FASTPATH_OUTPUT_SECURE_CHECKSUM = 0x1,
60 FASTPATH_OUTPUT_ENCRYPTED = 0x2
83static const char*
const FASTPATH_UPDATETYPE_STRINGS[] = {
89 "System Pointer Hidden",
90 "System Pointer Default",
98static const char* fastpath_update_to_string(UINT8 update)
100 if (update >= ARRAYSIZE(FASTPATH_UPDATETYPE_STRINGS))
103 return FASTPATH_UPDATETYPE_STRINGS[update];
106static BOOL fastpath_read_update_header(
wStream* s, BYTE* updateCode, BYTE* fragmentation,
109 BYTE updateHeader = 0;
111 if (!s || !updateCode || !fragmentation || !compression)
114 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
117 Stream_Read_UINT8(s, updateHeader);
118 *updateCode = updateHeader & 0x0F;
119 *fragmentation = (updateHeader >> 4) & 0x03;
120 *compression = (updateHeader >> 6) & 0x03;
126 BYTE updateHeader = 0;
127 WINPR_ASSERT(fpUpdateHeader);
129 updateHeader |= fpUpdateHeader->updateCode & 0x0F;
130 updateHeader |= (fpUpdateHeader->fragmentation & 0x03) << 4;
131 updateHeader |= (fpUpdateHeader->compression & 0x03) << 6;
133 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 1))
135 Stream_Write_UINT8(s, updateHeader);
137 if (fpUpdateHeader->compression)
139 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 1))
142 Stream_Write_UINT8(s, fpUpdateHeader->compressionFlags);
145 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 2))
148 Stream_Write_UINT16(s, fpUpdateHeader->size);
154 WINPR_ASSERT(fpUpdateHeader);
155 return (fpUpdateHeader->compression) ? 4 : 3;
158static BOOL fastpath_write_update_pdu_header(
wStream* s,
162 BYTE fpOutputHeader = 0;
163 WINPR_ASSERT(fpUpdatePduHeader);
166 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 3))
169 fpOutputHeader |= (fpUpdatePduHeader->action & 0x03);
170 fpOutputHeader |= (fpUpdatePduHeader->secFlags & 0x03) << 6;
171 Stream_Write_UINT8(s, fpOutputHeader);
172 Stream_Write_UINT8(s, 0x80 | (fpUpdatePduHeader->length >> 8));
173 Stream_Write_UINT8(s, fpUpdatePduHeader->length & 0xFF);
175 if (fpUpdatePduHeader->secFlags)
177 WINPR_ASSERT(rdp->settings);
179 ENCRYPTION_METHOD_FIPS)
181 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 4))
184 Stream_Write(s, fpUpdatePduHeader->fipsInformation, 4);
187 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
190 Stream_Write(s, fpUpdatePduHeader->dataSignature, 8);
201 if (!fpUpdatePduHeader || !rdp)
204 if (fpUpdatePduHeader->secFlags)
208 WINPR_ASSERT(rdp->settings);
210 ENCRYPTION_METHOD_FIPS)
217BOOL fastpath_read_header_rdp(rdpFastPath* fastpath,
wStream* s, UINT16* length)
224 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
227 Stream_Read_UINT8(s, header);
231 fastpath->encryptionFlags = (header & 0xC0) >> 6;
232 fastpath->numberEvents = (header & 0x3C) >> 2;
235 if (!per_read_length(s, length))
238 const size_t pos = Stream_GetPosition(s);
242 *length = *length - (UINT16)pos;
246static BOOL fastpath_recv_orders(rdpUpdate* update,
wStream* s)
248 UINT16 numberOrders = 0;
252 WLog_ERR(TAG,
"Invalid arguments");
258 WLog_ERR(TAG,
"Invalid configuration");
262 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
265 Stream_Read_UINT16(s, numberOrders);
267 while (numberOrders > 0)
269 if (!update_recv_order(update, s))
278static BOOL fastpath_recv_update_common(rdpUpdate* update,
wStream* s)
281 UINT16 updateType = 0;
282 BOOL defaultReturn = 0;
288 if (!update || !update->context)
291 rdpContext* context = update->context;
295 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
298 Stream_Read_UINT16(s, updateType);
301 case UPDATE_TYPE_BITMAP:
303 BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
308 up->stats.base[RDP_STATS_BITMAP_UPDATE]++;
309 rc = IFCALLRESULT(defaultReturn, update->BitmapUpdate, context, bitmap_update);
310 free_bitmap_update(context, bitmap_update);
314 case UPDATE_TYPE_PALETTE:
321 up->stats.base[RDP_STATS_PALETTE]++;
322 rc = IFCALLRESULT(defaultReturn, update->Palette, context, palette_update);
323 free_palette_update(context, palette_update);
334static BOOL fastpath_recv_update_synchronize(WINPR_ATTR_UNUSED rdpFastPath* fastpath,
wStream* s)
338 WINPR_ASSERT(fastpath);
341 const size_t len = Stream_GetRemainingLength(s);
342 const size_t skip = MIN(2, len);
343 return Stream_SafeSeek(s, skip);
346static BOOL fastpath_recv_update_paint_block(rdpUpdate* update,
wStream* s,
347 BOOL (*fkt)(rdpUpdate*,
wStream*))
350 if (!update_begin_paint(update))
353 BOOL res = fkt(update, s);
354 if (!update_end_paint(update))
359static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode,
wStream* s)
364 if (!fastpath || !fastpath->rdp || !s)
367 Stream_SealLength(s);
368 Stream_ResetPosition(s);
370 rdpUpdate* update = fastpath->rdp->update;
372 if (!update || !update->pointer || !update->context)
377 rdpContext* context = update->context;
378 WINPR_ASSERT(context);
380 rdpPointerUpdate* pointer = update->pointer;
381 WINPR_ASSERT(pointer);
384 DEBUG_RDP(fastpath->rdp,
"recv Fast-Path %s Update (0x%02" PRIX8
"), length:%" PRIuz
"",
385 fastpath_update_to_string(updateCode), updateCode, Stream_GetRemainingLength(s));
388 const BOOL defaultReturn =
392 case FASTPATH_UPDATETYPE_ORDERS:
393 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_orders);
396 case FASTPATH_UPDATETYPE_BITMAP:
397 case FASTPATH_UPDATETYPE_PALETTE:
398 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_update_common);
401 case FASTPATH_UPDATETYPE_SYNCHRONIZE:
402 if (!fastpath_recv_update_synchronize(fastpath, s))
403 WLog_ERR(TAG,
"fastpath_recv_update_synchronize failure but we continue");
406 up->stats.base[RDP_STATS_SYNC]++;
407 rc = IFCALLRESULT(TRUE, update->Synchronize, context);
412 case FASTPATH_UPDATETYPE_SURFCMDS:
413 status = fastpath_recv_update_paint_block(update, s, update_recv_surfcmds);
417 case FASTPATH_UPDATETYPE_PTR_NULL:
420 pointer_system.type = SYSPTR_NULL;
421 up->stats.base[RDP_STATS_POINTER_SYSTEM]++;
422 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
426 case FASTPATH_UPDATETYPE_PTR_DEFAULT:
429 pointer_system.type = SYSPTR_DEFAULT;
430 up->stats.base[RDP_STATS_POINTER_DEFAULT]++;
431 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
435 case FASTPATH_UPDATETYPE_PTR_POSITION:
439 if (pointer_position)
441 up->stats.base[RDP_STATS_POINTER_POSITION]++;
442 rc = IFCALLRESULT(defaultReturn, pointer->PointerPosition, context,
444 free_pointer_position_update(context, pointer_position);
449 case FASTPATH_UPDATETYPE_COLOR:
455 up->stats.base[RDP_STATS_POINTER_COLOR]++;
456 rc = IFCALLRESULT(defaultReturn, pointer->PointerColor, context, pointer_color);
457 free_pointer_color_update(context, pointer_color);
462 case FASTPATH_UPDATETYPE_CACHED:
468 up->stats.base[RDP_STATS_POINTER_CACHED]++;
469 rc = IFCALLRESULT(defaultReturn, pointer->PointerCached, context, pointer_cached);
470 free_pointer_cached_update(context, pointer_cached);
475 case FASTPATH_UPDATETYPE_POINTER:
481 up->stats.base[RDP_STATS_POINTER_NEW]++;
482 rc = IFCALLRESULT(defaultReturn, pointer->PointerNew, context, pointer_new);
483 free_pointer_new_update(context, pointer_new);
488 case FASTPATH_UPDATETYPE_LARGE_POINTER:
494 up->stats.base[RDP_STATS_POINTER_LARGE]++;
495 rc = IFCALLRESULT(defaultReturn, pointer->PointerLarge, context, pointer_large);
496 free_pointer_large_update(context, pointer_large);
504 Stream_ResetPosition(s);
507 WLog_ERR(TAG,
"Fastpath update %s [%" PRIx8
"] failed, status %d",
508 fastpath_update_to_string(updateCode), updateCode, status);
515static int fastpath_recv_update_data(rdpFastPath* fastpath,
wStream* s)
520 BYTE fragmentation = 0;
521 BYTE compression = 0;
522 BYTE compressionFlags = 0;
524 const BYTE* pDstData =
nullptr;
529 rdpRdp* rdp = fastpath->rdp;
534 rdpTransport* transport = rdp->transport;
539 if (!fastpath_read_update_header(s, &updateCode, &fragmentation, &compression))
542 if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
544 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
547 Stream_Read_UINT8(s, compressionFlags);
550 compressionFlags = 0;
552 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
555 Stream_Read_UINT16(s, size);
557 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
560 const int bulkStatus =
561 bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags);
562 Stream_Seek(s, size);
566 WLog_ERR(TAG,
"bulk_decompress() failed");
570 if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize))
573 Stream_Write(fastpath->updateData, pDstData, DstSize);
575 if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
577 if (fastpath->fragmentation != -1)
579 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_SINGLE");
583 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
587 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
593 rdpContext* context =
nullptr;
594 const size_t totalSize = Stream_GetPosition(fastpath->updateData);
596 context = transport_get_context(transport);
597 WINPR_ASSERT(context);
598 WINPR_ASSERT(context->settings);
604 TAG,
"Total size (%" PRIuz
") exceeds MultifragMaxRequestSize (%" PRIu32
")",
610 if (fragmentation == FASTPATH_FRAGMENT_FIRST)
612 if (fastpath->fragmentation != -1)
614 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_FIRST");
618 fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
620 else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
622 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
623 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
625 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_NEXT");
629 fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
631 else if (fragmentation == FASTPATH_FRAGMENT_LAST)
633 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
634 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
636 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_LAST");
640 fastpath->fragmentation = -1;
641 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
645 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
656state_run_t fastpath_recv_updates(rdpFastPath* fastpath,
wStream* s)
658 state_run_t rc = STATE_RUN_FAILED;
661 WINPR_ASSERT(fastpath);
662 WINPR_ASSERT(fastpath->rdp);
664 while (Stream_GetRemainingLength(s) >= 3)
666 if (fastpath_recv_update_data(fastpath, s) < 0)
668 WLog_ERR(TAG,
"fastpath_recv_update_data() fail");
669 rc = STATE_RUN_FAILED;
674 rc = STATE_RUN_SUCCESS;
680static BOOL fastpath_read_input_event_header(
wStream* s, BYTE* eventFlags, BYTE* eventCode)
682 BYTE eventHeader = 0;
685 WINPR_ASSERT(eventFlags);
686 WINPR_ASSERT(eventCode);
688 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
691 Stream_Read_UINT8(s, eventHeader);
692 *eventFlags = (eventHeader & 0x1F);
693 *eventCode = (eventHeader >> 5);
697static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
699 WINPR_ASSERT(fastpath);
700 WINPR_ASSERT(fastpath->rdp);
701 WINPR_ASSERT(fastpath->rdp->input);
704 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
707 rdpInput* input = fastpath->rdp->input;
709 const UINT8 code = Stream_Get_UINT8(s);
712 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
713 flags |= KBD_FLAGS_RELEASE;
715 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
716 flags |= KBD_FLAGS_EXTENDED;
718 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
719 flags |= KBD_FLAGS_EXTENDED1;
721 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
724static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath,
wStream* s,
725 WINPR_ATTR_UNUSED BYTE eventFlags)
727 rdpInput* input =
nullptr;
728 UINT16 pointerFlags = 0;
731 WINPR_ASSERT(fastpath);
732 WINPR_ASSERT(fastpath->rdp);
733 WINPR_ASSERT(fastpath->rdp->input);
736 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
739 input = fastpath->rdp->input;
741 Stream_Read_UINT16(s, pointerFlags);
742 Stream_Read_UINT16(s, xPos);
743 Stream_Read_UINT16(s, yPos);
744 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
747static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath,
wStream* s,
748 WINPR_ATTR_UNUSED BYTE eventFlags)
750 rdpInput* input =
nullptr;
751 UINT16 pointerFlags = 0;
754 WINPR_ASSERT(fastpath);
755 WINPR_ASSERT(fastpath->rdp);
756 WINPR_ASSERT(fastpath->rdp->context);
757 WINPR_ASSERT(fastpath->rdp->input);
760 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
763 input = fastpath->rdp->input;
765 Stream_Read_UINT16(s, pointerFlags);
766 Stream_Read_INT16(s, xDelta);
767 Stream_Read_INT16(s, yDelta);
772 "Received relative mouse event(flags=0x%04" PRIx16
", xPos=%" PRId16
773 ", yPos=%" PRId16
"), but we did not announce support for that",
774 pointerFlags, xDelta, yDelta);
778 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
781static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath,
wStream* s,
782 WINPR_ATTR_UNUSED BYTE eventFlags)
784 WINPR_ASSERT(fastpath);
785 WINPR_ASSERT(fastpath->rdp);
786 WINPR_ASSERT(fastpath->rdp->context);
787 WINPR_ASSERT(fastpath->rdp->input);
790 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
793 rdpInput* input = fastpath->rdp->input;
795 UINT32 timestampMS = 0;
796 Stream_Read_UINT32(s, timestampMS);
801 "Received qoe event(timestamp=%" PRIu32
802 "ms), but we did not announce support for that",
807 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
810static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath,
wStream* s,
811 WINPR_ATTR_UNUSED BYTE eventFlags)
813 rdpInput* input =
nullptr;
814 UINT16 pointerFlags = 0;
818 WINPR_ASSERT(fastpath);
819 WINPR_ASSERT(fastpath->rdp);
820 WINPR_ASSERT(fastpath->rdp->context);
821 WINPR_ASSERT(fastpath->rdp->input);
824 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
827 input = fastpath->rdp->input;
829 Stream_Read_UINT16(s, pointerFlags);
830 Stream_Read_UINT16(s, xPos);
831 Stream_Read_UINT16(s, yPos);
836 "Received extended mouse event(flags=0x%04" PRIx16
", xPos=%" PRIu16
837 ", yPos=%" PRIu16
"), but we did not announce support for that",
838 pointerFlags, xPos, yPos);
842 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
845static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED
wStream* s,
848 rdpInput* input =
nullptr;
850 WINPR_ASSERT(fastpath);
851 WINPR_ASSERT(fastpath->rdp);
852 WINPR_ASSERT(fastpath->rdp->input);
855 input = fastpath->rdp->input;
856 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
859static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
861 UINT16 unicodeCode = 0;
864 WINPR_ASSERT(fastpath);
867 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
870 Stream_Read_UINT16(s, unicodeCode);
873 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
874 flags |= KBD_FLAGS_RELEASE;
876 WINPR_ASSERT(fastpath->rdp);
877 WINPR_ASSERT(fastpath->rdp);
878 WINPR_ASSERT(fastpath->rdp->input);
879 return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input,
883static BOOL fastpath_recv_input_event(rdpFastPath* fastpath,
wStream* s)
888 WINPR_ASSERT(fastpath);
891 if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
896 case FASTPATH_INPUT_EVENT_SCANCODE:
897 if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
902 case FASTPATH_INPUT_EVENT_MOUSE:
903 if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
908 case FASTPATH_INPUT_EVENT_MOUSEX:
909 if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
914 case FASTPATH_INPUT_EVENT_SYNC:
915 if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
920 case FASTPATH_INPUT_EVENT_UNICODE:
921 if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
926 case TS_FP_RELPOINTER_EVENT:
927 if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
932 case TS_FP_QOETIMESTAMP_EVENT:
933 if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
938 WLog_ERR(TAG,
"Unknown eventCode %" PRIu8
"", eventCode);
945state_run_t fastpath_recv_inputs(rdpFastPath* fastpath,
wStream* s)
947 WINPR_ASSERT(fastpath);
950 if (fastpath->numberEvents == 0)
956 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
957 return STATE_RUN_FAILED;
959 Stream_Read_UINT8(s, fastpath->numberEvents);
962 for (BYTE i = 0; i < fastpath->numberEvents; i++)
964 if (!fastpath_recv_input_event(fastpath, s))
965 return STATE_RUN_FAILED;
968 return STATE_RUN_SUCCESS;
971static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp)
973 UINT32 sec_bytes = 0;
983 ENCRYPTION_METHOD_FIPS)
990wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags)
992 if (!fastpath || !fastpath->rdp)
995 rdpRdp* rdp = fastpath->rdp;
996 wStream* s = transport_send_stream_init(rdp->transport, 256);
1005 *sec_flags |= SEC_ENCRYPT;
1007 if (rdp->do_secure_checksum)
1008 *sec_flags |= SEC_SECURE_CHECKSUM;
1011 Stream_Seek(s, fastpath_get_sec_bytes(rdp));
1015wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode,
1019 s = fastpath_input_pdu_init_header(fastpath, sec_flags);
1024 WINPR_ASSERT(eventCode < 8);
1025 WINPR_ASSERT(eventFlags < 0x20);
1026 Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5)));
1030BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath,
wStream* s,
size_t iNumEvents,
1034 BYTE eventHeader = 0;
1035 BOOL should_unlock = FALSE;
1036 rdpRdp* rdp =
nullptr;
1038 WINPR_ASSERT(iNumEvents > 0);
1045 rdp = fastpath->rdp;
1049 const CONNECTION_STATE state = rdp_get_state(rdp);
1050 if (!rdp_is_active_state(rdp))
1052 WLog_WARN(TAG,
"called before activation [%s]", rdp_state_string(state));
1062 if (iNumEvents > 15)
1066 size_t length = Stream_GetPosition(s);
1068 if (length >= (2u << 14))
1070 WLog_ERR(TAG,
"Maximum FastPath PDU length is 32767");
1074 eventHeader = FASTPATH_INPUT_ACTION_FASTPATH;
1075 eventHeader |= (iNumEvents << 2);
1077 if (sec_flags & SEC_ENCRYPT)
1078 eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6);
1080 if (sec_flags & SEC_SECURE_CHECKSUM)
1081 eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
1083 Stream_ResetPosition(s);
1084 Stream_Write_UINT8(s, eventHeader);
1088 if (sec_flags & SEC_ENCRYPT)
1091 should_unlock = TRUE;
1093 const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp);
1094 if (sec_bytes + 3ULL > length)
1097 BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes;
1098 const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes);
1100 WINPR_ASSERT(rdp->settings);
1102 ENCRYPTION_METHOD_FIPS)
1106 if ((pad = 8 - (fpInputEvents_length % 8)) == 8)
1109 Stream_Write_UINT16(s, 0x10);
1110 Stream_Write_UINT8(s, 0x1);
1111 Stream_Write_UINT8(s, pad);
1113 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1116 if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s),
1121 memset(fpInputEvents + fpInputEvents_length, 0, pad);
1123 if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp))
1131 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1133 if (sec_flags & SEC_SECURE_CHECKSUM)
1134 res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1135 TRUE, Stream_Pointer(s), 8);
1137 res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1138 Stream_Pointer(s), 8);
1140 if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp))
1151 WINPR_ASSERT(length < UINT16_MAX);
1152 if (!Stream_SetPosition(s, 1))
1154 Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length);
1155 if (!Stream_SetPosition(s, length))
1157 Stream_SealLength(s);
1160 if (transport_write(rdp->transport, s) < 0)
1166 security_unlock(rdp);
1171BOOL fastpath_send_input_pdu(rdpFastPath* fastpath,
wStream* s, UINT16 sec_flags)
1173 return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags);
1176wStream* fastpath_update_pdu_init(rdpFastPath* fastpath)
1178 return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1181wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath)
1184 s = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1188BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode,
wStream* s,
1189 BOOL skipCompression)
1193 rdpSettings* settings =
nullptr;
1194 rdpRdp* rdp =
nullptr;
1195 UINT32 fpHeaderSize = 6;
1196 UINT32 fpUpdatePduHeaderSize = 0;
1197 UINT32 fpUpdateHeaderSize = 0;
1200 UINT16 sec_flags = 0;
1202 if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1205 rdp = fastpath->rdp;
1207 settings = rdp->settings;
1212 UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1216 const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1217 maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1221 size_t totalLength = Stream_GetPosition(s);
1222 Stream_ResetPosition(s);
1227 WLog_ERR(TAG,
"client does not support fast path output");
1235 "fast path update size (%" PRIuz
1236 ") exceeds the client's maximum request size (%" PRIu32
")",
1244 sec_flags |= SEC_ENCRYPT;
1246 if (rdp->do_secure_checksum)
1247 sec_flags |= SEC_SECURE_CHECKSUM;
1250 for (
int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1253 const BYTE* pDstData =
nullptr;
1254 UINT32 compressionFlags = 0;
1256 BYTE* pSignature =
nullptr;
1257 fpUpdatePduHeader.action = 0;
1258 fpUpdatePduHeader.secFlags = 0;
1259 fpUpdateHeader.compression = 0;
1260 fpUpdateHeader.compressionFlags = 0;
1261 fpUpdateHeader.updateCode = updateCode;
1262 fpUpdateHeader.size = (UINT16)(totalLength > maxLength) ? maxLength : (UINT16)totalLength;
1263 const BYTE* pSrcData = Stream_Pointer(s);
1264 UINT32 SrcSize = DstSize = fpUpdateHeader.size;
1265 BOOL should_unlock = FALSE;
1267 if (sec_flags & SEC_ENCRYPT)
1268 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1270 if (sec_flags & SEC_SECURE_CHECKSUM)
1271 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1275 if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1276 &compressionFlags) >= 0)
1278 if (compressionFlags)
1280 WINPR_ASSERT(compressionFlags <= UINT8_MAX);
1281 fpUpdateHeader.compressionFlags = (UINT8)compressionFlags;
1282 fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1287 if (!fpUpdateHeader.compression)
1289 pDstData = Stream_Pointer(s);
1290 DstSize = fpUpdateHeader.size;
1293 if (DstSize > UINT16_MAX)
1295 fpUpdateHeader.size = (UINT16)DstSize;
1296 totalLength -= SrcSize;
1298 if (totalLength == 0)
1299 fpUpdateHeader.fragmentation =
1300 (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
1302 fpUpdateHeader.fragmentation =
1303 (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
1305 fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1306 fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1307 fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1309 if (sec_flags & SEC_ENCRYPT)
1311 pSignature = Stream_Buffer(fs) + 3;
1314 ENCRYPTION_METHOD_FIPS)
1318 if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8)
1321 fpUpdatePduHeader.fipsInformation[0] = 0x10;
1322 fpUpdatePduHeader.fipsInformation[1] = 0x00;
1323 fpUpdatePduHeader.fipsInformation[2] = 0x01;
1324 fpUpdatePduHeader.fipsInformation[3] = pad;
1328 const size_t len = fpUpdateHeader.size + fpHeaderSize + pad;
1329 if (len > UINT16_MAX)
1332 fpUpdatePduHeader.length = (UINT16)len;
1333 Stream_ResetPosition(fs);
1334 if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1336 if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1339 if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (
size_t)DstSize + pad))
1341 Stream_Write(fs, pDstData, DstSize);
1344 Stream_Zero(fs, pad);
1347 if (sec_flags & SEC_ENCRYPT)
1351 should_unlock = TRUE;
1352 UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad;
1353 BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize;
1356 ENCRYPTION_METHOD_FIPS)
1359 if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp))
1362 if (!security_fips_encrypt(data, dataSize, rdp))
1368 if (sec_flags & SEC_SECURE_CHECKSUM)
1370 security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8);
1372 status = security_mac_signature(rdp, data, dataSize, pSignature, 8);
1374 if (!status || !security_encrypt(data, dataSize, rdp))
1380 Stream_SealLength(fs);
1382 if (transport_write(rdp->transport, fs) < 0)
1389 security_unlock(rdp);
1391 if (!res || !status)
1394 Stream_Seek(s, SrcSize);
1400rdpFastPath* fastpath_new(rdpRdp* rdp)
1402 rdpFastPath* fastpath =
nullptr;
1406 fastpath = (rdpFastPath*)calloc(1,
sizeof(rdpFastPath));
1411 fastpath->rdp = rdp;
1412 fastpath->fragmentation = -1;
1413 fastpath->fs = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1414 fastpath->updateData = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1416 if (!fastpath->fs || !fastpath->updateData)
1421 fastpath_free(fastpath);
1425void fastpath_free(rdpFastPath* fastpath)
1429 Stream_Free(fastpath->updateData, TRUE);
1430 Stream_Free(fastpath->fs, TRUE);
1435BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1437 WINPR_ASSERT(fastpath);
1438 return fastpath->encryptionFlags;
1441BOOL fastpath_decrypt(rdpFastPath* fastpath,
wStream* s, UINT16* length)
1443 WINPR_ASSERT(fastpath);
1444 if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1446 const UINT16 flags =
1447 (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1448 ? SEC_SECURE_CHECKSUM
1451 if (!rdp_decrypt(fastpath->rdp, s, length, flags))
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.