KeyManager.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file KeyManager.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <functional>
  20. #include <mutex>
  21. #include <libdevcore/FileSystem.h>
  22. #include <libdevcore/CommonData.h>
  23. #include <libdevcrypto/SecretStore.h>
  24. namespace dev
  25. {
  26. namespace eth
  27. {
  28. class PasswordUnknown: public Exception {};
  29. struct KeyInfo
  30. {
  31. KeyInfo() = default;
  32. KeyInfo(h256 const& _passHash, std::string const& _accountName, std::string const& _passwordHint = std::string()): passHash(_passHash), accountName(_accountName), passwordHint(_passwordHint) {}
  33. /// Hash of the password or h256() / UnknownPassword if unknown.
  34. h256 passHash;
  35. /// Name of the key, or JSON key info if begins with '{'.
  36. std::string accountName;
  37. /// Hint of the password. Alternative place for storage than the hash-based lookup.
  38. std::string passwordHint;
  39. };
  40. static h256 const UnknownPassword;
  41. /// Password query function that never returns a password.
  42. static auto const DontKnowThrow = [](){ throw PasswordUnknown(); return std::string(); };
  43. enum class SemanticPassword
  44. {
  45. Existing,
  46. Master
  47. };
  48. // TODO: This one is specifically for Ethereum, but we can make it generic in due course.
  49. // TODO: hidden-partition style key-store.
  50. /**
  51. * @brief High-level manager of password-encrypted keys for Ethereum.
  52. * Usage:
  53. *
  54. * Call exists() to check whether there is already a database. If so, get the master password from
  55. * the user and call load() with it. If not, get a new master password from the user (get them to type
  56. * it twice and keep some hint around!) and call create() with it.
  57. *
  58. * Uses a "key file" (and a corresponding .salt file) that contains encrypted information about the keys and
  59. * a directory called "secrets path" that contains a file for each key.
  60. */
  61. class KeyManager
  62. {
  63. public:
  64. enum class NewKeyType { DirectICAP = 0, NoVanity, FirstTwo, FirstTwoNextTwo, FirstThree, FirstFour };
  65. KeyManager(std::string const& _keysFile = defaultPath(), std::string const& _secretsPath = SecretStore::defaultPath());
  66. ~KeyManager();
  67. void setSecretsPath(std::string const& _secretsPath) { m_store.setPath(_secretsPath); }
  68. void setKeysFile(std::string const& _keysFile) { m_keysFile = _keysFile; }
  69. std::string const& keysFile() const { return m_keysFile; }
  70. bool exists() const;
  71. void create(std::string const& _pass);
  72. bool load(std::string const& _pass);
  73. void save(std::string const& _pass) const { write(_pass, m_keysFile); }
  74. void notePassword(std::string const& _pass) { m_cachedPasswords[hashPassword(_pass)] = _pass; }
  75. void noteHint(std::string const& _pass, std::string const& _hint) { if (!_hint.empty()) m_passwordHint[hashPassword(_pass)] = _hint; }
  76. bool haveHint(std::string const& _pass) const { auto h = hashPassword(_pass); return m_cachedPasswords.count(h) && !m_cachedPasswords.at(h).empty(); }
  77. /// @returns the list of account addresses.
  78. Addresses accounts() const;
  79. /// @returns a hashset of all account addresses.
  80. AddressHash accountsHash() const { return AddressHash() + accounts(); }
  81. bool hasAccount(Address const& _address) const;
  82. /// @returns the human-readable name or json-encoded info of the account for the given address.
  83. std::string const& accountName(Address const& _address) const;
  84. /// @returns the password hint for the account for the given address;
  85. std::string const& passwordHint(Address const& _address) const;
  86. /// Should be called to change password
  87. void changeName(Address const& _address, std::string const& _name);
  88. /// @returns true if the given address has a key (UUID) associated with it. Equivalent to !!uuid(_a)
  89. /// If the address has no key, it could be a brain wallet.
  90. bool haveKey(Address const& _a) const { return m_addrLookup.count(_a); }
  91. /// @returns the uuid of the key for the address @a _a or the empty hash on error.
  92. h128 uuid(Address const& _a) const;
  93. /// @returns the address corresponding to the key with uuid @a _uuid or the zero address on error.
  94. Address address(h128 const& _uuid) const;
  95. h128 import(Secret const& _s, std::string const& _accountName, std::string const& _pass, std::string const& _passwordHint);
  96. h128 import(Secret const& _s, std::string const& _accountName) { return import(_s, _accountName, defaultPassword(), std::string()); }
  97. Address importBrain(std::string const& _seed, std::string const& _accountName, std::string const& _seedHint);
  98. void importExistingBrain(Address const& _a, std::string const& _accountName, std::string const& _seedHint);
  99. SecretStore& store() { return m_store; }
  100. void importExisting(h128 const& _uuid, std::string const& _accountName, std::string const& _pass, std::string const& _passwordHint);
  101. void importExisting(h128 const& _uuid, std::string const& _accountName) { importExisting(_uuid, _accountName, defaultPassword(), std::string()); }
  102. void importExisting(h128 const& _uuid, std::string const& _accountName, Address const& _addr, h256 const& _passHash = h256(), std::string const& _passwordHint = std::string());
  103. /// @returns the secret key associated with an address provided the password query
  104. /// function @a _pass or the zero-secret key on error.
  105. Secret secret(Address const& _address, std::function<std::string()> const& _pass = DontKnowThrow, bool _usePasswordCache = true) const;
  106. /// @returns the secret key associated with the uuid of a key provided the password query
  107. /// function @a _pass or the zero-secret key on error.
  108. Secret secret(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow, bool _usePasswordCache = true) const;
  109. bool recode(Address const& _address, SemanticPassword _newPass, std::function<std::string()> const& _pass = DontKnowThrow, KDF _kdf = KDF::Scrypt);
  110. bool recode(Address const& _address, std::string const& _newPass, std::string const& _hint, std::function<std::string()> const& _pass = DontKnowThrow, KDF _kdf = KDF::Scrypt);
  111. void kill(h128 const& _id) { kill(address(_id)); }
  112. void kill(Address const& _a);
  113. static std::string defaultPath() { return getDataDir("ethereum") + "/keys.info"; }
  114. /// Extracts the secret key from the presale wallet.
  115. static KeyPair presaleSecret(std::string const& _json, std::function<std::string(bool)> const& _password);
  116. /// @returns the brainwallet secret for the given seed.
  117. static Secret brain(std::string const& _seed);
  118. /// @returns the HD subkey for a given key.
  119. static Secret subkey(Secret const& _s, unsigned _index);
  120. /// @returns new random keypair with given vanity
  121. static KeyPair newKeyPair(NewKeyType _type);
  122. private:
  123. std::string getPassword(h128 const& _uuid, std::function<std::string()> const& _pass = DontKnowThrow) const;
  124. std::string getPassword(h256 const& _passHash, std::function<std::string()> const& _pass = DontKnowThrow) const;
  125. std::string defaultPassword(std::function<std::string()> const& _pass = DontKnowThrow) const { return getPassword(m_master, _pass); }
  126. h256 hashPassword(std::string const& _pass) const;
  127. /// Stores the password by its hash in the password cache.
  128. void cachePassword(std::string const& _password) const;
  129. // Only use if previously loaded ok.
  130. // @returns false if wasn't previously loaded ok.
  131. bool write() const { return write(m_keysFile); }
  132. bool write(std::string const& _keysFile) const;
  133. void write(std::string const& _pass, std::string const& _keysFile) const; // TODO: all passwords should be a secure string.
  134. void write(SecureFixedHash<16> const& _key, std::string const& _keysFile) const;
  135. // Ethereum keys.
  136. /// Mapping key uuid -> address.
  137. std::unordered_map<h128, Address> m_uuidLookup;
  138. /// Mapping address -> key uuid.
  139. std::unordered_map<Address, h128> m_addrLookup;
  140. /// Mapping address -> key info.
  141. std::unordered_map<Address, KeyInfo> m_keyInfo;
  142. /// Mapping password hash -> password hint.
  143. std::unordered_map<h256, std::string> m_passwordHint;
  144. // Passwords that we're storing. Mapping password hash -> password.
  145. mutable std::unordered_map<h256, std::string> m_cachedPasswords;
  146. // DEPRECATED.
  147. // Used to be the default password for keys in the keystore, stored in the keys file.
  148. // Now the default password is based off the key of the keys file directly, so this is redundant
  149. // except for the fact that people have existing keys stored with it. Leave for now until/unless
  150. // we have an upgrade strategy.
  151. std::string m_defaultPasswordDeprecated;
  152. mutable std::string m_keysFile;
  153. mutable SecureFixedHash<16> m_keysFileKey;
  154. mutable h256 m_master;
  155. SecretStore m_store;
  156. };
  157. }
  158. }