crypto.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /// Resource loader/saver
  82. 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) {
  83. String el = p_path.get_extension().to_lower();
  84. if (el == "crt") {
  85. X509Certificate *cert = X509Certificate::create();
  86. if (cert)
  87. cert->load(p_path);
  88. return cert;
  89. } else if (el == "key") {
  90. CryptoKey *key = CryptoKey::create();
  91. if (key)
  92. key->load(p_path);
  93. return key;
  94. }
  95. return nullptr;
  96. }
  97. void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
  98. p_extensions->push_back("crt");
  99. p_extensions->push_back("key");
  100. }
  101. bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
  102. return p_type == "X509Certificate" || p_type == "CryptoKey";
  103. }
  104. String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {
  105. String el = p_path.get_extension().to_lower();
  106. if (el == "crt")
  107. return "X509Certificate";
  108. else if (el == "key")
  109. return "CryptoKey";
  110. return "";
  111. }
  112. Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  113. Error err;
  114. Ref<X509Certificate> cert = p_resource;
  115. Ref<CryptoKey> key = p_resource;
  116. if (cert.is_valid()) {
  117. err = cert->save(p_path);
  118. } else if (key.is_valid()) {
  119. err = key->save(p_path);
  120. } else {
  121. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  122. }
  123. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Crypto resource to file '" + p_path + "'.");
  124. return OK;
  125. }
  126. void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  127. const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);
  128. const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);
  129. if (cert) {
  130. p_extensions->push_back("crt");
  131. }
  132. if (key) {
  133. p_extensions->push_back("key");
  134. }
  135. }
  136. bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const {
  137. return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
  138. }