diff --git a/client/wivrn_client.cpp b/client/wivrn_client.cpp index 2b917839..ff5e660e 100644 --- a/client/wivrn_client.cpp +++ b/client/wivrn_client.cpp @@ -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(receive(10s)); - crypto::key server_key = crypto::key::from_public_key(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(receive(10s))}; - if (h.stream_port > 0 && !tcp_only) + if (crypto_handshake.public_key != "") { - 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); - stream.connect(address, h.stream_port); - init_stream(stream); + 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(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(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 diff --git a/server/driver/wivrn_connection.cpp b/server/driver/wivrn_connection.cpp index e13975a4..59fb269f 100644 --- a/server/driver/wivrn_connection.cpp +++ b/server/driver/wivrn_connection.cpp @@ -50,7 +50,7 @@ static std::string clean_key(std::string 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 pin, TCP && tcp) : control(std::move(tcp)), stream(-1), pin(pin) @@ -158,25 +158,36 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function(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( wivrn::known_keys(), [key = clean_key(crypto_handshake.public_key)](const wivrn::headset_key & k) { return k.public_key == key; }); - // Generate an ephemeral key pair just for exchanging the AES key - crypto::key server_key = crypto::key::generate_x448_keypair(); + if (pin) + { + // 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{ - .public_key = server_key.public_key(), - .pin_required = not is_public_key_known, - }); + control.send(to_headset::crypto_handshake{ + .public_key = server_key.public_key(), + .pin_required = not is_public_key_known, + }); - 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); - stream.set_aes_key_and_ivs(s.stream_key, s.stream_iv_header_from_headset, s.stream_iv_header_to_headset); + 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); + 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 if (not std::holds_alternative(receive().first)) @@ -208,7 +219,7 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function control; typed_socket stream; std::atomic active = false; - std::string pin; + std::optional pin; wivrn::from_headset::headset_info_packet info_packet; void init(std::stop_token stop_token, std::function tick = []() {}); public: - wivrn_connection(std::stop_token stop_token, const std::string & pin, TCP && tcp); + wivrn_connection(std::stop_token stop_token, std::optional pin, TCP && tcp); wivrn_connection(const wivrn_connection &) = delete; wivrn_connection & operator=(const wivrn_connection &) = delete; diff --git a/server/main.cpp b/server/main.cpp index 55607b14..3c577203 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -204,7 +204,8 @@ bool avahi_publish; guint listener_watch; -std::string pin; +bool disable_encryption; +std::optional pin; WivrnServer * dbus_server; @@ -521,16 +522,23 @@ gboolean control_received(gint fd, GIOCondition condition, gpointer user_data) void choose_pin() { - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distrib(0, 999999); + if (disable_encryption) + { + 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]; - snprintf(buffer, 7, "%06d", distrib(gen)); - pin = buffer; - std::cerr << "PIN code: " << pin << std::endl; + char buffer[7]; + snprintf(buffer, 7, "%06d", distrib(gen)); + pin = buffer; + 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, @@ -806,6 +814,7 @@ int main(int argc, char * argv[]) 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_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 // --application should only be used from wivrn-application unit file auto app_flag = app.add_flag("--application")->group(""); @@ -816,6 +825,7 @@ int main(int argc, char * argv[]) do_fork = not *no_fork; avahi_publish = not *no_publish; + disable_encryption = (bool)*no_encrypt; if (not config_file.empty()) configuration::set_config_file(config_file);