Avoid trivial getters/setters in KeyManager

This commit is contained in:
Stephen Shelton 2019-12-06 10:31:19 -07:00
parent 66a058a2af
commit 11410a2748
5 changed files with 21 additions and 93 deletions

View File

@ -81,7 +81,7 @@ namespace llarp
// 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
@ -94,7 +94,7 @@ namespace llarp
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)
@ -102,49 +102,13 @@ namespace llarp
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;
}
void
KeyManager::setIdentityKey(const llarp::SecretKey& key)
{
m_idKey = key;
}
const llarp::SecretKey&
KeyManager::getEncryptionKey() const
{
return m_encKey;
}
void
KeyManager::setEncryptionKey(const llarp::SecretKey& key)
{
m_encKey = key;
}
const llarp::SecretKey&
KeyManager::getTransportKey() const
{
return m_transportKey;
}
void
KeyManager::setTransportKey(const llarp::SecretKey& key)
{
m_transportKey = key;
}
bool
KeyManager::backupKeyFilesByMoving() const
{
@ -278,7 +242,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())
{
@ -290,9 +254,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;
}
@ -313,7 +277,7 @@ namespace llarp
if(ret)
{
LogInfo("Got Identity Keys from lokid: ",
RouterID(seckey_topublic(m_idKey)));
RouterID(seckey_topublic(identityKey)));
break;
}
else

View File

@ -38,42 +38,6 @@ namespace llarp
bool
initialize(const llarp::Config& config, bool genIfAbsent);
/// Obtain the identity key (e.g. ~/.lokinet/identity.private)
///
/// @return a reference to the identity key
const llarp::SecretKey&
getIdentityKey() const;
/// Set the identity key. This does not write anything to disk.
///
/// @param key is the key that will be copied-from.
void
setIdentityKey(const llarp::SecretKey& key);
/// Obtain the encryption key (e.g. ~/.lokinet/encryption.private)
///
/// @return a reference to the encryption key
const llarp::SecretKey&
getEncryptionKey() const;
/// Set the encryption key. This does not write anything to disk.
///
/// @param key is the key that will be copied-from.
void
setEncryptionKey(const llarp::SecretKey& key);
/// Obtain the transport key (e.g. ~/.lokinet/transport.private)
///
/// @return a reference to the transport key
const llarp::SecretKey&
getTransportKey() const;
/// Set the transport key. This does not write anything to disk.
///
/// @param key is the key that will be copied-from.
void
setTransportKey(const llarp::SecretKey& key);
/// Obtain the self-signed RouterContact
///
/// @param rc (out) will be modified to contian the RouterContact
@ -81,6 +45,10 @@ namespace llarp
bool
getRouterContact(llarp::RouterContact& rc) const;
llarp::SecretKey identityKey;
llarp::SecretKey encryptionKey;
llarp::SecretKey transportKey;
private:
std::string m_rcPath;
std::string m_idKeyPath;
@ -93,10 +61,6 @@ namespace llarp
std::string m_lokidRPCUser;
std::string m_lokidRPCPassword;
llarp::SecretKey m_idKey;
llarp::SecretKey m_encKey;
llarp::SecretKey m_transportKey;
/// Backup each key file (by copying, e.g. foo -> foo.bak)
bool
backupKeyFilesByMoving() const;

View File

@ -26,8 +26,8 @@ namespace llarp
, SessionClosed(std::move(closed))
, SessionRenegotiate(std::move(reneg))
, PumpDone(std::move(pumpDone))
, m_RouterEncSecret(keyManager->getEncryptionKey())
, m_SecretKey(keyManager->getTransportKey())
, m_RouterEncSecret(keyManager->encryptionKey)
, m_SecretKey(keyManager->transportKey)
{
}

View File

@ -208,8 +208,8 @@ namespace llarp
#endif
}
_identity = m_keyManager->getIdentityKey();
_encryption = m_keyManager->getEncryptionKey();
_identity = m_keyManager->identityKey;
_encryption = m_keyManager->encryptionKey;
if(_identity.IsZero())
return false;

View File

@ -29,15 +29,15 @@ struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
SecretKey signingKey;
CryptoManager::instance()->identity_keygen(signingKey);
keyManager->setIdentityKey(signingKey);
keyManager->identityKey = signingKey;
SecretKey encryptionKey;
CryptoManager::instance()->encryption_keygen(encryptionKey);
keyManager->setEncryptionKey(encryptionKey);
keyManager->encryptionKey = encryptionKey;
SecretKey transportKey;
CryptoManager::instance()->encryption_keygen(transportKey);
keyManager->setTransportKey(transportKey);
keyManager->transportKey = transportKey;
rc.pubkey = signingKey.toPublic();
@ -105,7 +105,7 @@ struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
rc.addrs.emplace_back();
if(!link->GetOurAddressInfo(rc.addrs[0]))
return false;
if(!rc.Sign(keyManager->getIdentityKey()))
if(!rc.Sign(keyManager->identityKey))
return false;
return link->Start(logic, worker);
}
@ -227,7 +227,7 @@ TEST_F(LinkLayerTest, TestIWP)
// SignBufferFunc
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.keyManager->getIdentityKey(), buf);
return m_crypto.sign(sig, Alice.keyManager->identityKey, buf);
},
// SessionEstablishedHandler
@ -294,7 +294,7 @@ TEST_F(LinkLayerTest, TestIWP)
// SignBufferFunc
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.keyManager->getIdentityKey(), buf);
return m_crypto.sign(sig, Bob.keyManager->identityKey, buf);
},
//SessionEstablishedHandler