From 69853a3f68ef4f92c8b6788b21b9381744484ac1 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Dec 2023 20:11:13 +0100 Subject: [PATCH] More cleanups. --- core/bind/core_bind.cpp | 1 - core/io/config_file.cpp | 81 -------- core/io/config_file.h | 6 - core/io/dtls_server.cpp | 29 --- core/io/dtls_server.h | 30 --- core/io/file_access_encrypted.cpp | 283 ---------------------------- core/io/file_access_encrypted.h | 65 ------- core/register_core_types.cpp | 2 - modules/gdscript/gdscript.cpp | 33 +--- modules/gdscript/register_types.cpp | 1 - 10 files changed, 1 insertion(+), 530 deletions(-) delete mode 100644 core/io/dtls_server.cpp delete mode 100644 core/io/dtls_server.h delete mode 100644 core/io/file_access_encrypted.cpp delete mode 100644 core/io/file_access_encrypted.h diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp index 35a25a6..ff56bfe 100644 --- a/core/bind/core_bind.cpp +++ b/core/bind/core_bind.cpp @@ -6,7 +6,6 @@ #include "core/config/project_settings.h" #include "core/crypto/crypto_core.h" -#include "core/io/file_access_encrypted.h" #include "core/io/json.h" #include "core/io/marshalls.h" #include "core/math/geometry.h" diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index d5e7159..0442746 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -4,7 +4,6 @@ #include "config_file.h" -#include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" #include "core/variant/variant_parser.h" @@ -111,43 +110,6 @@ Error ConfigFile::save(const String &p_path) { return _internal_save(file); } -Error ConfigFile::save_encrypted(const String &p_path, const Vector &p_key) { - Error err; - FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); - - if (err) { - return err; - } - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256); - if (err) { - memdelete(fae); - memdelete(f); - return err; - } - return _internal_save(fae); -} - -Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) { - Error err; - FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); - - if (err) { - return err; - } - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256); - if (err) { - memdelete(fae); - memdelete(f); - return err; - } - - return _internal_save(fae); -} - Error ConfigFile::_internal_save(FileAccess *file) { for (OrderedHashMap>::Element E = values.front(); E; E = E.next()) { if (E != values.front()) { @@ -178,43 +140,6 @@ Error ConfigFile::load(const String &p_path) { return _internal_load(p_path, f); } -Error ConfigFile::load_encrypted(const String &p_path, const Vector &p_key) { - Error err; - FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - - if (err) { - return err; - } - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ); - if (err) { - memdelete(fae); - memdelete(f); - return err; - } - return _internal_load(p_path, fae); -} - -Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) { - Error err; - FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - - if (err) { - return err; - } - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ); - if (err) { - memdelete(fae); - memdelete(f); - return err; - } - - return _internal_load(p_path, fae); -} - Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { VariantParser::StreamFile stream; stream.f = f; @@ -285,11 +210,5 @@ void ConfigFile::_bind_methods() { ClassDB::bind_method(D_METHOD("parse", "data"), &ConfigFile::parse); ClassDB::bind_method(D_METHOD("save", "path"), &ConfigFile::save); - ClassDB::bind_method(D_METHOD("load_encrypted", "path", "key"), &ConfigFile::load_encrypted); - ClassDB::bind_method(D_METHOD("load_encrypted_pass", "path", "password"), &ConfigFile::load_encrypted_pass); - - ClassDB::bind_method(D_METHOD("save_encrypted", "path", "key"), &ConfigFile::save_encrypted); - ClassDB::bind_method(D_METHOD("save_encrypted_pass", "path", "password"), &ConfigFile::save_encrypted_pass); - ClassDB::bind_method(D_METHOD("clear"), &ConfigFile::clear); } diff --git a/core/io/config_file.h b/core/io/config_file.h index cc3d276..c584690 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -42,12 +42,6 @@ public: Error parse(const String &p_data); void clear(); - - Error load_encrypted(const String &p_path, const Vector &p_key); - Error load_encrypted_pass(const String &p_path, const String &p_pass); - - Error save_encrypted(const String &p_path, const Vector &p_key); - Error save_encrypted_pass(const String &p_path, const String &p_pass); }; #endif // CONFIG_FILE_H diff --git a/core/io/dtls_server.cpp b/core/io/dtls_server.cpp deleted file mode 100644 index c922bf0..0000000 --- a/core/io/dtls_server.cpp +++ /dev/null @@ -1,29 +0,0 @@ - -/* dtls_server.cpp */ - - -#include "dtls_server.h" -#include "core/os/file_access.h" -#include "core/config/project_settings.h" - -DTLSServer *(*DTLSServer::_create)() = nullptr; -bool DTLSServer::available = false; - -DTLSServer *DTLSServer::create() { - if (_create) { - return _create(); - } - return nullptr; -} - -bool DTLSServer::is_available() { - return available; -} - -void DTLSServer::_bind_methods() { - ClassDB::bind_method(D_METHOD("setup", "key", "certificate", "chain"), &DTLSServer::setup, DEFVAL(Ref())); - ClassDB::bind_method(D_METHOD("take_connection", "udp_peer"), &DTLSServer::take_connection); -} - -DTLSServer::DTLSServer() { -} diff --git a/core/io/dtls_server.h b/core/io/dtls_server.h deleted file mode 100644 index 021b03c..0000000 --- a/core/io/dtls_server.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef DTLS_SERVER_H -#define DTLS_SERVER_H - -/* dtls_server.h */ - - -#include "core/io/net_socket.h" -#include "core/io/packet_peer_dtls.h" - -class DTLSServer : public Reference { - GDCLASS(DTLSServer, Reference); - -protected: - static DTLSServer *(*_create)(); - static void _bind_methods(); - - static bool available; - -public: - static bool is_available(); - static DTLSServer *create(); - - virtual Error setup(Ref p_key, Ref p_cert, Ref p_ca_chain = Ref()) = 0; - virtual void stop() = 0; - virtual Ref take_connection(Ref p_peer) = 0; - - DTLSServer(); -}; - -#endif // DTLS_SERVER_H diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp deleted file mode 100644 index 1f57f47..0000000 --- a/core/io/file_access_encrypted.cpp +++ /dev/null @@ -1,283 +0,0 @@ - -/* file_access_encrypted.cpp */ - - -#include "file_access_encrypted.h" - -#include "core/crypto/crypto_core.h" -#include "core/string/print_string.h" -#include "core/variant/variant.h" - -#include - -#define COMP_MAGIC 0x43454447 - -Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector &p_key, Mode p_mode) { - ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); - ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER); - - pos = 0; - eofed = false; - - if (p_mode == MODE_WRITE_AES256) { - data.clear(); - writing = true; - file = p_base; - mode = p_mode; - key = p_key; - - } else if (p_mode == MODE_READ) { - writing = false; - key = p_key; - uint32_t magic = p_base->get_32(); - 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); - - unsigned char md5d[16]; - p_base->get_buffer(md5d, 16); - length = p_base->get_64(); - base = p_base->get_position(); - ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT); - uint64_t ds = length; - if (ds % 16) { - ds += 16 - (ds % 16); - } - - data.resize(ds); - - uint64_t blen = p_base->get_buffer(data.ptrw(), ds); - ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT); - - CryptoCore::AESContext ctx; - ctx.set_decode_key(key.ptrw(), 256); - - for (uint64_t i = 0; i < ds; i += 16) { - ctx.decrypt_ecb(&data.write[i], &data.write[i]); - } - - data.resize(length); - - unsigned char hash[16]; - ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG); - - ERR_FAIL_COND_V_MSG(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT, "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."); - - file = p_base; - } - - return OK; -} - -Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode) { - String cs = p_key.md5_text(); - ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER); - Vector key; - key.resize(32); - for (int i = 0; i < 32; i++) { - key.write[i] = cs[i]; - } - - return open_and_parse(p_base, key, p_mode); -} - -Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) { - return OK; -} -void FileAccessEncrypted::close() { - if (!file) { - return; - } - - if (writing) { - Vector compressed; - uint64_t len = data.size(); - if (len % 16) { - len += 16 - (len % 16); - } - - unsigned char hash[16]; - ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug? - - compressed.resize(len); - memset(compressed.ptrw(), 0, len); - for (int i = 0; i < data.size(); i++) { - compressed.write[i] = data[i]; - } - - CryptoCore::AESContext ctx; - ctx.set_encode_key(key.ptrw(), 256); - - for (uint64_t i = 0; i < len; i += 16) { - ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]); - } - - file->store_32(COMP_MAGIC); - file->store_32(mode); - - file->store_buffer(hash, 16); - file->store_64(data.size()); - - file->store_buffer(compressed.ptr(), compressed.size()); - file->close(); - memdelete(file); - file = nullptr; - data.clear(); - - } else { - file->close(); - memdelete(file); - data.clear(); - file = nullptr; - } -} - -bool FileAccessEncrypted::is_open() const { - return file != nullptr; -} - -String FileAccessEncrypted::get_path() const { - if (file) { - return file->get_path(); - } else { - return ""; - } -} - -String FileAccessEncrypted::get_path_absolute() const { - if (file) { - return file->get_path_absolute(); - } else { - return ""; - } -} - -void FileAccessEncrypted::seek(uint64_t p_position) { - if (p_position > get_len()) { - p_position = get_len(); - } - - pos = p_position; - eofed = false; -} - -void FileAccessEncrypted::seek_end(int64_t p_position) { - seek(get_len() + p_position); -} - -uint64_t FileAccessEncrypted::get_position() const { - return pos; -} - -uint64_t FileAccessEncrypted::get_len() const { - return data.size(); -} - -bool FileAccessEncrypted::eof_reached() const { - return eofed; -} - -uint8_t FileAccessEncrypted::get_8() const { - ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); - if (pos >= get_len()) { - eofed = true; - return 0; - } - - uint8_t b = data[pos]; - pos++; - return b; -} - -uint64_t FileAccessEncrypted::get_buffer(uint8_t *p_dst, uint64_t p_length) const { - ERR_FAIL_COND_V(!p_dst && p_length > 0, -1); - ERR_FAIL_COND_V_MSG(writing, -1, "File has not been opened in read mode."); - - uint64_t to_copy = MIN(p_length, get_len() - pos); - for (uint64_t i = 0; i < to_copy; i++) { - p_dst[i] = data[pos++]; - } - - if (to_copy < p_length) { - eofed = true; - } - - return to_copy; -} - -Error FileAccessEncrypted::get_error() const { - return eofed ? ERR_FILE_EOF : OK; -} - -void FileAccessEncrypted::store_buffer(const uint8_t *p_src, uint64_t p_length) { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); - ERR_FAIL_COND(!p_src && p_length > 0); - - if (pos < get_len()) { - for (uint64_t i = 0; i < p_length; i++) { - store_8(p_src[i]); - } - } else if (pos == get_len()) { - data.resize(pos + p_length); - for (uint64_t i = 0; i < p_length; i++) { - data.write[pos + i] = p_src[i]; - } - pos += p_length; - } -} - -void FileAccessEncrypted::flush() { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); - - // encrypted files keep data in memory till close() -} - -void FileAccessEncrypted::store_8(uint8_t p_dest) { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); - - if (pos < get_len()) { - data.write[pos] = p_dest; - pos++; - } else if (pos == get_len()) { - data.push_back(p_dest); - pos++; - } -} - -bool FileAccessEncrypted::file_exists(const String &p_name) { - FileAccess *fa = FileAccess::open(p_name, FileAccess::READ); - if (!fa) { - return false; - } - memdelete(fa); - return true; -} - -uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) { - return 0; -} - -uint32_t FileAccessEncrypted::_get_unix_permissions(const String &p_file) { - return 0; -} - -Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t p_permissions) { - ERR_PRINT("Setting UNIX permissions on encrypted files is not implemented yet."); - return ERR_UNAVAILABLE; -} - -FileAccessEncrypted::FileAccessEncrypted() { - file = nullptr; - pos = 0; - eofed = false; - mode = MODE_MAX; - writing = false; -} - -FileAccessEncrypted::~FileAccessEncrypted() { - if (file) { - close(); - } -} diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h deleted file mode 100644 index dc4e5ba..0000000 --- a/core/io/file_access_encrypted.h +++ /dev/null @@ -1,65 +0,0 @@ -#ifndef FILE_ACCESS_ENCRYPTED_H -#define FILE_ACCESS_ENCRYPTED_H - -/* file_access_encrypted.h */ - - -#include "core/os/file_access.h" - -class FileAccessEncrypted : public FileAccess { -public: - enum Mode { - MODE_READ, - MODE_WRITE_AES256, - MODE_MAX - }; - -private: - Mode mode; - Vector key; - bool writing; - FileAccess *file; - uint64_t base; - uint64_t length; - Vector data; - mutable uint64_t pos; - mutable bool eofed; - -public: - Error open_and_parse(FileAccess *p_base, const Vector &p_key, Mode p_mode); - Error open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode); - - virtual Error _open(const String &p_path, int p_mode_flags); ///< open a file - virtual void close(); ///< close a file - virtual bool is_open() const; ///< true when file is open - - virtual String get_path() const; /// returns the path for the current open file - virtual String get_path_absolute() const; /// returns the absolute path for the current open file - - virtual void seek(uint64_t p_position); ///< seek to a given position - virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - virtual uint64_t get_position() const; ///< get position in the file - virtual uint64_t get_len() const; ///< get size of the file - - virtual bool eof_reached() const; ///< reading passed EOF - - virtual uint8_t get_8() const; ///< get a byte - virtual uint64_t get_buffer(uint8_t *p_dst, uint64_t p_length) const; - - virtual Error get_error() const; ///< get last error - - virtual void flush(); - virtual void store_8(uint8_t p_dest); ///< store a byte - virtual void store_buffer(const uint8_t *p_src, uint64_t p_length); ///< store an array of bytes - - virtual bool file_exists(const String &p_name); ///< return true if a file exists - - virtual uint64_t _get_modified_time(const String &p_file); - virtual uint32_t _get_unix_permissions(const String &p_file); - virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions); - - FileAccessEncrypted(); - ~FileAccessEncrypted(); -}; - -#endif // FILE_ACCESS_ENCRYPTED_H diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index f03a199..5368855 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -15,7 +15,6 @@ #include "core/input/input.h" #include "core/input/input_map.h" #include "core/io/config_file.h" -#include "core/io/dtls_server.h" #include "core/io/http_client.h" #include "core/io/image_loader.h" #include "core/io/marshalls.h" @@ -142,7 +141,6 @@ void register_core_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_custom_instance_class(); - ClassDB::register_custom_instance_class(); // Crypto ClassDB::register_class(); diff --git a/modules/gdscript/gdscript.cpp b/modules/gdscript/gdscript.cpp index 3c49b60..0c3110b 100644 --- a/modules/gdscript/gdscript.cpp +++ b/modules/gdscript/gdscript.cpp @@ -8,7 +8,6 @@ #include "core/config/project_settings.h" #include "core/core_string_names.h" #include "core/global_constants.h" -#include "core/io/file_access_encrypted.h" #include "core/os/file_access.h" #include "core/os/os.h" #include "gdscript_compiler.h" @@ -703,37 +702,7 @@ Vector GDScript::get_as_byte_code() const { Error GDScript::load_byte_code(const String &p_path) { Vector bytecode; - if (p_path.ends_with("gde")) { - FileAccess *fa = FileAccess::open(p_path, FileAccess::READ); - ERR_FAIL_COND_V(!fa, ERR_CANT_OPEN); - - FileAccessEncrypted *fae = memnew(FileAccessEncrypted); - ERR_FAIL_COND_V(!fae, ERR_CANT_OPEN); - - Vector key; - key.resize(32); - for (int i = 0; i < key.size(); i++) { - key.write[i] = script_encryption_key[i]; - } - - Error err = fae->open_and_parse(fa, key, FileAccessEncrypted::MODE_READ); - - if (err) { - fa->close(); - memdelete(fa); - memdelete(fae); - - ERR_FAIL_COND_V(err, err); - } - - bytecode.resize(fae->get_len()); - fae->get_buffer(bytecode.ptrw(), bytecode.size()); - fae->close(); - memdelete(fae); - - } else { - bytecode = FileAccess::get_file_as_array(p_path); - } + bytecode = FileAccess::get_file_as_array(p_path); ERR_FAIL_COND_V(bytecode.size() == 0, ERR_PARSE_ERROR); path = p_path; diff --git a/modules/gdscript/register_types.cpp b/modules/gdscript/register_types.cpp index 62121e4..889549b 100644 --- a/modules/gdscript/register_types.cpp +++ b/modules/gdscript/register_types.cpp @@ -4,7 +4,6 @@ #include "register_types.h" -#include "core/io/file_access_encrypted.h" #include "core/io/resource_loader.h" #include "core/os/dir_access.h" #include "core/os/file_access.h"