Fix rpc-hanging deadlocks
[novacoin.git] / src / keystore.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "headers.h"
7 #include "db.h"
8 #include "crypter.h"
9
10 std::vector<unsigned char> CKeyStore::GenerateNewKey()
11 {
12     RandAddSeedPerfmon();
13     CKey key;
14     key.MakeNewKey();
15     if (!AddKey(key))
16         throw std::runtime_error("CKeyStore::GenerateNewKey() : AddKey failed");
17     return key.GetPubKey();
18 }
19
20 bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
21 {
22     CKey key;
23     if (!GetKey(address, key))
24         return false;
25     vchPubKeyOut = key.GetPubKey();
26     return true;
27 }
28
29 bool CBasicKeyStore::AddKey(const CKey& key)
30 {
31     CRITICAL_BLOCK(cs_KeyStore)
32         mapKeys[key.GetAddress()] = key.GetSecret();
33     return true;
34 }
35
36 std::vector<unsigned char> CCryptoKeyStore::GenerateNewKey()
37 {
38     RandAddSeedPerfmon();
39     CKey key;
40     key.MakeNewKey();
41     if (!AddKey(key))
42         throw std::runtime_error("CCryptoKeyStore::GenerateNewKey() : AddKey failed");
43     return key.GetPubKey();
44 }
45
46 bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
47 {
48     CRITICAL_BLOCK(cs_KeyStore)
49     {
50         if (!SetCrypted())
51             return false;
52
53         CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
54         for (; mi != mapCryptedKeys.end(); ++mi)
55         {
56             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
57             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
58             CSecret vchSecret;
59             if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
60                 return false;
61             CKey key;
62             key.SetSecret(vchSecret);
63             if (key.GetPubKey() == vchPubKey)
64                 break;
65             return false;
66         }
67         vMasterKey = vMasterKeyIn;
68     }
69     return true;
70 }
71
72 bool CCryptoKeyStore::AddKey(const CKey& key)
73 {
74     CRITICAL_BLOCK(cs_KeyStore)
75     {
76         if (!IsCrypted())
77             return CBasicKeyStore::AddKey(key);
78
79         if (IsLocked())
80             return false;
81
82         std::vector<unsigned char> vchCryptedSecret;
83         std::vector<unsigned char> vchPubKey = key.GetPubKey();
84         if (!EncryptSecret(vMasterKey, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
85             return false;
86
87         if (!AddCryptedKey(key.GetPubKey(), vchCryptedSecret))
88             return false;
89     }
90     return true;
91 }
92
93
94 bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
95 {
96     CRITICAL_BLOCK(cs_KeyStore)
97     {
98         if (!SetCrypted())
99             return false;
100
101         mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
102     }
103     return true;
104 }
105
106 bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
107 {
108     CRITICAL_BLOCK(cs_KeyStore)
109     {
110         if (!IsCrypted())
111             return CBasicKeyStore::GetKey(address, keyOut);
112
113         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
114         if (mi != mapCryptedKeys.end())
115         {
116             const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
117             const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
118             CSecret vchSecret;
119             if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
120                 return false;
121             keyOut.SetSecret(vchSecret);
122             return true;
123         }
124     }
125     return false;
126 }
127
128 bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
129 {
130     CRITICAL_BLOCK(cs_KeyStore)
131     {
132         if (!IsCrypted())
133             return CKeyStore::GetPubKey(address, vchPubKeyOut);
134
135         CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
136         if (mi != mapCryptedKeys.end())
137         {
138             vchPubKeyOut = (*mi).second.first;
139             return true;
140         }
141     }
142     return false;
143 }
144
145 bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
146 {
147     CRITICAL_BLOCK(cs_KeyStore)
148     {
149         if (!mapCryptedKeys.empty() || IsCrypted())
150             return false;
151
152         fUseCrypto = true;
153         CKey key;
154         BOOST_FOREACH(KeyMap::value_type& mKey, mapKeys)
155         {
156             if (!key.SetSecret(mKey.second))
157                 return false;
158             const std::vector<unsigned char> vchPubKey = key.GetPubKey();
159             std::vector<unsigned char> vchCryptedSecret;
160             if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
161                 return false;
162             if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
163                 return false;
164         }
165         mapKeys.clear();
166     }
167     return true;
168 }