summaryrefslogtreecommitdiff
path: root/core/crypto
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-06-05 21:28:16 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2020-06-18 15:29:39 +0200
commit788f18086e01c67c817c1c095e88fd4cbbd3d345 (patch)
tree75cd7619ac36e6494f9df81c05c97d8f600f0a82 /core/crypto
parent9a462e07a4fa9f9ff44b2b5521a26eeeb68d34fe (diff)
CryptoKey supports public keys.
Diffstat (limited to 'core/crypto')
-rw-r--r--core/crypto/crypto.cpp25
-rw-r--r--core/crypto/crypto.h7
2 files changed, 24 insertions, 8 deletions
diff --git a/core/crypto/crypto.cpp b/core/crypto/crypto.cpp
index a9a7cabee9..a6b1fa9f03 100644
--- a/core/crypto/crypto.cpp
+++ b/core/crypto/crypto.cpp
@@ -45,8 +45,11 @@ CryptoKey *CryptoKey::create() {
}
void CryptoKey::_bind_methods() {
- ClassDB::bind_method(D_METHOD("save", "path"), &CryptoKey::save);
- ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load);
+ ClassDB::bind_method(D_METHOD("save", "path", "public_only"), &CryptoKey::save, DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("load", "path", "public_only"), &CryptoKey::load, DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("is_public_only"), &CryptoKey::is_public_only);
+ ClassDB::bind_method(D_METHOD("save_to_string", "public_only"), &CryptoKey::save_to_string, DEFVAL(false));
+ ClassDB::bind_method(D_METHOD("load_from_string", "string_key", "public_only"), &CryptoKey::load_from_string, DEFVAL(false));
}
X509Certificate *(*X509Certificate::_create)() = nullptr;
@@ -98,9 +101,14 @@ RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_origi
} else if (el == "key") {
CryptoKey *key = CryptoKey::create();
if (key) {
- key->load(p_path);
+ key->load(p_path, false);
}
return key;
+ } else if (el == "pub") {
+ CryptoKey *key = CryptoKey::create();
+ if (key)
+ key->load(p_path, true);
+ return key;
}
return nullptr;
}
@@ -108,6 +116,7 @@ RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_origi
void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->push_back("crt");
p_extensions->push_back("key");
+ p_extensions->push_back("pub");
}
bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
@@ -118,7 +127,7 @@ 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") {
+ } else if (el == "key" || el == "pub") {
return "CryptoKey";
}
return "";
@@ -131,7 +140,8 @@ Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resourc
if (cert.is_valid()) {
err = cert->save(p_path);
} else if (key.is_valid()) {
- err = key->save(p_path);
+ String el = p_path.get_extension().to_lower();
+ err = key->save(p_path, el == "pub");
} else {
ERR_FAIL_V(ERR_INVALID_PARAMETER);
}
@@ -146,7 +156,10 @@ void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource,
p_extensions->push_back("crt");
}
if (key) {
- p_extensions->push_back("key");
+ if (!key->is_public_only()) {
+ p_extensions->push_back("key");
+ }
+ p_extensions->push_back("pub");
}
}
diff --git a/core/crypto/crypto.h b/core/crypto/crypto.h
index 6cc5f46164..c1bc2024bb 100644
--- a/core/crypto/crypto.h
+++ b/core/crypto/crypto.h
@@ -45,8 +45,11 @@ protected:
public:
static CryptoKey *create();
- virtual Error load(String p_path) = 0;
- virtual Error save(String p_path) = 0;
+ virtual Error load(String p_path, bool p_public_only = false) = 0;
+ virtual Error save(String p_path, bool p_public_only = false) = 0;
+ virtual String save_to_string(bool p_public_only = false) = 0;
+ virtual Error load_from_string(String p_string_key, bool p_public_only = false) = 0;
+ virtual bool is_public_only() const = 0;
};
class X509Certificate : public Resource {