使用 open_ssl 和 curl 验证服务器证书

4

我有一个嵌入式设备(运行mbedTLS),它只能容纳非常有限数量的服务器证书,我想用curl验证我正在将PEM文件放在设备上。 我使用icanhazip.com(IPv6)进行验证。 为了创建PEM文件,我使用openSSL:

openssl s_client -connect icanhazip.com:443 -showcerts

将证书部分存储在icanhazip_com.pem中,并尝试使用curl进行验证:

curl --verbose --cacert icanhazip_com.pem https://icanhazip.com

但是我遇到了一个错误:
$ curl --verbose --cacert icanhazip_com.pem https://icanhazip.com
* Rebuilt URL to: https://icanhazip.com/
* timeout on name lookup is not supported
*   Trying 2607:ff68:116:f33d::13...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to icanhazip.com (2607:ff68:116:f33d::13) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
*   CAfile: icanhazip_com.pem
  CApath: none
* TLSv1.2 (OUT), TLS header, Certificate Status (22):
} [5 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* TLSv1.2 (IN), TLS handshake, Server hello (2):
{ [108 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [2792 bytes data]
* TLSv1.2 (OUT), TLS alert, Server hello (2):
} [2 bytes data]
* SSL certificate problem: unable to get issuer certificate
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Closing connection 0
} [5 bytes data]
* TLSv1.2 (OUT), TLS alert, Client hello (1):
} [2 bytes data]
curl: (60) SSL certificate problem: unable to get issuer certificate
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
1个回答

0
你存储了哪个证书部分?我怀疑你没有将正确的证书放入文件 icanhazip_com.pem 中。你必须放置由颁发者提供的 CA 证书(即 Let's Encrypt 提供的那个)。
openssl s_client 的输出来看,这是第二个证书而不是第一个证书:
Certificate chain
0 s:/CN=icanhazip.com
i:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
-----BEGIN CERTIFICATE-----
        ...
(icanhazip.com certificate)
        ...
-----END CERTIFICATE-----
1 s:/C=US/O=Let's Encrypt/CN=Let's Encrypt Authority X3
i:/O=Digital Signature Trust Co./CN=DST Root CA X3
-----BEGIN CERTIFICATE-----
        ...
(Let's Encrypt CA certificate)
        ...
-----END CERTIFICATE-----

您也可以选择将 openssl s_client 命令生成的所有证书存储在一个单独的捆绑文件中 (openssl s_client -connect icanhazip.com:443 -showcerts > icanhazip_com.pem),但如果您的磁盘空间有限,则这并不是理想的选择。


我尝试了两种方法,但都出现了上述错误... 如果我手动从 Let's Encrypt 网站下载证书,则可以正常工作。 - thomas.fogh
如果您只将Let's Encrypt证书而不是两个证书都放在icanhazip_com.pem中会怎样呢?也许您使用的curl版本不支持证书捆绑? - xhienne
我尝试了只使用第二个证书,但仍然出现相同的错误。 - thomas.fogh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接