fileman.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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::ifstream putfile;
  29. std::fstream getfile;
  30. std::vector<Json::Value> listdata;
  31. std::string getpath, getname, putpath, putname, cipherpath;
  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. void writeEnc(const std::vector<char> data);
  60. const char signature[4] = {'C', 'C', 'A', 'T'};
  61. enum decryptability { unknown, plaintext, decryptable, undecryptable };
  62. decryptability isDecryptable(const std::vector<char> data);
  63. public:
  64. /**
  65. * Constructor and Destructor
  66. */
  67. FileMan();
  68. ~FileMan();
  69. /**
  70. * Query internal state
  71. *
  72. * Return true if the corresponding action is being performed, false otherwise.
  73. * isListing returns true if either a list from the "list" or "extendedlist" is built.
  74. * isListingExtended corresponds to the "extendedlist" command, isListingSimple to "list".
  75. */
  76. virtual bool isGetting();
  77. virtual bool isPutting();
  78. virtual bool isListing();
  79. virtual bool isListingSimple();
  80. virtual bool isListingExtended();
  81. virtual bool isEncrypted();
  82. /**
  83. * Check for and prepare state and streams for reading/writing
  84. *
  85. * Return true if successful, false otherwise
  86. */
  87. virtual bool openPut(const std::string &path);
  88. virtual bool openGet(const std::string &path);
  89. virtual bool openList(bool extended);
  90. /**
  91. * Open file and read a hex string from it as key and initialize the IV
  92. * This will fail is a put or a get is in progress
  93. *
  94. * Return true if successful, false otherwise. After this put and get will en/decrypt data
  95. */
  96. virtual bool openKey(const std::string &path);
  97. /**
  98. * Close the respective filestream
  99. */
  100. virtual void closePut();
  101. virtual void closeGet();
  102. virtual void closeList();
  103. /**
  104. * Reset internal key state and disable en/decryption of data
  105. * This will fail if a put or get is in progress
  106. *
  107. * Return true if key was reset, false otherwise. After this put and get will use unencrypted data
  108. */
  109. virtual bool closeKey();
  110. /**
  111. * Query the names of the file currently being put or get
  112. */
  113. virtual std::string getPutName();
  114. virtual std::string getGetName();
  115. /**
  116. * Cancel a put, get or list, depreparing internal state (closing streams if
  117. * required)
  118. */
  119. virtual void cancelPut();
  120. virtual void cancelGet();
  121. virtual void cancelList();
  122. /**
  123. * Read max_rea_len bytes from the current file opened for put
  124. */
  125. virtual std::vector<char> readPut();
  126. /**
  127. * Write the provided vector to the current file opened for get
  128. */
  129. virtual void writeGet(std::vector<char> data);
  130. /**
  131. * Wrapper methods for reading and writing base64 encoded data instead of raw
  132. * bytes
  133. */
  134. virtual std::string readBase64();
  135. virtual void writeBase64(std::string data);
  136. /**
  137. * read and write emulating methods for list
  138. */
  139. virtual void putListData(std::vector<Json::Value> names);
  140. virtual std::vector<Json::Value> getListData();
  141. /**
  142. * Query internal state, requesting the corresponding size
  143. */
  144. virtual int getPutChunks();
  145. virtual int getGetChunks();
  146. virtual int getListChunks();
  147. virtual int getPutRemainingChunks();
  148. virtual int getGetRemainingChunks();
  149. virtual int getListRemainingChunks();
  150. virtual int getPutSize();
  151. /**
  152. * Set internal state, adjusting the chunks as well as chunks remaining for
  153. * get and list
  154. */
  155. virtual void setGetChunks(int chunks);
  156. virtual void setListChunks(int chunks);
  157. /**
  158. * Returns the filename of the passed (relative) path of a file
  159. */
  160. virtual std::string pathToFilename(std::string path);
  161. virtual std::string getOpensslError();
  162. };
  163. #endif