【发布时间】:2023-01-13 02:29:22
【问题描述】:
我正在尝试连接到第三方系统,这将使用双方的自签名证书,因为这不会面向公共互联网。
我将 Python 3 与 urllib3 池管理器一起使用。我有证书的副本,私钥和第三方的验证证书。在 curl 中使用这些确认连接有效:
curl https://third_party_url.com/hello -iv --cert ./cert.cert --cacert ./verify.cert --key ./key.key
但是,当我尝试在代码中使用它时:
client = urllib3.PoolManager(
cert_file = "./cert.cert",
key_file="./key.key",
ca_certs="./verify.cert",
cert_reqs="CERT_REQUIRED"
)
resp = client.request("GET", "https://third_party_url.com/hello")
出现异常:
Exception has occurred: SSLError
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)
很多类似问题的答案都是关于禁用验证,这绝对不是一个选项。任何投入将不胜感激。
编辑
@Steffen提出的问题的答案
1)
在没有 -cacert 参数的情况下运行时,curl 提供以下输出:
* Trying 10.10.10.10:443...
* Connected to third_party_url.com (10.10.10.10) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (OUT), TLS alert, unknown CA (560):
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
所以这是同样的问题。这就像 urllib3 不接受加载提供的参数的参数。
2)
在运行openssl x509 -in verify.cert -text
输出没有设置为 CA:true 的基本约束。
输出是:
Certificate:
Data:
Version: 1 (0x0)
Serial Number:
<SerialNumber>
Signature Algorithm: sha256WithRSAEncryption
Issuer: C = GB, O = me.com, OU = third-party
Validity
Not Before: Valid Date
Not After : Expiry Date
Subject: C = GB, O = Third Party Company Name, OU = third-party, CN = *third-party.com
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
<Modulus>
Exponent: <Redacted Value>
Signature Algorithm: sha256WithRSAEncryption
Signature Value:
<Signature Value>
-----BEGIN CERTIFICATE-----
<Certificate Details>
-----END CERTIFICATE-----
【问题讨论】:
-
您能否检查 1. 当未给出
-cacert参数时 curl 失败,即给定的 verify.cert 实际上是必不可少的,以及 2. 在获取 verify.cert 的详细信息时(如openssl x509 -in verify.cert -text),您会看到基本约束设置为 CA:true。 -
我已经用你问题的答案编辑了我的帖子@SteffenUllrich