Commits

Andreas Tscharner committed e7cbaae

Complete encryption: Make ResourceManager ready

* Update interface to accept encryption type and password
* Implement decryption before decompressing it

Comments (0)

Files changed (2)

src/include/ResourceManager.hxx

  * \c liblrc
  *
  * \author Andreas Tscharner
- * \date 2012-03-24
+ * \date 2012-05-06
  */
 
 
 			unsigned char *m_resData;  //!< Pointer to data of resource file
 			size_t m_resDataSize;      //!< Size of resource data
 			CompressionType m_compType;//!< Compression type for complete file
+			EncryptionType m_encType;  //!< Encryption type for complete file
+			unsigned char *m_password; //!< Password is complete file is encrypted
 
 			/*! \brief Method to load resource file
 			 *
 			 * \param[in] p_resFilename Filename of resource file
 			 * \param[in] p_compress Compression type if the complete file is
 			 *                       compressed
+			 * \param[in[ p_encrypt Encryption type if the complete file is
+			 *                      encrypted
+			 * \param[in] p_key Password if whole file is encrypted
 			 *
 			 * \exception lrcFileNotFoundException Will be thrown if the
 			 *                                     file does not exist
 			 */
-			ResourceManager(const char *, const CompressionType) throw (lrcFileNotFoundException);
+			ResourceManager(const char *, const CompressionType, const EncryptionType, const unsigned char *) throw (lrcFileNotFoundException);
 			/*! \brief Destructor
 			 *
 			 * The destructor cleans up all the used memory of the class

src/lib/ResourceManager.cxx

 //      MA 02110-1301, USA.
 
 
-#include <string.h>
-#include <stdio.h>
+#include <cstring>
+#include <cstdio>
 #include "../Utils.hxx"
 #include "../StatusCodes.hxx"
 #include "../Factories.hxx"
 
 int lrc::ResourceManager::load_from_file(void)
 {
+	const char WHOLE_FILE[] = "Whole file";
+
 	FILE *rdfFile;
 	size_t rdfFileSize;
 	size_t decompSize;
+	size_t decryptSize;
 	int retVal;
 	unsigned int numEntries;
 	size_t readItems;
 	unsigned char *rdfData;
 	unsigned char *decompData;
+	unsigned char *decryptData;
 	unsigned char *bufferPos;
 	CompressDecompress *decompressor;
+	EncryptDecrypt *decryptor;
 
 
 	DEBUG_PRINT(("Checking current data and entries\n"))
 		return ERROR_FILE_READ;
 	};
 
+	DEBUG_PRINT(("Decrypting data with decryption type %d.", m_encType))
+	try {
+		decryptor = EncryptionFactory::get_encryption_class(m_encType, const_cast<char *>(WHOLE_FILE));
+	} catch (lrcEncryptionDisabledException const &encDisEx) {
+		delete[] rdfData;
+		return ERROR_ENCRYPTION_NOT_AVAILABLE;
+	};
+	retVal = decryptor->decrypt(m_password, rdfData, rdfFileSize, &decryptData, decryptSize);
+	delete decryptor;
+	delete[] rdfData;
+	if (!success(retVal)) {
+		DEBUG_PRINT((" Decrypting failed with error code: %d\n", retVal))
+
+		return ERROR_ENCRYPTION_DECRYPT;
+	};
+	DEBUG_PRINT(("\n"))
+
 	DEBUG_PRINT(("Decompressing data with decompressor type %d. ", m_compType))
 	decompressor = CompressionFactory::get_compression_class(m_compType);
-	retVal = decompressor->decompress(rdfData, rdfFileSize, &decompData, decompSize);
+	retVal = decompressor->decompress(decryptData, decryptSize, &decompData, decompSize);
 	delete decompressor;
-	delete[] rdfData;
+	delete[] decryptData;
 	if (! success(retVal)) {
 		DEBUG_PRINT(("Decompression failed with error code: %d\n", retVal))
 
 		return ERROR_COMPRESSION_DECOMPRESS;
 	};
-	DEBUG_PRINT(("Old size: %d; new size: %d\n", rdfFileSize, decompSize))
+	DEBUG_PRINT(("Old size: %d; new size: %d\n", decryptSize, decompSize))
 
 	bufferPos = decompData;
 
 
 	m_numResEntries = numEntries;
 
+	delete[] decompData;
+
 	return NO_ERROR;
 }
 
-lrc::ResourceManager::ResourceManager(const char *p_resFilename, const lrc::CompressionType p_compress) throw (lrcFileNotFoundException)
+lrc::ResourceManager::ResourceManager(const char *p_resFilename,
+                                      const lrc::CompressionType p_compress,
+                                      const lrc::EncryptionType p_encrypt,
+                                      const unsigned char *p_key) throw (lrcFileNotFoundException)
 {
 	size_t len;
 
 	m_resDataSize = -1;
 
 	m_compType = p_compress;
+	m_encType  = p_encrypt;
+	m_password = const_cast<unsigned char *>(p_key);
 }
 
 lrc::ResourceManager::~ResourceManager(void)