Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to disable root hash check #43

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/autograph_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class SignatureVerifier:
:params ClientSession session: An aiohttp session, used to retrieve x5us.
:params Cache cache: A cache used to store results for x5u verification.
:params bytes root_hash: The expected hash for the first
certificate in a chain. This should not be encoded in any
certificate in a chain. Disabled if ``None``. This should not be encoded in any
way. Hashes can be decoded using decode_mozilla_hash.
:params SubjectNameCheck subject_name_check: Predicate to use to
validate cert subject names. Defaults to
Expand Down Expand Up @@ -344,8 +344,9 @@ async def verify_x5u(self, url):

# Verify chain of trust.
chain = certs[::-1]
root_hash = chain[0].fingerprint(SHA256())
if root_hash != self.root_hash:

# Check root certificate hash if specified
if self.root_hash and self.root_hash != (root_hash := chain[0].fingerprint(SHA256())):
raise CertificateHasWrongRoot(expected=self.root_hash, actual=root_hash)

current_cert = chain[0]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_autograph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ async def test_verify_wrong_root_hash(aiohttp_session, mock_with_x5u, cache, now
)


async def test_root_hash_is_ignored_if_none(aiohttp_session, mock_with_x5u, cache, now_fixed):
s = SignatureVerifier(
aiohttp_session,
cache,
root_hash=None,
)
await s.verify_x5u(FAKE_CERT_URL) # not raising


async def test_verify_broken_chain(aiohttp_session, mock_aioresponses, cache, now_fixed):
# Drop next-to-last cert in cert list
broken_chain = CERT_LIST[:1] + CERT_LIST[2:]
Expand Down
Loading