2019-04-22 18:35:19 +00:00
|
|
|
#include <service/identity.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
|
2019-05-28 19:45:08 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <util/fs.hpp>
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
Identity::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("e", enckey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("q", pq, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("s", signkey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", vanity, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
Identity::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf))
|
|
|
|
return false;
|
2019-02-17 12:13:34 +00:00
|
|
|
if(key == "q")
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t str;
|
|
|
|
if(!bencode_read_string(buf, &str))
|
|
|
|
return false;
|
|
|
|
if(str.sz == 3200 || str.sz == 2818)
|
|
|
|
{
|
|
|
|
pq = str.base;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-05-28 19:45:08 +00:00
|
|
|
Identity::RegenerateKeys()
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
auto crypto = CryptoManager::instance();
|
2019-01-11 00:12:43 +00:00
|
|
|
crypto->encryption_keygen(enckey);
|
|
|
|
crypto->identity_keygen(signkey);
|
2019-04-22 18:35:19 +00:00
|
|
|
pub.Update(seckey_topublic(enckey), seckey_topublic(signkey));
|
2019-01-11 00:12:43 +00:00
|
|
|
crypto->pqe_keygen(pq);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Identity::KeyExchange(path_dh_func dh, SharedSecret& result,
|
|
|
|
const ServiceInfo& other,
|
|
|
|
const KeyExchangeNonce& N) const
|
|
|
|
{
|
|
|
|
return dh(result, other.EncryptionPublicKey(), enckey, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Identity::Sign(Signature& sig, const llarp_buffer_t& buf) const
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
return CryptoManager::instance()->sign(sig, signkey, buf);
|
2019-01-11 00:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Identity::EnsureKeys(const std::string& fname)
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, 4096 > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-11 00:12:43 +00:00
|
|
|
std::error_code ec;
|
|
|
|
// check for file
|
|
|
|
if(!fs::exists(fname, ec))
|
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
2019-04-22 18:35:19 +00:00
|
|
|
LogError(ec);
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// regen and encode
|
2019-05-28 19:45:08 +00:00
|
|
|
RegenerateKeys();
|
2019-01-11 00:12:43 +00:00
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// write
|
|
|
|
std::ofstream f;
|
|
|
|
f.open(fname, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.write((char*)buf.cur, buf.sz);
|
|
|
|
}
|
2019-05-18 17:34:07 +00:00
|
|
|
|
|
|
|
if(!fs::is_regular_file(fname))
|
|
|
|
{
|
|
|
|
LogError("keyfile ", fname, " is not a regular file");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-11 00:12:43 +00:00
|
|
|
// read file
|
|
|
|
std::ifstream inf(fname, std::ios::binary);
|
|
|
|
inf.seekg(0, std::ios::end);
|
|
|
|
size_t sz = inf.tellg();
|
|
|
|
inf.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
if(sz > sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
// decode
|
|
|
|
inf.read((char*)buf.base, sz);
|
2019-05-24 02:01:36 +00:00
|
|
|
if(!bencode_decode_dict(*this, &buf))
|
2019-01-11 00:12:43 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ServiceInfo::OptNonce van;
|
|
|
|
if(!vanity.IsZero())
|
|
|
|
van = vanity;
|
|
|
|
// update pubkeys
|
2019-04-22 18:35:19 +00:00
|
|
|
pub.Update(seckey_topublic(enckey), seckey_topublic(signkey), van);
|
2019-01-11 00:12:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Identity::SignIntroSet(IntroSet& i, llarp_time_t now) const
|
2019-01-11 00:12:43 +00:00
|
|
|
{
|
|
|
|
if(i.I.size() == 0)
|
|
|
|
return false;
|
|
|
|
// set timestamp
|
|
|
|
// TODO: round to nearest 1000 ms
|
|
|
|
i.T = now;
|
|
|
|
// set service info
|
|
|
|
i.A = pub;
|
|
|
|
// set public encryption key
|
|
|
|
i.K = pq_keypair_to_public(pq);
|
|
|
|
// zero out signature for signing process
|
|
|
|
i.Z.Zero();
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, MAX_INTROSET_SIZE > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2019-01-11 00:12:43 +00:00
|
|
|
if(!i.BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind and resize buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2019-05-28 19:45:08 +00:00
|
|
|
return Sign(i.Z, buf);
|
2019-01-11 00:12:43 +00:00
|
|
|
}
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|