Skip to content

Commit

Permalink
Fixed non OpenSSl based builds
Browse files Browse the repository at this point in the history
  • Loading branch information
COM8 committed May 24, 2024
1 parent 1ca7e4d commit 0436415
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ cpr_option(CPR_ENABLE_SSL "Enables or disables the SSL backend. Required to perf
cpr_option(CPR_FORCE_OPENSSL_BACKEND "Force to use the OpenSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
cpr_option(CPR_FORCE_WINSSL_BACKEND "Force to use the WinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
cpr_option(CPR_FORCE_DARWINSSL_BACKEND "Force to use the DarwinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
cpr_option(CPR_FORCE_MBEDTLS_BACKEND "Force to use the Mbed TLS backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
cpr_option(CPR_FORCE_MBEDTLS_BACKEND "Force to use the Mbed TLS backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, CPR_FORCE_MBEDTLS_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." ON)
cpr_option(CPR_ENABLE_LINTING "Set to ON to enable clang linting." OFF)
cpr_option(CPR_ENABLE_CPPCHECK "Set to ON to enable Cppcheck static analysis. Requires CPR_BUILD_TESTS and CPR_BUILD_TESTS_SSL to be OFF to prevent checking google tests source code." OFF)
cpr_option(CPR_BUILD_TESTS "Set to ON to build cpr tests." OFF)
Expand Down
10 changes: 6 additions & 4 deletions cpr/callback.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include <functional>
#ifdef OPENSSL_BACKEND_USED
#include <iostream>
#endif // OPENSSL_BACKEND_USED

#include "cpr/callback.h"
#include "cpr/cprtypes.h"
#include <curl/curl.h>

#if SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#ifdef OPENSSL_BACKEND_USED
#include <openssl/bio.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
Expand All @@ -21,7 +23,7 @@
#else
#include <openssl/ossl_typ.h>
#endif
#endif // SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#endif // OPENSSL_BACKEND_USED

namespace cpr {

Expand All @@ -33,7 +35,7 @@ bool CancellationCallback::operator()(cpr_pf_arg_t dltotal, cpr_pf_arg_t dlnow,
return user_cb ? (cont_operation && (*user_cb)(dltotal, dlnow, ultotal, ulnow)) : cont_operation;
}

#if SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#ifdef OPENSSL_BACKEND_USED
namespace ssl {
/**
* The ssl_ctx parameter is actually a pointer to the SSL library's SSL_CTX for OpenSSL.
Expand Down Expand Up @@ -87,5 +89,5 @@ CURLcode tryLoadCaCertFromBuffer(CURL* /*curl*/, void* sslctx, void* raw_cert_bu
return CURLE_OK;
}
} // namespace ssl
#endif // SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#endif // OPENSSL_BACKEND_USED
} // namespace cpr
5 changes: 4 additions & 1 deletion cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,16 @@ void Session::SetSslOptions(const SslOptions& options) {
curl_easy_setopt(curl_->handle, CURLOPT_SSL_CTX_FUNCTION, ssl::tryLoadCaCertFromBuffer);
curl_easy_setopt(curl_->handle, CURLOPT_SSL_CTX_DATA, options.ca_buffer.c_str());
}
#endif

if (options.ssl_ctx_cb.callback) {
cbs_->sslctxcb_ = options.ssl_ctx_cb;
cbs_->sslctxcb_.SetCurlHolder(curl_);
curl_easy_setopt(curl_->handle, CURLOPT_SSL_CTX_FUNCTION, cpr::util::sslCtxUserFunction);
curl_easy_setopt(curl_->handle, CURLOPT_SSL_CTX_DATA, &cbs_->sslctxcb_);
} else if (options.ca_buffer.empty()) {
}
#ifdef OPENSSL_BACKEND_USED
else if (options.ca_buffer.empty()) {
curl_easy_setopt(curl_->handle, CURLOPT_SSL_CTX_FUNCTION, nullptr);
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/cpr/callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,9 @@ class SslCtxCallback {
}
};

#ifdef OPENSSL_BACKEND_USED
CURLcode tryLoadCaCertFromBuffer(CURL* curl, void* sslctx, void* raw_cert_buf);
#endif
} // namespace ssl
#endif
} // namespace cpr
Expand Down
6 changes: 6 additions & 0 deletions include/cpr/ssl_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,13 +320,15 @@ class CaPath {
};

#if SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#ifdef OPENSSL_BACKEND_USED
class CaBuffer {
public:
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
CaBuffer(std::string&& p_buffer) : buffer(std::move(p_buffer)) {}

const std::string buffer;
};
#endif // OPENSSL_BACKEND_USED
#endif

// specify a Certificate Revocation List file
Expand Down Expand Up @@ -436,7 +438,9 @@ struct SslOptions {
// We don't use fs::path here, as this leads to problems using windows
std::string ca_path;
#if SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#ifdef OPENSSL_BACKEND_USED
std::string ca_buffer;
#endif // OPENSSL_BACKEND_USED
#endif
// We don't use fs::path here, as this leads to problems using windows
std::string crl_file;
Expand Down Expand Up @@ -568,9 +572,11 @@ struct SslOptions {
ca_path = opt.filename.string();
}
#if SUPPORT_CURLOPT_SSL_CTX_FUNCTION
#ifdef OPENSSL_BACKEND_USED
void SetOption(const ssl::CaBuffer& opt) {
ca_buffer = opt.buffer;
}
#endif // OPENSSL_BACKEND_USED
#endif
void SetOption(const ssl::Crl& opt) {
crl_file = opt.filename.string();
Expand Down

0 comments on commit 0436415

Please sign in to comment.