curl - 使用 openssl 和 curl 验证服务器证书

标签 curl openssl mbedtls

我有一个嵌入式设备(运行 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.

最佳答案

您存储了哪个证书部分?我怀疑您没有在文件中放入正确的证书 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 ) 中,但如果您的磁盘空间有限,这不是真正可取的。

关于curl - 使用 openssl 和 curl 验证服务器证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50949459/

相关文章:

javascript - 推送通知 Codelab 未捕获语法错误 : Unexpected identifier in curl request

OpenSSL 服务器数据包被分成每个数据包 270 字节

c++ - 使用 mbedTLS 从服务器提取公共(public)证书链并存储为字符串

ssl - Tls 握手失败,即使密码套件是通用的

linux - libcurl 不支持 HTTPS

c++ - 将 curlpp 添加到 cmake 项目

php - 修复 curl_exec 在 Windows 8 apache 中挂起

Ruby 企业版 + OpenSSL -> "certificate verify failed"

c - 基于事件的openssl bio

c - mbedTLS sha256 的性能现实吗?