file_access_encrypted.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*************************************************************************/
  2. /* file_access_encrypted.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "file_access_encrypted.h"
  30. #include "aes256.h"
  31. #include "md5.h"
  32. #include "os/copymem.h"
  33. #include "print_string.h"
  34. #define COMP_MAGIC 0x43454447
  35. #include "core/variant.h"
  36. #include <stdio.h>
  37. Error FileAccessEncrypted::open_and_parse(FileAccess *p_base,const Vector<uint8_t>& p_key,Mode p_mode) {
  38. //print_line("open and parse!");
  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. print_line("MAGIC: "+itos(magic));
  54. ERR_FAIL_COND_V(magic!=COMP_MAGIC,ERR_FILE_UNRECOGNIZED);
  55. mode=Mode(p_base->get_32());
  56. ERR_FAIL_INDEX_V(mode,MODE_MAX,ERR_FILE_CORRUPT);
  57. ERR_FAIL_COND_V(mode==0,ERR_FILE_CORRUPT);
  58. print_line("MODE: "+itos(mode));
  59. unsigned char md5d[16];
  60. p_base->get_buffer(md5d,16);
  61. length=p_base->get_64();
  62. base=p_base->get_pos();
  63. ERR_FAIL_COND_V(p_base->get_len() < base+length, ERR_FILE_CORRUPT );
  64. int ds = length;
  65. if (ds % 16) {
  66. ds+=16-(ds % 16);
  67. }
  68. data.resize(ds);
  69. int blen = p_base->get_buffer(data.ptr(),ds);
  70. ERR_FAIL_COND_V(blen!=ds,ERR_FILE_CORRUPT);
  71. aes256_context ctx;
  72. aes256_init(&ctx,key.ptr());
  73. for(size_t i=0;i<ds;i+=16) {
  74. aes256_decrypt_ecb(&ctx,&data[i]);
  75. }
  76. aes256_done(&ctx);
  77. data.resize(length);
  78. MD5_CTX md5;
  79. MD5Init(&md5);
  80. MD5Update(&md5,data.ptr(),data.size());
  81. MD5Final(&md5);
  82. ERR_FAIL_COND_V(String::md5(md5.digest)!=String::md5(md5d),ERR_FILE_CORRUPT) ;
  83. file=p_base;
  84. }
  85. return OK;
  86. }
  87. Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base,const String& p_key,Mode p_mode){
  88. String cs = p_key.md5_text();
  89. ERR_FAIL_COND_V(cs.length()!=32,ERR_INVALID_PARAMETER);
  90. Vector<uint8_t> key;
  91. key.resize(32);
  92. for(int i=0;i<32;i++) {
  93. key[i]=cs[i];
  94. }
  95. return open_and_parse(p_base,key,p_mode);
  96. }
  97. Error FileAccessEncrypted::_open(const String& p_path, int p_mode_flags) {
  98. return OK;
  99. }
  100. void FileAccessEncrypted::close() {
  101. if (!file)
  102. return;
  103. if (writing) {
  104. Vector<uint8_t> compressed;
  105. size_t len = data.size();
  106. if (len % 16) {
  107. len+=16-(len % 16);
  108. }
  109. MD5_CTX md5;
  110. MD5Init(&md5);
  111. MD5Update(&md5,data.ptr(),data.size());
  112. MD5Final(&md5);
  113. compressed.resize(len);
  114. zeromem( compressed.ptr(), len );
  115. for(int i=0;i<data.size();i++) {
  116. compressed[i]=data[i];
  117. }
  118. aes256_context ctx;
  119. aes256_init(&ctx,key.ptr());
  120. for(size_t i=0;i<len;i+=16) {
  121. aes256_encrypt_ecb(&ctx,&compressed[i]);
  122. }
  123. aes256_done(&ctx);
  124. file->store_32(COMP_MAGIC);
  125. file->store_32(mode);
  126. file->store_buffer(md5.digest,16);
  127. file->store_64(data.size());
  128. file->store_buffer(compressed.ptr(),compressed.size());
  129. file->close();
  130. memdelete(file);
  131. file=NULL;
  132. data.clear();
  133. } else {
  134. file->close();
  135. memdelete(file);
  136. data.clear();
  137. file=NULL;
  138. }
  139. }
  140. bool FileAccessEncrypted::is_open() const{
  141. return file!=NULL;
  142. }
  143. void FileAccessEncrypted::seek(size_t p_position){
  144. if (p_position > (size_t)data.size())
  145. p_position=data.size();
  146. pos=p_position;
  147. eofed=false;
  148. }
  149. void FileAccessEncrypted::seek_end(int64_t p_position){
  150. seek( data.size() + p_position );
  151. }
  152. size_t FileAccessEncrypted::get_pos() const{
  153. return pos;
  154. }
  155. size_t FileAccessEncrypted::get_len() const{
  156. return data.size();
  157. }
  158. bool FileAccessEncrypted::eof_reached() const{
  159. return eofed;
  160. }
  161. uint8_t FileAccessEncrypted::get_8() const{
  162. ERR_FAIL_COND_V(writing,0);
  163. if (pos>=data.size()) {
  164. eofed=true;
  165. return 0;
  166. }
  167. uint8_t b = data[pos];
  168. pos++;
  169. return b;
  170. }
  171. int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const{
  172. ERR_FAIL_COND_V(writing,0);
  173. int to_copy=MIN(p_length,data.size()-pos);
  174. for(int i=0;i<to_copy;i++) {
  175. p_dst[i]=data[pos++];
  176. }
  177. if (to_copy<p_length) {
  178. eofed=true;
  179. }
  180. return to_copy;
  181. }
  182. Error FileAccessEncrypted::get_error() const{
  183. return eofed?ERR_FILE_EOF:OK;
  184. }
  185. void FileAccessEncrypted::store_buffer(const uint8_t *p_src,int p_length) {
  186. ERR_FAIL_COND(!writing);
  187. if (pos<data.size()) {
  188. for(int i=0;i<p_length;i++) {
  189. store_8(p_src[i]);
  190. }
  191. } else if (pos==data.size()) {
  192. data.resize(pos+p_length);
  193. for(int i=0;i<p_length;i++) {
  194. data[pos+i]=p_src[i];
  195. }
  196. pos+=p_length;
  197. }
  198. }
  199. void FileAccessEncrypted::store_8(uint8_t p_dest){
  200. ERR_FAIL_COND(!writing);
  201. if (pos<data.size()) {
  202. data[pos]=p_dest;
  203. pos++;
  204. } else if (pos==data.size()){
  205. data.push_back(p_dest);
  206. pos++;
  207. }
  208. }
  209. bool FileAccessEncrypted::file_exists(const String& p_name){
  210. FileAccess *fa = FileAccess::open(p_name,FileAccess::READ);
  211. if (!fa)
  212. return false;
  213. memdelete(fa);
  214. return true;
  215. }
  216. uint64_t FileAccessEncrypted::_get_modified_time(const String& p_file){
  217. return 0;
  218. }
  219. FileAccessEncrypted::FileAccessEncrypted() {
  220. file=NULL;
  221. pos=0;
  222. eofed=false;
  223. mode=MODE_MAX;
  224. writing=false;
  225. }
  226. FileAccessEncrypted::~FileAccessEncrypted() {
  227. if (file)
  228. close();
  229. }