2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2019-11-22 03:57:41 +00:00
|
|
|
|
2019-11-26 19:42:41 +00:00
|
|
|
#include <atomic>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "config.hpp"
|
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
2019-11-22 03:57:41 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2019-12-06 17:13:09 +00:00
|
|
|
/// KeyManager manages the cryptographic keys stored on disk for the local
|
|
|
|
/// node. This includes private keys as well as the self-signed router contact
|
|
|
|
/// file (e.g. "self.signed").
|
2019-11-22 03:57:41 +00:00
|
|
|
///
|
2019-12-06 17:13:09 +00:00
|
|
|
/// Keys are either read from disk if they exist and are valid (see below) or
|
|
|
|
/// are generated and written to disk.
|
|
|
|
///
|
|
|
|
/// In addition, the KeyManager detects when the keys obsolete (e.g. as a
|
|
|
|
/// result of a software upgrade) and backs up existing keys before writing
|
|
|
|
/// out new ones.
|
|
|
|
struct KeyManager
|
|
|
|
{
|
2019-12-06 18:21:14 +00:00
|
|
|
/// Utility function to backup a file by moving it. Attempts to find a new
|
|
|
|
/// filename based on the original that doesn't exist, then moves it. The
|
|
|
|
/// pattern used is originalFile.N.bak where N is the lowest integer
|
|
|
|
/// matching a filename that doesn't exist.
|
|
|
|
///
|
|
|
|
/// @param filepath is the name of the original file to backup.
|
|
|
|
/// @return true if the file could be moved or didn't exist, false otherwise
|
|
|
|
static bool
|
2020-04-01 23:11:04 +00:00
|
|
|
backupFileByMoving(const fs::path& filepath);
|
2019-12-06 18:21:14 +00:00
|
|
|
|
2019-11-22 03:57:41 +00:00
|
|
|
/// Constructor
|
2019-11-26 19:42:41 +00:00
|
|
|
KeyManager();
|
2019-11-22 03:57:41 +00:00
|
|
|
|
2020-05-20 11:41:42 +00:00
|
|
|
/// Initializes keys using the provided config, loading from disk
|
2019-11-22 03:57:41 +00:00
|
|
|
///
|
|
|
|
/// NOTE: Must be called prior to obtaining any keys.
|
2019-12-03 19:32:19 +00:00
|
|
|
/// NOTE: blocks on I/O
|
2019-11-22 03:57:41 +00:00
|
|
|
///
|
2019-11-26 19:42:41 +00:00
|
|
|
/// @param config should be a prepared config object
|
2020-05-18 17:17:25 +00:00
|
|
|
/// @param genIfAbsent determines whether or not we will create files if they
|
2019-11-22 03:57:41 +00:00
|
|
|
/// do not exist.
|
2021-03-23 19:00:46 +00:00
|
|
|
/// @param isSNode
|
2019-11-22 03:57:41 +00:00
|
|
|
/// @return true on success, false otherwise
|
|
|
|
bool
|
2021-03-23 19:00:46 +00:00
|
|
|
initialize(const llarp::Config& config, bool genIfAbsent, bool isSNode);
|
2019-11-22 03:57:41 +00:00
|
|
|
|
|
|
|
/// Obtain the self-signed RouterContact
|
|
|
|
///
|
|
|
|
/// @param rc (out) will be modified to contian the RouterContact
|
|
|
|
/// @return true on success, false otherwise
|
|
|
|
bool
|
|
|
|
getRouterContact(llarp::RouterContact& rc) const;
|
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
/// Return whether or not we need to backup keys as we load them
|
|
|
|
bool
|
|
|
|
needBackup() const
|
|
|
|
{
|
|
|
|
return m_needBackup;
|
|
|
|
}
|
|
|
|
|
2019-12-06 17:31:19 +00:00
|
|
|
llarp::SecretKey identityKey;
|
|
|
|
llarp::SecretKey encryptionKey;
|
|
|
|
llarp::SecretKey transportKey;
|
|
|
|
|
2020-04-01 23:11:04 +00:00
|
|
|
fs::path m_rcPath;
|
|
|
|
fs::path m_idKeyPath;
|
|
|
|
fs::path m_encKeyPath;
|
|
|
|
fs::path m_transportKeyPath;
|
2020-06-04 18:38:35 +00:00
|
|
|
|
|
|
|
private:
|
2019-11-26 19:42:41 +00:00
|
|
|
std::atomic_bool m_initialized;
|
2019-12-06 18:21:14 +00:00
|
|
|
std::atomic_bool m_needBackup;
|
2019-11-26 19:42:41 +00:00
|
|
|
|
2019-11-22 23:11:59 +00:00
|
|
|
/// Backup each key file (by copying, e.g. foo -> foo.bak)
|
|
|
|
bool
|
|
|
|
backupKeyFilesByMoving() const;
|
2019-11-26 19:42:41 +00:00
|
|
|
|
|
|
|
/// Load the key at a given filepath or create it
|
|
|
|
///
|
|
|
|
/// @param keygen is a function that will generate the key if needed
|
|
|
|
static bool
|
2020-04-07 20:41:11 +00:00
|
|
|
loadOrCreateKey(
|
2020-10-30 19:51:22 +00:00
|
|
|
fs::path filepath,
|
2020-04-07 20:41:11 +00:00
|
|
|
llarp::SecretKey& key,
|
|
|
|
std::function<void(llarp::SecretKey& key)> keygen);
|
2019-11-22 03:57:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace llarp
|