Fabcoin Core  0.16.2
P2P Digital Currency
Classes | Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
dev::SecretStore Class Reference

Manages encrypted keys stored in a certain directory on disk. More...

#include <SecretStore.h>

Classes

struct  EncryptedKey
 

Public Member Functions

 SecretStore ()=default
 Construct a new SecretStore but don't read any keys yet. More...
 
 SecretStore (std::string const &_path)
 Construct a new SecretStore and read all keys in the given directory. More...
 
void setPath (std::string const &_path)
 Set a path for finding secrets. More...
 
bytesSec secret (h128 const &_uuid, std::function< std::string()> const &_pass, bool _useCache=true) const
 
bytesSec secret (Address const &_address, std::function< std::string()> const &_pass) const
 
h128 importKey (std::string const &_file)
 Imports the (encrypted) key stored in the file _file and copies it to the managed directory. More...
 
h128 importKeyContent (std::string const &_content)
 Imports the (encrypted) key contained in the json formatted _content and stores it in the managed directory. More...
 
h128 importSecret (bytesSec const &_s, std::string const &_pass)
 Imports the decrypted key given by _s and stores it, encrypted with (a key derived from) the password _pass. More...
 
h128 importSecret (bytesConstRef _s, std::string const &_pass)
 
bool recode (h128 const &_uuid, std::string const &_newPass, std::function< std::string()> const &_pass, KDF _kdf=KDF::Scrypt)
 Decrypts and re-encrypts the key identified by _uuid. More...
 
bool recode (Address const &_address, std::string const &_newPass, std::function< std::string()> const &_pass, KDF _kdf=KDF::Scrypt)
 Decrypts and re-encrypts the key identified by _address. More...
 
void kill (h128 const &_uuid)
 Removes the key specified by _uuid from both memory and disk. More...
 
std::vector< h128keys () const
 Returns the uuids of all stored keys. More...
 
bool contains (h128 const &_k) const
 
void clearCache () const
 Clears all cached decrypted keys. More...
 
h128 readKey (std::string const &_file, bool _takeFileOwnership)
 Import the key from the file _file, but do not copy it to the managed directory yet. More...
 
h128 readKeyContent (std::string const &_content, std::string const &_file=std::string())
 Import the key contained in the json-encoded _content, but do not store it in the managed directory. More...
 
void save (std::string const &_keysPath)
 Store all keys in the directory _keysPath. More...
 
void save ()
 Store all keys in the managed directory. More...
 
bool noteAddress (h128 const &_uuid, Address const &_address)
 
Address address (h128 const &_uuid) const
 

Static Public Member Functions

static bytesSec secret (std::string const &_content, std::string const &_pass)
 
static std::string defaultPath ()
 

Private Member Functions

void load (std::string const &_keysPath)
 Loads all keys in the given directory. More...
 
void load ()
 
std::pair< h128 const, EncryptedKey > const * key (Address const &_address) const
 
std::pair< h128 const, EncryptedKey > * key (Address const &_address)
 

Static Private Member Functions

static std::string encrypt (bytesConstRef _v, std::string const &_pass, KDF _kdf=KDF::Scrypt)
 Encrypts _v with a key derived from _pass or the empty string on error. More...
 
static bytesSec decrypt (std::string const &_v, std::string const &_pass)
 Decrypts _v with a key derived from _pass or the empty byte array on error. More...
 

Private Attributes

std::unordered_map< h128, bytesSecm_cached
 Stores decrypted keys by uuid. More...
 
std::unordered_map< h128, EncryptedKeym_keys
 Stores encrypted keys together with the file they were loaded from by uuid. More...
 
std::string m_path
 

Detailed Description

Manages encrypted keys stored in a certain directory on disk.

The keys are read into memory and changes to the keys are automatically synced to the directory. Each file stores exactly one key in a specific JSON format whose file name is derived from the UUID of the key.

Note
that most of the functions here affect the filesystem and throw exceptions on failure, and they also throw exceptions upon rare malfunction in the cryptographic functions.

Definition at line 46 of file SecretStore.h.

Constructor & Destructor Documentation

dev::SecretStore::SecretStore ( )
default

Construct a new SecretStore but don't read any keys yet.

Call setPath in

dev::SecretStore::SecretStore ( std::string const &  _path)

Construct a new SecretStore and read all keys in the given directory.

Member Function Documentation

Address dev::SecretStore::address ( h128 const &  _uuid) const
inline
Returns
the address of the given key or the zero address if it is unknown.

Definition at line 119 of file SecretStore.h.

Here is the caller graph for this function:

void SecretStore::clearCache ( ) const

Clears all cached decrypted keys.

The passwords have to be supplied in order to retrieve secrets again after calling this function.

Definition at line 174 of file SecretStore.cpp.

Here is the call graph for this function:

bool dev::SecretStore::contains ( h128 const &  _k) const
inline
Returns
true iff we have the given key stored.

Definition at line 96 of file SecretStore.h.

Here is the caller graph for this function:

bytesSec SecretStore::decrypt ( std::string const &  _v,
std::string const &  _pass 
)
staticprivate

Decrypts _v with a key derived from _pass or the empty byte array on error.

Definition at line 367 of file SecretStore.cpp.

Here is the call graph for this function:

static std::string dev::SecretStore::defaultPath ( )
inlinestatic
Returns
the default path for the managed directory.

Definition at line 122 of file SecretStore.h.

Here is the call graph for this function:

string SecretStore::encrypt ( bytesConstRef  _v,
std::string const &  _pass,
KDF  _kdf = KDF::Scrypt 
)
staticprivate

Encrypts _v with a key derived from _pass or the empty string on error.

Definition at line 337 of file SecretStore.cpp.

Here is the call graph for this function:

h128 dev::SecretStore::importKey ( std::string const &  _file)
inline

Imports the (encrypted) key stored in the file _file and copies it to the managed directory.

Definition at line 77 of file SecretStore.h.

h128 dev::SecretStore::importKeyContent ( std::string const &  _content)
inline

Imports the (encrypted) key contained in the json formatted _content and stores it in the managed directory.

Definition at line 80 of file SecretStore.h.

h128 dev::SecretStore::importSecret ( bytesSec const &  _s,
std::string const &  _pass 
)

Imports the decrypted key given by _s and stores it, encrypted with (a key derived from) the password _pass.

Here is the caller graph for this function:

h128 dev::SecretStore::importSecret ( bytesConstRef  _s,
std::string const &  _pass 
)
pair< h128 const, SecretStore::EncryptedKey > const * SecretStore::key ( Address const &  _address) const
private
Returns
the key given the _address.

Definition at line 274 of file SecretStore.cpp.

pair< h128 const, SecretStore::EncryptedKey > * SecretStore::key ( Address const &  _address)
private

Definition at line 282 of file SecretStore.cpp.

Here is the call graph for this function:

std::vector<h128> dev::SecretStore::keys ( ) const
inline

Returns the uuids of all stored keys.

Definition at line 93 of file SecretStore.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void SecretStore::kill ( h128 const &  _uuid)

Removes the key specified by _uuid from both memory and disk.

Definition at line 164 of file SecretStore.cpp.

Here is the caller graph for this function:

void dev::SecretStore::load ( std::string const &  _keysPath)
private

Loads all keys in the given directory.

void dev::SecretStore::load ( )
inlineprivate

Definition at line 127 of file SecretStore.h.

Here is the call graph for this function:

Here is the caller graph for this function:

bool SecretStore::noteAddress ( h128 const &  _uuid,
Address const &  _address 
)
Returns
true if the current file
  • _uuid contains an empty address. m_keys will be updated with the given
  • _address.

Definition at line 202 of file SecretStore.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

h128 SecretStore::readKey ( std::string const &  _file,
bool  _takeFileOwnership 
)

Import the key from the file _file, but do not copy it to the managed directory yet.

Parameters
_takeFileOwnershipif true, deletes the file if it is not the canonical file for the key (derived from its uuid).

Definition at line 224 of file SecretStore.cpp.

Here is the call graph for this function:

h128 SecretStore::readKeyContent ( std::string const &  _content,
std::string const &  _file = std::string() 
)

Import the key contained in the json-encoded _content, but do not store it in the managed directory.

Parameters
_fileif given, assume this file contains _content and delete it later, if it is not the canonical file for the key (derived from the uuid).

Definition at line 230 of file SecretStore.cpp.

Here is the call graph for this function:

bool dev::SecretStore::recode ( h128 const &  _uuid,
std::string const &  _newPass,
std::function< std::string()> const &  _pass,
KDF  _kdf = KDF::Scrypt 
)

Decrypts and re-encrypts the key identified by _uuid.

Here is the caller graph for this function:

bool dev::SecretStore::recode ( Address const &  _address,
std::string const &  _newPass,
std::function< std::string()> const &  _pass,
KDF  _kdf = KDF::Scrypt 
)

Decrypts and re-encrypts the key identified by _address.

void dev::SecretStore::save ( std::string const &  _keysPath)

Store all keys in the directory _keysPath.

Here is the caller graph for this function:

void dev::SecretStore::save ( )
inline

Store all keys in the managed directory.

Definition at line 115 of file SecretStore.h.

Here is the call graph for this function:

Here is the caller graph for this function:

bytesSec dev::SecretStore::secret ( h128 const &  _uuid,
std::function< std::string()> const &  _pass,
bool  _useCache = true 
) const
Returns
the secret key stored by the given _uuid.
Parameters
_passfunction that returns the password for the key.
_useCacheif true, allow previously decrypted keys to be returned directly.

Here is the caller graph for this function:

static bytesSec dev::SecretStore::secret ( std::string const &  _content,
std::string const &  _pass 
)
static
Returns
the secret key stored by the given _uuid.
Parameters
_passfunction that returns the password for the key.
bytesSec dev::SecretStore::secret ( Address const &  _address,
std::function< std::string()> const &  _pass 
) const
Returns
the secret key stored by the given _address.
Parameters
_passfunction that returns the password for the key.
void SecretStore::setPath ( std::string const &  _path)

Set a path for finding secrets.

Definition at line 99 of file SecretStore.cpp.

Here is the call graph for this function:

Member Data Documentation

std::unordered_map<h128, bytesSec> dev::SecretStore::m_cached
mutableprivate

Stores decrypted keys by uuid.

Definition at line 136 of file SecretStore.h.

std::unordered_map<h128, EncryptedKey> dev::SecretStore::m_keys
private

Stores encrypted keys together with the file they were loaded from by uuid.

Definition at line 138 of file SecretStore.h.

std::string dev::SecretStore::m_path
private

Definition at line 140 of file SecretStore.h.


The documentation for this class was generated from the following files: