Files
funstraw/crypto.h

120 lines
3.6 KiB
C
Raw Normal View History

#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
2015-02-05 16:36:25 +00:00
2015-02-05 12:55:48 +00:00
#include <string>
#include "tlv.h"
class CryptoUtil {
2015-02-05 12:55:48 +00:00
public:
2015-02-07 13:38:51 -08:00
static std::string BinToHex(const std::string& bin);
static void GenKey(std::string* key);
static void GenKeyPair(std::string* secret_key, std::string* public_key);
static void DerivePublicKey(const std::string& secret_key, std::string* public_key);
2015-02-07 13:25:46 -08:00
static void ReadKeyFromFile(const std::string& filename, std::string* key);
static void WriteKeyToFile(const std::string& filename, const std::string& key);
static std::unique_ptr<TLVNode> EncodeEncrypt(const std::string& secret_key, const std::string& public_key, const TLVNode& input);
static std::unique_ptr<TLVNode> DecryptDecode(const std::string& secret_key, const std::string& public_key, const TLVNode& input);
};
2015-02-07 11:18:31 -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
};
class CryptoPubConnBase : public CryptoBase {
protected:
CryptoPubConnBase(const std::string& secret_key);
2015-02-07 15:56:29 -08:00
virtual ~CryptoPubConnBase();
void LogFatal(const std::string& msg, void *obj=nullptr);
std::unique_ptr<TLVNode> BuildSecureHandshake();
bool HandleSecureHandshake(const TLVNode& node);
2015-02-07 15:37:45 -08:00
void EncryptSend(const TLVNode& node);
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;
enum {
AWAITING_HANDSHAKE,
READY,
} state_;
2015-02-07 15:25:22 -08:00
struct bufferevent* bev_;
const std::string secret_key_;
std::string peer_public_key_;
std::string ephemeral_secret_key_;
std::string peer_ephemeral_public_key_;
};
class CryptoPubServerConnection;
class CryptoPubServer : public CryptoBase {
public:
CryptoPubServer(const std::string& secret_key);
~CryptoPubServer();
void Loop();
2015-02-07 16:18:07 -08:00
void Shutdown();
private:
2015-02-07 16:18:07 -08:00
static void Shutdown_(evutil_socket_t sig, short events, void *this__);
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_;
const std::string secret_key_;
};
class CryptoPubServerConnection : public CryptoPubConnBase {
2015-02-05 16:36:25 +00:00
public:
2015-02-07 17:07:31 +01:00
CryptoPubServerConnection(struct bufferevent* bev, const std::string& secret_key);
~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
static void OnError_(struct bufferevent* bev, const short what, void* this__);
void OnError(const short what);
void SendHandshake();
2015-02-07 10:58:20 -08:00
friend CryptoPubServer;
2015-02-05 12:55:48 +00:00
};
2015-02-07 17:07:31 +01:00
class CryptoPubClient : public CryptoPubConnBase {
2015-02-07 17:07:31 +01:00
public:
2015-02-07 15:37:45 -08:00
CryptoPubClient(struct sockaddr* addr, socklen_t addrlen, const std::string& secret_key, const std::string& server_public_key, const std::list<uint32_t>& channel_bitrates);
2015-02-07 17:07:31 +01:00
~CryptoPubClient();
2015-02-07 15:37:45 -08:00
static CryptoPubClient* FromHostname(const std::string& server_address, const std::string& server_port, const std::string& secret_key, const std::string& server_public_key, const std::list<uint32_t>& channel_bitrates);
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
void SendHandshake();
void SendTunnelRequest();
2015-02-07 17:07:31 +01:00
struct event_base* event_base_;
2015-02-07 15:37:45 -08:00
const std::list<uint32_t> channel_bitrates_;
2015-02-07 17:07:31 +01:00
};