upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/db.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/db.cpp')
-rw-r--r--src/db.cpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/db.cpp b/src/db.cpp
index c8642e9..244d5a7 100644
--- a/src/db.cpp
+++ b/src/db.cpp
@@ -1404,6 +1404,13 @@ static bool EncryptPrivateKeyForWallet(const CKeyingMaterial& vchMasterKey,
1404 return true; 1404 return true;
1405} 1405}
1406 1406
1407static void SecureClearBytes(vector<unsigned char>& vch)
1408{
1409 if (!vch.empty())
1410 OPENSSL_cleanse(&vch[0], vch.size());
1411 vch.clear();
1412}
1413
1407bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& strErrorRet) 1414bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& strErrorRet)
1408{ 1415{
1409 strBackupRet.clear(); 1416 strBackupRet.clear();
@@ -1423,6 +1430,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st
1423 map<vector<unsigned char>, CPrivKey> mapPlainKeys; 1430 map<vector<unsigned char>, CPrivKey> mapPlainKeys;
1424 vector<unsigned char> vchPlainHDMaster; 1431 vector<unsigned char> vchPlainHDMaster;
1425 vector<unsigned char> vchPlainHDChainCode; 1432 vector<unsigned char> vchPlainHDChainCode;
1433 bool fHadHDSeed = false;
1426 unsigned int nHDNextSnapshot = 0; 1434 unsigned int nHDNextSnapshot = 0;
1427 { 1435 {
1428 CRITICAL_BLOCK(cs_mapKeys) 1436 CRITICAL_BLOCK(cs_mapKeys)
@@ -1433,6 +1441,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st
1433 { 1441 {
1434 vchPlainHDMaster = vchHDMaster; 1442 vchPlainHDMaster = vchHDMaster;
1435 vchPlainHDChainCode = vchHDChainCode; 1443 vchPlainHDChainCode = vchHDChainCode;
1444 fHadHDSeed = !vchPlainHDMaster.empty() || !vchPlainHDChainCode.empty();
1436 nHDNextSnapshot = nHDNext; 1445 nHDNextSnapshot = nHDNext;
1437 } 1446 }
1438 } 1447 }
@@ -1483,11 +1492,13 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st
1483 1492
1484 vector<unsigned char> vchCryptedHDMasterNew; 1493 vector<unsigned char> vchCryptedHDMasterNew;
1485 vector<unsigned char> vchCryptedHDChainCodeNew; 1494 vector<unsigned char> vchCryptedHDChainCodeNew;
1486 if (!vchPlainHDMaster.empty() || !vchPlainHDChainCode.empty()) 1495 if (fHadHDSeed)
1487 { 1496 {
1488 if (vchPlainHDMaster.size() != 32 || vchPlainHDChainCode.size() != 32) 1497 if (vchPlainHDMaster.size() != 32 || vchPlainHDChainCode.size() != 32)
1489 { 1498 {
1490 strErrorRet = "wallet has an incomplete HD seed"; 1499 strErrorRet = "wallet has an incomplete HD seed";
1500 SecureClearBytes(vchPlainHDMaster);
1501 SecureClearBytes(vchPlainHDChainCode);
1491 return false; 1502 return false;
1492 } 1503 }
1493 if (!EncryptSecret(vchMasterKey, vchPlainHDMaster, 1504 if (!EncryptSecret(vchMasterKey, vchPlainHDMaster,
@@ -1496,8 +1507,12 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st
1496 WalletSecretIV("hdchaincode"), vchCryptedHDChainCodeNew)) 1507 WalletSecretIV("hdchaincode"), vchCryptedHDChainCodeNew))
1497 { 1508 {
1498 strErrorRet = "could not encrypt the HD seed"; 1509 strErrorRet = "could not encrypt the HD seed";
1510 SecureClearBytes(vchPlainHDMaster);
1511 SecureClearBytes(vchPlainHDChainCode);
1499 return false; 1512 return false;
1500 } 1513 }
1514 SecureClearBytes(vchPlainHDMaster);
1515 SecureClearBytes(vchPlainHDChainCode);
1501 } 1516 }
1502 1517
1503 vector<pair<int64, vector<unsigned char> > > vNewPool; 1518 vector<pair<int64, vector<unsigned char> > > vNewPool;
@@ -1506,7 +1521,7 @@ bool EncryptWallet(const string& strPassphrase, string& strBackupRet, string& st
1506 for (int i = 0; i < KEYPOOL_SIZE; i++) 1521 for (int i = 0; i < KEYPOOL_SIZE; i++)
1507 { 1522 {
1508 CKey key; 1523 CKey key;
1509 if (!vchPlainHDMaster.empty()) 1524 if (fHadHDSeed)
1510 { 1525 {
1511 string strDeriveError; 1526 string strDeriveError;
1512 if (!DeriveHDKey(nHDNextNew, key, strDeriveError)) 1527 if (!DeriveHDKey(nHDNextNew, key, strDeriveError))