C++ and cUrl : how to get SSL error codes

2019-03-01 07:35发布

I am establishing a connection to a secured server over SSL. Everything works fine, my CAcertificate is well used through

retCode=curl_easy_setopt(handleCurl, CURLOPT_CAINFO, sSSLCertificate);
retCode=curl_easy_setopt(handleCurl, CURLOPT_SSL_VERIFYPEER, 1);

I'm stuck when i try to manage curl errors. Basically i want to be able to be notified when a problem happens with the SSL (wrong cacert.pem, server identity not verified etc).

Nothing happens when CURLOPT_CAINFO is given an empty CAcert, retCode is OK.

I tried to get info after the request with that :

res = curl_easy_getinfo(m_pHandleCurl, CURLINFO_SSL_VERIFYRESULT, &lSSLVerifyResult);

But it always tells me that everithing is fine.

What am i missing ?

1条回答
做自己的国王
2楼-- · 2019-03-01 07:45

Add to your connection setup code:

// Make sure this is NOT a stack variable! The buffer
// must be available through whole live of the connection
char buffer[CURL_ERROR_SIZE+1] = {};

retCode=curl_easy_setopt(handleCurl, CURLOPT_ERRORBUFFER, buffer);

then when your connection has ended, check what's in the buffer - you should be able to see some hints regarding SSL state, too. It will be empty if no error occurred.

If you want actual code, numeric CURLcode is always returned by curl_easy_perform for easy handles.

If you use multi handles, use curl_multi_info_read instead. Here is example:

int u = 0;
if (CURLM_OK == curl_multi_perform(multi_, &u))
{
  int q = 0;
  CURLMsg *msg = NULL;
  while ((msg = curl_multi_info_read(multi_, &q)) != NULL)
  {
    if (msg->msg == CURLMSG_DONE)
    {
      CURL* easy = msg->easy_handle;
      CURLcode code = msg->data.result;
      // . . .
    }
  }
}
查看更多
登录 后发表回答