fileman.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef FILEMAN_H
  2. #define FILEMAN_H
  3. #include <fstream>
  4. #include <json/json.h>
  5. #include <openssl/conf.h>
  6. #include <openssl/err.h>
  7. #include <openssl/evp.h>
  8. #include <string>
  9. #include <vector>
  10. /**
  11. * @class FileMan
  12. *
  13. * Provides File I/O abstraction
  14. */
  15. class FileMan {
  16. private:
  17. /**
  18. * Internal state
  19. *
  20. * Filestreams for put and get
  21. * Vector for holding received filenames from listing
  22. * Paths and filenames for put and get
  23. * Size for internal read
  24. * Total and Remaining chunks for put, get and list
  25. * Boolean replacement for filestreams being open for list
  26. *
  27. */
  28. std::vector<std::vector<char>> putdata;
  29. std::fstream getfile;
  30. std::vector<Json::Value> listdata;
  31. std::string getpath, getname, putpath, putname;
  32. const unsigned int max_read_len = 4096;
  33. int putsize;
  34. int putchunks;
  35. int putchunksRemaining;
  36. int getchunks;
  37. int getchunksRemaining;
  38. int listchunks;
  39. int listchunksRemaining;
  40. bool isputting;
  41. bool islisting;
  42. bool keyenabled;
  43. bool cryptoreadye;
  44. bool cryptoreadyd;
  45. bool pendingerr;
  46. bool extendedListing;
  47. unsigned char iv[12]; // 96bits
  48. unsigned char tag[16]; // 128bits
  49. unsigned char key[32]; // 256bits
  50. const unsigned int cipherblocklen = 128;
  51. EVP_CIPHER_CTX *cryptctxe;
  52. EVP_CIPHER_CTX *cryptctxd;
  53. std::string opensslerr;
  54. void setOpensslError();
  55. bool initCryptoE();
  56. bool initCryptoD();
  57. void deinitCryptoE();
  58. void deinitCryptoD();
  59. std::vector<std::vector<char>> chunkify(char *data, unsigned int size);
  60. void writeEnc(const std::vector<char> data);
  61. const char signature[4] = {'C', 'C', 'A', 'T'};
  62. enum decryptability { unknown, plaintext, decryptable, undecryptable };
  63. decryptability isDecryptable(const std::vector<char> data);
  64. public:
  65. /**
  66. * Constructor and Destructor
  67. */
  68. FileMan();
  69. ~FileMan();
  70. /**
  71. * Query internal state
  72. *
  73. * Return true if the corresponding action is being performed, false otherwise.
  74. * isListing returns true if either a list from the "list" or "extendedlist" is built.
  75. * isListingExtended corresponds to the "extendedlist" command, isListingSimple to "list".
  76. */
  77. virtual bool isGetting();
  78. virtual bool isPutting();
  79. virtual bool isListing();
  80. virtual bool isListingSimple();
  81. virtual bool isListingExtended();
  82. virtual bool isEncrypted();
  83. /**
  84. * Check for and prepare state and streams for reading/writing
  85. *
  86. * Return true if successful, false otherwise
  87. */
  88. virtual bool openPut(const std::string &path);
  89. virtual bool openGet(const std::string &path);
  90. virtual bool openList(bool extended);
  91. /**
  92. * Open file and read a hex string from it as key and initialize the IV
  93. * This will fail is a put or a get is in progress
  94. *
  95. * Return true if successful, false otherwise. After this put and get will en/decrypt data
  96. */
  97. virtual bool openKey(const std::string &path);
  98. /**
  99. * Close the respective filestream
  100. */
  101. virtual void closePut();
  102. virtual void closeGet();
  103. virtual void closeList();
  104. /**
  105. * Reset internal key state and disable en/decryption of data
  106. * This will fail if a put or get is in progress
  107. *
  108. * Return true if key was reset, false otherwise. After this put and get will use unencrypted data
  109. */
  110. virtual bool closeKey();
  111. /**
  112. * Query the names of the file currently being put or get
  113. */
  114. virtual std::string getPutName();
  115. virtual std::string getGetName();
  116. /**
  117. * Cancel a put, get or list, depreparing internal state (closing streams if
  118. * required)
  119. */
  120. virtual void cancelPut();
  121. virtual void cancelGet();
  122. virtual void cancelList();
  123. /**
  124. * Read max_rea_len bytes from the current file opened for put
  125. */
  126. virtual std::vector<char> readPut();
  127. /**
  128. * Write the provided vector to the current file opened for get
  129. */
  130. virtual void writeGet(std::vector<char> data);
  131. /**
  132. * Wrapper methods for reading and writing base64 encoded data instead of raw
  133. * bytes
  134. */
  135. virtual std::string readBase64();
  136. virtual void writeBase64(std::string data);
  137. /**
  138. * read and write emulating methods for list
  139. */
  140. virtual void putListData(std::vector<Json::Value> names);
  141. virtual std::vector<Json::Value> getListData();
  142. /**
  143. * Query internal state, requesting the corresponding size
  144. */
  145. virtual int getPutChunks();
  146. virtual int getGetChunks();
  147. virtual int getListChunks();
  148. virtual int getPutRemainingChunks();
  149. virtual int getGetRemainingChunks();
  150. virtual int getListRemainingChunks();
  151. virtual int getPutSize();
  152. /**
  153. * Set internal state, adjusting the chunks as well as chunks remaining for
  154. * get and list
  155. */
  156. virtual void setGetChunks(int chunks);
  157. virtual void setListChunks(int chunks);
  158. /**
  159. * Returns the filename of the passed (relative) path of a file
  160. */
  161. virtual std::string pathToFilename(std::string path);
  162. virtual std::string getOpensslError();
  163. };
  164. #endif