Coverage Report

Created: 2025-08-24 06:59

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