Coverage Report

Created: 2024-09-08 06:20

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