Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cASNReader Class Reference

#include <cASNReader.h>

Inheritance diagram for GrayLib::cASNReader:
Gray::cMemBlock GrayLib::cX509Reader

Public Member Functions

 cASNReader (const BYTE *p=nullptr, size_t nSize=0) noexcept
 
 cASNReader (const cMemBlock &b) noexcept
 
void SetBlock (const BYTE *p, size_t nSize)
 
size_t get_LenRead () const
 
size_t get_LenLeft () const
 
bool isEOF () const
 
bool isEOFX () const
 
const BYTE * get_InpCur () const noexcept
 
BYTE * get_InpUnk () const
 
BYTE get_InpByte () const
 
void AddInpCur (size_t nSize)
 
void AddInpCur (cMemBlock &r)
 
void ResetReader ()
 
HRESULT ReadTagBlock (ASN_t nTagExpected, OUT cMemBlock &r)
 
HRESULT ReadTagBuf (ASN_t nTagExpected, OUT cASNBuf &r)
 
HRESULT ReadTagChild (ASN_t nTagExpected, OUT cASNReader &r)
 
HRESULT ReadTagBoolean (OUT bool &rVal)
 
HRESULT ReadTagInt (OUT int &riVal)
 
HRESULT ReadTagArrayBits (OUT cASNBuf &bs)
 
HRESULT ReadTagArrayBits0 (OUT size_t &rSizeData)
 
HRESULT ReadTagBigInt (OUT cBitArray &X)
 
HRESULT ReadTagTime (OUT cTimeUnits &time)
 
HRESULT ReadTagSequence (ASN_TYPE tag, cASNSequence &head)
 
HRESULT ReadTagSeqAlg (OUT cASNBuf &alg, OUT cASNBuf &params)
 
HRESULT ReadTagSeqAlgNull (OUT cASNBuf &alg)
 
HRESULT ReadSets (OUT cASNSets &cur)
 
- Public Member Functions inherited from Gray::cMemBlock
 cMemBlock () noexcept
 
 cMemBlock (const void *pData, size_t nSize) noexcept
 
 cMemBlock (const cMemBlock &block) noexcept
 
 cMemBlock (const cMemBlock *pBlock) noexcept
 
size_t get_DataSize () const noexcept
 
void * get_Data () const noexcept
 
BYTE * get_DataBytes () const noexcept
 
char * get_DataA () const noexcept
 
 operator const BYTE * () const noexcept
 
bool isValidPtr () const noexcept
 
bool IsValidIndex (size_t i) const noexcept
 
bool IsValidIndex2 (size_t i) const noexcept
 
bool IsValidPtr (const void *p) const noexcept
 
bool IsValidPtr2 (const void *p) const noexcept
 
bool IsZeros () const noexcept
 
bool IsEqualData (const void *pData, size_t nSize) const noexcept
 
bool IsEqualData (const cMemBlock *pData) const noexcept
 
bool IsEqualData (const cMemBlock &data) const noexcept
 
BYTE * GetOffset (size_t nOffset) const
 
const void * get_DataEnd () const noexcept
 
void put_DataPtr (void *pStart) noexcept
 
void put_Size (size_t nSize) noexcept
 
void SetBlock (void *pData, size_t nSize) noexcept
 
void SetEmptyBlock () noexcept
 
void InitZeros () noexcept
 
StrLen_t ConvertToString (char *pszDst, StrLen_t iDstSizeMax) const
 

Static Public Member Functions

static HRESULT ReadInt (const BYTE *pData, size_t nSizeData, OUT int &riVal)
 
static HRESULT ReadTime (const char *pData, size_t nSizeData, bool bGeneral, OUT cTimeUnits &time)
 
- Static Public Member Functions inherited from Gray::cMemBlock
static COMPARE_t __stdcall Compare (const void *pData1, size_t iLen1, const void *pData2, size_t iLen2)
 

Protected Member Functions

HRESULT ReadLengthCode (OUT size_t &rSize)
 
HRESULT ReadTag (ASN_t nTagExpected, OUT size_t &rSizeData)
 

Static Protected Member Functions

static HRESULT ReadStrUInt (const char *pData, size_t nSizeData, OUT UINT32 &ruVal)
 

Protected Attributes

const BYTE * m_pInpCur
 Current read position in cMemBlock. This Advances. More...
 
- Protected Attributes inherited from Gray::cMemBlock
size_t m_nSize
 size_t of m_pData in bytes. May be determined at runtime. More...
 
void * m_pData
 

Friends

class cASNBuf
 
class cASNSequence
 

Detailed Description

Helper class for reading/parsing an ASN blob that has been loaded into memory. Used for DER format. Similar to cStreamStatic. Opposite of cASNWriterRev

Constructor & Destructor Documentation

◆ cASNReader() [1/2]

GrayLib::cASNReader::cASNReader ( const BYTE *  p = nullptr,
size_t  nSize = 0 
)
inlinenoexcept

Create reader for bytes.

◆ cASNReader() [2/2]

GrayLib::cASNReader::cASNReader ( const cMemBlock b)
inlinenoexcept

Create reader for buffer.

Member Function Documentation

◆ AddInpCur() [1/2]

void GrayLib::cASNReader::AddInpCur ( cMemBlock r)
inline

Skip a whole block.

◆ AddInpCur() [2/2]

void GrayLib::cASNReader::AddInpCur ( size_t  nSize)
inline

Advance/Seek position of m_pInpCur.

◆ get_InpByte()

BYTE GrayLib::cASNReader::get_InpByte ( ) const
inline

get BYTE at the current read position. No advance.

◆ get_InpCur()

const BYTE* GrayLib::cASNReader::get_InpCur ( ) const
inlinenoexcept

get current read position/cursor. This Advances via Read* or AddInpCur().

◆ get_InpUnk()

BYTE* GrayLib::cASNReader::get_InpUnk ( ) const
inline

get read position. Special version for when i don't know if I might modify it or not.

◆ get_LenLeft()

size_t GrayLib::cASNReader::get_LenLeft ( ) const
inline

How much is left to read in the buffer? bytes.

◆ get_LenRead()

size_t GrayLib::cASNReader::get_LenRead ( ) const
inline

How much have I read? bytes.

◆ isEOF()

bool GrayLib::cASNReader::isEOF ( ) const
inline

is m_pInpCur at or after the end of block?

◆ isEOFX()

bool GrayLib::cASNReader::isEOFX ( ) const
inline

is m_pInpCur Exactly at end of block?

◆ ReadInt()

HRESULT GrayLib::cASNReader::ReadInt ( const BYTE *  pData,
size_t  nSizeData,
OUT int &  riVal 
)
static

Read packed integer value.

◆ ReadLengthCode()

HRESULT GrayLib::cASNReader::ReadLengthCode ( OUT size_t &  rSize)
protected

Read a variable length prefix according to ASN.1 DER decoding routines Get the length of an ASN.1 element. Updates the read pointer. opposite of WriteLengthCode();

◆ ReadSets()

HRESULT GrayLib::cASNReader::ReadSets ( OUT cASNSets cur)

Name ::= CHOICE { – only one possibility for now – rdnSequence RDNSequence }

RDNSequence ::= SEQUENCE OF RelativeDistinguishedName

RelativeDistinguishedName ::= SET OF AttributeTypeAndValue

AttributeTypeAndValue ::= SEQUENCE { type AttributeType, value AttributeValue }

AttributeType ::= OBJECT IDENTIFIER

AttributeValue ::= ANY DEFINED BY AttributeType

The data structure is optimized for the common case where each RDN has only one element, which is represented as a list of AttributeTypeAndValue. For the general case we still use a flat list, but we mark elements of the same set so that they are "merged" together in the functions that consume this list, eg GetDescriptionSets().

don't use recursion, we'd risk stack overflow if not optimized

◆ ReadStrUInt()

HRESULT GrayLib::cASNReader::ReadStrUInt ( const char *  pData,
size_t  nSizeData,
OUT UINT32 &  ruVal 
)
staticprotected

Reads string as DWORD 32 nSize = 4 .

◆ ReadTag()

HRESULT GrayLib::cASNReader::ReadTag ( ASN_t  nTagExpected,
OUT size_t &  rSizeData 
)
protected

Read the expected tag and the length code that follows. Get the tag and length of the tag. Check for the requested tag. Updates the pointer to immediately behind the tag and length.

  • nTagExpected = ASN_t e.g. I expect ASN_INTEGER. bitmask. ASN_Unknown = i don't care.
  • rSize = The variable that will receive the length
    Returns
    0 if successful,
    HRESULT_WIN32_C(ERROR_TAG_NOT_PRESENT) = not the tag i thought it was. try again. or another specific ASN.1 error code.

◆ ReadTagArrayBits()

HRESULT GrayLib::cASNReader::ReadTagArrayBits ( OUT cASNBuf bs)

Retrieve a ASN_ArrayBits ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.

  • bs = The variable that will receive the value
    Returns
    0 if successful or a specific ASN.1 error code. Certificate type is a single byte ASN_ArrayBits

◆ ReadTagArrayBits0()

HRESULT GrayLib::cASNReader::ReadTagArrayBits0 ( OUT size_t &  rSizeData)

Retrieve a ASN_ArrayBits ASN.1 tag without unused bits and its value.

  • rSizeData = Length of the actual bit/octet string in bytes
    Returns
    0 if successful or a specific ASN.1 error code.

◆ ReadTagBigInt()

HRESULT GrayLib::cASNReader::ReadTagBigInt ( OUT cBitArray X)

Retrieve a MPI value from an integer ASN.1 tag. ASN_INTEGER Updates the pointer to immediately behind the full tag.

  • X = The BigUnsigned that will receive the value
    Returns
    0 if successful or a specific ASN.1 or MPI error code.

◆ ReadTagBlock()

HRESULT GrayLib::cASNReader::ReadTagBlock ( ASN_t  nTagExpected,
OUT cMemBlock r 
)

Read the ASN block and store it as cASNBuf/cASNReader

Note
Does not advance pointer past data block.

◆ ReadTagBoolean()

HRESULT GrayLib::cASNReader::ReadTagBoolean ( OUT bool &  rVal)

Retrieve a ASN_BOOLEAN ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.

  • rVal = The variable that will receive the value
    Returns
    0 if successful or a specific ASN.1 error code.

◆ ReadTagBuf()

HRESULT GrayLib::cASNReader::ReadTagBuf ( ASN_t  nTagExpected,
OUT cASNBuf r 
)

Read the ASN block and store it as cASNBuf/cASNReader

Note
Does not advance pointer past data block.

◆ ReadTagChild()

HRESULT GrayLib::cASNReader::ReadTagChild ( ASN_t  nTagExpected,
OUT cASNReader r 
)

Read the ASN block and store it as cASNReader. skip reading it as a cASNBuf.

Returns
HRESULT_WIN32_C(ERROR_TAG_NOT_PRESENT)

◆ ReadTagInt()

HRESULT GrayLib::cASNReader::ReadTagInt ( OUT int &  riVal)

Retrieve an ASN_INTEGER ASN.1 tag and its value. Updates the pointer to immediately behind the full tag.

  • riVal = The variable that will receive the value
    Returns
    0 if successful or a specific ASN.1 error code. Int is arbitrary sized.

◆ ReadTagSeqAlg()

HRESULT GrayLib::cASNReader::ReadTagSeqAlg ( OUT cASNBuf alg,
OUT cASNBuf params 
)

Parse an AlgorithmIdentifier with (optional) parameters Retrieve an AlgorithmIdentifier ASN.1 sequence. Updates the pointer to immediately behind the full AlgorithmIdentifier.

  • alg = The buffer to receive the OID
  • params = The buffer to receive the params (if any)
    Returns
    0 if successful or <0 = a specific error code.

◆ ReadTagSeqAlgNull()

HRESULT GrayLib::cASNReader::ReadTagSeqAlgNull ( OUT cASNBuf alg)

Retrieve an AlgorithmIdentifier ASN.1 sequence with nullptr or no params. (eg for signatures) Updates the pointer to immediately behind the full AlgorithmIdentifier.

  • alg = The buffer to receive the OID
    Returns
    0 if successful or a specific ASN.1 or MPI error code.
    AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL }

◆ ReadTagSequence()

HRESULT GrayLib::cASNReader::ReadTagSequence ( ASN_TYPE  tag,
cASNSequence head 
)

Parses and splits an ASN.1 "SEQUENCE OF <tag>" Updated the pointer to immediately behind the full sequence tag.

  • nTag = Type of sequence
  • head = First variable in the chain to fill
    Returns
    0 if successful or a specific ASN.1 error code.

◆ ReadTagTime()

HRESULT GrayLib::cASNReader::ReadTagTime ( OUT cTimeUnits time)

TIME_FORMAT_ASN Data for ASN_TimeGeneral Time ::= CHOICE { utcTime UTCTime, generalTime GeneralizedTime } e.g. "" or "" compliment of WriteTagTime()

◆ ReadTime()

HRESULT GrayLib::cASNReader::ReadTime ( const char *  pData,
size_t  nSizeData,
bool  bGeneral,
OUT cTimeUnits time 
)
static

"YYYYMMDDhhmmss" or "YYMMDDhhmmss"

  • bGeneral = ASN_TimeGeneral else ASN_TimeUTC data body compliment of WriteTagTime() e.g. "20131231235959" for December 31st 2013 at 23:59:59 TIME_FORMAT_ASN

◆ ResetReader()

void GrayLib::cASNReader::ResetReader ( )
inline

Seek back to start.

◆ SetBlock()

void GrayLib::cASNReader::SetBlock ( const BYTE *  p,
size_t  nSize 
)
inline

Friends And Related Function Documentation

◆ cASNBuf

friend class cASNBuf
friend

◆ cASNSequence

friend class cASNSequence
friend

Member Data Documentation

◆ m_pInpCur

const BYTE* GrayLib::cASNReader::m_pInpCur
protected

Current read position in cMemBlock. This Advances.


The documentation for this class was generated from the following files: