Skip to content

Commit 4cbac30

Browse files
add client cert support for websocket (#1967)
1 parent 88d0668 commit 4cbac30

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

lib/inc/drogon/WebSocketClient.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,31 @@ class DROGON_EXPORT WebSocketClient
9090
virtual void connectToServer(const HttpRequestPtr &request,
9191
const WebSocketRequestCallback &callback) = 0;
9292

93+
/**
94+
* @brief Set the client certificate used by the HTTP connection
95+
*
96+
* @param cert Path to the certificate
97+
* @param key Path to the certificate's private key
98+
* @note this method has no effect if the HTTP client is communicating via
99+
* unencrypted HTTP
100+
*/
101+
virtual void setCertPath(const std::string &cert,
102+
const std::string &key) = 0;
103+
104+
/**
105+
* @brief Supplies command style options for `SSL_CONF_cmd`
106+
*
107+
* @param sslConfCmds options for SSL_CONF_cmd
108+
* @note this method has no effect if the HTTP client is communicating via
109+
* unencrypted HTTP
110+
* @code
111+
addSSLConfigs({{"-dhparam", "/path/to/dhparam"}, {"-strict", ""}});
112+
* @endcode
113+
*/
114+
virtual void addSSLConfigs(
115+
const std::vector<std::pair<std::string, std::string>>
116+
&sslConfCmds) = 0;
117+
93118
#ifdef __cpp_impl_coroutine
94119
/**
95120
* @brief Set messages handler. When a message is received from the server,

lib/src/WebSocketClientImpl.cc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ void WebSocketClientImpl::createTcpClient()
5757
auto policy = trantor::TLSPolicy::defaultClientPolicy();
5858
policy->setUseOldTLS(useOldTLS_)
5959
.setValidate(validateCert_)
60-
.setHostname(domain_);
60+
.setHostname(domain_)
61+
.setConfCmds(sslConfCmds_)
62+
.setCertPath(clientCertPath_)
63+
.setKeyPath(clientKeyPath_);
6164
tcpClientPtr_->enableSSL(std::move(policy));
6265
}
6366
auto thisPtr = shared_from_this();
@@ -452,6 +455,22 @@ void WebSocketClientImpl::connectToServer(
452455
}
453456
}
454457

458+
void WebSocketClientImpl::setCertPath(const std::string &cert,
459+
const std::string &key)
460+
{
461+
clientCertPath_ = cert;
462+
clientKeyPath_ = key;
463+
}
464+
465+
void WebSocketClientImpl::addSSLConfigs(
466+
const std::vector<std::pair<std::string, std::string>> &sslConfCmds)
467+
{
468+
for (const auto &cmd : sslConfCmds)
469+
{
470+
sslConfCmds_.push_back(cmd);
471+
}
472+
}
473+
455474
WebSocketClientPtr WebSocketClient::newWebSocketClient(const std::string &ip,
456475
uint16_t port,
457476
bool useSSL,

lib/src/WebSocketClientImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class WebSocketClientImpl
5151
void connectToServer(const HttpRequestPtr &request,
5252
const WebSocketRequestCallback &callback) override;
5353

54+
void setCertPath(const std::string &cert, const std::string &key) override;
55+
56+
void addSSLConfigs(const std::vector<std::pair<std::string, std::string>>
57+
&sslConfCmds) override;
58+
5459
trantor::EventLoop *getLoop() override
5560
{
5661
return loop_;
@@ -83,6 +88,9 @@ class WebSocketClientImpl
8388
bool stop_{false};
8489
std::string wsKey_;
8590
std::string wsAccept_;
91+
std::string clientCertPath_;
92+
std::string clientKeyPath_;
93+
std::vector<std::pair<std::string, std::string>> sslConfCmds_;
8694

8795
HttpRequestPtr upgradeRequest_;
8896
std::function<void(std::string &&,

0 commit comments

Comments
 (0)