file_access_encrypted.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/os/copymem.h"
  32. #include "core/print_string.h"
  33. #include "core/variant.h"
  34. #include "thirdparty/misc/aes256.h"
  35. #include "thirdparty/misc/md5.h"
  36. #include <stdio.h>
  37. #define COMP_MAGIC 0x43454447
  38. Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) {
  39. ERR_FAIL_COND_V(file != NULL, ERR_ALREADY_IN_USE);
  40. ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
  41. pos = 0;
  42. eofed = false;
  43. if (p_mode == MODE_WRITE_AES256) {
  44. data.clear();
  45. writing = true;
  46. file = p_base;
  47. mode = p_mode;
  48. key = p_key;
  49. } else if (p_mode == MODE_READ) {
  50. writing = false;
  51. key = p_key;
  52. uint32_t magic = p_base->get_32();
  53. ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED);
  54. mode = Mode(p_base->get_32());
  55. ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT);
  56. ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT);
  57. unsigned char md5d[16];
  58. p_base->get_buffer(md5d, 16);
  59. length = p_base->get_64();
  60. base = p_base->get_position();
  61. ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT);
  62. uint32_t ds = length;
  63. if (ds % 16) {
  64. ds += 16 - (ds % 16);
  65. }
  66. data.resize(ds);
  67. uint32_t blen = p_base->get_buffer(data.ptrw(), ds);
  68. ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
  69. aes256_context ctx;
  70. aes256_init(&ctx, key.ptrw());
  71. for (size_t i = 0; i < ds; i += 16) {
  72. aes256_decrypt_ecb(&ctx, &data.write[i]);
  73. }
  74. aes256_done(&ctx);
  75. data.resize(length);
  76. MD5_CTX md5;
  77. MD5Init(&md5);
  78. MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
  79. MD5Final(&md5);
  80. ERR_FAIL_COND_V(String::md5(md5.digest) != String::md5(md5d), ERR_FILE_CORRUPT);
  81. file = p_base;
  82. }
  83. return OK;
  84. }
  85. Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode) {
  86. String cs = p_key.md5_text();
  87. ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER);
  88. Vector<uint8_t> key;
  89. key.resize(32);
  90. for (int i = 0; i < 32; i++) {
  91. key.write[i] = cs[i];
  92. }
  93. return open_and_parse(p_base, key, p_mode);
  94. }
  95. Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) {
  96. return OK;
  97. }
  98. void FileAccessEncrypted::close() {
  99. if (!file)
  100. return;
  101. if (writing) {
  102. Vector<uint8_t> compressed;
  103. size_t len = data.size();
  104. if (len % 16) {
  105. len += 16 - (len % 16);
  106. }
  107. MD5_CTX md5;
  108. MD5Init(&md5);
  109. MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
  110. MD5Final(&md5);
  111. compressed.resize(len);
  112. zeromem(compressed.ptrw(), len);
  113. for (int i = 0; i < data.size(); i++) {
  114. compressed.write[i] = data[i];
  115. }
  116. aes256_context ctx;
  117. aes256_init(&ctx, key.ptrw());
  118. for (size_t i = 0; i < len; i += 16) {
  119. aes256_encrypt_ecb(&ctx, &compressed.write[i]);
  120. }
  121. aes256_done(&ctx);
  122. file->store_32(COMP_MAGIC);
  123. file->store_32(mode);
  124. file->store_buffer(md5.digest, 16);
  125. file->store_64(data.size());
  126. file->store_buffer(compressed.ptr(), compressed.size());
  127. file->close();
  128. memdelete(file);
  129. file = NULL;
  130. data.clear();
  131. } else {
  132. file->close();
  133. memdelete(file);
  134. data.clear();
  135. file = NULL;
  136. }
  137. }
  138. bool FileAccessEncrypted::is_open() const {
  139. return file != NULL;
  140. }
  141. void FileAccessEncrypted::seek(size_t p_position) {
  142. if (p_position > (size_t)data.size())
  143. p_position = data.size();
  144. pos = p_position;
  145. eofed = false;
  146. }
  147. void FileAccessEncrypted::seek_end(int64_t p_position) {
  148. seek(data.size() + p_position);
  149. }
  150. size_t FileAccessEncrypted::get_position() const {
  151. return pos;
  152. }
  153. size_t FileAccessEncrypted::get_len() const {
  154. return data.size();
  155. }
  156. bool FileAccessEncrypted::eof_reached() const {
  157. return eofed;
  158. }
  159. uint8_t FileAccessEncrypted::get_8() const {
  160. ERR_FAIL_COND_V(writing, 0);
  161. if (pos >= data.size()) {
  162. eofed = true;
  163. return 0;
  164. }
  165. uint8_t b = data[pos];
  166. pos++;
  167. return b;
  168. }
  169. int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const {
  170. ERR_FAIL_COND_V(writing, 0);
  171. int to_copy = MIN(p_length, data.size() - pos);
  172. for (int i = 0; i < to_copy; i++) {
  173. p_dst[i] = data[pos++];
  174. }
  175. if (to_copy < p_length) {
  176. eofed = true;
  177. }
  178. return to_copy;
  179. }
  180. Error FileAccessEncrypted::get_error() const {
  181. return eofed ? ERR_FILE_EOF : OK;
  182. }
  183. void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) {
  184. ERR_FAIL_COND(!writing);
  185. if (pos < data.size()) {
  186. for (int i = 0; i < p_length; i++) {
  187. store_8(p_src[i]);
  188. }
  189. } else if (pos == data.size()) {
  190. data.resize(pos + p_length);
  191. for (int i = 0; i < p_length; i++) {
  192. data.write[pos + i] = p_src[i];
  193. }
  194. pos += p_length;
  195. }
  196. }
  197. void FileAccessEncrypted::flush() {
  198. ERR_FAIL_COND(!writing);
  199. // encrypted files keep data in memory till close()
  200. }
  201. void FileAccessEncrypted::store_8(uint8_t p_dest) {
  202. ERR_FAIL_COND(!writing);
  203. if (pos < data.size()) {
  204. data.write[pos] = p_dest;
  205. pos++;
  206. } else if (pos == data.size()) {
  207. data.push_back(p_dest);
  208. pos++;
  209. }
  210. }
  211. bool FileAccessEncrypted::file_exists(const String &p_name) {
  212. FileAccess *fa = FileAccess::open(p_name, FileAccess::READ);
  213. if (!fa)
  214. return false;
  215. memdelete(fa);
  216. return true;
  217. }
  218. uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) {
  219. return 0;
  220. }
  221. uint32_t FileAccessEncrypted::_get_unix_permissions(const String &p_file) {
  222. return 0;
  223. }
  224. Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t p_permissions) {
  225. return FAILED;
  226. }
  227. FileAccessEncrypted::FileAccessEncrypted() {
  228. file = NULL;
  229. pos = 0;
  230. eofed = false;
  231. mode = MODE_MAX;
  232. writing = false;
  233. }
  234. FileAccessEncrypted::~FileAccessEncrypted() {
  235. if (file)
  236. close();
  237. }