2018-03-06 08:42:57 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
static bool cache_get(const string_view &server, const string_view &key_id, const m::keys::closure &);
|
|
|
|
static size_t cache_set(const json::object &);
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
extern "C" bool verify__keys(const m::keys &) noexcept;
|
2018-05-11 11:05:08 +02:00
|
|
|
extern "C" void get__keys(const string_view &server, const string_view &key_id, const m::keys::closure &);
|
|
|
|
extern "C" bool query__keys(const string_view &query_server, const m::keys::queries &, const m::keys::closure_bool &);
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-08-29 00:05:54 +02:00
|
|
|
extern "C" void create_my_key(const m::event &);
|
2018-08-17 19:37:12 +02:00
|
|
|
static void init_my_ed25519();
|
|
|
|
static void init_my_tls_crt();
|
|
|
|
extern "C" void init_my_keys();
|
|
|
|
|
2018-03-06 08:42:57 +01:00
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
"Server keys"
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
init_my_keys()
|
|
|
|
{
|
|
|
|
init_my_ed25519();
|
|
|
|
init_my_tls_crt();
|
|
|
|
}
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
tls_key_dir
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.tls_key_dir" },
|
|
|
|
{ "default", fs::cwd() }
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
void
|
|
|
|
init_my_tls_crt()
|
|
|
|
{
|
|
|
|
if(!m::self::origin)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"The m::self::origin must be set to init my ed25519 key."
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string private_key_path_parts[]
|
|
|
|
{
|
|
|
|
tls_key_dir,
|
|
|
|
m::self::origin + ".crt.key",
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string public_key_path_parts[]
|
|
|
|
{
|
|
|
|
tls_key_dir,
|
|
|
|
m::self::origin + ".crt.key.pub",
|
|
|
|
};
|
|
|
|
|
2018-08-29 00:06:07 +02:00
|
|
|
const std::string dhparam_path_parts[]
|
|
|
|
{
|
|
|
|
tls_key_dir,
|
|
|
|
m::self::origin + ".crt.dh",
|
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const std::string certificate_path_parts[]
|
|
|
|
{
|
|
|
|
tls_key_dir,
|
|
|
|
m::self::origin + ".crt",
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string private_key_file
|
|
|
|
{
|
|
|
|
fs::make_path(private_key_path_parts)
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string public_key_file
|
|
|
|
{
|
|
|
|
fs::make_path(public_key_path_parts)
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string cert_file
|
|
|
|
{
|
|
|
|
fs::make_path(certificate_path_parts)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!fs::exists(private_key_file))
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Failed to find certificate private key @ `%s'; creating...",
|
|
|
|
private_key_file
|
|
|
|
};
|
|
|
|
|
|
|
|
openssl::genrsa(private_key_file, public_key_file);
|
|
|
|
}
|
|
|
|
|
2018-08-30 01:17:25 +02:00
|
|
|
/*
|
|
|
|
const std::string dhparam_file
|
|
|
|
{
|
|
|
|
fs::make_path(dhparam_path_parts)
|
|
|
|
};
|
|
|
|
|
2018-08-29 00:06:07 +02:00
|
|
|
if(!fs::exists(dhparam_file))
|
|
|
|
{
|
|
|
|
log::warning
|
|
|
|
{
|
2018-08-29 00:10:16 +02:00
|
|
|
"Failed to find dhparam file @ `%s'; creating; "
|
|
|
|
"this will take about 2 to 5 minutes...",
|
2018-08-29 00:06:07 +02:00
|
|
|
dhparam_file
|
|
|
|
};
|
|
|
|
|
|
|
|
openssl::gendh(dhparam_file);
|
|
|
|
}
|
2018-08-30 01:17:25 +02:00
|
|
|
*/
|
2018-08-29 00:06:07 +02:00
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const json::object config{};
|
|
|
|
if(!fs::exists(cert_file))
|
|
|
|
{
|
|
|
|
std::string subject
|
|
|
|
{
|
|
|
|
config.get({"certificate", m::self::origin, "subject"})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!subject)
|
|
|
|
subject = json::strung{json::members
|
|
|
|
{
|
|
|
|
{ "CN", m::self::origin }
|
|
|
|
}};
|
|
|
|
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
"Failed to find SSL certificate @ `%s'; creating for '%s'...",
|
|
|
|
cert_file,
|
|
|
|
m::self::origin
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
1_MiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::strung opts{json::members
|
|
|
|
{
|
|
|
|
{ "private_key_pem_path", private_key_file },
|
|
|
|
{ "public_key_pem_path", public_key_file },
|
|
|
|
{ "subject", subject },
|
|
|
|
}};
|
|
|
|
|
|
|
|
const auto cert
|
|
|
|
{
|
|
|
|
openssl::genX509_rsa(buf, opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
fs::overwrite(cert_file, cert);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto cert_pem
|
|
|
|
{
|
|
|
|
fs::read(cert_file)
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> der_buf
|
|
|
|
{
|
|
|
|
8_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto cert_der
|
|
|
|
{
|
|
|
|
openssl::cert2d(der_buf, cert_pem)
|
|
|
|
};
|
|
|
|
|
|
|
|
const fixed_buffer<const_buffer, crh::sha256::digest_size> hash
|
|
|
|
{
|
|
|
|
sha256{cert_der}
|
|
|
|
};
|
|
|
|
|
|
|
|
m::self::tls_cert_der_sha256_b64 =
|
|
|
|
{
|
|
|
|
b64encode_unpadded(hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Certificate `%s' :PEM %zu bytes; DER %zu bytes; sha256b64 %s",
|
|
|
|
cert_file,
|
|
|
|
cert_pem.size(),
|
|
|
|
ircd::size(cert_der),
|
|
|
|
m::self::tls_cert_der_sha256_b64
|
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> print_buf
|
|
|
|
{
|
|
|
|
8_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Certificate `%s' :%s",
|
|
|
|
cert_file,
|
|
|
|
openssl::print_subject(print_buf, cert_pem)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
ed25519_key_dir
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.ed25519_key_dir" },
|
|
|
|
{ "default", fs::cwd() }
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
init_my_ed25519()
|
|
|
|
{
|
|
|
|
if(!m::self::origin)
|
|
|
|
throw error
|
|
|
|
{
|
|
|
|
"The m::self::origin must be set to init my ed25519 key."
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string path_parts[]
|
|
|
|
{
|
|
|
|
ed25519_key_dir,
|
|
|
|
m::self::origin + ".ed25519",
|
|
|
|
};
|
|
|
|
|
|
|
|
const std::string sk_file
|
|
|
|
{
|
|
|
|
fs::make_path(path_parts)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(fs::exists(sk_file))
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Using ed25519 secret key @ `%s'", sk_file
|
|
|
|
};
|
|
|
|
else
|
|
|
|
log::notice
|
|
|
|
{
|
|
|
|
m::log, "Creating ed25519 secret key @ `%s'", sk_file
|
|
|
|
};
|
|
|
|
|
|
|
|
m::self::secret_key = ed25519::sk
|
|
|
|
{
|
|
|
|
sk_file, &m::self::public_key
|
|
|
|
};
|
|
|
|
|
|
|
|
m::self::public_key_b64 = b64encode_unpadded(m::self::public_key);
|
|
|
|
const fixed_buffer<const_buffer, sha256::digest_size> hash
|
|
|
|
{
|
|
|
|
sha256{m::self::public_key}
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto public_key_hash_b58
|
|
|
|
{
|
|
|
|
b58encode(hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
static const auto trunc_size{8};
|
|
|
|
m::self::public_key_id = fmt::snstringf
|
|
|
|
{
|
|
|
|
32, "ed25519:%s", trunc(public_key_hash_b58, trunc_size)
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
m::log, "Current key is '%s' and the public key is: %s",
|
|
|
|
m::self::public_key_id,
|
|
|
|
m::self::public_key_b64
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const m::hookfn<>
|
|
|
|
create_my_key_hook
|
|
|
|
{
|
|
|
|
create_my_key,
|
|
|
|
{
|
|
|
|
{ "_site", "vm.notify" },
|
|
|
|
{ "room_id", m::my_node.room_id() },
|
|
|
|
{ "type", "m.room.create" },
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
create_my_key(const m::event &)
|
|
|
|
{
|
|
|
|
const json::members verify_keys_
|
|
|
|
{{
|
|
|
|
string_view{m::self::public_key_id},
|
|
|
|
{
|
|
|
|
{ "key", m::self::public_key_b64 }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
const json::members tlsfps
|
|
|
|
{
|
|
|
|
{ "sha256", m::self::tls_cert_der_sha256_b64 }
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::value tlsfp[1]
|
|
|
|
{
|
|
|
|
{ tlsfps }
|
|
|
|
};
|
|
|
|
|
|
|
|
m::keys my_key;
|
|
|
|
json::get<"server_name"_>(my_key) = my_host();
|
|
|
|
json::get<"old_verify_keys"_>(my_key) = "{}";
|
|
|
|
json::get<"valid_until_ts"_>(my_key) = ircd::time<milliseconds>() + duration_cast<milliseconds>(hours(2160)).count();
|
|
|
|
|
|
|
|
const json::strung verify_keys{verify_keys_}; // must be on stack until my_keys serialized.
|
|
|
|
json::get<"verify_keys"_>(my_key) = verify_keys;
|
|
|
|
|
|
|
|
const json::strung tls_fingerprints{json::value{tlsfp, 1}}; // must be on stack until my_keys serialized.
|
|
|
|
json::get<"tls_fingerprints"_>(my_key) = tls_fingerprints;
|
|
|
|
|
|
|
|
const json::strung presig
|
|
|
|
{
|
|
|
|
my_key
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
m::self::secret_key.sign(const_buffer{presig})
|
|
|
|
};
|
|
|
|
|
|
|
|
char signature[256];
|
|
|
|
const json::strung signatures{json::members
|
|
|
|
{
|
|
|
|
{ my_host(),
|
|
|
|
{
|
|
|
|
{ string_view{m::self::public_key_id}, b64encode_unpadded(signature, sig) }
|
|
|
|
}}
|
|
|
|
}};
|
|
|
|
|
|
|
|
json::get<"signatures"_>(my_key) = signatures;
|
|
|
|
cache_set(json::strung{my_key});
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// query
|
|
|
|
//
|
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
conf::item<milliseconds>
|
|
|
|
query_keys_timeout
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
{ "name", "ircd.keys.query.timeout" },
|
|
|
|
{ "default", 20000L }
|
2018-05-11 11:05:08 +02:00
|
|
|
};
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
extern "C" bool
|
|
|
|
query__keys(const string_view &query_server,
|
|
|
|
const m::keys::queries &queries,
|
|
|
|
const m::keys::closure_bool &closure)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(!query_server.empty());
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
m::v1::key::opts opts;
|
|
|
|
opts.remote = net::hostport{query_server};
|
|
|
|
opts.dynamic = true;
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
16_KiB
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
m::v1::key::query request
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
queries, buf, std::move(opts)
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
const milliseconds timeout(query_keys_timeout);
|
|
|
|
request.wait(timeout);
|
|
|
|
const auto &code
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
request.get()
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
const json::array &response
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
request
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
for(const json::object &k : response)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
const m::keys &key{k};
|
2018-05-11 11:05:08 +02:00
|
|
|
if(!verify__keys(key))
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
log::derror
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
|
|
|
"Failed to verify keys for '%s' from '%s'",
|
2018-05-08 01:04:11 +02:00
|
|
|
at<"server_name"_>(key),
|
2018-03-06 08:42:57 +01:00
|
|
|
query_server
|
|
|
|
};
|
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
continue;
|
2018-03-06 08:42:57 +01:00
|
|
|
}
|
|
|
|
|
2018-05-08 01:04:11 +02:00
|
|
|
log::debug
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
m::log, "Verified keys for '%s' from '%s'",
|
|
|
|
at<"server_name"_>(key),
|
|
|
|
query_server
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
2018-05-08 01:04:11 +02:00
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
if(!closure(k))
|
2018-05-08 01:04:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-03-06 08:42:57 +01:00
|
|
|
}
|
2018-05-08 01:04:11 +02:00
|
|
|
catch(const ctx::timeout &e)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
throw m::error
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
http::REQUEST_TIMEOUT, "M_TIMEOUT",
|
|
|
|
"Failed to query keys from '%s' in time",
|
|
|
|
query_server
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-11 11:05:08 +02:00
|
|
|
conf::item<milliseconds>
|
|
|
|
get_keys_timeout
|
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
{ "name", "ircd.keys.get.timeout" },
|
|
|
|
{ "default", 20000L }
|
2018-05-11 11:05:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
get__keys(const string_view &server_name,
|
|
|
|
const string_view &key_id,
|
|
|
|
const m::keys::closure &closure)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(!server_name.empty());
|
|
|
|
|
|
|
|
if(cache_get(server_name, key_id, closure))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(server_name == my_host())
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
|
|
|
"keys for '%s' (that's myself) not found", server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "Keys for %s not cached; querying network...", server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::key::opts opts;
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
16_KiB
|
|
|
|
};
|
|
|
|
|
|
|
|
m::v1::key::keys request
|
|
|
|
{
|
|
|
|
server_name, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
|
|
|
const milliseconds timeout(get_keys_timeout);
|
|
|
|
request.wait(timeout);
|
|
|
|
const auto &status
|
|
|
|
{
|
|
|
|
request.get()
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &response
|
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &keys
|
|
|
|
{
|
|
|
|
response
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!verify__keys(keys)) throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_INVALID_SIGNATURE",
|
|
|
|
"Failed to verify keys for '%s'",
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "Verified keys from '%s'", server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
cache_set(keys);
|
|
|
|
closure(keys);
|
|
|
|
}
|
|
|
|
catch(const ctx::timeout &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::REQUEST_TIMEOUT, "M_TIMEOUT",
|
|
|
|
"Failed to fetch keys for '%s' in time",
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-06 08:42:57 +01:00
|
|
|
bool
|
2018-05-11 11:05:08 +02:00
|
|
|
verify__keys(const m::keys &keys)
|
2018-03-06 08:42:57 +01:00
|
|
|
noexcept try
|
|
|
|
{
|
|
|
|
const auto &valid_until_ts
|
|
|
|
{
|
|
|
|
at<"valid_until_ts"_>(keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(valid_until_ts < ircd::time<milliseconds>())
|
2018-05-08 01:04:11 +02:00
|
|
|
throw ircd::error
|
|
|
|
{
|
|
|
|
"Key was valid until %s", timestr(valid_until_ts)
|
|
|
|
};
|
2018-03-06 08:42:57 +01:00
|
|
|
|
|
|
|
const json::object &verify_keys
|
|
|
|
{
|
|
|
|
at<"verify_keys"_>(keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &key_id
|
|
|
|
{
|
|
|
|
begin(verify_keys)->first
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &key
|
|
|
|
{
|
|
|
|
begin(verify_keys)->second
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::pk pk
|
|
|
|
{
|
|
|
|
[&key](auto &pk)
|
|
|
|
{
|
|
|
|
b64decode(pk, unquote(key.at("key")));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &server_name
|
|
|
|
{
|
|
|
|
unquote(at<"server_name"_>(keys))
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &server_signatures
|
|
|
|
{
|
|
|
|
signatures.at(server_name)
|
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::sig sig{[&server_signatures, &key_id](auto &sig)
|
|
|
|
{
|
|
|
|
b64decode(sig, unquote(server_signatures.at(key_id)));
|
|
|
|
}};
|
|
|
|
|
|
|
|
m::keys copy{keys};
|
|
|
|
at<"signatures"_>(copy) = string_view{};
|
2018-05-08 01:04:11 +02:00
|
|
|
|
|
|
|
thread_local char buf[4096];
|
|
|
|
const const_buffer preimage
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, copy)
|
|
|
|
};
|
|
|
|
|
|
|
|
return pk.verify(preimage, sig);
|
2018-03-06 08:42:57 +01:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2018-05-08 01:04:11 +02:00
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "key verification for '%s' failed: %s",
|
|
|
|
json::get<"server_name"_>(keys, "<no server name>"_sv),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-03-06 08:42:57 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
size_t
|
|
|
|
cache_set(const json::object &keys)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
const auto &server_name
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
unquote(keys.at("server_name"))
|
|
|
|
};
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const m::node::id::buf node_id
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
"", server_name
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const m::node::room node_room
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
node_id
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(node_room.room_id))
|
|
|
|
create(node_room, m::me.user_id);
|
|
|
|
|
|
|
|
const json::object &vks
|
|
|
|
{
|
|
|
|
keys.at("verify_keys")
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
size_t ret{0};
|
|
|
|
for(const auto &member : vks)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
if(ret > 16)
|
|
|
|
return ret;
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const auto &key_id(unquote(member.first));
|
|
|
|
send(node_room, m::me.user_id, "ircd.key", key_id, keys);
|
|
|
|
++ret;
|
|
|
|
}
|
2018-03-06 08:42:57 +01:00
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
cache_get(const string_view &server_name,
|
|
|
|
const string_view &key_id,
|
|
|
|
const m::keys::closure &closure)
|
|
|
|
{
|
|
|
|
const m::node::id::buf node_id
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
"", server_name
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const m::node::room node_room
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
node_id
|
2018-03-06 08:42:57 +01:00
|
|
|
};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
const auto reclosure{[&closure]
|
|
|
|
(const m::event &event)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2018-08-17 19:37:12 +02:00
|
|
|
closure(json::get<"content"_>(event));
|
2018-03-06 08:42:57 +01:00
|
|
|
}};
|
|
|
|
|
2018-08-17 19:37:12 +02:00
|
|
|
// Without a key_id we search for the most recent key; note this is not
|
|
|
|
// the same as making a state_key="" query, as that would be an actual
|
|
|
|
// ircd.key entry without an id (which shouldn't exist).
|
|
|
|
return !key_id?
|
|
|
|
node_room.get(std::nothrow, "ircd.key", reclosure):
|
|
|
|
node_room.get(std::nothrow, "ircd.key", key_id, reclosure);
|
2018-03-06 08:42:57 +01:00
|
|
|
}
|