2
0

file_access_encrypted.cpp 9.8 KB

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