file_access_encrypted.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**************************************************************************/
  2. /* file_access_encrypted.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "file_access_encrypted.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/print_string.h"
  33. #include "core/variant.h"
  34. #include <stdio.h>
  35. #define COMP_MAGIC 0x43454447
  36. Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) {
  37. ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open.");
  38. ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
  39. pos = 0;
  40. eofed = false;
  41. if (p_mode == MODE_WRITE_AES256) {
  42. data.clear();
  43. writing = true;
  44. file = p_base;
  45. mode = p_mode;
  46. key = p_key;
  47. } else if (p_mode == MODE_READ) {
  48. writing = false;
  49. key = p_key;
  50. uint32_t magic = p_base->get_32();
  51. ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED);
  52. mode = Mode(p_base->get_32());
  53. ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT);
  54. ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT);
  55. unsigned char md5d[16];
  56. p_base->get_buffer(md5d, 16);
  57. length = p_base->get_64();
  58. base = p_base->get_position();
  59. ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT);
  60. uint64_t ds = length;
  61. if (ds % 16) {
  62. ds += 16 - (ds % 16);
  63. }
  64. data.resize(ds);
  65. uint64_t blen = p_base->get_buffer(data.ptrw(), ds);
  66. ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
  67. CryptoCore::AESContext ctx;
  68. ctx.set_decode_key(key.ptrw(), 256);
  69. for (uint64_t i = 0; i < ds; i += 16) {
  70. ctx.decrypt_ecb(&data.write[i], &data.write[i]);
  71. }
  72. data.resize(length);
  73. unsigned char hash[16];
  74. ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG);
  75. ERR_FAIL_COND_V_MSG(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT, "The MD5 sum of the decrypted file does not match the expected value. It could be that the file is corrupt, or that the provided decryption key is invalid.");
  76. file = p_base;
  77. }
  78. return OK;
  79. }
  80. Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode) {
  81. String cs = p_key.md5_text();
  82. ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER);
  83. Vector<uint8_t> key;
  84. key.resize(32);
  85. for (int i = 0; i < 32; i++) {
  86. key.write[i] = cs[i];
  87. }
  88. return open_and_parse(p_base, key, p_mode);
  89. }
  90. Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) {
  91. return OK;
  92. }
  93. void FileAccessEncrypted::close() {
  94. if (!file) {
  95. return;
  96. }
  97. if (writing) {
  98. Vector<uint8_t> compressed;
  99. uint64_t len = data.size();
  100. if (len % 16) {
  101. len += 16 - (len % 16);
  102. }
  103. unsigned char hash[16];
  104. ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
  105. compressed.resize(len);
  106. memset(compressed.ptrw(), 0, len);
  107. for (int i = 0; i < data.size(); i++) {
  108. compressed.write[i] = data[i];
  109. }
  110. CryptoCore::AESContext ctx;
  111. ctx.set_encode_key(key.ptrw(), 256);
  112. for (uint64_t i = 0; i < len; i += 16) {
  113. ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]);
  114. }
  115. file->store_32(COMP_MAGIC);
  116. file->store_32(mode);
  117. file->store_buffer(hash, 16);
  118. file->store_64(data.size());
  119. file->store_buffer(compressed.ptr(), compressed.size());
  120. file->close();
  121. memdelete(file);
  122. file = nullptr;
  123. data.clear();
  124. } else {
  125. file->close();
  126. memdelete(file);
  127. data.clear();
  128. file = nullptr;
  129. }
  130. }
  131. bool FileAccessEncrypted::is_open() const {
  132. return file != nullptr;
  133. }
  134. String FileAccessEncrypted::get_path() const {
  135. if (file) {
  136. return file->get_path();
  137. } else {
  138. return "";
  139. }
  140. }
  141. String FileAccessEncrypted::get_path_absolute() const {
  142. if (file) {
  143. return file->get_path_absolute();
  144. } else {
  145. return "";
  146. }
  147. }
  148. void FileAccessEncrypted::seek(uint64_t p_position) {
  149. if (p_position > get_len()) {
  150. p_position = get_len();
  151. }
  152. pos = p_position;
  153. eofed = false;
  154. }
  155. void FileAccessEncrypted::seek_end(int64_t p_position) {
  156. seek(get_len() + p_position);
  157. }
  158. uint64_t FileAccessEncrypted::get_position() const {
  159. return pos;
  160. }
  161. uint64_t FileAccessEncrypted::get_len() const {
  162. return data.size();
  163. }
  164. bool FileAccessEncrypted::eof_reached() const {
  165. return eofed;
  166. }
  167. uint8_t FileAccessEncrypted::get_8() const {
  168. ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
  169. if (pos >= get_len()) {
  170. eofed = true;
  171. return 0;
  172. }
  173. uint8_t b = data[pos];
  174. pos++;
  175. return b;
  176. }
  177. uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  178. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  179. ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
  180. uint64_t to_copy = MIN(p_length, get_len() - pos);
  181. for (uint64_t i = 0; i < to_copy; i++) {
  182. p_dst[i] = data[pos++];
  183. }
  184. if (to_copy < p_length) {
  185. eofed = true;
  186. }
  187. return to_copy;
  188. }
  189. Error FileAccessEncrypted::get_error() const {
  190. return eofed ? ERR_FILE_EOF : OK;
  191. }
  192. void FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  193. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  194. ERR_FAIL_COND(!p_src && p_length > 0);
  195. if (pos < get_len()) {
  196. for (uint64_t i = 0; i < p_length; i++) {
  197. store_8(p_src[i]);
  198. }
  199. } else if (pos == get_len()) {
  200. data.resize(pos + p_length);
  201. for (uint64_t i = 0; i < p_length; i++) {
  202. data.write[pos + i] = p_src[i];
  203. }
  204. pos += p_length;
  205. }
  206. }
  207. void FileAccessEncrypted::flush() {
  208. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  209. // encrypted files keep data in memory till close()
  210. }
  211. void FileAccessEncrypted::store_8(uint8_t p_dest) {
  212. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  213. if (pos < get_len()) {
  214. data.write[pos] = p_dest;
  215. pos++;
  216. } else if (pos == get_len()) {
  217. data.push_back(p_dest);
  218. pos++;
  219. }
  220. }
  221. bool FileAccessEncrypted::file_exists(const String &p_name) {
  222. FileAccess *fa = FileAccess::open(p_name, FileAccess::READ);
  223. if (!fa) {
  224. return false;
  225. }
  226. memdelete(fa);
  227. return true;
  228. }
  229. uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) {
  230. return 0;
  231. }
  232. uint32_t FileAccessEncrypted::_get_unix_permissions(const String &p_file) {
  233. return 0;
  234. }
  235. Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  236. ERR_PRINT("Setting UNIX permissions on encrypted files is not implemented yet.");
  237. return ERR_UNAVAILABLE;
  238. }
  239. FileAccessEncrypted::FileAccessEncrypted() {
  240. file = nullptr;
  241. base = 0;
  242. length = 0;
  243. pos = 0;
  244. eofed = false;
  245. mode = MODE_MAX;
  246. writing = false;
  247. }
  248. FileAccessEncrypted::~FileAccessEncrypted() {
  249. if (file) {
  250. close();
  251. }
  252. }