在Python中使用M2Crypto关闭SSL检查

时间:2010-02-24 17:59:13

标签: python ssl soappy

有没有办法关闭SSL检查,以便在python中使用SOAPpy时不会生成WrongHost异常。

2 个答案:

答案 0 :(得分:5)

您可以在M2Crypto中禁用所有对等证书检查:

from M2Crypto import SSL, httpslib

context = SSL.Context("sslv3")

# Disable certificate checking
context.set_verify(0, depth = 0)

connection = httpslib.HTTPSConnection("somehostname", 443, ssl_context=context)

# Hack (!!!) for disabling host name check <CN> == <expected host name>.
# Will affect any future SSL connections made by M2Crypto!
SSL.Connection.postConnectionCheck = None

connection.connect() # <-- this would normally raise SSL verification errors
connection.request("GET", "/")

...

我希望您知道,对于使用M2Crypto创建的任何SSL连接,这将基本上禁用安全性。所以这根本不值得推荐,除非你只是与一台服务器进行通信,并认为中间人风险比没有加密的HTTP更可接受。

到目前为止,对于M2Crypto解决方案,但正如您的问题(与您的标题相对)请求SOAPpy(我尚未使用),解决方案可能会有所不同,因为SOAPpy config似乎使用了socket模块而不是M2Crypto.SSL(参见第132行)。我不知道如何阻止socket.ssl模块检查主机名。

答案 1 :(得分:0)

扩展AndiDog的答案,你可以在逐个实例的基础上设置postConnectionCheck,在M2Crypto的0.21.1版本(至少)中,有Connect.set_post_connection_check_callback()方法可以这样做:

sslsock = M2Crypto.SSL.Connection(sslcontext)
# Disable checking of server certificates
sslsock.set_post_connection_check_callback(None)

请注意,禁用检查连接到服务器和接受的客户端(默认情况下禁用后者)。

参数,如果不是None,是一个带证书和地址的函数,即:

check(self.get_peer_cert(), self.addr[0])

供参考,请参阅M2Crypto source

相关问题