Skip to content

Commit ca20bbd

Browse files
committed
dhtproxy: load client certificates
1 parent 739f4d0 commit ca20bbd

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

include/opendht/dht_proxy_server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class OPENDHT_PUBLIC DhtProxyServer
7474
DhtProxyServer(
7575
dht::crypto::Identity identity,
7676
std::shared_ptr<DhtRunner> dht, in_port_t port = 8000, const std::string& pushServer = "",
77-
std::shared_ptr<dht::Logger> logger = {});
77+
const std::string& client_certificate = "", std::shared_ptr<dht::Logger> logger = {});
7878

7979
virtual ~DhtProxyServer();
8080

src/dht_proxy_server.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ struct DhtProxyServer::RestRouterTraits : public restinio::default_traits_t
197197
DhtProxyServer::DhtProxyServer(
198198
dht::crypto::Identity identity,
199199
std::shared_ptr<DhtRunner> dht, in_port_t port, const std::string& pushServer,
200-
std::shared_ptr<dht::Logger> logger
200+
const std::string& client_certificate, std::shared_ptr<dht::Logger> logger
201201
)
202202
: dht_(dht), logger_(logger), lockListener_(std::make_shared<std::mutex>()),
203203
listeners_(std::make_shared<std::map<restinio::connection_id_t, http::ListenerSession>>()),
@@ -241,6 +241,14 @@ DhtProxyServer::DhtProxyServer(
241241
| asio::ssl::context::single_dh_use, ec);
242242
if (ec)
243243
throw std::runtime_error("Error setting tls context options: " + ec.message());
244+
// verify client auth
245+
if (!client_certificate.empty()){
246+
tls_context.set_verify_mode(asio::ssl::context::verify_fail_if_no_peer_cert
247+
| asio::ssl::context::verify_peer, ec);
248+
tls_context.load_verify_file(client_certificate);
249+
}
250+
if (ec)
251+
throw std::runtime_error("Error setting tls verify peer options: " + ec.message());
244252
// add more security options
245253
#ifdef SSL_OP_NO_RENEGOTIATION
246254
SSL_CTX_set_options(tls_context.native_handle(), SSL_OP_NO_RENEGOTIATION); // CVE-2009-3555

tests/dhtproxytester.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ DhtProxyTester::setUp() {
4848
new dht::DhtProxyServer(
4949
///*http*/dht::crypto::Identity{},
5050
/*https*/serverIdentity,
51-
nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", logger));
51+
nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", "", logger));
5252

5353
clientConfig.client_cert = serverIdentity.second;
5454
clientConfig.dht_config.node_config.maintain_storage = false;

tools/dhtnode.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ void cmd_loop(std::shared_ptr<DhtRunner>& node, dht_params& params
249249
#ifdef OPENDHT_PUSH_NOTIFICATIONS
250250
,pushServer
251251
#endif
252+
,params.proxy_client_certificate
252253
)));
253254
}
254255
else {
@@ -582,14 +583,15 @@ main(int argc, char **argv)
582583
if (params.proxyserverssl and params.proxy_id.first and params.proxy_id.second){
583584
#ifdef OPENDHT_PROXY_SERVER
584585
proxies.emplace(params.proxyserverssl, std::unique_ptr<DhtProxyServer>(
585-
new DhtProxyServer(params.proxy_id,
586-
node, params.proxyserverssl, params.pushserver, context.logger)));
586+
new DhtProxyServer(
587+
params.proxy_id, node, params.proxyserverssl, params.pushserver,
588+
params.proxy_client_certificate, context.logger)));
587589
}
588590
if (params.proxyserver) {
589591
proxies.emplace(params.proxyserver, std::unique_ptr<DhtProxyServer>(
590592
new DhtProxyServer(
591-
dht::crypto::Identity{},
592-
node, params.proxyserver, params.pushserver, context.logger)));
593+
dht::crypto::Identity{}, node, params.proxyserver, params.pushserver,
594+
"", context.logger)));
593595
#else
594596
std::cerr << "DHT proxy server requested but OpenDHT built without proxy server support." << std::endl;
595597
exit(EXIT_FAILURE);

tools/tools_common.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,11 @@ struct dht_params {
127127
std::string devicekey {};
128128
std::string persist_path {};
129129
dht::crypto::Identity id {};
130-
dht::crypto::Identity proxy_id {};
131130
std::string privkey_pwd {};
132-
std::string proxy_privkey_pwd {};
133131
std::string save_identity {};
132+
dht::crypto::Identity proxy_id {};
133+
std::string proxy_privkey_pwd {};
134+
std::string proxy_client_certificate {};
134135
};
135136

136137
static const constexpr struct option long_options[] = {
@@ -155,6 +156,7 @@ static const constexpr struct option long_options[] = {
155156
{"proxy-certificate", required_argument, nullptr, 'w'},
156157
{"proxy-privkey", required_argument, nullptr, 'K'},
157158
{"proxy-privkey-password", required_argument, nullptr, 'M'},
159+
{"proxy-client-certificate",required_argument, nullptr, 'P'},
158160
{"proxyclient", required_argument, nullptr, 'C'},
159161
{"pushserver", required_argument, nullptr, 'y'},
160162
{"devicekey", required_argument, nullptr, 'z'},
@@ -274,6 +276,9 @@ parseArgs(int argc, char **argv) {
274276
case 'I':
275277
params.save_identity = optarg;
276278
break;
279+
case 'P':
280+
params.proxy_client_certificate = optarg;
281+
break;
277282
default:
278283
break;
279284
}

0 commit comments

Comments
 (0)