Added challenge issue and verification for peer connections (#93)

Implemented flatbuffer peer challenge issue and verification messages.
Replaced session flags with challenge status enum.
This commit is contained in:
Ravidu Lashan
2020-04-20 05:55:42 +05:30
committed by GitHub
parent 721daf19f3
commit ef8ec0f51d
12 changed files with 303 additions and 145 deletions

View File

@@ -38,12 +38,14 @@ int peer_session_handler::on_connect(comm::comm_session &session) const
}
}
// Send our peer id.
// Send peer challenge.
flatbuffers::FlatBufferBuilder fbuf(1024);
p2pmsg::create_msg_from_peerid(fbuf, conf::cfg.self_peerid);
p2pmsg::create_msg_from_peer_challenge(fbuf, session.issued_challenge);
std::string_view msg = std::string_view(
reinterpret_cast<const char *>(fbuf.GetBufferPointer()), fbuf.GetSize());
return session.send(msg);
session.send(msg);
session.challenge_status = comm::CHALLENGE_ISSUED;
return 0;
}
//peer session on message callback method
@@ -74,19 +76,30 @@ int peer_session_handler::on_message(comm::comm_session &session, std::string_vi
const p2pmsg::Message content_message_type = content->message_type(); //i.e - proposal, npl, state request, state response, etc
if (content_message_type == p2pmsg::Message_PeerId_Notify_Message) // message is a peer id announcement
if (content_message_type == p2pmsg::Message_Peer_Challenge_Message) // message is a peer challenge announcement
{
// Ignore if Peer ID is already resolved.
if (!session.flags[comm::SESSION_FLAG::PEERID_RESOLVED])
// Sending the challenge response to the respected peer.
const std::string challenge = std::string(p2pmsg::get_peer_challenge_from_msg(*content->message_as_Peer_Challenge_Message()));
flatbuffers::FlatBufferBuilder fbuf(1024);
p2pmsg::create_peer_challenge_response_from_challenge(fbuf, challenge);
std::string_view msg = std::string_view(
reinterpret_cast<const char *>(fbuf.GetBufferPointer()), fbuf.GetSize());
return session.send(msg);
}
if (content_message_type == p2pmsg::Message_Peer_Challenge_Response_Message) // message is a peer challenge response
{
// Ignore if challenge is already resolved.
if (session.challenge_status == comm::CHALLENGE_ISSUED)
{
const std::string peerid = std::string(p2pmsg::get_peerid_from_msg(*content->message_as_PeerId_Notify_Message()));
return p2p::resolve_session_peerid(session, peerid);
const p2p::peer_challenge_response challenge_resp = p2pmsg::create_peer_challenge_response_from_msg(*content->message_as_Peer_Challenge_Response_Message(), container->pubkey());
return p2p::resolve_peer_challenge(session, challenge_resp);
}
}
if (!session.flags[comm::SESSION_FLAG::PEERID_RESOLVED])
if (session.challenge_status != comm::CHALLENGE_VERIFIED)
{
LOG_DBG << "Cannot accept messages. Peer id unresolved. " << session.uniqueid;
LOG_DBG << "Cannot accept messages. Peer challenge unresolved. " << session.uniqueid;
return 0;
}