Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/crypto/crypto_resource_format.cpp
45997 views
1
/**************************************************************************/
2
/* crypto_resource_format.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "crypto_resource_format.h"
32
33
#include "core/crypto/crypto.h"
34
35
Ref<Resource> ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
36
String el = p_path.get_extension().to_lower();
37
if (el == "crt") {
38
X509Certificate *cert = X509Certificate::create();
39
if (cert) {
40
cert->load(p_path);
41
}
42
return cert;
43
} else if (el == "key") {
44
CryptoKey *key = CryptoKey::create();
45
if (key) {
46
key->load(p_path, false);
47
}
48
return key;
49
} else if (el == "pub") {
50
CryptoKey *key = CryptoKey::create();
51
if (key) {
52
key->load(p_path, true);
53
}
54
return key;
55
}
56
return nullptr;
57
}
58
59
void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
60
p_extensions->push_back("crt");
61
p_extensions->push_back("key");
62
p_extensions->push_back("pub");
63
}
64
65
bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
66
return p_type == "X509Certificate" || p_type == "CryptoKey";
67
}
68
69
String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {
70
String el = p_path.get_extension().to_lower();
71
if (el == "crt") {
72
return "X509Certificate";
73
} else if (el == "key" || el == "pub") {
74
return "CryptoKey";
75
}
76
return "";
77
}
78
79
Error ResourceFormatSaverCrypto::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) {
80
Error err;
81
Ref<X509Certificate> cert = p_resource;
82
Ref<CryptoKey> key = p_resource;
83
if (cert.is_valid()) {
84
err = cert->save(p_path);
85
} else if (key.is_valid()) {
86
err = key->save(p_path, p_path.has_extension("pub"));
87
} else {
88
ERR_FAIL_V(ERR_INVALID_PARAMETER);
89
}
90
ERR_FAIL_COND_V_MSG(err != OK, err, vformat("Cannot save Crypto resource to file '%s'.", p_path));
91
return OK;
92
}
93
94
void ResourceFormatSaverCrypto::get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const {
95
const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);
96
const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);
97
if (cert) {
98
p_extensions->push_back("crt");
99
}
100
if (key) {
101
if (!key->is_public_only()) {
102
p_extensions->push_back("key");
103
}
104
p_extensions->push_back("pub");
105
}
106
}
107
108
bool ResourceFormatSaverCrypto::recognize(const Ref<Resource> &p_resource) const {
109
return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
110
}
111
112