From f3f68e4bcb7595b02d4b355266662f5c8490b2b1 Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Thu, 21 Jun 2018 05:52:45 -0700 Subject: [PATCH] llarp_findOrCreateEncryption refactor, llarp_rc_set_pubkey(), llarp_rc_set_pubenckey(), llarp_rc_set_pubsigkey(), include more debug on sig verify failure --- llarp/router.cpp | 145 +++++++++++++++++++++++++++++------------------ 1 file changed, 91 insertions(+), 54 deletions(-) diff --git a/llarp/router.cpp b/llarp/router.cpp index ead62098a..f547ea9ce 100644 --- a/llarp/router.cpp +++ b/llarp/router.cpp @@ -180,7 +180,7 @@ llarp_router::try_connect(fs::path rcfile) } } else - llarp::Error("failed to verify signature of RC"); + llarp::Error("failed to verify signature of RC", rcfile); } else llarp::Error("failed to decode RC"); @@ -199,28 +199,8 @@ llarp_router::EnsureIdentity() bool llarp_router::EnsureEncryptionKey() { - std::error_code ec; - if(!fs::exists(encryption_keyfile, ec)) - { - 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; + return llarp_findOrCreateEncryption(&crypto, encryption_keyfile.c_str(), + &this->encryption); } void @@ -612,9 +592,20 @@ llarp_router::Run() link->get_our_address(link, &addr); llarp_ai_list_pushback(rc.addrs, &addr); }; - // set public keys - memcpy(rc.enckey, llarp::seckey_topublic(encryption), PUBKEYSIZE); - llarp_rc_set_pubkey(&rc, pubkey()); + // set public encryption key + llarp_rc_set_pubenckey(&rc, llarp::seckey_topublic(encryption)); + + 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); @@ -826,39 +817,30 @@ llarp_rc_set_addrs(struct llarp_rc *rc, struct llarp_alloc *mem, } 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 - memcpy(rc->pubkey, pubkey, 32); + // set public encryption key + memcpy(rc->enckey, pubenckey, PUBKEYSIZE); } -bool -llarp_findOrCreateIdentity(llarp_crypto *crypto, const char *fpath, - byte_t *secretkey) +void +llarp_rc_set_pubsigkey(struct llarp_rc *rc, const uint8_t *pubsigkey) { - llarp::Debug("find or create ", fpath); - fs::path path(fpath); - std::error_code ec; - if(!fs::exists(path, ec)) - { - llarp::Info("regenerated 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; + // set public signing key + memcpy(rc->pubkey, pubsigkey, PUBKEYSIZE); +} + +void +llarp_rc_set_pubkey(struct llarp_rc *rc, const uint8_t *pubenckey, + const uint8_t *pubsigkey) +{ + // set public encryption key + llarp_rc_set_pubenckey(rc, pubenckey); + // set public signing key + llarp_rc_set_pubsigkey(rc, pubsigkey); } + struct llarp_rc * llarp_rc_read(const char *fpath) { @@ -895,7 +877,7 @@ llarp_rc_read(const char *fpath) } return rc; } - + bool 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) 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