Skip to content

Commit 63ff8d9

Browse files
authored
TcpClientImpl support SSL client certificate (#190)
1 parent 14e1554 commit 63ff8d9

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

trantor/net/TcpClient.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,14 @@ void TcpClient::enableSSL(
206206
bool useOldTLS,
207207
bool validateCert,
208208
std::string hostname,
209-
const std::vector<std::pair<std::string, std::string>> &sslConfCmds)
209+
const std::vector<std::pair<std::string, std::string>> &sslConfCmds,
210+
const std::string &certPath,
211+
const std::string &keyPath)
210212
{
211213
#ifdef USE_OPENSSL
212214
/* Create a new OpenSSL context */
213-
sslCtxPtr_ = newSSLContext(useOldTLS, validateCert, sslConfCmds);
215+
sslCtxPtr_ = newSSLClientContext(
216+
useOldTLS, validateCert, certPath, keyPath, sslConfCmds);
214217
validateCert_ = validateCert;
215218
if (!hostname.empty())
216219
{
@@ -228,6 +231,8 @@ void TcpClient::enableSSL(
228231
(void)validateCert;
229232
(void)hostname;
230233
(void)sslConfCmds;
234+
(void)certPath;
235+
(void)keyPath;
231236

232237
LOG_FATAL << "OpenSSL is not found in your system!";
233238
abort();

trantor/net/TcpClient.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,18 @@ class TRANTOR_EXPORT TcpClient : NonCopyable
199199
* not used.
200200
* @param sslConfCmds The commands used to call the SSL_CONF_cmd function in
201201
* OpenSSL.
202+
* @param certPath The path of the certificate file.
203+
* @param keyPath The path of the private key file.
202204
* @note It's well known that TLS 1.0 and 1.1 are not considered secure in
203205
* 2020. And it's a good practice to only use TLS 1.2 and above.
204206
*/
205207
void enableSSL(bool useOldTLS = false,
206208
bool validateCert = true,
207209
std::string hostname = "",
208210
const std::vector<std::pair<std::string, std::string>>
209-
&sslConfCmds = {});
211+
&sslConfCmds = {},
212+
const std::string &certPath = "",
213+
const std::string &keyPath = "");
210214

211215
private:
212216
/// Not thread safe, but in loop

trantor/net/inner/TcpConnectionImpl.cc

100755100644
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,43 @@ std::shared_ptr<SSLContext> newSSLServerContext(
347347
}
348348
return ctx;
349349
}
350+
std::shared_ptr<SSLContext> newSSLClientContext(
351+
bool useOldTLS,
352+
bool validateCert,
353+
const std::string &certPath,
354+
const std::string &keyPath,
355+
const std::vector<std::pair<std::string, std::string>> &sslConfCmds)
356+
{
357+
auto ctx = newSSLContext(useOldTLS, validateCert, sslConfCmds);
358+
if (certPath.empty() || keyPath.empty())
359+
return ctx;
360+
361+
auto r = SSL_CTX_use_certificate_chain_file(ctx->get(), certPath.c_str());
362+
char errbuf[BUFSIZ];
363+
if (!r)
364+
{
365+
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
366+
LOG_FATAL << "Reading certificate: " << errbuf;
367+
abort();
368+
}
369+
r = SSL_CTX_use_PrivateKey_file(ctx->get(),
370+
keyPath.c_str(),
371+
SSL_FILETYPE_PEM);
372+
if (!r)
373+
{
374+
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
375+
LOG_FATAL << "Reading private key: " << errbuf;
376+
abort();
377+
}
378+
r = SSL_CTX_check_private_key(ctx->get());
379+
if (!r)
380+
{
381+
ERR_error_string_n(ERR_get_error(), errbuf, sizeof(errbuf));
382+
LOG_FATAL << "Checking private key matches certificate: " << errbuf;
383+
abort();
384+
}
385+
return ctx;
386+
}
350387
} // namespace trantor
351388
#else
352389
namespace trantor

trantor/net/inner/TcpConnectionImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ std::shared_ptr<SSLContext> newSSLServerContext(
4747
const std::string &keyPath,
4848
bool useOldTLS,
4949
const std::vector<std::pair<std::string, std::string>> &sslConfCmds);
50+
std::shared_ptr<SSLContext> newSSLClientContext(
51+
bool useOldTLS,
52+
bool validateCert,
53+
const std::string &certPath = "",
54+
const std::string &keyPath = "",
55+
const std::vector<std::pair<std::string, std::string>> &sslConfCmds = {});
56+
5057
// void initServerSSLContext(const std::shared_ptr<SSLContext> &ctx,
5158
// const std::string &certPath,
5259
// const std::string &keyPath);

0 commit comments

Comments
 (0)