[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | // |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 5 | // This StreamSocket implementation is to be used with servers that |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 6 | // accept connections on port 443 but don't really use SSL. For |
| 7 | // example, the Google Talk servers do this to bypass proxies. (The |
| 8 | // connection is upgraded to TLS as part of the XMPP negotiation, so |
| 9 | // security is preserved.) A "fake" SSL handshake is done immediately |
| 10 | // after connection to fool proxies into thinking that this is a real |
| 11 | // SSL connection. |
| 12 | // |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 13 | // NOTE: This StreamSocket implementation does *not* do a real SSL |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 14 | // handshake nor does it do any encryption! |
| 15 | |
[email protected] | 47a5122 | 2012-07-24 01:54:41 | [diff] [blame] | 16 | #ifndef JINGLE_GLUE_FAKE_SSL_CLIENT_SOCKET_H_ |
| 17 | #define JINGLE_GLUE_FAKE_SSL_CLIENT_SOCKET_H_ |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 18 | |
| 19 | #include <cstddef> |
| 20 | |
| 21 | #include "base/basictypes.h" |
[email protected] | 3f19773 | 2011-11-17 20:08:56 | [diff] [blame] | 22 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 23 | #include "base/memory/ref_counted.h" |
| 24 | #include "base/memory/scoped_ptr.h" |
[email protected] | daf079a | 2013-04-17 21:42:40 | [diff] [blame] | 25 | #include "base/strings/string_piece.h" |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 26 | #include "net/base/completion_callback.h" |
| 27 | #include "net/base/net_errors.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 28 | #include "net/socket/stream_socket.h" |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 29 | |
| 30 | namespace net { |
| 31 | class DrainableIOBuffer; |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 32 | class SSLInfo; |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 33 | } // namespace net |
| 34 | |
[email protected] | 47a5122 | 2012-07-24 01:54:41 | [diff] [blame] | 35 | namespace jingle_glue { |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 36 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 37 | class FakeSSLClientSocket : public net::StreamSocket { |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 38 | public: |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 39 | explicit FakeSSLClientSocket(scoped_ptr<net::StreamSocket> transport_socket); |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 40 | |
dcheng | 5364bdee | 2014-10-22 23:38:31 | [diff] [blame] | 41 | ~FakeSSLClientSocket() override; |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 42 | |
| 43 | // Exposed for testing. |
| 44 | static base::StringPiece GetSslClientHello(); |
| 45 | static base::StringPiece GetSslServerHello(); |
| 46 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 47 | // net::StreamSocket implementation. |
dcheng | 5364bdee | 2014-10-22 23:38:31 | [diff] [blame] | 48 | int Read(net::IOBuffer* buf, |
| 49 | int buf_len, |
| 50 | const net::CompletionCallback& callback) override; |
| 51 | int Write(net::IOBuffer* buf, |
| 52 | int buf_len, |
| 53 | const net::CompletionCallback& callback) override; |
| 54 | int SetReceiveBufferSize(int32 size) override; |
| 55 | int SetSendBufferSize(int32 size) override; |
| 56 | int Connect(const net::CompletionCallback& callback) override; |
| 57 | void Disconnect() override; |
| 58 | bool IsConnected() const override; |
| 59 | bool IsConnectedAndIdle() const override; |
| 60 | int GetPeerAddress(net::IPEndPoint* address) const override; |
| 61 | int GetLocalAddress(net::IPEndPoint* address) const override; |
| 62 | const net::BoundNetLog& NetLog() const override; |
| 63 | void SetSubresourceSpeculation() override; |
| 64 | void SetOmniboxSpeculation() override; |
| 65 | bool WasEverUsed() const override; |
| 66 | bool UsingTCPFastOpen() const override; |
| 67 | bool WasNpnNegotiated() const override; |
| 68 | net::NextProto GetNegotiatedProtocol() const override; |
| 69 | bool GetSSLInfo(net::SSLInfo* ssl_info) override; |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 70 | |
| 71 | private: |
| 72 | enum HandshakeState { |
| 73 | STATE_NONE, |
| 74 | STATE_CONNECT, |
| 75 | STATE_SEND_CLIENT_HELLO, |
| 76 | STATE_VERIFY_SERVER_HELLO, |
| 77 | }; |
| 78 | |
| 79 | int DoHandshakeLoop(); |
| 80 | void RunUserConnectCallback(int status); |
| 81 | void DoHandshakeLoopWithUserConnectCallback(); |
| 82 | |
| 83 | int DoConnect(); |
| 84 | void OnConnectDone(int status); |
| 85 | void ProcessConnectDone(); |
| 86 | |
| 87 | int DoSendClientHello(); |
| 88 | void OnSendClientHelloDone(int status); |
| 89 | void ProcessSendClientHelloDone(size_t written); |
| 90 | |
| 91 | int DoVerifyServerHello(); |
| 92 | void OnVerifyServerHelloDone(int status); |
| 93 | net::Error ProcessVerifyServerHelloDone(size_t read); |
| 94 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 95 | scoped_ptr<net::StreamSocket> transport_socket_; |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 96 | |
| 97 | // During the handshake process, holds a value from HandshakeState. |
| 98 | // STATE_NONE otherwise. |
| 99 | HandshakeState next_handshake_state_; |
| 100 | |
| 101 | // True iff we're connected and we've finished the handshake. |
| 102 | bool handshake_completed_; |
| 103 | |
| 104 | // The callback passed to Connect(). |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 105 | net::CompletionCallback user_connect_callback_; |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 106 | |
| 107 | scoped_refptr<net::DrainableIOBuffer> write_buf_; |
| 108 | scoped_refptr<net::DrainableIOBuffer> read_buf_; |
| 109 | }; |
| 110 | |
[email protected] | 47a5122 | 2012-07-24 01:54:41 | [diff] [blame] | 111 | } // namespace jingle_glue |
[email protected] | 8a91a0a | 2010-09-01 02:12:45 | [diff] [blame] | 112 | |
[email protected] | 47a5122 | 2012-07-24 01:54:41 | [diff] [blame] | 113 | #endif // JINGLE_GLUE_FAKE_SSL_CLIENT_SOCKET_H_ |