Skip to content

Commit

Permalink
WIP: dhtproxy: load client cert in server memory
Browse files Browse the repository at this point in the history
  • Loading branch information
binarytrails committed Oct 7, 2019
1 parent 92766f7 commit 1eebd70
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/opendht/dht_proxy_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class OPENDHT_PUBLIC DhtProxyServer
DhtProxyServer(
dht::crypto::Identity identity,
std::shared_ptr<DhtRunner> dht, in_port_t port = 8000, const std::string& pushServer = "",
const std::string& client_certificate = "", std::shared_ptr<dht::Logger> logger = {});
std::shared_ptr<dht::crypto::Certificate> client_certificate = {}, std::shared_ptr<dht::Logger> logger = {});

virtual ~DhtProxyServer();

Expand Down
10 changes: 7 additions & 3 deletions src/dht_proxy_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ struct DhtProxyServer::RestRouterTraits : public restinio::default_traits_t
DhtProxyServer::DhtProxyServer(
dht::crypto::Identity identity,
std::shared_ptr<DhtRunner> dht, in_port_t port, const std::string& pushServer,
const std::string& client_certificate, std::shared_ptr<dht::Logger> logger
std::shared_ptr<dht::crypto::Certificate> client_certificate, std::shared_ptr<dht::Logger> logger
)
: dht_(dht), logger_(logger), lockListener_(std::make_shared<std::mutex>()),
listeners_(std::make_shared<std::map<restinio::connection_id_t, http::ListenerSession>>()),
Expand Down Expand Up @@ -242,10 +242,14 @@ DhtProxyServer::DhtProxyServer(
if (ec)
throw std::runtime_error("Error setting tls context options: " + ec.message());
// verify client auth
if (!client_certificate.empty()){
if (!client_certificate){
tls_context.set_verify_mode(asio::ssl::context::verify_fail_if_no_peer_cert
| asio::ssl::context::verify_peer, ec);
tls_context.load_verify_file(client_certificate);
auto ca = client_certificate->toString(false/*chain*/);
//tls_context.load_verify_file(client_certificate);
tls_context.add_certificate_authority(asio::const_buffer{ca.data(), ca.size()}, ec);
if (ec)
throw std::runtime_error("Error adding client certificate: " + ec.message());
}
if (ec)
throw std::runtime_error("Error setting tls verify peer options: " + ec.message());
Expand Down
2 changes: 1 addition & 1 deletion tests/dhtproxytester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ DhtProxyTester::setUp() {
new dht::DhtProxyServer(
///*http*/dht::crypto::Identity{},
/*https*/serverIdentity,
nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", "", logger));
nodeProxy, 8080, /*pushServer*/"127.0.0.1:8090", {}, logger));

clientConfig.client_cert = serverIdentity.second;
clientConfig.dht_config.node_config.maintain_storage = false;
Expand Down
2 changes: 1 addition & 1 deletion tests/httptester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ HttpTester::setUp() {

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

}

Expand Down
2 changes: 1 addition & 1 deletion tools/dhtnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ main(int argc, char **argv)
proxies.emplace(params.proxyserver, std::unique_ptr<DhtProxyServer>(
new DhtProxyServer(
dht::crypto::Identity{}, node, params.proxyserver, params.pushserver,
"", context.logger)));
{}, context.logger)));
#else
std::cerr << "DHT proxy server requested but OpenDHT built without proxy server support." << std::endl;
exit(EXIT_FAILURE);
Expand Down
11 changes: 8 additions & 3 deletions tools/tools_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct dht_params {
std::string save_identity {};
dht::crypto::Identity proxy_id {};
std::string proxy_privkey_pwd {};
std::string proxy_client_certificate {};
std::shared_ptr<dht::crypto::Certificate> proxy_client_certificate {};
};

static const constexpr struct option long_options[] = {
Expand Down Expand Up @@ -276,9 +276,14 @@ parseArgs(int argc, char **argv) {
case 'I':
params.save_identity = optarg;
break;
case 'P':
params.proxy_client_certificate = optarg;
case 'P': {
try {
params.proxy_client_certificate = std::make_shared<dht::crypto::Certificate>(loadFile(optarg));
} catch (const std::exception& e) {
throw std::runtime_error(std::string("Error loading proxy certificate: ") + e.what());
}
break;
}
default:
break;
}
Expand Down

0 comments on commit 1eebd70

Please sign in to comment.