Allow disabling encryption on the server command line

This commit is contained in:
Guillaume Meunier
2024-11-26 23:59:35 +01:00
committed by Patrick Nicolas
parent c6e773fd22
commit 2778f7dd7e
4 changed files with 80 additions and 41 deletions
+36 -18
View File
@@ -99,26 +99,44 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
to_headset::crypto_handshake crypto_handshake = std::get<to_headset::crypto_handshake>(receive(10s)); to_headset::crypto_handshake crypto_handshake = std::get<to_headset::crypto_handshake>(receive(10s));
crypto::key server_key = crypto::key::from_public_key(crypto_handshake.public_key); if (crypto_handshake.public_key != "")
const std::string pin = crypto_handshake.pin_required ? pin_enter(control.get_fd()) : "000000";
spdlog::info("Using pin \"{}\"", pin);
secrets s{headset_keypair, server_key, pin};
control.set_aes_key_and_ivs(s.control_key, s.control_iv_to_headset, s.control_iv_from_headset);
// Confirm that encryption is set up
send_control(from_headset::crypto_handshake{});
to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))};
if (h.stream_port > 0 && !tcp_only)
{ {
stream = decltype(stream)(); crypto::key server_key = crypto::key::from_public_key(crypto_handshake.public_key);
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_to_headset, s.stream_iv_header_from_headset); const std::string pin = crypto_handshake.pin_required ? pin_enter(control.get_fd()) : "000000";
stream.connect(address, h.stream_port);
init_stream(stream); spdlog::info("Using pin \"{}\"", pin);
secrets s{headset_keypair, server_key, pin};
control.set_aes_key_and_ivs(s.control_key, s.control_iv_to_headset, s.control_iv_from_headset);
// Confirm that encryption is set up
send_control(from_headset::crypto_handshake{});
to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))};
if (h.stream_port > 0 && !tcp_only)
{
stream = decltype(stream)();
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_to_headset, s.stream_iv_header_from_headset);
stream.connect(address, h.stream_port);
init_stream(stream);
}
}
else
{
spdlog::info("Encryption is disabled on server");
send_control(from_headset::crypto_handshake{});
to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))};
if (h.stream_port > 0 && !tcp_only)
{
stream = decltype(stream)();
stream.connect(address, h.stream_port);
init_stream(stream);
}
} }
// may be on control socket if forced TCP // may be on control socket if forced TCP
+23 -12
View File
@@ -50,7 +50,7 @@ static std::string clean_key(std::string key)
return key; return key;
} }
wivrn::wivrn_connection::wivrn_connection(std::stop_token stop_token, const std::string & pin, TCP && tcp) : wivrn::wivrn_connection::wivrn_connection(std::stop_token stop_token, std::optional<std::string> pin, TCP && tcp) :
control(std::move(tcp)), control(std::move(tcp)),
stream(-1), stream(-1),
pin(pin) pin(pin)
@@ -158,25 +158,36 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
// Wait for client to send handshake packet // Wait for client to send handshake packet
auto crypto_handshake = std::get<from_headset::crypto_handshake>(receive(10s).first); auto crypto_handshake = std::get<from_headset::crypto_handshake>(receive(10s).first);
crypto::key headset_key = crypto::key::from_public_key(crypto_handshake.public_key);
crypto::key headset_key = crypto::key::from_public_key(crypto_handshake.public_key);
bool is_public_key_known = std::ranges::any_of( bool is_public_key_known = std::ranges::any_of(
wivrn::known_keys(), wivrn::known_keys(),
[key = clean_key(crypto_handshake.public_key)](const wivrn::headset_key & k) { [key = clean_key(crypto_handshake.public_key)](const wivrn::headset_key & k) {
return k.public_key == key; return k.public_key == key;
}); });
// Generate an ephemeral key pair just for exchanging the AES key if (pin)
crypto::key server_key = crypto::key::generate_x448_keypair(); {
// Generate an ephemeral key pair just for exchanging the AES key
crypto::key server_key = crypto::key::generate_x448_keypair();
control.send(to_headset::crypto_handshake{ control.send(to_headset::crypto_handshake{
.public_key = server_key.public_key(), .public_key = server_key.public_key(),
.pin_required = not is_public_key_known, .pin_required = not is_public_key_known,
}); });
secrets s{server_key, headset_key, is_public_key_known ? "000000" : pin}; secrets s{server_key, headset_key, is_public_key_known ? "000000" : *pin};
control.set_aes_key_and_ivs(s.control_key, s.control_iv_from_headset, s.control_iv_to_headset); control.set_aes_key_and_ivs(s.control_key, s.control_iv_from_headset, s.control_iv_to_headset);
stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_from_headset, s.stream_iv_header_to_headset); stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_from_headset, s.stream_iv_header_to_headset);
}
else
{
// Encryption and authentication are disabled
control.send(to_headset::crypto_handshake{
.public_key = "",
.pin_required = false,
});
}
// Wait for confirmation that the client has set up encryption // Wait for confirmation that the client has set up encryption
if (not std::holds_alternative<from_headset::crypto_handshake>(receive().first)) if (not std::holds_alternative<from_headset::crypto_handshake>(receive().first))
@@ -208,7 +219,7 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
active = true; active = true;
if (not is_public_key_known) if (pin and not is_public_key_known)
wivrn::add_known_key({ wivrn::add_known_key({
.public_key = clean_key(headset_key.public_key()), .public_key = clean_key(headset_key.public_key()),
.name = crypto_handshake.name, .name = crypto_handshake.name,
+2 -2
View File
@@ -37,14 +37,14 @@ class wivrn_connection
typed_socket<TCP, from_headset::packets, to_headset::packets> control; typed_socket<TCP, from_headset::packets, to_headset::packets> control;
typed_socket<UDP, from_headset::packets, to_headset::packets> stream; typed_socket<UDP, from_headset::packets, to_headset::packets> stream;
std::atomic<bool> active = false; std::atomic<bool> active = false;
std::string pin; std::optional<std::string> pin;
wivrn::from_headset::headset_info_packet info_packet; wivrn::from_headset::headset_info_packet info_packet;
void init(std::stop_token stop_token, std::function<void()> tick = []() {}); void init(std::stop_token stop_token, std::function<void()> tick = []() {});
public: public:
wivrn_connection(std::stop_token stop_token, const std::string & pin, TCP && tcp); wivrn_connection(std::stop_token stop_token, std::optional<std::string> pin, TCP && tcp);
wivrn_connection(const wivrn_connection &) = delete; wivrn_connection(const wivrn_connection &) = delete;
wivrn_connection & operator=(const wivrn_connection &) = delete; wivrn_connection & operator=(const wivrn_connection &) = delete;
+19 -9
View File
@@ -204,7 +204,8 @@ bool avahi_publish;
guint listener_watch; guint listener_watch;
std::string pin; bool disable_encryption;
std::optional<std::string> pin;
WivrnServer * dbus_server; WivrnServer * dbus_server;
@@ -521,16 +522,23 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data)
void choose_pin() void choose_pin()
{ {
std::random_device rd; if (disable_encryption)
std::mt19937 gen(rd()); {
std::uniform_int_distribution<> distrib(0, 999999); std::cerr << "Encryption is disabled" << std::endl;
}
else
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distrib(0, 999999);
char buffer[7]; char buffer[7];
snprintf(buffer, 7, "%06d", distrib(gen)); snprintf(buffer, 7, "%06d", distrib(gen));
pin = buffer; pin = buffer;
std::cerr << "PIN code: " << pin << std::endl; std::cerr << "PIN code: " << *pin << std::endl;
wivrn_server_set_pin(dbus_server, buffer); wivrn_server_set_pin(dbus_server, buffer);
}
} }
gboolean on_handle_disconnect(WivrnServer * skeleton, gboolean on_handle_disconnect(WivrnServer * skeleton,
@@ -806,6 +814,7 @@ int main(int argc, char * argv[])
auto no_instructions = app.add_flag("--no-instructions")->group(""); auto no_instructions = app.add_flag("--no-instructions")->group("");
auto no_fork = app.add_flag("--no-fork")->description("disable fork to serve connection")->group("Debug"); auto no_fork = app.add_flag("--no-fork")->description("disable fork to serve connection")->group("Debug");
auto no_publish = app.add_flag("--no-publish-service")->description("disable publishing the service through avahi"); auto no_publish = app.add_flag("--no-publish-service")->description("disable publishing the service through avahi");
auto no_encrypt = app.add_flag("--no-encrypt")->description("disable encryption")->group("Debug");
#if WIVRN_USE_SYSTEMD #if WIVRN_USE_SYSTEMD
// --application should only be used from wivrn-application unit file // --application should only be used from wivrn-application unit file
auto app_flag = app.add_flag("--application")->group(""); auto app_flag = app.add_flag("--application")->group("");
@@ -816,6 +825,7 @@ int main(int argc, char * argv[])
do_fork = not *no_fork; do_fork = not *no_fork;
avahi_publish = not *no_publish; avahi_publish = not *no_publish;
disable_encryption = (bool)*no_encrypt;
if (not config_file.empty()) if (not config_file.empty())
configuration::set_config_file(config_file); configuration::set_config_file(config_file);