2024-02-01 12:36:19 +00:00
|
|
|
import tls
|
2024-03-28 06:41:01 +00:00
|
|
|
from tls import *
|
2017-08-22 22:07:05 +00:00
|
|
|
|
2024-02-01 12:36:19 +00:00
|
|
|
|
|
|
|
class SSLContext:
|
|
|
|
def __init__(self, *args):
|
|
|
|
self._context = tls.SSLContext(*args)
|
|
|
|
self._context.verify_mode = CERT_NONE
|
|
|
|
|
|
|
|
@property
|
|
|
|
def verify_mode(self):
|
|
|
|
return self._context.verify_mode
|
|
|
|
|
|
|
|
@verify_mode.setter
|
|
|
|
def verify_mode(self, val):
|
|
|
|
self._context.verify_mode = val
|
|
|
|
|
|
|
|
def load_cert_chain(self, certfile, keyfile):
|
|
|
|
if isinstance(certfile, str):
|
|
|
|
with open(certfile, "rb") as f:
|
|
|
|
certfile = f.read()
|
|
|
|
if isinstance(keyfile, str):
|
|
|
|
with open(keyfile, "rb") as f:
|
|
|
|
keyfile = f.read()
|
|
|
|
self._context.load_cert_chain(certfile, keyfile)
|
|
|
|
|
|
|
|
def load_verify_locations(self, cafile=None, cadata=None):
|
|
|
|
if cafile:
|
|
|
|
with open(cafile, "rb") as f:
|
|
|
|
cadata = f.read()
|
|
|
|
self._context.load_verify_locations(cadata)
|
|
|
|
|
|
|
|
def wrap_socket(
|
|
|
|
self, sock, server_side=False, do_handshake_on_connect=True, server_hostname=None
|
|
|
|
):
|
|
|
|
return self._context.wrap_socket(
|
|
|
|
sock,
|
|
|
|
server_side=server_side,
|
|
|
|
do_handshake_on_connect=do_handshake_on_connect,
|
|
|
|
server_hostname=server_hostname,
|
|
|
|
)
|
2017-08-22 22:08:35 +00:00
|
|
|
|
|
|
|
|
2021-05-27 05:50:04 +00:00
|
|
|
def wrap_socket(
|
|
|
|
sock,
|
|
|
|
server_side=False,
|
2024-02-01 12:36:19 +00:00
|
|
|
key=None,
|
|
|
|
cert=None,
|
2021-05-27 05:50:04 +00:00
|
|
|
cert_reqs=CERT_NONE,
|
2024-02-01 12:36:19 +00:00
|
|
|
cadata=None,
|
|
|
|
server_hostname=None,
|
|
|
|
do_handshake=True,
|
2021-05-27 05:50:04 +00:00
|
|
|
):
|
2024-02-01 12:36:19 +00:00
|
|
|
con = SSLContext(PROTOCOL_TLS_SERVER if server_side else PROTOCOL_TLS_CLIENT)
|
|
|
|
if cert or key:
|
|
|
|
con.load_cert_chain(cert, key)
|
|
|
|
if cadata:
|
|
|
|
con.load_verify_locations(cadata=cadata)
|
|
|
|
con.verify_mode = cert_reqs
|
|
|
|
return con.wrap_socket(
|
|
|
|
sock,
|
|
|
|
server_side=server_side,
|
|
|
|
do_handshake_on_connect=do_handshake,
|
|
|
|
server_hostname=server_hostname,
|
|
|
|
)
|