Skip to content

Commit a39bd22

Browse files
committed
dhtproxy: load client cert in server memory
1 parent 92766f7 commit a39bd22

File tree

6 files changed

+19
-10
lines changed

6 files changed

+19
-10
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-
const std::string& client_certificate = "", std::shared_ptr<dht::Logger> logger = {});
77+
std::shared_ptr<dht::crypto::Certificate> client_certificate = {}, std::shared_ptr<dht::Logger> logger = {});
7878

7979
virtual ~DhtProxyServer();
8080

src/dht_proxy_server.cpp

+7-3
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-
const std::string& client_certificate, std::shared_ptr<dht::Logger> logger
200+
std::shared_ptr<dht::crypto::Certificate> 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>>()),
@@ -242,10 +242,14 @@ DhtProxyServer::DhtProxyServer(
242242
if (ec)
243243
throw std::runtime_error("Error setting tls context options: " + ec.message());
244244
// verify client auth
245-
if (!client_certificate.empty()){
245+
if (client_certificate){
246246
tls_context.set_verify_mode(asio::ssl::context::verify_fail_if_no_peer_cert
247247
| asio::ssl::context::verify_peer, ec);
248-
tls_context.load_verify_file(client_certificate);
248+
auto ca = client_certificate->toString(false/*chain*/);
249+
//tls_context.load_verify_file(client_certificate);
250+
tls_context.add_certificate_authority(asio::const_buffer{ca.data(), ca.size()}, ec);
251+
if (ec)
252+
throw std::runtime_error("Error adding client certificate: " + ec.message());
249253
}
250254
if (ec)
251255
throw std::runtime_error("Error setting tls verify peer options: " + ec.message());

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;

tests/httptester.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ HttpTester::setUp() {
4242

4343
serverProxy = std::unique_ptr<dht::DhtProxyServer>(
4444
new dht::DhtProxyServer(
45-
/*http*/dht::crypto::Identity{}, nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", "", logger));
45+
/*http*/dht::crypto::Identity{}, nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", {}, logger));
4646

4747
}
4848

tools/dhtnode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ main(int argc, char **argv)
591591
proxies.emplace(params.proxyserver, std::unique_ptr<DhtProxyServer>(
592592
new DhtProxyServer(
593593
dht::crypto::Identity{}, node, params.proxyserver, params.pushserver,
594-
"", context.logger)));
594+
{}, context.logger)));
595595
#else
596596
std::cerr << "DHT proxy server requested but OpenDHT built without proxy server support." << std::endl;
597597
exit(EXIT_FAILURE);

tools/tools_common.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ struct dht_params {
131131
std::string save_identity {};
132132
dht::crypto::Identity proxy_id {};
133133
std::string proxy_privkey_pwd {};
134-
std::string proxy_client_certificate {};
134+
std::shared_ptr<dht::crypto::Certificate> proxy_client_certificate {};
135135
};
136136

137137
static const constexpr struct option long_options[] = {
@@ -276,9 +276,14 @@ parseArgs(int argc, char **argv) {
276276
case 'I':
277277
params.save_identity = optarg;
278278
break;
279-
case 'P':
280-
params.proxy_client_certificate = optarg;
279+
case 'P': {
280+
try {
281+
params.proxy_client_certificate = std::make_shared<dht::crypto::Certificate>(loadFile(optarg));
282+
} catch (const std::exception& e) {
283+
throw std::runtime_error(std::string("Error loading proxy certificate: ") + e.what());
284+
}
281285
break;
286+
}
282287
default:
283288
break;
284289
}

0 commit comments

Comments
 (0)