Coverage Report

Created: 2026-09-14 06:32

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