diff options
| author | Bitflash <btf01@proton.me> | 2026-08-04 10:01:36 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-04 10:01:36 -0300 |
| commit | 1db5f372c36eb701602a70a358de2344726ed734 (patch) | |
| tree | 9d028ed3097ac51368ca312ff439664c76f439b7 /src/main.h | |
| parent | 9176d5b3f32b907e4bafde5152828b2ae04ae8a1 (diff) | |
wallet encryption: the stack from #130–#133, with the two blockers fixed (#134)
* wallet: refuse future encrypted wallet formats
* wallet: add encrypted key unlock core
* wallet: add encryptwallet rewrite command
* wallet: read passphrases from stdin or files
* wallet: keep the derivation across encryption, and take the logs with it
Two things stopped the encryption stack from being safe to merge. Both
were found against a wallet holding real coin, not in the self-tests.
1. Encrypting turned a BIP44 wallet into a legacy one.
LoadWallet() ends by resetting the schema and the derivation counters
when no seed is in memory. That was written when "no seed after loading"
could only mean the wallet had none. An encrypted wallet has a seed --
it just has not been decrypted yet -- so the reset threw away metadata
that had been read correctly a moment earlier. Unlocking later restores
the seed and nothing restores the rest.
Measured on the wallet from the #124 proof: after encrypting, index 1
moved off BAtgSgra2X8Dqo5PJzPCLJhPwrrC2thYVJ, the address actually
holding 1.00 BTF, and the audit went from "1.00 covered by the phrase"
to "0.00 covered, 1.00 wallet.dat-only". Encrypt your wallet and the
program told you your recovery phrase no longer covered your money.
Now the condition also accepts a wallet whose seed is present but
encrypted. Same wallet, before and after: schema bip44, same four
derived addresses, 1.00 BTF still reported as covered.
2. The plaintext keys stayed behind in the Berkeley DB logs.
wallet.dat comes out clean -- verified by scanning the raw file for the
118 bytes of a known private key. database/log.0000000002 did not: it
held the whole key, and still did after a full node restart, because
every record this wallet ever wrote passed through the log.
That defeats the feature for the case it exists for, since our own
backup advice is to copy the whole directory rather than wallet.dat
alone. log_archive() cannot fix it: it only drops logs Berkeley DB
considers obsolete and never the one it is writing to, and with one 10
MB log per directory that is precisely where the keys are. They also did
not get there at encryption time -- they accumulated over the wallet's
whole life -- so nothing done during the rewrite can archive them out.
So the encrypt command now closes the environment and deletes the logs
on its way out, after the flush that makes every .dat self-contained.
This is not secure erasure; the blocks are unlinked, not overwritten.
It closes the case of someone reading a copied data directory.
Getting there uncovered a third bug, older than both.
DBFlush(true) is not safe to call twice. The first call closes the
environment, and txn_checkpoint() on a closed environment segfaults
instead of returning an error, so the try/catch around it never sees
anything. EncryptWallet() already flushes before renaming the wallet,
and the command flushes again on the way out -- that pair crashed every
single time. Guarded now.
The same function has been calling log_archive() with the address of an
uninitialised pointer since 0.1.0, on every clean shutdown, in a path
whose catch(...) cannot catch a structured fault on Windows.
DB_ARCH_REMOVE requires that argument to be NULL. Fixed while here,
because the encryption cleanup depends on that call not being a coin
flip.
---------
Co-authored-by: Gilcimar Duarte <ti@gpm.infra>
Diffstat (limited to 'src/main.h')
| -rw-r--r-- | src/main.h | 40 |
1 files changed, 40 insertions, 0 deletions
| @@ -129,6 +129,46 @@ FILE* AppendBlockFile(unsigned int& nFileRet); | |||
| 129 | bool AddKey(const CKey& key); | 129 | bool AddKey(const CKey& key); |
| 130 | vector<unsigned char> GenerateNewKey(); | 130 | vector<unsigned char> GenerateNewKey(); |
| 131 | 131 | ||
| 132 | // --- Wallet encryption ---------------------------------------------------- | ||
| 133 | // | ||
| 134 | // Plain wallets keep private keys in mapKeys. Encrypted wallets keep public | ||
| 135 | // keys visible so balances can still be recognized while locked, and decrypt | ||
| 136 | // private keys only after an unlock passphrase has supplied the master key. | ||
| 137 | typedef vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial; | ||
| 138 | extern map<vector<unsigned char>, vector<unsigned char> > mapCryptedKeys; | ||
| 139 | extern map<unsigned int, CWalletMasterKey> mapMasterKeys; | ||
| 140 | extern unsigned int nWalletMasterKeyMaxID; | ||
| 141 | extern CKeyingMaterial vWalletMasterKey; | ||
| 142 | extern vector<unsigned char> vchCryptedHDMaster; | ||
| 143 | extern vector<unsigned char> vchCryptedHDChainCode; | ||
| 144 | extern bool fWalletEncrypted; | ||
| 145 | extern bool fWalletLocked; | ||
| 146 | bool IsWalletEncrypted(); | ||
| 147 | bool IsWalletLocked(); | ||
| 148 | void LockWallet(); | ||
| 149 | bool UnlockWallet(const string& strPassphrase, string& strErrorRet); | ||
| 150 | bool DeriveWalletPassphraseKey(const string& strPassphrase, | ||
| 151 | const vector<unsigned char>& vchSalt, | ||
| 152 | unsigned int nDeriveIterations, | ||
| 153 | CKeyingMaterial& vchKeyRet, | ||
| 154 | vector<unsigned char>& vchIVRet); | ||
| 155 | bool EncryptSecret(const CKeyingMaterial& vchKey, | ||
| 156 | const vector<unsigned char>& vchPlaintext, | ||
| 157 | const vector<unsigned char>& vchIV, | ||
| 158 | vector<unsigned char>& vchCiphertextRet); | ||
| 159 | bool DecryptSecret(const CKeyingMaterial& vchKey, | ||
| 160 | const vector<unsigned char>& vchCiphertext, | ||
| 161 | const vector<unsigned char>& vchIV, | ||
| 162 | vector<unsigned char>& vchPlaintextRet); | ||
| 163 | vector<unsigned char> WalletKeyIV(const vector<unsigned char>& vchPubKey); | ||
| 164 | vector<unsigned char> WalletSecretIV(const string& strLabel); | ||
| 165 | bool AddCryptedKey(const vector<unsigned char>& vchPubKey, | ||
| 166 | const vector<unsigned char>& vchCryptedSecret); | ||
| 167 | bool WalletCanSpendKey(const vector<unsigned char>& vchPubKey); | ||
| 168 | bool GetWalletPrivKey(const vector<unsigned char>& vchPubKey, | ||
| 169 | CPrivKey& vchPrivKeyRet, | ||
| 170 | string& strErrorRet); | ||
| 171 | |||
| 132 | // --- Key pool ------------------------------------------------------------- | 172 | // --- Key pool ------------------------------------------------------------- |
| 133 | // | 173 | // |
| 134 | // A backup of this wallet only ever contained the keys that existed the moment | 174 | // A backup of this wallet only ever contained the keys that existed the moment |