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.
|
|
|
|
|
2019-06-25 15:59:42 +02:00
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty_oneline(std::ostream &s,
|
|
|
|
const m::keys &keys)
|
|
|
|
{
|
|
|
|
s << json::get<"server_name"_>(keys)
|
|
|
|
<< ' ';
|
|
|
|
|
|
|
|
char smbuf[32];
|
|
|
|
s << smalldate(smbuf, json::get<"valid_until_ts"_>(keys) / 1000L)
|
|
|
|
<< ' ';
|
|
|
|
|
2023-03-05 06:27:20 +01:00
|
|
|
for(const auto &[key_id, verify_key_] : json::get<"verify_keys"_>(keys))
|
2023-03-08 00:44:53 +01:00
|
|
|
s << " " << key_id << " ";
|
2019-06-25 15:59:42 +02:00
|
|
|
|
2020-10-16 06:36:45 +02:00
|
|
|
for(const auto &[domain, signature_] : json::get<"signatures"_>(keys))
|
2019-06-25 15:59:42 +02:00
|
|
|
{
|
2023-03-08 00:44:53 +01:00
|
|
|
s << "| " << domain << ' ';
|
2020-10-16 06:36:45 +02:00
|
|
|
for(const auto &[key_id, signature] : json::object(signature_))
|
2019-06-25 15:59:42 +02:00
|
|
|
s << key_id << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
|
|
|
ircd::m::pretty(std::ostream &s,
|
|
|
|
const m::keys &keys)
|
|
|
|
{
|
|
|
|
s << std::setw(16) << std::right << "server name "
|
|
|
|
<< json::get<"server_name"_>(keys)
|
|
|
|
<< '\n';
|
|
|
|
|
|
|
|
char tmbuf[64];
|
|
|
|
s << std::setw(16) << std::right << "valid until "
|
|
|
|
<< timef(tmbuf, json::get<"valid_until_ts"_>(keys) / 1000, ircd::localtime)
|
|
|
|
<< " (" << json::get<"valid_until_ts"_>(keys) << ")"
|
|
|
|
<< '\n';
|
|
|
|
|
2023-03-05 06:27:20 +01:00
|
|
|
for(const auto &[key_id, verify_key_] : json::get<"verify_keys"_>(keys))
|
|
|
|
for(const auto &[_, verify_key] : json::object(verify_key_))
|
2019-06-25 15:59:42 +02:00
|
|
|
s << std::setw(16) << std::right << "[verify_key] "
|
|
|
|
<< key_id << ' '
|
|
|
|
<< unquote(verify_key) << '\n';
|
|
|
|
|
2020-10-16 06:36:45 +02:00
|
|
|
for(const auto &[domain, signature_] : json::get<"signatures"_>(keys))
|
|
|
|
for(const auto &[key_id, signature] : json::object(signature_))
|
|
|
|
s << std::setw(16) << std::right << "[signature] "
|
|
|
|
<< domain << ' '
|
|
|
|
<< key_id << ' '
|
|
|
|
<< unquote(signature) << '\n';
|
|
|
|
|
2023-03-05 06:27:20 +01:00
|
|
|
for(const auto &[key_id, old_verify_key_] : json::get<"old_verify_keys"_>(keys))
|
|
|
|
for(const auto &[_, old_verify_key] : json::object(old_verify_key_))
|
2019-06-25 15:59:42 +02:00
|
|
|
s << std::setw(16) << std::right << "[old_verify_key] "
|
|
|
|
<< key_id << ' '
|
|
|
|
<< unquote(old_verify_key) << '\n';
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:19:08 +02:00
|
|
|
bool
|
|
|
|
ircd::m::verify(const m::keys &keys,
|
|
|
|
std::nothrow_t)
|
|
|
|
noexcept try
|
|
|
|
{
|
2019-06-24 07:06:50 +02:00
|
|
|
verify(keys);
|
|
|
|
return true;
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
m::log, "key verification for '%s' failed :%s",
|
|
|
|
json::get<"server_name"_>(keys, "<no server name>"_sv),
|
|
|
|
e.what()
|
|
|
|
};
|
2018-08-17 19:37:12 +02:00
|
|
|
|
2019-05-01 23:19:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::m::verify(const m::keys &keys)
|
2018-03-06 08:42:57 +01:00
|
|
|
{
|
2019-05-01 23:19:08 +02:00
|
|
|
const json::object &verify_keys
|
|
|
|
{
|
|
|
|
at<"verify_keys"_>(keys)
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &key_id
|
|
|
|
{
|
2020-02-21 04:46:56 +01:00
|
|
|
!empty(verify_keys)?
|
|
|
|
begin(verify_keys)->first:
|
|
|
|
string_view{}
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &key
|
|
|
|
{
|
2020-02-21 04:46:56 +01:00
|
|
|
!empty(verify_keys)?
|
|
|
|
begin(verify_keys)->second:
|
|
|
|
string_view{}
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const ed25519::pk pk
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
[&key](auto&& pk)
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::decode(pk, unquote(key.at("key")));
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(keys)
|
|
|
|
};
|
|
|
|
|
2019-12-21 22:56:16 +01:00
|
|
|
const json::string &server_name
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2019-12-21 22:56:16 +01:00
|
|
|
at<"server_name"_>(keys)
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const json::object &server_signatures
|
|
|
|
{
|
|
|
|
signatures.at(server_name)
|
|
|
|
};
|
|
|
|
|
2019-06-24 01:08:32 +02:00
|
|
|
const ed25519::sig sig
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
[&server_signatures, &key_id](auto&& sig)
|
2019-06-24 01:08:32 +02:00
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::decode(sig, unquote(server_signatures.at(key_id)));
|
2019-06-24 01:08:32 +02:00
|
|
|
}
|
|
|
|
};
|
2019-05-01 23:19:08 +02:00
|
|
|
|
|
|
|
m::keys copy{keys};
|
|
|
|
at<"signatures"_>(copy) = string_view{};
|
|
|
|
|
2019-06-24 01:08:32 +02:00
|
|
|
thread_local char buf[16_KiB];
|
2019-05-01 23:19:08 +02:00
|
|
|
const const_buffer preimage
|
|
|
|
{
|
|
|
|
json::stringify(mutable_buffer{buf}, copy)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!pk.verify(preimage, sig))
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "M_INVALID_SIGNATURE",
|
|
|
|
"Failed to verify signature for public key of '%s'",
|
|
|
|
server_name
|
|
|
|
};
|
2019-06-24 01:08:32 +02:00
|
|
|
|
|
|
|
if(expired(keys))
|
|
|
|
log::warning
|
|
|
|
{
|
|
|
|
m::log, "key '%s' for '%s' expired on %s.",
|
|
|
|
key_id,
|
|
|
|
json::get<"server_name"_>(keys, "<no server name>"_sv),
|
|
|
|
timestr(at<"valid_until_ts"_>(keys) / 1000L),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::expired(const m::keys &keys)
|
|
|
|
{
|
|
|
|
const auto &valid_until_ts
|
|
|
|
{
|
|
|
|
at<"valid_until_ts"_>(keys)
|
|
|
|
};
|
|
|
|
|
2019-06-25 14:16:18 +02:00
|
|
|
return valid_until_ts < ircd::time<milliseconds>();
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2019-06-22 01:41:43 +02:00
|
|
|
// query
|
2019-05-01 23:19:08 +02:00
|
|
|
//
|
|
|
|
|
2019-06-22 01:41:43 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
extern conf::item<milliseconds> keys_query_timeout;
|
2023-03-23 06:56:21 +01:00
|
|
|
extern conf::item<size_t> keys_query_buffer_size;
|
|
|
|
extern conf::item<bool> keys_query_buffer_dynamic;
|
2019-06-22 01:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::m::keys_query_timeout)
|
2019-05-01 23:19:08 +02:00
|
|
|
ircd::m::keys_query_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.query.timeout" },
|
|
|
|
{ "default", 20000L }
|
|
|
|
};
|
|
|
|
|
2023-03-23 06:56:21 +01:00
|
|
|
decltype(ircd::m::keys_query_buffer_size)
|
|
|
|
ircd::m::keys_query_buffer_size
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.query.buffer.size" },
|
|
|
|
{ "default", long(32_KiB) },
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::m::keys_query_buffer_dynamic)
|
|
|
|
ircd::m::keys_query_buffer_dynamic
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.query.buffer.dynamic" },
|
|
|
|
{ "default", false },
|
|
|
|
};
|
|
|
|
|
2020-04-22 03:29:44 +02:00
|
|
|
bool
|
2019-05-01 23:19:08 +02:00
|
|
|
ircd::m::keys::query(const string_view &query_server,
|
|
|
|
const queries &queries,
|
|
|
|
const closure_bool &closure)
|
2023-03-23 06:56:21 +01:00
|
|
|
{
|
|
|
|
const unique_mutable_buffer buf
|
|
|
|
{
|
|
|
|
keys_query_buffer_size
|
|
|
|
};
|
|
|
|
|
|
|
|
const bool dynamic
|
|
|
|
{
|
|
|
|
keys_query_buffer_dynamic
|
|
|
|
};
|
|
|
|
|
|
|
|
return query(query_server, queries, closure, buf, dynamic);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::keys::query(const string_view &query_server,
|
|
|
|
const queries &queries,
|
|
|
|
const closure_bool &closure,
|
|
|
|
const mutable_buffer &buf,
|
|
|
|
const bool dynamic)
|
2019-05-01 23:19:08 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(!query_server.empty());
|
|
|
|
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::key::opts opts;
|
2020-03-08 02:09:09 +01:00
|
|
|
opts.remote = query_server;
|
2023-03-23 06:56:21 +01:00
|
|
|
opts.dynamic = dynamic;
|
2020-03-06 04:35:20 +01:00
|
|
|
m::fed::key::query request
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
|
|
|
queries, buf, std::move(opts)
|
|
|
|
};
|
|
|
|
|
2020-11-02 20:41:56 +01:00
|
|
|
const auto &code
|
|
|
|
{
|
|
|
|
request.get(milliseconds(keys_query_timeout))
|
|
|
|
};
|
|
|
|
|
2019-06-23 01:12:14 +02:00
|
|
|
const json::array response
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
|
|
|
request
|
|
|
|
};
|
|
|
|
|
2020-04-22 03:29:44 +02:00
|
|
|
bool ret(false);
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::object key : response) try
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2019-06-24 01:22:40 +02:00
|
|
|
verify(m::keys{key});
|
|
|
|
if(!closure(key))
|
|
|
|
continue;
|
2019-05-01 23:19:08 +02:00
|
|
|
|
2019-06-24 01:22:40 +02:00
|
|
|
cache::set(key);
|
2020-04-22 03:29:44 +02:00
|
|
|
ret |= true;
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"Failed to verify keys for '%s' from '%s' :%s",
|
2019-06-24 01:22:40 +02:00
|
|
|
key.get("server_name"),
|
2019-05-01 23:19:08 +02:00
|
|
|
query_server,
|
|
|
|
e.what()
|
|
|
|
};
|
|
|
|
}
|
2020-04-22 03:29:44 +02:00
|
|
|
|
|
|
|
return ret;
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::timeout &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::REQUEST_TIMEOUT, "M_TIMEOUT",
|
|
|
|
"Failed to query keys from '%s' in time",
|
|
|
|
query_server
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:41:43 +02:00
|
|
|
//
|
|
|
|
// get
|
|
|
|
//
|
|
|
|
|
|
|
|
namespace ircd::m
|
|
|
|
{
|
2023-03-04 00:11:27 +01:00
|
|
|
static bool add_query(std::vector<fed::key::server_key> &, const fed::key::server_key &);
|
2019-06-22 01:41:43 +02:00
|
|
|
extern conf::item<milliseconds> keys_get_timeout;
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:19:08 +02:00
|
|
|
decltype(ircd::m::keys_get_timeout)
|
|
|
|
ircd::m::keys_get_timeout
|
|
|
|
{
|
|
|
|
{ "name", "ircd.keys.get.timeout" },
|
|
|
|
{ "default", 20000L }
|
2018-08-17 19:37:12 +02:00
|
|
|
};
|
|
|
|
|
2022-08-04 05:15:52 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::keys::fetch(const pdus &pdus)
|
|
|
|
{
|
|
|
|
using fed::key::server_key;
|
|
|
|
|
|
|
|
std::vector<server_key> q;
|
|
|
|
for(const auto &event : pdus)
|
|
|
|
for(const auto &[server_name, signatures] : json::get<"signatures"_>(event))
|
|
|
|
for(const auto &[key_id, signature] : json::object(signatures))
|
2023-03-04 00:11:27 +01:00
|
|
|
add_query(q, server_key
|
2022-08-04 05:15:52 +02:00
|
|
|
{
|
|
|
|
json::get<"origin"_>(event), key_id
|
2023-03-04 00:11:27 +01:00
|
|
|
});
|
2022-08-04 05:15:52 +02:00
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
const size_t fetched
|
|
|
|
{
|
|
|
|
fetch(queries(q))
|
|
|
|
};
|
2022-08-04 05:15:52 +02:00
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
return fetched;
|
|
|
|
}
|
2022-08-04 05:15:52 +02:00
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
bool
|
|
|
|
ircd::m::add_query(std::vector<fed::key::server_key> &q,
|
|
|
|
const fed::key::server_key &query)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto &[server, key_id]
|
|
|
|
{
|
|
|
|
query
|
|
|
|
};
|
2022-08-04 05:15:52 +02:00
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
// Check if we're already making a query.
|
|
|
|
if(std::binary_search(begin(q), end(q), query))
|
|
|
|
return false;
|
2022-08-04 05:15:52 +02:00
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
// Check if we already have the key.
|
|
|
|
if(keys::cache::has(server, key_id))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If there's a cached error on the host we can skip here.
|
|
|
|
if(fed::errant(server))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
q.emplace_back(query);
|
|
|
|
std::sort(begin(q), end(q));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const ctx::interrupted &e)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::derror
|
2022-08-04 05:15:52 +02:00
|
|
|
{
|
2023-03-04 00:11:27 +01:00
|
|
|
log, "Key '%s' from server '%s' :%s",
|
|
|
|
query.second,
|
|
|
|
query.first,
|
|
|
|
e.what(),
|
2022-08-04 05:15:52 +02:00
|
|
|
};
|
|
|
|
|
2023-03-04 00:11:27 +01:00
|
|
|
return false;
|
2022-08-04 05:15:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 05:27:30 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::keys::fetch(const queries &queries)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
2022-07-04 22:15:53 +02:00
|
|
|
get(queries, [&ret](const auto &) noexcept
|
2019-07-23 05:27:30 +02:00
|
|
|
{
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-04-22 03:29:44 +02:00
|
|
|
bool
|
2019-06-22 01:41:43 +02:00
|
|
|
ircd::m::keys::get(const string_view &server_name,
|
|
|
|
const closure &closure)
|
|
|
|
{
|
|
|
|
return get(server_name, string_view{}, closure);
|
|
|
|
}
|
|
|
|
|
2020-04-22 03:29:44 +02:00
|
|
|
bool
|
2019-05-01 23:19:08 +02:00
|
|
|
ircd::m::keys::get(const string_view &server_name,
|
|
|
|
const string_view &key_id,
|
|
|
|
const closure &closure)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
assert(!server_name.empty());
|
2019-06-22 01:41:43 +02:00
|
|
|
if(cache::get(server_name, key_id, closure))
|
2020-04-22 03:29:44 +02:00
|
|
|
return true;
|
2019-05-01 23:19:08 +02:00
|
|
|
|
|
|
|
if(server_name == my_host())
|
|
|
|
throw m::NOT_FOUND
|
|
|
|
{
|
2020-10-16 06:50:00 +02:00
|
|
|
"key %s for '%s' (that's myself) not found",
|
|
|
|
key_id?: "<all>"_sv,
|
2020-03-13 04:57:27 +01:00
|
|
|
server_name
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
2022-08-18 02:07:27 +02:00
|
|
|
log, "Key '%s' for %s not cached; querying network...",
|
|
|
|
key_id?: "<all>"_sv,
|
|
|
|
server_name,
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
2020-03-08 04:36:06 +01:00
|
|
|
32_KiB
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
2020-03-13 04:57:27 +01:00
|
|
|
m::fed::key::opts opts;
|
|
|
|
opts.dynamic = false;
|
|
|
|
opts.remote = server_name;
|
|
|
|
const m::fed::key::server_key query
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-03-13 04:57:27 +01:00
|
|
|
server_name, key_id
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
2020-03-13 04:57:27 +01:00
|
|
|
m::fed::key::query request
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-03-13 04:57:27 +01:00
|
|
|
{query}, buf, std::move(opts)
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
2020-11-02 20:41:56 +01:00
|
|
|
const auto status
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-11-02 20:41:56 +01:00
|
|
|
request.get(milliseconds(keys_get_timeout))
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
2020-03-13 04:57:27 +01:00
|
|
|
// note fed::key::query gives us "server_keys" array via cast operator.
|
|
|
|
const json::array response
|
2019-05-01 23:19:08 +02:00
|
|
|
{
|
2020-03-13 04:57:27 +01:00
|
|
|
request
|
2019-05-01 23:19:08 +02:00
|
|
|
};
|
|
|
|
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::object keys : response)
|
2020-03-13 04:57:27 +01:00
|
|
|
{
|
|
|
|
if(unquote(keys["server_name"]) != server_name)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
verify(m::keys(keys));
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
log, "Verified keys for '%s' from '%s'",
|
|
|
|
server_name,
|
|
|
|
server_name,
|
|
|
|
};
|
|
|
|
|
|
|
|
cache::set(keys);
|
|
|
|
closure(keys);
|
2020-04-22 03:29:44 +02:00
|
|
|
return true;
|
2020-03-13 04:57:27 +01:00
|
|
|
}
|
2020-04-22 03:29:44 +02:00
|
|
|
|
|
|
|
return false;
|
2019-05-01 23:19:08 +02:00
|
|
|
}
|
|
|
|
catch(const ctx::timeout &e)
|
|
|
|
{
|
|
|
|
throw m::error
|
|
|
|
{
|
|
|
|
http::REQUEST_TIMEOUT, "M_TIMEOUT",
|
|
|
|
"Failed to fetch keys for '%s' in time",
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-07-23 04:49:21 +02:00
|
|
|
bool
|
|
|
|
ircd::m::keys::get(const queries &queries,
|
|
|
|
const closure_bool &closure)
|
|
|
|
{
|
|
|
|
bool ret{true};
|
|
|
|
std::vector<m::feds::opts> opts;
|
|
|
|
opts.reserve(queries.size());
|
2020-05-05 22:25:45 +02:00
|
|
|
for(const auto &[server_name, key_id] : queries) try
|
2019-07-23 04:49:21 +02:00
|
|
|
{
|
2019-09-06 03:27:51 +02:00
|
|
|
assert(key_id);
|
|
|
|
assert(server_name);
|
2019-07-23 04:49:21 +02:00
|
|
|
const bool cached
|
|
|
|
{
|
|
|
|
cache::get(server_name, key_id, [&ret, &closure]
|
|
|
|
(const auto &object)
|
|
|
|
{
|
|
|
|
ret = closure(object);
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if(cached)
|
|
|
|
continue;
|
|
|
|
|
2019-07-23 23:41:53 +02:00
|
|
|
if(my_host(server_name))
|
2019-07-23 04:49:21 +02:00
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
m::log, "key '%s' for '%s' (that's myself) not found.",
|
|
|
|
key_id,
|
|
|
|
server_name,
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
log::debug
|
|
|
|
{
|
|
|
|
m::log, "Key '%s' for %s not cached; querying network...",
|
|
|
|
key_id,
|
|
|
|
server_name,
|
|
|
|
};
|
|
|
|
|
|
|
|
opts.emplace_back();
|
|
|
|
opts.back().op = feds::op::keys;
|
2020-05-05 20:53:26 +02:00
|
|
|
opts.back().exclude_myself = true;
|
|
|
|
opts.back().closure_errors = false;
|
|
|
|
opts.back().nothrow_closure = true;
|
2019-07-23 04:49:21 +02:00
|
|
|
opts.back().arg[0] = server_name;
|
|
|
|
opts.back().arg[1] = key_id;
|
|
|
|
}
|
2020-05-05 20:53:26 +02:00
|
|
|
catch(const ctx::interrupted &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Failed to start request for key '%s' of '%s' :%s",
|
|
|
|
key_id,
|
|
|
|
server_name,
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-07-23 04:49:21 +02:00
|
|
|
|
|
|
|
assert(opts.size() <= queries.size());
|
2019-08-14 10:26:02 +02:00
|
|
|
m::feds::execute(opts, [&ret, &closure]
|
2019-07-23 04:49:21 +02:00
|
|
|
(const auto &result)
|
|
|
|
{
|
2019-09-06 03:27:51 +02:00
|
|
|
const json::array &server_keys
|
|
|
|
{
|
|
|
|
result.object["server_keys"]
|
|
|
|
};
|
|
|
|
|
2021-02-25 23:33:52 +01:00
|
|
|
for(const json::object keys : server_keys)
|
2019-07-23 04:49:21 +02:00
|
|
|
{
|
2020-05-05 22:25:45 +02:00
|
|
|
const json::string &server_name
|
2019-07-23 04:49:21 +02:00
|
|
|
{
|
2020-05-05 22:25:45 +02:00
|
|
|
keys["server_name"]
|
2019-07-23 04:49:21 +02:00
|
|
|
};
|
|
|
|
|
2020-05-05 22:25:45 +02:00
|
|
|
if(server_name != result.request->arg[0] || server_name != result.origin)
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
m::log, "Origin mismatch for '%s' got '%s' from '%s'",
|
|
|
|
result.request->arg[0],
|
|
|
|
server_name,
|
|
|
|
result.origin,
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!verify(m::keys(keys), std::nothrow))
|
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
m::log, "Failed to verify key '%s' for '%s' from '%s'",
|
|
|
|
result.request->arg[0],
|
|
|
|
result.request->arg[1],
|
|
|
|
result.origin,
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache::set(keys);
|
|
|
|
if(!(ret = closure(keys)))
|
|
|
|
return ret;
|
2019-07-23 04:49:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:19:08 +02:00
|
|
|
//
|
2019-06-22 01:41:43 +02:00
|
|
|
// m::keys::cache
|
2019-05-01 23:19:08 +02:00
|
|
|
//
|
|
|
|
|
2019-06-22 01:41:43 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::keys::cache::set(const json::object &keys)
|
|
|
|
{
|
|
|
|
const json::string &server_name
|
|
|
|
{
|
|
|
|
keys.at("server_name")
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::node::room node_room
|
|
|
|
{
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!exists(node_room.room_id))
|
2019-10-01 05:50:58 +02:00
|
|
|
create(node_room, me());
|
2019-06-22 01:41:43 +02:00
|
|
|
|
2019-12-21 22:56:16 +01:00
|
|
|
const auto send_to_cache{[&node_room, &keys]
|
|
|
|
(const json::object::member &member)
|
2019-06-22 01:41:43 +02:00
|
|
|
{
|
2019-12-21 22:56:16 +01:00
|
|
|
const json::string &key_id{member.first};
|
|
|
|
send(node_room, me(), "ircd.key", key_id, keys);
|
|
|
|
}};
|
2019-06-22 01:41:43 +02:00
|
|
|
|
|
|
|
size_t ret{0};
|
2019-12-21 22:56:16 +01:00
|
|
|
static const size_t max{32};
|
|
|
|
const json::object &old_vks{keys["old_verify_keys"]};
|
|
|
|
for(auto it(begin(old_vks)); it != end(old_vks) && ret < max; ++it, ++ret)
|
2019-12-25 09:12:06 +01:00
|
|
|
send_to_cache(*it);
|
|
|
|
|
|
|
|
const json::object &vks{keys["verify_keys"]};
|
|
|
|
for(auto it(begin(vks)); it != end(vks) && ret < max; ++it, ++ret)
|
2019-12-21 22:56:16 +01:00
|
|
|
send_to_cache(*it);
|
2019-06-22 01:41:43 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::keys::cache::get(const string_view &server_name,
|
|
|
|
const string_view &key_id,
|
|
|
|
const keys::closure &closure)
|
|
|
|
{
|
|
|
|
const m::node::room node_room
|
|
|
|
{
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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).
|
2019-09-13 04:35:55 +02:00
|
|
|
const event::idx event_idx
|
2019-07-12 03:04:04 +02:00
|
|
|
{
|
|
|
|
key_id?
|
|
|
|
node_room.get(std::nothrow, "ircd.key", key_id):
|
|
|
|
node_room.get(std::nothrow, "ircd.key")
|
|
|
|
};
|
|
|
|
|
2019-09-13 04:35:55 +02:00
|
|
|
return event_idx?
|
|
|
|
m::get(std::nothrow, event_idx, "content", closure):
|
|
|
|
false;
|
2019-06-22 01:41:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-28 01:10:15 +02:00
|
|
|
bool
|
|
|
|
ircd::m::keys::cache::has(const string_view &server_name,
|
|
|
|
const string_view &key_id)
|
|
|
|
{
|
|
|
|
const m::node::room node_room
|
|
|
|
{
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.has("ircd.key", key_id):
|
|
|
|
node_room.has("ircd.key");
|
|
|
|
}
|
|
|
|
|
2019-06-24 00:32:22 +02:00
|
|
|
bool
|
|
|
|
ircd::m::keys::cache::for_each(const string_view &server_name,
|
|
|
|
const keys::closure_bool &closure)
|
|
|
|
{
|
|
|
|
const m::node::room node_room
|
|
|
|
{
|
|
|
|
server_name
|
|
|
|
};
|
|
|
|
|
|
|
|
const m::room::state state
|
|
|
|
{
|
|
|
|
node_room
|
|
|
|
};
|
|
|
|
|
|
|
|
return state.for_each("ircd.key", [&closure]
|
|
|
|
(const auto &type, const auto &key_id, const auto event_idx)
|
|
|
|
{
|
|
|
|
bool ret{true};
|
|
|
|
m::get(std::nothrow, event_idx, "content", [&closure, &ret]
|
|
|
|
(const json::object &content)
|
|
|
|
{
|
|
|
|
ret = closure(content);
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-22 01:41:43 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// (internal) ed25519 support sanity test
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef RB_DEBUG
|
|
|
|
#define TEST_ED25519 __attribute__((constructor))
|
|
|
|
#else
|
|
|
|
#define TEST_ED25519
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace ircd
|
|
|
|
{
|
|
|
|
static void _test_ed25519() noexcept TEST_ED25519;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::_test_ed25519()
|
2019-08-06 01:20:55 +02:00
|
|
|
noexcept try
|
2019-06-22 01:41:43 +02:00
|
|
|
{
|
|
|
|
char seed_buf[ed25519::SEED_SZ + 10];
|
|
|
|
const auto seed
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::decode(seed_buf, "YJDBA9Xnr2sVqXD9Vj7XVUnmFZcZrlw8Md7kMW+3XA1")
|
2019-06-22 01:41:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ed25519::pk pk;
|
|
|
|
ed25519::sk sk{&pk, seed};
|
|
|
|
|
|
|
|
const auto SERVER_NAME {"domain"};
|
|
|
|
const auto KEY_ID {"ed25519:1"};
|
|
|
|
|
|
|
|
const auto test{[&]
|
|
|
|
(const std::string &object) -> bool
|
|
|
|
{
|
|
|
|
const auto sig
|
|
|
|
{
|
|
|
|
sk.sign(const_buffer{object})
|
|
|
|
};
|
|
|
|
|
|
|
|
char sigb64_buf[128];
|
|
|
|
const auto sigb64
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::encode_unpadded(sigb64_buf, sig)
|
2019-06-22 01:41:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ed25519::sig unsig; const auto unsigb64
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::decode(unsig, sigb64)
|
2019-06-22 01:41:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return pk.verify(const_buffer{object}, unsig);
|
|
|
|
}};
|
|
|
|
|
|
|
|
const bool tests[]
|
|
|
|
{
|
|
|
|
test(std::string{json::object
|
|
|
|
{
|
|
|
|
"{}"
|
|
|
|
}}),
|
|
|
|
|
|
|
|
test(json::strung(json::members
|
|
|
|
{
|
|
|
|
{ "one", 1L },
|
|
|
|
{ "two", "Two" }
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!std::all_of(begin(tests), end(tests), [](const bool &b) { return b; }))
|
|
|
|
throw ircd::panic
|
|
|
|
{
|
|
|
|
"Seeded ed25519 test failed"
|
|
|
|
};
|
|
|
|
}
|
2019-08-06 01:20:55 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
ircd::terminate
|
|
|
|
{
|
|
|
|
std::current_exception()
|
|
|
|
};
|
|
|
|
}
|