Skip to content

Commit 0a42a43

Browse files
committed
Add CCryptoKeyBase_RawBuffer::EnsureRawDataPtrAvailable
Remove asserts in OpenSSL EVP implemtnation of CEC25519KeyBase::Wipe. We might have a raw copy, and that's OK. (This change was needed to fix some bugs in code that is not part of the opensource code.) P4:7307240
1 parent 6e8928b commit 0a42a43

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/common/crypto_25519_openssl.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ uint32 CEC25519KeyBase::GetRawData( void *pData ) const
7171

7272
void CEC25519KeyBase::Wipe()
7373
{
74-
// We should never be using the raw buffer
75-
Assert( CCryptoKeyBase_RawBuffer::m_pData == nullptr );
76-
Assert( CCryptoKeyBase_RawBuffer::m_cbData == 0 );
74+
if ( m_evp_pkey )
75+
{
76+
EVP_PKEY_free( (EVP_PKEY*)m_evp_pkey );
77+
m_evp_pkey = nullptr;
78+
}
7779

78-
EVP_PKEY_free( (EVP_PKEY*)m_evp_pkey );
79-
m_evp_pkey = nullptr;
80+
// Wipe raw buffer if we kept a separate copy
81+
CCryptoKeyBase_RawBuffer::Wipe();
8082
}
8183

8284
bool CEC25519KeyBase::SetRawData( const void *pData, size_t cbData )

src/common/keypair.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,26 @@ void CCryptoKeyBase_RawBuffer::InternalWipeRawDataBuffer()
457457
m_cbData = 0;
458458
}
459459

460+
bool CCryptoKeyBase_RawBuffer::EnsureRawDataPtrAvailable()
461+
{
462+
if ( !m_pData )
463+
{
464+
uint32 cbData = GetRawData(nullptr);
465+
if ( cbData == 0 )
466+
return false;
467+
m_pData = (uint8*)malloc( cbData );
468+
if ( !m_pData )
469+
return false;
470+
m_cbData = cbData;
471+
if ( GetRawData( m_pData ) != cbData )
472+
{
473+
InternalWipeRawDataBuffer();
474+
return false;
475+
}
476+
}
477+
return true;
478+
}
479+
460480
//-----------------------------------------------------------------------------
461481
// CEC25519PrivateKeyBase
462482
//-----------------------------------------------------------------------------

src/common/keypair.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,17 @@ class CCryptoKeyBase_RawBuffer : public CCryptoKeyBase
120120
virtual uint32 GetRawData( void *pData ) const override;
121121
virtual void Wipe() override;
122122

123+
// Access the raw data. This might not be available, depending on the crypto
124+
// implementation! Avoid using this except for very specialized situations.
125+
// Instead, prefer the base class functions such as GetRawData, BMatchesRawData, etc
123126
const uint8 *GetRawDataPtr() const { return m_pData; }
124127
uint32 GetRawDataSize() const { return m_cbData; }
125128

129+
// Make sure that we can call GetRawDataPtr(), perhaps making a copy
130+
// of the key from the crypto provider into our local buffer if necessary.
131+
// Returns false if the key is invalid or we fail to alloc memory.
132+
bool EnsureRawDataPtrAvailable();
133+
126134
#ifdef DBGFLAG_VALIDATE
127135
virtual void Validate( CValidator &validator, const char *pchName ) const; // Validate our internal structures
128136
#endif // DBGFLAG_VALIDATE

0 commit comments

Comments
 (0)