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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/string/print_string.h"
  33. #include "core/variant/variant.h"
  34. #include <stdio.h>
  35. Error FileAccessEncrypted::open_and_parse(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_len() < 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(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;
  86. key.resize(32);
  87. for (int i = 0; i < 32; i++) {
  88. key.write[i] = cs[i];
  89. }
  90. return open_and_parse(p_base, key, p_mode);
  91. }
  92. Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) {
  93. return OK;
  94. }
  95. void FileAccessEncrypted::close() {
  96. if (!file) {
  97. return;
  98. }
  99. _release();
  100. file->close();
  101. memdelete(file);
  102. file = nullptr;
  103. }
  104. void FileAccessEncrypted::release() {
  105. if (!file) {
  106. return;
  107. }
  108. _release();
  109. file = nullptr;
  110. }
  111. void FileAccessEncrypted::_release() {
  112. if (writing) {
  113. Vector<uint8_t> compressed;
  114. uint64_t len = data.size();
  115. if (len % 16) {
  116. len += 16 - (len % 16);
  117. }
  118. unsigned char hash[16];
  119. ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
  120. compressed.resize(len);
  121. memset(compressed.ptrw(), 0, len);
  122. for (int i = 0; i < data.size(); i++) {
  123. compressed.write[i] = data[i];
  124. }
  125. CryptoCore::AESContext ctx;
  126. ctx.set_encode_key(key.ptrw(), 256);
  127. if (use_magic) {
  128. file->store_32(ENCRYPTED_HEADER_MAGIC);
  129. }
  130. file->store_buffer(hash, 16);
  131. file->store_64(data.size());
  132. unsigned char iv[16];
  133. for (int i = 0; i < 16; i++) {
  134. iv[i] = Math::rand() % 256;
  135. file->store_8(iv[i]);
  136. }
  137. ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw());
  138. file->store_buffer(compressed.ptr(), compressed.size());
  139. data.clear();
  140. }
  141. }
  142. bool FileAccessEncrypted::is_open() const {
  143. return file != nullptr;
  144. }
  145. String FileAccessEncrypted::get_path() const {
  146. if (file) {
  147. return file->get_path();
  148. } else {
  149. return "";
  150. }
  151. }
  152. String FileAccessEncrypted::get_path_absolute() const {
  153. if (file) {
  154. return file->get_path_absolute();
  155. } else {
  156. return "";
  157. }
  158. }
  159. void FileAccessEncrypted::seek(uint64_t p_position) {
  160. if (p_position > get_len()) {
  161. p_position = get_len();
  162. }
  163. pos = p_position;
  164. eofed = false;
  165. }
  166. void FileAccessEncrypted::seek_end(int64_t p_position) {
  167. seek(get_len() + p_position);
  168. }
  169. uint64_t FileAccessEncrypted::get_position() const {
  170. return pos;
  171. }
  172. uint64_t FileAccessEncrypted::get_len() const {
  173. return data.size();
  174. }
  175. bool FileAccessEncrypted::eof_reached() const {
  176. return eofed;
  177. }
  178. uint8_t FileAccessEncrypted::get_8() const {
  179. ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode.");
  180. if (pos >= get_len()) {
  181. eofed = true;
  182. return 0;
  183. }
  184. uint8_t b = data[pos];
  185. pos++;
  186. return b;
  187. }
  188. uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const {
  189. ERR_FAIL_COND_V(!p_dst && p_length > 0, -1);
  190. ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode.");
  191. uint64_t to_copy = MIN(p_length, get_len() - pos);
  192. for (uint64_t 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, uint64_t p_length) {
  204. ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode.");
  205. if (pos < get_len()) {
  206. for (uint64_t i = 0; i < p_length; i++) {
  207. store_8(p_src[i]);
  208. }
  209. } else if (pos == get_len()) {
  210. data.resize(pos + p_length);
  211. for (uint64_t 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 write 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 write mode.");
  223. if (pos < get_len()) {
  224. data.write[pos] = p_dest;
  225. pos++;
  226. } else if (pos == get_len()) {
  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. }