Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/request/pkihandler.py
3554 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2026 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
ssl = None
9
try:
10
import ssl as _ssl
11
ssl = _ssl
12
except ImportError:
13
pass
14
15
from lib.core.data import conf
16
from lib.core.common import getSafeExString
17
from lib.core.exception import SqlmapConnectionException
18
from thirdparty.six.moves import http_client as _http_client
19
from thirdparty.six.moves import urllib as _urllib
20
21
22
class HTTPSPKIAuthHandler(_urllib.request.HTTPSHandler):
23
def __init__(self, auth_file):
24
_urllib.request.HTTPSHandler.__init__(self)
25
self.auth_file = auth_file
26
27
def https_open(self, req):
28
return self.do_open(self.getConnection, req)
29
30
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
37
try:
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:
49
errMsg = "error occurred while using key "
50
errMsg += "file '%s' ('%s')" % (self.auth_file, getSafeExString(ex))
51
raise SqlmapConnectionException(errMsg)
52
53