Browse Source

Fixes and use actual master password for encrypting by default.

cl-refactor
Gav Wood 10 years ago
parent
commit
422be5eb46
  1. 12
      alethzero/Transact.h
  2. 13
      libdevcrypto/SecretStore.cpp
  3. 52
      libethereum/KeyManager.cpp
  4. 5
      libethereum/KeyManager.h

12
alethzero/Transact.h

@ -44,13 +44,13 @@ public:
void setEnvironment(dev::AddressHash const& _accounts, dev::eth::Client* _eth, NatSpecFace* _natSpecDB);
private slots:
void on_from_currentIndexChanged(int) { rejigData(); }
void on_from_currentIndexChanged(int) { rejigData(); rejigData(); }
void on_destination_currentTextChanged(QString);
void on_value_valueChanged(int) { updateFee(); }
void on_gas_valueChanged(int) { updateFee(); }
void on_valueUnits_currentIndexChanged(int) { updateFee(); }
void on_gasPriceUnits_currentIndexChanged(int) { updateFee(); }
void on_gasPrice_valueChanged(int) { updateFee(); }
void on_value_valueChanged(int) { updateFee(); rejigData(); }
void on_gas_valueChanged(int) { updateFee(); rejigData(); }
void on_valueUnits_currentIndexChanged(int) { updateFee(); rejigData(); }
void on_gasPriceUnits_currentIndexChanged(int) { updateFee(); rejigData(); }
void on_gasPrice_valueChanged(int) { updateFee(); rejigData(); }
void on_data_textChanged() { rejigData(); }
void on_optimize_clicked() { rejigData(); }
void on_send_clicked();

13
libdevcrypto/SecretStore.cpp

@ -180,12 +180,10 @@ h128 SecretStore::readKey(std::string const& _file, bool _deleteFile)
bool SecretStore::recode(h128 const& _uuid, string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf)
{
cdebug << "recode:" << toUUID(_uuid);
cdebug << "newPass:" << _newPass;
// cdebug << "recode:" << toUUID(_uuid);
bytes s = secret(_uuid, _pass, true);
if (s.empty())
return false;
cdebug << "secret:" << toHex(s);
m_keys[_uuid].first = encrypt(s, _newPass, _kdf);
save();
return true;
@ -215,7 +213,6 @@ std::string SecretStore::encrypt(bytes const& _v, std::string const& _pass, KDF
ret["kdfparams"] = params;
}
derivedKey = scrypt(_pass, salt, iterations, p, r, dklen);
cdebug << "derivedKey" << toHex(derivedKey);
}
else
{
@ -230,13 +227,13 @@ std::string SecretStore::encrypt(bytes const& _v, std::string const& _pass, KDF
ret["kdfparams"] = params;
}
derivedKey = pbkdf2(_pass, salt, iterations, dklen);
cdebug << "derivedKey" << toHex(derivedKey);
}
// cdebug << "derivedKey" << toHex(derivedKey);
// cipher info
ret["cipher"] = "aes-128-ctr";
h128 key(derivedKey, h128::AlignLeft);
cdebug << "cipherKey" << key.hex();
// cdebug << "cipherKey" << key.hex();
h128 iv = h128::random();
{
js::mObject params;
@ -250,8 +247,8 @@ std::string SecretStore::encrypt(bytes const& _v, std::string const& _pass, KDF
// and mac.
h256 mac = sha3(ref(derivedKey).cropped(16, 16).toBytes() + cipherText);
cdebug << "macBody" << toHex(ref(derivedKey).cropped(16, 16).toBytes() + cipherText);
cdebug << "mac" << mac.hex();
// cdebug << "macBody" << toHex(ref(derivedKey).cropped(16, 16).toBytes() + cipherText);
// cdebug << "mac" << mac.hex();
ret["mac"] = toHex(mac.ref());
return js::write_string((js::mValue)ret, true);

52
libethereum/KeyManager.cpp

@ -52,17 +52,27 @@ void KeyManager::create(std::string const& _pass)
bool KeyManager::recode(Address const& _address, std::string const& _newPass, std::string const& _hint, std::function<string()> const& _pass, KDF _kdf)
{
noteHint(_newPass, _hint);
return store().recode(uuid(_address), _newPass, _pass, _kdf);
h128 u = uuid(_address);
if (!store().recode(u, _newPass, [&](){ return getPassword(u, _pass); }, _kdf))
return false;
m_keyInfo[u].passHash = hashPassword(_newPass);
write();
return true;
}
bool KeyManager::recode(Address const& _address, SemanticPassword _newPass, std::function<string()> const& _pass, KDF _kdf)
{
h128 u = uuid(_address);
std::string p;
if (_newPass == SemanticPassword::Existing)
return store().recode(u, getPassword(u, _pass), _pass, _kdf);
p = getPassword(u, _pass);
else if (_newPass == SemanticPassword::Master)
return store().recode(u, defaultPassword(), _pass, _kdf);
return false;
p = defaultPassword();
else
return false;
return recode(_address, p, string(), _pass, _kdf);
}
bool KeyManager::load(std::string const& _pass)
@ -87,7 +97,8 @@ bool KeyManager::load(std::string const& _pass)
m_password = (string)s[3];
}
m_cachedPasswords[hashPassword(m_password)] = m_password;
m_cachedPasswords[hashPassword(defaultPassword())] = defaultPassword();
m_cachedPasswords[hashPassword(asString(m_key.ref()))] = asString(m_key.ref());
m_cachedPasswords[m_master = hashPassword(_pass)] = _pass;
return true;
}
catch (...) {
@ -111,15 +122,29 @@ Secret KeyManager::secret(h128 const& _uuid, function<std::string()> const& _pas
std::string KeyManager::getPassword(h128 const& _uuid, function<std::string()> const& _pass) const
{
auto kit = m_keyInfo.find(_uuid);
h256 ph;
if (kit != m_keyInfo.end())
ph = kit->second.passHash;
return getPassword(ph, _pass);
}
std::string KeyManager::getPassword(h256 const& _passHash, function<std::string()> const& _pass) const
{
auto it = m_cachedPasswords.find(_passHash);
if (it != m_cachedPasswords.end())
return it->second;
for (unsigned i = 0; i< 10; ++i)
{
auto it = m_cachedPasswords.find(kit->second.passHash);
if (it != m_cachedPasswords.end())
return it->second;
std::string p = _pass();
if (p.empty())
break;
if (hashPassword(p) == _passHash || !_passHash)
{
m_cachedPasswords[hashPassword(p)] = p;
return p;
}
}
std::string p = _pass();
m_cachedPasswords[hashPassword(p)] = p;
return p;
return string();
}
h128 KeyManager::uuid(Address const& _a) const
@ -190,7 +215,7 @@ std::unordered_map<Address, std::pair<std::string, std::string>> KeyManager::acc
std::unordered_map<Address, std::pair<std::string, std::string>> ret;
for (auto const& i: m_addrLookup)
if (m_keyInfo.count(i.second) > 0)
ret[i.first] = make_pair(m_keyInfo.at(i.second).info, m_passwordInfo.at(m_keyInfo.at(i.second).passHash));
ret[i.first] = make_pair(m_keyInfo.count(i.second) ? m_keyInfo.at(i.second).info : "", m_keyInfo.count(i.second) && m_passwordInfo.count(m_keyInfo.at(i.second).passHash) ? m_passwordInfo.at(m_keyInfo.at(i.second).passHash) : "");
return ret;
}
@ -213,6 +238,9 @@ void KeyManager::write(std::string const& _pass, std::string const& _keysFile) c
bytes salt = h256::random().asBytes();
writeFile(_keysFile + ".salt", salt);
auto key = h128(pbkdf2(_pass, salt, 262144, 16));
m_cachedPasswords[hashPassword(_pass)] = _pass;
m_master = hashPassword(_pass);
write(key, _keysFile);
}

5
libethereum/KeyManager.h

@ -101,11 +101,13 @@ public:
private:
std::string getPassword(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow) const;
std::string defaultPassword() const { return asString(m_key.ref()); }
std::string getPassword(h256 const& _passHash, std::function<std::string()> const& _pass = DontKnowThrow) const;
std::string defaultPassword(std::function<std::string()> const& _pass = DontKnowThrow) const { return getPassword(m_master, _pass); }
h256 hashPassword(std::string const& _pass) const;
// Only use if previously loaded ok.
// @returns false if wasn't previously loaded ok.
bool write() const { return write(m_keysFile); }
bool write(std::string const& _keysFile) const;
void write(std::string const& _pass, std::string const& _keysFile) const;
void write(h128 const& _key, std::string const& _keysFile) const;
@ -127,6 +129,7 @@ private:
SecretStore m_store;
mutable h128 m_key;
mutable h256 m_master;
mutable std::string m_keysFile;
};

Loading…
Cancel
Save