llarp_findOrCreateEncryption refactor, llarp_rc_set_pubkey(), llarp_rc_set_pubenckey(), llarp_rc_set_pubsigkey(), include more debug on sig verify failure

pull/1/head
Ryan Tharp 6 years ago
parent 810c0c83bf
commit f3f68e4bcb

@ -180,7 +180,7 @@ llarp_router::try_connect(fs::path rcfile)
} }
} }
else else
llarp::Error("failed to verify signature of RC"); llarp::Error("failed to verify signature of RC", rcfile);
} }
else else
llarp::Error("failed to decode RC"); llarp::Error("failed to decode RC");
@ -199,28 +199,8 @@ llarp_router::EnsureIdentity()
bool bool
llarp_router::EnsureEncryptionKey() llarp_router::EnsureEncryptionKey()
{ {
std::error_code ec; return llarp_findOrCreateEncryption(&crypto, encryption_keyfile.c_str(),
if(!fs::exists(encryption_keyfile, ec)) &this->encryption);
{
llarp::Info("generating encryption key");
crypto.encryption_keygen(encryption);
std::ofstream f(encryption_keyfile, std::ios::binary);
if(!f.is_open())
{
llarp::Error("could not save encryption private key to ",
encryption_keyfile, " ", ec);
return false;
}
f.write((char *)encryption.data(), encryption.size());
}
std::ifstream f(encryption_keyfile, std::ios::binary);
if(!f.is_open())
{
llarp::Error("could not read ", encryption_keyfile);
return false;
}
f.read((char *)encryption.data(), encryption.size());
return true;
} }
void void
@ -612,9 +592,20 @@ llarp_router::Run()
link->get_our_address(link, &addr); link->get_our_address(link, &addr);
llarp_ai_list_pushback(rc.addrs, &addr); llarp_ai_list_pushback(rc.addrs, &addr);
}; };
// set public keys // set public encryption key
memcpy(rc.enckey, llarp::seckey_topublic(encryption), PUBKEYSIZE); llarp_rc_set_pubenckey(&rc, llarp::seckey_topublic(encryption));
llarp_rc_set_pubkey(&rc, pubkey());
char ftmp[68] = {0};
const char *hexKey = llarp::HexEncode< llarp::PubKey,
decltype(ftmp) >(llarp::seckey_topublic(encryption),
ftmp);
llarp::Info("Your Encryption pubkey ", hexKey);
// set public signing key
llarp_rc_set_pubsigkey(&rc, llarp::seckey_topublic(identity));
hexKey = llarp::HexEncode< llarp::PubKey,
decltype(ftmp) >(llarp::seckey_topublic(identity),
ftmp);
llarp::Info("Your Identity pubkey ", hexKey);
llarp_rc_sign(&crypto, identity, &rc); llarp_rc_sign(&crypto, identity, &rc);
@ -826,39 +817,30 @@ llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem,
} }
void void
llarp_rc_set_pubkey(struct llarp_rc *rc, const uint8_t *pubkey) llarp_rc_set_pubenckey(struct llarp_rc *rc, const uint8_t *pubenckey)
{ {
// set public key // set public encryption key
memcpy(rc->pubkey, pubkey, 32); memcpy(rc->enckey, pubenckey, PUBKEYSIZE);
} }
bool void
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath, llarp_rc_set_pubsigkey(struct llarp_rc *rc, const uint8_t *pubsigkey)
byte_t *secretkey)
{ {
llarp::Debug("find or create ", fpath); // set public signing key
fs::path path(fpath); memcpy(rc->pubkey, pubsigkey, PUBKEYSIZE);
std::error_code ec; }
if(!fs::exists(path, ec))
{ void
llarp::Info("regenerated identity key"); llarp_rc_set_pubkey(struct llarp_rc *rc, const uint8_t *pubenckey,
crypto->identity_keygen(secretkey); const uint8_t *pubsigkey)
std::ofstream f(path, std::ios::binary); {
if(f.is_open()) // set public encryption key
{ llarp_rc_set_pubenckey(rc, pubenckey);
f.write((char *)secretkey, SECKEYSIZE); // set public signing key
} llarp_rc_set_pubsigkey(rc, pubsigkey);
}
std::ifstream f(path, std::ios::binary);
if(f.is_open())
{
f.read((char *)secretkey, SECKEYSIZE);
return true;
}
llarp::Info("failed to get identity key");
return false;
} }
struct llarp_rc * struct llarp_rc *
llarp_rc_read(const char *fpath) llarp_rc_read(const char *fpath)
{ {
@ -895,7 +877,7 @@ llarp_rc_read(const char *fpath)
} }
return rc; return rc;
} }
bool bool
llarp_rc_write(struct llarp_rc *rc, const char *fpath) llarp_rc_write(struct llarp_rc *rc, const char *fpath)
{ {
@ -965,6 +947,61 @@ llarp_router_override_path_selection(struct llarp_router *router,
if(func) if(func)
router->selectHopFunc = func; router->selectHopFunc = func;
} }
bool
llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath,
byte_t *secretkey)
{
llarp::Debug("find or create ", fpath);
fs::path path(fpath);
std::error_code ec;
if(!fs::exists(path, ec))
{
llarp::Info("generating new identity key");
crypto->identity_keygen(secretkey);
std::ofstream f(path, std::ios::binary);
if(f.is_open())
{
f.write((char *)secretkey, SECKEYSIZE);
}
}
std::ifstream f(path, std::ios::binary);
if(f.is_open())
{
f.read((char *)secretkey, SECKEYSIZE);
return true;
}
llarp::Info("failed to get identity key");
return false;
}
} // end extern C
// C++
bool
llarp_findOrCreateEncryption(llarp_crypto *crypto, const char *fpath,
llarp::SecretKey *encryption)
{
llarp::Debug("find or create ", fpath);
fs::path path(fpath);
std::error_code ec;
if(!fs::exists(path, ec))
{
llarp::Info("generating new encryption key");
crypto->encryption_keygen(*encryption);
std::ofstream f(path, std::ios::binary);
if(f.is_open())
{
f.write((char *)encryption, SECKEYSIZE);
}
}
std::ifstream f(path, std::ios::binary);
if(f.is_open())
{
f.read((char *)encryption, SECKEYSIZE);
return true;
}
llarp::Info("failed to get encryption key");
return false;
} }
namespace llarp namespace llarp

Loading…
Cancel
Save