简体   繁体   English

如何修复“无法连接到 https://127.0.0.1 SSL 证书问题:自签名证书”

[英]How to fix "Unable to connect to https://127.0.0.1 SSL certificate problem: self signed certificate"

I know there are several posts about this topic but I've tried a lot and nothing has worked.我知道有几篇关于这个主题的帖子,但我已经尝试了很多,但没有任何效果。

    curl_setopt($ch, CURLOPT_URL, "https://127.0.0.1:2288");
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
    curl_setopt($ch, CURLOPT_USERPWD, $this->username . ":" . $this->password);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $pRequest);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CAINFO, "/etc/ssl/certs/cacert.pem");
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '2');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

When I execute it on Ubuntu 20.04 machine, i get this error:当我在 Ubuntu 20.04 机器上执行它时,我收到此错误:

Fatal error: Uncaught RuntimeException: Unable to connect to https://127.0.0.1:2288/ Error: SSL certificate problem: self signed certificate

The certificate is the current version from curl.se/docs/caextract.html该证书是 curl.se/docs/caextract.html 中的当前版本

php.ini: php.ini:

extension=curl
extension=php_curl.dll
curl.cainfo="/etc/ssl/certs/cacert.pem"

What else can I do without prohibiting curl ssl connections?如果不禁止 curl ssl 连接,我还能做什么?

If you are using self-signed certificates you either have to add it to your trusted certificates or you have to turn validation off.如果您使用自签名证书,则必须将其添加到受信任的证书中,或者必须关闭验证。 For the second case just switch the CURLOPT_SSL_VERIFYPEER to false.对于第二种情况,只需将 CURLOPT_SSL_VERIFYPEER 切换为 false。

You are connecting securely to localhost on Ubuntu and, as your error indicates, your server is responding with a self-signed certificate.您正在 Ubuntu 上安全地连接到 localhost,并且正如您的错误所示,您的服务器正在使用自签名证书进行响应。 Your code specifies that curl should attempt to verify the host it's connecting to (localhost, in your case):您的代码指定 curl 应尝试验证它连接到的主机(本地主机,在您的情况下):

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, '2');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

According to the docs , that value of 2 in your code for CURLOPT_SSL_VERIFYHOST is documented thusly:根据docs ,您的CURLOPT_SSL_VERIFYHOST代码中的值2记录如下:

2 to verify that a Common Name field or a Subject Alternate Name field in the SSL peer certificate matches the provided hostname. 2 验证 SSL 对等证书中的 Common Name 字段或 Subject Alternate Name 字段是否与提供的主机名匹配。 0 to not check the names. 0 不检查名称。 1 should not be used. 1 不应该使用。 In production environments the value of this option should be kept at 2 (default value).在生产环境中,此选项的值应保持为 2(默认值)。

If you want to strictly follow the documentation's recommendation, you'd have to map some domain onto localhost and install a cert for that domain on your server.如果您想严格遵循文档的建议,您必须将某个域映射到 localhost 并在您的服务器上为该域安装证书。 AFAIK, you cannot get a cert for 127.0.0.1 because that special IP address always refers to localhost...sort of a long story but it makes no sense for anyone to sign such a cert. AFAIK,您无法获得 127.0.0.1 的证书,因为该特殊 IP 地址始终指的是 localhost ......这是一个很长的故事,但任何人签署这样的证书都没有意义。

Your other option is to tell curl not to verify the ssl connection CURLOPT_SSL_VERIFYHOST to zero:您的另一个选择是告诉 curl 不要将 ssl 连接CURLOPT_SSL_VERIFYHOST验证为零:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// you may also need to set this?
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

That tells curl not to bother validating the certificate.这告诉 curl 不要费心验证证书。 This should be fairly safe if your are in control of your own server and if your server hasn't been compromised somehow.如果您可以控制自己的服务器并且您的服务器没有以某种方式受到损害,这应该是相当安全的。

EDIT I would add that downloading the cert extract from curl.se just grabs a bundle of widely trusted signatures/certificates that happens to be the bundle used by Firefox .编辑我想补充一点,从 curl.se 下载证书提取物只是抓取了一组广泛信任的签名/证书,恰好是Firefox使用的包。 This cert bundle is used to check any domain that you might visit by comparing the signature on that domain's certificate to some big/important certificates that have been signed by supposedly trustworthy organizations.此证书包用于检查您可能访问的任何,方法是将域证书上的签名与一些由可信组织签署的大/重要证书进行比较。 It will never be helpful in validating a self-signed certificate.它永远不会有助于验证自签名证书。 For more information about this elaborate concept, try reading about Web of Trust .有关此详细概念的更多信息,请尝试阅读有关Web of Trust 的信息

I should also mention that you might be able to use a few commands to grab the certificate that your local machine coughs up and configure your curl code to use that certificate, HOWEVER, the CURLOPT_SSL_VERIFYHOST setting of 2 instructs curl to check the Common Name field of this certificate against the IP address or domain to which you are connecting.我还应该提到,您可能能够使用一些命令来获取本地计算机发出的证书并配置您的 curl 代码以使用该证书,但是, CURLOPT_SSL_VERIFYHOST设置为2指示 curl 检查Common Name字段此证书针对您要连接的 IP 地址或域。 On my ubuntu workstation, the certificate's Common Name is 'Ubuntu' -- and that won't match either localhost or 127.0.0.1.在我的 ubuntu 工作站上,证书的通用名称是“Ubuntu”——这与 localhost 或 127.0.0.1 都不匹配。 For this approach to work, you'll either have to generate a new cert for your machine with a Common Name that matches the address to which you connect (localhost or 127.0.0.1) OR you'll need to set up something to name your machine 'ubuntu' on your local network.为了使这种方法起作用,您必须为您的机器生成一个新证书,其通用名称与您连接的地址(localhost 或 127.0.0.1)相匹配,或者您需要设置一些东西来命名您的本地网络上的机器“ubuntu”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM