Skip to content

Commit c44f8d5

Browse files
authored
Merge pull request libcpr#1151 from b3rgschu3tz/bugfix/fix_ssl_KeyBlob
[BUG] Fix cpr::ssl:KeyBlob: Copy blob to curl
2 parents 2bb749d + 75f8ad9 commit c44f8d5

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

cpr/session.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ void Session::SetSslOptions(const SslOptions& options) {
473473
// NOLINTNEXTLINE (readability-container-data-pointer)
474474
blob.data = &key_blob[0];
475475
blob.len = key_blob.length();
476+
blob.flags = CURL_BLOB_COPY;
476477
curl_easy_setopt(curl_->handle, CURLOPT_SSLKEY_BLOB, &blob);
477478
if (!options.key_type.empty()) {
478479
curl_easy_setopt(curl_->handle, CURLOPT_SSLKEYTYPE, options.key_type.c_str());

test/ssl_tests.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ static std::string serverPubKeyPath;
2121
static std::string clientKeyPath;
2222
static std::string clientCertPath;
2323

24-
std::string loadCertificateFromFile(const std::string certPath) {
25-
std::ifstream certFile(certPath);
24+
std::string loadFileContent(const std::string filePath) {
25+
std::ifstream file(filePath);
2626
std::stringstream buffer;
27-
buffer << certFile.rdbuf();
27+
buffer << file.rdbuf();
2828
return buffer.str();
2929
}
3030

@@ -147,7 +147,7 @@ TEST(SslTests, LoadCertFromBufferTestSimpel) {
147147
std::string baseDirPath{server->getBaseDirPath()};
148148
std::string crtPath{baseDirPath + "certificates/"};
149149
std::string keyPath{baseDirPath + "keys/"};
150-
std::string certBuffer = loadCertificateFromFile(crtPath + "ca-bundle.crt");
150+
std::string certBuffer = loadFileContent(crtPath + "ca-bundle.crt");
151151
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});
152152
Response response = cpr::Get(url, sslOpts, Timeout{5000}, Verbose{});
153153
std::string expected_text = "Hello world!";
@@ -159,6 +159,27 @@ TEST(SslTests, LoadCertFromBufferTestSimpel) {
159159
}
160160
#endif
161161

162+
#if SUPPORT_CURLOPT_SSLKEY_BLOB
163+
TEST(SslTests, LoadKeyFromBlobTestSimpel) {
164+
std::this_thread::sleep_for(std::chrono::seconds(1));
165+
166+
Url url{server->GetBaseUrl() + "/hello.html"};
167+
168+
std::string baseDirPath{server->getBaseDirPath()};
169+
std::string crtPath{baseDirPath + "certificates/"};
170+
std::string keyPath{baseDirPath + "keys/"};
171+
std::string keyBuffer = loadFileContent(keyPath + "client.key");
172+
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});
173+
Response response = cpr::Get(url, sslOpts, Timeout{5000}, Verbose{});
174+
std::string expected_text = "Hello world!";
175+
EXPECT_EQ(expected_text, response.text);
176+
EXPECT_EQ(url, response.url);
177+
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
178+
EXPECT_EQ(200, response.status_code);
179+
EXPECT_EQ(ErrorCode::OK, response.error.code) << response.error.message;
180+
}
181+
#endif
182+
162183
fs::path GetBasePath(const std::string& execPath) {
163184
return fs::path(fs::path{execPath}.parent_path().string() + "/").make_preferred();
164185
}

0 commit comments

Comments
 (0)