file_access_encrypted.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/string/print_string.h"
  33. #include "core/variant/variant.h"
  34. #include <stdio.h>
  35. Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic) {
  36. 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.");
  37. ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
  38. pos = 0;
  39. eofed = false;
  40. use_magic = p_with_magic;
  41. if (p_mode == MODE_WRITE_AES256) {
  42. data.clear();
  43. writing = true;
  44. file = p_base;
  45. key = p_key;
  46. } else if (p_mode == MODE_READ) {
  47. writing = false;
  48. key = p_key;
  49. if (use_magic) {
  50. uint32_t magic = p_base->get_32();
  51. ERR_FAIL_COND_V(magic != ENCRYPTED_HEADER_MAGIC, ERR_FILE_UNRECOGNIZED);
  52. }
  53. unsigned char md5d[16];
  54. p_base->get_buffer(md5d, 16);
  55. length = p_base->get_64();
  56. unsigned char iv[16];
  57. for (int i = 0; i < 16; i++) {
  58. iv[i] = p_base->get_8();
  59. }
  60. base = p_base->get_position();
  61. ERR_FAIL_COND_V(p_base->get_length() < base + length, ERR_FILE_CORRUPT);
  62. uint64_t ds = length;
  63. if (ds % 16) {
  64. ds += 16 - (ds % 16);
  65. }
  66. data.resize(ds);
  67. uint64_t blen = p_base->get_buffer(data.ptrw(), ds);
  68. ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
  69. {
  70. CryptoCore::AESContext ctx;
  71. ctx.set_encode_key(key.ptrw(), 256); // Due to the nature of CFB, same key schedule is used for both encryption and decryption!
  72. ctx.decrypt_cfb(ds, iv, data.ptrw(), data.ptrw());
  73. }
  74. data.resize(length);
  75. unsigned char hash[16];
  76. ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG);
  77. 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.");
  78. file = p_base;
  79. }
  80. return OK;
  81. }
  82. Error FileAccessEncrypted::open_and_parse_password(Ref<FileAccess> p_base, const String &p_key, Mode p_mode) {
  83. String cs = p_key.md5_text();
  84. ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER);
  85. Vector<uint8_t> key_md5;
  86. key_md5.resize(32);
  87. for (int i = 0; i < 32; i++) {
  88. key_md5.write[i] = cs[i];
  89. }
  90. return open_and_parse(p_base, key_md5, p_mode);
  91. }
  92. Error FileAccessEncrypted::open_internal(const String &p_path, int p_mode_flags) {
  93. return OK;
  94. }
  95. void FileAccessEncrypted::_close() {
  96. if (file.is_null()) {
  97. return;
  98. }
  99. if (writing) {
  100. Vector<uint8_t> compressed;
  101. uint64_t len = data.size();
  102. if (len % 16) {
  103. len += 16 - (len % 16);
  104. }
  105. unsigned char hash[16];
  106. ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
  107. compressed.resize(len);
  108. memset(compressed.ptrw(), 0, len);
  109. for (int i = 0; i < data.size(); i++) {
  110. compressed.write[i] = data[i];
  111. }
  112. CryptoCore::AESContext ctx;
  113. ctx.set_encode_key(key.ptrw(), 256);
  114. if (use_magic) {
  115. file->store_32(ENCRYPTED_HEADER_MAGIC);
  116. }
  117. file->store_buffer(hash, 16);
  118. file->store_64(data.size());
  119. unsigned char iv[16];
  120. for (int i = 0; i < 16; i++) {
  121. iv[i] = Math::rand() % 256;
  122. file->store_8(iv[i]);
  123. }
  124. ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw());
  125. file->store_buffer(compressed.ptr(), compressed.size());
  126. data.clear();
  127. }
  128. file.unref();
  129. }
  130. bool FileAccessEncrypted::is_open() const {
  131. return file != nullptr;
  132. }
  133. String FileAccessEncrypted::get_path() const {
  134. if (file.is_valid()) {
  135. return file->get_path();
  136. } else {
  137. return "";
  138. }
  139. }
  140. String FileAccessEncrypted::get_path_absolute() const {
  141. if (file.is_valid()) {
  142. return file->get_path_absolute();
  143. } else {
  144. return "";
  145. }
  146. }
  147. void FileAccessEncrypted::seek(uint64_t p_position) {
  148. if (p_position > get_length()) {
  149. p_position = get_length();
  150. }
  151. pos = p_position;
  152. eofed = false;
  153. }
  154. void FileAccessEncrypted::seek_end(int64_t p_position) {
  155. seek(get_length() + p_position);
  156. }
  157. uint64_t FileAccessEncrypted::get_position() const {
  158. return pos;
  159. }
  160. uint64_t FileAccessEncrypted::get_length() const {
  161. return data.size();
  162. }
  163. bool FileAccessEncrypted::eof_reached() const {
  164. return eofed;
  165. }
  166. uint8_t FileAccessEncrypted::get_8() const {
  167. ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
  168. if (pos >= get_length()) {
  169. eofed = true;
  170. return 0;
  171. }
  172. uint8_t b = data[pos];
  173. pos++;
  174. return b;
  175. }
  176. uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  177. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  178. ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
  179. uint64_t to_copy = MIN(p_length, get_length() - pos);
  180. for (uint64_t i = 0; i < to_copy; i++) {
  181. p_dst[i] = data[pos++];
  182. }
  183. if (to_copy < p_length) {
  184. eofed = true;
  185. }
  186. return to_copy;
  187. }
  188. Error FileAccessEncrypted::get_error() const {
  189. return eofed ? ERR_FILE_EOF : OK;
  190. }
  191. void FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) {
  192. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  193. ERR_FAIL_COND(!p_src && p_length > 0);
  194. if (pos < get_length()) {
  195. for (uint64_t i = 0; i < p_length; i++) {
  196. store_8(p_src[i]);
  197. }
  198. } else if (pos == get_length()) {
  199. data.resize(pos + p_length);
  200. for (uint64_t i = 0; i < p_length; i++) {
  201. data.write[pos + i] = p_src[i];
  202. }
  203. pos += p_length;
  204. }
  205. }
  206. void FileAccessEncrypted::flush() {
  207. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  208. // encrypted files keep data in memory till close()
  209. }
  210. void FileAccessEncrypted::store_8(uint8_t p_dest) {
  211. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  212. if (pos < get_length()) {
  213. data.write[pos] = p_dest;
  214. pos++;
  215. } else if (pos == get_length()) {
  216. data.push_back(p_dest);
  217. pos++;
  218. }
  219. }
  220. bool FileAccessEncrypted::file_exists(const String &p_name) {
  221. Ref<FileAccess> fa = FileAccess::open(p_name, FileAccess::READ);
  222. if (fa.is_null()) {
  223. return false;
  224. }
  225. return true;
  226. }
  227. uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) {
  228. return 0;
  229. }
  230. BitField<FileAccess::UnixPermissionFlags> FileAccessEncrypted::_get_unix_permissions(const String &p_file) {
  231. if (file.is_valid()) {
  232. return file->_get_unix_permissions(p_file);
  233. }
  234. return 0;
  235. }
  236. Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, BitField<FileAccess::UnixPermissionFlags> p_permissions) {
  237. if (file.is_valid()) {
  238. return file->_set_unix_permissions(p_file, p_permissions);
  239. }
  240. return FAILED;
  241. }
  242. bool FileAccessEncrypted::_get_hidden_attribute(const String &p_file) {
  243. if (file.is_valid()) {
  244. return file->_get_hidden_attribute(p_file);
  245. }
  246. return false;
  247. }
  248. Error FileAccessEncrypted::_set_hidden_attribute(const String &p_file, bool p_hidden) {
  249. if (file.is_valid()) {
  250. return file->_set_hidden_attribute(p_file, p_hidden);
  251. }
  252. return FAILED;
  253. }
  254. bool FileAccessEncrypted::_get_read_only_attribute(const String &p_file) {
  255. if (file.is_valid()) {
  256. return file->_get_read_only_attribute(p_file);
  257. }
  258. return false;
  259. }
  260. Error FileAccessEncrypted::_set_read_only_attribute(const String &p_file, bool p_ro) {
  261. if (file.is_valid()) {
  262. return file->_set_read_only_attribute(p_file, p_ro);
  263. }
  264. return FAILED;
  265. }
  266. void FileAccessEncrypted::close() {
  267. _close();
  268. }
  269. FileAccessEncrypted::~FileAccessEncrypted() {
  270. _close();
  271. }