X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fkeystore.cpp;h=215590eca883a470d6e227f907eb97e2e79d2b48;hb=9d14e64825d95061ea0857267646dfcb3d62e07a;hp=197b73ef12143d8336d6d02a1edf4d5c10408bc6;hpb=e5e4c598dc43bb5e01b3a30aaeb2dfc9376bd7b4;p=novacoin.git diff --git a/src/keystore.cpp b/src/keystore.cpp index 197b73e..215590e 100644 --- a/src/keystore.cpp +++ b/src/keystore.cpp @@ -29,6 +29,15 @@ bool CBasicKeyStore::AddKey(const CKey& key) return true; } +bool CBasicKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret& vchSecretH) +{ + { + LOCK(cs_KeyStore); + mapMalleableKeys[CMalleableKeyView(keyView)] = vchSecretH; + } + return true; +} + bool CBasicKeyStore::AddCScript(const CScript& redeemScript) { if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE) @@ -102,20 +111,6 @@ bool CBasicKeyStore::HaveWatchOnly() const return (!setWatchOnly.empty()); } -CCryptoKeyStore::CCryptoKeyStore() : fUseCrypto(false) -{ - std::string strMalleableKey = GetArg("-masterkey", ""); - CMalleableKey malleableKey; - if (strMalleableKey != "") - malleableKey.SetString(strMalleableKey); - else - malleableKey.MakeNewKeys(); - - CMalleableKeyView keyView(malleableKey); - - malleableKeyPair = std::pair(keyView, malleableKey); -} - bool CCryptoKeyStore::SetCrypted() { { @@ -204,6 +199,28 @@ bool CCryptoKeyStore::AddKey(const CKey& key) return true; } +bool CCryptoKeyStore::AddMalleableKey(const CMalleableKeyView& keyView, const CSecret &vchSecretH) +{ + { + LOCK(cs_KeyStore); + if (!SetCrypted()) + return CBasicKeyStore::AddMalleableKey(keyView, vchSecretH); + + if (IsLocked()) + return false; + + CKey keyH; + keyH.SetSecret(vchSecretH, true); + + std::vector vchCryptedSecretH; + if (!EncryptSecret(vMasterKey, vchSecretH, keyH.GetPubKey().GetHash(), vchCryptedSecretH)) + return false; + + if (!AddCryptedMalleableKey(keyView, vchCryptedSecretH)) + return false; + } + return true; +} bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector &vchCryptedSecret) { @@ -217,6 +234,71 @@ bool CCryptoKeyStore::AddCryptedKey(const CPubKey &vchPubKey, const std::vector< return true; } +bool CCryptoKeyStore::AddCryptedMalleableKey(const CMalleableKeyView& keyView, const std::vector &vchCryptedSecretH) +{ + { + LOCK(cs_KeyStore); + if (!SetCrypted()) + return false; + + mapCryptedMalleableKeys[CMalleableKeyView(keyView)] = vchCryptedSecretH; + } + return true; +} + +bool CCryptoKeyStore::CreatePrivKey(const CPubKey &pubKeyVariant, const CPubKey &R, CKey &privKey) const +{ + { + LOCK(cs_KeyStore); + if (!IsCrypted()) + return CBasicKeyStore::CreatePrivKey(pubKeyVariant, R, privKey); + + for (CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.begin(); mi != mapCryptedMalleableKeys.end(); mi++) + { + if (mi->first.CheckKeyVariant(R, pubKeyVariant)) + { + const CPubKey H = mi->first.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH)) + return false; + if (vchSecretH.size() != 32) + return false; + + CMalleableKey mKey = mi->first.GetMalleableKey(vchSecretH); + return mKey.CheckKeyVariant(R, pubKeyVariant, privKey);; + } + } + + } + return true; +} + +bool CCryptoKeyStore::GetMalleableKey(const CMalleableKeyView &keyView, CMalleableKey &mKey) const +{ + { + LOCK(cs_KeyStore); + if (!IsCrypted()) + return CBasicKeyStore::GetMalleableKey(keyView, mKey); + CryptedMalleableKeyMap::const_iterator mi = mapCryptedMalleableKeys.find(keyView); + if (mi != mapCryptedMalleableKeys.end()) + { + const CPubKey H = keyView.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if (!DecryptSecret(vMasterKey, mi->second, H.GetHash(), vchSecretH)) + return false; + + if (vchSecretH.size() != 32) + return false; + mKey = mi->first.GetMalleableKey(vchSecretH); + + return true; + } + } + return false; +} + bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const { { @@ -281,6 +363,17 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) return false; } mapKeys.clear(); + + BOOST_FOREACH(MalleableKeyMap::value_type& mKey, mapMalleableKeys) + { + const CPubKey vchPubKeyH = mKey.first.GetMalleablePubKey().GetH(); + std::vector vchCryptedSecretH; + if (!EncryptSecret(vMasterKeyIn, mKey.second, vchPubKeyH.GetHash(), vchCryptedSecretH)) + return false; + if (!AddCryptedMalleableKey(mKey.first, vchCryptedSecretH)) + return false; + } + mapMalleableKeys.clear(); } return true; } @@ -310,6 +403,22 @@ bool CCryptoKeyStore::DecryptKeys(const CKeyingMaterial& vMasterKeyIn) } mapCryptedKeys.clear(); + + CryptedMalleableKeyMap::const_iterator mi2 = mapCryptedMalleableKeys.begin(); + for(; mi2 != mapCryptedMalleableKeys.end(); ++mi2) + { + const CPubKey vchPubKeyH = mi2->first.GetMalleablePubKey().GetH(); + + CSecret vchSecretH; + if(!DecryptSecret(vMasterKeyIn, mi2->second, vchPubKeyH.GetHash(), vchSecretH)) + return false; + if (vchSecretH.size() != 32) + return false; + + if (!CBasicKeyStore::AddMalleableKey(mi2->first, vchSecretH)) + return false; + } + mapCryptedMalleableKeys.clear(); } return true;