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

#include <cPEM.h>

Inheritance diagram for GrayLib::cPEM:
Gray::cHeapBlock Gray::cMemBlock

Public Member Functions

 cPEM () noexcept
 
 ~cPEM () noexcept
 
HRESULT ReadPEM (const char *pszType, const BYTE *pData, const BYTE *pPassword=nullptr, size_t nPassSize=0)
 
 UNITTEST_FRIEND (cPEM)
 
- Public Member Functions inherited from Gray::cHeapBlock
 cHeapBlock () noexcept
 
 cHeapBlock (const THIS_t &ref)
 
 cHeapBlock (THIS_t &&ref) noexcept
 
 cHeapBlock (size_t nSize)
 
 cHeapBlock (const void *pDataCopy, size_t nSize)
 
 ~cHeapBlock ()
 
THIS_toperator= (const THIS_t &ref)
 
THIS_toperator= (THIS_t &&ref)
 
bool isValidRead () const noexcept
 
bool isCorrupt () const noexcept
 
size_t get_AllocSize () const
 
size_t GetHeapStats (OUT ITERATE_t &iAllocCount) const
 
void Free ()
 
void FreeSecure ()
 
void SetHeapBlock (void *pData, size_t nSize)
 
void DetachHeapBlock ()
 
bool Alloc (size_t nSize)
 
bool Alloc (const void *pData, size_t nSize)
 
bool ReAlloc (size_t nSize)
 
bool ReAlloc (const void *pData, size_t nSize)
 
bool ReAllocLazy (size_t iSizeNew)
 
bool SetCopy (const cHeapBlock &rSrc)
 
void * get_Data () const noexcept
 
BYTE * get_DataBytes () const noexcept
 
char * get_DataA () const noexcept
 
wchar_t * get_DataW () const noexcept
 
 operator void * () const noexcept
 
 operator BYTE * () const noexcept
 
 operator char * () const noexcept
 
- 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 const char * FindHeader (const char *pData, const char *pszType)
 
static HRESULT GRAYCALL WriteToPEM (char *pOut, StrLen_t nOutLen, const char *pszType, const BYTE *pDerData, size_t nSizeDer)
 
- 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)
 

Public Attributes

size_t m_nSizeRead
 The uncompressed read size. More...
 

Static Public Attributes

static const char k_BEGINf [19] = "-----BEGIN %s-----"
 
static const char k_ENDf [17] = "-----END %s-----"
 

Static Protected Member Functions

static bool GRAYCALL ReadIV (const char *s, BYTE *pIV, size_t nIVSize)
 
static void GRAYCALL DecryptPrep (BYTE *pKey, size_t nKeySize, BYTE *pIV, const BYTE *pPassword, size_t nPassSize)
 
static void GRAYCALL DecryptDES (BYTE pIV[8], BYTE *pBuffer, size_t nSizeBuf, const BYTE *pPassword, size_t nPassSize)
 
static void GRAYCALL DecryptDES3 (BYTE pIV[8], BYTE *pBuffer, size_t nSizeBuf, const BYTE *pPassword, size_t nPassSize)
 
static void GRAYCALL DecryptAES (BYTE pIV[16], UINT nKeySize, BYTE *pBuffer, size_t nSizeBuf, const BYTE *pPassword, size_t nPassSize)
 

Additional Inherited Members

- 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
 

Detailed Description

PEM file/format read/write helper Certificate/Key File format. PEM or Privacy Enhanced Mail is a Base64 encoded DER certificate. X509_FORMAT_PEM PEM certificates are frequently used for web servers as they can easily be translated into readable data using a simple text editor. Generally when a PEM encoded file is opened in a text editor, it contains very distinct headers and footers. These files may also bear the .CER or the .CRT extension. But the extension may also be used by other formats.

Constructor & Destructor Documentation

◆ cPEM()

GrayLib::cPEM::cPEM ( )
inlinenoexcept

◆ ~cPEM()

GrayLib::cPEM::~cPEM ( )
inlinenoexcept

Member Function Documentation

◆ DecryptAES()

void GRAYCALL GrayLib::cPEM::DecryptAES ( BYTE  pIV[16],
UINT  nKeySize,
BYTE *  pBuffer,
size_t  nSizeBuf,
const BYTE *  pPassword,
size_t  nPassSize 
)
staticprotected

Decrypt with AES-XXX-CBC, using PBKDF1 for key derivation

◆ DecryptDES()

void GRAYCALL GrayLib::cPEM::DecryptDES ( BYTE  pIV[8],
BYTE *  pBuffer,
size_t  nSizeBuf,
const BYTE *  pPassword,
size_t  nPassSize 
)
staticprotected

Decrypt with DES-CBC, using PBKDF1 for key derivation

◆ DecryptDES3()

void GRAYCALL GrayLib::cPEM::DecryptDES3 ( BYTE  pIV[8],
BYTE *  pBuffer,
size_t  nSizeBuf,
const BYTE *  pPassword,
size_t  nPassSize 
)
staticprotected

Decrypt with 3DES-CBC, using PBKDF1 for key derivation

◆ DecryptPrep()

void GRAYCALL GrayLib::cPEM::DecryptPrep ( BYTE *  pKey,
size_t  nKeySize,
BYTE *  pIV,
const BYTE *  pPassword,
size_t  nPassSize 
)
staticprotected

pKey[ 0..15] = MD5(pPassword || IV)

◆ FindHeader()

const char * GrayLib::cPEM::FindHeader ( const char *  pData,
const char *  pszType 
)
static

◆ ReadIV()

bool GRAYCALL GrayLib::cPEM::ReadIV ( const char *  s,
BYTE *  pIV,
size_t  nIVSize 
)
staticprotected

Read a 16-byte hex string and convert it to binary

◆ ReadPEM()

HRESULT GrayLib::cPEM::ReadPEM ( const char *  pszType,
const BYTE *  pData,
const BYTE *  pPassword = nullptr,
size_t  nPassSize = 0 
)

Read a buffer for PEM information and store the resulting pData into the specified context buffers.

  • pszType = header string to seek and expect
  • pData = source data to look in
  • pPassword = password for decryption (can be nullptr)
  • nPassSize = length of password
Note
Attempts to check password correctness by verifying if the decrypted text starts with an ASN.1 sequence of appropriate length
Returns
length on success, or <0 Error code: RPC_E_INVALID_HEADER = cant find header. no header/footer. HRESULT_WIN32_C(ERROR_BAD_FORMAT) = badly formed. probably cant recover from this. HRESULT_WIN32_C(ERROR_INVALID_DATA) = arguments are missing or unknown. Ignore this? EAS_E_CURRENT_USER_HAS_BLANK_PASSWORD = has not provided a password. It is required. HRESULT_WIN32_C(ERROR_INVALID_PASSWORD) = mismatching password.

◆ UNITTEST_FRIEND()

GrayLib::cPEM::UNITTEST_FRIEND ( cPEM  )

◆ WriteToPEM()

HRESULT GRAYCALL GrayLib::cPEM::WriteToPEM ( char *  pOut,
StrLen_t  nOutLen,
const char *  pszType,
const BYTE *  pDerData,
size_t  nSizeDer 
)
static

Write a buffer of PEM information from a DER encoded buffer.

  • pOut = buffer to write to
  • nOutLen = length of output buffer
  • pszType = header string to write
  • pDerData = DER data to write
  • nSizeDer = length of the DER data
    Returns
    > 0 = length written.

Member Data Documentation

◆ k_BEGINf

const char GrayLib::cPEM::k_BEGINf = "-----BEGIN %s-----"
static

◆ k_ENDf

const char GrayLib::cPEM::k_ENDf = "-----END %s-----"
static

◆ m_nSizeRead

size_t GrayLib::cPEM::m_nSizeRead

The uncompressed read size.


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