Initializing SSL and libcurl and getting “out of m

2020-04-16 05:48发布

I intend to do https requests with libcurl and openssl with a C++ program.

I initialized libcurl with curl_global_init(CURL_GLOBAL_ALL) as described in the documentation. Then I use an curl_easy handle that I initialize, populate with headers and a body, and send everything out to ´https://example.com:443/foo´. It works for non-https connections.

Looking around I find that there may be other libraries that are already getting an SSL context which is what causes libcurl to fail in doing precisely that. I get the following error message:

curl_easy_perform failed: Out of memory

In my case I am using libmicrohttpd which I initialize with

mhdDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL,
                               htons(port),
                               NULL,
                               NULL,
                               connectionTreat,                     NULL,
                               MHD_OPTION_HTTPS_MEM_KEY,            httpsKey,
                               MHD_OPTION_HTTPS_MEM_CERT,           httpsCertificate,
                               MHD_OPTION_CONNECTION_MEMORY_LIMIT,  memoryLimit,
                               MHD_OPTION_SOCK_ADDR,                (struct sockaddr*) &sad,
                               MHD_OPTION_NOTIFY_COMPLETED,         requestCompleted, NULL,
                               MHD_OPTION_END);

So I am indeed using openSSL somewhere else. The thing is, if I take out the MHD_USE_SSL part it does not fix the problem.

This is the list of libraries that are linked to the application (I'm using cmake):

-lmicrohttpd
-lmongoclient
-lboost_thread
-lboost_filesystem
-lboost_system
-lpthread

Is there any of the others that could be loading SSL? Is microhttpd loading it anyways even if I comment out the MHD_USE_SSL flag (plus all other related flags)? Could there be any other reason for this error?

标签: c++ ssl libcurl
2条回答
放我归山
2楼-- · 2020-04-16 06:20

As the descibed in this answer, you might need to disable SSLv3 if you are on Ubuntu 16.04 like so

curl_easy_setopt(curl_, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2));

It was disabled on Ubuntu 16.04 for security reasons, see more here.

查看更多
地球回转人心会变
3楼-- · 2020-04-16 06:35

I'm not aware of any problem in libcurl that would cause this error code to get returned if indeed a memory allocation function doesn't fail. Using OpenSSL in multiple modules does not cause such a failure. (I am the lead developer of libcurl.)

So, run your app with VERBOSE set to libcurl, or even strace to see which syscall that fails and it should give you more clues.

查看更多
登录 后发表回答