From commits-return-13701-apmail-activemq-commits-archive=activemq.apache.org@activemq.apache.org Fri Jun 04 22:34:38 2010 Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 79866 invoked from network); 4 Jun 2010 22:34:37 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 4 Jun 2010 22:34:37 -0000 Received: (qmail 98586 invoked by uid 500); 4 Jun 2010 22:34:37 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 98546 invoked by uid 500); 4 Jun 2010 22:34:37 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 98539 invoked by uid 99); 4 Jun 2010 22:34:37 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Jun 2010 22:34:37 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Jun 2010 22:34:35 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D3FCA23888EA; Fri, 4 Jun 2010 22:34:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r951599 - in /activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/security/windows: SecureRandomImpl.cpp SecureRandomImpl.h Date: Fri, 04 Jun 2010 22:34:13 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100604223413.D3FCA23888EA@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 +#include +#include +#include +#include + +#undef _WIN32_WINNT +#define _WIN32_WINNT 0x0500 + +#include +#include + 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; + + 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();