eliminate races: hold cs_KeyStore lock while reading fUseCrypto
This commit is contained in:
parent
5a5094bbb5
commit
d9fcc2b558
|
@ -185,24 +185,22 @@ static bool DecryptSaplingSpendingKey(const CKeyingMaterial& vMasterKey,
|
|||
return sk.expsk.full_viewing_key() == extfvk.fvk;
|
||||
}
|
||||
|
||||
// cs_KeyStore lock must be held by caller
|
||||
bool CCryptoKeyStore::SetCrypted()
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (fUseCrypto)
|
||||
return true;
|
||||
if (!(mapKeys.empty() && mapSproutSpendingKeys.empty() && mapSaplingSpendingKeys.empty()))
|
||||
return false;
|
||||
fUseCrypto = true;
|
||||
return true;
|
||||
return fUseCrypto = true;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::Lock()
|
||||
{
|
||||
if (!SetCrypted())
|
||||
return false;
|
||||
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!SetCrypted())
|
||||
return false;
|
||||
vMasterKey.clear();
|
||||
}
|
||||
|
||||
|
@ -291,7 +289,7 @@ bool CCryptoKeyStore::SetHDSeed(const HDSeed& seed)
|
|||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted()) {
|
||||
if (!fUseCrypto) {
|
||||
return CBasicKeyStore::SetHDSeed(seed);
|
||||
}
|
||||
|
||||
|
@ -316,9 +314,8 @@ bool CCryptoKeyStore::SetCryptedHDSeed(
|
|||
const uint256& seedFp,
|
||||
const std::vector<unsigned char>& vchCryptedSecret)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted()) {
|
||||
if (!fUseCrypto) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -329,14 +326,13 @@ bool CCryptoKeyStore::SetCryptedHDSeed(
|
|||
}
|
||||
|
||||
cryptedHDSeed = std::make_pair(seedFp, vchCryptedSecret);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::HaveHDSeed() const
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::HaveHDSeed();
|
||||
|
||||
return !cryptedHDSeed.second.empty();
|
||||
|
@ -345,7 +341,7 @@ bool CCryptoKeyStore::HaveHDSeed() const
|
|||
bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::GetHDSeed(seedOut);
|
||||
|
||||
if (cryptedHDSeed.second.empty())
|
||||
|
@ -356,9 +352,8 @@ bool CCryptoKeyStore::GetHDSeed(HDSeed& seedOut) const
|
|||
|
||||
bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::AddKeyPubKey(key, pubkey);
|
||||
|
||||
if (IsLocked())
|
||||
|
@ -369,30 +364,24 @@ bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
|
|||
if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret))
|
||||
return false;
|
||||
|
||||
if (!AddCryptedKey(pubkey, vchCryptedSecret))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return AddCryptedKey(pubkey, vchCryptedSecret);
|
||||
}
|
||||
|
||||
|
||||
bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!SetCrypted())
|
||||
return false;
|
||||
|
||||
mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::GetKey(address, keyOut);
|
||||
|
||||
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
|
||||
|
@ -402,15 +391,13 @@ bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
|
|||
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
|
||||
return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CKeyStore::GetPubKey(address, vchPubKeyOut);
|
||||
|
||||
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
|
||||
|
@ -419,15 +406,13 @@ bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) co
|
|||
vchPubKeyOut = (*mi).second.first;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::AddSproutSpendingKey(sk);
|
||||
|
||||
if (IsLocked())
|
||||
|
@ -441,19 +426,15 @@ bool CCryptoKeyStore::AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk
|
|||
if (!EncryptSecret(vMasterKey, vchSecret, address.GetHash(), vchCryptedSecret))
|
||||
return false;
|
||||
|
||||
if (!AddCryptedSproutSpendingKey(address, sk.receiving_key(), vchCryptedSecret))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return AddCryptedSproutSpendingKey(address, sk.receiving_key(), vchCryptedSecret);
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::AddSaplingSpendingKey(
|
||||
const libzcash::SaplingExtendedSpendingKey &sk,
|
||||
const libzcash::SaplingPaymentAddress &defaultAddr)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted()) {
|
||||
if (!fUseCrypto) {
|
||||
return CBasicKeyStore::AddSaplingSpendingKey(sk, defaultAddr);
|
||||
}
|
||||
|
||||
|
@ -470,11 +451,7 @@ bool CCryptoKeyStore::AddSaplingSpendingKey(
|
|||
return false;
|
||||
}
|
||||
|
||||
if (!AddCryptedSaplingSpendingKey(extfvk, vchCryptedSecret, defaultAddr)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return AddCryptedSaplingSpendingKey(extfvk, vchCryptedSecret, defaultAddr);
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::AddCryptedSproutSpendingKey(
|
||||
|
@ -482,14 +459,12 @@ bool CCryptoKeyStore::AddCryptedSproutSpendingKey(
|
|||
const libzcash::ReceivingKey &rk,
|
||||
const std::vector<unsigned char> &vchCryptedSecret)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!SetCrypted())
|
||||
return false;
|
||||
|
||||
mapCryptedSproutSpendingKeys[address] = vchCryptedSecret;
|
||||
mapNoteDecryptors.insert(std::make_pair(address, ZCNoteDecryption(rk)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -498,7 +473,6 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(
|
|||
const std::vector<unsigned char> &vchCryptedSecret,
|
||||
const libzcash::SaplingPaymentAddress &defaultAddr)
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!SetCrypted()) {
|
||||
return false;
|
||||
|
@ -510,15 +484,13 @@ bool CCryptoKeyStore::AddCryptedSaplingSpendingKey(
|
|||
}
|
||||
|
||||
mapCryptedSaplingSpendingKeys[extfvk] = vchCryptedSecret;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::GetSproutSpendingKey(address, skOut);
|
||||
|
||||
CryptedSproutSpendingKeyMap::const_iterator mi = mapCryptedSproutSpendingKeys.find(address);
|
||||
|
@ -527,15 +499,13 @@ bool CCryptoKeyStore::GetSproutSpendingKey(const libzcash::SproutPaymentAddress
|
|||
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second;
|
||||
return DecryptSproutSpendingKey(vMasterKey, vchCryptedSecret, address, skOut);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::GetSaplingSpendingKey(fvk, skOut);
|
||||
|
||||
for (auto entry : mapCryptedSaplingSpendingKeys) {
|
||||
|
@ -544,7 +514,6 @@ bool CCryptoKeyStore::GetSaplingSpendingKey(const libzcash::SaplingFullViewingKe
|
|||
return DecryptSaplingSpendingKey(vMasterKey, vchCryptedSecret, entry.first, skOut);
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -552,7 +521,7 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
|
|||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!mapCryptedKeys.empty() || IsCrypted())
|
||||
if (!mapCryptedKeys.empty() || fUseCrypto)
|
||||
return false;
|
||||
|
||||
fUseCrypto = true;
|
||||
|
|
|
@ -157,19 +157,14 @@ public:
|
|||
|
||||
bool IsCrypted() const
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
return fUseCrypto;
|
||||
}
|
||||
|
||||
bool IsLocked() const
|
||||
{
|
||||
if (!IsCrypted())
|
||||
return false;
|
||||
bool result;
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
result = vMasterKey.empty();
|
||||
}
|
||||
return result;
|
||||
return fUseCrypto && vMasterKey.empty();
|
||||
}
|
||||
|
||||
bool Lock();
|
||||
|
@ -182,20 +177,18 @@ public:
|
|||
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
|
||||
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey);
|
||||
bool HaveKey(const CKeyID &address) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::HaveKey(address);
|
||||
return mapCryptedKeys.count(address) > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool GetKey(const CKeyID &address, CKey& keyOut) const;
|
||||
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const;
|
||||
void GetKeys(std::set<CKeyID> &setAddress) const
|
||||
{
|
||||
if (!IsCrypted())
|
||||
LOCK(cs_KeyStore);
|
||||
if (!fUseCrypto)
|
||||
{
|
||||
CBasicKeyStore::GetKeys(setAddress);
|
||||
return;
|
||||
|
@ -214,19 +207,17 @@ public:
|
|||
const std::vector<unsigned char> &vchCryptedSecret);
|
||||
bool AddSproutSpendingKey(const libzcash::SproutSpendingKey &sk);
|
||||
bool HaveSproutSpendingKey(const libzcash::SproutPaymentAddress &address) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::HaveSproutSpendingKey(address);
|
||||
return mapCryptedSproutSpendingKeys.count(address) > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool GetSproutSpendingKey(const libzcash::SproutPaymentAddress &address, libzcash::SproutSpendingKey &skOut) const;
|
||||
void GetSproutPaymentAddresses(std::set<libzcash::SproutPaymentAddress> &setAddress) const
|
||||
{
|
||||
if (!IsCrypted())
|
||||
LOCK(cs_KeyStore);
|
||||
if (!fUseCrypto)
|
||||
{
|
||||
CBasicKeyStore::GetSproutPaymentAddresses(setAddress);
|
||||
return;
|
||||
|
@ -248,17 +239,15 @@ public:
|
|||
const libzcash::SaplingExtendedSpendingKey &sk,
|
||||
const libzcash::SaplingPaymentAddress &defaultAddr);
|
||||
bool HaveSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk) const
|
||||
{
|
||||
{
|
||||
LOCK(cs_KeyStore);
|
||||
if (!IsCrypted())
|
||||
if (!fUseCrypto)
|
||||
return CBasicKeyStore::HaveSaplingSpendingKey(fvk);
|
||||
for (auto entry : mapCryptedSaplingSpendingKeys) {
|
||||
if (entry.first.fvk == fvk) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool GetSaplingSpendingKey(const libzcash::SaplingFullViewingKey &fvk, libzcash::SaplingExtendedSpendingKey &skOut) const;
|
||||
|
|
Loading…
Reference in New Issue