0

I am not very familiar with curl or HTTP requests, but trying to learn.

In my case, I am trying to use libcurl in C++ (using Visual Studio 2019 on Windows 10) to perform a GET request. I tried solutions from Curl in C++ - Can't get data from HTTPS, but the only thing that worked for me was to disable SSL peer verification using:

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);

Here is my code:

void getPairPrice(string & pair) {
    string url(BINANCE_HOST);
    url += "/api/v3/ticker/price?symbol=";
    url += pair;
    CURL* curl;
    CURLcode res;
    std::string readBuffer;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
        curl_easy_setopt(curl, CURLOPT_URL,  url.c_str());
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_easy_setopt(curl, CURLOPT_ENCODING, "gzip");

        struct curl_slist* headers = NULL;
        headers = curl_slist_append(headers, PUBLIC_KEY_HEADER);
        headers = curl_slist_append(headers, CONTENT_TYPE_HEADER);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

        res = curl_easy_perform(curl);

        //cout << "res : " << res << endl;
        std::cout <<  "GET symbol " << pair << " price : "  << readBuffer << std::endl;
        const string jsonKey = "price";
        cout << "Price : " << extractJsonValue(readBuffer, jsonKey) << endl;
    }
    curl_easy_cleanup(curl);
    curl_global_cleanup();
}

Without disabling the SSL_VERIFYPEER option, the response is always 77. This is fine for testing, but I would like to know how to solve that when releasing my software. It seems that I should somehow download the host's SSL certificate in PEM format and point libcurl to it.

Can anyone help?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

0

Error 77 is CURLE_SSL_CACERT_BADFILE

Problem with reading the SSL CA cert (path? access rights?)

The CURLOPT_SSL_VERIFYPEER documentation says:

When negotiating a TLS or SSL connection, the server sends a certificate indicating its identity. Curl verifies whether the certificate is authentic, i.e. that you can trust that the server is who the certificate says it is. This trust is based on a chain of digital signatures, rooted in certification authority (CA) certificates you supply. curl uses a default bundle of CA certificates (the path for that is determined at build time) and you can specify alternate certificates with the CURLOPT_CAINFO option or the CURLOPT_CAPATH option.

When CURLOPT_SSL_VERIFYPEER is enabled, and the verification fails to prove that the certificate is authentic, the connection fails. When the option is zero, the peer certificate verification succeeds regardless.

So yes, if you intend to validate the server's certificate, you need to provide libcurl with the Certificate Authority (CA) that was used to sign the server's certificate. If you don't have the CA available, you can't validate the certificate.

The documentation also says:

Authenticating the certificate is not enough to be sure about the server. You typically also want to ensure that the server is the server you mean to be talking to. Use CURLOPT_SSL_VERIFYHOST for that. The check that the host name in the certificate is valid for the host name you're connecting to is done independently of the CURLOPT_SSL_VERIFYPEER option.

WARNING: disabling verification of the certificate allows bad guys to man-in-the-middle the communication without you knowing it. Disabling verification makes the communication insecure. Just having encryption on a transfer is not enough as you cannot be sure that you are communicating with the correct end-point.

NOTE: even when this option is disabled, depending on the used TLS backend, curl may still load the certificate file specified in CURLOPT_CAINFO. curl default settings in some distributions might use quite a large file as a default setting for CURLOPT_CAINFO, so loading the file can be quite expensive, especially when dealing with many connections. Thus, in some situations, you might want to disable verification fully to save resources by setting CURLOPT_CAINFO to NULL - but please also consider the warning above!

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770