mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-20 19:51:54 +00:00
Verify WebSocketClient certificates with CryptoAPI on Windows
SSLClient verifies the server certificate chain with CryptoAPI on Windows by default, while WebSocketClient always left it to the TLS backend, whose snapshot of the Windows store lacks the roots Windows fetches on demand. A wss:// connection could fail against a host that SSLClient connects to. WebSocketClient now makes the same decision through a shared helper, and gets enable_windows_certificate_verification() to turn it off. Fix #2685
This commit is contained in:
1 parent
9458c27c7c
commit
a6ab971a3f
4 files changed
+79
-10
No files matched your search
@@ -252,6 +252,9 @@ void enable_system_ca(bool enabled);
|
|||||||
void set_server_certificate_verifier(tls::VerifyCallback verifier);
|
void set_server_certificate_verifier(tls::VerifyCallback verifier);
|
||||||
void set_session_verifier(
|
void set_session_verifier(
|
||||||
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
||||||
|
// Windows only, as on SSLClient (see "Platform-specific Certificate Handling"
|
||||||
|
// in README.md)
|
||||||
|
void enable_windows_certificate_verification(bool enabled);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ On Windows, the CryptoAPI check can also be turned off at runtime. The TLS backe
|
|||||||
cli.enable_windows_certificate_verification(false);
|
cli.enable_windows_certificate_verification(false);
|
||||||
```
|
```
|
||||||
|
|
||||||
`WebSocketClient` does not use CryptoAPI; the TLS backend always verifies its chain.
|
`WebSocketClient` verifies the same way and has the same `enable_windows_certificate_verification()`.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
|
> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
|
||||||
|
|||||||
@@ -4922,6 +4922,9 @@ public:
|
|||||||
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
||||||
void set_proxy_digest_auth(const std::string &username,
|
void set_proxy_digest_auth(const std::string &username,
|
||||||
const std::string &password);
|
const std::string &password);
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
void enable_windows_certificate_verification(bool enabled);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -4994,6 +4997,10 @@ private:
|
|||||||
std::function<SSLVerifierResponse(tls::session_t)> session_verifier_;
|
std::function<SSLVerifierResponse(tls::session_t)> session_verifier_;
|
||||||
std::string proxy_digest_auth_username_;
|
std::string proxy_digest_auth_username_;
|
||||||
std::string proxy_digest_auth_password_;
|
std::string proxy_digest_auth_password_;
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
bool enable_windows_cert_verification_ = true;
|
||||||
|
bool server_certificate_verifier_set_ = false;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -11503,6 +11510,21 @@ inline bool load_client_ca_config(tls::ctx_t ctx,
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
// Schannel is skipped when a custom CA is specified, as the Windows
|
||||||
|
// certificate store would not know about it, and when system CA trust is
|
||||||
|
// explicitly disabled.
|
||||||
|
inline bool use_windows_cert_verification(bool enabled,
|
||||||
|
const std::string &ca_cert_file_path,
|
||||||
|
const std::string &ca_cert_dir_path,
|
||||||
|
bool has_custom_store,
|
||||||
|
SystemCAMode mode) {
|
||||||
|
return enabled && mode != SystemCAMode::Disabled &&
|
||||||
|
ca_cert_file_path.empty() && ca_cert_dir_path.empty() &&
|
||||||
|
!has_custom_store;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
// Serializes the CA certificates installed in ctx as PEM, so they can be
|
// Serializes the CA certificates installed in ctx as PEM, so they can be
|
||||||
// loaded into another context.
|
// loaded into another context.
|
||||||
inline std::string ca_certs_to_pem(tls::ctx_t ctx) {
|
inline std::string ca_certs_to_pem(tls::ctx_t ctx) {
|
||||||
@@ -11548,8 +11570,7 @@ struct ClientTlsSessionOptions {
|
|||||||
// begin with, so it passes nothing.
|
// begin with, so it passes nothing.
|
||||||
std::mutex *ctx_mutex = nullptr;
|
std::mutex *ctx_mutex = nullptr;
|
||||||
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
// The caller decides whether Schannel has anything to say about this
|
// See use_windows_cert_verification().
|
||||||
// connection; see SSLClient::initialize_ssl().
|
|
||||||
bool windows_cert_verification = false;
|
bool windows_cert_verification = false;
|
||||||
// A server certificate verifier works on the backend's chain verification,
|
// A server certificate verifier works on the backend's chain verification,
|
||||||
// so the backend keeps deciding and Schannel only adds its own check.
|
// so the backend keeps deciding and Schannel only adds its own check.
|
||||||
@@ -19708,13 +19729,9 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
|
|||||||
options.interrupted = stop_interrupt();
|
options.interrupted = stop_interrupt();
|
||||||
options.ctx_mutex = &ctx_mutex_;
|
options.ctx_mutex = &ctx_mutex_;
|
||||||
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
// Skip Schannel when a custom CA cert is specified, as the Windows
|
options.windows_cert_verification = detail::use_windows_cert_verification(
|
||||||
// certificate store would not know about user-provided CA certificates.
|
enable_windows_cert_verification_, ca_cert_file_path_, ca_cert_dir_path_,
|
||||||
// Also skip when system CA trust is explicitly disabled.
|
custom_ca_loaded_, system_ca_mode_);
|
||||||
options.windows_cert_verification =
|
|
||||||
enable_windows_cert_verification_ &&
|
|
||||||
system_ca_mode_ != SystemCAMode::Disabled && ca_cert_file_path_.empty() &&
|
|
||||||
ca_cert_dir_path_.empty() && !custom_ca_loaded_;
|
|
||||||
// Only a verifier set through set_server_certificate_verifier() is seen
|
// Only a verifier set through set_server_certificate_verifier() is seen
|
||||||
// here, not one installed with tls::set_verify_callback() directly.
|
// here, not one installed with tls::set_verify_callback() directly.
|
||||||
options.server_certificate_verifier_set =
|
options.server_certificate_verifier_set =
|
||||||
@@ -24215,6 +24232,12 @@ inline bool WebSocketClient::create_stream(std::unique_ptr<Stream> &strm,
|
|||||||
detail::ClientTlsSessionOptions options;
|
detail::ClientTlsSessionOptions options;
|
||||||
options.server_hostname_verification = server_hostname_verification_;
|
options.server_hostname_verification = server_hostname_verification_;
|
||||||
options.session_verifier = session_verifier_;
|
options.session_verifier = session_verifier_;
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
options.windows_cert_verification = detail::use_windows_cert_verification(
|
||||||
|
enable_windows_cert_verification_, ca_cert_file_path_,
|
||||||
|
ca_cert_dir_path_, custom_ca_loaded_, system_ca_mode_);
|
||||||
|
options.server_certificate_verifier_set = server_certificate_verifier_set_;
|
||||||
|
#endif
|
||||||
|
|
||||||
auto timeout = handshake_timeout();
|
auto timeout = handshake_timeout();
|
||||||
detail::ClientTlsSessionError tls_error;
|
detail::ClientTlsSessionError tls_error;
|
||||||
@@ -24524,6 +24547,9 @@ WebSocketClient::set_ca_cert_path(const std::string &ca_cert_file_path,
|
|||||||
|
|
||||||
inline void
|
inline void
|
||||||
WebSocketClient::set_server_certificate_verifier(tls::VerifyCallback verifier) {
|
WebSocketClient::set_server_certificate_verifier(tls::VerifyCallback verifier) {
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
server_certificate_verifier_set_ = static_cast<bool>(verifier);
|
||||||
|
#endif
|
||||||
if (tls_ctx_) { tls::set_verify_callback(tls_ctx_, std::move(verifier)); }
|
if (tls_ctx_) { tls::set_verify_callback(tls_ctx_, std::move(verifier)); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24563,6 +24589,13 @@ inline void WebSocketClient::enable_system_ca(bool enabled) {
|
|||||||
system_ca_mode_ = enabled ? SystemCAMode::Enabled : SystemCAMode::Disabled;
|
system_ca_mode_ = enabled ? SystemCAMode::Enabled : SystemCAMode::Disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE
|
||||||
|
inline void
|
||||||
|
WebSocketClient::enable_windows_certificate_verification(bool enabled) {
|
||||||
|
enable_windows_cert_verification_ = enabled;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
WebSocketClient::set_proxy_digest_auth(const std::string &username,
|
WebSocketClient::set_proxy_digest_auth(const std::string &username,
|
||||||
const std::string &password) {
|
const std::string &password) {
|
||||||
|
|||||||
@@ -16829,6 +16829,39 @@ TEST(SSLClientTest, WindowsCertificateVerification_RejectsUntrustedRoot) {
|
|||||||
EXPECT_EQ(Error::SSLServerVerification, res.error());
|
EXPECT_EQ(Error::SSLServerVerification, res.error());
|
||||||
EXPECT_EQ(static_cast<DWORD>(CERT_E_UNTRUSTEDROOT), res.ssl_backend_error());
|
EXPECT_EQ(static_cast<DWORD>(CERT_E_UNTRUSTEDROOT), res.ssl_backend_error());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(WebSocketSSLVerifyTest, WindowsCertificateVerification) {
|
||||||
|
SSLServer svr(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE);
|
||||||
|
ASSERT_TRUE(svr.is_valid());
|
||||||
|
|
||||||
|
thread t = thread([&]() { ASSERT_TRUE(svr.listen("127.0.0.1", PORT)); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
svr.stop();
|
||||||
|
t.join();
|
||||||
|
ASSERT_FALSE(svr.is_running());
|
||||||
|
});
|
||||||
|
|
||||||
|
svr.wait_until_ready();
|
||||||
|
|
||||||
|
auto url = "wss://127.0.0.1:" + std::to_string(PORT) + "/";
|
||||||
|
{
|
||||||
|
ws::WebSocketClient client(url);
|
||||||
|
auto res = client.connect();
|
||||||
|
ASSERT_FALSE(res);
|
||||||
|
EXPECT_EQ(Error::SSLServerVerification, res.error());
|
||||||
|
EXPECT_EQ(static_cast<DWORD>(CERT_E_UNTRUSTEDROOT),
|
||||||
|
res.ssl_backend_error());
|
||||||
|
}
|
||||||
|
{
|
||||||
|
ws::WebSocketClient client(url);
|
||||||
|
client.enable_windows_certificate_verification(false);
|
||||||
|
auto res = client.connect();
|
||||||
|
ASSERT_FALSE(res);
|
||||||
|
EXPECT_EQ(Error::SSLServerVerification, res.error());
|
||||||
|
EXPECT_NE(static_cast<DWORD>(CERT_E_UNTRUSTEDROOT),
|
||||||
|
res.ssl_backend_error());
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(SSLClientTest, ServerCertificateVerification1_Online) {
|
TEST(SSLClientTest, ServerCertificateVerification1_Online) {
|
||||||
|
|||||||
Reference in new issue
Block a user