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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-01-20 02:32:04 +04:00
|
|
|
#include <stdio.h>
|
2013-03-22 00:45:25 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <freerdp/crypto/ber.h>
|
2011-07-04 06:47:35 +04:00
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_length(wStream* s, int* length)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(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
|
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*length = byte;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int ber_write_length(wStream* s, int length)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2011-07-05 01:05:58 +04:00
|
|
|
if (length > 0x7F)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, 0x82);
|
|
|
|
Stream_Write_UINT16_BE(s, length);
|
2011-07-18 22:56:08 +04:00
|
|
|
return 3;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, length);
|
2011-07-18 22:56:08 +04:00
|
|
|
return 1;
|
2011-07-04 06:47:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-18 22:56:08 +04:00
|
|
|
int _ber_skip_length(int length)
|
|
|
|
{
|
2013-02-27 03:01:03 +04:00
|
|
|
if (length > 0x80)
|
2011-07-18 22:56:08 +04:00
|
|
|
return 3;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-07-19 00:16:37 +04:00
|
|
|
int ber_get_content_length(int length)
|
|
|
|
{
|
2013-02-27 00:32:36 +04:00
|
|
|
if (length > 0x81)
|
2011-07-19 00:16:37 +04:00
|
|
|
return length - 4;
|
|
|
|
else
|
|
|
|
return length - 2;
|
|
|
|
}
|
|
|
|
|
2011-07-13 05:43:52 +04:00
|
|
|
/**
|
|
|
|
* Read BER Universal tag.
|
|
|
|
* @param s stream
|
|
|
|
* @param tag BER universally-defined tag
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2011-07-27 10:41:00 +04:00
|
|
|
if (byte != (BER_CLASS_UNIV | BER_PC(pc) | (BER_TAG_MASK & tag)))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
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
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void ber_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
|
2011-07-04 06:47:35 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_UNIV | BER_PC(pc)) | (BER_TAG_MASK & tag));
|
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
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_application_tag(wStream* s, BYTE tag, int* length)
|
2011-07-09 23:28:59 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (tag > 30)
|
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (byte != ((BER_CLASS_APPL | BER_CONSTRUCT) | BER_TAG_MASK))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (byte != tag)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
{
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-09 23:28:59 +04:00
|
|
|
|
|
|
|
if (byte != ((BER_CLASS_APPL | BER_CONSTRUCT) | (BER_TAG_MASK & tag)))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void ber_write_application_tag(wStream* s, BYTE tag, int length)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
|
|
|
if (tag > 30)
|
|
|
|
{
|
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
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_contextual_tag(wStream* s, BYTE tag, int* length, BOOL pc)
|
2011-07-13 05:43:52 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
2011-07-13 05:43:52 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-13 05:43:52 +04:00
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
if (byte != ((BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag)))
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int ber_write_contextual_tag(wStream* s, BYTE tag, int length, BOOL pc)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag));
|
2011-07-18 22:56:08 +04:00
|
|
|
return ber_write_length(s, length) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ber_skip_contextual_tag(int length)
|
|
|
|
{
|
|
|
|
return _ber_skip_length(length) + 1;
|
2011-07-18 10:34:28 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_sequence_tag(wStream* s, int* length)
|
2011-07-18 10:34:28 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
2011-07-18 10:34:28 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Read_UINT8(s, byte);
|
2011-07-18 10:34:28 +04:00
|
|
|
|
|
|
|
if (byte != ((BER_CLASS_UNIV | BER_CONSTRUCT) | (BER_TAG_SEQUENCE_OF)))
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int ber_write_sequence_tag(wStream* s, int 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));
|
2011-07-18 22:56:08 +04:00
|
|
|
return ber_write_length(s, length) + 1;
|
|
|
|
}
|
|
|
|
|
2011-07-19 02:43:23 +04:00
|
|
|
int ber_skip_sequence(int length)
|
|
|
|
{
|
|
|
|
return 1 + _ber_skip_length(length) + length;
|
|
|
|
}
|
|
|
|
|
2011-07-19 01:02:06 +04:00
|
|
|
int ber_skip_sequence_tag(int length)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
2011-07-19 02:43:23 +04:00
|
|
|
return 1 + _ber_skip_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
|
|
|
{
|
|
|
|
int length;
|
|
|
|
|
2013-01-11 04:11:03 +04:00
|
|
|
if(!ber_read_universal_tag(s, BER_TAG_ENUMERATED, FALSE) ||
|
|
|
|
!ber_read_length(s, &length))
|
|
|
|
return FALSE;
|
2011-07-09 23:28:59 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (length != 1 || Stream_GetRemainingLength(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)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
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
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_bit_string(wStream* s, int* length, BYTE* padding)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
2013-01-11 04:11:03 +04:00
|
|
|
if(!ber_read_universal_tag(s, BER_TAG_BIT_STRING, FALSE) ||
|
|
|
|
!ber_read_length(s, length))
|
|
|
|
return FALSE;
|
2011-07-13 07:50:51 +04:00
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if(Stream_GetRemainingLength(s) < 1)
|
2013-01-11 04:11:03 +04:00
|
|
|
return FALSE;
|
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
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
void ber_write_octet_string(wStream* s, const BYTE* oct_str, int length)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, length);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write(s, oct_str, length);
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_octet_string_tag(wStream* s, int* length)
|
2012-05-24 23:52:20 +04:00
|
|
|
{
|
2013-01-11 04:11:03 +04: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
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int ber_write_octet_string_tag(wStream* s, int 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);
|
|
|
|
return 1 + _ber_skip_length(length);
|
|
|
|
}
|
|
|
|
|
2011-07-18 22:56:08 +04:00
|
|
|
int ber_skip_octet_string(int length)
|
|
|
|
{
|
|
|
|
return 1 + _ber_skip_length(length) + length;
|
|
|
|
}
|
|
|
|
|
2011-08-19 13:39:37 +04:00
|
|
|
/**
|
|
|
|
* Read a BER BOOLEAN
|
|
|
|
* @param s
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_BOOL(wStream* s, BOOL* value)
|
2011-08-19 13:39:37 +04:00
|
|
|
{
|
|
|
|
int length;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE v;
|
2011-08-19 13:39:37 +04:00
|
|
|
|
2013-01-11 04:11:03 +04: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
|
|
|
|
2013-04-30 06:35:15 +04:00
|
|
|
if (length != 1 || Stream_GetRemainingLength(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
|
|
|
|
* @param s
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int length;
|
|
|
|
|
2013-01-20 07:00:09 +04:00
|
|
|
if (!ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE) ||
|
2013-01-11 04:11:03 +04:00
|
|
|
!ber_read_length(s, &length) ||
|
2013-04-30 06:35:15 +04:00
|
|
|
Stream_GetRemainingLength(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)
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte;
|
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)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: should implement reading an 8 bytes integer\n", __FUNCTION__);
|
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
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: should implement reading an integer with length=%d\n", __FUNCTION__, 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
|
|
|
|
* @param s
|
|
|
|
* @param value
|
|
|
|
*/
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
int ber_write_integer(wStream* s, UINT32 value)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
|
2011-07-07 23:35:09 +04:00
|
|
|
|
|
|
|
if (value <= 0xFF)
|
|
|
|
{
|
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);
|
2011-07-19 02:43:23 +04:00
|
|
|
return 2;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2012-02-03 00:48:56 +04:00
|
|
|
else if (value < 0xFF80)
|
2011-07-07 23:35:09 +04:00
|
|
|
{
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 2);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT16_BE(s, value);
|
2011-07-19 02:43:23 +04:00
|
|
|
return 3;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2012-02-03 00:48:56 +04:00
|
|
|
else if (value < 0xFF8000)
|
|
|
|
{
|
|
|
|
ber_write_length(s, 3);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT8(s, (value >> 16));
|
|
|
|
Stream_Write_UINT16_BE(s, (value & 0xFFFF));
|
2012-02-03 00:48:56 +04:00
|
|
|
return 4;
|
|
|
|
}
|
2011-07-07 23:35:09 +04:00
|
|
|
else if (value <= 0xFFFFFFFF)
|
|
|
|
{
|
2011-07-08 00:38:41 +04:00
|
|
|
ber_write_length(s, 4);
|
2013-05-09 00:09:16 +04:00
|
|
|
Stream_Write_UINT32_BE(s, value);
|
2011-07-19 02:43:23 +04:00
|
|
|
return 5;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2011-07-19 02:43:23 +04:00
|
|
|
|
|
|
|
return 0;
|
2011-07-07 23:35:09 +04:00
|
|
|
}
|
2011-07-13 07:50:51 +04:00
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
int ber_skip_integer(UINT32 value)
|
2011-07-18 22:56:08 +04:00
|
|
|
{
|
|
|
|
if (value <= 0xFF)
|
|
|
|
{
|
|
|
|
return _ber_skip_length(1) + 2;
|
|
|
|
}
|
|
|
|
else if (value <= 0xFFFF)
|
|
|
|
{
|
|
|
|
return _ber_skip_length(2) + 3;
|
|
|
|
}
|
|
|
|
else if (value <= 0xFFFFFFFF)
|
|
|
|
{
|
|
|
|
return _ber_skip_length(4) + 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-03-21 23:19:33 +04:00
|
|
|
BOOL ber_read_integer_length(wStream* s, int* length)
|
2011-07-13 07:50:51 +04:00
|
|
|
{
|
2013-01-11 04:11:03 +04:00
|
|
|
return
|
|
|
|
ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE) &&
|
|
|
|
ber_read_length(s, length);
|
2011-07-13 07:50:51 +04:00
|
|
|
}
|