Show an error message if pairing is disabled and the headset is unknown

This commit is contained in:
Guillaume Meunier
2024-11-26 23:59:35 +01:00
committed by Patrick Nicolas
parent f8a79efc99
commit 5f39447d38
3 changed files with 63 additions and 38 deletions
+43 -32
View File
@@ -23,6 +23,7 @@
#include "spdlog/common.h" #include "spdlog/common.h"
#include "wivrn_packets.h" #include "wivrn_packets.h"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <boost/locale/gnu_gettext.hpp>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <linux/ipv6.h> #include <linux/ipv6.h>
#include <net/if.h> #include <net/if.h>
@@ -47,7 +48,7 @@ const char * handshake_error::what() const noexcept
} }
handshake_error::handshake_error(std::string_view message) : handshake_error::handshake_error(std::string_view message) :
message("Handshake error: " + std::string{message}) {} message(message) {}
namespace namespace
{ {
@@ -92,7 +93,7 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
} }
if (std::chrono::steady_clock::now() >= timeout_abs) if (std::chrono::steady_clock::now() >= timeout_abs)
throw std::runtime_error("Timeout"); throw std::runtime_error(boost::locale::gettext("Timeout"));
} }
}; };
@@ -103,44 +104,54 @@ 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));
if (crypto_handshake.public_key != "") std::string pin = "000000";
switch (crypto_handshake.state)
{ {
crypto::key server_key = crypto::key::from_public_key(crypto_handshake.public_key); case to_headset::crypto_handshake::crypto_state::encryption_disabled: {
spdlog::info("Encryption is disabled on server");
const std::string pin = crypto_handshake.pin_required ? pin_enter(control.get_fd()) : "000000"; send_control(from_headset::crypto_handshake{});
spdlog::info("Using pin \"{}\"", pin); to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))};
if (h.stream_port > 0 && !tcp_only)
{
stream = decltype(stream)();
secrets s{headset_keypair, server_key, pin}; stream.connect(address, h.stream_port);
control.set_aes_key_and_ivs(s.control_key, s.control_iv_to_headset, s.control_iv_from_headset); init_stream(stream);
}
// Confirm that encryption is set up break;
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{}); case to_headset::crypto_handshake::crypto_state::pin_needed:
pin = pin_enter(control.get_fd());
[[fallthrough]];
to_headset::handshake h{std::get<to_headset::handshake>(receive(10s))}; case to_headset::crypto_handshake::crypto_state::client_already_known: {
if (h.stream_port > 0 && !tcp_only) spdlog::info("Using pin \"{}\"", pin);
{
stream = decltype(stream)();
stream.connect(address, h.stream_port); crypto::key server_key = crypto::key::from_public_key(crypto_handshake.public_key);
init_stream(stream); 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);
}
break;
} }
case to_headset::crypto_handshake::crypto_state::enroll_disabled:
spdlog::info("Pairing is disabled on server");
throw std::runtime_error(boost::locale::gettext("Pairing is disabled on server"));
} }
// may be on control socket if forced TCP // may be on control socket if forced TCP
@@ -156,7 +167,7 @@ void wivrn_session::handshake(T address, bool tcp_only, crypto::key & headset_ke
} }
if (std::chrono::steady_clock::now() >= timeout) if (std::chrono::steady_clock::now() >= timeout)
throw std::runtime_error("Failed to establish connection"); throw std::runtime_error(boost::locale::gettext("Timeout"));
// If using stream socket, the handshake might be lost // If using stream socket, the handshake might be lost
if (stream) if (stream)
+11 -2
View File
@@ -39,7 +39,7 @@ namespace wivrn
// Default port for server to listen, both TCP and UDP // Default port for server to listen, both TCP and UDP
static const int default_port = 9757; static const int default_port = 9757;
static constexpr int protocol_revision = 1; static constexpr int protocol_revision = 0;
enum class device_id : uint8_t enum class device_id : uint8_t
{ {
@@ -266,10 +266,19 @@ using packets = std::variant<crypto_handshake, headset_info_packet, feedback, au
namespace to_headset namespace to_headset
{ {
struct crypto_handshake struct crypto_handshake
{ {
enum class crypto_state : uint8_t
{
encryption_disabled,
pin_needed,
client_already_known,
enroll_disabled,
};
std::string public_key; // In PEM format std::string public_key; // In PEM format
bool pin_required; crypto_state state;
}; };
struct handshake struct handshake
+9 -4
View File
@@ -172,15 +172,18 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
case encryption_state::disabled: case encryption_state::disabled:
// Encryption and authentication are disabled // Encryption and authentication are disabled
control.send(to_headset::crypto_handshake{ control.send(to_headset::crypto_handshake{
.public_key = "", .state = to_headset::crypto_handshake::crypto_state::encryption_disabled,
.pin_required = false,
}); });
break; break;
case encryption_state::enabled: case encryption_state::enabled:
if (not is_public_key_known) if (not is_public_key_known)
// TODO send error message to the client {
control.send(to_headset::crypto_handshake{
.state = to_headset::crypto_handshake::crypto_state::enroll_disabled,
});
throw std::runtime_error("Client not known and enroll is disabled"); throw std::runtime_error("Client not known and enroll is disabled");
}
[[fallthrough]]; [[fallthrough]];
@@ -190,7 +193,9 @@ void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<voi
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, .state = is_public_key_known
? to_headset::crypto_handshake::crypto_state::client_already_known
: to_headset::crypto_handshake::crypto_state::pin_needed,
}); });
secrets s{server_key, headset_key, is_public_key_known ? "000000" : pin}; secrets s{server_key, headset_key, is_public_key_known ? "000000" : pin};