2018-02-04 03:22:01 +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.
|
2017-12-12 21:33:14 +01:00
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
namespace ircd::m
|
|
|
|
{
|
|
|
|
static json::object make_hashes(const mutable_buffer &out, const sha256::buf &hash);
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
/// The maximum size of an event we will create. This may also be used in
|
|
|
|
/// some contexts for what we will accept, but the protocol limit and hard
|
|
|
|
/// worst-case buffer size is still event::MAX_SIZE.
|
|
|
|
decltype(ircd::m::event::max_size)
|
|
|
|
ircd::m::event::max_size
|
2018-05-12 04:21:56 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
{ "name", "m.event.max_size" },
|
|
|
|
{ "default", 65507L },
|
|
|
|
};
|
2018-05-12 04:21:56 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::json::object
|
|
|
|
ircd::m::hashes(const mutable_buffer &out,
|
|
|
|
const event &event)
|
2018-05-12 04:21:56 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const sha256::buf hash_
|
2019-01-15 23:46:35 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
hash(event)
|
2019-01-15 23:46:35 +01:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return make_hashes(out, hash_);
|
|
|
|
}
|
2018-05-12 04:21:56 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::json::object
|
|
|
|
ircd::m::event::hashes(const mutable_buffer &out,
|
|
|
|
json::iov &event,
|
|
|
|
const string_view &content)
|
|
|
|
{
|
|
|
|
const sha256::buf hash_
|
2018-05-12 04:21:56 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
hash(event, content)
|
2018-05-12 04:21:56 +02:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return make_hashes(out, hash_);
|
2018-05-12 04:21:56 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::json::object
|
|
|
|
ircd::m::make_hashes(const mutable_buffer &out,
|
|
|
|
const sha256::buf &hash)
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
static const auto b64bufsz(b64encode_size(sizeof(hash)));
|
|
|
|
thread_local char hashb64buf[b64bufsz];
|
|
|
|
const json::members hashes
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
{ "sha256", b64encode_unpadded(hashb64buf, hash) }
|
2019-01-16 21:12:59 +01:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return json::stringify(mutable_buffer{out}, hashes);
|
2019-01-16 21:12:59 +01:00
|
|
|
}
|
|
|
|
|
2019-02-07 05:52:42 +01:00
|
|
|
ircd::sha256::buf
|
|
|
|
ircd::m::event::hash(const json::object &event)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
static const size_t iov_max{json::iov::max_size};
|
|
|
|
thread_local std::array<json::object::member, iov_max> member;
|
|
|
|
|
|
|
|
size_t i(0);
|
|
|
|
for(const auto &m : event)
|
|
|
|
{
|
|
|
|
if(m.first == "signatures" ||
|
2019-05-12 01:09:06 +02:00
|
|
|
m.first == "hashes" ||
|
2019-02-07 05:52:42 +01:00
|
|
|
m.first == "unsigned" ||
|
2019-05-12 01:09:06 +02:00
|
|
|
m.first == "age_ts" ||
|
|
|
|
m.first == "outlier" ||
|
|
|
|
m.first == "destinations")
|
2019-02-07 05:52:42 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
member.at(i++) = m;
|
|
|
|
}
|
|
|
|
|
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const string_view reimage
|
|
|
|
{
|
|
|
|
json::stringify(buf, member.data(), member.data() + i)
|
|
|
|
};
|
|
|
|
|
|
|
|
return sha256{reimage};
|
|
|
|
}
|
|
|
|
catch(const std::out_of_range &e)
|
|
|
|
{
|
|
|
|
throw m::BAD_JSON
|
|
|
|
{
|
|
|
|
"Object has more than %zu member properties.",
|
|
|
|
json::iov::max_size
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::sha256::buf
|
|
|
|
ircd::m::event::hash(json::iov &event,
|
|
|
|
const string_view &content)
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::iov::push _content
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
event, { "content", content }
|
2019-01-16 21:12:59 +01:00
|
|
|
};
|
|
|
|
|
2019-07-06 04:45:02 +02:00
|
|
|
return m::hash(m::event{event});
|
2019-01-16 21:12:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::sha256::buf
|
|
|
|
ircd::m::hash(const event &event)
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-02-07 05:52:42 +01:00
|
|
|
if(event.source)
|
|
|
|
return event::hash(event.source);
|
2019-01-26 23:15:36 +01:00
|
|
|
|
2019-02-07 05:52:42 +01:00
|
|
|
m::event event_{event};
|
|
|
|
json::get<"signatures"_>(event_) = {};
|
|
|
|
json::get<"hashes"_>(event_) = {};
|
2019-01-16 21:12:59 +01:00
|
|
|
|
2019-02-07 05:52:42 +01:00
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const string_view preimage
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-02-07 05:52:42 +01:00
|
|
|
stringify(buf, event_)
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
|
|
|
|
2019-02-07 05:52:42 +01:00
|
|
|
return sha256{preimage};
|
2019-01-16 21:12:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::verify_hash(const event &event)
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const sha256::buf hash
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
m::hash(event)
|
2019-01-16 21:12:59 +01:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return verify_hash(event, hash);
|
2019-01-16 21:12:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::verify_hash(const event &event,
|
|
|
|
const sha256::buf &hash)
|
2018-02-09 20:22:46 +01:00
|
|
|
{
|
2019-06-23 08:28:48 +02:00
|
|
|
static constexpr size_t hashb64sz
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-06-24 07:09:41 +02:00
|
|
|
size_t(sha256::digest_size * 1.34) + 1
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
thread_local char b64buf[hashb64sz];
|
|
|
|
return verify_sha256b64(event, b64encode_unpadded(b64buf, hash));
|
2018-02-09 20:22:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::verify_sha256b64(const event &event,
|
|
|
|
const string_view &b64)
|
|
|
|
try
|
2018-02-09 20:22:46 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &object
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
at<"hashes"_>(event)
|
2018-04-18 04:14:39 +02:00
|
|
|
};
|
|
|
|
|
2019-09-12 22:45:36 +02:00
|
|
|
const json::string &hash
|
2019-01-17 23:08:26 +01:00
|
|
|
{
|
2019-09-12 22:45:36 +02:00
|
|
|
object.at("sha256")
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-17 23:08:26 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return hash == b64;
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
catch(const json::not_found &)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
return false;
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::json::object
|
|
|
|
ircd::m::event::signatures(const mutable_buffer &out,
|
|
|
|
json::iov &event,
|
|
|
|
const json::iov &content)
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
event.at("origin")
|
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const ed25519::sig sig
|
2019-01-15 23:46:35 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
sign(event, content)
|
2019-01-15 23:46:35 +01:00
|
|
|
};
|
|
|
|
|
2019-10-01 05:50:58 +02:00
|
|
|
const string_view public_key_id
|
|
|
|
{
|
2019-12-10 20:05:33 +01:00
|
|
|
m::public_key_id(m::my(origin))
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
thread_local char sigb64buf[b64encode_size(sizeof(sig))];
|
|
|
|
const json::members sigb64
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
{ public_key_id, b64encode_unpadded(sigb64buf, sig) }
|
2018-04-18 04:14:39 +02:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::members sigs
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
{ origin, sigb64 }
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-15 23:46:35 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return json::stringify(mutable_buffer{out}, sigs);
|
2018-02-09 20:22:46 +01:00
|
|
|
}
|
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
ircd::m::event
|
|
|
|
ircd::m::signatures(const mutable_buffer &out,
|
|
|
|
const m::event &event)
|
|
|
|
{
|
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
at<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return signatures(out, event, origin);
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::event
|
|
|
|
ircd::m::signatures(const mutable_buffer &out_,
|
2020-02-24 07:44:14 +01:00
|
|
|
const m::event &event_,
|
|
|
|
const string_view &origin)
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
|
|
|
thread_local char content[event::MAX_SIZE];
|
|
|
|
m::event event
|
|
|
|
{
|
|
|
|
essential(event_, content)
|
|
|
|
};
|
2019-01-17 02:53:47 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
thread_local char buf[event::MAX_SIZE];
|
2020-02-24 07:44:14 +01:00
|
|
|
const string_view &preimage
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
2019-01-17 02:53:47 +01:00
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
const auto &secret_key
|
|
|
|
{
|
|
|
|
m::secret_key(my(origin))
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view public_key_id
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2020-02-24 07:44:14 +01:00
|
|
|
m::public_key_id(my(origin))
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
const ed25519::sig my_sig
|
2019-10-01 05:50:58 +02:00
|
|
|
{
|
2020-02-24 07:44:14 +01:00
|
|
|
event::sign(preimage, secret_key)
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
static const auto sigb64bufsz
|
2019-02-05 09:41:15 +01:00
|
|
|
{
|
2020-02-24 07:44:14 +01:00
|
|
|
b64encode_size(sizeof(my_sig))
|
2019-02-05 09:41:15 +01:00
|
|
|
};
|
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
thread_local char sigb64buf[sigb64bufsz];
|
|
|
|
const json::member my_sig_member
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
origin, json::members
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2020-02-24 07:44:14 +01:00
|
|
|
{ public_key_id, b64encode_unpadded(sigb64buf, my_sig) }
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
|
|
|
};
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
static const size_t SIG_MAX{64};
|
|
|
|
thread_local std::array<json::member, SIG_MAX> sigs;
|
2018-04-18 04:14:39 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
size_t i(0);
|
2020-02-24 07:44:14 +01:00
|
|
|
sigs.at(i++) = my_sig_member;
|
2019-09-12 22:45:36 +02:00
|
|
|
for(const auto &[host, sig] : json::get<"signatures"_>(event_))
|
2020-02-24 07:44:14 +01:00
|
|
|
if(json::string(host) != origin)
|
2019-09-12 22:45:36 +02:00
|
|
|
sigs.at(i++) = { host, sig };
|
2019-01-17 02:53:47 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
event = event_;
|
|
|
|
mutable_buffer out{out_};
|
|
|
|
json::get<"signatures"_>(event) = json::stringify(out, sigs.data(), sigs.data() + i);
|
|
|
|
return event;
|
2018-04-18 04:14:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(json::iov &event,
|
|
|
|
const json::iov &contents)
|
2018-05-20 06:20:44 +02:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
event.at("origin")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &secret_key
|
|
|
|
{
|
2019-12-10 20:05:33 +01:00
|
|
|
m::secret_key(m::my(origin))
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return sign(event, contents, secret_key);
|
2018-05-20 06:20:44 +02:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(json::iov &event,
|
|
|
|
const json::iov &contents,
|
|
|
|
const ed25519::sk &sk)
|
2018-05-20 06:20:44 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
ed25519::sig sig;
|
|
|
|
essential(event, contents, [&sk, &sig]
|
|
|
|
(json::iov &event)
|
|
|
|
{
|
2019-07-06 04:45:02 +02:00
|
|
|
sig = m::sign(m::event{event}, sk);
|
2019-01-26 23:15:36 +01:00
|
|
|
});
|
2018-05-20 06:20:44 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return sig;
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::sign(const event &event)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const string_view &origin
|
|
|
|
{
|
2020-02-24 07:44:14 +01:00
|
|
|
at<"origin"_>(event)
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
2020-02-24 07:44:14 +01:00
|
|
|
return sign(event, origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::sign(const event &event,
|
|
|
|
const string_view &origin)
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const auto &secret_key
|
|
|
|
{
|
|
|
|
m::secret_key(my(origin))
|
|
|
|
};
|
|
|
|
|
|
|
|
return sign(event, secret_key);
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::sign(const event &event,
|
|
|
|
const ed25519::sk &sk)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const string_view preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return event::sign(preimage, sk);
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const json::object &event)
|
2019-01-26 22:49:34 +01:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const json::string &origin
|
|
|
|
{
|
|
|
|
event.at("origin")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &secret_key
|
|
|
|
{
|
2019-12-10 20:05:33 +01:00
|
|
|
m::secret_key(m::my(origin))
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return sign(event, secret_key);
|
2019-01-26 22:49:34 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const json::object &event,
|
|
|
|
const ed25519::sk &sk)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
//TODO: skip rewrite
|
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const string_view preimage
|
|
|
|
{
|
|
|
|
stringify(buf, event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return sign(preimage, sk);
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const string_view &event)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
const json::string &origin
|
|
|
|
{
|
|
|
|
json::object(event).at("origin")
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto &secret_key
|
|
|
|
{
|
2019-12-10 20:05:33 +01:00
|
|
|
m::secret_key(m::my(origin))
|
2019-10-01 05:50:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return sign(event, secret_key);
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
|
|
|
|
ircd::ed25519::sig
|
|
|
|
ircd::m::event::sign(const string_view &event,
|
|
|
|
const ed25519::sk &sk)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
sk.sign(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return sig;
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::verify(const event &event)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const string_view &origin
|
|
|
|
{
|
|
|
|
at<"origin"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
return verify(event, origin);
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const string_view &origin)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &signatures
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
at<"signatures"_>(event)
|
2019-01-18 20:10:21 +01:00
|
|
|
};
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &origin_sigs
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
signatures.at(origin)
|
2019-01-18 20:10:21 +01:00
|
|
|
};
|
|
|
|
|
2020-02-21 03:35:15 +01:00
|
|
|
for(const auto &[keyid, sig] : origin_sigs)
|
|
|
|
if(verify(event, origin, json::string(keyid)))
|
2019-01-26 23:15:36 +01:00
|
|
|
return true;
|
2019-01-18 20:10:21 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return false;
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const string_view &origin,
|
|
|
|
const string_view &keyid)
|
|
|
|
try
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const m::node node
|
|
|
|
{
|
2019-05-27 02:44:12 +02:00
|
|
|
origin
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-18 20:10:21 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool ret{false};
|
|
|
|
node.key(keyid, [&ret, &event, &origin, &keyid]
|
|
|
|
(const ed25519::pk &pk)
|
|
|
|
{
|
|
|
|
ret = verify(event, pk, origin, keyid);
|
|
|
|
});
|
2019-01-18 20:10:21 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
catch(const m::NOT_FOUND &e)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
"Failed to verify %s because key %s for %s :%s",
|
2019-07-10 09:26:25 +02:00
|
|
|
string_view{event.event_id},
|
2019-01-26 23:15:36 +01:00
|
|
|
keyid,
|
|
|
|
origin,
|
|
|
|
e.what()
|
|
|
|
};
|
2019-01-18 20:10:21 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::verify(const event &event,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const string_view &origin,
|
|
|
|
const string_view &keyid)
|
2018-05-20 12:01:58 +02:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &signatures
|
|
|
|
{
|
|
|
|
at<"signatures"_>(event)
|
|
|
|
};
|
2018-05-20 12:01:58 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &origin_sigs
|
|
|
|
{
|
|
|
|
signatures.at(origin)
|
|
|
|
};
|
2018-05-20 12:01:58 +02:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const ed25519::sig sig
|
|
|
|
{
|
|
|
|
[&origin_sigs, &keyid](auto &buf)
|
|
|
|
{
|
2019-09-12 22:45:36 +02:00
|
|
|
b64decode(buf, json::string(origin_sigs.at(keyid)));
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
|
|
|
};
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return verify(event, pk, sig);
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::verify(const event &event_,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-02-08 08:41:29 +01:00
|
|
|
thread_local char buf[2][event::MAX_SIZE];
|
2019-01-26 23:15:36 +01:00
|
|
|
m::event event
|
|
|
|
{
|
2019-02-08 08:41:29 +01:00
|
|
|
essential(event_, buf[0])
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::object &preimage
|
|
|
|
{
|
2019-02-08 08:41:29 +01:00
|
|
|
stringify(buf[1], event)
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-02-08 08:41:29 +01:00
|
|
|
return pk.verify(preimage, sig);
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::event::verify(const json::object &event,
|
|
|
|
const ed25519::pk &pk,
|
|
|
|
const ed25519::sig &sig)
|
2018-02-22 00:00:02 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
thread_local char buf[event::MAX_SIZE];
|
|
|
|
const string_view preimage
|
2018-02-22 00:00:02 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
stringify(buf, event)
|
2018-02-22 00:00:02 +01:00
|
|
|
};
|
|
|
|
|
2019-02-08 08:41:29 +01:00
|
|
|
return pk.verify(preimage, sig);
|
2018-02-21 23:24:34 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
void
|
|
|
|
ircd::m::event::essential(json::iov &event,
|
|
|
|
const json::iov &contents,
|
2019-05-04 00:34:56 +02:00
|
|
|
const event::closure_iov_mutable &closure)
|
2019-08-28 02:08:39 +02:00
|
|
|
try
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-05-04 00:34:56 +02:00
|
|
|
const auto &type
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-05-04 00:34:56 +02:00
|
|
|
event.at("type")
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-02-21 23:24:34 +01:00
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
if(type == "m.room.aliases")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "aliases", contents.at("aliases") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.create")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "creator", contents.at("creator") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.history_visibility")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "history_visibility", contents.at("history_visibility") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.join_rules")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "join_rule", contents.at("join_rule") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.member")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "membership", contents.at("membership") }
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.power_levels")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
|
|
|
{ "ban", contents.at("ban") },
|
|
|
|
{ "events", contents.at("events") },
|
|
|
|
{ "events_default", contents.at("events_default") },
|
|
|
|
{ "kick", contents.at("kick") },
|
|
|
|
{ "redact", contents.at("redact") },
|
|
|
|
{ "state_default", contents.at("state_default") },
|
|
|
|
{ "users", contents.at("users") },
|
|
|
|
{ "users_default", contents.at("users_default") },
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.redaction")
|
|
|
|
{
|
|
|
|
// This simply finds the redacts key and swaps it with jsundefined for
|
|
|
|
// the scope's duration. The redacts key will still be present and
|
|
|
|
// visible in the json::iov which is incorrect if directly serialized.
|
|
|
|
// However, this iov is turned into a json::tuple (m::event) which ends
|
|
|
|
// up being serialized for signing. That serialization is where the
|
|
|
|
// jsundefined redacts value is ignored.
|
|
|
|
auto &redacts{event.at("redacts")};
|
|
|
|
json::value temp(std::move(redacts));
|
|
|
|
redacts = json::value{};
|
|
|
|
const unwind _{[&redacts, &temp]
|
|
|
|
{
|
|
|
|
redacts = std::move(temp);
|
|
|
|
}};
|
|
|
|
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", "{}" }
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const json::iov::push _content
|
|
|
|
{
|
|
|
|
event, { "content", "{}" }
|
|
|
|
};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2019-10-03 02:13:48 +02:00
|
|
|
catch(const json::not_found &e)
|
2019-08-28 02:08:39 +02:00
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Error while isolating essential keys (redaction algorithm) :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-02-21 23:24:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::event
|
|
|
|
ircd::m::essential(m::event event,
|
|
|
|
const mutable_buffer &contentbuf)
|
2019-08-28 02:08:39 +02:00
|
|
|
try
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-05-04 00:34:56 +02:00
|
|
|
const auto &type
|
|
|
|
{
|
|
|
|
json::at<"type"_>(event)
|
|
|
|
};
|
|
|
|
|
|
|
|
json::object &content
|
|
|
|
{
|
|
|
|
json::get<"content"_>(event)
|
|
|
|
};
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
mutable_buffer essential
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2019-05-04 00:34:56 +02:00
|
|
|
contentbuf
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-03-09 16:11:00 +01:00
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
if(type == "m.room.aliases")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
if(content.has("aliases"))
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "aliases", content.at("aliases") }
|
|
|
|
});
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.create")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
if(content.has("creator"))
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "creator", content.at("creator") }
|
|
|
|
});
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.history_visibility")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
if(content.has("history_visibility"))
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "history_visibility", content.at("history_visibility") }
|
|
|
|
});
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.join_rules")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
if(content.has("join_rule"))
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "join_rule", content.at("join_rule") }
|
|
|
|
});
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.member")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
if(content.has("membership"))
|
|
|
|
content = json::stringify(essential, json::members
|
|
|
|
{
|
|
|
|
{ "membership", content.at("membership") }
|
|
|
|
});
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.power_levels")
|
|
|
|
{
|
2019-09-06 21:05:07 +02:00
|
|
|
json::stack out{essential};
|
|
|
|
json::stack::object top{out};
|
|
|
|
|
|
|
|
if(content.has("ban"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "ban", content.at("ban")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("events"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "events", content.at("events")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("events_default"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "events_default", content.at("events_default")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("kick"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "kick", content.at("kick")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("redact"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "redact", content.at("redact")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("state_default"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "state_default", content.at("state_default")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("users"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "users", content.at("users")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(content.has("users_default"))
|
|
|
|
json::stack::member
|
|
|
|
{
|
|
|
|
top, "users_default", content.at("users_default")
|
|
|
|
};
|
|
|
|
|
|
|
|
top.~object();
|
|
|
|
content = out.completed();
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
else if(type == "m.room.redaction")
|
|
|
|
{
|
|
|
|
json::get<"redacts"_>(event) = string_view{};
|
|
|
|
content = "{}"_sv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
content = "{}"_sv;
|
|
|
|
}
|
|
|
|
|
|
|
|
json::get<"signatures"_>(event) = {};
|
|
|
|
return event;
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2019-10-03 02:13:48 +02:00
|
|
|
catch(const json::not_found &e)
|
2019-08-28 02:08:39 +02:00
|
|
|
{
|
|
|
|
log::derror
|
|
|
|
{
|
|
|
|
log, "Error while isolating essential keys (redaction algorithm) :%s",
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2018-03-09 16:11:00 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
2019-07-10 11:04:08 +02:00
|
|
|
const string_view &version,
|
2019-01-26 23:15:36 +01:00
|
|
|
id::event::buf &buf)
|
|
|
|
{
|
2019-07-10 11:04:08 +02:00
|
|
|
if(version == "1" || version == "2")
|
|
|
|
{
|
|
|
|
const crh::sha256::buf hash{event};
|
|
|
|
return make_id(event, version, buf, hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(version == "3")
|
|
|
|
return event::id::v3
|
|
|
|
{
|
|
|
|
buf, event
|
|
|
|
};
|
|
|
|
|
|
|
|
return event::id::v4
|
|
|
|
{
|
|
|
|
buf, event
|
|
|
|
};
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-03-09 16:11:00 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
2019-07-10 11:04:08 +02:00
|
|
|
const string_view &version,
|
2019-01-26 23:15:36 +01:00
|
|
|
id::event::buf &buf,
|
|
|
|
const const_buffer &hash)
|
|
|
|
{
|
2019-07-10 11:04:08 +02:00
|
|
|
char readable[b64encode_size(sha256::digest_size)];
|
|
|
|
|
|
|
|
if(version == "1" || version == "2")
|
|
|
|
{
|
|
|
|
const id::event ret
|
|
|
|
{
|
2019-10-01 05:50:58 +02:00
|
|
|
buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), at<"origin"_>(event)
|
2019-07-10 11:04:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if(version == "3")
|
|
|
|
{
|
|
|
|
const id::event ret
|
|
|
|
{
|
|
|
|
buf, b64encode_unpadded(readable, hash), string_view{}
|
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
const id::event ret
|
|
|
|
{
|
2019-07-10 11:04:08 +02:00
|
|
|
buf, b64tob64url(readable, b64encode_unpadded(readable, hash)), string_view{}
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-03-09 16:11:00 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-07-10 09:19:20 +02:00
|
|
|
bool
|
|
|
|
ircd::m::check_id(const event &event)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
if(!event.event_id)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const string_view &version
|
|
|
|
{
|
|
|
|
event.event_id.version()
|
|
|
|
};
|
|
|
|
|
|
|
|
return check_id(event, version);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::check_id(const event &event,
|
|
|
|
const string_view &room_version)
|
|
|
|
noexcept try
|
|
|
|
{
|
2019-08-16 07:37:37 +02:00
|
|
|
assert(event.event_id);
|
2019-07-10 09:19:20 +02:00
|
|
|
const auto &version
|
|
|
|
{
|
2019-08-16 07:37:37 +02:00
|
|
|
room_version?: event.event_id.version()
|
2019-07-10 09:19:20 +02:00
|
|
|
};
|
|
|
|
|
2019-08-24 02:28:58 +02:00
|
|
|
char buf[64];
|
2019-08-14 11:53:02 +02:00
|
|
|
const event::id &check_id
|
2019-07-10 09:19:20 +02:00
|
|
|
{
|
2019-08-14 11:53:02 +02:00
|
|
|
version == "1" || version == "2"?
|
|
|
|
event::id{json::get<"event_id"_>(event)}:
|
2019-07-10 09:19:20 +02:00
|
|
|
|
2019-08-14 11:53:02 +02:00
|
|
|
version == "3"?
|
|
|
|
event::id{event::id::v3{buf, event}}:
|
2019-07-10 09:19:20 +02:00
|
|
|
|
2019-08-14 11:53:02 +02:00
|
|
|
event::id{event::id::v4{buf, event}}
|
|
|
|
};
|
2019-07-10 09:19:20 +02:00
|
|
|
|
|
|
|
return event.event_id == check_id;
|
|
|
|
}
|
2019-08-14 11:41:30 +02:00
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2019-08-24 02:59:14 +02:00
|
|
|
log::error
|
2019-08-14 11:41:30 +02:00
|
|
|
{
|
2019-08-24 02:59:14 +02:00
|
|
|
"m::check_id() :%s", e.what()
|
2019-08-14 11:41:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-10 09:19:20 +02:00
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::before(const event &a,
|
|
|
|
const event &b)
|
|
|
|
{
|
|
|
|
const event::prev prev{b};
|
2019-07-10 09:26:25 +02:00
|
|
|
return prev.prev_events_has(a.event_id);
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-03-12 04:48:34 +01:00
|
|
|
|
2019-09-18 19:30:23 +02:00
|
|
|
size_t
|
|
|
|
ircd::m::degree(const event &event)
|
|
|
|
{
|
|
|
|
return degree(event::prev{event});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::m::degree(const event::prev &prev)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
json::for_each(prev, [&ret]
|
|
|
|
(const auto &, const json::array &prevs)
|
|
|
|
{
|
|
|
|
ret += prevs.count();
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator>=(const event &a, const event &b)
|
|
|
|
{
|
2019-06-02 02:52:52 +02:00
|
|
|
//assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
2019-06-07 17:55:26 +02:00
|
|
|
return json::get<"depth"_>(a) >= json::get<"depth"_>(b);
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-02-21 23:24:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator<=(const event &a, const event &b)
|
|
|
|
{
|
2019-06-02 02:52:52 +02:00
|
|
|
//assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
2019-06-07 17:55:26 +02:00
|
|
|
return json::get<"depth"_>(a) <= json::get<"depth"_>(b);
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-02-21 23:24:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator>(const event &a, const event &b)
|
|
|
|
{
|
2019-06-02 02:52:52 +02:00
|
|
|
//assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
2019-06-07 17:55:26 +02:00
|
|
|
return json::get<"depth"_>(a) > json::get<"depth"_>(b);
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-02-21 23:24:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator<(const event &a, const event &b)
|
|
|
|
{
|
2019-06-02 02:52:52 +02:00
|
|
|
//assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
2019-06-07 17:55:26 +02:00
|
|
|
return json::get<"depth"_>(a) < json::get<"depth"_>(b);
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator==(const event &a, const event &b)
|
|
|
|
{
|
2019-06-02 02:52:52 +02:00
|
|
|
//assert(json::get<"room_id"_>(a) == json::get<"room_id"_>(b));
|
2019-07-10 09:26:25 +02:00
|
|
|
return a.event_id == b.event_id;
|
2019-01-26 23:15:36 +01:00
|
|
|
}
|
2018-02-21 03:09:39 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::bad(const id::event &event_id)
|
|
|
|
{
|
|
|
|
bool ret {false};
|
2020-04-02 01:12:41 +02:00
|
|
|
index(std::nothrow, event_id, [&ret]
|
2019-01-26 23:15:36 +01:00
|
|
|
(const event::idx &event_idx)
|
2018-03-12 04:48:34 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
ret = event_idx == 0;
|
|
|
|
});
|
2018-03-12 04:48:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2018-03-12 04:48:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::count(const event::prev &prev)
|
|
|
|
{
|
|
|
|
size_t ret{0};
|
|
|
|
m::for_each(prev, [&ret](const event::id &event_id)
|
2018-03-12 04:48:34 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
++ret;
|
2019-07-27 00:55:42 +02:00
|
|
|
return true;
|
2019-01-26 23:15:36 +01:00
|
|
|
});
|
2018-03-12 04:48:34 +01:00
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
return ret;
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::good(const id::event &event_id)
|
2018-02-28 05:55:59 +01:00
|
|
|
{
|
2020-04-02 01:12:41 +02:00
|
|
|
return bool(event_id) && index(std::nothrow, event_id) != 0;
|
2018-02-28 05:55:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::event &event_id,
|
|
|
|
const bool &good)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
return good?
|
|
|
|
m::good(event_id):
|
|
|
|
m::exists(event_id);
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::exists(const id::event &event_id)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_idx
|
|
|
|
};
|
|
|
|
|
2019-06-02 03:17:15 +02:00
|
|
|
return bool(event_id) && has(column, event_id);
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::my(const event &event)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-01-26 23:15:36 +01:00
|
|
|
const auto &origin(json::get<"origin"_>(event));
|
2019-07-14 05:01:47 +02:00
|
|
|
const auto &sender(json::get<"sender"_>(event));
|
2019-07-10 09:26:25 +02:00
|
|
|
const auto &eid(event.event_id);
|
2019-01-26 23:15:36 +01:00
|
|
|
return
|
|
|
|
origin?
|
|
|
|
my_host(origin):
|
2019-07-14 05:01:47 +02:00
|
|
|
sender?
|
|
|
|
my_host(user::id(sender).host()):
|
2019-01-26 23:15:36 +01:00
|
|
|
eid?
|
|
|
|
my(event::id(eid)):
|
|
|
|
false;
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-01-26 23:15:36 +01:00
|
|
|
ircd::m::my(const id::event &event_id)
|
2018-02-21 03:09:39 +01:00
|
|
|
{
|
2019-12-10 20:05:33 +01:00
|
|
|
return event_id.version() == "1"?
|
|
|
|
self::host(event_id.host()):
|
|
|
|
event::my(index(event_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::m::event::my(const idx &event_idx)
|
|
|
|
{
|
|
|
|
return m::query(std::nothrow, event_idx, "origin", []
|
|
|
|
(const string_view &origin)
|
|
|
|
{
|
|
|
|
return m::my_host(origin);
|
|
|
|
});
|
2018-02-21 03:09:39 +01:00
|
|
|
}
|
2019-05-04 00:34:56 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// event::event
|
|
|
|
//
|
|
|
|
|
2019-07-06 04:45:02 +02:00
|
|
|
ircd::m::event::event(const json::members &members)
|
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
members
|
|
|
|
}
|
|
|
|
,event_id
|
|
|
|
{
|
|
|
|
defined(json::get<"event_id"_>(*this))?
|
|
|
|
id{json::get<"event_id"_>(*this)}:
|
|
|
|
id{},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(const json::iov &members)
|
|
|
|
:event
|
|
|
|
{
|
|
|
|
members,
|
|
|
|
members.has("event_id")?
|
|
|
|
id{members.at("event_id")}:
|
|
|
|
id{}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(const json::iov &members,
|
|
|
|
const id &id)
|
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
members
|
|
|
|
}
|
|
|
|
,event_id
|
|
|
|
{
|
|
|
|
id
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
ircd::m::event::event(const json::object &source)
|
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
source
|
|
|
|
}
|
2019-07-06 04:45:02 +02:00
|
|
|
,event_id
|
|
|
|
{
|
|
|
|
defined(json::get<"event_id"_>(*this))?
|
|
|
|
id{json::get<"event_id"_>(*this)}:
|
|
|
|
id{},
|
|
|
|
}
|
2019-05-04 00:34:56 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(const json::object &source,
|
|
|
|
const keys &keys)
|
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
source, keys
|
|
|
|
}
|
2019-07-06 04:45:02 +02:00
|
|
|
,event_id
|
|
|
|
{
|
|
|
|
defined(json::get<"event_id"_>(*this))?
|
|
|
|
id{json::get<"event_id"_>(*this)}:
|
|
|
|
id{},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(id::buf &buf,
|
|
|
|
const json::object &source,
|
|
|
|
const string_view &version)
|
|
|
|
:event
|
|
|
|
{
|
|
|
|
source,
|
|
|
|
version == "1"?
|
2019-09-12 22:45:36 +02:00
|
|
|
id{json::string(source.get("event_id"))}:
|
2019-07-06 04:45:02 +02:00
|
|
|
version == "2"?
|
2019-09-12 22:45:36 +02:00
|
|
|
id{json::string(source.get("event_id"))}:
|
2019-07-06 04:45:02 +02:00
|
|
|
version == "3"?
|
|
|
|
id{id::v3{buf, source}}:
|
|
|
|
version == "4"?
|
|
|
|
id{id::v4{buf, source}}:
|
2019-07-12 03:50:52 +02:00
|
|
|
source.has("event_id")?
|
2019-09-12 22:45:36 +02:00
|
|
|
id{json::string(source.at("event_id"))}:
|
2019-07-06 04:45:02 +02:00
|
|
|
id{id::v4{buf, source}},
|
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event::event(const json::object &source,
|
|
|
|
const id &event_id)
|
2019-09-15 23:39:09 +02:00
|
|
|
try
|
2019-07-06 04:45:02 +02:00
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
source
|
|
|
|
}
|
|
|
|
,event_id
|
|
|
|
{
|
2019-07-12 04:42:30 +02:00
|
|
|
event_id?
|
|
|
|
event_id:
|
|
|
|
defined(json::get<"event_id"_>(*this))?
|
|
|
|
id{json::get<"event_id"_>(*this)}:
|
|
|
|
id{},
|
2019-07-06 04:45:02 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
}
|
2019-09-15 23:39:09 +02:00
|
|
|
catch(const json::parse_error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Event %s from JSON source (%zu bytes) :%s",
|
|
|
|
event_id?
|
|
|
|
string_view{event_id}:
|
|
|
|
"<event_id in source>"_sv,
|
|
|
|
string_view{source}.size(),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|
2019-07-06 04:45:02 +02:00
|
|
|
|
|
|
|
ircd::m::event::event(const json::object &source,
|
|
|
|
const id &event_id,
|
|
|
|
const keys &keys)
|
2019-09-15 23:39:09 +02:00
|
|
|
try
|
2019-07-06 04:45:02 +02:00
|
|
|
:super_type
|
|
|
|
{
|
|
|
|
source, keys
|
|
|
|
}
|
|
|
|
,event_id
|
|
|
|
{
|
2019-07-12 04:42:30 +02:00
|
|
|
event_id?
|
|
|
|
event_id:
|
|
|
|
defined(json::get<"event_id"_>(*this))?
|
|
|
|
id{json::get<"event_id"_>(*this)}:
|
|
|
|
id{},
|
2019-07-06 04:45:02 +02:00
|
|
|
}
|
2019-05-04 00:34:56 +02:00
|
|
|
{
|
|
|
|
}
|
2019-09-15 23:39:09 +02:00
|
|
|
catch(const json::parse_error &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
log, "Event %s from JSON source (%zu bytes) keys:%zu :%s",
|
|
|
|
string_view{event_id},
|
|
|
|
string_view{source}.size(),
|
|
|
|
keys.count(),
|
|
|
|
e.what(),
|
|
|
|
};
|
|
|
|
}
|