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.

215 lines
8.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2008, 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_OPENSSLSTREAMADAPTER_H__
  28. #define TALK_BASE_OPENSSLSTREAMADAPTER_H__
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/buffer.h"
  32. #include "talk/base/sslstreamadapter.h"
  33. #include "talk/base/opensslidentity.h"
  34. typedef struct ssl_st SSL;
  35. typedef struct ssl_ctx_st SSL_CTX;
  36. typedef struct x509_store_ctx_st X509_STORE_CTX;
  37. namespace talk_base {
  38. // This class was written with OpenSSLAdapter (a socket adapter) as a
  39. // starting point. It has similar structure and functionality, with
  40. // the peer-to-peer mode added.
  41. //
  42. // Static methods to initialize and deinit the SSL library are in
  43. // OpenSSLAdapter. This class also uses
  44. // OpenSSLAdapter::custom_verify_callback_ (a static field). These
  45. // should probably be moved out to a neutral class.
  46. //
  47. // In a few cases I have factored out some OpenSSLAdapter code into
  48. // static methods so it can be reused from this class. Eventually that
  49. // code should probably be moved to a common support
  50. // class. Unfortunately there remain a few duplicated sections of
  51. // code. I have not done more restructuring because I did not want to
  52. // affect existing code that uses OpenSSLAdapter.
  53. //
  54. // This class does not support the SSL connection restart feature
  55. // present in OpenSSLAdapter. I am not entirely sure how the feature
  56. // is useful and I am not convinced that it works properly.
  57. //
  58. // This implementation is careful to disallow data exchange after an
  59. // SSL error, and it has an explicit SSL_CLOSED state. It should not
  60. // be possible to send any data in clear after one of the StartSSL
  61. // methods has been called.
  62. // Look in sslstreamadapter.h for documentation of the methods.
  63. class OpenSSLIdentity;
  64. ///////////////////////////////////////////////////////////////////////////////
  65. class OpenSSLStreamAdapter : public SSLStreamAdapter {
  66. public:
  67. explicit OpenSSLStreamAdapter(StreamInterface* stream);
  68. virtual ~OpenSSLStreamAdapter();
  69. virtual void SetIdentity(SSLIdentity* identity);
  70. // Default argument is for compatibility
  71. virtual void SetServerRole(SSLRole role = SSL_SERVER);
  72. virtual void SetPeerCertificate(SSLCertificate* cert);
  73. virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
  74. const unsigned char* digest_val,
  75. size_t digest_len);
  76. virtual int StartSSLWithServer(const char* server_name);
  77. virtual int StartSSLWithPeer();
  78. virtual void SetMode(SSLMode mode);
  79. virtual StreamResult Read(void* data, size_t data_len,
  80. size_t* read, int* error);
  81. virtual StreamResult Write(const void* data, size_t data_len,
  82. size_t* written, int* error);
  83. virtual void Close();
  84. virtual StreamState GetState() const;
  85. // Key Extractor interface
  86. virtual bool ExportKeyingMaterial(const std::string& label,
  87. const uint8* context,
  88. size_t context_len,
  89. bool use_context,
  90. uint8* result,
  91. size_t result_len);
  92. // DTLS-SRTP interface
  93. virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
  94. virtual bool GetDtlsSrtpCipher(std::string* cipher);
  95. // Capabilities interfaces
  96. static bool HaveDtls();
  97. static bool HaveDtlsSrtp();
  98. static bool HaveExporter();
  99. protected:
  100. virtual void OnEvent(StreamInterface* stream, int events, int err);
  101. private:
  102. enum SSLState {
  103. // Before calling one of the StartSSL methods, data flows
  104. // in clear text.
  105. SSL_NONE,
  106. SSL_WAIT, // waiting for the stream to open to start SSL negotiation
  107. SSL_CONNECTING, // SSL negotiation in progress
  108. SSL_CONNECTED, // SSL stream successfully established
  109. SSL_ERROR, // some SSL error occurred, stream is closed
  110. SSL_CLOSED // Clean close
  111. };
  112. enum { MSG_TIMEOUT = MSG_MAX+1};
  113. // The following three methods return 0 on success and a negative
  114. // error code on failure. The error code may be from OpenSSL or -1
  115. // on some other error cases, so it can't really be interpreted
  116. // unfortunately.
  117. // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT,
  118. // depending on whether the underlying stream is already open or
  119. // not.
  120. int StartSSL();
  121. // Prepare SSL library, state is SSL_CONNECTING.
  122. int BeginSSL();
  123. // Perform SSL negotiation steps.
  124. int ContinueSSL();
  125. // Error handler helper. signal is given as true for errors in
  126. // asynchronous contexts (when an error method was not returned
  127. // through some other method), and in that case an SE_CLOSE event is
  128. // raised on the stream with the specified error.
  129. // A 0 error means a graceful close, otherwise there is not really enough
  130. // context to interpret the error code.
  131. void Error(const char* context, int err, bool signal);
  132. void Cleanup();
  133. // Override MessageHandler
  134. virtual void OnMessage(Message* msg);
  135. // Flush the input buffers by reading left bytes (for DTLS)
  136. void FlushInput(unsigned int left);
  137. // SSL library configuration
  138. SSL_CTX* SetupSSLContext();
  139. // SSL verification check
  140. bool SSLPostConnectionCheck(SSL* ssl, const char* server_name,
  141. const X509* peer_cert,
  142. const std::string& peer_digest);
  143. // SSL certification verification error handler, called back from
  144. // the openssl library. Returns an int interpreted as a boolean in
  145. // the C style: zero means verification failure, non-zero means
  146. // passed.
  147. static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
  148. SSLState state_;
  149. SSLRole role_;
  150. int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
  151. // Whether the SSL negotiation is blocked on needing to read or
  152. // write to the wrapped stream.
  153. bool ssl_read_needs_write_;
  154. bool ssl_write_needs_read_;
  155. SSL* ssl_;
  156. SSL_CTX* ssl_ctx_;
  157. // Our key and certificate, mostly useful in peer-to-peer mode.
  158. scoped_ptr<OpenSSLIdentity> identity_;
  159. // in traditional mode, the server name that the server's certificate
  160. // must specify. Empty in peer-to-peer mode.
  161. std::string ssl_server_name_;
  162. // In peer-to-peer mode, the certificate that the peer must
  163. // present. Empty in traditional mode.
  164. scoped_ptr<OpenSSLCertificate> peer_certificate_;
  165. // In peer-to-peer mode, the digest of the certificate that
  166. // the peer must present.
  167. Buffer peer_certificate_digest_value_;
  168. std::string peer_certificate_digest_algorithm_;
  169. // OpenSSLAdapter::custom_verify_callback_ result
  170. bool custom_verification_succeeded_;
  171. // The DtlsSrtp ciphers
  172. std::string srtp_ciphers_;
  173. // Do DTLS or not
  174. SSLMode ssl_mode_;
  175. };
  176. /////////////////////////////////////////////////////////////////////////////
  177. } // namespace talk_base
  178. #endif // TALK_BASE_OPENSSLSTREAMADAPTER_H__