Sfoglia il codice sorgente

Fix isDecryptable crashing on empty data argument, add unknown as result for bad signature or filesize, minor formatting fixes for extendedlist output in user and batchmodes

Missingmew 4 anni fa
parent
commit
56ebe6309f
5 ha cambiato i file con 19 aggiunte e 14 eliminazioni
  1. 1 1
      GUI-CLI Protocol.md
  2. 1 1
      cli/include/fileman.h
  3. 5 3
      cli/src/batchioman.cpp
  4. 7 6
      cli/src/fileman.cpp
  5. 5 3
      cli/src/userioman.cpp

+ 1 - 1
GUI-CLI Protocol.md

@@ -348,7 +348,7 @@ CLI:
 ```
 
 The list contains the `name` of a file and its `size` in kByte. <br />
-The `encrypted` field contains information wether the file is `unencrypted`, `decryptable` or `undecryptable`.
+The `encrypted` field contains information wether the file is `unknown` in case of bad signature or file size, `unencrypted`, `decryptable` or `undecryptable`.
 
 ## 3. Disconnecting and exiting
 

+ 1 - 1
cli/include/fileman.h

@@ -68,7 +68,7 @@ private:
 	void writeEnc(const std::vector<char> data);
 	const char signature[4] = {'C', 'C', 'A', 'T'};
 
-	enum decryptability { plaintext, decryptable, undecryptable };
+	enum decryptability { unknown, plaintext, decryptable, undecryptable };
 	decryptability isDecryptable(const std::vector<char> data);
 
 public:

+ 5 - 3
cli/src/batchioman.cpp

@@ -439,11 +439,13 @@ std::string BatchIoMan::printExtendedlist(Json::Value root) {
 				std::string encrypted = val["encrypted"].asString();
 				std::string decryptable;
 				if (encrypted == "decryptable") {
-					decryptable = "yes        ";
+					decryptable = "yes          ";
 				} else if (encrypted == "undecryptable") {
-					decryptable = "no         ";
+					decryptable = "no           ";
+				} else if (encrypted == "unknown") {
+					decryptable = "unknown      ";
 				} else {
-					decryptable = "plaintext  ";
+					decryptable = "plaintext    ";
 				}
 				std::string progress = std::to_string(val["progress"].asInt());
 				std::string file = val["name"].asString();

+ 7 - 6
cli/src/fileman.cpp

@@ -256,9 +256,10 @@ void FileMan::putListData(vector<Json::Value> files) {
 			fileInfo["size"] = file["size"];
 
 			string headString = file["head"].asString();
-			std::cerr << "checking decryptability of file " << file["name"].asString() << std::endl;
 			decryptability decr = isDecryptable(base64::decodeVector(headString));
-			if (decr == FileMan::undecryptable) {
+			if (decr == FileMan::unknown) {
+				fileInfo["encrypted"] = "unknown";
+			} else if (decr == FileMan::undecryptable) {
 				fileInfo["encrypted"] = "undecryptable";
 			} else if (decr == FileMan::decryptable) {
 				fileInfo["encrypted"] = "decryptable";
@@ -457,8 +458,8 @@ FileMan::decryptability FileMan::isDecryptable(const vector<char> data) {
 	unsigned char plain[sizeof(signature)] = {0}, *cipher;
 	int plainlen;
 	// check if signature matches in plaintext
-	if (memcmp(data.data(), signature, sizeof(signature))) {
-		// it does not
+	if (data.size() < sizeof(signature) || memcmp(data.data(), signature, sizeof(signature))) {
+		// either size or signature dont match
 		if (data.size() >= (sizeof(signature) + sizeof(iv) + sizeof(tag)) && keyenabled) {
 			// enough data, key is enabled
 			memcpy(iv, data.data(), sizeof(iv));
@@ -484,8 +485,8 @@ FileMan::decryptability FileMan::isDecryptable(const vector<char> data) {
 				return decryptable;
 			}
 		}
-		// no key enabled or decryption failed, assume undecryptable
-		return undecryptable;
+		// size mismatch or no key enabled, consider unknown
+		return unknown;
 	}
 	// it does, assume plaintext
 	return plaintext;

+ 5 - 3
cli/src/userioman.cpp

@@ -232,11 +232,13 @@ void UserIoMan::printExtendedlist(Json::Value root) {
 				std::string encrypted = val["encrypted"].asString();
 				std::string decryptable;
 				if (encrypted == "decryptable") {
-					decryptable = "\033[32myes\033[0m        "; // "yes" in green
+					decryptable = "\033[32myes\033[0m          "; // "yes" in green
 				} else if (encrypted == "undecryptable") {
-					decryptable = "\033[31mno\033[0m         "; // "no" in red
+					decryptable = "\033[31mno\033[0m           "; // "no" in red
+				} else if (encrypted == "unknown") {
+					decryptable = "unknown      "; // "no" in red
 				} else {
-					decryptable = "plaintext  ";
+					decryptable = "plaintext    ";
 				}
 				std::string progress = std::to_string(val["progress"].asInt());
 				std::string file = val["name"].asString();