Skip to content

Commit

Permalink
Fixes #148 (#149)
Browse files Browse the repository at this point in the history
* Fixes ##148

* respect ' instead of "

* add tests

* bug fix

* fix tests
  • Loading branch information
aersam authored Mar 21, 2023
1 parent ebd441b commit e697481
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/pytds/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ def shutdown(self):
def verify_cb(conn, cert, err_num, err_depth, ret_code):
return ret_code == 1

def is_san_matching(san: str, host_name: str) -> bool:
for item in san.split(','):
dnsentry = item.lstrip('DNS:').strip()
# SANs are usually have form like: DNS:hostname
if dnsentry == host_name:
return True
if dnsentry[0:2] == "*.": # support for wildcards, but only at the first position
afterstar_parts = dnsentry[2:]
afterstar_parts_sname = '.'.join(host_name.split('.')[1:]) # remove first part of dns name
if afterstar_parts == afterstar_parts_sname:
return True
return False

def validate_host(cert, name):
"""
Expand All @@ -105,11 +117,10 @@ def validate_host(cert, name):
ext = cert.get_extension(i)
if ext.get_short_name() == b'subjectAltName':
s = str(ext)
# SANs are usually have form like: DNS:hostname
if s.startswith('DNS:') and s[4:] == s_name:
if is_san_matching(s, s_name):
return True

# TODO handle wildcards
# TODO check if wildcard is needed in CN as well
return False


Expand Down
10 changes: 10 additions & 0 deletions tests/tls_san_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from pytds.tls import is_san_matching

def test_san():
assert not is_san_matching("", "host.com")
assert is_san_matching("database.com", "database.com")
assert not is_san_matching("notdatabase.com", "database.com")
assert not is_san_matching("*.database.com", "database.com")
assert is_san_matching("*.database.com", "test.database.com")
assert not is_san_matching("database.com", "*.database.com")
assert not is_san_matching("test.*.database.com", "test.subdomain.database.com") # That star should be at first position

0 comments on commit e697481

Please sign in to comment.