c ++ libcurl无法访问,命令行有效

c++ libcurl fails to access, command line works

我正在尝试使用 TLSv1.2 连接从 curl 访问 Web 服务。 我能够通过命令行成功访问该服务:

  curl -l --tlsv1.2 -E client.pem -v https://test-as.sgx.trustedservices.intel.com:443/attestation/sgx/v1/sigrl/00000010

但是当在 C++ 中使用 libcurl 尝试它时,我收到错误:

 error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure

这是代码的简短版本:

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

int main(void) {
    CURL *curl;
    CURLcode res = CURLE_OK;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "https://test-as.sgx.trustedservices.intel.com:443/attestation/sgx/v1/sigrl/00000010");

        curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
        curl_easy_setopt(curl, CURLOPT_CAINFO, "./client.pem");
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);

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

        curl_easy_cleanup(curl);
    }

    return (int)res;
}

我还需要设置其他选项吗?

curl 的版本:

 curl --version
 curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3

libcurl 的版本:

 ii  libcurl3:amd64                              7.47.0-1ubuntu2.2        
ii  libcurl3-gnutls:amd64                       7.47.0-1ubuntu2.2                        
ii  libcurl4-openssl-dev:amd64                  7.47.0-1ubuntu2.2                                    

似乎问题是您错误地使用了您的客户端证书,因为您将它设置为用作 CA 来验证服务器端证书

curl_easy_setopt(curl, CURLOPT_CAINFO, "./client.pem");

这与 client.pem 在带有 -E 标志的命令行中的使用方式不匹配。

-E, --cert <certificate[:password]>
          (SSL) Tells curl to use the specified client certificate file when getting a file with HTTPS, FTPS or another SSL-based  protocol. 

尝试删除该行并改用以下行:

curl_easy_setopt(curl, CURLOPT_SSLCERT, "./client.pem");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

如果可行,您应该将行设置 CURLOPT_SSL_VERIFYPEER 删除为 0 并尝试设置适当的 CA 来验证服务器端证书。