Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
make black happy
Browse files Browse the repository at this point in the history
  • Loading branch information
uhoreg committed Jul 22, 2019
1 parent 269203a commit 9d38b5e
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 178 deletions.
147 changes: 69 additions & 78 deletions synapse/handlers/e2e_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,32 +538,30 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
self_device_ids = list(self_signatures.keys())
try:
# get our self-signing key to verify the signatures
self_signing_key, self_signing_key_id, self_signing_verify_key \
= yield self._get_e2e_cross_signing_verify_key(
user_id, "self_signing"
)
self_signing_key, self_signing_key_id, self_signing_verify_key = yield self._get_e2e_cross_signing_verify_key(
user_id, "self_signing"
)

# get our master key, since it may be signed
master_key, master_key_id, master_verify_key \
= yield self._get_e2e_cross_signing_verify_key(
user_id, "master"
)
master_key, master_key_id, master_verify_key = yield self._get_e2e_cross_signing_verify_key(
user_id, "master"
)

# fetch our stored devices. This is used to 1. verify
# signatures on the master key, and 2. to can compare with what
# was sent if the device was signed
devices = yield self.store.get_e2e_device_keys([(user_id, None)])

if user_id not in devices:
raise SynapseError(
404, "No device keys found", Codes.NOT_FOUND
)
raise SynapseError(404, "No device keys found", Codes.NOT_FOUND)

devices = devices[user_id]
for device_id, device in self_signatures.items():
try:
if ("signatures" not in device or
user_id not in device["signatures"]):
if (
"signatures" not in device
or user_id not in device["signatures"]
):
# no signature was sent
raise SynapseError(
400, "Invalid signature", Codes.INVALID_SIGNATURE
Expand All @@ -581,15 +579,21 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
# signature list. (In practice, we're likely to
# only have only one signature anyways.)
master_key_signature_list = []
for signing_key_id, signature in device["signatures"][user_id].items():
for signing_key_id, signature in device["signatures"][
user_id
].items():
alg, signing_device_id = signing_key_id.split(":", 1)
if (signing_device_id not in devices or
signing_key_id not in
devices[signing_device_id]["keys"]["keys"]):
if (
signing_device_id not in devices
or signing_key_id
not in devices[signing_device_id]["keys"]["keys"]
):
# signed by an unknown device, or the
# device does not have the key
raise SynapseError(
400, "Invalid signature", Codes.INVALID_SIGNATURE
400,
"Invalid signature",
Codes.INVALID_SIGNATURE,
)

sigs = device["signatures"]
Expand All @@ -600,12 +604,12 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
master_key.pop("unsigned", None)

if master_key != device:
raise SynapseError(
400, "Key does not match"
)
raise SynapseError(400, "Key does not match")

# get the key and check the signature
pubkey = devices[signing_device_id]["keys"]["keys"][signing_key_id]
pubkey = devices[signing_device_id]["keys"]["keys"][
signing_key_id
]
verify_key = decode_verify_key_bytes(
signing_key_id, decode_base64(pubkey)
)
Expand All @@ -614,7 +618,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
verify_signed_json(device, user_id, verify_key)
except SignatureVerifyException:
raise SynapseError(
400, "Invalid signature", Codes.INVALID_SIGNATURE
400,
"Invalid signature",
Codes.INVALID_SIGNATURE,
)

master_key_signature_list.append(
Expand All @@ -636,11 +642,10 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
try:
stored_device = devices[device_id]["keys"]
except KeyError:
raise SynapseError(
404, "Unknown device", Codes.NOT_FOUND
)
if self_signing_key_id in stored_device.get("signatures", {}) \
.get(user_id, {}):
raise SynapseError(404, "Unknown device", Codes.NOT_FOUND)
if self_signing_key_id in stored_device.get(
"signatures", {}
).get(user_id, {}):
# we already have a signature on this device, so we
# can skip it, since it should be exactly the same
continue
Expand All @@ -654,8 +659,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
(self_signing_key_id, user_id, device_id, signature)
)
except SynapseError as e:
failures.setdefault(user_id, {})[device_id] \
= _exception_to_failure(e)
failures.setdefault(user_id, {})[
device_id
] = _exception_to_failure(e)
except SynapseError as e:
failures[user_id] = {
device: _exception_to_failure(e)
Expand All @@ -669,20 +675,18 @@ def upload_signatures_for_device_keys(self, user_id, signatures):

try:
# get our user-signing key to verify the signatures
user_signing_key, user_signing_key_id, user_signing_verify_key \
= yield self._get_e2e_cross_signing_verify_key(
user_id, "user_signing"
)
user_signing_key, user_signing_key_id, user_signing_verify_key = yield self._get_e2e_cross_signing_verify_key(
user_id, "user_signing"
)

for user, devicemap in signatures.items():
device_id = None
try:
# get the user's master key, to make sure it matches
# what was sent
stored_key, stored_key_id, _ \
= yield self._get_e2e_cross_signing_verify_key(
user, "master", user_id
)
stored_key, stored_key_id, _ = yield self._get_e2e_cross_signing_verify_key(
user, "master", user_id
)

# make sure that the user's master key is the one that
# was signed (and no others)
Expand All @@ -693,26 +697,25 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
device_id = None
logger.error(
"upload signature: wrong device: %s vs %s",
device, devicemap
)
raise SynapseError(
404, "Unknown device", Codes.NOT_FOUND
device,
devicemap,
)
raise SynapseError(404, "Unknown device", Codes.NOT_FOUND)
key = devicemap[device_id]
del devicemap[device_id]
if len(devicemap) > 0:
# other devices were signed -- mark those as failures
logger.error("upload signature: too many devices specified")
failure = _exception_to_failure(SynapseError(
404, "Unknown device", Codes.NOT_FOUND
))
failure = _exception_to_failure(
SynapseError(404, "Unknown device", Codes.NOT_FOUND)
)
failures[user] = {
device: failure
for device in devicemap.keys()
device: failure for device in devicemap.keys()
}

if user_signing_key_id in stored_key.get("signatures", {}) \
.get(user_id, {}):
if user_signing_key_id in stored_key.get("signatures", {}).get(
user_id, {}
):
# we already have the signature, so we can skip it
continue

Expand All @@ -729,17 +732,15 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
failure = _exception_to_failure(e)
if device_id is None:
failures[user] = {
device_id: failure
for device_id in devicemap.keys()
device_id: failure for device_id in devicemap.keys()
}
else:
failures.setdefault(user, {})[device_id] = failure
except SynapseError as e:
failure = _exception_to_failure(e)
for user, devicemap in signature.items():
failures[user] = {
device_id: failure
for device_id in devicemap.keys()
device_id: failure for device_id in devicemap.keys()
}

# store the signature, and send the appropriate notifications for sync
Expand All @@ -749,7 +750,9 @@ def upload_signatures_for_device_keys(self, user_id, signatures):
if len(self_device_ids):
yield self.device_handler.notify_device_update(user_id, self_device_ids)
if len(signed_users):
yield self.device_handler.notify_user_signature_update(user_id, signed_users)
yield self.device_handler.notify_user_signature_update(
user_id, signed_users
)

defer.returnValue({"failures": failures})

Expand All @@ -761,9 +764,7 @@ def _get_e2e_cross_signing_verify_key(self, user_id, key_type, from_user_id=None
if key is None:
logger.error("no %s key found for %s", key_type, user_id)
raise SynapseError(
404,
"No %s key found for %s" % (key_type, user_id),
Codes.NOT_FOUND
404, "No %s key found for %s" % (key_type, user_id), Codes.NOT_FOUND
)
key_id, verify_key = get_verify_key_from_cross_signing_key(key)
return key, key_id, verify_key
Expand Down Expand Up @@ -811,14 +812,13 @@ def _check_device_signature(user_id, verify_key, signed_device, stored_device):
key_id = "%s:%s" % (verify_key.alg, verify_key.version)

# make sure the device is signed
if ("signatures" not in signed_device or user_id not in signed_device["signatures"]
or key_id not in signed_device["signatures"][user_id]):
if (
"signatures" not in signed_device
or user_id not in signed_device["signatures"]
or key_id not in signed_device["signatures"][user_id]
):
logger.error("upload signature: user not found in signatures")
raise SynapseError(
400,
"Invalid signature",
Codes.INVALID_SIGNATURE
)
raise SynapseError(400, "Invalid signature", Codes.INVALID_SIGNATURE)

signature = signed_device["signatures"][user_id][key_id]

Expand All @@ -831,28 +831,19 @@ def _check_device_signature(user_id, verify_key, signed_device, stored_device):
if signed_device != stored_device:
logger.error(
"upload signatures: key does not match %s vs %s",
signed_device, stored_device
)
raise SynapseError(
400, "Key does not match",
signed_device,
stored_device,
)
raise SynapseError(400, "Key does not match")

# check the signature
signed_device["signatures"] = {
user_id: {
key_id: signature
}
}
signed_device["signatures"] = {user_id: {key_id: signature}}

try:
verify_signed_json(signed_device, user_id, verify_key)
except SignatureVerifyException:
logger.error("invalid signature on key")
raise SynapseError(
400,
"Invalid signature",
Codes.INVALID_SIGNATURE
)
raise SynapseError(400, "Invalid signature", Codes.INVALID_SIGNATURE)


def _exception_to_failure(e):
Expand Down
1 change: 1 addition & 0 deletions synapse/rest/client/v2_alpha/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ class SignaturesUploadServlet(RestServlet):
}
}
"""

PATTERNS = client_patterns("/keys/signatures/upload$")

def __init__(self, hs):
Expand Down
24 changes: 9 additions & 15 deletions synapse/storage/end_to_end_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def get_e2e_device_keys(
# add cross-signing signatures to the keys
if "signatures" in device_info:
for sig_user_id, sigs in device_info["signatures"].items():
device_info["keys"].setdefault("signatures", {}) \
.setdefault(sig_user_id, {}) \
.update(sigs)
device_info["keys"].setdefault("signatures", {}).setdefault(
sig_user_id, {}
).update(sigs)

defer.returnValue(results)

Expand Down Expand Up @@ -127,25 +127,19 @@ def _get_e2e_device_keys_txn(

# get signatures on the device
signature_sql = (
"SELECT * "
" FROM e2e_cross_signing_signatures "
" WHERE %s"
) % (
" OR ".join("(" + q + ")" for q in signature_query_clauses)
)
"SELECT * " " FROM e2e_cross_signing_signatures " " WHERE %s"
) % (" OR ".join("(" + q + ")" for q in signature_query_clauses))

txn.execute(signature_sql, signature_query_params)
rows = self.cursor_to_dict(txn)

for row in rows:
target_user_id = row["target_user_id"]
target_device_id = row["target_device_id"]
if target_user_id in result \
and target_device_id in result[target_user_id]:
result[target_user_id][target_device_id] \
.setdefault("signatures", {}) \
.setdefault(row["user_id"], {})[row["key_id"]] \
= row["signature"]
if target_user_id in result and target_device_id in result[target_user_id]:
result[target_user_id][target_device_id].setdefault(
"signatures", {}
).setdefault(row["user_id"], {})[row["key_id"]] = row["signature"]

return result

Expand Down
Loading

0 comments on commit 9d38b5e

Please sign in to comment.