From 8baaf366c4dbfc071bc53ac4728b9c1e8dc11a21 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 23 Feb 2020 22:44:14 -0800 Subject: [PATCH] ircd::m::event: Fix signature interface regression; optimize call path. --- include/ircd/m/event/event.h | 2 ++ matrix/event.cc | 55 +++++++++++++++++++++++++---------- modules/federation/invite.cc | 6 +++- modules/federation/invite2.cc | 2 +- 4 files changed, 48 insertions(+), 17 deletions(-) diff --git a/include/ircd/m/event/event.h b/include/ircd/m/event/event.h index b8db1af53..c2915ba05 100644 --- a/include/ircd/m/event/event.h +++ b/include/ircd/m/event/event.h @@ -43,6 +43,7 @@ namespace ircd::m id::event make_id(const event &, const string_view &version, id::event::buf &buf); json::object hashes(const mutable_buffer &, const event &); + event signatures(const mutable_buffer &, const m::event &, const string_view &origin); event signatures(const mutable_buffer &, const m::event &); event essential(event, const mutable_buffer &content); @@ -58,6 +59,7 @@ namespace ircd::m sha256::buf hash(const event &); ed25519::sig sign(const event &, const ed25519::sk &); + ed25519::sig sign(const event &, const string_view &origin); ed25519::sig sign(const event &); } diff --git a/matrix/event.cc b/matrix/event.cc index fadf86270..728c6a0d1 100644 --- a/matrix/event.cc +++ b/matrix/event.cc @@ -211,9 +211,22 @@ ircd::m::event::signatures(const mutable_buffer &out, return json::stringify(mutable_buffer{out}, sigs); } +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); +} + ircd::m::event ircd::m::signatures(const mutable_buffer &out_, - const m::event &event_) + const m::event &event_, + const string_view &origin) { thread_local char content[event::MAX_SIZE]; m::event event @@ -222,19 +235,14 @@ ircd::m::signatures(const mutable_buffer &out_, }; thread_local char buf[event::MAX_SIZE]; - const json::object &preimage + const string_view &preimage { stringify(buf, event) }; - const ed25519::sig sig + const auto &secret_key { - sign(preimage) - }; - - const auto &origin - { - json::at<"origin"_>(event) + m::secret_key(my(origin)) }; const string_view public_key_id @@ -242,12 +250,22 @@ ircd::m::signatures(const mutable_buffer &out_, m::public_key_id(my(origin)) }; - thread_local char sigb64buf[b64encode_size(sizeof(sig))]; - const json::member my_sig + const ed25519::sig my_sig + { + event::sign(preimage, secret_key) + }; + + static const auto sigb64bufsz + { + b64encode_size(sizeof(my_sig)) + }; + + thread_local char sigb64buf[sigb64bufsz]; + const json::member my_sig_member { origin, json::members { - { public_key_id, b64encode_unpadded(sigb64buf, sig) } + { public_key_id, b64encode_unpadded(sigb64buf, my_sig) } } }; @@ -255,9 +273,9 @@ ircd::m::signatures(const mutable_buffer &out_, thread_local std::array sigs; size_t i(0); - sigs.at(i++) = my_sig; + sigs.at(i++) = my_sig_member; for(const auto &[host, sig] : json::get<"signatures"_>(event_)) - if(!my_host(json::string(host))) + if(json::string(host) != origin) sigs.at(i++) = { host, sig }; event = event_; @@ -303,9 +321,16 @@ ircd::m::sign(const event &event) { const string_view &origin { - json::at<"origin"_>(event) + at<"origin"_>(event) }; + return sign(event, origin); +} + +ircd::ed25519::sig +ircd::m::sign(const event &event, + const string_view &origin) +{ const auto &secret_key { m::secret_key(my(origin)) diff --git a/modules/federation/invite.cc b/modules/federation/invite.cc index 69d7bd02d..a4386b0cd 100644 --- a/modules/federation/invite.cc +++ b/modules/federation/invite.cc @@ -110,11 +110,15 @@ put__invite(client &client, }; check_event(request, event); + const m::user::id target + { + at<"state_key"_>(event) + }; thread_local char sigs[4_KiB]; const m::event signed_event { - signatures(sigs, event) + signatures(sigs, event, target.host()) }; const json::strung revent diff --git a/modules/federation/invite2.cc b/modules/federation/invite2.cc index 5735338a8..f2974afaf 100644 --- a/modules/federation/invite2.cc +++ b/modules/federation/invite2.cc @@ -184,7 +184,7 @@ put__invite(client &client, thread_local char sigs[4_KiB]; m::event signed_event { - signatures(sigs, event) + signatures(sigs, event, target.host()) }; signed_event.event_id = event_id;