Implement an mlock()'d string class for storing passphrases

SecureString is identical to std::string except with secure_allocator
substituting for std::allocator. This makes casting between them
impossible, so converting between the two at API boundaries requires
calling ::c_str() for now.
This commit is contained in:
Dylan Noblesmith
2011-11-26 06:02:04 +00:00
parent d8b8640863
commit 94f778bdeb
9 changed files with 40 additions and 54 deletions

View File

@@ -71,16 +71,17 @@ void AskPassphraseDialog::setModel(WalletModel *model)
void AskPassphraseDialog::accept()
{
std::string oldpass, newpass1, newpass2;
SecureString oldpass, newpass1, newpass2;
if(!model)
return;
// TODO: mlock memory / munlock on return so they will not be swapped out, really need "mlockedstring" wrapper class to do this safely
oldpass.reserve(MAX_PASSPHRASE_SIZE);
newpass1.reserve(MAX_PASSPHRASE_SIZE);
newpass2.reserve(MAX_PASSPHRASE_SIZE);
oldpass.assign(ui->passEdit1->text().toStdString());
newpass1.assign(ui->passEdit2->text().toStdString());
newpass2.assign(ui->passEdit3->text().toStdString());
// TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string)
// Alternately, find a way to make this input mlock()'d to begin with.
oldpass.assign(ui->passEdit1->text().toStdString().c_str());
newpass1.assign(ui->passEdit2->text().toStdString().c_str());
newpass2.assign(ui->passEdit3->text().toStdString().c_str());
switch(mode)
{

View File

@@ -200,7 +200,7 @@ WalletModel::EncryptionStatus WalletModel::getEncryptionStatus() const
}
}
bool WalletModel::setWalletEncrypted(bool encrypted, const std::string &passphrase)
bool WalletModel::setWalletEncrypted(bool encrypted, const SecureString &passphrase)
{
if(encrypted)
{
@@ -214,7 +214,7 @@ bool WalletModel::setWalletEncrypted(bool encrypted, const std::string &passphra
}
}
bool WalletModel::setWalletLocked(bool locked, const std::string &passPhrase)
bool WalletModel::setWalletLocked(bool locked, const SecureString &passPhrase)
{
if(locked)
{
@@ -228,7 +228,7 @@ bool WalletModel::setWalletLocked(bool locked, const std::string &passPhrase)
}
}
bool WalletModel::changePassphrase(const std::string &oldPass, const std::string &newPass)
bool WalletModel::changePassphrase(const SecureString &oldPass, const SecureString &newPass)
{
bool retval;
CRITICAL_BLOCK(wallet->cs_wallet)

View File

@@ -2,7 +2,8 @@
#define WALLETMODEL_H
#include <QObject>
#include <string>
#include "util.h"
class OptionsModel;
class AddressTableModel;
@@ -72,10 +73,10 @@ public:
SendCoinsReturn sendCoins(const QList<SendCoinsRecipient> &recipients);
// Wallet encryption
bool setWalletEncrypted(bool encrypted, const std::string &passphrase);
bool setWalletEncrypted(bool encrypted, const SecureString &passphrase);
// Passphrase only needed when unlocking
bool setWalletLocked(bool locked, const std::string &passPhrase=std::string());
bool changePassphrase(const std::string &oldPass, const std::string &newPass);
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
// RAI object for unlocking wallet, returned by requestUnlock()
class UnlockContext