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.

185 lines
7.9 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_SSLSTREAMADAPTER_H__
  28. #define TALK_BASE_SSLSTREAMADAPTER_H__
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/stream.h"
  32. #include "talk/base/sslidentity.h"
  33. namespace talk_base {
  34. // SSLStreamAdapter : A StreamInterfaceAdapter that does SSL/TLS.
  35. // After SSL has been started, the stream will only open on successful
  36. // SSL verification of certificates, and the communication is
  37. // encrypted of course.
  38. //
  39. // This class was written with SSLAdapter as a starting point. It
  40. // offers a similar interface, with two differences: there is no
  41. // support for a restartable SSL connection, and this class has a
  42. // peer-to-peer mode.
  43. //
  44. // The SSL library requires initialization and cleanup. Static method
  45. // for doing this are in SSLAdapter. They should possibly be moved out
  46. // to a neutral class.
  47. enum SSLRole { SSL_CLIENT, SSL_SERVER };
  48. enum SSLMode { SSL_MODE_TLS, SSL_MODE_DTLS };
  49. // Errors for Read -- in the high range so no conflict with OpenSSL.
  50. enum { SSE_MSG_TRUNC = 0xff0001 };
  51. class SSLStreamAdapter : public StreamAdapterInterface {
  52. public:
  53. // Instantiate an SSLStreamAdapter wrapping the given stream,
  54. // (using the selected implementation for the platform).
  55. // Caller is responsible for freeing the returned object.
  56. static SSLStreamAdapter* Create(StreamInterface* stream);
  57. explicit SSLStreamAdapter(StreamInterface* stream)
  58. : StreamAdapterInterface(stream), ignore_bad_cert_(false) { }
  59. void set_ignore_bad_cert(bool ignore) { ignore_bad_cert_ = ignore; }
  60. bool ignore_bad_cert() const { return ignore_bad_cert_; }
  61. // Specify our SSL identity: key and certificate. Mostly this is
  62. // only used in the peer-to-peer mode (unless we actually want to
  63. // provide a client certificate to a server).
  64. // SSLStream takes ownership of the SSLIdentity object and will
  65. // free it when appropriate. Should be called no more than once on a
  66. // given SSLStream instance.
  67. virtual void SetIdentity(SSLIdentity* identity) = 0;
  68. // Call this to indicate that we are to play the server's role in
  69. // the peer-to-peer mode.
  70. // The default argument is for backward compatibility
  71. // TODO(ekr@rtfm.com): rename this SetRole to reflect its new function
  72. virtual void SetServerRole(SSLRole role = SSL_SERVER) = 0;
  73. // Do DTLS or TLS
  74. virtual void SetMode(SSLMode mode) = 0;
  75. // The mode of operation is selected by calling either
  76. // StartSSLWithServer or StartSSLWithPeer.
  77. // Use of the stream prior to calling either of these functions will
  78. // pass data in clear text.
  79. // Calling one of these functions causes SSL negotiation to begin as
  80. // soon as possible: right away if the underlying wrapped stream is
  81. // already opened, or else as soon as it opens.
  82. //
  83. // These functions return a negative error code on failure.
  84. // Returning 0 means success so far, but negotiation is probably not
  85. // complete and will continue asynchronously. In that case, the
  86. // exposed stream will open after successful negotiation and
  87. // verification, or an SE_CLOSE event will be raised if negotiation
  88. // fails.
  89. // StartSSLWithServer starts SSL negotiation with a server in
  90. // traditional mode. server_name specifies the expected server name
  91. // which the server's certificate needs to specify.
  92. virtual int StartSSLWithServer(const char* server_name) = 0;
  93. // StartSSLWithPeer starts negotiation in the special peer-to-peer
  94. // mode.
  95. // Generally, SetIdentity() and possibly SetServerRole() should have
  96. // been called before this.
  97. // SetPeerCertificate() must also be called. It may be called after
  98. // StartSSLWithPeer() but must be called before the underlying
  99. // stream opens.
  100. virtual int StartSSLWithPeer() = 0;
  101. // Specify the certificate that our peer is expected to use in
  102. // peer-to-peer mode. Only this certificate will be accepted during
  103. // SSL verification. The certificate is assumed to have been
  104. // obtained through some other secure channel (such as the XMPP
  105. // channel). (This could also specify the certificate authority that
  106. // will sign the peer's certificate.)
  107. // SSLStream takes ownership of the SSLCertificate object and will
  108. // free it when appropriate. Should be called no more than once on a
  109. // given SSLStream instance.
  110. virtual void SetPeerCertificate(SSLCertificate* cert) = 0;
  111. // Specify the digest of the certificate that our peer is expected to use in
  112. // peer-to-peer mode. Only this certificate will be accepted during
  113. // SSL verification. The certificate is assumed to have been
  114. // obtained through some other secure channel (such as the XMPP
  115. // channel). Unlike SetPeerCertificate(), this must specify the
  116. // terminal certificate, not just a CA.
  117. // SSLStream makes a copy of the digest value.
  118. virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
  119. const unsigned char* digest_val,
  120. size_t digest_len) = 0;
  121. // Key Exporter interface from RFC 5705
  122. // Arguments are:
  123. // label -- the exporter label.
  124. // part of the RFC defining each exporter
  125. // usage (IN)
  126. // context/context_len -- a context to bind to for this connection;
  127. // optional, can be NULL, 0 (IN)
  128. // use_context -- whether to use the context value
  129. // (needed to distinguish no context from
  130. // zero-length ones).
  131. // result -- where to put the computed value
  132. // result_len -- the length of the computed value
  133. virtual bool ExportKeyingMaterial(const std::string& label,
  134. const uint8* context,
  135. size_t context_len,
  136. bool use_context,
  137. uint8* result,
  138. size_t result_len) {
  139. return false; // Default is unsupported
  140. }
  141. // DTLS-SRTP interface
  142. virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers) {
  143. return false;
  144. }
  145. virtual bool GetDtlsSrtpCipher(std::string* cipher) {
  146. return false;
  147. }
  148. // Capabilities testing
  149. static bool HaveDtls();
  150. static bool HaveDtlsSrtp();
  151. static bool HaveExporter();
  152. // If true, the server certificate need not match the configured
  153. // server_name, and in fact missing certificate authority and other
  154. // verification errors are ignored.
  155. bool ignore_bad_cert_;
  156. };
  157. } // namespace talk_base
  158. #endif // TALK_BASE_SSLSTREAMADAPTER_H__