Add support for authentication and encryption
This commit is contained in:
committed by
Patrick Nicolas
parent
447b77c35c
commit
c6e773fd22
+37
-13
@@ -19,11 +19,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "crypto.h"
|
||||
#include "wivrn_serialization.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include <exception>
|
||||
#include <fcntl.h>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <netinet/ip.h>
|
||||
@@ -103,6 +105,16 @@ class UDP : public fd_base
|
||||
std::shared_ptr<uint8_t[]> buffer;
|
||||
std::vector<std::span<uint8_t>> messages;
|
||||
|
||||
crypto::decrypt_context decrypter;
|
||||
static thread_local crypto::encrypt_context encrypter;
|
||||
static std::atomic<uint64_t> iv_counter;
|
||||
static_assert(sizeof(iv_counter) == 8);
|
||||
|
||||
bool encrypted = false;
|
||||
std::array<uint8_t, 16> key;
|
||||
std::array<uint8_t, 16 - sizeof(iv_counter)> recv_iv_header;
|
||||
std::array<uint8_t, 16 - sizeof(iv_counter)> send_iv_header;
|
||||
|
||||
public:
|
||||
UDP();
|
||||
explicit UDP(int fd);
|
||||
@@ -110,9 +122,8 @@ public:
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
std::pair<wivrn::deserialization_packet, sockaddr_in6> receive_from_raw();
|
||||
void send_raw(const std::vector<uint8_t> & data);
|
||||
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
||||
void send_many_raw(std::span<const std::vector<std::span<uint8_t>> *> data);
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::vector<serialization_packet> && packets);
|
||||
|
||||
void connect(in6_addr address, int port);
|
||||
void connect(in_addr address, int port);
|
||||
@@ -122,6 +133,8 @@ public:
|
||||
void set_receive_buffer_size(int size);
|
||||
void set_send_buffer_size(int size);
|
||||
void set_tos(int type_of_service);
|
||||
|
||||
void 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);
|
||||
};
|
||||
|
||||
class TCP : public fd_base
|
||||
@@ -133,15 +146,21 @@ class TCP : public fd_base
|
||||
|
||||
void init();
|
||||
|
||||
crypto::decrypt_context decrypter;
|
||||
crypto::encrypt_context encrypter;
|
||||
|
||||
public:
|
||||
TCP() = default;
|
||||
TCP(in6_addr address, int port);
|
||||
TCP(in_addr address, int port);
|
||||
explicit TCP(int fd);
|
||||
|
||||
deserialization_packet receive_raw();
|
||||
deserialization_packet receive_pending();
|
||||
void send_raw(const std::vector<std::span<uint8_t>> & data);
|
||||
void send_many_raw(std::span<const std::vector<std::span<uint8_t>> *> data);
|
||||
void send_raw(serialization_packet && packet);
|
||||
void send_many_raw(std::vector<serialization_packet> && packets);
|
||||
|
||||
void 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);
|
||||
};
|
||||
|
||||
using UnixDatagram = UDP;
|
||||
@@ -149,7 +168,7 @@ using UnixDatagram = UDP;
|
||||
class TCPListener : public fd_base
|
||||
{
|
||||
public:
|
||||
TCPListener();
|
||||
TCPListener() = default;
|
||||
TCPListener(int port);
|
||||
|
||||
template <typename T = TCP>
|
||||
@@ -160,6 +179,7 @@ public:
|
||||
socklen_t addrlen = sizeof(addr);
|
||||
|
||||
int fd2 = ::accept(fd, (sockaddr *)&addr, &addrlen);
|
||||
fcntl(fd, F_SETFD, FD_CLOEXEC);
|
||||
if (fd2 < 0)
|
||||
throw std::system_error{errno, std::generic_category()};
|
||||
|
||||
@@ -185,6 +205,10 @@ struct Index<T, std::tuple<U, Types...>>
|
||||
{
|
||||
static const std::size_t value = 1 + Index<T, std::tuple<Types...>>::value;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
concept not_lvalue_reference = !std::is_lvalue_reference_v<T>;
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename Socket, typename ReceivedType, typename... VariantTypes>
|
||||
@@ -224,21 +248,21 @@ public:
|
||||
p.serialize(data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void send(const T & data)
|
||||
template <details::not_lvalue_reference T>
|
||||
void send(T && data)
|
||||
{
|
||||
thread_local serialization_packet p;
|
||||
serialize(p, data);
|
||||
this->send_raw(p);
|
||||
this->send_raw(std::move(p));
|
||||
}
|
||||
|
||||
void send(const std::span<serialization_packet> & packets)
|
||||
void send(std::vector<serialization_packet> && packets)
|
||||
{
|
||||
thread_local std::vector<const std::vector<std::span<uint8_t>> *> data;
|
||||
thread_local std::vector<serialization_packet> data;
|
||||
data.clear();
|
||||
for (auto & packet: packets)
|
||||
for (serialization_packet & packet: packets)
|
||||
data.emplace_back(packet);
|
||||
this->send_many_raw(data);
|
||||
this->send_many_raw(std::move(data));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user