crypto.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************/
  2. /* crypto.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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", "public_only"), &CryptoKey::save, DEFVAL(false));
  44. ClassDB::bind_method(D_METHOD("load", "path", "public_only"), &CryptoKey::load, DEFVAL(false));
  45. ClassDB::bind_method(D_METHOD("is_public_only"), &CryptoKey::is_public_only);
  46. ClassDB::bind_method(D_METHOD("save_to_string", "public_only"), &CryptoKey::save_to_string, DEFVAL(false));
  47. ClassDB::bind_method(D_METHOD("load_from_string", "string_key", "public_only"), &CryptoKey::load_from_string, DEFVAL(false));
  48. }
  49. X509Certificate *(*X509Certificate::_create)() = nullptr;
  50. X509Certificate *X509Certificate::create() {
  51. if (_create) {
  52. return _create();
  53. }
  54. return nullptr;
  55. }
  56. void X509Certificate::_bind_methods() {
  57. ClassDB::bind_method(D_METHOD("save", "path"), &X509Certificate::save);
  58. ClassDB::bind_method(D_METHOD("load", "path"), &X509Certificate::load);
  59. }
  60. /// Crypto
  61. void (*Crypto::_load_default_certificates)(String p_path) = nullptr;
  62. Crypto *(*Crypto::_create)() = nullptr;
  63. Crypto *Crypto::create() {
  64. if (_create) {
  65. return _create();
  66. }
  67. ERR_FAIL_V_MSG(nullptr, "Crypto is not available when the mbedtls module is disabled.");
  68. }
  69. void Crypto::load_default_certificates(String p_path) {
  70. if (_load_default_certificates) {
  71. _load_default_certificates(p_path);
  72. }
  73. }
  74. void Crypto::_bind_methods() {
  75. ClassDB::bind_method(D_METHOD("generate_random_bytes", "size"), &Crypto::generate_random_bytes);
  76. ClassDB::bind_method(D_METHOD("generate_rsa", "size"), &Crypto::generate_rsa);
  77. 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"));
  78. ClassDB::bind_method(D_METHOD("sign", "hash_type", "hash", "key"), &Crypto::sign);
  79. ClassDB::bind_method(D_METHOD("verify", "hash_type", "hash", "signature", "key"), &Crypto::verify);
  80. ClassDB::bind_method(D_METHOD("encrypt", "key", "plaintext"), &Crypto::encrypt);
  81. ClassDB::bind_method(D_METHOD("decrypt", "key", "ciphertext"), &Crypto::decrypt);
  82. }
  83. Crypto::Crypto() {
  84. }
  85. /// Resource loader/saver
  86. RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error) {
  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, false);
  98. }
  99. return key;
  100. } else if (el == "pub") {
  101. CryptoKey *key = CryptoKey::create();
  102. if (key) {
  103. key->load(p_path, true);
  104. }
  105. return key;
  106. }
  107. return nullptr;
  108. }
  109. void ResourceFormatLoaderCrypto::get_recognized_extensions(List<String> *p_extensions) const {
  110. p_extensions->push_back("crt");
  111. p_extensions->push_back("key");
  112. p_extensions->push_back("pub");
  113. }
  114. bool ResourceFormatLoaderCrypto::handles_type(const String &p_type) const {
  115. return p_type == "X509Certificate" || p_type == "CryptoKey";
  116. }
  117. String ResourceFormatLoaderCrypto::get_resource_type(const String &p_path) const {
  118. String el = p_path.get_extension().to_lower();
  119. if (el == "crt") {
  120. return "X509Certificate";
  121. } else if (el == "key" || el == "pub") {
  122. return "CryptoKey";
  123. }
  124. return "";
  125. }
  126. Error ResourceFormatSaverCrypto::save(const String &p_path, const RES &p_resource, uint32_t p_flags) {
  127. Error err;
  128. Ref<X509Certificate> cert = p_resource;
  129. Ref<CryptoKey> key = p_resource;
  130. if (cert.is_valid()) {
  131. err = cert->save(p_path);
  132. } else if (key.is_valid()) {
  133. String el = p_path.get_extension().to_lower();
  134. err = key->save(p_path, el == "pub");
  135. } else {
  136. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  137. }
  138. ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot save Crypto resource to file '" + p_path + "'.");
  139. return OK;
  140. }
  141. void ResourceFormatSaverCrypto::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
  142. const X509Certificate *cert = Object::cast_to<X509Certificate>(*p_resource);
  143. const CryptoKey *key = Object::cast_to<CryptoKey>(*p_resource);
  144. if (cert) {
  145. p_extensions->push_back("crt");
  146. }
  147. if (key) {
  148. if (!key->is_public_only()) {
  149. p_extensions->push_back("key");
  150. }
  151. p_extensions->push_back("pub");
  152. }
  153. }
  154. bool ResourceFormatSaverCrypto::recognize(const RES &p_resource) const {
  155. return Object::cast_to<X509Certificate>(*p_resource) || Object::cast_to<CryptoKey>(*p_resource);
  156. }