diff --git a/cpr/session.cpp b/cpr/session.cpp index f47f5a9ae..fc9df926c 100644 --- a/cpr/session.cpp +++ b/cpr/session.cpp @@ -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()); diff --git a/test/ssl_tests.cpp b/test/ssl_tests.cpp index 19ea4e4c8..c77f452fa 100644 --- a/test/ssl_tests.cpp +++ b/test/ssl_tests.cpp @@ -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(); } @@ -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!"; @@ -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(); }