当提供SSL密钥时,Pycurl SSL会话ID无法正常工作

时间:2017-01-13 20:03:44

标签: python curl libcurl pycurl

默认情况下,libcurl(和pycurl)支持ssl连接重用,这意味着对于发送后续HTTPS请求的情况,第二个将使用SSL会话ID并避免完全握手。 (基本上pycurl.SSL_SESSIONID_CACHETrue)。

当我把它测试为:

import pycurl

c = pycurl.Curl()
c.setopt(c.URL, 'https://myserver:443/X')
c.setopt(c.COOKIEFILE, '')
c.setopt(c.VERBOSE, True)

c.perform()

c.setopt(c.FRESH_CONNECT, 1)
c.setopt(c.URL, 'https://myserver:443/Y')
c.perform()

我可以看到pycurl使用第二个连接的会话ID。但是,一旦我添加了客户端密钥和CA信息(用于相互身份验证),它就会停止工作。这意味着客户端协商了一个新的SSL密钥,这是不可取的。

import pycurl

c = pycurl.Curl()
c.setopt(c.URL, 'https://myserver:443/X')
c.setopt(c.COOKIEFILE, '')
c.setopt(c.VERBOSE, True)
c.setopt(pycurl.SSL_VERIFYHOST, 2)

c.setopt(c.SSLCERTTYPE, "PEM")
c.setopt(c.SSLKEYTYPE, "PEM")
client_cert = "ssl_keys/client/client_crt.pem"
ca_cert = "ssl_keys/ca/ca_crt.pem"
client_key = "ssl_keys/client/client_pr.pem"
c.setopt(c.SSLCERT, client_cert)
c.setopt(c.SSLKEY, client_key)
c.setopt(c.CAINFO, ca_cert)

c.perform()
c.setopt(c.SSL_SESSIONID_CACHE, True) # that doesn't matter really
c.setopt(c.FRESH_CONNECT, 1)
c.setopt(c.URL, 'https://myserver:443/Y')
c.perform()

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

当客户端证书用于安全目的时,

curl禁用“SSL会话恢复”。有关详细信息,请参阅2016年8月的TLS session resumption client cert bypass安全公告。

即使使用客户端证书,此功能也可以工作,但是没有人为安全的方式编写代码而努力。我认为用例足够小,可以采用更简单的方法:禁用客户端证书。