stream_peer_ssl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*************************************************************************/
  2. /* stream_peer_ssl.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "stream_peer_ssl.h"
  31. #include "core/io/certs_compressed.gen.h"
  32. #include "core/io/compression.h"
  33. #include "core/os/file_access.h"
  34. #include "core/project_settings.h"
  35. StreamPeerSSL *(*StreamPeerSSL::_create)() = NULL;
  36. StreamPeerSSL *StreamPeerSSL::create() {
  37. return _create();
  38. }
  39. StreamPeerSSL::LoadCertsFromMemory StreamPeerSSL::load_certs_func = NULL;
  40. bool StreamPeerSSL::available = false;
  41. void StreamPeerSSL::load_certs_from_memory(const PoolByteArray &p_memory) {
  42. if (load_certs_func)
  43. load_certs_func(p_memory);
  44. }
  45. void StreamPeerSSL::load_certs_from_file(String p_path) {
  46. if (p_path != "") {
  47. PoolByteArray certs = get_cert_file_as_array(p_path);
  48. if (certs.size() > 0)
  49. load_certs_func(certs);
  50. }
  51. }
  52. bool StreamPeerSSL::is_available() {
  53. return available;
  54. }
  55. void StreamPeerSSL::set_blocking_handshake_enabled(bool p_enabled) {
  56. blocking_handshake = p_enabled;
  57. }
  58. bool StreamPeerSSL::is_blocking_handshake_enabled() const {
  59. return blocking_handshake;
  60. }
  61. PoolByteArray StreamPeerSSL::get_cert_file_as_array(String p_path) {
  62. PoolByteArray out;
  63. FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
  64. if (f) {
  65. int flen = f->get_len();
  66. out.resize(flen + 1);
  67. PoolByteArray::Write w = out.write();
  68. f->get_buffer(w.ptr(), flen);
  69. w[flen] = 0; // Make sure it ends with string terminator
  70. memdelete(f);
  71. #ifdef DEBUG_ENABLED
  72. print_verbose(vformat("Loaded certs from '%s'.", p_path));
  73. #endif
  74. }
  75. return out;
  76. }
  77. PoolByteArray StreamPeerSSL::get_project_cert_array() {
  78. PoolByteArray out;
  79. String certs_path = GLOBAL_DEF("network/ssl/certificates", "");
  80. ProjectSettings::get_singleton()->set_custom_property_info("network/ssl/certificates", PropertyInfo(Variant::STRING, "network/ssl/certificates", PROPERTY_HINT_FILE, "*.crt"));
  81. if (certs_path != "") {
  82. // Use certs defined in project settings.
  83. return get_cert_file_as_array(certs_path);
  84. }
  85. #ifdef BUILTIN_CERTS_ENABLED
  86. else {
  87. // Use builtin certs only if user did not override it in project settings.
  88. out.resize(_certs_uncompressed_size + 1);
  89. PoolByteArray::Write w = out.write();
  90. Compression::decompress(w.ptr(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_DEFLATE);
  91. w[_certs_uncompressed_size] = 0; // Make sure it ends with string terminator
  92. #ifdef DEBUG_ENABLED
  93. print_verbose("Loaded builtin certs");
  94. #endif
  95. }
  96. #endif
  97. return out;
  98. }
  99. void StreamPeerSSL::_bind_methods() {
  100. ClassDB::bind_method(D_METHOD("poll"), &StreamPeerSSL::poll);
  101. ClassDB::bind_method(D_METHOD("accept_stream", "base"), &StreamPeerSSL::accept_stream);
  102. ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname"), &StreamPeerSSL::connect_to_stream, DEFVAL(false), DEFVAL(String()));
  103. ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerSSL::get_status);
  104. ClassDB::bind_method(D_METHOD("disconnect_from_stream"), &StreamPeerSSL::disconnect_from_stream);
  105. ClassDB::bind_method(D_METHOD("set_blocking_handshake_enabled", "enabled"), &StreamPeerSSL::set_blocking_handshake_enabled);
  106. ClassDB::bind_method(D_METHOD("is_blocking_handshake_enabled"), &StreamPeerSSL::is_blocking_handshake_enabled);
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "blocking_handshake"), "set_blocking_handshake_enabled", "is_blocking_handshake_enabled");
  108. BIND_ENUM_CONSTANT(STATUS_DISCONNECTED);
  109. BIND_ENUM_CONSTANT(STATUS_HANDSHAKING);
  110. BIND_ENUM_CONSTANT(STATUS_CONNECTED);
  111. BIND_ENUM_CONSTANT(STATUS_ERROR);
  112. BIND_ENUM_CONSTANT(STATUS_ERROR_HOSTNAME_MISMATCH);
  113. }
  114. StreamPeerSSL::StreamPeerSSL() {
  115. blocking_handshake = true;
  116. }