2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* file_access_encrypted.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-08 00:11:42 +02:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-04-15 03:16:13 +02:00
|
|
|
#include "file_access_encrypted.h"
|
2017-04-28 18:29:15 +02:00
|
|
|
|
|
|
|
#include "core/variant.h"
|
2014-04-15 03:16:13 +02:00
|
|
|
#include "os/copymem.h"
|
2014-04-29 02:56:43 +02:00
|
|
|
#include "print_string.h"
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-04-28 18:29:15 +02:00
|
|
|
#include "thirdparty/misc/aes256.h"
|
|
|
|
#include "thirdparty/misc/md5.h"
|
|
|
|
|
2015-06-30 16:28:43 +02:00
|
|
|
#include <stdio.h>
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-04-28 18:29:15 +02:00
|
|
|
#define COMP_MAGIC 0x43454447
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2015-06-30 16:28:43 +02:00
|
|
|
//print_line("open and parse!");
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(file != NULL, ERR_ALREADY_IN_USE);
|
|
|
|
ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER);
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = 0;
|
|
|
|
eofed = false;
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (p_mode == MODE_WRITE_AES256) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
data.clear();
|
2017-03-05 16:44:50 +01:00
|
|
|
writing = true;
|
|
|
|
file = p_base;
|
|
|
|
mode = p_mode;
|
|
|
|
key = p_key;
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (p_mode == MODE_READ) {
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
writing = false;
|
|
|
|
key = p_key;
|
2014-04-29 02:56:43 +02:00
|
|
|
uint32_t magic = p_base->get_32();
|
2017-03-05 16:44:50 +01:00
|
|
|
print_line("MAGIC: " + itos(magic));
|
|
|
|
ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED);
|
|
|
|
mode = Mode(p_base->get_32());
|
|
|
|
ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT);
|
|
|
|
ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT);
|
|
|
|
print_line("MODE: " + itos(mode));
|
2014-04-29 02:56:43 +02:00
|
|
|
unsigned char md5d[16];
|
2017-03-05 16:44:50 +01:00
|
|
|
p_base->get_buffer(md5d, 16);
|
|
|
|
length = p_base->get_64();
|
|
|
|
base = p_base->get_pos();
|
|
|
|
ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT);
|
2014-04-29 02:56:43 +02:00
|
|
|
int ds = length;
|
|
|
|
if (ds % 16) {
|
2017-03-05 16:44:50 +01:00
|
|
|
ds += 16 - (ds % 16);
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data.resize(ds);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int blen = p_base->get_buffer(data.ptr(), ds);
|
|
|
|
ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
|
2014-04-29 02:56:43 +02:00
|
|
|
|
|
|
|
aes256_context ctx;
|
2017-03-05 16:44:50 +01:00
|
|
|
aes256_init(&ctx, key.ptr());
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (size_t i = 0; i < ds; i += 16) {
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
aes256_decrypt_ecb(&ctx, &data[i]);
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aes256_done(&ctx);
|
|
|
|
|
|
|
|
data.resize(length);
|
|
|
|
|
|
|
|
MD5_CTX md5;
|
|
|
|
MD5Init(&md5);
|
2017-03-05 16:44:50 +01:00
|
|
|
MD5Update(&md5, data.ptr(), data.size());
|
2014-04-29 02:56:43 +02:00
|
|
|
MD5Final(&md5);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(String::md5(md5.digest) != String::md5(md5d), ERR_FILE_CORRUPT);
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file = p_base;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
String cs = p_key.md5_text();
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER);
|
2014-04-15 03:16:13 +02:00
|
|
|
Vector<uint8_t> key;
|
|
|
|
key.resize(32);
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < 32; i++) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
key[i] = cs[i];
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return open_and_parse(p_base, key, p_mode);
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
void FileAccessEncrypted::close() {
|
|
|
|
|
|
|
|
if (!file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (writing) {
|
|
|
|
|
|
|
|
Vector<uint8_t> compressed;
|
|
|
|
size_t len = data.size();
|
|
|
|
if (len % 16) {
|
2017-03-05 16:44:50 +01:00
|
|
|
len += 16 - (len % 16);
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
MD5_CTX md5;
|
|
|
|
MD5Init(&md5);
|
2017-03-05 16:44:50 +01:00
|
|
|
MD5Update(&md5, data.ptr(), data.size());
|
2014-04-29 02:56:43 +02:00
|
|
|
MD5Final(&md5);
|
|
|
|
|
2014-04-15 03:16:13 +02:00
|
|
|
compressed.resize(len);
|
2017-03-05 16:44:50 +01:00
|
|
|
zeromem(compressed.ptr(), len);
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
compressed[i] = data[i];
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aes256_context ctx;
|
2017-03-05 16:44:50 +01:00
|
|
|
aes256_init(&ctx, key.ptr());
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (size_t i = 0; i < len; i += 16) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
aes256_encrypt_ecb(&ctx, &compressed[i]);
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
aes256_done(&ctx);
|
|
|
|
|
|
|
|
file->store_32(COMP_MAGIC);
|
|
|
|
file->store_32(mode);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file->store_buffer(md5.digest, 16);
|
2014-04-15 03:16:13 +02:00
|
|
|
file->store_64(data.size());
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file->store_buffer(compressed.ptr(), compressed.size());
|
2014-04-15 03:16:13 +02:00
|
|
|
file->close();
|
|
|
|
memdelete(file);
|
2017-03-05 16:44:50 +01:00
|
|
|
file = NULL;
|
2014-04-29 02:56:43 +02:00
|
|
|
data.clear();
|
|
|
|
|
|
|
|
} else {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
file->close();
|
|
|
|
memdelete(file);
|
|
|
|
data.clear();
|
2017-03-05 16:44:50 +01:00
|
|
|
file = NULL;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessEncrypted::is_open() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return file != NULL;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessEncrypted::seek(size_t p_position) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
if (p_position > (size_t)data.size())
|
2017-03-05 16:44:50 +01:00
|
|
|
p_position = data.size();
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
pos = p_position;
|
|
|
|
eofed = false;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessEncrypted::seek_end(int64_t p_position) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
seek(data.size() + p_position);
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
size_t FileAccessEncrypted::get_pos() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
return pos;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
size_t FileAccessEncrypted::get_len() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
return data.size();
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessEncrypted::eof_reached() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
return eofed;
|
|
|
|
}
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint8_t FileAccessEncrypted::get_8() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(writing, 0);
|
|
|
|
if (pos >= data.size()) {
|
|
|
|
eofed = true;
|
2014-04-29 02:56:43 +02:00
|
|
|
return 0;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2014-04-29 02:56:43 +02:00
|
|
|
uint8_t b = data[pos];
|
|
|
|
pos++;
|
|
|
|
return b;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ERR_FAIL_COND_V(writing, 0);
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
int to_copy = MIN(p_length, data.size() - pos);
|
|
|
|
for (int i = 0; i < to_copy; i++) {
|
2014-04-29 02:56:43 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
p_dst[i] = data[pos++];
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (to_copy < p_length) {
|
|
|
|
eofed = true;
|
2014-04-29 02:56:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return to_copy;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Error FileAccessEncrypted::get_error() const {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return eofed ? ERR_FILE_EOF : OK;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!writing);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos < data.size()) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < p_length; i++) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
store_8(p_src[i]);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (pos == data.size()) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data.resize(pos + p_length);
|
|
|
|
for (int i = 0; i < p_length; i++) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
data[pos + i] = p_src[i];
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
pos += p_length;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void FileAccessEncrypted::store_8(uint8_t p_dest) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!writing);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (pos < data.size()) {
|
|
|
|
data[pos] = p_dest;
|
2014-04-15 03:16:13 +02:00
|
|
|
pos++;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else if (pos == data.size()) {
|
2014-04-15 03:16:13 +02:00
|
|
|
data.push_back(p_dest);
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
bool FileAccessEncrypted::file_exists(const String &p_name) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
FileAccess *fa = FileAccess::open(p_name, FileAccess::READ);
|
2014-04-15 03:16:13 +02:00
|
|
|
if (!fa)
|
|
|
|
return false;
|
|
|
|
memdelete(fa);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) {
|
2014-04-15 03:16:13 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileAccessEncrypted::FileAccessEncrypted() {
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
file = NULL;
|
|
|
|
pos = 0;
|
|
|
|
eofed = false;
|
|
|
|
mode = MODE_MAX;
|
|
|
|
writing = false;
|
2014-04-15 03:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
FileAccessEncrypted::~FileAccessEncrypted() {
|
|
|
|
|
|
|
|
if (file)
|
|
|
|
close();
|
|
|
|
}
|