SecretStore.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 SecretStore.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <functional>
  20. #include <mutex>
  21. #include <libdevcore/FixedHash.h>
  22. #include <libdevcore/FileSystem.h>
  23. #include "Common.h"
  24. namespace dev
  25. {
  26. enum class KDF {
  27. PBKDF2_SHA256,
  28. Scrypt,
  29. };
  30. /**
  31. * Manages encrypted keys stored in a certain directory on disk. The keys are read into memory
  32. * and changes to the keys are automatically synced to the directory.
  33. * Each file stores exactly one key in a specific JSON format whose file name is derived from the
  34. * UUID of the key.
  35. * @note that most of the functions here affect the filesystem and throw exceptions on failure,
  36. * and they also throw exceptions upon rare malfunction in the cryptographic functions.
  37. */
  38. class SecretStore
  39. {
  40. public:
  41. struct EncryptedKey
  42. {
  43. std::string encryptedKey;
  44. std::string filename;
  45. Address address;
  46. };
  47. /// Construct a new SecretStore but don't read any keys yet.
  48. /// Call setPath in
  49. SecretStore() = default;
  50. /// Construct a new SecretStore and read all keys in the given directory.
  51. SecretStore(std::string const& _path);
  52. /// Set a path for finding secrets.
  53. void setPath(std::string const& _path);
  54. /// @returns the secret key stored by the given @a _uuid.
  55. /// @param _pass function that returns the password for the key.
  56. /// @param _useCache if true, allow previously decrypted keys to be returned directly.
  57. bytesSec secret(h128 const& _uuid, std::function<std::string()> const& _pass, bool _useCache = true) const;
  58. /// @returns the secret key stored by the given @a _uuid.
  59. /// @param _pass function that returns the password for the key.
  60. static bytesSec secret(std::string const& _content, std::string const& _pass);
  61. /// @returns the secret key stored by the given @a _address.
  62. /// @param _pass function that returns the password for the key.
  63. bytesSec secret(Address const& _address, std::function<std::string()> const& _pass) const;
  64. /// Imports the (encrypted) key stored in the file @a _file and copies it to the managed directory.
  65. h128 importKey(std::string const& _file) { auto ret = readKey(_file, false); if (ret) save(); return ret; }
  66. /// Imports the (encrypted) key contained in the json formatted @a _content and stores it in
  67. /// the managed directory.
  68. h128 importKeyContent(std::string const& _content) { auto ret = readKeyContent(_content, std::string()); if (ret) save(); return ret; }
  69. /// Imports the decrypted key given by @a _s and stores it, encrypted with
  70. /// (a key derived from) the password @a _pass.
  71. h128 importSecret(bytesSec const& _s, std::string const& _pass);
  72. h128 importSecret(bytesConstRef _s, std::string const& _pass);
  73. /// Decrypts and re-encrypts the key identified by @a _uuid.
  74. bool recode(h128 const& _uuid, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
  75. /// Decrypts and re-encrypts the key identified by @a _address.
  76. bool recode(Address const& _address, std::string const& _newPass, std::function<std::string()> const& _pass, KDF _kdf = KDF::Scrypt);
  77. /// Removes the key specified by @a _uuid from both memory and disk.
  78. void kill(h128 const& _uuid);
  79. /// Returns the uuids of all stored keys.
  80. std::vector<h128> keys() const { return keysOf(m_keys); }
  81. /// @returns true iff we have the given key stored.
  82. bool contains(h128 const& _k) const { return m_keys.count(_k); }
  83. /// Clears all cached decrypted keys. The passwords have to be supplied in order to retrieve
  84. /// secrets again after calling this function.
  85. void clearCache() const;
  86. /// Import the key from the file @a _file, but do not copy it to the managed directory yet.
  87. /// @param _takeFileOwnership if true, deletes the file if it is not the canonical file for the
  88. /// key (derived from its uuid).
  89. h128 readKey(std::string const& _file, bool _takeFileOwnership);
  90. /// Import the key contained in the json-encoded @a _content, but do not store it in the
  91. /// managed directory.
  92. /// @param _file if given, assume this file contains @a _content and delete it later, if it is
  93. /// not the canonical file for the key (derived from the uuid).
  94. h128 readKeyContent(std::string const& _content, std::string const& _file = std::string());
  95. /// Store all keys in the directory @a _keysPath.
  96. void save(std::string const& _keysPath);
  97. /// Store all keys in the managed directory.
  98. void save() { save(m_path); }
  99. /// @returns true if the current file @arg _uuid contains an empty address. m_keys will be updated with the given @arg _address.
  100. bool noteAddress(h128 const& _uuid, Address const& _address);
  101. /// @returns the address of the given key or the zero address if it is unknown.
  102. Address address(h128 const& _uuid) const { return m_keys.at(_uuid).address; }
  103. /// @returns the default path for the managed directory.
  104. static std::string defaultPath() { return getDataDir("web3") + "/keys"; }
  105. private:
  106. /// Loads all keys in the given directory.
  107. void load(std::string const& _keysPath);
  108. void load() { load(m_path); }
  109. /// Encrypts @a _v with a key derived from @a _pass or the empty string on error.
  110. static std::string encrypt(bytesConstRef _v, std::string const& _pass, KDF _kdf = KDF::Scrypt);
  111. /// Decrypts @a _v with a key derived from @a _pass or the empty byte array on error.
  112. static bytesSec decrypt(std::string const& _v, std::string const& _pass);
  113. /// @returns the key given the @a _address.
  114. std::pair<h128 const, EncryptedKey> const* key(Address const& _address) const;
  115. std::pair<h128 const, EncryptedKey>* key(Address const& _address);
  116. /// Stores decrypted keys by uuid.
  117. mutable std::unordered_map<h128, bytesSec> m_cached;
  118. /// Stores encrypted keys together with the file they were loaded from by uuid.
  119. std::unordered_map<h128, EncryptedKey> m_keys;
  120. std::string m_path;
  121. };
  122. }