2019-11-22 03:57:41 +00:00
|
|
|
#include <config/key_manager.hpp>
|
|
|
|
|
2019-11-26 19:42:41 +00:00
|
|
|
#include <system_error>
|
2019-11-22 03:57:41 +00:00
|
|
|
#include <util/logging/logger.hpp>
|
2019-11-26 19:42:41 +00:00
|
|
|
#include "config/config.hpp"
|
|
|
|
#include "crypto/crypto.hpp"
|
|
|
|
#include "crypto/types.hpp"
|
2019-11-22 03:57:41 +00:00
|
|
|
|
2020-05-18 18:15:06 +00:00
|
|
|
#ifdef HAVE_CURL
|
2019-12-03 19:32:19 +00:00
|
|
|
#include <curl/curl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/// curl callback
|
|
|
|
static size_t
|
2019-12-06 17:13:09 +00:00
|
|
|
curl_RecvIdentKey(char* ptr, size_t, size_t nmemb, void* userdata)
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
for (size_t idx = 0; idx < nmemb; idx++)
|
|
|
|
static_cast<std::vector<char>*>(userdata)->push_back(ptr[idx]);
|
2019-12-03 19:32:19 +00:00
|
|
|
return nmemb;
|
|
|
|
}
|
|
|
|
|
2019-11-22 03:57:41 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-12-06 18:21:14 +00:00
|
|
|
KeyManager::KeyManager() : m_initialized(false), m_needBackup(false)
|
2019-11-22 03:57:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-06-04 16:57:29 +00:00
|
|
|
KeyManager::initialize(const llarp::Config& config, bool genIfAbsent, bool isRouter)
|
2019-11-22 03:57:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_initialized)
|
2019-11-26 19:42:41 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-01 23:11:04 +00:00
|
|
|
fs::path root = config.router.m_dataDir;
|
|
|
|
|
2020-04-29 19:41:39 +00:00
|
|
|
// utility function to assign a path, using the specified config parameter if present and
|
|
|
|
// falling back to root / defaultName if not
|
|
|
|
auto deriveFile = [&](const std::string& defaultName, const std::string& option) {
|
|
|
|
if (option.empty())
|
|
|
|
{
|
|
|
|
return root / defaultName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fs::path file(option);
|
|
|
|
if (not file.is_absolute())
|
|
|
|
throw std::runtime_error(stringify("override for ", defaultName, " cannot be relative"));
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
m_rcPath = deriveFile(our_rc_filename, config.router.m_routerContactFile);
|
|
|
|
m_idKeyPath = deriveFile(our_identity_filename, config.router.m_identityKeyFile);
|
|
|
|
m_encKeyPath = deriveFile(our_enc_key_filename, config.router.m_encryptionKeyFile);
|
|
|
|
m_transportKeyPath = deriveFile(our_transport_key_filename, config.router.m_transportKeyFile);
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
m_usingLokid = config.lokid.whitelistRouters;
|
|
|
|
m_lokidRPCAddr = config.lokid.lokidRPCAddr;
|
|
|
|
m_lokidRPCUser = config.lokid.lokidRPCUser;
|
2019-12-03 19:32:19 +00:00
|
|
|
m_lokidRPCPassword = config.lokid.lokidRPCPassword;
|
|
|
|
|
2019-11-22 23:11:59 +00:00
|
|
|
RouterContact rc;
|
2020-06-04 16:57:29 +00:00
|
|
|
bool exists = rc.Read(m_rcPath.c_str());
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not exists and not genIfAbsent)
|
2019-11-22 23:11:59 +00:00
|
|
|
{
|
2019-11-27 18:30:19 +00:00
|
|
|
LogError("Could not read RouterContact at path ", m_rcPath);
|
|
|
|
return false;
|
2019-11-22 23:11:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
// we need to back up keys if our self.signed doesn't appear to have a
|
|
|
|
// valid signature
|
2020-06-04 16:57:29 +00:00
|
|
|
m_needBackup = (isRouter and not rc.VerifySignature());
|
2019-12-06 18:21:14 +00:00
|
|
|
|
2019-11-27 18:30:19 +00:00
|
|
|
// if our RC file can't be verified, assume it is out of date (e.g. uses
|
|
|
|
// older encryption) and needs to be regenerated. before doing so, backup
|
|
|
|
// files that will be overwritten
|
2020-04-07 18:38:56 +00:00
|
|
|
if (exists and m_needBackup)
|
2019-11-27 18:30:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!genIfAbsent)
|
2019-11-27 18:30:19 +00:00
|
|
|
{
|
|
|
|
LogError("Our RouterContact ", m_rcPath, " is invalid or out of date");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogWarn(
|
|
|
|
"Our RouterContact ",
|
|
|
|
m_rcPath,
|
|
|
|
" seems out of date, backing up and regenerating private keys");
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!backupKeyFilesByMoving())
|
2019-11-27 18:30:19 +00:00
|
|
|
{
|
2019-12-06 17:13:09 +00:00
|
|
|
LogError(
|
|
|
|
"Could not mv some key files, please ensure key files"
|
2019-11-22 23:11:59 +00:00
|
|
|
" are backed up if needed and remove");
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-27 18:30:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not m_usingLokid)
|
2019-11-27 18:30:19 +00:00
|
|
|
{
|
2019-12-03 19:32:19 +00:00
|
|
|
// load identity key or create if needed
|
2019-12-06 17:13:09 +00:00
|
|
|
auto identityKeygen = [](llarp::SecretKey& key) {
|
2019-12-03 19:32:19 +00:00
|
|
|
// TODO: handle generating from service node seed
|
|
|
|
llarp::CryptoManager::instance()->identity_keygen(key);
|
|
|
|
};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not loadOrCreateKey(m_idKeyPath, identityKey, identityKeygen))
|
2019-12-03 19:32:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not loadIdentityFromLokid())
|
2019-12-03 19:32:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-26 19:42:41 +00:00
|
|
|
|
2019-11-27 18:30:19 +00:00
|
|
|
// load encryption key
|
2019-12-06 17:13:09 +00:00
|
|
|
auto encryptionKeygen = [](llarp::SecretKey& key) {
|
2019-11-27 18:30:19 +00:00
|
|
|
llarp::CryptoManager::instance()->encryption_keygen(key);
|
|
|
|
};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not loadOrCreateKey(m_encKeyPath, encryptionKey, encryptionKeygen))
|
2019-11-27 18:30:19 +00:00
|
|
|
return false;
|
2019-11-26 19:42:41 +00:00
|
|
|
|
2019-11-27 18:30:19 +00:00
|
|
|
// TODO: transport key (currently done in LinkLayer)
|
2019-12-06 17:13:09 +00:00
|
|
|
auto transportKeygen = [](llarp::SecretKey& key) {
|
2019-12-03 17:58:53 +00:00
|
|
|
key.Zero();
|
|
|
|
CryptoManager::instance()->encryption_keygen(key);
|
|
|
|
};
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not loadOrCreateKey(m_transportKeyPath, transportKey, transportKeygen))
|
2019-12-03 17:58:53 +00:00
|
|
|
return false;
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2019-11-26 19:42:41 +00:00
|
|
|
m_initialized = true;
|
2019-11-22 03:57:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-22 23:11:59 +00:00
|
|
|
bool
|
2020-04-01 23:11:04 +00:00
|
|
|
KeyManager::backupFileByMoving(const fs::path& filepath)
|
2019-11-22 23:11:59 +00:00
|
|
|
{
|
|
|
|
auto findFreeBackupFilename = [](const fs::path& filepath) {
|
2020-04-07 18:38:56 +00:00
|
|
|
for (int i = 0; i < 9; i++)
|
2019-11-22 23:11:59 +00:00
|
|
|
{
|
|
|
|
std::string ext("." + std::to_string(i) + ".bak");
|
|
|
|
fs::path newPath = filepath;
|
|
|
|
newPath += ext;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not fs::exists(newPath))
|
2019-11-22 23:11:59 +00:00
|
|
|
return newPath;
|
|
|
|
}
|
|
|
|
return fs::path();
|
|
|
|
};
|
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
std::error_code ec;
|
|
|
|
bool exists = fs::exists(filepath, ec);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-12-06 18:21:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogError("Could not determine status of file ", filepath, ": ", ec.message());
|
2019-12-06 18:21:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not exists)
|
2019-11-22 23:11:59 +00:00
|
|
|
{
|
2019-12-06 18:21:14 +00:00
|
|
|
LogInfo("File ", filepath, " doesn't exist; no backup needed");
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-27 18:30:19 +00:00
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
fs::path newFilepath = findFreeBackupFilename(filepath);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (newFilepath.empty())
|
2019-12-06 18:21:14 +00:00
|
|
|
{
|
|
|
|
LogWarn("Could not find an appropriate backup filename for", filepath);
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-27 18:30:19 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
LogInfo("Backing up (moving) key file ", filepath, " to ", newFilepath, "...");
|
2019-12-06 18:21:14 +00:00
|
|
|
|
|
|
|
fs::rename(filepath, newFilepath, ec);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-12-06 18:21:14 +00:00
|
|
|
{
|
|
|
|
LogError("Failed to move key file ", ec.message());
|
|
|
|
return false;
|
|
|
|
}
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-11-22 23:11:59 +00:00
|
|
|
|
2019-12-06 18:21:14 +00:00
|
|
|
bool
|
|
|
|
KeyManager::backupKeyFilesByMoving() const
|
|
|
|
{
|
2020-05-27 03:42:01 +00:00
|
|
|
std::vector<fs::path> files = {m_rcPath, m_idKeyPath, m_encKeyPath, m_transportKeyPath};
|
2019-12-06 18:21:14 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
for (auto& filepath : files)
|
2019-12-06 18:21:14 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not backupFileByMoving(filepath))
|
2019-11-22 23:11:59 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-26 19:42:41 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
KeyManager::loadOrCreateKey(
|
2020-04-07 20:41:11 +00:00
|
|
|
const fs::path& filepath,
|
|
|
|
llarp::SecretKey& key,
|
|
|
|
std::function<void(llarp::SecretKey& key)> keygen)
|
2019-11-26 19:42:41 +00:00
|
|
|
{
|
|
|
|
fs::path path(filepath);
|
|
|
|
std::error_code ec;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!fs::exists(path, ec))
|
2019-11-26 19:42:41 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ec)
|
2019-11-26 19:42:41 +00:00
|
|
|
{
|
|
|
|
LogError("Error checking key", filepath, ec.message());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
LogInfo("Generating new key", filepath);
|
|
|
|
keygen(key);
|
|
|
|
|
2020-06-04 16:57:29 +00:00
|
|
|
if (!key.SaveToFile(filepath.c_str()))
|
2019-11-26 19:42:41 +00:00
|
|
|
{
|
|
|
|
LogError("Failed to save new key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 18:30:19 +00:00
|
|
|
LogDebug("Loading key from file ", filepath);
|
2020-06-04 16:57:29 +00:00
|
|
|
return key.LoadFromFile(filepath.c_str());
|
2019-11-22 23:11:59 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 19:32:19 +00:00
|
|
|
bool
|
|
|
|
KeyManager::loadIdentityFromLokid()
|
|
|
|
{
|
2020-05-18 18:15:06 +00:00
|
|
|
#ifndef HAVE_CURL
|
|
|
|
LogError("this lokinet was not built with service node mode support");
|
2019-12-11 21:18:47 +00:00
|
|
|
return false;
|
|
|
|
#else
|
2019-12-06 17:13:09 +00:00
|
|
|
CURL* curl = curl_easy_init();
|
2020-04-07 18:38:56 +00:00
|
|
|
if (curl)
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "http://" << m_lokidRPCAddr << "/json_rpc";
|
|
|
|
const auto url = ss.str();
|
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY);
|
|
|
|
const auto auth = m_lokidRPCUser + ":" + m_lokidRPCPassword;
|
|
|
|
curl_easy_setopt(curl, CURLOPT_USERPWD, auth.c_str());
|
2019-12-06 17:13:09 +00:00
|
|
|
curl_slist* list = nullptr;
|
2019-12-03 19:32:19 +00:00
|
|
|
list = curl_slist_append(list, "Content-Type: application/json");
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
nlohmann::json request = {
|
|
|
|
{"id", "0"}, {"jsonrpc", "2.0"}, {"method", "get_service_node_privkey"}};
|
|
|
|
const auto data = request.dump();
|
|
|
|
std::vector<char> resp;
|
2019-12-03 19:32:19 +00:00
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.size());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &curl_RecvIdentKey);
|
2020-03-11 16:05:46 +00:00
|
|
|
|
|
|
|
resp.clear();
|
|
|
|
LogInfo("Getting Identity Keys from lokid...");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (curl_easy_perform(curl) == CURLE_OK)
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
try
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
auto j = nlohmann::json::parse(resp);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not j.is_object())
|
2020-03-11 16:05:46 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const auto itr = j.find("result");
|
2020-04-07 18:38:56 +00:00
|
|
|
if (itr == j.end())
|
2020-03-11 16:05:46 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not itr->is_object())
|
2020-03-11 16:05:46 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto k = (*itr)["service_node_ed25519_privkey"].get<std::string>();
|
|
|
|
if (k.size() != (identityKey.size() * 2))
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (k.empty())
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
LogError("lokid gave no identity key");
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
LogError("lokid gave invalid identity key");
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
2020-03-11 16:05:46 +00:00
|
|
|
return false;
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (not HexDecode(k.c_str(), identityKey.data(), identityKey.size()))
|
2020-03-11 16:05:46 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (CryptoManager::instance()->check_identity_privkey(identityKey))
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError("lokid gave bogus identity key");
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
catch (nlohmann::json::exception& ex)
|
2019-12-03 19:32:19 +00:00
|
|
|
{
|
2020-03-11 16:05:46 +00:00
|
|
|
LogError("Bad response from lokid: ", ex.what());
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
2020-03-11 16:05:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError("failed to get identity keys");
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ret)
|
2020-03-11 16:05:46 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
LogInfo("Got Identity Keys from lokid: ", RouterID(seckey_topublic(identityKey)));
|
2020-03-11 16:05:46 +00:00
|
|
|
}
|
2019-12-03 19:32:19 +00:00
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
curl_slist_free_all(list);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogError("failed to init curl");
|
|
|
|
return false;
|
|
|
|
}
|
2019-12-11 21:18:47 +00:00
|
|
|
#endif
|
2019-12-03 19:32:19 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:57:41 +00:00
|
|
|
} // namespace llarp
|