Blame SOURCES/Implement-missing-TSG-debug-functions.patch

08a176
From ea20ba13ec764b30d6642cd570929e71e97d00f1 Mon Sep 17 00:00:00 2001
08a176
From: akallabeth <akallabeth@posteo.net>
08a176
Date: Tue, 12 Oct 2021 13:33:41 +0200
08a176
Subject: [PATCH] Implemented missing TSG debug functions
08a176
08a176
(cherry picked from commit c06c4638061b0305a269dbb8f7c87009cf886f02)
08a176
---
08a176
 libfreerdp/core/gateway/tsg.c | 330 ++++++++++++++++++++++++++++++++--
08a176
 1 file changed, 313 insertions(+), 17 deletions(-)
08a176
08a176
diff --git a/libfreerdp/core/gateway/tsg.c b/libfreerdp/core/gateway/tsg.c
08a176
index 1626b91..3376fb0 100644
08a176
--- a/libfreerdp/core/gateway/tsg.c
08a176
+++ b/libfreerdp/core/gateway/tsg.c
08a176
@@ -24,6 +24,7 @@
08a176
 #include "config.h"
08a176
 #endif
08a176
 
08a176
+#include <winpr/assert.h>
08a176
 #include <winpr/crt.h>
08a176
 #include <winpr/ndr.h>
08a176
 #include <winpr/error.h>
08a176
@@ -39,13 +40,14 @@
08a176
 
08a176
 #define TAG FREERDP_TAG("core.gateway.tsg")
08a176
 
08a176
+#define TSG_CAPABILITY_TYPE_NAP 0x00000001
08a176
+
08a176
 #define TSG_PACKET_TYPE_HEADER 0x00004844
08a176
 #define TSG_PACKET_TYPE_VERSIONCAPS 0x00005643
08a176
 #define TSG_PACKET_TYPE_QUARCONFIGREQUEST 0x00005143
08a176
 #define TSG_PACKET_TYPE_QUARREQUEST 0x00005152
08a176
 #define TSG_PACKET_TYPE_RESPONSE 0x00005052
08a176
 #define TSG_PACKET_TYPE_QUARENC_RESPONSE 0x00004552
08a176
-#define TSG_CAPABILITY_TYPE_NAP 0x00000001
08a176
 #define TSG_PACKET_TYPE_CAPS_RESPONSE 0x00004350
08a176
 #define TSG_PACKET_TYPE_MSGREQUEST_PACKET 0x00004752
08a176
 #define TSG_PACKET_TYPE_MESSAGE_PACKET 0x00004750
08a176
@@ -308,16 +310,35 @@ static BOOL tsg_print(char** buffer, size_t* len, const char* fmt, ...)
08a176
 static BOOL tsg_packet_header_to_string(char** buffer, size_t* length,
08a176
                                         const TSG_PACKET_HEADER* header)
08a176
 {
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(header);
08a176
+
08a176
 	return tsg_print(buffer, length,
08a176
 	                 "header { ComponentId=0x%04" PRIx16 ", PacketId=0x%04" PRIx16 " }",
08a176
 	                 header->ComponentId, header->PacketId);
08a176
 }
08a176
 
08a176
+static BOOL tsg_type_capability_nap_to_string(char** buffer, size_t* length,
08a176
+                                              const TSG_CAPABILITY_NAP* cur)
08a176
+{
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(cur);
08a176
+
08a176
+	return tsg_print(buffer, length, "%s { capabilities=0x%08" PRIx32 " }",
08a176
+	                 tsg_packet_id_to_string(TSG_CAPABILITY_TYPE_NAP), cur->capabilities);
08a176
+}
08a176
+
08a176
 static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
08a176
                                               const TSG_PACKET_CAPABILITIES* caps, UINT32 numCaps)
08a176
 {
08a176
 	UINT32 x;
08a176
 
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
 	if (!tsg_print(buffer, length, "capabilities { "))
08a176
 		return FALSE;
08a176
 
08a176
@@ -327,9 +348,7 @@ static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
08a176
 		switch (cur->capabilityType)
08a176
 		{
08a176
 			case TSG_CAPABILITY_TYPE_NAP:
08a176
-				if (!tsg_print(buffer, length, "%s { capabilities=0x%08" PRIx32 " }",
08a176
-				               tsg_packet_id_to_string(cur->capabilityType),
08a176
-				               cur->tsgPacket.tsgCapNap.capabilities))
08a176
+				if (!tsg_type_capability_nap_to_string(buffer, length, &cur->tsgPacket.tsgCapNap))
08a176
 					return FALSE;
08a176
 				break;
08a176
 			default:
08a176
@@ -344,6 +363,10 @@ static BOOL tsg_packet_capabilities_to_string(char** buffer, size_t* length,
08a176
 static BOOL tsg_packet_versioncaps_to_string(char** buffer, size_t* length,
08a176
                                              const TSG_PACKET_VERSIONCAPS* caps)
08a176
 {
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
 	if (!tsg_print(buffer, length, "versioncaps { "))
08a176
 		return FALSE;
08a176
 	if (!tsg_packet_header_to_string(buffer, length, &caps->tsgHeader))
08a176
@@ -365,6 +388,277 @@ static BOOL tsg_packet_versioncaps_to_string(char** buffer, size_t* length,
08a176
 	return tsg_print(buffer, length, " }");
08a176
 }
08a176
 
08a176
+static BOOL tsg_packet_quarconfigrequest_to_string(char** buffer, size_t* length,
08a176
+                                                   const TSG_PACKET_QUARCONFIGREQUEST* caps)
08a176
+{
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "quarconfigrequest { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " flags=0x%08" PRIx32, caps->flags))
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_quarrequest_to_string(char** buffer, size_t* length,
08a176
+                                             const TSG_PACKET_QUARREQUEST* caps)
08a176
+{
08a176
+	BOOL rc = FALSE;
08a176
+	char* name = NULL;
08a176
+	char* strdata = NULL;
08a176
+
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "quarrequest { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (caps->nameLength > 0)
08a176
+	{
08a176
+		if (ConvertFromUnicode(CP_UTF8, 0, caps->machineName, caps->nameLength, &name, 0, NULL,
08a176
+		                       NULL) < 0)
08a176
+			return FALSE;
08a176
+	}
08a176
+
08a176
+	strdata = winpr_BinToHexString(caps->data, caps->dataLen, TRUE);
08a176
+	if (strdata || (caps->dataLen == 0))
08a176
+		rc = tsg_print(buffer, length,
08a176
+		               " flags=0x%08" PRIx32 ", machineName=%s [%" PRIu32 "], data[%" PRIu32 "]=%s",
08a176
+		               caps->flags, name, caps->nameLength, caps->dataLen, strdata);
08a176
+	free(name);
08a176
+	free(strdata);
08a176
+	if (!rc)
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static const char* tsg_bool_to_string(BOOL val)
08a176
+{
08a176
+	if (val)
08a176
+		return "true";
08a176
+	return "false";
08a176
+}
08a176
+
08a176
+static const char* tsg_redirection_flags_to_string(char* buffer, size_t size,
08a176
+                                                   const TSG_REDIRECTION_FLAGS* flags)
08a176
+{
08a176
+	WINPR_ASSERT(buffer || (size == 0));
08a176
+	WINPR_ASSERT(flags);
08a176
+
08a176
+	_snprintf(buffer, size,
08a176
+	          "enableAllRedirections=%s,  disableAllRedirections=%s, driveRedirectionDisabled=%s, "
08a176
+	          "printerRedirectionDisabled=%s, portRedirectionDisabled=%s, reserved=%s, "
08a176
+	          "clipboardRedirectionDisabled=%s, pnpRedirectionDisabled=%s",
08a176
+	          tsg_bool_to_string(flags->enableAllRedirections),
08a176
+	          tsg_bool_to_string(flags->disableAllRedirections),
08a176
+	          tsg_bool_to_string(flags->driveRedirectionDisabled),
08a176
+	          tsg_bool_to_string(flags->printerRedirectionDisabled),
08a176
+	          tsg_bool_to_string(flags->portRedirectionDisabled),
08a176
+	          tsg_bool_to_string(flags->reserved),
08a176
+	          tsg_bool_to_string(flags->clipboardRedirectionDisabled),
08a176
+	          tsg_bool_to_string(flags->pnpRedirectionDisabled));
08a176
+	return buffer;
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_response_to_string(char** buffer, size_t* length,
08a176
+                                          const TSG_PACKET_RESPONSE* caps)
08a176
+{
08a176
+	BOOL rc = FALSE;
08a176
+	char* strdata = NULL;
08a176
+	char tbuffer[8192] = { 0 };
08a176
+
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "response { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " "))
08a176
+		return FALSE;
08a176
+
08a176
+	strdata = winpr_BinToHexString(caps->responseData, caps->responseDataLen, TRUE);
08a176
+	if (strdata || (caps->responseDataLen == 0))
08a176
+		rc = tsg_print(
08a176
+		    buffer, length,
08a176
+		    " flags=0x%08" PRIx32 ", reserved=0x%08" PRIx32 ", responseData[%" PRIu32
08a176
+		    "]=%s, redirectionFlags={ %s }",
08a176
+		    caps->flags, caps->reserved, caps->responseDataLen, strdata,
08a176
+		    tsg_redirection_flags_to_string(tbuffer, ARRAYSIZE(tbuffer), &caps->redirectionFlags));
08a176
+	free(strdata);
08a176
+	if (!rc)
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_quarenc_response_to_string(char** buffer, size_t* length,
08a176
+                                                  const TSG_PACKET_QUARENC_RESPONSE* caps)
08a176
+{
08a176
+	BOOL rc = FALSE;
08a176
+	char* strdata = NULL;
08a176
+	RPC_CSTR uuid;
08a176
+	char tbuffer[8192] = { 0 };
08a176
+	size_t size = ARRAYSIZE(tbuffer);
08a176
+	char* ptbuffer = tbuffer;
08a176
+
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "quarenc_response { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (caps->certChainLen > 0)
08a176
+	{
08a176
+		if (ConvertFromUnicode(CP_UTF8, 0, caps->certChainData, caps->certChainLen, &strdata, 0,
08a176
+		                       NULL, NULL) <= 0)
08a176
+			return FALSE;
08a176
+	}
08a176
+
08a176
+	tsg_packet_versioncaps_to_string(&ptbuffer, &size, caps->versionCaps);
08a176
+	UuidToStringA(&caps->nonce, &uuid);
08a176
+	if (strdata || (caps->certChainLen == 0))
08a176
+		rc =
08a176
+		    tsg_print(buffer, length,
08a176
+		              " flags=0x%08" PRIx32 ", certChain[%" PRIu32 "]=%s, nonce=%s, versionCaps=%s",
08a176
+		              caps->flags, caps->certChainLen, strdata, uuid, tbuffer);
08a176
+	free(strdata);
08a176
+	free(uuid);
08a176
+	if (!rc)
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_message_response_to_string(char** buffer, size_t* length,
08a176
+                                                  const TSG_PACKET_MSG_RESPONSE* caps)
08a176
+{
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "msg_response { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length,
08a176
+	               " msgID=0x%08" PRIx32 ", msgType=0x%08" PRIx32 ", isMsgPresent=%" PRId32,
08a176
+	               caps->msgID, caps->msgType, caps->isMsgPresent))
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_caps_response_to_string(char** buffer, size_t* length,
08a176
+                                               const TSG_PACKET_CAPS_RESPONSE* caps)
08a176
+{
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "caps_response { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_packet_quarenc_response_to_string(buffer, length, &caps->pktQuarEncResponse))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_packet_message_response_to_string(buffer, length, &caps->pktConsentMessage))
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_message_request_to_string(char** buffer, size_t* length,
08a176
+                                                 const TSG_PACKET_MSG_REQUEST* caps)
08a176
+{
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "caps_message_request { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " maxMessagesPerBatch=%" PRIu32, caps->maxMessagesPerBatch))
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_auth_to_string(char** buffer, size_t* length, const TSG_PACKET_AUTH* caps)
08a176
+{
08a176
+	BOOL rc = FALSE;
08a176
+	char* strdata = NULL;
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "caps_message_request { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_packet_versioncaps_to_string(buffer, length, &caps->tsgVersionCaps))
08a176
+		return FALSE;
08a176
+
08a176
+	strdata = winpr_BinToHexString(caps->cookie, caps->cookieLen, TRUE);
08a176
+	if (strdata || (caps->cookieLen == 0))
08a176
+		rc = tsg_print(buffer, length, " cookie[%" PRIu32 "]=%s", caps->cookieLen, strdata);
08a176
+	free(strdata);
08a176
+	if (!rc)
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
+static BOOL tsg_packet_reauth_to_string(char** buffer, size_t* length,
08a176
+                                        const TSG_PACKET_REAUTH* caps)
08a176
+{
08a176
+	BOOL rc = FALSE;
08a176
+	WINPR_ASSERT(buffer);
08a176
+	WINPR_ASSERT(length);
08a176
+	WINPR_ASSERT(caps);
08a176
+
08a176
+	if (!tsg_print(buffer, length, "caps_message_request { "))
08a176
+		return FALSE;
08a176
+
08a176
+	if (!tsg_print(buffer, length, " tunnelContext=0x%08" PRIx32 ", packetId=%s [0x%08" PRIx32 "]",
08a176
+	               caps->tunnelContext, tsg_packet_id_to_string(caps->packetId), caps->packetId))
08a176
+		return FALSE;
08a176
+
08a176
+	switch (caps->packetId)
08a176
+	{
08a176
+		case TSG_PACKET_TYPE_VERSIONCAPS:
08a176
+			rc = tsg_packet_versioncaps_to_string(buffer, length,
08a176
+			                                      caps->tsgInitialPacket.packetVersionCaps);
08a176
+			break;
08a176
+		case TSG_PACKET_TYPE_AUTH:
08a176
+			rc = tsg_packet_auth_to_string(buffer, length, caps->tsgInitialPacket.packetAuth);
08a176
+			break;
08a176
+		default:
08a176
+			rc = tsg_print(buffer, length, "TODO: Unhandled packet type %s [0x%08" PRIx32 "]",
08a176
+			               tsg_packet_id_to_string(caps->packetId), caps->packetId);
08a176
+			break;
08a176
+	}
08a176
+
08a176
+	if (!rc)
08a176
+		return FALSE;
08a176
+
08a176
+	return tsg_print(buffer, length, " }");
08a176
+}
08a176
+
08a176
 static const char* tsg_packet_to_string(const TSG_PACKET* packet)
08a176
 {
08a176
 	size_t len = 8192;
08a176
@@ -387,43 +681,45 @@ static const char* tsg_packet_to_string(const TSG_PACKET* packet)
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_QUARCONFIGREQUEST:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_quarconfigrequest_to_string(&buffer, &len,
08a176
+			                                            packet->tsgPacket.packetQuarConfigRequest))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_QUARREQUEST:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_quarrequest_to_string(&buffer, &len,
08a176
+			                                      packet->tsgPacket.packetQuarRequest))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_RESPONSE:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_response_to_string(&buffer, &len, packet->tsgPacket.packetResponse))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_QUARENC_RESPONSE:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
-				goto fail;
08a176
-			break;
08a176
-		case TSG_CAPABILITY_TYPE_NAP:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_quarenc_response_to_string(&buffer, &len,
08a176
+			                                           packet->tsgPacket.packetQuarEncResponse))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_CAPS_RESPONSE:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_caps_response_to_string(&buffer, &len,
08a176
+			                                        packet->tsgPacket.packetCapsResponse))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_MSGREQUEST_PACKET:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_message_request_to_string(&buffer, &len,
08a176
+			                                          packet->tsgPacket.packetMsgRequest))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_MESSAGE_PACKET:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_message_response_to_string(&buffer, &len,
08a176
+			                                           packet->tsgPacket.packetMsgResponse))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_AUTH:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_auth_to_string(&buffer, &len, packet->tsgPacket.packetAuth))
08a176
 				goto fail;
08a176
 			break;
08a176
 		case TSG_PACKET_TYPE_REAUTH:
08a176
-			if (!tsg_print(&buffer, &len, "TODO"))
08a176
+			if (!tsg_packet_reauth_to_string(&buffer, &len, packet->tsgPacket.packetReauth))
08a176
 				goto fail;
08a176
 			break;
08a176
 		default: