Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SSL options #276

Merged
merged 7 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cpr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ add_library(${CPR_LIBRARIES}
timeout.cpp
util.cpp
unix_socket.cpp
ssl_options.cpp

# Header files (useful in IDEs)
"${CPR_INCLUDE_DIRS}/cpr/api.h"
Expand Down
74 changes: 68 additions & 6 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace cpr {

const long ON = 1L;
const long OFF = 0L;

class Session::Impl {
public:
Impl();
Expand All @@ -38,10 +41,11 @@ class Session::Impl {
void SetBody(Body&& body);
void SetBody(const Body& body);
void SetLowSpeed(const LowSpeed& low_speed);
void SetVerbose(const Verbose& verbose);
void SetVerifySsl(const VerifySsl& verify);
void SetLimitRate(const LimitRate& limit_rate);
void SetUnixSocket(const UnixSocket& unix_socket);
void SetVerbose(const Verbose& verbose);
void SetSslOptions(const SslOptions& options);

Response Delete();
Response Download(std::ofstream& file);
Expand Down Expand Up @@ -165,7 +169,7 @@ void Session::Impl::SetConnectTimeout(const ConnectTimeout& timeout) {
void Session::Impl::SetVerbose(const Verbose& verbose) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_VERBOSE, verbose.verbose);
curl_easy_setopt(curl, CURLOPT_VERBOSE, verbose.verbose ? ON : OFF);
}
}

Expand Down Expand Up @@ -338,7 +342,7 @@ void Session::Impl::SetLowSpeed(const LowSpeed& low_speed) {
void Session::Impl::SetVerifySsl(const VerifySsl& verify) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, verify ? 1L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, verify ? ON : OFF);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, verify ? 2L : 0L);
}
}
Expand All @@ -350,6 +354,58 @@ void Session::Impl::SetUnixSocket(const UnixSocket& unix_socket) {
}
}

void Session::Impl::SetSslOptions(const SslOptions& opts) {
auto curl = curl_->handle;
if (curl) {
curl_easy_setopt(curl, CURLOPT_SSLCERT, opts.cert_file.c_str());
if (!opts.cert_type.empty()) {
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE, opts.cert_type.c_str());
}
curl_easy_setopt(curl, CURLOPT_SSLKEY, opts.key_file.c_str());
if (!opts.key_type.empty()) {
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, opts.key_type.c_str());
}
if (!opts.key_pass.empty()) {
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, opts.key_pass.c_str());
}
#if SUPPORT_ALPN
curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_ALPN, opts.enable_alpn ? ON : OFF);
#endif
#if SUPPORT_NPN
curl_easy_setopt(curl, CURLOPT_SSL_ENABLE_NPN, opts.enable_npn ? ON : OFF);
#endif
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, opts.verify_peer ? ON : OFF);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, opts.verify_host ? 2L : 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYSTATUS, opts.verify_status ? ON : OFF);
curl_easy_setopt(curl, CURLOPT_SSLVERSION,
opts.ssl_version
#if SUPPORT_MAX_TLS_VERSION
| opts.max_version
#endif
);
if (!opts.ca_info.empty()) {
curl_easy_setopt(curl, CURLOPT_CAINFO, opts.ca_info.c_str());
}
if (!opts.ca_path.empty()) {
curl_easy_setopt(curl, CURLOPT_CAPATH, opts.ca_path.c_str());
}
if (!opts.crl_file.empty()) {
curl_easy_setopt(curl, CURLOPT_CRLFILE, opts.crl_file.c_str());
}
if (!opts.ciphers.empty()) {
curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, opts.ciphers.c_str());
}
#if SUPPORT_TLSv13_CIPHERS
if (!opts.tls13_ciphers.empty()) {
curl_easy_setopt(curl, CURLOPT_TLS13_CIPHERS, opts.ciphers.c_str());
}
#endif
#if SUPPORT_SESSIONID_CACHE
curl_easy_setopt(curl, CURLOPT_SSL_SESSIONID_CACHE, opts.session_id_cache ? ON : OFF);
#endif
}
}

Response Session::Impl::Delete() {
auto curl = curl_->handle;
if (curl) {
Expand Down Expand Up @@ -478,7 +534,12 @@ Response Session::Impl::makeDownloadRequest(CURL* curl, std::ofstream& file) {

auto header = cpr::util::parseHeader(header_string);
return Response{static_cast<std::int32_t>(response_code),
std::string{}, header, raw_url, elapsed, cookies, error};
std::string{},
header,
raw_url,
elapsed,
cookies,
error};
}

Response Session::Impl::makeRequest(CURL* curl) {
Expand All @@ -498,8 +559,8 @@ Response Session::Impl::makeRequest(CURL* curl) {

#if LIBCURL_VERSION_MAJOR >= 7
#if LIBCURL_VERSION_MINOR >= 21
/* enable all supported built-in compressions */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
/* enable all supported built-in compressions */
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
#endif
#endif

Expand Down Expand Up @@ -596,6 +657,7 @@ void Session::SetOption(const LowSpeed& low_speed) { pimpl_->SetLowSpeed(low_spe
void Session::SetOption(const VerifySsl& verify) { pimpl_->SetVerifySsl(verify); }
void Session::SetOption(const Verbose& verbose) { pimpl_->SetVerbose(verbose); }
void Session::SetOption(const UnixSocket& unix_socket) { pimpl_->SetUnixSocket(unix_socket); }
void Session::SetOption(const SslOptions& options) { pimpl_->SetSslOptions(options); }

Response Session::Delete() { return pimpl_->Delete(); }
Response Session::Download(std::ofstream& file) { return pimpl_->Download(file); }
Expand Down
11 changes: 0 additions & 11 deletions cpr/ssl_options.cpp

This file was deleted.

13 changes: 13 additions & 0 deletions gen-test-key.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

mkdir -p test/data
cd test/data

openssl genrsa -out ca.key 2048
openssl req -new -x509 -key ca.key -out ca.crt

openssl genrsa -out key.pem 2048
openssl req -new -key key.pem -out cert.csr
openssl x509 -req -in cert.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out cert.pem

c_rehash .
10 changes: 6 additions & 4 deletions include/cpr/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@

#include "cpr/auth.h"
#include "cpr/body.h"
#include "cpr/connect_timeout.h"
#include "cpr/cookies.h"
#include "cpr/cprtypes.h"
#include "cpr/digest.h"
#include "cpr/limit_rate.h"
#include "cpr/low_speed.h"
#include "cpr/max_redirects.h"
#include "cpr/multipart.h"
#include "cpr/parameters.h"
#include "cpr/payload.h"
#include "cpr/proxies.h"
#include "cpr/response.h"
#include "cpr/timeout.h"
#include "cpr/connect_timeout.h"
#include "cpr/ssl_options.h"
#include "cpr/timeout.h"
#include "cpr/unix_socket.h"
#include "cpr/user_agent.h"
#include "cpr/verbose.h"
#include "cpr/limit_rate.h"
#include "cpr/unix_socket.h"

namespace cpr {

Expand Down Expand Up @@ -56,6 +55,8 @@ class Session {
void SetLowSpeed(const LowSpeed& low_speed);
void SetVerifySsl(const VerifySsl& verify);
void SetUnixSocket(const UnixSocket& unix_socket);
void SetSslOptions(const SslOptions& options);
void SetVerbose(const Verbose& verbose);

// Used in templated functions
void SetOption(const Url& url);
Expand Down Expand Up @@ -83,6 +84,7 @@ class Session {
void SetOption(const VerifySsl& verify);
void SetOption(const Verbose& verbose);
void SetOption(const UnixSocket& unix_socket);
void SetOption(const SslOptions& options);

Response Delete();
Response Download(std::ofstream& file);
Expand Down
Loading