henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2004--2008, Google Inc. |
| 4 | * |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 5 | * Redistribution and use in source and binary forms, with or without |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 8 | * 1. Redistributions of source code must retain the above copyright notice, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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. |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote products |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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, |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 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 |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TALK_BASE_SSLSTREAMADAPTERHELPER_H_ |
| 29 | #define TALK_BASE_SSLSTREAMADAPTERHELPER_H_ |
| 30 | |
| 31 | #include <string> |
| 32 | #include <vector> |
| 33 | |
| 34 | #include "talk/base/buffer.h" |
| 35 | #include "talk/base/stream.h" |
| 36 | #include "talk/base/sslidentity.h" |
| 37 | #include "talk/base/sslstreamadapter.h" |
| 38 | |
| 39 | namespace talk_base { |
| 40 | |
| 41 | // SSLStreamAdapterHelper : A stream adapter which implements much |
| 42 | // of the logic that is common between the known implementations |
| 43 | // (NSS and OpenSSL) |
| 44 | class SSLStreamAdapterHelper : public SSLStreamAdapter { |
| 45 | public: |
| 46 | explicit SSLStreamAdapterHelper(StreamInterface* stream) |
| 47 | : SSLStreamAdapter(stream), |
| 48 | state_(SSL_NONE), |
| 49 | role_(SSL_CLIENT), |
| 50 | ssl_error_code_(0), // Not meaningful yet |
| 51 | ssl_mode_(SSL_MODE_TLS) {} |
| 52 | |
| 53 | |
| 54 | // Overrides of SSLStreamAdapter |
| 55 | virtual void SetIdentity(SSLIdentity* identity); |
| 56 | virtual void SetServerRole(SSLRole role = SSL_SERVER); |
| 57 | virtual void SetMode(SSLMode mode); |
| 58 | |
| 59 | virtual int StartSSLWithServer(const char* server_name); |
| 60 | virtual int StartSSLWithPeer(); |
| 61 | |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 62 | virtual bool SetPeerCertificateDigest(const std::string& digest_alg, |
| 63 | const unsigned char* digest_val, |
| 64 | size_t digest_len); |
wu@webrtc.org | 62fe97f | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 65 | virtual bool GetPeerCertificate(SSLCertificate** cert) const; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | virtual StreamState GetState() const; |
| 67 | virtual void Close(); |
| 68 | |
| 69 | protected: |
| 70 | // Internal helper methods |
| 71 | // The following method returns 0 on success and a negative |
| 72 | // error code on failure. The error code may be either -1 or |
| 73 | // from the impl on some other error cases, so it can't really be |
| 74 | // interpreted unfortunately. |
| 75 | |
| 76 | // Perform SSL negotiation steps. |
| 77 | int ContinueSSL(); |
| 78 | |
| 79 | // Error handler helper. signal is given as true for errors in |
| 80 | // asynchronous contexts (when an error code was not returned |
| 81 | // through some other method), and in that case an SE_CLOSE event is |
| 82 | // raised on the stream with the specified error. |
| 83 | // A 0 error means a graceful close, otherwise there is not really enough |
| 84 | // context to interpret the error code. |
| 85 | virtual void Error(const char* context, int err, bool signal); |
| 86 | |
| 87 | // Must be implemented by descendents |
| 88 | virtual int BeginSSL() = 0; |
| 89 | virtual void Cleanup() = 0; |
pbos@webrtc.org | b951827 | 2014-03-07 15:22:04 +0000 | [diff] [blame] | 90 | virtual bool GetDigestLength(const std::string& algorithm, |
| 91 | size_t* length) = 0; |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 92 | |
| 93 | enum SSLState { |
| 94 | // Before calling one of the StartSSL methods, data flows |
| 95 | // in clear text. |
| 96 | SSL_NONE, |
| 97 | SSL_WAIT, // waiting for the stream to open to start SSL negotiation |
| 98 | SSL_CONNECTING, // SSL negotiation in progress |
| 99 | SSL_CONNECTED, // SSL stream successfully established |
| 100 | SSL_ERROR, // some SSL error occurred, stream is closed |
| 101 | SSL_CLOSED // Clean close |
| 102 | }; |
| 103 | |
| 104 | // MSG_MAX is the maximum generic stream message number. |
| 105 | enum { MSG_DTLS_TIMEOUT = MSG_MAX + 1 }; |
| 106 | |
| 107 | SSLState state_; |
| 108 | SSLRole role_; |
| 109 | int ssl_error_code_; // valid when state_ == SSL_ERROR |
| 110 | |
| 111 | // Our key and certificate, mostly useful in peer-to-peer mode. |
| 112 | scoped_ptr<SSLIdentity> identity_; |
| 113 | // in traditional mode, the server name that the server's certificate |
| 114 | // must specify. Empty in peer-to-peer mode. |
| 115 | std::string ssl_server_name_; |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 116 | // The peer's certificate. Only used for GetPeerCertificate. |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | scoped_ptr<SSLCertificate> peer_certificate_; |
| 118 | |
wu@webrtc.org | 2a81a38 | 2014-01-03 22:08:47 +0000 | [diff] [blame] | 119 | // The digest of the certificate that the peer must present. |
henrike@webrtc.org | 0e118e7 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 120 | Buffer peer_certificate_digest_value_; |
| 121 | std::string peer_certificate_digest_algorithm_; |
| 122 | |
| 123 | // Do DTLS or not |
| 124 | SSLMode ssl_mode_; |
| 125 | |
| 126 | private: |
| 127 | // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, |
| 128 | // depending on whether the underlying stream is already open or |
| 129 | // not. Returns 0 on success and a negative value on error. |
| 130 | int StartSSL(); |
| 131 | }; |
| 132 | |
| 133 | } // namespace talk_base |
| 134 | |
| 135 | #endif // TALK_BASE_SSLSTREAMADAPTERHELPER_H_ |