OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ | 11 #ifndef WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ |
12 #define WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ | 12 #define WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ |
13 | 13 |
14 #include <string> | 14 #include <string> |
15 #include <memory> | 15 #include <memory> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "webrtc/base/buffer.h" | 18 #include "webrtc/base/buffer.h" |
19 #include "webrtc/base/sslstreamadapter.h" | 19 #include "webrtc/base/sslstreamadapter.h" |
20 #include "webrtc/base/opensslidentity.h" | 20 #include "webrtc/base/opensslidentity.h" |
21 | 21 |
22 typedef struct ssl_st SSL; | 22 typedef struct ssl_st SSL; |
23 typedef struct ssl_ctx_st SSL_CTX; | 23 typedef struct ssl_ctx_st SSL_CTX; |
24 typedef struct ssl_cipher_st SSL_CIPHER; | 24 typedef struct ssl_cipher_st SSL_CIPHER; |
25 typedef struct x509_store_ctx_st X509_STORE_CTX; | 25 typedef struct x509_store_ctx_st X509_STORE_CTX; |
26 | 26 |
27 namespace rtc { | 27 namespace rtc { |
28 | 28 |
29 // This class was written with OpenSSLAdapter (a socket adapter) as a | 29 // This class was written with OpenSSLAdapter (a socket adapter) as a |
30 // starting point. It has similar structure and functionality, with | 30 // starting point. It has similar structure and functionality, but uses a |
31 // the peer-to-peer mode added. | 31 // "peer-to-peer" mode, verifying the peer's certificate using a digest |
| 32 // sent over a secure signaling channel. |
32 // | 33 // |
33 // Static methods to initialize and deinit the SSL library are in | 34 // Static methods to initialize and deinit the SSL library are in |
34 // OpenSSLAdapter. This class also uses | 35 // OpenSSLAdapter. These should probably be moved out to a neutral class. |
35 // OpenSSLAdapter::custom_verify_callback_ (a static field). These | |
36 // should probably be moved out to a neutral class. | |
37 // | 36 // |
38 // In a few cases I have factored out some OpenSSLAdapter code into | 37 // In a few cases I have factored out some OpenSSLAdapter code into static |
39 // static methods so it can be reused from this class. Eventually that | 38 // methods so it can be reused from this class. Eventually that code should |
40 // code should probably be moved to a common support | 39 // probably be moved to a common support class. Unfortunately there remain a |
41 // class. Unfortunately there remain a few duplicated sections of | 40 // few duplicated sections of code. I have not done more restructuring because |
42 // code. I have not done more restructuring because I did not want to | 41 // I did not want to affect existing code that uses OpenSSLAdapter. |
43 // affect existing code that uses OpenSSLAdapter. | |
44 // | 42 // |
45 // This class does not support the SSL connection restart feature | 43 // This class does not support the SSL connection restart feature present in |
46 // present in OpenSSLAdapter. I am not entirely sure how the feature | 44 // OpenSSLAdapter. I am not entirely sure how the feature is useful and I am |
47 // is useful and I am not convinced that it works properly. | 45 // not convinced that it works properly. |
48 // | 46 // |
49 // This implementation is careful to disallow data exchange after an | 47 // This implementation is careful to disallow data exchange after an SSL error, |
50 // SSL error, and it has an explicit SSL_CLOSED state. It should not | 48 // and it has an explicit SSL_CLOSED state. It should not be possible to send |
51 // be possible to send any data in clear after one of the StartSSL | 49 // any data in clear after one of the StartSSL methods has been called. |
52 // methods has been called. | |
53 | 50 |
54 // Look in sslstreamadapter.h for documentation of the methods. | 51 // Look in sslstreamadapter.h for documentation of the methods. |
55 | 52 |
56 class OpenSSLIdentity; | 53 class OpenSSLIdentity; |
57 | 54 |
58 /////////////////////////////////////////////////////////////////////////////// | 55 /////////////////////////////////////////////////////////////////////////////// |
59 | 56 |
60 class OpenSSLStreamAdapter : public SSLStreamAdapter { | 57 class OpenSSLStreamAdapter : public SSLStreamAdapter { |
61 public: | 58 public: |
62 explicit OpenSSLStreamAdapter(StreamInterface* stream); | 59 explicit OpenSSLStreamAdapter(StreamInterface* stream); |
63 ~OpenSSLStreamAdapter() override; | 60 ~OpenSSLStreamAdapter() override; |
64 | 61 |
65 void SetIdentity(SSLIdentity* identity) override; | 62 void SetIdentity(SSLIdentity* identity) override; |
66 | 63 |
67 // Default argument is for compatibility | 64 // Default argument is for compatibility |
68 void SetServerRole(SSLRole role = SSL_SERVER) override; | 65 void SetServerRole(SSLRole role = SSL_SERVER) override; |
69 bool SetPeerCertificateDigest(const std::string& digest_alg, | 66 bool SetPeerCertificateDigest(const std::string& digest_alg, |
70 const unsigned char* digest_val, | 67 const unsigned char* digest_val, |
71 size_t digest_len) override; | 68 size_t digest_len) override; |
72 | 69 |
73 std::unique_ptr<SSLCertificate> GetPeerCertificate() const override; | 70 std::unique_ptr<SSLCertificate> GetPeerCertificate() const override; |
74 | 71 |
75 int StartSSLWithServer(const char* server_name) override; | 72 // Goes from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, depending |
76 int StartSSLWithPeer() override; | 73 // on whether the underlying stream is already open or not. |
| 74 int StartSSL() override; |
77 void SetMode(SSLMode mode) override; | 75 void SetMode(SSLMode mode) override; |
78 void SetMaxProtocolVersion(SSLProtocolVersion version) override; | 76 void SetMaxProtocolVersion(SSLProtocolVersion version) override; |
79 | 77 |
80 StreamResult Read(void* data, | 78 StreamResult Read(void* data, |
81 size_t data_len, | 79 size_t data_len, |
82 size_t* read, | 80 size_t* read, |
83 int* error) override; | 81 int* error) override; |
84 StreamResult Write(const void* data, | 82 StreamResult Write(const void* data, |
85 size_t data_len, | 83 size_t data_len, |
86 size_t* written, | 84 size_t* written, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 SSL_CLOSED // Clean close | 129 SSL_CLOSED // Clean close |
132 }; | 130 }; |
133 | 131 |
134 enum { MSG_TIMEOUT = MSG_MAX+1}; | 132 enum { MSG_TIMEOUT = MSG_MAX+1}; |
135 | 133 |
136 // The following three methods return 0 on success and a negative | 134 // The following three methods return 0 on success and a negative |
137 // error code on failure. The error code may be from OpenSSL or -1 | 135 // error code on failure. The error code may be from OpenSSL or -1 |
138 // on some other error cases, so it can't really be interpreted | 136 // on some other error cases, so it can't really be interpreted |
139 // unfortunately. | 137 // unfortunately. |
140 | 138 |
141 // Go from state SSL_NONE to either SSL_CONNECTING or SSL_WAIT, | |
142 // depending on whether the underlying stream is already open or | |
143 // not. | |
144 int StartSSL(); | |
145 // Prepare SSL library, state is SSL_CONNECTING. | 139 // Prepare SSL library, state is SSL_CONNECTING. |
146 int BeginSSL(); | 140 int BeginSSL(); |
147 // Perform SSL negotiation steps. | 141 // Perform SSL negotiation steps. |
148 int ContinueSSL(); | 142 int ContinueSSL(); |
149 | 143 |
150 // Error handler helper. signal is given as true for errors in | 144 // Error handler helper. signal is given as true for errors in |
151 // asynchronous contexts (when an error method was not returned | 145 // asynchronous contexts (when an error method was not returned |
152 // through some other method), and in that case an SE_CLOSE event is | 146 // through some other method), and in that case an SE_CLOSE event is |
153 // raised on the stream with the specified error. | 147 // raised on the stream with the specified error. |
154 // A 0 error means a graceful close, otherwise there is not really enough | 148 // A 0 error means a graceful close, otherwise there is not really enough |
155 // context to interpret the error code. | 149 // context to interpret the error code. |
156 void Error(const char* context, int err, bool signal); | 150 void Error(const char* context, int err, bool signal); |
157 void Cleanup(); | 151 void Cleanup(); |
158 | 152 |
159 // Override MessageHandler | 153 // Override MessageHandler |
160 void OnMessage(Message* msg) override; | 154 void OnMessage(Message* msg) override; |
161 | 155 |
162 // Flush the input buffers by reading left bytes (for DTLS) | 156 // Flush the input buffers by reading left bytes (for DTLS) |
163 void FlushInput(unsigned int left); | 157 void FlushInput(unsigned int left); |
164 | 158 |
165 // SSL library configuration | 159 // SSL library configuration |
166 SSL_CTX* SetupSSLContext(); | 160 SSL_CTX* SetupSSLContext(); |
167 // SSL verification check | 161 // SSL verification check |
168 bool SSLPostConnectionCheck(SSL* ssl, const char* server_name, | 162 bool SSLPostConnectionCheck(SSL* ssl, |
169 const X509* peer_cert, | 163 const X509* peer_cert, |
170 const std::string& peer_digest); | 164 const std::string& peer_digest); |
171 // SSL certification verification error handler, called back from | 165 // SSL certification verification error handler, called back from |
172 // the openssl library. Returns an int interpreted as a boolean in | 166 // the openssl library. Returns an int interpreted as a boolean in |
173 // the C style: zero means verification failure, non-zero means | 167 // the C style: zero means verification failure, non-zero means |
174 // passed. | 168 // passed. |
175 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store); | 169 static int SSLVerifyCallback(int ok, X509_STORE_CTX* store); |
176 | 170 |
177 SSLState state_; | 171 SSLState state_; |
178 SSLRole role_; | 172 SSLRole role_; |
179 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED | 173 int ssl_error_code_; // valid when state_ == SSL_ERROR or SSL_CLOSED |
180 // Whether the SSL negotiation is blocked on needing to read or | 174 // Whether the SSL negotiation is blocked on needing to read or |
181 // write to the wrapped stream. | 175 // write to the wrapped stream. |
182 bool ssl_read_needs_write_; | 176 bool ssl_read_needs_write_; |
183 bool ssl_write_needs_read_; | 177 bool ssl_write_needs_read_; |
184 | 178 |
185 SSL* ssl_; | 179 SSL* ssl_; |
186 SSL_CTX* ssl_ctx_; | 180 SSL_CTX* ssl_ctx_; |
187 | 181 |
188 // Our key and certificate, mostly useful in peer-to-peer mode. | 182 // Our key and certificate. |
189 std::unique_ptr<OpenSSLIdentity> identity_; | 183 std::unique_ptr<OpenSSLIdentity> identity_; |
190 // in traditional mode, the server name that the server's certificate | 184 // The certificate that the peer presented. Initially null, until the |
191 // must specify. Empty in peer-to-peer mode. | 185 // connection is established. |
192 std::string ssl_server_name_; | |
193 // The certificate that the peer must present or did present. Initially | |
194 // null in traditional mode, until the connection is established. | |
195 std::unique_ptr<OpenSSLCertificate> peer_certificate_; | 186 std::unique_ptr<OpenSSLCertificate> peer_certificate_; |
196 // In peer-to-peer mode, the digest of the certificate that | 187 // The digest of the certificate that the peer must present. |
197 // the peer must present. | |
198 Buffer peer_certificate_digest_value_; | 188 Buffer peer_certificate_digest_value_; |
199 std::string peer_certificate_digest_algorithm_; | 189 std::string peer_certificate_digest_algorithm_; |
200 | 190 |
201 // OpenSSLAdapter::custom_verify_callback_ result | |
202 bool custom_verification_succeeded_; | |
203 | |
204 // The DtlsSrtp ciphers | 191 // The DtlsSrtp ciphers |
205 std::string srtp_ciphers_; | 192 std::string srtp_ciphers_; |
206 | 193 |
207 // Do DTLS or not | 194 // Do DTLS or not |
208 SSLMode ssl_mode_; | 195 SSLMode ssl_mode_; |
209 | 196 |
210 // Max. allowed protocol version | 197 // Max. allowed protocol version |
211 SSLProtocolVersion ssl_max_version_; | 198 SSLProtocolVersion ssl_max_version_; |
212 }; | 199 }; |
213 | 200 |
214 ///////////////////////////////////////////////////////////////////////////// | 201 ///////////////////////////////////////////////////////////////////////////// |
215 | 202 |
216 } // namespace rtc | 203 } // namespace rtc |
217 | 204 |
218 #endif // WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ | 205 #endif // WEBRTC_BASE_OPENSSLSTREAMADAPTER_H__ |
OLD | NEW |