Verify MAC challenge If our message did not include a digest_name prefix, the client is allowed to select a stronger digest_name from _ALLOWED_DIGESTS. In case our message is prefixed, a client cannot downgrade to a weaker algorithm, because the MAC is calculated over the entire me
(authkey, message, response)
| 901 | |
| 902 | |
| 903 | def _verify_challenge(authkey, message, response): |
| 904 | """Verify MAC challenge |
| 905 | |
| 906 | If our message did not include a digest_name prefix, the client is allowed |
| 907 | to select a stronger digest_name from _ALLOWED_DIGESTS. |
| 908 | |
| 909 | In case our message is prefixed, a client cannot downgrade to a weaker |
| 910 | algorithm, because the MAC is calculated over the entire message |
| 911 | including the '{digest_name}' prefix. |
| 912 | """ |
| 913 | import hmac |
| 914 | response_digest, response_mac = _get_digest_name_and_payload(response) |
| 915 | response_digest = response_digest or 'md5' |
| 916 | try: |
| 917 | expected = hmac.new(authkey, message, response_digest).digest() |
| 918 | except ValueError: |
| 919 | raise AuthenticationError(f'{response_digest=} unsupported') |
| 920 | if len(expected) != len(response_mac): |
| 921 | raise AuthenticationError( |
| 922 | f'expected {response_digest!r} of length {len(expected)} ' |
| 923 | f'got {len(response_mac)}') |
| 924 | if not hmac.compare_digest(expected, response_mac): |
| 925 | raise AuthenticationError('digest received was wrong') |
| 926 | |
| 927 | |
| 928 | def deliver_challenge(connection, authkey: bytes, digest_name='sha256'): |
no test coverage detected