|
|
|
@ -12,51 +12,53 @@
|
|
|
|
|
|
|
|
|
|
/// curl callback
|
|
|
|
|
static size_t
|
|
|
|
|
curl_RecvIdentKey(char *ptr, size_t, size_t nmemb, void *userdata)
|
|
|
|
|
curl_RecvIdentKey(char* ptr, size_t, size_t nmemb, void* userdata)
|
|
|
|
|
{
|
|
|
|
|
for(size_t idx = 0; idx < nmemb; idx++)
|
|
|
|
|
static_cast< std::vector< char > * >(userdata)->push_back(ptr[idx]);
|
|
|
|
|
static_cast< std::vector< char >* >(userdata)->push_back(ptr[idx]);
|
|
|
|
|
return nmemb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
|
{
|
|
|
|
|
KeyManager::KeyManager()
|
|
|
|
|
: m_initialized(false)
|
|
|
|
|
KeyManager::KeyManager() : m_initialized(false), m_needBackup(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
KeyManager::initialize(const llarp::Config& config, bool genIfAbsent)
|
|
|
|
|
{
|
|
|
|
|
if (m_initialized)
|
|
|
|
|
if(m_initialized)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_rcPath = config.router.ourRcFile();
|
|
|
|
|
m_idKeyPath = config.router.identKeyfile();
|
|
|
|
|
m_encKeyPath = config.router.encryptionKeyfile();
|
|
|
|
|
m_rcPath = config.router.ourRcFile();
|
|
|
|
|
m_idKeyPath = config.router.identKeyfile();
|
|
|
|
|
m_encKeyPath = config.router.encryptionKeyfile();
|
|
|
|
|
m_transportKeyPath = config.router.transportKeyfile();
|
|
|
|
|
|
|
|
|
|
m_usingLokid = config.lokid.whitelistRouters;
|
|
|
|
|
m_lokidRPCAddr = config.lokid.lokidRPCAddr;
|
|
|
|
|
m_lokidRPCUser = config.lokid.lokidRPCUser;
|
|
|
|
|
m_usingLokid = config.lokid.whitelistRouters;
|
|
|
|
|
m_lokidRPCAddr = config.lokid.lokidRPCAddr;
|
|
|
|
|
m_lokidRPCUser = config.lokid.lokidRPCUser;
|
|
|
|
|
m_lokidRPCPassword = config.lokid.lokidRPCPassword;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RouterContact rc;
|
|
|
|
|
bool exists = rc.Read(m_rcPath.c_str());
|
|
|
|
|
if (not exists and not genIfAbsent)
|
|
|
|
|
if(not exists and not genIfAbsent)
|
|
|
|
|
{
|
|
|
|
|
LogError("Could not read RouterContact at path ", m_rcPath);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we need to back up keys if our self.signed doesn't appear to have a
|
|
|
|
|
// valid signature
|
|
|
|
|
m_needBackup = (not rc.VerifySignature());
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
if (exists and not rc.VerifySignature())
|
|
|
|
|
if(exists and m_needBackup)
|
|
|
|
|
{
|
|
|
|
|
if (! genIfAbsent)
|
|
|
|
|
if(!genIfAbsent)
|
|
|
|
|
{
|
|
|
|
|
LogError("Our RouterContact ", m_rcPath, " is invalid or out of date");
|
|
|
|
|
return false;
|
|
|
|
@ -64,126 +66,114 @@ namespace llarp
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LogWarn("Our RouterContact ", m_rcPath,
|
|
|
|
|
" seems out of date, backing up and regenerating private keys");
|
|
|
|
|
" seems out of date, backing up and regenerating private keys");
|
|
|
|
|
|
|
|
|
|
if (! backupKeyFilesByMoving())
|
|
|
|
|
if(!backupKeyFilesByMoving())
|
|
|
|
|
{
|
|
|
|
|
LogError("Could not mv some key files, please ensure key files"
|
|
|
|
|
LogError(
|
|
|
|
|
"Could not mv some key files, please ensure key files"
|
|
|
|
|
" are backed up if needed and remove");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (not m_usingLokid)
|
|
|
|
|
if(not m_usingLokid)
|
|
|
|
|
{
|
|
|
|
|
// load identity key or create if needed
|
|
|
|
|
auto identityKeygen = [](llarp::SecretKey& key)
|
|
|
|
|
{
|
|
|
|
|
auto identityKeygen = [](llarp::SecretKey& key) {
|
|
|
|
|
// TODO: handle generating from service node seed
|
|
|
|
|
llarp::CryptoManager::instance()->identity_keygen(key);
|
|
|
|
|
};
|
|
|
|
|
if (not loadOrCreateKey(m_idKeyPath, m_idKey, identityKeygen))
|
|
|
|
|
if(not loadOrCreateKey(m_idKeyPath, identityKey, identityKeygen))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (not loadIdentityFromLokid())
|
|
|
|
|
if(not loadIdentityFromLokid())
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load encryption key
|
|
|
|
|
auto encryptionKeygen = [](llarp::SecretKey& key)
|
|
|
|
|
{
|
|
|
|
|
auto encryptionKeygen = [](llarp::SecretKey& key) {
|
|
|
|
|
llarp::CryptoManager::instance()->encryption_keygen(key);
|
|
|
|
|
};
|
|
|
|
|
if (not loadOrCreateKey(m_encKeyPath, m_encKey, encryptionKeygen))
|
|
|
|
|
if(not loadOrCreateKey(m_encKeyPath, encryptionKey, encryptionKeygen))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// TODO: transport key (currently done in LinkLayer)
|
|
|
|
|
auto transportKeygen = [](llarp::SecretKey& key)
|
|
|
|
|
{
|
|
|
|
|
auto transportKeygen = [](llarp::SecretKey& key) {
|
|
|
|
|
key.Zero();
|
|
|
|
|
CryptoManager::instance()->encryption_keygen(key);
|
|
|
|
|
};
|
|
|
|
|
if (not loadOrCreateKey(m_transportKeyPath, m_transportKey, transportKeygen))
|
|
|
|
|
if(not loadOrCreateKey(m_transportKeyPath, transportKey, transportKeygen))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const llarp::SecretKey&
|
|
|
|
|
KeyManager::getIdentityKey() const
|
|
|
|
|
{
|
|
|
|
|
return m_idKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const llarp::SecretKey&
|
|
|
|
|
KeyManager::getEncryptionKey() const
|
|
|
|
|
{
|
|
|
|
|
return m_encKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const llarp::SecretKey&
|
|
|
|
|
KeyManager::getTransportKey() const
|
|
|
|
|
{
|
|
|
|
|
return m_transportKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
KeyManager::backupKeyFilesByMoving() const
|
|
|
|
|
KeyManager::backupFileByMoving(const std::string& filepath)
|
|
|
|
|
{
|
|
|
|
|
auto findFreeBackupFilename = [](const fs::path& filepath) {
|
|
|
|
|
for (int i=0; i<9; i++)
|
|
|
|
|
for(int i = 0; i < 9; i++)
|
|
|
|
|
{
|
|
|
|
|
std::string ext("." + std::to_string(i) + ".bak");
|
|
|
|
|
fs::path newPath = filepath;
|
|
|
|
|
newPath += ext;
|
|
|
|
|
|
|
|
|
|
if (not fs::exists(newPath))
|
|
|
|
|
if(not fs::exists(newPath))
|
|
|
|
|
return newPath;
|
|
|
|
|
}
|
|
|
|
|
return fs::path();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> files = {
|
|
|
|
|
m_rcPath,
|
|
|
|
|
m_idKeyPath,
|
|
|
|
|
m_encKeyPath,
|
|
|
|
|
m_transportKeyPath
|
|
|
|
|
};
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
bool exists = fs::exists(filepath, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
{
|
|
|
|
|
LogError("Could not determine status of file ", filepath, ": ",
|
|
|
|
|
ec.message());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto& filepath : files)
|
|
|
|
|
if(not exists)
|
|
|
|
|
{
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
bool exists = fs::exists(filepath, ec);
|
|
|
|
|
if (ec)
|
|
|
|
|
{
|
|
|
|
|
LogError("Could not determine status of file ", filepath, ": ", ec.message());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
LogInfo("File ", filepath, " doesn't exist; no backup needed");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (not exists)
|
|
|
|
|
{
|
|
|
|
|
LogInfo("File ", filepath, " doesn't exist; no backup needed");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
fs::path newFilepath = findFreeBackupFilename(filepath);
|
|
|
|
|
if(newFilepath.empty())
|
|
|
|
|
{
|
|
|
|
|
LogWarn("Could not find an appropriate backup filename for", filepath);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs::path newFilepath = findFreeBackupFilename(filepath);
|
|
|
|
|
if (newFilepath.empty())
|
|
|
|
|
{
|
|
|
|
|
LogWarn("Could not find an appropriate backup filename for", filepath);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
LogInfo("Backing up (moving) key file ", filepath, " to ", newFilepath,
|
|
|
|
|
"...");
|
|
|
|
|
|
|
|
|
|
fs::rename(filepath, newFilepath, ec);
|
|
|
|
|
if(ec)
|
|
|
|
|
{
|
|
|
|
|
LogError("Failed to move key file ", ec.message());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LogInfo("Backing up (moving) key file ", filepath, " to ", newFilepath, "...");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs::rename(filepath, newFilepath, ec);
|
|
|
|
|
if (ec) {
|
|
|
|
|
LogError("Failed to move key file ", ec.message());
|
|
|
|
|
bool
|
|
|
|
|
KeyManager::backupKeyFilesByMoving() const
|
|
|
|
|
{
|
|
|
|
|
std::vector< std::string > files = {m_rcPath, m_idKeyPath, m_encKeyPath,
|
|
|
|
|
m_transportKeyPath};
|
|
|
|
|
|
|
|
|
|
for(auto& filepath : files)
|
|
|
|
|
{
|
|
|
|
|
if(not backupFileByMoving(filepath))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
@ -191,15 +181,14 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
KeyManager::loadOrCreateKey(
|
|
|
|
|
const std::string& filepath,
|
|
|
|
|
llarp::SecretKey& key,
|
|
|
|
|
std::function<void(llarp::SecretKey& key)> keygen)
|
|
|
|
|
const std::string& filepath, llarp::SecretKey& key,
|
|
|
|
|
std::function< void(llarp::SecretKey& key) > keygen)
|
|
|
|
|
{
|
|
|
|
|
fs::path path(filepath);
|
|
|
|
|
std::error_code ec;
|
|
|
|
|
if (! fs::exists(path, ec))
|
|
|
|
|
if(!fs::exists(path, ec))
|
|
|
|
|
{
|
|
|
|
|
if (ec)
|
|
|
|
|
if(ec)
|
|
|
|
|
{
|
|
|
|
|
LogError("Error checking key", filepath, ec.message());
|
|
|
|
|
return false;
|
|
|
|
@ -208,7 +197,7 @@ namespace llarp
|
|
|
|
|
LogInfo("Generating new key", filepath);
|
|
|
|
|
keygen(key);
|
|
|
|
|
|
|
|
|
|
if (! key.SaveToFile(filepath.c_str()))
|
|
|
|
|
if(!key.SaveToFile(filepath.c_str()))
|
|
|
|
|
{
|
|
|
|
|
LogError("Failed to save new key");
|
|
|
|
|
return false;
|
|
|
|
@ -222,7 +211,7 @@ namespace llarp
|
|
|
|
|
bool
|
|
|
|
|
KeyManager::loadIdentityFromLokid()
|
|
|
|
|
{
|
|
|
|
|
CURL *curl = curl_easy_init();
|
|
|
|
|
CURL* curl = curl_easy_init();
|
|
|
|
|
if(curl)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
@ -233,7 +222,7 @@ namespace llarp
|
|
|
|
|
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());
|
|
|
|
|
curl_slist *list = nullptr;
|
|
|
|
|
curl_slist* list = nullptr;
|
|
|
|
|
list = curl_slist_append(list, "Content-Type: application/json");
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
|
|
|
|
|
|
|
|
|
@ -266,7 +255,7 @@ namespace llarp
|
|
|
|
|
continue;
|
|
|
|
|
const auto k =
|
|
|
|
|
(*itr)["service_node_ed25519_privkey"].get< std::string >();
|
|
|
|
|
if(k.size() != (m_idKey.size() * 2))
|
|
|
|
|
if(k.size() != (identityKey.size() * 2))
|
|
|
|
|
{
|
|
|
|
|
if(k.empty())
|
|
|
|
|
{
|
|
|
|
@ -278,9 +267,9 @@ namespace llarp
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(not HexDecode(k.c_str(), m_idKey.data(), m_idKey.size()))
|
|
|
|
|
if(not HexDecode(k.c_str(), identityKey.data(), identityKey.size()))
|
|
|
|
|
continue;
|
|
|
|
|
if(CryptoManager::instance()->check_identity_privkey(m_idKey))
|
|
|
|
|
if(CryptoManager::instance()->check_identity_privkey(identityKey))
|
|
|
|
|
{
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
@ -289,7 +278,7 @@ namespace llarp
|
|
|
|
|
LogError("lokid gave bogus identity key");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch(nlohmann::json::exception &ex)
|
|
|
|
|
catch(nlohmann::json::exception& ex)
|
|
|
|
|
{
|
|
|
|
|
LogError("Bad response from lokid: ", ex.what());
|
|
|
|
|
}
|
|
|
|
@ -300,7 +289,8 @@ namespace llarp
|
|
|
|
|
}
|
|
|
|
|
if(ret)
|
|
|
|
|
{
|
|
|
|
|
LogInfo("Got Identity Keys from lokid: ", RouterID(seckey_topublic(m_idKey)));
|
|
|
|
|
LogInfo("Got Identity Keys from lokid: ",
|
|
|
|
|
RouterID(seckey_topublic(identityKey)));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|