Skip to content

Commit 483ba60

Browse files
author
jhazik
committed
Fix #2217 Transmit X509_STORE between SSLClient-s during redirect
1 parent abc1896 commit 483ba60

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

httplib.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10856,6 +10856,7 @@ inline SSLClient::SSLClient(const std::string &host, int port,
1085610856
}
1085710857

1085810858
inline SSLClient::~SSLClient() {
10859+
if (ca_cert_store_) { X509_STORE_free(ca_cert_store_); }
1085910860
if (ctx_) { SSL_CTX_free(ctx_); }
1086010861
// Make sure to shut down SSL since shutdown_ssl will resolve to the
1086110862
// base function rather than the derived function once we get to the
@@ -10870,7 +10871,8 @@ inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
1087010871
if (ctx_) {
1087110872
if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
1087210873
// Free memory allocated for old cert and use new store `ca_cert_store`
10873-
SSL_CTX_set_cert_store(ctx_, ca_cert_store);
10874+
SSL_CTX_set1_cert_store(ctx_, ca_cert_store);
10875+
ca_cert_store_ = ca_cert_store;
1087410876
}
1087510877
} else {
1087610878
X509_STORE_free(ca_cert_store);

test/test.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8963,6 +8963,45 @@ TEST(HttpToHttpsRedirectTest, CertFile) {
89638963
ASSERT_EQ(StatusCode::OK_200, res->status);
89648964
}
89658965

8966+
TEST(SSLClientRedirectTest, CertFile) {
8967+
SSLServer ssl_svr1(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
8968+
ASSERT_TRUE(ssl_svr1.is_valid());
8969+
ssl_svr1.Get("/index", [&](const Request &, Response &res) {
8970+
res.set_redirect("https://127.0.0.1:1235/index");
8971+
ssl_svr1.stop();
8972+
});
8973+
8974+
SSLServer ssl_svr2(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
8975+
ASSERT_TRUE(ssl_svr2.is_valid());
8976+
ssl_svr2.Get("/index", [&](const Request &, Response &res) {
8977+
res.set_content("test", "text/plain");
8978+
ssl_svr2.stop();
8979+
});
8980+
8981+
thread t = thread([&]() { ASSERT_TRUE(ssl_svr1.listen("127.0.0.1", PORT)); });
8982+
thread t2 = thread([&]() { ASSERT_TRUE(ssl_svr2.listen("127.0.0.1", 1235)); });
8983+
auto se = detail::scope_exit([&] {
8984+
t2.join();
8985+
t.join();
8986+
ASSERT_FALSE(ssl_svr1.is_running());
8987+
});
8988+
8989+
ssl_svr1.wait_until_ready();
8990+
ssl_svr2.wait_until_ready();
8991+
8992+
SSLClient cli("127.0.0.1", PORT);
8993+
std::string cert;
8994+
read_file(SERVER_CERT2_FILE, cert);
8995+
cli.load_ca_cert_store(cert.c_str(), cert.size());
8996+
cli.enable_server_certificate_verification(true);
8997+
cli.set_follow_location(true);
8998+
cli.set_connection_timeout(30);
8999+
9000+
auto res = cli.Get("/index");
9001+
ASSERT_TRUE(res);
9002+
ASSERT_EQ(StatusCode::OK_200, res->status);
9003+
}
9004+
89669005
TEST(MultipartFormDataTest, LargeData) {
89679006
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
89689007

0 commit comments

Comments
 (0)