make signatures work again ;~;

pull/15/head
Jeff Becker 6 years ago
parent 973f86c900
commit 4e693a2414
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -27,6 +27,9 @@ namespace llarp
~AddressInfo();
AddressInfo&
operator=(const AddressInfo& other);
bool
BEncode(llarp_buffer_t* buf) const;

@ -57,7 +57,7 @@ typedef bool (*llarp_hmac_func)(byte_t *, llarp_buffer_t, const byte_t *);
/// S(sig, secretkey, body)
typedef bool (*llarp_sign_func)(byte_t *, const byte_t *, llarp_buffer_t);
/// V(sig, body, pubkey)
/// V(pubkey, body, sig)
typedef bool (*llarp_verify_func)(const byte_t *, llarp_buffer_t,
const byte_t *);

@ -29,6 +29,9 @@ namespace llarp
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf);
ExitInfo &
operator=(const ExitInfo &other);
friend std::ostream &
operator<<(std::ostream &out, const ExitInfo &xi)
{

@ -26,7 +26,7 @@ namespace llarp
template < typename T, size_t align = 128 >
void
DumpBuffer(const T &buff)
DumpBufferHex(const T &buff)
{
size_t idx = 0;
printf("buffer of size %zu\n", buff.sz);
@ -51,7 +51,7 @@ namespace llarp
template < typename T, size_t align = 128 >
void
DumpBufferHex(const T &buff)
DumpBuffer(const T &buff)
{
size_t idx = 0;
printf("buffer of size %zu\n", buff.sz);
@ -65,6 +65,10 @@ namespace llarp
{
printf("%c", buff.base[idx]);
}
else
{
printf(".");
}
if(buff.base + idx == buff.cur)
{
printf("%c[0;0m", 27);

@ -5,7 +5,7 @@
#include <llarp/bencode.hpp>
#include <llarp/exit_info.hpp>
#include <list>
#include <vector>
#define MAX_RC_SIZE (1024)
#define NICKLEN (32)
@ -15,13 +15,13 @@ namespace llarp
struct RouterContact : public IBEncodeMessage
{
// advertised addresses
std::list< AddressInfo > addrs;
std::vector< AddressInfo > addrs;
// public encryption public key
llarp::PubKey enckey;
// public signing public key
llarp::PubKey pubkey;
// advertised exits
std::list< ExitInfo > exits;
std::vector< ExitInfo > exits;
// signature
llarp::Signature signature;
/// node nickname, yw kee
@ -32,6 +32,16 @@ namespace llarp
bool
BEncode(llarp_buffer_t *buf) const;
void
Clear();
bool
BDecode(llarp_buffer_t *buf)
{
Clear();
return IBEncodeMessage::BDecode(buf);
}
bool
DecodeKey(llarp_buffer_t k, llarp_buffer_t *buf);

@ -12,6 +12,17 @@ namespace llarp
{
}
AddressInfo &
AddressInfo::operator=(const AddressInfo &other)
{
rank = other.rank;
dialect = other.dialect;
pubkey = other.pubkey;
memcpy(ip.s6_addr, other.ip.s6_addr, 16);
port = other.port;
return *this;
}
bool
AddressInfo::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{

@ -14,6 +14,16 @@ namespace llarp
{
}
ExitInfo&
ExitInfo::operator=(const ExitInfo& other)
{
memcpy(address.s6_addr, other.address.s6_addr, 16);
memcpy(netmask.s6_addr, other.netmask.s6_addr, 16);
pubkey = other.pubkey;
version = other.version;
return *this;
}
bool
ExitInfo::BEncode(llarp_buffer_t* buf) const
{
@ -44,7 +54,7 @@ namespace llarp
ExitInfo::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
{
bool read = false;
// TODO: implement me
return read;
}

@ -233,26 +233,11 @@ llarp_router::SaveRC()
llarp::LogDebug("verify RC signature");
if(!rc.VerifySignature(&crypto))
{
rc.Dump< MAX_RC_SIZE >();
llarp::LogError("RC has bad signature not saving");
return false;
}
byte_t tmp[MAX_RC_SIZE];
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(rc.BEncode(&buf))
{
std::ofstream f(our_rc_file.string());
if(f.is_open())
{
f.write((char *)buf.base, buf.cur - buf.base);
llarp::LogInfo("our RC saved to ", our_rc_file.string().c_str());
return true;
}
}
llarp::LogError("did not save RC to ", our_rc_file.string().c_str());
return false;
return rc.Write(our_rc_file.string().c_str());
}
void
@ -694,6 +679,7 @@ llarp_router::Run()
rc.pubkey = llarp::seckey_topublic(identity);
llarp::LogInfo("Your Identity pubkey ", rc.pubkey);
llarp::LogInfo("Signing rc...");
if(!rc.Sign(&crypto, identity))
{
llarp::LogError("failed to sign rc");

@ -69,6 +69,18 @@ namespace llarp
return bencode_end(buf);
}
void
RouterContact::Clear()
{
addrs.clear();
exits.clear();
signature.Zero();
nickname.Zero();
enckey.Zero();
pubkey.Zero();
last_updated = 0;
}
bool
RouterContact::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
{
@ -79,12 +91,24 @@ namespace llarp
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictEntry("n", nickname, read, key, buf))
return false;
if(llarp_buffer_eq(key, "n"))
{
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz > nickname.size())
return false;
nickname.Zero();
memcpy(nickname.data(), strbuf.base, strbuf.sz);
return true;
}
if(!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
if(!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
return false;
@ -126,9 +150,11 @@ namespace llarp
bool
RouterContact::Sign(llarp_crypto *crypto, const SecretKey &secretkey)
{
pubkey = llarp::seckey_topublic(secretkey);
byte_t tmp[MAX_RC_SIZE] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
signature.Zero();
last_updated = llarp_time_now_ms();
if(!BEncode(&buf))
return false;
buf.sz = buf.cur - buf.base;
@ -139,15 +165,19 @@ namespace llarp
bool
RouterContact::VerifySignature(llarp_crypto *crypto) const
{
RouterContact copy = *this;
RouterContact copy;
copy = *this;
copy.signature.Zero();
byte_t tmp[MAX_RC_SIZE] = {0};
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
if(!copy.BEncode(&buf))
{
llarp::LogError("bencode failed");
return false;
}
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
return crypto->verify(signature, buf, pubkey);
return crypto->verify(pubkey, buf, signature);
}
bool
@ -161,7 +191,7 @@ namespace llarp
buf.cur = buf.base;
{
std::ofstream f;
f.open(fname);
f.open(fname, std::ios::binary);
if(!f.is_open())
return false;
f.write((char *)buf.base, buf.sz);
@ -175,9 +205,12 @@ namespace llarp
byte_t tmp[MAX_RC_SIZE] = {0};
{
std::ifstream f;
f.open(fname);
f.open(fname, std::ios::binary);
if(!f.is_open())
{
llarp::LogError("Failed to open ", fname);
return false;
}
f.seekg(0, std::ios::end);
auto l = f.tellg();
f.seekg(0, std::ios::beg);
@ -197,6 +230,7 @@ namespace llarp
enckey = other.enckey;
pubkey = other.pubkey;
nickname = other.nickname;
version = other.version;
return *this;
}

Loading…
Cancel
Save