20 #include <freerdp/config.h>
24 #include <winpr/assert.h>
25 #include <winpr/cast.h>
27 #include "activation.h"
30 #define TAG FREERDP_TAG("core.activation")
32 static BOOL rdp_recv_client_font_list_pdu(
wStream* s);
33 static BOOL rdp_recv_client_persistent_key_list_pdu(
wStream* s);
34 static BOOL rdp_send_server_font_map_pdu(rdpRdp* rdp);
36 static BOOL rdp_write_synchronize_pdu(
wStream* s,
const rdpSettings* settings)
40 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 4))
42 Stream_Write_UINT16(s, SYNCMSGTYPE_SYNC);
43 Stream_Write_UINT16(s,
44 WINPR_ASSERTING_INT_CAST(uint16_t, PduSource));
48 static BOOL rdp_recv_sync_pdu(rdpRdp* rdp,
wStream* s,
const char* what)
51 UINT16 targetUser = 0;
54 if (!Stream_CheckAndLogRequiredLengthEx(TAG, WLOG_WARN, s, 4, 1,
"%s(%s:%" PRIuz
") %s",
55 __func__, __FILE__, (
size_t)__LINE__, what))
57 Stream_Read_UINT16(s, msgType);
58 if (msgType != SYNCMSGTYPE_SYNC)
60 WLog_WARN(TAG,
"%s: Invalid messageType=0x%04" PRIx16
", expected 0x%04" PRIx16, what,
61 msgType, SYNCMSGTYPE_SYNC);
64 Stream_Read_UINT16(s, targetUser);
65 WLog_VRB(TAG,
"%s: targetUser=0x%04" PRIx16, what, targetUser);
69 BOOL rdp_recv_server_synchronize_pdu(rdpRdp* rdp,
wStream* s)
71 if (!rdp_recv_sync_pdu(rdp, s,
"[MS-RDPBCGR] 2.2.1.19 Server Synchronize PDU"))
73 return rdp_finalize_set_flag(rdp, FINALIZE_SC_SYNCHRONIZE_PDU);
76 BOOL rdp_send_server_synchronize_pdu(rdpRdp* rdp)
78 wStream* s = rdp_data_pdu_init(rdp);
83 if (!rdp_write_synchronize_pdu(s, rdp->settings))
89 WINPR_ASSERT(rdp->mcs);
90 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SYNCHRONIZE, rdp->mcs->userId);
93 BOOL rdp_recv_client_synchronize_pdu(rdpRdp* rdp,
wStream* s)
95 if (!rdp_recv_sync_pdu(rdp, s,
"[MS-RDPBCGR] 2.2.1.14 Client Synchronize PDU"))
97 return rdp_finalize_set_flag(rdp, FINALIZE_CS_SYNCHRONIZE_PDU);
100 BOOL rdp_send_client_synchronize_pdu(rdpRdp* rdp)
102 wStream* s = rdp_data_pdu_init(rdp);
107 if (!rdp_write_synchronize_pdu(s, rdp->settings))
109 Stream_Free(s, TRUE);
113 WINPR_ASSERT(rdp->mcs);
114 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SYNCHRONIZE, rdp->mcs->userId);
117 static BOOL rdp_recv_control_pdu(
wStream* s, UINT16* action, UINT16* grantId, UINT32* controlId)
120 WINPR_ASSERT(action);
121 WINPR_ASSERT(grantId);
122 WINPR_ASSERT(controlId);
124 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
127 Stream_Read_UINT16(s, *action);
128 Stream_Read_UINT16(s, *grantId);
129 Stream_Read_UINT32(s, *controlId);
133 static BOOL rdp_write_client_control_pdu(
wStream* s, UINT16 action, UINT16 grantId,
137 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
139 Stream_Write_UINT16(s, action);
140 Stream_Write_UINT16(s, grantId);
141 Stream_Write_UINT32(s, controlId);
145 BOOL rdp_recv_server_control_pdu(rdpRdp* rdp,
wStream* s)
149 UINT32 controlId = 0;
154 if (!rdp_recv_control_pdu(s, &action, &grantId, &controlId))
159 case CTRLACTION_COOPERATE:
160 return rdp_finalize_set_flag(rdp, FINALIZE_SC_CONTROL_COOPERATE_PDU);
162 case CTRLACTION_GRANTED_CONTROL:
163 rdp->resendFocus = TRUE;
164 return rdp_finalize_set_flag(rdp, FINALIZE_SC_CONTROL_GRANTED_PDU);
167 char buffer[128] = { 0 };
168 WLog_WARN(TAG,
"Unexpected control PDU %s",
169 rdp_ctrlaction_string(action, buffer,
sizeof(buffer)));
176 BOOL rdp_send_server_control_cooperate_pdu(rdpRdp* rdp)
178 wStream* s = rdp_data_pdu_init(rdp);
181 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
183 Stream_Free(s, TRUE);
186 Stream_Write_UINT16(s, CTRLACTION_COOPERATE);
187 Stream_Write_UINT16(s, 0);
188 Stream_Write_UINT32(s, 0);
190 WINPR_ASSERT(rdp->mcs);
191 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId);
194 BOOL rdp_send_server_control_granted_pdu(rdpRdp* rdp)
196 wStream* s = rdp_data_pdu_init(rdp);
199 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
201 Stream_Free(s, TRUE);
205 WINPR_ASSERT(rdp->mcs);
206 Stream_Write_UINT16(s, CTRLACTION_GRANTED_CONTROL);
207 Stream_Write_UINT16(s, rdp->mcs->userId);
208 Stream_Write_UINT32(s, 0x03EA);
209 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId);
212 BOOL rdp_send_client_control_pdu(rdpRdp* rdp, UINT16 action)
214 wStream* s = rdp_data_pdu_init(rdp);
216 UINT16 ControlId = 0;
220 case CTRLACTION_COOPERATE:
221 case CTRLACTION_REQUEST_CONTROL:
225 "Invalid client control PDU::action 0x%04" PRIx16
", not allowed by client",
232 if (!rdp_write_client_control_pdu(s, action, GrantId, ControlId))
234 Stream_Free(s, TRUE);
238 WINPR_ASSERT(rdp->mcs);
239 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_CONTROL, rdp->mcs->userId);
242 static BOOL rdp_write_client_persistent_key_list_pdu(
wStream* s,
248 if (!Stream_EnsureRemainingCapacity(s, 24))
251 Stream_Write_UINT16(s, info->numEntriesCache0);
252 Stream_Write_UINT16(s, info->numEntriesCache1);
253 Stream_Write_UINT16(s, info->numEntriesCache2);
254 Stream_Write_UINT16(s, info->numEntriesCache3);
255 Stream_Write_UINT16(s, info->numEntriesCache4);
256 Stream_Write_UINT16(s, info->totalEntriesCache0);
257 Stream_Write_UINT16(s, info->totalEntriesCache1);
258 Stream_Write_UINT16(s, info->totalEntriesCache2);
259 Stream_Write_UINT16(s, info->totalEntriesCache3);
260 Stream_Write_UINT16(s, info->totalEntriesCache4);
261 Stream_Write_UINT8(s, PERSIST_FIRST_PDU | PERSIST_LAST_PDU);
262 Stream_Write_UINT8(s, 0);
263 Stream_Write_UINT16(s, 0);
266 if (!Stream_EnsureRemainingCapacity(s, info->keyCount * 8ull))
269 for (UINT32 index = 0; index < info->keyCount; index++)
271 const UINT64 val = info->keyList[index];
272 Stream_Write_UINT64(s, val);
278 static UINT16 rdp_load_persistent_key_list(rdpRdp* rdp, UINT64** pKeyList)
281 UINT64* keyList = NULL;
282 rdpPersistentCache* persistent = NULL;
283 rdpSettings* settings = rdp->settings;
290 if (!settings->BitmapCachePersistFile)
293 persistent = persistent_cache_new();
299 persistent_cache_open(persistent, settings->BitmapCachePersistFile, FALSE, 0);
304 const int count = persistent_cache_get_count(persistent);
305 if ((count < 0) || (count > UINT16_MAX))
308 keyCount = (UINT16)count;
309 keyList = (UINT64*)calloc(keyCount,
sizeof(UINT64));
314 for (
int index = 0; index < count; index++)
318 if (persistent_cache_read_entry(persistent, &cacheEntry) < 1)
321 keyList[index] = cacheEntry.key64;
326 persistent_cache_free(persistent);
329 persistent_cache_free(persistent);
334 BOOL rdp_send_client_persistent_key_list_pdu(rdpRdp* rdp)
336 UINT16 keyMaxFrag = 2042;
337 UINT64* keyList = NULL;
340 rdpSettings* settings = rdp->settings;
341 UINT16 keyCount = rdp_load_persistent_key_list(rdp, &keyList);
343 WLog_DBG(TAG,
"Persistent Key List: TotalKeyCount: %" PRIu16
" MaxKeyFrag: %" PRIu16, keyCount,
352 if (keyCount > keyMaxFrag)
353 keyCount = keyMaxFrag;
355 WINPR_ASSERT(settings->BitmapCacheV2CellInfo[0].numEntries <= UINT16_MAX);
356 info.totalEntriesCache0 = (UINT16)settings->BitmapCacheV2CellInfo[0].numEntries;
358 WINPR_ASSERT(settings->BitmapCacheV2CellInfo[1].numEntries <= UINT16_MAX);
359 info.totalEntriesCache1 = (UINT16)settings->BitmapCacheV2CellInfo[1].numEntries;
361 WINPR_ASSERT(settings->BitmapCacheV2CellInfo[2].numEntries <= UINT16_MAX);
362 info.totalEntriesCache2 = (UINT16)settings->BitmapCacheV2CellInfo[2].numEntries;
364 WINPR_ASSERT(settings->BitmapCacheV2CellInfo[3].numEntries <= UINT16_MAX);
365 info.totalEntriesCache3 = (UINT16)settings->BitmapCacheV2CellInfo[3].numEntries;
367 WINPR_ASSERT(settings->BitmapCacheV2CellInfo[4].numEntries <= UINT16_MAX);
368 info.totalEntriesCache4 = (UINT16)settings->BitmapCacheV2CellInfo[4].numEntries;
370 info.numEntriesCache0 = MIN(keyCount, info.totalEntriesCache0);
371 keyCount -= info.numEntriesCache0;
372 info.numEntriesCache1 = MIN(keyCount, info.totalEntriesCache1);
373 keyCount -= info.numEntriesCache1;
374 info.numEntriesCache2 = MIN(keyCount, info.totalEntriesCache2);
375 keyCount -= info.numEntriesCache2;
376 info.numEntriesCache3 = MIN(keyCount, info.totalEntriesCache3);
377 keyCount -= info.numEntriesCache3;
378 info.numEntriesCache4 = MIN(keyCount, info.totalEntriesCache4);
380 info.totalEntriesCache0 = info.numEntriesCache0;
381 info.totalEntriesCache1 = info.numEntriesCache1;
382 info.totalEntriesCache2 = info.numEntriesCache2;
383 info.totalEntriesCache3 = info.numEntriesCache3;
384 info.totalEntriesCache4 = info.numEntriesCache4;
386 keyCount = info.totalEntriesCache0 + info.totalEntriesCache1 + info.totalEntriesCache2 +
387 info.totalEntriesCache3 + info.totalEntriesCache4;
389 info.keyCount = keyCount;
390 info.keyList = keyList;
392 WLog_DBG(TAG,
"persistentKeyList count: %" PRIu32, info.keyCount);
395 "numEntriesCache: [0]: %" PRIu16
" [1]: %" PRIu16
" [2]: %" PRIu16
" [3]: %" PRIu16
397 info.numEntriesCache0, info.numEntriesCache1, info.numEntriesCache2,
398 info.numEntriesCache3, info.numEntriesCache4);
401 "totalEntriesCache: [0]: %" PRIu16
" [1]: %" PRIu16
" [2]: %" PRIu16
" [3]: %" PRIu16
403 info.totalEntriesCache0, info.totalEntriesCache1, info.totalEntriesCache2,
404 info.totalEntriesCache3, info.totalEntriesCache4);
406 wStream* s = rdp_data_pdu_init(rdp);
414 if (!rdp_write_client_persistent_key_list_pdu(s, &info))
416 Stream_Free(s, TRUE);
421 WINPR_ASSERT(rdp->mcs);
424 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_BITMAP_CACHE_PERSISTENT_LIST, rdp->mcs->userId);
427 BOOL rdp_recv_client_font_list_pdu(
wStream* s)
431 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
434 return Stream_SafeSeek(s, 8);
437 BOOL rdp_recv_client_persistent_key_list_pdu(
wStream* s)
447 if (!Stream_CheckAndLogRequiredLength(TAG, s, 21))
449 WLog_ERR(TAG,
"short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need 21 bytes, got %" PRIuz,
450 Stream_GetRemainingLength(s));
454 for (
size_t x = 0; x < 5; x++)
456 Stream_Read_UINT16(s, cache);
461 for (
size_t x = 0; x < 5; x++)
464 Stream_Read_UINT16(s, tmp);
471 "TS_BITMAPCACHE_PERSISTENT_LIST_PDU::totalEntriesCacheX exceeds 262144 entries");
475 Stream_Read_UINT8(s, flags);
476 if ((flags & ~(PERSIST_LAST_PDU | PERSIST_FIRST_PDU)) != 0)
479 "TS_BITMAPCACHE_PERSISTENT_LIST_PDU::bBitMask has an invalid value of 0x%02" PRIx8,
485 if (!Stream_SafeSeek(s, 3))
487 WLog_ERR(TAG,
"short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need 3 bytes, got %" PRIuz,
488 Stream_GetRemainingLength(s));
492 if (!Stream_SafeSeek(s, count *
sizeof(UINT64)))
495 "short TS_BITMAPCACHE_PERSISTENT_LIST_PDU, need %" PRIuz
" bytes, got %" PRIuz,
496 count *
sizeof(UINT64), Stream_GetRemainingLength(s));
502 static BOOL rdp_write_client_font_list_pdu(
wStream* s, UINT16 flags)
506 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
508 Stream_Write_UINT16(s, 0);
509 Stream_Write_UINT16(s, 0);
510 Stream_Write_UINT16(s, flags);
511 Stream_Write_UINT16(s, 50);
515 BOOL rdp_send_client_font_list_pdu(rdpRdp* rdp, UINT16 flags)
517 wStream* s = rdp_data_pdu_init(rdp);
520 if (!rdp_write_client_font_list_pdu(s, flags))
522 Stream_Free(s, TRUE);
526 WINPR_ASSERT(rdp->mcs);
527 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_FONT_LIST, rdp->mcs->userId);
530 BOOL rdp_recv_font_map_pdu(rdpRdp* rdp,
wStream* s)
532 UINT16 numberEntries = 0;
533 UINT16 totalNumEntries = 0;
535 UINT16 entrySize = 0;
538 WINPR_ASSERT(rdp->settings);
543 if (Stream_CheckAndLogRequiredLength(TAG, s, 8))
545 Stream_Read_UINT16(s, numberEntries);
546 if (numberEntries != 0)
549 "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::numberEntries != 0 "
552 Stream_Read_UINT16(s, totalNumEntries);
553 if (totalNumEntries != 0)
556 "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::totalNumEntries != "
559 Stream_Read_UINT16(s, mapFlags);
560 if (mapFlags != (FONTLIST_FIRST | FONTLIST_LAST))
563 "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::mapFlags != 0x0003 "
564 "(FONTLIST_FIRST | FONTLIST_LAST) "
567 Stream_Read_UINT16(s, entrySize);
570 "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU)::entrySize != 4 "
576 "[MS-RDPBCGR] 2.2.1.22.1 Font Map PDU Data (TS_FONT_MAP_PDU) paylaod size is "
579 return rdp_finalize_set_flag(rdp, FINALIZE_SC_FONT_MAP_PDU);
582 BOOL rdp_send_server_font_map_pdu(rdpRdp* rdp)
584 wStream* s = rdp_data_pdu_init(rdp);
587 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 8))
589 Stream_Free(s, TRUE);
592 Stream_Write_UINT16(s, 0);
593 Stream_Write_UINT16(s, 0);
594 Stream_Write_UINT16(s, FONTLIST_FIRST | FONTLIST_LAST);
595 Stream_Write_UINT16(s, 4);
597 WINPR_ASSERT(rdp->mcs);
598 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_FONT_MAP, rdp->mcs->userId);
601 BOOL rdp_recv_deactivate_all(rdpRdp* rdp,
wStream* s)
603 UINT16 lengthSourceDescriptor = 0;
608 if (rdp_get_state(rdp) == CONNECTION_STATE_ACTIVE)
610 if (!rdp_finalize_set_flag(rdp, FINALIZE_DEACTIVATE_REACTIVATE))
613 rdp->was_deactivated = TRUE;
623 WINPR_ASSERT(rdp->settings);
624 if (Stream_GetRemainingLength(s) > 0)
629 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
632 Stream_Read_UINT32(s, ShareId);
636 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
639 Stream_Read_UINT16(s, lengthSourceDescriptor);
641 if (!Stream_CheckAndLogRequiredLength(TAG, s, lengthSourceDescriptor))
644 Stream_Seek(s, lengthSourceDescriptor);
648 return rdp_client_transition_to_state(rdp,
649 CONNECTION_STATE_CAPABILITIES_EXCHANGE_DEMAND_ACTIVE);
652 BOOL rdp_send_deactivate_all(rdpRdp* rdp)
654 wStream* s = rdp_send_stream_pdu_init(rdp);
660 if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 7))
663 WINPR_ASSERT(rdp->settings);
665 Stream_Write_UINT32(s, ShareId);
666 Stream_Write_UINT16(s, 1);
667 Stream_Write_UINT8(s, 0);
669 WINPR_ASSERT(rdp->mcs);
670 status = rdp_send_pdu(rdp, s, PDU_TYPE_DEACTIVATE_ALL, rdp->mcs->userId);
676 BOOL rdp_server_accept_client_control_pdu(rdpRdp* rdp,
wStream* s)
680 UINT32 ControlId = 0;
681 const CONNECTION_STATE state = rdp_get_state(rdp);
686 if (!rdp_recv_control_pdu(s, &action, &GrantId, &ControlId))
692 case CTRLACTION_REQUEST_CONTROL:
693 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU))
695 char abuffer[128] = { 0 };
696 char buffer[1024] = { 0 };
698 "Received action=%s with GrantId=0x%04" PRIx16
", ControlId=0x%08" PRIx32
699 " in unexpected state %s [missing %s]",
700 rdp_ctrlaction_string(action, abuffer,
sizeof(abuffer)), GrantId,
701 ControlId, rdp_state_string(state),
702 rdp_finalize_flags_to_str(FINALIZE_CS_CONTROL_COOPERATE_PDU, buffer,
706 if ((GrantId != 0) || (ControlId != 0))
709 "Received CTRLACTION_COOPERATE with GrantId=0x%04" PRIx16
710 " != 0x00, ControlId=0x%08" PRIx32
" != 0x00",
714 return rdp_finalize_set_flag(rdp, FINALIZE_CS_CONTROL_REQUEST_PDU);
715 case CTRLACTION_COOPERATE:
716 if (!rdp_finalize_is_flag_set(rdp, FINALIZE_CS_SYNCHRONIZE_PDU))
718 char abuffer[128] = { 0 };
719 char buffer[1024] = { 0 };
722 "Received action=%s with GrantId=0x%04" PRIx16
", ControlId=0x%08" PRIx32
723 " in unexpected state %s [missing %s]",
724 rdp_ctrlaction_string(action, abuffer,
sizeof(abuffer)), GrantId, ControlId,
725 rdp_state_string(state),
726 rdp_finalize_flags_to_str(FINALIZE_CS_SYNCHRONIZE_PDU, buffer,
sizeof(buffer)));
729 if ((GrantId != 0) || (ControlId != 0))
732 "Received CTRLACTION_COOPERATE with GrantId=0x%04" PRIx16
733 " != 0x00, ControlId=0x%08" PRIx32
" != 0x00",
737 return rdp_finalize_set_flag(rdp, FINALIZE_CS_CONTROL_COOPERATE_PDU);
740 char abuffer[128] = { 0 };
742 "Received unexpected action=%s with GrantId=0x%04" PRIx16
743 ", ControlId=0x%08" PRIx32,
744 rdp_ctrlaction_string(action, abuffer,
sizeof(abuffer)), GrantId, ControlId);
752 BOOL rdp_server_accept_client_font_list_pdu(rdpRdp* rdp,
wStream* s)
754 rdpSettings* settings = NULL;
755 freerdp_peer* peer = NULL;
760 settings = rdp->settings;
761 WINPR_ASSERT(settings);
763 WINPR_ASSERT(rdp->context);
764 peer = rdp->context->peer;
767 if (!rdp_recv_client_font_list_pdu(s))
769 rdp_finalize_set_flag(rdp, FINALIZE_CS_FONT_LIST_PDU);
771 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_FINALIZATION_CLIENT_FONT_MAP))
774 if (!rdp_send_server_font_map_pdu(rdp))
777 if (!rdp_server_transition_to_state(rdp, CONNECTION_STATE_ACTIVE))
783 BOOL rdp_server_accept_client_persistent_key_list_pdu(rdpRdp* rdp,
wStream* s)
788 if (!rdp_recv_client_persistent_key_list_pdu(s))
791 rdp_finalize_set_flag(rdp, FINALIZE_CS_PERSISTENT_KEY_LIST_PDU);
796 const char* rdp_ctrlaction_string(UINT16 action,
char* buffer,
size_t size)
798 const char* actstr = NULL;
801 case CTRLACTION_COOPERATE:
802 actstr =
"CTRLACTION_COOPERATE";
804 case CTRLACTION_DETACH:
805 actstr =
"CTRLACTION_DETACH";
807 case CTRLACTION_GRANTED_CONTROL:
808 actstr =
"CTRLACTION_GRANTED_CONTROL";
810 case CTRLACTION_REQUEST_CONTROL:
811 actstr =
"CTRLACTION_REQUEST_CONTROL";
814 actstr =
"CTRLACTION_UNKNOWN";
818 (void)_snprintf(buffer, size,
"%s [0x%04" PRIx16
"]", actstr, action);
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.