Skip to content

Commit 8f5b81e

Browse files
committed
Remove CCryptoKeyStore and move all of it's functionality into CWallet
Instead of having a separate CCryptoKeyStore that handles the encryption stuff, just roll it all into CWallet.
1 parent 37a79a4 commit 8f5b81e

File tree

5 files changed

+247
-256
lines changed

5 files changed

+247
-256
lines changed

src/interfaces/wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class WalletImpl : public Wallet
476476
}
477477
std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override
478478
{
479-
return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CCryptoKeyStore*) { fn(); }));
479+
return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); }));
480480
}
481481
std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override
482482
{

src/wallet/crypter.cpp

Lines changed: 3 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
107107
return true;
108108
}
109109

110-
111-
static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
110+
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
112111
{
113112
CCrypter cKeyCrypter;
114113
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
@@ -118,7 +117,7 @@ static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMateri
118117
return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
119118
}
120119

121-
static bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
120+
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
122121
{
123122
CCrypter cKeyCrypter;
124123
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
@@ -128,7 +127,7 @@ static bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<u
128127
return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
129128
}
130129

131-
static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
130+
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
132131
{
133132
CKeyingMaterial vchSecret;
134133
if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
@@ -140,188 +139,3 @@ static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsi
140139
key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
141140
return key.VerifyPubKey(vchPubKey);
142141
}
143-
144-
bool CCryptoKeyStore::SetCrypted()
145-
{
146-
LOCK(cs_KeyStore);
147-
if (fUseCrypto)
148-
return true;
149-
if (!mapKeys.empty())
150-
return false;
151-
fUseCrypto = true;
152-
return true;
153-
}
154-
155-
bool CCryptoKeyStore::IsLocked() const
156-
{
157-
if (!IsCrypted()) {
158-
return false;
159-
}
160-
LOCK(cs_KeyStore);
161-
return vMasterKey.empty();
162-
}
163-
164-
bool CCryptoKeyStore::Lock()
165-
{
166-
if (!SetCrypted())
167-
return false;
168-
169-
{
170-
LOCK(cs_KeyStore);
171-
vMasterKey.clear();
172-
}
173-
174-
NotifyStatusChanged(this);
175-
return true;
176-
}
177-
178-
bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys)
179-
{
180-
{
181-
LOCK(cs_KeyStore);
182-
if (!SetCrypted())
183-
return false;
184-
185-
bool keyPass = mapCryptedKeys.empty(); // Always pass when there are no encrypted keys
186-
bool keyFail = false;
187-
CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
188-
for (; mi != mapCryptedKeys.end(); ++mi)
189-
{
190-
const CPubKey &vchPubKey = (*mi).second.first;
191-
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
192-
CKey key;
193-
if (!DecryptKey(vMasterKeyIn, vchCryptedSecret, vchPubKey, key))
194-
{
195-
keyFail = true;
196-
break;
197-
}
198-
keyPass = true;
199-
if (fDecryptionThoroughlyChecked)
200-
break;
201-
}
202-
if (keyPass && keyFail)
203-
{
204-
LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all.\n");
205-
throw std::runtime_error("Error unlocking wallet: some keys decrypt but not all. Your wallet file may be corrupt.");
206-
}
207-
if (keyFail || (!keyPass && !accept_no_keys))
208-
return false;
209-
vMasterKey = vMasterKeyIn;
210-
fDecryptionThoroughlyChecked = true;
211-
}
212-
NotifyStatusChanged(this);
213-
return true;
214-
}
215-
216-
bool CCryptoKeyStore::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
217-
{
218-
LOCK(cs_KeyStore);
219-
if (!IsCrypted()) {
220-
return FillableSigningProvider::AddKeyPubKey(key, pubkey);
221-
}
222-
223-
if (IsLocked()) {
224-
return false;
225-
}
226-
227-
std::vector<unsigned char> vchCryptedSecret;
228-
CKeyingMaterial vchSecret(key.begin(), key.end());
229-
if (!EncryptSecret(vMasterKey, vchSecret, pubkey.GetHash(), vchCryptedSecret)) {
230-
return false;
231-
}
232-
233-
if (!AddCryptedKey(pubkey, vchCryptedSecret)) {
234-
return false;
235-
}
236-
return true;
237-
}
238-
239-
240-
bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
241-
{
242-
LOCK(cs_KeyStore);
243-
if (!SetCrypted()) {
244-
return false;
245-
}
246-
247-
mapCryptedKeys[vchPubKey.GetID()] = make_pair(vchPubKey, vchCryptedSecret);
248-
ImplicitlyLearnRelatedKeyScripts(vchPubKey);
249-
return true;
250-
}
251-
252-
bool CCryptoKeyStore::HaveKey(const CKeyID &address) const
253-
{
254-
LOCK(cs_KeyStore);
255-
if (!IsCrypted()) {
256-
return FillableSigningProvider::HaveKey(address);
257-
}
258-
return mapCryptedKeys.count(address) > 0;
259-
}
260-
261-
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const
262-
{
263-
LOCK(cs_KeyStore);
264-
if (!IsCrypted()) {
265-
return FillableSigningProvider::GetKey(address, keyOut);
266-
}
267-
268-
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
269-
if (mi != mapCryptedKeys.end())
270-
{
271-
const CPubKey &vchPubKey = (*mi).second.first;
272-
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
273-
return DecryptKey(vMasterKey, vchCryptedSecret, vchPubKey, keyOut);
274-
}
275-
return false;
276-
}
277-
278-
bool CCryptoKeyStore::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const
279-
{
280-
LOCK(cs_KeyStore);
281-
if (!IsCrypted())
282-
return FillableSigningProvider::GetPubKey(address, vchPubKeyOut);
283-
284-
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
285-
if (mi != mapCryptedKeys.end())
286-
{
287-
vchPubKeyOut = (*mi).second.first;
288-
return true;
289-
}
290-
// Check for watch-only pubkeys
291-
return FillableSigningProvider::GetPubKey(address, vchPubKeyOut);
292-
}
293-
294-
std::set<CKeyID> CCryptoKeyStore::GetKeys() const
295-
{
296-
LOCK(cs_KeyStore);
297-
if (!IsCrypted()) {
298-
return FillableSigningProvider::GetKeys();
299-
}
300-
std::set<CKeyID> set_address;
301-
for (const auto& mi : mapCryptedKeys) {
302-
set_address.insert(mi.first);
303-
}
304-
return set_address;
305-
}
306-
307-
bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
308-
{
309-
LOCK(cs_KeyStore);
310-
if (!mapCryptedKeys.empty() || IsCrypted())
311-
return false;
312-
313-
fUseCrypto = true;
314-
for (const KeyMap::value_type& mKey : mapKeys)
315-
{
316-
const CKey &key = mKey.second;
317-
CPubKey vchPubKey = key.GetPubKey();
318-
CKeyingMaterial vchSecret(key.begin(), key.end());
319-
std::vector<unsigned char> vchCryptedSecret;
320-
if (!EncryptSecret(vMasterKeyIn, vchSecret, vchPubKey.GetHash(), vchCryptedSecret))
321-
return false;
322-
if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
323-
return false;
324-
}
325-
mapKeys.clear();
326-
return true;
327-
}

src/wallet/crypter.h

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
#include <atomic>
1313

14-
#include <boost/signals2/signal.hpp>
15-
1614
const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
1715
const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
1816
const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
@@ -111,54 +109,8 @@ friend class wallet_crypto_tests::TestCrypter; // for test access to chKey/chIV
111109
}
112110
};
113111

114-
/** Keystore which keeps the private keys encrypted.
115-
* It derives from the basic key store, which is used if no encryption is active.
116-
*/
117-
class CCryptoKeyStore : public FillableSigningProvider
118-
{
119-
private:
120-
121-
CKeyingMaterial vMasterKey GUARDED_BY(cs_KeyStore);
122-
123-
//! if fUseCrypto is true, mapKeys must be empty
124-
//! if fUseCrypto is false, vMasterKey must be empty
125-
std::atomic<bool> fUseCrypto;
126-
127-
//! keeps track of whether Unlock has run a thorough check before
128-
bool fDecryptionThoroughlyChecked;
129-
130-
protected:
131-
using CryptedKeyMap = std::map<CKeyID, std::pair<CPubKey, std::vector<unsigned char>>>;
132-
133-
bool SetCrypted();
134-
135-
//! will encrypt previously unencrypted keys
136-
bool EncryptKeys(CKeyingMaterial& vMasterKeyIn);
137-
138-
bool Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys = false);
139-
CryptedKeyMap mapCryptedKeys GUARDED_BY(cs_KeyStore);
140-
141-
public:
142-
CCryptoKeyStore() : fUseCrypto(false), fDecryptionThoroughlyChecked(false)
143-
{
144-
}
145-
146-
bool IsCrypted() const { return fUseCrypto; }
147-
bool IsLocked() const;
148-
bool Lock();
149-
150-
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
151-
bool AddKeyPubKey(const CKey& key, const CPubKey &pubkey) override;
152-
bool HaveKey(const CKeyID &address) const override;
153-
bool GetKey(const CKeyID &address, CKey& keyOut) const override;
154-
bool GetPubKey(const CKeyID &address, CPubKey& vchPubKeyOut) const override;
155-
std::set<CKeyID> GetKeys() const override;
156-
157-
/**
158-
* Wallet status (encrypted, locked) changed.
159-
* Note: Called without locks held.
160-
*/
161-
boost::signals2::signal<void (CCryptoKeyStore* wallet)> NotifyStatusChanged;
162-
};
112+
bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext);
113+
bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext);
114+
bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key);
163115

164116
#endif // BITCOIN_WALLET_CRYPTER_H

0 commit comments

Comments
 (0)