diff options
Diffstat (limited to 'core/crypto')
-rw-r--r-- | core/crypto/SCsub | 8 | ||||
-rw-r--r-- | core/crypto/crypto.cpp | 63 | ||||
-rw-r--r-- | core/crypto/crypto.h | 19 | ||||
-rw-r--r-- | core/crypto/crypto_core.cpp | 8 | ||||
-rw-r--r-- | core/crypto/crypto_core.h | 9 | ||||
-rw-r--r-- | core/crypto/hashing_context.cpp | 40 | ||||
-rw-r--r-- | core/crypto/hashing_context.h | 12 |
7 files changed, 72 insertions, 87 deletions
diff --git a/core/crypto/SCsub b/core/crypto/SCsub index 0a3f05d87a..da4a9c9381 100644 --- a/core/crypto/SCsub +++ b/core/crypto/SCsub @@ -1,6 +1,6 @@ #!/usr/bin/env python -Import('env') +Import("env") env_crypto = env.Clone() @@ -22,7 +22,9 @@ if not has_module: env_thirdparty = env_crypto.Clone() env_thirdparty.disable_warnings() # Custom config file - env_thirdparty.Append(CPPDEFINES=[('MBEDTLS_CONFIG_FILE', '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"')]) + env_thirdparty.Append( + CPPDEFINES=[("MBEDTLS_CONFIG_FILE", '\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\"')] + ) thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/" thirdparty_mbedtls_sources = [ "aes.c", @@ -30,7 +32,7 @@ if not has_module: "md5.c", "sha1.c", "sha256.c", - "godot_core_mbedtls_platform.c" + "godot_core_mbedtls_platform.c", ] thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources] env_thirdparty.add_source_files(env.core_sources, thirdparty_mbedtls_sources) diff --git a/core/crypto/crypto.cpp b/core/crypto/crypto.cpp index 83a25da901..eb942c60c9 100644 --- a/core/crypto/crypto.cpp +++ b/core/crypto/crypto.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -36,11 +36,12 @@ /// Resources -CryptoKey *(*CryptoKey::_create)() = NULL; +CryptoKey *(*CryptoKey::_create)() = nullptr; CryptoKey *CryptoKey::create() { - if (_create) + if (_create) { return _create(); - return NULL; + } + return nullptr; } void CryptoKey::_bind_methods() { @@ -48,11 +49,12 @@ void CryptoKey::_bind_methods() { ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load); } -X509Certificate *(*X509Certificate::_create)() = NULL; +X509Certificate *(*X509Certificate::_create)() = nullptr; X509Certificate *X509Certificate::create() { - if (_create) + if (_create) { return _create(); - return NULL; + } + return nullptr; } void X509Certificate::_bind_methods() { @@ -62,18 +64,19 @@ void X509Certificate::_bind_methods() { /// Crypto -void (*Crypto::_load_default_certificates)(String p_path) = NULL; -Crypto *(*Crypto::_create)() = NULL; +void (*Crypto::_load_default_certificates)(String p_path) = nullptr; +Crypto *(*Crypto::_create)() = nullptr; Crypto *Crypto::create() { - if (_create) + if (_create) { return _create(); + } return memnew(Crypto); } void Crypto::load_default_certificates(String p_path) { - - if (_load_default_certificates) + if (_load_default_certificates) { _load_default_certificates(p_path); + } } void Crypto::_bind_methods() { @@ -82,63 +85,58 @@ void Crypto::_bind_methods() { ClassDB::bind_method(D_METHOD("generate_self_signed_certificate", "key", "issuer_name", "not_before", "not_after"), &Crypto::generate_self_signed_certificate, DEFVAL("CN=myserver,O=myorganisation,C=IT"), DEFVAL("20140101000000"), DEFVAL("20340101000000")); } -PoolByteArray Crypto::generate_random_bytes(int p_bytes) { - ERR_FAIL_V_MSG(PoolByteArray(), "generate_random_bytes is not available when mbedtls module is disabled."); +PackedByteArray Crypto::generate_random_bytes(int p_bytes) { + ERR_FAIL_V_MSG(PackedByteArray(), "generate_random_bytes is not available when mbedtls module is disabled."); } Ref<CryptoKey> Crypto::generate_rsa(int p_bytes) { - ERR_FAIL_V_MSG(NULL, "generate_rsa is not available when mbedtls module is disabled."); + ERR_FAIL_V_MSG(nullptr, "generate_rsa is not available when mbedtls module is disabled."); } Ref<X509Certificate> Crypto::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) { - ERR_FAIL_V_MSG(NULL, "generate_self_signed_certificate is not available when mbedtls module is disabled."); -} - -Crypto::Crypto() { + ERR_FAIL_V_MSG(nullptr, "generate_self_signed_certificate is not available when mbedtls module is disabled."); } /// Resource loader/saver -RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error) { - +RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { String el = p_path.get_extension().to_lower(); if (el == "crt") { X509Certificate *cert = X509Certificate::create(); - if (cert) + if (cert) { cert->load(p_path); + } return cert; } else if (el == "key") { CryptoKey *key = CryptoKey::create(); - if (key) + if (key) { key->load(p_path); + } return key; } - return NULL; + return nullptr; } void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("crt"); p_extensions->push_back("key"); } bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const { - return p_type == "X509Certificate" || p_type == "CryptoKey"; } String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const { - String el = p_path.get_extension().to_lower(); - if (el == "crt") + if (el == "crt") { return "X509Certificate"; - else if (el == "key") + } else if (el == "key") { return "CryptoKey"; + } return ""; } Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - Error err; Ref<X509Certificate> cert = p_resource; Ref<CryptoKey> key = p_resource; @@ -154,7 +152,6 @@ Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resourc } void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { - const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource); const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource); if (cert) { @@ -164,7 +161,7 @@ void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, p_extensions->push_back("key"); } } -bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const { +bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const { return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource); } diff --git a/core/crypto/crypto.h b/core/crypto/crypto.h index 2de81f5b57..cf21648a4a 100644 --- a/core/crypto/crypto.h +++ b/core/crypto/crypto.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,11 +31,10 @@ #ifndef CRYPTO_H #define CRYPTO_H -#include "core/reference.h" -#include "core/resource.h" - #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" +#include "core/reference.h" +#include "core/resource.h" class CryptoKey : public Resource { GDCLASS(CryptoKey, Resource); @@ -76,26 +75,22 @@ public: static Crypto *create(); static void load_default_certificates(String p_path); - virtual PoolByteArray generate_random_bytes(int p_bytes); + virtual PackedByteArray generate_random_bytes(int p_bytes); virtual Ref<CryptoKey> generate_rsa(int p_bytes); virtual Ref<X509Certificate> generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after); - Crypto(); + Crypto() {} }; class ResourceFormatLoaderCrypto : public ResourceFormatLoader { - GDCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader); - public: - virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); + virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false); virtual void get_recognized_extensions(List<String> *p_extensions) const; virtual bool handles_type(const String &p_type) const; virtual String get_resource_type(const String &p_path) const; }; class ResourceFormatSaverCrypto : public ResourceFormatSaver { - GDCLASS(ResourceFormatSaverCrypto, ResourceFormatSaver); - public: virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0); virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const; diff --git a/core/crypto/crypto_core.cpp b/core/crypto/crypto_core.cpp index 51c2e3c9e5..ec25ee0d38 100644 --- a/core/crypto/crypto_core.cpp +++ b/core/crypto/crypto_core.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -148,9 +148,9 @@ Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst // CryptoCore String CryptoCore::b64_encode_str(const uint8_t *p_src, int p_src_len) { int b64len = p_src_len / 3 * 4 + 4 + 1; - PoolVector<uint8_t> b64buff; + Vector<uint8_t> b64buff; b64buff.resize(b64len); - PoolVector<uint8_t>::Write w64 = b64buff.write(); + uint8_t *w64 = b64buff.ptrw(); size_t strlen = 0; int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len); w64[strlen] = 0; diff --git a/core/crypto/crypto_core.h b/core/crypto/crypto_core.h index c859d612d4..36d8ace723 100644 --- a/core/crypto/crypto_core.h +++ b/core/crypto/crypto_core.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -34,10 +34,8 @@ #include "core/reference.h" class CryptoCore { - public: class MD5Context { - private: void *ctx; // To include, or not to include... @@ -51,7 +49,6 @@ public: }; class SHA1Context { - private: void *ctx; // To include, or not to include... @@ -65,7 +62,6 @@ public: }; class SHA256Context { - private: void *ctx; // To include, or not to include... @@ -79,7 +75,6 @@ public: }; class AESContext { - private: void *ctx; // To include, or not to include... diff --git a/core/crypto/hashing_context.cpp b/core/crypto/hashing_context.cpp index b5aa0ddc18..fb0dadfbab 100644 --- a/core/crypto/hashing_context.cpp +++ b/core/crypto/hashing_context.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,9 +33,9 @@ #include "core/crypto/crypto_core.h" Error HashingContext::start(HashType p_type) { - ERR_FAIL_COND_V(ctx != NULL, ERR_ALREADY_IN_USE); + ERR_FAIL_COND_V(ctx != nullptr, ERR_ALREADY_IN_USE); _create_ctx(p_type); - ERR_FAIL_COND_V(ctx == NULL, ERR_UNAVAILABLE); + ERR_FAIL_COND_V(ctx == nullptr, ERR_UNAVAILABLE); switch (type) { case HASH_MD5: return ((CryptoCore::MD5Context *)ctx)->start(); @@ -47,11 +47,11 @@ Error HashingContext::start(HashType p_type) { return ERR_UNAVAILABLE; } -Error HashingContext::update(PoolByteArray p_chunk) { - ERR_FAIL_COND_V(ctx == NULL, ERR_UNCONFIGURED); +Error HashingContext::update(PackedByteArray p_chunk) { + ERR_FAIL_COND_V(ctx == nullptr, ERR_UNCONFIGURED); size_t len = p_chunk.size(); ERR_FAIL_COND_V(len == 0, FAILED); - PoolByteArray::Read r = p_chunk.read(); + const uint8_t *r = p_chunk.ptr(); switch (type) { case HASH_MD5: return ((CryptoCore::MD5Context *)ctx)->update(&r[0], len); @@ -63,26 +63,26 @@ Error HashingContext::update(PoolByteArray p_chunk) { return ERR_UNAVAILABLE; } -PoolByteArray HashingContext::finish() { - ERR_FAIL_COND_V(ctx == NULL, PoolByteArray()); - PoolByteArray out; +PackedByteArray HashingContext::finish() { + ERR_FAIL_COND_V(ctx == nullptr, PackedByteArray()); + PackedByteArray out; Error err = FAILED; switch (type) { case HASH_MD5: out.resize(16); - err = ((CryptoCore::MD5Context *)ctx)->finish(out.write().ptr()); + err = ((CryptoCore::MD5Context *)ctx)->finish(out.ptrw()); break; case HASH_SHA1: out.resize(20); - err = ((CryptoCore::SHA1Context *)ctx)->finish(out.write().ptr()); + err = ((CryptoCore::SHA1Context *)ctx)->finish(out.ptrw()); break; case HASH_SHA256: out.resize(32); - err = ((CryptoCore::SHA256Context *)ctx)->finish(out.write().ptr()); + err = ((CryptoCore::SHA256Context *)ctx)->finish(out.ptrw()); break; } _delete_ctx(); - ERR_FAIL_COND_V(err != OK, PoolByteArray()); + ERR_FAIL_COND_V(err != OK, PackedByteArray()); return out; } @@ -99,12 +99,11 @@ void HashingContext::_create_ctx(HashType p_type) { ctx = memnew(CryptoCore::SHA256Context); break; default: - ctx = NULL; + ctx = nullptr; } } void HashingContext::_delete_ctx() { - switch (type) { case HASH_MD5: memdelete((CryptoCore::MD5Context *)ctx); @@ -116,7 +115,7 @@ void HashingContext::_delete_ctx() { memdelete((CryptoCore::SHA256Context *)ctx); break; } - ctx = NULL; + ctx = nullptr; } void HashingContext::_bind_methods() { @@ -128,11 +127,8 @@ void HashingContext::_bind_methods() { BIND_ENUM_CONSTANT(HASH_SHA256); } -HashingContext::HashingContext() { - ctx = NULL; -} - HashingContext::~HashingContext() { - if (ctx != NULL) + if (ctx != nullptr) { _delete_ctx(); + } } diff --git a/core/crypto/hashing_context.h b/core/crypto/hashing_context.h index aa69636f2c..f9454fa891 100644 --- a/core/crypto/hashing_context.h +++ b/core/crypto/hashing_context.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -44,7 +44,7 @@ public: }; private: - void *ctx; + void *ctx = nullptr; HashType type; protected: @@ -54,10 +54,10 @@ protected: public: Error start(HashType p_type); - Error update(PoolByteArray p_chunk); - PoolByteArray finish(); + Error update(PackedByteArray p_chunk); + PackedByteArray finish(); - HashingContext(); + HashingContext() {} ~HashingContext(); }; |