Jonas Pflanzer 4 yıl önce
ebeveyn
işleme
05b3592a8f

+ 3 - 4
cli/include/batchioman.h

@@ -3,8 +3,8 @@
 
 #include "ioman.h"
 #include <fstream>
-#include <vector>
 #include <string>
+#include <vector>
 
 /**
  * @class BatchIoMan
@@ -27,7 +27,6 @@ private:
 	Json::StreamWriterBuilder wbuilder;
 	string jsonerror;
 
-
 	std::ofstream normalout;
 	std::ofstream errorout;
 	std::ofstream debugout;
@@ -41,12 +40,12 @@ private:
 	bool getnextline;
 	std::mutex linemutex;
 	std::condition_variable linecv;
-	
+
 	/**
 	 * Format and pretty print json for logfile output
 	 */
 	std::string printJson(Json::Value root);
-	
+
 	/**
 	 * Method prototypes for printing json output
 	 */

+ 4 - 6
cli/include/ioman.h

@@ -68,15 +68,15 @@ protected:
 	std::mutex initmutex;
 	std::condition_variable initcv;
 	bool connected;
-	
+
 	virtual void handleInCmdResponse(CmdMan::CmdRet cmdret);
 	virtual void handleOutCmdResponse(CmdMan::CmdRet cmdret, vector<string> &toput);
-	
+
 	boost::system::error_code errcode;
 	tcp::socket *tcpsock;
-	boost::asio::ssl::stream<tcp::socket&> *sslsock;
+	boost::asio::ssl::stream<tcp::socket &> *sslsock;
 	bool usessl;
-	
+
 private:
 	/**
 	 * Internal state to provide class-wide asio networking functionality
@@ -124,8 +124,6 @@ private:
 	std::mutex netmutex;
 	std::condition_variable netcv;
 
-
-
 	/**
 	 * Tokenizes input based on space as seperator
 	 * Respects double-quoted tokens

+ 27 - 24
cli/src/batchioman.cpp

@@ -1,17 +1,17 @@
 #include "../include/batchioman.h"
 
 #include <iostream>
-#include <vector>
 #include <string>
+#include <vector>
 
-BatchIoMan::BatchIoMan(char *ipcstring) : IoMan(ipcstring) {};
+BatchIoMan::BatchIoMan(char *ipcstring) : IoMan(ipcstring){};
 
 BatchIoMan::BatchIoMan(char *ipcstring, bool usessl, std::string batchpath) : IoMan(ipcstring, usessl) {
 	batchin.open(batchpath);
 	normalout.open(batchpath + ".out");
 	debugout.open(batchpath + ".debug");
 	errorout.open(batchpath + ".err");
-	
+
 	/* setup json stuff */
 	Json::CharReaderBuilder rbuilder;
 	wbuilder.settings_["indentation"] = "";
@@ -36,12 +36,13 @@ BatchIoMan::BatchIoMan(char *ipcstring, bool usessl, std::string batchpath) : Io
 	printmap["deleteme"] = &BatchIoMan::printDeleteme;
 	printmap["keyfile"] = &BatchIoMan::printKeyfile;
 	printmap["closekey"] = &BatchIoMan::printClosekey;
-	
+
 	getnextline = false;
 }
 
 bool BatchIoMan::init() {
-	if(!batchin.is_open() || !normalout.is_open() || !debugout.is_open() || !errorout.is_open()) return false;
+	if (!batchin.is_open() || !normalout.is_open() || !debugout.is_open() || !errorout.is_open())
+		return false;
 	return IoMan::init();
 }
 
@@ -92,7 +93,7 @@ void BatchIoMan::handleInCmdResponse(CmdMan::CmdRet cmdret) {
 	// determine wether to send something and do so if required
 	if (cmdret.type & CmdMan::rettype::print) {
 		printMessage(Json::writeString(wbuilder, cmdret.msg), normal);
-		if(!(cmdret.type ^ CmdMan::rettype::print)) {
+		if (!(cmdret.type ^ CmdMan::rettype::print)) {
 			// xor here works because flag is set at this point
 			// if we only printed something get the next line
 			linemutex.lock();
@@ -105,8 +106,10 @@ void BatchIoMan::handleInCmdResponse(CmdMan::CmdRet cmdret) {
 	}
 	if (cmdret.type & CmdMan::rettype::send) {
 		printMessage("IoMan::inputMain() sending json \"" + Json::writeString(wbuilder, cmdret.msg) + "\"", debug);
-		if(usessl) boost::asio::write(*sslsock, boost::asio::buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
-		else boost::asio::write(*tcpsock, boost::asio::buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
+		if (usessl)
+			boost::asio::write(*sslsock, boost::asio::buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
+		else
+			boost::asio::write(*tcpsock, boost::asio::buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
 		if (errcode) {
 			printMessage("IoMan::inputMain() couldnt send json data\n" + errcode.message() + "\n", error);
 			mainmutex.lock();
@@ -200,7 +203,7 @@ void BatchIoMan::handleOutCmdResponse(CmdMan::CmdRet cmdret, std::vector<std::st
 			toput.push_back(cmdret.nextcommand);
 		}
 	}
-	if(versionstatus == on && !(cmdret.type & CmdMan::rettype::send)) {
+	if (versionstatus == on && !(cmdret.type & CmdMan::rettype::send)) {
 		// only fetch next line if we did not send a new command on our own
 		// if we managed to get here, get next command from file
 		linemutex.lock();
@@ -219,48 +222,48 @@ void BatchIoMan::run() {
 	std::unique_lock<std::mutex> ulock;
 
 	runmain = true;
-	
+
 	mainmutex.lock();
 	while (runmain) {
 		mainmutex.unlock();
-		
+
 		line.erase();
 		ulock = std::unique_lock<std::mutex>(linemutex);
 		while (!getnextline && runmain) {
 			linecv.wait(ulock);
 		}
-		
-		if(!runmain) break;
+
+		if (!runmain)
+			break;
 		printMessage(string(__PRETTY_FUNCTION__) + " fetch next line", debug);
-		while(!line.size()) {
+		while (!line.size()) {
 			// skip empty lines until either eof or non-empty line
-			if(batchin.eof()) {
+			if (batchin.eof()) {
 				line = "disconnect";
-			}
-			else std::getline(batchin, line);
+			} else
+				std::getline(batchin, line);
 		}
-		
+
 		getnextline = false;
 		linemutex.unlock();
-		
+
 		localmutex.lock();
 		printMessage(string(__PRETTY_FUNCTION__) + string(" get localmutex"), debug);
 		localinput.push_back(line);
 		localmutex.unlock();
 		printMessage(string(__PRETTY_FUNCTION__) + string(" release localmutex"), debug);
 		localcv.notify_all();
-		
+
 		if (!connected)
 			break;
 		if (loginstatus == err)
 			break;
-		
+
 		mainmutex.lock();
 	}
 	mainmutex.unlock();
 }
 
-
 std::string BatchIoMan::printJson(Json::Value root) {
 	map<string, std::string (BatchIoMan::*)(Json::Value)>::iterator it = printmap.find(root["command"].asString());
 	if (it == printmap.end()) {
@@ -275,8 +278,8 @@ std::string BatchIoMan::printError(Json::Value root) { return std::string("Error
 
 std::string BatchIoMan::printConnect(Json::Value root) {
 	if (!root["accept"].asBool()) {
-		return std::string("Couldnt connect to ") + root["address"].asString() + ":" + std::to_string(root["port"].asUInt()) + "\n"
-		          + "Reason: " + root["error"].asString();
+		return std::string("Couldnt connect to ") + root["address"].asString() + ":" + std::to_string(root["port"].asUInt()) + "\n" +
+		       "Reason: " + root["error"].asString();
 	}
 	return "";
 }

+ 19 - 12
cli/src/ioman.cpp

@@ -27,12 +27,12 @@ void ioman_externalDebugPrint(string msg) { gIOMAN->printMessage(msg, gIOMAN->Ou
 
 IoMan::IoMan(char *ipcstring, bool enablessl) : IoMan(ipcstring) {
 	usessl = enablessl;
-	if(usessl) {
+	if (usessl) {
 		sslctx = new boost::asio::ssl::context(boost::asio::ssl::context::sslv23);
 		sslctx->set_verify_mode(boost::asio::ssl::verify_peer);
 		sslctx->set_options(boost::asio::ssl::context::no_sslv2);
 		sslctx->load_verify_file("rootca.crt");
-		sslsock = new boost::asio::ssl::stream<tcp::socket&>(*tcpsock, *sslctx);
+		sslsock = new boost::asio::ssl::stream<tcp::socket &>(*tcpsock, *sslctx);
 	}
 }
 
@@ -62,7 +62,7 @@ IoMan::~IoMan() {
 	if (connected) {
 		disconnect();
 	}
-	
+
 	if (runnetwork) {
 		networkmutex.lock();
 		runnetwork = false;
@@ -84,9 +84,11 @@ IoMan::~IoMan() {
 		tresponse.join();
 	}
 
-	if(usessl) delete sslsock;
+	if (usessl)
+		delete sslsock;
 	delete tcpsock;
-	if(usessl) delete sslctx;
+	if (usessl)
+		delete sslctx;
 	delete reader;
 }
 
@@ -172,7 +174,7 @@ bool IoMan::connect() {
 		}
 		delete ep;
 	}
-	if(usessl) {
+	if (usessl) {
 		// try to do ssl handshake
 		printMessage(string(__PRETTY_FUNCTION__) + string(" doing ssl handshake with ") + ipstring, debug);
 		sslsock->handshake(boost::asio::ssl::stream_base::client, errcode);
@@ -192,7 +194,8 @@ bool IoMan::connect() {
 void IoMan::disconnect() {
 	printMessage("IoMan::disconnect()", debug);
 	tcpsock->shutdown(tcp::socket::shutdown_both, errcode);
-	if(errcode) printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), error);
+	if (errcode)
+		printMessage(string(__PRETTY_FUNCTION__) + string("tcp shutdown says ") + errcode.message(), error);
 	connected = false;
 }
 
@@ -267,8 +270,10 @@ void IoMan::networkMain() {
 		*/
 
 		// read from network
-		if(usessl) readsize = boost::asio::read_until(*sslsock, recvbuf, '\n', errcode);
-		else readsize = boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
+		if (usessl)
+			readsize = boost::asio::read_until(*sslsock, recvbuf, '\n', errcode);
+		else
+			readsize = boost::asio::read_until(*tcpsock, recvbuf, '\n', errcode);
 		printMessage(string(__PRETTY_FUNCTION__) + string(" asio::read() ok ") + std::to_string(readsize), debug);
 		// printMessage(string("have ") + std::to_string(toprocess.size()) +
 		// string(" commands"), debug);
@@ -366,7 +371,7 @@ void IoMan::inputMain() {
 			command = args.front();
 			args.erase(args.begin());
 			cmdret = cmdman.execute(command, args);
-			
+
 			handleInCmdResponse(cmdret);
 		}
 
@@ -383,8 +388,10 @@ void IoMan::handleInCmdResponse(CmdMan::CmdRet cmdret) {
 	}
 	if (cmdret.type & CmdMan::rettype::send) {
 		printMessage("IoMan::inputMain() sending json \"" + Json::writeString(wbuilder, cmdret.msg) + "\"", debug);
-		if(usessl) boost::asio::write(*sslsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
-		else boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
+		if (usessl)
+			boost::asio::write(*sslsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
+		else
+			boost::asio::write(*tcpsock, buffer(Json::writeString(wbuilder, cmdret.msg) + "\n"), errcode);
 		if (errcode) {
 			printMessage("IoMan::inputMain() couldnt send json data\n" + errcode.message() + "\n", error);
 			return;

+ 2 - 2
cli/src/main.cpp

@@ -1,6 +1,6 @@
+#include "../include/batchioman.h"
 #include "../include/machineioman.h"
 #include "../include/userioman.h"
-#include "../include/batchioman.h"
 
 #include <boost/program_options.hpp>
 #include <iostream>
@@ -58,7 +58,7 @@ int main(int argc, char **argv) {
 		std::fprintf(stderr, "%s\n", ex.what());
 	}
 	std::printf("ip %s machine mode is %d file is %s enablessl is %d\n", argv[1], machine, file ? file : "", usessl);
-	if(batch) {
+	if (batch) {
 		ioman = new BatchIoMan(argv[1], usessl, file);
 	} else if (machine) {
 		ioman = new MachineIoMan(argv[1], usessl);

+ 2 - 2
cli/src/userioman.cpp

@@ -1,10 +1,10 @@
 #include "../include/userioman.h"
 
 #include <iostream>
-#include <vector>
 #include <readline/readline.h>
+#include <vector>
 
-UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {};
+UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring){};
 
 UserIoMan::UserIoMan(char *ipcstring, bool usessl) : IoMan(ipcstring, usessl) {
 	/* setup json stuff */

+ 130 - 161
cli/test/cryptotest-gcm.c

@@ -1,6 +1,6 @@
 #include <openssl/conf.h>
-#include <openssl/evp.h>
 #include <openssl/err.h>
+#include <openssl/evp.h>
 #include <stdio.h>
 #include <string.h>
 
@@ -8,26 +8,17 @@
 // refer to https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption
 
 void handleErrors(void);
-int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
-                unsigned char *key,
-                unsigned char *iv, int iv_len,
-                unsigned char *ciphertext,
-                unsigned char *tag);
-int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len,
-                unsigned char *tag,
-                unsigned char *key,
-                unsigned char *iv, int iv_len,
-                unsigned char *plaintext);
-
-int main (int argc, char** argv)
-{
-    /*
-     * Set up the key and iv. Do I need to say to not hard code these in a
-     * real application? :-)
-     */
-	
+int gcm_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char *ciphertext, unsigned char *tag);
+int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *tag, unsigned char *key, unsigned char *iv, int iv_len, unsigned char *plaintext);
+
+int main(int argc, char **argv) {
+	/*
+	 * Set up the key and iv. Do I need to say to not hard code these in a
+	 * real application? :-)
+	 */
+
 	FILE *f, *o;
-	unsigned char signature[4] = { 'C', 'C', 'A', 'T' };
+	unsigned char signature[4] = {'C', 'C', 'A', 'T'};
 	unsigned char key[32];
 	unsigned char iv[12];
 	unsigned char tag[16];
@@ -35,60 +26,55 @@ int main (int argc, char** argv)
 	unsigned char *cipher;
 	unsigned insize, mode;
 	int decryptedtext_len, ciphertext_len;
-	if(argc < 4) {
+	if (argc < 4) {
 		printf("not enough args. use %s mode key in out\n", argv[0]);
 		return 1;
 	}
-	
+
 	mode = strtoul(argv[1], NULL, 10);
-	
-	if(!(f = fopen(argv[2], "rb"))) {
+
+	if (!(f = fopen(argv[2], "rb"))) {
 		printf("cannot open key\n");
 		return 1;
 	}
 	fread(key, sizeof(key), 1, f);
 	fclose(f);
-	
-	if(!(f = fopen("/dev/urandom", "rb"))) {
+
+	if (!(f = fopen("/dev/urandom", "rb"))) {
 		printf("cannot open urandom\n");
 		return 1;
 	}
 	fread(iv, sizeof(iv), 1, f);
 	fclose(f);
-	
-	if(!(f = fopen(argv[3], "rb"))) {
+
+	if (!(f = fopen(argv[3], "rb"))) {
 		printf("cannot open in\n");
 		return 1;
 	}
-	if(!(o = fopen(argv[4], "wb"))) {
+	if (!(o = fopen(argv[4], "wb"))) {
 		printf("cannot open out\n");
 		return 1;
 	}
-	
-	
-	if(mode) {
+
+	if (mode) {
 		fseek(f, 0, SEEK_END);
-		insize = ftell(f)+4;
+		insize = ftell(f) + 4;
 		fseek(f, 0, SEEK_SET);
 		plain = malloc(insize);
 		cipher = malloc(insize);
-		fread(plain+4, insize, 1, f);
+		fread(plain + 4, insize, 1, f);
 		fclose(f);
 		/* prepend signature */
 		memcpy(plain, signature, 4);
 		/* Encrypt the plaintext */
-		ciphertext_len = gcm_encrypt(plain, insize,
-					 key,
-					 iv, sizeof(iv),
-					 cipher, tag);
+		ciphertext_len = gcm_encrypt(plain, insize, key, iv, sizeof(iv), cipher, tag);
 		fwrite(iv, sizeof(iv), 1, o);
 		fwrite(tag, sizeof(tag), 1, o);
 		fwrite(cipher, ciphertext_len, 1, o);
 		fclose(o);
-	}
-	else {
+	} else {
 		fseek(f, 0, SEEK_END);
-		insize = ftell(f)-sizeof(iv)-sizeof(tag);
+		insize = ftell(f) - sizeof(iv) - sizeof(tag);
 		fseek(f, 0, SEEK_SET);
 		cipher = malloc(insize);
 		plain = malloc(insize);
@@ -96,27 +82,24 @@ int main (int argc, char** argv)
 		fread(tag, sizeof(tag), 1, f);
 		fread(cipher, insize, 1, f);
 		fclose(f);
-		
-		decryptedtext_len = gcm_decrypt(cipher, insize,
-                                    tag,
-                                    key, iv, sizeof(iv),
-                                    plain);
-		if(decryptedtext_len < 0) {
+
+		decryptedtext_len = gcm_decrypt(cipher, insize, tag, key, iv, sizeof(iv), plain);
+		if (decryptedtext_len < 0) {
 			printf("decrypt failed\n");
 			fclose(o);
 			remove(argv[4]);
-		}
-		else if(memcmp(plain, signature, 4)) {
+		} else if (memcmp(plain, signature, 4)) {
 			printf("signature mismatch, expected ");
-			for(int i = 0; i < sizeof(signature); i++) printf("%02x ", signature[i]);
+			for (int i = 0; i < sizeof(signature); i++)
+				printf("%02x ", signature[i]);
 			printf("but got ");
-			for(int i = 0; i < sizeof(signature); i++) printf("%02x ", plain[i]);
+			for (int i = 0; i < sizeof(signature); i++)
+				printf("%02x ", plain[i]);
 			printf("\n");
 			fclose(o);
 			remove(argv[4]);
-		}
-		else {
-			fwrite(plain+4, decryptedtext_len-4, 1, o);
+		} else {
+			fwrite(plain + 4, decryptedtext_len - 4, 1, o);
 			fclose(o);
 		}
 	}
@@ -126,126 +109,112 @@ int main (int argc, char** argv)
 	return 0;
 }
 
-
-void handleErrors(void)
-{
-    ERR_print_errors_fp(stderr);
-    abort();
+void handleErrors(void) {
+	ERR_print_errors_fp(stderr);
+	abort();
 }
 
+int gcm_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, int iv_len, unsigned char *ciphertext, unsigned char *tag) {
+	EVP_CIPHER_CTX *ctx;
 
-int gcm_encrypt(unsigned char *plaintext, int plaintext_len,
-                unsigned char *key,
-                unsigned char *iv, int iv_len,
-                unsigned char *ciphertext,
-                unsigned char *tag)
-{
-    EVP_CIPHER_CTX *ctx;
+	int len;
 
-    int len;
+	int ciphertext_len;
 
-    int ciphertext_len;
+	/* Create and initialise the context */
+	if (!(ctx = EVP_CIPHER_CTX_new()))
+		handleErrors();
 
+	/* Initialise the encryption operation. */
+	if (1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
+		handleErrors();
 
-    /* Create and initialise the context */
-    if(!(ctx = EVP_CIPHER_CTX_new()))
-        handleErrors();
+	/*
+	 * Set IV length if default 12 bytes (96 bits) is not appropriate
+	 */
+	if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
+		handleErrors();
 
-    /* Initialise the encryption operation. */
-    if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
-        handleErrors();
+	/* Initialise key and IV */
+	if (1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
+		handleErrors();
 
-    /*
-     * Set IV length if default 12 bytes (96 bits) is not appropriate
-     */
-    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
-        handleErrors();
+	/*
+	 * Provide the message to be encrypted, and obtain the encrypted output.
+	 * EVP_EncryptUpdate can be called multiple times if necessary
+	 */
+	if (1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
+		handleErrors();
+	ciphertext_len = len;
 
-    /* Initialise key and IV */
-    if(1 != EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
-        handleErrors();
+	/*
+	 * Finalise the encryption. Normally ciphertext bytes may be written at
+	 * this stage, but this does not occur in GCM mode
+	 */
+	if (1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
+		handleErrors();
+	ciphertext_len += len;
 
-    /*
-     * Provide the message to be encrypted, and obtain the encrypted output.
-     * EVP_EncryptUpdate can be called multiple times if necessary
-     */
-    if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len))
-        handleErrors();
-    ciphertext_len = len;
+	/* Get the tag */
+	if (1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
+		handleErrors();
 
-    /*
-     * Finalise the encryption. Normally ciphertext bytes may be written at
-     * this stage, but this does not occur in GCM mode
-     */
-    if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len))
-        handleErrors();
-    ciphertext_len += len;
+	/* Clean up */
+	EVP_CIPHER_CTX_free(ctx);
 
-    /* Get the tag */
-    if(1 != EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag))
-        handleErrors();
-
-    /* Clean up */
-    EVP_CIPHER_CTX_free(ctx);
-
-    return ciphertext_len;
+	return ciphertext_len;
 }
 
-
-int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len,
-                unsigned char *tag,
-                unsigned char *key,
-                unsigned char *iv, int iv_len,
-                unsigned char *plaintext)
-{
-    EVP_CIPHER_CTX *ctx;
-    int len;
-    int plaintext_len;
-    int ret;
-
-    /* Create and initialise the context */
-    if(!(ctx = EVP_CIPHER_CTX_new()))
-        handleErrors();
-
-    /* Initialise the decryption operation. */
-    if(!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
-        handleErrors();
-
-    /* Set IV length. Not necessary if this is 12 bytes (96 bits) */
-    if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
-        handleErrors();
-
-    /* Initialise key and IV */
-    if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
-        handleErrors();
-
-    /*
-     * Provide the message to be decrypted, and obtain the plaintext output.
-     * EVP_DecryptUpdate can be called multiple times if necessary
-     */
-    if(!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
-        handleErrors();
-    plaintext_len = len;
-
-    /* Set expected tag value. Works in OpenSSL 1.0.1d and later */
-    if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
-        handleErrors();
-
-    /*
-     * Finalise the decryption. A positive return value indicates success,
-     * anything else is a failure - the plaintext is not trustworthy.
-     */
-    ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
-
-    /* Clean up */
-    EVP_CIPHER_CTX_free(ctx);
-
-    if(ret > 0) {
-        /* Success */
-        plaintext_len += len;
-        return plaintext_len;
-    } else {
-        /* Verify failed */
-        return -1;
-    }
+int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *tag, unsigned char *key, unsigned char *iv, int iv_len,
+                unsigned char *plaintext) {
+	EVP_CIPHER_CTX *ctx;
+	int len;
+	int plaintext_len;
+	int ret;
+
+	/* Create and initialise the context */
+	if (!(ctx = EVP_CIPHER_CTX_new()))
+		handleErrors();
+
+	/* Initialise the decryption operation. */
+	if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
+		handleErrors();
+
+	/* Set IV length. Not necessary if this is 12 bytes (96 bits) */
+	if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
+		handleErrors();
+
+	/* Initialise key and IV */
+	if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
+		handleErrors();
+
+	/*
+	 * Provide the message to be decrypted, and obtain the plaintext output.
+	 * EVP_DecryptUpdate can be called multiple times if necessary
+	 */
+	if (!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
+		handleErrors();
+	plaintext_len = len;
+
+	/* Set expected tag value. Works in OpenSSL 1.0.1d and later */
+	if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
+		handleErrors();
+
+	/*
+	 * Finalise the decryption. A positive return value indicates success,
+	 * anything else is a failure - the plaintext is not trustworthy.
+	 */
+	ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
+
+	/* Clean up */
+	EVP_CIPHER_CTX_free(ctx);
+
+	if (ret > 0) {
+		/* Success */
+		plaintext_len += len;
+		return plaintext_len;
+	} else {
+		/* Verify failed */
+		return -1;
+	}
 }

+ 6 - 5
daemon/src/CovertChannel/ProxyChannel.cpp

@@ -5,7 +5,8 @@ ProxyChannel::ProxyChannel(const std::string &innerInterface, const std::string
                            const std::string &originIP, const std::string &targetIP, const std::string &targetPort, const std::string &ownMAC,
                            const std::string &originMAC, const std::string &channelGatewayMAC, const std::string &gatewayMAC, const bool relayOnly)
     : CovertChannel(innerInterface, outerInterface,
-                    "(not (tcp and src host " + originIP + " and dst host " + targetIP + " and dst port " + targetPort + ")) and (not (dst host " + ownIP + "))",
+                    "(not (tcp and src host " + originIP + " and dst host " + targetIP + " and dst port " + targetPort + ")) and (not (dst host " + ownIP +
+                        "))",
                     "(not (tcp and src host " + targetIP + " and dst host " + ownIP + " and src port " + targetPort + ")) and (not (dst host " + ownIP + "))",
                     "tcp and src host " + originIP + " and dst host " + targetIP + " and dst port " + targetPort,
                     "tcp and src host " + targetIP + " and dst host " + ownIP + " and src port " + targetPort,
@@ -30,10 +31,10 @@ bool ProxyChannel::handleChannelFromOuter(Tins::PDU &pdu) {
 		outerSender.send(pdu);
 	} else {
 		// Just forward it
-		//eth.src_addr(gatewayMAC);
-		//eth.dst_addr(originMAC);
-		//ip.src_addr(targetAddress);
-		//ip.dst_addr(originAddress);
+		// eth.src_addr(gatewayMAC);
+		// eth.dst_addr(originMAC);
+		// ip.src_addr(targetAddress);
+		// ip.dst_addr(originAddress);
 		innerSender.send(pdu);
 		std::cout << "channel from outer" << std::endl;
 	}

+ 15 - 15
daemon/test/FileManagerMock.h

@@ -1,8 +1,8 @@
 #ifndef FILEMANAGERMOCK_H
 #define FILEMANAGERMOCK_H
 
-#include <gmock/gmock.h>
 #include "../include/FileManager.h"
+#include <gmock/gmock.h>
 
 /**
  * @class FileManagerMock
@@ -11,26 +11,26 @@
  */
 class FileManagerMock : public FileManager {
 public:
-  MOCK_METHOD((std::pair<bool, int>), openGetFile, (const std::string &filename), (override));
-  MOCK_METHOD(bool, openPutFile, (const std::string &filename), (override));
+	MOCK_METHOD((std::pair<bool, int>), openGetFile, (const std::string &filename), (override));
+	MOCK_METHOD(bool, openPutFile, (const std::string &filename), (override));
 
-  MOCK_METHOD(bool, isDownloading, (), (override));
-  MOCK_METHOD(bool, isUploading, (), (override));
+	MOCK_METHOD(bool, isDownloading, (), (override));
+	MOCK_METHOD(bool, isUploading, (), (override));
 
-  MOCK_METHOD(void, cancelPut, (), (override));
+	MOCK_METHOD(void, cancelPut, (), (override));
 
-  MOCK_METHOD(std::string, getGetBaseFileName, (), (override));
-  MOCK_METHOD(std::string, getPutBaseFileName, (), (override));
+	MOCK_METHOD(std::string, getGetBaseFileName, (), (override));
+	MOCK_METHOD(std::string, getPutBaseFileName, (), (override));
 
-  MOCK_METHOD(void, writePut, (const std::vector<char> &data), (override));
-  MOCK_METHOD(std::vector<char>, readGet, (), (override));
+	MOCK_METHOD(void, writePut, (const std::vector<char> &data), (override));
+	MOCK_METHOD(std::vector<char>, readGet, (), (override));
 
-  MOCK_METHOD(int, openList,(), (override));
-  MOCK_METHOD(int, getRemainingListChunks, (), (override));
-  MOCK_METHOD(int, getListSize, (), (override));
-  MOCK_METHOD(std::vector<std::string>, getNextChunkFromList, (), (override));
+	MOCK_METHOD(int, openList, (), (override));
+	MOCK_METHOD(int, getRemainingListChunks, (), (override));
+	MOCK_METHOD(int, getListSize, (), (override));
+	MOCK_METHOD(std::vector<std::string>, getNextChunkFromList, (), (override));
 
-  MOCK_METHOD((std::pair<std::vector<char>, headError>), getBytesFromFile, (const std::string &filename, int numOfBytes), (override));
+	MOCK_METHOD((std::pair<std::vector<char>, headError>), getBytesFromFile, (const std::string &filename, int numOfBytes), (override));
 };
 
 #endif

+ 7 - 7
gui/src/config.h

@@ -7,12 +7,12 @@
 #include <vector>
 
 namespace Config {
-  void setupDefaultConfig();
-  bool checkConfig();
-  bool loadFile();
-  void saveFile();
-  std::string getValue(const std::string &key);
-  void setValue(const std::string &key, const std::string &value);
-}
+void setupDefaultConfig();
+bool checkConfig();
+bool loadFile();
+void saveFile();
+std::string getValue(const std::string &key);
+void setValue(const std::string &key, const std::string &value);
+} // namespace Config
 
 #endif // CONFIG_H

+ 105 - 107
gui/src/qmlhandler.h

@@ -7,131 +7,129 @@
 extern bool _RESTART;
 
 class QMLHandler : public QObject {
-  Q_OBJECT
+	Q_OBJECT
 
 private:
-  void handleJSON(std::string buffer);
-  void readPipeLoop();
-  void reopenCLI(QString ip);
-  void closeCLI();
-  void fileExists(std::string name);
-  void loadSettingsToGUI();
+	void handleJSON(std::string buffer);
+	void readPipeLoop();
+	void reopenCLI(QString ip);
+	void closeCLI();
+	void fileExists(std::string name);
+	void loadSettingsToGUI();
 
 public:
-  explicit QMLHandler(QObject *parent = 0);
-  void onExit();
+	explicit QMLHandler(QObject *parent = 0);
+	void onExit();
 
-
-// C++ -> QML
+	// C++ -> QML
 signals:
-  // No Config Found Popup
-  void noConfigFoundPopupOpen();
-  void noConfigFoundPopupClose();
-
-  // Invalid Cli Path Popup
-  void invalidCliPathPopupOpen();
-  void invalidCliPathPopupClose();
-
-  // Invalid Config Popup
-  void invalidConfigPopupOpen();
-  void invalidConfigPopupClose();
-
-  // Sending
-  void sendingSetFileUrlText(QString signalText);
-  void sendingEnableSendButton();
-  void sendingDisableSendButton();
-
-  // Receiving
-  void receivingClearFileList();
-  void receivingListFile(QString fileName, bool existsLocally);
-  void receivingDisableDownloadButton(QString fileName);
-  void receivingCloseConfirmDeletePopup();
-
-  // Messages
-  void message(QString msg);
-
-  // Settings
-  void closeWindow();
-  void loadSettings(int covertMethod, bool saveIP, bool saveUsername, QString cliPath);
-  
-  // Delete Me Popup
-  void deleteMePopupSetStatus(QString status);
-
-  // Ip Popup
-  void ipPopupSetStatus(QString status);
-  void ipPopupSetIP(QString default_ip);
-  void ipPopupClose();
-  void ipPopupOpen();
-  void ipPopupEnableConnectButton();
-  void ipPopupDisableConnectButton();
-  void ipPopupCheckSaveCheckbox();
-
-  // Login Signup Popup
-  void loginSignupPopupClose();
-  void loginSignupPopupOpen();
-  void loginSignupCheckSaveCheckbox();
-
-  // Login
-  void loginSetStatus(QString status);
-  void loginEnableLoginButton();
-  void loginDisableLoginButton();
-  void loginSetUsername(QString username);
-
-  // Signup
-  void signupSetStatus(QString status);
-  void signupEnableRegisterButton();
-  void signupDisableRegisterButton();
-
-  // Footer
-  void log(QString logText);
-  void footerSetStatus(QString status);
-
-
-// QML -> C++
+	// No Config Found Popup
+	void noConfigFoundPopupOpen();
+	void noConfigFoundPopupClose();
+
+	// Invalid Cli Path Popup
+	void invalidCliPathPopupOpen();
+	void invalidCliPathPopupClose();
+
+	// Invalid Config Popup
+	void invalidConfigPopupOpen();
+	void invalidConfigPopupClose();
+
+	// Sending
+	void sendingSetFileUrlText(QString signalText);
+	void sendingEnableSendButton();
+	void sendingDisableSendButton();
+
+	// Receiving
+	void receivingClearFileList();
+	void receivingListFile(QString fileName, bool existsLocally);
+	void receivingDisableDownloadButton(QString fileName);
+	void receivingCloseConfirmDeletePopup();
+
+	// Messages
+	void message(QString msg);
+
+	// Settings
+	void closeWindow();
+	void loadSettings(int covertMethod, bool saveIP, bool saveUsername, QString cliPath);
+
+	// Delete Me Popup
+	void deleteMePopupSetStatus(QString status);
+
+	// Ip Popup
+	void ipPopupSetStatus(QString status);
+	void ipPopupSetIP(QString default_ip);
+	void ipPopupClose();
+	void ipPopupOpen();
+	void ipPopupEnableConnectButton();
+	void ipPopupDisableConnectButton();
+	void ipPopupCheckSaveCheckbox();
+
+	// Login Signup Popup
+	void loginSignupPopupClose();
+	void loginSignupPopupOpen();
+	void loginSignupCheckSaveCheckbox();
+
+	// Login
+	void loginSetStatus(QString status);
+	void loginEnableLoginButton();
+	void loginDisableLoginButton();
+	void loginSetUsername(QString username);
+
+	// Signup
+	void signupSetStatus(QString status);
+	void signupEnableRegisterButton();
+	void signupDisableRegisterButton();
+
+	// Footer
+	void log(QString logText);
+	void footerSetStatus(QString status);
+
+	// QML -> C++
 public slots:
-  void onStart();
+	void onStart();
 
-  // No Config Found Popup
-  void onNoConfigFoundPopupContinueButton(QString cli_path);
+	// No Config Found Popup
+	void onNoConfigFoundPopupContinueButton(QString cli_path);
 
-  // Invalid Cli Path Popup
-  void onInvalidCliPathPopupContinueButton(QString cli_path);
-  void onInvalidCliPathPopupQuitButton();
+	// Invalid Cli Path Popup
+	void onInvalidCliPathPopupContinueButton(QString cli_path);
+	void onInvalidCliPathPopupQuitButton();
 
-  // Invalid Config Popup
-  void onInvalidConfigPopupCreateDefaultButton();
-  void onInvalidConfigPopupQuitButton();
+	// Invalid Config Popup
+	void onInvalidConfigPopupCreateDefaultButton();
+	void onInvalidConfigPopupQuitButton();
 
-  // Sending
-  void onSendingSelectFileButton(QUrl url);
-  void onSendingSendFileButton();
-  void onSendingClearSelectionButton();
+	// Sending
+	void onSendingSelectFileButton(QUrl url);
+	void onSendingSendFileButton();
+	void onSendingClearSelectionButton();
 
-  // Receiving
-  void onReceivingListFilesButton();
-  void onReceivingDownloadFileButton(QString fileName);
-  void onReceivingConfirmDeleteFileButton(QString fileName);
+	// Receiving
+	void onReceivingListFilesButton();
+	void onReceivingDownloadFileButton(QString fileName);
+	void onReceivingConfirmDeleteFileButton(QString fileName);
 
-  // Messages
-  void onMessagesSendButton(QString msg);
+	// Messages
+	void onMessagesSendButton(QString msg);
 
-  // Settings
-  void onSettingsDeleteMeButton(QString password);
-  void onSettingsRevertChangesButton();
-  void onSettingsResetButton();
-  void onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath);
+	// Settings
+	void onSettingsDeleteMeButton(QString password);
+	void onSettingsRevertChangesButton();
+	void onSettingsResetButton();
+	void onSettingsSaveButton(int covertMethod, bool saveIP, bool saveUsername, QString cliPath);
 
-  // Ip Popup
-  void onIpPopupConnectButton(QString ip, bool saveAsDefault);
+	// Ip Popup
+	void onIpPopupConnectButton(QString ip, bool saveAsDefault);
 
-  // Login
-  void onLoginLoginButton(QString username, QString password, bool saveAsDefault);
+	// Login
+	void onLoginLoginButton(QString username, QString password, bool saveAsDefault);
 
-  // Signup
-  void onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault);
+	// Signup
+	void onSignupRegisterButton(QString username, QString passwordOne, QString passwordTwo, bool saveAsDefault);
 
-  // Footer
-  void onFooterGetStatusButton();
+	// Footer
+	void onFooterGetStatusButton();
 };
 
 #endif // QMLHANDLER_H