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);
519static int fastpath_recv_update_data(rdpFastPath* fastpath,
wStream* s)
524 BYTE fragmentation = 0;
525 BYTE compression = 0;
526 BYTE compressionFlags = 0;
528 const BYTE* pDstData =
nullptr;
533 rdpRdp* rdp = fastpath->rdp;
538 rdpTransport* transport = rdp->transport;
543 if (!fastpath_read_update_header(s, &updateCode, &fragmentation, &compression))
546 if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
548 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
551 Stream_Read_UINT8(s, compressionFlags);
554 compressionFlags = 0;
556 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
559 Stream_Read_UINT16(s, size);
561 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
564 const int bulkStatus =
565 bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags);
566 Stream_Seek(s, size);
570 WLog_ERR(TAG,
"bulk_decompress() failed");
574 if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize))
577 Stream_Write(fastpath->updateData, pDstData, DstSize);
579 if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
581 if (fastpath->fragmentation != -1)
583 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_SINGLE");
587 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
591 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
597 rdpContext* context =
nullptr;
598 const size_t totalSize = Stream_GetPosition(fastpath->updateData);
600 context = transport_get_context(transport);
601 WINPR_ASSERT(context);
602 WINPR_ASSERT(context->settings);
608 TAG,
"Total size (%" PRIuz
") exceeds MultifragMaxRequestSize (%" PRIu32
")",
614 if (fragmentation == FASTPATH_FRAGMENT_FIRST)
616 if (fastpath->fragmentation != -1)
618 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_FIRST");
622 fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
624 else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
626 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
627 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
629 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_NEXT");
633 fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
635 else if (fragmentation == FASTPATH_FRAGMENT_LAST)
637 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
638 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
640 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_LAST");
644 fastpath->fragmentation = -1;
645 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
649 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
660state_run_t fastpath_recv_updates(rdpFastPath* fastpath,
wStream* s)
662 state_run_t rc = STATE_RUN_FAILED;
665 WINPR_ASSERT(fastpath);
666 WINPR_ASSERT(fastpath->rdp);
668 while (Stream_GetRemainingLength(s) >= 3)
670 if (fastpath_recv_update_data(fastpath, s) < 0)
672 WLog_ERR(TAG,
"fastpath_recv_update_data() fail");
673 rc = STATE_RUN_FAILED;
678 rc = STATE_RUN_SUCCESS;
684static BOOL fastpath_read_input_event_header(
wStream* s, BYTE* eventFlags, BYTE* eventCode)
686 BYTE eventHeader = 0;
689 WINPR_ASSERT(eventFlags);
690 WINPR_ASSERT(eventCode);
692 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
695 Stream_Read_UINT8(s, eventHeader);
696 *eventFlags = (eventHeader & 0x1F);
697 *eventCode = (eventHeader >> 5);
701static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
703 WINPR_ASSERT(fastpath);
704 WINPR_ASSERT(fastpath->rdp);
705 WINPR_ASSERT(fastpath->rdp->input);
708 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
711 rdpInput* input = fastpath->rdp->input;
713 const UINT8 code = Stream_Get_UINT8(s);
716 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
717 flags |= KBD_FLAGS_RELEASE;
719 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
720 flags |= KBD_FLAGS_EXTENDED;
722 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
723 flags |= KBD_FLAGS_EXTENDED1;
725 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
728static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath,
wStream* s,
729 WINPR_ATTR_UNUSED BYTE eventFlags)
731 rdpInput* input =
nullptr;
732 UINT16 pointerFlags = 0;
735 WINPR_ASSERT(fastpath);
736 WINPR_ASSERT(fastpath->rdp);
737 WINPR_ASSERT(fastpath->rdp->input);
740 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
743 input = fastpath->rdp->input;
745 Stream_Read_UINT16(s, pointerFlags);
746 Stream_Read_UINT16(s, xPos);
747 Stream_Read_UINT16(s, yPos);
748 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
751static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath,
wStream* s,
752 WINPR_ATTR_UNUSED BYTE eventFlags)
754 rdpInput* input =
nullptr;
755 UINT16 pointerFlags = 0;
758 WINPR_ASSERT(fastpath);
759 WINPR_ASSERT(fastpath->rdp);
760 WINPR_ASSERT(fastpath->rdp->context);
761 WINPR_ASSERT(fastpath->rdp->input);
764 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
767 input = fastpath->rdp->input;
769 Stream_Read_UINT16(s, pointerFlags);
770 Stream_Read_INT16(s, xDelta);
771 Stream_Read_INT16(s, yDelta);
776 "Received relative mouse event(flags=0x%04" PRIx16
", xPos=%" PRId16
777 ", yPos=%" PRId16
"), but we did not announce support for that",
778 pointerFlags, xDelta, yDelta);
782 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
785static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath,
wStream* s,
786 WINPR_ATTR_UNUSED BYTE eventFlags)
788 WINPR_ASSERT(fastpath);
789 WINPR_ASSERT(fastpath->rdp);
790 WINPR_ASSERT(fastpath->rdp->context);
791 WINPR_ASSERT(fastpath->rdp->input);
794 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
797 rdpInput* input = fastpath->rdp->input;
799 UINT32 timestampMS = 0;
800 Stream_Read_UINT32(s, timestampMS);
805 "Received qoe event(timestamp=%" PRIu32
806 "ms), but we did not announce support for that",
811 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
814static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath,
wStream* s,
815 WINPR_ATTR_UNUSED BYTE eventFlags)
817 rdpInput* input =
nullptr;
818 UINT16 pointerFlags = 0;
822 WINPR_ASSERT(fastpath);
823 WINPR_ASSERT(fastpath->rdp);
824 WINPR_ASSERT(fastpath->rdp->context);
825 WINPR_ASSERT(fastpath->rdp->input);
828 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
831 input = fastpath->rdp->input;
833 Stream_Read_UINT16(s, pointerFlags);
834 Stream_Read_UINT16(s, xPos);
835 Stream_Read_UINT16(s, yPos);
840 "Received extended mouse event(flags=0x%04" PRIx16
", xPos=%" PRIu16
841 ", yPos=%" PRIu16
"), but we did not announce support for that",
842 pointerFlags, xPos, yPos);
846 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
849static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED
wStream* s,
852 rdpInput* input =
nullptr;
854 WINPR_ASSERT(fastpath);
855 WINPR_ASSERT(fastpath->rdp);
856 WINPR_ASSERT(fastpath->rdp->input);
859 input = fastpath->rdp->input;
860 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
863static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
865 UINT16 unicodeCode = 0;
868 WINPR_ASSERT(fastpath);
871 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
874 Stream_Read_UINT16(s, unicodeCode);
877 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
878 flags |= KBD_FLAGS_RELEASE;
880 WINPR_ASSERT(fastpath->rdp);
881 WINPR_ASSERT(fastpath->rdp);
882 WINPR_ASSERT(fastpath->rdp->input);
883 return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input,
887static BOOL fastpath_recv_input_event(rdpFastPath* fastpath,
wStream* s)
892 WINPR_ASSERT(fastpath);
895 if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
900 case FASTPATH_INPUT_EVENT_SCANCODE:
901 if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
906 case FASTPATH_INPUT_EVENT_MOUSE:
907 if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
912 case FASTPATH_INPUT_EVENT_MOUSEX:
913 if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
918 case FASTPATH_INPUT_EVENT_SYNC:
919 if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
924 case FASTPATH_INPUT_EVENT_UNICODE:
925 if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
930 case TS_FP_RELPOINTER_EVENT:
931 if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
936 case TS_FP_QOETIMESTAMP_EVENT:
937 if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
942 WLog_ERR(TAG,
"Unknown eventCode %" PRIu8
"", eventCode);
949state_run_t fastpath_recv_inputs(rdpFastPath* fastpath,
wStream* s)
951 WINPR_ASSERT(fastpath);
954 if (fastpath->numberEvents == 0)
960 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
961 return STATE_RUN_FAILED;
963 Stream_Read_UINT8(s, fastpath->numberEvents);
966 for (BYTE i = 0; i < fastpath->numberEvents; i++)
968 if (!fastpath_recv_input_event(fastpath, s))
969 return STATE_RUN_FAILED;
972 return STATE_RUN_SUCCESS;
975static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp)
977 UINT32 sec_bytes = 0;
987 ENCRYPTION_METHOD_FIPS)
994wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags)
996 if (!fastpath || !fastpath->rdp)
999 rdpRdp* rdp = fastpath->rdp;
1000 wStream* s = transport_send_stream_init(rdp->transport, 256);
1009 *sec_flags |= SEC_ENCRYPT;
1011 if (rdp->do_secure_checksum)
1012 *sec_flags |= SEC_SECURE_CHECKSUM;
1015 Stream_Seek(s, fastpath_get_sec_bytes(rdp));
1019wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode,
1023 s = fastpath_input_pdu_init_header(fastpath, sec_flags);
1028 WINPR_ASSERT(eventCode < 8);
1029 WINPR_ASSERT(eventFlags < 0x20);
1030 Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5)));
1034BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath,
wStream* s,
size_t iNumEvents,
1038 BYTE eventHeader = 0;
1039 BOOL should_unlock = FALSE;
1040 rdpRdp* rdp =
nullptr;
1042 WINPR_ASSERT(iNumEvents > 0);
1049 rdp = fastpath->rdp;
1053 const CONNECTION_STATE state = rdp_get_state(rdp);
1054 if (!rdp_is_active_state(rdp))
1056 WLog_WARN(TAG,
"called before activation [%s]", rdp_state_string(state));
1066 if (iNumEvents > 15)
1070 size_t length = Stream_GetPosition(s);
1072 if (length >= (2u << 14))
1074 WLog_ERR(TAG,
"Maximum FastPath PDU length is 32767");
1078 eventHeader = FASTPATH_INPUT_ACTION_FASTPATH;
1079 eventHeader |= (iNumEvents << 2);
1081 if (sec_flags & SEC_ENCRYPT)
1082 eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6);
1084 if (sec_flags & SEC_SECURE_CHECKSUM)
1085 eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
1087 Stream_ResetPosition(s);
1088 Stream_Write_UINT8(s, eventHeader);
1092 if (sec_flags & SEC_ENCRYPT)
1095 should_unlock = TRUE;
1097 const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp);
1098 if (sec_bytes + 3ULL > length)
1101 BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes;
1102 const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes);
1104 WINPR_ASSERT(rdp->settings);
1106 ENCRYPTION_METHOD_FIPS)
1110 if ((pad = 8 - (fpInputEvents_length % 8)) == 8)
1113 Stream_Write_UINT16(s, 0x10);
1114 Stream_Write_UINT8(s, 0x1);
1115 Stream_Write_UINT8(s, pad);
1117 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1120 if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s),
1125 memset(fpInputEvents + fpInputEvents_length, 0, pad);
1127 if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp))
1135 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1137 if (sec_flags & SEC_SECURE_CHECKSUM)
1138 res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1139 TRUE, Stream_Pointer(s), 8);
1141 res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1142 Stream_Pointer(s), 8);
1144 if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp))
1155 WINPR_ASSERT(length < UINT16_MAX);
1156 if (!Stream_SetPosition(s, 1))
1158 Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length);
1159 if (!Stream_SetPosition(s, length))
1161 Stream_SealLength(s);
1164 if (transport_write(rdp->transport, s) < 0)
1170 security_unlock(rdp);
1175BOOL fastpath_send_input_pdu(rdpFastPath* fastpath,
wStream* s, UINT16 sec_flags)
1177 return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags);
1180wStream* fastpath_update_pdu_init(rdpFastPath* fastpath)
1182 return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1185wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath)
1188 s = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1192BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode,
wStream* s,
1193 BOOL skipCompression)
1197 rdpSettings* settings =
nullptr;
1198 rdpRdp* rdp =
nullptr;
1199 UINT32 fpHeaderSize = 6;
1200 UINT32 fpUpdatePduHeaderSize = 0;
1201 UINT32 fpUpdateHeaderSize = 0;
1204 UINT16 sec_flags = 0;
1206 if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1209 rdp = fastpath->rdp;
1211 settings = rdp->settings;
1216 UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1220 const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1221 maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1225 size_t totalLength = Stream_GetPosition(s);
1226 Stream_ResetPosition(s);
1231 WLog_ERR(TAG,
"client does not support fast path output");
1239 "fast path update size (%" PRIuz
1240 ") exceeds the client's maximum request size (%" PRIu32
")",
1248 sec_flags |= SEC_ENCRYPT;
1250 if (rdp->do_secure_checksum)
1251 sec_flags |= SEC_SECURE_CHECKSUM;
1254 for (
int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1257 const BYTE* pDstData =
nullptr;
1258 UINT32 compressionFlags = 0;
1260 BYTE* pSignature =
nullptr;
1261 fpUpdatePduHeader.action = 0;
1262 fpUpdatePduHeader.secFlags = 0;
1263 fpUpdateHeader.compression = 0;
1264 fpUpdateHeader.compressionFlags = 0;
1265 fpUpdateHeader.updateCode = updateCode;
1266 fpUpdateHeader.size = (UINT16)(totalLength > maxLength) ? maxLength : (UINT16)totalLength;
1267 const BYTE* pSrcData = Stream_Pointer(s);
1268 UINT32 SrcSize = DstSize = fpUpdateHeader.size;
1269 BOOL should_unlock = FALSE;
1271 if (sec_flags & SEC_ENCRYPT)
1272 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1274 if (sec_flags & SEC_SECURE_CHECKSUM)
1275 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1279 if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1280 &compressionFlags) >= 0)
1282 if (compressionFlags)
1284 WINPR_ASSERT(compressionFlags <= UINT8_MAX);
1285 fpUpdateHeader.compressionFlags = (UINT8)compressionFlags;
1286 fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1291 if (!fpUpdateHeader.compression)
1293 pDstData = Stream_Pointer(s);
1294 DstSize = fpUpdateHeader.size;
1297 if (DstSize > UINT16_MAX)
1299 fpUpdateHeader.size = (UINT16)DstSize;
1300 totalLength -= SrcSize;
1302 if (totalLength == 0)
1303 fpUpdateHeader.fragmentation =
1304 (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
1306 fpUpdateHeader.fragmentation =
1307 (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
1309 fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1310 fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1311 fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1313 if (sec_flags & SEC_ENCRYPT)
1315 pSignature = Stream_Buffer(fs) + 3;
1318 ENCRYPTION_METHOD_FIPS)
1322 if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8)
1325 fpUpdatePduHeader.fipsInformation[0] = 0x10;
1326 fpUpdatePduHeader.fipsInformation[1] = 0x00;
1327 fpUpdatePduHeader.fipsInformation[2] = 0x01;
1328 fpUpdatePduHeader.fipsInformation[3] = pad;
1332 const size_t len = fpUpdateHeader.size + fpHeaderSize + pad;
1333 if (len > UINT16_MAX)
1336 fpUpdatePduHeader.length = (UINT16)len;
1337 Stream_ResetPosition(fs);
1338 if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1340 if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1343 if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (
size_t)DstSize + pad))
1345 Stream_Write(fs, pDstData, DstSize);
1348 Stream_Zero(fs, pad);
1351 if (sec_flags & SEC_ENCRYPT)
1355 should_unlock = TRUE;
1356 UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad;
1357 BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize;
1360 ENCRYPTION_METHOD_FIPS)
1363 if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp))
1366 if (!security_fips_encrypt(data, dataSize, rdp))
1372 if (sec_flags & SEC_SECURE_CHECKSUM)
1374 security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8);
1376 status = security_mac_signature(rdp, data, dataSize, pSignature, 8);
1378 if (!status || !security_encrypt(data, dataSize, rdp))
1384 Stream_SealLength(fs);
1386 if (transport_write(rdp->transport, fs) < 0)
1393 security_unlock(rdp);
1395 if (!res || !status)
1398 Stream_Seek(s, SrcSize);
1404rdpFastPath* fastpath_new(rdpRdp* rdp)
1406 rdpFastPath* fastpath =
nullptr;
1410 fastpath = (rdpFastPath*)calloc(1,
sizeof(rdpFastPath));
1415 fastpath->rdp = rdp;
1416 fastpath->fragmentation = -1;
1417 fastpath->fs = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1418 fastpath->updateData = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1420 if (!fastpath->fs || !fastpath->updateData)
1425 fastpath_free(fastpath);
1429void fastpath_free(rdpFastPath* fastpath)
1433 Stream_Free(fastpath->updateData, TRUE);
1434 Stream_Free(fastpath->fs, TRUE);
1439BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1441 WINPR_ASSERT(fastpath);
1442 return fastpath->encryptionFlags;
1445BOOL fastpath_decrypt(rdpFastPath* fastpath,
wStream* s, UINT16* length)
1447 WINPR_ASSERT(fastpath);
1448 if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1450 const UINT16 flags =
1451 (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1452 ? SEC_SECURE_CHECKSUM
1455 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.