Skip to content

Commit e1157d8

Browse files
committed
Updated tests to explicitly include subject alt names test. Tested with
remote peer with > 64 subject alt names
1 parent 64dc18d commit e1157d8

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

ndg/httpsclient/https.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class HTTPSConnection(HTTPConnection):
4848
@type default_ssl_method: int
4949
"""
5050
default_port = HTTPS_PORT
51-
default_ssl_method = SSL.SSLv23_METHOD
51+
default_ssl_method = SSL.TLSv1_2_METHOD
5252

5353
def __init__(self, host, port=None, strict=None,
5454
timeout=socket._GLOBAL_DEFAULT_TIMEOUT, ssl_context=None):
@@ -100,6 +100,8 @@ class HTTPSContextHandler(AbstractHTTPHandler):
100100
'''
101101
https_request = AbstractHTTPHandler.do_request_
102102

103+
SSL_METHOD = SSL.TLSv1_2_METHOD
104+
103105
def __init__(self, ssl_context, debuglevel=0):
104106
"""
105107
@param ssl_context:SSL context
@@ -116,7 +118,7 @@ def __init__(self, ssl_context, debuglevel=0):
116118
ssl_context)
117119
self.ssl_context = ssl_context
118120
else:
119-
self.ssl_context = SSL.Context(SSL.TLSv1_METHOD)
121+
self.ssl_context = SSL.Context(self.__class__.SSL_METHOD)
120122

121123
def https_open(self, req):
122124
"""Opens HTTPS request

ndg/httpsclient/test/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ class Constants(object):
1616
'''Convenience base class from which other unit tests can extend. Its
1717
sets the generic data directory path'''
1818
PORT = 4443
19+
# PORT = 443
1920
PORT2 = 4444
20-
HOSTNAME = 'localhost'
21+
HOSTNAME = 'localhost'
22+
# HOSTNAME = 'files.pythonhosted.org'
2123
TEST_URI = 'https://%s:%d' % (HOSTNAME, PORT)
2224
TEST_URI2 = 'https://%s:%d' % (HOSTNAME, PORT2)
23-
# TEST_URI = 'https://pypi.org'
24-
# TEST_URI2 = 'https://www.google.co.uk'
2525

2626
UNITTEST_DIR = os.path.dirname(os.path.abspath(__file__))
2727
CACERT_DIR = os.path.join(UNITTEST_DIR, 'pki', 'ca')

ndg/httpsclient/test/test_urllib2.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from OpenSSL import SSL
2222
from ndg.httpsclient.test import Constants
2323
from ndg.httpsclient.urllib2_build_opener import build_opener
24+
from ndg.httpsclient.ssl_peer_verification import ServerSSLCertVerification
2425

2526

2627
class Urllib2TestCase(unittest.TestCase):
@@ -36,21 +37,43 @@ def test02_open(self):
3637
self.assertTrue(res)
3738
print("res = %s" % res.read())
3839

40+
@unittest.skipIf(Constants.HOSTNAME != 'localhost', 'Skip non-local host')
3941
def test03_open_fails_unknown_loc(self):
4042
opener = build_opener()
4143
self.assertRaises(URLError_, opener.open, Constants.TEST_URI2)
4244

4345
def test04_open_peer_cert_verification_fails(self):
4446
# Explicitly set empty CA directory to make verification fail
45-
ctx = SSL.Context(SSL.TLSv1_METHOD)
47+
ctx = SSL.Context(SSL.TLSv1_2_METHOD)
4648
verify_callback = lambda conn, x509, errnum, errdepth, preverify_ok: \
4749
preverify_ok
4850

4951
ctx.set_verify(SSL.VERIFY_PEER, verify_callback)
5052
ctx.load_verify_locations(None, './')
5153
opener = build_opener(ssl_context=ctx)
5254
self.assertRaises(SSL.Error, opener.open, Constants.TEST_URI)
53-
55+
56+
def test05_open_with_subj_alt_names_verification(self):
57+
ctx = SSL.Context(SSL.TLSv1_2_METHOD)
58+
59+
# Set wildcard hostname for subject alternative name matching -
60+
# setting a minimum of two name components for hostname
61+
split_hostname = Constants.HOSTNAME.split('.', 1)
62+
if len(split_hostname) > 1:
63+
_hostname = '*.' + split_hostname[-1]
64+
else:
65+
_hostname = Constants.HOSTNAME
66+
67+
server_ssl_verify = ServerSSLCertVerification(hostname=_hostname)
68+
verify_callback_ = server_ssl_verify.get_verify_server_cert_func()
69+
ctx.set_verify(SSL.VERIFY_PEER, verify_callback_)
70+
ctx.set_default_verify_paths()
71+
72+
opener = build_opener(ssl_context=ctx)
73+
res = opener.open(Constants.TEST_URI)
74+
self.assertTrue(res)
75+
print("res = %s" % res.read())
76+
5477

5578
if __name__ == "__main__":
5679
unittest.main()

0 commit comments

Comments
 (0)