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;
287 if (!update || !update->context)
290 rdpContext* context = update->context;
294 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
297 Stream_Read_UINT16(s, updateType);
300 case UPDATE_TYPE_BITMAP:
302 BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
307 rc = IFCALLRESULT(defaultReturn, update->BitmapUpdate, context, bitmap_update);
308 free_bitmap_update(context, bitmap_update);
312 case UPDATE_TYPE_PALETTE:
319 rc = IFCALLRESULT(defaultReturn, update->Palette, context, palette_update);
320 free_palette_update(context, palette_update);
331static BOOL fastpath_recv_update_synchronize(WINPR_ATTR_UNUSED rdpFastPath* fastpath,
wStream* s)
335 WINPR_ASSERT(fastpath);
338 const size_t len = Stream_GetRemainingLength(s);
339 const size_t skip = MIN(2, len);
340 return Stream_SafeSeek(s, skip);
343static BOOL fastpath_recv_update_paint_block(rdpUpdate* update,
wStream* s,
344 BOOL (*fkt)(rdpUpdate*,
wStream*))
347 if (!update_begin_paint(update))
350 BOOL res = fkt(update, s);
351 if (!update_end_paint(update))
356static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode,
wStream* s)
361 if (!fastpath || !fastpath->rdp || !s)
364 Stream_SealLength(s);
365 Stream_ResetPosition(s);
367 rdpUpdate* update = fastpath->rdp->update;
369 if (!update || !update->pointer || !update->context)
372 rdpContext* context = update->context;
373 WINPR_ASSERT(context);
375 rdpPointerUpdate* pointer = update->pointer;
376 WINPR_ASSERT(pointer);
379 DEBUG_RDP(fastpath->rdp,
"recv Fast-Path %s Update (0x%02" PRIX8
"), length:%" PRIuz
"",
380 fastpath_update_to_string(updateCode), updateCode, Stream_GetRemainingLength(s));
383 const BOOL defaultReturn =
387 case FASTPATH_UPDATETYPE_ORDERS:
388 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_orders);
391 case FASTPATH_UPDATETYPE_BITMAP:
392 case FASTPATH_UPDATETYPE_PALETTE:
393 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_update_common);
396 case FASTPATH_UPDATETYPE_SYNCHRONIZE:
397 if (!fastpath_recv_update_synchronize(fastpath, s))
398 WLog_ERR(TAG,
"fastpath_recv_update_synchronize failure but we continue");
400 rc = IFCALLRESULT(TRUE, update->Synchronize, context);
404 case FASTPATH_UPDATETYPE_SURFCMDS:
405 status = fastpath_recv_update_paint_block(update, s, update_recv_surfcmds);
409 case FASTPATH_UPDATETYPE_PTR_NULL:
412 pointer_system.type = SYSPTR_NULL;
413 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
417 case FASTPATH_UPDATETYPE_PTR_DEFAULT:
420 pointer_system.type = SYSPTR_DEFAULT;
421 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
425 case FASTPATH_UPDATETYPE_PTR_POSITION:
429 if (pointer_position)
431 rc = IFCALLRESULT(defaultReturn, pointer->PointerPosition, context,
433 free_pointer_position_update(context, pointer_position);
438 case FASTPATH_UPDATETYPE_COLOR:
444 rc = IFCALLRESULT(defaultReturn, pointer->PointerColor, context, pointer_color);
445 free_pointer_color_update(context, pointer_color);
450 case FASTPATH_UPDATETYPE_CACHED:
456 rc = IFCALLRESULT(defaultReturn, pointer->PointerCached, context, pointer_cached);
457 free_pointer_cached_update(context, pointer_cached);
462 case FASTPATH_UPDATETYPE_POINTER:
468 rc = IFCALLRESULT(defaultReturn, pointer->PointerNew, context, pointer_new);
469 free_pointer_new_update(context, pointer_new);
474 case FASTPATH_UPDATETYPE_LARGE_POINTER:
480 rc = IFCALLRESULT(defaultReturn, pointer->PointerLarge, context, pointer_large);
481 free_pointer_large_update(context, pointer_large);
489 Stream_ResetPosition(s);
492 WLog_ERR(TAG,
"Fastpath update %s [%" PRIx8
"] failed, status %d",
493 fastpath_update_to_string(updateCode), updateCode, status);
500static int fastpath_recv_update_data(rdpFastPath* fastpath,
wStream* s)
505 BYTE fragmentation = 0;
506 BYTE compression = 0;
507 BYTE compressionFlags = 0;
509 const BYTE* pDstData =
nullptr;
514 rdpRdp* rdp = fastpath->rdp;
519 rdpTransport* transport = rdp->transport;
524 if (!fastpath_read_update_header(s, &updateCode, &fragmentation, &compression))
527 if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
529 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
532 Stream_Read_UINT8(s, compressionFlags);
535 compressionFlags = 0;
537 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
540 Stream_Read_UINT16(s, size);
542 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
545 const int bulkStatus =
546 bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags);
547 Stream_Seek(s, size);
551 WLog_ERR(TAG,
"bulk_decompress() failed");
555 if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize))
558 Stream_Write(fastpath->updateData, pDstData, DstSize);
560 if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
562 if (fastpath->fragmentation != -1)
564 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_SINGLE");
568 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
572 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
578 rdpContext* context =
nullptr;
579 const size_t totalSize = Stream_GetPosition(fastpath->updateData);
581 context = transport_get_context(transport);
582 WINPR_ASSERT(context);
583 WINPR_ASSERT(context->settings);
589 TAG,
"Total size (%" PRIuz
") exceeds MultifragMaxRequestSize (%" PRIu32
")",
595 if (fragmentation == FASTPATH_FRAGMENT_FIRST)
597 if (fastpath->fragmentation != -1)
599 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_FIRST");
603 fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
605 else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
607 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
608 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
610 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_NEXT");
614 fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
616 else if (fragmentation == FASTPATH_FRAGMENT_LAST)
618 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
619 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
621 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_LAST");
625 fastpath->fragmentation = -1;
626 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
630 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
641state_run_t fastpath_recv_updates(rdpFastPath* fastpath,
wStream* s)
643 state_run_t rc = STATE_RUN_FAILED;
646 WINPR_ASSERT(fastpath);
647 WINPR_ASSERT(fastpath->rdp);
649 while (Stream_GetRemainingLength(s) >= 3)
651 if (fastpath_recv_update_data(fastpath, s) < 0)
653 WLog_ERR(TAG,
"fastpath_recv_update_data() fail");
654 rc = STATE_RUN_FAILED;
659 rc = STATE_RUN_SUCCESS;
665static BOOL fastpath_read_input_event_header(
wStream* s, BYTE* eventFlags, BYTE* eventCode)
667 BYTE eventHeader = 0;
670 WINPR_ASSERT(eventFlags);
671 WINPR_ASSERT(eventCode);
673 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
676 Stream_Read_UINT8(s, eventHeader);
677 *eventFlags = (eventHeader & 0x1F);
678 *eventCode = (eventHeader >> 5);
682static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
684 WINPR_ASSERT(fastpath);
685 WINPR_ASSERT(fastpath->rdp);
686 WINPR_ASSERT(fastpath->rdp->input);
689 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
692 rdpInput* input = fastpath->rdp->input;
694 const UINT8 code = Stream_Get_UINT8(s);
697 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
698 flags |= KBD_FLAGS_RELEASE;
700 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
701 flags |= KBD_FLAGS_EXTENDED;
703 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
704 flags |= KBD_FLAGS_EXTENDED1;
706 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
709static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath,
wStream* s,
710 WINPR_ATTR_UNUSED BYTE eventFlags)
712 rdpInput* input =
nullptr;
713 UINT16 pointerFlags = 0;
716 WINPR_ASSERT(fastpath);
717 WINPR_ASSERT(fastpath->rdp);
718 WINPR_ASSERT(fastpath->rdp->input);
721 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
724 input = fastpath->rdp->input;
726 Stream_Read_UINT16(s, pointerFlags);
727 Stream_Read_UINT16(s, xPos);
728 Stream_Read_UINT16(s, yPos);
729 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
732static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath,
wStream* s,
733 WINPR_ATTR_UNUSED BYTE eventFlags)
735 rdpInput* input =
nullptr;
736 UINT16 pointerFlags = 0;
739 WINPR_ASSERT(fastpath);
740 WINPR_ASSERT(fastpath->rdp);
741 WINPR_ASSERT(fastpath->rdp->context);
742 WINPR_ASSERT(fastpath->rdp->input);
745 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
748 input = fastpath->rdp->input;
750 Stream_Read_UINT16(s, pointerFlags);
751 Stream_Read_INT16(s, xDelta);
752 Stream_Read_INT16(s, yDelta);
757 "Received relative mouse event(flags=0x%04" PRIx16
", xPos=%" PRId16
758 ", yPos=%" PRId16
"), but we did not announce support for that",
759 pointerFlags, xDelta, yDelta);
763 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
766static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath,
wStream* s,
767 WINPR_ATTR_UNUSED BYTE eventFlags)
769 WINPR_ASSERT(fastpath);
770 WINPR_ASSERT(fastpath->rdp);
771 WINPR_ASSERT(fastpath->rdp->context);
772 WINPR_ASSERT(fastpath->rdp->input);
775 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
778 rdpInput* input = fastpath->rdp->input;
780 UINT32 timestampMS = 0;
781 Stream_Read_UINT32(s, timestampMS);
786 "Received qoe event(timestamp=%" PRIu32
787 "ms), but we did not announce support for that",
792 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
795static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath,
wStream* s,
796 WINPR_ATTR_UNUSED BYTE eventFlags)
798 rdpInput* input =
nullptr;
799 UINT16 pointerFlags = 0;
803 WINPR_ASSERT(fastpath);
804 WINPR_ASSERT(fastpath->rdp);
805 WINPR_ASSERT(fastpath->rdp->context);
806 WINPR_ASSERT(fastpath->rdp->input);
809 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
812 input = fastpath->rdp->input;
814 Stream_Read_UINT16(s, pointerFlags);
815 Stream_Read_UINT16(s, xPos);
816 Stream_Read_UINT16(s, yPos);
821 "Received extended mouse event(flags=0x%04" PRIx16
", xPos=%" PRIu16
822 ", yPos=%" PRIu16
"), but we did not announce support for that",
823 pointerFlags, xPos, yPos);
827 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
830static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED
wStream* s,
833 rdpInput* input =
nullptr;
835 WINPR_ASSERT(fastpath);
836 WINPR_ASSERT(fastpath->rdp);
837 WINPR_ASSERT(fastpath->rdp->input);
840 input = fastpath->rdp->input;
841 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
844static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
846 UINT16 unicodeCode = 0;
849 WINPR_ASSERT(fastpath);
852 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
855 Stream_Read_UINT16(s, unicodeCode);
858 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
859 flags |= KBD_FLAGS_RELEASE;
861 WINPR_ASSERT(fastpath->rdp);
862 WINPR_ASSERT(fastpath->rdp);
863 WINPR_ASSERT(fastpath->rdp->input);
864 return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input,
868static BOOL fastpath_recv_input_event(rdpFastPath* fastpath,
wStream* s)
873 WINPR_ASSERT(fastpath);
876 if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
881 case FASTPATH_INPUT_EVENT_SCANCODE:
882 if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
887 case FASTPATH_INPUT_EVENT_MOUSE:
888 if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
893 case FASTPATH_INPUT_EVENT_MOUSEX:
894 if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
899 case FASTPATH_INPUT_EVENT_SYNC:
900 if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
905 case FASTPATH_INPUT_EVENT_UNICODE:
906 if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
911 case TS_FP_RELPOINTER_EVENT:
912 if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
917 case TS_FP_QOETIMESTAMP_EVENT:
918 if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
923 WLog_ERR(TAG,
"Unknown eventCode %" PRIu8
"", eventCode);
930state_run_t fastpath_recv_inputs(rdpFastPath* fastpath,
wStream* s)
932 WINPR_ASSERT(fastpath);
935 if (fastpath->numberEvents == 0)
941 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
942 return STATE_RUN_FAILED;
944 Stream_Read_UINT8(s, fastpath->numberEvents);
947 for (BYTE i = 0; i < fastpath->numberEvents; i++)
949 if (!fastpath_recv_input_event(fastpath, s))
950 return STATE_RUN_FAILED;
953 return STATE_RUN_SUCCESS;
956static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp)
958 UINT32 sec_bytes = 0;
969 ENCRYPTION_METHOD_FIPS)
976wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags)
978 if (!fastpath || !fastpath->rdp)
981 rdpRdp* rdp = fastpath->rdp;
982 wStream* s = transport_send_stream_init(rdp->transport, 256);
991 *sec_flags |= SEC_ENCRYPT;
993 if (rdp->do_secure_checksum)
994 *sec_flags |= SEC_SECURE_CHECKSUM;
997 Stream_Seek(s, fastpath_get_sec_bytes(rdp));
1001wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode,
1005 s = fastpath_input_pdu_init_header(fastpath, sec_flags);
1010 WINPR_ASSERT(eventCode < 8);
1011 WINPR_ASSERT(eventFlags < 0x20);
1012 Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5)));
1016BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath,
wStream* s,
size_t iNumEvents,
1020 BYTE eventHeader = 0;
1021 BOOL should_unlock = FALSE;
1022 rdpRdp* rdp =
nullptr;
1024 WINPR_ASSERT(iNumEvents > 0);
1031 rdp = fastpath->rdp;
1035 const CONNECTION_STATE state = rdp_get_state(rdp);
1036 if (!rdp_is_active_state(rdp))
1038 WLog_WARN(TAG,
"called before activation [%s]", rdp_state_string(state));
1048 if (iNumEvents > 15)
1052 size_t length = Stream_GetPosition(s);
1054 if (length >= (2u << 14))
1056 WLog_ERR(TAG,
"Maximum FastPath PDU length is 32767");
1060 eventHeader = FASTPATH_INPUT_ACTION_FASTPATH;
1061 eventHeader |= (iNumEvents << 2);
1063 if (sec_flags & SEC_ENCRYPT)
1064 eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6);
1066 if (sec_flags & SEC_SECURE_CHECKSUM)
1067 eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
1069 Stream_ResetPosition(s);
1070 Stream_Write_UINT8(s, eventHeader);
1074 if (sec_flags & SEC_ENCRYPT)
1077 should_unlock = TRUE;
1079 const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp);
1080 if (sec_bytes + 3ULL > length)
1083 BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes;
1084 const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes);
1086 WINPR_ASSERT(rdp->settings);
1088 ENCRYPTION_METHOD_FIPS)
1092 if ((pad = 8 - (fpInputEvents_length % 8)) == 8)
1095 Stream_Write_UINT16(s, 0x10);
1096 Stream_Write_UINT8(s, 0x1);
1097 Stream_Write_UINT8(s, pad);
1099 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1102 if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s),
1107 memset(fpInputEvents + fpInputEvents_length, 0, pad);
1109 if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp))
1117 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1119 if (sec_flags & SEC_SECURE_CHECKSUM)
1120 res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1121 TRUE, Stream_Pointer(s), 8);
1123 res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1124 Stream_Pointer(s), 8);
1126 if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp))
1137 WINPR_ASSERT(length < UINT16_MAX);
1138 if (!Stream_SetPosition(s, 1))
1140 Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length);
1141 if (!Stream_SetPosition(s, length))
1143 Stream_SealLength(s);
1146 if (transport_write(rdp->transport, s) < 0)
1152 security_unlock(rdp);
1157BOOL fastpath_send_input_pdu(rdpFastPath* fastpath,
wStream* s, UINT16 sec_flags)
1159 return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags);
1162wStream* fastpath_update_pdu_init(rdpFastPath* fastpath)
1164 return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1167wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath)
1170 s = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1174BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode,
wStream* s,
1175 BOOL skipCompression)
1179 rdpSettings* settings =
nullptr;
1180 rdpRdp* rdp =
nullptr;
1181 UINT32 fpHeaderSize = 6;
1182 UINT32 fpUpdatePduHeaderSize = 0;
1183 UINT32 fpUpdateHeaderSize = 0;
1186 UINT16 sec_flags = 0;
1188 if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1191 rdp = fastpath->rdp;
1193 settings = rdp->settings;
1198 UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1202 const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1203 maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1207 size_t totalLength = Stream_GetPosition(s);
1208 Stream_ResetPosition(s);
1213 WLog_ERR(TAG,
"client does not support fast path output");
1221 "fast path update size (%" PRIuz
1222 ") exceeds the client's maximum request size (%" PRIu32
")",
1230 sec_flags |= SEC_ENCRYPT;
1232 if (rdp->do_secure_checksum)
1233 sec_flags |= SEC_SECURE_CHECKSUM;
1236 for (
int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1239 const BYTE* pDstData =
nullptr;
1240 UINT32 compressionFlags = 0;
1242 BYTE* pSignature =
nullptr;
1243 fpUpdatePduHeader.action = 0;
1244 fpUpdatePduHeader.secFlags = 0;
1245 fpUpdateHeader.compression = 0;
1246 fpUpdateHeader.compressionFlags = 0;
1247 fpUpdateHeader.updateCode = updateCode;
1248 fpUpdateHeader.size = (UINT16)(totalLength > maxLength) ? maxLength : (UINT16)totalLength;
1249 const BYTE* pSrcData = Stream_Pointer(s);
1250 UINT32 SrcSize = DstSize = fpUpdateHeader.size;
1251 BOOL should_unlock = FALSE;
1253 if (sec_flags & SEC_ENCRYPT)
1254 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1256 if (sec_flags & SEC_SECURE_CHECKSUM)
1257 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1261 if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1262 &compressionFlags) >= 0)
1264 if (compressionFlags)
1266 WINPR_ASSERT(compressionFlags <= UINT8_MAX);
1267 fpUpdateHeader.compressionFlags = (UINT8)compressionFlags;
1268 fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1273 if (!fpUpdateHeader.compression)
1275 pDstData = Stream_Pointer(s);
1276 DstSize = fpUpdateHeader.size;
1279 if (DstSize > UINT16_MAX)
1281 fpUpdateHeader.size = (UINT16)DstSize;
1282 totalLength -= SrcSize;
1284 if (totalLength == 0)
1285 fpUpdateHeader.fragmentation =
1286 (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
1288 fpUpdateHeader.fragmentation =
1289 (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
1291 fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1292 fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1293 fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1295 if (sec_flags & SEC_ENCRYPT)
1297 pSignature = Stream_Buffer(fs) + 3;
1300 ENCRYPTION_METHOD_FIPS)
1304 if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8)
1307 fpUpdatePduHeader.fipsInformation[0] = 0x10;
1308 fpUpdatePduHeader.fipsInformation[1] = 0x00;
1309 fpUpdatePduHeader.fipsInformation[2] = 0x01;
1310 fpUpdatePduHeader.fipsInformation[3] = pad;
1314 const size_t len = fpUpdateHeader.size + fpHeaderSize + pad;
1315 if (len > UINT16_MAX)
1318 fpUpdatePduHeader.length = (UINT16)len;
1319 Stream_ResetPosition(fs);
1320 if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1322 if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1325 if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (
size_t)DstSize + pad))
1327 Stream_Write(fs, pDstData, DstSize);
1330 Stream_Zero(fs, pad);
1333 if (sec_flags & SEC_ENCRYPT)
1337 should_unlock = TRUE;
1338 UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad;
1339 BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize;
1342 ENCRYPTION_METHOD_FIPS)
1345 if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp))
1348 if (!security_fips_encrypt(data, dataSize, rdp))
1354 if (sec_flags & SEC_SECURE_CHECKSUM)
1356 security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8);
1358 status = security_mac_signature(rdp, data, dataSize, pSignature, 8);
1360 if (!status || !security_encrypt(data, dataSize, rdp))
1366 Stream_SealLength(fs);
1368 if (transport_write(rdp->transport, fs) < 0)
1375 security_unlock(rdp);
1377 if (!res || !status)
1380 Stream_Seek(s, SrcSize);
1386rdpFastPath* fastpath_new(rdpRdp* rdp)
1388 rdpFastPath* fastpath =
nullptr;
1392 fastpath = (rdpFastPath*)calloc(1,
sizeof(rdpFastPath));
1397 fastpath->rdp = rdp;
1398 fastpath->fragmentation = -1;
1399 fastpath->fs = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1400 fastpath->updateData = Stream_New(
nullptr, FASTPATH_MAX_PACKET_SIZE);
1402 if (!fastpath->fs || !fastpath->updateData)
1407 fastpath_free(fastpath);
1411void fastpath_free(rdpFastPath* fastpath)
1415 Stream_Free(fastpath->updateData, TRUE);
1416 Stream_Free(fastpath->fs, TRUE);
1421BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1423 WINPR_ASSERT(fastpath);
1424 return fastpath->encryptionFlags;
1427BOOL fastpath_decrypt(rdpFastPath* fastpath,
wStream* s, UINT16* length)
1429 WINPR_ASSERT(fastpath);
1430 if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1432 const UINT16 flags =
1433 (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1434 ? SEC_SECURE_CHECKSUM
1437 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.