2011-07-04 06:47:35 +04:00
|
|
|
/**
|
2012-02-21 09:56:55 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-04 06:47:35 +04:00
|
|
|
* ASN.1 Basic Encoding Rules (BER)
|
|
|
|
*
|
2012-02-21 09:56:55 +04:00
|
|
|
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2011-07-04 06:47:35 +04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2013-01-20 02:32:04 +04:00
|
|
|
#include <stdio.h>
|
2022-04-19 15:29:17 +03:00
|
|
|
#include <winpr/assert.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/crt.h>
|
2022-01-22 17:01:43 +03:00
|
|
|
#include <winpr/string.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#include <freerdp/log.h>
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <freerdp/crypto/ber.h>
|
2011-07-04 06:47:35 +04:00
|
|
|
|
2014-09-12 16:36:29 +04:00
|
|
|
#define TAG FREERDP_TAG("crypto")
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_length(wStream* s, size_t* length)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(length);
|
|
|
|
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (byte & 0x80)
|
|
|
|
{
|
2011-07-13 05:43:52 +04:00
|
|
|
byte &= ~(0x80);
|
2011-07-13 07:50:51 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, byte))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
|
|
|
|
2011-07-13 07:50:51 +04:00
|
|
|
if (byte == 1)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, *length);
|
2013-01-14 13:13:29 +04:00
|
|
|
else if (byte == 2)
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16_BE(s, *length);
|
2013-01-11 04:11:03 +04:00
|
|
|
else
|
2023-01-18 13:07:58 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "ber: unexpected byte 0x%02" PRIx8 ", expected [1,2]", byte);
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2023-01-18 13:07:58 +03:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*length = byte;
|
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
return TRUE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write BER length.
|
|
|
|
* @param s stream
|
|
|
|
* @param length length
|
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_length(wStream* s, size_t length)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
if (length > 0xFF)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2023-07-28 15:03:14 +03:00
|
|
|
WINPR_ASSERT(length <= UINT16_MAX);
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 3);
|
2013-05-21 19:06:00 +04:00
|
|
|
Stream_Write_UINT8(s, 0x80 ^ 2);
|
2023-07-28 15:03:14 +03:00
|
|
|
Stream_Write_UINT16_BE(s, (UINT16)length);
|
2011-07-18 22:56:08 +04:00
|
|
|
return 3;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2023-07-28 15:03:14 +03:00
|
|
|
WINPR_ASSERT(length <= UINT8_MAX);
|
2013-05-21 19:06:00 +04:00
|
|
|
if (length > 0x7F)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 2);
|
2013-05-21 19:06:00 +04:00
|
|
|
Stream_Write_UINT8(s, 0x80 ^ 1);
|
2023-07-28 15:03:14 +03:00
|
|
|
Stream_Write_UINT8(s, (UINT8)length);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 2;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
|
2023-07-28 15:03:14 +03:00
|
|
|
Stream_Write_UINT8(s, (UINT8)length);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t _ber_sizeof_length(size_t length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
if (length > 0xFF)
|
2011-07-18 22:56:08 +04:00
|
|
|
return 3;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
if (length > 0x7F)
|
|
|
|
return 2;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1;
|
2011-07-19 00:16:37 +04:00
|
|
|
}
|
|
|
|
|
2011-07-13 05:43:52 +04:00
|
|
|
/**
|
|
|
|
* Read BER Universal tag.
|
2022-12-09 16:35:03 +03:00
|
|
|
*
|
|
|
|
* @param s The stream to read from
|
2011-07-13 05:43:52 +04:00
|
|
|
* @param tag BER universally-defined tag
|
2022-12-09 16:35:03 +03:00
|
|
|
*
|
|
|
|
* @return \b TRUE for success, \b FALSE otherwise
|
2011-07-13 05:43:52 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_universal_tag(wStream* s, BYTE tag, BOOL pc)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2022-04-19 15:29:17 +03:00
|
|
|
const BYTE expect = (BER_CLASS_UNIV | BER_PC(pc) | (BER_TAG_MASK & tag));
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (byte != expect)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
|
|
|
* Write BER Universal tag.
|
|
|
|
* @param s stream
|
|
|
|
* @param tag BER universally-defined tag
|
2012-10-09 10:31:28 +04:00
|
|
|
* @param pc primitive (FALSE) or constructed (TRUE)
|
2011-07-05 05:35:32 +04:00
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_UNIV | BER_PC(pc)) | (BER_TAG_MASK & tag));
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
|
|
|
|
2011-07-05 05:35:32 +04:00
|
|
|
/**
|
2011-07-13 07:50:51 +04:00
|
|
|
* Read BER Application tag.
|
2011-07-05 05:35:32 +04:00
|
|
|
* @param s stream
|
|
|
|
* @param tag BER application-defined tag
|
|
|
|
* @param length length
|
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_application_tag(wStream* s, BYTE tag, size_t* length)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(length);
|
|
|
|
|
2011-07-09 23:28:59 +04:00
|
|
|
if (tag > 30)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
const BYTE expect = ((BER_CLASS_APPL | BER_CONSTRUCT) | BER_TAG_MASK);
|
|
|
|
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (byte != expect)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (byte != tag)
|
2022-04-19 15:29:17 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, tag);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
return ber_read_length(s, length);
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
const BYTE expect = ((BER_CLASS_APPL | BER_CONSTRUCT) | (BER_TAG_MASK & tag));
|
|
|
|
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (byte != expect)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
return ber_read_length(s, length);
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2011-07-13 07:50:51 +04:00
|
|
|
/**
|
|
|
|
* Write BER Application tag.
|
|
|
|
* @param s stream
|
|
|
|
* @param tag BER application-defined tag
|
|
|
|
* @param length length
|
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
void ber_write_application_tag(wStream* s, BYTE tag, size_t length)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2011-07-13 07:50:51 +04:00
|
|
|
if (tag > 30)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_APPL | BER_CONSTRUCT) | BER_TAG_MASK);
|
|
|
|
Stream_Write_UINT8(s, tag);
|
2011-07-13 07:50:51 +04:00
|
|
|
ber_write_length(s, length);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_APPL | BER_CONSTRUCT) | (BER_TAG_MASK & tag));
|
2011-07-13 07:50:51 +04:00
|
|
|
ber_write_length(s, length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_contextual_tag(wStream* s, BYTE tag, size_t* length, BOOL pc)
|
2011-07-13 05:43:52 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
const BYTE expect = ((BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag));
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2011-07-13 05:43:52 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(length);
|
|
|
|
|
2013-05-21 19:06:00 +04:00
|
|
|
if (Stream_GetRemainingLength(s) < 1)
|
2022-04-19 15:29:17 +03:00
|
|
|
{
|
|
|
|
WLog_VRB(TAG, "short data, got %" PRIuz ", expected %" PRIuz, Stream_GetRemainingLength(s),
|
|
|
|
1);
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-13 05:43:52 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (byte != expect)
|
2011-07-19 01:02:06 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WLog_VRB(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_Rewind(s, 1);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-19 01:02:06 +04:00
|
|
|
}
|
2011-07-13 05:43:52 +04:00
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
return ber_read_length(s, length);
|
2011-07-13 05:43:52 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_contextual_tag(wStream* s, BYTE tag, size_t length, BOOL pc)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag));
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + ber_write_length(s, length);
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_sizeof_contextual_tag(size_t length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + _ber_sizeof_length(length);
|
2011-07-18 10:34:28 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_sequence_tag(wStream* s, size_t* length)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
const BYTE expect = ((BER_CLASS_UNIV | BER_CONSTRUCT) | (BER_TAG_SEQUENCE_OF));
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (byte != expect)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
return ber_read_length(s, length);
|
2011-07-18 10:34:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write BER SEQUENCE tag.
|
|
|
|
* @param s stream
|
|
|
|
* @param length length
|
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_sequence_tag(wStream* s, size_t length)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_UNIV | BER_CONSTRUCT) | (BER_TAG_MASK & BER_TAG_SEQUENCE));
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + ber_write_length(s, length);
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_sizeof_sequence(size_t length)
|
2011-07-19 02:43:23 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + _ber_sizeof_length(length) + length;
|
2011-07-19 02:43:23 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_sizeof_sequence_tag(size_t length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + _ber_sizeof_length(length);
|
2011-07-08 00:38:41 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_enumerated(wStream* s, BYTE* enumerated, BYTE count)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(enumerated);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_universal_tag(s, BER_TAG_ENUMERATED, FALSE) || !ber_read_length(s, &length))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (length != 1)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "short data, got %" PRIuz ", expected %" PRIuz, length, 1);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, *enumerated);
|
2013-01-11 04:11:03 +04:00
|
|
|
|
2011-07-09 23:28:59 +04:00
|
|
|
/* check that enumerated value falls within expected range */
|
2011-07-10 07:54:23 +04:00
|
|
|
if (*enumerated + 1 > count)
|
2022-04-19 15:29:17 +03:00
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "invalid data, expected %" PRIu8 " < %" PRIu8, *enumerated, count);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2022-04-19 15:29:17 +03:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void ber_write_enumerated(wStream* s, BYTE enumerated, BYTE count)
|
2011-08-19 19:56:47 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_ENUMERATED, FALSE);
|
2011-08-19 19:56:47 +04:00
|
|
|
ber_write_length(s, 1);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, enumerated);
|
2011-08-19 19:56:47 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_bit_string(wStream* s, size_t* length, BYTE* padding)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_universal_tag(s, BER_TAG_BIT_STRING, FALSE) || !ber_read_length(s, length))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2011-07-13 07:50:51 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2018-01-17 10:12:54 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, *padding);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-13 07:50:51 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 23:35:09 +04:00
|
|
|
/**
|
|
|
|
* Write a BER OCTET_STRING
|
|
|
|
* @param s stream
|
|
|
|
* @param oct_str octet string
|
|
|
|
* @param length string length
|
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_octet_string(wStream* s, const BYTE* oct_str, size_t length)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t size = 0;
|
2022-04-19 15:29:17 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(oct_str || (length == 0));
|
2013-05-21 19:06:00 +04:00
|
|
|
size += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
|
|
|
|
size += ber_write_length(s, length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(s, oct_str, length);
|
2013-05-21 19:06:00 +04:00
|
|
|
size += length;
|
|
|
|
return size;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t ber_write_contextual_octet_string(wStream* s, BYTE tag, const BYTE* oct_str, size_t length)
|
|
|
|
{
|
|
|
|
size_t inner = ber_sizeof_octet_string(length);
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t ret = 0;
|
|
|
|
size_t r = 0;
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
ret = ber_write_contextual_tag(s, tag, inner, TRUE);
|
|
|
|
if (!ret)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
r = ber_write_octet_string(s, oct_str, length);
|
|
|
|
if (!r)
|
|
|
|
return 0;
|
|
|
|
return ret + r;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ber_write_char_to_unicode_octet_string(wStream* s, const char* str)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(str);
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t size = 0;
|
|
|
|
size_t length = strlen(str) + 1;
|
|
|
|
size += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
|
2022-10-28 09:09:27 +03:00
|
|
|
size += ber_write_length(s, length * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (Stream_Write_UTF16_String_From_UTF8(s, length, str, length, TRUE) < 0)
|
|
|
|
return 0;
|
|
|
|
return size + length * sizeof(WCHAR);
|
2022-01-22 17:01:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t ber_write_contextual_unicode_octet_string(wStream* s, BYTE tag, LPWSTR str)
|
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(str);
|
|
|
|
size_t len = _wcslen(str) * sizeof(WCHAR);
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t inner_len = ber_sizeof_octet_string(len);
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t ret = 0;
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
ret = ber_write_contextual_tag(s, tag, inner_len, TRUE);
|
|
|
|
return ret + ber_write_octet_string(s, (const BYTE*)str, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ber_write_contextual_char_to_unicode_octet_string(wStream* s, BYTE tag, const char* str)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t ret = 0;
|
2022-02-05 01:59:16 +03:00
|
|
|
size_t len = strlen(str);
|
|
|
|
size_t inner_len = ber_sizeof_octet_string(len * 2);
|
2022-01-22 17:01:43 +03:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(Stream_GetRemainingCapacity(s) < ber_sizeof_contextual_tag(inner_len) + inner_len);
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
ret = ber_write_contextual_tag(s, tag, inner_len, TRUE);
|
|
|
|
ret += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
|
2022-10-28 09:09:27 +03:00
|
|
|
ret += ber_write_length(s, len * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (Stream_Write_UTF16_String_From_UTF8(s, len, str, len, TRUE) < 0)
|
2022-02-05 01:59:16 +03:00
|
|
|
return 0;
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
return ret + len;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ber_read_unicode_octet_string(wStream* s, LPWSTR* str)
|
|
|
|
{
|
|
|
|
LPWSTR ret = NULL;
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
if (!ber_read_octet_string_tag(s, &length))
|
|
|
|
return FALSE;
|
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
|
2022-01-22 17:01:43 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ret = calloc(1, length + 2);
|
|
|
|
if (!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
2023-06-07 16:38:21 +03:00
|
|
|
memcpy(ret, Stream_ConstPointer(s), length);
|
2022-01-22 17:01:43 +03:00
|
|
|
ret[length / 2] = 0;
|
|
|
|
Stream_Seek(s, length);
|
|
|
|
*str = ret;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL ber_read_char_from_unicode_octet_string(wStream* s, char** str)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
|
|
|
char* ptr = NULL;
|
2022-01-22 17:01:43 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
*str = NULL;
|
2022-01-22 17:01:43 +03:00
|
|
|
if (!ber_read_octet_string_tag(s, &length))
|
|
|
|
return FALSE;
|
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
ptr = Stream_Read_UTF16_String_As_UTF8(s, length / sizeof(WCHAR), NULL);
|
2022-01-22 17:01:43 +03:00
|
|
|
if (!ptr)
|
|
|
|
return FALSE;
|
|
|
|
*str = ptr;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_octet_string_tag(wStream* s, size_t* length)
|
2012-05-24 23:52:20 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return ber_read_universal_tag(s, BER_TAG_OCTET_STRING, FALSE) && ber_read_length(s, length);
|
2012-05-24 23:52:20 +04:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:01:43 +03:00
|
|
|
BOOL ber_read_octet_string(wStream* s, BYTE** content, size_t* length)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE* ret = NULL;
|
2022-04-19 15:29:17 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
WINPR_ASSERT(content);
|
|
|
|
WINPR_ASSERT(length);
|
|
|
|
|
|
|
|
if (!ber_read_octet_string_tag(s, length))
|
|
|
|
return FALSE;
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, *length))
|
2022-01-22 17:01:43 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
ret = malloc(*length);
|
|
|
|
if (!ret)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Read(s, ret, *length);
|
|
|
|
*content = ret;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_octet_string_tag(wStream* s, size_t length)
|
2011-07-19 02:43:23 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
|
2011-07-19 02:43:23 +04:00
|
|
|
ber_write_length(s, length);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + _ber_sizeof_length(length);
|
2011-07-19 02:43:23 +04:00
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_sizeof_octet_string(size_t length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 1 + _ber_sizeof_length(length) + length;
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t ber_sizeof_contextual_octet_string(size_t length)
|
|
|
|
{
|
|
|
|
size_t ret = ber_sizeof_octet_string(length);
|
|
|
|
return ber_sizeof_contextual_tag(ret) + ret;
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:35:03 +03:00
|
|
|
/** \brief Read a BER BOOLEAN
|
|
|
|
*
|
|
|
|
* @param s The stream to read from.
|
|
|
|
* @param value A pointer to the value read, must not be NULL
|
|
|
|
*
|
|
|
|
* \return \b TRUE for success, \b FALSE for any failure
|
2011-08-19 13:39:37 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_BOOL(wStream* s, BOOL* value)
|
2011-08-19 13:39:37 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
|
|
|
BYTE v = 0;
|
2011-08-19 13:39:37 +04:00
|
|
|
|
2022-12-09 16:35:03 +03:00
|
|
|
WINPR_ASSERT(value);
|
2019-11-06 17:24:51 +03:00
|
|
|
if (!ber_read_universal_tag(s, BER_TAG_BOOLEAN, FALSE) || !ber_read_length(s, &length))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2013-01-11 04:11:03 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
if (length != 1)
|
|
|
|
{
|
|
|
|
WLog_WARN(TAG, "short data, got %" PRIuz ", expected %" PRIuz, length, 1);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2013-01-11 04:11:03 +04:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, v);
|
2012-10-09 10:31:28 +04:00
|
|
|
*value = (v ? TRUE : FALSE);
|
|
|
|
return TRUE;
|
2011-08-19 13:39:37 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 23:35:09 +04:00
|
|
|
/**
|
|
|
|
* Write a BER BOOLEAN
|
2022-12-09 16:35:03 +03:00
|
|
|
*
|
|
|
|
* @param s A pointer to the stream to write to
|
|
|
|
* @param value The value to write
|
2011-07-07 23:35:09 +04:00
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void ber_write_BOOL(wStream* s, BOOL value)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_BOOLEAN, FALSE);
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 1);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (value == TRUE) ? 0xFF : 0);
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_integer(wStream* s, UINT32* value)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
size_t length = 0;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
|
|
|
if (!ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE))
|
|
|
|
return FALSE;
|
|
|
|
if (!ber_read_length(s, &length))
|
|
|
|
return FALSE;
|
|
|
|
if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2011-07-13 05:43:52 +04:00
|
|
|
if (value == NULL)
|
|
|
|
{
|
2013-01-11 04:11:03 +04:00
|
|
|
// even if we don't care the integer value, check the announced size
|
2013-05-09 01:48:30 +04:00
|
|
|
return Stream_SafeSeek(s, length);
|
2011-07-13 05:43:52 +04:00
|
|
|
}
|
|
|
|
|
2011-07-09 23:28:59 +04:00
|
|
|
if (length == 1)
|
2013-01-20 07:00:09 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, *value);
|
2013-01-20 07:00:09 +04:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
else if (length == 2)
|
2013-01-20 07:00:09 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT16_BE(s, *value);
|
2013-01-20 07:00:09 +04:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
else if (length == 3)
|
|
|
|
{
|
2024-01-23 18:49:54 +03:00
|
|
|
BYTE byte = 0;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
|
|
|
Stream_Read_UINT16_BE(s, *value);
|
2011-07-09 23:28:59 +04:00
|
|
|
*value += (byte << 16);
|
|
|
|
}
|
2011-07-13 07:50:51 +04:00
|
|
|
else if (length == 4)
|
2013-01-20 07:00:09 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT32_BE(s, *value);
|
2013-01-20 07:00:09 +04:00
|
|
|
}
|
2013-01-20 02:32:04 +04:00
|
|
|
else if (length == 8)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
WLog_ERR(TAG, "should implement reading an 8 bytes integer");
|
2013-01-20 02:32:04 +04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
else
|
2013-01-20 07:00:09 +04:00
|
|
|
{
|
2022-09-29 15:55:27 +03:00
|
|
|
WLog_ERR(TAG, "should implement reading an integer with length=%" PRIuz, length);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2013-01-20 07:00:09 +04:00
|
|
|
}
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
|
2011-07-07 23:35:09 +04:00
|
|
|
/**
|
|
|
|
* Write a BER INTEGER
|
2022-12-09 16:35:03 +03:00
|
|
|
*
|
|
|
|
* @param s A pointer to the stream to write to
|
|
|
|
* @param value The value to write
|
|
|
|
*
|
|
|
|
* @return The size in bytes that were written
|
2011-07-07 23:35:09 +04:00
|
|
|
*/
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_write_integer(wStream* s, UINT32 value)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2022-04-19 15:29:17 +03:00
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
if (value < 0x80)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2013-05-22 17:20:26 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 1);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, value);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 3;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
else if (value < 0x8000)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2013-05-22 17:20:26 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 2);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16_BE(s, value);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 4;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
else if (value < 0x800000)
|
2012-02-03 00:48:56 +04:00
|
|
|
{
|
2013-05-22 17:20:26 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
2012-02-03 00:48:56 +04:00
|
|
|
ber_write_length(s, 3);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (value >> 16));
|
|
|
|
Stream_Write_UINT16_BE(s, (value & 0xFFFF));
|
2013-05-21 19:06:00 +04:00
|
|
|
return 5;
|
2012-02-03 00:48:56 +04:00
|
|
|
}
|
2019-11-06 17:24:51 +03:00
|
|
|
else if (value < 0x80000000)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2013-05-22 17:20:26 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 4);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32_BE(s, value);
|
2013-05-21 19:06:00 +04:00
|
|
|
return 6;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2016-03-18 15:32:13 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* treat as signed integer i.e. NT/HRESULT error codes */
|
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
|
|
|
ber_write_length(s, 4);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
2016-03-18 15:32:13 +03:00
|
|
|
Stream_Write_UINT32_BE(s, value);
|
|
|
|
return 6;
|
|
|
|
}
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2011-07-13 07:50:51 +04:00
|
|
|
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t ber_write_contextual_integer(wStream* s, BYTE tag, UINT32 value)
|
|
|
|
{
|
|
|
|
size_t len = ber_sizeof_integer(value);
|
2022-04-19 15:29:17 +03:00
|
|
|
|
|
|
|
WINPR_ASSERT(s);
|
|
|
|
|
|
|
|
WINPR_ASSERT(Stream_EnsureRemainingCapacity(s, len + 5));
|
2022-01-22 17:01:43 +03:00
|
|
|
|
|
|
|
len += ber_write_contextual_tag(s, tag, len, TRUE);
|
|
|
|
ber_write_integer(s, value);
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
size_t ber_sizeof_integer(UINT32 value)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
if (value < 0x80)
|
|
|
|
{
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
else if (value < 0x8000)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 4;
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
2013-05-21 19:06:00 +04:00
|
|
|
else if (value < 0x800000)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 5;
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
2013-05-21 19:06:00 +04:00
|
|
|
else if (value < 0x80000000)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2013-05-21 19:06:00 +04:00
|
|
|
return 6;
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
2016-03-18 15:32:13 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* treat as signed integer i.e. NT/HRESULT error codes */
|
|
|
|
return 6;
|
|
|
|
}
|
2011-07-18 22:56:08 +04:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:01:43 +03:00
|
|
|
size_t ber_sizeof_contextual_integer(UINT32 value)
|
|
|
|
{
|
|
|
|
size_t intSize = ber_sizeof_integer(value);
|
|
|
|
return ber_sizeof_contextual_tag(intSize) + intSize;
|
|
|
|
}
|
|
|
|
|
2018-01-17 10:12:54 +03:00
|
|
|
BOOL ber_read_integer_length(wStream* s, size_t* length)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE) && ber_read_length(s, length);
|
2011-07-13 07:50:51 +04:00
|
|
|
}
|