Add support for authentication and encryption

This commit is contained in:
Guillaume Meunier
2024-11-26 23:59:35 +01:00
committed by Patrick Nicolas
parent 447b77c35c
commit c6e773fd22
45 changed files with 1516 additions and 455 deletions
+149 -76
View File
@@ -19,11 +19,16 @@
#include "wivrn_connection.h"
#include "configuration.h"
#include "util/u_logging.h"
#include "secrets.h"
#include "wivrn_ipc.h"
#include "wivrn_packets.h"
#include <algorithm>
#include <arpa/inet.h>
#include <chrono>
#include <poll.h>
#include <regex>
#include <sys/socket.h>
#include <variant>
using namespace std::chrono_literals;
@@ -32,16 +37,30 @@ static void handle_event_from_main_loop(to_monado::disconnect)
// Ignore disconnect request when no headset is connected
}
wivrn::wivrn_connection::wivrn_connection(TCP && tcp) :
control(std::move(tcp)), stream(-1)
static std::string clean_key(std::string key)
{
init();
static const std::regex header{"^-+BEGIN .*-+$", std::regex_constants::multiline};
static const std::regex footer{"^-+END .*-+$", std::regex_constants::multiline};
static const std::regex whitespace{"[[:space:]]"};
key = std::regex_replace(key, header, "");
key = std::regex_replace(key, footer, "");
key = std::regex_replace(key, whitespace, "");
return key;
}
void wivrn::wivrn_connection::init()
wivrn::wivrn_connection::wivrn_connection(std::stop_token stop_token, const std::string & pin, TCP && tcp) :
control(std::move(tcp)),
stream(-1),
pin(pin)
{
init(stop_token);
}
void wivrn::wivrn_connection::init(std::stop_token stop_token, std::function<void()> tick)
{
active = false;
stream = -1;
sockaddr_in6 server_address;
socklen_t len = sizeof(server_address);
@@ -58,9 +77,6 @@ void wivrn::wivrn_connection::init()
throw std::system_error(errno, std::system_category(), "Cannot get client address");
}
// Wait for client to send handshake
auto timeout = std::chrono::steady_clock::now() + std::chrono::seconds(10);
if (configuration::read_user_configuration().tcp_only)
{
port = -1;
@@ -71,84 +87,141 @@ void wivrn::wivrn_connection::init()
stream.bind(port);
}
auto receive = [&](std::optional<std::chrono::seconds> timeout = std::nullopt, bool allow_stream_socket = false) {
// Returns the packet and the port (on the stream socket) or -1 (on the control socket)
std::chrono::steady_clock::time_point timeout_abs{};
if (timeout)
timeout_abs = std::chrono::steady_clock::now() + *timeout;
while (true)
{
if (stop_token.stop_requested())
throw std::runtime_error("Connection cancelled");
tick();
pollfd fds[3] = {};
if (allow_stream_socket)
{
fds[0].events = POLLIN;
fds[0].fd = stream.get_fd();
}
fds[1].events = POLLIN;
fds[1].fd = control.get_fd();
fds[2].events = POLLIN;
fds[2].fd = wivrn_ipc_socket_monado->get_fd();
// Make sure tick() is called at least every 100ms
int r = ::poll(fds, std::size(fds), 100);
if (r < 0)
throw std::system_error(errno, std::system_category());
if (allow_stream_socket and (fds[0].revents & (POLLHUP | POLLERR)))
throw std::runtime_error("Error on stream socket");
if (fds[1].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on control socket");
if (fds[2].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on IPC socket");
if (allow_stream_socket and (fds[0].revents & POLLIN))
{
auto [raw_packet, peer_addr] = stream.receive_from_raw();
// Ignore packets sent from the wrong address
if (memcmp(&peer_addr.sin6_addr, &client_address.sin6_addr, sizeof(peer_addr.sin6_addr)) == 0)
return std::make_pair(raw_packet.deserialize<from_headset::packets>(), (int)htons(peer_addr.sin6_port));
}
if (fds[1].revents & POLLIN)
{
std::optional<from_headset::packets> packet = control.receive();
if (packet)
return std::make_pair(std::move(*packet), -1);
}
if (fds[2].revents & POLLIN)
{
auto packet = receive_from_main();
if (packet)
std::visit([](auto && x) { handle_event_from_main_loop(x); }, *packet);
}
if (timeout and std::chrono::steady_clock::now() > timeout_abs)
{
throw std::runtime_error("No handshake received from client");
}
}
};
// Wait for client to send handshake packet
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);
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();
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);
// Wait for confirmation that the client has set up encryption
if (not std::holds_alternative<from_headset::crypto_handshake>(receive().first))
throw std::runtime_error("No handshake received from client");
control.send(to_headset::handshake{.stream_port = port});
while (true)
auto [stream_handshake, client_port] = receive(10s, true);
// Check the packet type
if (not std::holds_alternative<from_headset::handshake>(stream_handshake))
{
pollfd fds[3] = {};
fds[0].events = POLLIN;
fds[0].fd = stream.get_fd();
fds[1].events = POLLIN;
fds[1].fd = control.get_fd();
fds[2].fd = wivrn_ipc_socket_monado->get_fd();
fds[2].events = POLLIN;
int r = ::poll(fds, std::size(fds), 1000);
if (r < 0)
throw std::system_error(errno, std::system_category());
if (fds[0].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on stream socket");
if (fds[1].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on control socket");
if (fds[2].revents & (POLLHUP | POLLERR))
throw std::runtime_error("Error on IPC socket");
if (fds[0].revents & POLLIN)
{
auto [packet, peer_addr] = stream.receive_from_raw();
if (memcmp(&peer_addr.sin6_addr, &client_address.sin6_addr, sizeof(peer_addr.sin6_addr)) == 0)
{
int client_port = htons(peer_addr.sin6_port);
stream.connect(peer_addr.sin6_addr, client_port);
U_LOG_D("Stream socket connected, client port %d", client_port);
stream.set_send_buffer_size(1024 * 1024 * 5);
break;
}
}
if (fds[1].revents & POLLIN)
{
auto packet = control.receive();
if (packet)
{
if (std::holds_alternative<from_headset::handshake>(*packet))
{
stream = decltype(stream)(-1);
port = -1;
U_LOG_I("Using TCP only");
break;
}
else
{
throw std::runtime_error("Invalid handshake received from client");
}
}
}
if (fds[2].revents & POLLIN)
{
auto packet = receive_from_main();
if (packet)
std::visit([](auto && x) { handle_event_from_main_loop(x); }, *packet);
}
if (std::chrono::steady_clock::now() > timeout)
{
throw std::runtime_error("No handshake received from client");
}
throw std::runtime_error("No handshake received from client");
}
if (client_port >= 0)
{
stream.connect(client_address.sin6_addr, client_port);
stream.set_send_buffer_size(1024 * 1024 * 5);
}
else
{
// No stream socket
}
control.send(to_headset::handshake{.stream_port = port});
info_packet = std::get<from_headset::headset_info_packet>(receive(10s).first);
active = true;
if (not is_public_key_known)
wivrn::add_known_key({
.public_key = clean_key(headset_key.public_key()),
.name = crypto_handshake.name,
});
}
void wivrn::wivrn_connection::reset(TCP && tcp)
void wivrn::wivrn_connection::reset(TCP && tcp, std::function<void()> tick)
{
if (stream)
stream = decltype(stream)();
control = std::move(tcp);
init();
init({}, tick);
}
void wivrn::wivrn_connection::shutdown()