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
+173 -54
View File
@@ -19,6 +19,8 @@
#include "wivrn_sockets.h"
#include "crypto.h"
#include <algorithm>
#include <arpa/inet.h>
#include <cassert>
#include <memory>
@@ -26,12 +28,16 @@
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <system_error>
#include <unistd.h>
thread_local crypto::encrypt_context wivrn::UDP::encrypter{EVP_aes_128_ctr()};
std::atomic<uint64_t> wivrn::UDP::iv_counter;
const char * wivrn::invalid_packet::what() const noexcept
{
return "Invalid packet";
@@ -63,6 +69,7 @@ wivrn::fd_base::~fd_base()
wivrn::UDP::UDP()
{
fd = socket(AF_INET6, SOCK_DGRAM, 0);
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (fd < 0)
throw std::system_error{errno, std::generic_category()};
@@ -175,6 +182,7 @@ wivrn::TCP::TCP(int fd)
wivrn::TCP::TCP(in6_addr address, int port)
{
fd = socket(AF_INET6, SOCK_STREAM, 0);
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (fd < 0)
throw std::system_error{errno, std::generic_category()};
@@ -196,6 +204,7 @@ wivrn::TCP::TCP(in6_addr address, int port)
wivrn::TCP::TCP(in_addr address, int port)
{
fd = socket(AF_INET, SOCK_STREAM, 0);
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (fd < 0)
throw std::system_error{errno, std::generic_category()};
@@ -214,10 +223,6 @@ wivrn::TCP::TCP(in_addr address, int port)
init();
}
wivrn::TCPListener::TCPListener()
{
}
wivrn::TCPListener::TCPListener(int port)
{
fd = socket(AF_INET6, SOCK_STREAM, 0);
@@ -271,8 +276,25 @@ std::pair<wivrn::deserialization_packet, sockaddr_in6> wivrn::UDP::receive_from_
bytes_received_ += received;
auto tmp = buffer.get();
return {deserialization_packet{std::move(buffer), std::span(tmp, received)}, addr};
std::span message{buffer.get(), (size_t)received};
if (encrypted)
{
// Not big enough for the IV: drop the packet
if (received < sizeof(uint64_t))
return {};
std::array<uint8_t, 16> full_iv;
memcpy(full_iv.data(), buffer.get(), sizeof(uint64_t)); // TODO: endianness?
memcpy(full_iv.data() + sizeof(uint64_t), recv_iv_header.data(), recv_iv_header.size());
message = message.subspan(sizeof(uint64_t));
decrypter.set_iv(full_iv);
decrypter.decrypt_in_place(message);
}
return {deserialization_packet{std::move(buffer), message}, addr};
}
wivrn::deserialization_packet wivrn::UDP::receive_pending()
@@ -311,6 +333,7 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
.iov_base = buffer.get() + message_size * i,
.iov_len = message_size,
});
mmsgs.push_back(
{
.msg_hdr = {
@@ -328,66 +351,114 @@ wivrn::deserialization_packet wivrn::UDP::receive_raw()
throw socket_shutdown();
messages.reserve(received);
for (int i = received - 1; i > 0; --i)
for (int i = received - 1; i >= 0; --i)
{
messages.emplace_back(
(uint8_t *)iovecs[i].iov_base,
mmsgs[i].msg_len);
bytes_received_ += mmsgs[i].msg_len;
std::span<uint8_t> message{(uint8_t *)iovecs[i].iov_base, mmsgs[i].msg_len};
assert(message.data() != nullptr);
if (encrypted)
{
// Not big enough for the IV: drop the packet
if (message.size() < sizeof(uint64_t))
throw std::runtime_error("Packet too small: " + std::to_string(message.size()));
std::array<uint8_t, 16> full_iv;
memcpy(full_iv.data(), message.data(), sizeof(uint64_t)); // TODO: endianness?
memcpy(full_iv.data() + sizeof(uint64_t), recv_iv_header.data(), recv_iv_header.size());
message = message.subspan(sizeof(uint64_t));
decrypter.set_iv(full_iv);
decrypter.decrypt_in_place(message);
}
if (i == 0)
return deserialization_packet{buffer, message};
messages.push_back(message);
}
return deserialization_packet{buffer, std::span(buffer.get(), mmsgs[0].msg_len)};
__builtin_unreachable();
}
void wivrn::UDP::send_raw(const std::vector<uint8_t> & data)
void wivrn::UDP::send_raw(serialization_packet && packet)
{
ssize_t sent = ::send(fd, data.data(), data.size(), 0);
if (sent < 0)
throw std::system_error{errno, std::generic_category()};
thread_local std::vector<iovec> iovecs;
iovecs.clear();
bytes_sent_ += sent;
}
std::vector<std::span<uint8_t>> & data = packet;
uint64_t counter;
if (encrypted)
{
counter = iv_counter.fetch_add(1);
std::array<uint8_t, 16> full_iv;
memcpy(full_iv.data(), &counter, sizeof(uint64_t)); // TODO: endianness?
memcpy(full_iv.data() + sizeof(uint64_t), send_iv_header.data(), send_iv_header.size());
iovecs.emplace_back(&counter, sizeof(uint64_t));
encrypter.set_key_and_iv(key, full_iv);
encrypter.encrypt_in_place(data);
}
void wivrn::UDP::send_raw(const std::vector<std::span<uint8_t>> & data)
{
thread_local std::vector<iovec> spans;
spans.clear();
for (const auto & span: data)
spans.emplace_back((void *)span.data(), span.size());
iovecs.emplace_back(span.data(), span.size());
if (::writev(fd, spans.data(), spans.size()) < 0)
if (::writev(fd, iovecs.data(), iovecs.size()) < 0)
throw std::system_error{errno, std::generic_category()};
}
void wivrn::UDP::send_many_raw(std::span<const std::vector<std::span<uint8_t>> *> data)
void wivrn::UDP::send_many_raw(std::vector<serialization_packet> && packets)
{
thread_local std::vector<iovec> iovecs;
thread_local std::vector<mmsghdr> mmsgs;
thread_local std::vector<uint64_t> iv_counters;
iovecs.clear();
mmsgs.clear();
for (const auto & message: data)
iv_counters.clear();
iv_counters.reserve(packets.size());
for (serialization_packet & packet: packets)
{
for (const auto & span: *message)
std::vector<std::span<uint8_t>> & data = packet;
if (encrypted)
{
iovecs.push_back(
{
.iov_base = span.data(),
.iov_len = span.size_bytes(),
});
iv_counters.push_back(iv_counter.fetch_add(1));
std::array<uint8_t, 16> full_iv;
memcpy(full_iv.data(), &iv_counters.back(), sizeof(uint64_t)); // TODO: endianness?
memcpy(full_iv.data() + sizeof(uint64_t), send_iv_header.data(), send_iv_header.size());
iovecs.emplace_back(&iv_counters.back(), sizeof(uint64_t));
encrypter.set_key_and_iv(key, full_iv);
encrypter.encrypt_in_place(data);
}
for (const auto & span: data)
{
iovecs.emplace_back(span.data(), span.size_bytes());
}
if (encrypted)
mmsgs.push_back({.msg_hdr = {.msg_iovlen = data.size() + 1}});
else
mmsgs.push_back({.msg_hdr = {.msg_iovlen = data.size()}});
}
size_t i = 0;
for (const auto & message: data)
for (size_t i = 0, j = 0; i < packets.size(); ++i)
{
mmsgs.push_back(
{
.msg_hdr = {
.msg_iov = &iovecs[i],
.msg_iovlen = message->size(),
},
});
i += message->size();
mmsgs[i].msg_hdr.msg_iov = &iovecs[j];
j += mmsgs[i].msg_hdr.msg_iovlen;
}
// sendmmsg may not send all messages, just consider them as lost for UDP
if (sendmmsg(fd, mmsgs.data(), mmsgs.size(), 0) < 0)
throw std::system_error{errno, std::generic_category()};
@@ -422,18 +493,24 @@ wivrn::deserialization_packet wivrn::TCP::receive_raw()
capacity_left = new_size - data.size_bytes();
}
ssize_t received = recv(fd, &*data.end(), capacity_left, MSG_DONTWAIT);
ssize_t received_size = recv(fd, &*data.end(), capacity_left, MSG_DONTWAIT);
if (received < 0)
if (received_size < 0)
throw std::system_error{errno, std::generic_category()};
if (received == 0)
if (received_size == 0)
throw socket_shutdown{};
bytes_received_ += received;
bytes_received_ += received_size;
data = std::span(data.data(), data.size() + received);
capacity_left -= received;
if (decrypter)
{
std::span<uint8_t> received_data{&*data.end(), (size_t)received_size};
decrypter.decrypt_in_place(received_data);
}
data = std::span(data.data(), data.size() + received_size);
capacity_left -= received_size;
if (data.size_bytes() < sizeof(uint16_t))
return {};
@@ -466,14 +543,16 @@ wivrn::deserialization_packet wivrn::TCP::receive_pending()
return deserialization_packet{buffer, span};
}
void wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> & spans)
void wivrn::TCP::send_raw(serialization_packet && packet)
{
thread_local std::vector<iovec> iovecs;
iovecs.clear();
std::vector<std::span<uint8_t>> & data = packet;
uint16_t size = 0;
iovecs.emplace_back(&size, sizeof(size));
for (const auto & span: spans)
for (const auto & span: data)
{
size += span.size_bytes();
iovecs.emplace_back(span.data(), span.size_bytes());
@@ -490,9 +569,14 @@ void wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> & spans)
};
std::lock_guard lock(*mutex);
if (encrypter)
{
data.insert(data.begin(), {(uint8_t *)&size, sizeof(size)});
encrypter.encrypt_in_place(data);
}
while (true)
{
const auto & data = spans[0];
ssize_t sent = ::sendmsg(fd, &hdr, MSG_NOSIGNAL);
if (sent == 0)
@@ -517,22 +601,30 @@ void wivrn::TCP::send_raw(const std::vector<std::span<uint8_t>> & spans)
}
}
void wivrn::TCP::send_many_raw(std::span<const std::vector<std::span<uint8_t>> *> data)
void wivrn::TCP::send_many_raw(std::vector<serialization_packet> && packets)
{
thread_local std::vector<iovec> iovecs;
thread_local std::vector<uint16_t> sizes;
thread_local std::vector<std::span<uint8_t>> spans;
iovecs.clear();
sizes.clear();
spans.clear();
sizes.reserve(data.size());
for (const auto & spans: data)
sizes.reserve(packets.size());
for (serialization_packet & packet: packets)
{
std::vector<std::span<uint8_t>> & data = packet;
auto & size = sizes.emplace_back(0);
iovecs.emplace_back(&size, sizeof(size));
for (const auto & span: *spans)
spans.emplace_back((uint8_t *)&size, sizeof(size));
for (const auto & span: data)
{
size += span.size_bytes();
iovecs.emplace_back(span.data(), span.size_bytes());
spans.emplace_back(span.data(), span.size_bytes());
}
}
@@ -547,6 +639,11 @@ void wivrn::TCP::send_many_raw(std::span<const std::vector<std::span<uint8_t>> *
};
std::lock_guard lock(*mutex);
if (encrypter)
{
encrypter.encrypt_in_place(spans);
}
while (true)
{
ssize_t sent = ::sendmsg(fd, &hdr, MSG_NOSIGNAL);
@@ -572,3 +669,25 @@ void wivrn::TCP::send_many_raw(std::span<const std::vector<std::span<uint8_t>> *
hdr.msg_iov[0].iov_len -= sent;
}
}
void wivrn::UDP::set_aes_key_and_ivs(std::span<std::uint8_t, 16> key_, std::span<std::uint8_t, 8> recv_iv_header_, std::span<std::uint8_t, 8> send_iv_header_)
{
decrypter = crypto::decrypt_context{EVP_aes_128_ctr()};
decrypter.set_key(key_);
std::ranges::copy(key_, key.begin());
std::ranges::copy(recv_iv_header_, recv_iv_header.begin());
std::ranges::copy(send_iv_header_, send_iv_header.begin());
encrypted = true;
}
void wivrn::TCP::set_aes_key_and_ivs(std::span<std::uint8_t, 16> key, std::span<std::uint8_t, 16> recv_iv, std::span<std::uint8_t, 16> send_iv)
{
encrypter = crypto::encrypt_context{EVP_aes_128_ctr()};
encrypter.set_key(key);
encrypter.set_iv(send_iv);
decrypter = crypto::decrypt_context{EVP_aes_128_ctr()};
decrypter.set_key(key);
decrypter.set_iv(recv_iv);
}