stream_peer_openssl.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* stream_peer_openssl.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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. #ifndef STREAM_PEER_OPEN_SSL_H
  31. #define STREAM_PEER_OPEN_SSL_H
  32. #include "io/stream_peer_ssl.h"
  33. #include "os/file_access.h"
  34. #include "project_settings.h"
  35. #include "thirdparty/misc/curl_hostcheck.h"
  36. #include <openssl/bio.h> // BIO objects for I/O
  37. #include <openssl/err.h> // Error reporting
  38. #include <openssl/ssl.h> // SSL and SSL_CTX for SSL connections
  39. #include <openssl/x509v3.h>
  40. #include <stdio.h>
  41. class StreamPeerOpenSSL : public StreamPeerSSL {
  42. private:
  43. static int _bio_create(BIO *b);
  44. static int _bio_destroy(BIO *b);
  45. static int _bio_read(BIO *b, char *buf, int len);
  46. static int _bio_write(BIO *b, const char *buf, int len);
  47. static long _bio_ctrl(BIO *b, int cmd, long num, void *ptr);
  48. static int _bio_gets(BIO *b, char *buf, int len);
  49. static int _bio_puts(BIO *b, const char *str);
  50. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  51. static BIO_METHOD *_bio_method;
  52. #else
  53. static BIO_METHOD _bio_method;
  54. #endif
  55. static BIO_METHOD *_get_bio_method();
  56. static bool _match_host_name(const char *name, const char *hostname);
  57. static Error _match_common_name(const char *hostname, const X509 *server_cert);
  58. static Error _match_subject_alternative_name(const char *hostname, const X509 *server_cert);
  59. static int _cert_verify_callback(X509_STORE_CTX *x509_ctx, void *arg);
  60. Status status;
  61. String hostname;
  62. int max_cert_chain_depth;
  63. SSL_CTX *ctx;
  64. SSL *ssl;
  65. BIO *bio;
  66. bool connected;
  67. int flags;
  68. bool use_blocking;
  69. bool validate_certs;
  70. bool validate_hostname;
  71. Ref<StreamPeer> base;
  72. static StreamPeerSSL *_create_func();
  73. void _print_error(int err);
  74. static Vector<X509 *> certs;
  75. static void _load_certs(const PoolByteArray &p_array);
  76. protected:
  77. static void _bind_methods();
  78. public:
  79. virtual Error accept_stream(Ref<StreamPeer> p_base);
  80. virtual Error connect_to_stream(Ref<StreamPeer> p_base, bool p_validate_certs = false, const String &p_for_hostname = String());
  81. virtual Status get_status() const;
  82. virtual void disconnect_from_stream();
  83. virtual Error put_data(const uint8_t *p_data, int p_bytes);
  84. virtual Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent);
  85. virtual Error get_data(uint8_t *p_buffer, int p_bytes);
  86. virtual Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received);
  87. virtual int get_available_bytes() const;
  88. static void initialize_ssl();
  89. static void finalize_ssl();
  90. StreamPeerOpenSSL();
  91. ~StreamPeerOpenSSL();
  92. };
  93. #endif // STREAM_PEER_SSL_H