Skip to content

Commit

Permalink
Remove previous credential with the same name
Browse files Browse the repository at this point in the history
Needed due to the introduced overwrite protection
  • Loading branch information
szszszsz committed Aug 19, 2023
1 parent fff5032 commit 6c783fc
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions pynitrokey/test_secrets_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hmac
import logging
import time
from contextlib import suppress
from datetime import timedelta
from os import environ, wait
from sys import stderr
Expand Down Expand Up @@ -288,11 +289,13 @@ def test_reverse_hotp_failure(secretsAppRaw):
assert not secretsApp.verify_code(CREDID, 10**5)
helper_wait_after_failed_hotp_verification_request()

secretsApp.delete(CREDID)
secretsApp.register(CREDID, secretb, digits=7, kind=Kind.Hotp, algo=Algorithm.Sha1)
with pytest.raises(SecretsAppException, match="ConditionsOfUseNotSatisfied"):
assert not secretsApp.verify_code(CREDID, 10**6)
helper_wait_after_failed_hotp_verification_request()

secretsApp.delete(CREDID)
secretsApp.register(CREDID, secretb, digits=8, kind=Kind.Hotp, algo=Algorithm.Sha1)
with pytest.raises(SecretsAppException, match="ConditionsOfUseNotSatisfied"):
assert not secretsApp.verify_code(CREDID, 10**7)
Expand Down Expand Up @@ -642,8 +645,10 @@ def test_too_long_message2(secretsAppRaw):
for i in range(1, 255, 5):
secretsApp.logfn(f"Testing secret length {i} bytes")
try:
secretsApp.register("C", too_long_name[:i], DIGITS, kind=Kind.Hotp)
codes.append(secretsApp.calculate("C"))
with suppress(SecretsAppException):
secretsAppRaw.delete(CREDID)
secretsApp.register(CREDID, too_long_name[:i], DIGITS, kind=Kind.Hotp)
codes.append(secretsApp.calculate(CREDID))
except Exception:
break
assert i >= 40, "Maximum secret length should be at least 320 bits"
Expand Down Expand Up @@ -1497,12 +1502,17 @@ def test_hmac_low_level(secretsAppRaw):
"1c e3 0f d7 8d 20 dc fa 40 b5 0c 18 77 9a fb 0f 02 28 8d b7".replace(" ", "")
)
for slot_name in [b"HmacSlot2", b"HmacSlot1"]:
with suppress(SecretsAppException):
secretsAppRaw.delete(slot_name)
secretsAppRaw.register(
slot_name,
secret=secret,
kind=Kind.Hmac,
)

with suppress(SecretsAppException):
secretsAppRaw.delete(slot_name)

# Do not allow to register secret with different lengths than expected 20 bytes
for secret_len in [18, 21, 200]:
with pytest.raises(SecretsAppException, match="IncorrectDataParameter"):
Expand Down Expand Up @@ -1537,13 +1547,27 @@ def test_hmac_low_level(secretsAppRaw):
*/
"""

# The length of "1" is used by KeepassXC for test purposes. "63" is the maximum.
# Prepare HmacSlot2 again
slot_name = b"HmacSlot2"
with suppress(SecretsAppException):
secretsAppRaw.delete(slot_name)
secretsAppRaw.register(
slot_name,
secret=secret,
kind=Kind.Hmac,
)

# The length of "1" is used by KeepassXC for the purposes. "63" is the maximum.
# "64" should not work, as the last byte is always treated as the padding byte value.
for challenge_len in [1, 32, 63]:
challenge = b"c" * challenge_len
challenge_padded = helper_get_padded(challenge)
status, response_device = helper_send_receive_ins(
secretsAppRaw, YK_API_REQ, p1=slot, le=20, data_raw=challenge_padded
secretsAppRaw,
YK_API_REQ,
p1=YK_P1_CMD_HMAC_2,
le=20,
data_raw=challenge_padded,
)
response_lib = secretsAppRaw.get_response_for_secret(challenge, secret)
assert response_lib == response_device
Expand Down

0 comments on commit 6c783fc

Please sign in to comment.