mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2025-05-09 18:55:03 -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
6 changed files with 84 additions and 16 deletions
|
@ -24,15 +24,15 @@ from syutil.crypto.jsonsign import sign_json, verify_signed_json
|
|||
import hashlib
|
||||
|
||||
|
||||
def hash_event_pdu(pdu, hash_algortithm=hashlib.sha256):
|
||||
hashed = _compute_hash(pdu, hash_algortithm)
|
||||
def add_event_pdu_content_hash(pdu, hash_algorithm=hashlib.sha256):
|
||||
hashed = _compute_content_hash(pdu, hash_algorithm)
|
||||
pdu.hashes[hashed.name] = encode_base64(hashed.digest())
|
||||
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"""
|
||||
computed_hash = _compute_hash(pdu, hash_algortithm)
|
||||
computed_hash = _compute_content_hash(pdu, hash_algortithm)
|
||||
if computed_hash.name not in pdu.hashes:
|
||||
raise Exception("Algorithm %s not in hashes %s" % (
|
||||
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()
|
||||
|
||||
|
||||
def _compute_hash(pdu, hash_algorithm):
|
||||
def _compute_content_hash(pdu, hash_algorithm):
|
||||
pdu_json = pdu.get_dict()
|
||||
pdu_json.pop("meta", None)
|
||||
pdu_json.pop("signatures", None)
|
||||
|
@ -54,6 +54,15 @@ def _compute_hash(pdu, hash_algorithm):
|
|||
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):
|
||||
tmp_pdu = Pdu(**pdu.get_dict())
|
||||
tmp_pdu = prune_pdu(tmp_pdu)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue