-
+ 8E9AABB0569D092A6B1BFF5015211A38475530B28614A7163ADE52496314094BC00FD443EAFB66F1285EAA1C69A680233A3E6044058598D2E2CFFCF8797E0CC0
bitcoin/src/keystore.cpp
(0 . 0)(1 . 181)
8979 // Copyright (c) 2009-2010 Satoshi Nakamoto
8980 // Copyright (c) 2011 The Bitcoin developers
8981 // Distributed under the MIT/X11 software license, see the accompanying
8982 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
8983
8984 #include "headers.h"
8985 #include "db.h"
8986 #include "crypter.h"
8987
8988 std::vector<unsigned char> CKeyStore::GenerateNewKey()
8989 {
8990 RandAddSeedPerfmon();
8991 CKey key;
8992 key.MakeNewKey();
8993 if (!AddKey(key))
8994 throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
8995 return key.GetPubKey();
8996 }
8997
8998 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
8999 {
9000 CKey key;
9001 if (!GetKey(address, key))
9002 return false;
9003 vchPubKeyOut = key.GetPubKey();
9004 return true;
9005 }
9006
9007 bool CBasicKeyStore::AddKey(const CKey& key)
9008 {
9009 CRITICAL_BLOCK(cs_KeyStore)
9010 mapKeys[key.GetAddress()] = key.GetSecret();
9011 return true;
9012 }
9013
9014 bool CCryptoKeyStore::SetCrypted()
9015 {
9016 CRITICAL_BLOCK(cs_KeyStore)
9017 {
9018 if (fUseCrypto)
9019 return true;
9020 if (!mapKeys.empty())
9021 return false;
9022 fUseCrypto = true;
9023 }
9024 return true;
9025 }
9026
9027 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
9028 {
9029 RandAddSeedPerfmon();
9030 CKey key;
9031 key.MakeNewKey();
9032 if (!AddKey(key))
9033 throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
9034 return key.GetPubKey();
9035 }
9036
9037 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
9038 {
9039 CRITICAL_BLOCK(cs_KeyStore)
9040 {
9041 if (!SetCrypted())
9042 return false;
9043
9044 CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
9045 for (; mi != mapCryptedKeys.end(); ++mi)
9046 {
9047 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
9048 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
9049 CSecret vchSecret;
9050 if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
9051 return false;
9052 CKey key;
9053 key.SetSecret(vchSecret);
9054 if (key.GetPubKey() == vchPubKey)
9055 break;
9056 return false;
9057 }
9058 vMasterKey = vMasterKeyIn;
9059 }
9060 return true;
9061 }
9062
9063 bool CCryptoKeyStore::AddKey(const CKey& key)
9064 {
9065 CRITICAL_BLOCK(cs_KeyStore)
9066 {
9067 if (!IsCrypted())
9068 return CBasicKeyStore::AddKey(key);
9069
9070 if (IsLocked())
9071 return false;
9072
9073 std::vector<unsigned char> vchCryptedSecret;
9074 std::vector<unsigned char> vchPubKey = key.GetPubKey();
9075 if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
9076 return false;
9077
9078 if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
9079 return false;
9080 }
9081 return true;
9082 }
9083
9084
9085 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
9086 {
9087 CRITICAL_BLOCK(cs_KeyStore)
9088 {
9089 if (!SetCrypted())
9090 return false;
9091
9092 mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
9093 }
9094 return true;
9095 }
9096
9097 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
9098 {
9099 CRITICAL_BLOCK(cs_KeyStore)
9100 {
9101 if (!IsCrypted())
9102 return CBasicKeyStore::GetKey(address, keyOut);
9103
9104 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
9105 if (mi != mapCryptedKeys.end())
9106 {
9107 const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
9108 const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
9109 CSecret vchSecret;
9110 if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
9111 return false;
9112 keyOut.SetSecret(vchSecret);
9113 return true;
9114 }
9115 }
9116 return false;
9117 }
9118
9119 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
9120 {
9121 CRITICAL_BLOCK(cs_KeyStore)
9122 {
9123 if (!IsCrypted())
9124 return CKeyStore::GetPubKey(address, vchPubKeyOut);
9125
9126 CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
9127 if (mi != mapCryptedKeys.end())
9128 {
9129 vchPubKeyOut = (*mi).second.first;
9130 return true;
9131 }
9132 }
9133 return false;
9134 }
9135
9136 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
9137 {
9138 CRITICAL_BLOCK(cs_KeyStore)
9139 {
9140 if (!mapCryptedKeys.empty() || IsCrypted())
9141 return false;
9142
9143 fUseCrypto = true;
9144 CKey key;
9145 BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
9146 {
9147 if (!key.SetSecret(mKey.second))
9148 return false;
9149 const std::vector<unsigned char> vchPubKey = key.GetPubKey();
9150 std::vector<unsigned char> vchCryptedSecret;
9151 if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
9152 return false;
9153 if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
9154 return false;
9155 }
9156 mapKeys.clear();
9157 }
9158 return true;
9159 }