在尝试访问HTTPS网址时如何使用cURL处理证书?

moo*_*ats 178 api https curl

我使用curl得到以下错误:

curl: (77) error setting certificate verify locations:
  CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: none

如何设置此证书验证位置?谢谢.

Sco*_*ons 147

我还安装了最新版本的ca-certificates,但仍然收到错误:

curl: (77) error setting certificate verify locations:
  CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
Run Code Online (Sandbox Code Playgroud)

问题是卷曲期望证书在路径上/etc/pki/tls/certs/ca-bundle.crt但是找不到它,因为它在路径上/etc/ssl/certs/ca-certificates.crt.

通过运行将我的证书复制到预期目标

sudo cp /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt
Run Code Online (Sandbox Code Playgroud)

为我工作.如果目标目标不存在,则需要为目标目标创建文件夹

sudo mkdir -p /etc/pki/tls/certs
Run Code Online (Sandbox Code Playgroud)

如果需要,修改上面的命令以使目标文件名与curl期望的路径匹配,即替换/etc/pki/tls/certs/ca-bundle.crt为错误消息中"CAfile:"后面的路径.

  • 如果每次更新时都不想重新复制它,也可以使用`ln -s`创建符号链接. (28认同)
  • Fedora 25上的`rescuetime`应用程序有同样的问题.`sudo ln -s /etc/pki/tls/certs/ca-bundle.crt/etc/pki/tls/certs/ca-certificates.crt`解决了这个问题.(`CURL_CA_BUNDLE` env var不起作用) (4认同)

Yau*_*ich 80

把它放进你的 .bashrc

# fix CURL certificates path
export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
Run Code Online (Sandbox Code Playgroud)

(见Robert的评论)

  • 感谢您提供一种方法,不需要我手动删除系统文件,但仍然保持使用证书的安全性! (8认同)

Rub*_*zzo 72

此错误与缺少的包有关:ca-certificates.安装它.

在Ubuntu Linux(和类似的发行版)中:

# apt-get install ca-certificates
Run Code Online (Sandbox Code Playgroud)

在CygWin通过Apt-Cyg

# apt-cyg install ca-certificates
Run Code Online (Sandbox Code Playgroud)

在Arch Linux(Raspberry Pi)中

# pacman -S ca-certificates
Run Code Online (Sandbox Code Playgroud)

文档告诉:

此程序包包含CA证书的PEM文件,以允许基于SSL的应用程序检查SSL连接的真实性.

如下所示:Debian - squeeze中包ca-certificates的详细信息

  • ca-certificates已经是最新版本,但我仍然收到错误 (89认同)
  • 在Arch Linux上,您可能还需要`pacman -S ca-certificates-utils`.我做到了. (11认同)
  • @PastorBones我有同样的问题,包已经是最新的,我认为它已经被破坏所以'sudo apt install --reinstall ca-certificates`重新安装包并解决了我看到的错误 (6认同)
  • 此软件包已安装.这个答案没有帮助. (4认同)
  • 当然,如果你尝试使用curl和https://raw.github.com通过推荐的方法安装apt-cyg,你会得到同样的错误. (2认同)
  • 如何在 Windows 中做到这一点? (2认同)

pra*_*esh 28

创建~/.curlrc包含以下内容的文件

cacert=/etc/ssl/certs/ca-certificates.crt
Run Code Online (Sandbox Code Playgroud)

  • 最好的答案,这在 Linux Mint 17 上对我有用 (2认同)

10g*_*tic 18

解决错误的最快方法是在curl请求中的某处添加-k选项.该选项"允许连接到没有证书的SSL引用".(来自curl --help)

请注意,这可能意味着您没有与您认为自己所在的终端进行通信,因为他们提供的证书并非由您信任的CA签署.

例如:

$ curl -o /usr/bin/apt-cyg https://raw.github.com/cfg/apt-cyg/master/apt-cyg
Run Code Online (Sandbox Code Playgroud)

给了我以下错误回复:

curl: (77) error setting certificate verify locations:
  CAfile: /usr/ssl/certs/ca-bundle.crt
  CApath: none
Run Code Online (Sandbox Code Playgroud)

我在-k上添加了:

curl -o /usr/bin/apt-cyg https://raw.github.com/cfg/apt-cyg/master/apt-cyg -k
Run Code Online (Sandbox Code Playgroud)

并没有错误信息.作为奖励,现在我安装了apt-cyg.和ca证书.

  • 对真的."-k"选项是"--insecure"的简写.如果您有中间人,您认为他对您的数据做了什么?剧透警报:他正在解密它,窃取它,并可能修改它并注入不安全的流中.直接从手册页:" - k,--insecure(SSL)此选项明确允许curl执行"不安全"的SSL连接和传输.通过使用默认安装的CA证书包,尝试使所有SSL连接安全.这使得所有被认为是"不安全"的连接都会失败,除非使用-k,-insecure." (17认同)
  • 这可能会解决错误,但它也会使"安全"连接变得不安全. (9认同)
  • 并不真地。据我所知,您不能绕过安全连接的加密,因此它仍然是加密的,并且只能到达一个端点。如果我错了,有人会纠正我,但你唯一面临的风险是你可能成为中间人攻击的牺牲品。如果您使用 curl,仍然不太可能有风险。 (2认同)
  • 如果你需要SSL,你需要隐私和验证 - `-k`标志意味着你**失败**验证.根据您的需要,这可能是可以接受的.如果你假设你的网络和你正在与之通信的服务器是从闯入者那里得到保护的话,那么MITM就是非平凡的攻击(你能做出这样的假设吗?).风险会根据您的数据类型而增加(源代码和证书比图像风险更大).您可以在传输(校验和等)后验证数据的完整性,但现在您将信任转移到该校验和通道.最后,`-k`为你提供了更多的工作. (2认同)

Har*_*ath 13

@roens是正确的.这会影响所有Anaconda用户,并出现以下错误
curl: (77) error setting certificate verify locations: CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none

解决方法是使用默认的系统卷曲并避免弄乱前面的Anaconda PATH变量.你也可以

  1. 重命名Anaconda curl二进制文件:)
    mv /path/to/anaconda/bin/curl /path/to/anaconda/bin/curl_anaconda

  2. 删除Anaconda卷曲
    conda remove curl

$ which curl /usr/bin/curl

[0] Anaconda Ubuntu curl Github问题 https://github.com/conda/conda-recipes/issues/352


Mic*_*tan 13

如果有人仍然遇到问题,请尝试这个,它对我有用。删除目录中的文件/etc/ssl/certs/,然后重新安装 ca 证书:

sudo apt install ca-certificates --reinstall
Run Code Online (Sandbox Code Playgroud)

当我尝试安装 Linuxbrew 时就是这样做的。

  • 这对我有帮助,但我没有删除计算机上的文件。只需运行简单的命令,curl 就开始工作。 (2认同)

小智 12

来自$ man curl:

--cert-type <type>
    (SSL) Tells curl what certificate type the provided  certificate
    is in. PEM, DER and ENG are recognized types.  If not specified,
    PEM is assumed.

    If this option is used several times, the last one will be used.

--cacert <CA certificate>
    (SSL) Tells curl to use the specified certificate file to verify
    the peer. The file may contain  multiple  CA  certificates.  The
    certificate(s)  must be in PEM format. Normally curl is built to
    use a default file for this, so this option is typically used to
    alter that default file.
Run Code Online (Sandbox Code Playgroud)


Shr*_*ser 7

我有同样的问题。事实证明,我的/etc/ssl/certs/ca-certificates.crt文件格式错误。最后一个条目显示如下:

-----BEGIN CERTIFICATE-----
MIIEDTCCAvWgAwIBAgIJAN..lots of certificate text....AwIBAgIJAN-----END CERTIFICATE-----
Run Code Online (Sandbox Code Playgroud)

在之前添加换行符后-----END CERTIFICATE-----,curl 能够处理证书文件。

这很烦人,因为我的update-ca-certificates命令没有给我任何警告。

这可能是也可能不是 curl 的版本特定问题,所以这是我的版本,只是为了完整性:

curl --version
# curl 7.51.0 (x86_64-alpine-linux-musl) libcurl/7.51.0 OpenSSL/1.0.2j zlib/1.2.8 libssh2/1.7.0
# Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp 
# Features: IPv6 Largefile NTLM NTLM_WB SSL libz TLS-SRP UnixSockets 
Run Code Online (Sandbox Code Playgroud)


小智 6

这对我有用

sudo apt-get install ca-certificates
Run Code Online (Sandbox Code Playgroud)

然后进入证书文件夹

sudo cd /etc/ssl/certs
Run Code Online (Sandbox Code Playgroud)

然后你将ca-certificates.crt文件复制到 /etc/pki/tls/certs

sudo cp ca-certificates.crt /etc/pki/tls/certs
Run Code Online (Sandbox Code Playgroud)

如果没有tls/certs文件夹:使用chmod 777 -R folderNAME创建一个并更改权限

  • 我尝试了这个,但这对我不起作用,而且我仍然遇到相同的错误。有任何想法吗 ? (2认同)

Pab*_*ier 6

解决此问题的另一种方法是禁用证书验证:

echo insecure >> ~/.curlrc
Run Code Online (Sandbox Code Playgroud)

  • 解决方法,但它在长时间使用卷曲证书后对我有帮助。谢谢。 (2认同)

med*_*eda 5

curlSSL默认执行证书验证,使用“捆绑”Certificate Authority (CA)公钥(CA 证书)。默认包名为 curl-ca-bundle.crt;您可以使用 --cacert 选项指定备用文件。

如果此HTTPS服务器使用由捆绑中表示的 CA 签名的证书,则证书验证可能会因证书问题而失败(它可能已过期,或者名称可能与 URL 中的域名不匹配)。

如果您想关闭 curl 对证书的验证,请使用 -k(或--insecure)选项。

例如

curl --insecure http://........
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,“信任来源”在这里非常无关紧要,因为如果没有针对 CA 正确验证证书,您将不知道“来源”是谁。 (3认同)

LJT*_*LJT 5

对于在Windows上运行在XAMPP上的PHP代码,我发现我需要编辑php.ini以包含以下内容

[curl]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo = curl-ca-bundle.crt
Run Code Online (Sandbox Code Playgroud)

然后复制到文件https://curl.haxx.se/ca/cacert.pem并重命名为curl-ca-bundle.crt并将其放在\ xampp路径下(我无法使curl.capath工作) .我还发现cURL站点上的CAbundle对于我连接的远程站点来说还不够,所以在http://winampplugins.co.uk上使用了一个预编译的Windows版本的curl 7.47.1 . /卷曲/