从命令行使用CURL进行https连接

use*_*392 110 ssl https command-line curl certificate

我是Curl和Cacerts世界的新手,在连接服务器时遇到问题.基本上,我需要测试从一台机器到另一台机器的https连接.我有一个我需要从机器A(Linux机器)连接的URL我在命令提示符下尝试了这个

cmd> curl https://[my domain or IP address]
Run Code Online (Sandbox Code Playgroud)

并得到以下:

curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Run Code Online (Sandbox Code Playgroud)

通过互联网上的一些文章我做了这个:

openssl s_client -connect <domain name or Ip address>:443
Run Code Online (Sandbox Code Playgroud)

并获得一些响应,包括服务器证书(内部-----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----).

接下来我该怎么办?我想,我只需要复制粘贴文本 BEGIN CERTIFICATE & END CERTIFICATE并将其保存在文件中.但是,它应该是什么类型的文件?.pem,.crt?那之后该怎么办?

我试过这个 - 复制里面的文本BEGIN CERTIFICATE & END CERTIFICATE并将其保存在一个.crt文件中 - 将其命名为my-ca.crt(也通过将其命名为my-ca.pem文件来尝试同样的事情),然后执行此操作:

cmd>curl --cacert my-ca.crt https://[my domain or IP address]
Run Code Online (Sandbox Code Playgroud)

但得到了同样的错误.

Dav*_*ild 123

我遇到了同样的问题 - 我从我自己的网站上获取了一个页面,该页面是通过HTTPS提供的,但是curl给出了相同的"SSL证书问题"消息.我通过-k在调用中添加一个标志来解决这个问题以允许不安全的连接.

curl -k https://whatever.com/script.php
Run Code Online (Sandbox Code Playgroud)

编辑:我发现了问题的根源.我使用的是SSL证书(来自StartSSL,但我认为这并不重要)并且没有正确设置中间证书.如果你遇到与上面的user1270392相同的问题,那么在诉诸修复之前测试你的SSL证书并修复它的任何问题可能是个好主意curl -k.

  • -k标志是一个很好的捷径.为我工作! (4认同)

Ant*_*osa 40

简单解决方案

这是我的日常脚本:

curl --insecure -v https://www.google.com 2>&1 | awk 'BEGIN { cert=0 } /^\* Server certificate:/ { cert=1 } /^\*/ { if (cert) print }'
Run Code Online (Sandbox Code Playgroud)

输出:

* Server certificate:
*    subject: C=US; ST=California; L=Mountain View; O=Google Inc; CN=www.google.com
*    start date: 2016-01-07 11:34:33 GMT
*    expire date: 2016-04-06 00:00:00 GMT
*    issuer: C=US; O=Google Inc; CN=Google Internet Authority G2
*    SSL certificate verify ok.
* Server GFE/2.0 is not blacklisted
* Connection #0 to host www.google.com left intact
Run Code Online (Sandbox Code Playgroud)

  • @Brethlosze 感谢您的评论。但是,事实并非如此。重点是从各种证书中获取 SSL 信息。 (5认同)
  • 因此,您每天都允许通过TSL建立“-不安全”连接... (4认同)

Som*_*era 27

您需要提供整个证书链以进行卷曲,因为curl不再附带任何CA证书.由于cacert选项只能使用一个文件,因此需要将完整的链信息连接到1个文件中

将证书链(例如,从您的浏览器)复制到DER编码的二进制文件x.509(.cer)中.为每个证书执行此操作.

将证书转换为PEM,并将它们连接成1个文件.

openssl x509 -inform DES -in file1.cer -out file1.pem -text
openssl x509 -inform DES -in file2.cer -out file2.pem -text
openssl x509 -inform DES -in file3.cer -out file3.pem -text

cat *.pem > certRepo

curl --cacert certRepo -u user:passwd -X GET -H 'Content-Type: application/json' "https//somesecureserver.com/rest/field"
Run Code Online (Sandbox Code Playgroud)

我在这里写了一篇关于如何做到这一点的博客:http://javamemento.blogspot.no/2015/10/using-curl-with-ssl-cert-chain.html

  • 您能解释一下传递用户名和密码的目的吗?如果我正在编写 Java 客户端,在这种情况下,我是否需要在 GET 请求的标头中传递用户名和密码? (2认同)

小智 16

用于--cacert指定.crt文件. ca-root-nss.crt例如.


gec*_*kob 7

我实际上有这种问题,我通过以下步骤解决它:

  1. 从此处获取根CA证书包:https://curl.haxx.se/ca/cacert.pem并将其保存在本地

  2. 找到该php.ini文件

  3. 将the设置curl.cainfo为证书的路径.所以它会像:

curl.cainfo = /path/of/the/keys/cacert.pem


lmi*_*lmh 5

在这里您可以找到 CA 证书以及下载和转换 Mozilla CA 证书的说明。一旦你得到ca-bundle.crtcacert.pem你只是使用:

curl.exe --cacert cacert.pem https://www.google.com
Run Code Online (Sandbox Code Playgroud)

或者

curl.exe --cacert ca-bundle.crt https://www.google.com
Run Code Online (Sandbox Code Playgroud)