You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2010/06/05 00:34:13 UTC
svn commit: r951599 - in
/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows:
SecureRandomImpl.cpp SecureRandomImpl.h
Author: tabish
Date: Fri Jun 4 22:34:13 2010
New Revision: 951599
URL: http://svn.apache.org/viewvc?rev=951599&view=rev
Log:
Windows version of the SecureRandomImpl
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp?rev=951599&r1=951598&r2=951599&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.cpp Fri Jun 4 22:34:13 2010
@@ -17,30 +17,133 @@
#include "SecureRandomImpl.h"
+#include <decaf/lang/Exception.h>
+#include <decaf/lang/exceptions/RuntimeException.h>
+#include <decaf/lang/exceptions/NullPointerException.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
+#include <decaf/util/Random.h>
+
+#undef _WIN32_WINNT
+#define _WIN32_WINNT 0x0500
+
+#include <windows.h>
+#include <wincrypt.h>
+
using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+using namespace decaf::util;
using namespace decaf::security;
using namespace decaf::internal;
using namespace decaf::internal::security;
////////////////////////////////////////////////////////////////////////////////
+namespace decaf {
+namespace internal {
+namespace security {
+
+ class SRNGData {
+ public:
+
+ std::auto_ptr<Random> random;
+
+ SRNGData() : random() {
+ }
+
+ };
+
+}}}
+
+////////////////////////////////////////////////////////////////////////////////
SecureRandomImpl::SecureRandomImpl() {
+
+ this->config = new SRNGData();
+
+ try{
+
+ HCRYPTPROV provider;
+ int result = CryptAcquireContext( &provider, NULL, NULL, PROV_DSS, CRYPT_VERIFYCONTEXT );
+
+ // Defaults to the Decaf version.
+ if( result == 0 ) {
+ this->config->random.reset( new Random() );
+ } else {
+ CryptReleaseContext( provider, 0 );
+ }
+ }
+ DECAF_CATCH_RETHROW( Exception )
+ DECAF_CATCHALL_THROW( Exception )
}
////////////////////////////////////////////////////////////////////////////////
SecureRandomImpl::~SecureRandomImpl() {
+ try{
+ delete this->config;
+ }
+ DECAF_CATCH_NOTHROW( Exception )
+ DECAF_CATCHALL_NOTHROW()
}
////////////////////////////////////////////////////////////////////////////////
void SecureRandomImpl::providerSetSeed( const unsigned char* seed, int size ) {
+ // Only seed the default random, the other sources don't need a seed.
+ if( this->config->random.get() != NULL ) {
+
+ for( int i = 0; i < size; i++ ) {
+ this->config->random->setSeed( (long long)seed[i] );
+ }
+ }
}
////////////////////////////////////////////////////////////////////////////////
void SecureRandomImpl::providerNextBytes( unsigned char* bytes, int numBytes ) {
+ if( bytes == NULL ) {
+ throw NullPointerException(
+ __FILE__, __LINE__, "Byte Buffer passed cannot be NULL." );
+ }
+
+ if( numBytes < 0 ) {
+ throw IllegalArgumentException(
+ __FILE__, __LINE__, "Number of bytes to read was negative: %d", numBytes );
+ }
+
+ if( this->config->random.get() == NULL ) {
+
+ HCRYPTPROV provider;
+
+ int result;
+
+ result = CryptAcquireContext( &provider, NULL, NULL, PROV_DSS, CRYPT_VERIFYCONTEXT );
+
+ if ( result == 0 ) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to acquire the system cryptographic provider." );
+ }
+
+ result = CryptGenRandom( provider, numBytes, bytes );
+
+ if( result == 0 ) {
+ throw RuntimeException(
+ __FILE__, __LINE__, "Failed to get random bytes from the cryptographic provider." );
+ }
+
+ CryptReleaseContext( provider, 0 );
+
+ } else {
+ this->config->random->nextBytes( bytes, numBytes );
+ }
}
////////////////////////////////////////////////////////////////////////////////
unsigned char* SecureRandomImpl::providerGenerateSeed( int numBytes ) {
+ if( numBytes == 0 ) {
+ return NULL;
+ }
+
+ unsigned char* buffer = new unsigned char[numBytes];
+ providerNextBytes( buffer, numBytes );
+ return buffer;
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h?rev=951599&r1=951598&r2=951599&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows/SecureRandomImpl.h Fri Jun 4 22:34:13 2010
@@ -26,12 +26,26 @@ namespace decaf {
namespace internal {
namespace security {
+ class SRNGData;
+
+ /**
+ * Secure Random Number Generator for Windows based platforms that attempts to obtain
+ * secure bytes with high entropy from known sources. If the platform does not have
+ * a source of secure bytes then the platform random number generator is used if one
+ * exists otherwise the Decaf RNG is used as a last resort.
+ *
+ * @since 1.0
+ */
class DECAF_API SecureRandomImpl : public decaf::security::SecureRandomSpi {
private:
SecureRandomImpl( const SecureRandomImpl& );
SecureRandomImpl& operator= ( const SecureRandomImpl& );
+ private:
+
+ SRNGData* config;
+
public:
SecureRandomImpl();