1
0
Fork 0

Initial C++ SSL classes.

This commit is contained in:
madmaxoft 2014-04-24 21:34:45 +02:00
parent 449cf77420
commit c701adbd24
7 changed files with 248 additions and 2 deletions

View File

@ -264,9 +264,11 @@ template class SizeChecker<UInt16, 2>;
// Same as assert but in all Self test builds
#ifdef SELF_TEST
#define assert_test(x) ( !!(x) || (assert(!#x), exit(1), 0))
#define assert_test(x) ( !!(x) || (assert(!#x), exit(1), 0))
#endif
#define SharedPtr std::tr1::shared_ptr
@ -296,7 +298,7 @@ T Clamp(T a_Value, T a_Min, T a_Max)
#ifndef TOLUA_TEMPLATE_BIND
#define TOLUA_TEMPLATE_BIND(x)
#define TOLUA_TEMPLATE_BIND(x)
#endif

View File

@ -0,0 +1,49 @@
// CtrDrbgContext.cpp
// Implements the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in PolarSSL
#include "Globals.h"
#include "CtrDrbgContext.h"
#include "EntropyContext.h"
cCtrDrbgContext::cCtrDrbgContext(void) :
m_EntropyContext(new cEntropyContext),
m_IsValid(false)
{
}
cCtrDrbgContext::cCtrDrbgContext(const SharedPtr<cEntropyContext> & a_EntropyContext) :
m_EntropyContext(a_EntropyContext),
m_IsValid(false)
{
}
int cCtrDrbgContext::Initialize(const void * a_Custom, size_t a_CustomSize)
{
if (m_IsValid)
{
// Already initialized
return 0;
}
int res = ctr_drbg_init(&m_CtrDrbg, entropy_func, &(m_EntropyContext->m_Entropy), (const unsigned char *)a_Custom, a_CustomSize);
m_IsValid = (res == 0);
return res;
}

View File

@ -0,0 +1,60 @@
// CtrDrbgContext.h
// Declares the cCtrDrbgContext class representing a wrapper over CTR-DRBG implementation in PolarSSL
#pragma once
#include "polarssl/ctr_drbg.h"
// fwd: EntropyContext.h
class cEntropyContext;
class cCtrDrbgContext
{
friend class cSslContext;
public:
/** Constructs the context with a new entropy context. */
cCtrDrbgContext(void);
/** Constructs the context with the specified entropy context. */
cCtrDrbgContext(const SharedPtr<cEntropyContext> & a_EntropyContext);
/** Initializes the context.
a_Custom is optional additional data to use for entropy, nullptr is accepted.
Returns 0 if successful, PolarSSL error code on failure. */
int Initialize(const void * a_Custom, size_t a_CustomSize);
/** Returns true if the object is valid (has been initialized properly) */
bool IsValid(void) const { return m_IsValid; }
/** Returns the internal context ptr. Only use in PolarSSL API calls. */
__declspec(deprecated) ctr_drbg_context * Get(void) { return &m_CtrDrbg; }
protected:
/** The entropy source used for generating the random */
SharedPtr<cEntropyContext> m_EntropyContext;
/** The random generator context */
ctr_drbg_context m_CtrDrbg;
/** Set to true if the object is valid (has been initialized properly) */
bool m_IsValid;
} ;

View File

@ -0,0 +1,29 @@
// EntropyContext.cpp
// Implements the cEntropyContext class representing a wrapper over entropy contexts in PolarSSL
#include "Globals.h"
#include "EntropyContext.h"
cEntropyContext::cEntropyContext(void)
{
entropy_init(&m_Entropy);
}
cEntropyContext::~cEntropyContext()
{
entropy_free(&m_Entropy);
}

View File

@ -0,0 +1,31 @@
// EntropyContext.h
// Declares the cEntropyContext class representing a wrapper over entropy contexts in PolarSSL
#pragma once
#include "polarssl/entropy.h"
class cEntropyContext
{
friend class cCtrDrbgContext;
public:
cEntropyContext(void);
~cEntropyContext();
protected:
entropy_context m_Entropy;
} ;

View File

@ -0,0 +1,38 @@
// X509Cert.cpp
// Implements the cX509Cert class representing a wrapper over X509 certs in PolarSSL
#include "Globals.h"
#include "X509Cert.h"
cX509Cert::cX509Cert(void)
{
x509_crt_init(&m_Cert);
}
cX509Cert::~cX509Cert()
{
x509_crt_free(&m_Cert);
}
int cX509Cert::Parse(const void * a_CertContents, size_t a_Size)
{
return x509_crt_parse(&m_Cert, (const unsigned char *)a_CertContents, a_Size);
}

37
src/PolarSSL++/X509Cert.h Normal file
View File

@ -0,0 +1,37 @@
// X509Cert.h
// Declares the cX509Cert class representing a wrapper over X509 certs in PolarSSL
#pragma once
#include "polarssl/x509_crt.h"
class cX509Cert
{
public:
cX509Cert(void);
~cX509Cert(void);
/** Parses the certificate chain data into the context.
Returns 0 on succes, or PolarSSL error code on failure. */
int Parse(const void * a_CertContents, size_t a_Size);
/** Returns the internal cert ptr. Only use in PolarSSL API calls. */
__declspec(deprecated) x509_crt * Get(void) { return &m_Cert; }
protected:
x509_crt m_Cert;
} ;