This part of ASAB is currently under construction.
Transport Layer Security protocol (TLS, also known as "Secure Sockets Layer") is a cryptographic protocol that provides communication security over a computer network, so that the web servers can use HTTPS.
For adding the HTTPS to ASAB web applications, there is a asab.tls.SSLContextBuilder class that is connected to asab.web.WebContainer.
Path to a PEM file containing the certificate as well as any number of CA certificates needed to establish the certificate’s authenticity
key
Path to a file containing the private key. If not provided, the private key will be taken from the file specified in cert.
cafile
Path to a file containing the CA
capath
Path to a directory containing CA certificates
cadata
String containing CA certificates in PEM format
ciphers
String specifying the allowed SSL/TLS ciphers for the connection
dh_params
Path to a file containing Diffie-Hellman parameters for key exchange
verify_mode
Control the verification mode for peer certificates. Possible values are 'CERT_NONE' (no certificate verification), 'CERT_OPTIONAL' (verification but not required), and 'CERT_REQUIRED' (verification required)
Class for creating SSL context from a configuration.
Examples:
ssl_context_builder=asab.tls.SSLContextBuilder(config_section)ssl_context=ssl_context_builder.build(protocol=ssl.PROTOCOL_TLS_CLIENT)# ssl_context is later used as a parameter when making HTTP requests
classSSLContextBuilder(Configurable):""" Class for creating SSL context from a configuration. Examples: ```python ssl_context_builder = asab.tls.SSLContextBuilder(config_section) ssl_context = ssl_context_builder.build(protocol=ssl.PROTOCOL_TLS_CLIENT) # ssl_context is later used as a parameter when making HTTP requests ``` """ConfigDefaults={'cert':'',# The certfile string must be the path to a PEM file containing the certificate as well as any number of CA certificates needed to establish the certificate’s authenticity.'key':'',# The keyfile string, if present, must point to a file containing the private key in. Otherwise the private key will be taken from certfile as well.'key_password':'',# Following three options are fed into SSLContext.load_verify_locations(...)'cafile':'','capath':'','cadata':'','ciphers':'','dh_params':'','verify_mode':'',# empty or one of CERT_NONE, CERT_OPTIONAL or CERT_REQUIRED'check_hostname':'','options':'',}defbuild(self,protocol=ssl.PROTOCOL_TLS)->ssl.SSLContext:""" Create SSL Context for the specified protocol. Allowed `protocol` values: - ssl.PROTOCOL_TLS - ssl.PROTOCOL_TLS_CLIENT: used for the client - ssl.PROTOCOL_TLS_SERVER: used for the server Args: protocol: TLS protocol used for the communication. """ctx=ssl.SSLContext(protocol=protocol)ctx.options|=ssl.OP_NO_SSLv2ctx.options|=ssl.OP_NO_SSLv3keyfile=self.Config.get("key")iflen(keyfile)==0:keyfile=Nonekey_password=self.Config.get("key_password")iflen(key_password)==0:key_password=Nonecert=self.Config.get("cert")iflen(cert)!=0:ctx.load_cert_chain(cert,keyfile=keyfile,password=key_password,)cafile=self.Config.get("cafile")iflen(cafile)==0:cafile=Nonecapath=self.Config.get("capath")iflen(capath)==0:capath=Nonecadata=self.Config.get("cadata")iflen(cadata)==0:cadata=Noneif(cafileisnotNone)or(capathisnotNone)or(cadataisnotNone):ctx.load_verify_locations(cafile=cafile,capath=capath,cadata=cadata)ciphers=self.Config.get("ciphers")iflen(ciphers)!=0:ctx.set_ciphers(ciphers)dh_params=self.Config.get("dh_params")iflen(dh_params)!=0:ctx.load_dh_params(dh_params)verify_mode=self.Config.get("verify_mode")iflen(verify_mode)>0:verify_mode_tx={'CERT_NONE':ssl.CERT_NONE,'CERT_OPTIONAL':ssl.CERT_OPTIONAL,'CERT_REQUIRED':ssl.CERT_REQUIRED,}.get(verify_mode.upper())ifverify_mode_txisNone:raiseRuntimeError("Unknown value {}".format(verify_mode))ctx.verify_mode=verify_mode_tx# TODO: check_hostname > ctx.check_hostname# TODO: options > ctx.optionsreturnctx
defbuild(self,protocol=ssl.PROTOCOL_TLS)->ssl.SSLContext:""" Create SSL Context for the specified protocol. Allowed `protocol` values: - ssl.PROTOCOL_TLS - ssl.PROTOCOL_TLS_CLIENT: used for the client - ssl.PROTOCOL_TLS_SERVER: used for the server Args: protocol: TLS protocol used for the communication. """ctx=ssl.SSLContext(protocol=protocol)ctx.options|=ssl.OP_NO_SSLv2ctx.options|=ssl.OP_NO_SSLv3keyfile=self.Config.get("key")iflen(keyfile)==0:keyfile=Nonekey_password=self.Config.get("key_password")iflen(key_password)==0:key_password=Nonecert=self.Config.get("cert")iflen(cert)!=0:ctx.load_cert_chain(cert,keyfile=keyfile,password=key_password,)cafile=self.Config.get("cafile")iflen(cafile)==0:cafile=Nonecapath=self.Config.get("capath")iflen(capath)==0:capath=Nonecadata=self.Config.get("cadata")iflen(cadata)==0:cadata=Noneif(cafileisnotNone)or(capathisnotNone)or(cadataisnotNone):ctx.load_verify_locations(cafile=cafile,capath=capath,cadata=cadata)ciphers=self.Config.get("ciphers")iflen(ciphers)!=0:ctx.set_ciphers(ciphers)dh_params=self.Config.get("dh_params")iflen(dh_params)!=0:ctx.load_dh_params(dh_params)verify_mode=self.Config.get("verify_mode")iflen(verify_mode)>0:verify_mode_tx={'CERT_NONE':ssl.CERT_NONE,'CERT_OPTIONAL':ssl.CERT_OPTIONAL,'CERT_REQUIRED':ssl.CERT_REQUIRED,}.get(verify_mode.upper())ifverify_mode_txisNone:raiseRuntimeError("Unknown value {}".format(verify_mode))ctx.verify_mode=verify_mode_tx# TODO: check_hostname > ctx.check_hostname# TODO: options > ctx.optionsreturnctx