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

[BUG] Fix cpr::ssl:KeyBlob: Copy blob to curl #1151

Merged
merged 1 commit into from
Dec 14, 2024
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: 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
Loading