1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-07 08:38:46 +00:00

core/webauthn: unify signatures of Credential.from_bytes and friends

This commit is contained in:
matejcik 2019-11-06 13:56:52 +01:00
parent 1397bbfeb5
commit 28d30ffd2f
2 changed files with 11 additions and 10 deletions

View File

@ -54,7 +54,7 @@ class Credential:
return storage.device.next_u2f_counter() or 0 return storage.device.next_u2f_counter() or 0
@staticmethod @staticmethod
def from_bytes(data: bytes, rp_id_hash: bytes) -> Optional["Credential"]: def from_bytes(data: bytes, rp_id_hash: bytes) -> "Credential":
try: try:
return Fido2Credential.from_cred_id(data, rp_id_hash) return Fido2Credential.from_cred_id(data, rp_id_hash)
except Exception: except Exception:
@ -274,11 +274,9 @@ class U2fCredential(Credential):
return app_name return app_name
@staticmethod @staticmethod
def from_key_handle( def from_key_handle(key_handle: bytes, rp_id_hash: bytes) -> "U2fCredential":
key_handle: bytes, rp_id_hash: bytes
) -> Optional["U2fCredential"]:
if len(key_handle) != _KEY_HANDLE_LENGTH: if len(key_handle) != _KEY_HANDLE_LENGTH:
return None raise ValueError # key length mismatch
# check the keyHandle and generate the signing key # check the keyHandle and generate the signing key
node = U2fCredential._node_from_key_handle(rp_id_hash, key_handle, "<8L") node = U2fCredential._node_from_key_handle(rp_id_hash, key_handle, "<8L")
@ -289,7 +287,7 @@ class U2fCredential(Credential):
node = U2fCredential._node_from_key_handle(rp_id_hash, key_handle, ">8L") node = U2fCredential._node_from_key_handle(rp_id_hash, key_handle, ">8L")
if node is None: if node is None:
# specific error logged in msg_authenticate_genkey # specific error logged in msg_authenticate_genkey
return None raise ValueError # failed to parse key handle in either direction
cred = U2fCredential() cred = U2fCredential()
cred.id = key_handle cred.id = key_handle

View File

@ -1162,8 +1162,9 @@ def msg_authenticate(req: Msg, dialog_mgr: DialogManager) -> Cmd:
khlen = req.data[_REQ_CMD_AUTHENTICATE_KHLEN] khlen = req.data[_REQ_CMD_AUTHENTICATE_KHLEN]
auth = overlay_struct(req.data, req_cmd_authenticate(khlen)) auth = overlay_struct(req.data, req_cmd_authenticate(khlen))
cred = Credential.from_bytes(auth.keyHandle, bytes(auth.appId)) try:
if cred is None: cred = Credential.from_bytes(auth.keyHandle, bytes(auth.appId))
except Exception:
# specific error logged in msg_authenticate_genkey # specific error logged in msg_authenticate_genkey
return msg_error(req.cid, _SW_WRONG_DATA) return msg_error(req.cid, _SW_WRONG_DATA)
@ -1264,9 +1265,11 @@ def credentials_from_descriptor_list(
credential_id = credential_descriptor["id"] credential_id = credential_descriptor["id"]
if not isinstance(credential_id, (bytes, bytearray)): if not isinstance(credential_id, (bytes, bytearray)):
raise TypeError raise TypeError
cred = Credential.from_bytes(credential_id, rp_id_hash) try:
if cred is not None: cred = Credential.from_bytes(credential_id, rp_id_hash)
cred_list.append(cred) cred_list.append(cred)
except Exception:
pass
return cred_list return cred_list