Coverage Report

Created: 2026-07-16 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/core/info.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * RDP Client Info
4
 *
5
 * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2015 Thincast Technologies GmbH
7
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <winpr/wtypes.h>
23
#include <freerdp/config.h>
24
25
#include "settings.h"
26
27
#include <winpr/crt.h>
28
#include <winpr/assert.h>
29
30
#include <freerdp/crypto/crypto.h>
31
#include <freerdp/log.h>
32
#include <freerdp/session.h>
33
#include <stdio.h>
34
35
#include "timezone.h"
36
37
#include "info.h"
38
39
28
#define TAG FREERDP_TAG("core.info")
40
41
224
#define logonInfoV2Size (2u + 4u + 4u + 4u + 4u)
42
159
#define logonInfoV2ReservedSize 558u
43
112
#define logonInfoV2TotalSize (logonInfoV2Size + logonInfoV2ReservedSize)
44
45
const char* freerdp_session_logon_type_str(uint32_t type)
46
673
{
47
673
  switch (type)
48
673
  {
49
373
    case INFO_TYPE_LOGON:
50
373
      return "Logon Info V1";
51
113
    case INFO_TYPE_LOGON_LONG:
52
113
      return "Logon Info V2";
53
8
    case INFO_TYPE_LOGON_PLAIN_NOTIFY:
54
8
      return "Logon Plain Notify";
55
179
    case INFO_TYPE_LOGON_EXTENDED_INF:
56
179
      return "Logon Extended Info";
57
0
    default:
58
0
      return "INFO_TYPE_UNKNOWN";
59
673
  }
60
673
}
61
62
/* This define limits the length of the strings in the label field. */
63
0
#define MAX_LABEL_LENGTH 40
64
struct info_flags_t
65
{
66
  UINT32 flag;
67
  const char* label;
68
};
69
70
static const struct info_flags_t info_flags[] = {
71
  { INFO_MOUSE, "INFO_MOUSE" },
72
  { INFO_DISABLECTRLALTDEL, "INFO_DISABLECTRLALTDEL" },
73
  { INFO_AUTOLOGON, "INFO_AUTOLOGON" },
74
  { INFO_UNICODE, "INFO_UNICODE" },
75
  { INFO_MAXIMIZESHELL, "INFO_MAXIMIZESHELL" },
76
  { INFO_LOGONNOTIFY, "INFO_LOGONNOTIFY" },
77
  { INFO_COMPRESSION, "INFO_COMPRESSION" },
78
  { INFO_ENABLEWINDOWSKEY, "INFO_ENABLEWINDOWSKEY" },
79
  { INFO_REMOTECONSOLEAUDIO, "INFO_REMOTECONSOLEAUDIO" },
80
  { INFO_FORCE_ENCRYPTED_CS_PDU, "INFO_FORCE_ENCRYPTED_CS_PDU" },
81
  { INFO_RAIL, "INFO_RAIL" },
82
  { INFO_LOGONERRORS, "INFO_LOGONERRORS" },
83
  { INFO_MOUSE_HAS_WHEEL, "INFO_MOUSE_HAS_WHEEL" },
84
  { INFO_PASSWORD_IS_SC_PIN, "INFO_PASSWORD_IS_SC_PIN" },
85
  { INFO_NOAUDIOPLAYBACK, "INFO_NOAUDIOPLAYBACK" },
86
  { INFO_USING_SAVED_CREDS, "INFO_USING_SAVED_CREDS" },
87
  { INFO_AUDIOCAPTURE, "INFO_AUDIOCAPTURE" },
88
  { INFO_VIDEO_DISABLE, "INFO_VIDEO_DISABLE" },
89
  { INFO_HIDEF_RAIL_SUPPORTED, "INFO_HIDEF_RAIL_SUPPORTED" },
90
};
91
92
static BOOL rdp_read_info_null_string(rdpSettings* settings, FreeRDP_Settings_Keys_String id,
93
                                      const char* what, UINT32 flags, wStream* s, size_t cbLen,
94
                                      size_t max)
95
1.69k
{
96
1.69k
  const BOOL unicode = (flags & INFO_UNICODE) != 0;
97
98
1.69k
  if (!freerdp_settings_set_string(settings, id, nullptr))
99
0
    return FALSE;
100
101
1.69k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(cbLen)))
102
117
    return FALSE;
103
104
1.57k
  if (cbLen > 0)
105
392
  {
106
392
    if ((cbLen > max) || (unicode && ((cbLen % 2) != 0)))
107
288
    {
108
288
      WLog_ERR(TAG, "protocol error: %s has invalid value: %" PRIuz "", what, cbLen);
109
288
      return FALSE;
110
288
    }
111
112
104
    if (unicode)
113
104
    {
114
104
      const WCHAR* domain = Stream_PointerAs(s, WCHAR);
115
104
      if (!freerdp_settings_set_string_from_utf16N(settings, id, domain,
116
104
                                                   cbLen / sizeof(WCHAR)))
117
11
      {
118
11
        WLog_ERR(TAG, "protocol error: no data to read for %s [expected %" PRIuz "]", what,
119
11
                 cbLen);
120
11
        return FALSE;
121
11
      }
122
104
    }
123
0
    else
124
0
    {
125
0
      const char* domain = Stream_ConstPointer(s);
126
0
      if (!freerdp_settings_set_string_len(settings, id, domain, cbLen))
127
0
        return FALSE;
128
0
    }
129
104
  }
130
1.27k
  Stream_Seek(s, cbLen);
131
132
1.27k
  return TRUE;
133
1.57k
}
134
135
static char* rdp_info_package_flags_description(UINT32 flags)
136
0
{
137
0
  char* result = nullptr;
138
0
  size_t maximum_size = 1 + MAX_LABEL_LENGTH * ARRAYSIZE(info_flags);
139
140
0
  result = calloc(maximum_size, sizeof(char));
141
142
0
  if (!result)
143
0
    return nullptr;
144
145
0
  for (size_t i = 0; i < ARRAYSIZE(info_flags); i++)
146
0
  {
147
0
    const struct info_flags_t* cur = &info_flags[i];
148
0
    if (cur->flag & flags)
149
0
    {
150
0
      winpr_str_append(cur->label, result, maximum_size, "|");
151
0
    }
152
0
  }
153
154
0
  return result;
155
0
}
156
157
static BOOL rdp_compute_client_auto_reconnect_cookie(rdpRdp* rdp)
158
0
{
159
0
  BYTE ClientRandom[CLIENT_RANDOM_LENGTH] = WINPR_C_ARRAY_INIT;
160
0
  BYTE AutoReconnectRandom[32] = WINPR_C_ARRAY_INIT;
161
0
  ARC_SC_PRIVATE_PACKET* serverCookie = nullptr;
162
0
  ARC_CS_PRIVATE_PACKET* clientCookie = nullptr;
163
164
0
  WINPR_ASSERT(rdp);
165
0
  rdpSettings* settings = rdp->settings;
166
0
  WINPR_ASSERT(settings);
167
168
0
  serverCookie = settings->ServerAutoReconnectCookie;
169
0
  clientCookie = settings->ClientAutoReconnectCookie;
170
0
  clientCookie->cbLen = 28;
171
0
  clientCookie->version = serverCookie->version;
172
0
  clientCookie->logonId = serverCookie->logonId;
173
0
  ZeroMemory(clientCookie->securityVerifier, sizeof(clientCookie->securityVerifier));
174
0
  CopyMemory(AutoReconnectRandom, serverCookie->arcRandomBits,
175
0
             sizeof(serverCookie->arcRandomBits));
176
177
0
  if (settings->SelectedProtocol == PROTOCOL_RDP)
178
0
    CopyMemory(ClientRandom, settings->ClientRandom, settings->ClientRandomLength);
179
180
  /* SecurityVerifier = HMAC_MD5(AutoReconnectRandom, ClientRandom) */
181
182
0
  if (!winpr_HMAC(WINPR_MD_MD5, AutoReconnectRandom, 16, ClientRandom, sizeof(ClientRandom),
183
0
                  clientCookie->securityVerifier, sizeof(clientCookie->securityVerifier)))
184
0
    return FALSE;
185
186
0
  return TRUE;
187
0
}
188
189
/**
190
 * Read Server Auto Reconnect Cookie (ARC_SC_PRIVATE_PACKET).
191
 * msdn{cc240540}
192
 */
193
194
static BOOL rdp_read_server_auto_reconnect_cookie(rdpRdp* rdp, wStream* s, logon_info_ex* info)
195
83
{
196
83
  BYTE* p = nullptr;
197
83
  ARC_SC_PRIVATE_PACKET* autoReconnectCookie = nullptr;
198
83
  rdpSettings* settings = rdp->settings;
199
83
  autoReconnectCookie = settings->ServerAutoReconnectCookie;
200
201
83
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
202
4
    return FALSE;
203
204
79
  Stream_Read_UINT32(s, autoReconnectCookie->cbLen); /* cbLen (4 bytes) */
205
206
79
  if (autoReconnectCookie->cbLen != 28)
207
51
  {
208
51
    WLog_ERR(TAG, "ServerAutoReconnectCookie.cbLen != 28");
209
51
    return FALSE;
210
51
  }
211
212
28
  Stream_Read_UINT32(s, autoReconnectCookie->version);    /* Version (4 bytes) */
213
28
  Stream_Read_UINT32(s, autoReconnectCookie->logonId);    /* LogonId (4 bytes) */
214
28
  Stream_Read(s, autoReconnectCookie->arcRandomBits, 16); /* ArcRandomBits (16 bytes) */
215
28
  p = autoReconnectCookie->arcRandomBits;
216
28
  WLog_DBG(TAG,
217
28
           "ServerAutoReconnectCookie: Version: %" PRIu32 " LogonId: %" PRIu32
218
28
           " SecurityVerifier: "
219
28
           "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
220
28
           "%02" PRIX8 ""
221
28
           "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
222
28
           "%02" PRIX8 "",
223
28
           autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
224
28
           p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
225
28
  info->LogonId = autoReconnectCookie->logonId;
226
28
  CopyMemory(info->ArcRandomBits, p, 16);
227
228
28
  if ((settings->PrintReconnectCookie))
229
0
  {
230
0
    char* base64 = nullptr;
231
0
    base64 = crypto_base64_encode((BYTE*)autoReconnectCookie, sizeof(ARC_SC_PRIVATE_PACKET));
232
0
    WLog_INFO(TAG, "Reconnect-cookie: %s", base64);
233
0
    free(base64);
234
0
  }
235
236
28
  return TRUE;
237
79
}
238
239
/**
240
 * Read Client Auto Reconnect Cookie (ARC_CS_PRIVATE_PACKET).
241
 * msdn{cc240541}
242
 */
243
244
static BOOL rdp_read_client_auto_reconnect_cookie(rdpRdp* rdp, wStream* s)
245
411
{
246
411
  ARC_CS_PRIVATE_PACKET* autoReconnectCookie = nullptr;
247
411
  rdpSettings* settings = rdp->settings;
248
411
  autoReconnectCookie = settings->ClientAutoReconnectCookie;
249
250
411
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
251
7
    return FALSE;
252
253
404
  Stream_Read_UINT32(s, autoReconnectCookie->cbLen);         /* cbLen (4 bytes) */
254
404
  Stream_Read_UINT32(s, autoReconnectCookie->version);       /* version (4 bytes) */
255
404
  Stream_Read_UINT32(s, autoReconnectCookie->logonId);       /* LogonId (4 bytes) */
256
404
  Stream_Read(s, autoReconnectCookie->securityVerifier, 16); /* SecurityVerifier */
257
404
  return TRUE;
258
411
}
259
260
/**
261
 * Write Client Auto Reconnect Cookie (ARC_CS_PRIVATE_PACKET).
262
 * msdn{cc240541}
263
 */
264
265
static BOOL rdp_write_client_auto_reconnect_cookie(rdpRdp* rdp, wStream* s)
266
0
{
267
0
  BYTE* p = nullptr;
268
0
  ARC_CS_PRIVATE_PACKET* autoReconnectCookie = nullptr;
269
0
  rdpSettings* settings = nullptr;
270
271
0
  WINPR_ASSERT(rdp);
272
273
0
  settings = rdp->settings;
274
0
  WINPR_ASSERT(settings);
275
276
0
  autoReconnectCookie = settings->ClientAutoReconnectCookie;
277
0
  WINPR_ASSERT(autoReconnectCookie);
278
279
0
  p = autoReconnectCookie->securityVerifier;
280
0
  WINPR_ASSERT(p);
281
282
0
  WLog_DBG(TAG,
283
0
           "ClientAutoReconnectCookie: Version: %" PRIu32 " LogonId: %" PRIu32 " ArcRandomBits: "
284
0
           "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
285
0
           "%02" PRIX8 ""
286
0
           "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
287
0
           "%02" PRIX8 "",
288
0
           autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
289
0
           p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
290
0
  if (!Stream_EnsureRemainingCapacity(s, 12ull + 16ull))
291
0
    return FALSE;
292
0
  Stream_Write_UINT32(s, autoReconnectCookie->cbLen);         /* cbLen (4 bytes) */
293
0
  Stream_Write_UINT32(s, autoReconnectCookie->version);       /* version (4 bytes) */
294
0
  Stream_Write_UINT32(s, autoReconnectCookie->logonId);       /* LogonId (4 bytes) */
295
0
  Stream_Write(s, autoReconnectCookie->securityVerifier, 16); /* SecurityVerifier (16 bytes) */
296
0
  return TRUE;
297
0
}
298
299
/*
300
 * Get the cbClientAddress size limit
301
 * see [MS-RDPBCGR] 2.2.1.11.1.1.1 Extended Info Packet (TS_EXTENDED_INFO_PACKET)
302
 */
303
304
static size_t rdp_get_client_address_max_size(const rdpRdp* rdp)
305
611
{
306
611
  UINT32 version = 0;
307
611
  rdpSettings* settings = nullptr;
308
309
611
  WINPR_ASSERT(rdp);
310
311
611
  settings = rdp->settings;
312
611
  WINPR_ASSERT(settings);
313
314
611
  version = freerdp_settings_get_uint32(settings, FreeRDP_RdpVersion);
315
611
  if (version < RDP_VERSION_10_0)
316
0
    return 64;
317
611
  return 80;
318
611
}
319
320
/**
321
 * Read Extended Info Packet (TS_EXTENDED_INFO_PACKET).
322
 * msdn{cc240476}
323
 */
324
325
static BOOL rdp_read_extended_info_packet(rdpRdp* rdp, wStream* s)
326
616
{
327
616
  UINT16 clientAddressFamily = 0;
328
616
  UINT16 cbClientAddress = 0;
329
616
  UINT16 cbClientDir = 0;
330
616
  UINT16 cbAutoReconnectLen = 0;
331
332
616
  WINPR_ASSERT(rdp);
333
334
616
  rdpSettings* settings = rdp->settings;
335
616
  WINPR_ASSERT(settings);
336
337
616
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
338
5
    return FALSE;
339
340
611
  Stream_Read_UINT16(s, clientAddressFamily); /* clientAddressFamily (2 bytes) */
341
611
  Stream_Read_UINT16(s, cbClientAddress);     /* cbClientAddress (2 bytes) */
342
343
611
  settings->IPv6Enabled = ((clientAddressFamily == ADDRESS_FAMILY_INET6));
344
345
611
  if (!rdp_read_info_null_string(settings, FreeRDP_ClientAddress, "cbClientAddress", INFO_UNICODE,
346
611
                                 s, cbClientAddress, rdp_get_client_address_max_size(rdp)))
347
28
    return FALSE;
348
349
583
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
350
10
    return FALSE;
351
352
573
  Stream_Read_UINT16(s, cbClientDir); /* cbClientDir (2 bytes) */
353
354
  /* cbClientDir is the size in bytes of the character data in the clientDir field.
355
   * This size includes the length of the mandatory null terminator.
356
   * The maximum allowed value is 512 bytes.
357
   * Note: Although according to [MS-RDPBCGR 2.2.1.11.1.1.1] the null terminator
358
   * is mandatory the Microsoft Android client (starting with version 8.1.31.44)
359
   * sets cbClientDir to 0.
360
   */
361
362
573
  if (!rdp_read_info_null_string(settings, FreeRDP_ClientDir, "cbClientDir", INFO_UNICODE, s,
363
573
                                 cbClientDir, 512))
364
8
    return FALSE;
365
366
  /**
367
   * down below all fields are optional but if one field is not present,
368
   * then all of the subsequent fields also MUST NOT be present.
369
   */
370
371
  /* optional: clientTimeZone (172 bytes) */
372
565
  if (Stream_GetRemainingLength(s) == 0)
373
4
    goto end;
374
375
561
  if (!rdp_read_client_time_zone(s, settings))
376
11
    return FALSE;
377
378
  /* optional: clientSessionId (4 bytes), should be set to 0 */
379
550
  if (Stream_GetRemainingLength(s) == 0)
380
3
    goto end;
381
547
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
382
3
    return FALSE;
383
384
544
  Stream_Read_UINT32(s, settings->ClientSessionId);
385
386
  /* optional: performanceFlags (4 bytes) */
387
544
  if (Stream_GetRemainingLength(s) == 0)
388
3
    goto end;
389
390
541
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
391
5
    return FALSE;
392
393
536
  Stream_Read_UINT32(s, settings->PerformanceFlags);
394
536
  freerdp_performance_flags_split(settings);
395
396
  /* optional: cbAutoReconnectLen (2 bytes) */
397
536
  if (Stream_GetRemainingLength(s) == 0)
398
2
    goto end;
399
400
534
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
401
2
    return FALSE;
402
403
532
  Stream_Read_UINT16(s, cbAutoReconnectLen);
404
405
  /* optional: autoReconnectCookie (28 bytes) */
406
  /* must be present if cbAutoReconnectLen is > 0 */
407
532
  if (cbAutoReconnectLen > 0)
408
411
  {
409
411
    if (!rdp_read_client_auto_reconnect_cookie(rdp, s))
410
7
      return FALSE;
411
411
  }
412
413
  /* skip reserved1 and reserved2 fields */
414
525
  if (Stream_GetRemainingLength(s) == 0)
415
5
    goto end;
416
417
520
  if (!Stream_SafeSeek(s, 2))
418
2
    return FALSE;
419
420
518
  if (Stream_GetRemainingLength(s) == 0)
421
2
    goto end;
422
423
516
  if (!Stream_SafeSeek(s, 2))
424
2
    return FALSE;
425
426
514
  if (Stream_GetRemainingLength(s) == 0)
427
2
    goto end;
428
429
512
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
430
2
    return FALSE;
431
432
510
  if (freerdp_settings_get_bool(settings, FreeRDP_SupportDynamicTimeZone))
433
510
  {
434
510
    UINT16 cbDynamicDSTTimeZoneKeyName = 0;
435
436
510
    Stream_Read_UINT16(s, cbDynamicDSTTimeZoneKeyName);
437
438
510
    if (!rdp_read_info_null_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName,
439
510
                                   "cbDynamicDSTTimeZoneKeyName", INFO_UNICODE, s,
440
510
                                   cbDynamicDSTTimeZoneKeyName, 254))
441
380
      return FALSE;
442
443
130
    if (Stream_GetRemainingLength(s) == 0)
444
4
      goto end;
445
446
126
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
447
5
      return FALSE;
448
121
    UINT16 DynamicDaylightTimeDisabled = 0;
449
121
    Stream_Read_UINT16(s, DynamicDaylightTimeDisabled);
450
121
    if (DynamicDaylightTimeDisabled > 1)
451
66
    {
452
66
      WLog_WARN(TAG,
453
66
                "[MS-RDPBCGR] 2.2.1.11.1.1.1 Extended Info Packet "
454
66
                "(TS_EXTENDED_INFO_PACKET)::dynamicDaylightTimeDisabled value %d"
455
66
                " not allowed in [0,1]",
456
66
                settings->DynamicDaylightTimeDisabled);
457
66
      return FALSE;
458
66
    }
459
55
    if (!freerdp_settings_set_bool(settings, FreeRDP_DynamicDaylightTimeDisabled,
460
55
                                   DynamicDaylightTimeDisabled != 0))
461
0
      return FALSE;
462
55
    DEBUG_TIMEZONE("DynamicTimeZone=%s [%s]",
463
55
                   freerdp_settings_get_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName),
464
55
                   freerdp_settings_get_bool(settings, FreeRDP_DynamicDaylightTimeDisabled)
465
55
                       ? "no-DST"
466
55
                       : "DST");
467
55
  }
468
469
80
end:
470
80
  return TRUE;
471
510
}
472
473
/**
474
 * Write Extended Info Packet (TS_EXTENDED_INFO_PACKET).
475
 * msdn{cc240476}
476
 */
477
478
static BOOL rdp_write_extended_info_packet(rdpRdp* rdp, wStream* s)
479
0
{
480
0
  BOOL ret = FALSE;
481
0
  size_t cbClientAddress = 0;
482
0
  const size_t cbClientAddressMax = rdp_get_client_address_max_size(rdp);
483
0
  WCHAR* clientDir = nullptr;
484
0
  size_t cbClientDir = 0;
485
0
  const size_t cbClientDirMax = 512;
486
0
  UINT16 cbAutoReconnectCookie = 0;
487
488
0
  WINPR_ASSERT(rdp);
489
490
0
  rdpSettings* settings = rdp->settings;
491
0
  WINPR_ASSERT(settings);
492
493
0
  UINT16 clientAddressFamily = ADDRESS_FAMILY_INET;
494
0
  if (settings->ConnectChildSession)
495
0
    clientAddressFamily = 0x0000;
496
0
  else if (settings->IPv6Enabled)
497
0
    clientAddressFamily = ADDRESS_FAMILY_INET6;
498
499
0
  WCHAR* clientAddress = ConvertUtf8ToWCharAlloc(settings->ClientAddress, &cbClientAddress);
500
501
0
  if (cbClientAddress > (UINT16_MAX / sizeof(WCHAR)))
502
0
  {
503
0
    WLog_ERR(TAG, "cbClientAddress > UINT16_MAX");
504
0
    goto fail;
505
0
  }
506
507
0
  if (cbClientAddress > 0)
508
0
  {
509
0
    cbClientAddress = (cbClientAddress + 1) * sizeof(WCHAR);
510
0
    if (cbClientAddress > cbClientAddressMax)
511
0
    {
512
0
      WLog_WARN(TAG,
513
0
                "the client address %s [%" PRIuz "] exceeds the limit of %" PRIuz
514
0
                ", truncating.",
515
0
                settings->ClientAddress, cbClientAddress, cbClientAddressMax);
516
517
0
      clientAddress[(cbClientAddressMax / sizeof(WCHAR)) - 1] = '\0';
518
0
      cbClientAddress = cbClientAddressMax;
519
0
    }
520
0
  }
521
522
0
  clientDir = ConvertUtf8ToWCharAlloc(settings->ClientDir, &cbClientDir);
523
0
  if (cbClientDir > (UINT16_MAX / sizeof(WCHAR)))
524
0
  {
525
0
    WLog_ERR(TAG, "cbClientDir > UINT16_MAX");
526
0
    goto fail;
527
0
  }
528
529
0
  if (cbClientDir > 0)
530
0
  {
531
0
    cbClientDir = (cbClientDir + 1) * sizeof(WCHAR);
532
0
    if (cbClientDir > cbClientDirMax)
533
0
    {
534
0
      WLog_WARN(TAG,
535
0
                "the client dir %s [%" PRIuz "] exceeds the limit of %" PRIuz ", truncating.",
536
0
                settings->ClientDir, cbClientDir, cbClientDirMax);
537
538
0
      clientDir[(cbClientDirMax / sizeof(WCHAR)) - 1] = '\0';
539
0
      cbClientDir = cbClientDirMax;
540
0
    }
541
0
  }
542
543
0
  if (settings->ServerAutoReconnectCookie->cbLen > UINT16_MAX)
544
0
  {
545
0
    WLog_ERR(TAG, "ServerAutoreconnectCookie::cbLen > UINT16_MAX");
546
0
    goto fail;
547
0
  }
548
549
0
  cbAutoReconnectCookie = (UINT16)settings->ServerAutoReconnectCookie->cbLen;
550
551
0
  if (!Stream_EnsureRemainingCapacity(s, 4ull + cbClientAddress + 2ull + cbClientDir))
552
0
    goto fail;
553
554
0
  Stream_Write_UINT16(s, clientAddressFamily);     /* clientAddressFamily (2 bytes) */
555
0
  Stream_Write_UINT16(s, (UINT16)cbClientAddress); /* cbClientAddress (2 bytes) */
556
557
0
  Stream_Write(s, clientAddress, cbClientAddress); /* clientAddress */
558
559
0
  Stream_Write_UINT16(s, (UINT16)cbClientDir); /* cbClientDir (2 bytes) */
560
561
0
  Stream_Write(s, clientDir, cbClientDir); /* clientDir */
562
563
0
  if (!rdp_write_client_time_zone(s, settings)) /* clientTimeZone (172 bytes) */
564
0
    goto fail;
565
566
0
  if (!Stream_EnsureRemainingCapacity(s, 10ull))
567
0
    goto fail;
568
569
  /* clientSessionId (4 bytes), should be set to 0 */
570
0
  Stream_Write_UINT32(s, settings->ClientSessionId);
571
0
  freerdp_performance_flags_make(settings);
572
0
  Stream_Write_UINT32(s, settings->PerformanceFlags); /* performanceFlags (4 bytes) */
573
0
  Stream_Write_UINT16(s, cbAutoReconnectCookie);      /* cbAutoReconnectCookie (2 bytes) */
574
575
0
  if (cbAutoReconnectCookie > 0)
576
0
  {
577
0
    if (!rdp_compute_client_auto_reconnect_cookie(rdp))
578
0
      goto fail;
579
0
    if (!rdp_write_client_auto_reconnect_cookie(rdp, s)) /* autoReconnectCookie */
580
0
      goto fail;
581
0
  }
582
583
0
  if (freerdp_settings_get_bool(settings, FreeRDP_SupportDynamicTimeZone))
584
0
  {
585
0
    if (!Stream_EnsureRemainingCapacity(s, 8 + 254 * sizeof(WCHAR)))
586
0
      goto fail;
587
588
0
    Stream_Write_UINT16(s, 0); /* reserved1 (2 bytes) */
589
0
    Stream_Write_UINT16(s, 0); /* reserved2 (2 bytes) */
590
591
0
    size_t rstrlen = 0;
592
0
    size_t rlen = 0;
593
0
    const char* tz = freerdp_settings_get_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName);
594
0
    if (tz)
595
0
    {
596
0
      rstrlen = strnlen(tz, 254);
597
0
      const SSIZE_T wlen = ConvertUtf8NToWChar(tz, rstrlen, nullptr, 0);
598
0
      if (wlen < 0)
599
0
        goto fail;
600
0
      rlen = WINPR_ASSERTING_INT_CAST(size_t, wlen);
601
0
    }
602
0
    Stream_Write_UINT16(s, (UINT16)rlen * sizeof(WCHAR));
603
0
    if (Stream_Write_UTF16_String_From_UTF8(s, rlen, tz, rstrlen, FALSE) < 0)
604
0
      goto fail;
605
0
    Stream_Write_UINT16(s, settings->DynamicDaylightTimeDisabled ? 0x01 : 0x00);
606
0
  }
607
608
0
  ret = TRUE;
609
0
fail:
610
0
  free(clientAddress);
611
0
  free(clientDir);
612
0
  return ret;
613
0
}
614
615
static BOOL rdp_read_info_string(rdpSettings* settings, FreeRDP_Settings_Keys_String id,
616
                                 UINT32 flags, wStream* s, size_t cbLenNonNull, size_t max)
617
3.51k
{
618
3.51k
  union
619
3.51k
  {
620
3.51k
    char c;
621
3.51k
    WCHAR w;
622
3.51k
    BYTE b[2];
623
3.51k
  } terminator;
624
625
3.51k
  const BOOL unicode = (flags & INFO_UNICODE) != 0;
626
3.51k
  const size_t nullSize = unicode ? sizeof(WCHAR) : sizeof(CHAR);
627
628
3.51k
  if (!freerdp_settings_set_string(settings, id, nullptr))
629
0
    return FALSE;
630
631
3.51k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(cbLenNonNull + nullSize)))
632
38
    return FALSE;
633
634
3.47k
  if (cbLenNonNull > 0)
635
227
  {
636
    /* cbDomain is the size in bytes of the character data in the Domain field.
637
     * This size excludes (!) the length of the mandatory null terminator.
638
     * Maximum value including the mandatory null terminator: 512
639
     */
640
227
    if ((cbLenNonNull % 2) || (cbLenNonNull > (max - nullSize)))
641
27
    {
642
27
      WLog_ERR(TAG, "protocol error: invalid value: %" PRIuz "", cbLenNonNull);
643
27
      return FALSE;
644
27
    }
645
646
200
    if (unicode)
647
170
    {
648
170
      const WCHAR* domain = Stream_PointerAs(s, WCHAR);
649
170
      if (!freerdp_settings_set_string_from_utf16N(settings, id, domain,
650
170
                                                   cbLenNonNull / sizeof(WCHAR)))
651
18
        return FALSE;
652
170
    }
653
30
    else
654
30
    {
655
30
      const char* domain = Stream_PointerAs(s, char);
656
30
      if (!freerdp_settings_set_string_len(settings, id, domain, cbLenNonNull))
657
0
        return FALSE;
658
30
    }
659
200
  }
660
661
3.43k
  Stream_Seek(s, cbLenNonNull);
662
663
3.43k
  terminator.w = L'\0';
664
3.43k
  Stream_Read(s, terminator.b, nullSize);
665
666
3.43k
  if (terminator.w != L'\0')
667
129
  {
668
129
    WLog_ERR(TAG, "protocol error: Domain must be null terminated");
669
129
    if (!freerdp_settings_set_string(settings, id, nullptr))
670
0
      WLog_ERR(TAG, "freerdp_settings_set_string(settings, id=%d, nullptr) failed", id);
671
672
129
    return FALSE;
673
129
  }
674
675
3.30k
  return TRUE;
676
3.43k
}
677
678
/**
679
 * Read Info Packet (TS_INFO_PACKET).
680
 * msdn{cc240475}
681
 */
682
683
static BOOL rdp_read_info_packet(rdpRdp* rdp, wStream* s, UINT16 tpktlength)
684
844
{
685
844
  BOOL smallsize = FALSE;
686
844
  UINT32 flags = 0;
687
844
  UINT16 cbDomain = 0;
688
844
  UINT16 cbUserName = 0;
689
844
  UINT16 cbPassword = 0;
690
844
  UINT16 cbAlternateShell = 0;
691
844
  UINT16 cbWorkingDir = 0;
692
844
  UINT32 CompressionLevel = 0;
693
844
  rdpSettings* settings = rdp->settings;
694
695
844
  if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 18))
696
16
    return FALSE;
697
698
828
  Stream_Read_UINT32(s, settings->KeyboardCodePage); /* CodePage (4 bytes ) */
699
828
  Stream_Read_UINT32(s, flags);                      /* flags (4 bytes) */
700
828
  settings->AudioCapture = ((flags & INFO_AUDIOCAPTURE) != 0);
701
828
  settings->AudioPlayback = (!(flags & INFO_NOAUDIOPLAYBACK));
702
828
  settings->AutoLogonEnabled = ((flags & INFO_AUTOLOGON) != 0);
703
828
  settings->RemoteApplicationMode = ((flags & INFO_RAIL) != 0);
704
828
  settings->HiDefRemoteApp = ((flags & INFO_HIDEF_RAIL_SUPPORTED) != 0);
705
828
  settings->RemoteConsoleAudio = ((flags & INFO_REMOTECONSOLEAUDIO) != 0);
706
828
  settings->CompressionEnabled = ((flags & INFO_COMPRESSION) != 0);
707
828
  settings->LogonNotify = ((flags & INFO_LOGONNOTIFY) != 0);
708
828
  settings->MouseHasWheel = ((flags & INFO_MOUSE_HAS_WHEEL) != 0);
709
828
  settings->DisableCtrlAltDel = ((flags & INFO_DISABLECTRLALTDEL) != 0);
710
828
  settings->ForceEncryptedCsPdu = ((flags & INFO_FORCE_ENCRYPTED_CS_PDU) != 0);
711
828
  settings->PasswordIsSmartcardPin = ((flags & INFO_PASSWORD_IS_SC_PIN) != 0);
712
713
828
  if (flags & INFO_COMPRESSION)
714
142
  {
715
142
    CompressionLevel = ((flags & 0x00001E00) >> 9);
716
142
    settings->CompressionLevel = CompressionLevel;
717
142
  }
718
686
  else
719
686
  {
720
686
    settings->CompressionLevel = 0;
721
686
  }
722
723
  /* RDP 4 and 5 have smaller credential limits */
724
828
  if (settings->RdpVersion < RDP_VERSION_5_PLUS)
725
0
    smallsize = TRUE;
726
727
828
  Stream_Read_UINT16(s, cbDomain);         /* cbDomain (2 bytes) */
728
828
  Stream_Read_UINT16(s, cbUserName);       /* cbUserName (2 bytes) */
729
828
  Stream_Read_UINT16(s, cbPassword);       /* cbPassword (2 bytes) */
730
828
  Stream_Read_UINT16(s, cbAlternateShell); /* cbAlternateShell (2 bytes) */
731
828
  Stream_Read_UINT16(s, cbWorkingDir);     /* cbWorkingDir (2 bytes) */
732
733
828
  if (!rdp_read_info_string(settings, FreeRDP_Domain, flags, s, cbDomain, smallsize ? 52 : 512))
734
121
    return FALSE;
735
736
707
  if (!rdp_read_info_string(settings, FreeRDP_Username, flags, s, cbUserName,
737
707
                            smallsize ? 44 : 512))
738
25
    return FALSE;
739
740
682
  if (!rdp_read_info_string(settings, FreeRDP_Password, flags, s, cbPassword,
741
682
                            smallsize ? 32 : 512))
742
22
    return FALSE;
743
744
660
  if (!rdp_read_info_string(settings, FreeRDP_AlternateShell, flags, s, cbAlternateShell, 512))
745
21
    return FALSE;
746
747
639
  if (!rdp_read_info_string(settings, FreeRDP_ShellWorkingDirectory, flags, s, cbWorkingDir, 512))
748
23
    return FALSE;
749
750
616
  if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
751
616
  {
752
616
    if (!rdp_read_extended_info_packet(rdp, s)) /* extraInfo */
753
536
      return FALSE;
754
616
  }
755
756
80
  const size_t xrem = Stream_GetRemainingLength(s);
757
80
  if (!tpkt_ensure_stream_consumed(rdp->log, s, tpktlength))
758
53
    Stream_Seek(s, xrem);
759
80
  return TRUE;
760
616
}
761
762
/**
763
 * Write Info Packet (TS_INFO_PACKET).
764
 * msdn{cc240475}
765
 */
766
767
static BOOL rdp_write_info_packet(rdpRdp* rdp, wStream* s)
768
0
{
769
0
  BOOL ret = FALSE;
770
0
  UINT32 flags = 0;
771
0
  WCHAR* domainW = nullptr;
772
0
  size_t cbDomain = 0;
773
0
  WCHAR* userNameW = nullptr;
774
0
  size_t cbUserName = 0;
775
0
  WCHAR* passwordW = nullptr;
776
0
  size_t cbPassword = 0;
777
0
  WCHAR* alternateShellW = nullptr;
778
0
  size_t cbAlternateShell = 0;
779
0
  WCHAR* workingDirW = nullptr;
780
0
  size_t cbWorkingDir = 0;
781
0
  BOOL usedPasswordCookie = FALSE;
782
0
  rdpSettings* settings = nullptr;
783
784
0
  WINPR_ASSERT(rdp);
785
0
  settings = rdp->settings;
786
0
  WINPR_ASSERT(settings);
787
788
0
  flags = INFO_MOUSE | INFO_UNICODE | INFO_LOGONERRORS | INFO_MAXIMIZESHELL |
789
0
          INFO_ENABLEWINDOWSKEY | INFO_DISABLECTRLALTDEL | INFO_MOUSE_HAS_WHEEL |
790
0
          INFO_FORCE_ENCRYPTED_CS_PDU;
791
792
0
  if (settings->SmartcardLogon)
793
0
  {
794
0
    flags |= INFO_AUTOLOGON;
795
0
    flags |= INFO_PASSWORD_IS_SC_PIN;
796
0
  }
797
798
0
  if (settings->AudioCapture)
799
0
    flags |= INFO_AUDIOCAPTURE;
800
801
0
  if (!settings->AudioPlayback)
802
0
    flags |= INFO_NOAUDIOPLAYBACK;
803
804
0
  if (settings->VideoDisable)
805
0
    flags |= INFO_VIDEO_DISABLE;
806
807
0
  if (settings->AutoLogonEnabled)
808
0
    flags |= INFO_AUTOLOGON;
809
810
0
  if (settings->RemoteApplicationMode)
811
0
  {
812
0
    if (settings->HiDefRemoteApp)
813
0
    {
814
0
      if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
815
0
        flags |= INFO_HIDEF_RAIL_SUPPORTED;
816
0
    }
817
818
0
    flags |= INFO_RAIL;
819
0
  }
820
821
0
  if (settings->RemoteConsoleAudio)
822
0
    flags |= INFO_REMOTECONSOLEAUDIO;
823
824
0
  if (settings->CompressionEnabled)
825
0
  {
826
0
    flags |= INFO_COMPRESSION;
827
0
    flags |= ((settings->CompressionLevel << 9) & 0x00001E00);
828
0
  }
829
830
0
  if (settings->LogonNotify)
831
0
    flags |= INFO_LOGONNOTIFY;
832
833
0
  if (settings->PasswordIsSmartcardPin)
834
0
    flags |= INFO_PASSWORD_IS_SC_PIN;
835
836
0
  {
837
0
    char* flags_description = rdp_info_package_flags_description(flags);
838
839
0
    if (flags_description)
840
0
    {
841
0
      WLog_DBG(TAG, "Client Info Packet Flags = %s", flags_description);
842
0
      free(flags_description);
843
0
    }
844
0
  }
845
846
0
  domainW = freerdp_settings_get_string_as_utf16(settings, FreeRDP_Domain, &cbDomain);
847
0
  if (cbDomain > UINT16_MAX / sizeof(WCHAR))
848
0
  {
849
0
    WLog_ERR(TAG, "cbDomain > UINT16_MAX");
850
0
    goto fail;
851
0
  }
852
0
  cbDomain *= sizeof(WCHAR);
853
854
  /* user name provided by the expert for connecting to the novice computer */
855
0
  userNameW = freerdp_settings_get_string_as_utf16(settings, FreeRDP_Username, &cbUserName);
856
0
  if (cbUserName > UINT16_MAX / sizeof(WCHAR))
857
0
  {
858
0
    WLog_ERR(TAG, "cbUserName > UINT16_MAX");
859
0
    goto fail;
860
0
  }
861
0
  cbUserName *= sizeof(WCHAR);
862
863
0
  {
864
0
    const char* pin = "*";
865
0
    if (!settings->RemoteAssistanceMode)
866
0
    {
867
      /* Ignore redirection password if we´re using smartcard and have the pin as password */
868
0
      if (((flags & INFO_PASSWORD_IS_SC_PIN) == 0) && settings->RedirectionPassword &&
869
0
          (settings->RedirectionPasswordLength > 0))
870
0
      {
871
0
        union
872
0
        {
873
0
          BYTE* bp;
874
0
          WCHAR* wp;
875
0
        } ptrconv;
876
877
0
        if (settings->RedirectionPasswordLength > UINT16_MAX)
878
0
        {
879
0
          WLog_ERR(TAG, "RedirectionPasswordLength > UINT16_MAX");
880
0
          goto fail;
881
0
        }
882
0
        usedPasswordCookie = TRUE;
883
884
0
        ptrconv.bp = settings->RedirectionPassword;
885
0
        passwordW = ptrconv.wp;
886
0
        cbPassword = (UINT16)settings->RedirectionPasswordLength;
887
0
      }
888
0
      else
889
0
        pin = freerdp_settings_get_string(settings, FreeRDP_Password);
890
0
    }
891
892
0
    if (!usedPasswordCookie && pin)
893
0
    {
894
0
      passwordW = ConvertUtf8ToWCharAlloc(pin, &cbPassword);
895
0
      if (cbPassword > UINT16_MAX / sizeof(WCHAR))
896
0
      {
897
0
        WLog_ERR(TAG, "cbPassword > UINT16_MAX");
898
0
        goto fail;
899
0
      }
900
0
      cbPassword = (UINT16)cbPassword * sizeof(WCHAR);
901
0
    }
902
0
  }
903
904
0
  {
905
0
    const char* altShell = nullptr;
906
0
    if (!settings->RemoteAssistanceMode)
907
0
      altShell = freerdp_settings_get_string(settings, FreeRDP_AlternateShell);
908
0
    else if (settings->RemoteAssistancePassStub)
909
0
      altShell = "*"; /* This field MUST be filled with "*" */
910
0
    else
911
0
      altShell = freerdp_settings_get_string(settings, FreeRDP_RemoteAssistancePassword);
912
913
0
    if (altShell && strlen(altShell) > 0)
914
0
    {
915
0
      alternateShellW = ConvertUtf8ToWCharAlloc(altShell, &cbAlternateShell);
916
0
      if (!alternateShellW)
917
0
      {
918
0
        WLog_ERR(TAG, "alternateShellW == nullptr");
919
0
        goto fail;
920
0
      }
921
0
      if (cbAlternateShell > (UINT16_MAX / sizeof(WCHAR)))
922
0
      {
923
0
        WLog_ERR(TAG, "cbAlternateShell > UINT16_MAX");
924
0
        goto fail;
925
0
      }
926
0
      cbAlternateShell = (UINT16)cbAlternateShell * sizeof(WCHAR);
927
0
    }
928
0
  }
929
930
0
  {
931
0
    FreeRDP_Settings_Keys_String inputId = FreeRDP_RemoteAssistanceSessionId;
932
0
    if (!freerdp_settings_get_bool(settings, FreeRDP_RemoteAssistanceMode))
933
0
      inputId = FreeRDP_ShellWorkingDirectory;
934
935
0
    workingDirW = freerdp_settings_get_string_as_utf16(settings, inputId, &cbWorkingDir);
936
0
  }
937
0
  if (cbWorkingDir > (UINT16_MAX / sizeof(WCHAR)))
938
0
  {
939
0
    WLog_ERR(TAG, "cbWorkingDir > UINT16_MAX");
940
0
    goto fail;
941
0
  }
942
0
  cbWorkingDir = (UINT16)cbWorkingDir * sizeof(WCHAR);
943
944
0
  if (!Stream_EnsureRemainingCapacity(s, 18ull + cbDomain + cbUserName + cbPassword +
945
0
                                             cbAlternateShell + cbWorkingDir + 5 * sizeof(WCHAR)))
946
0
    goto fail;
947
948
0
  Stream_Write_UINT32(s, settings->KeyboardCodePage); /* CodePage (4 bytes) */
949
0
  Stream_Write_UINT32(s, flags);                      /* flags (4 bytes) */
950
0
  Stream_Write_UINT16(s, (UINT16)cbDomain);           /* cbDomain (2 bytes) */
951
0
  Stream_Write_UINT16(s, (UINT16)cbUserName);         /* cbUserName (2 bytes) */
952
0
  Stream_Write_UINT16(s, (UINT16)cbPassword);         /* cbPassword (2 bytes) */
953
0
  Stream_Write_UINT16(s, (UINT16)cbAlternateShell);   /* cbAlternateShell (2 bytes) */
954
0
  Stream_Write_UINT16(s, (UINT16)cbWorkingDir);       /* cbWorkingDir (2 bytes) */
955
956
0
  Stream_Write(s, domainW, cbDomain);
957
958
  /* the mandatory null terminator */
959
0
  Stream_Write_UINT16(s, 0);
960
961
0
  Stream_Write(s, userNameW, cbUserName);
962
963
  /* the mandatory null terminator */
964
0
  Stream_Write_UINT16(s, 0);
965
966
0
  Stream_Write(s, passwordW, cbPassword);
967
968
  /* the mandatory null terminator */
969
0
  Stream_Write_UINT16(s, 0);
970
971
0
  Stream_Write(s, alternateShellW, cbAlternateShell);
972
973
  /* the mandatory null terminator */
974
0
  Stream_Write_UINT16(s, 0);
975
976
0
  Stream_Write(s, workingDirW, cbWorkingDir);
977
978
  /* the mandatory null terminator */
979
0
  Stream_Write_UINT16(s, 0);
980
0
  ret = TRUE;
981
0
fail:
982
0
  free(domainW);
983
0
  free(userNameW);
984
0
  free(alternateShellW);
985
0
  free(workingDirW);
986
987
0
  if (!usedPasswordCookie)
988
0
    free(passwordW);
989
990
0
  if (!ret)
991
0
    return FALSE;
992
993
0
  if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
994
0
    ret = rdp_write_extended_info_packet(rdp, s); /* extraInfo */
995
996
0
  return ret;
997
0
}
998
999
/**
1000
 * Read Client Info PDU (CLIENT_INFO_PDU).
1001
 * msdn{cc240474}
1002
 * @param rdp RDP module
1003
 * @param s stream
1004
 */
1005
1006
BOOL rdp_recv_client_info(rdpRdp* rdp, wStream* s)
1007
16.4k
{
1008
16.4k
  UINT16 length = 0;
1009
16.4k
  UINT16 channelId = 0;
1010
16.4k
  UINT16 securityFlags = 0;
1011
1012
16.4k
  WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE);
1013
1014
16.4k
  if (!rdp_read_header(rdp, s, &length, &channelId))
1015
15.5k
    return FALSE;
1016
1017
887
  if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
1018
25
    return FALSE;
1019
1020
862
  if ((securityFlags & SEC_INFO_PKT) == 0)
1021
18
    return FALSE;
1022
1023
844
  if (rdp->settings->UseRdpSecurityLayer)
1024
0
  {
1025
0
    if (securityFlags & SEC_REDIRECTION_PKT)
1026
0
    {
1027
0
      WLog_ERR(TAG, "Error: SEC_REDIRECTION_PKT unsupported");
1028
0
      return FALSE;
1029
0
    }
1030
1031
0
    if (securityFlags & SEC_ENCRYPT)
1032
0
    {
1033
0
      if (!rdp_decrypt(rdp, s, &length, securityFlags))
1034
0
        return FALSE;
1035
0
    }
1036
0
  }
1037
1038
844
  return rdp_read_info_packet(rdp, s, length);
1039
844
}
1040
1041
/**
1042
 * Send Client Info PDU (CLIENT_INFO_PDU).
1043
 * msdn{cc240474}
1044
 * @param rdp RDP module
1045
 */
1046
1047
BOOL rdp_send_client_info(rdpRdp* rdp)
1048
0
{
1049
0
  UINT16 sec_flags = SEC_INFO_PKT;
1050
0
  wStream* s = nullptr;
1051
0
  WINPR_ASSERT(rdp);
1052
0
  s = rdp_send_stream_init(rdp, &sec_flags);
1053
1054
0
  if (!s)
1055
0
  {
1056
0
    WLog_ERR(TAG, "Stream_New failed!");
1057
0
    return FALSE;
1058
0
  }
1059
1060
0
  if (!rdp_write_info_packet(rdp, s))
1061
0
  {
1062
0
    Stream_Release(s);
1063
0
    return FALSE;
1064
0
  }
1065
0
  return rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
1066
0
}
1067
1068
static void rdp_free_logon_info(logon_info* info)
1069
660
{
1070
660
  if (!info)
1071
0
    return;
1072
660
  free(info->domain);
1073
660
  free(info->username);
1074
1075
660
  const logon_info empty = WINPR_C_ARRAY_INIT;
1076
660
  *info = empty;
1077
660
}
1078
1079
static BOOL rdp_info_read_string(const char* what, wStream* s, size_t size, size_t max,
1080
                                 BOOL skipMax, char** dst)
1081
735
{
1082
735
  WINPR_ASSERT(dst);
1083
735
  *dst = nullptr;
1084
1085
735
  if (size == 0)
1086
467
  {
1087
467
    if (skipMax)
1088
389
      return Stream_SafeSeek(s, max);
1089
78
    return TRUE;
1090
467
  }
1091
1092
268
  if (((size % sizeof(WCHAR)) != 0) || (size > max))
1093
233
  {
1094
233
    WLog_ERR(TAG, "protocol error: invalid %s value: %" PRIuz "", what, size);
1095
233
    return FALSE;
1096
233
  }
1097
1098
35
  const WCHAR* str = Stream_ConstPointer(s);
1099
35
  if (!Stream_SafeSeek(s, skipMax ? max : size))
1100
2
    return FALSE;
1101
1102
33
  if (str[size / sizeof(WCHAR) - 1])
1103
7
  {
1104
7
    WLog_ERR(TAG, "protocol error: %s must be null terminated", what);
1105
7
    return FALSE;
1106
7
  }
1107
1108
26
  size_t len = 0;
1109
26
  char* rc = ConvertWCharNToUtf8Alloc(str, size / sizeof(WCHAR), &len);
1110
26
  if (!rc)
1111
1
  {
1112
1
    WLog_ERR(TAG, "failed to convert the %s string", what);
1113
1
    free(rc);
1114
1
    return FALSE;
1115
1
  }
1116
1117
25
  *dst = rc;
1118
25
  return TRUE;
1119
26
}
1120
1121
static BOOL rdp_recv_logon_info_v1(rdpRdp* rdp, wStream* s, logon_info* info)
1122
510
{
1123
510
  UINT32 cbDomain = 0;
1124
510
  UINT32 cbUserName = 0;
1125
1126
510
  WINPR_UNUSED(rdp);
1127
510
  WINPR_ASSERT(info);
1128
1129
510
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1130
140
    return FALSE;
1131
1132
370
  Stream_Read_UINT32(s, cbDomain); /* cbDomain (4 bytes) */
1133
1134
  /* cbDomain is the size of the Unicode character data (including the mandatory
1135
   * null terminator) in bytes present in the fixed-length (52 bytes) Domain field
1136
   */
1137
370
  if (!rdp_info_read_string("Domain", s, cbDomain, 52, TRUE, &info->domain))
1138
94
    goto fail;
1139
1140
276
  Stream_Read_UINT32(s, cbUserName); /* cbUserName (4 bytes) */
1141
1142
  /* cbUserName is the size of the Unicode character data (including the mandatory
1143
   * null terminator) in bytes present in the fixed-length (512 bytes) UserName field.
1144
   */
1145
276
  if (!rdp_info_read_string("UserName", s, cbUserName, 512, TRUE, &info->username))
1146
139
    goto fail;
1147
1148
137
  Stream_Read_UINT32(s, info->sessionId); /* SessionId (4 bytes) */
1149
137
  WLog_DBG(TAG, "LogonInfoV1: SessionId: 0x%08" PRIX32 " UserName: [%s] Domain: [%s]",
1150
137
           info->sessionId, info->username, info->domain);
1151
137
  return TRUE;
1152
233
fail:
1153
233
  return FALSE;
1154
276
}
1155
1156
static BOOL rdp_recv_logon_info_v2(rdpRdp* rdp, wStream* s, logon_info* info)
1157
150
{
1158
150
  UINT16 Version = 0;
1159
150
  UINT32 Size = 0;
1160
150
  UINT32 cbDomain = 0;
1161
150
  UINT32 cbUserName = 0;
1162
1163
150
  WINPR_ASSERT(rdp);
1164
150
  WINPR_ASSERT(s);
1165
150
  WINPR_ASSERT(info);
1166
1167
150
  WINPR_UNUSED(rdp);
1168
1169
150
  if (!Stream_CheckAndLogRequiredLength(TAG, s, logonInfoV2TotalSize))
1170
15
    return FALSE;
1171
1172
135
  Stream_Read_UINT16(s, Version); /* Version (2 bytes) */
1173
135
  if (Version != SAVE_SESSION_PDU_VERSION_ONE)
1174
23
  {
1175
23
    WLog_WARN(TAG, "LogonInfoV2::Version expected %d bytes, got %" PRIu16,
1176
23
              SAVE_SESSION_PDU_VERSION_ONE, Version);
1177
23
    return FALSE;
1178
23
  }
1179
1180
112
  Stream_Read_UINT32(s, Size); /* Size (4 bytes) */
1181
1182
  /* [MS-RDPBCGR] 2.2.10.1.1.2 Logon Info Version 2 (TS_LOGON_INFO_VERSION_2)
1183
   * should be logonInfoV2TotalSize
1184
   * but even MS server 2019 sends logonInfoV2Size
1185
   */
1186
112
  if (Size != logonInfoV2TotalSize)
1187
112
  {
1188
112
    if (Size != logonInfoV2Size)
1189
65
    {
1190
65
      WLog_WARN(TAG, "LogonInfoV2::Size expected %" PRIu32 " bytes, got %" PRIu32,
1191
65
                logonInfoV2TotalSize, Size);
1192
65
      return FALSE;
1193
65
    }
1194
112
  }
1195
1196
47
  Stream_Read_UINT32(s, info->sessionId);  /* SessionId (4 bytes) */
1197
47
  Stream_Read_UINT32(s, cbDomain);         /* cbDomain (4 bytes) */
1198
47
  Stream_Read_UINT32(s, cbUserName);       /* cbUserName (4 bytes) */
1199
47
  Stream_Seek(s, logonInfoV2ReservedSize); /* pad (558 bytes) */
1200
1201
  /* cbDomain is the size in bytes of the Unicode character data in the Domain field.
1202
   * The size of the mandatory null terminator is include in this value.
1203
   * Note: Since MS-RDPBCGR 2.2.10.1.1.2 does not mention any size limits we assume
1204
   *       that the maximum value is 52 bytes, according to the fixed size of the
1205
   *       Domain field in the Logon Info Version 1 (TS_LOGON_INFO) structure.
1206
   */
1207
47
  if (!rdp_info_read_string("Domain", s, cbDomain, 52, FALSE, &info->domain))
1208
5
    goto fail;
1209
1210
  /* cbUserName is the size in bytes of the Unicode character data in the UserName field.
1211
   * The size of the mandatory null terminator is include in this value.
1212
   * Note: Since MS-RDPBCGR 2.2.10.1.1.2 does not mention any size limits we assume
1213
   *       that the maximum value is 512 bytes, according to the fixed size of the
1214
   *       Username field in the Logon Info Version 1 (TS_LOGON_INFO) structure.
1215
   */
1216
42
  if (!rdp_info_read_string("UserName", s, cbUserName, 512, FALSE, &info->username))
1217
5
    goto fail;
1218
1219
  /* We´ve seen undocumented padding with windows 11 here.
1220
   * unless it has actual data in it ignore it.
1221
   * if there is unexpected data, print a warning and dump the contents
1222
   */
1223
37
  {
1224
37
    const size_t rem = Stream_GetRemainingLength(s);
1225
37
    if (rem > 0)
1226
36
    {
1227
36
      BOOL warn = FALSE;
1228
36
      const char* str = Stream_ConstPointer(s);
1229
378k
      for (size_t x = 0; x < rem; x++)
1230
378k
      {
1231
378k
        if (str[x] != '\0')
1232
282k
          warn = TRUE;
1233
378k
      }
1234
36
      if (warn)
1235
28
      {
1236
28
        WLog_WARN(TAG, "unexpected padding of %" PRIuz " bytes, data not '\\0'", rem);
1237
28
        winpr_HexDump(TAG, WLOG_TRACE, str, rem);
1238
28
      }
1239
1240
36
      if (!Stream_SafeSeek(s, rem))
1241
0
        goto fail;
1242
36
    }
1243
37
  }
1244
1245
37
  WLog_DBG(TAG, "LogonInfoV2: SessionId: 0x%08" PRIX32 " UserName: [%s] Domain: [%s]",
1246
37
           info->sessionId, info->username, info->domain);
1247
37
  return TRUE;
1248
10
fail:
1249
10
  return FALSE;
1250
37
}
1251
1252
static BOOL rdp_recv_logon_plain_notify(rdpRdp* rdp, wStream* s)
1253
36
{
1254
36
  WINPR_UNUSED(rdp);
1255
36
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1256
8
    return FALSE;
1257
1258
28
  Stream_Seek(s, 576); /* pad (576 bytes) */
1259
28
  WLog_DBG(TAG, "LogonPlainNotify");
1260
28
  return TRUE;
1261
36
}
1262
1263
static BOOL rdp_recv_logon_error_info(rdpRdp* rdp, wStream* s, logon_info_ex* info)
1264
114
{
1265
114
  freerdp* instance = nullptr;
1266
114
  UINT32 errorNotificationType = 0;
1267
114
  UINT32 errorNotificationData = 0;
1268
1269
114
  WINPR_ASSERT(rdp);
1270
114
  WINPR_ASSERT(rdp->context);
1271
114
  WINPR_ASSERT(s);
1272
114
  WINPR_ASSERT(info);
1273
1274
114
  instance = rdp->context->instance;
1275
114
  WINPR_ASSERT(instance);
1276
1277
114
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1278
1
    return FALSE;
1279
1280
113
  Stream_Read_UINT32(s, errorNotificationType); /* errorNotificationType (4 bytes) */
1281
113
  Stream_Read_UINT32(s, errorNotificationData); /* errorNotificationData (4 bytes) */
1282
113
  WLog_DBG(TAG, "LogonErrorInfo: Data: 0x%08" PRIX32 " Type: 0x%08" PRIX32 "",
1283
113
           errorNotificationData, errorNotificationType);
1284
113
  if (instance->LogonErrorInfo)
1285
113
  {
1286
113
    const int rc =
1287
113
        instance->LogonErrorInfo(instance, errorNotificationData, errorNotificationType);
1288
113
    if (rc < 0)
1289
0
      return FALSE;
1290
113
  }
1291
113
  info->ErrorNotificationType = errorNotificationType;
1292
113
  info->ErrorNotificationData = errorNotificationData;
1293
113
  return TRUE;
1294
113
}
1295
1296
static BOOL rdp_recv_logon_info_extended(rdpRdp* rdp, wStream* s, logon_info_ex* info)
1297
219
{
1298
219
  UINT32 cbFieldData = 0;
1299
219
  UINT32 fieldsPresent = 0;
1300
219
  UINT16 Length = 0;
1301
1302
219
  WINPR_ASSERT(rdp);
1303
219
  WINPR_ASSERT(s);
1304
219
  WINPR_ASSERT(info);
1305
1306
219
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1307
3
  {
1308
3
    WLog_WARN(TAG, "received short logon info extended, need 6 bytes, got %" PRIuz,
1309
3
              Stream_GetRemainingLength(s));
1310
3
    return FALSE;
1311
3
  }
1312
1313
216
  Stream_Read_UINT16(s, Length);        /* Length (2 bytes) */
1314
216
  Stream_Read_UINT32(s, fieldsPresent); /* fieldsPresent (4 bytes) */
1315
1316
216
  if ((Length < 6) || (!Stream_CheckAndLogRequiredLength(TAG, s, (Length - 6U))))
1317
18
  {
1318
18
    WLog_WARN(TAG,
1319
18
              "received short logon info extended, need %" PRIu16 " - 6 bytes, got %" PRIuz,
1320
18
              Length, Stream_GetRemainingLength(s));
1321
18
    return FALSE;
1322
18
  }
1323
1324
198
  WLog_DBG(TAG, "LogonInfoExtended: fieldsPresent: 0x%08" PRIX32 "", fieldsPresent);
1325
1326
  /* logonFields */
1327
1328
198
  if (fieldsPresent & LOGON_EX_AUTORECONNECTCOOKIE)
1329
90
  {
1330
90
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1331
1
      return FALSE;
1332
1333
89
    info->haveCookie = TRUE;
1334
89
    Stream_Read_UINT32(s, cbFieldData); /* cbFieldData (4 bytes) */
1335
1336
89
    if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1337
6
      return FALSE;
1338
1339
83
    if (!rdp_read_server_auto_reconnect_cookie(rdp, s, info))
1340
55
      return FALSE;
1341
83
  }
1342
1343
136
  if (fieldsPresent & LOGON_EX_LOGONERRORS)
1344
128
  {
1345
128
    info->haveErrorInfo = TRUE;
1346
1347
128
    if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1348
2
      return FALSE;
1349
1350
126
    Stream_Read_UINT32(s, cbFieldData); /* cbFieldData (4 bytes) */
1351
1352
126
    if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1353
12
      return FALSE;
1354
1355
114
    if (!rdp_recv_logon_error_info(rdp, s, info))
1356
1
      return FALSE;
1357
114
  }
1358
1359
121
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 570))
1360
81
    return FALSE;
1361
1362
40
  Stream_Seek(s, 570); /* pad (570 bytes) */
1363
40
  return TRUE;
1364
121
}
1365
1366
BOOL rdp_recv_save_session_info(rdpRdp* rdp, wStream* s)
1367
8.80k
{
1368
8.80k
  UINT32 infoType = 0;
1369
8.80k
  BOOL status = FALSE;
1370
1371
8.80k
  rdpContext* context = rdp->context;
1372
8.80k
  rdpUpdate* update = rdp->context->update;
1373
1374
8.80k
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1375
5.53k
    return FALSE;
1376
1377
3.26k
  Stream_Read_UINT32(s, infoType); /* infoType (4 bytes) */
1378
1379
3.26k
  switch (infoType)
1380
3.26k
  {
1381
510
    case INFO_TYPE_LOGON:
1382
510
    {
1383
510
      logon_info logonInfo = WINPR_C_ARRAY_INIT;
1384
510
      status = rdp_recv_logon_info_v1(rdp, s, &logonInfo);
1385
1386
510
      if (status && update->SaveSessionInfo)
1387
0
        status = update->SaveSessionInfo(context, infoType, &logonInfo);
1388
1389
510
      rdp_free_logon_info(&logonInfo);
1390
510
    }
1391
510
    break;
1392
1393
150
    case INFO_TYPE_LOGON_LONG:
1394
150
    {
1395
150
      logon_info logonInfo = WINPR_C_ARRAY_INIT;
1396
150
      status = rdp_recv_logon_info_v2(rdp, s, &logonInfo);
1397
1398
150
      if (status && update->SaveSessionInfo)
1399
0
        status = update->SaveSessionInfo(context, infoType, &logonInfo);
1400
1401
150
      rdp_free_logon_info(&logonInfo);
1402
150
    }
1403
150
    break;
1404
1405
36
    case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1406
36
      status = rdp_recv_logon_plain_notify(rdp, s);
1407
1408
36
      if (status && update->SaveSessionInfo)
1409
0
        status = update->SaveSessionInfo(context, infoType, nullptr);
1410
1411
36
      break;
1412
1413
219
    case INFO_TYPE_LOGON_EXTENDED_INF:
1414
219
    {
1415
219
      logon_info_ex logonInfoEx = WINPR_C_ARRAY_INIT;
1416
219
      status = rdp_recv_logon_info_extended(rdp, s, &logonInfoEx);
1417
1418
219
      if (status && update->SaveSessionInfo)
1419
0
        status = update->SaveSessionInfo(context, infoType, &logonInfoEx);
1420
219
    }
1421
219
    break;
1422
1423
2.35k
    default:
1424
2.35k
      WLog_WARN(TAG, "Unhandled saveSessionInfo type 0x%" PRIx32 "", infoType);
1425
2.35k
      status = TRUE;
1426
2.35k
      break;
1427
3.26k
  }
1428
1429
3.26k
  if (!status)
1430
673
  {
1431
673
    WLog_WARN(TAG, "SaveSessionInfo error: infoType: %s (%" PRIu32 ")",
1432
673
              freerdp_session_logon_type_str(infoType), infoType);
1433
673
  }
1434
1435
3.26k
  return status;
1436
3.26k
}
1437
1438
static BOOL rdp_write_logon_info_v1(wStream* s, logon_info* info)
1439
0
{
1440
0
  const size_t charLen = 52 / sizeof(WCHAR);
1441
0
  const size_t userCharLen = 512 / sizeof(WCHAR);
1442
1443
0
  size_t sz = 4 + 52 + 4 + 512 + 4;
1444
1445
0
  if (!Stream_EnsureRemainingCapacity(s, sz))
1446
0
    return FALSE;
1447
1448
  /* domain */
1449
0
  {
1450
0
    WINPR_ASSERT(info);
1451
0
    if (!info->domain || !info->username)
1452
0
      return FALSE;
1453
0
    const size_t len = strnlen(info->domain, charLen + 1);
1454
0
    if (len > charLen)
1455
0
      return FALSE;
1456
1457
0
    const size_t wlen = len * sizeof(WCHAR);
1458
0
    if (wlen > UINT32_MAX)
1459
0
      return FALSE;
1460
1461
0
    Stream_Write_UINT32(s, (UINT32)wlen);
1462
0
    if (Stream_Write_UTF16_String_From_UTF8(s, charLen, info->domain, len, TRUE) < 0)
1463
0
      return FALSE;
1464
0
  }
1465
1466
  /* username */
1467
0
  {
1468
0
    const size_t len = strnlen(info->username, userCharLen + 1);
1469
0
    if (len > userCharLen)
1470
0
      return FALSE;
1471
1472
0
    const size_t wlen = len * sizeof(WCHAR);
1473
0
    if (wlen > UINT32_MAX)
1474
0
      return FALSE;
1475
1476
0
    Stream_Write_UINT32(s, (UINT32)wlen);
1477
1478
0
    if (Stream_Write_UTF16_String_From_UTF8(s, userCharLen, info->username, len, TRUE) < 0)
1479
0
      return FALSE;
1480
0
  }
1481
1482
  /* sessionId */
1483
0
  Stream_Write_UINT32(s, info->sessionId);
1484
0
  return TRUE;
1485
0
}
1486
1487
static BOOL rdp_write_logon_info_v2(wStream* s, const logon_info* info)
1488
0
{
1489
0
  size_t domainLen = 0;
1490
0
  size_t usernameLen = 0;
1491
0
  size_t domainStrLen = 0;
1492
0
  size_t usernameStrLen = 0;
1493
1494
0
  if (!Stream_EnsureRemainingCapacity(s, logonInfoV2TotalSize))
1495
0
    return FALSE;
1496
1497
0
  Stream_Write_UINT16(s, SAVE_SESSION_PDU_VERSION_ONE);
1498
  /* [MS-RDPBCGR] 2.2.10.1.1.2 Logon Info Version 2 (TS_LOGON_INFO_VERSION_2)
1499
   * should be logonInfoV2TotalSize
1500
   * but even MS server 2019 sends logonInfoV2Size
1501
   */
1502
0
  Stream_Write_UINT32(s, logonInfoV2Size);
1503
0
  Stream_Write_UINT32(s, info->sessionId);
1504
0
  if (info->domain)
1505
0
  {
1506
0
    domainStrLen = strnlen(info->domain, 256); /* lmcons.h UNLEN */
1507
0
    const SSIZE_T wlen = ConvertUtf8NToWChar(info->domain, domainStrLen, nullptr, 0);
1508
0
    if (wlen < 0)
1509
0
      return FALSE;
1510
0
    domainLen = WINPR_ASSERTING_INT_CAST(size_t, wlen);
1511
0
  }
1512
0
  if (domainLen >= UINT32_MAX / sizeof(WCHAR))
1513
0
    return FALSE;
1514
0
  Stream_Write_UINT32(s, (UINT32)(domainLen + 1) * sizeof(WCHAR));
1515
1516
0
  if (info->username)
1517
0
  {
1518
0
    usernameStrLen = strnlen(info->username, 256); /* lmcons.h UNLEN */
1519
0
    const SSIZE_T wlen = ConvertUtf8NToWChar(info->username, usernameStrLen, nullptr, 0);
1520
0
    if (wlen < 0)
1521
0
      return FALSE;
1522
0
    usernameLen = WINPR_ASSERTING_INT_CAST(size_t, wlen);
1523
0
  }
1524
0
  if (usernameLen >= UINT32_MAX / sizeof(WCHAR))
1525
0
    return FALSE;
1526
0
  Stream_Write_UINT32(s, (UINT32)(usernameLen + 1) * sizeof(WCHAR));
1527
0
  Stream_Seek(s, logonInfoV2ReservedSize);
1528
0
  if (Stream_Write_UTF16_String_From_UTF8(s, domainLen + 1, info->domain, domainStrLen, TRUE) < 0)
1529
0
    return FALSE;
1530
0
  if (Stream_Write_UTF16_String_From_UTF8(s, usernameLen + 1, info->username, usernameStrLen,
1531
0
                                          TRUE) < 0)
1532
0
    return FALSE;
1533
0
  return TRUE;
1534
0
}
1535
1536
static BOOL rdp_write_logon_info_plain(wStream* s)
1537
0
{
1538
0
  if (!Stream_EnsureRemainingCapacity(s, 576))
1539
0
    return FALSE;
1540
1541
0
  Stream_Seek(s, 576);
1542
0
  return TRUE;
1543
0
}
1544
1545
static BOOL rdp_write_logon_info_ex(wStream* s, logon_info_ex* info)
1546
0
{
1547
0
  UINT32 FieldsPresent = 0;
1548
0
  UINT16 Size = 2 + 4 + 570;
1549
1550
0
  if (info->haveCookie)
1551
0
  {
1552
0
    FieldsPresent |= LOGON_EX_AUTORECONNECTCOOKIE;
1553
0
    Size += 28;
1554
0
  }
1555
1556
0
  if (info->haveErrorInfo)
1557
0
  {
1558
0
    FieldsPresent |= LOGON_EX_LOGONERRORS;
1559
0
    Size += 8;
1560
0
  }
1561
1562
0
  if (!Stream_EnsureRemainingCapacity(s, Size))
1563
0
    return FALSE;
1564
1565
0
  Stream_Write_UINT16(s, Size);
1566
0
  Stream_Write_UINT32(s, FieldsPresent);
1567
1568
0
  if (info->haveCookie)
1569
0
  {
1570
0
    Stream_Write_UINT32(s, 28);                       /* cbFieldData (4 bytes) */
1571
0
    Stream_Write_UINT32(s, 28);                       /* cbLen (4 bytes) */
1572
0
    Stream_Write_UINT32(s, AUTO_RECONNECT_VERSION_1); /* Version (4 bytes) */
1573
0
    Stream_Write_UINT32(s, info->LogonId);            /* LogonId (4 bytes) */
1574
0
    Stream_Write(s, info->ArcRandomBits, 16);         /* ArcRandomBits (16 bytes) */
1575
0
  }
1576
1577
0
  if (info->haveErrorInfo)
1578
0
  {
1579
0
    Stream_Write_UINT32(s, 8);                           /* cbFieldData (4 bytes) */
1580
0
    Stream_Write_UINT32(s, info->ErrorNotificationType); /* ErrorNotificationType (4 bytes) */
1581
0
    Stream_Write_UINT32(s, info->ErrorNotificationData); /* ErrorNotificationData (4 bytes) */
1582
0
  }
1583
1584
0
  Stream_Seek(s, 570);
1585
0
  return TRUE;
1586
0
}
1587
1588
BOOL rdp_send_save_session_info(rdpContext* context, UINT32 type, void* data)
1589
0
{
1590
0
  UINT16 sec_flags = 0;
1591
0
  BOOL status = 0;
1592
1593
0
  WINPR_ASSERT(context);
1594
0
  rdpRdp* rdp = context->rdp;
1595
0
  wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
1596
1597
0
  if (!s)
1598
0
    return FALSE;
1599
1600
0
  Stream_Write_UINT32(s, type);
1601
1602
0
  switch (type)
1603
0
  {
1604
0
    case INFO_TYPE_LOGON:
1605
0
      status = rdp_write_logon_info_v1(s, (logon_info*)data);
1606
0
      break;
1607
1608
0
    case INFO_TYPE_LOGON_LONG:
1609
0
      status = rdp_write_logon_info_v2(s, (logon_info*)data);
1610
0
      break;
1611
1612
0
    case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1613
0
      status = rdp_write_logon_info_plain(s);
1614
0
      break;
1615
1616
0
    case INFO_TYPE_LOGON_EXTENDED_INF:
1617
0
      status = rdp_write_logon_info_ex(s, (logon_info_ex*)data);
1618
0
      break;
1619
1620
0
    default:
1621
0
      WLog_ERR(TAG, "saveSessionInfo type 0x%" PRIx32 " not handled", type);
1622
0
      status = FALSE;
1623
0
      break;
1624
0
  }
1625
1626
0
  if (status)
1627
0
    status =
1628
0
        rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SAVE_SESSION_INFO, rdp->mcs->userId, sec_flags);
1629
0
  else
1630
0
    Stream_Release(s);
1631
1632
0
  return status;
1633
0
}
1634
1635
BOOL rdp_send_server_status_info(rdpContext* context, UINT32 status)
1636
0
{
1637
0
  UINT16 sec_flags = 0;
1638
0
  wStream* s = nullptr;
1639
0
  rdpRdp* rdp = context->rdp;
1640
0
  s = rdp_data_pdu_init(rdp, &sec_flags);
1641
1642
0
  if (!s)
1643
0
    return FALSE;
1644
1645
0
  Stream_Write_UINT32(s, status);
1646
0
  return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_STATUS_INFO, rdp->mcs->userId, sec_flags);
1647
0
}