hat.drivers.ssl

 1from ssl import *  # NOQA
 2
 3import enum
 4import pathlib
 5import ssl
 6
 7
 8class SslProtocol(enum.Enum):
 9    TLS_CLIENT = ssl.PROTOCOL_TLS_CLIENT
10    TLS_SERVER = ssl.PROTOCOL_TLS_SERVER
11
12
13def create_ssl_ctx(protocol: SslProtocol,
14                   *,
15                   check_hostname: bool = False,
16                   verify_cert: bool = False,
17                   load_default_certs: bool = True,
18                   cert_path: pathlib.PurePath | None = None,
19                   key_path: pathlib.PurePath | None = None,
20                   ca_path: pathlib.PurePath | None = None,
21                   password: str | None = None
22                   ) -> ssl.SSLContext:
23    ctx = ssl.SSLContext(protocol.value)
24    ctx.check_hostname = check_hostname
25
26    if verify_cert:
27        ctx.verify_mode = ssl.VerifyMode.CERT_REQUIRED
28
29        if load_default_certs:
30            ctx.load_default_certs(ssl.Purpose.CLIENT_AUTH
31                                   if protocol == SslProtocol.TLS_SERVER
32                                   else ssl.Purpose.SERVER_AUTH)
33
34        if ca_path:
35            ctx.load_verify_locations(cafile=str(ca_path))
36
37    else:
38        ctx.verify_mode = ssl.VerifyMode.CERT_NONE
39
40    if cert_path:
41        ctx.load_cert_chain(certfile=str(cert_path),
42                            keyfile=str(key_path) if key_path else None,
43                            password=password)
44
45    return ctx
class SslProtocol(enum.Enum):
 9class SslProtocol(enum.Enum):
10    TLS_CLIENT = ssl.PROTOCOL_TLS_CLIENT
11    TLS_SERVER = ssl.PROTOCOL_TLS_SERVER
TLS_CLIENT = <SslProtocol.TLS_CLIENT: <_SSLMethod.PROTOCOL_TLS_CLIENT: 16>>
TLS_SERVER = <SslProtocol.TLS_SERVER: <_SSLMethod.PROTOCOL_TLS_SERVER: 17>>
def create_ssl_ctx( protocol: SslProtocol, *, check_hostname: bool = False, verify_cert: bool = False, load_default_certs: bool = True, cert_path: pathlib.PurePath | None = None, key_path: pathlib.PurePath | None = None, ca_path: pathlib.PurePath | None = None, password: str | None = None) -> ssl.SSLContext:
14def create_ssl_ctx(protocol: SslProtocol,
15                   *,
16                   check_hostname: bool = False,
17                   verify_cert: bool = False,
18                   load_default_certs: bool = True,
19                   cert_path: pathlib.PurePath | None = None,
20                   key_path: pathlib.PurePath | None = None,
21                   ca_path: pathlib.PurePath | None = None,
22                   password: str | None = None
23                   ) -> ssl.SSLContext:
24    ctx = ssl.SSLContext(protocol.value)
25    ctx.check_hostname = check_hostname
26
27    if verify_cert:
28        ctx.verify_mode = ssl.VerifyMode.CERT_REQUIRED
29
30        if load_default_certs:
31            ctx.load_default_certs(ssl.Purpose.CLIENT_AUTH
32                                   if protocol == SslProtocol.TLS_SERVER
33                                   else ssl.Purpose.SERVER_AUTH)
34
35        if ca_path:
36            ctx.load_verify_locations(cafile=str(ca_path))
37
38    else:
39        ctx.verify_mode = ssl.VerifyMode.CERT_NONE
40
41    if cert_path:
42        ctx.load_cert_chain(certfile=str(cert_path),
43                            keyfile=str(key_path) if key_path else None,
44                            password=password)
45
46    return ctx