Skip to content

Commit

Permalink
[BUG] Fix cpr::ssl:KeyBlob: Copy blob to curl
Browse files Browse the repository at this point in the history
  • Loading branch information
b3rgschu3tz committed Dec 4, 2024
1 parent 1d296d5 commit 75f8ad9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions cpr/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ void Session::SetSslOptions(const SslOptions& options) {
// NOLINTNEXTLINE (readability-container-data-pointer)
blob.data = &key_blob[0];
blob.len = key_blob.length();
blob.flags = CURL_BLOB_COPY;
curl_easy_setopt(curl_->handle, CURLOPT_SSLKEY_BLOB, &blob);
if (!options.key_type.empty()) {
curl_easy_setopt(curl_->handle, CURLOPT_SSLKEYTYPE, options.key_type.c_str());
Expand Down
29 changes: 25 additions & 4 deletions test/ssl_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ static std::string serverPubKeyPath;
static std::string clientKeyPath;
static std::string clientCertPath;

std::string loadCertificateFromFile(const std::string certPath) {
std::ifstream certFile(certPath);
std::string loadFileContent(const std::string filePath) {
std::ifstream file(filePath);
std::stringstream buffer;
buffer << certFile.rdbuf();
buffer << file.rdbuf();
return buffer.str();
}

Expand Down Expand Up @@ -147,7 +147,7 @@ TEST(SslTests, LoadCertFromBufferTestSimpel) {
std::string baseDirPath{server->getBaseDirPath()};
std::string crtPath{baseDirPath + "certificates/"};
std::string keyPath{baseDirPath + "keys/"};
std::string certBuffer = loadCertificateFromFile(crtPath + "ca-bundle.crt");
std::string certBuffer = loadFileContent(crtPath + "ca-bundle.crt");
SslOptions sslOpts = Ssl(ssl::CaBuffer{std::move(certBuffer)}, ssl::CertFile{crtPath + "client.crt"}, ssl::KeyFile{keyPath + "client.key"}, ssl::VerifyPeer{true}, ssl::VerifyHost{true}, ssl::VerifyStatus{false});
Response response = cpr::Get(url, sslOpts, Timeout{5000}, Verbose{});
std::string expected_text = "Hello world!";
Expand All @@ -159,6 +159,27 @@ TEST(SslTests, LoadCertFromBufferTestSimpel) {
}
#endif

#if SUPPORT_CURLOPT_SSLKEY_BLOB
TEST(SslTests, LoadKeyFromBlobTestSimpel) {
std::this_thread::sleep_for(std::chrono::seconds(1));

Url url{server->GetBaseUrl() + "/hello.html"};

std::string baseDirPath{server->getBaseDirPath()};
std::string crtPath{baseDirPath + "certificates/"};
std::string keyPath{baseDirPath + "keys/"};
std::string keyBuffer = loadFileContent(keyPath + "client.key");
SslOptions sslOpts = Ssl(ssl::CaInfo{crtPath + "ca-bundle.crt"}, ssl::CertFile{crtPath + "client.crt"}, ssl::KeyBlob{std::move(keyBuffer)}, ssl::VerifyPeer{true}, ssl::VerifyHost{true}, ssl::VerifyStatus{false});
Response response = cpr::Get(url, sslOpts, Timeout{5000}, Verbose{});
std::string expected_text = "Hello world!";
EXPECT_EQ(expected_text, response.text);
EXPECT_EQ(url, response.url);
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
EXPECT_EQ(200, response.status_code);
EXPECT_EQ(ErrorCode::OK, response.error.code) << response.error.message;
}
#endif

fs::path GetBasePath(const std::string& execPath) {
return fs::path(fs::path{execPath}.parent_path().string() + "/").make_preferred();
}
Expand Down

0 comments on commit 75f8ad9

Please sign in to comment.