crypto.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*************************************************************************/
  2. /* crypto.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "crypto.h"
  31. #include "core/engine.h"
  32. #include "core/io/certs_compressed.gen.h"
  33. #include "core/io/compression.h"
  34. /// Resources
  35. CryptoKey *(*CryptoKey::_create)() = nullptr;
  36. CryptoKey *CryptoKey::create() {
  37. if (_create) {
  38. return _create();
  39. }
  40. return nullptr;
  41. }
  42. void CryptoKey::_bind_methods() {
  43. ClassDB::bind_method(D_METHOD("save", "path"), &CryptoKey::save);
  44. ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load);
  45. }
  46. X509Certificate *(*X509Certificate::_create)() = nullptr;
  47. X509Certificate *X509Certificate::create() {
  48. if (_create) {
  49. return _create();
  50. }
  51. return nullptr;
  52. }
  53. void X509Certificate::_bind_methods() {
  54. ClassDB::bind_method(D_METHOD("save", "path"), &X509Certificate::save);
  55. ClassDB::bind_method(D_METHOD("load", "path"), &X509Certificate::load);
  56. }
  57. /// Crypto
  58. void (*Crypto::_load_default_certificates)(String p_path) = nullptr;
  59. Crypto *(*Crypto::_create)() = nullptr;
  60. Crypto *Crypto::create() {
  61. if (_create) {
  62. return _create();
  63. }
  64. return memnew(Crypto);
  65. }
  66. void Crypto::load_default_certificates(String p_path) {
  67. if (_load_default_certificates) {
  68. _load_default_certificates(p_path);
  69. }
  70. }
  71. void Crypto::_bind_methods() {
  72. ClassDB::bind_method(D_METHOD("generate_random_bytes", "size"), &Crypto::generate_random_bytes);
  73. ClassDB::bind_method(D_METHOD("generate_rsa", "size"), &Crypto::generate_rsa);
  74. 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"));
  75. }
  76. PackedByteArray Crypto::generate_random_bytes(int p_bytes) {
  77. ERR_FAIL_V_MSG(PackedByteArray(), "generate_random_bytes is not available when mbedtls module is disabled.");
  78. }
  79. Ref<CryptoKey> Crypto::generate_rsa(int p_bytes) {
  80. ERR_FAIL_V_MSG(nullptr, "generate_rsa is not available when mbedtls module is disabled.");
  81. }
  82. Ref<X509Certificate> Crypto::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) {
  83. ERR_FAIL_V_MSG(nullptr, "generate_self_signed_certificate is not available when mbedtls module is disabled.");
  84. }
  85. /// Resource loader/saver
  86. 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) {
  87. String el = p_path.get_extension().to_lower();
  88. if (el == "crt") {
  89. X509Certificate *cert = X509Certificate::create();
  90. if (cert) {
  91. cert->load(p_path);
  92. }
  93. return cert;
  94. } else if (el == "key") {
  95. CryptoKey *key = CryptoKey::create();
  96. if (key) {
  97. key->load(p_path);
  98. }
  99. return key;
  100. }
  101. return nullptr;
  102. }
  103. void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
  104. p_extensions->push_back("crt");
  105. p_extensions->push_back("key");
  106. }
  107. bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
  108. return p_type == "X509Certificate" || p_type == "CryptoKey";
  109. }
  110. String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {
  111. String el = p_path.get_extension().to_lower();
  112. if (el == "crt") {
  113. return "X509Certificate";
  114. } else if (el == "key") {
  115. return "CryptoKey";
  116. }
  117. return "";
  118. }
  119. Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  120. Error err;
  121. Ref<X509Certificate> cert = p_resource;
  122. Ref<CryptoKey> key = p_resource;
  123. if (cert.is_valid()) {
  124. err = cert->save(p_path);
  125. } else if (key.is_valid()) {
  126. err = key->save(p_path);
  127. } else {
  128. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  129. }
  130. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Crypto resource to file '" + p_path + "'.");
  131. return OK;
  132. }
  133. void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  134. const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);
  135. const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);
  136. if (cert) {
  137. p_extensions->push_back("crt");
  138. }
  139. if (key) {
  140. p_extensions->push_back("key");
  141. }
  142. }
  143. bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const {
  144. return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
  145. }