122 lines
3.7 KiB
C++
122 lines
3.7 KiB
C++
#pragma once
|
|
#include "config.hpp"
|
|
#include <arpa/inet.h>
|
|
#include <memory>
|
|
#include <netinet/in.h>
|
|
#include <openssl/err.h>
|
|
#include <openssl/evp.h>
|
|
#include <openssl/ssl.h>
|
|
#include <sys/socket.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
using Bytes = std::vector<unsigned char>;
|
|
using Context = std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)>;
|
|
using Session = std::unique_ptr<SSL, decltype(&SSL_free)>;
|
|
struct FD {
|
|
int fd;
|
|
explicit FD(int s) : fd(s) { require(s >= 0, "socket failed"); }
|
|
FD(const FD &) = delete;
|
|
~FD() { close(fd); }
|
|
};
|
|
inline void sslok(int rc, const char *op) {
|
|
if (rc != 1)
|
|
throw std::runtime_error(std::string(op) + " / " +
|
|
ERR_error_string(ERR_get_error(), nullptr));
|
|
}
|
|
inline Context context(bool client) {
|
|
Context ctx(SSL_CTX_new(client ? TLS_client_method() : TLS_server_method()),
|
|
SSL_CTX_free);
|
|
require(bool(ctx), "TLS context");
|
|
sslok(SSL_CTX_set_min_proto_version(ctx.get(), TLS1_3_VERSION),
|
|
"TLS minimum");
|
|
sslok(SSL_CTX_set_max_proto_version(ctx.get(), TLS1_3_VERSION),
|
|
"TLS maximum");
|
|
return ctx;
|
|
}
|
|
inline Session session(SSL_CTX *ctx, int fd) {
|
|
Session s(SSL_new(ctx), SSL_free);
|
|
require(bool(s), "TLS allocation");
|
|
sslok(SSL_set_fd(s.get(), fd), "TLS socket");
|
|
return s;
|
|
}
|
|
enum Type { CONFIG = 1, ACCEPTED, FRAME, ACK, FINISHED, ERROR_MSG };
|
|
inline void put(Bytes &b, u64 v, int width) {
|
|
for (int i = width - 1; i >= 0; --i)
|
|
b.push_back(v >> (8 * i));
|
|
}
|
|
inline u64 get(const unsigned char *p, int width) {
|
|
u64 v = 0;
|
|
while (width--)
|
|
v = (v << 8) | *p++;
|
|
return v;
|
|
}
|
|
inline void sendBytes(SSL *s, const void *data, size_t n) {
|
|
auto p = static_cast<const unsigned char *>(data);
|
|
while (n) {
|
|
size_t m = 0;
|
|
sslok(SSL_write_ex(s, p, n, &m), "TLS write");
|
|
p += m;
|
|
n -= m;
|
|
}
|
|
}
|
|
inline void recvBytes(SSL *s, void *data, size_t n) {
|
|
auto p = static_cast<unsigned char *>(data);
|
|
while (n) {
|
|
size_t m = 0;
|
|
sslok(SSL_read_ex(s, p, n, &m), "TLS read");
|
|
p += m;
|
|
n -= m;
|
|
}
|
|
}
|
|
inline void header(SSL *s, int type, u64 n) {
|
|
Bytes b;
|
|
put(b, 0x52505331, 4);
|
|
put(b, 1, 2);
|
|
put(b, type, 2);
|
|
put(b, n, 8);
|
|
sendBytes(s, b.data(), 16);
|
|
}
|
|
inline void sendMsg(SSL *s, int type, const Bytes &b) {
|
|
header(s, type, b.size());
|
|
sendBytes(s, b.data(), b.size());
|
|
}
|
|
inline std::pair<int, Bytes> receive(SSL *s,
|
|
std::initializer_list<int> allowed) {
|
|
unsigned char h[16];
|
|
recvBytes(s, h, 16);
|
|
int t = get(h + 6, 2);
|
|
u64 n = get(h + 8, 8);
|
|
require(get(h, 4) == 0x52505331 && get(h + 4, 2) == 1 &&
|
|
std::find(allowed.begin(), allowed.end(), t) != allowed.end(),
|
|
"unexpected header");
|
|
require(((t == CONFIG || t == ACCEPTED) && n && n <= 65536) ||
|
|
(t == FRAME && n >= 36 && n <= 33554464) ||
|
|
(t == ACK && n == 8) || (t == FINISHED && n == 12) ||
|
|
(t == ERROR_MSG && n >= 4 && n <= 4096),
|
|
"invalid payload length");
|
|
Bytes b(n);
|
|
recvBytes(s, b.data(), n);
|
|
if (t == ERROR_MSG)
|
|
throw std::runtime_error(std::string(b.begin() + 4, b.end()));
|
|
return {t, std::move(b)};
|
|
}
|
|
struct HashException : std::runtime_error {
|
|
using std::runtime_error::runtime_error;
|
|
};
|
|
inline Bytes digest(const Bytes &bytes) {
|
|
Bytes out(32);
|
|
unsigned len = 0;
|
|
if (EVP_Digest(bytes.data(), bytes.size(), out.data(), &len, EVP_sha256(),
|
|
nullptr) != 1 ||
|
|
len != 32)
|
|
throw HashException("SHA256 failed");
|
|
return out;
|
|
}
|
|
inline u64 hash64(u64 seed) {
|
|
const char prefix[] = "RPS-PRIORITY-v1";
|
|
Bytes msg(prefix, prefix + sizeof(prefix));
|
|
put(msg, seed, 8);
|
|
return get(digest(msg).data(), 8);
|
|
}
|
|
|
|
inline int priority(u64 seed) { return hash64(seed) % 6; }
|