【问题标题】:libcurl https example errorlibcurl https 示例错误
【发布时间】:2018-07-25 04:08:30
【问题描述】:

我从 curl 网页下载并稍微修改了这个 c example。测试应用程序使用我之前构建的 curl.dll。如果不禁用对等验证,我将无法连接到 https 服务器。

我原来的测试应用:

curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(curl) 
{
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
        printf ("res = %d curl_easy_perform() failed: %s\n",res, curl_easy_strerror(res));
    curl_easy_cleanup(curl);
}
curl_global_cleanup();

输出是:

*   Trying XX.XXX.XXX.XX...
* TCP_NODELAY set
* Connected to example.com (XX.XXX.XXX.XX) port 443 (#0)
* ALPN, offering http/1.1
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
res = 60 curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates

如果我从 mozilla 下载 cacert.pem 文件并将其存储在我的 exe 旁边并将示例修改为:

curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(curl) 
{
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    curl_easy_setopt(curl, CURLOPT_CAPATH, ".");
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
        printf ("res = %d curl_easy_perform() failed: %s\n",res, curl_easy_strerror(res));
    curl_easy_cleanup(curl);
}
curl_global_cleanup();

我仍然收到错误消息

*   Trying XX.XXX.XXX.XX...
* TCP_NODELAY set
* Connected to example.com (XX.XXX.XXX.XX) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: none
      CApath: .
* SSL certificate problem: unable to get local issuer certificate
* Closing connection 0
res = 60 curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates

如果我禁用对等验证,它工作正常。

curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(curl) 
{
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    curl_easy_setopt(curl, CURLOPT_CAPATH, ".");
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
        printf ("res = %d curl_easy_perform() failed: %s\n",res, curl_easy_strerror(res));
    curl_easy_cleanup(curl);
}
curl_global_cleanup();

有谁知道是什么导致了这种行为?我必须设置对等证书吗?我还没有找到这个的二传手。

【问题讨论】:

标签: c curl https ssl-certificate


【解决方案1】:

我通过添加选项 CAINFO

解决了这个问题
curl_global_init(CURL_GLOBAL_DEFAULT);
CURL *curl = curl_easy_init();
if(curl)
{
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
    curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem");
    CURLcode res = curl_easy_perform(curl);
    if(res != CURLE_OK)
        printf ("res = %d curl_easy_perform() failed: %s\n",res, curl_easy_strerror(res));
    curl_easy_cleanup(curl);
}
curl_global_cleanup();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 2017-10-26
    • 2020-11-10
    • 1970-01-01
    • 2015-11-29
    相关资源
    最近更新 更多