mirror of
https://mau.dev/maunium/synapse.git
synced 2024-10-01 01:36:05 -04:00
Hash the same content covered by the signature when referencing previous PDUs rather than reusing the PDU content hashes
This commit is contained in:
parent
bb04447c44
commit
c8f996e29f
@ -24,15 +24,15 @@ from syutil.crypto.jsonsign import sign_json, verify_signed_json
|
|||||||
import hashlib
|
import hashlib
|
||||||
|
|
||||||
|
|
||||||
def hash_event_pdu(pdu, hash_algortithm=hashlib.sha256):
|
def add_event_pdu_content_hash(pdu, hash_algorithm=hashlib.sha256):
|
||||||
hashed = _compute_hash(pdu, hash_algortithm)
|
hashed = _compute_content_hash(pdu, hash_algorithm)
|
||||||
pdu.hashes[hashed.name] = encode_base64(hashed.digest())
|
pdu.hashes[hashed.name] = encode_base64(hashed.digest())
|
||||||
return pdu
|
return pdu
|
||||||
|
|
||||||
|
|
||||||
def check_event_pdu_hash(pdu, hash_algorithm=hashlib.sha256):
|
def check_event_pdu_content_hash(pdu, hash_algorithm=hashlib.sha256):
|
||||||
"""Check whether the hash for this PDU matches the contents"""
|
"""Check whether the hash for this PDU matches the contents"""
|
||||||
computed_hash = _compute_hash(pdu, hash_algortithm)
|
computed_hash = _compute_content_hash(pdu, hash_algortithm)
|
||||||
if computed_hash.name not in pdu.hashes:
|
if computed_hash.name not in pdu.hashes:
|
||||||
raise Exception("Algorithm %s not in hashes %s" % (
|
raise Exception("Algorithm %s not in hashes %s" % (
|
||||||
computed_hash.name, list(pdu.hashes)
|
computed_hash.name, list(pdu.hashes)
|
||||||
@ -45,7 +45,7 @@ def check_event_pdu_hash(pdu, hash_algorithm=hashlib.sha256):
|
|||||||
return message_hash_bytes == computed_hash.digest()
|
return message_hash_bytes == computed_hash.digest()
|
||||||
|
|
||||||
|
|
||||||
def _compute_hash(pdu, hash_algorithm):
|
def _compute_content_hash(pdu, hash_algorithm):
|
||||||
pdu_json = pdu.get_dict()
|
pdu_json = pdu.get_dict()
|
||||||
pdu_json.pop("meta", None)
|
pdu_json.pop("meta", None)
|
||||||
pdu_json.pop("signatures", None)
|
pdu_json.pop("signatures", None)
|
||||||
@ -54,6 +54,15 @@ def _compute_hash(pdu, hash_algorithm):
|
|||||||
return hash_algorithm(pdu_json_bytes)
|
return hash_algorithm(pdu_json_bytes)
|
||||||
|
|
||||||
|
|
||||||
|
def compute_pdu_event_reference_hash(pdu, hash_algorithm=hashlib.sha256):
|
||||||
|
tmp_pdu = Pdu(**pdu.get_dict())
|
||||||
|
tmp_pdu = prune_pdu(tmp_pdu)
|
||||||
|
pdu_json = tmp_pdu.get_dict()
|
||||||
|
pdu_json_bytes = encode_canonical_json(pdu_json)
|
||||||
|
hashed = hash_algorithm(pdu_json_bytes)
|
||||||
|
return (hashed.name, hashed.digest())
|
||||||
|
|
||||||
|
|
||||||
def sign_event_pdu(pdu, signature_name, signing_key):
|
def sign_event_pdu(pdu, signature_name, signing_key):
|
||||||
tmp_pdu = Pdu(**pdu.get_dict())
|
tmp_pdu = Pdu(**pdu.get_dict())
|
||||||
tmp_pdu = prune_pdu(tmp_pdu)
|
tmp_pdu = prune_pdu(tmp_pdu)
|
||||||
|
@ -14,7 +14,9 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from .units import Pdu
|
from .units import Pdu
|
||||||
from synapse.crypto.event_signing import hash_event_pdu, sign_event_pdu
|
from synapse.crypto.event_signing import (
|
||||||
|
add_event_pdu_content_hash, sign_event_pdu
|
||||||
|
)
|
||||||
|
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
@ -97,5 +99,5 @@ class PduCodec(object):
|
|||||||
kwargs["ts"] = int(self.clock.time_msec())
|
kwargs["ts"] = int(self.clock.time_msec())
|
||||||
|
|
||||||
pdu = Pdu(**kwargs)
|
pdu = Pdu(**kwargs)
|
||||||
pdu = hash_event_pdu(pdu)
|
pdu = add_event_pdu_content_hash(pdu)
|
||||||
return sign_event_pdu(pdu, self.server_name, self.signing_key)
|
return sign_event_pdu(pdu, self.server_name, self.signing_key)
|
||||||
|
@ -44,6 +44,8 @@ from .signatures import SignatureStore
|
|||||||
|
|
||||||
from syutil.base64util import decode_base64
|
from syutil.base64util import decode_base64
|
||||||
|
|
||||||
|
from synapse.crypto.event_signing import compute_pdu_event_reference_hash
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -165,7 +167,7 @@ class DataStore(RoomMemberStore, RoomStore,
|
|||||||
|
|
||||||
for hash_alg, hash_base64 in pdu.hashes.items():
|
for hash_alg, hash_base64 in pdu.hashes.items():
|
||||||
hash_bytes = decode_base64(hash_base64)
|
hash_bytes = decode_base64(hash_base64)
|
||||||
self._store_pdu_hash_txn(
|
self._store_pdu_content_hash_txn(
|
||||||
txn, pdu.pdu_id, pdu.origin, hash_alg, hash_bytes,
|
txn, pdu.pdu_id, pdu.origin, hash_alg, hash_bytes,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -185,6 +187,11 @@ class DataStore(RoomMemberStore, RoomStore,
|
|||||||
hash_bytes
|
hash_bytes
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(ref_alg, ref_hash_bytes) = compute_pdu_event_reference_hash(pdu)
|
||||||
|
self._store_pdu_reference_hash_txn(
|
||||||
|
txn, pdu.pdu_id, pdu.origin, ref_alg, ref_hash_bytes
|
||||||
|
)
|
||||||
|
|
||||||
if pdu.is_state:
|
if pdu.is_state:
|
||||||
self._persist_state_txn(txn, pdu.prev_pdus, cols)
|
self._persist_state_txn(txn, pdu.prev_pdus, cols)
|
||||||
else:
|
else:
|
||||||
|
@ -69,7 +69,7 @@ class PduStore(SQLBaseStore):
|
|||||||
|
|
||||||
edge_hashes = self._get_prev_pdu_hashes_txn(txn, pdu_id, origin)
|
edge_hashes = self._get_prev_pdu_hashes_txn(txn, pdu_id, origin)
|
||||||
|
|
||||||
hashes = self._get_pdu_hashes_txn(txn, pdu_id, origin)
|
hashes = self._get_pdu_content_hashes_txn(txn, pdu_id, origin)
|
||||||
signatures = self._get_pdu_origin_signatures_txn(
|
signatures = self._get_pdu_origin_signatures_txn(
|
||||||
txn, pdu_id, origin
|
txn, pdu_id, origin
|
||||||
)
|
)
|
||||||
@ -317,7 +317,7 @@ class PduStore(SQLBaseStore):
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
for pdu_id, origin, depth in txn.fetchall():
|
for pdu_id, origin, depth in txn.fetchall():
|
||||||
hashes = self._get_pdu_hashes_txn(txn, pdu_id, origin)
|
hashes = self._get_pdu_reference_hashes_txn(txn, pdu_id, origin)
|
||||||
sha256_bytes = hashes["sha256"]
|
sha256_bytes = hashes["sha256"]
|
||||||
prev_hashes = {"sha256": encode_base64(sha256_bytes)}
|
prev_hashes = {"sha256": encode_base64(sha256_bytes)}
|
||||||
results.append((pdu_id, origin, prev_hashes, depth))
|
results.append((pdu_id, origin, prev_hashes, depth))
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS pdu_hashes (
|
CREATE TABLE IF NOT EXISTS pdu_content_hashes (
|
||||||
pdu_id TEXT,
|
pdu_id TEXT,
|
||||||
origin TEXT,
|
origin TEXT,
|
||||||
algorithm TEXT,
|
algorithm TEXT,
|
||||||
@ -21,7 +21,21 @@ CREATE TABLE IF NOT EXISTS pdu_hashes (
|
|||||||
CONSTRAINT uniqueness UNIQUE (pdu_id, origin, algorithm)
|
CONSTRAINT uniqueness UNIQUE (pdu_id, origin, algorithm)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE INDEX IF NOT EXISTS pdu_hashes_id ON pdu_hashes (pdu_id, origin);
|
CREATE INDEX IF NOT EXISTS pdu_content_hashes_id ON pdu_content_hashes (
|
||||||
|
pdu_id, origin
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS pdu_reference_hashes (
|
||||||
|
pdu_id TEXT,
|
||||||
|
origin TEXT,
|
||||||
|
algorithm TEXT,
|
||||||
|
hash BLOB,
|
||||||
|
CONSTRAINT uniqueness UNIQUE (pdu_id, origin, algorithm)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS pdu_reference_hashes_id ON pdu_reference_hashes (
|
||||||
|
pdu_id, origin
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS pdu_origin_signatures (
|
CREATE TABLE IF NOT EXISTS pdu_origin_signatures (
|
||||||
pdu_id TEXT,
|
pdu_id TEXT,
|
||||||
|
@ -21,7 +21,7 @@ from twisted.internet import defer
|
|||||||
class SignatureStore(SQLBaseStore):
|
class SignatureStore(SQLBaseStore):
|
||||||
"""Persistence for PDU signatures and hashes"""
|
"""Persistence for PDU signatures and hashes"""
|
||||||
|
|
||||||
def _get_pdu_hashes_txn(self, txn, pdu_id, origin):
|
def _get_pdu_content_hashes_txn(self, txn, pdu_id, origin):
|
||||||
"""Get all the hashes for a given PDU.
|
"""Get all the hashes for a given PDU.
|
||||||
Args:
|
Args:
|
||||||
txn (cursor):
|
txn (cursor):
|
||||||
@ -32,13 +32,14 @@ class SignatureStore(SQLBaseStore):
|
|||||||
"""
|
"""
|
||||||
query = (
|
query = (
|
||||||
"SELECT algorithm, hash"
|
"SELECT algorithm, hash"
|
||||||
" FROM pdu_hashes"
|
" FROM pdu_content_hashes"
|
||||||
" WHERE pdu_id = ? and origin = ?"
|
" WHERE pdu_id = ? and origin = ?"
|
||||||
)
|
)
|
||||||
txn.execute(query, (pdu_id, origin))
|
txn.execute(query, (pdu_id, origin))
|
||||||
return dict(txn.fetchall())
|
return dict(txn.fetchall())
|
||||||
|
|
||||||
def _store_pdu_hash_txn(self, txn, pdu_id, origin, algorithm, hash_bytes):
|
def _store_pdu_content_hash_txn(self, txn, pdu_id, origin, algorithm,
|
||||||
|
hash_bytes):
|
||||||
"""Store a hash for a PDU
|
"""Store a hash for a PDU
|
||||||
Args:
|
Args:
|
||||||
txn (cursor):
|
txn (cursor):
|
||||||
@ -47,13 +48,48 @@ class SignatureStore(SQLBaseStore):
|
|||||||
algorithm (str): Hashing algorithm.
|
algorithm (str): Hashing algorithm.
|
||||||
hash_bytes (bytes): Hash function output bytes.
|
hash_bytes (bytes): Hash function output bytes.
|
||||||
"""
|
"""
|
||||||
self._simple_insert_txn(txn, "pdu_hashes", {
|
self._simple_insert_txn(txn, "pdu_content_hashes", {
|
||||||
"pdu_id": pdu_id,
|
"pdu_id": pdu_id,
|
||||||
"origin": origin,
|
"origin": origin,
|
||||||
"algorithm": algorithm,
|
"algorithm": algorithm,
|
||||||
"hash": buffer(hash_bytes),
|
"hash": buffer(hash_bytes),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
def _get_pdu_reference_hashes_txn(self, txn, pdu_id, origin):
|
||||||
|
"""Get all the hashes for a given PDU.
|
||||||
|
Args:
|
||||||
|
txn (cursor):
|
||||||
|
pdu_id (str): Id for the PDU.
|
||||||
|
origin (str): origin of the PDU.
|
||||||
|
Returns:
|
||||||
|
A dict of algorithm -> hash.
|
||||||
|
"""
|
||||||
|
query = (
|
||||||
|
"SELECT algorithm, hash"
|
||||||
|
" FROM pdu_reference_hashes"
|
||||||
|
" WHERE pdu_id = ? and origin = ?"
|
||||||
|
)
|
||||||
|
txn.execute(query, (pdu_id, origin))
|
||||||
|
return dict(txn.fetchall())
|
||||||
|
|
||||||
|
def _store_pdu_reference_hash_txn(self, txn, pdu_id, origin, algorithm,
|
||||||
|
hash_bytes):
|
||||||
|
"""Store a hash for a PDU
|
||||||
|
Args:
|
||||||
|
txn (cursor):
|
||||||
|
pdu_id (str): Id for the PDU.
|
||||||
|
origin (str): origin of the PDU.
|
||||||
|
algorithm (str): Hashing algorithm.
|
||||||
|
hash_bytes (bytes): Hash function output bytes.
|
||||||
|
"""
|
||||||
|
self._simple_insert_txn(txn, "pdu_reference_hashes", {
|
||||||
|
"pdu_id": pdu_id,
|
||||||
|
"origin": origin,
|
||||||
|
"algorithm": algorithm,
|
||||||
|
"hash": buffer(hash_bytes),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
def _get_pdu_origin_signatures_txn(self, txn, pdu_id, origin):
|
def _get_pdu_origin_signatures_txn(self, txn, pdu_id, origin):
|
||||||
"""Get all the signatures for a given PDU.
|
"""Get all the signatures for a given PDU.
|
||||||
Args:
|
Args:
|
||||||
|
Loading…
Reference in New Issue
Block a user