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.

137 lines
5.2 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_SSLSTREAMADAPTERHELPER_H_
  28. #define TALK_BASE_SSLSTREAMADAPTERHELPER_H_
  29. #include <string>
  30. #include <vector>
  31. #include "talk/base/buffer.h"
  32. #include "talk/base/stream.h"
  33. #include "talk/base/sslidentity.h"
  34. #include "talk/base/sslstreamadapter.h"
  35. namespace talk_base {
  36. // SSLStreamAdapterHelper : A stream adapter which implements much
  37. // of the logic that is common between the known implementations
  38. // (NSS and OpenSSL)
  39. class SSLStreamAdapterHelper : public SSLStreamAdapter {
  40. public:
  41. explicit SSLStreamAdapterHelper(StreamInterface* stream)
  42. : SSLStreamAdapter(stream),
  43. state_(SSL_NONE),
  44. role_(SSL_CLIENT),
  45. ssl_error_code_(0), // Not meaningful yet
  46. ssl_mode_(SSL_MODE_TLS) {}
  47. // Overrides of SSLStreamAdapter
  48. virtual void SetIdentity(SSLIdentity* identity);
  49. virtual void SetServerRole(SSLRole role = SSL_SERVER);
  50. virtual void SetMode(SSLMode mode);
  51. virtual int StartSSLWithServer(const char* server_name);
  52. virtual int StartSSLWithPeer();
  53. virtual void SetPeerCertificate(SSLCertificate* cert);
  54. virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
  55. const unsigned char* digest_val,
  56. size_t digest_len);
  57. virtual StreamState GetState() const;
  58. virtual void Close();
  59. protected:
  60. // Internal helper methods
  61. // The following method returns 0 on success and a negative
  62. // error code on failure. The error code may be either -1 or
  63. // from the impl on some other error cases, so it can't really be
  64. // interpreted unfortunately.
  65. // Perform SSL negotiation steps.
  66. int ContinueSSL();
  67. // Error handler helper. signal is given as true for errors in
  68. // asynchronous contexts (when an error code was not returned
  69. // through some other method), and in that case an SE_CLOSE event is
  70. // raised on the stream with the specified error.
  71. // A 0 error means a graceful close, otherwise there is not really enough
  72. // context to interpret the error code.
  73. virtual void Error(const char* context, int err, bool signal);
  74. // Must be implemented by descendents
  75. virtual int BeginSSL() = 0;
  76. virtual void Cleanup() = 0;
  77. virtual bool GetDigestLength(const std::string &algorithm,
  78. std::size_t *length) = 0;
  79. enum SSLState {
  80. // Before calling one of the StartSSL methods, data flows
  81. // in clear text.
  82. SSL_NONE,
  83. SSL_WAIT, // waiting for the stream to open to start SSL negotiation
  84. SSL_CONNECTING, // SSL negotiation in progress
  85. SSL_CONNECTED, // SSL stream successfully established
  86. SSL_ERROR, // some SSL error occurred, stream is closed
  87. SSL_CLOSED // Clean close
  88. };
  89. // MSG_MAX is the maximum generic stream message number.
  90. enum { MSG_DTLS_TIMEOUT = MSG_MAX + 1 };
  91. SSLState state_;
  92. SSLRole role_;
  93. int ssl_error_code_; // valid when state_ == SSL_ERROR
  94. // Our key and certificate, mostly useful in peer-to-peer mode.
  95. scoped_ptr<SSLIdentity> identity_;
  96. // in traditional mode, the server name that the server's certificate
  97. // must specify. Empty in peer-to-peer mode.
  98. std::string ssl_server_name_;
  99. // In peer-to-peer mode, the certificate that the peer must
  100. // present. Empty in traditional mode.
  101. scoped_ptr<SSLCertificate> peer_certificate_;
  102. // In peer-to-peer mode, the digest of the certificate that
  103. // the peer must present.
  104. Buffer peer_certificate_digest_value_;
  105. std::string peer_certificate_digest_algorithm_;
  106. // Do DTLS or not
  107. SSLMode ssl_mode_;
  108. private:
  109. // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT,
  110. // depending on whether the underlying stream is already open or
  111. // not. Returns 0 on success and a negative value on error.
  112. int StartSSL();
  113. };
  114. } // namespace talk_base
  115. #endif // TALK_BASE_SSLSTREAMADAPTERHELPER_H_