FreeRDP
Loading...
Searching...
No Matches
fastpath.c
1
23#include <freerdp/config.h>
24
25#include "settings.h"
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include <winpr/crt.h>
32#include <winpr/assert.h>
33#include <winpr/stream.h>
34
35#include <freerdp/api.h>
36#include <freerdp/log.h>
37#include <freerdp/crypto/per.h>
38
39#include "orders.h"
40#include "update.h"
41#include "surface.h"
42#include "fastpath.h"
43#include "rdp.h"
44
45#include "../cache/pointer.h"
46#include "../cache/palette.h"
47#include "../cache/bitmap.h"
48
49#define TAG FREERDP_TAG("core.fastpath")
50
51enum FASTPATH_INPUT_ENCRYPTION_FLAGS
52{
53 FASTPATH_INPUT_SECURE_CHECKSUM = 0x1,
54 FASTPATH_INPUT_ENCRYPTED = 0x2
55};
56
57enum FASTPATH_OUTPUT_ENCRYPTION_FLAGS
58{
59 FASTPATH_OUTPUT_SECURE_CHECKSUM = 0x1,
60 FASTPATH_OUTPUT_ENCRYPTED = 0x2
61};
62
63struct rdp_fastpath
64{
65 rdpRdp* rdp;
66 wStream* fs;
67 BYTE encryptionFlags;
68 BYTE numberEvents;
69 wStream* updateData;
70 int fragmentation;
71};
72
83static const char* const FASTPATH_UPDATETYPE_STRINGS[] = {
84 "Orders", /* 0x0 */
85 "Bitmap", /* 0x1 */
86 "Palette", /* 0x2 */
87 "Synchronize", /* 0x3 */
88 "Surface Commands", /* 0x4 */
89 "System Pointer Hidden", /* 0x5 */
90 "System Pointer Default", /* 0x6 */
91 "???", /* 0x7 */
92 "Pointer Position", /* 0x8 */
93 "Color Pointer", /* 0x9 */
94 "Cached Pointer", /* 0xA */
95 "New Pointer", /* 0xB */
96};
97
98static const char* fastpath_update_to_string(UINT8 update)
99{
100 if (update >= ARRAYSIZE(FASTPATH_UPDATETYPE_STRINGS))
101 return "UNKNOWN";
102
103 return FASTPATH_UPDATETYPE_STRINGS[update];
104}
105
106static BOOL fastpath_read_update_header(wStream* s, BYTE* updateCode, BYTE* fragmentation,
107 BYTE* compression)
108{
109 BYTE updateHeader = 0;
110
111 if (!s || !updateCode || !fragmentation || !compression)
112 return FALSE;
113
114 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
115 return FALSE;
116
117 Stream_Read_UINT8(s, updateHeader);
118 *updateCode = updateHeader & 0x0F;
119 *fragmentation = (updateHeader >> 4) & 0x03;
120 *compression = (updateHeader >> 6) & 0x03;
121 return TRUE;
122}
123
124static BOOL fastpath_write_update_header(wStream* s, const FASTPATH_UPDATE_HEADER* fpUpdateHeader)
125{
126 BYTE updateHeader = 0;
127 WINPR_ASSERT(fpUpdateHeader);
128
129 updateHeader |= fpUpdateHeader->updateCode & 0x0F;
130 updateHeader |= (fpUpdateHeader->fragmentation & 0x03) << 4;
131 updateHeader |= (fpUpdateHeader->compression & 0x03) << 6;
132
133 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 1))
134 return FALSE;
135 Stream_Write_UINT8(s, updateHeader);
136
137 if (fpUpdateHeader->compression)
138 {
139 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 1))
140 return FALSE;
141
142 Stream_Write_UINT8(s, fpUpdateHeader->compressionFlags);
143 }
144
145 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 2))
146 return FALSE;
147
148 Stream_Write_UINT16(s, fpUpdateHeader->size);
149 return TRUE;
150}
151
152static UINT32 fastpath_get_update_header_size(FASTPATH_UPDATE_HEADER* fpUpdateHeader)
153{
154 WINPR_ASSERT(fpUpdateHeader);
155 return (fpUpdateHeader->compression) ? 4 : 3;
156}
157
158static BOOL fastpath_write_update_pdu_header(wStream* s,
159 const FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader,
160 rdpRdp* rdp)
161{
162 BYTE fpOutputHeader = 0;
163 WINPR_ASSERT(fpUpdatePduHeader);
164 WINPR_ASSERT(rdp);
165
166 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 3))
167 return FALSE;
168
169 fpOutputHeader |= (fpUpdatePduHeader->action & 0x03);
170 fpOutputHeader |= (fpUpdatePduHeader->secFlags & 0x03) << 6;
171 Stream_Write_UINT8(s, fpOutputHeader); /* fpOutputHeader (1 byte) */
172 Stream_Write_UINT8(s, 0x80 | (fpUpdatePduHeader->length >> 8)); /* length1 */
173 Stream_Write_UINT8(s, fpUpdatePduHeader->length & 0xFF); /* length2 */
174
175 if (fpUpdatePduHeader->secFlags)
176 {
177 WINPR_ASSERT(rdp->settings);
178 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
179 ENCRYPTION_METHOD_FIPS)
180 {
181 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 4))
182 return FALSE;
183
184 Stream_Write(s, fpUpdatePduHeader->fipsInformation, 4);
185 }
186
187 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
188 return FALSE;
189
190 Stream_Write(s, fpUpdatePduHeader->dataSignature, 8);
191 }
192
193 return TRUE;
194}
195
196static UINT32 fastpath_get_update_pdu_header_size(FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader,
197 rdpRdp* rdp)
198{
199 UINT32 size = 3; /* fpUpdatePduHeader + length1 + length2 */
200
201 if (!fpUpdatePduHeader || !rdp)
202 return 0;
203
204 if (fpUpdatePduHeader->secFlags)
205 {
206 size += 8; /* dataSignature */
207
208 WINPR_ASSERT(rdp->settings);
209 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
210 ENCRYPTION_METHOD_FIPS)
211 size += 4; /* fipsInformation */
212 }
213
214 return size;
215}
216
217BOOL fastpath_read_header_rdp(rdpFastPath* fastpath, wStream* s, UINT16* length)
218{
219 BYTE header = 0;
220
221 if (!s || !length)
222 return FALSE;
223
224 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
225 return FALSE;
226
227 Stream_Read_UINT8(s, header);
228
229 if (fastpath)
230 {
231 fastpath->encryptionFlags = (header & 0xC0) >> 6;
232 fastpath->numberEvents = (header & 0x3C) >> 2;
233 }
234
235 if (!per_read_length(s, length))
236 return FALSE;
237
238 const size_t pos = Stream_GetPosition(s);
239 if (pos > *length)
240 return FALSE;
241
242 *length = *length - (UINT16)pos;
243 return TRUE;
244}
245
246static BOOL fastpath_recv_orders(rdpUpdate* update, wStream* s)
247{
248 UINT16 numberOrders = 0;
249
250 if (!s)
251 {
252 WLog_ERR(TAG, "Invalid arguments");
253 return FALSE;
254 }
255
256 if (!update)
257 {
258 WLog_ERR(TAG, "Invalid configuration");
259 return FALSE;
260 }
261
262 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
263 return FALSE;
264
265 Stream_Read_UINT16(s, numberOrders); /* numberOrders (2 bytes) */
266
267 while (numberOrders > 0)
268 {
269 if (!update_recv_order(update, s))
270 return FALSE;
271
272 numberOrders--;
273 }
274
275 return TRUE;
276}
277
278static BOOL fastpath_recv_update_common(rdpUpdate* update, wStream* s)
279{
280 BOOL rc = FALSE;
281 UINT16 updateType = 0;
282 BOOL defaultReturn = 0;
283
284 rdp_update_internal* up = update_cast(update);
285 if (!s)
286 return FALSE;
287
288 if (!update || !update->context)
289 return FALSE;
290
291 rdpContext* context = update->context;
292
293 defaultReturn = freerdp_settings_get_bool(context->settings, FreeRDP_DeactivateClientDecoding);
294
295 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
296 return FALSE;
297
298 Stream_Read_UINT16(s, updateType); /* updateType (2 bytes) */
299 switch (updateType)
300 {
301 case UPDATE_TYPE_BITMAP:
302 {
303 BITMAP_UPDATE* bitmap_update = update_read_bitmap_update(update, s);
304
305 if (!bitmap_update)
306 return FALSE;
307
308 up->stats.base[RDP_STATS_BITMAP_UPDATE]++;
309 rc = IFCALLRESULT(defaultReturn, update->BitmapUpdate, context, bitmap_update);
310 free_bitmap_update(context, bitmap_update);
311 }
312 break;
313
314 case UPDATE_TYPE_PALETTE:
315 {
316 PALETTE_UPDATE* palette_update = update_read_palette(update, s);
317
318 if (!palette_update)
319 return FALSE;
320
321 up->stats.base[RDP_STATS_PALETTE]++;
322 rc = IFCALLRESULT(defaultReturn, update->Palette, context, palette_update);
323 free_palette_update(context, palette_update);
324 }
325 break;
326
327 default:
328 break;
329 }
330
331 return rc;
332}
333
334static BOOL fastpath_recv_update_synchronize(WINPR_ATTR_UNUSED rdpFastPath* fastpath, wStream* s)
335{
336 /* server 2008 can send invalid synchronize packet with missing padding,
337 so don't return FALSE even if the packet is invalid */
338 WINPR_ASSERT(fastpath);
339 WINPR_ASSERT(s);
340
341 const size_t len = Stream_GetRemainingLength(s);
342 const size_t skip = MIN(2, len);
343 return Stream_SafeSeek(s, skip); /* size (2 bytes), MUST be set to zero */
344}
345
346static BOOL fastpath_recv_update_paint_block(rdpUpdate* update, wStream* s,
347 BOOL (*fkt)(rdpUpdate*, wStream*))
348{
349 WINPR_ASSERT(fkt);
350 if (!update_begin_paint(update))
351 return FALSE;
352
353 BOOL res = fkt(update, s);
354 if (!update_end_paint(update))
355 return FALSE;
356 return res;
357}
358
359static int fastpath_recv_update(rdpFastPath* fastpath, BYTE updateCode, wStream* s)
360{
361 BOOL rc = FALSE;
362 int status = 0;
363
364 if (!fastpath || !fastpath->rdp || !s)
365 return -1;
366
367 Stream_SealLength(s);
368 Stream_ResetPosition(s);
369
370 rdpUpdate* update = fastpath->rdp->update;
371
372 if (!update || !update->pointer || !update->context)
373 return -1;
374
375 rdp_update_internal* up = update_cast(update);
376
377 rdpContext* context = update->context;
378 WINPR_ASSERT(context);
379
380 rdpPointerUpdate* pointer = update->pointer;
381 WINPR_ASSERT(pointer);
382
383#ifdef WITH_DEBUG_RDP
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));
386#endif
387
388 const BOOL defaultReturn =
389 freerdp_settings_get_bool(context->settings, FreeRDP_DeactivateClientDecoding);
390 switch (updateCode)
391 {
392 case FASTPATH_UPDATETYPE_ORDERS:
393 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_orders);
394 break;
395
396 case FASTPATH_UPDATETYPE_BITMAP:
397 case FASTPATH_UPDATETYPE_PALETTE:
398 rc = fastpath_recv_update_paint_block(update, s, fastpath_recv_update_common);
399 break;
400
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");
404 else
405 {
406 up->stats.base[RDP_STATS_SYNC]++;
407 rc = IFCALLRESULT(TRUE, update->Synchronize, context);
408 }
409
410 break;
411
412 case FASTPATH_UPDATETYPE_SURFCMDS:
413 status = fastpath_recv_update_paint_block(update, s, update_recv_surfcmds);
414 rc = (status >= 0);
415 break;
416
417 case FASTPATH_UPDATETYPE_PTR_NULL:
418 {
419 POINTER_SYSTEM_UPDATE pointer_system = WINPR_C_ARRAY_INIT;
420 pointer_system.type = SYSPTR_NULL;
421 up->stats.base[RDP_STATS_POINTER_SYSTEM]++;
422 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
423 }
424 break;
425
426 case FASTPATH_UPDATETYPE_PTR_DEFAULT:
427 {
428 POINTER_SYSTEM_UPDATE pointer_system = WINPR_C_ARRAY_INIT;
429 pointer_system.type = SYSPTR_DEFAULT;
430 up->stats.base[RDP_STATS_POINTER_DEFAULT]++;
431 rc = IFCALLRESULT(defaultReturn, pointer->PointerSystem, context, &pointer_system);
432 }
433 break;
434
435 case FASTPATH_UPDATETYPE_PTR_POSITION:
436 {
437 POINTER_POSITION_UPDATE* pointer_position = update_read_pointer_position(update, s);
438
439 if (pointer_position)
440 {
441 up->stats.base[RDP_STATS_POINTER_POSITION]++;
442 rc = IFCALLRESULT(defaultReturn, pointer->PointerPosition, context,
443 pointer_position);
444 free_pointer_position_update(context, pointer_position);
445 }
446 }
447 break;
448
449 case FASTPATH_UPDATETYPE_COLOR:
450 {
451 POINTER_COLOR_UPDATE* pointer_color = update_read_pointer_color(update, s, 24);
452
453 if (pointer_color)
454 {
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);
458 }
459 }
460 break;
461
462 case FASTPATH_UPDATETYPE_CACHED:
463 {
464 POINTER_CACHED_UPDATE* pointer_cached = update_read_pointer_cached(update, s);
465
466 if (pointer_cached)
467 {
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);
471 }
472 }
473 break;
474
475 case FASTPATH_UPDATETYPE_POINTER:
476 {
477 POINTER_NEW_UPDATE* pointer_new = update_read_pointer_new(update, s);
478
479 if (pointer_new)
480 {
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);
484 }
485 }
486 break;
487
488 case FASTPATH_UPDATETYPE_LARGE_POINTER:
489 {
490 POINTER_LARGE_UPDATE* pointer_large = update_read_pointer_large(update, s);
491
492 if (pointer_large)
493 {
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);
497 }
498 }
499 break;
500 default:
501 break;
502 }
503
504 Stream_ResetPosition(s);
505 if (!rc)
506 {
507 WLog_ERR(TAG, "Fastpath update %s [%" PRIx8 "] failed, status %d",
508 fastpath_update_to_string(updateCode), updateCode, status);
509 return -1;
510 }
511
512 return status;
513}
514
515static int fastpath_recv_update_data(rdpFastPath* fastpath, wStream* s)
516{
517 int status = 0;
518 UINT16 size = 0;
519 BYTE updateCode = 0;
520 BYTE fragmentation = 0;
521 BYTE compression = 0;
522 BYTE compressionFlags = 0;
523 UINT32 DstSize = 0;
524 const BYTE* pDstData = nullptr;
525
526 if (!fastpath || !s)
527 return -1;
528
529 rdpRdp* rdp = fastpath->rdp;
530
531 if (!rdp)
532 return -1;
533
534 rdpTransport* transport = rdp->transport;
535
536 if (!transport)
537 return -1;
538
539 if (!fastpath_read_update_header(s, &updateCode, &fragmentation, &compression))
540 return -1;
541
542 if (compression == FASTPATH_OUTPUT_COMPRESSION_USED)
543 {
544 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
545 return -1;
546
547 Stream_Read_UINT8(s, compressionFlags);
548 }
549 else
550 compressionFlags = 0;
551
552 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
553 return -1;
554
555 Stream_Read_UINT16(s, size);
556
557 if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
558 return -1;
559
560 const int bulkStatus =
561 bulk_decompress(rdp->bulk, Stream_Pointer(s), size, &pDstData, &DstSize, compressionFlags);
562 Stream_Seek(s, size);
563
564 if (bulkStatus < 0)
565 {
566 WLog_ERR(TAG, "bulk_decompress() failed");
567 return -1;
568 }
569
570 if (!Stream_EnsureRemainingCapacity(fastpath->updateData, DstSize))
571 return -1;
572
573 Stream_Write(fastpath->updateData, pDstData, DstSize);
574
575 if (fragmentation == FASTPATH_FRAGMENT_SINGLE)
576 {
577 if (fastpath->fragmentation != -1)
578 {
579 WLog_ERR(TAG, "Unexpected FASTPATH_FRAGMENT_SINGLE");
580 goto out_fail;
581 }
582
583 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
584
585 if (status < 0)
586 {
587 WLog_ERR(TAG, "fastpath_recv_update() - %i", status);
588 goto out_fail;
589 }
590 }
591 else
592 {
593 rdpContext* context = nullptr;
594 const size_t totalSize = Stream_GetPosition(fastpath->updateData);
595
596 context = transport_get_context(transport);
597 WINPR_ASSERT(context);
598 WINPR_ASSERT(context->settings);
599
600 if (totalSize >
601 freerdp_settings_get_uint32(context->settings, FreeRDP_MultifragMaxRequestSize))
602 {
603 WLog_ERR(
604 TAG, "Total size (%" PRIuz ") exceeds MultifragMaxRequestSize (%" PRIu32 ")",
605 totalSize,
606 freerdp_settings_get_uint32(context->settings, FreeRDP_MultifragMaxRequestSize));
607 goto out_fail;
608 }
609
610 if (fragmentation == FASTPATH_FRAGMENT_FIRST)
611 {
612 if (fastpath->fragmentation != -1)
613 {
614 WLog_ERR(TAG, "Unexpected FASTPATH_FRAGMENT_FIRST");
615 goto out_fail;
616 }
617
618 fastpath->fragmentation = FASTPATH_FRAGMENT_FIRST;
619 }
620 else if (fragmentation == FASTPATH_FRAGMENT_NEXT)
621 {
622 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
623 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
624 {
625 WLog_ERR(TAG, "Unexpected FASTPATH_FRAGMENT_NEXT");
626 goto out_fail;
627 }
628
629 fastpath->fragmentation = FASTPATH_FRAGMENT_NEXT;
630 }
631 else if (fragmentation == FASTPATH_FRAGMENT_LAST)
632 {
633 if ((fastpath->fragmentation != FASTPATH_FRAGMENT_FIRST) &&
634 (fastpath->fragmentation != FASTPATH_FRAGMENT_NEXT))
635 {
636 WLog_ERR(TAG, "Unexpected FASTPATH_FRAGMENT_LAST");
637 goto out_fail;
638 }
639
640 fastpath->fragmentation = -1;
641 status = fastpath_recv_update(fastpath, updateCode, fastpath->updateData);
642
643 if (status < 0)
644 {
645 WLog_ERR(TAG, "fastpath_recv_update() - %i", status);
646 goto out_fail;
647 }
648 }
649 }
650
651 return status;
652out_fail:
653 return -1;
654}
655
656state_run_t fastpath_recv_updates(rdpFastPath* fastpath, wStream* s)
657{
658 state_run_t rc = STATE_RUN_FAILED;
659
660 WINPR_ASSERT(s);
661 WINPR_ASSERT(fastpath);
662 WINPR_ASSERT(fastpath->rdp);
663
664 while (Stream_GetRemainingLength(s) >= 3)
665 {
666 if (fastpath_recv_update_data(fastpath, s) < 0)
667 {
668 WLog_ERR(TAG, "fastpath_recv_update_data() fail");
669 rc = STATE_RUN_FAILED;
670 goto fail;
671 }
672 }
673
674 rc = STATE_RUN_SUCCESS;
675fail:
676
677 return rc;
678}
679
680static BOOL fastpath_read_input_event_header(wStream* s, BYTE* eventFlags, BYTE* eventCode)
681{
682 BYTE eventHeader = 0;
683
684 WINPR_ASSERT(s);
685 WINPR_ASSERT(eventFlags);
686 WINPR_ASSERT(eventCode);
687
688 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
689 return FALSE;
690
691 Stream_Read_UINT8(s, eventHeader); /* eventHeader (1 byte) */
692 *eventFlags = (eventHeader & 0x1F);
693 *eventCode = (eventHeader >> 5);
694 return TRUE;
695}
696
697static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
698{
699 WINPR_ASSERT(fastpath);
700 WINPR_ASSERT(fastpath->rdp);
701 WINPR_ASSERT(fastpath->rdp->input);
702 WINPR_ASSERT(s);
703
704 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
705 return FALSE;
706
707 rdpInput* input = fastpath->rdp->input;
708
709 const UINT8 code = Stream_Get_UINT8(s); /* keyCode (1 byte) */
710
711 UINT16 flags = 0;
712 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
713 flags |= KBD_FLAGS_RELEASE;
714
715 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
716 flags |= KBD_FLAGS_EXTENDED;
717
718 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
719 flags |= KBD_FLAGS_EXTENDED1;
720
721 return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
722}
723
724static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath, wStream* s,
725 WINPR_ATTR_UNUSED BYTE eventFlags)
726{
727 rdpInput* input = nullptr;
728 UINT16 pointerFlags = 0;
729 UINT16 xPos = 0;
730 UINT16 yPos = 0;
731 WINPR_ASSERT(fastpath);
732 WINPR_ASSERT(fastpath->rdp);
733 WINPR_ASSERT(fastpath->rdp->input);
734 WINPR_ASSERT(s);
735
736 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
737 return FALSE;
738
739 input = fastpath->rdp->input;
740
741 Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
742 Stream_Read_UINT16(s, xPos); /* xPos (2 bytes) */
743 Stream_Read_UINT16(s, yPos); /* yPos (2 bytes) */
744 return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
745}
746
747static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath, wStream* s,
748 WINPR_ATTR_UNUSED BYTE eventFlags)
749{
750 rdpInput* input = nullptr;
751 UINT16 pointerFlags = 0;
752 INT16 xDelta = 0;
753 INT16 yDelta = 0;
754 WINPR_ASSERT(fastpath);
755 WINPR_ASSERT(fastpath->rdp);
756 WINPR_ASSERT(fastpath->rdp->context);
757 WINPR_ASSERT(fastpath->rdp->input);
758 WINPR_ASSERT(s);
759
760 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
761 return FALSE;
762
763 input = fastpath->rdp->input;
764
765 Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
766 Stream_Read_INT16(s, xDelta); /* xDelta (2 bytes) */
767 Stream_Read_INT16(s, yDelta); /* yDelta (2 bytes) */
768
769 if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasRelativeMouseEvent))
770 {
771 WLog_ERR(TAG,
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);
775 return FALSE;
776 }
777
778 return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
779}
780
781static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath, wStream* s,
782 WINPR_ATTR_UNUSED BYTE eventFlags)
783{
784 WINPR_ASSERT(fastpath);
785 WINPR_ASSERT(fastpath->rdp);
786 WINPR_ASSERT(fastpath->rdp->context);
787 WINPR_ASSERT(fastpath->rdp->input);
788 WINPR_ASSERT(s);
789
790 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
791 return FALSE;
792
793 rdpInput* input = fastpath->rdp->input;
794
795 UINT32 timestampMS = 0;
796 Stream_Read_UINT32(s, timestampMS); /* timestamp (4 bytes) */
797
798 if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasQoeEvent))
799 {
800 WLog_ERR(TAG,
801 "Received qoe event(timestamp=%" PRIu32
802 "ms), but we did not announce support for that",
803 timestampMS);
804 return FALSE;
805 }
806
807 return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
808}
809
810static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath, wStream* s,
811 WINPR_ATTR_UNUSED BYTE eventFlags)
812{
813 rdpInput* input = nullptr;
814 UINT16 pointerFlags = 0;
815 UINT16 xPos = 0;
816 UINT16 yPos = 0;
817
818 WINPR_ASSERT(fastpath);
819 WINPR_ASSERT(fastpath->rdp);
820 WINPR_ASSERT(fastpath->rdp->context);
821 WINPR_ASSERT(fastpath->rdp->input);
822 WINPR_ASSERT(s);
823
824 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
825 return FALSE;
826
827 input = fastpath->rdp->input;
828
829 Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
830 Stream_Read_UINT16(s, xPos); /* xPos (2 bytes) */
831 Stream_Read_UINT16(s, yPos); /* yPos (2 bytes) */
832
833 if (!freerdp_settings_get_bool(input->context->settings, FreeRDP_HasExtendedMouseEvent))
834 {
835 WLog_ERR(TAG,
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);
839 return FALSE;
840 }
841
842 return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
843}
844
845static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, WINPR_ATTR_UNUSED wStream* s,
846 BYTE eventFlags)
847{
848 rdpInput* input = nullptr;
849
850 WINPR_ASSERT(fastpath);
851 WINPR_ASSERT(fastpath->rdp);
852 WINPR_ASSERT(fastpath->rdp->input);
853 WINPR_ASSERT(s);
854
855 input = fastpath->rdp->input;
856 return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
857}
858
859static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
860{
861 UINT16 unicodeCode = 0;
862 UINT16 flags = 0;
863
864 WINPR_ASSERT(fastpath);
865 WINPR_ASSERT(s);
866
867 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
868 return FALSE;
869
870 Stream_Read_UINT16(s, unicodeCode); /* unicodeCode (2 bytes) */
871 flags = 0;
872
873 if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
874 flags |= KBD_FLAGS_RELEASE;
875
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,
880 flags, unicodeCode);
881}
882
883static BOOL fastpath_recv_input_event(rdpFastPath* fastpath, wStream* s)
884{
885 BYTE eventFlags = 0;
886 BYTE eventCode = 0;
887
888 WINPR_ASSERT(fastpath);
889 WINPR_ASSERT(s);
890
891 if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
892 return FALSE;
893
894 switch (eventCode)
895 {
896 case FASTPATH_INPUT_EVENT_SCANCODE:
897 if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
898 return FALSE;
899
900 break;
901
902 case FASTPATH_INPUT_EVENT_MOUSE:
903 if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
904 return FALSE;
905
906 break;
907
908 case FASTPATH_INPUT_EVENT_MOUSEX:
909 if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
910 return FALSE;
911
912 break;
913
914 case FASTPATH_INPUT_EVENT_SYNC:
915 if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
916 return FALSE;
917
918 break;
919
920 case FASTPATH_INPUT_EVENT_UNICODE:
921 if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
922 return FALSE;
923
924 break;
925
926 case TS_FP_RELPOINTER_EVENT:
927 if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
928 return FALSE;
929
930 break;
931
932 case TS_FP_QOETIMESTAMP_EVENT:
933 if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
934 return FALSE;
935 break;
936
937 default:
938 WLog_ERR(TAG, "Unknown eventCode %" PRIu8 "", eventCode);
939 break;
940 }
941
942 return TRUE;
943}
944
945state_run_t fastpath_recv_inputs(rdpFastPath* fastpath, wStream* s)
946{
947 WINPR_ASSERT(fastpath);
948 WINPR_ASSERT(s);
949
950 if (fastpath->numberEvents == 0)
951 {
956 if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
957 return STATE_RUN_FAILED;
958
959 Stream_Read_UINT8(s, fastpath->numberEvents); /* eventHeader (1 byte) */
960 }
961
962 for (BYTE i = 0; i < fastpath->numberEvents; i++)
963 {
964 if (!fastpath_recv_input_event(fastpath, s))
965 return STATE_RUN_FAILED;
966 }
967
968 return STATE_RUN_SUCCESS;
969}
970
971static UINT32 fastpath_get_sec_bytes(rdpRdp* rdp)
972{
973 UINT32 sec_bytes = 0;
974
975 if (!rdp)
976 return 0;
977
978 if (rdp->do_crypt)
979 {
980 sec_bytes = 8;
981
982 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
983 ENCRYPTION_METHOD_FIPS)
984 sec_bytes += 4;
985 }
986
987 return sec_bytes;
988}
989
990wStream* fastpath_input_pdu_init_header(rdpFastPath* fastpath, UINT16* sec_flags)
991{
992 if (!fastpath || !fastpath->rdp)
993 return nullptr;
994
995 rdpRdp* rdp = fastpath->rdp;
996 wStream* s = transport_send_stream_init(rdp->transport, 256);
997
998 if (!s)
999 return nullptr;
1000
1001 Stream_Seek(s, 3); /* fpInputHeader, length1 and length2 */
1002
1003 if (rdp->do_crypt)
1004 {
1005 *sec_flags |= SEC_ENCRYPT;
1006
1007 if (rdp->do_secure_checksum)
1008 *sec_flags |= SEC_SECURE_CHECKSUM;
1009 }
1010
1011 Stream_Seek(s, fastpath_get_sec_bytes(rdp));
1012 return s;
1013}
1014
1015wStream* fastpath_input_pdu_init(rdpFastPath* fastpath, BYTE eventFlags, BYTE eventCode,
1016 UINT16* sec_flags)
1017{
1018 wStream* s = nullptr;
1019 s = fastpath_input_pdu_init_header(fastpath, sec_flags);
1020
1021 if (!s)
1022 return nullptr;
1023
1024 WINPR_ASSERT(eventCode < 8);
1025 WINPR_ASSERT(eventFlags < 0x20);
1026 Stream_Write_UINT8(s, (UINT8)(eventFlags | (eventCode << 5))); /* eventHeader (1 byte) */
1027 return s;
1028}
1029
1030BOOL fastpath_send_multiple_input_pdu(rdpFastPath* fastpath, wStream* s, size_t iNumEvents,
1031 UINT16 sec_flags)
1032{
1033 BOOL rc = FALSE;
1034 BYTE eventHeader = 0;
1035 BOOL should_unlock = FALSE;
1036 rdpRdp* rdp = nullptr;
1037
1038 WINPR_ASSERT(iNumEvents > 0);
1039 if (!s)
1040 return FALSE;
1041
1042 if (!fastpath)
1043 goto fail;
1044
1045 rdp = fastpath->rdp;
1046 WINPR_ASSERT(rdp);
1047
1048 {
1049 const CONNECTION_STATE state = rdp_get_state(rdp);
1050 if (!rdp_is_active_state(rdp))
1051 {
1052 WLog_WARN(TAG, "called before activation [%s]", rdp_state_string(state));
1053 goto fail;
1054 }
1055 }
1056
1057 /*
1058 * A maximum of 15 events are allowed per request
1059 * if the optional numEvents field isn't used
1060 * see MS-RDPBCGR 2.2.8.1.2 for details
1061 */
1062 if (iNumEvents > 15)
1063 goto fail;
1064
1065 {
1066 size_t length = Stream_GetPosition(s);
1067
1068 if (length >= (2u << 14))
1069 {
1070 WLog_ERR(TAG, "Maximum FastPath PDU length is 32767");
1071 goto fail;
1072 }
1073
1074 eventHeader = FASTPATH_INPUT_ACTION_FASTPATH;
1075 eventHeader |= (iNumEvents << 2); /* numberEvents */
1076
1077 if (sec_flags & SEC_ENCRYPT)
1078 eventHeader |= (FASTPATH_INPUT_ENCRYPTED << 6);
1079
1080 if (sec_flags & SEC_SECURE_CHECKSUM)
1081 eventHeader |= (FASTPATH_INPUT_SECURE_CHECKSUM << 6);
1082
1083 Stream_ResetPosition(s);
1084 Stream_Write_UINT8(s, eventHeader);
1085 /* Write length later, RDP encryption might add a padding */
1086 Stream_Seek(s, 2);
1087
1088 if (sec_flags & SEC_ENCRYPT)
1089 {
1090 security_lock(rdp);
1091 should_unlock = TRUE;
1092
1093 const size_t sec_bytes = fastpath_get_sec_bytes(fastpath->rdp);
1094 if (sec_bytes + 3ULL > length)
1095 goto fail;
1096
1097 BYTE* fpInputEvents = Stream_PointerAs(s, BYTE) + sec_bytes;
1098 const UINT16 fpInputEvents_length = (UINT16)(length - 3 - sec_bytes);
1099
1100 WINPR_ASSERT(rdp->settings);
1101 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
1102 ENCRYPTION_METHOD_FIPS)
1103 {
1104 BYTE pad = 0;
1105
1106 if ((pad = 8 - (fpInputEvents_length % 8)) == 8)
1107 pad = 0;
1108
1109 Stream_Write_UINT16(s, 0x10); /* length */
1110 Stream_Write_UINT8(s, 0x1); /* TSFIPS_VERSION 1*/
1111 Stream_Write_UINT8(s, pad); /* padding */
1112
1113 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1114 goto fail;
1115
1116 if (!security_hmac_signature(fpInputEvents, fpInputEvents_length, Stream_Pointer(s),
1117 8, rdp))
1118 goto fail;
1119
1120 if (pad)
1121 memset(fpInputEvents + fpInputEvents_length, 0, pad);
1122
1123 if (!security_fips_encrypt(fpInputEvents, fpInputEvents_length + pad, rdp))
1124 goto fail;
1125
1126 length += pad;
1127 }
1128 else
1129 {
1130 BOOL res = 0;
1131 if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 8))
1132 goto fail;
1133 if (sec_flags & SEC_SECURE_CHECKSUM)
1134 res = security_salted_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1135 TRUE, Stream_Pointer(s), 8);
1136 else
1137 res = security_mac_signature(rdp, fpInputEvents, fpInputEvents_length,
1138 Stream_Pointer(s), 8);
1139
1140 if (!res || !security_encrypt(fpInputEvents, fpInputEvents_length, rdp))
1141 goto fail;
1142 }
1143 }
1144
1145 /*
1146 * We always encode length in two bytes, even though we could use
1147 * only one byte if length <= 0x7F. It is just easier that way,
1148 * because we can leave room for fixed-length header, store all
1149 * the data first and then store the header.
1150 */
1151 WINPR_ASSERT(length < UINT16_MAX);
1152 if (!Stream_SetPosition(s, 1))
1153 goto fail;
1154 Stream_Write_UINT16_BE(s, 0x8000 | (UINT16)length);
1155 if (!Stream_SetPosition(s, length))
1156 goto fail;
1157 Stream_SealLength(s);
1158 }
1159
1160 if (transport_write(rdp->transport, s) < 0)
1161 goto fail;
1162
1163 rc = TRUE;
1164fail:
1165 if (should_unlock)
1166 security_unlock(rdp);
1167 Stream_Release(s);
1168 return rc;
1169}
1170
1171BOOL fastpath_send_input_pdu(rdpFastPath* fastpath, wStream* s, UINT16 sec_flags)
1172{
1173 return fastpath_send_multiple_input_pdu(fastpath, s, 1, sec_flags);
1174}
1175
1176wStream* fastpath_update_pdu_init(rdpFastPath* fastpath)
1177{
1178 return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1179}
1180
1181wStream* fastpath_update_pdu_init_new(WINPR_ATTR_UNUSED rdpFastPath* fastpath)
1182{
1183 wStream* s = nullptr;
1184 s = Stream_New(nullptr, FASTPATH_MAX_PACKET_SIZE);
1185 return s;
1186}
1187
1188BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s,
1189 BOOL skipCompression)
1190{
1191 BOOL status = TRUE;
1192 wStream* fs = nullptr;
1193 rdpSettings* settings = nullptr;
1194 rdpRdp* rdp = nullptr;
1195 UINT32 fpHeaderSize = 6;
1196 UINT32 fpUpdatePduHeaderSize = 0;
1197 UINT32 fpUpdateHeaderSize = 0;
1198 FASTPATH_UPDATE_PDU_HEADER fpUpdatePduHeader = WINPR_C_ARRAY_INIT;
1199 FASTPATH_UPDATE_HEADER fpUpdateHeader = WINPR_C_ARRAY_INIT;
1200 UINT16 sec_flags = 0;
1201
1202 if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1203 return FALSE;
1204
1205 rdp = fastpath->rdp;
1206 fs = fastpath->fs;
1207 settings = rdp->settings;
1208
1209 if (!settings)
1210 return FALSE;
1211
1212 UINT16 maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1213
1214 if (freerdp_settings_get_bool(rdp->settings, FreeRDP_CompressionEnabled) && !skipCompression)
1215 {
1216 const UINT16 CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1217 maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1218 maxLength -= 20;
1219 }
1220
1221 size_t totalLength = Stream_GetPosition(s);
1222 Stream_ResetPosition(s);
1223
1224 /* check if fast path output is possible */
1225 if (!freerdp_settings_get_bool(rdp->settings, FreeRDP_FastPathOutput))
1226 {
1227 WLog_ERR(TAG, "client does not support fast path output");
1228 return FALSE;
1229 }
1230
1231 /* check if the client's fast path pdu buffer is large enough */
1232 if (totalLength > freerdp_settings_get_uint32(settings, FreeRDP_MultifragMaxRequestSize))
1233 {
1234 WLog_ERR(TAG,
1235 "fast path update size (%" PRIuz
1236 ") exceeds the client's maximum request size (%" PRIu32 ")",
1237 totalLength,
1238 freerdp_settings_get_uint32(settings, FreeRDP_MultifragMaxRequestSize));
1239 return FALSE;
1240 }
1241
1242 if (rdp->do_crypt)
1243 {
1244 sec_flags |= SEC_ENCRYPT;
1245
1246 if (rdp->do_secure_checksum)
1247 sec_flags |= SEC_SECURE_CHECKSUM;
1248 }
1249
1250 for (int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1251 {
1252 UINT32 DstSize = 0;
1253 const BYTE* pDstData = nullptr;
1254 UINT32 compressionFlags = 0;
1255 BYTE pad = 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;
1266
1267 if (sec_flags & SEC_ENCRYPT)
1268 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1269
1270 if (sec_flags & SEC_SECURE_CHECKSUM)
1271 fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1272
1273 if (freerdp_settings_get_bool(settings, FreeRDP_CompressionEnabled) && !skipCompression)
1274 {
1275 if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1276 &compressionFlags) >= 0)
1277 {
1278 if (compressionFlags)
1279 {
1280 WINPR_ASSERT(compressionFlags <= UINT8_MAX);
1281 fpUpdateHeader.compressionFlags = (UINT8)compressionFlags;
1282 fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1283 }
1284 }
1285 }
1286
1287 if (!fpUpdateHeader.compression)
1288 {
1289 pDstData = Stream_Pointer(s);
1290 DstSize = fpUpdateHeader.size;
1291 }
1292
1293 if (DstSize > UINT16_MAX)
1294 return FALSE;
1295 fpUpdateHeader.size = (UINT16)DstSize;
1296 totalLength -= SrcSize;
1297
1298 if (totalLength == 0)
1299 fpUpdateHeader.fragmentation =
1300 (fragment == 0) ? FASTPATH_FRAGMENT_SINGLE : FASTPATH_FRAGMENT_LAST;
1301 else
1302 fpUpdateHeader.fragmentation =
1303 (fragment == 0) ? FASTPATH_FRAGMENT_FIRST : FASTPATH_FRAGMENT_NEXT;
1304
1305 fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1306 fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1307 fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1308
1309 if (sec_flags & SEC_ENCRYPT)
1310 {
1311 pSignature = Stream_Buffer(fs) + 3;
1312
1313 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
1314 ENCRYPTION_METHOD_FIPS)
1315 {
1316 pSignature += 4;
1317
1318 if ((pad = 8 - ((DstSize + fpUpdateHeaderSize) % 8)) == 8)
1319 pad = 0;
1320
1321 fpUpdatePduHeader.fipsInformation[0] = 0x10;
1322 fpUpdatePduHeader.fipsInformation[1] = 0x00;
1323 fpUpdatePduHeader.fipsInformation[2] = 0x01;
1324 fpUpdatePduHeader.fipsInformation[3] = pad;
1325 }
1326 }
1327
1328 const size_t len = fpUpdateHeader.size + fpHeaderSize + pad;
1329 if (len > UINT16_MAX)
1330 return FALSE;
1331
1332 fpUpdatePduHeader.length = (UINT16)len;
1333 Stream_ResetPosition(fs);
1334 if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1335 return FALSE;
1336 if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1337 return FALSE;
1338
1339 if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (size_t)DstSize + pad))
1340 return FALSE;
1341 Stream_Write(fs, pDstData, DstSize);
1342
1343 if (pad)
1344 Stream_Zero(fs, pad);
1345
1346 BOOL res = FALSE;
1347 if (sec_flags & SEC_ENCRYPT)
1348 {
1349 security_lock(rdp);
1350
1351 should_unlock = TRUE;
1352 UINT32 dataSize = fpUpdateHeaderSize + DstSize + pad;
1353 BYTE* data = Stream_PointerAs(fs, BYTE) - dataSize;
1354
1355 if (freerdp_settings_get_uint32(rdp->settings, FreeRDP_EncryptionMethods) ==
1356 ENCRYPTION_METHOD_FIPS)
1357 {
1358 // TODO: Ensure stream capacity
1359 if (!security_hmac_signature(data, dataSize - pad, pSignature, 8, rdp))
1360 goto unlock;
1361
1362 if (!security_fips_encrypt(data, dataSize, rdp))
1363 goto unlock;
1364 }
1365 else
1366 {
1367 // TODO: Ensure stream capacity
1368 if (sec_flags & SEC_SECURE_CHECKSUM)
1369 status =
1370 security_salted_mac_signature(rdp, data, dataSize, TRUE, pSignature, 8);
1371 else
1372 status = security_mac_signature(rdp, data, dataSize, pSignature, 8);
1373
1374 if (!status || !security_encrypt(data, dataSize, rdp))
1375 goto unlock;
1376 }
1377 }
1378 res = TRUE;
1379
1380 Stream_SealLength(fs);
1381
1382 if (transport_write(rdp->transport, fs) < 0)
1383 {
1384 status = FALSE;
1385 }
1386
1387 unlock:
1388 if (should_unlock)
1389 security_unlock(rdp);
1390
1391 if (!res || !status)
1392 return FALSE;
1393
1394 Stream_Seek(s, SrcSize);
1395 }
1396
1397 return status;
1398}
1399
1400rdpFastPath* fastpath_new(rdpRdp* rdp)
1401{
1402 rdpFastPath* fastpath = nullptr;
1403
1404 WINPR_ASSERT(rdp);
1405
1406 fastpath = (rdpFastPath*)calloc(1, sizeof(rdpFastPath));
1407
1408 if (!fastpath)
1409 return nullptr;
1410
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);
1415
1416 if (!fastpath->fs || !fastpath->updateData)
1417 goto out_free;
1418
1419 return fastpath;
1420out_free:
1421 fastpath_free(fastpath);
1422 return nullptr;
1423}
1424
1425void fastpath_free(rdpFastPath* fastpath)
1426{
1427 if (fastpath)
1428 {
1429 Stream_Free(fastpath->updateData, TRUE);
1430 Stream_Free(fastpath->fs, TRUE);
1431 free(fastpath);
1432 }
1433}
1434
1435BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1436{
1437 WINPR_ASSERT(fastpath);
1438 return fastpath->encryptionFlags;
1439}
1440
1441BOOL fastpath_decrypt(rdpFastPath* fastpath, wStream* s, UINT16* length)
1442{
1443 WINPR_ASSERT(fastpath);
1444 if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1445 {
1446 const UINT16 flags =
1447 (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1448 ? SEC_SECURE_CHECKSUM
1449 : 0;
1450
1451 if (!rdp_decrypt(fastpath->rdp, s, length, flags))
1452 return FALSE;
1453 }
1454
1455 return TRUE;
1456}
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.