Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
RouterInfo: verify signed router
Browse files Browse the repository at this point in the history
Verify that a router has a valid signature.

Referencing #627 + #917
  • Loading branch information
coneiric committed Jul 1, 2018
1 parent 012017b commit ed3efbb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/core/router/info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,7 @@ void RouterInfo::ReadFromBuffer(bool verify_signature)
// Verify signature
if (verify_signature)
{
// Note: signature length is guaranteed to be no less than buffer length
std::uint16_t const len =
m_Buffer.size() - m_RouterIdentity.GetSignatureLen();
if (!m_RouterIdentity.Verify(
m_Buffer.data(), len, m_Buffer.data() + len))
{
LOG(error) << "RouterInfo: signature verification failed";
m_IsUnreachable = true;
}
Verify();
m_RouterIdentity.DropVerifier();
}
}
Expand Down Expand Up @@ -699,6 +691,32 @@ void RouterInfo::CreateBuffer(const PrivateKeys& private_keys)
m_Buffer(
reinterpret_cast<const std::uint8_t*>(router_info.Str().c_str()),
router_info.Str().size());

// Verify signature
Verify();
}
catch (...)
{
m_Exception.Dispatch(__func__);
throw;
}
}

void RouterInfo::Verify()
{
try
{
if (!m_Buffer.data())
throw std::runtime_error("RouterInfo: null buffer");
std::size_t const len = m_Buffer.size() - m_RouterIdentity.GetSignatureLen();
if (len < Size::MinUnsignedBuffer)
throw std::length_error("RouterInfo: invalid RouterInfo size");
auto const buf = m_Buffer.data();
if (!m_RouterIdentity.Verify(buf, len, &buf[len]))
{
m_IsUnreachable = true;
throw std::runtime_error("RouterInfo: signature verification failed");
}
}
catch (...)
{
Expand Down
6 changes: 6 additions & 0 deletions src/core/router/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct RouterInfoTraits
{
MinBuffer = core::DSA_SIGNATURE_LENGTH, // TODO(unassigned): see #498
MaxBuffer = 2048, // TODO(anonimal): review if arbitrary
MinUnsignedBuffer = 399, // Minimum RouterInfo length w/o signature, see spec
// TODO(unassigned): algorithm to dynamically determine cost
NTCPCost = 10, // NTCP *should* have priority over SSU
SSUCost = 5,
Expand Down Expand Up @@ -523,6 +524,11 @@ class RouterInfo : public RouterInfoTraits, public RoutingDestination
/// (and subsequently sign the RI with)
void CreateBuffer(const PrivateKeys& private_keys);

/// @brief Verify RI signature
/// @throws std::length_error if unsigned buffer length is below minimum
/// @throws std::runtime_error if signature verification fails
void Verify();

/// @brief Save RI to file
/// @param path Full RI path of file to save to
void SaveToFile(const std::string& path);
Expand Down

0 comments on commit ed3efbb

Please sign in to comment.