file_access_encrypted.cpp 8.5 KB

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