mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
wallet: refactor: dedup master key decryption
This commit is contained in:
parent
846545947c
commit
5a92077fd5
1 changed files with 17 additions and 8 deletions
|
@ -605,19 +605,30 @@ static bool EncryptMasterKey(const SecureString& wallet_passphrase, const CKeyin
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
static bool DecryptMasterKey(const SecureString& wallet_passphrase, const CMasterKey& master_key, CKeyingMaterial& plain_master_key)
|
||||||
{
|
{
|
||||||
CCrypter crypter;
|
CCrypter crypter;
|
||||||
|
if (!crypter.SetKeyFromPassphrase(wallet_passphrase, master_key.vchSalt, master_key.nDeriveIterations, master_key.nDerivationMethod)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!crypter.Decrypt(master_key.vchCryptedKey, plain_master_key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CWallet::Unlock(const SecureString& strWalletPassphrase)
|
||||||
|
{
|
||||||
CKeyingMaterial _vMasterKey;
|
CKeyingMaterial _vMasterKey;
|
||||||
|
|
||||||
{
|
{
|
||||||
LOCK(cs_wallet);
|
LOCK(cs_wallet);
|
||||||
for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
for (const MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
||||||
{
|
{
|
||||||
if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
|
if (!DecryptMasterKey(strWalletPassphrase, pMasterKey.second, _vMasterKey)) {
|
||||||
return false;
|
|
||||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
|
|
||||||
continue; // try another master key
|
continue; // try another master key
|
||||||
|
}
|
||||||
if (Unlock(_vMasterKey)) {
|
if (Unlock(_vMasterKey)) {
|
||||||
// Now that we've unlocked, upgrade the key metadata
|
// Now that we've unlocked, upgrade the key metadata
|
||||||
UpgradeKeyMetadata();
|
UpgradeKeyMetadata();
|
||||||
|
@ -638,14 +649,12 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
|
||||||
LOCK2(m_relock_mutex, cs_wallet);
|
LOCK2(m_relock_mutex, cs_wallet);
|
||||||
Lock();
|
Lock();
|
||||||
|
|
||||||
CCrypter crypter;
|
|
||||||
CKeyingMaterial _vMasterKey;
|
CKeyingMaterial _vMasterKey;
|
||||||
for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
for (MasterKeyMap::value_type& pMasterKey : mapMasterKeys)
|
||||||
{
|
{
|
||||||
if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
|
if (!DecryptMasterKey(strOldWalletPassphrase, pMasterKey.second, _vMasterKey)) {
|
||||||
return false;
|
|
||||||
if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, _vMasterKey))
|
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
if (Unlock(_vMasterKey))
|
if (Unlock(_vMasterKey))
|
||||||
{
|
{
|
||||||
if (!EncryptMasterKey(strNewWalletPassphrase, _vMasterKey, pMasterKey.second)) {
|
if (!EncryptMasterKey(strNewWalletPassphrase, _vMasterKey, pMasterKey.second)) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue