使用libcurl进行CA证书身份验证

时间:2015-02-19 19:35:34

标签: c++ ssl g++ libcurl

我在获取使用libcurl的证书时遇到问题。

C ++代码:

#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
    CURL *curl;
    CURLcode res;
    // const char* szURL = "https://www.google.se/";
    const char* szURL = "https://api-sandbox.oanda.com/";

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, szURL);

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1);
    curl_easy_setopt(curl, CURLOPT_CAINFO, "./ca.crt");

    /* Perform the request, res will get the return code */ 
    res = curl_easy_perform(curl);

    /* Check for errors */ 
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res));

    /* always cleanup */ 
    curl_easy_cleanup(curl);
    }

    curl_global_cleanup();
    return 0;
}

文件ca.crt与我的示例程序放在同一个文件夹中,文件如下:

-----开始证书----- / *很多字符和数字* / -----结束证书-----

我通过执行以下操作检索的文件内容:

openssl s_client -connect api-sandbox.oanda.com:443 > logfile

然后我复制粘贴----开始证书----- ..... ----结束证书----- 分成一个空文件并保存为ca.crt。

程序打印:

curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates

我在Windows 8上使用MinGW 64位(g ++编译器)。 我错过了什么?

我已经能够通过使用由libcurl-development包附带的VB脚本生成的ca-bundle向https://www.google.se/发出请求。显然,这是一个

的剧本
从Mozilla.org网站获取certdata.txt的脚本并创建一个 &#39; * ca-bundle.crt用于OpenSSL / libcurl / libcurl绑定

感谢您的任何指示。

1 个答案:

答案 0 :(得分:0)

我解决了。 我下载了这个文件:https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt

此文件似乎包含来自Thawte Server CA的证书。 运行openssl命令时,我们可以看到:

i:/C=US/O=thawte, Inc./OU=Certification Services Division/OU=(c) 2006 thawte, Inc. - For authorized use only/CN=thawte Primary Root CA

这暗示了Thawte。我上面发布的代码确实是正确的,似乎证书不正确。为什么那是我不知道的(任何有知识的人都可以使用openssl工具:随意发布答案)。