file_access_encrypted.cpp 9.0 KB

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