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