【发布时间】:2015-02-19 19:35:34
【问题描述】:
我在获取证书以使用 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
然后我复制粘贴了 ----BEGIN CERTIFICATE----- ..... ----END CERTIFICATE----- 部分放入一个空文件并将其保存为 ca.crt。
程序打印:
curl_easy_perform() failed: Peer certificate cannot be authenticated with given CA certificates
我在 Windows 8 上使用 MinGW 64 位(g++ 编译器)。 我错过了什么?
我已经能够通过使用 ca-bundle 向https://www.google.se/ 发出请求,该 ca-bundle 由 libcurl-development 包附带的 VB 脚本生成。显然,这是一个脚本
从 Mozilla.org 站点获取 certdata.txt 并创建一个 '* ca-bundle.crt 用于 OpenSSL / libcurl / libcurl 绑定
感谢您的任何指点。
【问题讨论】: