2011-07-02 22:40:03 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-03 01:10:10 +04:00
|
|
|
* Transport Packets (TPKTs)
|
2011-07-02 22:40:03 +04:00
|
|
|
*
|
|
|
|
* Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-07-03 01:10:10 +04:00
|
|
|
#include "tpdu.h"
|
|
|
|
|
2011-07-02 22:40:03 +04:00
|
|
|
#include "tpkt.h"
|
|
|
|
|
2020-02-19 19:00:36 +03:00
|
|
|
#include <winpr/wlog.h>
|
|
|
|
|
2020-02-21 11:17:00 +03:00
|
|
|
#define TAG FREERDP_TAG("core.tpkt")
|
2020-02-19 19:00:36 +03:00
|
|
|
|
2011-07-02 22:40:03 +04:00
|
|
|
/**
|
2011-07-03 01:10:10 +04:00
|
|
|
* TPKTs are defined in:
|
2011-07-02 22:40:03 +04:00
|
|
|
*
|
|
|
|
* http://tools.ietf.org/html/rfc1006/
|
|
|
|
* RFC 1006 - ISO Transport Service on top of the TCP
|
|
|
|
*
|
|
|
|
* http://www.itu.int/rec/T-REC-T.123/
|
|
|
|
* ITU-T T.123 (01/2007) - Network-specific data protocol stacks for multimedia conferencing
|
|
|
|
*
|
|
|
|
* TPKT Header
|
|
|
|
* ____________________ byte
|
|
|
|
* | |
|
|
|
|
* | 3 (version) | 1
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | Reserved | 2
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | Length (MSB) | 3
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
|
|
|
* | Length (LSB) | 4
|
|
|
|
* |____________________|
|
|
|
|
* | |
|
2011-07-03 01:10:10 +04:00
|
|
|
* | X.224 TPDU | 5 - ?
|
2011-07-02 22:40:03 +04:00
|
|
|
* ....
|
2011-07-03 01:10:10 +04:00
|
|
|
*
|
|
|
|
* A TPKT header is of fixed length 4, and the following X.224 TPDU is at least three bytes long.
|
|
|
|
* Therefore, the minimum TPKT length is 7, and the maximum TPKT length is 65535. Because the TPKT
|
|
|
|
* length includes the TPKT header (4 bytes), the maximum X.224 TPDU length is 65531.
|
2011-07-02 22:40:03 +04:00
|
|
|
*/
|
|
|
|
|
2011-08-09 12:40:58 +04:00
|
|
|
/**
|
2022-12-09 16:35:03 +03:00
|
|
|
* Verify if a packet has valid TPKT header.
|
|
|
|
*
|
|
|
|
* @param s A stream to read from
|
|
|
|
*
|
|
|
|
* @return \b TRUE for success, \b FALSE otherwise
|
2011-08-09 12:40:58 +04:00
|
|
|
*/
|
|
|
|
|
2022-10-21 10:19:29 +03:00
|
|
|
int tpkt_verify_header(wStream* s)
|
2011-08-09 12:40:58 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE version = 0;
|
2011-08-09 12:40:58 +04:00
|
|
|
|
2022-10-21 10:19:29 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
|
|
|
return -1;
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Peek_UINT8(s, version);
|
2012-11-13 20:06:33 +04:00
|
|
|
|
2011-08-09 12:40:58 +04:00
|
|
|
if (version == 3)
|
2022-10-21 10:19:29 +03:00
|
|
|
return 1;
|
2011-08-09 12:40:58 +04:00
|
|
|
else
|
2022-10-21 10:19:29 +03:00
|
|
|
return 0;
|
2011-08-09 12:40:58 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
2022-12-09 16:35:03 +03:00
|
|
|
* Read a TPKT header.
|
|
|
|
*
|
|
|
|
* @param s A stream to read from
|
|
|
|
* @param length A pointer to the result, must not be NULL
|
|
|
|
*
|
|
|
|
* @return \b TRUE for success, \b FALSE otherwise
|
2011-07-05 05:35:32 +04:00
|
|
|
*/
|
|
|
|
|
2017-05-29 11:50:22 +03:00
|
|
|
BOOL tpkt_read_header(wStream* s, UINT16* length)
|
2011-07-02 22:40:03 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE version = 0;
|
2017-05-29 11:50:22 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2017-05-29 11:50:22 +03:00
|
|
|
return FALSE;
|
2011-07-02 22:40:03 +04:00
|
|
|
|
2013-05-09 00:27:21 +04:00
|
|
|
Stream_Peek_UINT8(s, version);
|
2011-07-02 22:40:03 +04:00
|
|
|
|
|
|
|
if (version == 3)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
UINT16 len = 0;
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
|
2017-05-29 11:50:22 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Seek(s, 2);
|
2017-05-29 11:50:22 +03:00
|
|
|
Stream_Read_UINT16_BE(s, len);
|
2020-02-19 19:00:36 +03:00
|
|
|
|
|
|
|
/* ITU-T Rec. T.123 8 Packet header to delimit data units in an octet stream */
|
|
|
|
if (len < 7)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "TPKT header too short, require minimum of 7 bytes, got %" PRId16, len);
|
2017-05-29 11:50:22 +03:00
|
|
|
return FALSE;
|
2020-02-19 19:00:36 +03:00
|
|
|
}
|
2017-05-29 11:50:22 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, len - 4))
|
2020-02-19 19:00:36 +03:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WLog_ERR(TAG, "TPKT header length %" PRIu16 ", but received less", len);
|
2020-02-19 19:00:36 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
2017-05-29 11:50:22 +03:00
|
|
|
*length = len;
|
2011-07-02 22:40:03 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* not a TPKT header */
|
2017-05-29 11:50:22 +03:00
|
|
|
*length = 0;
|
2011-07-02 22:40:03 +04:00
|
|
|
}
|
|
|
|
|
2017-05-29 11:50:22 +03:00
|
|
|
return TRUE;
|
2011-07-02 22:40:03 +04:00
|
|
|
}
|
|
|
|
|
2024-01-11 13:13:34 +03:00
|
|
|
BOOL tpkt_ensure_stream_consumed_(wStream* s, size_t length, const char* fkt)
|
2020-02-20 10:22:19 +03:00
|
|
|
{
|
2024-01-11 13:13:34 +03:00
|
|
|
if (length > UINT16_MAX)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "[%s] length %" PRIuz " > %" PRIu16, fkt, length, UINT16_MAX);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2020-02-20 10:22:19 +03:00
|
|
|
size_t rem = Stream_GetRemainingLength(s);
|
|
|
|
if (rem > 0)
|
|
|
|
{
|
2020-03-05 13:16:56 +03:00
|
|
|
WLog_ERR(TAG,
|
|
|
|
"[%s] Received invalid TPKT header length %" PRIu16 ", %" PRIdz " bytes too long!",
|
|
|
|
fkt, length, rem);
|
2020-02-20 10:22:19 +03:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
2022-12-09 16:35:03 +03:00
|
|
|
* Write a TPKT header.
|
|
|
|
*
|
|
|
|
* @param s A stream to write to
|
|
|
|
* @param length The value to write
|
|
|
|
*
|
|
|
|
* @return \b TRUE for success, \b FALSE otherwise
|
2011-07-05 05:35:32 +04:00
|
|
|
*/
|
|
|
|
|
2024-09-03 15:38:45 +03:00
|
|
|
BOOL tpkt_write_header(wStream* s, size_t length)
|
2011-07-02 22:40:03 +04:00
|
|
|
{
|
2023-01-24 14:19:56 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredCapacity(TAG, (s), 4))
|
2020-11-19 17:21:49 +03:00
|
|
|
return FALSE;
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write_UINT8(s, 3); /* version */
|
|
|
|
Stream_Write_UINT8(s, 0); /* reserved */
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16_BE(s, length); /* length */
|
2020-11-19 17:21:49 +03:00
|
|
|
return TRUE;
|
2011-07-02 22:40:03 +04:00
|
|
|
}
|