如何在Twisted Python服务器中禁用TLS 1.3,现有配置失效该如何解决?
问题根因
Ubuntu 18.04官方源提供的Twisted版本(v17.9.0)对TLS 1.3的配置支持存在局限:lowerMaximumSecurityTo 参数在该版本中仅会限制TLS 1.2及更早版本的最大协商版本,不会作用于TLS 1.3的独立配置逻辑;同时你配置的acceptableCiphers仅对TLS 1.2及更早版本生效,不会覆盖TLS 1.3专用的密码套件列表,因此TLS 1.3仍能正常协商启用。
解决方法
你可以通过以下任意一种方式实现禁用TLS 1.3的需求:
方法一:清空TLS 1.3专用密码套件(适配绝大多数场景)
在原有的CertificateOptions配置中新增tlsv13Ciphers参数,将TLS 1.3的可用密码套件设为空,直接阻断TLS 1.3协商:
from twisted.internet import ssl ciphers = ssl.AcceptableCiphers.fromOpenSSLCipherString( "ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA" ) tls_options = ssl.CertificateOptions( privateKey=my_key, certificate=my_cert, extraCertChain=my_cert_chain, acceptableCiphers=ciphers, lowerMaximumSecurityTo=ssl.TLSVersion.TLSv1_2, # 新增行:清空TLS 1.3专属密码套件,禁用TLS 1.3 tlsv13Ciphers=ssl.AcceptableCiphers.fromOpenSSLCipherString("") ) listening_port = my_reactor.listenSSL(8000, my_factory, tls_options)
方法二:自定义证书配置类强制限制最大TLS版本(适配无tlsv13Ciphers参数的极低版本)
如果你的Twisted版本过低不支持tlsv13Ciphers参数,可以继承CertificateOptions手动修改SSL上下文的最大版本限制:
from twisted.internet import ssl from OpenSSL import SSL class NoTLS13CertificateOptions(ssl.CertificateOptions): def postContextFactory(self, ctx): super().postContextFactory(ctx) # 强制指定最大支持TLS版本为1.2 ctx.set_max_proto_version(SSL.TLS1_2_VERSION) # 替换原有ssl.CertificateOptions为自定义类即可 ciphers = ssl.AcceptableCiphers.fromOpenSSLCipherString( "ECDHE-RSA-AES128-GCM-SHA256:AES128-SHA" ) tls_options = NoTLS13CertificateOptions( privateKey=my_key, certificate=my_cert, extraCertChain=my_cert_chain, acceptableCiphers=ciphers, lowerMaximumSecurityTo=ssl.TLSVersion.TLSv1_2 )
补充说明
如果需要同时禁用不安全的TLS 1.0、TLS 1.1版本,可在CertificateOptions配置中新增raiseMinimumSecurityTo=ssl.TLSVersion.TLSv1_2参数,只保留TLS 1.2可用。
内容的提问来源于stack exchange,提问作者Jon
相关产品推荐
相关产品推荐

