2017-07-10 08:44:15 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2017 Vector Creations Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2017-10-27 04:28:12 -06:00
|
|
|
"""Attestations ensure that users and groups can't lie about their memberships.
|
|
|
|
|
|
|
|
When a user joins a group the HS and GS swap attestations, which allow them
|
|
|
|
both to independently prove to third parties their membership.These
|
|
|
|
attestations have a validity period so need to be periodically renewed.
|
|
|
|
|
|
|
|
If a user leaves (or gets kicked out of) a group, either side can still use
|
|
|
|
their attestation to "prove" their membership, until the attestation expires.
|
|
|
|
Therefore attestations shouldn't be relied on to prove membership in important
|
|
|
|
cases, but can for less important situtations, e.g. showing a users membership
|
|
|
|
of groups on their profile, showing flairs, etc.abs
|
|
|
|
|
|
|
|
An attestsation is a signed blob of json that looks like:
|
|
|
|
|
|
|
|
{
|
|
|
|
"user_id": "@foo:a.example.com",
|
|
|
|
"group_id": "+bar:b.example.com",
|
|
|
|
"valid_until_ms": 1507994728530,
|
|
|
|
"signatures":{"matrix.org":{"ed25519:auto":"..."}}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2017-10-27 03:54:02 -06:00
|
|
|
import logging
|
2017-10-27 04:57:27 -06:00
|
|
|
import random
|
2017-10-27 03:54:02 -06:00
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError
|
|
|
|
from synapse.types import get_domain_from_id
|
|
|
|
from synapse.util.logcontext import preserve_fn
|
|
|
|
|
|
|
|
from signedjson.sign import sign_json
|
|
|
|
|
|
|
|
|
2017-10-27 03:54:02 -06:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2017-07-11 07:31:36 -06:00
|
|
|
# Default validity duration for new attestations we create
|
2017-07-10 08:44:15 -06:00
|
|
|
DEFAULT_ATTESTATION_LENGTH_MS = 3 * 24 * 60 * 60 * 1000
|
2017-07-11 07:31:36 -06:00
|
|
|
|
2017-11-07 06:32:35 -07:00
|
|
|
# We add some jitter to the validity duration of attestations so that if we
|
|
|
|
# add lots of users at once we don't need to renew them all at once.
|
|
|
|
# The jitter is a multiplier picked randomly between the first and second number
|
|
|
|
DEFAULT_ATTESTATION_JITTER = (0.9, 1.3)
|
|
|
|
|
2017-07-11 07:31:36 -06:00
|
|
|
# Start trying to update our attestations when they come this close to expiring
|
2017-07-10 08:44:15 -06:00
|
|
|
UPDATE_ATTESTATION_TIME_MS = 1 * 24 * 60 * 60 * 1000
|
|
|
|
|
|
|
|
|
|
|
|
class GroupAttestationSigning(object):
|
2017-07-11 02:58:59 -06:00
|
|
|
"""Creates and verifies group attestations.
|
|
|
|
"""
|
2017-07-10 08:44:15 -06:00
|
|
|
def __init__(self, hs):
|
|
|
|
self.keyring = hs.get_keyring()
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.server_name = hs.hostname
|
|
|
|
self.signing_key = hs.config.signing_key[0]
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def verify_attestation(self, attestation, group_id, user_id, server_name=None):
|
2017-07-11 07:23:50 -06:00
|
|
|
"""Verifies that the given attestation matches the given parameters.
|
2017-07-11 02:58:59 -06:00
|
|
|
|
|
|
|
An optional server_name can be supplied to explicitly set which server's
|
|
|
|
signature is expected. Otherwise assumes that either the group_id or user_id
|
|
|
|
is local and uses the other's server as the one to check.
|
|
|
|
"""
|
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
if not server_name:
|
|
|
|
if get_domain_from_id(group_id) == self.server_name:
|
|
|
|
server_name = get_domain_from_id(user_id)
|
2017-07-11 02:58:59 -06:00
|
|
|
elif get_domain_from_id(user_id) == self.server_name:
|
2017-07-10 08:44:15 -06:00
|
|
|
server_name = get_domain_from_id(group_id)
|
2017-07-11 02:58:59 -06:00
|
|
|
else:
|
2017-07-11 07:23:50 -06:00
|
|
|
raise Exception("Expected either group_id or user_id to be local")
|
2017-07-10 08:44:15 -06:00
|
|
|
|
|
|
|
if user_id != attestation["user_id"]:
|
|
|
|
raise SynapseError(400, "Attestation has incorrect user_id")
|
|
|
|
|
|
|
|
if group_id != attestation["group_id"]:
|
|
|
|
raise SynapseError(400, "Attestation has incorrect group_id")
|
|
|
|
valid_until_ms = attestation["valid_until_ms"]
|
2017-07-11 07:31:36 -06:00
|
|
|
|
|
|
|
# TODO: We also want to check that *new* attestations that people give
|
|
|
|
# us to store are valid for at least a little while.
|
|
|
|
if valid_until_ms < self.clock.time_msec():
|
|
|
|
raise SynapseError(400, "Attestation expired")
|
2017-07-10 08:44:15 -06:00
|
|
|
|
|
|
|
yield self.keyring.verify_json_for_server(server_name, attestation)
|
|
|
|
|
|
|
|
def create_attestation(self, group_id, user_id):
|
2017-07-11 02:58:59 -06:00
|
|
|
"""Create an attestation for the group_id and user_id with default
|
|
|
|
validity length.
|
|
|
|
"""
|
2017-11-07 06:32:35 -07:00
|
|
|
validity_period = DEFAULT_ATTESTATION_LENGTH_MS
|
|
|
|
validity_period *= random.uniform(*DEFAULT_ATTESTATION_JITTER)
|
|
|
|
valid_until_ms = int(self.clock.time_msec() + validity_period)
|
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
return sign_json({
|
|
|
|
"group_id": group_id,
|
|
|
|
"user_id": user_id,
|
2017-11-07 06:32:35 -07:00
|
|
|
"valid_until_ms": valid_until_ms,
|
2017-07-10 08:44:15 -06:00
|
|
|
}, self.server_name, self.signing_key)
|
|
|
|
|
|
|
|
|
|
|
|
class GroupAttestionRenewer(object):
|
2017-07-11 02:58:59 -06:00
|
|
|
"""Responsible for sending and receiving attestation updates.
|
|
|
|
"""
|
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
def __init__(self, hs):
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.assestations = hs.get_groups_attestation_signing()
|
|
|
|
self.transport_client = hs.get_federation_transport_client()
|
2017-07-11 07:29:03 -06:00
|
|
|
self.is_mine_id = hs.is_mine_id
|
2017-10-19 03:55:44 -06:00
|
|
|
self.attestations = hs.get_groups_attestation_signing()
|
2017-07-10 08:44:15 -06:00
|
|
|
|
|
|
|
self._renew_attestations_loop = self.clock.looping_call(
|
|
|
|
self._renew_attestations, 30 * 60 * 1000,
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def on_renew_attestation(self, group_id, user_id, content):
|
2017-07-11 02:58:59 -06:00
|
|
|
"""When a remote updates an attestation
|
|
|
|
"""
|
2017-07-10 08:44:15 -06:00
|
|
|
attestation = content["attestation"]
|
|
|
|
|
2017-07-11 02:58:59 -06:00
|
|
|
if not self.is_mine_id(group_id) and not self.is_mine_id(user_id):
|
|
|
|
raise SynapseError(400, "Neither user not group are on this server")
|
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
yield self.attestations.verify_attestation(
|
|
|
|
attestation,
|
|
|
|
user_id=user_id,
|
|
|
|
group_id=group_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.store.update_remote_attestion(group_id, user_id, attestation)
|
|
|
|
|
|
|
|
defer.returnValue({})
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _renew_attestations(self):
|
2017-07-11 02:58:59 -06:00
|
|
|
"""Called periodically to check if we need to update any of our attestations
|
|
|
|
"""
|
|
|
|
|
2017-07-10 08:44:15 -06:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
|
|
|
|
rows = yield self.store.get_attestations_need_renewals(
|
|
|
|
now + UPDATE_ATTESTATION_TIME_MS
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2017-10-19 03:17:43 -06:00
|
|
|
def _renew_attestation(group_id, user_id):
|
2017-10-27 02:55:01 -06:00
|
|
|
if not self.is_mine_id(group_id):
|
|
|
|
destination = get_domain_from_id(group_id)
|
2017-10-27 03:30:21 -06:00
|
|
|
elif not self.is_mine_id(user_id):
|
2017-07-10 08:44:15 -06:00
|
|
|
destination = get_domain_from_id(user_id)
|
|
|
|
else:
|
2017-10-27 02:55:01 -06:00
|
|
|
logger.warn(
|
|
|
|
"Incorrectly trying to do attestations for user: %r in %r",
|
|
|
|
user_id, group_id,
|
|
|
|
)
|
2017-10-27 02:58:13 -06:00
|
|
|
yield self.store.remove_attestation_renewal(group_id, user_id)
|
2017-10-27 02:55:01 -06:00
|
|
|
return
|
2017-07-10 08:44:15 -06:00
|
|
|
|
2017-10-27 03:30:21 -06:00
|
|
|
attestation = self.attestations.create_attestation(group_id, user_id)
|
2017-07-10 08:44:15 -06:00
|
|
|
|
|
|
|
yield self.transport_client.renew_group_attestation(
|
|
|
|
destination, group_id, user_id,
|
|
|
|
content={"attestation": attestation},
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.store.update_attestation_renewal(
|
|
|
|
group_id, user_id, attestation
|
|
|
|
)
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
group_id = row["group_id"]
|
|
|
|
user_id = row["user_id"]
|
|
|
|
|
|
|
|
preserve_fn(_renew_attestation)(group_id, user_id)
|