Files
funstraw/crypto.h

158 lines
4.1 KiB
C
Raw Normal View History

#include <event2/bufferevent.h>
#include <event2/event.h>
#include <event2/listener.h>
#include <sodium/core.h>
2015-02-05 16:36:25 +00:00
2015-02-05 12:55:48 +00:00
#include <string>
#include "tlv.h"
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;
bool IsSet() const;
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:
PublicKey();
2015-02-07 13:38:51 -08: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
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 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 SecretKey& 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();
std::unique_ptr<TLVNode> BuildHandshake();
void SendHandshake();
bool HandleSecureHandshake(const TLVNode& node);
bool HandleHandshake(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 SecretKey& secret_key_;
PublicKey peer_public_key_;
SecretKey ephemeral_secret_key_;
PublicKey peer_ephemeral_public_key_;
};
class CryptoPubServerConnection;
class CryptoPubServer : public CryptoBase {
public:
CryptoPubServer(const SecretKey& 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_;
struct event* sigevent_;
const SecretKey& secret_key_;
};
class CryptoPubServerConnection : public CryptoPubConnBase {
2015-02-05 16:36:25 +00:00
public:
CryptoPubServerConnection(struct bufferevent* bev, const SecretKey& 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);
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:
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();
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: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 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
};