为什么 cURL 不能在 Windows 上正确验证证书?

Che*_*eso 32 windows curl

当我尝试在 Windows 上使用 Curl 来检索httpsurl 时,我收到了可怕的“连接错误 (60)”。

在此处输入图片说明

确切的错误消息是:

curl: (60) SSL 证书问题,验证 CA 证书是否正常。详细信息:
错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败
更多详细信息:http ://curl.haxx.se/docs/sslcerts.html

如何解决这个问题?

Che*_*eso 39

我不知道为什么,但我没有在一个地方找到这些信息。

  1. 下载支持 SSL 的 Curl 版本,或者自己构建支持 SSL 的版本。

  2. http://curl.haxx.se/docs/caextract.html下载 cacert.pem 文件。

  3. 将 curl.exe 和 .pem 文件放在同一目录中。

  4. cacert.pem文件重命名为curl-ca-bundle.crt

  5. 重新运行 curl.exe !


编辑:

还有其他方法可以解决问题。这种特殊方式依赖于 Curl 制造商生产的 cacert。这可能不是您想要的,特别是,它可能不适用于您对 SSL 站点使用的证书拥有不太知名的认证机构(例如只有您的公司知道的机构)的情况. 在这种情况下,您将需要生成自己的curl-ca-bundle.crt文件。您可以使用 certreq.exe 和 openssl.exe 从 IE/Windows 商店导出这样的证书,然后分别转换为 pem 格式。


Ram*_*ein 8

我创建了一个 PowerShell 脚本,该脚本能够ca-cert.crt根据安装在 Windows 证书存储(CurrentUser 或 LocalMachine)中的 CA 证书编写文件。像这样运行脚本:

CreateCaCert.ps1 -StoreLocation CurrentUser | Out-File -Encoding utf8 curl-ca-cert.crt
Run Code Online (Sandbox Code Playgroud)

这将创建curl-ca-cert.crt应该存储在相同目录中的文件,curl.exe并且您应该能够像在 Windows 应用程序中一样验证相同的站点(请注意,此文件也可以被 使用git)。

“官方”脚本可以在GitHub找到,但这里列出了初始版本以供参考:

[CmdletBinding()]
Param(
    [ValidateSet(
        [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser,
        [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)]
    [string]
    $StoreLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
)

$maxLineLength = 77

# Open the store
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store ([System.Security.Cryptography.X509Certificates.StoreName]::Root, $StoreLocation)
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly);

# Write header
Write-Output "# Root certificates ($StoreLocation) generated at $(Get-Date)"

# Write all certificates
Foreach ($certificate in $store.Certificates)
{
    # Start with an empty line
    Write-Output ""

    # Convert the certificate to a BASE64 encoded string
    $certString = [Convert]::ToBase64String($certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert));

    # Write the actual certificate
    Write-Output "# Friendly name: $($certificate.FriendlyName)"
    Write-Output "# Issuer:        $($certificate.Issuer)"
    Write-Output "# Expiration:    $($certificate.GetExpirationDateString())"
    Write-Output "# Serial:        $($certificate.SerialNumber)"
    Write-Output "# Thumbprint:    $($certificate.Thumbprint)"
    Write-Output "-----BEGIN CERTIFICATE-----"
    For ($i = 0; $i -lt $certString.Length; $i += $maxLineLength)
    {
        Write-Output $certString.Substring($i, [Math]::Min($maxLineLength, $certString.Length - $i))
    }
    Write-Output "-----END CERTIFICATE-----"
}
Run Code Online (Sandbox Code Playgroud)