3 * Copyright 2004--2008, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_OPENSSLSTREAMADAPTER_H__
29 #define TALK_BASE_OPENSSLSTREAMADAPTER_H__
34 #include "talk/base/buffer.h"
35 #include "talk/base/sslstreamadapter.h"
36 #include "talk/base/opensslidentity.h"
38 typedef struct ssl_st SSL;
39 typedef struct ssl_ctx_st SSL_CTX;
40 typedef struct x509_store_ctx_st X509_STORE_CTX;
44 // This class was written with OpenSSLAdapter (a socket adapter) as a
45 // starting point. It has similar structure and functionality, with
46 // the peer-to-peer mode added.
48 // Static methods to initialize and deinit the SSL library are in
49 // OpenSSLAdapter. This class also uses
50 // OpenSSLAdapter::custom_verify_callback_ (a static field). These
51 // should probably be moved out to a neutral class.
53 // In a few cases I have factored out some OpenSSLAdapter code into
54 // static methods so it can be reused from this class. Eventually that
55 // code should probably be moved to a common support
56 // class. Unfortunately there remain a few duplicated sections of
57 // code. I have not done more restructuring because I did not want to
58 // affect existing code that uses OpenSSLAdapter.
60 // This class does not support the SSL connection restart feature
61 // present in OpenSSLAdapter. I am not entirely sure how the feature
62 // is useful and I am not convinced that it works properly.
64 // This implementation is careful to disallow data exchange after an
65 // SSL error, and it has an explicit SSL_CLOSED state. It should not
66 // be possible to send any data in clear after one of the StartSSL
67 // methods has been called.
69 // Look in sslstreamadapter.h for documentation of the methods.
71 class OpenSSLIdentity;
73 ///////////////////////////////////////////////////////////////////////////////
75 class OpenSSLStreamAdapter : public SSLStreamAdapter {
77 explicit OpenSSLStreamAdapter(StreamInterface* stream);
78 virtual ~OpenSSLStreamAdapter();
80 virtual void SetIdentity(SSLIdentity* identity);
82 // Default argument is for compatibility
83 virtual void SetServerRole(SSLRole role = SSL_SERVER);
84 virtual void SetPeerCertificate(SSLCertificate* cert);
85 virtual bool SetPeerCertificateDigest(const std::string& digest_alg,
86 const unsigned char* digest_val,
89 virtual bool GetPeerCertificate(SSLCertificate** cert) const;
91 virtual int StartSSLWithServer(const char* server_name);
92 virtual int StartSSLWithPeer();
93 virtual void SetMode(SSLMode mode);
95 virtual StreamResult Read(void* data, size_t data_len,
96 size_t* read, int* error);
97 virtual StreamResult Write(const void* data, size_t data_len,
98 size_t* written, int* error);
100 virtual StreamState GetState() const;
102 // Key Extractor interface
103 virtual bool ExportKeyingMaterial(const std::string& label,
104 const uint8* context,
111 // DTLS-SRTP interface
112 virtual bool SetDtlsSrtpCiphers(const std::vector<std::string>& ciphers);
113 virtual bool GetDtlsSrtpCipher(std::string* cipher);
115 // Capabilities interfaces
116 static bool HaveDtls();
117 static bool HaveDtlsSrtp();
118 static bool HaveExporter();
121 virtual void OnEvent(StreamInterface* stream, int events, int err);
125 // Before calling one of the StartSSL methods, data flows
128 SSL_WAIT, // waiting for the stream to open to start SSL negotiation
129 SSL_CONNECTING, // SSL negotiation in progress
130 SSL_CONNECTED, // SSL stream successfully established
131 SSL_ERROR, // some SSL error occurred, stream is closed
132 SSL_CLOSED // Clean close
135 enum { MSG_TIMEOUT = MSG_MAX+1};
137 // The following three methods return 0 on success and a negative
138 // error code on failure. The error code may be from OpenSSL or -1
139 // on some other error cases, so it can't really be interpreted
142 // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT,
143 // depending on whether the underlying stream is already open or
146 // Prepare SSL library, state is SSL_CONNECTING.
148 // Perform SSL negotiation steps.
151 // Error handler helper. signal is given as true for errors in
152 // asynchronous contexts (when an error method was not returned
153 // through some other method), and in that case an SE_CLOSE event is
154 // raised on the stream with the specified error.
155 // A 0 error means a graceful close, otherwise there is not really enough
156 // context to interpret the error code.
157 void Error(const char* context, int err, bool signal);
160 // Override MessageHandler
161 virtual void OnMessage(Message* msg);
163 // Flush the input buffers by reading left bytes (for DTLS)
164 void FlushInput(unsigned int left);
166 // SSL library configuration
167 SSL_CTX* SetupSSLContext();
168 // SSL verification check
169 bool SSLPostConnectionCheck(SSL* ssl, const char* server_name,
170 const X509* peer_cert,
171 const std::string& peer_digest);
172 // SSL certification verification error handler, called back from
173 // the openssl library. Returns an int interpreted as a boolean in
174 // the C style: zero means verification failure, non-zero means
176 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store);
181 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED
182 // Whether the SSL negotiation is blocked on needing to read or
183 // write to the wrapped stream.
184 bool ssl_read_needs_write_;
185 bool ssl_write_needs_read_;
190 // Our key and certificate, mostly useful in peer-to-peer mode.
191 scoped_ptr<OpenSSLIdentity> identity_;
192 // in traditional mode, the server name that the server's certificate
193 // must specify. Empty in peer-to-peer mode.
194 std::string ssl_server_name_;
195 // The certificate that the peer must present or did present. Initially
196 // null in traditional mode, until the connection is established.
197 scoped_ptr<OpenSSLCertificate> peer_certificate_;
198 // In peer-to-peer mode, the digest of the certificate that
199 // the peer must present.
200 Buffer peer_certificate_digest_value_;
201 std::string peer_certificate_digest_algorithm_;
203 // OpenSSLAdapter::custom_verify_callback_ result
204 bool custom_verification_succeeded_;
206 // The DtlsSrtp ciphers
207 std::string srtp_ciphers_;
213 /////////////////////////////////////////////////////////////////////////////
215 } // namespace talk_base
217 #endif // TALK_BASE_OPENSSLSTREAMADAPTER_H__