file_access_encrypted.cpp 9.1 KB

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