55See the file 'LICENSE' for copying permission
66"""
77
8+ ssl = None
9+ try :
10+ import ssl as _ssl
11+ ssl = _ssl
12+ except ImportError :
13+ pass
14+
815from lib .core .data import conf
916from lib .core .common import getSafeExString
1017from lib .core .exception import SqlmapConnectionException
1118from thirdparty .six .moves import http_client as _http_client
1219from thirdparty .six .moves import urllib as _urllib
1320
21+
1422class HTTPSPKIAuthHandler (_urllib .request .HTTPSHandler ):
1523 def __init__ (self , auth_file ):
1624 _urllib .request .HTTPSHandler .__init__ (self )
@@ -20,10 +28,24 @@ def https_open(self, req):
2028 return self .do_open (self .getConnection , req )
2129
2230 def getConnection (self , host , timeout = None ):
31+ if timeout is None :
32+ timeout = conf .timeout
33+
34+ if not hasattr (_http_client , "HTTPSConnection" ):
35+ raise SqlmapConnectionException ("HTTPS support is not available in this Python build" )
36+
2337 try :
24- # Reference: https://docs.python.org/2/library/ssl.html#ssl.SSLContext.load_cert_chain
25- return _http_client .HTTPSConnection (host , cert_file = self .auth_file , key_file = self .auth_file , timeout = conf .timeout )
26- except IOError as ex :
38+ if ssl and hasattr (ssl , "SSLContext" ) and hasattr (ssl , "create_default_context" ):
39+ ctx = ssl .create_default_context ()
40+ ctx .load_cert_chain (certfile = self .auth_file , keyfile = self .auth_file )
41+ try :
42+ return _http_client .HTTPSConnection (host , timeout = timeout , context = ctx )
43+ except TypeError :
44+ pass
45+
46+ return _http_client .HTTPSConnection (host , cert_file = self .auth_file , key_file = self .auth_file , timeout = timeout )
47+
48+ except (IOError , OSError ) as ex :
2749 errMsg = "error occurred while using key "
2850 errMsg += "file '%s' ('%s')" % (self .auth_file , getSafeExString (ex ))
2951 raise SqlmapConnectionException (errMsg )
0 commit comments