Skip to content

Commit

Permalink
openssl: abstract out OPENSSL_cleanse
Browse files Browse the repository at this point in the history
This makes it easier for us to replace it if desired, since it's now only in
one spot. Also, it avoids the openssl include from allocators.h, which
essentially forced openssl to be included from every compilation unit.

Signed-off-by: observerdev <dev@obsr.org>
  • Loading branch information
theuni authored and observerdev committed Feb 12, 2019
1 parent 7a3cbdc commit 7d2c20a
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Expand Up @@ -158,6 +158,7 @@ BITCOIN_CORE_H = \
sporkdb.h \
stakeinput.h \
streams.h \
support/cleanse.h \
sync.h \
threadsafety.h \
timedata.h \
Expand Down Expand Up @@ -385,6 +386,7 @@ libbitcoin_util_a_SOURCES = \
clientversion.cpp \
random.cpp \
rpc/protocol.cpp \
support/cleanse.cpp \
sync.cpp \
uint256.cpp \
util.cpp \
Expand Down
10 changes: 5 additions & 5 deletions src/allocators.h
Expand Up @@ -6,6 +6,8 @@
#ifndef BITCOIN_ALLOCATORS_H
#define BITCOIN_ALLOCATORS_H

#include "support/cleanse.h"

#include <map>
#include <string.h>
#include <string>
Expand All @@ -14,8 +16,6 @@
#include <boost/thread/mutex.hpp>
#include <boost/thread/once.hpp>

#include <openssl/crypto.h> // for OPENSSL_cleanse()

/**
* Thread-safe class to keep track of locked (ie, non-swappable) memory pages.
*
Expand Down Expand Up @@ -173,7 +173,7 @@ void LockObject(const T& t)
template <typename T>
void UnlockObject(const T& t)
{
OPENSSL_cleanse((void*)(&t), sizeof(T));
memory_cleanse((void*)(&t), sizeof(T));
LockedPageManager::Instance().UnlockRange((void*)(&t), sizeof(T));
}

Expand Down Expand Up @@ -216,7 +216,7 @@ struct secure_allocator : public std::allocator<T> {
void deallocate(T* p, std::size_t n)
{
if (p != NULL) {
OPENSSL_cleanse(p, sizeof(T) * n);
memory_cleanse(p, sizeof(T) * n);
LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
}
std::allocator<T>::deallocate(p, n);
Expand Down Expand Up @@ -253,7 +253,7 @@ struct zero_after_free_allocator : public std::allocator<T> {
void deallocate(T* p, std::size_t n)
{
if (p != NULL)
OPENSSL_cleanse(p, sizeof(T) * n);
memory_cleanse(p, sizeof(T) * n);
std::allocator<T>::deallocate(p, n);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/base58.cpp
Expand Up @@ -190,7 +190,7 @@ bool CBase58Data::SetString(const char* psz, unsigned int nVersionBytes)
vchData.resize(vchTemp.size() - nVersionBytes);
if (!vchData.empty())
memcpy(&vchData[0], &vchTemp[nVersionBytes], vchData.size());
OPENSSL_cleanse(&vchTemp[0], vchData.size());
memory_cleanse(&vchTemp[0], vchData.size());
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypter.cpp
Expand Up @@ -28,8 +28,8 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
(unsigned char*)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);

if (i != (int)WALLET_CRYPTO_KEY_SIZE) {
OPENSSL_cleanse(chKey, sizeof(chKey));
OPENSSL_cleanse(chIV, sizeof(chIV));
memory_cleanse(chKey, sizeof(chKey));
memory_cleanse(chIV, sizeof(chIV));
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/crypter.h
Expand Up @@ -85,8 +85,8 @@ class CCrypter

void CleanKey()
{
OPENSSL_cleanse(chKey, sizeof(chKey));
OPENSSL_cleanse(chIV, sizeof(chIV));
memory_cleanse(chKey, sizeof(chKey));
memory_cleanse(chIV, sizeof(chIV));
fKeySet = false;
}

Expand Down
2 changes: 0 additions & 2 deletions src/db.cpp
Expand Up @@ -21,8 +21,6 @@
#include <boost/thread.hpp>
#include <boost/version.hpp>

#include <openssl/rand.h>

using namespace std;
using namespace boost;

Expand Down
1 change: 0 additions & 1 deletion src/qt/paymentrequestplus.cpp
Expand Up @@ -13,7 +13,6 @@

#include <stdexcept>

#include <openssl/x509.h>
#include <openssl/x509_vfy.h>

#include <QDateTime>
Expand Down
2 changes: 2 additions & 0 deletions src/qt/paymentrequestplus.h
Expand Up @@ -14,6 +14,8 @@

#include "base58.h"

#include <openssl/x509.h>

#include <QByteArray>
#include <QList>
#include <QString>
Expand Down
1 change: 0 additions & 1 deletion src/qt/paymentserver.cpp
Expand Up @@ -19,7 +19,6 @@

#include <cstdlib>

#include <openssl/x509.h>
#include <openssl/x509_vfy.h>

#include <QApplication>
Expand Down
6 changes: 3 additions & 3 deletions src/random.cpp
Expand Up @@ -5,6 +5,7 @@

#include "random.h"

#include "support/cleanse.h"
#ifdef WIN32
#include "compat.h" // for Windows API
#endif
Expand All @@ -18,7 +19,6 @@
#include <sys/time.h>
#endif

#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/rand.h>

Expand All @@ -40,7 +40,7 @@ void RandAddSeed()
// Seed with CPU performance counter
int64_t nCounter = GetPerformanceCounter();
RAND_add(&nCounter, sizeof(nCounter), 1.5);
OPENSSL_cleanse((void*)&nCounter, sizeof(nCounter));
memory_cleanse((void*)&nCounter, sizeof(nCounter));
}

void RandAddSeedPerfmon()
Expand Down Expand Up @@ -71,7 +71,7 @@ void RandAddSeedPerfmon()
RegCloseKey(HKEY_PERFORMANCE_DATA);
if (ret == ERROR_SUCCESS) {
RAND_add(begin_ptr(vData), nSize, nSize / 100.0);
OPENSSL_cleanse(begin_ptr(vData), nSize);
memory_cleanse(begin_ptr(vData), nSize);
LogPrint("rand", "%s: %lu bytes\n", __func__, nSize);
} else {
static bool warned = false; // Warn only once
Expand Down
1 change: 1 addition & 0 deletions src/streams.h
Expand Up @@ -18,6 +18,7 @@
#include <map>
#include <set>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
Expand Down
13 changes: 13 additions & 0 deletions src/support/cleanse.cpp
@@ -0,0 +1,13 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "cleanse.h"

#include <openssl/crypto.h>

void memory_cleanse(void *ptr, size_t len)
{
OPENSSL_cleanse(ptr, len);
}
13 changes: 13 additions & 0 deletions src/support/cleanse.h
@@ -0,0 +1,13 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_SUPPORT_CLEANSE_H
#define BITCOIN_SUPPORT_CLEANSE_H

#include <stdlib.h>

void memory_cleanse(void *ptr, size_t len);

#endif // BITCOIN_SUPPORT_CLEANSE_H
1 change: 0 additions & 1 deletion src/util.cpp
Expand Up @@ -25,7 +25,6 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/crypto.h> // for OPENSSL_cleanse()
#include <openssl/evp.h>


Expand Down
3 changes: 1 addition & 2 deletions src/utilstrencodings.cpp
Expand Up @@ -16,7 +16,6 @@

#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/crypto.h> // for OPENSSL_cleanse()
#include <openssl/evp.h>


Expand Down Expand Up @@ -283,7 +282,7 @@ SecureString EncodeBase64Secure(const SecureString& input)
SecureString output(bptr->data, bptr->length);

// Cleanse secure data buffer from memory
OPENSSL_cleanse((void*)bptr->data, bptr->length);
memory_cleanse((void*)bptr->data, bptr->length);

// Free memory
BIO_free_all(b64);
Expand Down

0 comments on commit 7d2c20a

Please sign in to comment.