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