ircrobots/ircrobots/security.py

30 lines
647 B
Python
Raw Permalink Normal View History

2020-04-02 21:43:34 +00:00
import ssl
2023-02-06 19:42:27 +00:00
from dataclasses import dataclass
from typing import Optional, Tuple
2020-04-02 21:43:34 +00:00
2023-02-06 19:42:27 +00:00
@dataclass
class TLS:
2023-02-06 19:42:27 +00:00
client_keypair: Optional[Tuple[str, str]] = None
# tls without verification
class TLSNoVerify(TLS):
pass
# verify via CAs
class TLSVerifyChain(TLS):
pass
# verify by a pinned hash
class TLSVerifyHash(TLSNoVerify):
def __init__(self, sum: str):
self.sum = sum.lower()
class TLSVerifySHA512(TLSVerifyHash):
pass
def tls_context(verify: bool=True) -> ssl.SSLContext:
ctx = ssl.create_default_context()
if not verify:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
return ctx