Coverage Report

Created: 2026-07-30 07:11

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