2014-08-28 11:19:47 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket 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.
|
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import hashlib
|
|
|
|
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
|
|
|
|
2015-08-24 09:17:38 -06:00
|
|
|
from signedjson.key import decode_verify_key_bytes
|
2014-08-28 11:19:47 -06:00
|
|
|
|
2018-07-09 00:09:20 -06:00
|
|
|
import OpenSSL
|
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.util.caches.descriptors import cachedInlineCallbacks
|
|
|
|
|
|
|
|
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:
|
|
|
|
db_binary_type = buffer
|
|
|
|
else:
|
|
|
|
db_binary_type = memoryview
|
|
|
|
|
2014-10-30 05:10:17 -06:00
|
|
|
|
2014-08-28 11:19:47 -06:00
|
|
|
class KeyStore(SQLBaseStore):
|
|
|
|
"""Persistence for signature verification keys and tls X.509 certificates
|
|
|
|
"""
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_server_certificate(self, server_name):
|
|
|
|
"""Retrieve the TLS X.509 certificate for the given server
|
|
|
|
Args:
|
|
|
|
server_name (bytes): The name of the server.
|
|
|
|
Returns:
|
|
|
|
(OpenSSL.crypto.X509): The tls certificate.
|
|
|
|
"""
|
|
|
|
tls_certificate_bytes, = yield self._simple_select_one(
|
|
|
|
table="server_tls_certificates",
|
|
|
|
keyvalues={"server_name": server_name},
|
|
|
|
retcols=("tls_certificate",),
|
2016-02-03 09:22:35 -07:00
|
|
|
desc="get_server_certificate",
|
2014-08-28 11:19:47 -06:00
|
|
|
)
|
|
|
|
tls_certificate = OpenSSL.crypto.load_certificate(
|
|
|
|
OpenSSL.crypto.FILETYPE_ASN1, tls_certificate_bytes,
|
|
|
|
)
|
|
|
|
defer.returnValue(tls_certificate)
|
|
|
|
|
2014-09-30 08:15:10 -06:00
|
|
|
def store_server_certificate(self, server_name, from_server, time_now_ms,
|
2014-08-28 11:19:47 -06:00
|
|
|
tls_certificate):
|
|
|
|
"""Stores the TLS X.509 certificate for the given server
|
|
|
|
Args:
|
2014-09-30 08:15:10 -06:00
|
|
|
server_name (str): The name of the server.
|
|
|
|
from_server (str): Where the certificate was looked up
|
|
|
|
time_now_ms (int): The time now in milliseconds
|
2014-08-28 11:19:47 -06:00
|
|
|
tls_certificate (OpenSSL.crypto.X509): The X.509 certificate.
|
|
|
|
"""
|
|
|
|
tls_certificate_bytes = OpenSSL.crypto.dump_certificate(
|
|
|
|
OpenSSL.crypto.FILETYPE_ASN1, tls_certificate
|
|
|
|
)
|
2014-09-30 08:15:10 -06:00
|
|
|
fingerprint = hashlib.sha256(tls_certificate_bytes).hexdigest()
|
2015-04-02 03:06:22 -06:00
|
|
|
return self._simple_upsert(
|
2014-08-28 11:19:47 -06:00
|
|
|
table="server_tls_certificates",
|
2015-04-02 03:06:22 -06:00
|
|
|
keyvalues={
|
2014-08-28 11:19:47 -06:00
|
|
|
"server_name": server_name,
|
2014-09-30 08:15:10 -06:00
|
|
|
"fingerprint": fingerprint,
|
2015-04-02 03:06:22 -06:00
|
|
|
},
|
|
|
|
values={
|
2014-09-30 08:15:10 -06:00
|
|
|
"from_server": from_server,
|
|
|
|
"ts_added_ms": time_now_ms,
|
2018-05-29 09:42:43 -06:00
|
|
|
"tls_certificate": db_binary_type(tls_certificate_bytes),
|
2014-08-28 11:19:47 -06:00
|
|
|
},
|
2015-04-02 03:06:22 -06:00
|
|
|
desc="store_server_certificate",
|
2014-08-28 11:19:47 -06:00
|
|
|
)
|
|
|
|
|
2015-07-27 06:57:29 -06:00
|
|
|
@cachedInlineCallbacks()
|
2016-07-27 08:51:43 -06:00
|
|
|
def _get_server_verify_key(self, server_name, key_id):
|
|
|
|
verify_key_bytes = yield self._simple_select_one_onecol(
|
2015-06-26 02:52:24 -06:00
|
|
|
table="server_signature_keys",
|
|
|
|
keyvalues={
|
|
|
|
"server_name": server_name,
|
2016-07-27 08:51:43 -06:00
|
|
|
"key_id": key_id,
|
2015-06-26 02:52:24 -06:00
|
|
|
},
|
2016-07-27 08:51:43 -06:00
|
|
|
retcol="verify_key",
|
|
|
|
desc="_get_server_verify_key",
|
|
|
|
allow_none=True,
|
2015-06-26 02:52:24 -06:00
|
|
|
)
|
|
|
|
|
2016-07-27 08:51:43 -06:00
|
|
|
if verify_key_bytes:
|
|
|
|
defer.returnValue(decode_verify_key_bytes(
|
2018-05-05 14:47:18 -06:00
|
|
|
key_id, bytes(verify_key_bytes)
|
2016-07-27 08:51:43 -06:00
|
|
|
))
|
2015-06-26 02:52:24 -06:00
|
|
|
|
2014-08-28 11:19:47 -06:00
|
|
|
@defer.inlineCallbacks
|
2014-09-30 08:15:10 -06:00
|
|
|
def get_server_verify_keys(self, server_name, key_ids):
|
|
|
|
"""Retrieve the NACL verification key for a given server for the given
|
|
|
|
key_ids
|
2014-08-28 11:19:47 -06:00
|
|
|
Args:
|
2014-09-30 08:15:10 -06:00
|
|
|
server_name (str): The name of the server.
|
2017-03-20 09:34:35 -06:00
|
|
|
key_ids (iterable[str]): key_ids to try and look up.
|
2014-08-28 11:19:47 -06:00
|
|
|
Returns:
|
2017-03-20 09:34:35 -06:00
|
|
|
Deferred: resolves to dict[str, VerifyKey]: map from
|
|
|
|
key_id to verification key.
|
2014-08-28 11:19:47 -06:00
|
|
|
"""
|
2016-07-27 08:51:43 -06:00
|
|
|
keys = {}
|
|
|
|
for key_id in key_ids:
|
|
|
|
key = yield self._get_server_verify_key(server_name, key_id)
|
|
|
|
if key:
|
|
|
|
keys[key_id] = key
|
|
|
|
defer.returnValue(keys)
|
2014-09-30 08:15:10 -06:00
|
|
|
|
|
|
|
def store_server_verify_key(self, server_name, from_server, time_now_ms,
|
|
|
|
verify_key):
|
2014-08-28 11:19:47 -06:00
|
|
|
"""Stores a NACL verification key for the given server.
|
|
|
|
Args:
|
2014-09-30 08:15:10 -06:00
|
|
|
server_name (str): The name of the server.
|
|
|
|
from_server (str): Where the verification key was looked up
|
2017-09-19 18:32:42 -06:00
|
|
|
time_now_ms (int): The time now in milliseconds
|
|
|
|
verify_key (nacl.signing.VerifyKey): The NACL verify key.
|
2014-08-28 11:19:47 -06:00
|
|
|
"""
|
2017-09-19 18:32:42 -06:00
|
|
|
key_id = "%s:%s" % (verify_key.alg, verify_key.version)
|
|
|
|
|
2018-09-06 16:56:47 -06:00
|
|
|
# XXX fix this to not need a lock (#3819)
|
2017-09-19 18:32:42 -06:00
|
|
|
def _txn(txn):
|
|
|
|
self._simple_upsert_txn(
|
|
|
|
txn,
|
|
|
|
table="server_signature_keys",
|
|
|
|
keyvalues={
|
|
|
|
"server_name": server_name,
|
|
|
|
"key_id": key_id,
|
|
|
|
},
|
|
|
|
values={
|
|
|
|
"from_server": from_server,
|
|
|
|
"ts_added_ms": time_now_ms,
|
2018-05-29 09:42:43 -06:00
|
|
|
"verify_key": db_binary_type(verify_key.encode()),
|
2017-09-19 18:32:42 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
txn.call_after(
|
|
|
|
self._get_server_verify_key.invalidate,
|
|
|
|
(server_name, key_id)
|
|
|
|
)
|
|
|
|
|
|
|
|
return self.runInteraction("store_server_verify_key", _txn)
|
2015-04-15 09:58:35 -06:00
|
|
|
|
|
|
|
def store_server_keys_json(self, server_name, key_id, from_server,
|
|
|
|
ts_now_ms, ts_expires_ms, key_json_bytes):
|
|
|
|
"""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:
|
|
|
|
Dict mapping (server_name, key_id, source) triplets to dicts with
|
|
|
|
"ts_valid_until_ms" and "key_json" keys.
|
|
|
|
"""
|
|
|
|
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
|
2015-04-22 07:21:08 -06:00
|
|
|
return self.runInteraction(
|
2015-04-15 09:58:35 -06:00
|
|
|
"get_server_keys_json", _get_server_keys_json_txn
|
|
|
|
)
|