You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_OPENSSLADAPTER_H__
  28. #define TALK_BASE_OPENSSLADAPTER_H__
  29. #include <string>
  30. #include "talk/base/ssladapter.h"
  31. typedef struct ssl_st SSL;
  32. typedef struct ssl_ctx_st SSL_CTX;
  33. typedef struct x509_store_ctx_st X509_STORE_CTX;
  34. namespace talk_base {
  35. ///////////////////////////////////////////////////////////////////////////////
  36. class OpenSSLAdapter : public SSLAdapter {
  37. public:
  38. static bool InitializeSSL(VerificationCallback callback);
  39. static bool InitializeSSLThread();
  40. static bool CleanupSSL();
  41. OpenSSLAdapter(AsyncSocket* socket);
  42. virtual ~OpenSSLAdapter();
  43. virtual int StartSSL(const char* hostname, bool restartable);
  44. virtual int Send(const void* pv, size_t cb);
  45. virtual int Recv(void* pv, size_t cb);
  46. virtual int Close();
  47. // Note that the socket returns ST_CONNECTING while SSL is being negotiated.
  48. virtual ConnState GetState() const;
  49. protected:
  50. virtual void OnConnectEvent(AsyncSocket* socket);
  51. virtual void OnReadEvent(AsyncSocket* socket);
  52. virtual void OnWriteEvent(AsyncSocket* socket);
  53. virtual void OnCloseEvent(AsyncSocket* socket, int err);
  54. private:
  55. enum SSLState {
  56. SSL_NONE, SSL_WAIT, SSL_CONNECTING, SSL_CONNECTED, SSL_ERROR
  57. };
  58. int BeginSSL();
  59. int ContinueSSL();
  60. void Error(const char* context, int err, bool signal = true);
  61. void Cleanup();
  62. static bool VerifyServerName(SSL* ssl, const char* host,
  63. bool ignore_bad_cert);
  64. bool SSLPostConnectionCheck(SSL* ssl, const char* host);
  65. #if _DEBUG
  66. static void SSLInfoCallback(const SSL* s, int where, int ret);
  67. #endif // !_DEBUG
  68. static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
  69. static VerificationCallback custom_verify_callback_;
  70. friend class OpenSSLStreamAdapter; // for custom_verify_callback_;
  71. static bool ConfigureTrustedRootCertificates(SSL_CTX* ctx);
  72. static SSL_CTX* SetupSSLContext();
  73. SSLState state_;
  74. bool ssl_read_needs_write_;
  75. bool ssl_write_needs_read_;
  76. // If true, socket will retain SSL configuration after Close.
  77. bool restartable_;
  78. SSL* ssl_;
  79. SSL_CTX* ssl_ctx_;
  80. std::string ssl_host_name_;
  81. bool custom_verification_succeeded_;
  82. };
  83. /////////////////////////////////////////////////////////////////////////////
  84. } // namespace talk_base
  85. #endif // TALK_BASE_OPENSSLADAPTER_H__