2014-08-28 11:19:47 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2019-04-08 07:51:07 -06:00
|
|
|
# Copyright 2019 New Vector Ltd.
|
2014-08-28 11:19:47 -06:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2019-04-08 07:51:07 -06:00
|
|
|
import itertools
|
2018-07-09 00:09:20 -06:00
|
|
|
import logging
|
2014-08-28 11:19:47 -06:00
|
|
|
|
2018-05-29 09:10:06 -06:00
|
|
|
import six
|
2014-08-28 11:19:47 -06:00
|
|
|
|
2019-04-03 11:10:24 -06:00
|
|
|
import attr
|
2015-08-24 09:17:38 -06:00
|
|
|
from signedjson.key import decode_verify_key_bytes
|
2014-08-28 11:19:47 -06:00
|
|
|
|
2019-04-08 07:51:07 -06:00
|
|
|
from synapse.util import batch_iter
|
|
|
|
from synapse.util.caches.descriptors import cached, cachedList
|
2018-07-09 00:09:20 -06:00
|
|
|
|
|
|
|
from ._base import SQLBaseStore
|
2016-07-21 10:37:44 -06:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-05-29 09:10:06 -06:00
|
|
|
# py2 sqlite has buffer hardcoded as only binary type, so we must use it,
|
|
|
|
# despite being deprecated and removed in favor of memoryview
|
|
|
|
if six.PY2:
|
2018-10-19 18:16:55 -06:00
|
|
|
db_binary_type = six.moves.builtins.buffer
|
2018-05-29 09:10:06 -06:00
|
|
|
else:
|
|
|
|
db_binary_type = memoryview
|
|
|
|
|
2014-10-30 05:10:17 -06:00
|
|
|
|
2019-04-03 11:10:24 -06:00
|
|
|
@attr.s(slots=True, frozen=True)
|
|
|
|
class FetchKeyResult(object):
|
|
|
|
verify_key = attr.ib() # VerifyKey: the key itself
|
|
|
|
valid_until_ts = attr.ib() # int: how long we can use this key for
|
|
|
|
|
|
|
|
|
2014-08-28 11:19:47 -06:00
|
|
|
class KeyStore(SQLBaseStore):
|
2019-04-08 14:50:18 -06:00
|
|
|
"""Persistence for signature verification keys
|
2014-08-28 11:19:47 -06:00
|
|
|
"""
|
2019-04-08 07:51:07 -06:00
|
|
|
|
|
|
|
@cached()
|
|
|
|
def _get_server_verify_key(self, server_name_and_key_id):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
@cachedList(
|
|
|
|
cached_method_name="_get_server_verify_key", list_name="server_name_and_key_ids"
|
|
|
|
)
|
|
|
|
def get_server_verify_keys(self, server_name_and_key_ids):
|
|
|
|
"""
|
2014-08-28 11:19:47 -06:00
|
|
|
Args:
|
2019-04-08 07:51:07 -06:00
|
|
|
server_name_and_key_ids (iterable[Tuple[str, str]]):
|
|
|
|
iterable of (server_name, key-id) tuples to fetch keys for
|
|
|
|
|
2014-08-28 11:19:47 -06:00
|
|
|
Returns:
|
2019-04-03 11:10:24 -06:00
|
|
|
Deferred: resolves to dict[Tuple[str, str], FetchKeyResult|None]:
|
|
|
|
map from (server_name, key_id) -> FetchKeyResult, or None if the key is
|
2019-04-08 07:51:07 -06:00
|
|
|
unknown
|
2014-08-28 11:19:47 -06:00
|
|
|
"""
|
2016-07-27 08:51:43 -06:00
|
|
|
keys = {}
|
2019-04-08 07:51:07 -06:00
|
|
|
|
|
|
|
def _get_keys(txn, batch):
|
|
|
|
"""Processes a batch of keys to fetch, and adds the result to `keys`."""
|
|
|
|
|
|
|
|
# batch_iter always returns tuples so it's safe to do len(batch)
|
|
|
|
sql = (
|
2019-04-03 11:10:24 -06:00
|
|
|
"SELECT server_name, key_id, verify_key, ts_valid_until_ms "
|
|
|
|
"FROM server_signature_keys WHERE 1=0"
|
2019-04-08 07:51:07 -06:00
|
|
|
) + " OR (server_name=? AND key_id=?)" * len(batch)
|
|
|
|
|
|
|
|
txn.execute(sql, tuple(itertools.chain.from_iterable(batch)))
|
|
|
|
|
|
|
|
for row in txn:
|
2019-04-03 11:10:24 -06:00
|
|
|
server_name, key_id, key_bytes, ts_valid_until_ms = row
|
2019-06-10 07:31:05 -06:00
|
|
|
|
|
|
|
if ts_valid_until_ms is None:
|
|
|
|
# Old keys may be stored with a ts_valid_until_ms of null,
|
|
|
|
# in which case we treat this as if it was set to `0`, i.e.
|
|
|
|
# it won't match key requests that define a minimum
|
|
|
|
# `ts_valid_until_ms`.
|
|
|
|
ts_valid_until_ms = 0
|
|
|
|
|
2019-04-03 11:10:24 -06:00
|
|
|
res = FetchKeyResult(
|
|
|
|
verify_key=decode_verify_key_bytes(key_id, bytes(key_bytes)),
|
|
|
|
valid_until_ts=ts_valid_until_ms,
|
2019-04-08 07:51:07 -06:00
|
|
|
)
|
2019-04-03 11:10:24 -06:00
|
|
|
keys[(server_name, key_id)] = res
|
2019-04-08 07:51:07 -06:00
|
|
|
|
|
|
|
def _txn(txn):
|
|
|
|
for batch in batch_iter(server_name_and_key_ids, 50):
|
|
|
|
_get_keys(txn, batch)
|
|
|
|
return keys
|
|
|
|
|
|
|
|
return self.runInteraction("get_server_verify_keys", _txn)
|
2014-09-30 08:15:10 -06:00
|
|
|
|
2019-05-23 04:45:39 -06:00
|
|
|
def store_server_verify_keys(self, from_server, ts_added_ms, verify_keys):
|
|
|
|
"""Stores NACL verification keys for remote servers.
|
2014-08-28 11:19:47 -06:00
|
|
|
Args:
|
2019-05-23 04:45:39 -06:00
|
|
|
from_server (str): Where the verification keys were looked up
|
|
|
|
ts_added_ms (int): The time to record that the key was added
|
2019-04-03 11:10:24 -06:00
|
|
|
verify_keys (iterable[tuple[str, str, FetchKeyResult]]):
|
2019-05-23 04:45:39 -06:00
|
|
|
keys to be stored. Each entry is a triplet of
|
|
|
|
(server_name, key_id, key).
|
2014-08-28 11:19:47 -06:00
|
|
|
"""
|
2019-05-23 04:45:39 -06:00
|
|
|
key_values = []
|
|
|
|
value_values = []
|
|
|
|
invalidations = []
|
2019-04-03 11:10:24 -06:00
|
|
|
for server_name, key_id, fetch_result in verify_keys:
|
2019-05-23 04:45:39 -06:00
|
|
|
key_values.append((server_name, key_id))
|
|
|
|
value_values.append(
|
|
|
|
(
|
|
|
|
from_server,
|
|
|
|
ts_added_ms,
|
2019-04-03 11:10:24 -06:00
|
|
|
fetch_result.valid_until_ts,
|
|
|
|
db_binary_type(fetch_result.verify_key.encode()),
|
2019-05-23 04:45:39 -06:00
|
|
|
)
|
2017-09-19 18:32:42 -06:00
|
|
|
)
|
2019-04-08 07:51:07 -06:00
|
|
|
# invalidate takes a tuple corresponding to the params of
|
|
|
|
# _get_server_verify_key. _get_server_verify_key only takes one
|
|
|
|
# param, which is itself the 2-tuple (server_name, key_id).
|
2019-05-23 04:45:39 -06:00
|
|
|
invalidations.append((server_name, key_id))
|
|
|
|
|
|
|
|
def _invalidate(res):
|
|
|
|
f = self._get_server_verify_key.invalidate
|
|
|
|
for i in invalidations:
|
2019-06-20 03:32:02 -06:00
|
|
|
f((i,))
|
2019-05-23 04:45:39 -06:00
|
|
|
return res
|
|
|
|
|
|
|
|
return self.runInteraction(
|
|
|
|
"store_server_verify_keys",
|
|
|
|
self._simple_upsert_many_txn,
|
|
|
|
table="server_signature_keys",
|
|
|
|
key_names=("server_name", "key_id"),
|
|
|
|
key_values=key_values,
|
|
|
|
value_names=(
|
|
|
|
"from_server",
|
|
|
|
"ts_added_ms",
|
2019-04-03 11:10:24 -06:00
|
|
|
"ts_valid_until_ms",
|
2019-05-23 04:45:39 -06:00
|
|
|
"verify_key",
|
|
|
|
),
|
|
|
|
value_values=value_values,
|
|
|
|
).addCallback(_invalidate)
|
2015-04-15 09:58:35 -06:00
|
|
|
|
2019-04-03 03:07:29 -06:00
|
|
|
def store_server_keys_json(
|
|
|
|
self, server_name, key_id, from_server, ts_now_ms, ts_expires_ms, key_json_bytes
|
|
|
|
):
|
2015-04-15 09:58:35 -06:00
|
|
|
"""Stores the JSON bytes for a set of keys from a server
|
|
|
|
The JSON should be signed by the originating server, the intermediate
|
|
|
|
server, and by this server. Updates the value for the
|
|
|
|
(server_name, key_id, from_server) triplet if one already existed.
|
|
|
|
Args:
|
|
|
|
server_name (str): The name of the server.
|
|
|
|
key_id (str): The identifer of the key this JSON is for.
|
|
|
|
from_server (str): The server this JSON was fetched from.
|
|
|
|
ts_now_ms (int): The time now in milliseconds.
|
|
|
|
ts_valid_until_ms (int): The time when this json stops being valid.
|
|
|
|
key_json (bytes): The encoded JSON.
|
|
|
|
"""
|
2015-04-30 08:21:31 -06:00
|
|
|
return self._simple_upsert(
|
2015-04-15 09:58:35 -06:00
|
|
|
table="server_keys_json",
|
2015-04-30 08:21:31 -06:00
|
|
|
keyvalues={
|
|
|
|
"server_name": server_name,
|
|
|
|
"key_id": key_id,
|
|
|
|
"from_server": from_server,
|
|
|
|
},
|
2015-04-15 09:58:35 -06:00
|
|
|
values={
|
|
|
|
"server_name": server_name,
|
|
|
|
"key_id": key_id,
|
|
|
|
"from_server": from_server,
|
|
|
|
"ts_added_ms": ts_now_ms,
|
2015-04-22 07:21:08 -06:00
|
|
|
"ts_valid_until_ms": ts_expires_ms,
|
2018-05-29 09:42:43 -06:00
|
|
|
"key_json": db_binary_type(key_json_bytes),
|
2015-04-15 09:58:35 -06:00
|
|
|
},
|
2015-06-26 02:52:24 -06:00
|
|
|
desc="store_server_keys_json",
|
2015-04-15 09:58:35 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_server_keys_json(self, server_keys):
|
|
|
|
"""Retrive the key json for a list of server_keys and key ids.
|
|
|
|
If no keys are found for a given server, key_id and source then
|
2015-04-22 07:21:08 -06:00
|
|
|
that server, key_id, and source triplet entry will be an empty list.
|
|
|
|
The JSON is returned as a byte array so that it can be efficiently
|
|
|
|
used in an HTTP response.
|
2015-04-15 09:58:35 -06:00
|
|
|
Args:
|
|
|
|
server_keys (list): List of (server_name, key_id, source) triplets.
|
|
|
|
Returns:
|
2019-04-04 11:54:03 -06:00
|
|
|
Deferred[dict[Tuple[str, str, str|None], list[dict]]]:
|
|
|
|
Dict mapping (server_name, key_id, source) triplets to lists of dicts
|
2015-04-15 09:58:35 -06:00
|
|
|
"""
|
2019-04-03 03:07:29 -06:00
|
|
|
|
2015-04-15 09:58:35 -06:00
|
|
|
def _get_server_keys_json_txn(txn):
|
|
|
|
results = {}
|
|
|
|
for server_name, key_id, from_server in server_keys:
|
2015-04-22 07:21:08 -06:00
|
|
|
keyvalues = {"server_name": server_name}
|
|
|
|
if key_id is not None:
|
|
|
|
keyvalues["key_id"] = key_id
|
|
|
|
if from_server is not None:
|
|
|
|
keyvalues["from_server"] = from_server
|
|
|
|
rows = self._simple_select_list_txn(
|
|
|
|
txn,
|
|
|
|
"server_keys_json",
|
|
|
|
keyvalues=keyvalues,
|
|
|
|
retcols=(
|
|
|
|
"key_id",
|
|
|
|
"from_server",
|
|
|
|
"ts_added_ms",
|
|
|
|
"ts_valid_until_ms",
|
|
|
|
"key_json",
|
|
|
|
),
|
2015-04-15 09:58:35 -06:00
|
|
|
)
|
|
|
|
results[(server_name, key_id, from_server)] = rows
|
|
|
|
return results
|
2019-04-03 03:07:29 -06:00
|
|
|
|
|
|
|
return self.runInteraction("get_server_keys_json", _get_server_keys_json_txn)
|