Coverage Report

Created: 2024-05-20 06:11

/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
158
{
126
158
  BYTE updateHeader = 0;
127
158
  WINPR_ASSERT(fpUpdateHeader);
128
129
158
  updateHeader |= fpUpdateHeader->updateCode & 0x0F;
130
158
  updateHeader |= (fpUpdateHeader->fragmentation & 0x03) << 4;
131
158
  updateHeader |= (fpUpdateHeader->compression & 0x03) << 6;
132
133
158
  if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 1))
134
0
    return FALSE;
135
158
  Stream_Write_UINT8(s, updateHeader);
136
137
158
  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
158
  if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 2))
146
0
    return FALSE;
147
148
158
  Stream_Write_UINT16(s, fpUpdateHeader->size);
149
158
  return TRUE;
150
158
}
151
152
static UINT32 fastpath_get_update_header_size(FASTPATH_UPDATE_HEADER* fpUpdateHeader)
153
158
{
154
158
  WINPR_ASSERT(fpUpdateHeader);
155
158
  return (fpUpdateHeader->compression) ? 4 : 3;
156
158
}
157
158
static BOOL fastpath_write_update_pdu_header(wStream* s,
159
                                             const FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader,
160
                                             rdpRdp* rdp)
161
158
{
162
158
  BYTE fpOutputHeader = 0;
163
158
  WINPR_ASSERT(fpUpdatePduHeader);
164
158
  WINPR_ASSERT(rdp);
165
166
158
  if (!Stream_CheckAndLogRequiredCapacity(TAG, s, 3))
167
0
    return FALSE;
168
169
158
  fpOutputHeader |= (fpUpdatePduHeader->action & 0x03);
170
158
  fpOutputHeader |= (fpUpdatePduHeader->secFlags & 0x03) << 6;
171
158
  Stream_Write_UINT8(s, fpOutputHeader);                          /* fpOutputHeader (1 byte) */
172
158
  Stream_Write_UINT8(s, 0x80 | (fpUpdatePduHeader->length >> 8)); /* length1 */
173
158
  Stream_Write_UINT8(s, fpUpdatePduHeader->length & 0xFF);        /* length2 */
174
175
158
  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
158
  return TRUE;
193
158
}
194
195
static UINT32 fastpath_get_update_pdu_header_size(FASTPATH_UPDATE_PDU_HEADER* fpUpdatePduHeader,
196
                                                  rdpRdp* rdp)
197
158
{
198
158
  UINT32 size = 3; /* fpUpdatePduHeader + length1 + length2 */
199
200
158
  if (!fpUpdatePduHeader || !rdp)
201
0
    return 0;
202
203
158
  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
158
  return size;
213
158
}
214
215
BOOL fastpath_read_header_rdp(rdpFastPath* fastpath, wStream* s, UINT16* length)
216
14.9k
{
217
14.9k
  BYTE header = 0;
218
219
14.9k
  if (!s || !length)
220
0
    return FALSE;
221
222
14.9k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
223
356
    return FALSE;
224
225
14.6k
  Stream_Read_UINT8(s, header);
226
227
14.6k
  if (fastpath)
228
14.6k
  {
229
14.6k
    fastpath->encryptionFlags = (header & 0xC0) >> 6;
230
14.6k
    fastpath->numberEvents = (header & 0x3C) >> 2;
231
14.6k
  }
232
233
14.6k
  if (!per_read_length(s, length))
234
76
    return FALSE;
235
236
14.5k
  const size_t pos = Stream_GetPosition(s);
237
14.5k
  if (pos > *length)
238
6.82k
    return FALSE;
239
240
7.71k
  *length = *length - (UINT16)pos;
241
7.71k
  return TRUE;
242
14.5k
}
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
14.9k
{
632
14.9k
  state_run_t rc = STATE_RUN_FAILED;
633
634
14.9k
  WINPR_ASSERT(s);
635
14.9k
  WINPR_ASSERT(fastpath);
636
14.9k
  WINPR_ASSERT(fastpath->rdp);
637
638
14.9k
  rdpUpdate* update = fastpath->rdp->update;
639
14.9k
  WINPR_ASSERT(update);
640
641
14.9k
  if (!update_begin_paint(update))
642
14.9k
    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
14.9k
fail:
656
657
14.9k
  if (!update_end_paint(update))
658
0
    return STATE_RUN_FAILED;
659
660
14.9k
  return rc;
661
14.9k
}
662
663
static BOOL fastpath_read_input_event_header(wStream* s, BYTE* eventFlags, BYTE* eventCode)
664
43.0k
{
665
43.0k
  BYTE eventHeader = 0;
666
667
43.0k
  WINPR_ASSERT(s);
668
43.0k
  WINPR_ASSERT(eventFlags);
669
43.0k
  WINPR_ASSERT(eventCode);
670
671
43.0k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
672
236
    return FALSE;
673
674
42.8k
  Stream_Read_UINT8(s, eventHeader); /* eventHeader (1 byte) */
675
42.8k
  *eventFlags = (eventHeader & 0x1F);
676
42.8k
  *eventCode = (eventHeader >> 5);
677
42.8k
  return TRUE;
678
43.0k
}
679
680
static BOOL fastpath_recv_input_event_scancode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
681
18.7k
{
682
18.7k
  rdpInput* input = NULL;
683
18.7k
  UINT16 flags = 0;
684
18.7k
  UINT16 code = 0;
685
18.7k
  WINPR_ASSERT(fastpath);
686
18.7k
  WINPR_ASSERT(fastpath->rdp);
687
18.7k
  WINPR_ASSERT(fastpath->rdp->input);
688
18.7k
  WINPR_ASSERT(s);
689
690
18.7k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
691
35
    return FALSE;
692
693
18.7k
  input = fastpath->rdp->input;
694
695
18.7k
  Stream_Read_UINT8(s, code); /* keyCode (1 byte) */
696
18.7k
  flags = 0;
697
698
18.7k
  if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
699
4.23k
    flags |= KBD_FLAGS_RELEASE;
700
701
18.7k
  if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_EXTENDED))
702
3.73k
    flags |= KBD_FLAGS_EXTENDED;
703
704
18.7k
  if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_PREFIX_E1))
705
4.26k
    flags |= KBD_FLAGS_EXTENDED1;
706
707
18.7k
  return IFCALLRESULT(TRUE, input->KeyboardEvent, input, flags, code);
708
18.7k
}
709
710
static BOOL fastpath_recv_input_event_mouse(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
711
3.20k
{
712
3.20k
  rdpInput* input = NULL;
713
3.20k
  UINT16 pointerFlags = 0;
714
3.20k
  UINT16 xPos = 0;
715
3.20k
  UINT16 yPos = 0;
716
3.20k
  WINPR_ASSERT(fastpath);
717
3.20k
  WINPR_ASSERT(fastpath->rdp);
718
3.20k
  WINPR_ASSERT(fastpath->rdp->input);
719
3.20k
  WINPR_ASSERT(s);
720
721
3.20k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
722
64
    return FALSE;
723
724
3.14k
  input = fastpath->rdp->input;
725
726
3.14k
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
727
3.14k
  Stream_Read_UINT16(s, xPos);         /* xPos (2 bytes) */
728
3.14k
  Stream_Read_UINT16(s, yPos);         /* yPos (2 bytes) */
729
3.14k
  return IFCALLRESULT(TRUE, input->MouseEvent, input, pointerFlags, xPos, yPos);
730
3.20k
}
731
732
static BOOL fastpath_recv_input_event_relmouse(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
733
1.80k
{
734
1.80k
  rdpInput* input = NULL;
735
1.80k
  UINT16 pointerFlags = 0;
736
1.80k
  INT16 xDelta = 0;
737
1.80k
  INT16 yDelta = 0;
738
1.80k
  WINPR_ASSERT(fastpath);
739
1.80k
  WINPR_ASSERT(fastpath->rdp);
740
1.80k
  WINPR_ASSERT(fastpath->rdp->context);
741
1.80k
  WINPR_ASSERT(fastpath->rdp->input);
742
1.80k
  WINPR_ASSERT(s);
743
744
1.80k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
745
19
    return FALSE;
746
747
1.78k
  input = fastpath->rdp->input;
748
749
1.78k
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
750
1.78k
  Stream_Read_INT16(s, xDelta);        /* xDelta (2 bytes) */
751
1.78k
  Stream_Read_INT16(s, yDelta);        /* yDelta (2 bytes) */
752
753
1.78k
  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.78k
  return IFCALLRESULT(TRUE, input->RelMouseEvent, input, pointerFlags, xDelta, yDelta);
763
1.78k
}
764
765
static BOOL fastpath_recv_input_event_qoe(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
766
2.59k
{
767
2.59k
  WINPR_ASSERT(fastpath);
768
2.59k
  WINPR_ASSERT(fastpath->rdp);
769
2.59k
  WINPR_ASSERT(fastpath->rdp->context);
770
2.59k
  WINPR_ASSERT(fastpath->rdp->input);
771
2.59k
  WINPR_ASSERT(s);
772
773
2.59k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
774
18
    return FALSE;
775
776
2.57k
  rdpInput* input = fastpath->rdp->input;
777
778
2.57k
  UINT32 timestampMS = 0;
779
2.57k
  Stream_Read_UINT32(s, timestampMS); /* timestamp (4 bytes) */
780
781
2.57k
  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.57k
  return IFCALLRESULT(TRUE, input->QoEEvent, input, timestampMS);
791
2.57k
}
792
793
static BOOL fastpath_recv_input_event_mousex(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
794
2.49k
{
795
2.49k
  rdpInput* input = NULL;
796
2.49k
  UINT16 pointerFlags = 0;
797
2.49k
  UINT16 xPos = 0;
798
2.49k
  UINT16 yPos = 0;
799
800
2.49k
  WINPR_ASSERT(fastpath);
801
2.49k
  WINPR_ASSERT(fastpath->rdp);
802
2.49k
  WINPR_ASSERT(fastpath->rdp->context);
803
2.49k
  WINPR_ASSERT(fastpath->rdp->input);
804
2.49k
  WINPR_ASSERT(s);
805
806
2.49k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
807
42
    return FALSE;
808
809
2.45k
  input = fastpath->rdp->input;
810
811
2.45k
  Stream_Read_UINT16(s, pointerFlags); /* pointerFlags (2 bytes) */
812
2.45k
  Stream_Read_UINT16(s, xPos);         /* xPos (2 bytes) */
813
2.45k
  Stream_Read_UINT16(s, yPos);         /* yPos (2 bytes) */
814
815
2.45k
  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.45k
  return IFCALLRESULT(TRUE, input->ExtendedMouseEvent, input, pointerFlags, xPos, yPos);
825
2.45k
}
826
827
static BOOL fastpath_recv_input_event_sync(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
828
3.29k
{
829
3.29k
  rdpInput* input = NULL;
830
831
3.29k
  WINPR_ASSERT(fastpath);
832
3.29k
  WINPR_ASSERT(fastpath->rdp);
833
3.29k
  WINPR_ASSERT(fastpath->rdp->input);
834
3.29k
  WINPR_ASSERT(s);
835
836
3.29k
  input = fastpath->rdp->input;
837
3.29k
  return IFCALLRESULT(TRUE, input->SynchronizeEvent, input, eventFlags);
838
3.29k
}
839
840
static BOOL fastpath_recv_input_event_unicode(rdpFastPath* fastpath, wStream* s, BYTE eventFlags)
841
946
{
842
946
  UINT16 unicodeCode = 0;
843
946
  UINT16 flags = 0;
844
845
946
  WINPR_ASSERT(fastpath);
846
946
  WINPR_ASSERT(s);
847
848
946
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
849
15
    return FALSE;
850
851
931
  Stream_Read_UINT16(s, unicodeCode); /* unicodeCode (2 bytes) */
852
931
  flags = 0;
853
854
931
  if ((eventFlags & FASTPATH_INPUT_KBDFLAGS_RELEASE))
855
448
    flags |= KBD_FLAGS_RELEASE;
856
857
931
  WINPR_ASSERT(fastpath->rdp);
858
931
  WINPR_ASSERT(fastpath->rdp);
859
931
  WINPR_ASSERT(fastpath->rdp->input);
860
931
  return IFCALLRESULT(FALSE, fastpath->rdp->input->UnicodeKeyboardEvent, fastpath->rdp->input,
861
931
                      flags, unicodeCode);
862
931
}
863
864
static BOOL fastpath_recv_input_event(rdpFastPath* fastpath, wStream* s)
865
43.0k
{
866
43.0k
  BYTE eventFlags = 0;
867
43.0k
  BYTE eventCode = 0;
868
869
43.0k
  WINPR_ASSERT(fastpath);
870
43.0k
  WINPR_ASSERT(s);
871
872
43.0k
  if (!fastpath_read_input_event_header(s, &eventFlags, &eventCode))
873
236
    return FALSE;
874
875
42.8k
  switch (eventCode)
876
42.8k
  {
877
18.7k
    case FASTPATH_INPUT_EVENT_SCANCODE:
878
18.7k
      if (!fastpath_recv_input_event_scancode(fastpath, s, eventFlags))
879
35
        return FALSE;
880
881
18.7k
      break;
882
883
18.7k
    case FASTPATH_INPUT_EVENT_MOUSE:
884
3.20k
      if (!fastpath_recv_input_event_mouse(fastpath, s, eventFlags))
885
64
        return FALSE;
886
887
3.14k
      break;
888
889
3.14k
    case FASTPATH_INPUT_EVENT_MOUSEX:
890
2.49k
      if (!fastpath_recv_input_event_mousex(fastpath, s, eventFlags))
891
42
        return FALSE;
892
893
2.45k
      break;
894
895
3.29k
    case FASTPATH_INPUT_EVENT_SYNC:
896
3.29k
      if (!fastpath_recv_input_event_sync(fastpath, s, eventFlags))
897
0
        return FALSE;
898
899
3.29k
      break;
900
901
3.29k
    case FASTPATH_INPUT_EVENT_UNICODE:
902
946
      if (!fastpath_recv_input_event_unicode(fastpath, s, eventFlags))
903
946
        return FALSE;
904
905
0
      break;
906
907
1.80k
    case TS_FP_RELPOINTER_EVENT:
908
1.80k
      if (!fastpath_recv_input_event_relmouse(fastpath, s, eventFlags))
909
19
        return FALSE;
910
911
1.78k
      break;
912
913
2.59k
    case TS_FP_QOETIMESTAMP_EVENT:
914
2.59k
      if (!fastpath_recv_input_event_qoe(fastpath, s, eventFlags))
915
18
        return FALSE;
916
2.57k
      break;
917
918
9.71k
    default:
919
9.71k
      WLog_ERR(TAG, "Unknown eventCode %" PRIu8 "", eventCode);
920
9.71k
      break;
921
42.8k
  }
922
923
41.6k
  return TRUE;
924
42.8k
}
925
926
state_run_t fastpath_recv_inputs(rdpFastPath* fastpath, wStream* s)
927
14.9k
{
928
14.9k
  WINPR_ASSERT(fastpath);
929
14.9k
  WINPR_ASSERT(s);
930
931
14.9k
  if (fastpath->numberEvents == 0)
932
14.9k
  {
933
    /**
934
     * If numberEvents is not provided in fpInputHeader, it will be provided
935
     * as one additional byte here.
936
     */
937
14.9k
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
938
0
      return STATE_RUN_FAILED;
939
940
14.9k
    Stream_Read_UINT8(s, fastpath->numberEvents); /* eventHeader (1 byte) */
941
14.9k
  }
942
943
56.6k
  for (BYTE i = 0; i < fastpath->numberEvents; i++)
944
43.0k
  {
945
43.0k
    if (!fastpath_recv_input_event(fastpath, s))
946
1.36k
      return STATE_RUN_FAILED;
947
43.0k
  }
948
949
13.6k
  return STATE_RUN_SUCCESS;
950
14.9k
}
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
522
{
1153
522
  return transport_send_stream_init(fastpath->rdp->transport, FASTPATH_MAX_PACKET_SIZE);
1154
522
}
1155
1156
wStream* fastpath_update_pdu_init_new(rdpFastPath* fastpath)
1157
8.30k
{
1158
8.30k
  wStream* s = NULL;
1159
8.30k
  s = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1160
8.30k
  return s;
1161
8.30k
}
1162
1163
BOOL fastpath_send_update_pdu(rdpFastPath* fastpath, BYTE updateCode, wStream* s,
1164
                              BOOL skipCompression)
1165
550
{
1166
550
  UINT16 maxLength = 0;
1167
550
  UINT32 totalLength = 0;
1168
550
  BOOL status = TRUE;
1169
550
  wStream* fs = NULL;
1170
550
  rdpSettings* settings = NULL;
1171
550
  rdpRdp* rdp = NULL;
1172
550
  UINT32 fpHeaderSize = 6;
1173
550
  UINT32 fpUpdatePduHeaderSize = 0;
1174
550
  UINT32 fpUpdateHeaderSize = 0;
1175
550
  UINT32 CompressionMaxSize = 0;
1176
550
  FASTPATH_UPDATE_PDU_HEADER fpUpdatePduHeader = { 0 };
1177
550
  FASTPATH_UPDATE_HEADER fpUpdateHeader = { 0 };
1178
1179
550
  if (!fastpath || !fastpath->rdp || !fastpath->fs || !s)
1180
0
    return FALSE;
1181
1182
550
  rdp = fastpath->rdp;
1183
550
  fs = fastpath->fs;
1184
550
  settings = rdp->settings;
1185
1186
550
  if (!settings)
1187
0
    return FALSE;
1188
1189
550
  maxLength = FASTPATH_MAX_PACKET_SIZE - 20;
1190
1191
550
  if (settings->CompressionEnabled && !skipCompression)
1192
550
  {
1193
550
    CompressionMaxSize = bulk_compression_max_size(rdp->bulk);
1194
550
    maxLength = (maxLength < CompressionMaxSize) ? maxLength : CompressionMaxSize;
1195
550
    maxLength -= 20;
1196
550
  }
1197
1198
550
  totalLength = Stream_GetPosition(s);
1199
550
  Stream_SetPosition(s, 0);
1200
1201
  /* check if fast path output is possible */
1202
550
  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
550
  if (totalLength > settings->MultifragMaxRequestSize)
1210
392
  {
1211
392
    WLog_ERR(TAG,
1212
392
             "fast path update size (%" PRIu32
1213
392
             ") exceeds the client's maximum request size (%" PRIu32 ")",
1214
392
             totalLength, settings->MultifragMaxRequestSize);
1215
392
    return FALSE;
1216
392
  }
1217
1218
158
  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
158
  for (int fragment = 0; (totalLength > 0) || (fragment == 0); fragment++)
1227
158
  {
1228
158
    const BYTE* pSrcData = NULL;
1229
158
    UINT32 SrcSize = 0;
1230
158
    UINT32 DstSize = 0;
1231
158
    const BYTE* pDstData = NULL;
1232
158
    UINT32 compressionFlags = 0;
1233
158
    BYTE pad = 0;
1234
158
    BYTE* pSignature = NULL;
1235
158
    fpUpdatePduHeader.action = 0;
1236
158
    fpUpdatePduHeader.secFlags = 0;
1237
158
    fpUpdateHeader.compression = 0;
1238
158
    fpUpdateHeader.compressionFlags = 0;
1239
158
    fpUpdateHeader.updateCode = updateCode;
1240
158
    fpUpdateHeader.size = (totalLength > maxLength) ? maxLength : totalLength;
1241
158
    pSrcData = Stream_Pointer(s);
1242
158
    SrcSize = DstSize = fpUpdateHeader.size;
1243
1244
158
    if (rdp->sec_flags & SEC_ENCRYPT)
1245
0
      fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_ENCRYPTED;
1246
1247
158
    if (rdp->sec_flags & SEC_SECURE_CHECKSUM)
1248
0
      fpUpdatePduHeader.secFlags |= FASTPATH_OUTPUT_SECURE_CHECKSUM;
1249
1250
158
    if (settings->CompressionEnabled && !skipCompression)
1251
158
    {
1252
158
      if (bulk_compress(rdp->bulk, pSrcData, SrcSize, &pDstData, &DstSize,
1253
158
                        &compressionFlags) >= 0)
1254
158
      {
1255
158
        if (compressionFlags)
1256
0
        {
1257
0
          fpUpdateHeader.compressionFlags = compressionFlags;
1258
0
          fpUpdateHeader.compression = FASTPATH_OUTPUT_COMPRESSION_USED;
1259
0
        }
1260
158
      }
1261
158
    }
1262
1263
158
    if (!fpUpdateHeader.compression)
1264
158
    {
1265
158
      pDstData = Stream_Pointer(s);
1266
158
      DstSize = fpUpdateHeader.size;
1267
158
    }
1268
1269
158
    fpUpdateHeader.size = DstSize;
1270
158
    totalLength -= SrcSize;
1271
1272
158
    if (totalLength == 0)
1273
158
      fpUpdateHeader.fragmentation =
1274
158
          (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
158
    fpUpdateHeaderSize = fastpath_get_update_header_size(&fpUpdateHeader);
1280
158
    fpUpdatePduHeaderSize = fastpath_get_update_pdu_header_size(&fpUpdatePduHeader, rdp);
1281
158
    fpHeaderSize = fpUpdateHeaderSize + fpUpdatePduHeaderSize;
1282
1283
158
    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
158
    fpUpdatePduHeader.length = fpUpdateHeader.size + fpHeaderSize + pad;
1302
158
    Stream_SetPosition(fs, 0);
1303
158
    if (!fastpath_write_update_pdu_header(fs, &fpUpdatePduHeader, rdp))
1304
0
      return FALSE;
1305
158
    if (!fastpath_write_update_header(fs, &fpUpdateHeader))
1306
0
      return FALSE;
1307
1308
158
    if (!Stream_CheckAndLogRequiredCapacity(TAG, (fs), (size_t)DstSize + pad))
1309
0
      return FALSE;
1310
158
    Stream_Write(fs, pDstData, DstSize);
1311
1312
158
    if (pad)
1313
0
      Stream_Zero(fs, pad);
1314
1315
158
    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
158
    Stream_SealLength(fs);
1354
1355
158
    if (transport_write(rdp->transport, fs) < 0)
1356
158
    {
1357
158
      status = FALSE;
1358
158
      break;
1359
158
    }
1360
1361
0
    Stream_Seek(s, SrcSize);
1362
0
  }
1363
1364
158
  rdp->sec_flags = 0;
1365
158
  return status;
1366
158
}
1367
1368
rdpFastPath* fastpath_new(rdpRdp* rdp)
1369
14.9k
{
1370
14.9k
  rdpFastPath* fastpath = NULL;
1371
1372
14.9k
  WINPR_ASSERT(rdp);
1373
1374
14.9k
  fastpath = (rdpFastPath*)calloc(1, sizeof(rdpFastPath));
1375
1376
14.9k
  if (!fastpath)
1377
0
    return NULL;
1378
1379
14.9k
  fastpath->rdp = rdp;
1380
14.9k
  fastpath->fragmentation = -1;
1381
14.9k
  fastpath->fs = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1382
14.9k
  fastpath->updateData = Stream_New(NULL, FASTPATH_MAX_PACKET_SIZE);
1383
1384
14.9k
  if (!fastpath->fs || !fastpath->updateData)
1385
0
    goto out_free;
1386
1387
14.9k
  return fastpath;
1388
0
out_free:
1389
0
  fastpath_free(fastpath);
1390
0
  return NULL;
1391
14.9k
}
1392
1393
void fastpath_free(rdpFastPath* fastpath)
1394
14.9k
{
1395
14.9k
  if (fastpath)
1396
14.9k
  {
1397
14.9k
    Stream_Free(fastpath->updateData, TRUE);
1398
14.9k
    Stream_Free(fastpath->fs, TRUE);
1399
14.9k
    free(fastpath);
1400
14.9k
  }
1401
14.9k
}
1402
1403
BYTE fastpath_get_encryption_flags(rdpFastPath* fastpath)
1404
17.8k
{
1405
17.8k
  WINPR_ASSERT(fastpath);
1406
17.8k
  return fastpath->encryptionFlags;
1407
17.8k
}
1408
1409
BOOL fastpath_decrypt(rdpFastPath* fastpath, wStream* s, UINT16* length)
1410
14.9k
{
1411
14.9k
  WINPR_ASSERT(fastpath);
1412
14.9k
  if (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_ENCRYPTED)
1413
2.85k
  {
1414
2.85k
    const UINT16 flags =
1415
2.85k
        (fastpath_get_encryption_flags(fastpath) & FASTPATH_OUTPUT_SECURE_CHECKSUM)
1416
2.85k
            ? SEC_SECURE_CHECKSUM
1417
2.85k
            : 0;
1418
1419
2.85k
    if (!rdp_decrypt(fastpath->rdp, s, length, flags))
1420
0
      return FALSE;
1421
2.85k
  }
1422
1423
14.9k
  return TRUE;
1424
14.9k
}