crypto.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return nullptr;
  40. }
  41. void CryptoKey::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("save", "path"), &CryptoKey::save);
  43. ClassDB::bind_method(D_METHOD("load", "path"), &CryptoKey::load);
  44. }
  45. X509Certificate *(*X509Certificate::_create)() = nullptr;
  46. X509Certificate *X509Certificate::create() {
  47. if (_create)
  48. return _create();
  49. return nullptr;
  50. }
  51. void X509Certificate::_bind_methods() {
  52. ClassDB::bind_method(D_METHOD("save", "path"), &X509Certificate::save);
  53. ClassDB::bind_method(D_METHOD("load", "path"), &X509Certificate::load);
  54. }
  55. /// Crypto
  56. void (*Crypto::_load_default_certificates)(String p_path) = nullptr;
  57. Crypto *(*Crypto::_create)() = nullptr;
  58. Crypto *Crypto::create() {
  59. if (_create)
  60. return _create();
  61. return memnew(Crypto);
  62. }
  63. void Crypto::load_default_certificates(String p_path) {
  64. if (_load_default_certificates)
  65. _load_default_certificates(p_path);
  66. }
  67. void Crypto::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("generate_random_bytes", "size"), &Crypto::generate_random_bytes);
  69. ClassDB::bind_method(D_METHOD("generate_rsa", "size"), &Crypto::generate_rsa);
  70. 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"));
  71. }
  72. PackedByteArray Crypto::generate_random_bytes(int p_bytes) {
  73. ERR_FAIL_V_MSG(PackedByteArray(), "generate_random_bytes is not available when mbedtls module is disabled.");
  74. }
  75. Ref<CryptoKey> Crypto::generate_rsa(int p_bytes) {
  76. ERR_FAIL_V_MSG(nullptr, "generate_rsa is not available when mbedtls module is disabled.");
  77. }
  78. Ref<X509Certificate> Crypto::generate_self_signed_certificate(Ref<CryptoKey> p_key, String p_issuer_name, String p_not_before, String p_not_after) {
  79. ERR_FAIL_V_MSG(nullptr, "generate_self_signed_certificate is not available when mbedtls module is disabled.");
  80. }
  81. Crypto::Crypto() {
  82. }
  83. /// Resource loader/saver
  84. 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) {
  85. String el = p_path.get_extension().to_lower();
  86. if (el == "crt") {
  87. X509Certificate *cert = X509Certificate::create();
  88. if (cert)
  89. cert->load(p_path);
  90. return cert;
  91. } else if (el == "key") {
  92. CryptoKey *key = CryptoKey::create();
  93. if (key)
  94. key->load(p_path);
  95. return key;
  96. }
  97. return nullptr;
  98. }
  99. void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
  100. p_extensions->push_back("crt");
  101. p_extensions->push_back("key");
  102. }
  103. bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
  104. return p_type == "X509Certificate" || p_type == "CryptoKey";
  105. }
  106. String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {
  107. String el = p_path.get_extension().to_lower();
  108. if (el == "crt")
  109. return "X509Certificate";
  110. else if (el == "key")
  111. return "CryptoKey";
  112. return "";
  113. }
  114. Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  115. Error err;
  116. Ref<X509Certificate> cert = p_resource;
  117. Ref<CryptoKey> key = p_resource;
  118. if (cert.is_valid()) {
  119. err = cert->save(p_path);
  120. } else if (key.is_valid()) {
  121. err = key->save(p_path);
  122. } else {
  123. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  124. }
  125. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Crypto resource to file '" + p_path + "'.");
  126. return OK;
  127. }
  128. void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  129. const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);
  130. const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);
  131. if (cert) {
  132. p_extensions->push_back("crt");
  133. }
  134. if (key) {
  135. p_extensions->push_back("key");
  136. }
  137. }
  138. bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const {
  139. return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
  140. }