| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ | 5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ |
| 6 #define NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ | 6 #define NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ |
| 7 | 7 |
| 8 #include <openssl/base.h> | 8 #include <openssl/base.h> |
| 9 #include <openssl/ssl.h> | 9 #include <openssl/ssl.h> |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <string> | 14 #include <string> |
| 15 #include <vector> | 15 #include <vector> |
| 16 | 16 |
| 17 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 18 #include "base/containers/mru_cache.h" | 18 #include "base/containers/mru_cache.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
| 22 #include "net/base/completion_callback.h" | 22 #include "net/base/completion_callback.h" |
| 23 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 24 #include "net/cert/cert_verifier.h" | 24 #include "net/cert/cert_verifier.h" |
| 25 #include "net/cert/cert_verify_result.h" | 25 #include "net/cert/cert_verify_result.h" |
| 26 #include "net/cert/ct_verify_result.h" | 26 #include "net/cert/ct_verify_result.h" |
| 27 #include "net/log/net_log_with_source.h" | 27 #include "net/log/net_log_with_source.h" |
| 28 #include "net/socket/client_socket_handle.h" | 28 #include "net/socket/client_socket_handle.h" |
| 29 #include "net/socket/socket_bio_adapter.h" |
| 29 #include "net/socket/ssl_client_socket.h" | 30 #include "net/socket/ssl_client_socket.h" |
| 30 #include "net/ssl/channel_id_service.h" | 31 #include "net/ssl/channel_id_service.h" |
| 31 #include "net/ssl/openssl_ssl_util.h" | 32 #include "net/ssl/openssl_ssl_util.h" |
| 32 #include "net/ssl/ssl_client_cert_type.h" | 33 #include "net/ssl/ssl_client_cert_type.h" |
| 33 #include "net/ssl/ssl_config_service.h" | 34 #include "net/ssl/ssl_config_service.h" |
| 34 | 35 |
| 35 namespace base { | 36 namespace base { |
| 36 class FilePath; | 37 class FilePath; |
| 37 class SequencedTaskRunner; | 38 class SequencedTaskRunner; |
| 38 } | 39 } |
| 39 | 40 |
| 40 namespace crypto { | 41 namespace crypto { |
| 41 class OpenSSLErrStackTracer; | 42 class OpenSSLErrStackTracer; |
| 42 } | 43 } |
| 43 | 44 |
| 44 namespace net { | 45 namespace net { |
| 45 | 46 |
| 46 class CertVerifier; | 47 class CertVerifier; |
| 47 class CTVerifier; | 48 class CTVerifier; |
| 49 class SocketBIOAdapter; |
| 48 class SSLCertRequestInfo; | 50 class SSLCertRequestInfo; |
| 49 class SSLInfo; | 51 class SSLInfo; |
| 50 | 52 |
| 51 using TokenBindingSignatureMap = | 53 using TokenBindingSignatureMap = |
| 52 base::MRUCache<std::pair<TokenBindingType, std::string>, | 54 base::MRUCache<std::pair<TokenBindingType, std::string>, |
| 53 std::vector<uint8_t>>; | 55 std::vector<uint8_t>>; |
| 54 | 56 |
| 55 class SSLClientSocketImpl : public SSLClientSocket { | 57 class SSLClientSocketImpl : public SSLClientSocket, |
| 58 public SocketBIOAdapter::Delegate { |
| 56 public: | 59 public: |
| 57 // Takes ownership of the transport_socket, which may already be connected. | 60 // Takes ownership of the transport_socket, which may already be connected. |
| 58 // The given hostname will be compared with the name(s) in the server's | 61 // The given hostname will be compared with the name(s) in the server's |
| 59 // certificate during the SSL handshake. ssl_config specifies the SSL | 62 // certificate during the SSL handshake. ssl_config specifies the SSL |
| 60 // settings. | 63 // settings. |
| 61 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket, | 64 SSLClientSocketImpl(std::unique_ptr<ClientSocketHandle> transport_socket, |
| 62 const HostPortPair& host_and_port, | 65 const HostPortPair& host_and_port, |
| 63 const SSLConfig& ssl_config, | 66 const SSLConfig& ssl_config, |
| 64 const SSLClientSocketContext& context); | 67 const SSLClientSocketContext& context); |
| 65 ~SSLClientSocketImpl() override; | 68 ~SSLClientSocketImpl() override; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 // Socket implementation. | 117 // Socket implementation. |
| 115 int Read(IOBuffer* buf, | 118 int Read(IOBuffer* buf, |
| 116 int buf_len, | 119 int buf_len, |
| 117 const CompletionCallback& callback) override; | 120 const CompletionCallback& callback) override; |
| 118 int Write(IOBuffer* buf, | 121 int Write(IOBuffer* buf, |
| 119 int buf_len, | 122 int buf_len, |
| 120 const CompletionCallback& callback) override; | 123 const CompletionCallback& callback) override; |
| 121 int SetReceiveBufferSize(int32_t size) override; | 124 int SetReceiveBufferSize(int32_t size) override; |
| 122 int SetSendBufferSize(int32_t size) override; | 125 int SetSendBufferSize(int32_t size) override; |
| 123 | 126 |
| 127 // SocketBIOAdapter implementation: |
| 128 void OnReadReady() override; |
| 129 void OnWriteReady() override; |
| 130 |
| 124 private: | 131 private: |
| 125 class PeerCertificateChain; | 132 class PeerCertificateChain; |
| 126 class SSLContext; | 133 class SSLContext; |
| 127 friend class SSLClientSocket; | 134 friend class SSLClientSocket; |
| 128 friend class SSLContext; | 135 friend class SSLContext; |
| 129 | 136 |
| 130 int Init(); | 137 int Init(); |
| 131 void DoReadCallback(int result); | 138 void DoReadCallback(int result); |
| 132 void DoWriteCallback(int result); | 139 void DoWriteCallback(int result); |
| 133 | 140 |
| 134 bool DoTransportIO(); | |
| 135 int DoHandshake(); | 141 int DoHandshake(); |
| 136 int DoHandshakeComplete(int result); | 142 int DoHandshakeComplete(int result); |
| 137 int DoChannelIDLookup(); | 143 int DoChannelIDLookup(); |
| 138 int DoChannelIDLookupComplete(int result); | 144 int DoChannelIDLookupComplete(int result); |
| 139 int DoVerifyCert(int result); | 145 int DoVerifyCert(int result); |
| 140 int DoVerifyCertComplete(int result); | 146 int DoVerifyCertComplete(int result); |
| 141 void DoConnectCallback(int result); | 147 void DoConnectCallback(int result); |
| 142 void UpdateServerCert(); | 148 void UpdateServerCert(); |
| 143 | 149 |
| 144 void OnHandshakeIOComplete(int result); | 150 void OnHandshakeIOComplete(int result); |
| 145 void OnSendComplete(int result); | |
| 146 void OnRecvComplete(int result); | |
| 147 | 151 |
| 148 int DoHandshakeLoop(int last_io_result); | 152 int DoHandshakeLoop(int last_io_result); |
| 149 int DoReadLoop(); | |
| 150 int DoWriteLoop(); | |
| 151 int DoPayloadRead(); | 153 int DoPayloadRead(); |
| 152 int DoPayloadWrite(); | 154 int DoPayloadWrite(); |
| 153 | 155 |
| 154 // Called when an asynchronous event completes which may have blocked the | 156 // Called when an asynchronous event completes which may have blocked the |
| 155 // pending Read or Write calls, if any. Retries both state machines and, if | 157 // pending Connect, Read or Write calls, if any. Retries all state machines |
| 156 // complete, runs the respective callbacks. | 158 // and, if complete, runs the respective callbacks. |
| 157 void PumpReadWriteEvents(); | 159 void RetryAllOperations(); |
| 158 | 160 |
| 159 int BufferSend(); | |
| 160 int BufferRecv(); | |
| 161 void BufferSendComplete(int result); | |
| 162 void BufferRecvComplete(int result); | |
| 163 void TransportWriteComplete(int result); | |
| 164 int TransportReadComplete(int result); | |
| 165 int VerifyCT(); | 161 int VerifyCT(); |
| 166 | 162 |
| 167 // Callback from the SSL layer that indicates the remote server is requesting | 163 // Callback from the SSL layer that indicates the remote server is requesting |
| 168 // a certificate for this client. | 164 // a certificate for this client. |
| 169 int ClientCertRequestCallback(SSL* ssl); | 165 int ClientCertRequestCallback(SSL* ssl); |
| 170 | 166 |
| 171 // CertVerifyCallback is called to verify the server's certificates. We do | 167 // CertVerifyCallback is called to verify the server's certificates. We do |
| 172 // verification after the handshake so this function only enforces that the | 168 // verification after the handshake so this function only enforces that the |
| 173 // certificates don't change during renegotiation. | 169 // certificates don't change during renegotiation. |
| 174 int CertVerifyCallback(X509_STORE_CTX* store_ctx); | 170 int CertVerifyCallback(X509_STORE_CTX* store_ctx); |
| 175 | 171 |
| 176 // Called during an operation on |transport_bio_|'s peer. Checks saved | |
| 177 // transport error state and, if appropriate, returns an error through | |
| 178 // OpenSSL's error system. | |
| 179 long MaybeReplayTransportError(BIO* bio, | |
| 180 int cmd, | |
| 181 const char* argp, | |
| 182 int argi, | |
| 183 long argl, | |
| 184 long retvalue); | |
| 185 | |
| 186 // Callback from the SSL layer when an operation is performed on | |
| 187 // |transport_bio_|'s peer. | |
| 188 static long BIOCallback(BIO* bio, | |
| 189 int cmd, | |
| 190 const char* argp, | |
| 191 int argi, | |
| 192 long argl, | |
| 193 long retvalue); | |
| 194 | |
| 195 // Called after the initial handshake completes and after the server | 172 // Called after the initial handshake completes and after the server |
| 196 // certificate has been verified. The order of handshake completion and | 173 // certificate has been verified. The order of handshake completion and |
| 197 // certificate verification depends on whether the connection was false | 174 // certificate verification depends on whether the connection was false |
| 198 // started. After both have happened (thus calling this twice), the session is | 175 // started. After both have happened (thus calling this twice), the session is |
| 199 // safe to cache and will be cached. | 176 // safe to cache and will be cached. |
| 200 void MaybeCacheSession(); | 177 void MaybeCacheSession(); |
| 201 | 178 |
| 202 // Called from the SSL layer whenever a new session is established. | 179 // Called from the SSL layer whenever a new session is established. |
| 203 int NewSessionCallback(SSL_SESSION* session); | 180 int NewSessionCallback(SSL_SESSION* session); |
| 204 | 181 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 | 228 |
| 252 // Returns whether TLS channel ID is enabled. | 229 // Returns whether TLS channel ID is enabled. |
| 253 bool IsChannelIDEnabled() const; | 230 bool IsChannelIDEnabled() const; |
| 254 | 231 |
| 255 // Returns the net error corresponding to the most recent OpenSSL | 232 // Returns the net error corresponding to the most recent OpenSSL |
| 256 // error. ssl_error is the output of SSL_get_error. | 233 // error. ssl_error is the output of SSL_get_error. |
| 257 int MapLastOpenSSLError(int ssl_error, | 234 int MapLastOpenSSLError(int ssl_error, |
| 258 const crypto::OpenSSLErrStackTracer& tracer, | 235 const crypto::OpenSSLErrStackTracer& tracer, |
| 259 OpenSSLErrorInfo* info); | 236 OpenSSLErrorInfo* info); |
| 260 | 237 |
| 261 bool transport_send_busy_; | |
| 262 bool transport_recv_busy_; | |
| 263 | |
| 264 // Buffers which are shared by BoringSSL and SSLClientSocketImpl. | |
| 265 // GrowableIOBuffer is used to keep ownership and setting offset. | |
| 266 scoped_refptr<GrowableIOBuffer> send_buffer_; | |
| 267 scoped_refptr<GrowableIOBuffer> recv_buffer_; | |
| 268 | |
| 269 CompletionCallback user_connect_callback_; | 238 CompletionCallback user_connect_callback_; |
| 270 CompletionCallback user_read_callback_; | 239 CompletionCallback user_read_callback_; |
| 271 CompletionCallback user_write_callback_; | 240 CompletionCallback user_write_callback_; |
| 272 | 241 |
| 273 // Used by Read function. | 242 // Used by Read function. |
| 274 scoped_refptr<IOBuffer> user_read_buf_; | 243 scoped_refptr<IOBuffer> user_read_buf_; |
| 275 int user_read_buf_len_; | 244 int user_read_buf_len_; |
| 276 | 245 |
| 277 // Used by Write function. | 246 // Used by Write function. |
| 278 scoped_refptr<IOBuffer> user_write_buf_; | 247 scoped_refptr<IOBuffer> user_write_buf_; |
| 279 int user_write_buf_len_; | 248 int user_write_buf_len_; |
| 280 | 249 |
| 281 // Used by DoPayloadRead() when attempting to fill the caller's buffer with | 250 // Used by DoPayloadRead() when attempting to fill the caller's buffer with |
| 282 // as much data as possible without blocking. | 251 // as much data as possible without blocking. |
| 283 // If DoPayloadRead() encounters an error after having read some data, stores | 252 // If DoPayloadRead() encounters an error after having read some data, stores |
| 284 // the result to return on the *next* call to DoPayloadRead(). A value > 0 | 253 // the result to return on the *next* call to DoPayloadRead(). A value > 0 |
| 285 // indicates there is no pending result, otherwise 0 indicates EOF and < 0 | 254 // indicates there is no pending result, otherwise 0 indicates EOF and < 0 |
| 286 // indicates an error. | 255 // indicates an error. |
| 287 int pending_read_error_; | 256 int pending_read_error_; |
| 288 | 257 |
| 289 // If there is a pending read result, the OpenSSL result code (output of | 258 // If there is a pending read result, the OpenSSL result code (output of |
| 290 // SSL_get_error) associated with it. | 259 // SSL_get_error) associated with it. |
| 291 int pending_read_ssl_error_; | 260 int pending_read_ssl_error_; |
| 292 | 261 |
| 293 // If there is a pending read result, the OpenSSLErrorInfo associated with it. | 262 // If there is a pending read result, the OpenSSLErrorInfo associated with it. |
| 294 OpenSSLErrorInfo pending_read_error_info_; | 263 OpenSSLErrorInfo pending_read_error_info_; |
| 295 | 264 |
| 296 // Used by TransportReadComplete() to signify an error reading from the | |
| 297 // transport socket. A value of OK indicates the socket is still | |
| 298 // readable. EOFs are mapped to ERR_CONNECTION_CLOSED. | |
| 299 int transport_read_error_; | |
| 300 | |
| 301 // Used by TransportWriteComplete() and TransportReadComplete() to signify an | |
| 302 // error writing to the transport socket. A value of OK indicates no error. | |
| 303 int transport_write_error_; | |
| 304 | |
| 305 // Set when Connect finishes. | 265 // Set when Connect finishes. |
| 306 std::unique_ptr<PeerCertificateChain> server_cert_chain_; | 266 std::unique_ptr<PeerCertificateChain> server_cert_chain_; |
| 307 scoped_refptr<X509Certificate> server_cert_; | 267 scoped_refptr<X509Certificate> server_cert_; |
| 308 CertVerifyResult server_cert_verify_result_; | 268 CertVerifyResult server_cert_verify_result_; |
| 309 std::string ocsp_response_; | 269 std::string ocsp_response_; |
| 310 bool completed_connect_; | 270 bool completed_connect_; |
| 311 | 271 |
| 312 // Set when Read() or Write() successfully reads or writes data to or from the | 272 // Set when Read() or Write() successfully reads or writes data to or from the |
| 313 // network. | 273 // network. |
| 314 bool was_ever_used_; | 274 bool was_ever_used_; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 329 CTVerifier* cert_transparency_verifier_; | 289 CTVerifier* cert_transparency_verifier_; |
| 330 | 290 |
| 331 // The service for retrieving Channel ID keys. May be NULL. | 291 // The service for retrieving Channel ID keys. May be NULL. |
| 332 ChannelIDService* channel_id_service_; | 292 ChannelIDService* channel_id_service_; |
| 333 bool tb_was_negotiated_; | 293 bool tb_was_negotiated_; |
| 334 TokenBindingParam tb_negotiated_param_; | 294 TokenBindingParam tb_negotiated_param_; |
| 335 TokenBindingSignatureMap tb_signature_map_; | 295 TokenBindingSignatureMap tb_signature_map_; |
| 336 | 296 |
| 337 // OpenSSL stuff | 297 // OpenSSL stuff |
| 338 bssl::UniquePtr<SSL> ssl_; | 298 bssl::UniquePtr<SSL> ssl_; |
| 339 bssl::UniquePtr<BIO> transport_bio_; | |
| 340 | 299 |
| 341 std::unique_ptr<ClientSocketHandle> transport_; | 300 std::unique_ptr<ClientSocketHandle> transport_; |
| 301 std::unique_ptr<SocketBIOAdapter> transport_adapter_; |
| 342 const HostPortPair host_and_port_; | 302 const HostPortPair host_and_port_; |
| 343 SSLConfig ssl_config_; | 303 SSLConfig ssl_config_; |
| 344 // ssl_session_cache_shard_ is an opaque string that partitions the SSL | 304 // ssl_session_cache_shard_ is an opaque string that partitions the SSL |
| 345 // session cache. i.e. sessions created with one value will not attempt to | 305 // session cache. i.e. sessions created with one value will not attempt to |
| 346 // resume on the socket with a different value. | 306 // resume on the socket with a different value. |
| 347 const std::string ssl_session_cache_shard_; | 307 const std::string ssl_session_cache_shard_; |
| 348 | 308 |
| 349 enum State { | 309 enum State { |
| 350 STATE_NONE, | 310 STATE_NONE, |
| 351 STATE_HANDSHAKE, | 311 STATE_HANDSHAKE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 // True if PKP is bypassed due to a local trust anchor. | 350 // True if PKP is bypassed due to a local trust anchor. |
| 391 bool pkp_bypassed_; | 351 bool pkp_bypassed_; |
| 392 | 352 |
| 393 NetLogWithSource net_log_; | 353 NetLogWithSource net_log_; |
| 394 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_; | 354 base::WeakPtrFactory<SSLClientSocketImpl> weak_factory_; |
| 395 }; | 355 }; |
| 396 | 356 |
| 397 } // namespace net | 357 } // namespace net |
| 398 | 358 |
| 399 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ | 359 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_IMPL_H_ |
| OLD | NEW |