Skip to content

Commit

Permalink
Merge pull request #496 from Nitrokey/log
Browse files Browse the repository at this point in the history
nk3 secrets: Log payload length
  • Loading branch information
robin-nitrokey authored Jan 23, 2024
2 parents 89f4a43 + 17300b3 commit 6fdaf76
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions pynitrokey/nk3/secrets_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,7 @@ def _send_receive(
return self._send_receive_inner(bytes_data, log_info=f"{ins}")

def _send_receive_inner(self, data: bytes, log_info: str = "") -> bytes:
self.logfn(
f"Sending {log_info if log_info else ''} {data.hex() if data else data!r}"
)
self.logfn(f"Sending {log_info if log_info else ''} (data: {len(data)} bytes)")

try:
result = self.dev.otp(data=data)
Expand All @@ -358,18 +356,14 @@ def _send_receive_inner(self, data: bytes, log_info: str = "") -> bytes:
raise

status_bytes, result = result[:2], result[2:]
self.logfn(
f"Received [{status_bytes.hex()}] {result.hex() if result else result!r}"
)
self.logfn(f"Received [{status_bytes.hex()}] (data: {len(result)} bytes)")

log_multipacket = False
data_final = result
MORE_DATA_STATUS_BYTE = 0x61
while status_bytes[0] == MORE_DATA_STATUS_BYTE:
if log_multipacket:
self.logfn(
f"Got RemainingData status: [{status_bytes.hex()}] {result.hex() if result else result!r}"
)
self.logfn(f"Got RemainingData status: [{status_bytes.hex()}]")
log_multipacket = True
ins_b, p1, p2 = self._encode_command(Instruction.SendRemaining)
bytes_data = iso7816_compose(ins_b, p1, p2)
Expand All @@ -380,9 +374,7 @@ def _send_receive_inner(self, data: bytes, log_info: str = "") -> bytes:
raise
# Data order is different here than in APDU - SW is first, then the data if any
status_bytes, result = result[:2], result[2:]
self.logfn(
f"Received [{status_bytes.hex()}] {result.hex() if result else result!r}"
)
self.logfn(f"Received [{status_bytes.hex()}] (data: {len(result)} bytes)")
if status_bytes[0] in [0x90, MORE_DATA_STATUS_BYTE]:
data_final += result

Expand All @@ -391,15 +383,15 @@ def _send_receive_inner(self, data: bytes, log_info: str = "") -> bytes:

if log_multipacket:
self.logfn(
f"Received final data: [{status_bytes.hex()}] {data_final.hex() if data_final else data_final!r}"
f"Received final data: [{status_bytes.hex()}] (data: {len(data_final)} bytes)"
)

if data_final:
try:
self.logfn(
f"Decoded received: {[e.data for e in tlv8.decode(data_final)]}"
)
tlv8.decode(data_final)
self.logfn("TLV-decoding of data successful")
except Exception:
self.logfn("TLV-decoding of data failed")
pass

return data_final
Expand Down Expand Up @@ -481,7 +473,7 @@ def get_credential(self, cred_id: bytes) -> PasswordSafeEntry:
for e in resd:
# e: tlv8.Entry
res[e.type_id] = e.data
self.logfn(f"{hex(e.type_id)} {hex(len(e.data))} {e.data.hex()}")
self.logfn(f"{hex(e.type_id)} {hex(len(e.data))}")
p = PasswordSafeEntry(
login=res.get(Tag.PwsLogin.value),
password=res.get(Tag.PwsPassword.value),
Expand Down

0 comments on commit 6fdaf76

Please sign in to comment.