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);
178 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
180 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 4))
183 Stream_Write(s, fpUpdatePduHeader->fipsInformation, 4);
186 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
189 Stream_Write(s, fpUpdatePduHeader->dataSignature, 8);
200 if (!fpUpdatePduHeader || !rdp)
203 if (fpUpdatePduHeader->secFlags)
207 WINPR_ASSERT(rdp->settings);
208 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
215BOOL fastpath_read_header_rdp(rdpFastPath* fastpath,
wStream* s, UINT16* length)
222 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
225 Stream_Read_UINT8(s, header);
229 fastpath->encryptionFlags = (header & 0xC0) >> 6;
230 fastpath->numberEvents = (header & 0x3C) >> 2;
233 if (!per_read_length(s, length))
236 const size_t pos = Stream_GetPosition(s);
240 *length = *length - (UINT16)pos;
244static BOOL fastpath_recv_orders(rdpFastPath* fastpath,
wStream* s)
246 rdpUpdate* update = NULL;
247 UINT16 numberOrders = 0;
249 if (!fastpath || !fastpath->rdp || !s)
251 WLog_ERR(TAG,
"Invalid arguments");
255 update = fastpath->rdp->update;
259 WLog_ERR(TAG,
"Invalid configuration");
263 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
266 Stream_Read_UINT16(s, numberOrders);
268 while (numberOrders > 0)
270 if (!update_recv_order(update, s))
279static BOOL fastpath_recv_update_common(rdpFastPath* fastpath,
wStream* s)
282 UINT16 updateType = 0;
283 rdpUpdate* update = NULL;
284 rdpContext* context = NULL;
285 BOOL defaultReturn = 0;
287 if (!fastpath || !s || !fastpath->rdp)
290 update = fastpath->rdp->update;
292 if (!update || !update->context)
295 context = update->context;
299 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
302 Stream_Read_UINT16(s, updateType);
305 case UPDATE_TYPE_BITMAP:
307 BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
312 rc = IFCALLRESULT(defaultReturn, update->BitmapUpdate, context, bitmap_update);
313 free_bitmap_update(context, bitmap_update);
317 case UPDATE_TYPE_PALETTE:
324 rc = IFCALLRESULT(defaultReturn, update->Palette, context, palette_update);
325 free_palette_update(context, palette_update);
336static BOOL fastpath_recv_update_synchronize(WINPR_ATTR_UNUSED rdpFastPath* fastpath,
wStream* s)
340 WINPR_ASSERT(fastpath);
343 const size_t len = Stream_GetRemainingLength(s);
344 const size_t skip = MIN(2, len);
345 return Stream_SafeSeek(s, skip);
348static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode,
wStream* s)
353 if (!fastpath || !fastpath->rdp || !s)
356 Stream_SealLength(s);
357 Stream_SetPosition(s, 0);
359 rdpUpdate* update = fastpath->rdp->update;
361 if (!update || !update->pointer || !update->context)
364 rdpContext* context = update->context;
365 WINPR_ASSERT(context);
367 rdpPointerUpdate* pointer = update->pointer;
368 WINPR_ASSERT(pointer);
371 DEBUG_RDP(fastpath->rdp,
"recv Fast-Path %s Update (0x%02" PRIX8
"), length:%" PRIuz
"",
372 fastpath_update_to_string(updateCode), updateCode, Stream_GetRemainingLength(s));
375 const BOOL defaultReturn =
379 case FASTPATH_UPDATETYPE_ORDERS:
380 rc = fastpath_recv_orders(fastpath, s);
383 case FASTPATH_UPDATETYPE_BITMAP:
384 case FASTPATH_UPDATETYPE_PALETTE:
385 rc = fastpath_recv_update_common(fastpath, s);
388 case FASTPATH_UPDATETYPE_SYNCHRONIZE:
389 if (!fastpath_recv_update_synchronize(fastpath, s))
390 WLog_ERR(TAG,
"fastpath_recv_update_synchronize failure but we continue");
392 rc = IFCALLRESULT(TRUE, update->Synchronize, context);
396 case FASTPATH_UPDATETYPE_SURFCMDS:
397 status = update_recv_surfcmds(update, s);
398 rc = (status < 0) ? FALSE : TRUE;
401 case FASTPATH_UPDATETYPE_PTR_NULL:
404 pointer_system.type = SYSPTR_NULL;
405 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
409 case FASTPATH_UPDATETYPE_PTR_DEFAULT:
412 pointer_system.type = SYSPTR_DEFAULT;
413 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
417 case FASTPATH_UPDATETYPE_PTR_POSITION:
421 if (pointer_position)
423 rc = IFCALLRESULT(defaultReturn, pointer->PointerPosition, context,
425 free_pointer_position_update(context, pointer_position);
430 case FASTPATH_UPDATETYPE_COLOR:
436 rc = IFCALLRESULT(defaultReturn, pointer->PointerColor, context, pointer_color);
437 free_pointer_color_update(context, pointer_color);
442 case FASTPATH_UPDATETYPE_CACHED:
448 rc = IFCALLRESULT(defaultReturn, pointer->PointerCached, context, pointer_cached);
449 free_pointer_cached_update(context, pointer_cached);
454 case FASTPATH_UPDATETYPE_POINTER:
460 rc = IFCALLRESULT(defaultReturn, pointer->PointerNew, context, pointer_new);
461 free_pointer_new_update(context, pointer_new);
466 case FASTPATH_UPDATETYPE_LARGE_POINTER:
472 rc = IFCALLRESULT(defaultReturn, pointer->PointerLarge, context, pointer_large);
473 free_pointer_large_update(context, pointer_large);
481 Stream_SetPosition(s, 0);
484 WLog_ERR(TAG,
"Fastpath update %s [%" PRIx8
"] failed, status %d",
485 fastpath_update_to_string(updateCode), updateCode, status);
492static int fastpath_recv_update_data(rdpFastPath* fastpath,
wStream* s)
497 BYTE fragmentation = 0;
498 BYTE compression = 0;
499 BYTE compressionFlags = 0;
501 const BYTE* pDstData = NULL;
506 rdpRdp* rdp = fastpath->rdp;
511 rdpTransport* transport = rdp->transport;
516 if (!fastpath_read_update_header(s, &updateCode, &fragmentation, &compression))
519 if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
521 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
524 Stream_Read_UINT8(s, compressionFlags);
527 compressionFlags = 0;
529 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
532 Stream_Read_UINT16(s, size);
534 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
537 const int bulkStatus =
538 bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags);
539 Stream_Seek(s, size);
543 WLog_ERR(TAG,
"bulk_decompress() failed");
547 if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize))
550 Stream_Write(fastpath->updateData, pDstData, DstSize);
552 if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
554 if (fastpath->fragmentation != -1)
556 WLog_ERR(TAG,
"Unexpected FASTPATH_FRAGMENT_SINGLE");
560 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
564 WLog_ERR(TAG,
"fastpath_recv_update() - %i", status);
570 rdpContext* context = NULL;
571 const size_t totalSize = Stream_GetPosition(fastpath->updateData);
573 context = transport_get_context(transport);
574 WINPR_ASSERT(context);
575 WINPR_ASSERT(context->settings);
577 if (totalSize > context->settings->MultifragMaxRequestSize)
579 WLog_ERR(TAG,
"Total size (%" PRIuz
") exceeds MultifragMaxRequestSize (%" PRIu32
")",
580 totalSize, context->settings->MultifragMaxRequestSize);
584 if (fragmentation == FASTPATH_FRAGMENT_FIRST)
586 if (fastpath->fragmentation != -1)
588 WLog_ERR(TAG,
"fastpath_recv_update_data: Unexpected FASTPATH_FRAGMENT_FIRST");
592 fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
594 else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
596 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
597 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
599 WLog_ERR(TAG,
"fastpath_recv_update_data: Unexpected FASTPATH_FRAGMENT_NEXT");
603 fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
605 else if (fragmentation == FASTPATH_FRAGMENT_LAST)
607 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
608 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
610 WLog_ERR(TAG,
"fastpath_recv_update_data: Unexpected FASTPATH_FRAGMENT_LAST");
614 fastpath->fragmentation = -1;
615 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
619 WLog_ERR(TAG,
"fastpath_recv_update_data: fastpath_recv_update() - %i", status);
630state_run_t fastpath_recv_updates(rdpFastPath* fastpath,
wStream* s)
632 state_run_t rc = STATE_RUN_FAILED;
635 WINPR_ASSERT(fastpath);
636 WINPR_ASSERT(fastpath->rdp);
638 rdpUpdate* update = fastpath->rdp->update;
639 WINPR_ASSERT(update);
641 if (!update_begin_paint(update))
644 while (Stream_GetRemainingLength(s) >= 3)
646 if (fastpath_recv_update_data(fastpath, s) < 0)
648 WLog_ERR(TAG,
"fastpath_recv_update_data() fail");
649 rc = STATE_RUN_FAILED;
654 rc = STATE_RUN_SUCCESS;
657 if (!update_end_paint(update))
658 return STATE_RUN_FAILED;
663static BOOL fastpath_read_input_event_header(
wStream* s, BYTE* eventFlags, BYTE* eventCode)
665 BYTE eventHeader = 0;
668 WINPR_ASSERT(eventFlags);
669 WINPR_ASSERT(eventCode);
671 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
674 Stream_Read_UINT8(s, eventHeader);
675 *eventFlags = (eventHeader & 0x1F);
676 *eventCode = (eventHeader >> 5);
680static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
682 WINPR_ASSERT(fastpath);
683 WINPR_ASSERT(fastpath->rdp);
684 WINPR_ASSERT(fastpath->rdp->input);
687 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
690 rdpInput* input = fastpath->rdp->input;
692 const UINT8 code = Stream_Get_UINT8(s);
695 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
696 flags |= KBD_FLAGS_RELEASE;
698 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
699 flags |= KBD_FLAGS_EXTENDED;
701 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
702 flags |= KBD_FLAGS_EXTENDED1;
704 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
707static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath,
wStream* s,
708 WINPR_ATTR_UNUSED BYTE eventFlags)
710 rdpInput* input = NULL;
711 UINT16 pointerFlags = 0;
714 WINPR_ASSERT(fastpath);
715 WINPR_ASSERT(fastpath->rdp);
716 WINPR_ASSERT(fastpath->rdp->input);
719 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
722 input = fastpath->rdp->input;
724 Stream_Read_UINT16(s, pointerFlags);
725 Stream_Read_UINT16(s, xPos);
726 Stream_Read_UINT16(s, yPos);
727 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
730static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath,
wStream* s,
731 WINPR_ATTR_UNUSED BYTE eventFlags)
733 rdpInput* input = NULL;
734 UINT16 pointerFlags = 0;
737 WINPR_ASSERT(fastpath);
738 WINPR_ASSERT(fastpath->rdp);
739 WINPR_ASSERT(fastpath->rdp->context);
740 WINPR_ASSERT(fastpath->rdp->input);
743 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
746 input = fastpath->rdp->input;
748 Stream_Read_UINT16(s, pointerFlags);
749 Stream_Read_INT16(s, xDelta);
750 Stream_Read_INT16(s, yDelta);
755 "Received relative mouse event(flags=0x%04" PRIx16
", xPos=%" PRId16
756 ", yPos=%" PRId16
"), but we did not announce support for that",
757 pointerFlags, xDelta, yDelta);
761 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
764static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath,
wStream* s,
765 WINPR_ATTR_UNUSED BYTE eventFlags)
767 WINPR_ASSERT(fastpath);
768 WINPR_ASSERT(fastpath->rdp);
769 WINPR_ASSERT(fastpath->rdp->context);
770 WINPR_ASSERT(fastpath->rdp->input);
773 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
776 rdpInput* input = fastpath->rdp->input;
778 UINT32 timestampMS = 0;
779 Stream_Read_UINT32(s, timestampMS);
784 "Received qoe event(timestamp=%" PRIu32
785 "ms), but we did not announce support for that",
790 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
793static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath,
wStream* s,
794 WINPR_ATTR_UNUSED BYTE eventFlags)
796 rdpInput* input = NULL;
797 UINT16 pointerFlags = 0;
801 WINPR_ASSERT(fastpath);
802 WINPR_ASSERT(fastpath->rdp);
803 WINPR_ASSERT(fastpath->rdp->context);
804 WINPR_ASSERT(fastpath->rdp->input);
807 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
810 input = fastpath->rdp->input;
812 Stream_Read_UINT16(s, pointerFlags);
813 Stream_Read_UINT16(s, xPos);
814 Stream_Read_UINT16(s, yPos);
819 "Received extended mouse event(flags=0x%04" PRIx16
", xPos=%" PRIu16
820 ", yPos=%" PRIu16
"), but we did not announce support for that",
821 pointerFlags, xPos, yPos);
825 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
828static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED
wStream* s,
831 rdpInput* input = NULL;
833 WINPR_ASSERT(fastpath);
834 WINPR_ASSERT(fastpath->rdp);
835 WINPR_ASSERT(fastpath->rdp->input);
838 input = fastpath->rdp->input;
839 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
842static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath,
wStream* s, BYTE eventFlags)
844 UINT16 unicodeCode = 0;
847 WINPR_ASSERT(fastpath);
850 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
853 Stream_Read_UINT16(s, unicodeCode);
856 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
857 flags |= KBD_FLAGS_RELEASE;
859 WINPR_ASSERT(fastpath->rdp);
860 WINPR_ASSERT(fastpath->rdp);
861 WINPR_ASSERT(fastpath->rdp->input);
862 return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input,
866static BOOL fastpath_recv_input_event(rdpFastPath* fastpath,
wStream* s)
871 WINPR_ASSERT(fastpath);
874 if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
879 case FASTPATH_INPUT_EVENT_SCANCODE:
880 if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
885 case FASTPATH_INPUT_EVENT_MOUSE:
886 if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
891 case FASTPATH_INPUT_EVENT_MOUSEX:
892 if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
897 case FASTPATH_INPUT_EVENT_SYNC:
898 if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
903 case FASTPATH_INPUT_EVENT_UNICODE:
904 if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
909 case TS_FP_RELPOINTER_EVENT:
910 if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
915 case TS_FP_QOETIMESTAMP_EVENT:
916 if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
921 WLog_ERR(TAG,
"Unknown eventCode %" PRIu8
"", eventCode);
928state_run_t fastpath_recv_inputs(rdpFastPath* fastpath,
wStream* s)
930 WINPR_ASSERT(fastpath);
933 if (fastpath->numberEvents == 0)
939 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
940 return STATE_RUN_FAILED;
942 Stream_Read_UINT8(s, fastpath->numberEvents);
945 for (BYTE i = 0; i < fastpath->numberEvents; i++)
947 if (!fastpath_recv_input_event(fastpath, s))
948 return STATE_RUN_FAILED;
951 return STATE_RUN_SUCCESS;
954static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp)
956 UINT32 sec_bytes = 0;
966 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
973wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags)
975 if (!fastpath || !fastpath->rdp)
978 rdpRdp* rdp = fastpath->rdp;
979 wStream* s = transport_send_stream_init(rdp->transport, 256);
988 *sec_flags |= SEC_ENCRYPT;
990 if (rdp->do_secure_checksum)
991 *sec_flags |= SEC_SECURE_CHECKSUM;
994 Stream_Seek(s, fastpath_get_sec_bytes(rdp));
998wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode,
1002 s = fastpath_input_pdu_init_header(fastpath, sec_flags);
1007 WINPR_ASSERT(eventCode < 8);
1008 WINPR_ASSERT(eventFlags < 0x20);
1009 Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5)));
1013BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath,
wStream* s,
size_t iNumEvents,
1017 BYTE eventHeader = 0;
1018 BOOL should_unlock = FALSE;
1021 WINPR_ASSERT(iNumEvents > 0);
1028 rdp = fastpath->rdp;
1031 CONNECTION_STATE state = rdp_get_state(rdp);
1032 if (!rdp_is_active_state(rdp))
1034 WLog_WARN(TAG,
"called before activation [%s]", rdp_state_string(state));
1043 if (iNumEvents > 15)
1046 size_t length = Stream_GetPosition(s);
1048 if (length >= (2 << 14))
1050 WLog_ERR(TAG,
"Maximum FastPath PDU length is 32767");
1054 eventHeader = FASTPATH_INPUT_ACTION_FASTPATH;
1055 eventHeader |= (iNumEvents << 2);
1057 if (sec_flags & SEC_ENCRYPT)
1058 eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6);
1060 if (sec_flags & SEC_SECURE_CHECKSUM)
1061 eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
1063 Stream_SetPosition(s, 0);
1064 Stream_Write_UINT8(s, eventHeader);
1068 if (sec_flags & SEC_ENCRYPT)
1070 if (!security_lock(rdp))
1072 should_unlock = TRUE;
1074 const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp);
1075 if (sec_bytes + 3ULL > length)
1078 BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes;
1079 const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes);
1081 WINPR_ASSERT(rdp->settings);
1082 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
1086 if ((pad = 8 - (fpInputEvents_length % 8)) == 8)
1089 Stream_Write_UINT16(s, 0x10);
1090 Stream_Write_UINT8(s, 0x1);
1091 Stream_Write_UINT8(s, pad);
1093 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1096 if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s), 8,
1101 memset(fpInputEvents + fpInputEvents_length, 0, pad);
1103 if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp))
1111 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1113 if (sec_flags & SEC_SECURE_CHECKSUM)
1114 res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length, TRUE,
1115 Stream_Pointer(s), 8);
1117 res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1118 Stream_Pointer(s), 8);
1120 if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp))
1131 WINPR_ASSERT(length < UINT16_MAX);
1132 Stream_SetPosition(s, 1);
1133 Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length);
1134 Stream_SetPosition(s, length);
1135 Stream_SealLength(s);
1137 if (transport_write(rdp->transport, s) < 0)
1142 if (should_unlock && !security_unlock(rdp))
1148BOOL fastpath_send_input_pdu(rdpFastPath* fastpath,
wStream* s, UINT16 sec_flags)
1150 return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags);
1153wStream* fastpath_update_pdu_init(rdpFastPath* fastpath)
1155 return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1158wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath)
1161 s = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1165BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode,
wStream* s,
1166 BOOL skipCompression)
1170 rdpSettings* settings = NULL;
1172 UINT32 fpHeaderSize = 6;
1173 UINT32 fpUpdatePduHeaderSize = 0;
1174 UINT32 fpUpdateHeaderSize = 0;
1177 UINT16 sec_flags = 0;
1179 if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1182 rdp = fastpath->rdp;
1184 settings = rdp->settings;
1189 UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1191 if (settings->CompressionEnabled && !skipCompression)
1193 const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1194 maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1198 size_t totalLength = Stream_GetPosition(s);
1199 Stream_SetPosition(s, 0);
1202 if (!settings->FastPathOutput)
1204 WLog_ERR(TAG,
"client does not support fast path output");
1209 if (totalLength > settings->MultifragMaxRequestSize)
1212 "fast path update size (%" PRIuz
1213 ") exceeds the client's maximum request size (%" PRIu32
")",
1214 totalLength, settings->MultifragMaxRequestSize);
1220 sec_flags |= SEC_ENCRYPT;
1222 if (rdp->do_secure_checksum)
1223 sec_flags |= SEC_SECURE_CHECKSUM;
1226 for (
int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1229 const BYTE* pDstData = NULL;
1230 UINT32 compressionFlags = 0;
1232 BYTE* pSignature = NULL;
1233 fpUpdatePduHeader.action = 0;
1234 fpUpdatePduHeader.secFlags = 0;
1235 fpUpdateHeader.compression = 0;
1236 fpUpdateHeader.compressionFlags = 0;
1237 fpUpdateHeader.updateCode = updateCode;
1238 fpUpdateHeader.size = (UINT16)(totalLength > maxLength) ? maxLength : (UINT16)totalLength;
1239 const BYTE* pSrcData = Stream_Pointer(s);
1240 UINT32 SrcSize = DstSize = fpUpdateHeader.size;
1241 BOOL should_unlock = FALSE;
1243 if (sec_flags & SEC_ENCRYPT)
1244 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1246 if (sec_flags & SEC_SECURE_CHECKSUM)
1247 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1249 if (settings->CompressionEnabled && !skipCompression)
1251 if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1252 &compressionFlags) >= 0)
1254 if (compressionFlags)
1256 WINPR_ASSERT(compressionFlags <= UINT8_MAX);
1257 fpUpdateHeader.compressionFlags = (UINT8)compressionFlags;
1258 fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1263 if (!fpUpdateHeader.compression)
1265 pDstData = Stream_Pointer(s);
1266 DstSize = fpUpdateHeader.size;
1269 if (DstSize > UINT16_MAX)
1271 fpUpdateHeader.size = (UINT16)DstSize;
1272 totalLength -= SrcSize;
1274 if (totalLength == 0)
1275 fpUpdateHeader.fragmentation =
1276 (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
1278 fpUpdateHeader.fragmentation =
1279 (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
1281 fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1282 fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1283 fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1285 if (sec_flags & SEC_ENCRYPT)
1287 pSignature = Stream_Buffer(fs) + 3;
1289 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
1293 if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8)
1296 fpUpdatePduHeader.fipsInformation[0] = 0x10;
1297 fpUpdatePduHeader.fipsInformation[1] = 0x00;
1298 fpUpdatePduHeader.fipsInformation[2] = 0x01;
1299 fpUpdatePduHeader.fipsInformation[3] = pad;
1303 const size_t len = fpUpdateHeader.size + fpHeaderSize + pad;
1304 if (len > UINT16_MAX)
1307 fpUpdatePduHeader.length = (UINT16)len;
1308 Stream_SetPosition(fs, 0);
1309 if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1311 if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1314 if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (
size_t)DstSize + pad))
1316 Stream_Write(fs, pDstData, DstSize);
1319 Stream_Zero(fs, pad);
1322 if (sec_flags & SEC_ENCRYPT)
1324 if (!security_lock(rdp))
1326 should_unlock = TRUE;
1327 UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad;
1328 BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize;
1330 if (rdp->settings->EncryptionMethods == ENCRYPTION_METHOD_FIPS)
1333 if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp))
1336 if (!security_fips_encrypt(data, dataSize, rdp))
1342 if (sec_flags & SEC_SECURE_CHECKSUM)
1344 security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8);
1346 status = security_mac_signature(rdp, data, dataSize, pSignature, 8);
1348 if (!status || !security_encrypt(data, dataSize, rdp))
1354 Stream_SealLength(fs);
1356 if (transport_write(rdp->transport, fs) < 0)
1362 if (should_unlock && !security_unlock(rdp))
1365 if (!res || !status)
1368 Stream_Seek(s, SrcSize);
1374rdpFastPath* fastpath_new(rdpRdp* rdp)
1376 rdpFastPath* fastpath = NULL;
1380 fastpath = (rdpFastPath*)calloc(1,
sizeof(rdpFastPath));
1385 fastpath->rdp = rdp;
1386 fastpath->fragmentation = -1;
1387 fastpath->fs = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1388 fastpath->updateData = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1390 if (!fastpath->fs || !fastpath->updateData)
1395 fastpath_free(fastpath);
1399void fastpath_free(rdpFastPath* fastpath)
1403 Stream_Free(fastpath->updateData, TRUE);
1404 Stream_Free(fastpath->fs, TRUE);
1409BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1411 WINPR_ASSERT(fastpath);
1412 return fastpath->encryptionFlags;
1415BOOL fastpath_decrypt(rdpFastPath* fastpath,
wStream* s, UINT16* length)
1417 WINPR_ASSERT(fastpath);
1418 if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1420 const UINT16 flags =
1421 (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1422 ? SEC_SECURE_CHECKSUM
1425 if (!rdp_decrypt(fastpath->rdp, s, length, flags))
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.