Skip to content

Commit 2d8d524

Browse files
committed
Fix #2251
1 parent afa88db commit 2d8d524

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

httplib.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11208,6 +11208,11 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
1120811208
return true;
1120911209
}
1121011210

11211+
if (ctx_ == nullptr) {
11212+
error = Error::SSLConnection;
11213+
last_openssl_error_ = ERR_get_error();
11214+
}
11215+
1121111216
shutdown_socket(socket);
1121211217
close_socket(socket);
1121311218
return false;

test/test.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8380,6 +8380,48 @@ TEST(SSLClientTest, ErrorReportingWhenInvalid) {
83808380
EXPECT_EQ(Error::SSLConnection, res.error());
83818381
}
83828382

8383+
TEST(SSLClientTest, Issue2251_SwappedClientCertAndKey) {
8384+
// Test for Issue #2251: SSL error not properly reported when client cert
8385+
// and key paths are swapped or mismatched
8386+
// This simulates the scenario where user accidentally swaps the cert and key
8387+
// files
8388+
8389+
// Using client cert file as private key and vice versa (completely wrong)
8390+
SSLClient cli("localhost", 8080, "client.key.pem", "client.cert.pem");
8391+
8392+
// Should fail validation due to cert/key mismatch
8393+
ASSERT_FALSE(cli.is_valid());
8394+
8395+
// Attempt to make a request should fail with proper error
8396+
auto res = cli.Get("/");
8397+
ASSERT_FALSE(res);
8398+
EXPECT_EQ(Error::SSLConnection, res.error());
8399+
8400+
// SSL error should be recorded in the Result object (this is the key fix for
8401+
// Issue #2251)
8402+
auto openssl_error = res.ssl_openssl_error();
8403+
EXPECT_NE(0u, openssl_error);
8404+
}
8405+
8406+
TEST(SSLClientTest, Issue2251_ClientCertFileNotMatchingKey) {
8407+
// Another variant: using valid file paths but with mismatched cert/key pair
8408+
// This tests the case where files exist but contain incompatible key material
8409+
8410+
// Using client cert with wrong key (cert2 key)
8411+
SSLClient cli("localhost", 8080, "client.cert.pem", "key.pem");
8412+
8413+
// Should fail validation
8414+
ASSERT_FALSE(cli.is_valid());
8415+
8416+
auto res = cli.Get("/");
8417+
ASSERT_FALSE(res);
8418+
// Must report error properly, not appear as success
8419+
EXPECT_EQ(Error::SSLConnection, res.error());
8420+
8421+
// OpenSSL error should be captured in Result
8422+
EXPECT_NE(0u, res.ssl_openssl_error());
8423+
}
8424+
83838425
#if 0
83848426
TEST(SSLClientTest, SetInterfaceWithINET6) {
83858427
auto cli = std::make_shared<httplib::Client>("https://httpbin.org");

0 commit comments

Comments
 (0)