Skip to content

Commit 1838b62

Browse files
committed
Break out code that sets/wipes the raw buffer into separate functions
This is so that derived classes can use them more easily. (Specifically, to be able to set the raw buffer without the virtual Wipe() being called.) Also: WIPE NEEDS TO SECURELY WIPE, not just free the buffer! P4:7307228
1 parent a1ec953 commit 1838b62

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/common/keypair.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,11 @@ bool CCryptoKeyBase::LoadFromAndWipeBuffer( void *pBuffer, size_t cBytes )
391391

392392
CCryptoKeyBase_RawBuffer::~CCryptoKeyBase_RawBuffer()
393393
{
394-
Wipe();
394+
// Note that we don't call virtual Wipe() here. We're in a
395+
// destructor, so it would just call our own Wipe(),
396+
// anyway, but that relies on a relatively subtle aspect
397+
// ot C++ destructor semantics, and this is more clear.
398+
InternalWipeRawDataBuffer();
395399
}
396400

397401
bool CCryptoKeyBase_RawBuffer::IsValid() const
@@ -408,7 +412,12 @@ uint32 CCryptoKeyBase_RawBuffer::GetRawData( void *pData ) const
408412

409413
bool CCryptoKeyBase_RawBuffer::SetRawData( const void *pData, size_t cbData )
410414
{
411-
Wipe();
415+
return InternalSetRawDataBuffer( pData, cbData );
416+
}
417+
418+
bool CCryptoKeyBase_RawBuffer::InternalSetRawDataBuffer( const void *pData, size_t cbData )
419+
{
420+
InternalWipeRawDataBuffer();
412421
m_pData = (uint8*)malloc( cbData );
413422
if ( !m_pData )
414423
return false;
@@ -418,9 +427,15 @@ bool CCryptoKeyBase_RawBuffer::SetRawData( const void *pData, size_t cbData )
418427
}
419428

420429
void CCryptoKeyBase_RawBuffer::Wipe()
430+
{
431+
InternalWipeRawDataBuffer();
432+
}
433+
434+
void CCryptoKeyBase_RawBuffer::InternalWipeRawDataBuffer()
421435
{
422436
if ( m_pData )
423437
{
438+
SecureZeroMemory( m_pData, m_cbData );
424439
free( m_pData );
425440
m_pData = nullptr;
426441
}

src/common/keypair.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ class CCryptoKeyBase_RawBuffer : public CCryptoKeyBase
126126

127127
protected:
128128
virtual bool SetRawData( const void *pData, size_t cbData ) override;
129+
void InternalWipeRawDataBuffer();
130+
bool InternalSetRawDataBuffer( const void *pData, size_t cbData );
129131
inline CCryptoKeyBase_RawBuffer( ECryptoKeyType keyType ) : CCryptoKeyBase( keyType ), m_pData( nullptr ), m_cbData( 0 ) {}
130132

131133
uint8 *m_pData;

0 commit comments

Comments
 (0)