2015-02-07 16:44:42 +01:00
|
|
|
#include <event2/bufferevent.h>
|
|
|
|
|
#include <event2/event.h>
|
|
|
|
|
#include <event2/listener.h>
|
2015-02-08 19:02:37 +00:00
|
|
|
#include <sodium/core.h>
|
2015-02-05 16:36:25 +00:00
|
|
|
|
2015-02-05 12:55:48 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
2015-02-07 19:01:48 +01:00
|
|
|
#include "tlv.h"
|
|
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
class CryptoKey {
|
|
|
|
|
public:
|
|
|
|
|
CryptoKey(const size_t key_bytes);
|
|
|
|
|
~CryptoKey();
|
|
|
|
|
void ReadFromFile(const std::string& filename);
|
|
|
|
|
void WriteToFile(const std::string& filename) const;
|
|
|
|
|
|
|
|
|
|
const unsigned char* Key() const;
|
2015-02-08 19:50:09 +00:00
|
|
|
bool IsSet() const;
|
2015-02-08 19:02:37 +00:00
|
|
|
|
|
|
|
|
unsigned char* MutableKey();
|
|
|
|
|
void MarkSet();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
unsigned char* const key_;
|
|
|
|
|
bool is_set_;
|
|
|
|
|
const size_t key_bytes_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SharedKey : public CryptoKey {
|
|
|
|
|
public:
|
|
|
|
|
SharedKey();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SecretKey : public CryptoKey {
|
|
|
|
|
public:
|
|
|
|
|
SecretKey();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class PublicKey : public CryptoKey {
|
2015-02-05 12:55:48 +00:00
|
|
|
public:
|
2015-02-08 19:02:37 +00:00
|
|
|
PublicKey();
|
2015-02-07 13:38:51 -08:00
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
std::string AsString() const;
|
|
|
|
|
std::string ToHex() const;
|
|
|
|
|
void FromString(const std::string& str);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CryptoUtil {
|
|
|
|
|
public:
|
|
|
|
|
static void GenKey(SharedKey* key);
|
|
|
|
|
static void GenKeyPair(SecretKey* secret_key, PublicKey* public_key);
|
|
|
|
|
static void DerivePublicKey(const SecretKey& secret_key, PublicKey* public_key);
|
2015-02-07 13:25:46 -08:00
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
static std::unique_ptr<TLVNode> EncodeEncrypt(const SecretKey& secret_key, const PublicKey& public_key, const TLVNode& input);
|
|
|
|
|
static std::unique_ptr<TLVNode> DecryptDecode(const SecretKey& secret_key, const PublicKey& public_key, const TLVNode& input);
|
2015-02-07 14:32:53 -08:00
|
|
|
};
|
2015-02-07 11:18:31 -08:00
|
|
|
|
2015-02-07 14:32:53 -08:00
|
|
|
class CryptoBase {
|
2015-02-07 15:56:29 -08:00
|
|
|
protected:
|
2015-02-07 11:18:31 -08:00
|
|
|
std::ostream& Log(void *obj=nullptr);
|
2015-02-05 16:36:25 +00:00
|
|
|
};
|
|
|
|
|
|
2015-02-07 15:26:39 -08:00
|
|
|
class CryptoPubConnBase : public CryptoBase {
|
2015-02-07 14:32:53 -08:00
|
|
|
protected:
|
2015-02-08 19:02:37 +00:00
|
|
|
CryptoPubConnBase(const SecretKey& secret_key);
|
2015-02-07 15:56:29 -08:00
|
|
|
virtual ~CryptoPubConnBase();
|
|
|
|
|
|
|
|
|
|
void LogFatal(const std::string& msg, void *obj=nullptr);
|
2015-02-07 14:32:53 -08:00
|
|
|
|
2015-02-07 15:07:34 -08:00
|
|
|
std::unique_ptr<TLVNode> BuildSecureHandshake();
|
2015-02-08 19:50:09 +00:00
|
|
|
std::unique_ptr<TLVNode> BuildHandshake();
|
|
|
|
|
void SendHandshake();
|
|
|
|
|
|
2015-02-07 15:07:34 -08:00
|
|
|
bool HandleSecureHandshake(const TLVNode& node);
|
2015-02-08 19:50:09 +00:00
|
|
|
bool HandleHandshake(const TLVNode& node);
|
|
|
|
|
|
2015-02-07 15:37:45 -08:00
|
|
|
void EncryptSend(const TLVNode& node);
|
2015-02-07 15:07:34 -08:00
|
|
|
|
2015-02-07 15:25:22 -08:00
|
|
|
static void OnReadable_(struct bufferevent* bev, void* this__);
|
|
|
|
|
void OnReadable();
|
|
|
|
|
virtual void OnHandshake(const TLVNode& decoded) = 0;
|
|
|
|
|
virtual bool OnMessage(const TLVNode& node) = 0;
|
|
|
|
|
|
2015-02-07 14:32:53 -08:00
|
|
|
enum {
|
|
|
|
|
AWAITING_HANDSHAKE,
|
|
|
|
|
READY,
|
|
|
|
|
} state_;
|
|
|
|
|
|
2015-02-07 15:25:22 -08:00
|
|
|
struct bufferevent* bev_;
|
|
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
const SecretKey& secret_key_;
|
|
|
|
|
PublicKey peer_public_key_;
|
|
|
|
|
SecretKey ephemeral_secret_key_;
|
|
|
|
|
PublicKey peer_ephemeral_public_key_;
|
2015-02-07 14:32:53 -08:00
|
|
|
};
|
|
|
|
|
|
2015-02-07 17:15:09 +01:00
|
|
|
class CryptoPubServerConnection;
|
|
|
|
|
|
|
|
|
|
class CryptoPubServer : public CryptoBase {
|
|
|
|
|
public:
|
2015-02-08 19:02:37 +00:00
|
|
|
CryptoPubServer(const SecretKey& secret_key);
|
2015-02-07 17:15:09 +01:00
|
|
|
~CryptoPubServer();
|
|
|
|
|
void Loop();
|
2015-02-07 16:18:07 -08:00
|
|
|
void Shutdown();
|
2015-02-07 17:15:09 +01:00
|
|
|
|
|
|
|
|
private:
|
2015-02-07 16:18:07 -08:00
|
|
|
static void Shutdown_(evutil_socket_t sig, short events, void *this__);
|
|
|
|
|
|
2015-02-07 17:15:09 +01:00
|
|
|
static void OnNewConn_(struct evconnlistener* listener, int fd, struct sockaddr* client_addr, int client_addrlen, void* this__);
|
|
|
|
|
void OnNewConn(int fd, struct sockaddr* client_addr, int client_addrlen);
|
|
|
|
|
|
|
|
|
|
struct event_base* event_base_;
|
|
|
|
|
struct evconnlistener* listener_;
|
2015-02-08 19:13:00 +00:00
|
|
|
struct event* sigevent_;
|
2015-02-07 17:15:09 +01:00
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
const SecretKey& secret_key_;
|
2015-02-07 17:15:09 +01:00
|
|
|
};
|
|
|
|
|
|
2015-02-07 15:26:39 -08:00
|
|
|
class CryptoPubServerConnection : public CryptoPubConnBase {
|
2015-02-05 16:36:25 +00:00
|
|
|
public:
|
2015-02-08 19:02:37 +00:00
|
|
|
CryptoPubServerConnection(struct bufferevent* bev, const SecretKey& secret_key);
|
2015-02-07 16:44:42 +01:00
|
|
|
~CryptoPubServerConnection();
|
2015-02-05 16:36:25 +00:00
|
|
|
|
|
|
|
|
private:
|
2015-02-07 13:49:19 -08:00
|
|
|
void OnHandshake(const TLVNode& decoded);
|
2015-02-07 15:25:22 -08:00
|
|
|
bool OnMessage(const TLVNode& node);
|
2015-02-07 16:04:40 -08:00
|
|
|
bool OnTunnelRequest(const TLVNode& node);
|
2015-02-07 15:25:22 -08:00
|
|
|
|
2015-02-07 17:15:09 +01:00
|
|
|
static void OnError_(struct bufferevent* bev, const short what, void* this__);
|
|
|
|
|
void OnError(const short what);
|
|
|
|
|
|
|
|
|
|
friend CryptoPubServer;
|
2015-02-05 12:55:48 +00:00
|
|
|
};
|
2015-02-07 17:07:31 +01:00
|
|
|
|
2015-02-07 15:26:39 -08:00
|
|
|
class CryptoPubClient : public CryptoPubConnBase {
|
2015-02-07 17:07:31 +01:00
|
|
|
public:
|
2015-02-08 19:02:37 +00:00
|
|
|
CryptoPubClient(struct sockaddr* addr, socklen_t addrlen, const SecretKey& secret_key, const PublicKey& server_public_key, const std::list<uint32_t>& channel_bitrates);
|
2015-02-07 17:07:31 +01:00
|
|
|
~CryptoPubClient();
|
2015-02-07 17:15:09 +01:00
|
|
|
|
2015-02-08 19:02:37 +00:00
|
|
|
static CryptoPubClient* FromHostname(const std::string& server_address, const std::string& server_port, const SecretKey& secret_key, const PublicKey& server_public_key, const std::list<uint32_t>& channel_bitrates);
|
2015-02-07 17:15:09 +01:00
|
|
|
|
2015-02-07 17:07:31 +01:00
|
|
|
void Loop();
|
|
|
|
|
|
|
|
|
|
private:
|
2015-02-07 13:49:19 -08:00
|
|
|
void OnHandshake(const TLVNode& decoded);
|
2015-02-07 15:25:22 -08:00
|
|
|
bool OnMessage(const TLVNode& node);
|
|
|
|
|
|
2015-02-07 17:32:57 +01:00
|
|
|
static void OnConnectOrError_(struct bufferevent* bev, const short what, void* this__);
|
|
|
|
|
void OnConnect();
|
2015-02-07 11:18:31 -08:00
|
|
|
void OnError();
|
2015-02-07 17:32:57 +01:00
|
|
|
|
2015-02-07 14:32:53 -08:00
|
|
|
void SendTunnelRequest();
|
|
|
|
|
|
2015-02-07 17:07:31 +01:00
|
|
|
struct event_base* event_base_;
|
2015-02-07 19:01:48 +01:00
|
|
|
|
2015-02-07 15:37:45 -08:00
|
|
|
const std::list<uint32_t> channel_bitrates_;
|
2015-02-07 17:07:31 +01:00
|
|
|
};
|