lokinet/llarp/exit/obtain_exit.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

#include <messages/exit.hpp>
#include <crypto/crypto.hpp>
2018-12-12 02:04:32 +00:00
#include <routing/handler.hpp>
namespace llarp
{
namespace routing
{
bool
ObtainExitMessage::Sign(const llarp::SecretKey& sk)
{
2019-02-02 23:12:42 +00:00
std::array< byte_t, 1024 > tmp;
llarp_buffer_t buf(tmp);
I = seckey_topublic(sk);
Z.Zero();
if(!BEncode(&buf))
{
return false;
}
buf.sz = buf.cur - buf.base;
return CryptoManager::instance()->sign(Z, sk, buf);
}
bool
ObtainExitMessage::Verify() const
{
2019-02-02 23:12:42 +00:00
std::array< byte_t, 1024 > tmp;
llarp_buffer_t buf(tmp);
ObtainExitMessage copy;
copy = *this;
copy.Z.Zero();
if(!copy.BEncode(&buf))
{
return false;
}
// rewind buffer
buf.sz = buf.cur - buf.base;
return CryptoManager::instance()->verify(I, buf, Z);
}
bool
ObtainExitMessage::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
2018-11-14 20:26:13 +00:00
if(!BEncodeWriteDictMsgType(buf, "A", "O"))
return false;
if(!BEncodeWriteDictArray("B", B, buf))
return false;
if(!BEncodeWriteDictInt("E", E, buf))
return false;
if(!BEncodeWriteDictEntry("I", I, buf))
return false;
if(!BEncodeWriteDictInt("S", S, buf))
return false;
if(!BEncodeWriteDictInt("T", T, buf))
return false;
if(!BEncodeWriteDictInt("V", version, buf))
return false;
if(!BEncodeWriteDictArray("W", W, buf))
return false;
if(!BEncodeWriteDictInt("X", X, buf))
return false;
if(!BEncodeWriteDictEntry("Z", Z, buf))
return false;
return bencode_end(buf);
}
bool
2019-02-02 23:12:42 +00:00
ObtainExitMessage::DecodeKey(const llarp_buffer_t& k, llarp_buffer_t* buf)
{
bool read = false;
if(!BEncodeMaybeReadDictList("B", B, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("E", E, read, k, buf))
return false;
if(!BEncodeMaybeReadDictEntry("I", I, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("S", S, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("T", T, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("V", version, read, k, buf))
return false;
if(!BEncodeMaybeReadDictList("W", W, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("X", X, read, k, buf))
return false;
if(!BEncodeMaybeReadDictEntry("Z", Z, read, k, buf))
return false;
return read;
}
bool
ObtainExitMessage::HandleMessage(IMessageHandler* h,
AbstractRouter* r) const
{
return h->HandleObtainExitMessage(*this, r);
}
} // namespace routing
} // namespace llarp