HTTPS 和 SSL/TLS 证书验证是 cURL 安全通信的核心。正确配置 SSL 参数能确保数据传输的安全性。
基本 HTTPS 请求
bash# 标准 HTTPS 请求(自动验证证书) curl https://api.example.com # 显示 SSL 握手信息 curl -v https://api.example.com 2>&1 | grep -A 20 "SSL connection"
SSL 证书验证选项
bash# 忽略证书验证(不安全,仅测试用) curl -k https://self-signed.badssl.com curl --insecure https://api.example.com # 指定 CA 证书文件 curl --cacert /path/to/ca.crt https://api.example.com # 指定 CA 证书目录 curl --capath /etc/ssl/certs/ https://api.example.com # 使用系统默认证书库 curl https://api.example.com
客户端证书认证
bash# 使用客户端证书(双向 SSL) curl --cert /path/to/client.crt \ --key /path/to/client.key \ https://api.example.com # 带密码的客户端证书 curl --cert /path/to/client.crt:password \ --key /path/to/client.key \ https://api.example.com # PKCS#12 格式证书 curl --cert /path/to/cert.p12:password \ https://api.example.com
SSL 协议和加密套件
bash# 指定最低 SSL 版本 curl --ssl-version tls1.2 https://api.example.com # 指定 SSL 版本范围 curl --ssl-allow-beast \ --tlsv1.2 \ --tls-max tls1.3 \ https://api.example.com # 列出支持的加密套件 curl --ciphers 'HIGH:!aNULL:!MD5' https://api.example.com # 强制使用 TLS 1.3 curl --tlsv1.3 https://api.example.com
证书链验证
bash# 查看服务器证书信息 curl -v https://api.example.com 2>&1 | openssl x509 -text -noout # 验证证书链 curl --cacert /path/to/ca-bundle.crt \ -v https://api.example.com 2>&1 | grep "SSL certificate verify" # 导出服务器证书 openssl s_client -connect api.example.com:443 -showcerts # 测试特定证书 curl --cacert <(echo | openssl s_client -connect api.example.com:443 2>/dev/null | openssl x509) \ https://api.example.com
SSL 调试技巧
bash# 详细的 SSL 信息 curl -v --trace-ascii ssl-debug.txt https://api.example.com # 查看 SSL 握手过程 curl -v https://api.example.com 2>&1 | grep -E "(SSL|TLS|certificate)" # 测试 SSL 连接 openssl s_client -connect api.example.com:443 -servername api.example.com # 检查证书过期时间 echo | openssl s_client -servername api.example.com -connect api.example.com:443 2>/dev/null | openssl x509 -noout -dates
常见 SSL 问题解决
bash# 问题 1:自签名证书 # 解决:添加到信任列表或使用 -k(仅测试) curl --cacert /path/to/self-signed.crt https://internal.example.com # 问题 2:证书链不完整 # 解决:提供完整的 CA 链 curl --cacert /path/to/fullchain.crt https://api.example.com # 问题 3:主机名不匹配 # 解决:使用正确的主机名或 resolve curl --resolve api.example.com:443:192.168.1.100 https://api.example.com # 问题 4:过期证书 # 解决:更新证书或临时跳过验证 curl -k https://api.example.com # 问题 5:中间证书缺失 # 解决:下载并指定中间证书 curl --cacert /path/to/intermediate.crt https://api.example.com
安全最佳实践
bash# 推荐:始终验证证书 curl https://api.example.com # 推荐:使用最新的 TLS 版本 curl --tlsv1.2 --tls-max tls1.3 https://api.example.com # 推荐:指定证书固定 curl --pinnedpubkey sha256//BASE64ENCODED= https://api.example.com # 推荐:检查证书撤销 curl --crlfile /path/to/crl.pem https://api.example.com # 推荐:使用 OCSP 装订 curl --cert-status https://api.example.com
SSL 相关参数汇总
| 参数 | 作用 | 示例 |
|---|---|---|
-k 或 --insecure | 忽略证书验证 | -k |
--cacert | 指定 CA 证书 | --cacert ca.crt |
--capath | 指定 CA 目录 | --capath /etc/ssl/certs |
--cert | 客户端证书 | --cert client.crt |
--key | 客户端私钥 | --key client.key |
--tlsv1.2 | 使用 TLS 1.2 | --tlsv1.2 |
--tlsv1.3 | 使用 TLS 1.3 | --tlsv1.3 |
--ciphers | 加密套件 | --ciphers 'HIGH:!aNULL' |
完整 HTTPS 调用示例
bash# 安全的 API 调用 curl -X POST https://api.example.com/v1/data \ --tlsv1.2 \ --cacert /path/to/ca-bundle.crt \ --cert /path/to/client.crt \ --key /path/to/client.key \ -H "Content-Type: application/json" \ -H "Authorization: Bearer token123" \ -d '{"action":"secure_transfer"}' \ -v