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
|
|
|
{
|
2023-02-26 09:21:08 +01:00
|
|
|
{ "name", "ircd.m.event.max_size" },
|
|
|
|
{ "default", 65507L },
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2018-05-12 04:21:56 +02:00
|
|
|
|
2022-07-20 19:42:13 +02:00
|
|
|
[[gnu::tls_model("global-dynamic")]]
|
2020-11-30 00:15:54 +01:00
|
|
|
decltype(ircd::m::event::buf)
|
|
|
|
thread_local
|
|
|
|
ircd::m::event::buf;
|
|
|
|
|
2020-06-19 07:10:24 +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
|
|
|
|
{
|
|
|
|
assert(event.event_id);
|
|
|
|
const auto &version
|
|
|
|
{
|
|
|
|
room_version?: event.event_id.version()
|
|
|
|
};
|
|
|
|
|
|
|
|
char buf[64];
|
|
|
|
const event::id &check_id
|
|
|
|
{
|
|
|
|
version == "1" || version == "2"?
|
|
|
|
event::id{json::get<"event_id"_>(event)}:
|
|
|
|
|
|
|
|
version == "3"?
|
|
|
|
event::id{event::id::v3{buf, event}}:
|
|
|
|
|
|
|
|
event::id{event::id::v4{buf, event}}
|
|
|
|
};
|
|
|
|
|
|
|
|
return event.event_id == check_id;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error
|
|
|
|
{
|
|
|
|
"m::check_id() :%s", e.what()
|
|
|
|
};
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
|
|
|
const string_view &version,
|
|
|
|
id::event::buf &buf)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::id::event
|
|
|
|
ircd::m::make_id(const event &event,
|
|
|
|
const string_view &version,
|
|
|
|
id::event::buf &buf,
|
|
|
|
const const_buffer &hash)
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
char readable[b64::encode_size(sha256::digest_size)];
|
2020-06-19 07:10:24 +02:00
|
|
|
|
|
|
|
if(version == "1" || version == "2")
|
|
|
|
{
|
|
|
|
const id::event ret
|
|
|
|
{
|
2023-03-04 07:20:27 +01:00
|
|
|
buf, b64::encode_unpadded(readable, hash, b64::urlsafe), at<"origin"_>(event)
|
2020-06-19 07:10:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
else if(version == "3")
|
|
|
|
{
|
|
|
|
const id::event ret
|
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
buf, b64::encode_unpadded(readable, hash), string_view{}
|
2020-06-19 07:10:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id::event ret
|
|
|
|
{
|
2023-03-04 07:20:27 +01:00
|
|
|
buf, b64::encode_unpadded(readable, hash, b64::urlsafe), string_view{}
|
2020-06-19 07:10:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buf.assigned(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-09-29 06:32:32 +02:00
|
|
|
static const auto b64bufsz
|
|
|
|
{
|
|
|
|
b64::encode_size(sizeof(hash))
|
|
|
|
};
|
|
|
|
|
|
|
|
char hashb64buf[b64bufsz];
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::members hashes
|
2019-01-16 21:12:59 +01:00
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
{ "sha256", b64::encode_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
|
2020-06-19 07:12:07 +02:00
|
|
|
ircd::m::event::hash(const json::object &event_)
|
2019-02-07 05:52:42 +01:00
|
|
|
{
|
2020-06-20 07:22:54 +02:00
|
|
|
const json::object preimage
|
2019-02-07 05:52:42 +01:00
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
event::preimage(buf[3], event_)
|
2019-02-07 05:52:42 +01:00
|
|
|
};
|
|
|
|
|
2020-06-19 07:12:07 +02:00
|
|
|
return sha256
|
2019-02-07 05:52:42 +01:00
|
|
|
{
|
2020-06-20 07:22:54 +02:00
|
|
|
preimage
|
2019-02-07 05:52:42 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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_) = {};
|
|
|
|
const string_view preimage
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
stringify(event::buf[3], 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,
|
2020-09-28 14:49:18 +02:00
|
|
|
const sha256::buf &actual)
|
2019-01-26 23:15:36 +01:00
|
|
|
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
|
|
|
|
2020-09-29 06:32:32 +02:00
|
|
|
char buf[32];
|
2020-09-28 14:49:18 +02:00
|
|
|
const auto claim
|
|
|
|
{
|
|
|
|
b64::decode(buf, hash)
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(buf) == sizeof(actual));
|
2020-09-29 06:32:32 +02:00
|
|
|
if(unlikely(ircd::size(claim) != sizeof(actual)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(memcmp(buf, ircd::data(actual), sizeof(buf)) != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
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
|
|
|
};
|
|
|
|
|
2020-09-29 06:32:32 +02:00
|
|
|
char sigb64buf[b64::encode_size(sizeof(sig))];
|
2019-01-26 23:15:36 +01:00
|
|
|
const json::members sigb64
|
2018-04-18 04:14:39 +02:00
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
{ public_key_id, b64::encode_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
|
2023-03-15 04:51:46 +01:00
|
|
|
ircd::m::signatures(const mutable_buffer &out,
|
|
|
|
const m::event &event,
|
2020-02-24 07:44:14 +01:00
|
|
|
const string_view &origin)
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2023-03-15 04:51:46 +01:00
|
|
|
const auto &secret_key
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2023-03-15 04:51:46 +01:00
|
|
|
m::secret_key(my(origin))
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-17 02:53:47 +01:00
|
|
|
|
2023-03-15 04:51:46 +01:00
|
|
|
const string_view public_key_id
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2023-03-15 04:51:46 +01:00
|
|
|
m::public_key_id(my(origin))
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-17 02:53:47 +01:00
|
|
|
|
2023-03-15 04:51:46 +01:00
|
|
|
return signatures(out, event, origin, secret_key, public_key_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::m::event
|
|
|
|
ircd::m::signatures(const mutable_buffer &out_,
|
|
|
|
const m::event &event_,
|
|
|
|
const string_view &origin,
|
|
|
|
const ed25519::sk &secret_key,
|
|
|
|
const string_view &public_key_id)
|
|
|
|
{
|
|
|
|
m::event event
|
2020-02-24 07:44:14 +01:00
|
|
|
{
|
2023-03-15 04:51:46 +01:00
|
|
|
essential(event_, event::buf[3])
|
2020-02-24 07:44:14 +01:00
|
|
|
};
|
|
|
|
|
2023-03-15 04:51:46 +01:00
|
|
|
const string_view &preimage
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2023-03-15 04:51:46 +01:00
|
|
|
stringify(event::buf[2], event)
|
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-08-10 12:51:09 +02:00
|
|
|
b64::encode_size(sizeof(my_sig))
|
2019-02-05 09:41:15 +01:00
|
|
|
};
|
|
|
|
|
2020-09-29 06:32:32 +02:00
|
|
|
char sigb64buf[sigb64bufsz];
|
2020-02-24 07:44:14 +01:00
|
|
|
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-08-10 12:51:09 +02:00
|
|
|
{ public_key_id, b64::encode_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
|
|
|
const string_view preimage
|
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
stringify(event::buf[3], event)
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
const string_view preimage
|
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
stringify(buf[3], event)
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-04-22 03:54:08 +02:00
|
|
|
const m::node::keys node_keys
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
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};
|
2020-04-22 03:54:08 +02:00
|
|
|
node_keys.get(keyid, [&ret, &event, &origin, &keyid]
|
2019-01-26 23:15:36 +01:00
|
|
|
(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;
|
|
|
|
}
|
2020-06-05 02:49:49 +02:00
|
|
|
catch(const ctx::interrupted &e)
|
2019-01-18 20:10:21 +01:00
|
|
|
{
|
2020-06-05 02:49:49 +02:00
|
|
|
log::error
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2020-06-05 02:49:49 +02:00
|
|
|
log, "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,
|
2020-06-05 02:49:49 +02:00
|
|
|
e.what(),
|
2019-01-26 23:15:36 +01:00
|
|
|
};
|
2019-01-18 20:10:21 +01:00
|
|
|
|
2020-06-05 02:49:49 +02:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
2020-06-07 11:24:49 +02:00
|
|
|
throw m::error
|
2020-06-05 02:49:49 +02:00
|
|
|
{
|
2020-06-07 11:24:49 +02:00
|
|
|
http::UNAUTHORIZED, "M_UNVERIFIABLE_SIGNATURE",
|
2022-07-03 03:47:12 +02:00
|
|
|
"%s key %s :%s",
|
2020-06-05 02:49:49 +02:00
|
|
|
origin,
|
2022-07-03 03:47:12 +02:00
|
|
|
keyid,
|
2020-06-05 02:49:49 +02:00
|
|
|
e.what(),
|
|
|
|
};
|
2019-01-18 20:10:21 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
[&origin_sigs, &keyid](auto&& buf)
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2020-08-10 12:51:09 +02:00
|
|
|
b64::decode(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-01-26 23:15:36 +01:00
|
|
|
m::event event
|
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
essential(event_, event::buf[3])
|
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
|
|
|
|
{
|
2020-11-30 00:15:54 +01:00
|
|
|
stringify(event::buf[2], 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,
|
2020-12-16 03:31:02 +01:00
|
|
|
const ed25519::sig &sig,
|
|
|
|
const bool &canonical)
|
2018-02-22 00:00:02 +01:00
|
|
|
{
|
2020-12-16 03:31:02 +01:00
|
|
|
const auto preimage
|
2018-02-22 00:00:02 +01:00
|
|
|
{
|
2020-12-16 03:31:02 +01:00
|
|
|
canonical?
|
|
|
|
string_view{event}:
|
|
|
|
stringify(buf[3], 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
|
|
|
|
{
|
2022-07-11 00:22:27 +02:00
|
|
|
{ "join_rule", contents.at("join_rule") }
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
closure(event);
|
|
|
|
}
|
|
|
|
else if(type == "m.room.member")
|
|
|
|
{
|
|
|
|
const json::iov::push _content{event,
|
|
|
|
{
|
|
|
|
"content", json::members
|
|
|
|
{
|
2022-07-11 00:22:27 +02:00
|
|
|
{ "membership", contents.at("membership") }
|
2019-05-04 00:34:56 +02:00
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
|
|
|
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,
|
2020-12-21 19:26:59 +01:00
|
|
|
const mutable_buffer &contentbuf,
|
2020-12-21 19:23:41 +01:00
|
|
|
const bool &sigs)
|
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
|
|
|
|
{
|
2020-11-30 11:08:58 +01:00
|
|
|
json::get<"type"_>(event)
|
2019-05-04 00:34:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-07-11 00:22:27 +02:00
|
|
|
{ "join_rule", content.at("join_rule") }
|
2019-09-06 21:05:07 +02:00
|
|
|
});
|
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
|
|
|
|
{
|
2022-07-11 00:22:27 +02:00
|
|
|
{ "membership", content.at("membership") }
|
2019-09-06 21:05:07 +02:00
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
|
2020-12-21 19:23:41 +01:00
|
|
|
if(likely(!sigs))
|
|
|
|
json::get<"signatures"_>(event) = {};
|
|
|
|
|
2019-05-04 00:34:56 +02:00
|
|
|
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
|
|
|
|
2020-06-19 07:12:07 +02:00
|
|
|
ircd::json::object
|
2020-06-20 07:22:54 +02:00
|
|
|
ircd::m::event::preimage(const mutable_buffer &buf_,
|
|
|
|
const json::object &event)
|
2020-06-19 07:12:07 +02:00
|
|
|
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" ||
|
|
|
|
m.first == "hashes" ||
|
|
|
|
m.first == "unsigned" ||
|
|
|
|
m.first == "age_ts" ||
|
|
|
|
m.first == "outlier" ||
|
|
|
|
m.first == "destinations")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
member.at(i++) = m;
|
|
|
|
}
|
|
|
|
|
2020-06-20 07:22:54 +02:00
|
|
|
mutable_buffer buf{buf_};
|
2020-06-19 07:12:07 +02:00
|
|
|
const string_view ret
|
|
|
|
{
|
2020-06-20 07:22:54 +02:00
|
|
|
json::stringify(buf, member.data(), member.data() + i)
|
2020-06-19 07:12:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
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
|
|
|
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});
|
|
|
|
}
|
|
|
|
|
2019-01-26 23:15:36 +01:00
|
|
|
bool
|
|
|
|
ircd::m::operator>=(const event &a, const event &b)
|
|
|
|
{
|
2020-09-11 05:06:28 +02:00
|
|
|
const int room_id_cmp
|
|
|
|
{
|
|
|
|
cmp(json::get<"room_id"_>(a), json::get<"room_id"_>(b))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto depth_gte
|
|
|
|
{
|
|
|
|
json::get<"depth"_>(a) >= json::get<"depth"_>(b)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room_id_cmp > 0 || (room_id_cmp == 0 && depth_gte);
|
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)
|
|
|
|
{
|
2020-09-11 05:06:28 +02:00
|
|
|
const int room_id_cmp
|
|
|
|
{
|
|
|
|
cmp(json::get<"room_id"_>(a), json::get<"room_id"_>(b))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto depth_lte
|
|
|
|
{
|
|
|
|
json::get<"depth"_>(a) <= json::get<"depth"_>(b)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room_id_cmp < 0 || (room_id_cmp == 0 && depth_lte);
|
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)
|
|
|
|
{
|
2020-09-11 05:06:28 +02:00
|
|
|
const int room_id_cmp
|
|
|
|
{
|
|
|
|
cmp(json::get<"room_id"_>(a), json::get<"room_id"_>(b))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto depth_gt
|
|
|
|
{
|
|
|
|
json::get<"depth"_>(a) > json::get<"depth"_>(b)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room_id_cmp > 0 || (room_id_cmp == 0 && depth_gt);
|
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
|
2020-09-11 05:06:28 +02:00
|
|
|
ircd::m::operator<(const event &a,
|
|
|
|
const event &b)
|
2019-01-26 23:15:36 +01:00
|
|
|
{
|
2020-09-11 05:06:28 +02:00
|
|
|
const int room_id_cmp
|
|
|
|
{
|
|
|
|
cmp(json::get<"room_id"_>(a), json::get<"room_id"_>(b))
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto depth_lt
|
|
|
|
{
|
|
|
|
json::get<"depth"_>(a) < json::get<"depth"_>(b)
|
|
|
|
};
|
|
|
|
|
|
|
|
return room_id_cmp < 0 || (room_id_cmp == 0 && depth_lt);
|
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
|
|
|
|
2023-02-05 22:25:45 +01:00
|
|
|
size_t
|
|
|
|
ircd::m::exists_count(const vector_view<const id::event> &event_ids)
|
|
|
|
{
|
|
|
|
const auto mask
|
|
|
|
{
|
|
|
|
exists(event_ids)
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ret
|
|
|
|
{
|
2023-02-07 20:27:51 +01:00
|
|
|
popcount(mask)
|
2023-02-05 22:25:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(size_t(ret) <= event_ids.size());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-06-09 08:11:05 +02:00
|
|
|
uint64_t
|
|
|
|
ircd::m::exists(const vector_view<const id::event> &event_ids)
|
|
|
|
{
|
|
|
|
const vector_view<const string_view> key
|
|
|
|
{
|
|
|
|
static_cast<const string_view *>(event_ids.data()), event_ids.size()
|
|
|
|
};
|
|
|
|
|
|
|
|
auto &column
|
|
|
|
{
|
|
|
|
dbs::event_idx
|
|
|
|
};
|
|
|
|
|
|
|
|
return db::has(column, key);
|
|
|
|
}
|
|
|
|
|
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]
|
2022-07-04 22:15:53 +02:00
|
|
|
(const event::idx &event_idx) noexcept
|
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
|
|
|
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(),
|
|
|
|
};
|
|
|
|
}
|