Fabio Alessandrelli 6 роки тому
батько
коміт
1b36aa662c
4 змінених файлів з 335 додано та 0 видалено
  1. 38 0
      core/crypto/SCsub
  2. 170 0
      core/crypto/crypto.cpp
  3. 105 0
      core/crypto/crypto.h
  4. 22 0
      core/register_core_types.cpp

+ 38 - 0
core/crypto/SCsub

@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+Import('env')
+
+env_crypto = env.Clone()
+
+is_builtin = env["builtin_mbedtls"]
+has_module = env["module_mbedtls_enabled"]
+
+if is_builtin or not has_module:
+    # Use our headers for builtin or if the module is not going to be compiled.
+    # We decided not to depend on system mbedtls just for these few files that can
+    # be easily extracted.
+    env_crypto.Prepend(CPPPATH=["#thirdparty/mbedtls/include"])
+
+# MbedTLS core functions (for CryptoCore).
+# If the mbedtls module is compiled we don't need to add the .c files with our
+# custom config since they will be built by the module itself.
+# Only if the module is not enabled, we must compile here the required sources
+# to make a "light" build with only the necessary mbedtls files.
+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\\"')])
+    thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/"
+    thirdparty_mbedtls_sources = [
+        "aes.c",
+        "base64.c",
+        "md5.c",
+        "sha1.c",
+        "sha256.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)
+
+env_crypto.add_source_files(env.core_sources, "*.cpp")

+ 170 - 0
core/crypto/crypto.cpp

@@ -0,0 +1,170 @@
+/*************************************************************************/
+/*  crypto.cpp                                                           */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2019 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       */
+/* "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.                */
+/*************************************************************************/
+
+#include "crypto.h"
+
+#include "core/engine.h"
+#include "core/io/certs_compressed.gen.h"
+#include "core/io/compression.h"
+
+/// Resources
+
+CryptoKey *(*CryptoKey::_create)() = NULL;
+CryptoKey *CryptoKey::create() {
+	if (_create)
+		return _create();
+	return NULL;
+}
+
+void CryptoKey::_bind_methods() {
+	ClassDB::bind_method(D_METHOD("save", "path"), &CryptoKey::save);
+	ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load);
+}
+
+X509Certificate *(*X509Certificate::_create)() = NULL;
+X509Certificate *X509Certificate::create() {
+	if (_create)
+		return _create();
+	return NULL;
+}
+
+void X509Certificate::_bind_methods() {
+	ClassDB::bind_method(D_METHOD("save", "path"), &X509Certificate::save);
+	ClassDB::bind_method(D_METHOD("load", "path"), &X509Certificate::load);
+}
+
+/// Crypto
+
+void (*Crypto::_load_default_certificates)(String p_path) = NULL;
+Crypto *(*Crypto::_create)() = NULL;
+Crypto *Crypto::create() {
+	if (_create)
+		return _create();
+	return memnew(Crypto);
+}
+
+void Crypto::load_default_certificates(String p_path) {
+
+	if (_load_default_certificates)
+		_load_default_certificates(p_path);
+}
+
+void Crypto::_bind_methods() {
+	ClassDB::bind_method(D_METHOD("generate_random_bytes", "size"), &Crypto::generate_random_bytes);
+	ClassDB::bind_method(D_METHOD("generate_rsa", "size"), &Crypto::generate_rsa);
+	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.");
+}
+
+Ref<CryptoKey> Crypto::generate_rsa(int p_bytes) {
+	ERR_FAIL_V_MSG(NULL, "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() {
+}
+
+/// Resource loader/saver
+
+RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error) {
+
+	String el = p_path.get_extension().to_lower();
+	if (el == "crt") {
+		X509Certificate *cert = X509Certificate::create();
+		if (cert)
+			cert->load(p_path);
+		return cert;
+	} else if (el == "key") {
+		CryptoKey *key = CryptoKey::create();
+		if (key)
+			key->load(p_path);
+		return key;
+	}
+	return NULL;
+}
+
+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")
+		return "X509Certificate";
+	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;
+	if (cert.is_valid()) {
+		err = cert->save(p_path);
+	} else if (key.is_valid()) {
+		err = key->save(p_path);
+	} else {
+		ERR_FAIL_V(ERR_INVALID_PARAMETER);
+	}
+	ERR_FAIL_COND_V(err != OK, err);
+	return OK;
+}
+
+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) {
+		p_extensions->push_back("crt");
+	}
+	if (key) {
+		p_extensions->push_back("key");
+	}
+}
+bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const {
+
+	return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
+}

+ 105 - 0
core/crypto/crypto.h

@@ -0,0 +1,105 @@
+/*************************************************************************/
+/*  crypto.h                                                             */
+/*************************************************************************/
+/*                       This file is part of:                           */
+/*                           GODOT ENGINE                                */
+/*                      https://godotengine.org                          */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
+/* Copyright (c) 2014-2019 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       */
+/* "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.                */
+/*************************************************************************/
+
+#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"
+
+class CryptoKey : public Resource {
+	GDCLASS(CryptoKey, Resource);
+
+protected:
+	static void _bind_methods();
+	static CryptoKey *(*_create)();
+
+public:
+	static CryptoKey *create();
+	virtual Error load(String p_path) = 0;
+	virtual Error save(String p_path) = 0;
+};
+
+class X509Certificate : public Resource {
+	GDCLASS(X509Certificate, Resource);
+
+protected:
+	static void _bind_methods();
+	static X509Certificate *(*_create)();
+
+public:
+	static X509Certificate *create();
+	virtual Error load(String p_path) = 0;
+	virtual Error load_from_memory(const uint8_t *p_buffer, int p_len) = 0;
+	virtual Error save(String p_path) = 0;
+};
+
+class Crypto : public Reference {
+	GDCLASS(Crypto, Reference);
+
+protected:
+	static void _bind_methods();
+	static Crypto *(*_create)();
+	static void (*_load_default_certificates)(String p_path);
+
+public:
+	static Crypto *create();
+	static void load_default_certificates(String p_path);
+
+	virtual PoolByteArray 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();
+};
+
+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 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;
+	virtual bool recognize(const RES &p_resource) const;
+};
+
+#endif // CRYPTO_H

+ 22 - 0
core/register_core_types.cpp

@@ -34,6 +34,7 @@
 #include "core/class_db.h"
 #include "core/compressed_translation.h"
 #include "core/core_string_names.h"
+#include "core/crypto/crypto.h"
 #include "core/engine.h"
 #include "core/func_ref.h"
 #include "core/input_map.h"
@@ -70,6 +71,8 @@ static Ref<ResourceFormatLoaderBinary> resource_loader_binary;
 static Ref<ResourceFormatImporter> resource_format_importer;
 static Ref<ResourceFormatLoaderImage> resource_format_image;
 static Ref<TranslationLoaderPO> resource_format_po;
+static Ref<ResourceFormatSaverCrypto> resource_format_saver_crypto;
+static Ref<ResourceFormatLoaderCrypto> resource_format_loader_crypto;
 
 static _ResourceLoader *_resource_loader = NULL;
 static _ResourceSaver *_resource_saver = NULL;
@@ -151,7 +154,18 @@ void register_core_types() {
 	ClassDB::register_class<StreamPeerTCP>();
 	ClassDB::register_class<TCP_Server>();
 	ClassDB::register_class<PacketPeerUDP>();
+
+	// Crypto
+	ClassDB::register_custom_instance_class<X509Certificate>();
+	ClassDB::register_custom_instance_class<CryptoKey>();
+	ClassDB::register_custom_instance_class<Crypto>();
 	ClassDB::register_custom_instance_class<StreamPeerSSL>();
+
+	resource_format_saver_crypto.instance();
+	ResourceSaver::add_resource_format_saver(resource_format_saver_crypto);
+	resource_format_loader_crypto.instance();
+	ResourceLoader::add_resource_format_loader(resource_format_loader_crypto);
+
 	ClassDB::register_virtual_class<IP>();
 	ClassDB::register_virtual_class<PacketPeer>();
 	ClassDB::register_class<PacketPeerStream>();
@@ -211,6 +225,9 @@ void register_core_settings() {
 	ProjectSettings::get_singleton()->set_custom_property_info("network/limits/tcp/connect_timeout_seconds", PropertyInfo(Variant::INT, "network/limits/tcp/connect_timeout_seconds", PROPERTY_HINT_RANGE, "1,1800,1"));
 	GLOBAL_DEF_RST("network/limits/packet_peer_stream/max_buffer_po2", (16));
 	ProjectSettings::get_singleton()->set_custom_property_info("network/limits/packet_peer_stream/max_buffer_po2", PropertyInfo(Variant::INT, "network/limits/packet_peer_stream/max_buffer_po2", PROPERTY_HINT_RANGE, "0,64,1,or_greater"));
+
+	GLOBAL_DEF("network/ssl/certificates", "");
+	ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt"));
 }
 
 void register_core_singletons() {
@@ -272,6 +289,11 @@ void unregister_core_types() {
 	ResourceLoader::remove_resource_format_loader(resource_format_po);
 	resource_format_po.unref();
 
+	ResourceSaver::remove_resource_format_saver(resource_format_saver_crypto);
+	resource_format_saver_crypto.unref();
+	ResourceLoader::remove_resource_format_loader(resource_format_loader_crypto);
+	resource_format_loader_crypto.unref();
+
 	if (ip)
 		memdelete(ip);