在C ++中使用LibCurl和自签名证书

时间:2016-05-29 00:23:52

标签: c++ libcurl

我使用libcurl和ssl时遇到了问题。

如果我尝试使用以下curl命令连接到我的网站:

curl -q --cert client-2048.crt --key client-2048.key https:// ****** -d“username = & password = “ - H”X-Application:curlCommandLineTest“

一切顺利(证书是自签名的)

如何使用相同的usign libcurl?

我试图遵循libcurl ssl示例但是证书和私钥 有不同的扩展名,所以我不知道从哪里开始。

到目前为止,我尝试了以下(以及许多其他组合):

static const char *pCertFile = "client-2048.crt";
static const char *pCACertFile = "client-2048.pem";
static const char *pKeyName = "client-2048.key";

curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();
if (curl) {
    /* what call to write: */
    curl_easy_setopt(curl, CURLOPT_URL, "https://*****");
    curl_easy_setopt(curl, CURLOPT_HEADERDATA, headerfile);

        /* cert is stored PEM coded in file... */
        /* since PEM is default, we needn't set it for PEM */
        curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, "PEM");

        /* set the cert for client authentication */
        curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);

        /* set the private key (file or ID in engine) */
        curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyName);

        /* disconnect if we can't validate server's cert */
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);

        /* 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));

        /* we are done... */
    } while (0);
    /* always cleanup */
    curl_easy_cleanup(curl);
    return 0;

但是我收到了消息:

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

那么Libcurl代码反映成功的调用会是什么?

由于

1 个答案:

答案 0 :(得分:1)

自签名证书:

curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,0L);