2
0

file_access_encrypted.cpp 7.3 KB

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