|
| 1 | +import secrets |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from fido2.ctap import CtapError |
| 5 | +from fido2.ctap2 import Config, ClientPin, PinProtocolV2 |
| 6 | +from parameterized import parameterized |
| 7 | + |
| 8 | +from .ctap_test import CTAPTestCase |
| 9 | + |
| 10 | + |
| 11 | +class SetMinPinTestCase(CTAPTestCase): |
| 12 | + |
| 13 | + cp: ClientPin |
| 14 | + pin: Optional[str] = None |
| 15 | + |
| 16 | + def setUp(self, install_params: Optional[bytes] = None) -> None: |
| 17 | + super().setUp(install_params=install_params) |
| 18 | + self.cp = ClientPin(self.ctap2) |
| 19 | + |
| 20 | + def get_cfg(self) -> Config: |
| 21 | + if self.pin is None: |
| 22 | + return Config(self.ctap2) |
| 23 | + uv = self.cp.get_pin_token(self.pin, |
| 24 | + permissions=ClientPin.PERMISSION.AUTHENTICATOR_CFG) |
| 25 | + return Config(self.ctap2, pin_uv_protocol=PinProtocolV2(), pin_uv_token=uv) |
| 26 | + |
| 27 | + def test_info_min_pin_length(self): |
| 28 | + info = self.ctap2.get_info() |
| 29 | + self.assertEqual(4, info.min_pin_length) |
| 30 | + |
| 31 | + def test_info_max_rps_for_setminpin(self): |
| 32 | + info = self.ctap2.get_info() |
| 33 | + self.assertEqual(0, info.max_rpids_for_min_pin) |
| 34 | + |
| 35 | + def test_setminpin_option(self): |
| 36 | + info = self.ctap2.get_info() |
| 37 | + self.assertEqual(True, info.options.get("setMinPINLength")) |
| 38 | + |
| 39 | + @parameterized.expand([ |
| 40 | + ("authenticated", True), |
| 41 | + ("unauthenticated", False), |
| 42 | + ]) |
| 43 | + def test_setminpin_visible_in_info(self, _, setpin: bool): |
| 44 | + if setpin: |
| 45 | + self.pin = secrets.token_hex(10) |
| 46 | + self.cp.set_pin(self.pin) |
| 47 | + |
| 48 | + self.get_cfg().set_min_pin_length(min_pin_length=8) |
| 49 | + |
| 50 | + info = self.ctap2.get_info() |
| 51 | + self.assertEqual(8, info.min_pin_length) |
| 52 | + |
| 53 | + def test_setminpin_requires_pin_when_set(self): |
| 54 | + self.pin = secrets.token_hex(10) |
| 55 | + self.cp.set_pin(self.pin) |
| 56 | + |
| 57 | + with self.assertRaises(CtapError) as e: |
| 58 | + Config(self.ctap2).set_min_pin_length(min_pin_length=2) |
| 59 | + |
| 60 | + self.assertEqual(CtapError.ERR.PUAT_REQUIRED, e.exception.code) |
| 61 | + |
| 62 | + @parameterized.expand([ |
| 63 | + (4, 3), |
| 64 | + (8, 7), |
| 65 | + (30, 4), |
| 66 | + ]) |
| 67 | + def test_setminpin_cannot_go_down(self, original_value, new_value): |
| 68 | + self.get_cfg().set_min_pin_length(min_pin_length=original_value) |
| 69 | + |
| 70 | + with self.assertRaises(CtapError) as e: |
| 71 | + self.get_cfg().set_min_pin_length(min_pin_length=new_value) |
| 72 | + |
| 73 | + self.assertEqual(CtapError.ERR.PIN_POLICY_VIOLATION, e.exception.code) |
| 74 | + |
| 75 | + def test_setminpin_overlong(self): |
| 76 | + with self.assertRaises(CtapError) as e: |
| 77 | + self.get_cfg().set_min_pin_length(min_pin_length=70) |
| 78 | + |
| 79 | + self.assertEqual(CtapError.ERR.PIN_POLICY_VIOLATION, e.exception.code) |
| 80 | + |
| 81 | + def test_four_ascii_chars(self): |
| 82 | + self.cp.set_pin("aaaa") |
| 83 | + |
| 84 | + def test_four_ascii_chars_rejected_when_length_increased(self): |
| 85 | + self.get_cfg().set_min_pin_length(min_pin_length=5) |
| 86 | + |
| 87 | + with self.assertRaises(CtapError) as e: |
| 88 | + self.cp.set_pin("aaaa") |
| 89 | + |
| 90 | + self.assertEqual(CtapError.ERR.PIN_POLICY_VIOLATION, e.exception.code) |
| 91 | + |
| 92 | + def test_four_multibyte_chars_rejected_when_length_increased(self): |
| 93 | + self.get_cfg().set_min_pin_length(min_pin_length=5) |
| 94 | + |
| 95 | + with self.assertRaises(CtapError) as e: |
| 96 | + self.cp.set_pin("✈✈✈✈") |
| 97 | + |
| 98 | + self.assertEqual(CtapError.ERR.PIN_POLICY_VIOLATION, e.exception.code) |
| 99 | + |
| 100 | + def test_five_multibyte_chars_accepted_when_length_increased(self): |
| 101 | + self.get_cfg().set_min_pin_length(min_pin_length=5) |
| 102 | + |
| 103 | + self.cp.set_pin("✈✈ä✈✈") |
| 104 | + |
| 105 | + def test_four_multibyte_chars_accepted_normally(self): |
| 106 | + self.cp.set_pin("✈✈✈✈") |
| 107 | + |
| 108 | + def test_change_without_pin_does_not_force_change(self): |
| 109 | + self.get_cfg().set_min_pin_length(min_pin_length=10) |
| 110 | + info = self.ctap2.get_info() |
| 111 | + |
| 112 | + self.assertEqual(False, info.force_pin_change) |
| 113 | + |
| 114 | + def test_change_with_pin_does_force_change(self): |
| 115 | + self.pin = secrets.token_hex(10) |
| 116 | + self.cp.set_pin(self.pin) |
| 117 | + |
| 118 | + self.get_cfg().set_min_pin_length(min_pin_length=10) |
| 119 | + info = self.ctap2.get_info() |
| 120 | + |
| 121 | + self.assertEqual(True, info.force_pin_change) |
| 122 | + |
| 123 | + def test_change_with_pin_to_same_length_does_not_force_change(self): |
| 124 | + self.pin = secrets.token_hex(10) |
| 125 | + self.cp.set_pin(self.pin) |
| 126 | + |
| 127 | + self.get_cfg().set_min_pin_length(min_pin_length=4) |
| 128 | + info = self.ctap2.get_info() |
| 129 | + |
| 130 | + self.assertEqual(False, info.force_pin_change) |
| 131 | + |
| 132 | + def test_cannot_get_uv_when_change_forced(self): |
| 133 | + self.pin = secrets.token_hex(10) |
| 134 | + self.cp.set_pin(self.pin) |
| 135 | + self.get_cfg().set_min_pin_length(force_change_pin=True) |
| 136 | + |
| 137 | + with self.assertRaises(CtapError) as e: |
| 138 | + self.cp.get_pin_token(self.pin) |
| 139 | + |
| 140 | + self.assertEqual(CtapError.ERR.PIN_POLICY_VIOLATION, e.exception.code) |
| 141 | + |
| 142 | + def test_cannot_force_change_without_pin(self): |
| 143 | + with self.assertRaises(CtapError) as e: |
| 144 | + self.get_cfg().set_min_pin_length(force_change_pin=True) |
| 145 | + |
| 146 | + self.assertEqual(CtapError.ERR.PIN_NOT_SET, e.exception.code) |
0 commit comments