fileman.h 4.2 KB

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