Coverage Report

Created: 2026-11-10 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/activation.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Activation Sequence
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
20
#include <freerdp/config.h>
21
22
#include "settings.h"
23
24
#include <winpr/assert.h>
25
#include <winpr/cast.h>
26
27
#include "activation.h"
28
#include "display.h"
29
30
31.6k
#define TAG FREERDP_TAG("core.activation")
31
32
WINPR_ATTR_NODISCARD
33
static BOOL rdp_recv_client_font_list_pdu(wStream* s);
34
35
WINPR_ATTR_NODISCARD
36
static BOOL rdp_recv_client_persistent_key_list_pdu(wStream* s);
37
38
WINPR_ATTR_NODISCARD
39
static BOOL rdp_send_server_font_map_pdu(rdpRdp* rdp);
40
41
WINPR_ATTR_NODISCARD
42
static BOOL rdp_write_synchronize_pdu(wStream* s, const rdpSettings* settings)
43
0
{
44
0
  const UINT32 PduSource = freerdp_settings_get_uint32(settings, FreeRDP_PduSource);
45
46
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 4))
47
0
    return FALSE;
48
0
  Stream_Write_UINT16(s, SYNCMSGTYPE_SYNC); /* messageType (2 bytes) */
49
0
  Stream_Write_UINT16(s,
50
0
                      WINPR_ASSERTING_INT_CAST(uint16_t, PduSource)); /* targetUser (2 bytes) */
51
0
  return TRUE;
52
0
}
53
54
WINPR_ATTR_NODISCARD
55
static BOOL rdp_recv_sync_pdu(rdpRdp* rdp, wStream* s, const char* what)
56
31.6k
{
57
31.6k
  UINT16 msgType = 0;
58
31.6k
  UINT16 targetUser = 0;
59
60
31.6k
  WINPR_UNUSED(rdp);
61
31.6k
  if (!Stream_CheckAndLogRequiredLengthEx(TAG, WLOG_WARN, s, 4, 1, "%s(%s:%" PRIuz ") %s",
62
31.6k
                                          __func__, __FILE__, (size_t)__LINE__, what))
63
25.0k
    return FALSE;
64
6.65k
  Stream_Read_UINT16(s, msgType);
65
6.65k
  if (msgType != SYNCMSGTYPE_SYNC)
66
6.47k
  {
67
6.47k
    WLog_WARN(TAG, "%s: Invalid messageType=0x%04" PRIx16 ", expected 0x%04" PRIx16, what,
68
6.47k
              msgType, WINPR_CXX_COMPAT_CAST(uint16_t, SYNCMSGTYPE_SYNC));
69
6.47k
    return FALSE;
70
6.47k
  }
71
181
  Stream_Read_UINT16(s, targetUser);
72
181
  WLog_VRB(TAG, "%s: targetUser=0x%04" PRIx16, what, targetUser);
73
181
  return TRUE;
74
6.65k
}
75
76
BOOL rdp_recv_server_synchronize_pdu(rdpRdp* rdp, wStream* s)
77
15.8k
{
78
15.8k
  if (!rdp_recv_sync_pdu(rdp, s, "[MS-RDPBCGR] 2.2.1.19 Server Synchronize PDU"))
79
15.7k
    return FALSE;
80
120
  return rdp_finalize_set_flag(rdp, FINALIZE_SC_SYNCHRONIZE_PDU);
81
15.8k
}
82
83
BOOL rdp_send_server_synchronize_pdu(rdpRdp* rdp)
84
0
{
85
0
  UINT16 sec_flags = 0;
86
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
87
0
  if (!s)
88
0
    return FALSE;
89
90
0
  WINPR_ASSERT(rdp);
91
0
  if (!rdp_write_synchronize_pdu(s, rdp->settings))
92
0
  {
93
0
    Stream_Free(s, TRUE);
94
0
    return FALSE;
95
0
  }
96
97
0
  WINPR_ASSERT(rdp->mcs);
98
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SYNCHRONIZE, rdp->mcs->userId, sec_flags);
99
0
}
100
101
BOOL rdp_recv_client_synchronize_pdu(rdpRdp* rdp, wStream* s)
102
15.8k
{
103
15.8k
  if (!rdp_recv_sync_pdu(rdp, s, "[MS-RDPBCGR] 2.2.1.14 Client Synchronize PDU"))
104
15.7k
    return FALSE;
105
61
  return rdp_finalize_set_flag(rdp, FINALIZE_CS_SYNCHRONIZE_PDU);
106
15.8k
}
107
108
BOOL rdp_send_client_synchronize_pdu(rdpRdp* rdp)
109
0
{
110
0
  UINT16 sec_flags = 0;
111
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
112
0
  if (!s)
113
0
    return FALSE;
114
115
0
  WINPR_ASSERT(rdp);
116
0
  if (!rdp_write_synchronize_pdu(s, rdp->settings))
117
0
  {
118
0
    Stream_Free(s, TRUE);
119
0
    return FALSE;
120
0
  }
121
122
0
  WINPR_ASSERT(rdp->mcs);
123
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SYNCHRONIZE, rdp->mcs->userId, sec_flags);
124
0
}
125
126
WINPR_ATTR_NODISCARD
127
static BOOL rdp_recv_control_pdu(wStream* s, UINT16* action, UINT16* grantId, UINT32* controlId)
128
30
{
129
30
  WINPR_ASSERT(s);
130
30
  WINPR_ASSERT(action);
131
30
  WINPR_ASSERT(grantId);
132
30
  WINPR_ASSERT(controlId);
133
134
30
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
135
2
    return FALSE;
136
137
28
  Stream_Read_UINT16(s, *action);    /* action (2 bytes) */
138
28
  Stream_Read_UINT16(s, *grantId);   /* grantId (2 bytes) */
139
28
  Stream_Read_UINT32(s, *controlId); /* controlId (4 bytes) */
140
28
  return TRUE;
141
30
}
142
143
WINPR_ATTR_NODISCARD
144
static BOOL rdp_write_client_control_pdu(wStream* s, UINT16 action, UINT16 grantId,
145
                                         UINT32 controlId)
146
0
{
147
0
  WINPR_ASSERT(s);
148
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
149
0
    return FALSE;
150
0
  Stream_Write_UINT16(s, action);    /* action (2 bytes) */
151
0
  Stream_Write_UINT16(s, grantId);   /* grantId (2 bytes) */
152
0
  Stream_Write_UINT32(s, controlId); /* controlId (4 bytes) */
153
0
  return TRUE;
154
0
}
155
156
BOOL rdp_recv_server_control_pdu(rdpRdp* rdp, wStream* s)
157
30
{
158
30
  UINT16 action = 0;
159
30
  UINT16 grantId = 0;
160
30
  UINT32 controlId = 0;
161
162
30
  WINPR_ASSERT(rdp);
163
30
  WINPR_ASSERT(s);
164
165
30
  if (!rdp_recv_control_pdu(s, &action, &grantId, &controlId))
166
2
    return FALSE;
167
168
28
  switch (action)
169
28
  {
170
4
    case CTRLACTION_COOPERATE:
171
4
      return rdp_finalize_set_flag(rdp, FINALIZE_SC_CONTROL_COOPERATE_PDU);
172
173
1
    case CTRLACTION_GRANTED_CONTROL:
174
1
      rdp->resendFocus = TRUE;
175
1
      return rdp_finalize_set_flag(rdp, FINALIZE_SC_CONTROL_GRANTED_PDU);
176
23
    default:
177
23
    {
178
23
      char buffer[128] = WINPR_C_ARRAY_INIT;
179
23
      WLog_WARN(TAG, "Unexpected control PDU %s",
180
23
                rdp_ctrlaction_string(action, buffer, sizeof(buffer)));
181
182
23
      return FALSE;
183
0
    }
184
28
  }
185
28
}
186
187
BOOL rdp_send_server_control_cooperate_pdu(rdpRdp* rdp)
188
0
{
189
0
  UINT16 sec_flags = 0;
190
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
191
0
  if (!s)
192
0
    return FALSE;
193
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
194
0
  {
195
0
    Stream_Free(s, TRUE);
196
0
    return FALSE;
197
0
  }
198
0
  Stream_Write_UINT16(s, CTRLACTION_COOPERATE); /* action (2 bytes) */
199
0
  Stream_Write_UINT16(s, 0);                    /* grantId (2 bytes) */
200
0
  Stream_Write_UINT32(s, 0);                    /* controlId (4 bytes) */
201
202
0
  WINPR_ASSERT(rdp->mcs);
203
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId, sec_flags);
204
0
}
205
206
BOOL rdp_send_server_control_granted_pdu(rdpRdp* rdp)
207
0
{
208
0
  UINT16 sec_flags = 0;
209
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
210
0
  if (!s)
211
0
    return FALSE;
212
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
213
0
  {
214
0
    Stream_Free(s, TRUE);
215
0
    return FALSE;
216
0
  }
217
218
0
  WINPR_ASSERT(rdp->mcs);
219
0
  Stream_Write_UINT16(s, CTRLACTION_GRANTED_CONTROL); /* action (2 bytes) */
220
0
  Stream_Write_UINT16(s, rdp->mcs->userId);           /* grantId (2 bytes) */
221
0
  Stream_Write_UINT32(s, 0x03EA);                     /* controlId (4 bytes) */
222
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId, sec_flags);
223
0
}
224
225
BOOL rdp_send_client_control_pdu(rdpRdp* rdp, UINT16 action)
226
0
{
227
0
  UINT16 GrantId = 0;
228
0
  UINT16 ControlId = 0;
229
230
0
  switch (action)
231
0
  {
232
0
    case CTRLACTION_COOPERATE:
233
0
    case CTRLACTION_REQUEST_CONTROL:
234
0
      break;
235
0
    default:
236
0
      WLog_WARN(TAG,
237
0
                "Invalid client control PDU::action 0x%04" PRIx16 ", not allowed by client",
238
0
                action);
239
0
      return FALSE;
240
0
  }
241
242
0
  UINT16 sec_flags = 0;
243
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
244
0
  if (!s)
245
0
    return FALSE;
246
0
  if (!rdp_write_client_control_pdu(s, action, GrantId, ControlId))
247
0
  {
248
0
    Stream_Free(s, TRUE);
249
0
    return FALSE;
250
0
  }
251
252
0
  WINPR_ASSERT(rdp->mcs);
253
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId, sec_flags);
254
0
}
255
256
WINPR_ATTR_NODISCARD
257
static BOOL rdp_write_client_persistent_key_list_pdu(wStream* s, UINT8 flags,
258
                                                     const RDP_BITMAP_PERSISTENT_INFO* info)
259
0
{
260
0
  WINPR_ASSERT(s);
261
0
  WINPR_ASSERT(info);
262
263
0
  if (!Stream_EnsureRemainingCapacity(s, 24))
264
0
    return FALSE;
265
266
0
  Stream_Write_UINT16(s, info->numEntriesCache0);              /* numEntriesCache0 (2 bytes) */
267
0
  Stream_Write_UINT16(s, info->numEntriesCache1);              /* numEntriesCache1 (2 bytes) */
268
0
  Stream_Write_UINT16(s, info->numEntriesCache2);              /* numEntriesCache2 (2 bytes) */
269
0
  Stream_Write_UINT16(s, info->numEntriesCache3);              /* numEntriesCache3 (2 bytes) */
270
0
  Stream_Write_UINT16(s, info->numEntriesCache4);              /* numEntriesCache4 (2 bytes) */
271
0
  Stream_Write_UINT16(s, info->totalEntriesCache0);            /* totalEntriesCache0 (2 bytes) */
272
0
  Stream_Write_UINT16(s, info->totalEntriesCache1);            /* totalEntriesCache1 (2 bytes) */
273
0
  Stream_Write_UINT16(s, info->totalEntriesCache2);            /* totalEntriesCache2 (2 bytes) */
274
0
  Stream_Write_UINT16(s, info->totalEntriesCache3);            /* totalEntriesCache3 (2 bytes) */
275
0
  Stream_Write_UINT16(s, info->totalEntriesCache4);            /* totalEntriesCache4 (2 bytes) */
276
0
  Stream_Write_UINT8(s, flags);                                /* bBitMask (1 byte) */
277
0
  Stream_Write_UINT8(s, 0);                                    /* pad1 (1 byte) */
278
0
  Stream_Write_UINT16(s, 0);                                   /* pad3 (2 bytes) */
279
                                                               /* entries */
280
281
0
  if (!Stream_CheckAndLogRequiredCapacityOfSize(TAG, s, info->keyCount, sizeof(UINT64)))
282
0
    return FALSE;
283
284
0
  for (UINT32 index = 0; index < info->keyCount; index++)
285
0
  {
286
0
    const UINT64 val = info->keyList[index];
287
0
    Stream_Write_UINT64(s, val);
288
0
  }
289
290
0
  return TRUE;
291
0
}
292
293
WINPR_ATTR_NODISCARD
294
static BOOL rdp_send_persistent_key_list_pdu(rdpRdp* rdp, UINT8 flags,
295
                                             const RDP_BITMAP_PERSISTENT_INFO* info)
296
0
{
297
0
  WINPR_ASSERT(rdp);
298
0
  WINPR_ASSERT(info);
299
300
0
  UINT16 sec_flags = 0;
301
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
302
303
0
  if (!s)
304
0
    return FALSE;
305
306
0
  if (!rdp_write_client_persistent_key_list_pdu(s, flags, info))
307
0
  {
308
0
    Stream_Free(s, TRUE);
309
0
    return FALSE;
310
0
  }
311
312
0
  WINPR_ASSERT(rdp->mcs);
313
314
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST, rdp->mcs->userId,
315
0
                           sec_flags);
316
0
}
317
318
WINPR_ATTR_NODISCARD
319
static UINT16 rdp_load_persistent_key_list(rdpRdp* rdp, UINT64** pKeyList)
320
0
{
321
0
  UINT16 keyCount = 0;
322
0
  UINT64* keyList = nullptr;
323
324
0
  WINPR_ASSERT(rdp);
325
0
  WINPR_ASSERT(pKeyList);
326
0
  rdpSettings* settings = rdp->settings;
327
328
0
  *pKeyList = nullptr;
329
330
0
  if (!freerdp_settings_get_bool(settings, FreeRDP_BitmapCachePersistEnabled))
331
0
    return 0;
332
333
0
  if (!settings->BitmapCachePersistFile)
334
0
    return 0;
335
336
0
  rdpPersistentCache* persistent = persistent_cache_new();
337
338
0
  if (!persistent)
339
0
    return 0;
340
341
0
  const int status =
342
0
      persistent_cache_open(persistent, settings->BitmapCachePersistFile, FALSE, 0);
343
344
0
  if (status < 1)
345
0
    goto error;
346
347
0
  {
348
0
    const int count = persistent_cache_get_count(persistent);
349
0
    if ((count < 0) || (count > UINT16_MAX))
350
0
      goto error;
351
352
0
    keyCount = (UINT16)count;
353
0
    keyList = (UINT64*)calloc(keyCount, sizeof(UINT64));
354
355
0
    if (!keyList)
356
0
      goto error;
357
358
0
    for (int index = 0; index < count; index++)
359
0
    {
360
0
      PERSISTENT_CACHE_ENTRY cacheEntry = WINPR_C_ARRAY_INIT;
361
362
0
      if (persistent_cache_read_entry(persistent, &cacheEntry) < 1)
363
0
        continue;
364
365
0
      keyList[index] = cacheEntry.key64;
366
0
    }
367
0
  }
368
369
0
  *pKeyList = keyList;
370
371
0
  persistent_cache_free(persistent);
372
0
  return keyCount;
373
0
error:
374
0
  persistent_cache_free(persistent);
375
0
  free(keyList);
376
0
  return 0;
377
0
}
378
379
WINPR_ATTR_NODISCARD
380
static RDP_BITMAP_PERSISTENT_INFO rdp_persist_update(UINT32 offset, UINT16 step,
381
                                                     const RDP_BITMAP_PERSISTENT_INFO* info)
382
0
{
383
0
  WINPR_ASSERT(info);
384
385
0
  const UINT16 maxSlots = step;
386
0
  RDP_BITMAP_PERSISTENT_INFO copy = *info;
387
0
  copy.keyList = &info->keyList[offset];
388
389
0
  if (offset < info->totalEntriesCache0)
390
0
  {
391
0
    copy.numEntriesCache0 =
392
0
        MIN(step, WINPR_ASSERTING_INT_CAST(UINT16, (info->totalEntriesCache0 - offset)));
393
0
    step -= copy.numEntriesCache0;
394
0
    offset += copy.numEntriesCache0;
395
0
  }
396
397
0
  if (offset < 1ull * info->totalEntriesCache0 + info->totalEntriesCache1)
398
0
  {
399
0
    copy.numEntriesCache1 =
400
0
        MIN(step, WINPR_ASSERTING_INT_CAST(UINT16, (info->totalEntriesCache1 - offset)));
401
0
    step -= copy.numEntriesCache1;
402
0
    offset += copy.numEntriesCache1;
403
0
  }
404
0
  if (offset <
405
0
      1ull * info->totalEntriesCache0 + info->totalEntriesCache1 + info->totalEntriesCache2)
406
0
  {
407
0
    copy.numEntriesCache2 =
408
0
        MIN(step, WINPR_ASSERTING_INT_CAST(UINT16, (info->totalEntriesCache2 - offset)));
409
0
    step -= copy.numEntriesCache2;
410
0
  }
411
0
  if (offset < 1ull * info->totalEntriesCache0 + info->totalEntriesCache1 +
412
0
                   info->totalEntriesCache2 + info->totalEntriesCache3)
413
0
  {
414
0
    copy.numEntriesCache3 =
415
0
        MIN(step, WINPR_ASSERTING_INT_CAST(UINT16, (info->totalEntriesCache3 - offset)));
416
0
    step -= copy.numEntriesCache3;
417
0
  }
418
0
  if (offset < 1ull * info->totalEntriesCache0 + info->totalEntriesCache1 +
419
0
                   info->totalEntriesCache2 + info->totalEntriesCache3 + info->totalEntriesCache4)
420
0
  {
421
0
    copy.numEntriesCache4 =
422
0
        MIN(step, WINPR_ASSERTING_INT_CAST(UINT16, (info->totalEntriesCache4 - offset)));
423
0
    step -= copy.numEntriesCache4;
424
0
  }
425
0
  copy.keyCount = maxSlots - step;
426
0
  return copy;
427
0
}
428
429
BOOL rdp_send_client_persistent_key_list_pdu(rdpRdp* rdp)
430
0
{
431
0
  WINPR_ASSERT(rdp);
432
433
0
  rdpSettings* settings = rdp->settings;
434
0
  const UINT32 cellInfoCount =
435
0
      freerdp_settings_get_uint32(settings, FreeRDP_BitmapCacheV2NumCells);
436
0
  if (cellInfoCount != 5)
437
0
  {
438
0
    WLog_ERR(TAG, "BitmapCacheV2NumCells %" PRIu32 ", but must be 5. Aborting.", cellInfoCount);
439
0
    return FALSE;
440
0
  }
441
442
0
  UINT64* keyList = nullptr;
443
0
  RDP_BITMAP_PERSISTENT_INFO info = WINPR_C_ARRAY_INIT;
444
0
  const UINT16 keyCount = rdp_load_persistent_key_list(rdp, &keyList);
445
446
0
  WLog_DBG(TAG, "Persistent Key List: TotalKeyCount: %" PRIu16, keyCount);
447
448
0
  info.totalEntriesCache0 =
449
0
      (UINT16)MIN(UINT16_MAX, settings->BitmapCacheV2CellInfo[0].numEntries);
450
0
  info.totalEntriesCache1 =
451
0
      (UINT16)MIN(UINT16_MAX, settings->BitmapCacheV2CellInfo[1].numEntries);
452
0
  info.totalEntriesCache2 =
453
0
      (UINT16)MIN(UINT16_MAX, settings->BitmapCacheV2CellInfo[2].numEntries);
454
0
  info.totalEntriesCache3 =
455
0
      (UINT16)MIN(UINT16_MAX, settings->BitmapCacheV2CellInfo[3].numEntries);
456
0
  info.totalEntriesCache4 =
457
0
      (UINT16)MIN(UINT16_MAX, settings->BitmapCacheV2CellInfo[4].numEntries);
458
459
0
  info.keyCount = keyCount;
460
0
  info.keyList = keyList;
461
462
0
  WLog_DBG(TAG, "persistentKeyList count: %" PRIu32, info.keyCount);
463
464
0
  WLog_DBG(TAG,
465
0
           "numEntriesCache: [0]: %" PRIu16 " [1]: %" PRIu16 " [2]: %" PRIu16 " [3]: %" PRIu16
466
0
           " [4]: %" PRIu16,
467
0
           info.numEntriesCache0, info.numEntriesCache1, info.numEntriesCache2,
468
0
           info.numEntriesCache3, info.numEntriesCache4);
469
470
0
  WLog_DBG(TAG,
471
0
           "totalEntriesCache: [0]: %" PRIu16 " [1]: %" PRIu16 " [2]: %" PRIu16 " [3]: %" PRIu16
472
0
           " [4]: %" PRIu16,
473
0
           info.totalEntriesCache0, info.totalEntriesCache1, info.totalEntriesCache2,
474
0
           info.totalEntriesCache3, info.totalEntriesCache4);
475
476
0
  BOOL res = TRUE;
477
0
  const UINT16 maxKeysPerPDU = 169;
478
0
  for (UINT32 offset = 0; offset < info.keyCount; offset += maxKeysPerPDU)
479
0
  {
480
0
    const UINT16 curKeysPerPDU = (offset + maxKeysPerPDU) > info.keyCount
481
0
                                     ? (UINT16)(info.keyCount - offset)
482
0
                                     : maxKeysPerPDU;
483
0
    RDP_BITMAP_PERSISTENT_INFO cur = rdp_persist_update(offset, curKeysPerPDU, &info);
484
485
0
    UINT8 flags = 0;
486
0
    if (offset == 0)
487
0
      flags |= PERSIST_FIRST_PDU;
488
0
    if (offset + curKeysPerPDU >= info.keyCount)
489
0
      flags |= PERSIST_LAST_PDU;
490
0
    res = rdp_send_persistent_key_list_pdu(rdp, flags, &cur);
491
0
    if (!res)
492
0
      break;
493
0
  }
494
0
  free(keyList);
495
0
  return res;
496
0
}
497
498
BOOL rdp_recv_client_font_list_pdu(wStream* s)
499
0
{
500
0
  WINPR_ASSERT(s);
501
  /* 2.2.1.18 Client Font List PDU */
502
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
503
0
    return FALSE;
504
505
0
  return Stream_SafeSeek(s, 8);
506
0
}
507
508
BOOL rdp_recv_client_persistent_key_list_pdu(wStream* s)
509
0
{
510
0
  BYTE flags = 0;
511
0
  size_t count = 0;
512
0
  size_t total = 0;
513
0
  UINT16 cache = 0;
514
515
0
  WINPR_ASSERT(s);
516
517
  /* 2.2.1.17.1 Persistent Key List PDU Data (TS_BITMAPCACHE_PERSISTENT_LIST_PDU) */
518
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 21))
519
0
  {
520
0
    WLog_ERR(TAG, "short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need 21 bytes, got %" PRIuz,
521
0
             Stream_GetRemainingLength(s));
522
0
    return FALSE;
523
0
  }
524
  /* Read numEntriesCacheX for variable length data in PDU */
525
0
  for (size_t x = 0; x < 5; x++)
526
0
  {
527
0
    Stream_Read_UINT16(s, cache);
528
0
    count += cache;
529
0
  }
530
531
  /* Skip totalEntriesCacheX */
532
0
  for (size_t x = 0; x < 5; x++)
533
0
  {
534
0
    UINT16 tmp = 0;
535
0
    Stream_Read_UINT16(s, tmp);
536
0
    total += tmp;
537
0
  }
538
539
0
  if (total > 262144)
540
0
  {
541
0
    WLog_ERR(TAG,
542
0
             "TS_BITMAPCACHE_PERSISTENT_LIST_PDU::totalEntriesCacheX exceeds 262144 entries");
543
0
    return FALSE;
544
0
  }
545
546
0
  Stream_Read_UINT8(s, flags);
547
0
  if ((flags & ~(PERSIST_LAST_PDU | PERSIST_FIRST_PDU)) != 0)
548
0
  {
549
0
    WLog_ERR(TAG,
550
0
             "TS_BITMAPCACHE_PERSISTENT_LIST_PDU::bBitMask has an invalid value of 0x%02" PRIx8,
551
0
             flags);
552
0
    return FALSE;
553
0
  }
554
555
  /* Skip padding */
556
0
  if (!Stream_SafeSeek(s, 3))
557
0
  {
558
0
    WLog_ERR(TAG, "short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need 3 bytes, got %" PRIuz,
559
0
             Stream_GetRemainingLength(s));
560
0
    return FALSE;
561
0
  }
562
  /* Skip actual entries sent by client */
563
0
  if (!Stream_SafeSeek(s, count * sizeof(UINT64)))
564
0
  {
565
0
    WLog_ERR(TAG,
566
0
             "short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need %" PRIuz " bytes, got %" PRIuz,
567
0
             count * sizeof(UINT64), Stream_GetRemainingLength(s));
568
0
    return FALSE;
569
0
  }
570
0
  return TRUE;
571
0
}
572
573
WINPR_ATTR_NODISCARD
574
static BOOL rdp_write_client_font_list_pdu(wStream* s, UINT16 flags)
575
0
{
576
0
  WINPR_ASSERT(s);
577
578
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
579
0
    return FALSE;
580
0
  Stream_Write_UINT16(s, 0);     /* numberFonts (2 bytes) */
581
0
  Stream_Write_UINT16(s, 0);     /* totalNumFonts (2 bytes) */
582
0
  Stream_Write_UINT16(s, flags); /* listFlags (2 bytes) */
583
0
  Stream_Write_UINT16(s, 50);    /* entrySize (2 bytes) */
584
0
  return TRUE;
585
0
}
586
587
BOOL rdp_send_client_font_list_pdu(rdpRdp* rdp, UINT16 flags)
588
0
{
589
0
  UINT16 sec_flags = 0;
590
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
591
0
  if (!s)
592
0
    return FALSE;
593
0
  if (!rdp_write_client_font_list_pdu(s, flags))
594
0
  {
595
0
    Stream_Free(s, TRUE);
596
0
    return FALSE;
597
0
  }
598
599
0
  WINPR_ASSERT(rdp->mcs);
600
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_FONT_LIST, rdp->mcs->userId, sec_flags);
601
0
}
602
603
BOOL rdp_recv_font_map_pdu(rdpRdp* rdp, wStream* s)
604
8.65k
{
605
8.65k
  UINT16 numberEntries = 0;
606
8.65k
  UINT16 totalNumEntries = 0;
607
8.65k
  UINT16 mapFlags = 0;
608
8.65k
  UINT16 entrySize = 0;
609
610
8.65k
  WINPR_ASSERT(rdp);
611
8.65k
  WINPR_ASSERT(rdp->settings);
612
8.65k
  WINPR_ASSERT(s);
613
8.65k
  WINPR_ASSERT(!freerdp_settings_get_bool(rdp->settings, FreeRDP_ServerMode));
614
615
8.65k
  if (!rdp_has_reached_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP))
616
1
    return FALSE;
617
618
  /* Do not fail here, see https://github.com/FreeRDP/FreeRDP/issues/925 */
619
8.65k
  if (Stream_CheckAndLogRequiredLength(TAG, s, 8))
620
810
  {
621
810
    Stream_Read_UINT16(s, numberEntries); /* numberEntries (2 bytes) */
622
810
    if (numberEntries != 0)
623
471
      WLog_WARN(
624
810
          TAG,
625
810
          "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::numberEntries != 0 "
626
810
          "[%" PRIu16 "]",
627
810
          numberEntries);
628
810
    Stream_Read_UINT16(s, totalNumEntries); /* totalNumEntries (2 bytes) */
629
810
    if (totalNumEntries != 0)
630
489
      WLog_WARN(
631
810
          TAG,
632
810
          "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::totalNumEntries != "
633
810
          "0 [%" PRIu16 "]",
634
810
          totalNumEntries);
635
810
    Stream_Read_UINT16(s, mapFlags); /* mapFlags (2 bytes) */
636
810
    if (mapFlags != (FONTLIST_FIRST | FONTLIST_LAST))
637
809
      WLog_WARN(
638
810
          TAG,
639
810
          "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::mapFlags != 0x0003 "
640
810
          "(FONTLIST_FIRST | FONTLIST_LAST) "
641
810
          "[0x%04" PRIx16 "]",
642
810
          mapFlags);
643
810
    Stream_Read_UINT16(s, entrySize); /* entrySize (2 bytes) */
644
810
    if (entrySize != 4)
645
803
      WLog_WARN(TAG,
646
810
                "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::entrySize != 4 "
647
810
                "[%" PRIu16 "]",
648
810
                entrySize);
649
810
  }
650
7.84k
  else
651
7.84k
    WLog_WARN(TAG,
652
8.65k
              "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU) paylaod size is "
653
8.65k
              "0 instead of 8");
654
655
8.65k
  return rdp_finalize_set_flag(rdp, FINALIZE_SC_FONT_MAP_PDU);
656
8.65k
}
657
658
BOOL rdp_send_server_font_map_pdu(rdpRdp* rdp)
659
0
{
660
0
  UINT16 sec_flags = 0;
661
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
662
0
  if (!s)
663
0
    return FALSE;
664
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
665
0
  {
666
0
    Stream_Free(s, TRUE);
667
0
    return FALSE;
668
0
  }
669
0
  Stream_Write_UINT16(s, 0);                              /* numberEntries (2 bytes) */
670
0
  Stream_Write_UINT16(s, 0);                              /* totalNumEntries (2 bytes) */
671
0
  Stream_Write_UINT16(s, FONTLIST_FIRST | FONTLIST_LAST); /* mapFlags (2 bytes) */
672
0
  Stream_Write_UINT16(s, 4);                              /* entrySize (2 bytes) */
673
674
0
  WINPR_ASSERT(rdp->mcs);
675
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_FONT_MAP, rdp->mcs->userId, sec_flags);
676
0
}
677
678
BOOL rdp_recv_deactivate_all(rdpRdp* rdp, wStream* s)
679
15.8k
{
680
15.8k
  UINT16 lengthSourceDescriptor = 0;
681
682
15.8k
  WINPR_ASSERT(rdp);
683
15.8k
  WINPR_ASSERT(s);
684
685
15.8k
  if (rdp_get_state(rdp) == CONNECTION_STATE_ACTIVE)
686
8.65k
  {
687
8.65k
    if (!rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE))
688
0
      return FALSE;
689
690
8.65k
    rdp->was_deactivated = TRUE;
691
8.65k
    rdp->deactivated_height = freerdp_settings_get_uint32(rdp->settings, FreeRDP_DesktopHeight);
692
8.65k
    rdp->deactivated_width = freerdp_settings_get_uint32(rdp->settings, FreeRDP_DesktopWidth);
693
8.65k
  }
694
695
  /*
696
   * Windows XP can send short DEACTIVATE_ALL PDU that doesn't contain
697
   * the following fields.
698
   */
699
700
15.8k
  WINPR_ASSERT(rdp->settings);
701
15.8k
  if (Stream_GetRemainingLength(s) > 0)
702
7.52k
  {
703
7.52k
    do
704
7.52k
    {
705
7.52k
      UINT32 ShareId = 0;
706
7.52k
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
707
3.71k
        break;
708
709
3.80k
      Stream_Read_UINT32(s, ShareId); /* shareId (4 bytes) */
710
3.80k
      if (!freerdp_settings_set_uint32(rdp->settings, FreeRDP_ShareId, ShareId))
711
0
        return FALSE;
712
713
3.80k
      if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
714
325
        break;
715
716
3.48k
      Stream_Read_UINT16(s, lengthSourceDescriptor); /* lengthSourceDescriptor (2 bytes) */
717
718
3.48k
      if (!Stream_CheckAndLogRequiredLength(TAG, s, lengthSourceDescriptor))
719
1.52k
        break;
720
721
1.96k
      Stream_Seek(s, lengthSourceDescriptor); /* sourceDescriptor (should be 0x00) */
722
1.96k
    } while (0);
723
7.52k
  }
724
725
15.8k
  return rdp_client_transition_to_state(rdp,
726
15.8k
                                        CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE);
727
15.8k
}
728
729
BOOL rdp_send_deactivate_all(rdpRdp* rdp)
730
0
{
731
0
  WINPR_ASSERT(rdp);
732
0
  WINPR_ASSERT(rdp->mcs);
733
734
0
  if (rdp->mcs->userId == 0)
735
0
  {
736
0
    WLog_Print(rdp->log, WLOG_WARN,
737
0
               "rdpMcs::userId == 0, skip sending PDU_TYPE_DEACTIVATE_ALL");
738
0
    return TRUE;
739
0
  }
740
741
0
  UINT16 sec_flags = 0;
742
0
  wStream* s = rdp_send_stream_pdu_init(rdp, &sec_flags);
743
0
  BOOL status = FALSE;
744
745
0
  if (!s)
746
0
    return FALSE;
747
748
0
  if (!Stream_CheckAndLogRequiredCapacityWLog(rdp->log, (s), 7))
749
0
    goto fail;
750
751
0
  WINPR_ASSERT(rdp->settings);
752
0
  {
753
0
    const UINT32 ShareId = freerdp_settings_get_uint32(rdp->settings, FreeRDP_ShareId);
754
0
    Stream_Write_UINT32(s, ShareId); /* shareId (4 bytes) */
755
0
  }
756
0
  Stream_Write_UINT16(s, 1); /* lengthSourceDescriptor (2 bytes) */
757
0
  Stream_Write_UINT8(s, 0);  /* sourceDescriptor (should be 0x00) */
758
759
0
  WINPR_ASSERT(rdp->mcs);
760
0
  status = rdp_send_pdu(rdp, s, PDU_TYPE_DEACTIVATE_ALL, rdp->mcs->userId, sec_flags);
761
0
fail:
762
0
  Stream_Release(s);
763
0
  return status;
764
0
}
765
766
BOOL rdp_server_accept_client_control_pdu(rdpRdp* rdp, wStream* s)
767
0
{
768
0
  UINT16 action = 0;
769
0
  UINT16 GrantId = 0;
770
0
  UINT32 ControlId = 0;
771
0
  const CONNECTION_STATE state = rdp_get_state(rdp);
772
773
0
  WINPR_ASSERT(rdp);
774
0
  WINPR_ASSERT(s);
775
776
0
  if (!rdp_recv_control_pdu(s, &action, &GrantId, &ControlId))
777
0
    return FALSE;
778
779
0
  switch (action)
780
0
  {
781
782
0
    case CTRLACTION_REQUEST_CONTROL:
783
0
      if (!rdp_finalize_is_flag_set(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU))
784
0
      {
785
0
        char abuffer[128] = WINPR_C_ARRAY_INIT;
786
0
        char buffer[1024] = WINPR_C_ARRAY_INIT;
787
0
        WLog_WARN(TAG,
788
0
                  "Received action=%s with GrantId=0x%04" PRIx16 ", ControlId=0x%08" PRIx32
789
0
                  " in unexpected state %s [missing %s]",
790
0
                  rdp_ctrlaction_string(action, abuffer, sizeof(abuffer)), GrantId,
791
0
                  ControlId, rdp_state_string(state),
792
0
                  rdp_finalize_flags_to_str(FINALIZE_CS_CONTROL_COOPERATE_PDU, buffer,
793
0
                                            sizeof(buffer)));
794
0
        return FALSE;
795
0
      }
796
0
      if ((GrantId != 0) || (ControlId != 0))
797
0
      {
798
0
        WLog_WARN(TAG,
799
0
                  "Received CTRLACTION_COOPERATE with GrantId=0x%04" PRIx16
800
0
                  " != 0x00, ControlId=0x%08" PRIx32 " != 0x00",
801
0
                  GrantId, ControlId);
802
0
        return FALSE;
803
0
      }
804
0
      return rdp_finalize_set_flag(rdp, FINALIZE_CS_CONTROL_REQUEST_PDU);
805
0
    case CTRLACTION_COOPERATE:
806
0
      if (!rdp_finalize_is_flag_set(rdp, FINALIZE_CS_SYNCHRONIZE_PDU))
807
0
      {
808
0
        char abuffer[128] = WINPR_C_ARRAY_INIT;
809
0
        char buffer[1024] = WINPR_C_ARRAY_INIT;
810
0
        WLog_WARN(
811
0
            TAG,
812
0
            "Received action=%s with GrantId=0x%04" PRIx16 ", ControlId=0x%08" PRIx32
813
0
            " in unexpected state %s [missing %s]",
814
0
            rdp_ctrlaction_string(action, abuffer, sizeof(abuffer)), GrantId, ControlId,
815
0
            rdp_state_string(state),
816
0
            rdp_finalize_flags_to_str(FINALIZE_CS_SYNCHRONIZE_PDU, buffer, sizeof(buffer)));
817
0
        return FALSE;
818
0
      }
819
0
      if ((GrantId != 0) || (ControlId != 0))
820
0
      {
821
0
        WLog_WARN(TAG,
822
0
                  "Received CTRLACTION_COOPERATE with GrantId=0x%04" PRIx16
823
0
                  " != 0x00, ControlId=0x%08" PRIx32 " != 0x00",
824
0
                  GrantId, ControlId);
825
0
        return FALSE;
826
0
      }
827
0
      return rdp_finalize_set_flag(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU);
828
0
    default:
829
0
    {
830
0
      char abuffer[128] = WINPR_C_ARRAY_INIT;
831
0
      WLog_WARN(TAG,
832
0
                "Received unexpected action=%s with GrantId=0x%04" PRIx16
833
0
                ", ControlId=0x%08" PRIx32,
834
0
                rdp_ctrlaction_string(action, abuffer, sizeof(abuffer)), GrantId, ControlId);
835
0
      return FALSE;
836
0
    }
837
0
  }
838
839
0
  return TRUE;
840
0
}
841
842
BOOL rdp_server_accept_client_font_list_pdu(rdpRdp* rdp, wStream* s)
843
0
{
844
0
  WINPR_ASSERT(rdp);
845
0
  WINPR_ASSERT(s);
846
847
0
  if (!rdp_recv_client_font_list_pdu(s))
848
0
    return FALSE;
849
0
  rdp_finalize_set_flag(rdp, FINALIZE_CS_FONT_LIST_PDU);
850
851
0
  if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP))
852
0
    return FALSE;
853
854
0
  if (!rdp_send_server_font_map_pdu(rdp))
855
0
    return FALSE;
856
857
0
  if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_ACTIVE))
858
0
    return FALSE;
859
860
0
  return TRUE;
861
0
}
862
863
BOOL rdp_server_accept_client_persistent_key_list_pdu(rdpRdp* rdp, wStream* s)
864
0
{
865
0
  WINPR_ASSERT(rdp);
866
0
  WINPR_ASSERT(s);
867
868
0
  if (!rdp_recv_client_persistent_key_list_pdu(s))
869
0
    return FALSE;
870
871
0
  rdp_finalize_set_flag(rdp, FINALIZE_CS_PERSISTENT_KEY_LIST_PDU);
872
  // TODO: Actually do something with this
873
0
  return TRUE;
874
0
}
875
876
const char* rdp_ctrlaction_string(UINT16 action, char* buffer, size_t size)
877
23
{
878
23
  const char* actstr = nullptr;
879
23
  switch (action)
880
23
  {
881
0
    case CTRLACTION_COOPERATE:
882
0
      actstr = "CTRLACTION_COOPERATE";
883
0
      break;
884
0
    case CTRLACTION_DETACH:
885
0
      actstr = "CTRLACTION_DETACH";
886
0
      break;
887
0
    case CTRLACTION_GRANTED_CONTROL:
888
0
      actstr = "CTRLACTION_GRANTED_CONTROL";
889
0
      break;
890
0
    case CTRLACTION_REQUEST_CONTROL:
891
0
      actstr = "CTRLACTION_REQUEST_CONTROL";
892
0
      break;
893
23
    default:
894
23
      actstr = "CTRLACTION_UNKNOWN";
895
23
      break;
896
23
  }
897
898
23
  (void)_snprintf(buffer, size, "%s [0x%04" PRIx16 "]", actstr, action);
899
23
  return buffer;
900
23
}